xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 7341)
1*7341Sroot /*	lfs_inode.c	4.15	82/07/01	*/
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"
1224Sbill #include "../h/inline.h"
1324Sbill 
1424Sbill #define	INOHSZ	63
157334Skre #if	((INOHSZ&(INOHSZ-1)) == 0)
167334Skre #define	INOHASH(dev,ino)	(((dev)+(ino))&(INOHSZ-1))
177334Skre #else
1824Sbill #define	INOHASH(dev,ino)	(((dev)+(ino))%INOHSZ)
197334Skre #endif
2024Sbill 
217334Skre union ihead {				/* inode LRU cache, Chris Maltby */
227334Skre 	union  ihead *ih_head[2];
237334Skre 	struct inode *ih_chain[2];
247334Skre } ihead[INOHSZ];
257334Skre 
267334Skre struct inode *ifreeh, **ifreet;
277334Skre 
2824Sbill /*
2924Sbill  * Initialize hash links for inodes
3024Sbill  * and build inode free list.
3124Sbill  */
3224Sbill ihinit()
3324Sbill {
3424Sbill 	register int i;
352737Swnj 	register struct inode *ip = inode;
367334Skre 	register union  ihead *ih = ihead;
3724Sbill 
387334Skre 	for (i = INOHSZ; --i >= 0; ih++) {
397334Skre 		ih->ih_head[0] = ih;
407334Skre 		ih->ih_head[1] = ih;
417334Skre 	}
427334Skre 	ifreeh = ip;
437334Skre 	ifreet = &ip->i_freef;
447334Skre 	ip->i_freeb = &ifreeh;
457334Skre 	ip->i_forw = ip;
467334Skre 	ip->i_back = ip;
477334Skre 	for (i = ninode; --i > 0; ) {
487334Skre 		++ip;
497334Skre 		ip->i_forw = ip;
507334Skre 		ip->i_back = ip;
517334Skre 		*ifreet = ip;
527334Skre 		ip->i_freeb = ifreet;
537334Skre 		ifreet = &ip->i_freef;
547334Skre 	}
557334Skre 	ip->i_freef = NULL;
5624Sbill }
5724Sbill 
587334Skre #ifdef notdef
5924Sbill /*
607334Skre  * Find an inode if it is incore.
617334Skre  * This is the equivalent, for inodes,
627334Skre  * of ``incore'' in bio.c or ``pfind'' in subr.c.
637334Skre  */
647334Skre struct inode *
657334Skre ifind(dev, ino)
667334Skre 	dev_t dev;
677334Skre 	ino_t ino;
687334Skre {
697334Skre 	register struct inode *ip;
707334Skre 	register union  ihead *ih;
717334Skre 
727334Skre 	ih = &ihead[INOHASH(dev, ino)];
737334Skre 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw)
747334Skre 		if (ino==ip->i_number && dev==ip->i_dev)
757334Skre 			return (ip);
767334Skre 	return ((struct inode *)0);
777334Skre }
787334Skre #endif notdef
797334Skre 
807334Skre /*
8124Sbill  * Look up an inode by device,inumber.
8224Sbill  * If it is in core (in the inode structure),
8324Sbill  * honor the locking protocol.
8424Sbill  * If it is not in core, read it in from the
8524Sbill  * specified device.
8624Sbill  * If the inode is mounted on, perform
8724Sbill  * the indicated indirection.
8824Sbill  * In all cases, a pointer to a locked
8924Sbill  * inode structure is returned.
9024Sbill  *
9124Sbill  * panic: no imt -- if the mounted file
9224Sbill  *	system is not in the mount table.
9324Sbill  *	"cannot happen"
9424Sbill  */
9524Sbill struct inode *
966569Smckusic iget(dev, fs, ino)
974818Swnj 	dev_t dev;
986569Smckusic 	register struct fs *fs;
994818Swnj 	ino_t ino;
10024Sbill {
1017335Skre 	register struct inode *ip;
1027335Skre 	register union  ihead *ih;
10324Sbill 	register struct mount *mp;
10424Sbill 	register struct buf *bp;
10524Sbill 	register struct dinode *dp;
1067334Skre 	register struct inode *iq;
10724Sbill 
10824Sbill loop:
1096569Smckusic 	if (getfs(dev) != fs)
1106569Smckusic 		panic("iget: bad fs");
1117334Skre 	ih = &ihead[INOHASH(dev, ino)];
1127334Skre 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw)
1134818Swnj 		if (ino == ip->i_number && dev == ip->i_dev) {
1144818Swnj 			if ((ip->i_flag&ILOCK) != 0) {
11524Sbill 				ip->i_flag |= IWANT;
11624Sbill 				sleep((caddr_t)ip, PINOD);
11724Sbill 				goto loop;
11824Sbill 			}
1194818Swnj 			if ((ip->i_flag&IMOUNT) != 0) {
1206569Smckusic 				for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++)
1217334Skre 					if(mp->m_inodp == ip) {
1227334Skre 						dev = mp->m_dev;
1237334Skre 						fs = mp->m_bufp->b_un.b_fs;
1247334Skre 						ino = ROOTINO;
1257334Skre 						goto loop;
1267334Skre 					}
12724Sbill 				panic("no imt");
12824Sbill 			}
1297334Skre 			if (ip->i_count == 0) {		/* ino on free list */
1307334Skre 				if (iq = ip->i_freef)
1317334Skre 					iq->i_freeb = ip->i_freeb;
1327334Skre 				else
1337334Skre 					ifreet = ip->i_freeb;
1347334Skre 				*ip->i_freeb = iq;
1357334Skre 				ip->i_freef = NULL;
1367334Skre 				ip->i_freeb = NULL;
1377334Skre 			}
13824Sbill 			ip->i_count++;
13924Sbill 			ip->i_flag |= ILOCK;
14024Sbill 			return(ip);
14124Sbill 		}
1427334Skre 
1437334Skre 	if ((ip = ifreeh) == NULL) {
1442933Swnj 		tablefull("inode");
14524Sbill 		u.u_error = ENFILE;
14624Sbill 		return(NULL);
14724Sbill 	}
1487334Skre 	if (iq = ip->i_freef)
1497334Skre 		iq->i_freeb = &ifreeh;
1507334Skre 	ifreeh = iq;
1517334Skre 	ip->i_freef = NULL;
1527334Skre 	ip->i_freeb = NULL;
1537334Skre 	/*
1547334Skre 	 * Now to take inode off the hash chain it was on
1557334Skre 	 * (initially, or after an iflush, it is on a "hash chain"
1567334Skre 	 * consisting entirely of itself, and pointed to by no-one,
1577334Skre 	 * but that doesn't matter), and put it on the chain for
1587334Skre 	 * its new (ino, dev) pair
1597334Skre 	 */
1607335Skre 	remque(ip);
1617335Skre 	insque(ip, ih);
16224Sbill 	ip->i_dev = dev;
1636569Smckusic 	ip->i_fs = fs;
16424Sbill 	ip->i_number = ino;
16524Sbill 	ip->i_flag = ILOCK;
16624Sbill 	ip->i_count++;
1676569Smckusic 	ip->i_lastr = 0;
1686569Smckusic 	bp = bread(dev, fsbtodb(fs, itod(fs, ino)), fs->fs_bsize);
16924Sbill 	/*
17024Sbill 	 * Check I/O errors
17124Sbill 	 */
1724818Swnj 	if ((bp->b_flags&B_ERROR) != 0) {
17324Sbill 		brelse(bp);
1747334Skre 		/*
1757334Skre 		 * the inode doesn't contain anything useful, so it would
1767334Skre 		 * be misleading to leave it on its hash chain.
1777334Skre 		 * 'iput' will take care of putting it back on the free list.
1787334Skre 		 */
1797335Skre 		remque(ip);
1807334Skre 		ip->i_forw = ip;
1817334Skre 		ip->i_back = ip;
1827334Skre 		/*
1837334Skre 		 * we also loose its inumber, just in case (as iput
1847334Skre 		 * doesn't do that any more) - but as it isn't on its
1857334Skre 		 * hash chain, I doubt if this is really necessary .. kre
1867334Skre 		 * (probably the two methods are interchangable)
1877334Skre 		 */
1887334Skre 		ip->i_number = 0;
18924Sbill 		iput(ip);
19024Sbill 		return(NULL);
19124Sbill 	}
19224Sbill 	dp = bp->b_un.b_dino;
1936569Smckusic 	dp += itoo(fs, ino);
1946569Smckusic 	ip->i_ic = dp->di_ic;
19524Sbill 	brelse(bp);
1966569Smckusic 	return (ip);
19724Sbill }
19824Sbill 
19924Sbill /*
20024Sbill  * Decrement reference count of
20124Sbill  * an inode structure.
20224Sbill  * On the last reference,
20324Sbill  * write the inode out and if necessary,
20424Sbill  * truncate and deallocate the file.
20524Sbill  */
20624Sbill iput(ip)
2074818Swnj 	register struct inode *ip;
20824Sbill {
2097118Smckusick 
2107118Smckusick 	if ((ip->i_flag & ILOCK) == 0)
2117118Smckusick 		panic("iput");
2127118Smckusick 	iunlock(ip);
2137118Smckusick 	irele(ip);
2147118Smckusick }
2157118Smckusick 
2167118Smckusick irele(ip)
2177118Smckusick 	register struct inode *ip;
2187118Smckusick {
21924Sbill 	register int i, x;
22024Sbill 	register struct inode *jp;
2216569Smckusic 	int mode;
22224Sbill 
2234818Swnj 	if (ip->i_count == 1) {
22424Sbill 		ip->i_flag |= ILOCK;
2254818Swnj 		if (ip->i_nlink <= 0) {
22624Sbill 			itrunc(ip);
2276569Smckusic 			mode = ip->i_mode;
22824Sbill 			ip->i_mode = 0;
22924Sbill 			ip->i_flag |= IUPD|ICHG;
2306569Smckusic 			ifree(ip, ip->i_number, mode);
23124Sbill 		}
2321203Sbill 		IUPDAT(ip, &time, &time, 0);
2337118Smckusick 		iunlock(ip);
2347334Skre 		ip->i_flag = 0;
2357334Skre 		/*
2367334Skre 		 * Put the inode on the end of the free list.
2377334Skre 		 * Possibly in some cases it would be better to
2387334Skre 		 * put the inode at the head of the free list,
2397334Skre 		 * (eg: where i_mode == 0 || i_number == 0)
2407334Skre 		 * but I will think about that later .. kre
2417334Skre 		 * (i_number is rarely 0 - only after an i/o error in iget,
2427334Skre 		 * where i_mode == 0, the inode will probably be wanted
2437334Skre 		 * again soon for an ialloc, so possibly we should keep it)
2447334Skre 		 */
2457334Skre 		if (ifreeh) {
2467334Skre 			*ifreet = ip;
2477334Skre 			ip->i_freeb = ifreet;
24824Sbill 		} else {
2497334Skre 			ifreeh = ip;
2507334Skre 			ip->i_freeb = &ifreeh;
25124Sbill 		}
2527334Skre 		ip->i_freef = NULL;
2537334Skre 		ifreet = &ip->i_freef;
2547118Smckusick 	}
25524Sbill 	ip->i_count--;
25624Sbill }
25724Sbill 
25824Sbill /*
25924Sbill  * Check accessed and update flags on
26024Sbill  * an inode structure.
26124Sbill  * If any is on, update the inode
26224Sbill  * with the current time.
2631203Sbill  * If waitfor is given, then must insure
2641203Sbill  * i/o order so wait for write to complete.
26524Sbill  */
2661203Sbill iupdat(ip, ta, tm, waitfor)
2674818Swnj 	register struct inode *ip;
2684818Swnj 	time_t *ta, *tm;
2694818Swnj 	int waitfor;
27024Sbill {
27124Sbill 	register struct buf *bp;
27224Sbill 	struct dinode *dp;
2736569Smckusic 	register struct fs *fp;
27424Sbill 
2756569Smckusic 	fp = ip->i_fs;
2766569Smckusic 	if ((ip->i_flag & (IUPD|IACC|ICHG)) != 0) {
2776569Smckusic 		if (fp->fs_ronly)
27824Sbill 			return;
2796569Smckusic 		bp = bread(ip->i_dev, fsbtodb(fp, itod(fp, ip->i_number)),
2806569Smckusic 			fp->fs_bsize);
28124Sbill 		if (bp->b_flags & B_ERROR) {
28224Sbill 			brelse(bp);
28324Sbill 			return;
28424Sbill 		}
2854818Swnj 		if (ip->i_flag&IACC)
2866569Smckusic 			ip->i_atime = *ta;
2874818Swnj 		if (ip->i_flag&IUPD)
2886569Smckusic 			ip->i_mtime = *tm;
2894818Swnj 		if (ip->i_flag&ICHG)
2906569Smckusic 			ip->i_ctime = time;
29124Sbill 		ip->i_flag &= ~(IUPD|IACC|ICHG);
2921203Sbill 		if (waitfor)
2931203Sbill 			bwrite(bp);
2941203Sbill 		else
2951203Sbill 			bdwrite(bp);
29624Sbill 	}
29724Sbill }
29824Sbill 
29924Sbill /*
30024Sbill  * Free all the disk blocks associated
30124Sbill  * with the specified inode structure.
30224Sbill  * The blocks of the file are removed
30324Sbill  * in reverse order. This FILO
30424Sbill  * algorithm will tend to maintain
30524Sbill  * a contiguous free list much longer
30624Sbill  * than FIFO.
30724Sbill  */
30824Sbill itrunc(ip)
3094818Swnj 	register struct inode *ip;
31024Sbill {
31124Sbill 	register i;
31224Sbill 	dev_t dev;
31324Sbill 	daddr_t bn;
3141203Sbill 	struct inode itmp;
3156569Smckusic 	register struct fs *fs;
31624Sbill 
3171203Sbill 	/*
3181203Sbill 	 * Clean inode on disk before freeing blocks
3191203Sbill 	 * to insure no duplicates if system crashes.
3201203Sbill 	 */
3211203Sbill 	itmp = *ip;
3221203Sbill 	itmp.i_size = 0;
3236569Smckusic 	for (i = 0; i < NDADDR; i++)
3246569Smckusic 		itmp.i_db[i] = 0;
3256569Smckusic 	for (i = 0; i < NIADDR; i++)
3266569Smckusic 		itmp.i_ib[i] = 0;
3271203Sbill 	itmp.i_flag |= ICHG|IUPD;
3281203Sbill 	iupdat(&itmp, &time, &time, 1);
3291203Sbill 	ip->i_flag &= ~(IUPD|IACC|ICHG);
3301203Sbill 
3311203Sbill 	/*
332*7341Sroot 	 * Only plain files, directories and symbolic
333*7341Sroot 	 * links contain blocks.
334*7341Sroot 	 */
335*7341Sroot 	i = ip->i_mode & IFMT;
336*7341Sroot 	if (i != IFREG && i != IFDIR && i != IFLNK)
337*7341Sroot 		return;
338*7341Sroot 	/*
3391203Sbill 	 * Now return blocks to free list... if machine
3401203Sbill 	 * crashes, they will be harmless MISSING blocks.
3411203Sbill 	 */
34224Sbill 	dev = ip->i_dev;
3436569Smckusic 	fs = ip->i_fs;
3446569Smckusic 	/*
3456569Smckusic 	 * release double indirect block first
3466569Smckusic 	 */
3476569Smckusic 	bn = ip->i_ib[NIADDR-1];
3486569Smckusic 	if (bn != (daddr_t)0) {
3496569Smckusic 		ip->i_ib[NIADDR - 1] = (daddr_t)0;
3506569Smckusic 		tloop(ip, bn, 1);
3516569Smckusic 	}
3526569Smckusic 	/*
3536569Smckusic 	 * release single indirect blocks second
3546569Smckusic 	 */
3556569Smckusic 	for (i = NIADDR - 2; i >= 0; i--) {
3566569Smckusic 		bn = ip->i_ib[i];
3576569Smckusic 		if (bn != (daddr_t)0) {
3586569Smckusic 			ip->i_ib[i] = (daddr_t)0;
3596569Smckusic 			tloop(ip, bn, 0);
3606569Smckusic 		}
3616569Smckusic 	}
3626569Smckusic 	/*
3636569Smckusic 	 * finally release direct blocks
3646569Smckusic 	 */
3656569Smckusic 	for (i = NDADDR - 1; i>=0; i--) {
3666569Smckusic 		bn = ip->i_db[i];
3674818Swnj 		if (bn == (daddr_t)0)
36824Sbill 			continue;
3696569Smckusic 		ip->i_db[i] = (daddr_t)0;
3706569Smckusic 		fre(ip, bn, (off_t)blksize(fs, ip, i));
37124Sbill 	}
37224Sbill 	ip->i_size = 0;
3731203Sbill 	/*
3741203Sbill 	 * Inode was written and flags updated above.
3751203Sbill 	 * No need to modify flags here.
3761203Sbill 	 */
37724Sbill }
37824Sbill 
3796569Smckusic tloop(ip, bn, indflg)
3806569Smckusic 	register struct inode *ip;
3816569Smckusic 	daddr_t bn;
3826569Smckusic 	int indflg;
38324Sbill {
38424Sbill 	register i;
38524Sbill 	register struct buf *bp;
38624Sbill 	register daddr_t *bap;
3876569Smckusic 	register struct fs *fs;
38824Sbill 	daddr_t nb;
38924Sbill 
39024Sbill 	bp = NULL;
3916569Smckusic 	fs = ip->i_fs;
3926569Smckusic 	for (i = NINDIR(fs) - 1; i >= 0; i--) {
3934818Swnj 		if (bp == NULL) {
3946569Smckusic 			bp = bread(ip->i_dev, fsbtodb(fs, bn), fs->fs_bsize);
39524Sbill 			if (bp->b_flags & B_ERROR) {
39624Sbill 				brelse(bp);
39724Sbill 				return;
39824Sbill 			}
39924Sbill 			bap = bp->b_un.b_daddr;
40024Sbill 		}
40124Sbill 		nb = bap[i];
4024818Swnj 		if (nb == (daddr_t)0)
40324Sbill 			continue;
4046569Smckusic 		if (indflg)
4056569Smckusic 			tloop(ip, nb, 0);
4066569Smckusic 		else
4076569Smckusic 			fre(ip, nb, fs->fs_bsize);
40824Sbill 	}
4094818Swnj 	if (bp != NULL)
41024Sbill 		brelse(bp);
4116569Smckusic 	fre(ip, bn, fs->fs_bsize);
41224Sbill }
41324Sbill 
41424Sbill /*
41524Sbill  * Make a new file.
41624Sbill  */
41724Sbill struct inode *
41824Sbill maknode(mode)
4196569Smckusic 	int mode;
42024Sbill {
42124Sbill 	register struct inode *ip;
4226569Smckusic 	ino_t ipref;
42324Sbill 
4246569Smckusic 	if ((mode & IFMT) == IFDIR)
4256569Smckusic 		ipref = dirpref(u.u_pdir->i_fs);
4266569Smckusic 	else
4276569Smckusic 		ipref = u.u_pdir->i_number;
4286569Smckusic 	ip = ialloc(u.u_pdir, ipref, mode);
4294818Swnj 	if (ip == NULL) {
43024Sbill 		iput(u.u_pdir);
43124Sbill 		return(NULL);
43224Sbill 	}
43324Sbill 	ip->i_flag |= IACC|IUPD|ICHG;
4346569Smckusic 	if ((mode & IFMT) == 0)
43524Sbill 		mode |= IFREG;
43624Sbill 	ip->i_mode = mode & ~u.u_cmask;
43724Sbill 	ip->i_nlink = 1;
43824Sbill 	ip->i_uid = u.u_uid;
4395855Swnj 	ip->i_gid = u.u_pdir->i_gid;
4401203Sbill 
4411203Sbill 	/*
4421203Sbill 	 * Make sure inode goes to disk before directory entry.
4431203Sbill 	 */
4441203Sbill 	iupdat(ip, &time, &time, 1);
44524Sbill 	wdir(ip);
4466569Smckusic 	if (u.u_error) {
4476569Smckusic 		/*
4486569Smckusic 		 * write error occurred trying to update directory
4496569Smckusic 		 * so must deallocate the inode
4506569Smckusic 		 */
4516569Smckusic 		ip->i_nlink = 0;
4526569Smckusic 		ip->i_flag |= ICHG;
4536569Smckusic 		iput(ip);
4546569Smckusic 		return(NULL);
4556569Smckusic 	}
45624Sbill 	return(ip);
45724Sbill }
45824Sbill 
45924Sbill /*
46024Sbill  * Write a directory entry with
46124Sbill  * parameters left as side effects
46224Sbill  * to a call to namei.
46324Sbill  */
46424Sbill wdir(ip)
4654818Swnj 	struct inode *ip;
46624Sbill {
4676569Smckusic 	register struct direct *dp, *ndp;
4686569Smckusic 	struct fs *fs;
4696569Smckusic 	struct buf *bp;
4706569Smckusic 	int lbn, bn, base;
4716569Smckusic 	int loc, dsize, spccnt, newsize;
4726569Smckusic 	char *dirbuf;
47324Sbill 
47424Sbill 	u.u_dent.d_ino = ip->i_number;
47524Sbill 	u.u_segflg = 1;
4766569Smckusic 	newsize = DIRSIZ(&u.u_dent);
4776569Smckusic 	/*
4786569Smckusic 	 * if u.u_count == 0, a new directory block must be allocated.
4796569Smckusic 	 */
4806569Smckusic 	if (u.u_count == 0) {
4816569Smckusic 		u.u_dent.d_reclen = DIRBLKSIZ;
4826569Smckusic 		u.u_count = newsize;
4836569Smckusic 		u.u_base = (caddr_t)&u.u_dent;
4846569Smckusic 		writei(u.u_pdir);
4856569Smckusic 		iput(u.u_pdir);
4866569Smckusic 		return;
4876569Smckusic 	}
4886569Smckusic 	/*
4896569Smckusic 	 * must read in an existing directory block
4906569Smckusic 	 * to prepare to place the new entry into it.
4916569Smckusic 	 */
4926569Smckusic 	fs = u.u_pdir->i_fs;
4936569Smckusic 	lbn = lblkno(fs, u.u_offset);
4946569Smckusic 	base = blkoff(fs, u.u_offset);
4956569Smckusic 	bn = fsbtodb(fs, bmap(u.u_pdir, lbn, B_WRITE, base + u.u_count));
4966569Smckusic 	if (u.u_offset + u.u_count > u.u_pdir->i_size)
4976569Smckusic 		u.u_pdir->i_size = u.u_offset + u.u_count;
4986569Smckusic 	bp = bread(u.u_pdir->i_dev, bn, blksize(fs, u.u_pdir, lbn));
4996569Smckusic 	if (bp->b_flags & B_ERROR) {
5006569Smckusic 		brelse(bp);
5016569Smckusic 		return;
5026569Smckusic 	}
5036569Smckusic 	dirbuf = bp->b_un.b_addr + base;
5046569Smckusic 	dp = (struct direct *)dirbuf;
5056569Smckusic 	dsize = DIRSIZ(dp);
5066569Smckusic 	spccnt = dp->d_reclen - dsize;
5076569Smckusic 	/*
5086569Smckusic 	 * if there is insufficient room to make an entry at this point
5096569Smckusic 	 * namei insures that compacting from u.u_offset for u.u_count
5106569Smckusic 	 * bytes will provide the necessary space.
5116569Smckusic 	 */
5126569Smckusic 	for (loc = dp->d_reclen; loc < u.u_count; ) {
5136569Smckusic 		ndp = (struct direct *)(dirbuf + loc);
5146569Smckusic 		if (dp->d_ino == 0) {
5156569Smckusic 			spccnt += dsize;
5166569Smckusic 		} else {
5176569Smckusic 			dp->d_reclen = dsize;
5186569Smckusic 			dp = (struct direct *)((char *)dp + dsize);
5196569Smckusic 		}
5206569Smckusic 		dsize = DIRSIZ(ndp);
5216569Smckusic 		spccnt += ndp->d_reclen - dsize;
5226569Smckusic 		loc += ndp->d_reclen;
5236569Smckusic 		bcopy(ndp, dp, dsize);
5246569Smckusic 	}
5256569Smckusic 	/*
5266569Smckusic 	 * Update the pointer fields in the previous entry (if any),
5276569Smckusic 	 * copy in the new entry, and write out the block.
5286569Smckusic 	 */
5296569Smckusic 	if (dp->d_ino == 0) {
5306569Smckusic 		if (spccnt + dsize < newsize)
5316569Smckusic 			panic("wdir: compact failed");
5326569Smckusic 		u.u_dent.d_reclen = spccnt + dsize;
5336569Smckusic 	} else {
5346569Smckusic 		if (spccnt < newsize)
5356569Smckusic 			panic("wdir: compact failed");
5366569Smckusic 		u.u_dent.d_reclen = spccnt;
5376569Smckusic 		dp->d_reclen = dsize;
5386569Smckusic 		dp = (struct direct *)((char *)dp + dsize);
5396569Smckusic 	}
5406569Smckusic 	bcopy(&u.u_dent, dp, newsize);
5416569Smckusic 	bwrite(bp);
5426569Smckusic 	u.u_pdir->i_flag |= IUPD|ICHG;
54324Sbill 	iput(u.u_pdir);
54424Sbill }
5453617Sroot 
5467334Skre /*
5477334Skre  * remove any inodes in the inode cache belonging to dev
5487334Skre  *
5497334Skre  * There should not be any active ones, return error if any are found
5507334Skre  * (nb: this is a user error, not a system err)
5517334Skre  *
5527334Skre  * Also, count the references to dev by block devices - this really
5537334Skre  * has nothing to do with the object of the procedure, but as we have
5547334Skre  * to scan the inode table here anyway, we might as well get the
5557334Skre  * extra benefit.
5567334Skre  *
5577334Skre  * this is called from sumount()/sys3.c when dev is being unmounted
5587334Skre  */
5597334Skre iflush(dev)
5607334Skre 	dev_t dev;
5617334Skre {
5627335Skre 	register struct inode *ip;
5637334Skre 	register open = 0;
5647334Skre 
5657334Skre 	for (ip = inode; ip < inodeNINODE; ip++) {
5667334Skre 		if (ip->i_dev == dev)
5677334Skre 			if (ip->i_count)
5687334Skre 				return(-1);
5697334Skre 			else {
5707335Skre 				remque(ip);
5717334Skre 				ip->i_forw = ip;
5727334Skre 				ip->i_back = ip;
5737334Skre 				/*
5747334Skre 				 * as i_count == 0, the inode was on the free
5757334Skre 				 * list already, just leave it there, it will
5767334Skre 				 * fall off the bottom eventually. We could
5777334Skre 				 * perhaps move it to the head of the free
5787334Skre 				 * list, but as umounts are done so
5797334Skre 				 * infrequently, we would gain very little,
5807334Skre 				 * while making the code bigger.
5817334Skre 				 */
5827334Skre 			}
5837334Skre 		else if (ip->i_count && (ip->i_mode&IFMT)==IFBLK &&
5847334Skre 		    ip->i_rdev == dev)
5857334Skre 			open++;
5867334Skre 	}
5877334Skre 	return (open);
5887334Skre }
5897334Skre 
5904818Swnj #ifdef ilock
5914818Swnj #undef ilock
5923617Sroot #endif
5937118Smckusick #ifdef iunlock
5947118Smckusick #undef iunlock
5953617Sroot #endif
5963617Sroot /*
5974818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
5983617Sroot  */
5994818Swnj ilock(ip)
6004818Swnj 	register struct inode *ip;
6013617Sroot {
6023617Sroot 
6034818Swnj 	while (ip->i_flag&ILOCK) {
6043617Sroot 		ip->i_flag |= IWANT;
6053617Sroot 		sleep((caddr_t)ip, PINOD);
6063617Sroot 	}
6073617Sroot 	ip->i_flag |= ILOCK;
6083617Sroot }
6093617Sroot 
6103617Sroot /*
6114818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
6123617Sroot  */
6137118Smckusick iunlock(ip)
6144818Swnj 	register struct inode *ip;
6153617Sroot {
6163617Sroot 
6173617Sroot 	ip->i_flag &= ~ILOCK;
6184818Swnj 	if (ip->i_flag&IWANT) {
6193617Sroot 		ip->i_flag &= ~IWANT;
6203617Sroot 		wakeup((caddr_t)ip);
6213617Sroot 	}
6223617Sroot }
623