xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 69289)
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*69289Smckusick  *	@(#)lfs_inode.c	8.9 (Berkeley) 05/08/95
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 
3052834Sbostic /* Search a block for a specific dinode. */
3154694Sbostic struct dinode *
lfs_ifind(fs,ino,dip)3252834Sbostic lfs_ifind(fs, ino, dip)
3352834Sbostic 	struct lfs *fs;
3452834Sbostic 	ino_t ino;
3552834Sbostic 	register struct dinode *dip;
3652834Sbostic {
3752834Sbostic 	register int cnt;
3854264Sbostic 	register struct dinode *ldip;
3952834Sbostic 
4054264Sbostic 	for (cnt = INOPB(fs), ldip = dip + (cnt - 1); cnt--; --ldip)
4156867Smargo 		if (ldip->di_inumber == ino)
4254264Sbostic 			return (ldip);
4352834Sbostic 
4452834Sbostic 	panic("lfs_ifind: dinode %u not found", ino);
4552834Sbostic 	/* NOTREACHED */
4652834Sbostic }
4752834Sbostic 
4851346Sbostic int
lfs_update(ap)4954694Sbostic lfs_update(ap)
5054694Sbostic 	struct vop_update_args /* {
5154694Sbostic 		struct vnode *a_vp;
5264418Sbostic 		struct timeval *a_access;
5364418Sbostic 		struct timeval *a_modify;
5454694Sbostic 		int a_waitfor;
5554694Sbostic 	} */ *ap;
567118Smckusick {
5753867Sheideman 	struct vnode *vp = ap->a_vp;
5851562Smckusick 	struct inode *ip;
5951562Smckusick 
6053867Sheideman 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
6151562Smckusick 		return (0);
6253867Sheideman 	ip = VTOI(vp);
6364610Sbostic 	if ((ip->i_flag &
6464610Sbostic 	    (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0)
6551562Smckusick 		return (0);
6664610Sbostic 	if (ip->i_flag & IN_ACCESS)
6768549Smckusick 		ip->i_atime = ap->a_access->tv_sec;
6864610Sbostic 	if (ip->i_flag & IN_UPDATE) {
6968549Smckusick 		ip->i_mtime = ap->a_modify->tv_sec;
7054128Smckusick 		(ip)->i_modrev++;
7152018Smckusick 	}
7264610Sbostic 	if (ip->i_flag & IN_CHANGE)
7368549Smckusick 		ip->i_ctime = time.tv_sec;
7464610Sbostic 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
7551562Smckusick 
7664610Sbostic 	if (!(ip->i_flag & IN_MODIFIED))
7755938Sbostic 		++(VFSTOUFS(vp->v_mount)->um_lfs->lfs_uinodes);
7864610Sbostic 	ip->i_flag |= IN_MODIFIED;
7955938Sbostic 
8055938Sbostic 	/* If sync, push back the vnode and any dirty blocks it may have. */
8155549Sbostic 	return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0);
8224Sbill }
8324Sbill 
8452222Sbostic /* Update segment usage information when removing a block. */
8552225Sbostic #define UPDATE_SEGUSE \
8652225Sbostic 	if (lastseg != -1) { \
8752225Sbostic 		LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
88*69289Smckusick 		if (num > sup->su_nbytes) \
8955786Sbostic 			panic("lfs_truncate: negative bytes in segment %d\n", \
9055786Sbostic 			    lastseg); \
91*69289Smckusick 		sup->su_nbytes -= num; \
9255938Sbostic 		e1 = VOP_BWRITE(sup_bp); \
93*69289Smckusick 		fragsreleased += numfrags(fs, num); \
9452225Sbostic 	}
9552222Sbostic 
96*69289Smckusick #define SEGDEC(S) { \
9756026Sbostic 	if (daddr != 0) { \
9852222Sbostic 		if (lastseg != (seg = datosn(fs, daddr))) { \
9952222Sbostic 			UPDATE_SEGUSE; \
100*69289Smckusick 			num = (S); \
10152222Sbostic 			lastseg = seg; \
10252222Sbostic 		} else \
103*69289Smckusick 			num += (S); \
10452222Sbostic 	} \
10552222Sbostic }
10652222Sbostic 
10724Sbill /*
10852222Sbostic  * Truncate the inode ip to at most length size.  Update segment usage
10952222Sbostic  * table information.
11024Sbill  */
11151484Sbostic /* ARGSUSED */
11251484Sbostic int
lfs_truncate(ap)11354694Sbostic lfs_truncate(ap)
11454694Sbostic 	struct vop_truncate_args /* {
11554694Sbostic 		struct vnode *a_vp;
11654694Sbostic 		off_t a_length;
11754694Sbostic 		int a_flags;
11854694Sbostic 		struct ucred *a_cred;
11954694Sbostic 		struct proc *a_p;
12054694Sbostic 	} */ *ap;
12124Sbill {
12256476Smargo 	register struct indir *inp;
12352222Sbostic 	register int i;
12468550Smckusick 	register ufs_daddr_t *daddrp;
12554694Sbostic 	register struct vnode *vp = ap->a_vp;
12654694Sbostic 	off_t length = ap->a_length;
12752225Sbostic 	struct buf *bp, *sup_bp;
12854765Smckusick 	struct timeval tv;
12952327Sbostic 	struct ifile *ifp;
13052222Sbostic 	struct inode *ip;
13152222Sbostic 	struct lfs *fs;
13256476Smargo 	struct indir a[NIADDR + 2], a_end[NIADDR + 2];
13352222Sbostic 	SEGUSE *sup;
134*69289Smckusick   	ufs_daddr_t daddr, lastblock, lbn, olastblock;
135*69289Smckusick 	ufs_daddr_t oldsize_lastblock, oldsize_newlast, newsize;
136*69289Smckusick 	long off, a_released, fragsreleased, i_released;
137*69289Smckusick 	int e1, e2, depth, lastseg, num, offset, seg, freesize;
1389165Ssam 
13955456Sbostic 	ip = VTOI(vp);
14055456Sbostic 	tv = time;
14155456Sbostic 	if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
14255456Sbostic #ifdef DIAGNOSTIC
14355456Sbostic 		if (length != 0)
14455456Sbostic 			panic("lfs_truncate: partial truncate of symlink");
14555456Sbostic #endif
14655456Sbostic 		bzero((char *)&ip->i_shortlink, (u_int)ip->i_size);
14755456Sbostic 		ip->i_size = 0;
14864610Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
14955549Sbostic 		return (VOP_UPDATE(vp, &tv, &tv, 0));
15055456Sbostic 	}
15154694Sbostic 	vnode_pager_setsize(vp, (u_long)length);
15252327Sbostic 
15352327Sbostic 	fs = ip->i_lfs;
15452327Sbostic 
15554694Sbostic 	/* If length is larger than the file, just update the times. */
15654694Sbostic 	if (ip->i_size <= length) {
15764610Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
15855549Sbostic 		return (VOP_UPDATE(vp, &tv, &tv, 0));
15913000Ssam 	}
16052327Sbostic 
16152222Sbostic 	/*
16252222Sbostic 	 * Calculate index into inode's block list of last direct and indirect
16352222Sbostic 	 * blocks (if any) which we want to keep.  Lastblock is 0 when the
16452222Sbostic 	 * file is truncated to 0.
16552222Sbostic 	 */
16654694Sbostic 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
16752222Sbostic 	olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1;
16851484Sbostic 
1691203Sbill 	/*
17051484Sbostic 	 * Update the size of the file. If the file is not being truncated to
17151484Sbostic 	 * a block boundry, the contents of the partial block following the end
17251484Sbostic 	 * of the file must be zero'ed in case it ever become accessable again
173*69289Smckusick 	 * because of subsequent file growth.  For this part of the code,
174*69289Smckusick 	 * oldsize_newlast refers to the old size of the new last block in the file.
1751203Sbill 	 */
17654694Sbostic 	offset = blkoff(fs, length);
177*69289Smckusick 	lbn = lblkno(fs, length);
178*69289Smckusick 	oldsize_newlast = blksize(fs, ip, lbn);
179*69289Smckusick 
180*69289Smckusick 	/* Now set oldsize to the current size of the current last block */
181*69289Smckusick 	oldsize_lastblock = blksize(fs, ip, olastblock);
18251484Sbostic 	if (offset == 0)
18354694Sbostic 		ip->i_size = length;
18451484Sbostic 	else {
18541313Smckusick #ifdef QUOTA
18654694Sbostic 		if (e1 = getinoquota(ip))
18754694Sbostic 			return (e1);
18851183Sbostic #endif
189*69289Smckusick 		if (e1 = bread(vp, lbn, oldsize_newlast, NOCRED, &bp))
19054694Sbostic 			return (e1);
19154694Sbostic 		ip->i_size = length;
19254694Sbostic 		(void)vnode_pager_uncache(vp);
193*69289Smckusick 		newsize = blksize(fs, ip, lbn);
194*69289Smckusick 		bzero((char *)bp->b_data + offset, (u_int)(newsize - offset));
195*69289Smckusick 		allocbuf(bp, newsize);
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 	 */
203*69289Smckusick 	fragsreleased = 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);
210*69289Smckusick 		if (lbn == olastblock) {
21152222Sbostic 			for (i = NIADDR + 2; i--;)
21252222Sbostic 				a_end[i] = a[i];
213*69289Smckusick 			freesize = oldsize_lastblock;
214*69289Smckusick 		} else
215*69289Smckusick 			freesize = fs->lfs_bsize;
216*69289Smckusick 
21752222Sbostic 		switch (depth) {
21852222Sbostic 		case 0:				/* Direct block. */
21952222Sbostic 			daddr = ip->i_db[lbn];
220*69289Smckusick 			SEGDEC(freesize);
22152222Sbostic 			ip->i_db[lbn] = 0;
22252222Sbostic 			--lbn;
22352222Sbostic 			break;
22452222Sbostic #ifdef DIAGNOSTIC
22552222Sbostic 		case 1:				/* An indirect block. */
22656476Smargo 			panic("lfs_truncate: ufs_bmaparray returned depth 1");
22752222Sbostic 			/* NOTREACHED */
22852222Sbostic #endif
22952222Sbostic 		default:			/* Chain of indirect blocks. */
23053505Sheideman 			inp = a + --depth;
23153505Sheideman 			if (inp->in_off > 0 && lbn != lastblock) {
23253505Sheideman 				lbn -= inp->in_off < lbn - lastblock ?
23353505Sheideman 				    inp->in_off : lbn - lastblock;
23452222Sbostic 				break;
23552222Sbostic 			}
23653505Sheideman 			for (; depth && (inp->in_off == 0 || lbn == lastblock);
23753505Sheideman 			    --inp, --depth) {
23854694Sbostic 				if (bread(vp,
23953505Sheideman 				    inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
24052222Sbostic 					panic("lfs_truncate: bread bno %d",
24153505Sheideman 					    inp->in_lbn);
24268550Smckusick 				daddrp = (ufs_daddr_t *)bp->b_data +
24368550Smckusick 				    inp->in_off;
24453505Sheideman 				for (i = inp->in_off;
24552222Sbostic 				    i++ <= a_end[depth].in_off;) {
24652222Sbostic 					daddr = *daddrp++;
247*69289Smckusick 					SEGDEC(freesize);
24852222Sbostic 				}
24953144Sstaelin 				a_end[depth].in_off = NINDIR(fs) - 1;
25053505Sheideman 				if (inp->in_off == 0)
25153144Sstaelin 					brelse (bp);
25253144Sstaelin 				else {
25368550Smckusick 					bzero((ufs_daddr_t *)bp->b_data +
25464525Sbostic 					    inp->in_off, fs->lfs_bsize -
25568550Smckusick 					    inp->in_off * sizeof(ufs_daddr_t));
25655938Sbostic 					if (e1 = VOP_BWRITE(bp))
25755938Sbostic 						return (e1);
25853144Sstaelin 				}
25952222Sbostic 			}
26053144Sstaelin 			if (depth == 0 && a[1].in_off == 0) {
26152222Sbostic 				off = a[0].in_off;
26252222Sbostic 				daddr = ip->i_ib[off];
263*69289Smckusick 				SEGDEC(freesize);
26452222Sbostic 				ip->i_ib[off] = 0;
26552222Sbostic 			}
26652681Sstaelin 			if (lbn == lastblock || lbn <= NDADDR)
26752222Sbostic 				--lbn;
26852222Sbostic 			else {
26952222Sbostic 				lbn -= NINDIR(fs);
27052222Sbostic 				if (lbn < lastblock)
27152222Sbostic 					lbn = lastblock;
27252222Sbostic 			}
27352222Sbostic 		}
27452222Sbostic 	}
27552225Sbostic 	UPDATE_SEGUSE;
27655786Sbostic 
27755786Sbostic 	/* If truncating the file to 0, update the version number. */
27855786Sbostic 	if (length == 0) {
27955786Sbostic 		LFS_IENTRY(ifp, fs, ip->i_number, bp);
28055786Sbostic 		++ifp->if_version;
28155938Sbostic 		(void) VOP_BWRITE(bp);
28255786Sbostic 	}
28355786Sbostic 
28455459Sbostic #ifdef DIAGNOSTIC
285*69289Smckusick 	if (ip->i_blocks < fragstodb(fs, fragsreleased)) {
286*69289Smckusick 		printf("lfs_truncate: frag count < 0\n");
287*69289Smckusick 		fragsreleased = dbtofrags(fs, ip->i_blocks);
288*69289Smckusick 		panic("lfs_truncate: frag count < 0\n");
28956867Smargo 	}
29055459Sbostic #endif
291*69289Smckusick 	ip->i_blocks -= fragstodb(fs, fragsreleased);
292*69289Smckusick 	fs->lfs_bfree +=  fragstodb(fs, fragsreleased);
29364610Sbostic 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
29455938Sbostic 	/*
29555938Sbostic 	 * Traverse dirty block list counting number of dirty buffers
29655938Sbostic 	 * that are being deleted out of the cache, so that the lfs_avail
29755938Sbostic 	 * field can be updated.
29855938Sbostic 	 */
29956158Smargo 	a_released = 0;
30056158Smargo 	i_released = 0;
30165241Smckusick 	for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next)
30256158Smargo 		if (bp->b_flags & B_LOCKED) {
303*69289Smckusick 			a_released += numfrags(fs, bp->b_bcount);
30456158Smargo 			/*
30556158Smargo 			 * XXX
30656158Smargo 			 * When buffers are created in the cache, their block
30756158Smargo 			 * number is set equal to their logical block number.
30856158Smargo 			 * If that is still true, we are assuming that the
30956158Smargo 			 * blocks are new (not yet on disk) and weren't
31056158Smargo 			 * counted above.  However, there is a slight chance
31156158Smargo 			 * that a block's disk address is equal to its logical
31256158Smargo 			 * block number in which case, we'll get an overcounting
31356158Smargo 			 * here.
31456158Smargo 			 */
31556158Smargo 			if (bp->b_blkno == bp->b_lblkno)
316*69289Smckusick 				i_released += numfrags(fs, bp->b_bcount);
31756158Smargo 		}
318*69289Smckusick 	fragsreleased = i_released;
31956158Smargo #ifdef DIAGNOSTIC
320*69289Smckusick 	if (fragsreleased > dbtofrags(fs, ip->i_blocks)) {
32156158Smargo 		printf("lfs_inode: Warning! %s\n",
322*69289Smckusick 		    "more frags released from inode than are in inode");
323*69289Smckusick 		fragsreleased = dbtofrags(fs, ip->i_blocks);
324*69289Smckusick 		panic("lfs_inode: Warning.  More frags released\n");
32556158Smargo 	}
32656158Smargo #endif
327*69289Smckusick 	fs->lfs_bfree += fragstodb(fs, fragsreleased);
328*69289Smckusick 	ip->i_blocks -= fragstodb(fs, fragsreleased);
32956158Smargo #ifdef DIAGNOSTIC
330*69289Smckusick 	if (length == 0 && ip->i_blocks != 0) {
33156158Smargo 		printf("lfs_inode: Warning! %s%d%s\n",
33256158Smargo 		    "Truncation to zero, but ", ip->i_blocks,
33356158Smargo 		    " blocks left on inode");
334*69289Smckusick 		panic("lfs_inode");
335*69289Smckusick 	}
33656158Smargo #endif
337*69289Smckusick 	fs->lfs_avail += fragstodb(fs, a_released);
33857804Smckusick 	e1 = vinvalbuf(vp, (length > 0) ? V_SAVE : 0, ap->a_cred, ap->a_p,
33957804Smckusick 	    0, 0);
34055456Sbostic 	e2 = VOP_UPDATE(vp, &tv, &tv, 0);
34154694Sbostic 	return (e1 ? e1 : e2 ? e2 : 0);
34224Sbill }
343