xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 16665)
1*16665Smckusick /*	lfs_inode.c	6.7	84/07/04	*/
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 
1716524Skarels #define	INOHSZ	64
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;
11016656Smckusick 	struct inode *xp;
11124Sbill 
11216656Smckusick 
11324Sbill loop:
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) {
11716642Ssam 			/*
11816642Ssam 			 * Following is essentially an inline expanded
11916642Ssam 			 * copy of igrab(), expanded inline for speed,
12016642Ssam 			 * and so that the test for a mounted on inode
12116642Ssam 			 * can be deferred until after we are sure that
12216642Ssam 			 * the inode isn't busy.
12316642Ssam 			 */
1248452Sroot 			if ((ip->i_flag&ILOCKED) != 0) {
12524Sbill 				ip->i_flag |= IWANT;
12624Sbill 				sleep((caddr_t)ip, PINOD);
12724Sbill 				goto loop;
12824Sbill 			}
1294818Swnj 			if ((ip->i_flag&IMOUNT) != 0) {
1306569Smckusic 				for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++)
1317334Skre 					if(mp->m_inodp == ip) {
1327334Skre 						dev = mp->m_dev;
1337334Skre 						fs = mp->m_bufp->b_un.b_fs;
1347334Skre 						ino = ROOTINO;
1357334Skre 						goto loop;
1367334Skre 					}
13724Sbill 				panic("no imt");
13824Sbill 			}
1397334Skre 			if (ip->i_count == 0) {		/* ino on free list */
1407334Skre 				if (iq = ip->i_freef)
1417334Skre 					iq->i_freeb = ip->i_freeb;
1427334Skre 				else
1437334Skre 					ifreet = ip->i_freeb;
1447334Skre 				*ip->i_freeb = iq;
1457334Skre 				ip->i_freef = NULL;
1467334Skre 				ip->i_freeb = NULL;
1477334Skre 			}
14824Sbill 			ip->i_count++;
1498452Sroot 			ip->i_flag |= ILOCKED;
15024Sbill 			return(ip);
15124Sbill 		}
1527334Skre 
1537334Skre 	if ((ip = ifreeh) == NULL) {
1542933Swnj 		tablefull("inode");
15524Sbill 		u.u_error = ENFILE;
15624Sbill 		return(NULL);
15724Sbill 	}
1587334Skre 	if (iq = ip->i_freef)
1597334Skre 		iq->i_freeb = &ifreeh;
1607334Skre 	ifreeh = iq;
1617334Skre 	ip->i_freef = NULL;
1627334Skre 	ip->i_freeb = NULL;
1637334Skre 	/*
1647334Skre 	 * Now to take inode off the hash chain it was on
1657334Skre 	 * (initially, or after an iflush, it is on a "hash chain"
1667334Skre 	 * consisting entirely of itself, and pointed to by no-one,
1677334Skre 	 * but that doesn't matter), and put it on the chain for
1687334Skre 	 * its new (ino, dev) pair
1697334Skre 	 */
1707335Skre 	remque(ip);
1717335Skre 	insque(ip, ih);
1727651Ssam #ifdef QUOTA
1737492Skre 	dqrele(ip->i_dquot);
1747492Skre #endif
17524Sbill 	ip->i_dev = dev;
1766569Smckusic 	ip->i_fs = fs;
17724Sbill 	ip->i_number = ino;
17816642Ssam 	ip->i_id = ++nextinodeid;	/* also used in rename */
17916642Ssam 	/*
18016642Ssam 	 * At an absurd rate of 100 calls/second,
18116656Smckusick 	 * this should occur once every 8 months.
18216642Ssam 	 */
18316656Smckusick 	if (nextinodeid < 0)
18416656Smckusick 		for (nextinodeid = 0, xp = inode; xp < inodeNINODE; xp++)
18516656Smckusick 			xp->i_id = 0;
1868452Sroot 	ip->i_flag = ILOCKED;
18724Sbill 	ip->i_count++;
1886569Smckusic 	ip->i_lastr = 0;
1898618Sroot 	bp = bread(dev, fsbtodb(fs, itod(fs, ino)), (int)fs->fs_bsize);
19024Sbill 	/*
19124Sbill 	 * Check I/O errors
19224Sbill 	 */
1934818Swnj 	if ((bp->b_flags&B_ERROR) != 0) {
19424Sbill 		brelse(bp);
1957334Skre 		/*
1967334Skre 		 * the inode doesn't contain anything useful, so it would
1977334Skre 		 * be misleading to leave it on its hash chain.
1987334Skre 		 * 'iput' will take care of putting it back on the free list.
1997334Skre 		 */
2007335Skre 		remque(ip);
2017334Skre 		ip->i_forw = ip;
2027334Skre 		ip->i_back = ip;
2037334Skre 		/*
2047334Skre 		 * we also loose its inumber, just in case (as iput
2057334Skre 		 * doesn't do that any more) - but as it isn't on its
2067334Skre 		 * hash chain, I doubt if this is really necessary .. kre
2077334Skre 		 * (probably the two methods are interchangable)
2087334Skre 		 */
2097334Skre 		ip->i_number = 0;
2107651Ssam #ifdef QUOTA
2117492Skre 		ip->i_dquot = NODQUOT;
2127492Skre #endif
21324Sbill 		iput(ip);
21424Sbill 		return(NULL);
21524Sbill 	}
21624Sbill 	dp = bp->b_un.b_dino;
2176569Smckusic 	dp += itoo(fs, ino);
2186569Smckusic 	ip->i_ic = dp->di_ic;
21924Sbill 	brelse(bp);
2207651Ssam #ifdef QUOTA
2217492Skre 	if (ip->i_mode == 0)
2227492Skre 		ip->i_dquot = NODQUOT;
2237492Skre 	else
2247492Skre 		ip->i_dquot = inoquota(ip);
2257492Skre #endif
2266569Smckusic 	return (ip);
22724Sbill }
22824Sbill 
22924Sbill /*
23016642Ssam  * Convert a pointer to an inode into a reference to an inode.
23116642Ssam  *
23216642Ssam  * This is basically the internal piece of iget (after the
23316642Ssam  * inode pointer is located) but without the test for mounted
23416642Ssam  * filesystems.  It is caller's responsibility to check that
23516642Ssam  * the inode pointer is valid.
23616642Ssam  */
23716642Ssam igrab(ip)
23816642Ssam 	register struct inode *ip;
23916642Ssam {
24016642Ssam 	while ((ip->i_flag&ILOCKED) != 0) {
24116642Ssam 		ip->i_flag |= IWANT;
24216642Ssam 		sleep((caddr_t)ip, PINOD);
24316642Ssam 	}
24416642Ssam 	if (ip->i_count == 0) {		/* ino on free list */
24516642Ssam 		register struct inode *iq;
24616642Ssam 
24716642Ssam 		if (iq = ip->i_freef)
24816642Ssam 			iq->i_freeb = ip->i_freeb;
24916642Ssam 		else
25016642Ssam 			ifreet = ip->i_freeb;
25116642Ssam 		*ip->i_freeb = iq;
25216642Ssam 		ip->i_freef = NULL;
25316642Ssam 		ip->i_freeb = NULL;
25416642Ssam 	}
25516642Ssam 	ip->i_count++;
25616642Ssam 	ip->i_flag |= ILOCKED;
25716642Ssam }
25816642Ssam 
25916642Ssam /*
26024Sbill  * Decrement reference count of
26124Sbill  * an inode structure.
26224Sbill  * On the last reference,
26324Sbill  * write the inode out and if necessary,
26424Sbill  * truncate and deallocate the file.
26524Sbill  */
26624Sbill iput(ip)
2674818Swnj 	register struct inode *ip;
26824Sbill {
2697118Smckusick 
2708452Sroot 	if ((ip->i_flag & ILOCKED) == 0)
2717118Smckusick 		panic("iput");
272*16665Smckusick 	IUNLOCK(ip);
2737118Smckusick 	irele(ip);
2747118Smckusick }
2757118Smckusick 
2767118Smckusick irele(ip)
2777118Smckusick 	register struct inode *ip;
2787118Smckusick {
2796569Smckusic 	int mode;
28024Sbill 
2814818Swnj 	if (ip->i_count == 1) {
2828452Sroot 		ip->i_flag |= ILOCKED;
2834818Swnj 		if (ip->i_nlink <= 0) {
2849165Ssam 			itrunc(ip, (u_long)0);
2856569Smckusic 			mode = ip->i_mode;
28624Sbill 			ip->i_mode = 0;
2877351Skre 			ip->i_rdev = 0;
28824Sbill 			ip->i_flag |= IUPD|ICHG;
2896569Smckusic 			ifree(ip, ip->i_number, mode);
2907651Ssam #ifdef QUOTA
29112645Ssam 			(void) chkiq(ip->i_dev, ip, ip->i_uid, 0);
2927492Skre 			dqrele(ip->i_dquot);
2937492Skre 			ip->i_dquot = NODQUOT;
2947492Skre #endif
29524Sbill 		}
2968671Sroot 		IUPDAT(ip, &time, &time, 0);
297*16665Smckusick 		IUNLOCK(ip);
2987334Skre 		ip->i_flag = 0;
2997334Skre 		/*
3007334Skre 		 * Put the inode on the end of the free list.
3017334Skre 		 * Possibly in some cases it would be better to
3027334Skre 		 * put the inode at the head of the free list,
3037334Skre 		 * (eg: where i_mode == 0 || i_number == 0)
3047334Skre 		 * but I will think about that later .. kre
3057334Skre 		 * (i_number is rarely 0 - only after an i/o error in iget,
3067334Skre 		 * where i_mode == 0, the inode will probably be wanted
3077334Skre 		 * again soon for an ialloc, so possibly we should keep it)
3087334Skre 		 */
3097334Skre 		if (ifreeh) {
3107334Skre 			*ifreet = ip;
3117334Skre 			ip->i_freeb = ifreet;
31224Sbill 		} else {
3137334Skre 			ifreeh = ip;
3147334Skre 			ip->i_freeb = &ifreeh;
31524Sbill 		}
3167334Skre 		ip->i_freef = NULL;
3177334Skre 		ifreet = &ip->i_freef;
31816058Skarels 	} else if (!(ip->i_flag & ILOCKED))
31916058Skarels 		ITIMES(ip, &time, &time);
32024Sbill 	ip->i_count--;
32124Sbill }
32224Sbill 
32324Sbill /*
32424Sbill  * Check accessed and update flags on
32524Sbill  * an inode structure.
32624Sbill  * If any is on, update the inode
32724Sbill  * with the current time.
3281203Sbill  * If waitfor is given, then must insure
3291203Sbill  * i/o order so wait for write to complete.
33024Sbill  */
3311203Sbill iupdat(ip, ta, tm, waitfor)
3324818Swnj 	register struct inode *ip;
3338630Sroot 	struct timeval *ta, *tm;
3344818Swnj 	int waitfor;
33524Sbill {
33624Sbill 	register struct buf *bp;
33724Sbill 	struct dinode *dp;
3386569Smckusic 	register struct fs *fp;
33924Sbill 
3406569Smckusic 	fp = ip->i_fs;
34116058Skarels 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) != 0) {
3426569Smckusic 		if (fp->fs_ronly)
34324Sbill 			return;
3446569Smckusic 		bp = bread(ip->i_dev, fsbtodb(fp, itod(fp, ip->i_number)),
3458618Sroot 			(int)fp->fs_bsize);
34624Sbill 		if (bp->b_flags & B_ERROR) {
34724Sbill 			brelse(bp);
34824Sbill 			return;
34924Sbill 		}
3504818Swnj 		if (ip->i_flag&IACC)
3518630Sroot 			ip->i_atime = ta->tv_sec;
3524818Swnj 		if (ip->i_flag&IUPD)
3538630Sroot 			ip->i_mtime = tm->tv_sec;
3544818Swnj 		if (ip->i_flag&ICHG)
3558106Sroot 			ip->i_ctime = time.tv_sec;
35616058Skarels 		ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
3577343Skre 		dp = bp->b_un.b_dino + itoo(fp, ip->i_number);
3587343Skre 		dp->di_ic = ip->i_ic;
3591203Sbill 		if (waitfor)
3601203Sbill 			bwrite(bp);
3611203Sbill 		else
3621203Sbill 			bdwrite(bp);
36324Sbill 	}
36424Sbill }
36524Sbill 
36610736Ssam #define	SINGLE	0	/* index of single indirect block */
36710736Ssam #define	DOUBLE	1	/* index of double indirect block */
36810736Ssam #define	TRIPLE	2	/* index of triple indirect block */
36924Sbill /*
3707702Ssam  * Truncate the inode ip to at most
3717702Ssam  * length size.  Free affected disk
3727702Ssam  * blocks -- the blocks of the file
3737702Ssam  * are removed in reverse order.
37410736Ssam  *
37510736Ssam  * NB: triple indirect blocks are untested.
37624Sbill  */
37710736Ssam itrunc(oip, length)
37810736Ssam 	struct inode *oip;
3799165Ssam 	u_long length;
38024Sbill {
38124Sbill 	register i;
3829165Ssam 	register daddr_t lastblock;
38310736Ssam 	daddr_t bn, lastiblock[NIADDR];
3846569Smckusic 	register struct fs *fs;
38510736Ssam 	register struct inode *ip;
38610736Ssam 	struct inode tip;
3879165Ssam 	long blocksreleased = 0, nblocks;
3889165Ssam 	long indirtrunc();
38912645Ssam 	int level;
3909165Ssam 
39113000Ssam 	if (oip->i_size <= length) {
39213000Ssam 		oip->i_flag |= ICHG|IUPD;
39313000Ssam 		iupdat(oip, &time, &time, 1);
3949165Ssam 		return;
39513000Ssam 	}
3961203Sbill 	/*
39710736Ssam 	 * Calculate index into inode's block list of
39810736Ssam 	 * last direct and indirect blocks (if any)
39910736Ssam 	 * which we want to keep.  Lastblock is -1 when
40010736Ssam 	 * the file is truncated to 0.
4011203Sbill 	 */
40210736Ssam 	fs = oip->i_fs;
4039165Ssam 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
40410736Ssam 	lastiblock[SINGLE] = lastblock - NDADDR;
40510736Ssam 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
40610736Ssam 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
40712645Ssam 	nblocks = btodb(fs->fs_bsize);
4086569Smckusic 	/*
40910736Ssam 	 * Update size of file and block pointers
41010736Ssam 	 * on disk before we start freeing blocks.
41110736Ssam 	 * If we crash before free'ing blocks below,
41210736Ssam 	 * the blocks will be returned to the free list.
41310736Ssam 	 * lastiblock values are also normalized to -1
41410736Ssam 	 * for calls to indirtrunc below.
41510736Ssam 	 * (? fsck doesn't check validity of pointers in indirect blocks)
4166569Smckusic 	 */
41710736Ssam 	tip = *oip;
41810736Ssam 	for (level = TRIPLE; level >= SINGLE; level--)
41910736Ssam 		if (lastiblock[level] < 0) {
42010736Ssam 			oip->i_ib[level] = 0;
42110736Ssam 			lastiblock[level] = -1;
4229165Ssam 		}
42310736Ssam 	for (i = NDADDR - 1; i > lastblock; i--)
42410736Ssam 		oip->i_db[i] = 0;
42510736Ssam 	oip->i_size = length;
42610736Ssam 	oip->i_flag |= ICHG|IUPD;
42710736Ssam 	iupdat(oip, &time, &time, 1);
42810736Ssam 	ip = &tip;
42910736Ssam 
4306569Smckusic 	/*
43110736Ssam 	 * Indirect blocks first.
4326569Smckusic 	 */
43310736Ssam 	for (level = TRIPLE; level >= SINGLE; level--) {
43410736Ssam 		bn = ip->i_ib[level];
4359165Ssam 		if (bn != 0) {
43610736Ssam 			blocksreleased +=
43712645Ssam 			    indirtrunc(ip, bn, lastiblock[level], level);
43810736Ssam 			if (lastiblock[level] < 0) {
43910736Ssam 				ip->i_ib[level] = 0;
44010736Ssam 				free(ip, bn, (off_t)fs->fs_bsize);
44110736Ssam 				blocksreleased += nblocks;
44210736Ssam 			}
44310736Ssam 		}
44410736Ssam 		if (lastiblock[level] >= 0)
44510736Ssam 			goto done;
4469165Ssam 	}
44710736Ssam 
4486569Smckusic 	/*
44910736Ssam 	 * All whole direct blocks or frags.
4506569Smckusic 	 */
4519165Ssam 	for (i = NDADDR - 1; i > lastblock; i--) {
4529165Ssam 		register int size;
4539165Ssam 
4546569Smckusic 		bn = ip->i_db[i];
4559165Ssam 		if (bn == 0)
45624Sbill 			continue;
4579165Ssam 		ip->i_db[i] = 0;
4589165Ssam 		size = (off_t)blksize(fs, ip, i);
4599165Ssam 		free(ip, bn, size);
46012645Ssam 		blocksreleased += btodb(size);
46124Sbill 	}
46210736Ssam 	if (lastblock < 0)
46310736Ssam 		goto done;
46410736Ssam 
4651203Sbill 	/*
4669165Ssam 	 * Finally, look for a change in size of the
4679165Ssam 	 * last direct block; release any frags.
4681203Sbill 	 */
46910736Ssam 	bn = ip->i_db[lastblock];
47010736Ssam 	if (bn != 0) {
47110736Ssam 		int oldspace, newspace;
47210736Ssam 
4739165Ssam 		/*
4749165Ssam 		 * Calculate amount of space we're giving
4759165Ssam 		 * back as old block size minus new block size.
4769165Ssam 		 */
47710736Ssam 		oldspace = blksize(fs, ip, lastblock);
4789165Ssam 		ip->i_size = length;
47910736Ssam 		newspace = blksize(fs, ip, lastblock);
48010736Ssam 		if (newspace == 0)
48110736Ssam 			panic("itrunc: newspace");
48210736Ssam 		if (oldspace - newspace > 0) {
4839165Ssam 			/*
4849165Ssam 			 * Block number of space to be free'd is
4859165Ssam 			 * the old block # plus the number of frags
4869165Ssam 			 * required for the storage we're keeping.
4879165Ssam 			 */
48810736Ssam 			bn += numfrags(fs, newspace);
48910736Ssam 			free(ip, bn, oldspace - newspace);
49012645Ssam 			blocksreleased += btodb(oldspace - newspace);
4919165Ssam 		}
4929165Ssam 	}
4939165Ssam done:
49410736Ssam /* BEGIN PARANOIA */
49510736Ssam 	for (level = SINGLE; level <= TRIPLE; level++)
49610736Ssam 		if (ip->i_ib[level] != oip->i_ib[level])
49710736Ssam 			panic("itrunc1");
49810736Ssam 	for (i = 0; i < NDADDR; i++)
49910736Ssam 		if (ip->i_db[i] != oip->i_db[i])
50010736Ssam 			panic("itrunc2");
50110736Ssam /* END PARANOIA */
50212645Ssam 	oip->i_blocks -= blocksreleased;
50312645Ssam 	if (oip->i_blocks < 0)			/* sanity */
50412645Ssam 		oip->i_blocks = 0;
50512645Ssam 	oip->i_flag |= ICHG;
5069165Ssam #ifdef QUOTA
50712645Ssam 	(void) chkdq(oip, -blocksreleased, 0);
5089165Ssam #endif
50924Sbill }
51024Sbill 
5119165Ssam /*
5129165Ssam  * Release blocks associated with the inode ip and
5139165Ssam  * stored in the indirect block bn.  Blocks are free'd
5149165Ssam  * in LIFO order up to (but not including) lastbn.  If
51510736Ssam  * level is greater than SINGLE, the block is an indirect
51610736Ssam  * block and recursive calls to indirtrunc must be used to
51710736Ssam  * cleanse other indirect blocks.
51810736Ssam  *
51910736Ssam  * NB: triple indirect blocks are untested.
5209165Ssam  */
5217492Skre long
52210736Ssam indirtrunc(ip, bn, lastbn, level)
5236569Smckusic 	register struct inode *ip;
5249165Ssam 	daddr_t bn, lastbn;
52510736Ssam 	int level;
52624Sbill {
5279165Ssam 	register int i;
52810736Ssam 	struct buf *bp, *copy;
52924Sbill 	register daddr_t *bap;
53010736Ssam 	register struct fs *fs = ip->i_fs;
5319165Ssam 	daddr_t nb, last;
53210736Ssam 	long factor;
5339165Ssam 	int blocksreleased = 0, nblocks;
53424Sbill 
53510736Ssam 	/*
53610736Ssam 	 * Calculate index in current block of last
53710736Ssam 	 * block to be kept.  -1 indicates the entire
53810736Ssam 	 * block so we need not calculate the index.
53910736Ssam 	 */
54010736Ssam 	factor = 1;
54110736Ssam 	for (i = SINGLE; i < level; i++)
54210736Ssam 		factor *= NINDIR(fs);
5439165Ssam 	last = lastbn;
54410736Ssam 	if (lastbn > 0)
54510736Ssam 		last /= factor;
54612645Ssam 	nblocks = btodb(fs->fs_bsize);
54710736Ssam 	/*
54810736Ssam 	 * Get buffer of block pointers, zero those
54910736Ssam 	 * entries corresponding to blocks to be free'd,
55010736Ssam 	 * and update on disk copy first.
55110736Ssam 	 */
55210736Ssam 	copy = geteblk((int)fs->fs_bsize);
55310736Ssam 	bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize);
55410736Ssam 	if (bp->b_flags&B_ERROR) {
55510736Ssam 		brelse(copy);
55610736Ssam 		brelse(bp);
55710736Ssam 		return (0);
55810736Ssam 	}
55910736Ssam 	bap = bp->b_un.b_daddr;
56010736Ssam 	bcopy((caddr_t)bap, (caddr_t)copy->b_un.b_daddr, (u_int)fs->fs_bsize);
56110736Ssam 	bzero((caddr_t)&bap[last + 1],
56210736Ssam 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
56310736Ssam 	bwrite(bp);
56410736Ssam 	bp = copy, bap = bp->b_un.b_daddr;
56510736Ssam 
56610736Ssam 	/*
56710736Ssam 	 * Recursively free totally unused blocks.
56810736Ssam 	 */
5699165Ssam 	for (i = NINDIR(fs) - 1; i > last; i--) {
57024Sbill 		nb = bap[i];
5719165Ssam 		if (nb == 0)
57224Sbill 			continue;
57310736Ssam 		if (level > SINGLE)
5749165Ssam 			blocksreleased +=
57512645Ssam 			    indirtrunc(ip, nb, (daddr_t)-1, level - 1);
5769165Ssam 		free(ip, nb, (int)fs->fs_bsize);
5779165Ssam 		blocksreleased += nblocks;
57824Sbill 	}
57910736Ssam 
58010736Ssam 	/*
58110736Ssam 	 * Recursively free last partial block.
58210736Ssam 	 */
58310736Ssam 	if (level > SINGLE && lastbn >= 0) {
58410736Ssam 		last = lastbn % factor;
5859165Ssam 		nb = bap[i];
5869165Ssam 		if (nb != 0)
58712645Ssam 			blocksreleased += indirtrunc(ip, nb, last, level - 1);
5889165Ssam 	}
58910736Ssam 	brelse(bp);
5909165Ssam 	return (blocksreleased);
59124Sbill }
59224Sbill 
59324Sbill /*
5947334Skre  * remove any inodes in the inode cache belonging to dev
5957334Skre  *
5967334Skre  * There should not be any active ones, return error if any are found
5977334Skre  * (nb: this is a user error, not a system err)
5987334Skre  *
5997334Skre  * Also, count the references to dev by block devices - this really
6007334Skre  * has nothing to do with the object of the procedure, but as we have
6017334Skre  * to scan the inode table here anyway, we might as well get the
6027334Skre  * extra benefit.
6037334Skre  *
6047334Skre  * this is called from sumount()/sys3.c when dev is being unmounted
6057334Skre  */
6067651Ssam #ifdef QUOTA
6077504Sroot iflush(dev, iq)
6087492Skre 	dev_t dev;
6097504Sroot 	struct inode *iq;
6107492Skre #else
6117334Skre iflush(dev)
6127334Skre 	dev_t dev;
6137492Skre #endif
6147334Skre {
6157335Skre 	register struct inode *ip;
6167334Skre 	register open = 0;
6177334Skre 
6187334Skre 	for (ip = inode; ip < inodeNINODE; ip++) {
6197651Ssam #ifdef QUOTA
6207492Skre 		if (ip != iq && ip->i_dev == dev)
6217492Skre #else
6227334Skre 		if (ip->i_dev == dev)
6237492Skre #endif
6247334Skre 			if (ip->i_count)
6257334Skre 				return(-1);
6267334Skre 			else {
6277335Skre 				remque(ip);
6287334Skre 				ip->i_forw = ip;
6297334Skre 				ip->i_back = ip;
6307334Skre 				/*
6317334Skre 				 * as i_count == 0, the inode was on the free
6327334Skre 				 * list already, just leave it there, it will
6337334Skre 				 * fall off the bottom eventually. We could
6347334Skre 				 * perhaps move it to the head of the free
6357334Skre 				 * list, but as umounts are done so
6367334Skre 				 * infrequently, we would gain very little,
6377334Skre 				 * while making the code bigger.
6387334Skre 				 */
6397651Ssam #ifdef QUOTA
6407492Skre 				dqrele(ip->i_dquot);
6417492Skre 				ip->i_dquot = NODQUOT;
6427492Skre #endif
6437334Skre 			}
6447334Skre 		else if (ip->i_count && (ip->i_mode&IFMT)==IFBLK &&
6457334Skre 		    ip->i_rdev == dev)
6467334Skre 			open++;
6477334Skre 	}
6487334Skre 	return (open);
6497334Skre }
6507334Skre 
6513617Sroot /*
6524818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
6533617Sroot  */
6544818Swnj ilock(ip)
6554818Swnj 	register struct inode *ip;
6563617Sroot {
6573617Sroot 
6588452Sroot 	ILOCK(ip);
6593617Sroot }
6603617Sroot 
6613617Sroot /*
6624818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
6633617Sroot  */
6647118Smckusick iunlock(ip)
6654818Swnj 	register struct inode *ip;
6663617Sroot {
6673617Sroot 
6688452Sroot 	IUNLOCK(ip);
6693617Sroot }
670