xref: /csrg-svn/sys/ufs/lfs/lfs_balloc.c (revision 53574)
123396Smckusick /*
251494Sbostic  * Copyright (c) 1989, 1991 Regents of the University of California.
337736Smckusick  * All rights reserved.
423396Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*53574Sheideman  *	@(#)lfs_balloc.c	7.29 (Berkeley) 05/15/92
823396Smckusick  */
97443Sroot 
1051486Sbostic #include <sys/param.h>
1151486Sbostic #include <sys/buf.h>
1251486Sbostic #include <sys/proc.h>
1351486Sbostic #include <sys/vnode.h>
1451486Sbostic #include <sys/mount.h>
1551486Sbostic #include <sys/resourcevar.h>
1651486Sbostic #include <sys/specdev.h>
1751486Sbostic #include <sys/trace.h>
187443Sroot 
1951494Sbostic #include <ufs/ufs/quota.h>
2051494Sbostic #include <ufs/ufs/inode.h>
2151494Sbostic #include <ufs/ufs/ufsmount.h>
2247571Skarels 
2351494Sbostic #include <ufs/lfs/lfs.h>
2451494Sbostic #include <ufs/lfs/lfs_extern.h>
2551486Sbostic 
2651962Sbostic int lfs_getlbns __P((struct vnode *, daddr_t, INDIR *, int *));
2751855Sbostic 
287443Sroot /*
2951486Sbostic  * Bmap converts a the logical block number of a file to its physical block
3051486Sbostic  * number on the disk. The conversion is done by using the logical block
3151486Sbostic  * number to index into the array of block pointers described by the dinode.
3251855Sbostic  */
3351855Sbostic int
3453528Sheideman lfs_bmap (ap)
3553528Sheideman 	struct vop_bmap_args *ap;
3653528Sheideman #define vp (ap->a_vp)
3753528Sheideman #define bn (ap->a_bn)
3853528Sheideman #define vpp (ap->a_vpp)
3953528Sheideman #define bnp (ap->a_bnp)
4051855Sbostic {
4151855Sbostic #ifdef VERBOSE
4251855Sbostic 	printf("lfs_bmap\n");
4351855Sbostic #endif
4451855Sbostic 	/*
4551855Sbostic 	 * Check for underlying vnode requests and ensure that logical
4651855Sbostic 	 * to physical mapping is requested.
4751855Sbostic 	 */
4851855Sbostic 	if (vpp != NULL)
4951855Sbostic 		*vpp = VTOI(vp)->i_devvp;
5051855Sbostic 	if (bnp == NULL)
5151855Sbostic 		return (0);
5251855Sbostic 
5351855Sbostic 	return (lfs_bmaparray(vp, bn, bnp, NULL, NULL));
5451855Sbostic }
5553528Sheideman #undef vp
5653528Sheideman #undef bn
5753528Sheideman #undef vpp
5853528Sheideman #undef bnp
5951855Sbostic 
6051855Sbostic /*
6151846Sbostic  * LFS has a different version of bmap from FFS because of a naming conflict.
6251846Sbostic  * In FFS, meta blocks are given real disk addresses at allocation time, and
6351846Sbostic  * are linked into the device vnode, using a logical block number which is
6451846Sbostic  * the same as the physical block number.  This can't be done by LFS because
6551846Sbostic  * blocks aren't given disk addresses until they're written, so there's no
6651846Sbostic  * way to distinguish the meta-data blocks for one file from any other file.
6751846Sbostic  * This means that meta-data blocks have to be on the vnode for the file so
6851846Sbostic  * they can be found, and have to have "names" different from the standard
6951846Sbostic  * data blocks.  To do this, we divide the name space into positive and
7051846Sbostic  * negative block numbers, and give the meta-data blocks negative logical
7151847Sbostic  * numbers.  Indirect blocks are addressed by the negative address of the
7251847Sbostic  * first data block to which they point.  Double indirect blocks are addressed
7351847Sbostic  * by one less than the address of the first indirect block to which they
7451847Sbostic  * point.  Triple indirect blocks are addressed by one less than the address
7551847Sbostic  * of the first double indirect block to which they point.
767443Sroot  */
7751348Sroot int
7851855Sbostic lfs_bmaparray(vp, bn, bnp, ap, nump)
7951561Smckusick 	struct vnode *vp;
8037736Smckusick 	register daddr_t bn;
8151561Smckusick 	daddr_t *bnp;
8251855Sbostic 	INDIR *ap;
8351855Sbostic 	int *nump;
847443Sroot {
85*53574Sheideman 	USES_VOP_STRATEGY;
8651561Smckusick 	register struct inode *ip;
8751846Sbostic 	struct buf *bp;
8851855Sbostic 	struct lfs *fs;
8951561Smckusick 	struct vnode *devvp;
9051855Sbostic 	INDIR a[NIADDR], *xap;
9151855Sbostic 	daddr_t *bap, daddr;
9251855Sbostic 	long metalbn;
9351855Sbostic 	int error, num, off;
9437736Smckusick 
9551855Sbostic 
9651561Smckusick 	ip = VTOI(vp);
9751846Sbostic #ifdef VERBOSE
9851855Sbostic 	printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
9951846Sbostic #endif
10051855Sbostic #ifdef DIAGNOSTIC
10151855Sbostic 	if (ap != NULL && nump == NULL || ap == NULL && nump != NULL)
10251855Sbostic 		panic("lfs_bmaparray: invalid arguments");
10351855Sbostic #endif
10451847Sbostic 
10551855Sbostic 	xap = ap == NULL ? a : ap;
10651855Sbostic 	if (error = lfs_getlbns(vp, bn, xap, nump))
10751855Sbostic 		return (error);
10851855Sbostic 
10951855Sbostic 	num = *nump;
11052994Sbostic 
11151855Sbostic 	if (num == 0) {
11251855Sbostic 		*bnp = ip->i_db[bn];
11351855Sbostic 		if (*bnp == 0)
11451348Sroot 			*bnp = UNASSIGNED;
11537736Smckusick 		return (0);
11637736Smckusick 	}
11751486Sbostic 
11852994Sbostic 
11952994Sbostic 	/* Get disk address out of indirect block array */
12052994Sbostic 	daddr = ip->i_ib[xap->in_off];
12152994Sbostic 
12251855Sbostic 	/* Fetch through the indirect blocks. */
12352994Sbostic 	fs = ip->i_lfs;
12451183Sbostic 	devvp = VFSTOUFS(vp->v_mount)->um_devvp;
12552994Sbostic 
12652994Sbostic 	for (bp = NULL, ++xap; daddr && --num; ++xap) {
12752994Sbostic 		/* If looking for a meta-block, break out when we find it. */
12851855Sbostic 		metalbn = xap->in_lbn;
12951855Sbostic 		if (metalbn == bn)
13051847Sbostic 			break;
13151847Sbostic 
13251846Sbostic 		/*
13351846Sbostic 		 * Read in the appropriate indirect block.  LFS can't do a
13451846Sbostic 		 * bread because bread knows that FFS will hand it the device
13551846Sbostic 		 * vnode, not the file vnode, so the b_dev and b_blkno would
13651846Sbostic 		 * be wrong.
13751846Sbostic 		 *
13851846Sbostic 		 * XXX
13951846Sbostic 		 * This REALLY needs to be fixed, at the very least it needs
14051855Sbostic 		 * to be rethought when the buffer cache goes away.  When it's
14151855Sbostic 		 * fixed, change lfs_bmaparray and lfs_getlbns to take an ip,
14251855Sbostic 		 * not a vp.
14351846Sbostic 		 */
14451183Sbostic 		if (bp)
14537736Smckusick 			brelse(bp);
14651847Sbostic 		bp = getblk(vp, metalbn, fs->lfs_bsize);
14751183Sbostic 		if (bp->b_flags & (B_DONE | B_DELWRI)) {
14851847Sbostic 			trace(TR_BREADHIT, pack(vp, size), metalbn);
14951183Sbostic 		} else {
15051847Sbostic 			trace(TR_BREADMISS, pack(vp, size), metalbn);
15151847Sbostic 			bp->b_blkno = daddr;
15251846Sbostic 			bp->b_flags |= B_READ;
15351183Sbostic 			bp->b_dev = devvp->v_rdev;
154*53574Sheideman 			/*
155*53574Sheideman 			 * Call a strategy VOP by hand.
156*53574Sheideman 			 */
157*53574Sheideman 			vop_strategy_a.a_desc = VDESC(vop_strategy);
158*53574Sheideman 			vop_strategy_a.a_bp=bp;
159*53574Sheideman 			VOCALL(devvp->v_op, VOFFSET(vop_strategy), \
160*53574Sheideman 			       &vop_strategy_a);
16151183Sbostic 			curproc->p_stats->p_ru.ru_inblock++;	/* XXX */
16251183Sbostic 			if (error = biowait(bp)) {
16351183Sbostic 				brelse(bp);
16451183Sbostic 				return (error);
16551183Sbostic 			}
16637736Smckusick 		}
16752994Sbostic 		daddr = bp->b_un.b_daddr[xap->in_off];
16851183Sbostic 	}
16951183Sbostic 	if (bp)
17039679Smckusick 		brelse(bp);
17151183Sbostic 
17252994Sbostic 	*bnp = daddr == 0 ? UNASSIGNED : daddr;
17337736Smckusick 	return (0);
17437736Smckusick }
17551855Sbostic 
17651855Sbostic /*
17751855Sbostic  * Create an array of logical block number/offset pairs which represent the
17851855Sbostic  * path of indirect blocks required to access a data block.  The first "pair"
17951855Sbostic  * contains the logical block number of the appropriate single, double or
18051855Sbostic  * triple indirect block and the offset into the inode indirect block array.
18151855Sbostic  * Note, the logical block number of the inode single/double/triple indirect
18251855Sbostic  * block appears twice in the array, once with the offset into the i_ib and
18351855Sbostic  * once with the offset into the page itself.
18451855Sbostic  */
18551855Sbostic int
18651855Sbostic lfs_getlbns(vp, bn, ap, nump)
18751855Sbostic 	struct vnode *vp;
18851855Sbostic 	register daddr_t bn;
18951855Sbostic 	INDIR *ap;
19051855Sbostic 	int *nump;
19151855Sbostic {
19251855Sbostic 	struct lfs *fs;
19351855Sbostic 	long metalbn, realbn;
19451855Sbostic 	int j, off, sh;
19551855Sbostic 
19651855Sbostic #ifdef VERBOSE
19751855Sbostic 	printf("lfs_getlbns: bn %d, inode %d\n", bn, VTOI(vp)->i_number);
19851855Sbostic #endif
19951855Sbostic 	*nump = 0;
20051855Sbostic 	realbn = bn;
20151855Sbostic 	if ((long)bn < 0)
20251855Sbostic 		bn = -(long)bn;
20351855Sbostic 
20451855Sbostic 	/* The first NDADDR blocks are direct blocks. */
20551855Sbostic 	if (bn < NDADDR)
20652994Sbostic 		return (0);
20751855Sbostic 
20851855Sbostic 	/*
20951855Sbostic 	 * Determine the number of levels of indirection.  After this loop
21051855Sbostic 	 * is done, sh indicates the number of data blocks possible at the
21151855Sbostic 	 * given level of indirection, and NIADDR - j is the number of levels
21251855Sbostic 	 * of indirection needed to locate the requested block.
21351855Sbostic 	 */
21451855Sbostic 	bn -= NDADDR;
21551855Sbostic 	fs = VTOI(vp)->i_lfs;
21651855Sbostic 	sh = 1;
21751855Sbostic 	for (j = NIADDR; j > 0; j--) {
21851855Sbostic 		sh *= NINDIR(fs);
21951855Sbostic 		if (bn < sh)
22051855Sbostic 			break;
22151855Sbostic 		bn -= sh;
22251855Sbostic 	}
22351855Sbostic 	if (j == 0)
22451855Sbostic 		return (EFBIG);
22551855Sbostic 
22651855Sbostic 	/* Calculate the address of the first meta-block. */
22751855Sbostic 	if (realbn >= 0)
22851855Sbostic 		metalbn = -(realbn - bn + NIADDR - j);
22951855Sbostic 	else
23051855Sbostic 		metalbn = -(-realbn - bn + NIADDR - j);
23151855Sbostic 
23251855Sbostic 	/*
23351855Sbostic 	 * At each iteration, off is the offset into the bap array which is
23451855Sbostic 	 * an array of disk addresses at the current level of indirection.
23551855Sbostic 	 * The logical block number and the offset in that block are stored
23651855Sbostic 	 * into the argument array.
23751855Sbostic 	 */
23851855Sbostic 	++*nump;
23951855Sbostic 	ap->in_lbn = metalbn;
24051855Sbostic 	ap->in_off = off = NIADDR - j;
24151855Sbostic 	ap++;
24251855Sbostic 	for (; j <= NIADDR; j++) {
24351855Sbostic 		/* If searching for a meta-data block, quit when found. */
24451855Sbostic 		if (metalbn == realbn)
24551855Sbostic 			break;
24651855Sbostic 
24751855Sbostic 		sh /= NINDIR(fs);
24851855Sbostic 		off = (bn / sh) % NINDIR(fs);
24951855Sbostic 
25051855Sbostic 		++*nump;
25151855Sbostic 		ap->in_lbn = metalbn;
25251855Sbostic 		ap->in_off = off;
25351855Sbostic 		++ap;
25451855Sbostic 
25551855Sbostic 		metalbn -= -1 + off * sh;
25651855Sbostic 	}
25751855Sbostic 	return (0);
25851855Sbostic }
25952082Sbostic 
26052082Sbostic int
26152082Sbostic lfs_balloc(vp, iosize, lbn, bpp)
26252082Sbostic 	struct vnode *vp;
26352082Sbostic 	u_long iosize;
26452082Sbostic 	daddr_t lbn;
26552082Sbostic 	struct buf **bpp;
26652082Sbostic {
26753528Sheideman 	USES_VOP_BMAP;
26852082Sbostic 	struct buf *bp;
26952082Sbostic 	struct inode *ip;
27052082Sbostic 	struct lfs *fs;
27152082Sbostic 	daddr_t daddr;
27252082Sbostic 	int error, newblock;
27352082Sbostic 
27452082Sbostic 	ip = VTOI(vp);
27552082Sbostic 	fs = ip->i_lfs;
27652082Sbostic 
27752082Sbostic 	/*
27852082Sbostic 	 * Three cases: it's a block beyond the end of file, it's a block in
27952082Sbostic 	 * the file that may or may not have been assigned a disk address or
28052082Sbostic 	 * we're writing an entire block.  Note, if the daddr is unassigned,
28152082Sbostic 	 * the block might still have existed in the cache.  If it did, make
28252082Sbostic 	 * sure we don't count it as a new block or zero out its contents.
28352082Sbostic 	 */
28452082Sbostic 	newblock = ip->i_size <= lbn << fs->lfs_bshift;
28553528Sheideman 	if (!newblock && (error = VOP_BMAP(vp, lbn, NULL, &daddr)))
28652994Sbostic 		return (error);
28752082Sbostic 
28852172Sbostic 	if (newblock || daddr == UNASSIGNED || iosize == fs->lfs_bsize) {
28952082Sbostic 		*bpp = bp = getblk(vp, lbn, fs->lfs_bsize);
29052082Sbostic 		if (newblock ||
29152172Sbostic 		    daddr == UNASSIGNED && !(bp->b_flags & B_CACHE)) {
29252082Sbostic 			++ip->i_blocks;
29352082Sbostic 			if (iosize != fs->lfs_bsize)
29452082Sbostic 				clrbuf(bp);
29552082Sbostic 		}
29652994Sbostic 		return (0);
29752082Sbostic 	}
29852994Sbostic 	return (bread(vp, lbn, fs->lfs_bsize, NOCRED, bpp));
29952082Sbostic 
30052082Sbostic }
301