xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 39492)
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*39492Smckusick  *	@(#)lfs_inode.c	7.17 (Berkeley) 11/03/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 
4924Sbill /*
5039392Smckusick  * Initialize hash links for inodes.
5124Sbill  */
5239440Smckusick ufs_init()
5324Sbill {
5424Sbill 	register int i;
5539392Smckusick 	register union ihead *ih = ihead;
5624Sbill 
57*39492Smckusick #ifndef lint
5839392Smckusick 	if (VN_MAXPRIVATE < sizeof(struct inode))
5939392Smckusick 		panic("ihinit: too small");
60*39492Smckusick #endif /* not lint */
617334Skre 	for (i = INOHSZ; --i >= 0; ih++) {
627334Skre 		ih->ih_head[0] = ih;
637334Skre 		ih->ih_head[1] = ih;
647334Skre 	}
6524Sbill }
6624Sbill 
6724Sbill /*
6837736Smckusick  * Look up an vnode/inode by device,inumber.
6924Sbill  * If it is in core (in the inode structure),
7024Sbill  * honor the locking protocol.
7124Sbill  * If it is not in core, read it in from the
7224Sbill  * specified device.
7337736Smckusick  * Callers must check for mount points!!
7424Sbill  * In all cases, a pointer to a locked
7524Sbill  * inode structure is returned.
7624Sbill  */
7737736Smckusick iget(xp, ino, ipp)
7837736Smckusick 	struct inode *xp;
794818Swnj 	ino_t ino;
8037736Smckusick 	struct inode **ipp;
8124Sbill {
8237736Smckusick 	dev_t dev = xp->i_dev;
8337736Smckusick 	struct mount *mntp = ITOV(xp)->v_mount;
8437736Smckusick 	register struct fs *fs = VFSTOUFS(mntp)->um_fs;
8539440Smckusick 	extern struct vnodeops ufs_vnodeops, spec_inodeops;
8637736Smckusick 	register struct inode *ip, *iq;
8737736Smckusick 	register struct vnode *vp;
8839440Smckusick 	struct vnode *nvp;
8937736Smckusick 	struct buf *bp;
9039440Smckusick 	struct dinode *dp;
9137736Smckusick 	union  ihead *ih;
9237736Smckusick 	int error;
9324Sbill 
9439440Smckusick 	ih = &ihead[INOHASH(dev, ino)];
9524Sbill loop:
9639392Smckusick 	for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw) {
9739392Smckusick 		if (ino != ip->i_number || dev != ip->i_dev)
9839392Smckusick 			continue;
9939392Smckusick 		if ((ip->i_flag&ILOCKED) != 0) {
10039392Smckusick 			ip->i_flag |= IWANT;
10139392Smckusick 			sleep((caddr_t)ip, PINOD);
10239392Smckusick 			goto loop;
10339392Smckusick 		}
10439440Smckusick 		if (vget(ITOV(ip)))
10539440Smckusick 			goto loop;
10639392Smckusick 		*ipp = ip;
10739392Smckusick 		return(0);
10839392Smckusick 	}
10939440Smckusick 	/*
11039440Smckusick 	 * Allocate a new inode.
11139440Smckusick 	 */
11239440Smckusick 	if (error = getnewvnode(VT_UFS, mntp, &ufs_vnodeops, &nvp)) {
11337736Smckusick 		*ipp = 0;
11437736Smckusick 		return (error);
11537736Smckusick 	}
11639440Smckusick 	ip = VTOI(nvp);
11739440Smckusick 	ip->i_vnode = nvp;
11839440Smckusick 	ip->i_flag = 0;
11939440Smckusick 	ip->i_devvp = 0;
12039440Smckusick 	ip->i_lastr = 0;
12139440Smckusick 	ip->i_mode = 0;
12239440Smckusick 	ip->i_flags = 0;
12339440Smckusick #ifdef QUOTA
12439440Smckusick 	ip->i_dquot = NODQUOT;
12539440Smckusick #endif
12637736Smckusick 	/*
12739440Smckusick 	 * Put it onto its hash chain and lock it so that other requests for
12839440Smckusick 	 * this inode will block if they arrive while we are sleeping waiting
12939440Smckusick 	 * for old data structures to be purged or for the contents of the
13039440Smckusick 	 * disk portion of this inode to be read.
13139440Smckusick 	 */
13239440Smckusick 	ip->i_dev = dev;
13339440Smckusick 	ip->i_number = ino;
13439440Smckusick 	insque(ip, ih);
13539440Smckusick 	ILOCK(ip);
13639440Smckusick 	/*
13737736Smckusick 	 * Read in the disk contents for the inode.
13837736Smckusick 	 */
13937736Smckusick 	if (error = bread(VFSTOUFS(mntp)->um_devvp, fsbtodb(fs, itod(fs, ino)),
14038776Smckusick 	    (int)fs->fs_bsize, NOCRED, &bp)) {
14137736Smckusick 		/*
14239392Smckusick 		 * Unlock and discard unneeded inode.
14337736Smckusick 		 */
14439440Smckusick 		iput(ip);
14537736Smckusick 		brelse(bp);
14637736Smckusick 		*ipp = 0;
14739440Smckusick 		return (error);
14837736Smckusick 	}
14939440Smckusick 	dp = bp->b_un.b_dino;
15039440Smckusick 	dp += itoo(fs, ino);
15139440Smckusick 	ip->i_din = *dp;
15239440Smckusick 	brelse(bp);
15337736Smckusick 	/*
15439440Smckusick 	 * Initialize the associated vnode
15537736Smckusick 	 */
15639440Smckusick 	vp = ITOV(ip);
15739440Smckusick 	vp->v_type = IFTOVT(ip->i_mode);
15839440Smckusick 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
15939440Smckusick 		vp->v_rdev = ip->i_rdev;
16039440Smckusick 		vp->v_op = &spec_inodeops;
16139440Smckusick 		if (nvp = checkalias(vp, mntp)) {
16237736Smckusick 			/*
16339440Smckusick 			 * Reinitialize aliased inode.
16437736Smckusick 			 */
16539440Smckusick 			vp = nvp;
16639440Smckusick 			iq = VTOI(vp);
16739440Smckusick 			iq->i_vnode = vp;
16839440Smckusick 			iq->i_lastr = 0;
16939440Smckusick 			iq->i_flags = 0;
17039440Smckusick 			ILOCK(iq);
17139440Smckusick 			iq->i_din = ip->i_din;
17239440Smckusick 			iq->i_dev = dev;
17339440Smckusick 			iq->i_number = ino;
17439440Smckusick 			insque(iq, ih);
17537736Smckusick 			/*
17639440Smckusick 			 * Discard unneeded vnode
17737736Smckusick 			 */
17839440Smckusick 			ip->i_mode = 0;
17939440Smckusick 			iput(ip);
18037736Smckusick 			ip = iq;
18137736Smckusick 		}
18237736Smckusick 	}
18339440Smckusick 	if (ino == ROOTINO)
18439440Smckusick 		vp->v_flag |= VROOT;
18537736Smckusick 	/*
18637736Smckusick 	 * Finish inode initialization.
18737736Smckusick 	 */
18837736Smckusick 	ip->i_fs = fs;
18937736Smckusick 	ip->i_devvp = VFSTOUFS(mntp)->um_devvp;
19038345Smckusick 	VREF(ip->i_devvp);
19137736Smckusick #ifdef QUOTA
19237736Smckusick 	if (ip->i_mode != 0)
19337736Smckusick 		ip->i_dquot = inoquota(ip);
19437736Smckusick #endif
19538256Smckusick 	/*
19638256Smckusick 	 * Set up a generation number for this inode if it does not
19738256Smckusick 	 * already have one. This should only happen on old filesystems.
19838256Smckusick 	 */
19938256Smckusick 	if (ip->i_gen == 0) {
20038256Smckusick 		if (++nextgennumber < (u_long)time.tv_sec)
20138256Smckusick 			nextgennumber = time.tv_sec;
20238256Smckusick 		ip->i_gen = nextgennumber;
20338256Smckusick 		if ((vp->v_mount->m_flag & M_RDONLY) == 0)
20438256Smckusick 			ip->i_flag |= IMOD;
20538256Smckusick 	}
20637736Smckusick 	*ipp = ip;
20737736Smckusick 	return (0);
20837736Smckusick }
2097334Skre 
21037736Smckusick /*
21139392Smckusick  * Unlock and decrement the reference count of an inode structure.
21224Sbill  */
21324Sbill iput(ip)
2144818Swnj 	register struct inode *ip;
21524Sbill {
2167118Smckusick 
2178452Sroot 	if ((ip->i_flag & ILOCKED) == 0)
2187118Smckusick 		panic("iput");
21916665Smckusick 	IUNLOCK(ip);
22037736Smckusick 	vrele(ITOV(ip));
2217118Smckusick }
2227118Smckusick 
22339392Smckusick /*
22439392Smckusick  * Last reference to an inode, write the inode out and if necessary,
22539392Smckusick  * truncate and deallocate the file.
22639392Smckusick  */
22737736Smckusick ufs_inactive(vp)
22837736Smckusick 	struct vnode *vp;
2297118Smckusick {
23037736Smckusick 	register struct inode *ip = VTOI(vp);
23139392Smckusick 	int mode, error = 0;
23224Sbill 
23339364Smckusick 	if (vp->v_count != 0)
23439440Smckusick 		printf("ufs_inactive: pushing active ino %d dev 0x%x\n",
23539440Smckusick 			ip->i_number, ip->i_dev);
23638452Smckusick 	/*
23738452Smckusick 	 * Get rid of inodes related to stale file handles.
23838452Smckusick 	 */
23939440Smckusick 	if (ip->i_mode == 0) {
24039440Smckusick 		vgone(vp);
24139440Smckusick 		return (0);
24239440Smckusick 	}
24338226Smckusick 	ILOCK(ip);
24439364Smckusick 	if (ip->i_nlink <= 0 && (vp->v_mount->m_flag & M_RDONLY) == 0) {
24537736Smckusick 		error = itrunc(ip, (u_long)0);
24637736Smckusick 		mode = ip->i_mode;
24737736Smckusick 		ip->i_mode = 0;
24837736Smckusick 		ip->i_rdev = 0;
24937736Smckusick 		ip->i_flag |= IUPD|ICHG;
25037736Smckusick 		ifree(ip, ip->i_number, mode);
2517651Ssam #ifdef QUOTA
25237736Smckusick 		(void) chkiq(ip->i_dev, ip, ip->i_uid, 0);
25337736Smckusick 		dqrele(ip->i_dquot);
25437736Smckusick 		ip->i_dquot = NODQUOT;
2557492Skre #endif
25637736Smckusick 	}
25737736Smckusick 	IUPDAT(ip, &time, &time, 0);
25837736Smckusick 	IUNLOCK(ip);
25937736Smckusick 	ip->i_flag = 0;
26037736Smckusick 	/*
26139392Smckusick 	 * If we are done with the inode, reclaim it
26239392Smckusick 	 * so that it can be reused immediately.
26337736Smckusick 	 */
26439440Smckusick 	if (vp->v_count == 0 && ip->i_mode == 0)
26539440Smckusick 		vgone(vp);
26637736Smckusick 	return (error);
26724Sbill }
26824Sbill 
26924Sbill /*
27039392Smckusick  * Reclaim an inode so that it can be used for other purposes.
27124Sbill  */
27239392Smckusick ufs_reclaim(vp)
27339392Smckusick 	register struct vnode *vp;
27439392Smckusick {
275*39492Smckusick 	register struct inode *ip = VTOI(vp);
27639392Smckusick 
27739392Smckusick 	if (vp->v_count != 0)
27839440Smckusick 		printf("ufs_reclaim: pushing active ino %d dev 0x%x\n",
27939440Smckusick 			ip->i_number, ip->i_dev);
28039392Smckusick 	/*
28139392Smckusick 	 * Remove the inode from its hash chain.
28239392Smckusick 	 */
28339392Smckusick 	remque(ip);
28439392Smckusick 	ip->i_forw = ip;
28539392Smckusick 	ip->i_back = ip;
28639392Smckusick 	/*
28739392Smckusick 	 * Purge old data structures associated with the inode.
28839392Smckusick 	 */
28939392Smckusick 	cache_purge(vp);
29039392Smckusick 	if (ip->i_devvp) {
29139392Smckusick 		vrele(ip->i_devvp);
29239392Smckusick 		ip->i_devvp = 0;
29339392Smckusick 	}
29439392Smckusick #ifdef QUOTA
29539392Smckusick 	dqrele(ip->i_dquot);
29639392Smckusick 	ip->i_dquot = NODQUOT;
29739392Smckusick #endif
29839392Smckusick 	ip->i_flag = 0;
29939392Smckusick 	return (0);
30039392Smckusick }
30139392Smckusick 
30239392Smckusick /*
30339392Smckusick  * Check accessed and update flags on an inode structure.
30439392Smckusick  * If any is on, update the inode with the current time.
30539392Smckusick  * If waitfor is given, then must ensure I/O order,
30639392Smckusick  * so wait for write to complete.
30739392Smckusick  */
3081203Sbill iupdat(ip, ta, tm, waitfor)
3094818Swnj 	register struct inode *ip;
3108630Sroot 	struct timeval *ta, *tm;
3114818Swnj 	int waitfor;
31224Sbill {
31337736Smckusick 	struct buf *bp;
31437736Smckusick 	struct vnode *vp = ITOV(ip);
31524Sbill 	struct dinode *dp;
31630749Skarels 	register struct fs *fs;
31737736Smckusick 	int error;
31824Sbill 
31930749Skarels 	fs = ip->i_fs;
32037736Smckusick 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
32137736Smckusick 		return (0);
32237736Smckusick 	if (vp->v_mount->m_flag & M_RDONLY)
32337736Smckusick 		return (0);
32437736Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, itod(fs, ip->i_number)),
32538776Smckusick 		(int)fs->fs_bsize, NOCRED, &bp);
32637736Smckusick 	if (error) {
32737736Smckusick 		brelse(bp);
32837736Smckusick 		return (error);
32924Sbill 	}
33037736Smckusick 	if (ip->i_flag&IACC)
33137736Smckusick 		ip->i_atime = ta->tv_sec;
33237736Smckusick 	if (ip->i_flag&IUPD)
33337736Smckusick 		ip->i_mtime = tm->tv_sec;
33437736Smckusick 	if (ip->i_flag&ICHG)
33537736Smckusick 		ip->i_ctime = time.tv_sec;
33637736Smckusick 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
33737736Smckusick 	dp = bp->b_un.b_dino + itoo(fs, ip->i_number);
33839392Smckusick 	*dp = ip->i_din;
33937736Smckusick 	if (waitfor) {
34037736Smckusick 		return (bwrite(bp));
34137736Smckusick 	} else {
34237736Smckusick 		bdwrite(bp);
34337736Smckusick 		return (0);
34437736Smckusick 	}
34524Sbill }
34624Sbill 
34710736Ssam #define	SINGLE	0	/* index of single indirect block */
34810736Ssam #define	DOUBLE	1	/* index of double indirect block */
34910736Ssam #define	TRIPLE	2	/* index of triple indirect block */
35024Sbill /*
35139392Smckusick  * Truncate the inode ip to at most length size.  Free affected disk
35239392Smckusick  * blocks -- the blocks of the file are removed in reverse order.
35310736Ssam  *
35410736Ssam  * NB: triple indirect blocks are untested.
35524Sbill  */
35610736Ssam itrunc(oip, length)
35717942Smckusick 	register struct inode *oip;
3589165Ssam 	u_long length;
35924Sbill {
3609165Ssam 	register daddr_t lastblock;
36126272Skarels 	daddr_t bn, lbn, lastiblock[NIADDR];
3626569Smckusic 	register struct fs *fs;
36310736Ssam 	register struct inode *ip;
36417942Smckusick 	struct buf *bp;
36537736Smckusick 	int offset, osize, size, level;
36637736Smckusick 	long count, nblocks, blocksreleased = 0;
36717942Smckusick 	register int i;
36837736Smckusick 	int error, allerror = 0;
36910736Ssam 	struct inode tip;
3709165Ssam 
37113000Ssam 	if (oip->i_size <= length) {
37213000Ssam 		oip->i_flag |= ICHG|IUPD;
37337736Smckusick 		error = iupdat(oip, &time, &time, 1);
37437736Smckusick 		return (error);
37513000Ssam 	}
3761203Sbill 	/*
37710736Ssam 	 * Calculate index into inode's block list of
37810736Ssam 	 * last direct and indirect blocks (if any)
37910736Ssam 	 * which we want to keep.  Lastblock is -1 when
38010736Ssam 	 * the file is truncated to 0.
3811203Sbill 	 */
38210736Ssam 	fs = oip->i_fs;
3839165Ssam 	lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
38410736Ssam 	lastiblock[SINGLE] = lastblock - NDADDR;
38510736Ssam 	lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
38610736Ssam 	lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
38712645Ssam 	nblocks = btodb(fs->fs_bsize);
3886569Smckusic 	/*
38917942Smckusick 	 * Update the size of the file. If the file is not being
39017942Smckusick 	 * truncated to a block boundry, the contents of the
39117942Smckusick 	 * partial block following the end of the file must be
39217942Smckusick 	 * zero'ed in case it ever become accessable again because
39317942Smckusick 	 * of subsequent file growth.
39417942Smckusick 	 */
39517942Smckusick 	osize = oip->i_size;
39617942Smckusick 	offset = blkoff(fs, length);
39717942Smckusick 	if (offset == 0) {
39817942Smckusick 		oip->i_size = length;
39917942Smckusick 	} else {
40017942Smckusick 		lbn = lblkno(fs, length);
40137736Smckusick 		error = balloc(oip, lbn, offset, &bn, B_CLRBUF);
40237736Smckusick 		if (error)
40337736Smckusick 			return (error);
40437736Smckusick 		if ((long)bn < 0)
40537736Smckusick 			panic("itrunc: hole");
40617942Smckusick 		oip->i_size = length;
40717942Smckusick 		size = blksize(fs, oip, lbn);
40830749Skarels 		count = howmany(size, CLBYTES);
40930749Skarels 		for (i = 0; i < count; i++)
41037736Smckusick 			munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
41138776Smckusick 		error = bread(oip->i_devvp, bn, size, NOCRED, &bp);
41237736Smckusick 		if (error) {
41317942Smckusick 			oip->i_size = osize;
41417942Smckusick 			brelse(bp);
41537736Smckusick 			return (error);
41617942Smckusick 		}
41726272Skarels 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
41817942Smckusick 		bdwrite(bp);
41917942Smckusick 	}
42017942Smckusick 	/*
42117942Smckusick 	 * Update file and block pointers
42210736Ssam 	 * on disk before we start freeing blocks.
42310736Ssam 	 * If we crash before free'ing blocks below,
42410736Ssam 	 * the blocks will be returned to the free list.
42510736Ssam 	 * lastiblock values are also normalized to -1
42610736Ssam 	 * for calls to indirtrunc below.
4276569Smckusic 	 */
42810736Ssam 	tip = *oip;
42917942Smckusick 	tip.i_size = osize;
43010736Ssam 	for (level = TRIPLE; level >= SINGLE; level--)
43110736Ssam 		if (lastiblock[level] < 0) {
43210736Ssam 			oip->i_ib[level] = 0;
43310736Ssam 			lastiblock[level] = -1;
4349165Ssam 		}
43510736Ssam 	for (i = NDADDR - 1; i > lastblock; i--)
43610736Ssam 		oip->i_db[i] = 0;
43710736Ssam 	oip->i_flag |= ICHG|IUPD;
43837736Smckusick 	allerror = syncip(oip);
43910736Ssam 
4406569Smckusic 	/*
44110736Ssam 	 * Indirect blocks first.
4426569Smckusic 	 */
44317942Smckusick 	ip = &tip;
44410736Ssam 	for (level = TRIPLE; level >= SINGLE; level--) {
44510736Ssam 		bn = ip->i_ib[level];
4469165Ssam 		if (bn != 0) {
44737736Smckusick 			error = indirtrunc(ip, bn, lastiblock[level], level,
44837736Smckusick 				&count);
44937736Smckusick 			if (error)
45037736Smckusick 				allerror = error;
45137736Smckusick 			blocksreleased += count;
45210736Ssam 			if (lastiblock[level] < 0) {
45310736Ssam 				ip->i_ib[level] = 0;
45431402Smckusick 				blkfree(ip, bn, (off_t)fs->fs_bsize);
45510736Ssam 				blocksreleased += nblocks;
45610736Ssam 			}
45710736Ssam 		}
45810736Ssam 		if (lastiblock[level] >= 0)
45910736Ssam 			goto done;
4609165Ssam 	}
46110736Ssam 
4626569Smckusic 	/*
46310736Ssam 	 * All whole direct blocks or frags.
4646569Smckusic 	 */
4659165Ssam 	for (i = NDADDR - 1; i > lastblock; i--) {
46626359Skarels 		register off_t bsize;
4679165Ssam 
4686569Smckusic 		bn = ip->i_db[i];
4699165Ssam 		if (bn == 0)
47024Sbill 			continue;
4719165Ssam 		ip->i_db[i] = 0;
47224525Sbloom 		bsize = (off_t)blksize(fs, ip, i);
47331402Smckusick 		blkfree(ip, bn, bsize);
47424525Sbloom 		blocksreleased += btodb(bsize);
47524Sbill 	}
47610736Ssam 	if (lastblock < 0)
47710736Ssam 		goto done;
47810736Ssam 
4791203Sbill 	/*
4809165Ssam 	 * Finally, look for a change in size of the
4819165Ssam 	 * last direct block; release any frags.
4821203Sbill 	 */
48310736Ssam 	bn = ip->i_db[lastblock];
48410736Ssam 	if (bn != 0) {
48526359Skarels 		off_t oldspace, newspace;
48610736Ssam 
4879165Ssam 		/*
4889165Ssam 		 * Calculate amount of space we're giving
4899165Ssam 		 * back as old block size minus new block size.
4909165Ssam 		 */
49110736Ssam 		oldspace = blksize(fs, ip, lastblock);
4929165Ssam 		ip->i_size = length;
49310736Ssam 		newspace = blksize(fs, ip, lastblock);
49410736Ssam 		if (newspace == 0)
49510736Ssam 			panic("itrunc: newspace");
49610736Ssam 		if (oldspace - newspace > 0) {
4979165Ssam 			/*
4989165Ssam 			 * Block number of space to be free'd is
4999165Ssam 			 * the old block # plus the number of frags
5009165Ssam 			 * required for the storage we're keeping.
5019165Ssam 			 */
50210736Ssam 			bn += numfrags(fs, newspace);
50331402Smckusick 			blkfree(ip, bn, oldspace - newspace);
50412645Ssam 			blocksreleased += btodb(oldspace - newspace);
5059165Ssam 		}
5069165Ssam 	}
5079165Ssam done:
50810736Ssam /* BEGIN PARANOIA */
50910736Ssam 	for (level = SINGLE; level <= TRIPLE; level++)
51010736Ssam 		if (ip->i_ib[level] != oip->i_ib[level])
51110736Ssam 			panic("itrunc1");
51210736Ssam 	for (i = 0; i < NDADDR; i++)
51310736Ssam 		if (ip->i_db[i] != oip->i_db[i])
51410736Ssam 			panic("itrunc2");
51510736Ssam /* END PARANOIA */
51612645Ssam 	oip->i_blocks -= blocksreleased;
51712645Ssam 	if (oip->i_blocks < 0)			/* sanity */
51812645Ssam 		oip->i_blocks = 0;
51912645Ssam 	oip->i_flag |= ICHG;
5209165Ssam #ifdef QUOTA
52112645Ssam 	(void) chkdq(oip, -blocksreleased, 0);
5229165Ssam #endif
52337736Smckusick 	return (allerror);
52424Sbill }
52524Sbill 
5269165Ssam /*
5279165Ssam  * Release blocks associated with the inode ip and
5289165Ssam  * stored in the indirect block bn.  Blocks are free'd
5299165Ssam  * in LIFO order up to (but not including) lastbn.  If
53010736Ssam  * level is greater than SINGLE, the block is an indirect
53110736Ssam  * block and recursive calls to indirtrunc must be used to
53210736Ssam  * cleanse other indirect blocks.
53310736Ssam  *
53410736Ssam  * NB: triple indirect blocks are untested.
5359165Ssam  */
53637736Smckusick indirtrunc(ip, bn, lastbn, level, countp)
5376569Smckusic 	register struct inode *ip;
5389165Ssam 	daddr_t bn, lastbn;
53910736Ssam 	int level;
54037736Smckusick 	long *countp;
54124Sbill {
5429165Ssam 	register int i;
54331661Smckusick 	struct buf *bp;
54431661Smckusick 	register struct fs *fs = ip->i_fs;
54524Sbill 	register daddr_t *bap;
54631661Smckusick 	daddr_t *copy, nb, last;
54737736Smckusick 	long blkcount, factor;
54837736Smckusick 	int nblocks, blocksreleased = 0;
54937736Smckusick 	int error, allerror = 0;
55024Sbill 
55110736Ssam 	/*
55210736Ssam 	 * Calculate index in current block of last
55310736Ssam 	 * block to be kept.  -1 indicates the entire
55410736Ssam 	 * block so we need not calculate the index.
55510736Ssam 	 */
55610736Ssam 	factor = 1;
55710736Ssam 	for (i = SINGLE; i < level; i++)
55810736Ssam 		factor *= NINDIR(fs);
5599165Ssam 	last = lastbn;
56010736Ssam 	if (lastbn > 0)
56110736Ssam 		last /= factor;
56212645Ssam 	nblocks = btodb(fs->fs_bsize);
56310736Ssam 	/*
56410736Ssam 	 * Get buffer of block pointers, zero those
56510736Ssam 	 * entries corresponding to blocks to be free'd,
56610736Ssam 	 * and update on disk copy first.
56710736Ssam 	 */
56838776Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, bn), (int)fs->fs_bsize,
56938776Smckusick 		NOCRED, &bp);
57037736Smckusick 	if (error) {
57110736Ssam 		brelse(bp);
57237736Smckusick 		*countp = 0;
57337736Smckusick 		return (error);
57410736Ssam 	}
57510736Ssam 	bap = bp->b_un.b_daddr;
57631661Smckusick 	MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
57731661Smckusick 	bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
57810736Ssam 	bzero((caddr_t)&bap[last + 1],
57910736Ssam 	  (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
58037736Smckusick 	error = bwrite(bp);
58137736Smckusick 	if (error)
58237736Smckusick 		allerror = error;
58331661Smckusick 	bap = copy;
58410736Ssam 
58510736Ssam 	/*
58610736Ssam 	 * Recursively free totally unused blocks.
58710736Ssam 	 */
5889165Ssam 	for (i = NINDIR(fs) - 1; i > last; i--) {
58924Sbill 		nb = bap[i];
5909165Ssam 		if (nb == 0)
59124Sbill 			continue;
59237736Smckusick 		if (level > SINGLE) {
59337736Smckusick 			error = indirtrunc(ip, nb, (daddr_t)-1, level - 1,
59437736Smckusick 				&blkcount);
59537736Smckusick 			if (error)
59637736Smckusick 				allerror = error;
59737736Smckusick 			blocksreleased += blkcount;
59837736Smckusick 		}
59931402Smckusick 		blkfree(ip, nb, (off_t)fs->fs_bsize);
6009165Ssam 		blocksreleased += nblocks;
60124Sbill 	}
60210736Ssam 
60310736Ssam 	/*
60410736Ssam 	 * Recursively free last partial block.
60510736Ssam 	 */
60610736Ssam 	if (level > SINGLE && lastbn >= 0) {
60710736Ssam 		last = lastbn % factor;
6089165Ssam 		nb = bap[i];
60937736Smckusick 		if (nb != 0) {
61037736Smckusick 			error = indirtrunc(ip, nb, last, level - 1, &blkcount);
61137736Smckusick 			if (error)
61237736Smckusick 				allerror = error;
61337736Smckusick 			blocksreleased += blkcount;
61437736Smckusick 		}
6159165Ssam 	}
61631661Smckusick 	FREE(copy, M_TEMP);
61737736Smckusick 	*countp = blocksreleased;
61837736Smckusick 	return (allerror);
61924Sbill }
62024Sbill 
62124Sbill /*
62230749Skarels  * Remove any inodes in the inode cache belonging to dev.
6237334Skre  *
6247334Skre  * There should not be any active ones, return error if any are found
62530749Skarels  * (nb: this is a user error, not a system err).
6267334Skre  */
62739363Smckusick int busyprt = 0;	/* patch to print out busy inodes */
62839363Smckusick 
6297651Ssam #ifdef QUOTA
63039392Smckusick iflush(mp, iq)
63139392Smckusick 	struct mount *mp;
6327504Sroot 	struct inode *iq;
6337492Skre #else
63439392Smckusick iflush(mp)
63539392Smckusick 	struct mount *mp;
6367492Skre #endif
6377334Skre {
63839440Smckusick 	register struct vnode *vp, *nvp;
6397335Skre 	register struct inode *ip;
64039363Smckusick 	int busy = 0;
6417334Skre 
64239440Smckusick 	for (vp = mp->m_mounth; vp; vp = nvp) {
64339440Smckusick 		nvp = vp->v_mountf;
64439392Smckusick 		ip = VTOI(vp);
6457651Ssam #ifdef QUOTA
64639392Smckusick 		if (ip == iq)
64739363Smckusick 			continue;
6487492Skre #endif
64939392Smckusick 		if (vp->v_count) {
65039363Smckusick 			busy++;
65139363Smckusick 			if (!busyprt)
65239363Smckusick 				continue;
65339363Smckusick 			printf("%s %d on dev 0x%x count %d type %d\n",
65439363Smckusick 			    "iflush: busy inode ", ip->i_number, ip->i_dev,
65539392Smckusick 			    vp->v_count, vp->v_type);
65639363Smckusick 			continue;
65739363Smckusick 		}
65839363Smckusick 		/*
65939440Smckusick 		 * With v_count == 0, all we need to do is clear out the
66039440Smckusick 		 * vnode data structures and we are done.
66139363Smckusick 		 */
66239440Smckusick 		vgone(vp);
6637334Skre 	}
66439363Smckusick 	if (busy)
66539363Smckusick 		return (EBUSY);
66630749Skarels 	return (0);
6677334Skre }
6687334Skre 
6693617Sroot /*
6704818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
6713617Sroot  */
6724818Swnj ilock(ip)
6734818Swnj 	register struct inode *ip;
6743617Sroot {
6753617Sroot 
67637736Smckusick 	while (ip->i_flag & ILOCKED) {
67737736Smckusick 		ip->i_flag |= IWANT;
67837736Smckusick 		(void) sleep((caddr_t)ip, PINOD);
67937736Smckusick 	}
68037736Smckusick 	ip->i_flag |= ILOCKED;
6813617Sroot }
6823617Sroot 
6833617Sroot /*
6844818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
6853617Sroot  */
6867118Smckusick iunlock(ip)
6874818Swnj 	register struct inode *ip;
6883617Sroot {
6893617Sroot 
69037736Smckusick 	if ((ip->i_flag & ILOCKED) == 0)
69137736Smckusick 		printf("unlocking unlocked inode %d on dev 0x%x\n",
69237736Smckusick 			ip->i_number, ip->i_dev);
69337736Smckusick 	ip->i_flag &= ~ILOCKED;
69437736Smckusick 	if (ip->i_flag&IWANT) {
69537736Smckusick 		ip->i_flag &= ~IWANT;
69637736Smckusick 		wakeup((caddr_t)ip);
69737736Smckusick 	}
6983617Sroot }
69937736Smckusick 
70037736Smckusick /*
70137736Smckusick  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
70237736Smckusick  * The mode is shifted to select the owner/group/other fields. The
70337736Smckusick  * super user is granted all permissions.
70437736Smckusick  *
70537736Smckusick  * NB: Called from vnode op table. It seems this could all be done
70637736Smckusick  * using vattr's but...
70737736Smckusick  */
70837736Smckusick iaccess(ip, mode, cred)
70937736Smckusick 	register struct inode *ip;
71037736Smckusick 	register int mode;
71137736Smckusick 	struct ucred *cred;
71237736Smckusick {
71337736Smckusick 	register gid_t *gp;
71437736Smckusick 	int i;
71537736Smckusick 
71637736Smckusick 	/*
71739392Smckusick 	 * If you're the super-user, you always get access.
71837736Smckusick 	 */
71937736Smckusick 	if (cred->cr_uid == 0)
72037736Smckusick 		return (0);
72137736Smckusick 	/*
72237736Smckusick 	 * Access check is based on only one of owner, group, public.
72337736Smckusick 	 * If not owner, then check group. If not a member of the
72437736Smckusick 	 * group, then check public access.
72537736Smckusick 	 */
72637736Smckusick 	if (cred->cr_uid != ip->i_uid) {
72737736Smckusick 		mode >>= 3;
72837736Smckusick 		gp = cred->cr_groups;
72937736Smckusick 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
73037736Smckusick 			if (ip->i_gid == *gp)
73137736Smckusick 				goto found;
73237736Smckusick 		mode >>= 3;
73337736Smckusick found:
73437736Smckusick 		;
73537736Smckusick 	}
73637736Smckusick 	if ((ip->i_mode & mode) != 0)
73737736Smckusick 		return (0);
73837736Smckusick 	return (EACCES);
73937736Smckusick }
740