xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 63375)
123399Smckusick /*
2*63375Sbostic  * Copyright (c) 1986, 1989, 1991, 1993
3*63375Sbostic  *	The Regents of the University of California.  All rights reserved.
423399Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*63375Sbostic  *	@(#)lfs_inode.c	8.1 (Berkeley) 06/11/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;
5854694Sbostic 		struct timeval *a_ta;
5954694Sbostic 		struct timeval *a_tm;
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);
6955938Sbostic 	if ((ip->i_flag & (IUPD | IACC | ICHG | IMOD)) == 0)
7051562Smckusick 		return (0);
7155938Sbostic 	if (ip->i_flag & IACC)
7254103Smckusick 		ip->i_atime.ts_sec = ap->a_ta->tv_sec;
7355938Sbostic 	if (ip->i_flag & IUPD) {
7454103Smckusick 		ip->i_mtime.ts_sec = ap->a_tm->tv_sec;
7554128Smckusick 		(ip)->i_modrev++;
7652018Smckusick 	}
7755938Sbostic 	if (ip->i_flag & ICHG)
7854103Smckusick 		ip->i_ctime.ts_sec = time.tv_sec;
7955938Sbostic 	ip->i_flag &= ~(IUPD|IACC|ICHG);
8051562Smckusick 
8155938Sbostic 	if (!(ip->i_flag & IMOD))
8255938Sbostic 		++(VFSTOUFS(vp->v_mount)->um_lfs->lfs_uinodes);
8355938Sbostic 	ip->i_flag |= IMOD;
8455938Sbostic 
8555938Sbostic 	/* If sync, push back the vnode and any dirty blocks it may have. */
8655549Sbostic 	return (ap->a_waitfor & LFS_SYNC ? lfs_vflush(vp) : 0);
8724Sbill }
8824Sbill 
8952222Sbostic /* Update segment usage information when removing a block. */
9052225Sbostic #define UPDATE_SEGUSE \
9152225Sbostic 	if (lastseg != -1) { \
9252225Sbostic 		LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
9355786Sbostic 		if ((num << fs->lfs_bshift) > sup->su_nbytes) \
9455786Sbostic 			panic("lfs_truncate: negative bytes in segment %d\n", \
9555786Sbostic 			    lastseg); \
9652681Sstaelin 		sup->su_nbytes -= num << fs->lfs_bshift; \
9755938Sbostic 		e1 = VOP_BWRITE(sup_bp); \
9852225Sbostic 		blocksreleased += num; \
9952225Sbostic 	}
10052222Sbostic 
10152222Sbostic #define SEGDEC { \
10256026Sbostic 	if (daddr != 0) { \
10352222Sbostic 		if (lastseg != (seg = datosn(fs, daddr))) { \
10452222Sbostic 			UPDATE_SEGUSE; \
10552222Sbostic 			num = 1; \
10652222Sbostic 			lastseg = seg; \
10752222Sbostic 		} else \
10852222Sbostic 			++num; \
10952222Sbostic 	} \
11052222Sbostic }
11152222Sbostic 
11224Sbill /*
11352222Sbostic  * Truncate the inode ip to at most length size.  Update segment usage
11452222Sbostic  * table information.
11524Sbill  */
11651484Sbostic /* ARGSUSED */
11751484Sbostic int
11854694Sbostic lfs_truncate(ap)
11954694Sbostic 	struct vop_truncate_args /* {
12054694Sbostic 		struct vnode *a_vp;
12154694Sbostic 		off_t a_length;
12254694Sbostic 		int a_flags;
12354694Sbostic 		struct ucred *a_cred;
12454694Sbostic 		struct proc *a_p;
12554694Sbostic 	} */ *ap;
12624Sbill {
12756476Smargo 	register struct indir *inp;
12852222Sbostic 	register int i;
12952222Sbostic 	register daddr_t *daddrp;
13054694Sbostic 	register struct vnode *vp = ap->a_vp;
13154694Sbostic 	off_t length = ap->a_length;
13252225Sbostic 	struct buf *bp, *sup_bp;
13354765Smckusick 	struct timeval tv;
13452327Sbostic 	struct ifile *ifp;
13552222Sbostic 	struct inode *ip;
13652222Sbostic 	struct lfs *fs;
13756476Smargo 	struct indir a[NIADDR + 2], a_end[NIADDR + 2];
13852222Sbostic 	SEGUSE *sup;
13952222Sbostic 	daddr_t daddr, lastblock, lbn, olastblock;
14056158Smargo 	long off, a_released, blocksreleased, i_released;
14154694Sbostic 	int e1, e2, depth, lastseg, num, offset, seg, size;
1429165Ssam 
14355456Sbostic 	ip = VTOI(vp);
14455456Sbostic 	tv = time;
14555456Sbostic 	if (vp->v_type == VLNK && vp->v_mount->mnt_maxsymlinklen > 0) {
14655456Sbostic #ifdef DIAGNOSTIC
14755456Sbostic 		if (length != 0)
14855456Sbostic 			panic("lfs_truncate: partial truncate of symlink");
14955456Sbostic #endif
15055456Sbostic 		bzero((char *)&ip->i_shortlink, (u_int)ip->i_size);
15155456Sbostic 		ip->i_size = 0;
15255456Sbostic 		ip->i_flag |= ICHG|IUPD;
15355549Sbostic 		return (VOP_UPDATE(vp, &tv, &tv, 0));
15455456Sbostic 	}
15554694Sbostic 	vnode_pager_setsize(vp, (u_long)length);
15652327Sbostic 
15752327Sbostic 	fs = ip->i_lfs;
15852327Sbostic 
15954694Sbostic 	/* If length is larger than the file, just update the times. */
16054694Sbostic 	if (ip->i_size <= length) {
16152222Sbostic 		ip->i_flag |= ICHG|IUPD;
16255549Sbostic 		return (VOP_UPDATE(vp, &tv, &tv, 0));
16313000Ssam 	}
16452327Sbostic 
16552222Sbostic 	/*
16652222Sbostic 	 * Calculate index into inode's block list of last direct and indirect
16752222Sbostic 	 * blocks (if any) which we want to keep.  Lastblock is 0 when the
16852222Sbostic 	 * file is truncated to 0.
16952222Sbostic 	 */
17054694Sbostic 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
17152222Sbostic 	olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1;
17251484Sbostic 
1731203Sbill 	/*
17451484Sbostic 	 * Update the size of the file. If the file is not being truncated to
17551484Sbostic 	 * a block boundry, the contents of the partial block following the end
17651484Sbostic 	 * of the file must be zero'ed in case it ever become accessable again
17751484Sbostic 	 * because of subsequent file growth.
1781203Sbill 	 */
17954694Sbostic 	offset = blkoff(fs, length);
18051484Sbostic 	if (offset == 0)
18154694Sbostic 		ip->i_size = length;
18251484Sbostic 	else {
18354694Sbostic 		lbn = lblkno(fs, length);
18441313Smckusick #ifdef QUOTA
18554694Sbostic 		if (e1 = getinoquota(ip))
18654694Sbostic 			return (e1);
18751183Sbostic #endif
18854694Sbostic 		if (e1 = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp))
18954694Sbostic 			return (e1);
19054694Sbostic 		ip->i_size = length;
19151857Sbostic 		size = blksize(fs);
19254694Sbostic 		(void)vnode_pager_uncache(vp);
19326272Skarels 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
19445112Smckusick 		allocbuf(bp, size);
19555938Sbostic 		if (e1 = VOP_BWRITE(bp))
19655938Sbostic 			return (e1);
19717942Smckusick 	}
19852078Sbostic 	/*
19952222Sbostic 	 * Modify sup->su_nbyte counters for each deleted block; keep track
20052222Sbostic 	 * of number of blocks removed for ip->i_blocks.
20152222Sbostic 	 */
20252222Sbostic 	blocksreleased = 0;
20352222Sbostic 	num = 0;
20452222Sbostic 	lastseg = -1;
20552222Sbostic 
20652222Sbostic 	for (lbn = olastblock; lbn >= lastblock;) {
20756476Smargo 		/* XXX use run length from bmap array to make this faster */
20856476Smargo 		ufs_bmaparray(vp, lbn, &daddr, a, &depth, NULL);
20952222Sbostic 		if (lbn == olastblock)
21052222Sbostic 			for (i = NIADDR + 2; i--;)
21152222Sbostic 				a_end[i] = a[i];
21252222Sbostic 		switch (depth) {
21352222Sbostic 		case 0:				/* Direct block. */
21452222Sbostic 			daddr = ip->i_db[lbn];
21552222Sbostic 			SEGDEC;
21652222Sbostic 			ip->i_db[lbn] = 0;
21752222Sbostic 			--lbn;
21852222Sbostic 			break;
21952222Sbostic #ifdef DIAGNOSTIC
22052222Sbostic 		case 1:				/* An indirect block. */
22156476Smargo 			panic("lfs_truncate: ufs_bmaparray returned depth 1");
22252222Sbostic 			/* NOTREACHED */
22352222Sbostic #endif
22452222Sbostic 		default:			/* Chain of indirect blocks. */
22553505Sheideman 			inp = a + --depth;
22653505Sheideman 			if (inp->in_off > 0 && lbn != lastblock) {
22753505Sheideman 				lbn -= inp->in_off < lbn - lastblock ?
22853505Sheideman 				    inp->in_off : lbn - lastblock;
22952222Sbostic 				break;
23052222Sbostic 			}
23153505Sheideman 			for (; depth && (inp->in_off == 0 || lbn == lastblock);
23253505Sheideman 			    --inp, --depth) {
23354694Sbostic 				if (bread(vp,
23453505Sheideman 				    inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
23552222Sbostic 					panic("lfs_truncate: bread bno %d",
23653505Sheideman 					    inp->in_lbn);
23753505Sheideman 				daddrp = bp->b_un.b_daddr + inp->in_off;
23853505Sheideman 				for (i = inp->in_off;
23952222Sbostic 				    i++ <= a_end[depth].in_off;) {
24052222Sbostic 					daddr = *daddrp++;
24152222Sbostic 					SEGDEC;
24252222Sbostic 				}
24353144Sstaelin 				a_end[depth].in_off = NINDIR(fs) - 1;
24453505Sheideman 				if (inp->in_off == 0)
24553144Sstaelin 					brelse (bp);
24653144Sstaelin 				else {
24753505Sheideman 					bzero(bp->b_un.b_daddr + inp->in_off,
24852222Sbostic 					    fs->lfs_bsize -
24953505Sheideman 					    inp->in_off * sizeof(daddr_t));
25055938Sbostic 					if (e1 = VOP_BWRITE(bp))
25155938Sbostic 						return (e1);
25253144Sstaelin 				}
25352222Sbostic 			}
25453144Sstaelin 			if (depth == 0 && a[1].in_off == 0) {
25552222Sbostic 				off = a[0].in_off;
25652222Sbostic 				daddr = ip->i_ib[off];
25752222Sbostic 				SEGDEC;
25852222Sbostic 				ip->i_ib[off] = 0;
25952222Sbostic 			}
26052681Sstaelin 			if (lbn == lastblock || lbn <= NDADDR)
26152222Sbostic 				--lbn;
26252222Sbostic 			else {
26352222Sbostic 				lbn -= NINDIR(fs);
26452222Sbostic 				if (lbn < lastblock)
26552222Sbostic 					lbn = lastblock;
26652222Sbostic 			}
26752222Sbostic 		}
26852222Sbostic 	}
26952225Sbostic 	UPDATE_SEGUSE;
27055786Sbostic 
27155786Sbostic 	/* If truncating the file to 0, update the version number. */
27255786Sbostic 	if (length == 0) {
27355786Sbostic 		LFS_IENTRY(ifp, fs, ip->i_number, bp);
27455786Sbostic 		++ifp->if_version;
27555938Sbostic 		(void) VOP_BWRITE(bp);
27655786Sbostic 	}
27755786Sbostic 
27855459Sbostic #ifdef DIAGNOSTIC
27956867Smargo 	if (ip->i_blocks < fsbtodb(fs, blocksreleased)) {
28056867Smargo 		printf("lfs_truncate: block count < 0\n");
28156867Smargo 		blocksreleased = ip->i_blocks;
28256867Smargo 	}
28355459Sbostic #endif
28455938Sbostic 	ip->i_blocks -= fsbtodb(fs, blocksreleased);
28555938Sbostic 	fs->lfs_bfree +=  fsbtodb(fs, blocksreleased);
28652222Sbostic 	ip->i_flag |= ICHG|IUPD;
28755938Sbostic 	/*
28855938Sbostic 	 * Traverse dirty block list counting number of dirty buffers
28955938Sbostic 	 * that are being deleted out of the cache, so that the lfs_avail
29055938Sbostic 	 * field can be updated.
29155938Sbostic 	 */
29256158Smargo 	a_released = 0;
29356158Smargo 	i_released = 0;
29456476Smargo 	for (bp = vp->v_dirtyblkhd.le_next; bp; bp = bp->b_vnbufs.qe_next)
29556158Smargo 		if (bp->b_flags & B_LOCKED) {
29656158Smargo 			++a_released;
29756158Smargo 			/*
29856158Smargo 			 * XXX
29956158Smargo 			 * When buffers are created in the cache, their block
30056158Smargo 			 * number is set equal to their logical block number.
30156158Smargo 			 * If that is still true, we are assuming that the
30256158Smargo 			 * blocks are new (not yet on disk) and weren't
30356158Smargo 			 * counted above.  However, there is a slight chance
30456158Smargo 			 * that a block's disk address is equal to its logical
30556158Smargo 			 * block number in which case, we'll get an overcounting
30656158Smargo 			 * here.
30756158Smargo 			 */
30856158Smargo 			if (bp->b_blkno == bp->b_lblkno)
30956158Smargo 				++i_released;
31056158Smargo 		}
31156158Smargo 	blocksreleased = fsbtodb(fs, i_released);
31256158Smargo #ifdef DIAGNOSTIC
31356158Smargo 	if (blocksreleased > ip->i_blocks) {
31456158Smargo 		printf("lfs_inode: Warning! %s\n",
31556158Smargo 		    "more blocks released from inode than are in inode");
31656158Smargo 		blocksreleased = ip->i_blocks;
31756158Smargo 	}
31856158Smargo #endif
31956158Smargo 	fs->lfs_bfree += blocksreleased;
32056158Smargo 	ip->i_blocks -= blocksreleased;
32156158Smargo #ifdef DIAGNOSTIC
32256158Smargo 	if (length == 0 && ip->i_blocks != 0)
32356158Smargo 		printf("lfs_inode: Warning! %s%d%s\n",
32456158Smargo 		    "Truncation to zero, but ", ip->i_blocks,
32556158Smargo 		    " blocks left on inode");
32656158Smargo #endif
32756350Smargo 	fs->lfs_avail += fsbtodb(fs, a_released);
32857804Smckusick 	e1 = vinvalbuf(vp, (length > 0) ? V_SAVE : 0, ap->a_cred, ap->a_p,
32957804Smckusick 	    0, 0);
33055456Sbostic 	e2 = VOP_UPDATE(vp, &tv, &tv, 0);
33154694Sbostic 	return (e1 ? e1 : e2 ? e2 : 0);
33224Sbill }
333