xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 7351)
1*7351Skre /*	lfs_inode.c	4.17	82/07/03	*/
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;
229*7351Skre 			ip->i_rdev = 0;
23024Sbill 			ip->i_flag |= IUPD|ICHG;
2316569Smckusic 			ifree(ip, ip->i_number, mode);
23224Sbill 		}
2331203Sbill 		IUPDAT(ip, &time, &time, 0);
2347118Smckusick 		iunlock(ip);
2357334Skre 		ip->i_flag = 0;
2367334Skre 		/*
2377334Skre 		 * Put the inode on the end of the free list.
2387334Skre 		 * Possibly in some cases it would be better to
2397334Skre 		 * put the inode at the head of the free list,
2407334Skre 		 * (eg: where i_mode == 0 || i_number == 0)
2417334Skre 		 * but I will think about that later .. kre
2427334Skre 		 * (i_number is rarely 0 - only after an i/o error in iget,
2437334Skre 		 * where i_mode == 0, the inode will probably be wanted
2447334Skre 		 * again soon for an ialloc, so possibly we should keep it)
2457334Skre 		 */
2467334Skre 		if (ifreeh) {
2477334Skre 			*ifreet = ip;
2487334Skre 			ip->i_freeb = ifreet;
24924Sbill 		} else {
2507334Skre 			ifreeh = ip;
2517334Skre 			ip->i_freeb = &ifreeh;
25224Sbill 		}
2537334Skre 		ip->i_freef = NULL;
2547334Skre 		ifreet = &ip->i_freef;
2557118Smckusick 	}
25624Sbill 	ip->i_count--;
25724Sbill }
25824Sbill 
25924Sbill /*
26024Sbill  * Check accessed and update flags on
26124Sbill  * an inode structure.
26224Sbill  * If any is on, update the inode
26324Sbill  * with the current time.
2641203Sbill  * If waitfor is given, then must insure
2651203Sbill  * i/o order so wait for write to complete.
26624Sbill  */
2671203Sbill iupdat(ip, ta, tm, waitfor)
2684818Swnj 	register struct inode *ip;
2694818Swnj 	time_t *ta, *tm;
2704818Swnj 	int waitfor;
27124Sbill {
27224Sbill 	register struct buf *bp;
27324Sbill 	struct dinode *dp;
2746569Smckusic 	register struct fs *fp;
27524Sbill 
2766569Smckusic 	fp = ip->i_fs;
2776569Smckusic 	if ((ip->i_flag & (IUPD|IACC|ICHG)) != 0) {
2786569Smckusic 		if (fp->fs_ronly)
27924Sbill 			return;
2806569Smckusic 		bp = bread(ip->i_dev, fsbtodb(fp, itod(fp, ip->i_number)),
2816569Smckusic 			fp->fs_bsize);
28224Sbill 		if (bp->b_flags & B_ERROR) {
28324Sbill 			brelse(bp);
28424Sbill 			return;
28524Sbill 		}
2864818Swnj 		if (ip->i_flag&IACC)
2876569Smckusic 			ip->i_atime = *ta;
2884818Swnj 		if (ip->i_flag&IUPD)
2896569Smckusic 			ip->i_mtime = *tm;
2904818Swnj 		if (ip->i_flag&ICHG)
2916569Smckusic 			ip->i_ctime = time;
29224Sbill 		ip->i_flag &= ~(IUPD|IACC|ICHG);
2937343Skre 		dp = bp->b_un.b_dino + itoo(fp, ip->i_number);
2947343Skre 		dp->di_ic = ip->i_ic;
2951203Sbill 		if (waitfor)
2961203Sbill 			bwrite(bp);
2971203Sbill 		else
2981203Sbill 			bdwrite(bp);
29924Sbill 	}
30024Sbill }
30124Sbill 
30224Sbill /*
30324Sbill  * Free all the disk blocks associated
30424Sbill  * with the specified inode structure.
30524Sbill  * The blocks of the file are removed
30624Sbill  * in reverse order. This FILO
30724Sbill  * algorithm will tend to maintain
30824Sbill  * a contiguous free list much longer
30924Sbill  * than FIFO.
31024Sbill  */
31124Sbill itrunc(ip)
3124818Swnj 	register struct inode *ip;
31324Sbill {
31424Sbill 	register i;
31524Sbill 	dev_t dev;
31624Sbill 	daddr_t bn;
3171203Sbill 	struct inode itmp;
3186569Smckusic 	register struct fs *fs;
31924Sbill 
3201203Sbill 	/*
3211203Sbill 	 * Clean inode on disk before freeing blocks
3221203Sbill 	 * to insure no duplicates if system crashes.
3231203Sbill 	 */
3241203Sbill 	itmp = *ip;
3251203Sbill 	itmp.i_size = 0;
3266569Smckusic 	for (i = 0; i < NDADDR; i++)
3276569Smckusic 		itmp.i_db[i] = 0;
3286569Smckusic 	for (i = 0; i < NIADDR; i++)
3296569Smckusic 		itmp.i_ib[i] = 0;
3301203Sbill 	itmp.i_flag |= ICHG|IUPD;
3311203Sbill 	iupdat(&itmp, &time, &time, 1);
3321203Sbill 	ip->i_flag &= ~(IUPD|IACC|ICHG);
3331203Sbill 
3341203Sbill 	/*
3357341Sroot 	 * Only plain files, directories and symbolic
3367341Sroot 	 * links contain blocks.
3377341Sroot 	 */
3387341Sroot 	i = ip->i_mode & IFMT;
3397341Sroot 	if (i != IFREG && i != IFDIR && i != IFLNK)
3407341Sroot 		return;
3417341Sroot 	/*
3421203Sbill 	 * Now return blocks to free list... if machine
3431203Sbill 	 * crashes, they will be harmless MISSING blocks.
3441203Sbill 	 */
34524Sbill 	dev = ip->i_dev;
3466569Smckusic 	fs = ip->i_fs;
3476569Smckusic 	/*
3486569Smckusic 	 * release double indirect block first
3496569Smckusic 	 */
3506569Smckusic 	bn = ip->i_ib[NIADDR-1];
3516569Smckusic 	if (bn != (daddr_t)0) {
3526569Smckusic 		ip->i_ib[NIADDR - 1] = (daddr_t)0;
3536569Smckusic 		tloop(ip, bn, 1);
3546569Smckusic 	}
3556569Smckusic 	/*
3566569Smckusic 	 * release single indirect blocks second
3576569Smckusic 	 */
3586569Smckusic 	for (i = NIADDR - 2; i >= 0; i--) {
3596569Smckusic 		bn = ip->i_ib[i];
3606569Smckusic 		if (bn != (daddr_t)0) {
3616569Smckusic 			ip->i_ib[i] = (daddr_t)0;
3626569Smckusic 			tloop(ip, bn, 0);
3636569Smckusic 		}
3646569Smckusic 	}
3656569Smckusic 	/*
3666569Smckusic 	 * finally release direct blocks
3676569Smckusic 	 */
3686569Smckusic 	for (i = NDADDR - 1; i>=0; i--) {
3696569Smckusic 		bn = ip->i_db[i];
3704818Swnj 		if (bn == (daddr_t)0)
37124Sbill 			continue;
3726569Smckusic 		ip->i_db[i] = (daddr_t)0;
3736569Smckusic 		fre(ip, bn, (off_t)blksize(fs, ip, i));
37424Sbill 	}
37524Sbill 	ip->i_size = 0;
3761203Sbill 	/*
3771203Sbill 	 * Inode was written and flags updated above.
3781203Sbill 	 * No need to modify flags here.
3791203Sbill 	 */
38024Sbill }
38124Sbill 
3826569Smckusic tloop(ip, bn, indflg)
3836569Smckusic 	register struct inode *ip;
3846569Smckusic 	daddr_t bn;
3856569Smckusic 	int indflg;
38624Sbill {
38724Sbill 	register i;
38824Sbill 	register struct buf *bp;
38924Sbill 	register daddr_t *bap;
3906569Smckusic 	register struct fs *fs;
39124Sbill 	daddr_t nb;
39224Sbill 
39324Sbill 	bp = NULL;
3946569Smckusic 	fs = ip->i_fs;
3956569Smckusic 	for (i = NINDIR(fs) - 1; i >= 0; i--) {
3964818Swnj 		if (bp == NULL) {
3976569Smckusic 			bp = bread(ip->i_dev, fsbtodb(fs, bn), fs->fs_bsize);
39824Sbill 			if (bp->b_flags & B_ERROR) {
39924Sbill 				brelse(bp);
40024Sbill 				return;
40124Sbill 			}
40224Sbill 			bap = bp->b_un.b_daddr;
40324Sbill 		}
40424Sbill 		nb = bap[i];
4054818Swnj 		if (nb == (daddr_t)0)
40624Sbill 			continue;
4076569Smckusic 		if (indflg)
4086569Smckusic 			tloop(ip, nb, 0);
4096569Smckusic 		else
4106569Smckusic 			fre(ip, nb, fs->fs_bsize);
41124Sbill 	}
4124818Swnj 	if (bp != NULL)
41324Sbill 		brelse(bp);
4146569Smckusic 	fre(ip, bn, fs->fs_bsize);
41524Sbill }
41624Sbill 
41724Sbill /*
41824Sbill  * Make a new file.
41924Sbill  */
42024Sbill struct inode *
42124Sbill maknode(mode)
4226569Smckusic 	int mode;
42324Sbill {
42424Sbill 	register struct inode *ip;
4256569Smckusic 	ino_t ipref;
42624Sbill 
4276569Smckusic 	if ((mode & IFMT) == IFDIR)
4286569Smckusic 		ipref = dirpref(u.u_pdir->i_fs);
4296569Smckusic 	else
4306569Smckusic 		ipref = u.u_pdir->i_number;
4316569Smckusic 	ip = ialloc(u.u_pdir, ipref, mode);
4324818Swnj 	if (ip == NULL) {
43324Sbill 		iput(u.u_pdir);
43424Sbill 		return(NULL);
43524Sbill 	}
43624Sbill 	ip->i_flag |= IACC|IUPD|ICHG;
4376569Smckusic 	if ((mode & IFMT) == 0)
43824Sbill 		mode |= IFREG;
43924Sbill 	ip->i_mode = mode & ~u.u_cmask;
44024Sbill 	ip->i_nlink = 1;
44124Sbill 	ip->i_uid = u.u_uid;
4425855Swnj 	ip->i_gid = u.u_pdir->i_gid;
4431203Sbill 
4441203Sbill 	/*
4451203Sbill 	 * Make sure inode goes to disk before directory entry.
4461203Sbill 	 */
4471203Sbill 	iupdat(ip, &time, &time, 1);
44824Sbill 	wdir(ip);
4496569Smckusic 	if (u.u_error) {
4506569Smckusic 		/*
4516569Smckusic 		 * write error occurred trying to update directory
4526569Smckusic 		 * so must deallocate the inode
4536569Smckusic 		 */
4546569Smckusic 		ip->i_nlink = 0;
4556569Smckusic 		ip->i_flag |= ICHG;
4566569Smckusic 		iput(ip);
4576569Smckusic 		return(NULL);
4586569Smckusic 	}
45924Sbill 	return(ip);
46024Sbill }
46124Sbill 
46224Sbill /*
46324Sbill  * Write a directory entry with
46424Sbill  * parameters left as side effects
46524Sbill  * to a call to namei.
46624Sbill  */
46724Sbill wdir(ip)
4684818Swnj 	struct inode *ip;
46924Sbill {
4706569Smckusic 	register struct direct *dp, *ndp;
4716569Smckusic 	struct fs *fs;
4726569Smckusic 	struct buf *bp;
4736569Smckusic 	int lbn, bn, base;
4746569Smckusic 	int loc, dsize, spccnt, newsize;
4756569Smckusic 	char *dirbuf;
47624Sbill 
47724Sbill 	u.u_dent.d_ino = ip->i_number;
47824Sbill 	u.u_segflg = 1;
4796569Smckusic 	newsize = DIRSIZ(&u.u_dent);
4806569Smckusic 	/*
4816569Smckusic 	 * if u.u_count == 0, a new directory block must be allocated.
4826569Smckusic 	 */
4836569Smckusic 	if (u.u_count == 0) {
4846569Smckusic 		u.u_dent.d_reclen = DIRBLKSIZ;
4856569Smckusic 		u.u_count = newsize;
4866569Smckusic 		u.u_base = (caddr_t)&u.u_dent;
4876569Smckusic 		writei(u.u_pdir);
4886569Smckusic 		iput(u.u_pdir);
4896569Smckusic 		return;
4906569Smckusic 	}
4916569Smckusic 	/*
4926569Smckusic 	 * must read in an existing directory block
4936569Smckusic 	 * to prepare to place the new entry into it.
4946569Smckusic 	 */
4956569Smckusic 	fs = u.u_pdir->i_fs;
4966569Smckusic 	lbn = lblkno(fs, u.u_offset);
4976569Smckusic 	base = blkoff(fs, u.u_offset);
4986569Smckusic 	bn = fsbtodb(fs, bmap(u.u_pdir, lbn, B_WRITE, base + u.u_count));
4996569Smckusic 	if (u.u_offset + u.u_count > u.u_pdir->i_size)
5006569Smckusic 		u.u_pdir->i_size = u.u_offset + u.u_count;
5016569Smckusic 	bp = bread(u.u_pdir->i_dev, bn, blksize(fs, u.u_pdir, lbn));
5026569Smckusic 	if (bp->b_flags & B_ERROR) {
5036569Smckusic 		brelse(bp);
5046569Smckusic 		return;
5056569Smckusic 	}
5066569Smckusic 	dirbuf = bp->b_un.b_addr + base;
5076569Smckusic 	dp = (struct direct *)dirbuf;
5086569Smckusic 	dsize = DIRSIZ(dp);
5096569Smckusic 	spccnt = dp->d_reclen - dsize;
5106569Smckusic 	/*
5116569Smckusic 	 * if there is insufficient room to make an entry at this point
5126569Smckusic 	 * namei insures that compacting from u.u_offset for u.u_count
5136569Smckusic 	 * bytes will provide the necessary space.
5146569Smckusic 	 */
5156569Smckusic 	for (loc = dp->d_reclen; loc < u.u_count; ) {
5166569Smckusic 		ndp = (struct direct *)(dirbuf + loc);
5176569Smckusic 		if (dp->d_ino == 0) {
5186569Smckusic 			spccnt += dsize;
5196569Smckusic 		} else {
5206569Smckusic 			dp->d_reclen = dsize;
5216569Smckusic 			dp = (struct direct *)((char *)dp + dsize);
5226569Smckusic 		}
5236569Smckusic 		dsize = DIRSIZ(ndp);
5246569Smckusic 		spccnt += ndp->d_reclen - dsize;
5256569Smckusic 		loc += ndp->d_reclen;
5266569Smckusic 		bcopy(ndp, dp, dsize);
5276569Smckusic 	}
5286569Smckusic 	/*
5296569Smckusic 	 * Update the pointer fields in the previous entry (if any),
5306569Smckusic 	 * copy in the new entry, and write out the block.
5316569Smckusic 	 */
5326569Smckusic 	if (dp->d_ino == 0) {
5336569Smckusic 		if (spccnt + dsize < newsize)
5346569Smckusic 			panic("wdir: compact failed");
5356569Smckusic 		u.u_dent.d_reclen = spccnt + dsize;
5366569Smckusic 	} else {
5376569Smckusic 		if (spccnt < newsize)
5386569Smckusic 			panic("wdir: compact failed");
5396569Smckusic 		u.u_dent.d_reclen = spccnt;
5406569Smckusic 		dp->d_reclen = dsize;
5416569Smckusic 		dp = (struct direct *)((char *)dp + dsize);
5426569Smckusic 	}
5436569Smckusic 	bcopy(&u.u_dent, dp, newsize);
5446569Smckusic 	bwrite(bp);
5456569Smckusic 	u.u_pdir->i_flag |= IUPD|ICHG;
54624Sbill 	iput(u.u_pdir);
54724Sbill }
5483617Sroot 
5497334Skre /*
5507334Skre  * remove any inodes in the inode cache belonging to dev
5517334Skre  *
5527334Skre  * There should not be any active ones, return error if any are found
5537334Skre  * (nb: this is a user error, not a system err)
5547334Skre  *
5557334Skre  * Also, count the references to dev by block devices - this really
5567334Skre  * has nothing to do with the object of the procedure, but as we have
5577334Skre  * to scan the inode table here anyway, we might as well get the
5587334Skre  * extra benefit.
5597334Skre  *
5607334Skre  * this is called from sumount()/sys3.c when dev is being unmounted
5617334Skre  */
5627334Skre iflush(dev)
5637334Skre 	dev_t dev;
5647334Skre {
5657335Skre 	register struct inode *ip;
5667334Skre 	register open = 0;
5677334Skre 
5687334Skre 	for (ip = inode; ip < inodeNINODE; ip++) {
5697334Skre 		if (ip->i_dev == dev)
5707334Skre 			if (ip->i_count)
5717334Skre 				return(-1);
5727334Skre 			else {
5737335Skre 				remque(ip);
5747334Skre 				ip->i_forw = ip;
5757334Skre 				ip->i_back = ip;
5767334Skre 				/*
5777334Skre 				 * as i_count == 0, the inode was on the free
5787334Skre 				 * list already, just leave it there, it will
5797334Skre 				 * fall off the bottom eventually. We could
5807334Skre 				 * perhaps move it to the head of the free
5817334Skre 				 * list, but as umounts are done so
5827334Skre 				 * infrequently, we would gain very little,
5837334Skre 				 * while making the code bigger.
5847334Skre 				 */
5857334Skre 			}
5867334Skre 		else if (ip->i_count && (ip->i_mode&IFMT)==IFBLK &&
5877334Skre 		    ip->i_rdev == dev)
5887334Skre 			open++;
5897334Skre 	}
5907334Skre 	return (open);
5917334Skre }
5927334Skre 
5934818Swnj #ifdef ilock
5944818Swnj #undef ilock
5953617Sroot #endif
5967118Smckusick #ifdef iunlock
5977118Smckusick #undef iunlock
5983617Sroot #endif
5993617Sroot /*
6004818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
6013617Sroot  */
6024818Swnj ilock(ip)
6034818Swnj 	register struct inode *ip;
6043617Sroot {
6053617Sroot 
6064818Swnj 	while (ip->i_flag&ILOCK) {
6073617Sroot 		ip->i_flag |= IWANT;
6083617Sroot 		sleep((caddr_t)ip, PINOD);
6093617Sroot 	}
6103617Sroot 	ip->i_flag |= ILOCK;
6113617Sroot }
6123617Sroot 
6133617Sroot /*
6144818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
6153617Sroot  */
6167118Smckusick iunlock(ip)
6174818Swnj 	register struct inode *ip;
6183617Sroot {
6193617Sroot 
6203617Sroot 	ip->i_flag &= ~ILOCK;
6214818Swnj 	if (ip->i_flag&IWANT) {
6223617Sroot 		ip->i_flag &= ~IWANT;
6233617Sroot 		wakeup((caddr_t)ip);
6243617Sroot 	}
6253617Sroot }
626