xref: /csrg-svn/sys/ufs/lfs/lfs_inode.c (revision 52225)
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*52225Sbostic  *	@(#)lfs_inode.c	7.55 (Berkeley) 01/19/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 
2051498Sbostic #include <ufs/ufs/quota.h>
2151498Sbostic #include <ufs/ufs/inode.h>
2251498Sbostic #include <ufs/ufs/ufsmount.h>
2351498Sbostic #include <ufs/ufs/ufs_extern.h>
2447571Skarels 
2551498Sbostic #include <ufs/lfs/lfs.h>
2651498Sbostic #include <ufs/lfs/lfs_extern.h>
2724Sbill 
2851346Sbostic int
2951155Sbostic lfs_init()
3024Sbill {
3151857Sbostic #ifdef VERBOSE
3251857Sbostic 	printf("lfs_init\n");
3351857Sbostic #endif
3451484Sbostic 	return (ufs_init());
3524Sbill }
3624Sbill 
3724Sbill /*
3851484Sbostic  * Look up an LFS dinode number to find its incore vnode.  If not already
3951484Sbostic  * in core, read it in from the specified device.  Return the inode locked.
4051484Sbostic  * Detection and handling of mount points must be done by the calling routine.
4124Sbill  */
4251346Sbostic int
4351562Smckusick lfs_vget(mntp, ino, vpp)
4451562Smckusick 	struct mount *mntp;
454818Swnj 	ino_t ino;
4651562Smckusick 	struct vnode **vpp;
4724Sbill {
4851498Sbostic 	register struct lfs *fs;
4951484Sbostic 	register struct inode *ip;
5051484Sbostic 	struct buf *bp;
5152224Sbostic 	struct ifile *ifp;
5251498Sbostic 	struct vnode *vp;
5351562Smckusick 	struct ufsmount *ump;
5452224Sbostic 	daddr_t daddr;
5551484Sbostic 	dev_t dev;
5651346Sbostic 	int error;
5724Sbill 
5851857Sbostic #ifdef VERBOSE
5951857Sbostic 	printf("lfs_vget\n");
6051857Sbostic #endif
6151562Smckusick 	ump = VFSTOUFS(mntp);
6251562Smckusick 	dev = ump->um_dev;
6351562Smckusick 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
6451484Sbostic 		return (0);
6551484Sbostic 
6652224Sbostic 	/* Translate the inode number to a disk address. */
6752224Sbostic 	fs = ump->um_lfs;
6852224Sbostic 	if (ino == LFS_IFILE_INUM)
6952224Sbostic 		daddr = fs->lfs_idaddr;
7052224Sbostic 	else {
7152224Sbostic 		LFS_IENTRY(ifp, fs, ino, bp);
7252224Sbostic 		daddr = ifp->if_daddr;
7352224Sbostic 		brelse(bp);
7452224Sbostic 		if (daddr == LFS_UNUSED_DADDR)
7552224Sbostic 			return (ENOENT);
7652224Sbostic 	}
7752224Sbostic 
7851155Sbostic 	/* Allocate new vnode/inode. */
7951498Sbostic 	if (error = lfs_vcreate(mntp, ino, &vp)) {
8051562Smckusick 		*vpp = NULL;
8137736Smckusick 		return (error);
8237736Smckusick 	}
8352224Sbostic 
8437736Smckusick 	/*
8539440Smckusick 	 * Put it onto its hash chain and lock it so that other requests for
8639440Smckusick 	 * this inode will block if they arrive while we are sleeping waiting
8739440Smckusick 	 * for old data structures to be purged or for the contents of the
8839440Smckusick 	 * disk portion of this inode to be read.
8939440Smckusick 	 */
9051562Smckusick 	ip = VTOI(vp);
9151484Sbostic 	ufs_ihashins(ip);
9251155Sbostic 
9352224Sbostic 	/*
9452224Sbostic 	 * XXX
9552224Sbostic 	 * This may not need to be here, logically it should go down with
9652224Sbostic 	 * the i_devvp initialization.
9752224Sbostic 	 * Ask Kirk.
9852224Sbostic 	 */
9952224Sbostic 	ip->i_lfs = ump->um_lfs;
10052224Sbostic 
10151484Sbostic 	/* Read in the disk contents for the inode, copy into the inode. */
10252224Sbostic 	if (error =
10352224Sbostic 	    bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) {
10437736Smckusick 		/*
10551857Sbostic 		 * The inode does not contain anything useful, so it
10651857Sbostic 		 * would be misleading to leave it on its hash chain.
10751857Sbostic 		 * Iput() will return it to the free list.
10841334Smckusick 		 */
10941334Smckusick 		remque(ip);
11041334Smckusick 		ip->i_forw = ip;
11141334Smckusick 		ip->i_back = ip;
11251484Sbostic 
11351484Sbostic 		/* Unlock and discard unneeded inode. */
11451484Sbostic 		ufs_iput(ip);
11537736Smckusick 		brelse(bp);
11651562Smckusick 		*vpp = NULL;
11739440Smckusick 		return (error);
11837736Smckusick 	}
11951155Sbostic 	ip->i_din = *lfs_ifind(fs, ino, bp->b_un.b_dino);
12039440Smckusick 	brelse(bp);
12151155Sbostic 
12251498Sbostic 	/*
12351498Sbostic 	 * Initialize the vnode from the inode, check for aliases.  In all
12451498Sbostic 	 * cases re-init ip, the underlying vnode/inode may have changed.
12551498Sbostic 	 */
12651595Smckusick 	if (error = ufs_vinit(mntp, &lfs_specops, LFS_FIFOOPS, &vp)) {
12751484Sbostic 		ufs_iput(ip);
12851562Smckusick 		*vpp = NULL;
12951484Sbostic 		return (error);
13040289Smckusick 	}
13151562Smckusick 	/*
13251562Smckusick 	 * Finish inode initialization now that aliasing has been resolved.
13351562Smckusick 	 */
13451562Smckusick 	ip->i_devvp = ump->um_devvp;
13551562Smckusick 	VREF(ip->i_devvp);
13651562Smckusick 	*vpp = vp;
13737736Smckusick 	return (0);
13837736Smckusick }
1397334Skre 
14051346Sbostic int
14151562Smckusick lfs_update(vp, ta, tm, waitfor)
14251562Smckusick 	register struct vnode *vp;
14351484Sbostic 	struct timeval *ta, *tm;
14451484Sbostic         int waitfor;
1457118Smckusick {
14651562Smckusick 	struct inode *ip;
14751562Smckusick 
14851857Sbostic #ifdef VERBOSE
14951857Sbostic 	printf("lfs_update\n");
15051857Sbostic #endif
15151562Smckusick 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
15251562Smckusick 		return (0);
15351562Smckusick 	ip = VTOI(vp);
15451562Smckusick 	if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
15551562Smckusick 		return (0);
15651562Smckusick 	if (ip->i_flag&IACC)
15751562Smckusick 		ip->i_atime = ta->tv_sec;
15852018Smckusick 	if (ip->i_flag&IUPD) {
15951562Smckusick 		ip->i_mtime = tm->tv_sec;
16052018Smckusick 		INCRQUAD((ip)->i_modrev);
16152018Smckusick 	}
16251562Smckusick 	if (ip->i_flag&ICHG)
16351562Smckusick 		ip->i_ctime = time.tv_sec;
16451562Smckusick 	ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
16551562Smckusick 
16638452Smckusick 	/*
16751484Sbostic 	 * XXX
16851562Smckusick 	 * I'm not real sure what to do here; once we have fsync and partial
16951562Smckusick 	 * segments working in the LFS context, this must be fixed to be
17051562Smckusick 	 * correct.  The contents of the inode have to be pushed back to
17152078Sbostic 	 * stable storage.
17238452Smckusick 	 */
17351484Sbostic 	return (0);
17424Sbill }
17524Sbill 
17652222Sbostic /* Update segment usage information when removing a block. */
177*52225Sbostic #define UPDATE_SEGUSE \
178*52225Sbostic 	if (lastseg != -1) { \
179*52225Sbostic 		LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
180*52225Sbostic 		sup->su_nbytes -= fs->lfs_bsize * num; \
181*52225Sbostic 		LFS_UBWRITE(sup_bp); \
182*52225Sbostic 		blocksreleased += num; \
183*52225Sbostic 	}
18452222Sbostic 
18552222Sbostic #define SEGDEC { \
18652222Sbostic 	if (daddr != UNASSIGNED) { \
18752222Sbostic 		if (lastseg != (seg = datosn(fs, daddr))) { \
18852222Sbostic 			UPDATE_SEGUSE; \
18952222Sbostic 			num = 1; \
19052222Sbostic 			lastseg = seg; \
19152222Sbostic 		} else \
19252222Sbostic 			++num; \
19352222Sbostic 	} \
19452222Sbostic }
19552222Sbostic 
19624Sbill /*
19752222Sbostic  * Truncate the inode ip to at most length size.  Update segment usage
19852222Sbostic  * table information.
19924Sbill  */
20051484Sbostic /* ARGSUSED */
20151484Sbostic int
20252222Sbostic lfs_truncate(vp, length, flags)
20352222Sbostic 	struct vnode *vp;
2049165Ssam 	u_long length;
20539676Smckusick 	int flags;
20624Sbill {
20752222Sbostic 	register INDIR *ap;
20852222Sbostic 	register int i;
20952222Sbostic 	register daddr_t *daddrp;
210*52225Sbostic 	struct buf *bp, *sup_bp;
21152222Sbostic 	struct inode *ip;
21252222Sbostic 	struct lfs *fs;
21352222Sbostic 	INDIR a[NIADDR + 2], a_end[NIADDR + 2];
21452222Sbostic 	SEGUSE *sup;
21552222Sbostic 	daddr_t daddr, lastblock, lbn, olastblock;
21652222Sbostic 	off_t off;
21752222Sbostic 	long blocksreleased;
21852222Sbostic 	int error, depth, lastseg, num, offset, seg, size;
2199165Ssam 
22051857Sbostic #ifdef VERBOSE
22151857Sbostic 	printf("lfs_truncate\n");
22251857Sbostic #endif
22352222Sbostic 	vnode_pager_setsize(vp, length);
22452222Sbostic 	ip = VTOI(vp);
22551484Sbostic 	/* If length is larger than the file, just update the times. */
22652222Sbostic 	if (ip->i_size <= length) {
22752222Sbostic 		ip->i_flag |= ICHG|IUPD;
22852222Sbostic 		return (lfs_update(vp, &time, &time, 1));
22913000Ssam 	}
23052222Sbostic 	/*
23152222Sbostic 	 * Calculate index into inode's block list of last direct and indirect
23252222Sbostic 	 * blocks (if any) which we want to keep.  Lastblock is 0 when the
23352222Sbostic 	 * file is truncated to 0.
23452222Sbostic 	 */
23552222Sbostic 	fs = ip->i_lfs;
23652222Sbostic 	lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
23752222Sbostic 	olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1;
23851484Sbostic 
2391203Sbill 	/*
24051484Sbostic 	 * Update the size of the file. If the file is not being truncated to
24151484Sbostic 	 * a block boundry, the contents of the partial block following the end
24251484Sbostic 	 * of the file must be zero'ed in case it ever become accessable again
24351484Sbostic 	 * because of subsequent file growth.
2441203Sbill 	 */
24517942Smckusick 	offset = blkoff(fs, length);
24651484Sbostic 	if (offset == 0)
24752222Sbostic 		ip->i_size = length;
24851484Sbostic 	else {
24917942Smckusick 		lbn = lblkno(fs, length);
25041313Smckusick #ifdef QUOTA
25152222Sbostic 		if (error = getinoquota(ip))
25241313Smckusick 			return (error);
25351183Sbostic #endif
25452222Sbostic 		if (error = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp))
25537736Smckusick 			return (error);
25652222Sbostic 		ip->i_size = length;
25751857Sbostic 		size = blksize(fs);
25852222Sbostic 		(void)vnode_pager_uncache(vp);
25926272Skarels 		bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
26045112Smckusick 		allocbuf(bp, size);
26152078Sbostic 		LFS_UBWRITE(bp);
26217942Smckusick 	}
26352078Sbostic 	/*
26452222Sbostic 	 * Modify sup->su_nbyte counters for each deleted block; keep track
26552222Sbostic 	 * of number of blocks removed for ip->i_blocks.
26652222Sbostic 	 */
26752222Sbostic 	blocksreleased = 0;
26852222Sbostic 	num = 0;
26952222Sbostic 	lastseg = -1;
27052222Sbostic 
27152222Sbostic 	for (lbn = olastblock; lbn >= lastblock;) {
27252222Sbostic 		lfs_bmaparray(vp, lbn, &daddr, a, &depth);
27352222Sbostic 		if (lbn == olastblock)
27452222Sbostic 			for (i = NIADDR + 2; i--;)
27552222Sbostic 				a_end[i] = a[i];
27652222Sbostic 		switch (depth) {
27752222Sbostic 		case 0:				/* Direct block. */
27852222Sbostic 			daddr = ip->i_db[lbn];
27952222Sbostic 			SEGDEC;
28052222Sbostic 			ip->i_db[lbn] = 0;
28152222Sbostic 			--lbn;
28252222Sbostic 			break;
28352222Sbostic #ifdef DIAGNOSTIC
28452222Sbostic 		case 1:				/* An indirect block. */
28552222Sbostic 			panic("lfs_truncate: lfs_bmaparray returned depth 1");
28652222Sbostic 			/* NOTREACHED */
28752222Sbostic #endif
28852222Sbostic 		default:			/* Chain of indirect blocks. */
28952222Sbostic 			ap = a + --depth;
29052222Sbostic 			if (ap->in_off > 0 && lbn != lastblock) {
29152222Sbostic 				lbn -= ap->in_off < lbn - lastblock ?
29252222Sbostic 				    ap->in_off : lbn - lastblock;
29352222Sbostic 				break;
29452222Sbostic 			}
29552222Sbostic 			for (; depth && (ap->in_off == 0 || lbn == lastblock);
29652222Sbostic 			    --ap, --depth) {
29752222Sbostic 				/*
29852222Sbostic 				 * XXX
29952222Sbostic 				 * The indirect block may not yet exist, so
30052222Sbostic 				 * bread will create one just so we can free
30152222Sbostic 				 * it.
30252222Sbostic 				 */
30352222Sbostic 				if (bread(vp,
30452222Sbostic 				    ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
30552222Sbostic 					panic("lfs_truncate: bread bno %d",
30652222Sbostic 					    ap->in_lbn);
30752222Sbostic 				daddrp = bp->b_un.b_daddr + ap->in_off;
30852222Sbostic 				for (i = ap->in_off;
30952222Sbostic 				    i++ <= a_end[depth].in_off;) {
31052222Sbostic 					daddr = *daddrp++;
31152222Sbostic 					SEGDEC;
31252222Sbostic 				}
31352222Sbostic 				a_end[depth].in_off=NINDIR(fs)-1;
31452222Sbostic 				if (ap->in_off > 0 && lbn == lastblock) {
31552222Sbostic 					bzero(bp->b_un.b_daddr + ap->in_off,
31652222Sbostic 					    fs->lfs_bsize -
31752222Sbostic 					    ap->in_off * sizeof(daddr_t));
31852222Sbostic 					LFS_UBWRITE(bp);
31952222Sbostic 				} else
32052222Sbostic 					brelse (bp);
32152222Sbostic 			}
32252222Sbostic 			if (a[1].in_off == 0) {
32352222Sbostic 				off = a[0].in_off;
32452222Sbostic 				daddr = ip->i_ib[off];
32552222Sbostic 				SEGDEC;
32652222Sbostic 				ip->i_ib[off] = 0;
32752222Sbostic 			}
32852222Sbostic 			if (lbn == lastblock)
32952222Sbostic 				--lbn;
33052222Sbostic 			else {
33152222Sbostic 				lbn -= NINDIR(fs);
33252222Sbostic 				if (lbn < lastblock)
33352222Sbostic 					lbn = lastblock;
33452222Sbostic 			}
33552222Sbostic 		}
33652222Sbostic 	}
337*52225Sbostic 	UPDATE_SEGUSE;
33852222Sbostic 	ip->i_blocks -= blocksreleased;
33952222Sbostic 	/*
34052078Sbostic 	 * XXX
34152222Sbostic 	 * Currently, we don't know when we allocate an indirect block, so
34252222Sbostic 	 * ip->i_blocks isn't getting incremented appropriately.  As a result,
34352222Sbostic 	 * when we delete any indirect blocks, we get a bad number here.
34452078Sbostic 	 */
34552222Sbostic 	if (ip->i_blocks < 0)
34652222Sbostic 		ip->i_blocks = 0;
34752222Sbostic 	ip->i_flag |= ICHG|IUPD;
34852222Sbostic 	(void)vinvalbuf(vp, length > 0);
34952222Sbostic 	error = lfs_update(vp, &time, &time, MNT_WAIT);
35051857Sbostic 	return (0);
35124Sbill }
352