xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 38226)
123399Smckusick /*
237736Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337736Smckusick  * All rights reserved.
423399Smckusick  *
537736Smckusick  * Redistribution and use in source and binary forms are permitted
637736Smckusick  * provided that the above copyright notice and this paragraph are
737736Smckusick  * duplicated in all such forms and that any documentation,
837736Smckusick  * advertising materials, and other materials related to such
937736Smckusick  * distribution and use acknowledge that the software was developed
1037736Smckusick  * by the University of California, Berkeley.  The name of the
1137736Smckusick  * University may not be used to endorse or promote products derived
1237736Smckusick  * from this software without specific prior written permission.
1337736Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437736Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537736Smckusick  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637736Smckusick  *
17*38226Smckusick  *	@(#)lfs_inode.c	7.7 (Berkeley) 06/06/89
1823399Smckusick  */
1924Sbill 
2017099Sbloom #include "param.h"
2117099Sbloom #include "systm.h"
2217099Sbloom #include "mount.h"
2317099Sbloom #include "user.h"
2437736Smckusick #include "file.h"
2517099Sbloom #include "buf.h"
2624525Sbloom #include "cmap.h"
2737736Smckusick #include "vnode.h"
2837736Smckusick #include "../ufs/inode.h"
2937736Smckusick #include "../ufs/fs.h"
3037736Smckusick #include "../ufs/ufsmount.h"
317651Ssam #ifdef QUOTA
3237736Smckusick #include "../ufs/quota.h"
337504Sroot #endif
3417099Sbloom #include "kernel.h"
3531661Smckusick #include "malloc.h"
3624Sbill 
3716840Smckusick #define	INOHSZ	512
387334Skre #if	((INOHSZ&(INOHSZ-1)) == 0)
397334Skre #define	INOHASH(dev,ino)	(((dev)+(ino))&(INOHSZ-1))
407334Skre #else
4110852Ssam #define	INOHASH(dev,ino)	(((unsigned)((dev)+(ino)))%INOHSZ)
427334Skre #endif
4324Sbill 
4437736Smckusick #define INSFREE(ip) {\
4537736Smckusick 	if (ifreeh) { \
4637736Smckusick 		*ifreet = (ip); \
4737736Smckusick 		(ip)->i_freeb = ifreet; \
4837736Smckusick 	} else { \
4937736Smckusick 		ifreeh = (ip); \
5037736Smckusick 		(ip)->i_freeb = &ifreeh; \
5137736Smckusick 	} \
5237736Smckusick 	(ip)->i_freef = NULL; \
5337736Smckusick 	ifreet = &(ip)->i_freef; \
5437736Smckusick }
5537736Smckusick 
567334Skre union ihead {				/* inode LRU cache, Chris Maltby */
577334Skre 	union  ihead *ih_head[2];
587334Skre 	struct inode *ih_chain[2];
597334Skre } ihead[INOHSZ];
607334Skre 
6137736Smckusick struct inode *ifreeh, **ifreet, *bdevlisth;
627334Skre 
6324Sbill /*
6424Sbill  * Initialize hash links for inodes
6524Sbill  * and build inode free list.
6624Sbill  */
6724Sbill ihinit()
6824Sbill {
6924Sbill 	register int i;
702737Swnj 	register struct inode *ip = inode;
717334Skre 	register union  ihead *ih = ihead;
7224Sbill 
737334Skre 	for (i = INOHSZ; --i >= 0; ih++) {
747334Skre 		ih->ih_head[0] = ih;
757334Skre 		ih->ih_head[1] = ih;
767334Skre 	}
777334Skre 	ifreeh = ip;
787334Skre 	ifreet = &ip->i_freef;
797334Skre 	ip->i_freeb = &ifreeh;
807334Skre 	ip->i_forw = ip;
817334Skre 	ip->i_back = ip;
8237736Smckusick 	ITOV(ip)->v_data = (qaddr_t)ip;
837334Skre 	for (i = ninode; --i > 0; ) {
847334Skre 		++ip;
857334Skre 		ip->i_forw = ip;
867334Skre 		ip->i_back = ip;
8737736Smckusick 		ITOV(ip)->v_data = (qaddr_t)ip;
887334Skre 		*ifreet = ip;
897334Skre 		ip->i_freeb = ifreet;
907334Skre 		ifreet = &ip->i_freef;
917334Skre 	}
927334Skre 	ip->i_freef = NULL;
9324Sbill }
9424Sbill 
9524Sbill /*
9637736Smckusick  * Look up an vnode/inode by device,inumber.
9724Sbill  * If it is in core (in the inode structure),
9824Sbill  * honor the locking protocol.
9924Sbill  * If it is not in core, read it in from the
10024Sbill  * specified device.
10137736Smckusick  * Callers must check for mount points!!
10224Sbill  * In all cases, a pointer to a locked
10324Sbill  * inode structure is returned.
10424Sbill  */
10537736Smckusick iget(xp, ino, ipp)
10637736Smckusick 	struct inode *xp;
1074818Swnj 	ino_t ino;
10837736Smckusick 	struct inode **ipp;
10924Sbill {
11037736Smckusick 	dev_t dev = xp->i_dev;
11137736Smckusick 	struct mount *mntp = ITOV(xp)->v_mount;
11237736Smckusick 	register struct fs *fs = VFSTOUFS(mntp)->um_fs;
11337736Smckusick 	register struct inode *ip, *iq;
11437736Smckusick 	register struct vnode *vp;
11537736Smckusick 	struct inode *nip;
11637736Smckusick 	struct buf *bp;
11737736Smckusick 	struct dinode tdip, *dp;
11837736Smckusick 	union  ihead *ih;
11937736Smckusick 	int error;
12024Sbill 
12124Sbill loop:
1227334Skre 	ih = &ihead[INOHASH(dev, ino)];
1237334Skre 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw)
1244818Swnj 		if (ino == ip->i_number && dev == ip->i_dev) {
12516642Ssam 			/*
12616642Ssam 			 * Following is essentially an inline expanded
12716642Ssam 			 * copy of igrab(), expanded inline for speed,
12816642Ssam 			 * and so that the test for a mounted on inode
12916642Ssam 			 * can be deferred until after we are sure that
13016642Ssam 			 * the inode isn't busy.
13116642Ssam 			 */
1328452Sroot 			if ((ip->i_flag&ILOCKED) != 0) {
13324Sbill 				ip->i_flag |= IWANT;
13424Sbill 				sleep((caddr_t)ip, PINOD);
13524Sbill 				goto loop;
13624Sbill 			}
13737736Smckusick 			vp = ITOV(ip);
13837736Smckusick 			if (vp->v_count == 0) {		/* ino on free list */
1397334Skre 				if (iq = ip->i_freef)
1407334Skre 					iq->i_freeb = ip->i_freeb;
1417334Skre 				else
1427334Skre 					ifreet = ip->i_freeb;
1437334Skre 				*ip->i_freeb = iq;
1447334Skre 				ip->i_freef = NULL;
1457334Skre 				ip->i_freeb = NULL;
1467334Skre 			}
147*38226Smckusick 			ILOCK(ip);
14837736Smckusick 			vp->v_count++;
14937736Smckusick 			*ipp = ip;
15037736Smckusick 			return(0);
15124Sbill 		}
15237736Smckusick 	if (error = getnewino(dev, ino, &nip)) {
15337736Smckusick 		*ipp = 0;
15437736Smckusick 		return (error);
15537736Smckusick 	}
15637736Smckusick 	ip = nip;
15737736Smckusick 	/*
15837736Smckusick 	 * Read in the disk contents for the inode.
15937736Smckusick 	 */
16037736Smckusick 	if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)),
16137736Smckusick 	    (int)fs->fs_bsize, &bp)) {
16237736Smckusick 		/*
16337736Smckusick 		 * The inode doesn't contain anything useful, so it would
16437736Smckusick 		 * be misleading to leave it on its hash chain. Iput() will
16537736Smckusick 		 * take care of putting it back on the free list. We also
16637736Smckusick 		 * lose its inumber, just in case.
16737736Smckusick 		 */
16837736Smckusick 		remque(ip);
16937736Smckusick 		ip->i_forw = ip;
17037736Smckusick 		ip->i_back = ip;
17137736Smckusick 		ip->i_number = 0;
17237736Smckusick 		INSFREE(ip);
173*38226Smckusick 		iunlock(ip);
17437736Smckusick 		ip->i_flag = 0;
17537736Smckusick 		brelse(bp);
17637736Smckusick 		*ipp = 0;
17737736Smckusick 		return(error);
17837736Smckusick 	}
17937736Smckusick 	/*
18037736Smckusick 	 * Check to see if the new inode represents a block device
18137736Smckusick 	 * for which we already have an inode (either because of
18237736Smckusick 	 * bdevvp() or because of a different inode representing
18337736Smckusick 	 * the same block device). If such an alias exists, put the
18437736Smckusick 	 * just allocated inode back on the free list, and replace
18537736Smckusick 	 * the contents of the existing inode with the contents of
18637736Smckusick 	 * the new inode.
18737736Smckusick 	 */
18837736Smckusick 	dp = bp->b_un.b_dino;
18937736Smckusick 	dp += itoo(fs, ino);
19037736Smckusick 	if ((dp->di_mode & IFMT) != IFBLK) {
19137736Smckusick 		ip->i_ic = dp->di_ic;
19237736Smckusick 		brelse(bp);
19337736Smckusick 	} else {
19437736Smckusick again:
19537736Smckusick 		for (iq = bdevlisth; iq; iq = iq->i_devlst) {
19637736Smckusick 			if (dp->di_rdev != ITOV(iq)->v_rdev)
19737736Smckusick 				continue;
19837736Smckusick 			igrab(iq);
19937736Smckusick 			if (dp->di_rdev != ITOV(iq)->v_rdev) {
20037736Smckusick 				iput(iq);
20137736Smckusick 				goto again;
20237736Smckusick 			}
20337736Smckusick 			/*
20437736Smckusick 			 * Discard unneeded inode.
20537736Smckusick 			 */
20637736Smckusick 			remque(ip);
20737736Smckusick 			ip->i_forw = ip;
20837736Smckusick 			ip->i_back = ip;
20937736Smckusick 			ip->i_number = 0;
21037736Smckusick 			INSFREE(ip);
211*38226Smckusick 			iunlock(ip);
21237736Smckusick 			ip->i_flag = 0;
21337736Smckusick 			/*
21437736Smckusick 			 * Reinitialize aliased inode.
21537736Smckusick 			 * We must release the buffer that we just read
21637736Smckusick 			 * before doing the iupdat() to avoid a possible
21737736Smckusick 			 * deadlock with updating an inode in the same
21837736Smckusick 			 * disk block.
21937736Smckusick 			 */
22037736Smckusick 			ip = iq;
22137736Smckusick 			vp = ITOV(iq);
22237736Smckusick 			tdip.di_ic = dp->di_ic;
22337736Smckusick 			brelse(bp);
22437736Smckusick 			error = iupdat(ip, &time, &time, 1);
22537736Smckusick 			ip->i_ic = tdip.di_ic;
22637736Smckusick 			remque(ip);
22737736Smckusick 			insque(ip, ih);
22837736Smckusick 			ip->i_dev = dev;
22937736Smckusick 			ip->i_number = ino;
23037736Smckusick 			if (ip->i_devvp) {
23137736Smckusick 				vrele(ip->i_devvp);
23237736Smckusick 				ip->i_devvp = 0;
23337736Smckusick 			}
23437736Smckusick 			cache_purge(vp);
23537736Smckusick 			break;
23637736Smckusick 		}
23737736Smckusick 		if (iq == 0) {
23837736Smckusick 			ip->i_ic = dp->di_ic;
23937736Smckusick 			brelse(bp);
24037736Smckusick 			ip->i_devlst = bdevlisth;
24137736Smckusick 			bdevlisth = ip;
24237736Smckusick 		}
24337736Smckusick 	}
24437736Smckusick 	/*
24537736Smckusick 	 * Finish inode initialization.
24637736Smckusick 	 */
24737736Smckusick 	ip->i_fs = fs;
24837736Smckusick 	ip->i_devvp = VFSTOUFS(mntp)->um_devvp;
24937736Smckusick 	ip->i_devvp->v_count++;
25037736Smckusick 	/*
25137736Smckusick 	 * Initialize the associated vnode
25237736Smckusick 	 */
25337736Smckusick 	vp = ITOV(ip);
25437736Smckusick 	vinit(vp, mntp, IFTOVT(ip->i_mode), &ufs_vnodeops);
25537736Smckusick 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
25637736Smckusick 		vp->v_rdev = ip->i_rdev;
25737736Smckusick 		vp->v_op = &blk_vnodeops;
25837736Smckusick 	}
25937736Smckusick 	if (ino == ROOTINO)
26037736Smckusick 		vp->v_flag |= VROOT;
26137736Smckusick #ifdef QUOTA
26237736Smckusick 	if (ip->i_mode != 0)
26337736Smckusick 		ip->i_dquot = inoquota(ip);
26437736Smckusick #endif
26537736Smckusick 	*ipp = ip;
26637736Smckusick 	return (0);
26737736Smckusick }
2687334Skre 
26937736Smckusick /*
27037736Smckusick  * Allocate a new inode.
27137736Smckusick  *
27237736Smckusick  * Put it onto its hash chain and lock it so that other requests for
27337736Smckusick  * this inode will block if they arrive while we are sleeping waiting
27437736Smckusick  * for old data structures to be purged or for the contents of the disk
27537736Smckusick  * portion of this inode to be read.
27637736Smckusick  */
27737736Smckusick getnewino(dev, ino, ipp)
27837736Smckusick 	dev_t dev;
27937736Smckusick 	ino_t ino;
28037736Smckusick 	struct inode **ipp;
28137736Smckusick {
28237736Smckusick 	union ihead *ih;
28337736Smckusick 	register struct inode *ip, *iq;
28437736Smckusick 	register struct vnode *vp;
28537736Smckusick 
28637736Smckusick 	/*
28737736Smckusick 	 * Remove the next inode from the free list.
28837736Smckusick 	 */
2897334Skre 	if ((ip = ifreeh) == NULL) {
2902933Swnj 		tablefull("inode");
29137736Smckusick 		*ipp = 0;
29237736Smckusick 		return(ENFILE);
29324Sbill 	}
29437736Smckusick 	vp = ITOV(ip);
29537736Smckusick 	if (vp->v_count)
29616720Skarels 		panic("free inode isn't");
2977334Skre 	if (iq = ip->i_freef)
2987334Skre 		iq->i_freeb = &ifreeh;
2997334Skre 	ifreeh = iq;
3007334Skre 	ip->i_freef = NULL;
3017334Skre 	ip->i_freeb = NULL;
3027334Skre 	/*
3037334Skre 	 * Now to take inode off the hash chain it was on
3047334Skre 	 * (initially, or after an iflush, it is on a "hash chain"
30537736Smckusick 	 * consisting entirely of itself, and pointed to by no-one)
30637736Smckusick 	 * and put it on the chain for its new (ino, dev) pair.
3077334Skre 	 */
3087335Skre 	remque(ip);
30924Sbill 	ip->i_dev = dev;
31024Sbill 	ip->i_number = ino;
31137736Smckusick 	if (dev != NODEV) {
31237736Smckusick 		ih = &ihead[INOHASH(dev, ino)];
31337736Smckusick 		insque(ip, ih);
31437736Smckusick 	}
315*38226Smckusick 	ip->i_flag = 0;
316*38226Smckusick 	ILOCK(ip);
3176569Smckusic 	ip->i_lastr = 0;
31824Sbill 	/*
31937736Smckusick 	 * Purge old data structures associated with the inode.
32024Sbill 	 */
32137736Smckusick 	cache_purge(vp);
32237736Smckusick 	if (ip->i_devvp) {
32337736Smckusick 		vrele(ip->i_devvp);
32437736Smckusick 		ip->i_devvp = 0;
32537736Smckusick 	}
3267651Ssam #ifdef QUOTA
32737736Smckusick 	dqrele(ip->i_dquot);
32837736Smckusick 	ip->i_dquot = NODQUOT;
3297492Skre #endif
33037736Smckusick 	if (vp->v_type == VBLK) {
33137736Smckusick 		if (bdevlisth == ip) {
33237736Smckusick 			bdevlisth = ip->i_devlst;
33337736Smckusick 		} else {
33437736Smckusick 			for (iq = bdevlisth; iq; iq = iq->i_devlst) {
33537736Smckusick 				if (iq->i_devlst != ip)
33637736Smckusick 					continue;
33737736Smckusick 				iq->i_devlst = ip->i_devlst;
33837736Smckusick 				break;
33937736Smckusick 			}
34037736Smckusick 			if (iq == NULL)
34137736Smckusick 				panic("missing bdev");
34237736Smckusick 		}
34324Sbill 	}
34437736Smckusick 	*ipp = ip;
34537736Smckusick 	return (0);
34624Sbill }
34724Sbill 
34824Sbill /*
34916642Ssam  * Convert a pointer to an inode into a reference to an inode.
35016642Ssam  *
35116642Ssam  * This is basically the internal piece of iget (after the
35216642Ssam  * inode pointer is located) but without the test for mounted
35316642Ssam  * filesystems.  It is caller's responsibility to check that
35416642Ssam  * the inode pointer is valid.
35516642Ssam  */
35616642Ssam igrab(ip)
35716642Ssam 	register struct inode *ip;
35816642Ssam {
35937736Smckusick 	register struct vnode *vp = ITOV(ip);
36037736Smckusick 
36116642Ssam 	while ((ip->i_flag&ILOCKED) != 0) {
36216642Ssam 		ip->i_flag |= IWANT;
36316642Ssam 		sleep((caddr_t)ip, PINOD);
36416642Ssam 	}
36537736Smckusick 	if (vp->v_count == 0) {		/* ino on free list */
36616642Ssam 		register struct inode *iq;
36716642Ssam 
36816642Ssam 		if (iq = ip->i_freef)
36916642Ssam 			iq->i_freeb = ip->i_freeb;
37016642Ssam 		else
37116642Ssam 			ifreet = ip->i_freeb;
37216642Ssam 		*ip->i_freeb = iq;
37316642Ssam 		ip->i_freef = NULL;
37416642Ssam 		ip->i_freeb = NULL;
37516642Ssam 	}
37637736Smckusick 	vp->v_count++;
377*38226Smckusick 	ILOCK(ip);
37816642Ssam }
37916642Ssam 
38016642Ssam /*
38137736Smckusick  * Create a vnode for a block device.
38237736Smckusick  * Used for root filesystem, argdev, and swap areas.
38337736Smckusick  */
38437736Smckusick bdevvp(dev, vpp)
38537736Smckusick 	dev_t dev;
38637736Smckusick 	struct vnode **vpp;
38737736Smckusick {
38837736Smckusick 	register struct inode *ip;
38937736Smckusick 	register struct vnode *vp;
39037736Smckusick 	struct inode *nip;
39137736Smckusick 	int error;
39237736Smckusick 
39337736Smckusick 	/*
39437736Smckusick 	 * Check for the existence of an existing vnode.
39537736Smckusick 	 */
39637736Smckusick again:
39737736Smckusick 	for (ip = bdevlisth; ip; ip = ip->i_devlst) {
39837736Smckusick 		vp = ITOV(ip);
39937736Smckusick 		if (dev != vp->v_rdev)
40037736Smckusick 			continue;
40137736Smckusick 		igrab(ip);
40237736Smckusick 		if (dev != vp->v_rdev) {
40337736Smckusick 			iput(ip);
40437736Smckusick 			goto again;
40537736Smckusick 		}
40637736Smckusick 		IUNLOCK(ip);
40737736Smckusick 		*vpp = vp;
40837736Smckusick 		return (0);
40937736Smckusick 	}
41037736Smckusick 	if (error = getnewino(NODEV, (ino_t)0, &nip)) {
41137736Smckusick 		*vpp = 0;
41237736Smckusick 		return (error);
41337736Smckusick 	}
41437736Smckusick 	ip = nip;
41537736Smckusick 	ip->i_fs = 0;
41637736Smckusick 	ip->i_devlst = bdevlisth;
41737736Smckusick 	bdevlisth = ip;
41837736Smckusick 	vp = ITOV(ip);
41937736Smckusick 	vinit(vp, 0, VBLK, &blk_vnodeops);
42037736Smckusick 	vp->v_rdev = dev;
42137736Smckusick 	IUNLOCK(ip);
42237736Smckusick 	*vpp = vp;
42337736Smckusick 	return (0);
42437736Smckusick }
42537736Smckusick 
42637736Smckusick /*
42724Sbill  * Decrement reference count of
42824Sbill  * an inode structure.
42924Sbill  * On the last reference,
43024Sbill  * write the inode out and if necessary,
43124Sbill  * truncate and deallocate the file.
43224Sbill  */
43324Sbill iput(ip)
4344818Swnj 	register struct inode *ip;
43524Sbill {
4367118Smckusick 
4378452Sroot 	if ((ip->i_flag & ILOCKED) == 0)
4387118Smckusick 		panic("iput");
43916665Smckusick 	IUNLOCK(ip);
44037736Smckusick 	vrele(ITOV(ip));
4417118Smckusick }
4427118Smckusick 
44337736Smckusick 
44437736Smckusick ufs_inactive(vp)
44537736Smckusick 	struct vnode *vp;
4467118Smckusick {
44737736Smckusick 	register struct inode *ip = VTOI(vp);
44837736Smckusick 	int mode, error;
44924Sbill 
45037736Smckusick 	if (ITOV(ip)->v_count != 0)
45137736Smckusick 		panic("ufs_inactive: not inactive");
452*38226Smckusick 	ILOCK(ip);
45337736Smckusick 	if (ip->i_nlink <= 0 && (ITOV(ip)->v_mount->m_flag&M_RDONLY) == 0) {
45437736Smckusick 		error = itrunc(ip, (u_long)0);
45537736Smckusick 		mode = ip->i_mode;
45637736Smckusick 		ip->i_mode = 0;
45737736Smckusick 		ip->i_rdev = 0;
45837736Smckusick 		ip->i_flag |= IUPD|ICHG;
45937736Smckusick 		ifree(ip, ip->i_number, mode);
4607651Ssam #ifdef QUOTA
46137736Smckusick 		(void) chkiq(ip->i_dev, ip, ip->i_uid, 0);
46237736Smckusick 		dqrele(ip->i_dquot);
46337736Smckusick 		ip->i_dquot = NODQUOT;
4647492Skre #endif
46537736Smckusick 	}
46637736Smckusick 	IUPDAT(ip, &time, &time, 0);
46737736Smckusick 	IUNLOCK(ip);
46837736Smckusick 	ip->i_flag = 0;
46937736Smckusick 	/*
47037736Smckusick 	 * Put the inode on the end of the free list.
47137736Smckusick 	 * Possibly in some cases it would be better to
47237736Smckusick 	 * put the inode at the head of the free list,
47337736Smckusick 	 * (eg: where i_mode == 0 || i_number == 0).
47437736Smckusick 	 */
47537736Smckusick 	INSFREE(ip);
47637736Smckusick 	return (error);
47724Sbill }
47824Sbill 
47924Sbill /*
48024Sbill  * Check accessed and update flags on
48124Sbill  * an inode structure.
48224Sbill  * If any is on, update the inode
48324Sbill  * with the current time.
4841203Sbill  * If waitfor is given, then must insure
4851203Sbill  * i/o order so wait for write to complete.
48624Sbill  */
4871203Sbill iupdat(ip, ta, tm, waitfor)
4884818Swnj 	register struct inode *ip;
4898630Sroot 	struct timeval *ta, *tm;
4904818Swnj 	int waitfor;
49124Sbill {
49237736Smckusick 	struct buf *bp;
49337736Smckusick 	struct vnode *vp = ITOV(ip);
49424Sbill 	struct dinode *dp;
49530749Skarels 	register struct fs *fs;
49637736Smckusick 	int error;
49724Sbill 
49830749Skarels 	fs = ip->i_fs;
49937736Smckusick 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
50037736Smckusick 		return (0);
50137736Smckusick 	if (vp->v_mount->m_flag & M_RDONLY)
50237736Smckusick 		return (0);
50337736Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
50437736Smckusick 		(int)fs->fs_bsize, &bp);
50537736Smckusick 	if (error) {
50637736Smckusick 		brelse(bp);
50737736Smckusick 		return (error);
50824Sbill 	}
50937736Smckusick 	if (ip->i_flag&IACC)
51037736Smckusick 		ip->i_atime = ta->tv_sec;
51137736Smckusick 	if (ip->i_flag&IUPD)
51237736Smckusick 		ip->i_mtime = tm->tv_sec;
51337736Smckusick 	if (ip->i_flag&ICHG)
51437736Smckusick 		ip->i_ctime = time.tv_sec;
51537736Smckusick 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
51637736Smckusick 	dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
51737736Smckusick 	dp->di_ic = ip->i_ic;
51837736Smckusick 	if (waitfor) {
51937736Smckusick 		return (bwrite(bp));
52037736Smckusick 	} else {
52137736Smckusick 		bdwrite(bp);
52237736Smckusick 		return (0);
52337736Smckusick 	}
52424Sbill }
52524Sbill 
52610736Ssam #define	SINGLE	0	/* index of single indirect block */
52710736Ssam #define	DOUBLE	1	/* index of double indirect block */
52810736Ssam #define	TRIPLE	2	/* index of triple indirect block */
52924Sbill /*
5307702Ssam  * Truncate the inode ip to at most
5317702Ssam  * length size.  Free affected disk
5327702Ssam  * blocks -- the blocks of the file
5337702Ssam  * are removed in reverse order.
53410736Ssam  *
53510736Ssam  * NB: triple indirect blocks are untested.
53624Sbill  */
53710736Ssam itrunc(oip, length)
53817942Smckusick 	register struct inode *oip;
5399165Ssam 	u_long length;
54024Sbill {
5419165Ssam 	register daddr_t lastblock;
54226272Skarels 	daddr_t bn, lbn, lastiblock[NIADDR];
5436569Smckusic 	register struct fs *fs;
54410736Ssam 	register struct inode *ip;
54517942Smckusick 	struct buf *bp;
54637736Smckusick 	int offset, osize, size, level;
54737736Smckusick 	long count, nblocks, blocksreleased = 0;
54817942Smckusick 	register int i;
54937736Smckusick 	int error, allerror = 0;
55010736Ssam 	struct inode tip;
5519165Ssam 
55213000Ssam 	if (oip->i_size <= length) {
55313000Ssam 		oip->i_flag |= ICHG|IUPD;
55437736Smckusick 		error = iupdat(oip, &time, &time, 1);
55537736Smckusick 		return (error);
55613000Ssam 	}
5571203Sbill 	/*
55810736Ssam 	 * Calculate index into inode's block list of
55910736Ssam 	 * last direct and indirect blocks (if any)
56010736Ssam 	 * which we want to keep.  Lastblock is -1 when
56110736Ssam 	 * the file is truncated to 0.
5621203Sbill 	 */
56310736Ssam 	fs = oip->i_fs;
5649165Ssam 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
56510736Ssam 	lastiblock[SINGLE] = lastblock - NDADDR;
56610736Ssam 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
56710736Ssam 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
56812645Ssam 	nblocks = btodb(fs->fs_bsize);
5696569Smckusic 	/*
57017942Smckusick 	 * Update the size of the file. If the file is not being
57117942Smckusick 	 * truncated to a block boundry, the contents of the
57217942Smckusick 	 * partial block following the end of the file must be
57317942Smckusick 	 * zero'ed in case it ever become accessable again because
57417942Smckusick 	 * of subsequent file growth.
57517942Smckusick 	 */
57617942Smckusick 	osize = oip->i_size;
57717942Smckusick 	offset = blkoff(fs, length);
57817942Smckusick 	if (offset == 0) {
57917942Smckusick 		oip->i_size = length;
58017942Smckusick 	} else {
58117942Smckusick 		lbn = lblkno(fs, length);
58237736Smckusick 		error = balloc(oip, lbn, offset, &bn, B_CLRBUF);
58337736Smckusick 		if (error)
58437736Smckusick 			return (error);
58537736Smckusick 		if ((long)bn < 0)
58637736Smckusick 			panic("itrunc: hole");
58717942Smckusick 		oip->i_size = length;
58817942Smckusick 		size = blksize(fs, oip, lbn);
58930749Skarels 		count = howmany(size, CLBYTES);
59030749Skarels 		for (i = 0; i < count; i++)
59137736Smckusick 			munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
59237736Smckusick 		error = bread(oip->i_devvp, bn, size, &bp);
59337736Smckusick 		if (error) {
59417942Smckusick 			oip->i_size = osize;
59517942Smckusick 			brelse(bp);
59637736Smckusick 			return (error);
59717942Smckusick 		}
59826272Skarels 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
59917942Smckusick 		bdwrite(bp);
60017942Smckusick 	}
60117942Smckusick 	/*
60217942Smckusick 	 * Update file and block pointers
60310736Ssam 	 * on disk before we start freeing blocks.
60410736Ssam 	 * If we crash before free'ing blocks below,
60510736Ssam 	 * the blocks will be returned to the free list.
60610736Ssam 	 * lastiblock values are also normalized to -1
60710736Ssam 	 * for calls to indirtrunc below.
6086569Smckusic 	 */
60910736Ssam 	tip = *oip;
61017942Smckusick 	tip.i_size = osize;
61110736Ssam 	for (level = TRIPLE; level >= SINGLE; level--)
61210736Ssam 		if (lastiblock[level] < 0) {
61310736Ssam 			oip->i_ib[level] = 0;
61410736Ssam 			lastiblock[level] = -1;
6159165Ssam 		}
61610736Ssam 	for (i = NDADDR - 1; i > lastblock; i--)
61710736Ssam 		oip->i_db[i] = 0;
61810736Ssam 	oip->i_flag |= ICHG|IUPD;
61937736Smckusick 	allerror = syncip(oip);
62010736Ssam 
6216569Smckusic 	/*
62210736Ssam 	 * Indirect blocks first.
6236569Smckusic 	 */
62417942Smckusick 	ip = &tip;
62510736Ssam 	for (level = TRIPLE; level >= SINGLE; level--) {
62610736Ssam 		bn = ip->i_ib[level];
6279165Ssam 		if (bn != 0) {
62837736Smckusick 			error = indirtrunc(ip, bn, lastiblock[level], level,
62937736Smckusick 				&count);
63037736Smckusick 			if (error)
63137736Smckusick 				allerror = error;
63237736Smckusick 			blocksreleased += count;
63310736Ssam 			if (lastiblock[level] < 0) {
63410736Ssam 				ip->i_ib[level] = 0;
63531402Smckusick 				blkfree(ip, bn, (off_t)fs->fs_bsize);
63610736Ssam 				blocksreleased += nblocks;
63710736Ssam 			}
63810736Ssam 		}
63910736Ssam 		if (lastiblock[level] >= 0)
64010736Ssam 			goto done;
6419165Ssam 	}
64210736Ssam 
6436569Smckusic 	/*
64410736Ssam 	 * All whole direct blocks or frags.
6456569Smckusic 	 */
6469165Ssam 	for (i = NDADDR - 1; i > lastblock; i--) {
64726359Skarels 		register off_t bsize;
6489165Ssam 
6496569Smckusic 		bn = ip->i_db[i];
6509165Ssam 		if (bn == 0)
65124Sbill 			continue;
6529165Ssam 		ip->i_db[i] = 0;
65324525Sbloom 		bsize = (off_t)blksize(fs, ip, i);
65431402Smckusick 		blkfree(ip, bn, bsize);
65524525Sbloom 		blocksreleased += btodb(bsize);
65624Sbill 	}
65710736Ssam 	if (lastblock < 0)
65810736Ssam 		goto done;
65910736Ssam 
6601203Sbill 	/*
6619165Ssam 	 * Finally, look for a change in size of the
6629165Ssam 	 * last direct block; release any frags.
6631203Sbill 	 */
66410736Ssam 	bn = ip->i_db[lastblock];
66510736Ssam 	if (bn != 0) {
66626359Skarels 		off_t oldspace, newspace;
66710736Ssam 
6689165Ssam 		/*
6699165Ssam 		 * Calculate amount of space we're giving
6709165Ssam 		 * back as old block size minus new block size.
6719165Ssam 		 */
67210736Ssam 		oldspace = blksize(fs, ip, lastblock);
6739165Ssam 		ip->i_size = length;
67410736Ssam 		newspace = blksize(fs, ip, lastblock);
67510736Ssam 		if (newspace == 0)
67610736Ssam 			panic("itrunc: newspace");
67710736Ssam 		if (oldspace - newspace > 0) {
6789165Ssam 			/*
6799165Ssam 			 * Block number of space to be free'd is
6809165Ssam 			 * the old block # plus the number of frags
6819165Ssam 			 * required for the storage we're keeping.
6829165Ssam 			 */
68310736Ssam 			bn += numfrags(fs, newspace);
68431402Smckusick 			blkfree(ip, bn, oldspace - newspace);
68512645Ssam 			blocksreleased += btodb(oldspace - newspace);
6869165Ssam 		}
6879165Ssam 	}
6889165Ssam done:
68910736Ssam /* BEGIN PARANOIA */
69010736Ssam 	for (level = SINGLE; level <= TRIPLE; level++)
69110736Ssam 		if (ip->i_ib[level] != oip->i_ib[level])
69210736Ssam 			panic("itrunc1");
69310736Ssam 	for (i = 0; i < NDADDR; i++)
69410736Ssam 		if (ip->i_db[i] != oip->i_db[i])
69510736Ssam 			panic("itrunc2");
69610736Ssam /* END PARANOIA */
69712645Ssam 	oip->i_blocks -= blocksreleased;
69812645Ssam 	if (oip->i_blocks < 0)			/* sanity */
69912645Ssam 		oip->i_blocks = 0;
70012645Ssam 	oip->i_flag |= ICHG;
7019165Ssam #ifdef QUOTA
70212645Ssam 	(void) chkdq(oip, -blocksreleased, 0);
7039165Ssam #endif
70437736Smckusick 	return (allerror);
70524Sbill }
70624Sbill 
7079165Ssam /*
7089165Ssam  * Release blocks associated with the inode ip and
7099165Ssam  * stored in the indirect block bn.  Blocks are free'd
7109165Ssam  * in LIFO order up to (but not including) lastbn.  If
71110736Ssam  * level is greater than SINGLE, the block is an indirect
71210736Ssam  * block and recursive calls to indirtrunc must be used to
71310736Ssam  * cleanse other indirect blocks.
71410736Ssam  *
71510736Ssam  * NB: triple indirect blocks are untested.
7169165Ssam  */
71737736Smckusick indirtrunc(ip, bn, lastbn, level, countp)
7186569Smckusic 	register struct inode *ip;
7199165Ssam 	daddr_t bn, lastbn;
72010736Ssam 	int level;
72137736Smckusick 	long *countp;
72224Sbill {
7239165Ssam 	register int i;
72431661Smckusick 	struct buf *bp;
72531661Smckusick 	register struct fs *fs = ip->i_fs;
72624Sbill 	register daddr_t *bap;
72731661Smckusick 	daddr_t *copy, nb, last;
72837736Smckusick 	long blkcount, factor;
72937736Smckusick 	int nblocks, blocksreleased = 0;
73037736Smckusick 	int error, allerror = 0;
73124Sbill 
73210736Ssam 	/*
73310736Ssam 	 * Calculate index in current block of last
73410736Ssam 	 * block to be kept.  -1 indicates the entire
73510736Ssam 	 * block so we need not calculate the index.
73610736Ssam 	 */
73710736Ssam 	factor = 1;
73810736Ssam 	for (i = SINGLE; i < level; i++)
73910736Ssam 		factor *= NINDIR(fs);
7409165Ssam 	last = lastbn;
74110736Ssam 	if (lastbn > 0)
74210736Ssam 		last /= factor;
74312645Ssam 	nblocks = btodb(fs->fs_bsize);
74410736Ssam 	/*
74510736Ssam 	 * Get buffer of block pointers, zero those
74610736Ssam 	 * entries corresponding to blocks to be free'd,
74710736Ssam 	 * and update on disk copy first.
74810736Ssam 	 */
74937736Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize, &bp);
75037736Smckusick 	if (error) {
75110736Ssam 		brelse(bp);
75237736Smckusick 		*countp = 0;
75337736Smckusick 		return (error);
75410736Ssam 	}
75510736Ssam 	bap = bp->b_un.b_daddr;
75631661Smckusick 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
75731661Smckusick 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
75810736Ssam 	bzero((caddr_t)&bap[last + 1],
75910736Ssam 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
76037736Smckusick 	error = bwrite(bp);
76137736Smckusick 	if (error)
76237736Smckusick 		allerror = error;
76331661Smckusick 	bap = copy;
76410736Ssam 
76510736Ssam 	/*
76610736Ssam 	 * Recursively free totally unused blocks.
76710736Ssam 	 */
7689165Ssam 	for (i = NINDIR(fs) - 1; i > last; i--) {
76924Sbill 		nb = bap[i];
7709165Ssam 		if (nb == 0)
77124Sbill 			continue;
77237736Smckusick 		if (level > SINGLE) {
77337736Smckusick 			error = indirtrunc(ip, nb, (daddr_t)-1, level - 1,
77437736Smckusick 				&blkcount);
77537736Smckusick 			if (error)
77637736Smckusick 				allerror = error;
77737736Smckusick 			blocksreleased += blkcount;
77837736Smckusick 		}
77931402Smckusick 		blkfree(ip, nb, (off_t)fs->fs_bsize);
7809165Ssam 		blocksreleased += nblocks;
78124Sbill 	}
78210736Ssam 
78310736Ssam 	/*
78410736Ssam 	 * Recursively free last partial block.
78510736Ssam 	 */
78610736Ssam 	if (level > SINGLE && lastbn >= 0) {
78710736Ssam 		last = lastbn % factor;
7889165Ssam 		nb = bap[i];
78937736Smckusick 		if (nb != 0) {
79037736Smckusick 			error = indirtrunc(ip, nb, last, level - 1, &blkcount);
79137736Smckusick 			if (error)
79237736Smckusick 				allerror = error;
79337736Smckusick 			blocksreleased += blkcount;
79437736Smckusick 		}
7959165Ssam 	}
79631661Smckusick 	FREE(copy, M_TEMP);
79737736Smckusick 	*countp = blocksreleased;
79837736Smckusick 	return (allerror);
79924Sbill }
80024Sbill 
80124Sbill /*
80230749Skarels  * Remove any inodes in the inode cache belonging to dev.
8037334Skre  *
8047334Skre  * There should not be any active ones, return error if any are found
80530749Skarels  * (nb: this is a user error, not a system err).
8067334Skre  */
8077651Ssam #ifdef QUOTA
8087504Sroot iflush(dev, iq)
8097492Skre 	dev_t dev;
8107504Sroot 	struct inode *iq;
8117492Skre #else
8127334Skre iflush(dev)
8137334Skre 	dev_t dev;
8147492Skre #endif
8157334Skre {
8167335Skre 	register struct inode *ip;
8177334Skre 
8187334Skre 	for (ip = inode; ip < inodeNINODE; ip++) {
8197651Ssam #ifdef QUOTA
8207492Skre 		if (ip != iq && ip->i_dev == dev)
8217492Skre #else
8227334Skre 		if (ip->i_dev == dev)
8237492Skre #endif
82437736Smckusick 			if (ITOV(ip)->v_count)
82530749Skarels 				return (EBUSY);
8267334Skre 			else {
8277335Skre 				remque(ip);
8287334Skre 				ip->i_forw = ip;
8297334Skre 				ip->i_back = ip;
8307334Skre 				/*
83137736Smckusick 				 * as v_count == 0, the inode was on the free
8327334Skre 				 * list already, just leave it there, it will
8337334Skre 				 * fall off the bottom eventually. We could
8347334Skre 				 * perhaps move it to the head of the free
8357334Skre 				 * list, but as umounts are done so
8367334Skre 				 * infrequently, we would gain very little,
8377334Skre 				 * while making the code bigger.
8387334Skre 				 */
8397651Ssam #ifdef QUOTA
8407492Skre 				dqrele(ip->i_dquot);
8417492Skre 				ip->i_dquot = NODQUOT;
8427492Skre #endif
84337736Smckusick 				if (ip->i_devvp) {
84437736Smckusick 					vrele(ip->i_devvp);
84537736Smckusick 					ip->i_devvp = 0;
84637736Smckusick 				}
8477334Skre 			}
8487334Skre 	}
84930749Skarels 	return (0);
8507334Skre }
8517334Skre 
8523617Sroot /*
8534818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
8543617Sroot  */
8554818Swnj ilock(ip)
8564818Swnj 	register struct inode *ip;
8573617Sroot {
8583617Sroot 
85937736Smckusick 	while (ip->i_flag & ILOCKED) {
86037736Smckusick 		ip->i_flag |= IWANT;
86137736Smckusick 		(void) sleep((caddr_t)ip, PINOD);
86237736Smckusick 	}
86337736Smckusick 	ip->i_flag |= ILOCKED;
8643617Sroot }
8653617Sroot 
8663617Sroot /*
8674818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
8683617Sroot  */
8697118Smckusick iunlock(ip)
8704818Swnj 	register struct inode *ip;
8713617Sroot {
8723617Sroot 
87337736Smckusick 	if ((ip->i_flag & ILOCKED) == 0)
87437736Smckusick 		printf("unlocking unlocked inode %d on dev 0x%x\n",
87537736Smckusick 			ip->i_number, ip->i_dev);
87637736Smckusick 	ip->i_flag &= ~ILOCKED;
87737736Smckusick 	if (ip->i_flag&IWANT) {
87837736Smckusick 		ip->i_flag &= ~IWANT;
87937736Smckusick 		wakeup((caddr_t)ip);
88037736Smckusick 	}
8813617Sroot }
88237736Smckusick 
88337736Smckusick /*
88437736Smckusick  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
88537736Smckusick  * The mode is shifted to select the owner/group/other fields. The
88637736Smckusick  * super user is granted all permissions.
88737736Smckusick  *
88837736Smckusick  * NB: Called from vnode op table. It seems this could all be done
88937736Smckusick  * using vattr's but...
89037736Smckusick  */
89137736Smckusick iaccess(ip, mode, cred)
89237736Smckusick 	register struct inode *ip;
89337736Smckusick 	register int mode;
89437736Smckusick 	struct ucred *cred;
89537736Smckusick {
89637736Smckusick 	register gid_t *gp;
89737736Smckusick 	register struct vnode *vp = ITOV(ip);
89837736Smckusick 	int i;
89937736Smckusick 
90037736Smckusick 	/*
90137736Smckusick 	 * If you're the super-user,
90237736Smckusick 	 * you always get access.
90337736Smckusick 	 */
90437736Smckusick 	if (cred->cr_uid == 0)
90537736Smckusick 		return (0);
90637736Smckusick 	/*
90737736Smckusick 	 * Access check is based on only one of owner, group, public.
90837736Smckusick 	 * If not owner, then check group. If not a member of the
90937736Smckusick 	 * group, then check public access.
91037736Smckusick 	 */
91137736Smckusick 	if (cred->cr_uid != ip->i_uid) {
91237736Smckusick 		mode >>= 3;
91337736Smckusick 		gp = cred->cr_groups;
91437736Smckusick 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
91537736Smckusick 			if (ip->i_gid == *gp)
91637736Smckusick 				goto found;
91737736Smckusick 		mode >>= 3;
91837736Smckusick found:
91937736Smckusick 		;
92037736Smckusick 	}
92137736Smckusick 	if ((ip->i_mode & mode) != 0)
92237736Smckusick 		return (0);
92337736Smckusick 	return (EACCES);
92437736Smckusick }
925