xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 45721)
123399Smckusick /*
237736Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337736Smckusick  * All rights reserved.
423399Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*45721Smckusick  *	@(#)lfs_inode.c	7.36 (Berkeley) 12/05/90
823399Smckusick  */
924Sbill 
1017099Sbloom #include "param.h"
1117099Sbloom #include "systm.h"
1217099Sbloom #include "mount.h"
1317099Sbloom #include "user.h"
1439795Smckusick #include "proc.h"
1537736Smckusick #include "file.h"
1617099Sbloom #include "buf.h"
1724525Sbloom #include "cmap.h"
1837736Smckusick #include "vnode.h"
1941313Smckusick #include "../ufs/quota.h"
2037736Smckusick #include "../ufs/inode.h"
2137736Smckusick #include "../ufs/fs.h"
2237736Smckusick #include "../ufs/ufsmount.h"
2317099Sbloom #include "kernel.h"
2431661Smckusick #include "malloc.h"
2524Sbill 
2616840Smckusick #define	INOHSZ	512
277334Skre #if	((INOHSZ&(INOHSZ-1)) == 0)
287334Skre #define	INOHASH(dev,ino)	(((dev)+(ino))&(INOHSZ-1))
297334Skre #else
3010852Ssam #define	INOHASH(dev,ino)	(((unsigned)((dev)+(ino)))%INOHSZ)
317334Skre #endif
3224Sbill 
3339392Smckusick union ihead {
347334Skre 	union  ihead *ih_head[2];
357334Skre 	struct inode *ih_chain[2];
367334Skre } ihead[INOHSZ];
377334Skre 
3839574Smckusick int prtactive;	/* 1 => print out reclaim of active vnodes */
3939574Smckusick 
4024Sbill /*
4139392Smckusick  * Initialize hash links for inodes.
4224Sbill  */
4339440Smckusick ufs_init()
4424Sbill {
4524Sbill 	register int i;
4639392Smckusick 	register union ihead *ih = ihead;
4724Sbill 
4839492Smckusick #ifndef lint
4939392Smckusick 	if (VN_MAXPRIVATE < sizeof(struct inode))
5039392Smckusick 		panic("ihinit: too small");
5139492Smckusick #endif /* not lint */
527334Skre 	for (i = INOHSZ; --i >= 0; ih++) {
537334Skre 		ih->ih_head[0] = ih;
547334Skre 		ih->ih_head[1] = ih;
557334Skre 	}
5641313Smckusick #ifdef QUOTA
5741313Smckusick 	dqinit();
5841313Smckusick #endif /* QUOTA */
5924Sbill }
6024Sbill 
6124Sbill /*
6237736Smckusick  * Look up an vnode/inode by device,inumber.
6324Sbill  * If it is in core (in the inode structure),
6424Sbill  * honor the locking protocol.
6524Sbill  * If it is not in core, read it in from the
6624Sbill  * specified device.
6737736Smckusick  * Callers must check for mount points!!
6824Sbill  * In all cases, a pointer to a locked
6924Sbill  * inode structure is returned.
7024Sbill  */
7137736Smckusick iget(xp, ino, ipp)
7237736Smckusick 	struct inode *xp;
734818Swnj 	ino_t ino;
7437736Smckusick 	struct inode **ipp;
7524Sbill {
7637736Smckusick 	dev_t dev = xp->i_dev;
7737736Smckusick 	struct mount *mntp = ITOV(xp)->v_mount;
7837736Smckusick 	register struct fs *fs = VFSTOUFS(mntp)->um_fs;
7939440Smckusick 	extern struct vnodeops ufs_vnodeops, spec_inodeops;
8037736Smckusick 	register struct inode *ip, *iq;
8137736Smckusick 	register struct vnode *vp;
8239440Smckusick 	struct vnode *nvp;
8337736Smckusick 	struct buf *bp;
8439440Smckusick 	struct dinode *dp;
8541313Smckusick 	union ihead *ih;
8641313Smckusick 	int i, error;
8724Sbill 
8839440Smckusick 	ih = &ihead[INOHASH(dev, ino)];
8924Sbill loop:
9039392Smckusick 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) {
9139392Smckusick 		if (ino != ip->i_number || dev != ip->i_dev)
9239392Smckusick 			continue;
9339392Smckusick 		if ((ip->i_flag&ILOCKED) != 0) {
9439392Smckusick 			ip->i_flag |= IWANT;
9539392Smckusick 			sleep((caddr_t)ip, PINOD);
9639392Smckusick 			goto loop;
9739392Smckusick 		}
9839440Smckusick 		if (vget(ITOV(ip)))
9939440Smckusick 			goto loop;
10039392Smckusick 		*ipp = ip;
10139392Smckusick 		return(0);
10239392Smckusick 	}
10339440Smckusick 	/*
10439440Smckusick 	 * Allocate a new inode.
10539440Smckusick 	 */
10639440Smckusick 	if (error = getnewvnode(VT_UFS, mntp, &ufs_vnodeops, &nvp)) {
10737736Smckusick 		*ipp = 0;
10837736Smckusick 		return (error);
10937736Smckusick 	}
11039440Smckusick 	ip = VTOI(nvp);
11139440Smckusick 	ip->i_vnode = nvp;
11239440Smckusick 	ip->i_flag = 0;
11339440Smckusick 	ip->i_devvp = 0;
11439440Smckusick 	ip->i_mode = 0;
11539879Smckusick 	ip->i_diroff = 0;
11639440Smckusick #ifdef QUOTA
11741313Smckusick 	for (i = 0; i < MAXQUOTAS; i++)
11841313Smckusick 		ip->i_dquot[i] = NODQUOT;
11939440Smckusick #endif
12037736Smckusick 	/*
12139440Smckusick 	 * Put it onto its hash chain and lock it so that other requests for
12239440Smckusick 	 * this inode will block if they arrive while we are sleeping waiting
12339440Smckusick 	 * for old data structures to be purged or for the contents of the
12439440Smckusick 	 * disk portion of this inode to be read.
12539440Smckusick 	 */
12639440Smckusick 	ip->i_dev = dev;
12739440Smckusick 	ip->i_number = ino;
12839440Smckusick 	insque(ip, ih);
12939440Smckusick 	ILOCK(ip);
13039440Smckusick 	/*
13137736Smckusick 	 * Read in the disk contents for the inode.
13237736Smckusick 	 */
13337736Smckusick 	if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)),
13438776Smckusick 	    (int)fs->fs_bsize, NOCRED, &bp)) {
13537736Smckusick 		/*
13641334Smckusick 		 * The inode does not contain anything useful, so it would
13741334Smckusick 		 * be misleading to leave it on its hash chain.
13841334Smckusick 		 * Iput() will take care of putting it back on the free list.
13941334Smckusick 		 */
14041334Smckusick 		remque(ip);
14141334Smckusick 		ip->i_forw = ip;
14241334Smckusick 		ip->i_back = ip;
14341334Smckusick 		/*
14439392Smckusick 		 * Unlock and discard unneeded inode.
14537736Smckusick 		 */
14639440Smckusick 		iput(ip);
14737736Smckusick 		brelse(bp);
14837736Smckusick 		*ipp = 0;
14939440Smckusick 		return (error);
15037736Smckusick 	}
15139440Smckusick 	dp = bp->b_un.b_dino;
15239440Smckusick 	dp += itoo(fs, ino);
15339440Smckusick 	ip->i_din = *dp;
15439440Smckusick 	brelse(bp);
15537736Smckusick 	/*
15639440Smckusick 	 * Initialize the associated vnode
15737736Smckusick 	 */
15839440Smckusick 	vp = ITOV(ip);
15939440Smckusick 	vp->v_type = IFTOVT(ip->i_mode);
16040289Smckusick 	if (vp->v_type == VFIFO) {
16140289Smckusick #ifdef FIFO
16240289Smckusick 		extern struct vnodeops fifo_inodeops;
16340289Smckusick 		vp->v_op = &fifo_inodeops;
16440289Smckusick #else
16540289Smckusick 		iput(ip);
16640289Smckusick 		*ipp = 0;
16740289Smckusick 		return (EOPNOTSUPP);
16840289Smckusick #endif /* FIFO */
16940289Smckusick 	}
17039440Smckusick 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
17139440Smckusick 		vp->v_op = &spec_inodeops;
17239617Smckusick 		if (nvp = checkalias(vp, ip->i_rdev, mntp)) {
17337736Smckusick 			/*
17439440Smckusick 			 * Reinitialize aliased inode.
17537736Smckusick 			 */
17639440Smckusick 			vp = nvp;
17739440Smckusick 			iq = VTOI(vp);
17839440Smckusick 			iq->i_vnode = vp;
17939517Smckusick 			iq->i_flag = 0;
18039440Smckusick 			ILOCK(iq);
18139440Smckusick 			iq->i_din = ip->i_din;
18239440Smckusick 			iq->i_dev = dev;
18339440Smckusick 			iq->i_number = ino;
18439440Smckusick 			insque(iq, ih);
18537736Smckusick 			/*
18639440Smckusick 			 * Discard unneeded vnode
18737736Smckusick 			 */
18839440Smckusick 			ip->i_mode = 0;
18939440Smckusick 			iput(ip);
19037736Smckusick 			ip = iq;
19137736Smckusick 		}
19237736Smckusick 	}
19339440Smckusick 	if (ino == ROOTINO)
19439440Smckusick 		vp->v_flag |= VROOT;
19537736Smckusick 	/*
19637736Smckusick 	 * Finish inode initialization.
19737736Smckusick 	 */
19837736Smckusick 	ip->i_fs = fs;
19937736Smckusick 	ip->i_devvp = VFSTOUFS(mntp)->um_devvp;
20038345Smckusick 	VREF(ip->i_devvp);
20138256Smckusick 	/*
20238256Smckusick 	 * Set up a generation number for this inode if it does not
20338256Smckusick 	 * already have one. This should only happen on old filesystems.
20438256Smckusick 	 */
20538256Smckusick 	if (ip->i_gen == 0) {
20638256Smckusick 		if (++nextgennumber < (u_long)time.tv_sec)
20738256Smckusick 			nextgennumber = time.tv_sec;
20838256Smckusick 		ip->i_gen = nextgennumber;
20941397Smckusick 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
21038256Smckusick 			ip->i_flag |= IMOD;
21138256Smckusick 	}
21237736Smckusick 	*ipp = ip;
21337736Smckusick 	return (0);
21437736Smckusick }
2157334Skre 
21637736Smckusick /*
21739392Smckusick  * Unlock and decrement the reference count of an inode structure.
21824Sbill  */
21924Sbill iput(ip)
2204818Swnj 	register struct inode *ip;
22124Sbill {
2227118Smckusick 
2238452Sroot 	if ((ip->i_flag & ILOCKED) == 0)
2247118Smckusick 		panic("iput");
22516665Smckusick 	IUNLOCK(ip);
22637736Smckusick 	vrele(ITOV(ip));
2277118Smckusick }
2287118Smckusick 
22939392Smckusick /*
23039392Smckusick  * Last reference to an inode, write the inode out and if necessary,
23139392Smckusick  * truncate and deallocate the file.
23239392Smckusick  */
23337736Smckusick ufs_inactive(vp)
23437736Smckusick 	struct vnode *vp;
2357118Smckusick {
23637736Smckusick 	register struct inode *ip = VTOI(vp);
23739392Smckusick 	int mode, error = 0;
23824Sbill 
23939816Smckusick 	if (prtactive && vp->v_usecount != 0)
24039676Smckusick 		vprint("ufs_inactive: pushing active", vp);
24138452Smckusick 	/*
24238452Smckusick 	 * Get rid of inodes related to stale file handles.
24338452Smckusick 	 */
24439440Smckusick 	if (ip->i_mode == 0) {
24539676Smckusick 		if ((vp->v_flag & VXLOCK) == 0)
24639676Smckusick 			vgone(vp);
24739440Smckusick 		return (0);
24839440Smckusick 	}
24938226Smckusick 	ILOCK(ip);
25041397Smckusick 	if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
25141313Smckusick #ifdef QUOTA
25241313Smckusick 		if (!getinoquota(ip))
25341313Smckusick 			(void) chkiq(ip, -1, NOCRED, 0);
25441313Smckusick #endif
25539676Smckusick 		error = itrunc(ip, (u_long)0, 0);
25637736Smckusick 		mode = ip->i_mode;
25737736Smckusick 		ip->i_mode = 0;
25844893Strent 		ip->i_rdev = 0;
25937736Smckusick 		ip->i_flag |= IUPD|ICHG;
26037736Smckusick 		ifree(ip, ip->i_number, mode);
26137736Smckusick 	}
26237736Smckusick 	IUPDAT(ip, &time, &time, 0);
26340033Smckusick 	IUNLOCK(ip);
26440033Smckusick 	ip->i_flag = 0;
26537736Smckusick 	/*
26639392Smckusick 	 * If we are done with the inode, reclaim it
26739392Smckusick 	 * so that it can be reused immediately.
26837736Smckusick 	 */
26941313Smckusick 	if (vp->v_usecount == 0 && ip->i_mode == 0)
27040033Smckusick 		vgone(vp);
27137736Smckusick 	return (error);
27224Sbill }
27324Sbill 
27424Sbill /*
27539392Smckusick  * Reclaim an inode so that it can be used for other purposes.
27624Sbill  */
27739392Smckusick ufs_reclaim(vp)
27839392Smckusick 	register struct vnode *vp;
27939392Smckusick {
28039492Smckusick 	register struct inode *ip = VTOI(vp);
28141313Smckusick 	int i;
28239392Smckusick 
28339816Smckusick 	if (prtactive && vp->v_usecount != 0)
28439676Smckusick 		vprint("ufs_reclaim: pushing active", vp);
28539392Smckusick 	/*
28639392Smckusick 	 * Remove the inode from its hash chain.
28739392Smckusick 	 */
28839392Smckusick 	remque(ip);
28939392Smckusick 	ip->i_forw = ip;
29039392Smckusick 	ip->i_back = ip;
29139392Smckusick 	/*
29239392Smckusick 	 * Purge old data structures associated with the inode.
29339392Smckusick 	 */
29439392Smckusick 	cache_purge(vp);
29539392Smckusick 	if (ip->i_devvp) {
29639392Smckusick 		vrele(ip->i_devvp);
29739392Smckusick 		ip->i_devvp = 0;
29839392Smckusick 	}
29939392Smckusick #ifdef QUOTA
30041313Smckusick 	for (i = 0; i < MAXQUOTAS; i++) {
30141313Smckusick 		if (ip->i_dquot[i] != NODQUOT) {
30241313Smckusick 			dqrele(vp, ip->i_dquot[i]);
30341313Smckusick 			ip->i_dquot[i] = NODQUOT;
30441313Smckusick 		}
30541313Smckusick 	}
30639392Smckusick #endif
30739392Smckusick 	ip->i_flag = 0;
30839392Smckusick 	return (0);
30939392Smckusick }
31039392Smckusick 
31139392Smckusick /*
31239392Smckusick  * Check accessed and update flags on an inode structure.
31339392Smckusick  * If any is on, update the inode with the current time.
31439392Smckusick  * If waitfor is given, then must ensure I/O order,
31539392Smckusick  * so wait for write to complete.
31639392Smckusick  */
3171203Sbill iupdat(ip, ta, tm, waitfor)
3184818Swnj 	register struct inode *ip;
3198630Sroot 	struct timeval *ta, *tm;
3204818Swnj 	int waitfor;
32124Sbill {
32237736Smckusick 	struct buf *bp;
32337736Smckusick 	struct vnode *vp = ITOV(ip);
32424Sbill 	struct dinode *dp;
32530749Skarels 	register struct fs *fs;
32637736Smckusick 	int error;
32724Sbill 
32830749Skarels 	fs = ip->i_fs;
32937736Smckusick 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
33037736Smckusick 		return (0);
33141397Smckusick 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
33237736Smckusick 		return (0);
33337736Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
33438776Smckusick 		(int)fs->fs_bsize, NOCRED, &bp);
33537736Smckusick 	if (error) {
33637736Smckusick 		brelse(bp);
33737736Smckusick 		return (error);
33824Sbill 	}
33937736Smckusick 	if (ip->i_flag&IACC)
34037736Smckusick 		ip->i_atime = ta->tv_sec;
34137736Smckusick 	if (ip->i_flag&IUPD)
34237736Smckusick 		ip->i_mtime = tm->tv_sec;
34337736Smckusick 	if (ip->i_flag&ICHG)
34437736Smckusick 		ip->i_ctime = time.tv_sec;
34537736Smckusick 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
34637736Smckusick 	dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
34739392Smckusick 	*dp = ip->i_din;
34837736Smckusick 	if (waitfor) {
34937736Smckusick 		return (bwrite(bp));
35037736Smckusick 	} else {
35137736Smckusick 		bdwrite(bp);
35237736Smckusick 		return (0);
35337736Smckusick 	}
35424Sbill }
35524Sbill 
35610736Ssam #define	SINGLE	0	/* index of single indirect block */
35710736Ssam #define	DOUBLE	1	/* index of double indirect block */
35810736Ssam #define	TRIPLE	2	/* index of triple indirect block */
35924Sbill /*
36039392Smckusick  * Truncate the inode ip to at most length size.  Free affected disk
36139392Smckusick  * blocks -- the blocks of the file are removed in reverse order.
36210736Ssam  *
36310736Ssam  * NB: triple indirect blocks are untested.
36424Sbill  */
36539676Smckusick itrunc(oip, length, flags)
36617942Smckusick 	register struct inode *oip;
3679165Ssam 	u_long length;
36839676Smckusick 	int flags;
36924Sbill {
3709165Ssam 	register daddr_t lastblock;
37126272Skarels 	daddr_t bn, lbn, lastiblock[NIADDR];
3726569Smckusic 	register struct fs *fs;
37310736Ssam 	register struct inode *ip;
37417942Smckusick 	struct buf *bp;
37537736Smckusick 	int offset, osize, size, level;
37637736Smckusick 	long count, nblocks, blocksreleased = 0;
37717942Smckusick 	register int i;
37839676Smckusick 	int aflags, error, allerror;
37910736Ssam 	struct inode tip;
3809165Ssam 
381*45721Smckusick 	vnode_pager_setsize(ITOV(oip), length);
38213000Ssam 	if (oip->i_size <= length) {
38313000Ssam 		oip->i_flag |= ICHG|IUPD;
38437736Smckusick 		error = iupdat(oip, &time, &time, 1);
38537736Smckusick 		return (error);
38613000Ssam 	}
3871203Sbill 	/*
38810736Ssam 	 * Calculate index into inode's block list of
38910736Ssam 	 * last direct and indirect blocks (if any)
39010736Ssam 	 * which we want to keep.  Lastblock is -1 when
39110736Ssam 	 * the file is truncated to 0.
3921203Sbill 	 */
39310736Ssam 	fs = oip->i_fs;
3949165Ssam 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
39510736Ssam 	lastiblock[SINGLE] = lastblock - NDADDR;
39610736Ssam 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
39710736Ssam 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
39812645Ssam 	nblocks = btodb(fs->fs_bsize);
3996569Smckusic 	/*
40017942Smckusick 	 * Update the size of the file. If the file is not being
40117942Smckusick 	 * truncated to a block boundry, the contents of the
40217942Smckusick 	 * partial block following the end of the file must be
40317942Smckusick 	 * zero'ed in case it ever become accessable again because
40417942Smckusick 	 * of subsequent file growth.
40517942Smckusick 	 */
40617942Smckusick 	osize = oip->i_size;
40717942Smckusick 	offset = blkoff(fs, length);
40817942Smckusick 	if (offset == 0) {
40917942Smckusick 		oip->i_size = length;
41017942Smckusick 	} else {
41117942Smckusick 		lbn = lblkno(fs, length);
41239676Smckusick 		aflags = B_CLRBUF;
41339676Smckusick 		if (flags & IO_SYNC)
41439676Smckusick 			aflags |= B_SYNC;
41541313Smckusick #ifdef QUOTA
41641313Smckusick 		if (error = getinoquota(oip))
41741313Smckusick 			return (error);
41841313Smckusick #endif
41939676Smckusick 		if (error = balloc(oip, lbn, offset, &bp, aflags))
42037736Smckusick 			return (error);
42117942Smckusick 		oip->i_size = length;
42217942Smckusick 		size = blksize(fs, oip, lbn);
423*45721Smckusick 		(void) vnode_pager_uncache(ITOV(oip));
42426272Skarels 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
42545112Smckusick 		allocbuf(bp, size);
42639676Smckusick 		if (flags & IO_SYNC)
42739676Smckusick 			bwrite(bp);
42839676Smckusick 		else
42939676Smckusick 			bdwrite(bp);
43017942Smckusick 	}
43117942Smckusick 	/*
43217942Smckusick 	 * Update file and block pointers
43310736Ssam 	 * on disk before we start freeing blocks.
43410736Ssam 	 * If we crash before free'ing blocks below,
43510736Ssam 	 * the blocks will be returned to the free list.
43610736Ssam 	 * lastiblock values are also normalized to -1
43710736Ssam 	 * for calls to indirtrunc below.
4386569Smckusic 	 */
43910736Ssam 	tip = *oip;
44017942Smckusick 	tip.i_size = osize;
44110736Ssam 	for (level = TRIPLE; level >= SINGLE; level--)
44210736Ssam 		if (lastiblock[level] < 0) {
44310736Ssam 			oip->i_ib[level] = 0;
44410736Ssam 			lastiblock[level] = -1;
4459165Ssam 		}
44610736Ssam 	for (i = NDADDR - 1; i > lastblock; i--)
44710736Ssam 		oip->i_db[i] = 0;
44810736Ssam 	oip->i_flag |= ICHG|IUPD;
44939676Smckusick 	vinvalbuf(ITOV(oip), (length > 0));
45039740Smckusick 	allerror = iupdat(oip, &time, &time, MNT_WAIT);
45110736Ssam 
4526569Smckusic 	/*
45310736Ssam 	 * Indirect blocks first.
4546569Smckusic 	 */
45517942Smckusick 	ip = &tip;
45610736Ssam 	for (level = TRIPLE; level >= SINGLE; level--) {
45710736Ssam 		bn = ip->i_ib[level];
4589165Ssam 		if (bn != 0) {
45937736Smckusick 			error = indirtrunc(ip, bn, lastiblock[level], level,
46037736Smckusick 				&count);
46137736Smckusick 			if (error)
46237736Smckusick 				allerror = error;
46337736Smckusick 			blocksreleased += count;
46410736Ssam 			if (lastiblock[level] < 0) {
46510736Ssam 				ip->i_ib[level] = 0;
46631402Smckusick 				blkfree(ip, bn, (off_t)fs->fs_bsize);
46710736Ssam 				blocksreleased += nblocks;
46810736Ssam 			}
46910736Ssam 		}
47010736Ssam 		if (lastiblock[level] >= 0)
47110736Ssam 			goto done;
4729165Ssam 	}
47310736Ssam 
4746569Smckusic 	/*
47510736Ssam 	 * All whole direct blocks or frags.
4766569Smckusic 	 */
4779165Ssam 	for (i = NDADDR - 1; i > lastblock; i--) {
47826359Skarels 		register off_t bsize;
4799165Ssam 
4806569Smckusic 		bn = ip->i_db[i];
4819165Ssam 		if (bn == 0)
48224Sbill 			continue;
4839165Ssam 		ip->i_db[i] = 0;
48424525Sbloom 		bsize = (off_t)blksize(fs, ip, i);
48531402Smckusick 		blkfree(ip, bn, bsize);
48624525Sbloom 		blocksreleased += btodb(bsize);
48724Sbill 	}
48810736Ssam 	if (lastblock < 0)
48910736Ssam 		goto done;
49010736Ssam 
4911203Sbill 	/*
4929165Ssam 	 * Finally, look for a change in size of the
4939165Ssam 	 * last direct block; release any frags.
4941203Sbill 	 */
49510736Ssam 	bn = ip->i_db[lastblock];
49610736Ssam 	if (bn != 0) {
49726359Skarels 		off_t oldspace, newspace;
49810736Ssam 
4999165Ssam 		/*
5009165Ssam 		 * Calculate amount of space we're giving
5019165Ssam 		 * back as old block size minus new block size.
5029165Ssam 		 */
50310736Ssam 		oldspace = blksize(fs, ip, lastblock);
5049165Ssam 		ip->i_size = length;
50510736Ssam 		newspace = blksize(fs, ip, lastblock);
50610736Ssam 		if (newspace == 0)
50710736Ssam 			panic("itrunc: newspace");
50810736Ssam 		if (oldspace - newspace > 0) {
5099165Ssam 			/*
5109165Ssam 			 * Block number of space to be free'd is
5119165Ssam 			 * the old block # plus the number of frags
5129165Ssam 			 * required for the storage we're keeping.
5139165Ssam 			 */
51410736Ssam 			bn += numfrags(fs, newspace);
51531402Smckusick 			blkfree(ip, bn, oldspace - newspace);
51612645Ssam 			blocksreleased += btodb(oldspace - newspace);
5179165Ssam 		}
5189165Ssam 	}
5199165Ssam done:
52010736Ssam /* BEGIN PARANOIA */
52110736Ssam 	for (level = SINGLE; level <= TRIPLE; level++)
52210736Ssam 		if (ip->i_ib[level] != oip->i_ib[level])
52310736Ssam 			panic("itrunc1");
52410736Ssam 	for (i = 0; i < NDADDR; i++)
52510736Ssam 		if (ip->i_db[i] != oip->i_db[i])
52610736Ssam 			panic("itrunc2");
52710736Ssam /* END PARANOIA */
52812645Ssam 	oip->i_blocks -= blocksreleased;
52912645Ssam 	if (oip->i_blocks < 0)			/* sanity */
53012645Ssam 		oip->i_blocks = 0;
53112645Ssam 	oip->i_flag |= ICHG;
5329165Ssam #ifdef QUOTA
53341313Smckusick 	if (!getinoquota(oip))
53441313Smckusick 		(void) chkdq(oip, -blocksreleased, NOCRED, 0);
5359165Ssam #endif
53637736Smckusick 	return (allerror);
53724Sbill }
53824Sbill 
5399165Ssam /*
5409165Ssam  * Release blocks associated with the inode ip and
5419165Ssam  * stored in the indirect block bn.  Blocks are free'd
5429165Ssam  * in LIFO order up to (but not including) lastbn.  If
54310736Ssam  * level is greater than SINGLE, the block is an indirect
54410736Ssam  * block and recursive calls to indirtrunc must be used to
54510736Ssam  * cleanse other indirect blocks.
54610736Ssam  *
54710736Ssam  * NB: triple indirect blocks are untested.
5489165Ssam  */
54937736Smckusick indirtrunc(ip, bn, lastbn, level, countp)
5506569Smckusic 	register struct inode *ip;
5519165Ssam 	daddr_t bn, lastbn;
55210736Ssam 	int level;
55337736Smckusick 	long *countp;
55424Sbill {
5559165Ssam 	register int i;
55631661Smckusick 	struct buf *bp;
55731661Smckusick 	register struct fs *fs = ip->i_fs;
55824Sbill 	register daddr_t *bap;
55931661Smckusick 	daddr_t *copy, nb, last;
56037736Smckusick 	long blkcount, factor;
56137736Smckusick 	int nblocks, blocksreleased = 0;
56237736Smckusick 	int error, allerror = 0;
56324Sbill 
56410736Ssam 	/*
56510736Ssam 	 * Calculate index in current block of last
56610736Ssam 	 * block to be kept.  -1 indicates the entire
56710736Ssam 	 * block so we need not calculate the index.
56810736Ssam 	 */
56910736Ssam 	factor = 1;
57010736Ssam 	for (i = SINGLE; i < level; i++)
57110736Ssam 		factor *= NINDIR(fs);
5729165Ssam 	last = lastbn;
57310736Ssam 	if (lastbn > 0)
57410736Ssam 		last /= factor;
57512645Ssam 	nblocks = btodb(fs->fs_bsize);
57610736Ssam 	/*
57710736Ssam 	 * Get buffer of block pointers, zero those
57810736Ssam 	 * entries corresponding to blocks to be free'd,
57910736Ssam 	 * and update on disk copy first.
58010736Ssam 	 */
58138776Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
58238776Smckusick 		NOCRED, &bp);
58337736Smckusick 	if (error) {
58410736Ssam 		brelse(bp);
58537736Smckusick 		*countp = 0;
58637736Smckusick 		return (error);
58710736Ssam 	}
58810736Ssam 	bap = bp->b_un.b_daddr;
58931661Smckusick 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
59031661Smckusick 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
59110736Ssam 	bzero((caddr_t)&bap[last + 1],
59210736Ssam 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
59339676Smckusick 	if (last == -1)
59439676Smckusick 		bp->b_flags |= B_INVAL;
59537736Smckusick 	error = bwrite(bp);
59637736Smckusick 	if (error)
59737736Smckusick 		allerror = error;
59831661Smckusick 	bap = copy;
59910736Ssam 
60010736Ssam 	/*
60110736Ssam 	 * Recursively free totally unused blocks.
60210736Ssam 	 */
6039165Ssam 	for (i = NINDIR(fs) - 1; i > last; i--) {
60424Sbill 		nb = bap[i];
6059165Ssam 		if (nb == 0)
60624Sbill 			continue;
60737736Smckusick 		if (level > SINGLE) {
60837736Smckusick 			error = indirtrunc(ip, nb, (daddr_t)-1, level - 1,
60937736Smckusick 				&blkcount);
61037736Smckusick 			if (error)
61137736Smckusick 				allerror = error;
61237736Smckusick 			blocksreleased += blkcount;
61337736Smckusick 		}
61431402Smckusick 		blkfree(ip, nb, (off_t)fs->fs_bsize);
6159165Ssam 		blocksreleased += nblocks;
61624Sbill 	}
61710736Ssam 
61810736Ssam 	/*
61910736Ssam 	 * Recursively free last partial block.
62010736Ssam 	 */
62110736Ssam 	if (level > SINGLE && lastbn >= 0) {
62210736Ssam 		last = lastbn % factor;
6239165Ssam 		nb = bap[i];
62437736Smckusick 		if (nb != 0) {
62537736Smckusick 			error = indirtrunc(ip, nb, last, level - 1, &blkcount);
62637736Smckusick 			if (error)
62737736Smckusick 				allerror = error;
62837736Smckusick 			blocksreleased += blkcount;
62937736Smckusick 		}
6309165Ssam 	}
63131661Smckusick 	FREE(copy, M_TEMP);
63237736Smckusick 	*countp = blocksreleased;
63337736Smckusick 	return (allerror);
63424Sbill }
63524Sbill 
63624Sbill /*
6374818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
6383617Sroot  */
6394818Swnj ilock(ip)
6404818Swnj 	register struct inode *ip;
6413617Sroot {
6423617Sroot 
64337736Smckusick 	while (ip->i_flag & ILOCKED) {
64437736Smckusick 		ip->i_flag |= IWANT;
64539795Smckusick 		if (ip->i_spare0 == u.u_procp->p_pid)
64639795Smckusick 			panic("locking against myself");
64739795Smckusick 		ip->i_spare1 = u.u_procp->p_pid;
64837736Smckusick 		(void) sleep((caddr_t)ip, PINOD);
64937736Smckusick 	}
65039795Smckusick 	ip->i_spare1 = 0;
65139795Smckusick 	ip->i_spare0 = u.u_procp->p_pid;
65239795Smckusick 	u.u_spare[0]++;
65337736Smckusick 	ip->i_flag |= ILOCKED;
6543617Sroot }
6553617Sroot 
6563617Sroot /*
6574818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
6583617Sroot  */
6597118Smckusick iunlock(ip)
6604818Swnj 	register struct inode *ip;
6613617Sroot {
6623617Sroot 
66337736Smckusick 	if ((ip->i_flag & ILOCKED) == 0)
66439676Smckusick 		vprint("iunlock: unlocked inode", ITOV(ip));
66539795Smckusick 	ip->i_spare0 = 0;
66639795Smckusick 	u.u_spare[0]--;
66737736Smckusick 	ip->i_flag &= ~ILOCKED;
66837736Smckusick 	if (ip->i_flag&IWANT) {
66937736Smckusick 		ip->i_flag &= ~IWANT;
67037736Smckusick 		wakeup((caddr_t)ip);
67137736Smckusick 	}
6723617Sroot }
673