xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 48037)
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*48037Smckusick  *	@(#)lfs_inode.c	7.39 (Berkeley) 04/16/91
823399Smckusick  */
924Sbill 
1017099Sbloom #include "param.h"
1117099Sbloom #include "systm.h"
1217099Sbloom #include "mount.h"
1339795Smckusick #include "proc.h"
1437736Smckusick #include "file.h"
1517099Sbloom #include "buf.h"
1637736Smckusick #include "vnode.h"
1717099Sbloom #include "kernel.h"
1831661Smckusick #include "malloc.h"
1924Sbill 
2047571Skarels #include "quota.h"
2147571Skarels #include "inode.h"
2247571Skarels #include "fs.h"
2347571Skarels #include "ufsmount.h"
2447571Skarels 
2516840Smckusick #define	INOHSZ	512
267334Skre #if	((INOHSZ&(INOHSZ-1)) == 0)
277334Skre #define	INOHASH(dev,ino)	(((dev)+(ino))&(INOHSZ-1))
287334Skre #else
2910852Ssam #define	INOHASH(dev,ino)	(((unsigned)((dev)+(ino)))%INOHSZ)
307334Skre #endif
3124Sbill 
3239392Smckusick union ihead {
337334Skre 	union  ihead *ih_head[2];
347334Skre 	struct inode *ih_chain[2];
357334Skre } ihead[INOHSZ];
367334Skre 
3739574Smckusick int prtactive;	/* 1 => print out reclaim of active vnodes */
3839574Smckusick 
3924Sbill /*
4039392Smckusick  * Initialize hash links for inodes.
4124Sbill  */
4239440Smckusick ufs_init()
4324Sbill {
4424Sbill 	register int i;
4539392Smckusick 	register union ihead *ih = ihead;
4624Sbill 
4739492Smckusick #ifndef lint
4839392Smckusick 	if (VN_MAXPRIVATE < sizeof(struct inode))
4939392Smckusick 		panic("ihinit: too small");
5039492Smckusick #endif /* not lint */
517334Skre 	for (i = INOHSZ; --i >= 0; ih++) {
527334Skre 		ih->ih_head[0] = ih;
537334Skre 		ih->ih_head[1] = ih;
547334Skre 	}
5541313Smckusick #ifdef QUOTA
5641313Smckusick 	dqinit();
5741313Smckusick #endif /* QUOTA */
5824Sbill }
5924Sbill 
6024Sbill /*
6137736Smckusick  * Look up an vnode/inode by device,inumber.
6224Sbill  * If it is in core (in the inode structure),
6324Sbill  * honor the locking protocol.
6424Sbill  * If it is not in core, read it in from the
6524Sbill  * specified device.
6637736Smckusick  * Callers must check for mount points!!
6724Sbill  * In all cases, a pointer to a locked
6824Sbill  * inode structure is returned.
6924Sbill  */
7037736Smckusick iget(xp, ino, ipp)
7137736Smckusick 	struct inode *xp;
724818Swnj 	ino_t ino;
7337736Smckusick 	struct inode **ipp;
7424Sbill {
7537736Smckusick 	dev_t dev = xp->i_dev;
7637736Smckusick 	struct mount *mntp = ITOV(xp)->v_mount;
7737736Smckusick 	register struct fs *fs = VFSTOUFS(mntp)->um_fs;
7839440Smckusick 	extern struct vnodeops ufs_vnodeops, spec_inodeops;
7937736Smckusick 	register struct inode *ip, *iq;
8037736Smckusick 	register struct vnode *vp;
8139440Smckusick 	struct vnode *nvp;
8237736Smckusick 	struct buf *bp;
8339440Smckusick 	struct dinode *dp;
8441313Smckusick 	union ihead *ih;
8541313Smckusick 	int i, error;
8624Sbill 
8739440Smckusick 	ih = &ihead[INOHASH(dev, ino)];
8824Sbill loop:
8939392Smckusick 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) {
9039392Smckusick 		if (ino != ip->i_number || dev != ip->i_dev)
9139392Smckusick 			continue;
9239392Smckusick 		if ((ip->i_flag&ILOCKED) != 0) {
9339392Smckusick 			ip->i_flag |= IWANT;
9439392Smckusick 			sleep((caddr_t)ip, PINOD);
9539392Smckusick 			goto loop;
9639392Smckusick 		}
9739440Smckusick 		if (vget(ITOV(ip)))
9839440Smckusick 			goto loop;
9939392Smckusick 		*ipp = ip;
10039392Smckusick 		return(0);
10139392Smckusick 	}
10239440Smckusick 	/*
10339440Smckusick 	 * Allocate a new inode.
10439440Smckusick 	 */
10539440Smckusick 	if (error = getnewvnode(VT_UFS, mntp, &ufs_vnodeops, &nvp)) {
10637736Smckusick 		*ipp = 0;
10737736Smckusick 		return (error);
10837736Smckusick 	}
10939440Smckusick 	ip = VTOI(nvp);
11039440Smckusick 	ip->i_vnode = nvp;
11139440Smckusick 	ip->i_flag = 0;
11239440Smckusick 	ip->i_devvp = 0;
11339440Smckusick 	ip->i_mode = 0;
11439879Smckusick 	ip->i_diroff = 0;
11546205Smckusick 	ip->i_lockf = 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  */
233*48037Smckusick ufs_inactive(vp, p)
23437736Smckusick 	struct vnode *vp;
235*48037Smckusick 	struct proc *p;
2367118Smckusick {
23737736Smckusick 	register struct inode *ip = VTOI(vp);
23839392Smckusick 	int mode, error = 0;
23924Sbill 
24039816Smckusick 	if (prtactive && vp->v_usecount != 0)
24139676Smckusick 		vprint("ufs_inactive: pushing active", vp);
24238452Smckusick 	/*
24338452Smckusick 	 * Get rid of inodes related to stale file handles.
24438452Smckusick 	 */
24539440Smckusick 	if (ip->i_mode == 0) {
24639676Smckusick 		if ((vp->v_flag & VXLOCK) == 0)
24739676Smckusick 			vgone(vp);
24839440Smckusick 		return (0);
24939440Smckusick 	}
25038226Smckusick 	ILOCK(ip);
25141397Smckusick 	if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
25241313Smckusick #ifdef QUOTA
25341313Smckusick 		if (!getinoquota(ip))
25441313Smckusick 			(void) chkiq(ip, -1, NOCRED, 0);
25541313Smckusick #endif
25639676Smckusick 		error = itrunc(ip, (u_long)0, 0);
25737736Smckusick 		mode = ip->i_mode;
25837736Smckusick 		ip->i_mode = 0;
25944893Strent 		ip->i_rdev = 0;
26037736Smckusick 		ip->i_flag |= IUPD|ICHG;
26137736Smckusick 		ifree(ip, ip->i_number, mode);
26237736Smckusick 	}
26337736Smckusick 	IUPDAT(ip, &time, &time, 0);
26440033Smckusick 	IUNLOCK(ip);
26540033Smckusick 	ip->i_flag = 0;
26637736Smckusick 	/*
26739392Smckusick 	 * If we are done with the inode, reclaim it
26839392Smckusick 	 * so that it can be reused immediately.
26937736Smckusick 	 */
27041313Smckusick 	if (vp->v_usecount == 0 && ip->i_mode == 0)
27140033Smckusick 		vgone(vp);
27237736Smckusick 	return (error);
27324Sbill }
27424Sbill 
27524Sbill /*
27639392Smckusick  * Reclaim an inode so that it can be used for other purposes.
27724Sbill  */
27839392Smckusick ufs_reclaim(vp)
27939392Smckusick 	register struct vnode *vp;
28039392Smckusick {
28139492Smckusick 	register struct inode *ip = VTOI(vp);
28241313Smckusick 	int i;
28339392Smckusick 
28439816Smckusick 	if (prtactive && vp->v_usecount != 0)
28539676Smckusick 		vprint("ufs_reclaim: pushing active", vp);
28639392Smckusick 	/*
28739392Smckusick 	 * Remove the inode from its hash chain.
28839392Smckusick 	 */
28939392Smckusick 	remque(ip);
29039392Smckusick 	ip->i_forw = ip;
29139392Smckusick 	ip->i_back = ip;
29239392Smckusick 	/*
29339392Smckusick 	 * Purge old data structures associated with the inode.
29439392Smckusick 	 */
29539392Smckusick 	cache_purge(vp);
29639392Smckusick 	if (ip->i_devvp) {
29739392Smckusick 		vrele(ip->i_devvp);
29839392Smckusick 		ip->i_devvp = 0;
29939392Smckusick 	}
30039392Smckusick #ifdef QUOTA
30141313Smckusick 	for (i = 0; i < MAXQUOTAS; i++) {
30241313Smckusick 		if (ip->i_dquot[i] != NODQUOT) {
30341313Smckusick 			dqrele(vp, ip->i_dquot[i]);
30441313Smckusick 			ip->i_dquot[i] = NODQUOT;
30541313Smckusick 		}
30641313Smckusick 	}
30739392Smckusick #endif
30839392Smckusick 	ip->i_flag = 0;
30939392Smckusick 	return (0);
31039392Smckusick }
31139392Smckusick 
31239392Smckusick /*
31339392Smckusick  * Check accessed and update flags on an inode structure.
31439392Smckusick  * If any is on, update the inode with the current time.
31539392Smckusick  * If waitfor is given, then must ensure I/O order,
31639392Smckusick  * so wait for write to complete.
31739392Smckusick  */
3181203Sbill iupdat(ip, ta, tm, waitfor)
3194818Swnj 	register struct inode *ip;
3208630Sroot 	struct timeval *ta, *tm;
3214818Swnj 	int waitfor;
32224Sbill {
32337736Smckusick 	struct buf *bp;
32437736Smckusick 	struct vnode *vp = ITOV(ip);
32524Sbill 	struct dinode *dp;
32630749Skarels 	register struct fs *fs;
32737736Smckusick 	int error;
32824Sbill 
32930749Skarels 	fs = ip->i_fs;
33037736Smckusick 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
33137736Smckusick 		return (0);
33241397Smckusick 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
33337736Smckusick 		return (0);
33437736Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
33538776Smckusick 		(int)fs->fs_bsize, NOCRED, &bp);
33637736Smckusick 	if (error) {
33737736Smckusick 		brelse(bp);
33837736Smckusick 		return (error);
33924Sbill 	}
34037736Smckusick 	if (ip->i_flag&IACC)
34137736Smckusick 		ip->i_atime = ta->tv_sec;
34237736Smckusick 	if (ip->i_flag&IUPD)
34337736Smckusick 		ip->i_mtime = tm->tv_sec;
34437736Smckusick 	if (ip->i_flag&ICHG)
34537736Smckusick 		ip->i_ctime = time.tv_sec;
34637736Smckusick 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
34737736Smckusick 	dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
34839392Smckusick 	*dp = ip->i_din;
34937736Smckusick 	if (waitfor) {
35037736Smckusick 		return (bwrite(bp));
35137736Smckusick 	} else {
35237736Smckusick 		bdwrite(bp);
35337736Smckusick 		return (0);
35437736Smckusick 	}
35524Sbill }
35624Sbill 
35710736Ssam #define	SINGLE	0	/* index of single indirect block */
35810736Ssam #define	DOUBLE	1	/* index of double indirect block */
35910736Ssam #define	TRIPLE	2	/* index of triple indirect block */
36024Sbill /*
36139392Smckusick  * Truncate the inode ip to at most length size.  Free affected disk
36239392Smckusick  * blocks -- the blocks of the file are removed in reverse order.
36310736Ssam  *
36410736Ssam  * NB: triple indirect blocks are untested.
36524Sbill  */
36639676Smckusick itrunc(oip, length, flags)
36717942Smckusick 	register struct inode *oip;
3689165Ssam 	u_long length;
36939676Smckusick 	int flags;
37024Sbill {
3719165Ssam 	register daddr_t lastblock;
37226272Skarels 	daddr_t bn, lbn, lastiblock[NIADDR];
3736569Smckusic 	register struct fs *fs;
37410736Ssam 	register struct inode *ip;
37517942Smckusick 	struct buf *bp;
37637736Smckusick 	int offset, osize, size, level;
37737736Smckusick 	long count, nblocks, blocksreleased = 0;
37817942Smckusick 	register int i;
37939676Smckusick 	int aflags, error, allerror;
38010736Ssam 	struct inode tip;
3819165Ssam 
38245721Smckusick 	vnode_pager_setsize(ITOV(oip), length);
38313000Ssam 	if (oip->i_size <= length) {
38413000Ssam 		oip->i_flag |= ICHG|IUPD;
38537736Smckusick 		error = iupdat(oip, &time, &time, 1);
38637736Smckusick 		return (error);
38713000Ssam 	}
3881203Sbill 	/*
38910736Ssam 	 * Calculate index into inode's block list of
39010736Ssam 	 * last direct and indirect blocks (if any)
39110736Ssam 	 * which we want to keep.  Lastblock is -1 when
39210736Ssam 	 * the file is truncated to 0.
3931203Sbill 	 */
39410736Ssam 	fs = oip->i_fs;
3959165Ssam 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
39610736Ssam 	lastiblock[SINGLE] = lastblock - NDADDR;
39710736Ssam 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
39810736Ssam 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
39912645Ssam 	nblocks = btodb(fs->fs_bsize);
4006569Smckusic 	/*
40117942Smckusick 	 * Update the size of the file. If the file is not being
40217942Smckusick 	 * truncated to a block boundry, the contents of the
40317942Smckusick 	 * partial block following the end of the file must be
40417942Smckusick 	 * zero'ed in case it ever become accessable again because
40517942Smckusick 	 * of subsequent file growth.
40617942Smckusick 	 */
40717942Smckusick 	osize = oip->i_size;
40817942Smckusick 	offset = blkoff(fs, length);
40917942Smckusick 	if (offset == 0) {
41017942Smckusick 		oip->i_size = length;
41117942Smckusick 	} else {
41217942Smckusick 		lbn = lblkno(fs, length);
41339676Smckusick 		aflags = B_CLRBUF;
41439676Smckusick 		if (flags & IO_SYNC)
41539676Smckusick 			aflags |= B_SYNC;
41641313Smckusick #ifdef QUOTA
41741313Smckusick 		if (error = getinoquota(oip))
41841313Smckusick 			return (error);
41941313Smckusick #endif
42039676Smckusick 		if (error = balloc(oip, lbn, offset, &bp, aflags))
42137736Smckusick 			return (error);
42217942Smckusick 		oip->i_size = length;
42317942Smckusick 		size = blksize(fs, oip, lbn);
42445721Smckusick 		(void) vnode_pager_uncache(ITOV(oip));
42526272Skarels 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
42645112Smckusick 		allocbuf(bp, size);
42739676Smckusick 		if (flags & IO_SYNC)
42839676Smckusick 			bwrite(bp);
42939676Smckusick 		else
43039676Smckusick 			bdwrite(bp);
43117942Smckusick 	}
43217942Smckusick 	/*
43317942Smckusick 	 * Update file and block pointers
43410736Ssam 	 * on disk before we start freeing blocks.
43510736Ssam 	 * If we crash before free'ing blocks below,
43610736Ssam 	 * the blocks will be returned to the free list.
43710736Ssam 	 * lastiblock values are also normalized to -1
43810736Ssam 	 * for calls to indirtrunc below.
4396569Smckusic 	 */
44010736Ssam 	tip = *oip;
44117942Smckusick 	tip.i_size = osize;
44210736Ssam 	for (level = TRIPLE; level >= SINGLE; level--)
44310736Ssam 		if (lastiblock[level] < 0) {
44410736Ssam 			oip->i_ib[level] = 0;
44510736Ssam 			lastiblock[level] = -1;
4469165Ssam 		}
44710736Ssam 	for (i = NDADDR - 1; i > lastblock; i--)
44810736Ssam 		oip->i_db[i] = 0;
44910736Ssam 	oip->i_flag |= ICHG|IUPD;
45039676Smckusick 	vinvalbuf(ITOV(oip), (length > 0));
45139740Smckusick 	allerror = iupdat(oip, &time, &time, MNT_WAIT);
45210736Ssam 
4536569Smckusic 	/*
45410736Ssam 	 * Indirect blocks first.
4556569Smckusic 	 */
45617942Smckusick 	ip = &tip;
45710736Ssam 	for (level = TRIPLE; level >= SINGLE; level--) {
45810736Ssam 		bn = ip->i_ib[level];
4599165Ssam 		if (bn != 0) {
46037736Smckusick 			error = indirtrunc(ip, bn, lastiblock[level], level,
46137736Smckusick 				&count);
46237736Smckusick 			if (error)
46337736Smckusick 				allerror = error;
46437736Smckusick 			blocksreleased += count;
46510736Ssam 			if (lastiblock[level] < 0) {
46610736Ssam 				ip->i_ib[level] = 0;
46731402Smckusick 				blkfree(ip, bn, (off_t)fs->fs_bsize);
46810736Ssam 				blocksreleased += nblocks;
46910736Ssam 			}
47010736Ssam 		}
47110736Ssam 		if (lastiblock[level] >= 0)
47210736Ssam 			goto done;
4739165Ssam 	}
47410736Ssam 
4756569Smckusic 	/*
47610736Ssam 	 * All whole direct blocks or frags.
4776569Smckusic 	 */
4789165Ssam 	for (i = NDADDR - 1; i > lastblock; i--) {
47926359Skarels 		register off_t bsize;
4809165Ssam 
4816569Smckusic 		bn = ip->i_db[i];
4829165Ssam 		if (bn == 0)
48324Sbill 			continue;
4849165Ssam 		ip->i_db[i] = 0;
48524525Sbloom 		bsize = (off_t)blksize(fs, ip, i);
48631402Smckusick 		blkfree(ip, bn, bsize);
48724525Sbloom 		blocksreleased += btodb(bsize);
48824Sbill 	}
48910736Ssam 	if (lastblock < 0)
49010736Ssam 		goto done;
49110736Ssam 
4921203Sbill 	/*
4939165Ssam 	 * Finally, look for a change in size of the
4949165Ssam 	 * last direct block; release any frags.
4951203Sbill 	 */
49610736Ssam 	bn = ip->i_db[lastblock];
49710736Ssam 	if (bn != 0) {
49826359Skarels 		off_t oldspace, newspace;
49910736Ssam 
5009165Ssam 		/*
5019165Ssam 		 * Calculate amount of space we're giving
5029165Ssam 		 * back as old block size minus new block size.
5039165Ssam 		 */
50410736Ssam 		oldspace = blksize(fs, ip, lastblock);
5059165Ssam 		ip->i_size = length;
50610736Ssam 		newspace = blksize(fs, ip, lastblock);
50710736Ssam 		if (newspace == 0)
50810736Ssam 			panic("itrunc: newspace");
50910736Ssam 		if (oldspace - newspace > 0) {
5109165Ssam 			/*
5119165Ssam 			 * Block number of space to be free'd is
5129165Ssam 			 * the old block # plus the number of frags
5139165Ssam 			 * required for the storage we're keeping.
5149165Ssam 			 */
51510736Ssam 			bn += numfrags(fs, newspace);
51631402Smckusick 			blkfree(ip, bn, oldspace - newspace);
51712645Ssam 			blocksreleased += btodb(oldspace - newspace);
5189165Ssam 		}
5199165Ssam 	}
5209165Ssam done:
52110736Ssam /* BEGIN PARANOIA */
52210736Ssam 	for (level = SINGLE; level <= TRIPLE; level++)
52310736Ssam 		if (ip->i_ib[level] != oip->i_ib[level])
52410736Ssam 			panic("itrunc1");
52510736Ssam 	for (i = 0; i < NDADDR; i++)
52610736Ssam 		if (ip->i_db[i] != oip->i_db[i])
52710736Ssam 			panic("itrunc2");
52810736Ssam /* END PARANOIA */
52912645Ssam 	oip->i_blocks -= blocksreleased;
53012645Ssam 	if (oip->i_blocks < 0)			/* sanity */
53112645Ssam 		oip->i_blocks = 0;
53212645Ssam 	oip->i_flag |= ICHG;
5339165Ssam #ifdef QUOTA
53441313Smckusick 	if (!getinoquota(oip))
53541313Smckusick 		(void) chkdq(oip, -blocksreleased, NOCRED, 0);
5369165Ssam #endif
53737736Smckusick 	return (allerror);
53824Sbill }
53924Sbill 
5409165Ssam /*
5419165Ssam  * Release blocks associated with the inode ip and
5429165Ssam  * stored in the indirect block bn.  Blocks are free'd
5439165Ssam  * in LIFO order up to (but not including) lastbn.  If
54410736Ssam  * level is greater than SINGLE, the block is an indirect
54510736Ssam  * block and recursive calls to indirtrunc must be used to
54610736Ssam  * cleanse other indirect blocks.
54710736Ssam  *
54810736Ssam  * NB: triple indirect blocks are untested.
5499165Ssam  */
55037736Smckusick indirtrunc(ip, bn, lastbn, level, countp)
5516569Smckusic 	register struct inode *ip;
5529165Ssam 	daddr_t bn, lastbn;
55310736Ssam 	int level;
55437736Smckusick 	long *countp;
55524Sbill {
5569165Ssam 	register int i;
55731661Smckusick 	struct buf *bp;
55831661Smckusick 	register struct fs *fs = ip->i_fs;
55924Sbill 	register daddr_t *bap;
56031661Smckusick 	daddr_t *copy, nb, last;
56137736Smckusick 	long blkcount, factor;
56237736Smckusick 	int nblocks, blocksreleased = 0;
56337736Smckusick 	int error, allerror = 0;
56424Sbill 
56510736Ssam 	/*
56610736Ssam 	 * Calculate index in current block of last
56710736Ssam 	 * block to be kept.  -1 indicates the entire
56810736Ssam 	 * block so we need not calculate the index.
56910736Ssam 	 */
57010736Ssam 	factor = 1;
57110736Ssam 	for (i = SINGLE; i < level; i++)
57210736Ssam 		factor *= NINDIR(fs);
5739165Ssam 	last = lastbn;
57410736Ssam 	if (lastbn > 0)
57510736Ssam 		last /= factor;
57612645Ssam 	nblocks = btodb(fs->fs_bsize);
57710736Ssam 	/*
57810736Ssam 	 * Get buffer of block pointers, zero those
57910736Ssam 	 * entries corresponding to blocks to be free'd,
58010736Ssam 	 * and update on disk copy first.
58110736Ssam 	 */
58238776Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
58338776Smckusick 		NOCRED, &bp);
58437736Smckusick 	if (error) {
58510736Ssam 		brelse(bp);
58637736Smckusick 		*countp = 0;
58737736Smckusick 		return (error);
58810736Ssam 	}
58910736Ssam 	bap = bp->b_un.b_daddr;
59031661Smckusick 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
59131661Smckusick 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
59210736Ssam 	bzero((caddr_t)&bap[last + 1],
59310736Ssam 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
59439676Smckusick 	if (last == -1)
59539676Smckusick 		bp->b_flags |= B_INVAL;
59637736Smckusick 	error = bwrite(bp);
59737736Smckusick 	if (error)
59837736Smckusick 		allerror = error;
59931661Smckusick 	bap = copy;
60010736Ssam 
60110736Ssam 	/*
60210736Ssam 	 * Recursively free totally unused blocks.
60310736Ssam 	 */
6049165Ssam 	for (i = NINDIR(fs) - 1; i > last; i--) {
60524Sbill 		nb = bap[i];
6069165Ssam 		if (nb == 0)
60724Sbill 			continue;
60837736Smckusick 		if (level > SINGLE) {
60937736Smckusick 			error = indirtrunc(ip, nb, (daddr_t)-1, level - 1,
61037736Smckusick 				&blkcount);
61137736Smckusick 			if (error)
61237736Smckusick 				allerror = error;
61337736Smckusick 			blocksreleased += blkcount;
61437736Smckusick 		}
61531402Smckusick 		blkfree(ip, nb, (off_t)fs->fs_bsize);
6169165Ssam 		blocksreleased += nblocks;
61724Sbill 	}
61810736Ssam 
61910736Ssam 	/*
62010736Ssam 	 * Recursively free last partial block.
62110736Ssam 	 */
62210736Ssam 	if (level > SINGLE && lastbn >= 0) {
62310736Ssam 		last = lastbn % factor;
6249165Ssam 		nb = bap[i];
62537736Smckusick 		if (nb != 0) {
62637736Smckusick 			error = indirtrunc(ip, nb, last, level - 1, &blkcount);
62737736Smckusick 			if (error)
62837736Smckusick 				allerror = error;
62937736Smckusick 			blocksreleased += blkcount;
63037736Smckusick 		}
6319165Ssam 	}
63231661Smckusick 	FREE(copy, M_TEMP);
63337736Smckusick 	*countp = blocksreleased;
63437736Smckusick 	return (allerror);
63524Sbill }
63624Sbill 
63724Sbill /*
6384818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
6393617Sroot  */
6404818Swnj ilock(ip)
6414818Swnj 	register struct inode *ip;
6423617Sroot {
6433617Sroot 
64437736Smckusick 	while (ip->i_flag & ILOCKED) {
64537736Smckusick 		ip->i_flag |= IWANT;
64647571Skarels 		if (ip->i_spare0 == curproc->p_pid)
64739795Smckusick 			panic("locking against myself");
64847571Skarels 		ip->i_spare1 = curproc->p_pid;
64937736Smckusick 		(void) sleep((caddr_t)ip, PINOD);
65037736Smckusick 	}
65139795Smckusick 	ip->i_spare1 = 0;
65247571Skarels 	ip->i_spare0 = curproc->p_pid;
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;
66637736Smckusick 	ip->i_flag &= ~ILOCKED;
66737736Smckusick 	if (ip->i_flag&IWANT) {
66837736Smckusick 		ip->i_flag &= ~IWANT;
66937736Smckusick 		wakeup((caddr_t)ip);
67037736Smckusick 	}
6713617Sroot }
672