xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 44893)
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*44893Strent  *	@(#)lfs_inode.c	7.34 (Berkeley) 07/03/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;
258*44893Strent 		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 
38113000Ssam 	if (oip->i_size <= length) {
38213000Ssam 		oip->i_flag |= ICHG|IUPD;
38337736Smckusick 		error = iupdat(oip, &time, &time, 1);
38437736Smckusick 		return (error);
38513000Ssam 	}
3861203Sbill 	/*
38710736Ssam 	 * Calculate index into inode's block list of
38810736Ssam 	 * last direct and indirect blocks (if any)
38910736Ssam 	 * which we want to keep.  Lastblock is -1 when
39010736Ssam 	 * the file is truncated to 0.
3911203Sbill 	 */
39210736Ssam 	fs = oip->i_fs;
3939165Ssam 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
39410736Ssam 	lastiblock[SINGLE] = lastblock - NDADDR;
39510736Ssam 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
39610736Ssam 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
39712645Ssam 	nblocks = btodb(fs->fs_bsize);
3986569Smckusic 	/*
39917942Smckusick 	 * Update the size of the file. If the file is not being
40017942Smckusick 	 * truncated to a block boundry, the contents of the
40117942Smckusick 	 * partial block following the end of the file must be
40217942Smckusick 	 * zero'ed in case it ever become accessable again because
40317942Smckusick 	 * of subsequent file growth.
40417942Smckusick 	 */
40517942Smckusick 	osize = oip->i_size;
40617942Smckusick 	offset = blkoff(fs, length);
40717942Smckusick 	if (offset == 0) {
40817942Smckusick 		oip->i_size = length;
40917942Smckusick 	} else {
41017942Smckusick 		lbn = lblkno(fs, length);
41139676Smckusick 		aflags = B_CLRBUF;
41239676Smckusick 		if (flags & IO_SYNC)
41339676Smckusick 			aflags |= B_SYNC;
41441313Smckusick #ifdef QUOTA
41541313Smckusick 		if (error = getinoquota(oip))
41641313Smckusick 			return (error);
41741313Smckusick #endif
41839676Smckusick 		if (error = balloc(oip, lbn, offset, &bp, aflags))
41937736Smckusick 			return (error);
42017942Smckusick 		oip->i_size = length;
42117942Smckusick 		size = blksize(fs, oip, lbn);
42239676Smckusick 		bn = bp->b_blkno;
42330749Skarels 		count = howmany(size, CLBYTES);
42430749Skarels 		for (i = 0; i < count; i++)
42537736Smckusick 			munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
42626272Skarels 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
42739676Smckusick 		brealloc(bp, size);
42839676Smckusick 		if (flags & IO_SYNC)
42939676Smckusick 			bwrite(bp);
43039676Smckusick 		else
43139676Smckusick 			bdwrite(bp);
43217942Smckusick 	}
43317942Smckusick 	/*
43417942Smckusick 	 * Update file and block pointers
43510736Ssam 	 * on disk before we start freeing blocks.
43610736Ssam 	 * If we crash before free'ing blocks below,
43710736Ssam 	 * the blocks will be returned to the free list.
43810736Ssam 	 * lastiblock values are also normalized to -1
43910736Ssam 	 * for calls to indirtrunc below.
4406569Smckusic 	 */
44110736Ssam 	tip = *oip;
44217942Smckusick 	tip.i_size = osize;
44310736Ssam 	for (level = TRIPLE; level >= SINGLE; level--)
44410736Ssam 		if (lastiblock[level] < 0) {
44510736Ssam 			oip->i_ib[level] = 0;
44610736Ssam 			lastiblock[level] = -1;
4479165Ssam 		}
44810736Ssam 	for (i = NDADDR - 1; i > lastblock; i--)
44910736Ssam 		oip->i_db[i] = 0;
45010736Ssam 	oip->i_flag |= ICHG|IUPD;
45139676Smckusick 	vinvalbuf(ITOV(oip), (length > 0));
45239740Smckusick 	allerror = iupdat(oip, &time, &time, MNT_WAIT);
45310736Ssam 
4546569Smckusic 	/*
45510736Ssam 	 * Indirect blocks first.
4566569Smckusic 	 */
45717942Smckusick 	ip = &tip;
45810736Ssam 	for (level = TRIPLE; level >= SINGLE; level--) {
45910736Ssam 		bn = ip->i_ib[level];
4609165Ssam 		if (bn != 0) {
46137736Smckusick 			error = indirtrunc(ip, bn, lastiblock[level], level,
46237736Smckusick 				&count);
46337736Smckusick 			if (error)
46437736Smckusick 				allerror = error;
46537736Smckusick 			blocksreleased += count;
46610736Ssam 			if (lastiblock[level] < 0) {
46710736Ssam 				ip->i_ib[level] = 0;
46831402Smckusick 				blkfree(ip, bn, (off_t)fs->fs_bsize);
46910736Ssam 				blocksreleased += nblocks;
47010736Ssam 			}
47110736Ssam 		}
47210736Ssam 		if (lastiblock[level] >= 0)
47310736Ssam 			goto done;
4749165Ssam 	}
47510736Ssam 
4766569Smckusic 	/*
47710736Ssam 	 * All whole direct blocks or frags.
4786569Smckusic 	 */
4799165Ssam 	for (i = NDADDR - 1; i > lastblock; i--) {
48026359Skarels 		register off_t bsize;
4819165Ssam 
4826569Smckusic 		bn = ip->i_db[i];
4839165Ssam 		if (bn == 0)
48424Sbill 			continue;
4859165Ssam 		ip->i_db[i] = 0;
48624525Sbloom 		bsize = (off_t)blksize(fs, ip, i);
48731402Smckusick 		blkfree(ip, bn, bsize);
48824525Sbloom 		blocksreleased += btodb(bsize);
48924Sbill 	}
49010736Ssam 	if (lastblock < 0)
49110736Ssam 		goto done;
49210736Ssam 
4931203Sbill 	/*
4949165Ssam 	 * Finally, look for a change in size of the
4959165Ssam 	 * last direct block; release any frags.
4961203Sbill 	 */
49710736Ssam 	bn = ip->i_db[lastblock];
49810736Ssam 	if (bn != 0) {
49926359Skarels 		off_t oldspace, newspace;
50010736Ssam 
5019165Ssam 		/*
5029165Ssam 		 * Calculate amount of space we're giving
5039165Ssam 		 * back as old block size minus new block size.
5049165Ssam 		 */
50510736Ssam 		oldspace = blksize(fs, ip, lastblock);
5069165Ssam 		ip->i_size = length;
50710736Ssam 		newspace = blksize(fs, ip, lastblock);
50810736Ssam 		if (newspace == 0)
50910736Ssam 			panic("itrunc: newspace");
51010736Ssam 		if (oldspace - newspace > 0) {
5119165Ssam 			/*
5129165Ssam 			 * Block number of space to be free'd is
5139165Ssam 			 * the old block # plus the number of frags
5149165Ssam 			 * required for the storage we're keeping.
5159165Ssam 			 */
51610736Ssam 			bn += numfrags(fs, newspace);
51731402Smckusick 			blkfree(ip, bn, oldspace - newspace);
51812645Ssam 			blocksreleased += btodb(oldspace - newspace);
5199165Ssam 		}
5209165Ssam 	}
5219165Ssam done:
52210736Ssam /* BEGIN PARANOIA */
52310736Ssam 	for (level = SINGLE; level <= TRIPLE; level++)
52410736Ssam 		if (ip->i_ib[level] != oip->i_ib[level])
52510736Ssam 			panic("itrunc1");
52610736Ssam 	for (i = 0; i < NDADDR; i++)
52710736Ssam 		if (ip->i_db[i] != oip->i_db[i])
52810736Ssam 			panic("itrunc2");
52910736Ssam /* END PARANOIA */
53012645Ssam 	oip->i_blocks -= blocksreleased;
53112645Ssam 	if (oip->i_blocks < 0)			/* sanity */
53212645Ssam 		oip->i_blocks = 0;
53312645Ssam 	oip->i_flag |= ICHG;
5349165Ssam #ifdef QUOTA
53541313Smckusick 	if (!getinoquota(oip))
53641313Smckusick 		(void) chkdq(oip, -blocksreleased, NOCRED, 0);
5379165Ssam #endif
53837736Smckusick 	return (allerror);
53924Sbill }
54024Sbill 
5419165Ssam /*
5429165Ssam  * Release blocks associated with the inode ip and
5439165Ssam  * stored in the indirect block bn.  Blocks are free'd
5449165Ssam  * in LIFO order up to (but not including) lastbn.  If
54510736Ssam  * level is greater than SINGLE, the block is an indirect
54610736Ssam  * block and recursive calls to indirtrunc must be used to
54710736Ssam  * cleanse other indirect blocks.
54810736Ssam  *
54910736Ssam  * NB: triple indirect blocks are untested.
5509165Ssam  */
55137736Smckusick indirtrunc(ip, bn, lastbn, level, countp)
5526569Smckusic 	register struct inode *ip;
5539165Ssam 	daddr_t bn, lastbn;
55410736Ssam 	int level;
55537736Smckusick 	long *countp;
55624Sbill {
5579165Ssam 	register int i;
55831661Smckusick 	struct buf *bp;
55931661Smckusick 	register struct fs *fs = ip->i_fs;
56024Sbill 	register daddr_t *bap;
56131661Smckusick 	daddr_t *copy, nb, last;
56237736Smckusick 	long blkcount, factor;
56337736Smckusick 	int nblocks, blocksreleased = 0;
56437736Smckusick 	int error, allerror = 0;
56524Sbill 
56610736Ssam 	/*
56710736Ssam 	 * Calculate index in current block of last
56810736Ssam 	 * block to be kept.  -1 indicates the entire
56910736Ssam 	 * block so we need not calculate the index.
57010736Ssam 	 */
57110736Ssam 	factor = 1;
57210736Ssam 	for (i = SINGLE; i < level; i++)
57310736Ssam 		factor *= NINDIR(fs);
5749165Ssam 	last = lastbn;
57510736Ssam 	if (lastbn > 0)
57610736Ssam 		last /= factor;
57712645Ssam 	nblocks = btodb(fs->fs_bsize);
57810736Ssam 	/*
57910736Ssam 	 * Get buffer of block pointers, zero those
58010736Ssam 	 * entries corresponding to blocks to be free'd,
58110736Ssam 	 * and update on disk copy first.
58210736Ssam 	 */
58338776Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
58438776Smckusick 		NOCRED, &bp);
58537736Smckusick 	if (error) {
58610736Ssam 		brelse(bp);
58737736Smckusick 		*countp = 0;
58837736Smckusick 		return (error);
58910736Ssam 	}
59010736Ssam 	bap = bp->b_un.b_daddr;
59131661Smckusick 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
59231661Smckusick 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
59310736Ssam 	bzero((caddr_t)&bap[last + 1],
59410736Ssam 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
59539676Smckusick 	if (last == -1)
59639676Smckusick 		bp->b_flags |= B_INVAL;
59737736Smckusick 	error = bwrite(bp);
59837736Smckusick 	if (error)
59937736Smckusick 		allerror = error;
60031661Smckusick 	bap = copy;
60110736Ssam 
60210736Ssam 	/*
60310736Ssam 	 * Recursively free totally unused blocks.
60410736Ssam 	 */
6059165Ssam 	for (i = NINDIR(fs) - 1; i > last; i--) {
60624Sbill 		nb = bap[i];
6079165Ssam 		if (nb == 0)
60824Sbill 			continue;
60937736Smckusick 		if (level > SINGLE) {
61037736Smckusick 			error = indirtrunc(ip, nb, (daddr_t)-1, level - 1,
61137736Smckusick 				&blkcount);
61237736Smckusick 			if (error)
61337736Smckusick 				allerror = error;
61437736Smckusick 			blocksreleased += blkcount;
61537736Smckusick 		}
61631402Smckusick 		blkfree(ip, nb, (off_t)fs->fs_bsize);
6179165Ssam 		blocksreleased += nblocks;
61824Sbill 	}
61910736Ssam 
62010736Ssam 	/*
62110736Ssam 	 * Recursively free last partial block.
62210736Ssam 	 */
62310736Ssam 	if (level > SINGLE && lastbn >= 0) {
62410736Ssam 		last = lastbn % factor;
6259165Ssam 		nb = bap[i];
62637736Smckusick 		if (nb != 0) {
62737736Smckusick 			error = indirtrunc(ip, nb, last, level - 1, &blkcount);
62837736Smckusick 			if (error)
62937736Smckusick 				allerror = error;
63037736Smckusick 			blocksreleased += blkcount;
63137736Smckusick 		}
6329165Ssam 	}
63331661Smckusick 	FREE(copy, M_TEMP);
63437736Smckusick 	*countp = blocksreleased;
63537736Smckusick 	return (allerror);
63624Sbill }
63724Sbill 
63824Sbill /*
6394818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
6403617Sroot  */
6414818Swnj ilock(ip)
6424818Swnj 	register struct inode *ip;
6433617Sroot {
6443617Sroot 
64537736Smckusick 	while (ip->i_flag & ILOCKED) {
64637736Smckusick 		ip->i_flag |= IWANT;
64739795Smckusick 		if (ip->i_spare0 == u.u_procp->p_pid)
64839795Smckusick 			panic("locking against myself");
64939795Smckusick 		ip->i_spare1 = u.u_procp->p_pid;
65037736Smckusick 		(void) sleep((caddr_t)ip, PINOD);
65137736Smckusick 	}
65239795Smckusick 	ip->i_spare1 = 0;
65339795Smckusick 	ip->i_spare0 = u.u_procp->p_pid;
65439795Smckusick 	u.u_spare[0]++;
65537736Smckusick 	ip->i_flag |= ILOCKED;
6563617Sroot }
6573617Sroot 
6583617Sroot /*
6594818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
6603617Sroot  */
6617118Smckusick iunlock(ip)
6624818Swnj 	register struct inode *ip;
6633617Sroot {
6643617Sroot 
66537736Smckusick 	if ((ip->i_flag & ILOCKED) == 0)
66639676Smckusick 		vprint("iunlock: unlocked inode", ITOV(ip));
66739795Smckusick 	ip->i_spare0 = 0;
66839795Smckusick 	u.u_spare[0]--;
66937736Smckusick 	ip->i_flag &= ~ILOCKED;
67037736Smckusick 	if (ip->i_flag&IWANT) {
67137736Smckusick 		ip->i_flag &= ~IWANT;
67237736Smckusick 		wakeup((caddr_t)ip);
67337736Smckusick 	}
6743617Sroot }
675