xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 12645)
1*12645Ssam /*	lfs_inode.c	4.35	83/05/21	*/
224Sbill 
324Sbill #include "../h/param.h"
424Sbill #include "../h/systm.h"
524Sbill #include "../h/mount.h"
624Sbill #include "../h/dir.h"
724Sbill #include "../h/user.h"
824Sbill #include "../h/inode.h"
96569Smckusic #include "../h/fs.h"
1024Sbill #include "../h/conf.h"
1124Sbill #include "../h/buf.h"
127651Ssam #ifdef QUOTA
137504Sroot #include "../h/quota.h"
147504Sroot #endif
158106Sroot #include "../h/kernel.h"
1624Sbill 
1724Sbill #define	INOHSZ	63
187334Skre #if	((INOHSZ&(INOHSZ-1)) == 0)
197334Skre #define	INOHASH(dev,ino)	(((dev)+(ino))&(INOHSZ-1))
207334Skre #else
2110852Ssam #define	INOHASH(dev,ino)	(((unsigned)((dev)+(ino)))%INOHSZ)
227334Skre #endif
2324Sbill 
247334Skre union ihead {				/* inode LRU cache, Chris Maltby */
257334Skre 	union  ihead *ih_head[2];
267334Skre 	struct inode *ih_chain[2];
277334Skre } ihead[INOHSZ];
287334Skre 
297334Skre struct inode *ifreeh, **ifreet;
307334Skre 
3124Sbill /*
3224Sbill  * Initialize hash links for inodes
3324Sbill  * and build inode free list.
3424Sbill  */
3524Sbill ihinit()
3624Sbill {
3724Sbill 	register int i;
382737Swnj 	register struct inode *ip = inode;
397334Skre 	register union  ihead *ih = ihead;
4024Sbill 
417334Skre 	for (i = INOHSZ; --i >= 0; ih++) {
427334Skre 		ih->ih_head[0] = ih;
437334Skre 		ih->ih_head[1] = ih;
447334Skre 	}
457334Skre 	ifreeh = ip;
467334Skre 	ifreet = &ip->i_freef;
477334Skre 	ip->i_freeb = &ifreeh;
487334Skre 	ip->i_forw = ip;
497334Skre 	ip->i_back = ip;
507334Skre 	for (i = ninode; --i > 0; ) {
517334Skre 		++ip;
527334Skre 		ip->i_forw = ip;
537334Skre 		ip->i_back = ip;
547334Skre 		*ifreet = ip;
557334Skre 		ip->i_freeb = ifreet;
567334Skre 		ifreet = &ip->i_freef;
577334Skre 	}
587334Skre 	ip->i_freef = NULL;
5924Sbill }
6024Sbill 
617334Skre #ifdef notdef
6224Sbill /*
637334Skre  * Find an inode if it is incore.
647334Skre  * This is the equivalent, for inodes,
657334Skre  * of ``incore'' in bio.c or ``pfind'' in subr.c.
667334Skre  */
677334Skre struct inode *
687334Skre ifind(dev, ino)
697334Skre 	dev_t dev;
707334Skre 	ino_t ino;
717334Skre {
727334Skre 	register struct inode *ip;
737334Skre 	register union  ihead *ih;
747334Skre 
757334Skre 	ih = &ihead[INOHASH(dev, ino)];
767334Skre 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw)
777334Skre 		if (ino==ip->i_number && dev==ip->i_dev)
787334Skre 			return (ip);
797334Skre 	return ((struct inode *)0);
807334Skre }
817334Skre #endif notdef
827334Skre 
837334Skre /*
8424Sbill  * Look up an inode by device,inumber.
8524Sbill  * If it is in core (in the inode structure),
8624Sbill  * honor the locking protocol.
8724Sbill  * If it is not in core, read it in from the
8824Sbill  * specified device.
8924Sbill  * If the inode is mounted on, perform
9024Sbill  * the indicated indirection.
9124Sbill  * In all cases, a pointer to a locked
9224Sbill  * inode structure is returned.
9324Sbill  *
9424Sbill  * panic: no imt -- if the mounted file
9524Sbill  *	system is not in the mount table.
9624Sbill  *	"cannot happen"
9724Sbill  */
9824Sbill struct inode *
996569Smckusic iget(dev, fs, ino)
1004818Swnj 	dev_t dev;
1016569Smckusic 	register struct fs *fs;
1024818Swnj 	ino_t ino;
10324Sbill {
1047335Skre 	register struct inode *ip;
1057335Skre 	register union  ihead *ih;
10624Sbill 	register struct mount *mp;
10724Sbill 	register struct buf *bp;
10824Sbill 	register struct dinode *dp;
1097334Skre 	register struct inode *iq;
11024Sbill 
11124Sbill loop:
1126569Smckusic 	if (getfs(dev) != fs)
1136569Smckusic 		panic("iget: bad fs");
1147334Skre 	ih = &ihead[INOHASH(dev, ino)];
1157334Skre 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw)
1164818Swnj 		if (ino == ip->i_number && dev == ip->i_dev) {
1178452Sroot 			if ((ip->i_flag&ILOCKED) != 0) {
11824Sbill 				ip->i_flag |= IWANT;
11924Sbill 				sleep((caddr_t)ip, PINOD);
12024Sbill 				goto loop;
12124Sbill 			}
1224818Swnj 			if ((ip->i_flag&IMOUNT) != 0) {
1236569Smckusic 				for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++)
1247334Skre 					if(mp->m_inodp == ip) {
1257334Skre 						dev = mp->m_dev;
1267334Skre 						fs = mp->m_bufp->b_un.b_fs;
1277334Skre 						ino = ROOTINO;
1287334Skre 						goto loop;
1297334Skre 					}
13024Sbill 				panic("no imt");
13124Sbill 			}
1327334Skre 			if (ip->i_count == 0) {		/* ino on free list */
1337334Skre 				if (iq = ip->i_freef)
1347334Skre 					iq->i_freeb = ip->i_freeb;
1357334Skre 				else
1367334Skre 					ifreet = ip->i_freeb;
1377334Skre 				*ip->i_freeb = iq;
1387334Skre 				ip->i_freef = NULL;
1397334Skre 				ip->i_freeb = NULL;
1407334Skre 			}
14124Sbill 			ip->i_count++;
1428452Sroot 			ip->i_flag |= ILOCKED;
14324Sbill 			return(ip);
14424Sbill 		}
1457334Skre 
1467334Skre 	if ((ip = ifreeh) == NULL) {
1472933Swnj 		tablefull("inode");
14824Sbill 		u.u_error = ENFILE;
14924Sbill 		return(NULL);
15024Sbill 	}
1517334Skre 	if (iq = ip->i_freef)
1527334Skre 		iq->i_freeb = &ifreeh;
1537334Skre 	ifreeh = iq;
1547334Skre 	ip->i_freef = NULL;
1557334Skre 	ip->i_freeb = NULL;
1567334Skre 	/*
1577334Skre 	 * Now to take inode off the hash chain it was on
1587334Skre 	 * (initially, or after an iflush, it is on a "hash chain"
1597334Skre 	 * consisting entirely of itself, and pointed to by no-one,
1607334Skre 	 * but that doesn't matter), and put it on the chain for
1617334Skre 	 * its new (ino, dev) pair
1627334Skre 	 */
1637335Skre 	remque(ip);
1647335Skre 	insque(ip, ih);
1657651Ssam #ifdef QUOTA
1667492Skre 	dqrele(ip->i_dquot);
1677492Skre #endif
16824Sbill 	ip->i_dev = dev;
1696569Smckusic 	ip->i_fs = fs;
17024Sbill 	ip->i_number = ino;
1718452Sroot 	ip->i_flag = ILOCKED;
17224Sbill 	ip->i_count++;
1736569Smckusic 	ip->i_lastr = 0;
1748618Sroot 	bp = bread(dev, fsbtodb(fs, itod(fs, ino)), (int)fs->fs_bsize);
17524Sbill 	/*
17624Sbill 	 * Check I/O errors
17724Sbill 	 */
1784818Swnj 	if ((bp->b_flags&B_ERROR) != 0) {
17924Sbill 		brelse(bp);
1807334Skre 		/*
1817334Skre 		 * the inode doesn't contain anything useful, so it would
1827334Skre 		 * be misleading to leave it on its hash chain.
1837334Skre 		 * 'iput' will take care of putting it back on the free list.
1847334Skre 		 */
1857335Skre 		remque(ip);
1867334Skre 		ip->i_forw = ip;
1877334Skre 		ip->i_back = ip;
1887334Skre 		/*
1897334Skre 		 * we also loose its inumber, just in case (as iput
1907334Skre 		 * doesn't do that any more) - but as it isn't on its
1917334Skre 		 * hash chain, I doubt if this is really necessary .. kre
1927334Skre 		 * (probably the two methods are interchangable)
1937334Skre 		 */
1947334Skre 		ip->i_number = 0;
1957651Ssam #ifdef QUOTA
1967492Skre 		ip->i_dquot = NODQUOT;
1977492Skre #endif
19824Sbill 		iput(ip);
19924Sbill 		return(NULL);
20024Sbill 	}
20124Sbill 	dp = bp->b_un.b_dino;
2026569Smckusic 	dp += itoo(fs, ino);
2036569Smckusic 	ip->i_ic = dp->di_ic;
20424Sbill 	brelse(bp);
2057651Ssam #ifdef QUOTA
2067492Skre 	if (ip->i_mode == 0)
2077492Skre 		ip->i_dquot = NODQUOT;
2087492Skre 	else
2097492Skre 		ip->i_dquot = inoquota(ip);
2107492Skre #endif
2116569Smckusic 	return (ip);
21224Sbill }
21324Sbill 
21424Sbill /*
21524Sbill  * Decrement reference count of
21624Sbill  * an inode structure.
21724Sbill  * On the last reference,
21824Sbill  * write the inode out and if necessary,
21924Sbill  * truncate and deallocate the file.
22024Sbill  */
22124Sbill iput(ip)
2224818Swnj 	register struct inode *ip;
22324Sbill {
2247118Smckusick 
2258452Sroot 	if ((ip->i_flag & ILOCKED) == 0)
2267118Smckusick 		panic("iput");
2277118Smckusick 	iunlock(ip);
2287118Smckusick 	irele(ip);
2297118Smckusick }
2307118Smckusick 
2317118Smckusick irele(ip)
2327118Smckusick 	register struct inode *ip;
2337118Smckusick {
2346569Smckusic 	int mode;
23524Sbill 
2364818Swnj 	if (ip->i_count == 1) {
2378452Sroot 		ip->i_flag |= ILOCKED;
2384818Swnj 		if (ip->i_nlink <= 0) {
2399165Ssam 			itrunc(ip, (u_long)0);
2406569Smckusic 			mode = ip->i_mode;
24124Sbill 			ip->i_mode = 0;
2427351Skre 			ip->i_rdev = 0;
24324Sbill 			ip->i_flag |= IUPD|ICHG;
2446569Smckusic 			ifree(ip, ip->i_number, mode);
2457651Ssam #ifdef QUOTA
246*12645Ssam 			(void) chkiq(ip->i_dev, ip, ip->i_uid, 0);
2477492Skre 			dqrele(ip->i_dquot);
2487492Skre 			ip->i_dquot = NODQUOT;
2497492Skre #endif
25024Sbill 		}
2518671Sroot 		IUPDAT(ip, &time, &time, 0);
2527118Smckusick 		iunlock(ip);
2537334Skre 		ip->i_flag = 0;
2547334Skre 		/*
2557334Skre 		 * Put the inode on the end of the free list.
2567334Skre 		 * Possibly in some cases it would be better to
2577334Skre 		 * put the inode at the head of the free list,
2587334Skre 		 * (eg: where i_mode == 0 || i_number == 0)
2597334Skre 		 * but I will think about that later .. kre
2607334Skre 		 * (i_number is rarely 0 - only after an i/o error in iget,
2617334Skre 		 * where i_mode == 0, the inode will probably be wanted
2627334Skre 		 * again soon for an ialloc, so possibly we should keep it)
2637334Skre 		 */
2647334Skre 		if (ifreeh) {
2657334Skre 			*ifreet = ip;
2667334Skre 			ip->i_freeb = ifreet;
26724Sbill 		} else {
2687334Skre 			ifreeh = ip;
2697334Skre 			ip->i_freeb = &ifreeh;
27024Sbill 		}
2717334Skre 		ip->i_freef = NULL;
2727334Skre 		ifreet = &ip->i_freef;
2737118Smckusick 	}
27424Sbill 	ip->i_count--;
27524Sbill }
27624Sbill 
27724Sbill /*
27824Sbill  * Check accessed and update flags on
27924Sbill  * an inode structure.
28024Sbill  * If any is on, update the inode
28124Sbill  * with the current time.
2821203Sbill  * If waitfor is given, then must insure
2831203Sbill  * i/o order so wait for write to complete.
28424Sbill  */
2851203Sbill iupdat(ip, ta, tm, waitfor)
2864818Swnj 	register struct inode *ip;
2878630Sroot 	struct timeval *ta, *tm;
2884818Swnj 	int waitfor;
28924Sbill {
29024Sbill 	register struct buf *bp;
29124Sbill 	struct dinode *dp;
2926569Smckusic 	register struct fs *fp;
29324Sbill 
2946569Smckusic 	fp = ip->i_fs;
2956569Smckusic 	if ((ip->i_flag & (IUPD|IACC|ICHG)) != 0) {
2966569Smckusic 		if (fp->fs_ronly)
29724Sbill 			return;
2986569Smckusic 		bp = bread(ip->i_dev, fsbtodb(fp, itod(fp, ip->i_number)),
2998618Sroot 			(int)fp->fs_bsize);
30024Sbill 		if (bp->b_flags & B_ERROR) {
30124Sbill 			brelse(bp);
30224Sbill 			return;
30324Sbill 		}
3044818Swnj 		if (ip->i_flag&IACC)
3058630Sroot 			ip->i_atime = ta->tv_sec;
3064818Swnj 		if (ip->i_flag&IUPD)
3078630Sroot 			ip->i_mtime = tm->tv_sec;
3084818Swnj 		if (ip->i_flag&ICHG)
3098106Sroot 			ip->i_ctime = time.tv_sec;
31024Sbill 		ip->i_flag &= ~(IUPD|IACC|ICHG);
3117343Skre 		dp = bp->b_un.b_dino + itoo(fp, ip->i_number);
3127343Skre 		dp->di_ic = ip->i_ic;
3131203Sbill 		if (waitfor)
3141203Sbill 			bwrite(bp);
3151203Sbill 		else
3161203Sbill 			bdwrite(bp);
31724Sbill 	}
31824Sbill }
31924Sbill 
32010736Ssam #define	SINGLE	0	/* index of single indirect block */
32110736Ssam #define	DOUBLE	1	/* index of double indirect block */
32210736Ssam #define	TRIPLE	2	/* index of triple indirect block */
32324Sbill /*
3247702Ssam  * Truncate the inode ip to at most
3257702Ssam  * length size.  Free affected disk
3267702Ssam  * blocks -- the blocks of the file
3277702Ssam  * are removed in reverse order.
32810736Ssam  *
32910736Ssam  * NB: triple indirect blocks are untested.
33024Sbill  */
33110736Ssam itrunc(oip, length)
33210736Ssam 	struct inode *oip;
3339165Ssam 	u_long length;
33424Sbill {
33524Sbill 	register i;
3369165Ssam 	register daddr_t lastblock;
33710736Ssam 	daddr_t bn, lastiblock[NIADDR];
3386569Smckusic 	register struct fs *fs;
33910736Ssam 	register struct inode *ip;
34010736Ssam 	struct inode tip;
3419165Ssam 	long blocksreleased = 0, nblocks;
3429165Ssam 	long indirtrunc();
343*12645Ssam 	int level;
3449165Ssam 
34510736Ssam 	if (oip->i_size <= length)
3469165Ssam 		return;
3471203Sbill 	/*
34810736Ssam 	 * Calculate index into inode's block list of
34910736Ssam 	 * last direct and indirect blocks (if any)
35010736Ssam 	 * which we want to keep.  Lastblock is -1 when
35110736Ssam 	 * the file is truncated to 0.
3521203Sbill 	 */
35310736Ssam 	fs = oip->i_fs;
3549165Ssam 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
35510736Ssam 	lastiblock[SINGLE] = lastblock - NDADDR;
35610736Ssam 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
35710736Ssam 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
358*12645Ssam 	nblocks = btodb(fs->fs_bsize);
3596569Smckusic 	/*
36010736Ssam 	 * Update size of file and block pointers
36110736Ssam 	 * on disk before we start freeing blocks.
36210736Ssam 	 * If we crash before free'ing blocks below,
36310736Ssam 	 * the blocks will be returned to the free list.
36410736Ssam 	 * lastiblock values are also normalized to -1
36510736Ssam 	 * for calls to indirtrunc below.
36610736Ssam 	 * (? fsck doesn't check validity of pointers in indirect blocks)
3676569Smckusic 	 */
36810736Ssam 	tip = *oip;
36910736Ssam 	for (level = TRIPLE; level >= SINGLE; level--)
37010736Ssam 		if (lastiblock[level] < 0) {
37110736Ssam 			oip->i_ib[level] = 0;
37210736Ssam 			lastiblock[level] = -1;
3739165Ssam 		}
37410736Ssam 	for (i = NDADDR - 1; i > lastblock; i--)
37510736Ssam 		oip->i_db[i] = 0;
37610736Ssam 	oip->i_size = length;
37710736Ssam 	oip->i_flag |= ICHG|IUPD;
37810736Ssam 	iupdat(oip, &time, &time, 1);
37910736Ssam 	ip = &tip;
38010736Ssam 
3816569Smckusic 	/*
38210736Ssam 	 * Indirect blocks first.
3836569Smckusic 	 */
38410736Ssam 	for (level = TRIPLE; level >= SINGLE; level--) {
38510736Ssam 		bn = ip->i_ib[level];
3869165Ssam 		if (bn != 0) {
38710736Ssam 			blocksreleased +=
388*12645Ssam 			    indirtrunc(ip, bn, lastiblock[level], level);
38910736Ssam 			if (lastiblock[level] < 0) {
39010736Ssam 				ip->i_ib[level] = 0;
39110736Ssam 				free(ip, bn, (off_t)fs->fs_bsize);
39210736Ssam 				blocksreleased += nblocks;
39310736Ssam 			}
39410736Ssam 		}
39510736Ssam 		if (lastiblock[level] >= 0)
39610736Ssam 			goto done;
3979165Ssam 	}
39810736Ssam 
3996569Smckusic 	/*
40010736Ssam 	 * All whole direct blocks or frags.
4016569Smckusic 	 */
4029165Ssam 	for (i = NDADDR - 1; i > lastblock; i--) {
4039165Ssam 		register int size;
4049165Ssam 
4056569Smckusic 		bn = ip->i_db[i];
4069165Ssam 		if (bn == 0)
40724Sbill 			continue;
4089165Ssam 		ip->i_db[i] = 0;
4099165Ssam 		size = (off_t)blksize(fs, ip, i);
4109165Ssam 		free(ip, bn, size);
411*12645Ssam 		blocksreleased += btodb(size);
41224Sbill 	}
41310736Ssam 	if (lastblock < 0)
41410736Ssam 		goto done;
41510736Ssam 
4161203Sbill 	/*
4179165Ssam 	 * Finally, look for a change in size of the
4189165Ssam 	 * last direct block; release any frags.
4191203Sbill 	 */
42010736Ssam 	bn = ip->i_db[lastblock];
42110736Ssam 	if (bn != 0) {
42210736Ssam 		int oldspace, newspace;
42310736Ssam 
4249165Ssam 		/*
4259165Ssam 		 * Calculate amount of space we're giving
4269165Ssam 		 * back as old block size minus new block size.
4279165Ssam 		 */
42810736Ssam 		oldspace = blksize(fs, ip, lastblock);
4299165Ssam 		ip->i_size = length;
43010736Ssam 		newspace = blksize(fs, ip, lastblock);
43110736Ssam 		if (newspace == 0)
43210736Ssam 			panic("itrunc: newspace");
43310736Ssam 		if (oldspace - newspace > 0) {
4349165Ssam 			/*
4359165Ssam 			 * Block number of space to be free'd is
4369165Ssam 			 * the old block # plus the number of frags
4379165Ssam 			 * required for the storage we're keeping.
4389165Ssam 			 */
43910736Ssam 			bn += numfrags(fs, newspace);
44010736Ssam 			free(ip, bn, oldspace - newspace);
441*12645Ssam 			blocksreleased += btodb(oldspace - newspace);
4429165Ssam 		}
4439165Ssam 	}
4449165Ssam done:
44510736Ssam /* BEGIN PARANOIA */
44610736Ssam 	for (level = SINGLE; level <= TRIPLE; level++)
44710736Ssam 		if (ip->i_ib[level] != oip->i_ib[level])
44810736Ssam 			panic("itrunc1");
44910736Ssam 	for (i = 0; i < NDADDR; i++)
45010736Ssam 		if (ip->i_db[i] != oip->i_db[i])
45110736Ssam 			panic("itrunc2");
45210736Ssam /* END PARANOIA */
453*12645Ssam 	oip->i_blocks -= blocksreleased;
454*12645Ssam 	if (oip->i_blocks < 0)			/* sanity */
455*12645Ssam 		oip->i_blocks = 0;
456*12645Ssam 	oip->i_flag |= ICHG;
4579165Ssam #ifdef QUOTA
458*12645Ssam 	(void) chkdq(oip, -blocksreleased, 0);
4599165Ssam #endif
46024Sbill }
46124Sbill 
4629165Ssam /*
4639165Ssam  * Release blocks associated with the inode ip and
4649165Ssam  * stored in the indirect block bn.  Blocks are free'd
4659165Ssam  * in LIFO order up to (but not including) lastbn.  If
46610736Ssam  * level is greater than SINGLE, the block is an indirect
46710736Ssam  * block and recursive calls to indirtrunc must be used to
46810736Ssam  * cleanse other indirect blocks.
46910736Ssam  *
47010736Ssam  * NB: triple indirect blocks are untested.
4719165Ssam  */
4727492Skre long
47310736Ssam indirtrunc(ip, bn, lastbn, level)
4746569Smckusic 	register struct inode *ip;
4759165Ssam 	daddr_t bn, lastbn;
47610736Ssam 	int level;
47724Sbill {
4789165Ssam 	register int i;
47910736Ssam 	struct buf *bp, *copy;
48024Sbill 	register daddr_t *bap;
48110736Ssam 	register struct fs *fs = ip->i_fs;
4829165Ssam 	daddr_t nb, last;
48310736Ssam 	long factor;
4849165Ssam 	int blocksreleased = 0, nblocks;
48524Sbill 
48610736Ssam 	/*
48710736Ssam 	 * Calculate index in current block of last
48810736Ssam 	 * block to be kept.  -1 indicates the entire
48910736Ssam 	 * block so we need not calculate the index.
49010736Ssam 	 */
49110736Ssam 	factor = 1;
49210736Ssam 	for (i = SINGLE; i < level; i++)
49310736Ssam 		factor *= NINDIR(fs);
4949165Ssam 	last = lastbn;
49510736Ssam 	if (lastbn > 0)
49610736Ssam 		last /= factor;
497*12645Ssam 	nblocks = btodb(fs->fs_bsize);
49810736Ssam 	/*
49910736Ssam 	 * Get buffer of block pointers, zero those
50010736Ssam 	 * entries corresponding to blocks to be free'd,
50110736Ssam 	 * and update on disk copy first.
50210736Ssam 	 */
50310736Ssam 	copy = geteblk((int)fs->fs_bsize);
50410736Ssam 	bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize);
50510736Ssam 	if (bp->b_flags&B_ERROR) {
50610736Ssam 		brelse(copy);
50710736Ssam 		brelse(bp);
50810736Ssam 		return (0);
50910736Ssam 	}
51010736Ssam 	bap = bp->b_un.b_daddr;
51110736Ssam 	bcopy((caddr_t)bap, (caddr_t)copy->b_un.b_daddr, (u_int)fs->fs_bsize);
51210736Ssam 	bzero((caddr_t)&bap[last + 1],
51310736Ssam 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
51410736Ssam 	bwrite(bp);
51510736Ssam 	bp = copy, bap = bp->b_un.b_daddr;
51610736Ssam 
51710736Ssam 	/*
51810736Ssam 	 * Recursively free totally unused blocks.
51910736Ssam 	 */
5209165Ssam 	for (i = NINDIR(fs) - 1; i > last; i--) {
52124Sbill 		nb = bap[i];
5229165Ssam 		if (nb == 0)
52324Sbill 			continue;
52410736Ssam 		if (level > SINGLE)
5259165Ssam 			blocksreleased +=
526*12645Ssam 			    indirtrunc(ip, nb, (daddr_t)-1, level - 1);
5279165Ssam 		free(ip, nb, (int)fs->fs_bsize);
5289165Ssam 		blocksreleased += nblocks;
52924Sbill 	}
53010736Ssam 
53110736Ssam 	/*
53210736Ssam 	 * Recursively free last partial block.
53310736Ssam 	 */
53410736Ssam 	if (level > SINGLE && lastbn >= 0) {
53510736Ssam 		last = lastbn % factor;
5369165Ssam 		nb = bap[i];
5379165Ssam 		if (nb != 0)
538*12645Ssam 			blocksreleased += indirtrunc(ip, nb, last, level - 1);
5399165Ssam 	}
54010736Ssam 	brelse(bp);
5419165Ssam 	return (blocksreleased);
54224Sbill }
54324Sbill 
54424Sbill /*
5457334Skre  * remove any inodes in the inode cache belonging to dev
5467334Skre  *
5477334Skre  * There should not be any active ones, return error if any are found
5487334Skre  * (nb: this is a user error, not a system err)
5497334Skre  *
5507334Skre  * Also, count the references to dev by block devices - this really
5517334Skre  * has nothing to do with the object of the procedure, but as we have
5527334Skre  * to scan the inode table here anyway, we might as well get the
5537334Skre  * extra benefit.
5547334Skre  *
5557334Skre  * this is called from sumount()/sys3.c when dev is being unmounted
5567334Skre  */
5577651Ssam #ifdef QUOTA
5587504Sroot iflush(dev, iq)
5597492Skre 	dev_t dev;
5607504Sroot 	struct inode *iq;
5617492Skre #else
5627334Skre iflush(dev)
5637334Skre 	dev_t dev;
5647492Skre #endif
5657334Skre {
5667335Skre 	register struct inode *ip;
5677334Skre 	register open = 0;
5687334Skre 
5697334Skre 	for (ip = inode; ip < inodeNINODE; ip++) {
5707651Ssam #ifdef QUOTA
5717492Skre 		if (ip != iq && ip->i_dev == dev)
5727492Skre #else
5737334Skre 		if (ip->i_dev == dev)
5747492Skre #endif
5757334Skre 			if (ip->i_count)
5767334Skre 				return(-1);
5777334Skre 			else {
5787335Skre 				remque(ip);
5797334Skre 				ip->i_forw = ip;
5807334Skre 				ip->i_back = ip;
5817334Skre 				/*
5827334Skre 				 * as i_count == 0, the inode was on the free
5837334Skre 				 * list already, just leave it there, it will
5847334Skre 				 * fall off the bottom eventually. We could
5857334Skre 				 * perhaps move it to the head of the free
5867334Skre 				 * list, but as umounts are done so
5877334Skre 				 * infrequently, we would gain very little,
5887334Skre 				 * while making the code bigger.
5897334Skre 				 */
5907651Ssam #ifdef QUOTA
5917492Skre 				dqrele(ip->i_dquot);
5927492Skre 				ip->i_dquot = NODQUOT;
5937492Skre #endif
5947334Skre 			}
5957334Skre 		else if (ip->i_count && (ip->i_mode&IFMT)==IFBLK &&
5967334Skre 		    ip->i_rdev == dev)
5977334Skre 			open++;
5987334Skre 	}
5997334Skre 	return (open);
6007334Skre }
6017334Skre 
6023617Sroot /*
6034818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
6043617Sroot  */
6054818Swnj ilock(ip)
6064818Swnj 	register struct inode *ip;
6073617Sroot {
6083617Sroot 
6098452Sroot 	ILOCK(ip);
6103617Sroot }
6113617Sroot 
6123617Sroot /*
6134818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
6143617Sroot  */
6157118Smckusick iunlock(ip)
6164818Swnj 	register struct inode *ip;
6173617Sroot {
6183617Sroot 
6198452Sroot 	IUNLOCK(ip);
6203617Sroot }
621