xref: /csrg-svn/sys/ufs/ffs/ufs_inode.c (revision 40289)
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*40289Smckusick  *	@(#)ufs_inode.c	7.29 (Berkeley) 03/05/90
1823399Smckusick  */
1924Sbill 
2017099Sbloom #include "param.h"
2117099Sbloom #include "systm.h"
2217099Sbloom #include "mount.h"
2317099Sbloom #include "user.h"
2439795Smckusick #include "proc.h"
2537736Smckusick #include "file.h"
2617099Sbloom #include "buf.h"
2724525Sbloom #include "cmap.h"
2837736Smckusick #include "vnode.h"
2937736Smckusick #include "../ufs/inode.h"
3037736Smckusick #include "../ufs/fs.h"
3137736Smckusick #include "../ufs/ufsmount.h"
327651Ssam #ifdef QUOTA
3337736Smckusick #include "../ufs/quota.h"
347504Sroot #endif
3517099Sbloom #include "kernel.h"
3631661Smckusick #include "malloc.h"
3724Sbill 
3816840Smckusick #define	INOHSZ	512
397334Skre #if	((INOHSZ&(INOHSZ-1)) == 0)
407334Skre #define	INOHASH(dev,ino)	(((dev)+(ino))&(INOHSZ-1))
417334Skre #else
4210852Ssam #define	INOHASH(dev,ino)	(((unsigned)((dev)+(ino)))%INOHSZ)
437334Skre #endif
4424Sbill 
4539392Smckusick union ihead {
467334Skre 	union  ihead *ih_head[2];
477334Skre 	struct inode *ih_chain[2];
487334Skre } ihead[INOHSZ];
497334Skre 
5039574Smckusick int prtactive;	/* 1 => print out reclaim of active vnodes */
5139574Smckusick 
5224Sbill /*
5339392Smckusick  * Initialize hash links for inodes.
5424Sbill  */
5539440Smckusick ufs_init()
5624Sbill {
5724Sbill 	register int i;
5839392Smckusick 	register union ihead *ih = ihead;
5924Sbill 
6039492Smckusick #ifndef lint
6139392Smckusick 	if (VN_MAXPRIVATE < sizeof(struct inode))
6239392Smckusick 		panic("ihinit: too small");
6339492Smckusick #endif /* not lint */
647334Skre 	for (i = INOHSZ; --i >= 0; ih++) {
657334Skre 		ih->ih_head[0] = ih;
667334Skre 		ih->ih_head[1] = ih;
677334Skre 	}
6824Sbill }
6924Sbill 
7024Sbill /*
7137736Smckusick  * Look up an vnode/inode by device,inumber.
7224Sbill  * If it is in core (in the inode structure),
7324Sbill  * honor the locking protocol.
7424Sbill  * If it is not in core, read it in from the
7524Sbill  * specified device.
7637736Smckusick  * Callers must check for mount points!!
7724Sbill  * In all cases, a pointer to a locked
7824Sbill  * inode structure is returned.
7924Sbill  */
8037736Smckusick iget(xp, ino, ipp)
8137736Smckusick 	struct inode *xp;
824818Swnj 	ino_t ino;
8337736Smckusick 	struct inode **ipp;
8424Sbill {
8537736Smckusick 	dev_t dev = xp->i_dev;
8637736Smckusick 	struct mount *mntp = ITOV(xp)->v_mount;
8737736Smckusick 	register struct fs *fs = VFSTOUFS(mntp)->um_fs;
8839440Smckusick 	extern struct vnodeops ufs_vnodeops, spec_inodeops;
8937736Smckusick 	register struct inode *ip, *iq;
9037736Smckusick 	register struct vnode *vp;
9139440Smckusick 	struct vnode *nvp;
9237736Smckusick 	struct buf *bp;
9339440Smckusick 	struct dinode *dp;
9437736Smckusick 	union  ihead *ih;
9537736Smckusick 	int error;
9624Sbill 
9739440Smckusick 	ih = &ihead[INOHASH(dev, ino)];
9824Sbill loop:
9939392Smckusick 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) {
10039392Smckusick 		if (ino != ip->i_number || dev != ip->i_dev)
10139392Smckusick 			continue;
10239392Smckusick 		if ((ip->i_flag&ILOCKED) != 0) {
10339392Smckusick 			ip->i_flag |= IWANT;
10439392Smckusick 			sleep((caddr_t)ip, PINOD);
10539392Smckusick 			goto loop;
10639392Smckusick 		}
10739440Smckusick 		if (vget(ITOV(ip)))
10839440Smckusick 			goto loop;
10939392Smckusick 		*ipp = ip;
11039392Smckusick 		return(0);
11139392Smckusick 	}
11239440Smckusick 	/*
11339440Smckusick 	 * Allocate a new inode.
11439440Smckusick 	 */
11539440Smckusick 	if (error = getnewvnode(VT_UFS, mntp, &ufs_vnodeops, &nvp)) {
11637736Smckusick 		*ipp = 0;
11737736Smckusick 		return (error);
11837736Smckusick 	}
11939440Smckusick 	ip = VTOI(nvp);
12039440Smckusick 	ip->i_vnode = nvp;
12139440Smckusick 	ip->i_flag = 0;
12239440Smckusick 	ip->i_devvp = 0;
12339440Smckusick 	ip->i_mode = 0;
12439879Smckusick 	ip->i_diroff = 0;
12539440Smckusick #ifdef QUOTA
12639440Smckusick 	ip->i_dquot = NODQUOT;
12739440Smckusick #endif
12837736Smckusick 	/*
12939440Smckusick 	 * Put it onto its hash chain and lock it so that other requests for
13039440Smckusick 	 * this inode will block if they arrive while we are sleeping waiting
13139440Smckusick 	 * for old data structures to be purged or for the contents of the
13239440Smckusick 	 * disk portion of this inode to be read.
13339440Smckusick 	 */
13439440Smckusick 	ip->i_dev = dev;
13539440Smckusick 	ip->i_number = ino;
13639440Smckusick 	insque(ip, ih);
13739440Smckusick 	ILOCK(ip);
13839440Smckusick 	/*
13937736Smckusick 	 * Read in the disk contents for the inode.
14037736Smckusick 	 */
14137736Smckusick 	if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)),
14238776Smckusick 	    (int)fs->fs_bsize, NOCRED, &bp)) {
14337736Smckusick 		/*
14439392Smckusick 		 * Unlock and discard unneeded inode.
14537736Smckusick 		 */
14639440Smckusick 		iput(ip);
14737736Smckusick 		brelse(bp);
14837736Smckusick 		*ipp = 0;
14939440Smckusick 		return (error);
15037736Smckusick 	}
15139440Smckusick 	dp = bp->b_un.b_dino;
15239440Smckusick 	dp += itoo(fs, ino);
15339440Smckusick 	ip->i_din = *dp;
15439440Smckusick 	brelse(bp);
15537736Smckusick 	/*
15639440Smckusick 	 * Initialize the associated vnode
15737736Smckusick 	 */
15839440Smckusick 	vp = ITOV(ip);
15939440Smckusick 	vp->v_type = IFTOVT(ip->i_mode);
160*40289Smckusick 	if (vp->v_type == VFIFO) {
161*40289Smckusick #ifdef FIFO
162*40289Smckusick 		extern struct vnodeops fifo_inodeops;
163*40289Smckusick 		vp->v_op = &fifo_inodeops;
164*40289Smckusick #else
165*40289Smckusick 		iput(ip);
166*40289Smckusick 		*ipp = 0;
167*40289Smckusick 		return (EOPNOTSUPP);
168*40289Smckusick #endif /* FIFO */
169*40289Smckusick 	}
17039440Smckusick 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
17139440Smckusick 		vp->v_op = &spec_inodeops;
17239617Smckusick 		if (nvp = checkalias(vp, ip->i_rdev, mntp)) {
17337736Smckusick 			/*
17439440Smckusick 			 * Reinitialize aliased inode.
17537736Smckusick 			 */
17639440Smckusick 			vp = nvp;
17739440Smckusick 			iq = VTOI(vp);
17839440Smckusick 			iq->i_vnode = vp;
17939517Smckusick 			iq->i_flag = 0;
18039440Smckusick 			ILOCK(iq);
18139440Smckusick 			iq->i_din = ip->i_din;
18239440Smckusick 			iq->i_dev = dev;
18339440Smckusick 			iq->i_number = ino;
18439440Smckusick 			insque(iq, ih);
18537736Smckusick 			/*
18639440Smckusick 			 * Discard unneeded vnode
18737736Smckusick 			 */
18839440Smckusick 			ip->i_mode = 0;
18939440Smckusick 			iput(ip);
19037736Smckusick 			ip = iq;
19137736Smckusick 		}
19237736Smckusick 	}
19339440Smckusick 	if (ino == ROOTINO)
19439440Smckusick 		vp->v_flag |= VROOT;
19537736Smckusick 	/*
19637736Smckusick 	 * Finish inode initialization.
19737736Smckusick 	 */
19837736Smckusick 	ip->i_fs = fs;
19937736Smckusick 	ip->i_devvp = VFSTOUFS(mntp)->um_devvp;
20038345Smckusick 	VREF(ip->i_devvp);
20137736Smckusick #ifdef QUOTA
20237736Smckusick 	if (ip->i_mode != 0)
20337736Smckusick 		ip->i_dquot = inoquota(ip);
20437736Smckusick #endif
20538256Smckusick 	/*
20638256Smckusick 	 * Set up a generation number for this inode if it does not
20738256Smckusick 	 * already have one. This should only happen on old filesystems.
20838256Smckusick 	 */
20938256Smckusick 	if (ip->i_gen == 0) {
21038256Smckusick 		if (++nextgennumber < (u_long)time.tv_sec)
21138256Smckusick 			nextgennumber = time.tv_sec;
21238256Smckusick 		ip->i_gen = nextgennumber;
21338256Smckusick 		if ((vp->v_mount->m_flag & M_RDONLY) == 0)
21438256Smckusick 			ip->i_flag |= IMOD;
21538256Smckusick 	}
21637736Smckusick 	*ipp = ip;
21737736Smckusick 	return (0);
21837736Smckusick }
2197334Skre 
22037736Smckusick /*
22139392Smckusick  * Unlock and decrement the reference count of an inode structure.
22224Sbill  */
22324Sbill iput(ip)
2244818Swnj 	register struct inode *ip;
22524Sbill {
2267118Smckusick 
2278452Sroot 	if ((ip->i_flag & ILOCKED) == 0)
2287118Smckusick 		panic("iput");
22916665Smckusick 	IUNLOCK(ip);
23037736Smckusick 	vrele(ITOV(ip));
2317118Smckusick }
2327118Smckusick 
23339392Smckusick /*
23439392Smckusick  * Last reference to an inode, write the inode out and if necessary,
23539392Smckusick  * truncate and deallocate the file.
23639392Smckusick  */
23737736Smckusick ufs_inactive(vp)
23837736Smckusick 	struct vnode *vp;
2397118Smckusick {
24037736Smckusick 	register struct inode *ip = VTOI(vp);
24139392Smckusick 	int mode, error = 0;
24224Sbill 
24339816Smckusick 	if (prtactive && vp->v_usecount != 0)
24439676Smckusick 		vprint("ufs_inactive: pushing active", vp);
24538452Smckusick 	/*
24638452Smckusick 	 * Get rid of inodes related to stale file handles.
24738452Smckusick 	 */
24839440Smckusick 	if (ip->i_mode == 0) {
24939676Smckusick 		if ((vp->v_flag & VXLOCK) == 0)
25039676Smckusick 			vgone(vp);
25139440Smckusick 		return (0);
25239440Smckusick 	}
25338226Smckusick 	ILOCK(ip);
25439364Smckusick 	if (ip->i_nlink <= 0 && (vp->v_mount->m_flag & M_RDONLY) == 0) {
25539676Smckusick 		error = itrunc(ip, (u_long)0, 0);
25637736Smckusick 		mode = ip->i_mode;
25737736Smckusick 		ip->i_mode = 0;
25837736Smckusick 		ip->i_rdev = 0;
25937736Smckusick 		ip->i_flag |= IUPD|ICHG;
26037736Smckusick 		ifree(ip, ip->i_number, mode);
2617651Ssam #ifdef QUOTA
26237736Smckusick 		(void) chkiq(ip->i_dev, ip, ip->i_uid, 0);
26337736Smckusick 		dqrele(ip->i_dquot);
26437736Smckusick 		ip->i_dquot = NODQUOT;
2657492Skre #endif
26637736Smckusick 	}
26737736Smckusick 	IUPDAT(ip, &time, &time, 0);
26840033Smckusick 	IUNLOCK(ip);
26940033Smckusick 	ip->i_flag = 0;
27037736Smckusick 	/*
27139392Smckusick 	 * If we are done with the inode, reclaim it
27239392Smckusick 	 * so that it can be reused immediately.
27337736Smckusick 	 */
27440033Smckusick 	if (vp->v_usecount == 0 && ip->i_mode == 0 &&
27540033Smckusick 	    (vp->v_flag & VXLOCK) == 0)
27640033Smckusick 		vgone(vp);
27737736Smckusick 	return (error);
27824Sbill }
27924Sbill 
28024Sbill /*
28139392Smckusick  * Reclaim an inode so that it can be used for other purposes.
28224Sbill  */
28339392Smckusick ufs_reclaim(vp)
28439392Smckusick 	register struct vnode *vp;
28539392Smckusick {
28639492Smckusick 	register struct inode *ip = VTOI(vp);
28739392Smckusick 
28839816Smckusick 	if (prtactive && vp->v_usecount != 0)
28939676Smckusick 		vprint("ufs_reclaim: pushing active", vp);
29039392Smckusick 	/*
29139392Smckusick 	 * Remove the inode from its hash chain.
29239392Smckusick 	 */
29339392Smckusick 	remque(ip);
29439392Smckusick 	ip->i_forw = ip;
29539392Smckusick 	ip->i_back = ip;
29639392Smckusick 	/*
29739392Smckusick 	 * Purge old data structures associated with the inode.
29839392Smckusick 	 */
29939392Smckusick 	cache_purge(vp);
30039392Smckusick 	if (ip->i_devvp) {
30139392Smckusick 		vrele(ip->i_devvp);
30239392Smckusick 		ip->i_devvp = 0;
30339392Smckusick 	}
30439392Smckusick #ifdef QUOTA
30539392Smckusick 	dqrele(ip->i_dquot);
30639392Smckusick 	ip->i_dquot = NODQUOT;
30739392Smckusick #endif
30839392Smckusick 	ip->i_flag = 0;
30939392Smckusick 	return (0);
31039392Smckusick }
31139392Smckusick 
31239392Smckusick /*
31339392Smckusick  * Check accessed and update flags on an inode structure.
31439392Smckusick  * If any is on, update the inode with the current time.
31539392Smckusick  * If waitfor is given, then must ensure I/O order,
31639392Smckusick  * so wait for write to complete.
31739392Smckusick  */
3181203Sbill iupdat(ip, ta, tm, waitfor)
3194818Swnj 	register struct inode *ip;
3208630Sroot 	struct timeval *ta, *tm;
3214818Swnj 	int waitfor;
32224Sbill {
32337736Smckusick 	struct buf *bp;
32437736Smckusick 	struct vnode *vp = ITOV(ip);
32524Sbill 	struct dinode *dp;
32630749Skarels 	register struct fs *fs;
32737736Smckusick 	int error;
32824Sbill 
32930749Skarels 	fs = ip->i_fs;
33037736Smckusick 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
33137736Smckusick 		return (0);
33237736Smckusick 	if (vp->v_mount->m_flag & M_RDONLY)
33337736Smckusick 		return (0);
33437736Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
33538776Smckusick 		(int)fs->fs_bsize, NOCRED, &bp);
33637736Smckusick 	if (error) {
33737736Smckusick 		brelse(bp);
33837736Smckusick 		return (error);
33924Sbill 	}
34037736Smckusick 	if (ip->i_flag&IACC)
34137736Smckusick 		ip->i_atime = ta->tv_sec;
34237736Smckusick 	if (ip->i_flag&IUPD)
34337736Smckusick 		ip->i_mtime = tm->tv_sec;
34437736Smckusick 	if (ip->i_flag&ICHG)
34537736Smckusick 		ip->i_ctime = time.tv_sec;
34637736Smckusick 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
34737736Smckusick 	dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
34839392Smckusick 	*dp = ip->i_din;
34937736Smckusick 	if (waitfor) {
35037736Smckusick 		return (bwrite(bp));
35137736Smckusick 	} else {
35237736Smckusick 		bdwrite(bp);
35337736Smckusick 		return (0);
35437736Smckusick 	}
35524Sbill }
35624Sbill 
35710736Ssam #define	SINGLE	0	/* index of single indirect block */
35810736Ssam #define	DOUBLE	1	/* index of double indirect block */
35910736Ssam #define	TRIPLE	2	/* index of triple indirect block */
36024Sbill /*
36139392Smckusick  * Truncate the inode ip to at most length size.  Free affected disk
36239392Smckusick  * blocks -- the blocks of the file are removed in reverse order.
36310736Ssam  *
36410736Ssam  * NB: triple indirect blocks are untested.
36524Sbill  */
36639676Smckusick itrunc(oip, length, flags)
36717942Smckusick 	register struct inode *oip;
3689165Ssam 	u_long length;
36939676Smckusick 	int flags;
37024Sbill {
3719165Ssam 	register daddr_t lastblock;
37226272Skarels 	daddr_t bn, lbn, lastiblock[NIADDR];
3736569Smckusic 	register struct fs *fs;
37410736Ssam 	register struct inode *ip;
37517942Smckusick 	struct buf *bp;
37637736Smckusick 	int offset, osize, size, level;
37737736Smckusick 	long count, nblocks, blocksreleased = 0;
37817942Smckusick 	register int i;
37939676Smckusick 	int aflags, error, allerror;
38010736Ssam 	struct inode tip;
3819165Ssam 
38213000Ssam 	if (oip->i_size <= length) {
38313000Ssam 		oip->i_flag |= ICHG|IUPD;
38437736Smckusick 		error = iupdat(oip, &time, &time, 1);
38537736Smckusick 		return (error);
38613000Ssam 	}
3871203Sbill 	/*
38810736Ssam 	 * Calculate index into inode's block list of
38910736Ssam 	 * last direct and indirect blocks (if any)
39010736Ssam 	 * which we want to keep.  Lastblock is -1 when
39110736Ssam 	 * the file is truncated to 0.
3921203Sbill 	 */
39310736Ssam 	fs = oip->i_fs;
3949165Ssam 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
39510736Ssam 	lastiblock[SINGLE] = lastblock - NDADDR;
39610736Ssam 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
39710736Ssam 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
39812645Ssam 	nblocks = btodb(fs->fs_bsize);
3996569Smckusic 	/*
40017942Smckusick 	 * Update the size of the file. If the file is not being
40117942Smckusick 	 * truncated to a block boundry, the contents of the
40217942Smckusick 	 * partial block following the end of the file must be
40317942Smckusick 	 * zero'ed in case it ever become accessable again because
40417942Smckusick 	 * of subsequent file growth.
40517942Smckusick 	 */
40617942Smckusick 	osize = oip->i_size;
40717942Smckusick 	offset = blkoff(fs, length);
40817942Smckusick 	if (offset == 0) {
40917942Smckusick 		oip->i_size = length;
41017942Smckusick 	} else {
41117942Smckusick 		lbn = lblkno(fs, length);
41239676Smckusick 		aflags = B_CLRBUF;
41339676Smckusick 		if (flags & IO_SYNC)
41439676Smckusick 			aflags |= B_SYNC;
41539676Smckusick 		if (error = balloc(oip, lbn, offset, &bp, aflags))
41637736Smckusick 			return (error);
41717942Smckusick 		oip->i_size = length;
41817942Smckusick 		size = blksize(fs, oip, lbn);
41939676Smckusick 		bn = bp->b_blkno;
42030749Skarels 		count = howmany(size, CLBYTES);
42130749Skarels 		for (i = 0; i < count; i++)
42237736Smckusick 			munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
42326272Skarels 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
42439676Smckusick 		brealloc(bp, size);
42539676Smckusick 		if (flags & IO_SYNC)
42639676Smckusick 			bwrite(bp);
42739676Smckusick 		else
42839676Smckusick 			bdwrite(bp);
42917942Smckusick 	}
43017942Smckusick 	/*
43117942Smckusick 	 * Update file and block pointers
43210736Ssam 	 * on disk before we start freeing blocks.
43310736Ssam 	 * If we crash before free'ing blocks below,
43410736Ssam 	 * the blocks will be returned to the free list.
43510736Ssam 	 * lastiblock values are also normalized to -1
43610736Ssam 	 * for calls to indirtrunc below.
4376569Smckusic 	 */
43810736Ssam 	tip = *oip;
43917942Smckusick 	tip.i_size = osize;
44010736Ssam 	for (level = TRIPLE; level >= SINGLE; level--)
44110736Ssam 		if (lastiblock[level] < 0) {
44210736Ssam 			oip->i_ib[level] = 0;
44310736Ssam 			lastiblock[level] = -1;
4449165Ssam 		}
44510736Ssam 	for (i = NDADDR - 1; i > lastblock; i--)
44610736Ssam 		oip->i_db[i] = 0;
44710736Ssam 	oip->i_flag |= ICHG|IUPD;
44839676Smckusick 	vinvalbuf(ITOV(oip), (length > 0));
44939740Smckusick 	allerror = iupdat(oip, &time, &time, MNT_WAIT);
45010736Ssam 
4516569Smckusic 	/*
45210736Ssam 	 * Indirect blocks first.
4536569Smckusic 	 */
45417942Smckusick 	ip = &tip;
45510736Ssam 	for (level = TRIPLE; level >= SINGLE; level--) {
45610736Ssam 		bn = ip->i_ib[level];
4579165Ssam 		if (bn != 0) {
45837736Smckusick 			error = indirtrunc(ip, bn, lastiblock[level], level,
45937736Smckusick 				&count);
46037736Smckusick 			if (error)
46137736Smckusick 				allerror = error;
46237736Smckusick 			blocksreleased += count;
46310736Ssam 			if (lastiblock[level] < 0) {
46410736Ssam 				ip->i_ib[level] = 0;
46531402Smckusick 				blkfree(ip, bn, (off_t)fs->fs_bsize);
46610736Ssam 				blocksreleased += nblocks;
46710736Ssam 			}
46810736Ssam 		}
46910736Ssam 		if (lastiblock[level] >= 0)
47010736Ssam 			goto done;
4719165Ssam 	}
47210736Ssam 
4736569Smckusic 	/*
47410736Ssam 	 * All whole direct blocks or frags.
4756569Smckusic 	 */
4769165Ssam 	for (i = NDADDR - 1; i > lastblock; i--) {
47726359Skarels 		register off_t bsize;
4789165Ssam 
4796569Smckusic 		bn = ip->i_db[i];
4809165Ssam 		if (bn == 0)
48124Sbill 			continue;
4829165Ssam 		ip->i_db[i] = 0;
48324525Sbloom 		bsize = (off_t)blksize(fs, ip, i);
48431402Smckusick 		blkfree(ip, bn, bsize);
48524525Sbloom 		blocksreleased += btodb(bsize);
48624Sbill 	}
48710736Ssam 	if (lastblock < 0)
48810736Ssam 		goto done;
48910736Ssam 
4901203Sbill 	/*
4919165Ssam 	 * Finally, look for a change in size of the
4929165Ssam 	 * last direct block; release any frags.
4931203Sbill 	 */
49410736Ssam 	bn = ip->i_db[lastblock];
49510736Ssam 	if (bn != 0) {
49626359Skarels 		off_t oldspace, newspace;
49710736Ssam 
4989165Ssam 		/*
4999165Ssam 		 * Calculate amount of space we're giving
5009165Ssam 		 * back as old block size minus new block size.
5019165Ssam 		 */
50210736Ssam 		oldspace = blksize(fs, ip, lastblock);
5039165Ssam 		ip->i_size = length;
50410736Ssam 		newspace = blksize(fs, ip, lastblock);
50510736Ssam 		if (newspace == 0)
50610736Ssam 			panic("itrunc: newspace");
50710736Ssam 		if (oldspace - newspace > 0) {
5089165Ssam 			/*
5099165Ssam 			 * Block number of space to be free'd is
5109165Ssam 			 * the old block # plus the number of frags
5119165Ssam 			 * required for the storage we're keeping.
5129165Ssam 			 */
51310736Ssam 			bn += numfrags(fs, newspace);
51431402Smckusick 			blkfree(ip, bn, oldspace - newspace);
51512645Ssam 			blocksreleased += btodb(oldspace - newspace);
5169165Ssam 		}
5179165Ssam 	}
5189165Ssam done:
51910736Ssam /* BEGIN PARANOIA */
52010736Ssam 	for (level = SINGLE; level <= TRIPLE; level++)
52110736Ssam 		if (ip->i_ib[level] != oip->i_ib[level])
52210736Ssam 			panic("itrunc1");
52310736Ssam 	for (i = 0; i < NDADDR; i++)
52410736Ssam 		if (ip->i_db[i] != oip->i_db[i])
52510736Ssam 			panic("itrunc2");
52610736Ssam /* END PARANOIA */
52712645Ssam 	oip->i_blocks -= blocksreleased;
52812645Ssam 	if (oip->i_blocks < 0)			/* sanity */
52912645Ssam 		oip->i_blocks = 0;
53012645Ssam 	oip->i_flag |= ICHG;
5319165Ssam #ifdef QUOTA
53212645Ssam 	(void) chkdq(oip, -blocksreleased, 0);
5339165Ssam #endif
53437736Smckusick 	return (allerror);
53524Sbill }
53624Sbill 
5379165Ssam /*
5389165Ssam  * Release blocks associated with the inode ip and
5399165Ssam  * stored in the indirect block bn.  Blocks are free'd
5409165Ssam  * in LIFO order up to (but not including) lastbn.  If
54110736Ssam  * level is greater than SINGLE, the block is an indirect
54210736Ssam  * block and recursive calls to indirtrunc must be used to
54310736Ssam  * cleanse other indirect blocks.
54410736Ssam  *
54510736Ssam  * NB: triple indirect blocks are untested.
5469165Ssam  */
54737736Smckusick indirtrunc(ip, bn, lastbn, level, countp)
5486569Smckusic 	register struct inode *ip;
5499165Ssam 	daddr_t bn, lastbn;
55010736Ssam 	int level;
55137736Smckusick 	long *countp;
55224Sbill {
5539165Ssam 	register int i;
55431661Smckusick 	struct buf *bp;
55531661Smckusick 	register struct fs *fs = ip->i_fs;
55624Sbill 	register daddr_t *bap;
55731661Smckusick 	daddr_t *copy, nb, last;
55837736Smckusick 	long blkcount, factor;
55937736Smckusick 	int nblocks, blocksreleased = 0;
56037736Smckusick 	int error, allerror = 0;
56124Sbill 
56210736Ssam 	/*
56310736Ssam 	 * Calculate index in current block of last
56410736Ssam 	 * block to be kept.  -1 indicates the entire
56510736Ssam 	 * block so we need not calculate the index.
56610736Ssam 	 */
56710736Ssam 	factor = 1;
56810736Ssam 	for (i = SINGLE; i < level; i++)
56910736Ssam 		factor *= NINDIR(fs);
5709165Ssam 	last = lastbn;
57110736Ssam 	if (lastbn > 0)
57210736Ssam 		last /= factor;
57312645Ssam 	nblocks = btodb(fs->fs_bsize);
57410736Ssam 	/*
57510736Ssam 	 * Get buffer of block pointers, zero those
57610736Ssam 	 * entries corresponding to blocks to be free'd,
57710736Ssam 	 * and update on disk copy first.
57810736Ssam 	 */
57938776Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
58038776Smckusick 		NOCRED, &bp);
58137736Smckusick 	if (error) {
58210736Ssam 		brelse(bp);
58337736Smckusick 		*countp = 0;
58437736Smckusick 		return (error);
58510736Ssam 	}
58610736Ssam 	bap = bp->b_un.b_daddr;
58731661Smckusick 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
58831661Smckusick 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
58910736Ssam 	bzero((caddr_t)&bap[last + 1],
59010736Ssam 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
59139676Smckusick 	if (last == -1)
59239676Smckusick 		bp->b_flags |= B_INVAL;
59337736Smckusick 	error = bwrite(bp);
59437736Smckusick 	if (error)
59537736Smckusick 		allerror = error;
59631661Smckusick 	bap = copy;
59710736Ssam 
59810736Ssam 	/*
59910736Ssam 	 * Recursively free totally unused blocks.
60010736Ssam 	 */
6019165Ssam 	for (i = NINDIR(fs) - 1; i > last; i--) {
60224Sbill 		nb = bap[i];
6039165Ssam 		if (nb == 0)
60424Sbill 			continue;
60537736Smckusick 		if (level > SINGLE) {
60637736Smckusick 			error = indirtrunc(ip, nb, (daddr_t)-1, level - 1,
60737736Smckusick 				&blkcount);
60837736Smckusick 			if (error)
60937736Smckusick 				allerror = error;
61037736Smckusick 			blocksreleased += blkcount;
61137736Smckusick 		}
61231402Smckusick 		blkfree(ip, nb, (off_t)fs->fs_bsize);
6139165Ssam 		blocksreleased += nblocks;
61424Sbill 	}
61510736Ssam 
61610736Ssam 	/*
61710736Ssam 	 * Recursively free last partial block.
61810736Ssam 	 */
61910736Ssam 	if (level > SINGLE && lastbn >= 0) {
62010736Ssam 		last = lastbn % factor;
6219165Ssam 		nb = bap[i];
62237736Smckusick 		if (nb != 0) {
62337736Smckusick 			error = indirtrunc(ip, nb, last, level - 1, &blkcount);
62437736Smckusick 			if (error)
62537736Smckusick 				allerror = error;
62637736Smckusick 			blocksreleased += blkcount;
62737736Smckusick 		}
6289165Ssam 	}
62931661Smckusick 	FREE(copy, M_TEMP);
63037736Smckusick 	*countp = blocksreleased;
63137736Smckusick 	return (allerror);
63224Sbill }
63324Sbill 
63424Sbill /*
6354818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
6363617Sroot  */
6374818Swnj ilock(ip)
6384818Swnj 	register struct inode *ip;
6393617Sroot {
6403617Sroot 
64137736Smckusick 	while (ip->i_flag & ILOCKED) {
64237736Smckusick 		ip->i_flag |= IWANT;
64339795Smckusick 		if (ip->i_spare0 == u.u_procp->p_pid)
64439795Smckusick 			panic("locking against myself");
64539795Smckusick 		ip->i_spare1 = u.u_procp->p_pid;
64637736Smckusick 		(void) sleep((caddr_t)ip, PINOD);
64737736Smckusick 	}
64839795Smckusick 	ip->i_spare1 = 0;
64939795Smckusick 	ip->i_spare0 = u.u_procp->p_pid;
65039795Smckusick 	u.u_spare[0]++;
65137736Smckusick 	ip->i_flag |= ILOCKED;
6523617Sroot }
6533617Sroot 
6543617Sroot /*
6554818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
6563617Sroot  */
6577118Smckusick iunlock(ip)
6584818Swnj 	register struct inode *ip;
6593617Sroot {
6603617Sroot 
66137736Smckusick 	if ((ip->i_flag & ILOCKED) == 0)
66239676Smckusick 		vprint("iunlock: unlocked inode", ITOV(ip));
66339795Smckusick 	ip->i_spare0 = 0;
66439795Smckusick 	u.u_spare[0]--;
66537736Smckusick 	ip->i_flag &= ~ILOCKED;
66637736Smckusick 	if (ip->i_flag&IWANT) {
66737736Smckusick 		ip->i_flag &= ~IWANT;
66837736Smckusick 		wakeup((caddr_t)ip);
66937736Smckusick 	}
6703617Sroot }
67137736Smckusick 
67237736Smckusick /*
67337736Smckusick  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
67437736Smckusick  * The mode is shifted to select the owner/group/other fields. The
67537736Smckusick  * super user is granted all permissions.
67637736Smckusick  *
67737736Smckusick  * NB: Called from vnode op table. It seems this could all be done
67837736Smckusick  * using vattr's but...
67937736Smckusick  */
68037736Smckusick iaccess(ip, mode, cred)
68137736Smckusick 	register struct inode *ip;
68237736Smckusick 	register int mode;
68337736Smckusick 	struct ucred *cred;
68437736Smckusick {
68537736Smckusick 	register gid_t *gp;
68637736Smckusick 	int i;
68737736Smckusick 
68837736Smckusick 	/*
68939392Smckusick 	 * If you're the super-user, you always get access.
69037736Smckusick 	 */
69137736Smckusick 	if (cred->cr_uid == 0)
69237736Smckusick 		return (0);
69337736Smckusick 	/*
69437736Smckusick 	 * Access check is based on only one of owner, group, public.
69537736Smckusick 	 * If not owner, then check group. If not a member of the
69637736Smckusick 	 * group, then check public access.
69737736Smckusick 	 */
69837736Smckusick 	if (cred->cr_uid != ip->i_uid) {
69937736Smckusick 		mode >>= 3;
70037736Smckusick 		gp = cred->cr_groups;
70137736Smckusick 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
70237736Smckusick 			if (ip->i_gid == *gp)
70337736Smckusick 				goto found;
70437736Smckusick 		mode >>= 3;
70537736Smckusick found:
70637736Smckusick 		;
70737736Smckusick 	}
70837736Smckusick 	if ((ip->i_mode & mode) != 0)
70937736Smckusick 		return (0);
71037736Smckusick 	return (EACCES);
71137736Smckusick }
712