xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 39574)
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*39574Smckusick  *	@(#)lfs_inode.c	7.20 (Berkeley) 11/21/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 
4439392Smckusick union ihead {
457334Skre 	union  ihead *ih_head[2];
467334Skre 	struct inode *ih_chain[2];
477334Skre } ihead[INOHSZ];
487334Skre 
49*39574Smckusick int prtactive;	/* 1 => print out reclaim of active vnodes */
50*39574Smckusick 
5124Sbill /*
5239392Smckusick  * Initialize hash links for inodes.
5324Sbill  */
5439440Smckusick ufs_init()
5524Sbill {
5624Sbill 	register int i;
5739392Smckusick 	register union ihead *ih = ihead;
5824Sbill 
5939492Smckusick #ifndef lint
6039392Smckusick 	if (VN_MAXPRIVATE < sizeof(struct inode))
6139392Smckusick 		panic("ihinit: too small");
6239492Smckusick #endif /* not lint */
637334Skre 	for (i = INOHSZ; --i >= 0; ih++) {
647334Skre 		ih->ih_head[0] = ih;
657334Skre 		ih->ih_head[1] = ih;
667334Skre 	}
6724Sbill }
6824Sbill 
6924Sbill /*
7037736Smckusick  * Look up an vnode/inode by device,inumber.
7124Sbill  * If it is in core (in the inode structure),
7224Sbill  * honor the locking protocol.
7324Sbill  * If it is not in core, read it in from the
7424Sbill  * specified device.
7537736Smckusick  * Callers must check for mount points!!
7624Sbill  * In all cases, a pointer to a locked
7724Sbill  * inode structure is returned.
7824Sbill  */
7937736Smckusick iget(xp, ino, ipp)
8037736Smckusick 	struct inode *xp;
814818Swnj 	ino_t ino;
8237736Smckusick 	struct inode **ipp;
8324Sbill {
8437736Smckusick 	dev_t dev = xp->i_dev;
8537736Smckusick 	struct mount *mntp = ITOV(xp)->v_mount;
8637736Smckusick 	register struct fs *fs = VFSTOUFS(mntp)->um_fs;
8739440Smckusick 	extern struct vnodeops ufs_vnodeops, spec_inodeops;
8837736Smckusick 	register struct inode *ip, *iq;
8937736Smckusick 	register struct vnode *vp;
9039440Smckusick 	struct vnode *nvp;
9137736Smckusick 	struct buf *bp;
9239440Smckusick 	struct dinode *dp;
9337736Smckusick 	union  ihead *ih;
9437736Smckusick 	int error;
9524Sbill 
9639440Smckusick 	ih = &ihead[INOHASH(dev, ino)];
9724Sbill loop:
9839392Smckusick 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) {
9939392Smckusick 		if (ino != ip->i_number || dev != ip->i_dev)
10039392Smckusick 			continue;
10139392Smckusick 		if ((ip->i_flag&ILOCKED) != 0) {
10239392Smckusick 			ip->i_flag |= IWANT;
10339392Smckusick 			sleep((caddr_t)ip, PINOD);
10439392Smckusick 			goto loop;
10539392Smckusick 		}
10639440Smckusick 		if (vget(ITOV(ip)))
10739440Smckusick 			goto loop;
10839392Smckusick 		*ipp = ip;
10939392Smckusick 		return(0);
11039392Smckusick 	}
11139440Smckusick 	/*
11239440Smckusick 	 * Allocate a new inode.
11339440Smckusick 	 */
11439440Smckusick 	if (error = getnewvnode(VT_UFS, mntp, &ufs_vnodeops, &nvp)) {
11537736Smckusick 		*ipp = 0;
11637736Smckusick 		return (error);
11737736Smckusick 	}
11839440Smckusick 	ip = VTOI(nvp);
11939440Smckusick 	ip->i_vnode = nvp;
12039440Smckusick 	ip->i_flag = 0;
12139440Smckusick 	ip->i_devvp = 0;
12239440Smckusick 	ip->i_lastr = 0;
12339440Smckusick 	ip->i_mode = 0;
12439440Smckusick #ifdef QUOTA
12539440Smckusick 	ip->i_dquot = NODQUOT;
12639440Smckusick #endif
12737736Smckusick 	/*
12839440Smckusick 	 * Put it onto its hash chain and lock it so that other requests for
12939440Smckusick 	 * this inode will block if they arrive while we are sleeping waiting
13039440Smckusick 	 * for old data structures to be purged or for the contents of the
13139440Smckusick 	 * disk portion of this inode to be read.
13239440Smckusick 	 */
13339440Smckusick 	ip->i_dev = dev;
13439440Smckusick 	ip->i_number = ino;
13539440Smckusick 	insque(ip, ih);
13639440Smckusick 	ILOCK(ip);
13739440Smckusick 	/*
13837736Smckusick 	 * Read in the disk contents for the inode.
13937736Smckusick 	 */
14037736Smckusick 	if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)),
14138776Smckusick 	    (int)fs->fs_bsize, NOCRED, &bp)) {
14237736Smckusick 		/*
14339392Smckusick 		 * Unlock and discard unneeded inode.
14437736Smckusick 		 */
14539440Smckusick 		iput(ip);
14637736Smckusick 		brelse(bp);
14737736Smckusick 		*ipp = 0;
14839440Smckusick 		return (error);
14937736Smckusick 	}
15039440Smckusick 	dp = bp->b_un.b_dino;
15139440Smckusick 	dp += itoo(fs, ino);
15239440Smckusick 	ip->i_din = *dp;
15339440Smckusick 	brelse(bp);
15437736Smckusick 	/*
15539440Smckusick 	 * Initialize the associated vnode
15637736Smckusick 	 */
15739440Smckusick 	vp = ITOV(ip);
15839440Smckusick 	vp->v_type = IFTOVT(ip->i_mode);
15939440Smckusick 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
16039440Smckusick 		vp->v_rdev = ip->i_rdev;
16139440Smckusick 		vp->v_op = &spec_inodeops;
16239440Smckusick 		if (nvp = checkalias(vp, mntp)) {
16337736Smckusick 			/*
16439440Smckusick 			 * Reinitialize aliased inode.
16537736Smckusick 			 */
16639440Smckusick 			vp = nvp;
16739440Smckusick 			iq = VTOI(vp);
16839440Smckusick 			iq->i_vnode = vp;
16939440Smckusick 			iq->i_lastr = 0;
17039517Smckusick 			iq->i_flag = 0;
17139440Smckusick 			ILOCK(iq);
17239440Smckusick 			iq->i_din = ip->i_din;
17339440Smckusick 			iq->i_dev = dev;
17439440Smckusick 			iq->i_number = ino;
17539440Smckusick 			insque(iq, ih);
17637736Smckusick 			/*
17739440Smckusick 			 * Discard unneeded vnode
17837736Smckusick 			 */
17939440Smckusick 			ip->i_mode = 0;
18039440Smckusick 			iput(ip);
18137736Smckusick 			ip = iq;
18237736Smckusick 		}
18337736Smckusick 	}
18439440Smckusick 	if (ino == ROOTINO)
18539440Smckusick 		vp->v_flag |= VROOT;
18637736Smckusick 	/*
18737736Smckusick 	 * Finish inode initialization.
18837736Smckusick 	 */
18937736Smckusick 	ip->i_fs = fs;
19037736Smckusick 	ip->i_devvp = VFSTOUFS(mntp)->um_devvp;
19138345Smckusick 	VREF(ip->i_devvp);
19237736Smckusick #ifdef QUOTA
19337736Smckusick 	if (ip->i_mode != 0)
19437736Smckusick 		ip->i_dquot = inoquota(ip);
19537736Smckusick #endif
19638256Smckusick 	/*
19738256Smckusick 	 * Set up a generation number for this inode if it does not
19838256Smckusick 	 * already have one. This should only happen on old filesystems.
19938256Smckusick 	 */
20038256Smckusick 	if (ip->i_gen == 0) {
20138256Smckusick 		if (++nextgennumber < (u_long)time.tv_sec)
20238256Smckusick 			nextgennumber = time.tv_sec;
20338256Smckusick 		ip->i_gen = nextgennumber;
20438256Smckusick 		if ((vp->v_mount->m_flag & M_RDONLY) == 0)
20538256Smckusick 			ip->i_flag |= IMOD;
20638256Smckusick 	}
20737736Smckusick 	*ipp = ip;
20837736Smckusick 	return (0);
20937736Smckusick }
2107334Skre 
21137736Smckusick /*
21239392Smckusick  * Unlock and decrement the reference count of an inode structure.
21324Sbill  */
21424Sbill iput(ip)
2154818Swnj 	register struct inode *ip;
21624Sbill {
2177118Smckusick 
2188452Sroot 	if ((ip->i_flag & ILOCKED) == 0)
2197118Smckusick 		panic("iput");
22016665Smckusick 	IUNLOCK(ip);
22137736Smckusick 	vrele(ITOV(ip));
2227118Smckusick }
2237118Smckusick 
22439392Smckusick /*
22539392Smckusick  * Last reference to an inode, write the inode out and if necessary,
22639392Smckusick  * truncate and deallocate the file.
22739392Smckusick  */
22837736Smckusick ufs_inactive(vp)
22937736Smckusick 	struct vnode *vp;
2307118Smckusick {
23137736Smckusick 	register struct inode *ip = VTOI(vp);
23239392Smckusick 	int mode, error = 0;
23324Sbill 
234*39574Smckusick 	if (prtactive && vp->v_count != 0)
23539440Smckusick 		printf("ufs_inactive: pushing active ino %d dev 0x%x\n",
23639440Smckusick 			ip->i_number, ip->i_dev);
23738452Smckusick 	/*
23838452Smckusick 	 * Get rid of inodes related to stale file handles.
23938452Smckusick 	 */
24039440Smckusick 	if (ip->i_mode == 0) {
24139440Smckusick 		vgone(vp);
24239440Smckusick 		return (0);
24339440Smckusick 	}
24438226Smckusick 	ILOCK(ip);
24539364Smckusick 	if (ip->i_nlink <= 0 && (vp->v_mount->m_flag & M_RDONLY) == 0) {
24637736Smckusick 		error = itrunc(ip, (u_long)0);
24737736Smckusick 		mode = ip->i_mode;
24837736Smckusick 		ip->i_mode = 0;
24937736Smckusick 		ip->i_rdev = 0;
25037736Smckusick 		ip->i_flag |= IUPD|ICHG;
25137736Smckusick 		ifree(ip, ip->i_number, mode);
2527651Ssam #ifdef QUOTA
25337736Smckusick 		(void) chkiq(ip->i_dev, ip, ip->i_uid, 0);
25437736Smckusick 		dqrele(ip->i_dquot);
25537736Smckusick 		ip->i_dquot = NODQUOT;
2567492Skre #endif
25737736Smckusick 	}
25837736Smckusick 	IUPDAT(ip, &time, &time, 0);
25937736Smckusick 	IUNLOCK(ip);
26037736Smckusick 	ip->i_flag = 0;
26137736Smckusick 	/*
26239392Smckusick 	 * If we are done with the inode, reclaim it
26339392Smckusick 	 * so that it can be reused immediately.
26437736Smckusick 	 */
26539440Smckusick 	if (vp->v_count == 0 && ip->i_mode == 0)
26639440Smckusick 		vgone(vp);
26737736Smckusick 	return (error);
26824Sbill }
26924Sbill 
27024Sbill /*
27139392Smckusick  * Reclaim an inode so that it can be used for other purposes.
27224Sbill  */
27339392Smckusick ufs_reclaim(vp)
27439392Smckusick 	register struct vnode *vp;
27539392Smckusick {
27639492Smckusick 	register struct inode *ip = VTOI(vp);
27739392Smckusick 
278*39574Smckusick 	if (prtactive && vp->v_count != 0)
27939440Smckusick 		printf("ufs_reclaim: pushing active ino %d dev 0x%x\n",
28039440Smckusick 			ip->i_number, ip->i_dev);
28139392Smckusick 	/*
28239392Smckusick 	 * Remove the inode from its hash chain.
28339392Smckusick 	 */
28439392Smckusick 	remque(ip);
28539392Smckusick 	ip->i_forw = ip;
28639392Smckusick 	ip->i_back = ip;
28739392Smckusick 	/*
28839392Smckusick 	 * Purge old data structures associated with the inode.
28939392Smckusick 	 */
29039392Smckusick 	cache_purge(vp);
29139392Smckusick 	if (ip->i_devvp) {
29239392Smckusick 		vrele(ip->i_devvp);
29339392Smckusick 		ip->i_devvp = 0;
29439392Smckusick 	}
29539392Smckusick #ifdef QUOTA
29639392Smckusick 	dqrele(ip->i_dquot);
29739392Smckusick 	ip->i_dquot = NODQUOT;
29839392Smckusick #endif
29939392Smckusick 	ip->i_flag = 0;
30039392Smckusick 	return (0);
30139392Smckusick }
30239392Smckusick 
30339392Smckusick /*
30439392Smckusick  * Check accessed and update flags on an inode structure.
30539392Smckusick  * If any is on, update the inode with the current time.
30639392Smckusick  * If waitfor is given, then must ensure I/O order,
30739392Smckusick  * so wait for write to complete.
30839392Smckusick  */
3091203Sbill iupdat(ip, ta, tm, waitfor)
3104818Swnj 	register struct inode *ip;
3118630Sroot 	struct timeval *ta, *tm;
3124818Swnj 	int waitfor;
31324Sbill {
31437736Smckusick 	struct buf *bp;
31537736Smckusick 	struct vnode *vp = ITOV(ip);
31624Sbill 	struct dinode *dp;
31730749Skarels 	register struct fs *fs;
31837736Smckusick 	int error;
31924Sbill 
32030749Skarels 	fs = ip->i_fs;
32137736Smckusick 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
32237736Smckusick 		return (0);
32337736Smckusick 	if (vp->v_mount->m_flag & M_RDONLY)
32437736Smckusick 		return (0);
32537736Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
32638776Smckusick 		(int)fs->fs_bsize, NOCRED, &bp);
32737736Smckusick 	if (error) {
32837736Smckusick 		brelse(bp);
32937736Smckusick 		return (error);
33024Sbill 	}
33137736Smckusick 	if (ip->i_flag&IACC)
33237736Smckusick 		ip->i_atime = ta->tv_sec;
33337736Smckusick 	if (ip->i_flag&IUPD)
33437736Smckusick 		ip->i_mtime = tm->tv_sec;
33537736Smckusick 	if (ip->i_flag&ICHG)
33637736Smckusick 		ip->i_ctime = time.tv_sec;
33737736Smckusick 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
33837736Smckusick 	dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
33939392Smckusick 	*dp = ip->i_din;
34037736Smckusick 	if (waitfor) {
34137736Smckusick 		return (bwrite(bp));
34237736Smckusick 	} else {
34337736Smckusick 		bdwrite(bp);
34437736Smckusick 		return (0);
34537736Smckusick 	}
34624Sbill }
34724Sbill 
34810736Ssam #define	SINGLE	0	/* index of single indirect block */
34910736Ssam #define	DOUBLE	1	/* index of double indirect block */
35010736Ssam #define	TRIPLE	2	/* index of triple indirect block */
35124Sbill /*
35239392Smckusick  * Truncate the inode ip to at most length size.  Free affected disk
35339392Smckusick  * blocks -- the blocks of the file are removed in reverse order.
35410736Ssam  *
35510736Ssam  * NB: triple indirect blocks are untested.
35624Sbill  */
35710736Ssam itrunc(oip, length)
35817942Smckusick 	register struct inode *oip;
3599165Ssam 	u_long length;
36024Sbill {
3619165Ssam 	register daddr_t lastblock;
36226272Skarels 	daddr_t bn, lbn, lastiblock[NIADDR];
3636569Smckusic 	register struct fs *fs;
36410736Ssam 	register struct inode *ip;
36517942Smckusick 	struct buf *bp;
36637736Smckusick 	int offset, osize, size, level;
36737736Smckusick 	long count, nblocks, blocksreleased = 0;
36817942Smckusick 	register int i;
36937736Smckusick 	int error, allerror = 0;
37010736Ssam 	struct inode tip;
3719165Ssam 
37213000Ssam 	if (oip->i_size <= length) {
37313000Ssam 		oip->i_flag |= ICHG|IUPD;
37437736Smckusick 		error = iupdat(oip, &time, &time, 1);
37537736Smckusick 		return (error);
37613000Ssam 	}
3771203Sbill 	/*
37810736Ssam 	 * Calculate index into inode's block list of
37910736Ssam 	 * last direct and indirect blocks (if any)
38010736Ssam 	 * which we want to keep.  Lastblock is -1 when
38110736Ssam 	 * the file is truncated to 0.
3821203Sbill 	 */
38310736Ssam 	fs = oip->i_fs;
3849165Ssam 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
38510736Ssam 	lastiblock[SINGLE] = lastblock - NDADDR;
38610736Ssam 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
38710736Ssam 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
38812645Ssam 	nblocks = btodb(fs->fs_bsize);
3896569Smckusic 	/*
39017942Smckusick 	 * Update the size of the file. If the file is not being
39117942Smckusick 	 * truncated to a block boundry, the contents of the
39217942Smckusick 	 * partial block following the end of the file must be
39317942Smckusick 	 * zero'ed in case it ever become accessable again because
39417942Smckusick 	 * of subsequent file growth.
39517942Smckusick 	 */
39617942Smckusick 	osize = oip->i_size;
39717942Smckusick 	offset = blkoff(fs, length);
39817942Smckusick 	if (offset == 0) {
39917942Smckusick 		oip->i_size = length;
40017942Smckusick 	} else {
40117942Smckusick 		lbn = lblkno(fs, length);
40237736Smckusick 		error = balloc(oip, lbn, offset, &bn, B_CLRBUF);
40337736Smckusick 		if (error)
40437736Smckusick 			return (error);
40537736Smckusick 		if ((long)bn < 0)
40637736Smckusick 			panic("itrunc: hole");
40717942Smckusick 		oip->i_size = length;
40817942Smckusick 		size = blksize(fs, oip, lbn);
40930749Skarels 		count = howmany(size, CLBYTES);
41030749Skarels 		for (i = 0; i < count; i++)
41137736Smckusick 			munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
41238776Smckusick 		error = bread(oip->i_devvp, bn, size, NOCRED, &bp);
41337736Smckusick 		if (error) {
41417942Smckusick 			oip->i_size = osize;
41517942Smckusick 			brelse(bp);
41637736Smckusick 			return (error);
41717942Smckusick 		}
41826272Skarels 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
41917942Smckusick 		bdwrite(bp);
42017942Smckusick 	}
42117942Smckusick 	/*
42217942Smckusick 	 * Update file and block pointers
42310736Ssam 	 * on disk before we start freeing blocks.
42410736Ssam 	 * If we crash before free'ing blocks below,
42510736Ssam 	 * the blocks will be returned to the free list.
42610736Ssam 	 * lastiblock values are also normalized to -1
42710736Ssam 	 * for calls to indirtrunc below.
4286569Smckusic 	 */
42910736Ssam 	tip = *oip;
43017942Smckusick 	tip.i_size = osize;
43110736Ssam 	for (level = TRIPLE; level >= SINGLE; level--)
43210736Ssam 		if (lastiblock[level] < 0) {
43310736Ssam 			oip->i_ib[level] = 0;
43410736Ssam 			lastiblock[level] = -1;
4359165Ssam 		}
43610736Ssam 	for (i = NDADDR - 1; i > lastblock; i--)
43710736Ssam 		oip->i_db[i] = 0;
43810736Ssam 	oip->i_flag |= ICHG|IUPD;
43937736Smckusick 	allerror = syncip(oip);
44010736Ssam 
4416569Smckusic 	/*
44210736Ssam 	 * Indirect blocks first.
4436569Smckusic 	 */
44417942Smckusick 	ip = &tip;
44510736Ssam 	for (level = TRIPLE; level >= SINGLE; level--) {
44610736Ssam 		bn = ip->i_ib[level];
4479165Ssam 		if (bn != 0) {
44837736Smckusick 			error = indirtrunc(ip, bn, lastiblock[level], level,
44937736Smckusick 				&count);
45037736Smckusick 			if (error)
45137736Smckusick 				allerror = error;
45237736Smckusick 			blocksreleased += count;
45310736Ssam 			if (lastiblock[level] < 0) {
45410736Ssam 				ip->i_ib[level] = 0;
45531402Smckusick 				blkfree(ip, bn, (off_t)fs->fs_bsize);
45610736Ssam 				blocksreleased += nblocks;
45710736Ssam 			}
45810736Ssam 		}
45910736Ssam 		if (lastiblock[level] >= 0)
46010736Ssam 			goto done;
4619165Ssam 	}
46210736Ssam 
4636569Smckusic 	/*
46410736Ssam 	 * All whole direct blocks or frags.
4656569Smckusic 	 */
4669165Ssam 	for (i = NDADDR - 1; i > lastblock; i--) {
46726359Skarels 		register off_t bsize;
4689165Ssam 
4696569Smckusic 		bn = ip->i_db[i];
4709165Ssam 		if (bn == 0)
47124Sbill 			continue;
4729165Ssam 		ip->i_db[i] = 0;
47324525Sbloom 		bsize = (off_t)blksize(fs, ip, i);
47431402Smckusick 		blkfree(ip, bn, bsize);
47524525Sbloom 		blocksreleased += btodb(bsize);
47624Sbill 	}
47710736Ssam 	if (lastblock < 0)
47810736Ssam 		goto done;
47910736Ssam 
4801203Sbill 	/*
4819165Ssam 	 * Finally, look for a change in size of the
4829165Ssam 	 * last direct block; release any frags.
4831203Sbill 	 */
48410736Ssam 	bn = ip->i_db[lastblock];
48510736Ssam 	if (bn != 0) {
48626359Skarels 		off_t oldspace, newspace;
48710736Ssam 
4889165Ssam 		/*
4899165Ssam 		 * Calculate amount of space we're giving
4909165Ssam 		 * back as old block size minus new block size.
4919165Ssam 		 */
49210736Ssam 		oldspace = blksize(fs, ip, lastblock);
4939165Ssam 		ip->i_size = length;
49410736Ssam 		newspace = blksize(fs, ip, lastblock);
49510736Ssam 		if (newspace == 0)
49610736Ssam 			panic("itrunc: newspace");
49710736Ssam 		if (oldspace - newspace > 0) {
4989165Ssam 			/*
4999165Ssam 			 * Block number of space to be free'd is
5009165Ssam 			 * the old block # plus the number of frags
5019165Ssam 			 * required for the storage we're keeping.
5029165Ssam 			 */
50310736Ssam 			bn += numfrags(fs, newspace);
50431402Smckusick 			blkfree(ip, bn, oldspace - newspace);
50512645Ssam 			blocksreleased += btodb(oldspace - newspace);
5069165Ssam 		}
5079165Ssam 	}
5089165Ssam done:
50910736Ssam /* BEGIN PARANOIA */
51010736Ssam 	for (level = SINGLE; level <= TRIPLE; level++)
51110736Ssam 		if (ip->i_ib[level] != oip->i_ib[level])
51210736Ssam 			panic("itrunc1");
51310736Ssam 	for (i = 0; i < NDADDR; i++)
51410736Ssam 		if (ip->i_db[i] != oip->i_db[i])
51510736Ssam 			panic("itrunc2");
51610736Ssam /* END PARANOIA */
51712645Ssam 	oip->i_blocks -= blocksreleased;
51812645Ssam 	if (oip->i_blocks < 0)			/* sanity */
51912645Ssam 		oip->i_blocks = 0;
52012645Ssam 	oip->i_flag |= ICHG;
5219165Ssam #ifdef QUOTA
52212645Ssam 	(void) chkdq(oip, -blocksreleased, 0);
5239165Ssam #endif
52437736Smckusick 	return (allerror);
52524Sbill }
52624Sbill 
5279165Ssam /*
5289165Ssam  * Release blocks associated with the inode ip and
5299165Ssam  * stored in the indirect block bn.  Blocks are free'd
5309165Ssam  * in LIFO order up to (but not including) lastbn.  If
53110736Ssam  * level is greater than SINGLE, the block is an indirect
53210736Ssam  * block and recursive calls to indirtrunc must be used to
53310736Ssam  * cleanse other indirect blocks.
53410736Ssam  *
53510736Ssam  * NB: triple indirect blocks are untested.
5369165Ssam  */
53737736Smckusick indirtrunc(ip, bn, lastbn, level, countp)
5386569Smckusic 	register struct inode *ip;
5399165Ssam 	daddr_t bn, lastbn;
54010736Ssam 	int level;
54137736Smckusick 	long *countp;
54224Sbill {
5439165Ssam 	register int i;
54431661Smckusick 	struct buf *bp;
54531661Smckusick 	register struct fs *fs = ip->i_fs;
54624Sbill 	register daddr_t *bap;
54731661Smckusick 	daddr_t *copy, nb, last;
54837736Smckusick 	long blkcount, factor;
54937736Smckusick 	int nblocks, blocksreleased = 0;
55037736Smckusick 	int error, allerror = 0;
55124Sbill 
55210736Ssam 	/*
55310736Ssam 	 * Calculate index in current block of last
55410736Ssam 	 * block to be kept.  -1 indicates the entire
55510736Ssam 	 * block so we need not calculate the index.
55610736Ssam 	 */
55710736Ssam 	factor = 1;
55810736Ssam 	for (i = SINGLE; i < level; i++)
55910736Ssam 		factor *= NINDIR(fs);
5609165Ssam 	last = lastbn;
56110736Ssam 	if (lastbn > 0)
56210736Ssam 		last /= factor;
56312645Ssam 	nblocks = btodb(fs->fs_bsize);
56410736Ssam 	/*
56510736Ssam 	 * Get buffer of block pointers, zero those
56610736Ssam 	 * entries corresponding to blocks to be free'd,
56710736Ssam 	 * and update on disk copy first.
56810736Ssam 	 */
56938776Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
57038776Smckusick 		NOCRED, &bp);
57137736Smckusick 	if (error) {
57210736Ssam 		brelse(bp);
57337736Smckusick 		*countp = 0;
57437736Smckusick 		return (error);
57510736Ssam 	}
57610736Ssam 	bap = bp->b_un.b_daddr;
57731661Smckusick 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
57831661Smckusick 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
57910736Ssam 	bzero((caddr_t)&bap[last + 1],
58010736Ssam 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
58137736Smckusick 	error = bwrite(bp);
58237736Smckusick 	if (error)
58337736Smckusick 		allerror = error;
58431661Smckusick 	bap = copy;
58510736Ssam 
58610736Ssam 	/*
58710736Ssam 	 * Recursively free totally unused blocks.
58810736Ssam 	 */
5899165Ssam 	for (i = NINDIR(fs) - 1; i > last; i--) {
59024Sbill 		nb = bap[i];
5919165Ssam 		if (nb == 0)
59224Sbill 			continue;
59337736Smckusick 		if (level > SINGLE) {
59437736Smckusick 			error = indirtrunc(ip, nb, (daddr_t)-1, level - 1,
59537736Smckusick 				&blkcount);
59637736Smckusick 			if (error)
59737736Smckusick 				allerror = error;
59837736Smckusick 			blocksreleased += blkcount;
59937736Smckusick 		}
60031402Smckusick 		blkfree(ip, nb, (off_t)fs->fs_bsize);
6019165Ssam 		blocksreleased += nblocks;
60224Sbill 	}
60310736Ssam 
60410736Ssam 	/*
60510736Ssam 	 * Recursively free last partial block.
60610736Ssam 	 */
60710736Ssam 	if (level > SINGLE && lastbn >= 0) {
60810736Ssam 		last = lastbn % factor;
6099165Ssam 		nb = bap[i];
61037736Smckusick 		if (nb != 0) {
61137736Smckusick 			error = indirtrunc(ip, nb, last, level - 1, &blkcount);
61237736Smckusick 			if (error)
61337736Smckusick 				allerror = error;
61437736Smckusick 			blocksreleased += blkcount;
61537736Smckusick 		}
6169165Ssam 	}
61731661Smckusick 	FREE(copy, M_TEMP);
61837736Smckusick 	*countp = blocksreleased;
61937736Smckusick 	return (allerror);
62024Sbill }
62124Sbill 
62224Sbill /*
6234818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
6243617Sroot  */
6254818Swnj ilock(ip)
6264818Swnj 	register struct inode *ip;
6273617Sroot {
6283617Sroot 
62937736Smckusick 	while (ip->i_flag & ILOCKED) {
63037736Smckusick 		ip->i_flag |= IWANT;
63137736Smckusick 		(void) sleep((caddr_t)ip, PINOD);
63237736Smckusick 	}
63337736Smckusick 	ip->i_flag |= ILOCKED;
6343617Sroot }
6353617Sroot 
6363617Sroot /*
6374818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
6383617Sroot  */
6397118Smckusick iunlock(ip)
6404818Swnj 	register struct inode *ip;
6413617Sroot {
6423617Sroot 
64337736Smckusick 	if ((ip->i_flag & ILOCKED) == 0)
64437736Smckusick 		printf("unlocking unlocked inode %d on dev 0x%x\n",
64537736Smckusick 			ip->i_number, ip->i_dev);
64637736Smckusick 	ip->i_flag &= ~ILOCKED;
64737736Smckusick 	if (ip->i_flag&IWANT) {
64837736Smckusick 		ip->i_flag &= ~IWANT;
64937736Smckusick 		wakeup((caddr_t)ip);
65037736Smckusick 	}
6513617Sroot }
65237736Smckusick 
65337736Smckusick /*
65437736Smckusick  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
65537736Smckusick  * The mode is shifted to select the owner/group/other fields. The
65637736Smckusick  * super user is granted all permissions.
65737736Smckusick  *
65837736Smckusick  * NB: Called from vnode op table. It seems this could all be done
65937736Smckusick  * using vattr's but...
66037736Smckusick  */
66137736Smckusick iaccess(ip, mode, cred)
66237736Smckusick 	register struct inode *ip;
66337736Smckusick 	register int mode;
66437736Smckusick 	struct ucred *cred;
66537736Smckusick {
66637736Smckusick 	register gid_t *gp;
66737736Smckusick 	int i;
66837736Smckusick 
66937736Smckusick 	/*
67039392Smckusick 	 * If you're the super-user, you always get access.
67137736Smckusick 	 */
67237736Smckusick 	if (cred->cr_uid == 0)
67337736Smckusick 		return (0);
67437736Smckusick 	/*
67537736Smckusick 	 * Access check is based on only one of owner, group, public.
67637736Smckusick 	 * If not owner, then check group. If not a member of the
67737736Smckusick 	 * group, then check public access.
67837736Smckusick 	 */
67937736Smckusick 	if (cred->cr_uid != ip->i_uid) {
68037736Smckusick 		mode >>= 3;
68137736Smckusick 		gp = cred->cr_groups;
68237736Smckusick 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
68337736Smckusick 			if (ip->i_gid == *gp)
68437736Smckusick 				goto found;
68537736Smckusick 		mode >>= 3;
68637736Smckusick found:
68737736Smckusick 		;
68837736Smckusick 	}
68937736Smckusick 	if ((ip->i_mode & mode) != 0)
69037736Smckusick 		return (0);
69137736Smckusick 	return (EACCES);
69237736Smckusick }
693