xref: /csrg-svn/sys/ufs/lfs/lfs_balloc.c (revision 54687)
123396Smckusick /*
251494Sbostic  * Copyright (c) 1989, 1991 Regents of the University of California.
337736Smckusick  * All rights reserved.
423396Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*54687Sbostic  *	@(#)lfs_balloc.c	7.31 (Berkeley) 07/05/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
34*54687Sbostic lfs_bmap(ap)
35*54687Sbostic 	struct vop_bmap_args /* {
36*54687Sbostic 		struct vnode *a_vp;
37*54687Sbostic 		daddr_t  a_bn;
38*54687Sbostic 		struct vnode **a_vpp;
39*54687Sbostic 		daddr_t *a_bnp;
40*54687Sbostic 	} */ *ap;
4151855Sbostic {
4251855Sbostic #ifdef VERBOSE
4351855Sbostic 	printf("lfs_bmap\n");
4451855Sbostic #endif
4551855Sbostic 	/*
4651855Sbostic 	 * Check for underlying vnode requests and ensure that logical
4751855Sbostic 	 * to physical mapping is requested.
4851855Sbostic 	 */
4953591Sheideman 	if (ap->a_vpp != NULL)
5053591Sheideman 		*ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
5153591Sheideman 	if (ap->a_bnp == NULL)
5251855Sbostic 		return (0);
5351855Sbostic 
5453591Sheideman 	return (lfs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL));
5551855Sbostic }
5651855Sbostic 
5751855Sbostic /*
5851846Sbostic  * LFS has a different version of bmap from FFS because of a naming conflict.
5951846Sbostic  * In FFS, meta blocks are given real disk addresses at allocation time, and
6051846Sbostic  * are linked into the device vnode, using a logical block number which is
6151846Sbostic  * the same as the physical block number.  This can't be done by LFS because
6251846Sbostic  * blocks aren't given disk addresses until they're written, so there's no
6351846Sbostic  * way to distinguish the meta-data blocks for one file from any other file.
6451846Sbostic  * This means that meta-data blocks have to be on the vnode for the file so
6551846Sbostic  * they can be found, and have to have "names" different from the standard
6651846Sbostic  * data blocks.  To do this, we divide the name space into positive and
6751846Sbostic  * negative block numbers, and give the meta-data blocks negative logical
6851847Sbostic  * numbers.  Indirect blocks are addressed by the negative address of the
6951847Sbostic  * first data block to which they point.  Double indirect blocks are addressed
7051847Sbostic  * by one less than the address of the first indirect block to which they
7151847Sbostic  * point.  Triple indirect blocks are addressed by one less than the address
7251847Sbostic  * of the first double indirect block to which they point.
737443Sroot  */
7451348Sroot int
7551855Sbostic lfs_bmaparray(vp, bn, bnp, ap, nump)
7651561Smckusick 	struct vnode *vp;
7737736Smckusick 	register daddr_t bn;
7851561Smckusick 	daddr_t *bnp;
7951855Sbostic 	INDIR *ap;
8051855Sbostic 	int *nump;
817443Sroot {
8251561Smckusick 	register struct inode *ip;
8351846Sbostic 	struct buf *bp;
8451855Sbostic 	struct lfs *fs;
8551561Smckusick 	struct vnode *devvp;
8651855Sbostic 	INDIR a[NIADDR], *xap;
8751855Sbostic 	daddr_t *bap, daddr;
8851855Sbostic 	long metalbn;
8951855Sbostic 	int error, num, off;
90*54687Sbostic 	struct vop_strategy_args vop_strategy_a;
9137736Smckusick 
9251561Smckusick 	ip = VTOI(vp);
9351846Sbostic #ifdef VERBOSE
9451855Sbostic 	printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
9551846Sbostic #endif
9651855Sbostic #ifdef DIAGNOSTIC
9751855Sbostic 	if (ap != NULL && nump == NULL || ap == NULL && nump != NULL)
9851855Sbostic 		panic("lfs_bmaparray: invalid arguments");
9951855Sbostic #endif
10051847Sbostic 
10151855Sbostic 	xap = ap == NULL ? a : ap;
10251855Sbostic 	if (error = lfs_getlbns(vp, bn, xap, nump))
10351855Sbostic 		return (error);
10451855Sbostic 
10551855Sbostic 	num = *nump;
10652994Sbostic 
10751855Sbostic 	if (num == 0) {
10851855Sbostic 		*bnp = ip->i_db[bn];
10951855Sbostic 		if (*bnp == 0)
11051348Sroot 			*bnp = UNASSIGNED;
11137736Smckusick 		return (0);
11237736Smckusick 	}
11351486Sbostic 
11452994Sbostic 
11552994Sbostic 	/* Get disk address out of indirect block array */
11652994Sbostic 	daddr = ip->i_ib[xap->in_off];
11752994Sbostic 
11851855Sbostic 	/* Fetch through the indirect blocks. */
11952994Sbostic 	fs = ip->i_lfs;
12051183Sbostic 	devvp = VFSTOUFS(vp->v_mount)->um_devvp;
12152994Sbostic 
12252994Sbostic 	for (bp = NULL, ++xap; daddr && --num; ++xap) {
12352994Sbostic 		/* If looking for a meta-block, break out when we find it. */
12451855Sbostic 		metalbn = xap->in_lbn;
12551855Sbostic 		if (metalbn == bn)
12651847Sbostic 			break;
12751847Sbostic 
12851846Sbostic 		/*
12951846Sbostic 		 * Read in the appropriate indirect block.  LFS can't do a
13051846Sbostic 		 * bread because bread knows that FFS will hand it the device
13151846Sbostic 		 * vnode, not the file vnode, so the b_dev and b_blkno would
13251846Sbostic 		 * be wrong.
13351846Sbostic 		 *
13451846Sbostic 		 * XXX
13551846Sbostic 		 * This REALLY needs to be fixed, at the very least it needs
13651855Sbostic 		 * to be rethought when the buffer cache goes away.  When it's
13751855Sbostic 		 * fixed, change lfs_bmaparray and lfs_getlbns to take an ip,
13851855Sbostic 		 * not a vp.
13951846Sbostic 		 */
14051183Sbostic 		if (bp)
14137736Smckusick 			brelse(bp);
14251847Sbostic 		bp = getblk(vp, metalbn, fs->lfs_bsize);
14351183Sbostic 		if (bp->b_flags & (B_DONE | B_DELWRI)) {
14451847Sbostic 			trace(TR_BREADHIT, pack(vp, size), metalbn);
14551183Sbostic 		} else {
14651847Sbostic 			trace(TR_BREADMISS, pack(vp, size), metalbn);
14751847Sbostic 			bp->b_blkno = daddr;
14851846Sbostic 			bp->b_flags |= B_READ;
14951183Sbostic 			bp->b_dev = devvp->v_rdev;
15053574Sheideman 			/*
15153574Sheideman 			 * Call a strategy VOP by hand.
15253574Sheideman 			 */
15353574Sheideman 			vop_strategy_a.a_desc = VDESC(vop_strategy);
15453574Sheideman 			vop_strategy_a.a_bp=bp;
15553574Sheideman 			VOCALL(devvp->v_op, VOFFSET(vop_strategy), \
15653574Sheideman 			       &vop_strategy_a);
15751183Sbostic 			curproc->p_stats->p_ru.ru_inblock++;	/* XXX */
15851183Sbostic 			if (error = biowait(bp)) {
15951183Sbostic 				brelse(bp);
16051183Sbostic 				return (error);
16151183Sbostic 			}
16237736Smckusick 		}
16352994Sbostic 		daddr = bp->b_un.b_daddr[xap->in_off];
16451183Sbostic 	}
16551183Sbostic 	if (bp)
16639679Smckusick 		brelse(bp);
16751183Sbostic 
16852994Sbostic 	*bnp = daddr == 0 ? UNASSIGNED : daddr;
16937736Smckusick 	return (0);
17037736Smckusick }
17151855Sbostic 
17251855Sbostic /*
17351855Sbostic  * Create an array of logical block number/offset pairs which represent the
17451855Sbostic  * path of indirect blocks required to access a data block.  The first "pair"
17551855Sbostic  * contains the logical block number of the appropriate single, double or
17651855Sbostic  * triple indirect block and the offset into the inode indirect block array.
17751855Sbostic  * Note, the logical block number of the inode single/double/triple indirect
17851855Sbostic  * block appears twice in the array, once with the offset into the i_ib and
17951855Sbostic  * once with the offset into the page itself.
18051855Sbostic  */
18151855Sbostic int
18251855Sbostic lfs_getlbns(vp, bn, ap, nump)
18351855Sbostic 	struct vnode *vp;
18451855Sbostic 	register daddr_t bn;
18551855Sbostic 	INDIR *ap;
18651855Sbostic 	int *nump;
18751855Sbostic {
18851855Sbostic 	struct lfs *fs;
18951855Sbostic 	long metalbn, realbn;
190*54687Sbostic 	int j, numlevels, off, sh;
19151855Sbostic 
19251855Sbostic #ifdef VERBOSE
19351855Sbostic 	printf("lfs_getlbns: bn %d, inode %d\n", bn, VTOI(vp)->i_number);
19451855Sbostic #endif
195*54687Sbostic 	if (nump)
196*54687Sbostic 		*nump = 0;
197*54687Sbostic 	numlevels = 0;
19851855Sbostic 	realbn = bn;
19951855Sbostic 	if ((long)bn < 0)
20051855Sbostic 		bn = -(long)bn;
20151855Sbostic 
20251855Sbostic 	/* The first NDADDR blocks are direct blocks. */
20351855Sbostic 	if (bn < NDADDR)
20452994Sbostic 		return (0);
20551855Sbostic 
20651855Sbostic 	/*
20751855Sbostic 	 * Determine the number of levels of indirection.  After this loop
20851855Sbostic 	 * is done, sh indicates the number of data blocks possible at the
20951855Sbostic 	 * given level of indirection, and NIADDR - j is the number of levels
21051855Sbostic 	 * of indirection needed to locate the requested block.
21151855Sbostic 	 */
21251855Sbostic 	bn -= NDADDR;
21351855Sbostic 	fs = VTOI(vp)->i_lfs;
21451855Sbostic 	sh = 1;
21551855Sbostic 	for (j = NIADDR; j > 0; j--) {
21651855Sbostic 		sh *= NINDIR(fs);
21751855Sbostic 		if (bn < sh)
21851855Sbostic 			break;
21951855Sbostic 		bn -= sh;
22051855Sbostic 	}
22151855Sbostic 	if (j == 0)
22251855Sbostic 		return (EFBIG);
22351855Sbostic 
22451855Sbostic 	/* Calculate the address of the first meta-block. */
22551855Sbostic 	if (realbn >= 0)
22651855Sbostic 		metalbn = -(realbn - bn + NIADDR - j);
22751855Sbostic 	else
22851855Sbostic 		metalbn = -(-realbn - bn + NIADDR - j);
22951855Sbostic 
23051855Sbostic 	/*
23151855Sbostic 	 * At each iteration, off is the offset into the bap array which is
23251855Sbostic 	 * an array of disk addresses at the current level of indirection.
23351855Sbostic 	 * The logical block number and the offset in that block are stored
23451855Sbostic 	 * into the argument array.
23551855Sbostic 	 */
236*54687Sbostic 	++numlevels;
23751855Sbostic 	ap->in_lbn = metalbn;
23851855Sbostic 	ap->in_off = off = NIADDR - j;
23951855Sbostic 	ap++;
24051855Sbostic 	for (; j <= NIADDR; j++) {
24151855Sbostic 		/* If searching for a meta-data block, quit when found. */
24251855Sbostic 		if (metalbn == realbn)
24351855Sbostic 			break;
24451855Sbostic 
24551855Sbostic 		sh /= NINDIR(fs);
24651855Sbostic 		off = (bn / sh) % NINDIR(fs);
24751855Sbostic 
248*54687Sbostic 		++numlevels;
24951855Sbostic 		ap->in_lbn = metalbn;
25051855Sbostic 		ap->in_off = off;
25151855Sbostic 		++ap;
25251855Sbostic 
25351855Sbostic 		metalbn -= -1 + off * sh;
25451855Sbostic 	}
255*54687Sbostic 	if (nump)
256*54687Sbostic 		*nump = numlevels;
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 {
26752082Sbostic 	struct buf *bp;
26852082Sbostic 	struct inode *ip;
26952082Sbostic 	struct lfs *fs;
27052082Sbostic 	daddr_t daddr;
27152082Sbostic 	int error, newblock;
27252082Sbostic 
27352082Sbostic 	ip = VTOI(vp);
27452082Sbostic 	fs = ip->i_lfs;
27552082Sbostic 
27652082Sbostic 	/*
27752082Sbostic 	 * Three cases: it's a block beyond the end of file, it's a block in
27852082Sbostic 	 * the file that may or may not have been assigned a disk address or
27952082Sbostic 	 * we're writing an entire block.  Note, if the daddr is unassigned,
28052082Sbostic 	 * the block might still have existed in the cache.  If it did, make
28152082Sbostic 	 * sure we don't count it as a new block or zero out its contents.
28252082Sbostic 	 */
28352082Sbostic 	newblock = ip->i_size <= lbn << fs->lfs_bshift;
28453528Sheideman 	if (!newblock && (error = VOP_BMAP(vp, lbn, NULL, &daddr)))
28552994Sbostic 		return (error);
28652082Sbostic 
28752172Sbostic 	if (newblock || daddr == UNASSIGNED || iosize == fs->lfs_bsize) {
28852082Sbostic 		*bpp = bp = getblk(vp, lbn, fs->lfs_bsize);
28952082Sbostic 		if (newblock ||
29052172Sbostic 		    daddr == UNASSIGNED && !(bp->b_flags & B_CACHE)) {
29152082Sbostic 			++ip->i_blocks;
29252082Sbostic 			if (iosize != fs->lfs_bsize)
29352082Sbostic 				clrbuf(bp);
29452082Sbostic 		}
29552994Sbostic 		return (0);
29652082Sbostic 	}
29752994Sbostic 	return (bread(vp, lbn, fs->lfs_bsize, NOCRED, bpp));
29852082Sbostic 
29952082Sbostic }
300