xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 65241)
123399Smckusick /*
263375Sbostic  * Copyright (c) 1986, 1989, 1991, 1993
363375Sbostic  *	The Regents of the University of California.  All rights reserved.
423399Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*65241Smckusick  *	@(#)lfs_inode.c	8.5 (Berkeley) 12/30/93
823399Smckusick  */
924Sbill 
1051484Sbostic #include <sys/param.h>
1151484Sbostic #include <sys/systm.h>
1251484Sbostic #include <sys/mount.h>
1351484Sbostic #include <sys/proc.h>
1451484Sbostic #include <sys/file.h>
1551484Sbostic #include <sys/buf.h>
1651484Sbostic #include <sys/vnode.h>
1751484Sbostic #include <sys/kernel.h>
1851484Sbostic #include <sys/malloc.h>
1924Sbill 
2053477Smckusick #include <vm/vm.h>
2153477Smckusick 
2251498Sbostic #include <ufs/ufs/quota.h>
2351498Sbostic #include <ufs/ufs/inode.h>
2451498Sbostic #include <ufs/ufs/ufsmount.h>
2551498Sbostic #include <ufs/ufs/ufs_extern.h>
2647571Skarels 
2751498Sbostic #include <ufs/lfs/lfs.h>
2851498Sbostic #include <ufs/lfs/lfs_extern.h>
2924Sbill 
3051346Sbostic int
3151155Sbostic lfs_init()
3224Sbill {
3351484Sbostic 	return (ufs_init());
3424Sbill }
3524Sbill 
3652834Sbostic /* Search a block for a specific dinode. */
3754694Sbostic struct dinode *
3852834Sbostic lfs_ifind(fs, ino, dip)
3952834Sbostic 	struct lfs *fs;
4052834Sbostic 	ino_t ino;
4152834Sbostic 	register struct dinode *dip;
4252834Sbostic {
4352834Sbostic 	register int cnt;
4454264Sbostic 	register struct dinode *ldip;
4552834Sbostic 
4654264Sbostic 	for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
4756867Smargo 		if (ldip->di_inumber == ino)
4854264Sbostic 			return (ldip);
4952834Sbostic 
5052834Sbostic 	panic("lfs_ifind: dinode %u not found", ino);
5152834Sbostic 	/* NOTREACHED */
5252834Sbostic }
5352834Sbostic 
5451346Sbostic int
5554694Sbostic lfs_update(ap)
5654694Sbostic 	struct vop_update_args /* {
5754694Sbostic 		struct vnode *a_vp;
5864418Sbostic 		struct timeval *a_access;
5964418Sbostic 		struct timeval *a_modify;
6054694Sbostic 		int a_waitfor;
6154694Sbostic 	} */ *ap;
627118Smckusick {
6353867Sheideman 	struct vnode *vp = ap->a_vp;
6451562Smckusick 	struct inode *ip;
6551562Smckusick 
6653867Sheideman 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
6751562Smckusick 		return (0);
6853867Sheideman 	ip = VTOI(vp);
6964610Sbostic 	if ((ip->i_flag &
7064610Sbostic 	    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0)
7151562Smckusick 		return (0);
7264610Sbostic 	if (ip->i_flag & IN_ACCESS)
7364418Sbostic 		ip->i_atime.ts_sec = ap->a_access->tv_sec;
7464610Sbostic 	if (ip->i_flag & IN_UPDATE) {
7564418Sbostic 		ip->i_mtime.ts_sec = ap->a_modify->tv_sec;
7654128Smckusick 		(ip)->i_modrev++;
7752018Smckusick 	}
7864610Sbostic 	if (ip->i_flag & IN_CHANGE)
7954103Smckusick 		ip->i_ctime.ts_sec = time.tv_sec;
8064610Sbostic 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
8151562Smckusick 
8264610Sbostic 	if (!(ip->i_flag & IN_MODIFIED))
8355938Sbostic 		++(VFSTOUFS(vp->v_mount)->um_lfs->lfs_uinodes);
8464610Sbostic 	ip->i_flag |= IN_MODIFIED;
8555938Sbostic 
8655938Sbostic 	/* If sync, push back the vnode and any dirty blocks it may have. */
8755549Sbostic 	return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0);
8824Sbill }
8924Sbill 
9052222Sbostic /* Update segment usage information when removing a block. */
9152225Sbostic #define UPDATE_SEGUSE \
9252225Sbostic 	if (lastseg != -1) { \
9352225Sbostic 		LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
9455786Sbostic 		if ((num << fs->lfs_bshift) > sup->su_nbytes) \
9555786Sbostic 			panic("lfs_truncate: negative bytes in segment %d\n", \
9655786Sbostic 			    lastseg); \
9752681Sstaelin 		sup->su_nbytes -= num << fs->lfs_bshift; \
9855938Sbostic 		e1 = VOP_BWRITE(sup_bp); \
9952225Sbostic 		blocksreleased += num; \
10052225Sbostic 	}
10152222Sbostic 
10252222Sbostic #define SEGDEC { \
10356026Sbostic 	if (daddr != 0) { \
10452222Sbostic 		if (lastseg != (seg = datosn(fs, daddr))) { \
10552222Sbostic 			UPDATE_SEGUSE; \
10652222Sbostic 			num = 1; \
10752222Sbostic 			lastseg = seg; \
10852222Sbostic 		} else \
10952222Sbostic 			++num; \
11052222Sbostic 	} \
11152222Sbostic }
11252222Sbostic 
11324Sbill /*
11452222Sbostic  * Truncate the inode ip to at most length size.  Update segment usage
11552222Sbostic  * table information.
11624Sbill  */
11751484Sbostic /* ARGSUSED */
11851484Sbostic int
11954694Sbostic lfs_truncate(ap)
12054694Sbostic 	struct vop_truncate_args /* {
12154694Sbostic 		struct vnode *a_vp;
12254694Sbostic 		off_t a_length;
12354694Sbostic 		int a_flags;
12454694Sbostic 		struct ucred *a_cred;
12554694Sbostic 		struct proc *a_p;
12654694Sbostic 	} */ *ap;
12724Sbill {
12856476Smargo 	register struct indir *inp;
12952222Sbostic 	register int i;
13052222Sbostic 	register daddr_t *daddrp;
13154694Sbostic 	register struct vnode *vp = ap->a_vp;
13254694Sbostic 	off_t length = ap->a_length;
13352225Sbostic 	struct buf *bp, *sup_bp;
13454765Smckusick 	struct timeval tv;
13552327Sbostic 	struct ifile *ifp;
13652222Sbostic 	struct inode *ip;
13752222Sbostic 	struct lfs *fs;
13856476Smargo 	struct indir a[NIADDR + 2], a_end[NIADDR + 2];
13952222Sbostic 	SEGUSE *sup;
14052222Sbostic 	daddr_t daddr, lastblock, lbn, olastblock;
14156158Smargo 	long off, a_released, blocksreleased, i_released;
14254694Sbostic 	int e1, e2, depth, lastseg, num, offset, seg, size;
1439165Ssam 
14455456Sbostic 	ip = VTOI(vp);
14555456Sbostic 	tv = time;
14655456Sbostic 	if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
14755456Sbostic #ifdef DIAGNOSTIC
14855456Sbostic 		if (length != 0)
14955456Sbostic 			panic("lfs_truncate: partial truncate of symlink");
15055456Sbostic #endif
15155456Sbostic 		bzero((char *)&ip->i_shortlink, (u_int)ip->i_size);
15255456Sbostic 		ip->i_size = 0;
15364610Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
15455549Sbostic 		return (VOP_UPDATE(vp, &tv, &tv, 0));
15555456Sbostic 	}
15654694Sbostic 	vnode_pager_setsize(vp, (u_long)length);
15752327Sbostic 
15852327Sbostic 	fs = ip->i_lfs;
15952327Sbostic 
16054694Sbostic 	/* If length is larger than the file, just update the times. */
16154694Sbostic 	if (ip->i_size <= length) {
16264610Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
16355549Sbostic 		return (VOP_UPDATE(vp, &tv, &tv, 0));
16413000Ssam 	}
16552327Sbostic 
16652222Sbostic 	/*
16752222Sbostic 	 * Calculate index into inode's block list of last direct and indirect
16852222Sbostic 	 * blocks (if any) which we want to keep.  Lastblock is 0 when the
16952222Sbostic 	 * file is truncated to 0.
17052222Sbostic 	 */
17154694Sbostic 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
17252222Sbostic 	olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1;
17351484Sbostic 
1741203Sbill 	/*
17551484Sbostic 	 * Update the size of the file. If the file is not being truncated to
17651484Sbostic 	 * a block boundry, the contents of the partial block following the end
17751484Sbostic 	 * of the file must be zero'ed in case it ever become accessable again
17851484Sbostic 	 * because of subsequent file growth.
1791203Sbill 	 */
18054694Sbostic 	offset = blkoff(fs, length);
18151484Sbostic 	if (offset == 0)
18254694Sbostic 		ip->i_size = length;
18351484Sbostic 	else {
18454694Sbostic 		lbn = lblkno(fs, length);
18541313Smckusick #ifdef QUOTA
18654694Sbostic 		if (e1 = getinoquota(ip))
18754694Sbostic 			return (e1);
18851183Sbostic #endif
18954694Sbostic 		if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp))
19054694Sbostic 			return (e1);
19154694Sbostic 		ip->i_size = length;
19251857Sbostic 		size = blksize(fs);
19354694Sbostic 		(void)vnode_pager_uncache(vp);
19464525Sbostic 		bzero((char *)bp->b_data + offset, (u_int)(size - offset));
19545112Smckusick 		allocbuf(bp, size);
19655938Sbostic 		if (e1 = VOP_BWRITE(bp))
19755938Sbostic 			return (e1);
19817942Smckusick 	}
19952078Sbostic 	/*
20052222Sbostic 	 * Modify sup->su_nbyte counters for each deleted block; keep track
20152222Sbostic 	 * of number of blocks removed for ip->i_blocks.
20252222Sbostic 	 */
20352222Sbostic 	blocksreleased = 0;
20452222Sbostic 	num = 0;
20552222Sbostic 	lastseg = -1;
20652222Sbostic 
20752222Sbostic 	for (lbn = olastblock; lbn >= lastblock;) {
20856476Smargo 		/* XXX use run length from bmap array to make this faster */
20956476Smargo 		ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL);
21052222Sbostic 		if (lbn == olastblock)
21152222Sbostic 			for (i = NIADDR + 2; i--;)
21252222Sbostic 				a_end[i] = a[i];
21352222Sbostic 		switch (depth) {
21452222Sbostic 		case 0:				/* Direct block. */
21552222Sbostic 			daddr = ip->i_db[lbn];
21652222Sbostic 			SEGDEC;
21752222Sbostic 			ip->i_db[lbn] = 0;
21852222Sbostic 			--lbn;
21952222Sbostic 			break;
22052222Sbostic #ifdef DIAGNOSTIC
22152222Sbostic 		case 1:				/* An indirect block. */
22256476Smargo 			panic("lfs_truncate: ufs_bmaparray returned depth 1");
22352222Sbostic 			/* NOTREACHED */
22452222Sbostic #endif
22552222Sbostic 		default:			/* Chain of indirect blocks. */
22653505Sheideman 			inp = a + --depth;
22753505Sheideman 			if (inp->in_off > 0 && lbn != lastblock) {
22853505Sheideman 				lbn -= inp->in_off < lbn - lastblock ?
22953505Sheideman 				    inp->in_off : lbn - lastblock;
23052222Sbostic 				break;
23152222Sbostic 			}
23253505Sheideman 			for (; depth && (inp->in_off == 0 || lbn == lastblock);
23353505Sheideman 			    --inp, --depth) {
23454694Sbostic 				if (bread(vp,
23553505Sheideman 				    inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
23652222Sbostic 					panic("lfs_truncate: bread bno %d",
23753505Sheideman 					    inp->in_lbn);
23864525Sbostic 				daddrp = (daddr_t *)bp->b_data + inp->in_off;
23953505Sheideman 				for (i = inp->in_off;
24052222Sbostic 				    i++ <= a_end[depth].in_off;) {
24152222Sbostic 					daddr = *daddrp++;
24252222Sbostic 					SEGDEC;
24352222Sbostic 				}
24453144Sstaelin 				a_end[depth].in_off = NINDIR(fs) - 1;
24553505Sheideman 				if (inp->in_off == 0)
24653144Sstaelin 					brelse (bp);
24753144Sstaelin 				else {
24864525Sbostic 					bzero((daddr_t *)bp->b_data +
24964525Sbostic 					    inp->in_off, fs->lfs_bsize -
25053505Sheideman 					    inp->in_off * sizeof(daddr_t));
25155938Sbostic 					if (e1 = VOP_BWRITE(bp))
25255938Sbostic 						return (e1);
25353144Sstaelin 				}
25452222Sbostic 			}
25553144Sstaelin 			if (depth == 0 && a[1].in_off == 0) {
25652222Sbostic 				off = a[0].in_off;
25752222Sbostic 				daddr = ip->i_ib[off];
25852222Sbostic 				SEGDEC;
25952222Sbostic 				ip->i_ib[off] = 0;
26052222Sbostic 			}
26152681Sstaelin 			if (lbn == lastblock || lbn <= NDADDR)
26252222Sbostic 				--lbn;
26352222Sbostic 			else {
26452222Sbostic 				lbn -= NINDIR(fs);
26552222Sbostic 				if (lbn < lastblock)
26652222Sbostic 					lbn = lastblock;
26752222Sbostic 			}
26852222Sbostic 		}
26952222Sbostic 	}
27052225Sbostic 	UPDATE_SEGUSE;
27155786Sbostic 
27255786Sbostic 	/* If truncating the file to 0, update the version number. */
27355786Sbostic 	if (length == 0) {
27455786Sbostic 		LFS_IENTRY(ifp, fs, ip->i_number, bp);
27555786Sbostic 		++ifp->if_version;
27655938Sbostic 		(void) VOP_BWRITE(bp);
27755786Sbostic 	}
27855786Sbostic 
27955459Sbostic #ifdef DIAGNOSTIC
28056867Smargo 	if (ip->i_blocks < fsbtodb(fs, blocksreleased)) {
28156867Smargo 		printf("lfs_truncate: block count < 0\n");
28256867Smargo 		blocksreleased = ip->i_blocks;
28356867Smargo 	}
28455459Sbostic #endif
28555938Sbostic 	ip->i_blocks -= fsbtodb(fs, blocksreleased);
28655938Sbostic 	fs->lfs_bfree +=  fsbtodb(fs, blocksreleased);
28764610Sbostic 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
28855938Sbostic 	/*
28955938Sbostic 	 * Traverse dirty block list counting number of dirty buffers
29055938Sbostic 	 * that are being deleted out of the cache, so that the lfs_avail
29155938Sbostic 	 * field can be updated.
29255938Sbostic 	 */
29356158Smargo 	a_released = 0;
29456158Smargo 	i_released = 0;
295*65241Smckusick 	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next)
29656158Smargo 		if (bp->b_flags & B_LOCKED) {
29756158Smargo 			++a_released;
29856158Smargo 			/*
29956158Smargo 			 * XXX
30056158Smargo 			 * When buffers are created in the cache, their block
30156158Smargo 			 * number is set equal to their logical block number.
30256158Smargo 			 * If that is still true, we are assuming that the
30356158Smargo 			 * blocks are new (not yet on disk) and weren't
30456158Smargo 			 * counted above.  However, there is a slight chance
30556158Smargo 			 * that a block's disk address is equal to its logical
30656158Smargo 			 * block number in which case, we'll get an overcounting
30756158Smargo 			 * here.
30856158Smargo 			 */
30956158Smargo 			if (bp->b_blkno == bp->b_lblkno)
31056158Smargo 				++i_released;
31156158Smargo 		}
31256158Smargo 	blocksreleased = fsbtodb(fs, i_released);
31356158Smargo #ifdef DIAGNOSTIC
31456158Smargo 	if (blocksreleased > ip->i_blocks) {
31556158Smargo 		printf("lfs_inode: Warning! %s\n",
31656158Smargo 		    "more blocks released from inode than are in inode");
31756158Smargo 		blocksreleased = ip->i_blocks;
31856158Smargo 	}
31956158Smargo #endif
32056158Smargo 	fs->lfs_bfree += blocksreleased;
32156158Smargo 	ip->i_blocks -= blocksreleased;
32256158Smargo #ifdef DIAGNOSTIC
32356158Smargo 	if (length == 0 && ip->i_blocks != 0)
32456158Smargo 		printf("lfs_inode: Warning! %s%d%s\n",
32556158Smargo 		    "Truncation to zero, but ", ip->i_blocks,
32656158Smargo 		    " blocks left on inode");
32756158Smargo #endif
32856350Smargo 	fs->lfs_avail += fsbtodb(fs, a_released);
32957804Smckusick 	e1 = vinvalbuf(vp, (length > 0) ? V_SAVE : 0, ap->a_cred, ap->a_p,
33057804Smckusick 	    0, 0);
33155456Sbostic 	e2 = VOP_UPDATE(vp, &tv, &tv, 0);
33254694Sbostic 	return (e1 ? e1 : e2 ? e2 : 0);
33324Sbill }
334