* Copyright (c) 1982, 1986, 1989 Regents of the University of California.
* %sccs.include.redist.c%
* @(#)lfs_inode.c 7.43 (Berkeley) %G%
#include "../ufs/quota.h"
#include "../ufs/inode.h"
#include "../ufs/ufsmount.h"
#include "../vm/vm_param.h"
#if ((INOHSZ&(INOHSZ-1)) == 0)
#define INOHASH(dev,ino) (((dev)+(ino))&(INOHSZ-1))
#define INOHASH(dev,ino) (((unsigned)((dev)+(ino)))%INOHSZ)
union lfsihead
{ /* LFS */
union lfsihead
*ih_head
[2];
struct inode
*ih_chain
[2];
extern int prtactive
; /* 1 => print out reclaim of active vnodes */
lock_data_t lfs_sync_lock
;
* Initialize hash links for inodes.
register union lfsihead
*ih
= lfsihead
;
if (VN_MAXPRIVATE
< sizeof(struct inode
))
panic("ihinit: too small");
lock_init(&lfs_sync_lock
, 1);
for (i
= INOHSZ
; --i
>= 0; ih
++) {
ih
= &lfsihead
[INOHASH(ip
->i_dev
, ip
->i_number
)];
* Look up a UFS dinode number to find its incore vnode.
* If it is not in core, read it in from the specified device.
* If it is in core, wait for the lock bit to clear, then
* return the inode locked. Detection and handling of mount
* points must be done by the calling routine.
struct mount
*mntp
= ITOV(xp
)->v_mount
;
register LFS
*fs
= VFSTOUFS(mntp
)->um_lfs
; /* LFS */
extern struct vnodeops ufs_vnodeops
, spec_inodeops
;
register struct inode
*ip
, *iq
;
register struct vnode
*vp
;
printf("lfs_iget ino %d\n", ino
);
ih
= &lfsihead
[INOHASH(dev
, ino
)];
for (ip
= ih
->ih_chain
[0]; ip
!= (struct inode
*)ih
; ip
= ip
->i_forw
) {
if (ino
!= ip
->i_number
|| dev
!= ip
->i_dev
)
if ((ip
->i_flag
&ILOCKED
) != 0) {
sleep((caddr_t
)ip
, PINOD
);
/* Allocate new vnode/inode. */
error
= lfs_vcreate(mntp
, ino
, &nvp
);
* Put it onto its hash chain and lock it so that other requests for
* this inode will block if they arrive while we are sleeping waiting
* for old data structures to be purged or for the contents of the
* disk portion of this inode to be read.
/* Read in the disk contents for the inode, copy into the vnode. */
if (error
= bread(VFSTOUFS(mntp
)->um_devvp
, itod(fs
, ino
),
(int)fs
->lfs_bsize
, NOCRED
, &bp
)) { /* LFS */
* The inode does not contain anything useful, so it would
* be misleading to leave it on its hash chain.
* Iput() will take care of putting it back on the free list.
* Unlock and discard unneeded inode.
ip
->i_din
= *lfs_ifind(fs
, ino
, bp
->b_un
.b_dino
);
* Initialize the associated vnode
vp
->v_type
= IFTOVT(ip
->i_mode
);
if (vp
->v_type
== VFIFO
) {
extern struct vnodeops fifo_inodeops
;
vp
->v_op
= &fifo_inodeops
;
if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
vp
->v_op
= &spec_inodeops
;
if (nvp
= checkalias(vp
, ip
->i_rdev
, mntp
)) {
* Reinitialize aliased inode.
* Last reference to an inode, write the inode out and if necessary,
* truncate and deallocate the file.
register struct inode
*ip
= VTOI(vp
);
printf("lfs_inactive: ino %d mode %d nlink %d\n",
ip
->i_number
, ip
->i_mode
, ip
->i_nlink
);
if (prtactive
&& vp
->v_usecount
!= 0)
vprint("ufs_inactive: pushing active", vp
);
* Get rid of inodes related to stale file handles.
if ((vp
->v_flag
& VXLOCK
) == 0)
if (ip
->i_nlink
<= 0 && (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) == 0) {
(void) chkiq(ip
, -1, NOCRED
, 0);
error
= lfs_itrunc(ip
, (u_long
)0, 0); /* LFS */
ifree(ip
, ip
->i_number
, mode
);
ITIMES(ip
, &time
, &time
);
* If we are done with the inode, reclaim it
* so that it can be reused immediately.
if (vp
->v_usecount
== 0 && ip
->i_mode
== 0)
#define SINGLE 0 /* index of single indirect block */
#define DOUBLE 1 /* index of double indirect block */
#define TRIPLE 2 /* index of triple indirect block */
* Truncate the inode ip to at most length size. Free affected disk
* blocks -- the blocks of the file are removed in reverse order.
* NB: triple indirect blocks are untested.
lfs_itrunc(oip
, length
, flags
)
register struct inode
*oip
;
register daddr_t lastblock
;
daddr_t bn
, lbn
, lastiblock
[NIADDR
];
register LFS
*fs
; /* LFS */
register struct inode
*ip
;
int offset
, osize
, size
, level
;
long count
, nblocks
, blocksreleased
= 0;
int aflags
, error
, allerror
;
vnode_pager_setsize(ITOV(oip
), length
);
if (oip
->i_size
<= length
) {
oip
->i_flag
|= ICHG
|IUPD
;
ITIMES(oip
, &time
, &time
);
* Calculate index into inode's block list of
* last direct and indirect blocks (if any)
* which we want to keep. Lastblock is -1 when
* the file is truncated to 0.
fs
= oip
->i_lfs
; /* LFS */
lastblock
= lblkno(fs
, length
+ fs
->lfs_bsize
- 1) - 1; /* LFS */
lastiblock
[SINGLE
] = lastblock
- NDADDR
;
lastiblock
[DOUBLE
] = lastiblock
[SINGLE
] - NINDIR(fs
);
lastiblock
[TRIPLE
] = lastiblock
[DOUBLE
] - NINDIR(fs
) * NINDIR(fs
);
nblocks
= btodb(fs
->lfs_bsize
); /* LFS */
* Update the size of the file. If the file is not being
* truncated to a block boundry, the contents of the
* partial block following the end of the file must be
* zero'ed in case it ever become accessable again because
* of subsequent file growth.
offset
= blkoff(fs
, length
);
lbn
= lblkno(fs
, length
);
if (error
= getinoquota(oip
))
if (error
= bread(ITOV(oip
), lbn
, fs
->lfs_bsize
, NOCRED
, &bp
))
size
= blksize(fs
); /* LFS */
(void) vnode_pager_uncache(ITOV(oip
));
bzero(bp
->b_un
.b_addr
+ offset
, (unsigned)(size
- offset
));
if (flags
& IO_SYNC
) /* LFS */
* Update file and block pointers
* on disk before we start freeing blocks.
* If we crash before free'ing blocks below,
* the blocks will be returned to the free list.
* lastiblock values are also normalized to -1
* for calls to indirtrunc below.
/* Will need to modify the segment usage information */ /* LFS */
for (level
= TRIPLE
; level
>= SINGLE
; level
--)
if (lastiblock
[level
] < 0) {
for (i
= NDADDR
- 1; i
> lastblock
; i
--)
oip
->i_flag
|= ICHG
|IUPD
;
vinvalbuf(ITOV(oip
), (length
> 0));
allerror
= ITIMES(oip
, &time
, &time
);
/* Need lfs_vinvalbuf to get rid of invalid buffers in the cache */
ITIMES(oip
, &time
, &time
);
for (level
= TRIPLE
; level
>= SINGLE
; level
--) {
error
= indirtrunc(ip
, bn
, lastiblock
[level
], level
,
if (lastiblock
[level
] < 0) {
blkfree(ip
, bn
, (off_t
)fs
->fs_bsize
);
blocksreleased
+= nblocks
;
if (lastiblock
[level
] >= 0)
/* LFS -- not yet implemented. Need to rewrite indirect blocks */
panic("lfs_itrunc: not yet implemented");
* All whole direct blocks or frags.
for (i
= NDADDR
- 1; i
> lastblock
; i
--) {
bsize
= (off_t
)blksize(fs
); /* LFS */
/* LFS Update segment usage information */
blocksreleased
+= btodb(bsize
);
* Finally, look for a change in size of the
* last direct block; release any frags.
bn
= ip
->i_db
[lastblock
];
off_t oldspace
, newspace
;
* Calculate amount of space we're giving
* back as old block size minus new block size.
oldspace
= blksize(fs
); /* LFS */
newspace
= blksize(fs
); /* LFS */
panic("lfs_itrunc: newspace");
if (oldspace
- newspace
> 0) {
* Block number of space to be free'd is
* the old block # plus the number of frags
* required for the storage we're keeping.
bn
+= numfrags(fs
, newspace
);
blkfree(ip
, bn
, oldspace
- newspace
);
blocksreleased
+= btodb(oldspace
- newspace
);
for (level
= SINGLE
; level
<= TRIPLE
; level
++)
if (ip
->i_ib
[level
] != oip
->i_ib
[level
])
for (i
= 0; i
< NDADDR
; i
++)
if (ip
->i_db
[i
] != oip
->i_db
[i
])
oip
->i_blocks
-= blocksreleased
;
if (oip
->i_blocks
< 0) /* sanity */
(void) chkdq(oip
, -blocksreleased
, NOCRED
, 0);
* Release blocks associated with the inode ip and
* stored in the indirect block bn. Blocks are free'd
* in LIFO order up to (but not including) lastbn. If
* level is greater than SINGLE, the block is an indirect
* block and recursive calls to indirtrunc must be used to
* cleanse other indirect blocks.
* NB: triple indirect blocks are untested.
lfs_indirtrunc(ip
, bn
, lastbn
, level
, countp
)
register struct inode
*ip
;
register struct fs
*fs
= ip
->i_fs
;
int nblocks
, blocksreleased
= 0;
* Calculate index in current block of last
* block to be kept. -1 indicates the entire
* block so we need not calculate the index.
for (i
= SINGLE
; i
< level
; i
++)
nblocks
= btodb(fs
->fs_bsize
);
* Get buffer of block pointers, zero those
* entries corresponding to blocks to be free'd,
* and update on disk copy first.
bp
= bread(ip
->i_dev
, fsbtodb(fs
, bn
), (int)fs
->fs_bsize
,
error
= bread(ip
->i_devvp
, fsbtodb(fs
, bn
), (int)fs
->fs_bsize
,
MALLOC(copy
, daddr_t
*, fs
->fs_bsize
, M_TEMP
, M_WAITOK
);
bcopy((caddr_t
)bap
, (caddr_t
)copy
, (u_int
)fs
->fs_bsize
);
bzero((caddr_t
)&bap
[last
+ 1],
(u_int
)(NINDIR(fs
) - (last
+ 1)) * sizeof (daddr_t
));
* Recursively free totally unused blocks.
for (i
= NINDIR(fs
) - 1; i
> last
; i
--) {
error
= indirtrunc(ip
, nb
, (daddr_t
)-1, level
- 1,
blocksreleased
+= blkcount
;
blkfree(ip
, nb
, (off_t
)fs
->fs_bsize
);
blocksreleased
+= nblocks
;
* Recursively free last partial block.
if (level
> SINGLE
&& lastbn
>= 0) {
error
= indirtrunc(ip
, nb
, last
, level
- 1, &blkcount
);
blocksreleased
+= blkcount
;
*countp
= blocksreleased
;
/* LFS IMPLEMENT -- lfs_indirtrunc */
panic("lfs_indirtrunc not implemented");