xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 53592)
123399Smckusick /*
251498Sbostic  * Copyright (c) 1986, 1989, 1991 Regents of the University of California.
337736Smckusick  * All rights reserved.
423399Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*53592Sheideman  *	@(#)lfs_inode.c	7.65 (Berkeley) 05/15/92
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 static struct dinode *lfs_ifind __P((struct lfs *, ino_t, struct dinode *));
3152834Sbostic 
3251346Sbostic int
3351155Sbostic lfs_init()
3424Sbill {
3551857Sbostic #ifdef VERBOSE
3651857Sbostic 	printf("lfs_init\n");
3751857Sbostic #endif
3851484Sbostic 	return (ufs_init());
3924Sbill }
4024Sbill 
4124Sbill /*
4251484Sbostic  * Look up an LFS dinode number to find its incore vnode.  If not already
4351484Sbostic  * in core, read it in from the specified device.  Return the inode locked.
4451484Sbostic  * Detection and handling of mount points must be done by the calling routine.
4524Sbill  */
4651346Sbostic int
4753529Sheideman lfs_vget (ap)
4853529Sheideman 	struct vop_vget_args *ap;
4924Sbill {
5051498Sbostic 	register struct lfs *fs;
5151484Sbostic 	register struct inode *ip;
5251484Sbostic 	struct buf *bp;
5352224Sbostic 	struct ifile *ifp;
5451498Sbostic 	struct vnode *vp;
5551562Smckusick 	struct ufsmount *ump;
5652224Sbostic 	daddr_t daddr;
5751484Sbostic 	dev_t dev;
5851346Sbostic 	int error;
5924Sbill 
6051857Sbostic #ifdef VERBOSE
6151857Sbostic 	printf("lfs_vget\n");
6251857Sbostic #endif
63*53592Sheideman 	ump = VFSTOUFS(ap->a_mp);
6451562Smckusick 	dev = ump->um_dev;
65*53592Sheideman 	if ((*ap->a_vpp = ufs_ihashget(dev, ap->a_ino)) != NULL)
6651484Sbostic 		return (0);
6751484Sbostic 
6852224Sbostic 	/* Translate the inode number to a disk address. */
6952224Sbostic 	fs = ump->um_lfs;
70*53592Sheideman 	if (ap->a_ino == LFS_IFILE_INUM)
7152224Sbostic 		daddr = fs->lfs_idaddr;
7252224Sbostic 	else {
73*53592Sheideman 		LFS_IENTRY(ifp, fs, ap->a_ino, bp);
7452224Sbostic 		daddr = ifp->if_daddr;
7552224Sbostic 		brelse(bp);
7652224Sbostic 		if (daddr == LFS_UNUSED_DADDR)
7752224Sbostic 			return (ENOENT);
7852224Sbostic 	}
7952224Sbostic 
8051155Sbostic 	/* Allocate new vnode/inode. */
81*53592Sheideman 	if (error = lfs_vcreate(ap->a_mp, ap->a_ino, &vp)) {
82*53592Sheideman 		*ap->a_vpp = NULL;
8337736Smckusick 		return (error);
8437736Smckusick 	}
8552224Sbostic 
8637736Smckusick 	/*
8739440Smckusick 	 * Put it onto its hash chain and lock it so that other requests for
8839440Smckusick 	 * this inode will block if they arrive while we are sleeping waiting
8939440Smckusick 	 * for old data structures to be purged or for the contents of the
9039440Smckusick 	 * disk portion of this inode to be read.
9139440Smckusick 	 */
9251562Smckusick 	ip = VTOI(vp);
9351484Sbostic 	ufs_ihashins(ip);
9451155Sbostic 
9552224Sbostic 	/*
9652224Sbostic 	 * XXX
9752224Sbostic 	 * This may not need to be here, logically it should go down with
9852224Sbostic 	 * the i_devvp initialization.
9952224Sbostic 	 * Ask Kirk.
10052224Sbostic 	 */
10152224Sbostic 	ip->i_lfs = ump->um_lfs;
10252224Sbostic 
10351484Sbostic 	/* Read in the disk contents for the inode, copy into the inode. */
10452224Sbostic 	if (error =
10552224Sbostic 	    bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) {
10637736Smckusick 		/*
10751857Sbostic 		 * The inode does not contain anything useful, so it
10851857Sbostic 		 * would be misleading to leave it on its hash chain.
10951857Sbostic 		 * Iput() will return it to the free list.
11041334Smckusick 		 */
11141334Smckusick 		remque(ip);
11241334Smckusick 		ip->i_forw = ip;
11341334Smckusick 		ip->i_back = ip;
11451484Sbostic 
11551484Sbostic 		/* Unlock and discard unneeded inode. */
11651484Sbostic 		ufs_iput(ip);
11737736Smckusick 		brelse(bp);
118*53592Sheideman 		*ap->a_vpp = NULL;
11939440Smckusick 		return (error);
12037736Smckusick 	}
121*53592Sheideman 	ip->i_din = *lfs_ifind(fs, ap->a_ino, bp->b_un.b_dino);
12239440Smckusick 	brelse(bp);
12351155Sbostic 
12451498Sbostic 	/*
12551498Sbostic 	 * Initialize the vnode from the inode, check for aliases.  In all
12651498Sbostic 	 * cases re-init ip, the underlying vnode/inode may have changed.
12751498Sbostic 	 */
128*53592Sheideman 	if (error = ufs_vinit(ap->a_mp, lfs_specop_p, LFS_FIFOOPS, &vp)) {
12951484Sbostic 		ufs_iput(ip);
130*53592Sheideman 		*ap->a_vpp = NULL;
13151484Sbostic 		return (error);
13240289Smckusick 	}
13351562Smckusick 	/*
13451562Smckusick 	 * Finish inode initialization now that aliasing has been resolved.
13551562Smckusick 	 */
13651562Smckusick 	ip->i_devvp = ump->um_devvp;
13751562Smckusick 	VREF(ip->i_devvp);
138*53592Sheideman 	*ap->a_vpp = vp;
13937736Smckusick 	return (0);
14037736Smckusick }
1417334Skre 
14252834Sbostic /* Search a block for a specific dinode. */
14352834Sbostic static struct dinode *
14452834Sbostic lfs_ifind(fs, ino, dip)
14552834Sbostic 	struct lfs *fs;
14652834Sbostic 	ino_t ino;
14752834Sbostic 	register struct dinode *dip;
14852834Sbostic {
14952834Sbostic 	register int cnt;
15052834Sbostic 
15152834Sbostic #ifdef VERBOSE
15252834Sbostic 	printf("lfs_ifind: inode %d\n", ino);
15352834Sbostic #endif
15452834Sbostic 	for (cnt = INOPB(fs); cnt--; ++dip)
15552834Sbostic 		if (dip->di_inum == ino)
15652834Sbostic 			return (dip);
15752834Sbostic 
15852834Sbostic 	panic("lfs_ifind: dinode %u not found", ino);
15952834Sbostic 	/* NOTREACHED */
16052834Sbostic }
16152834Sbostic 
16251346Sbostic int
16353529Sheideman lfs_update (ap)
16453529Sheideman 	struct vop_update_args *ap;
1657118Smckusick {
16651562Smckusick 	struct inode *ip;
16751562Smckusick 
16851857Sbostic #ifdef VERBOSE
16951857Sbostic 	printf("lfs_update\n");
17051857Sbostic #endif
171*53592Sheideman 	if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
17251562Smckusick 		return (0);
173*53592Sheideman 	ip = VTOI(ap->a_vp);
17451562Smckusick 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
17551562Smckusick 		return (0);
17651562Smckusick 	if (ip->i_flag&IACC)
177*53592Sheideman 		ip->i_atime.tv_sec = ap->a_ta->tv_sec;
17852018Smckusick 	if (ip->i_flag&IUPD) {
179*53592Sheideman 		ip->i_mtime.tv_sec = ap->a_tm->tv_sec;
18052018Smckusick 		INCRQUAD((ip)->i_modrev);
18152018Smckusick 	}
18251562Smckusick 	if (ip->i_flag&ICHG)
18353477Smckusick 		ip->i_ctime.tv_sec = time.tv_sec;
18451562Smckusick 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
18551562Smckusick 
18652327Sbostic 	/* Push back the vnode and any dirty blocks it may have. */
187*53592Sheideman 	return (ap->a_waitfor ? lfs_vflush(ap->a_vp) : 0);
18824Sbill }
18924Sbill 
19052222Sbostic /* Update segment usage information when removing a block. */
19152225Sbostic #define UPDATE_SEGUSE \
19252225Sbostic 	if (lastseg != -1) { \
19352225Sbostic 		LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
19452681Sstaelin 		sup->su_nbytes -= num << fs->lfs_bshift; \
19552225Sbostic 		LFS_UBWRITE(sup_bp); \
19652225Sbostic 		blocksreleased += num; \
19752225Sbostic 	}
19852222Sbostic 
19952222Sbostic #define SEGDEC { \
20052222Sbostic 	if (daddr != UNASSIGNED) { \
20152222Sbostic 		if (lastseg != (seg = datosn(fs, daddr))) { \
20252222Sbostic 			UPDATE_SEGUSE; \
20352222Sbostic 			num = 1; \
20452222Sbostic 			lastseg = seg; \
20552222Sbostic 		} else \
20652222Sbostic 			++num; \
20752222Sbostic 	} \
20852222Sbostic }
20952222Sbostic 
21024Sbill /*
21152222Sbostic  * Truncate the inode ip to at most length size.  Update segment usage
21252222Sbostic  * table information.
21324Sbill  */
21451484Sbostic /* ARGSUSED */
21551484Sbostic int
21653529Sheideman lfs_truncate (ap)
21753529Sheideman 	struct vop_truncate_args *ap;
21824Sbill {
21953529Sheideman 	USES_VOP_UPDATE;
22053505Sheideman 	register INDIR *inp;
22152222Sbostic 	register int i;
22252222Sbostic 	register daddr_t *daddrp;
22352225Sbostic 	struct buf *bp, *sup_bp;
22452327Sbostic 	struct ifile *ifp;
22552222Sbostic 	struct inode *ip;
22652222Sbostic 	struct lfs *fs;
22752222Sbostic 	INDIR a[NIADDR + 2], a_end[NIADDR + 2];
22852222Sbostic 	SEGUSE *sup;
22952222Sbostic 	daddr_t daddr, lastblock, lbn, olastblock;
23053233Smckusick 	long off, blocksreleased;
23152222Sbostic 	int error, depth, lastseg, num, offset, seg, size;
2329165Ssam 
23351857Sbostic #ifdef VERBOSE
23451857Sbostic 	printf("lfs_truncate\n");
23551857Sbostic #endif
236*53592Sheideman 	vnode_pager_setsize(ap->a_vp, (u_long)ap->a_length);
23752327Sbostic 
238*53592Sheideman 	ip = VTOI(ap->a_vp);
23952327Sbostic 	fs = ip->i_lfs;
24052327Sbostic 
24152327Sbostic 	/* If truncating the file to 0, update the version number. */
242*53592Sheideman 	if (ap->a_length == 0) {
24352327Sbostic 		LFS_IENTRY(ifp, fs, ip->i_number, bp);
24452327Sbostic 		++ifp->if_version;
24552327Sbostic 		LFS_UBWRITE(bp);
24652327Sbostic 	}
24752327Sbostic 
248*53592Sheideman 	/* If ap->a_length is larger than the file, just update the times. */
249*53592Sheideman 	if (ip->i_size <= ap->a_length) {
25052222Sbostic 		ip->i_flag |= ICHG|IUPD;
251*53592Sheideman 		return (VOP_UPDATE(ap->a_vp, &time, &time, 1));
25213000Ssam 	}
25352327Sbostic 
25452222Sbostic 	/*
25552222Sbostic 	 * Calculate index into inode's block list of last direct and indirect
25652222Sbostic 	 * blocks (if any) which we want to keep.  Lastblock is 0 when the
25752222Sbostic 	 * file is truncated to 0.
25852222Sbostic 	 */
259*53592Sheideman 	lastblock = lblkno(fs, ap->a_length + fs->lfs_bsize - 1);
26052222Sbostic 	olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1;
26151484Sbostic 
2621203Sbill 	/*
26351484Sbostic 	 * Update the size of the file. If the file is not being truncated to
26451484Sbostic 	 * a block boundry, the contents of the partial block following the end
26551484Sbostic 	 * of the file must be zero'ed in case it ever become accessable again
26651484Sbostic 	 * because of subsequent file growth.
2671203Sbill 	 */
268*53592Sheideman 	offset = blkoff(fs, ap->a_length);
26951484Sbostic 	if (offset == 0)
270*53592Sheideman 		ip->i_size = ap->a_length;
27151484Sbostic 	else {
272*53592Sheideman 		lbn = lblkno(fs, ap->a_length);
27341313Smckusick #ifdef QUOTA
27452222Sbostic 		if (error = getinoquota(ip))
27541313Smckusick 			return (error);
27651183Sbostic #endif
277*53592Sheideman 		if (error = bread(ap->a_vp, lbn, fs->lfs_bsize, NOCRED, &bp))
27837736Smckusick 			return (error);
279*53592Sheideman 		ip->i_size = ap->a_length;
28051857Sbostic 		size = blksize(fs);
281*53592Sheideman 		(void)vnode_pager_uncache(ap->a_vp);
28226272Skarels 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
28345112Smckusick 		allocbuf(bp, size);
28452078Sbostic 		LFS_UBWRITE(bp);
28517942Smckusick 	}
28652078Sbostic 	/*
28752222Sbostic 	 * Modify sup->su_nbyte counters for each deleted block; keep track
28852222Sbostic 	 * of number of blocks removed for ip->i_blocks.
28952222Sbostic 	 */
29052222Sbostic 	blocksreleased = 0;
29152222Sbostic 	num = 0;
29252222Sbostic 	lastseg = -1;
29352222Sbostic 
29452222Sbostic 	for (lbn = olastblock; lbn >= lastblock;) {
295*53592Sheideman 		lfs_bmaparray(ap->a_vp, lbn, &daddr, a, &depth);
29652222Sbostic 		if (lbn == olastblock)
29752222Sbostic 			for (i = NIADDR + 2; i--;)
29852222Sbostic 				a_end[i] = a[i];
29952222Sbostic 		switch (depth) {
30052222Sbostic 		case 0:				/* Direct block. */
30152222Sbostic 			daddr = ip->i_db[lbn];
30252222Sbostic 			SEGDEC;
30352222Sbostic 			ip->i_db[lbn] = 0;
30452222Sbostic 			--lbn;
30552222Sbostic 			break;
30652222Sbostic #ifdef DIAGNOSTIC
30752222Sbostic 		case 1:				/* An indirect block. */
30852222Sbostic 			panic("lfs_truncate: lfs_bmaparray returned depth 1");
30952222Sbostic 			/* NOTREACHED */
31052222Sbostic #endif
31152222Sbostic 		default:			/* Chain of indirect blocks. */
31253505Sheideman 			inp = a + --depth;
31353505Sheideman 			if (inp->in_off > 0 && lbn != lastblock) {
31453505Sheideman 				lbn -= inp->in_off < lbn - lastblock ?
31553505Sheideman 				    inp->in_off : lbn - lastblock;
31652222Sbostic 				break;
31752222Sbostic 			}
31853505Sheideman 			for (; depth && (inp->in_off == 0 || lbn == lastblock);
31953505Sheideman 			    --inp, --depth) {
32052222Sbostic 				/*
32152222Sbostic 				 * XXX
32252222Sbostic 				 * The indirect block may not yet exist, so
32352222Sbostic 				 * bread will create one just so we can free
32452222Sbostic 				 * it.
32552222Sbostic 				 */
326*53592Sheideman 				if (bread(ap->a_vp,
32753505Sheideman 				    inp->in_lbn, fs->lfs_bsize, NOCRED, &bp))
32852222Sbostic 					panic("lfs_truncate: bread bno %d",
32953505Sheideman 					    inp->in_lbn);
33053505Sheideman 				daddrp = bp->b_un.b_daddr + inp->in_off;
33153505Sheideman 				for (i = inp->in_off;
33252222Sbostic 				    i++ <= a_end[depth].in_off;) {
33352222Sbostic 					daddr = *daddrp++;
33452222Sbostic 					SEGDEC;
33552222Sbostic 				}
33653144Sstaelin 				a_end[depth].in_off = NINDIR(fs) - 1;
33753505Sheideman 				if (inp->in_off == 0)
33853144Sstaelin 					brelse (bp);
33953144Sstaelin 				else {
34053505Sheideman 					bzero(bp->b_un.b_daddr + inp->in_off,
34152222Sbostic 					    fs->lfs_bsize -
34253505Sheideman 					    inp->in_off * sizeof(daddr_t));
34352222Sbostic 					LFS_UBWRITE(bp);
34453144Sstaelin 				}
34552222Sbostic 			}
34653144Sstaelin 			if (depth == 0 && a[1].in_off == 0) {
34752222Sbostic 				off = a[0].in_off;
34852222Sbostic 				daddr = ip->i_ib[off];
34952222Sbostic 				SEGDEC;
35052222Sbostic 				ip->i_ib[off] = 0;
35152222Sbostic 			}
35252681Sstaelin 			if (lbn == lastblock || lbn <= NDADDR)
35352222Sbostic 				--lbn;
35452222Sbostic 			else {
35552222Sbostic 				lbn -= NINDIR(fs);
35652222Sbostic 				if (lbn < lastblock)
35752222Sbostic 					lbn = lastblock;
35852222Sbostic 			}
35952222Sbostic 		}
36052222Sbostic 	}
36152225Sbostic 	UPDATE_SEGUSE;
36252222Sbostic 	ip->i_blocks -= blocksreleased;
36352222Sbostic 	/*
36452078Sbostic 	 * XXX
36552222Sbostic 	 * Currently, we don't know when we allocate an indirect block, so
36652222Sbostic 	 * ip->i_blocks isn't getting incremented appropriately.  As a result,
36752222Sbostic 	 * when we delete any indirect blocks, we get a bad number here.
36852078Sbostic 	 */
36952222Sbostic 	if (ip->i_blocks < 0)
37052222Sbostic 		ip->i_blocks = 0;
37152222Sbostic 	ip->i_flag |= ICHG|IUPD;
372*53592Sheideman 	(void)vinvalbuf(ap->a_vp, ap->a_length > 0);
373*53592Sheideman 	error = VOP_UPDATE(ap->a_vp, &time, &time, MNT_WAIT);
37451857Sbostic 	return (0);
37524Sbill }
376