xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 8593)
1*8593Sroot /*	lfs_inode.c	4.26	82/10/17	*/
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
2124Sbill #define	INOHASH(dev,ino)	(((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;
1746569Smckusic 	bp = bread(dev, fsbtodb(fs, itod(fs, ino)), 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) {
2397702Ssam 			itrunc(ip, 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
2467492Skre 			chkiq(ip->i_dev, ip, ip->i_uid, 0);
2477492Skre 			dqrele(ip->i_dquot);
2487492Skre 			ip->i_dquot = NODQUOT;
2497492Skre #endif
25024Sbill 		}
2518106Sroot 		IUPDAT(ip, &time.tv_sec, &time.tv_sec, 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;
2874818Swnj 	time_t *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)),
2996569Smckusic 			fp->fs_bsize);
30024Sbill 		if (bp->b_flags & B_ERROR) {
30124Sbill 			brelse(bp);
30224Sbill 			return;
30324Sbill 		}
3044818Swnj 		if (ip->i_flag&IACC)
3056569Smckusic 			ip->i_atime = *ta;
3064818Swnj 		if (ip->i_flag&IUPD)
3076569Smckusic 			ip->i_mtime = *tm;
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 
32024Sbill /*
3217702Ssam  * Truncate the inode ip to at most
3227702Ssam  * length size.  Free affected disk
3237702Ssam  * blocks -- the blocks of the file
3247702Ssam  * are removed in reverse order.
32524Sbill  */
3267702Ssam itrunc(ip, length)
3274818Swnj 	register struct inode *ip;
3287702Ssam 	register int length;
32924Sbill {
33024Sbill 	register i;
33124Sbill 	dev_t dev;
33224Sbill 	daddr_t bn;
3331203Sbill 	struct inode itmp;
3346569Smckusic 	register struct fs *fs;
3357651Ssam #ifdef QUOTA
3367492Skre 	register long cnt = 0;
3377492Skre 	long tloop();
3387492Skre #endif
3397606Ssam 	/*
3407606Ssam 	 * Only plain files, directories and symbolic
3417606Ssam 	 * links contain blocks.
3427606Ssam 	 */
3437606Ssam 	i = ip->i_mode & IFMT;
3447606Ssam 	if (i != IFREG && i != IFDIR && i != IFLNK)
3457606Ssam 		return;
3467702Ssam 	if (ip->i_size <= length)
3477702Ssam 		return;
34824Sbill 
3491203Sbill 	/*
3501203Sbill 	 * Clean inode on disk before freeing blocks
3511203Sbill 	 * to insure no duplicates if system crashes.
3521203Sbill 	 */
3531203Sbill 	itmp = *ip;
3547702Ssam 	itmp.i_size = length;
3556569Smckusic 	for (i = 0; i < NDADDR; i++)
3566569Smckusic 		itmp.i_db[i] = 0;
3576569Smckusic 	for (i = 0; i < NIADDR; i++)
3586569Smckusic 		itmp.i_ib[i] = 0;
3591203Sbill 	itmp.i_flag |= ICHG|IUPD;
3608106Sroot 	iupdat(&itmp, &time.tv_sec, &time.tv_sec, 1);
3611203Sbill 	ip->i_flag &= ~(IUPD|IACC|ICHG);
3621203Sbill 
3631203Sbill 	/*
3641203Sbill 	 * Now return blocks to free list... if machine
3651203Sbill 	 * crashes, they will be harmless MISSING blocks.
3661203Sbill 	 */
3676569Smckusic 	fs = ip->i_fs;
3686569Smckusic 	/*
3696569Smckusic 	 * release double indirect block first
3706569Smckusic 	 */
3716569Smckusic 	bn = ip->i_ib[NIADDR-1];
3726569Smckusic 	if (bn != (daddr_t)0) {
3736569Smckusic 		ip->i_ib[NIADDR - 1] = (daddr_t)0;
3747651Ssam #ifdef QUOTA
3757492Skre 		cnt +=
3767492Skre #endif
3777492Skre 			tloop(ip, bn, 1);
3786569Smckusic 	}
3796569Smckusic 	/*
3806569Smckusic 	 * release single indirect blocks second
3816569Smckusic 	 */
3826569Smckusic 	for (i = NIADDR - 2; i >= 0; i--) {
3836569Smckusic 		bn = ip->i_ib[i];
3846569Smckusic 		if (bn != (daddr_t)0) {
3856569Smckusic 			ip->i_ib[i] = (daddr_t)0;
3867651Ssam #ifdef QUOTA
3877492Skre 			cnt +=
3887492Skre #endif
3897492Skre 				tloop(ip, bn, 0);
3906569Smckusic 		}
3916569Smckusic 	}
3926569Smckusic 	/*
3936569Smckusic 	 * finally release direct blocks
3946569Smckusic 	 */
3956569Smckusic 	for (i = NDADDR - 1; i>=0; i--) {
3967492Skre 		register size;
3977492Skre 
3986569Smckusic 		bn = ip->i_db[i];
3994818Swnj 		if (bn == (daddr_t)0)
40024Sbill 			continue;
4016569Smckusic 		ip->i_db[i] = (daddr_t)0;
402*8593Sroot #ifndef QUOTA
403*8593Sroot 		fre(ip, bn, (off_t)blksize(fs, ip, i));
404*8593Sroot #else
4057492Skre 		fre(ip, bn, size = (off_t)blksize(fs, ip, i));
4067492Skre 		cnt += size / DEV_BSIZE;
4077492Skre #endif
40824Sbill 	}
40924Sbill 	ip->i_size = 0;
4101203Sbill 	/*
4111203Sbill 	 * Inode was written and flags updated above.
4121203Sbill 	 * No need to modify flags here.
4131203Sbill 	 */
4147651Ssam #ifdef QUOTA
4157492Skre 	(void) chkdq(ip, -cnt, 0);
4167492Skre #endif
41724Sbill }
41824Sbill 
4197651Ssam #ifdef QUOTA
4207492Skre long
4217492Skre #endif
4226569Smckusic tloop(ip, bn, indflg)
4236569Smckusic 	register struct inode *ip;
4246569Smckusic 	daddr_t bn;
4256569Smckusic 	int indflg;
42624Sbill {
42724Sbill 	register i;
42824Sbill 	register struct buf *bp;
42924Sbill 	register daddr_t *bap;
4306569Smckusic 	register struct fs *fs;
43124Sbill 	daddr_t nb;
4327651Ssam #ifdef QUOTA
4337492Skre 	register long cnt = 0;
4347492Skre #endif
43524Sbill 
43624Sbill 	bp = NULL;
4376569Smckusic 	fs = ip->i_fs;
4386569Smckusic 	for (i = NINDIR(fs) - 1; i >= 0; i--) {
4394818Swnj 		if (bp == NULL) {
4406569Smckusic 			bp = bread(ip->i_dev, fsbtodb(fs, bn), fs->fs_bsize);
44124Sbill 			if (bp->b_flags & B_ERROR) {
44224Sbill 				brelse(bp);
44324Sbill 				return;
44424Sbill 			}
44524Sbill 			bap = bp->b_un.b_daddr;
44624Sbill 		}
44724Sbill 		nb = bap[i];
4484818Swnj 		if (nb == (daddr_t)0)
44924Sbill 			continue;
4507492Skre 		if (indflg) {
4517651Ssam #ifdef QUOTA
4527492Skre 			cnt +=
4537492Skre #endif
4547492Skre 				tloop(ip, nb, 0);
4557492Skre 		} else {
4566569Smckusic 			fre(ip, nb, fs->fs_bsize);
4577651Ssam #ifdef QUOTA
4587492Skre 			cnt += fs->fs_bsize / DEV_BSIZE;
4597492Skre #endif
4607492Skre 		}
46124Sbill 	}
4624818Swnj 	if (bp != NULL)
46324Sbill 		brelse(bp);
4646569Smckusic 	fre(ip, bn, fs->fs_bsize);
4657651Ssam #ifdef QUOTA
4667492Skre 	cnt += fs->fs_bsize / DEV_BSIZE;
4677492Skre 	return(cnt);
4687492Skre #endif
46924Sbill }
47024Sbill 
47124Sbill /*
4727334Skre  * remove any inodes in the inode cache belonging to dev
4737334Skre  *
4747334Skre  * There should not be any active ones, return error if any are found
4757334Skre  * (nb: this is a user error, not a system err)
4767334Skre  *
4777334Skre  * Also, count the references to dev by block devices - this really
4787334Skre  * has nothing to do with the object of the procedure, but as we have
4797334Skre  * to scan the inode table here anyway, we might as well get the
4807334Skre  * extra benefit.
4817334Skre  *
4827334Skre  * this is called from sumount()/sys3.c when dev is being unmounted
4837334Skre  */
4847651Ssam #ifdef QUOTA
4857504Sroot iflush(dev, iq)
4867492Skre 	dev_t dev;
4877504Sroot 	struct inode *iq;
4887492Skre #else
4897334Skre iflush(dev)
4907334Skre 	dev_t dev;
4917492Skre #endif
4927334Skre {
4937335Skre 	register struct inode *ip;
4947334Skre 	register open = 0;
4957334Skre 
4967334Skre 	for (ip = inode; ip < inodeNINODE; ip++) {
4977651Ssam #ifdef QUOTA
4987492Skre 		if (ip != iq && ip->i_dev == dev)
4997492Skre #else
5007334Skre 		if (ip->i_dev == dev)
5017492Skre #endif
5027334Skre 			if (ip->i_count)
5037334Skre 				return(-1);
5047334Skre 			else {
5057335Skre 				remque(ip);
5067334Skre 				ip->i_forw = ip;
5077334Skre 				ip->i_back = ip;
5087334Skre 				/*
5097334Skre 				 * as i_count == 0, the inode was on the free
5107334Skre 				 * list already, just leave it there, it will
5117334Skre 				 * fall off the bottom eventually. We could
5127334Skre 				 * perhaps move it to the head of the free
5137334Skre 				 * list, but as umounts are done so
5147334Skre 				 * infrequently, we would gain very little,
5157334Skre 				 * while making the code bigger.
5167334Skre 				 */
5177651Ssam #ifdef QUOTA
5187492Skre 				dqrele(ip->i_dquot);
5197492Skre 				ip->i_dquot = NODQUOT;
5207492Skre #endif
5217334Skre 			}
5227334Skre 		else if (ip->i_count && (ip->i_mode&IFMT)==IFBLK &&
5237334Skre 		    ip->i_rdev == dev)
5247334Skre 			open++;
5257334Skre 	}
5267334Skre 	return (open);
5277334Skre }
5287334Skre 
5293617Sroot /*
5304818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
5313617Sroot  */
5324818Swnj ilock(ip)
5334818Swnj 	register struct inode *ip;
5343617Sroot {
5353617Sroot 
5368452Sroot 	ILOCK(ip);
5373617Sroot }
5383617Sroot 
5393617Sroot /*
5404818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
5413617Sroot  */
5427118Smckusick iunlock(ip)
5434818Swnj 	register struct inode *ip;
5443617Sroot {
5453617Sroot 
5468452Sroot 	IUNLOCK(ip);
5473617Sroot }
548