xref: /csrg-svn/sys/ufs/lfs/lfs_balloc.c (revision 55594)
123396Smckusick /*
251494Sbostic  * Copyright (c) 1989, 1991 Regents of the University of California.
337736Smckusick  * All rights reserved.
423396Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*55594Sbostic  *	@(#)lfs_balloc.c	7.34 (Berkeley) 07/23/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/trace.h>
177443Sroot 
1855048Smckusick #include <miscfs/specfs/specdev.h>
1955048Smckusick 
2051494Sbostic #include <ufs/ufs/quota.h>
2151494Sbostic #include <ufs/ufs/inode.h>
2251494Sbostic #include <ufs/ufs/ufsmount.h>
2347571Skarels 
2451494Sbostic #include <ufs/lfs/lfs.h>
2551494Sbostic #include <ufs/lfs/lfs_extern.h>
2651486Sbostic 
2751962Sbostic int lfs_getlbns __P((struct vnode *, daddr_t, INDIR *, int *));
2851855Sbostic 
297443Sroot /*
3051486Sbostic  * Bmap converts a the logical block number of a file to its physical block
3151486Sbostic  * number on the disk. The conversion is done by using the logical block
3251486Sbostic  * number to index into the array of block pointers described by the dinode.
3351855Sbostic  */
3451855Sbostic int
3554687Sbostic lfs_bmap(ap)
3654687Sbostic 	struct vop_bmap_args /* {
3754687Sbostic 		struct vnode *a_vp;
3854687Sbostic 		daddr_t  a_bn;
3954687Sbostic 		struct vnode **a_vpp;
4054687Sbostic 		daddr_t *a_bnp;
4154687Sbostic 	} */ *ap;
4251855Sbostic {
4351855Sbostic #ifdef VERBOSE
4451855Sbostic 	printf("lfs_bmap\n");
4551855Sbostic #endif
4651855Sbostic 	/*
4751855Sbostic 	 * Check for underlying vnode requests and ensure that logical
4851855Sbostic 	 * to physical mapping is requested.
4951855Sbostic 	 */
5053591Sheideman 	if (ap->a_vpp != NULL)
5153591Sheideman 		*ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
5253591Sheideman 	if (ap->a_bnp == NULL)
5351855Sbostic 		return (0);
5451855Sbostic 
5553591Sheideman 	return (lfs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL));
5651855Sbostic }
5751855Sbostic 
5851855Sbostic /*
5951846Sbostic  * LFS has a different version of bmap from FFS because of a naming conflict.
6051846Sbostic  * In FFS, meta blocks are given real disk addresses at allocation time, and
6151846Sbostic  * are linked into the device vnode, using a logical block number which is
6251846Sbostic  * the same as the physical block number.  This can't be done by LFS because
6351846Sbostic  * blocks aren't given disk addresses until they're written, so there's no
6451846Sbostic  * way to distinguish the meta-data blocks for one file from any other file.
6551846Sbostic  * This means that meta-data blocks have to be on the vnode for the file so
6651846Sbostic  * they can be found, and have to have "names" different from the standard
6751846Sbostic  * data blocks.  To do this, we divide the name space into positive and
6851846Sbostic  * negative block numbers, and give the meta-data blocks negative logical
6951847Sbostic  * numbers.  Indirect blocks are addressed by the negative address of the
7051847Sbostic  * first data block to which they point.  Double indirect blocks are addressed
7151847Sbostic  * by one less than the address of the first indirect block to which they
7251847Sbostic  * point.  Triple indirect blocks are addressed by one less than the address
7351847Sbostic  * of the first double indirect block to which they point.
747443Sroot  */
7551348Sroot int
7651855Sbostic lfs_bmaparray(vp, bn, bnp, ap, nump)
7751561Smckusick 	struct vnode *vp;
7837736Smckusick 	register daddr_t bn;
7951561Smckusick 	daddr_t *bnp;
8051855Sbostic 	INDIR *ap;
8151855Sbostic 	int *nump;
827443Sroot {
8351561Smckusick 	register struct inode *ip;
8451846Sbostic 	struct buf *bp;
8551855Sbostic 	struct lfs *fs;
8651561Smckusick 	struct vnode *devvp;
8751855Sbostic 	INDIR a[NIADDR], *xap;
8851855Sbostic 	daddr_t *bap, daddr;
8951855Sbostic 	long metalbn;
9051855Sbostic 	int error, num, off;
9154687Sbostic 	struct vop_strategy_args vop_strategy_a;
9237736Smckusick 
9351561Smckusick 	ip = VTOI(vp);
9451846Sbostic #ifdef VERBOSE
9551855Sbostic 	printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
9651846Sbostic #endif
9751855Sbostic #ifdef DIAGNOSTIC
9851855Sbostic 	if (ap != NULL && nump == NULL || ap == NULL && nump != NULL)
9951855Sbostic 		panic("lfs_bmaparray: invalid arguments");
10051855Sbostic #endif
10151847Sbostic 
10251855Sbostic 	xap = ap == NULL ? a : ap;
10355457Sbostic 	if (!nump)
10455457Sbostic 		nump = &num;
10551855Sbostic 	if (error = lfs_getlbns(vp, bn, xap, nump))
10651855Sbostic 		return (error);
10751855Sbostic 
10851855Sbostic 	num = *nump;
10952994Sbostic 
11051855Sbostic 	if (num == 0) {
11151855Sbostic 		*bnp = ip->i_db[bn];
11251855Sbostic 		if (*bnp == 0)
11351348Sroot 			*bnp = UNASSIGNED;
11437736Smckusick 		return (0);
11537736Smckusick 	}
11651486Sbostic 
11752994Sbostic 
11852994Sbostic 	/* Get disk address out of indirect block array */
11952994Sbostic 	daddr = ip->i_ib[xap->in_off];
12052994Sbostic 
12151855Sbostic 	/* Fetch through the indirect blocks. */
12252994Sbostic 	fs = ip->i_lfs;
12351183Sbostic 	devvp = VFSTOUFS(vp->v_mount)->um_devvp;
12452994Sbostic 
12552994Sbostic 	for (bp = NULL, ++xap; daddr && --num; ++xap) {
12652994Sbostic 		/* If looking for a meta-block, break out when we find it. */
12751855Sbostic 		metalbn = xap->in_lbn;
12851855Sbostic 		if (metalbn == bn)
12951847Sbostic 			break;
13051847Sbostic 
13151846Sbostic 		/*
13251846Sbostic 		 * Read in the appropriate indirect block.  LFS can't do a
13351846Sbostic 		 * bread because bread knows that FFS will hand it the device
13451846Sbostic 		 * vnode, not the file vnode, so the b_dev and b_blkno would
13551846Sbostic 		 * be wrong.
13651846Sbostic 		 *
13751846Sbostic 		 * XXX
13851846Sbostic 		 * This REALLY needs to be fixed, at the very least it needs
13951855Sbostic 		 * to be rethought when the buffer cache goes away.  When it's
14051855Sbostic 		 * fixed, change lfs_bmaparray and lfs_getlbns to take an ip,
14151855Sbostic 		 * not a vp.
14251846Sbostic 		 */
14351183Sbostic 		if (bp)
14437736Smckusick 			brelse(bp);
14551847Sbostic 		bp = getblk(vp, metalbn, fs->lfs_bsize);
14651183Sbostic 		if (bp->b_flags & (B_DONE | B_DELWRI)) {
14751847Sbostic 			trace(TR_BREADHIT, pack(vp, size), metalbn);
14851183Sbostic 		} else {
14951847Sbostic 			trace(TR_BREADMISS, pack(vp, size), metalbn);
15051847Sbostic 			bp->b_blkno = daddr;
15151846Sbostic 			bp->b_flags |= B_READ;
15251183Sbostic 			bp->b_dev = devvp->v_rdev;
15353574Sheideman 			/*
15453574Sheideman 			 * Call a strategy VOP by hand.
15553574Sheideman 			 */
15653574Sheideman 			vop_strategy_a.a_desc = VDESC(vop_strategy);
15753574Sheideman 			vop_strategy_a.a_bp=bp;
15853574Sheideman 			VOCALL(devvp->v_op, VOFFSET(vop_strategy), \
15953574Sheideman 			       &vop_strategy_a);
16051183Sbostic 			curproc->p_stats->p_ru.ru_inblock++;	/* XXX */
16151183Sbostic 			if (error = biowait(bp)) {
16251183Sbostic 				brelse(bp);
16351183Sbostic 				return (error);
16451183Sbostic 			}
16537736Smckusick 		}
16652994Sbostic 		daddr = bp->b_un.b_daddr[xap->in_off];
16751183Sbostic 	}
16851183Sbostic 	if (bp)
16939679Smckusick 		brelse(bp);
17051183Sbostic 
17152994Sbostic 	*bnp = daddr == 0 ? UNASSIGNED : daddr;
17237736Smckusick 	return (0);
17337736Smckusick }
17451855Sbostic 
17551855Sbostic /*
17651855Sbostic  * Create an array of logical block number/offset pairs which represent the
17751855Sbostic  * path of indirect blocks required to access a data block.  The first "pair"
17851855Sbostic  * contains the logical block number of the appropriate single, double or
17951855Sbostic  * triple indirect block and the offset into the inode indirect block array.
18051855Sbostic  * Note, the logical block number of the inode single/double/triple indirect
18151855Sbostic  * block appears twice in the array, once with the offset into the i_ib and
18251855Sbostic  * once with the offset into the page itself.
18351855Sbostic  */
18451855Sbostic int
18551855Sbostic lfs_getlbns(vp, bn, ap, nump)
18651855Sbostic 	struct vnode *vp;
18751855Sbostic 	register daddr_t bn;
18851855Sbostic 	INDIR *ap;
18951855Sbostic 	int *nump;
19051855Sbostic {
19151855Sbostic 	struct lfs *fs;
19251855Sbostic 	long metalbn, realbn;
19354687Sbostic 	int j, numlevels, off, sh;
19451855Sbostic 
19551855Sbostic #ifdef VERBOSE
19651855Sbostic 	printf("lfs_getlbns: bn %d, inode %d\n", bn, VTOI(vp)->i_number);
19751855Sbostic #endif
19854687Sbostic 	if (nump)
19954687Sbostic 		*nump = 0;
20054687Sbostic 	numlevels = 0;
20151855Sbostic 	realbn = bn;
20251855Sbostic 	if ((long)bn < 0)
20351855Sbostic 		bn = -(long)bn;
20451855Sbostic 
20551855Sbostic 	/* The first NDADDR blocks are direct blocks. */
20651855Sbostic 	if (bn < NDADDR)
20752994Sbostic 		return (0);
20851855Sbostic 
20951855Sbostic 	/*
21051855Sbostic 	 * Determine the number of levels of indirection.  After this loop
21151855Sbostic 	 * is done, sh indicates the number of data blocks possible at the
21251855Sbostic 	 * given level of indirection, and NIADDR - j is the number of levels
21351855Sbostic 	 * of indirection needed to locate the requested block.
21451855Sbostic 	 */
21551855Sbostic 	bn -= NDADDR;
21651855Sbostic 	fs = VTOI(vp)->i_lfs;
21751855Sbostic 	sh = 1;
21851855Sbostic 	for (j = NIADDR; j > 0; j--) {
21951855Sbostic 		sh *= NINDIR(fs);
22051855Sbostic 		if (bn < sh)
22151855Sbostic 			break;
22251855Sbostic 		bn -= sh;
22351855Sbostic 	}
22451855Sbostic 	if (j == 0)
22551855Sbostic 		return (EFBIG);
22651855Sbostic 
22751855Sbostic 	/* Calculate the address of the first meta-block. */
22851855Sbostic 	if (realbn >= 0)
22951855Sbostic 		metalbn = -(realbn - bn + NIADDR - j);
23051855Sbostic 	else
23151855Sbostic 		metalbn = -(-realbn - bn + NIADDR - j);
23251855Sbostic 
23351855Sbostic 	/*
23451855Sbostic 	 * At each iteration, off is the offset into the bap array which is
23551855Sbostic 	 * an array of disk addresses at the current level of indirection.
23651855Sbostic 	 * The logical block number and the offset in that block are stored
23751855Sbostic 	 * into the argument array.
23851855Sbostic 	 */
23954687Sbostic 	++numlevels;
24051855Sbostic 	ap->in_lbn = metalbn;
24151855Sbostic 	ap->in_off = off = NIADDR - j;
24251855Sbostic 	ap++;
24351855Sbostic 	for (; j <= NIADDR; j++) {
24451855Sbostic 		/* If searching for a meta-data block, quit when found. */
24551855Sbostic 		if (metalbn == realbn)
24651855Sbostic 			break;
24751855Sbostic 
24851855Sbostic 		sh /= NINDIR(fs);
24951855Sbostic 		off = (bn / sh) % NINDIR(fs);
25051855Sbostic 
25154687Sbostic 		++numlevels;
25251855Sbostic 		ap->in_lbn = metalbn;
25351855Sbostic 		ap->in_off = off;
25451855Sbostic 		++ap;
25551855Sbostic 
25651855Sbostic 		metalbn -= -1 + off * sh;
25751855Sbostic 	}
25854687Sbostic 	if (nump)
25954687Sbostic 		*nump = numlevels;
26051855Sbostic 	return (0);
26151855Sbostic }
26252082Sbostic 
26352082Sbostic int
26452082Sbostic lfs_balloc(vp, iosize, lbn, bpp)
26552082Sbostic 	struct vnode *vp;
26652082Sbostic 	u_long iosize;
26752082Sbostic 	daddr_t lbn;
26852082Sbostic 	struct buf **bpp;
26952082Sbostic {
27052082Sbostic 	struct buf *bp;
27152082Sbostic 	struct inode *ip;
27252082Sbostic 	struct lfs *fs;
27352082Sbostic 	daddr_t daddr;
27452082Sbostic 	int error, newblock;
27552082Sbostic 
27652082Sbostic 	ip = VTOI(vp);
27752082Sbostic 	fs = ip->i_lfs;
27852082Sbostic 
27952082Sbostic 	/*
28052082Sbostic 	 * Three cases: it's a block beyond the end of file, it's a block in
28152082Sbostic 	 * the file that may or may not have been assigned a disk address or
28252082Sbostic 	 * we're writing an entire block.  Note, if the daddr is unassigned,
28352082Sbostic 	 * the block might still have existed in the cache.  If it did, make
28452082Sbostic 	 * sure we don't count it as a new block or zero out its contents.
28552082Sbostic 	 */
28652082Sbostic 	newblock = ip->i_size <= lbn << fs->lfs_bshift;
28753528Sheideman 	if (!newblock && (error = VOP_BMAP(vp, lbn, NULL, &daddr)))
28852994Sbostic 		return (error);
28952082Sbostic 
29052172Sbostic 	if (newblock || daddr == UNASSIGNED || iosize == fs->lfs_bsize) {
29152082Sbostic 		*bpp = bp = getblk(vp, lbn, fs->lfs_bsize);
29252082Sbostic 		if (newblock ||
29352172Sbostic 		    daddr == UNASSIGNED && !(bp->b_flags & B_CACHE)) {
29455457Sbostic 			ip->i_blocks += btodb(fs->lfs_bsize);
295*55594Sbostic 			fs->lfs_bfree -= btodb(fs->lfs_bsize);
29652082Sbostic 			if (iosize != fs->lfs_bsize)
29752082Sbostic 				clrbuf(bp);
29852082Sbostic 		}
29952994Sbostic 		return (0);
30052082Sbostic 	}
30152994Sbostic 	return (bread(vp, lbn, fs->lfs_bsize, NOCRED, bpp));
30252082Sbostic 
30352082Sbostic }
304