xref: /csrg-svn/sys/ufs/lfs/lfs_balloc.c (revision 53528)
123396Smckusick /*
251494Sbostic  * Copyright (c) 1989, 1991 Regents of the University of California.
337736Smckusick  * All rights reserved.
423396Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*53528Sheideman  *	@(#)lfs_balloc.c	7.28 (Berkeley) 05/14/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*53528Sheideman lfs_bmap (ap)
35*53528Sheideman 	struct vop_bmap_args *ap;
36*53528Sheideman #define vp (ap->a_vp)
37*53528Sheideman #define bn (ap->a_bn)
38*53528Sheideman #define vpp (ap->a_vpp)
39*53528Sheideman #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 }
55*53528Sheideman #undef vp
56*53528Sheideman #undef bn
57*53528Sheideman #undef vpp
58*53528Sheideman #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 {
8551561Smckusick 	register struct inode *ip;
8651846Sbostic 	struct buf *bp;
8751855Sbostic 	struct lfs *fs;
8851561Smckusick 	struct vnode *devvp;
8951855Sbostic 	INDIR a[NIADDR], *xap;
9051855Sbostic 	daddr_t *bap, daddr;
9151855Sbostic 	long metalbn;
9251855Sbostic 	int error, num, off;
9337736Smckusick 
9451855Sbostic 
9551561Smckusick 	ip = VTOI(vp);
9651846Sbostic #ifdef VERBOSE
9751855Sbostic 	printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
9851846Sbostic #endif
9951855Sbostic #ifdef DIAGNOSTIC
10051855Sbostic 	if (ap != NULL && nump == NULL || ap == NULL && nump != NULL)
10151855Sbostic 		panic("lfs_bmaparray: invalid arguments");
10251855Sbostic #endif
10351847Sbostic 
10451855Sbostic 	xap = ap == NULL ? a : ap;
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;
15351215Sbostic 			(devvp->v_op->vop_strategy)(bp);
15451183Sbostic 			curproc->p_stats->p_ru.ru_inblock++;	/* XXX */
15551183Sbostic 			if (error = biowait(bp)) {
15651183Sbostic 				brelse(bp);
15751183Sbostic 				return (error);
15851183Sbostic 			}
15937736Smckusick 		}
16052994Sbostic 		daddr = bp->b_un.b_daddr[xap->in_off];
16151183Sbostic 	}
16251183Sbostic 	if (bp)
16339679Smckusick 		brelse(bp);
16451183Sbostic 
16552994Sbostic 	*bnp = daddr == 0 ? UNASSIGNED : daddr;
16637736Smckusick 	return (0);
16737736Smckusick }
16851855Sbostic 
16951855Sbostic /*
17051855Sbostic  * Create an array of logical block number/offset pairs which represent the
17151855Sbostic  * path of indirect blocks required to access a data block.  The first "pair"
17251855Sbostic  * contains the logical block number of the appropriate single, double or
17351855Sbostic  * triple indirect block and the offset into the inode indirect block array.
17451855Sbostic  * Note, the logical block number of the inode single/double/triple indirect
17551855Sbostic  * block appears twice in the array, once with the offset into the i_ib and
17651855Sbostic  * once with the offset into the page itself.
17751855Sbostic  */
17851855Sbostic int
17951855Sbostic lfs_getlbns(vp, bn, ap, nump)
18051855Sbostic 	struct vnode *vp;
18151855Sbostic 	register daddr_t bn;
18251855Sbostic 	INDIR *ap;
18351855Sbostic 	int *nump;
18451855Sbostic {
18551855Sbostic 	struct lfs *fs;
18651855Sbostic 	long metalbn, realbn;
18751855Sbostic 	int j, off, sh;
18851855Sbostic 
18951855Sbostic #ifdef VERBOSE
19051855Sbostic 	printf("lfs_getlbns: bn %d, inode %d\n", bn, VTOI(vp)->i_number);
19151855Sbostic #endif
19251855Sbostic 	*nump = 0;
19351855Sbostic 	realbn = bn;
19451855Sbostic 	if ((long)bn < 0)
19551855Sbostic 		bn = -(long)bn;
19651855Sbostic 
19751855Sbostic 	/* The first NDADDR blocks are direct blocks. */
19851855Sbostic 	if (bn < NDADDR)
19952994Sbostic 		return (0);
20051855Sbostic 
20151855Sbostic 	/*
20251855Sbostic 	 * Determine the number of levels of indirection.  After this loop
20351855Sbostic 	 * is done, sh indicates the number of data blocks possible at the
20451855Sbostic 	 * given level of indirection, and NIADDR - j is the number of levels
20551855Sbostic 	 * of indirection needed to locate the requested block.
20651855Sbostic 	 */
20751855Sbostic 	bn -= NDADDR;
20851855Sbostic 	fs = VTOI(vp)->i_lfs;
20951855Sbostic 	sh = 1;
21051855Sbostic 	for (j = NIADDR; j > 0; j--) {
21151855Sbostic 		sh *= NINDIR(fs);
21251855Sbostic 		if (bn < sh)
21351855Sbostic 			break;
21451855Sbostic 		bn -= sh;
21551855Sbostic 	}
21651855Sbostic 	if (j == 0)
21751855Sbostic 		return (EFBIG);
21851855Sbostic 
21951855Sbostic 	/* Calculate the address of the first meta-block. */
22051855Sbostic 	if (realbn >= 0)
22151855Sbostic 		metalbn = -(realbn - bn + NIADDR - j);
22251855Sbostic 	else
22351855Sbostic 		metalbn = -(-realbn - bn + NIADDR - j);
22451855Sbostic 
22551855Sbostic 	/*
22651855Sbostic 	 * At each iteration, off is the offset into the bap array which is
22751855Sbostic 	 * an array of disk addresses at the current level of indirection.
22851855Sbostic 	 * The logical block number and the offset in that block are stored
22951855Sbostic 	 * into the argument array.
23051855Sbostic 	 */
23151855Sbostic 	++*nump;
23251855Sbostic 	ap->in_lbn = metalbn;
23351855Sbostic 	ap->in_off = off = NIADDR - j;
23451855Sbostic 	ap++;
23551855Sbostic 	for (; j <= NIADDR; j++) {
23651855Sbostic 		/* If searching for a meta-data block, quit when found. */
23751855Sbostic 		if (metalbn == realbn)
23851855Sbostic 			break;
23951855Sbostic 
24051855Sbostic 		sh /= NINDIR(fs);
24151855Sbostic 		off = (bn / sh) % NINDIR(fs);
24251855Sbostic 
24351855Sbostic 		++*nump;
24451855Sbostic 		ap->in_lbn = metalbn;
24551855Sbostic 		ap->in_off = off;
24651855Sbostic 		++ap;
24751855Sbostic 
24851855Sbostic 		metalbn -= -1 + off * sh;
24951855Sbostic 	}
25051855Sbostic 	return (0);
25151855Sbostic }
25252082Sbostic 
25352082Sbostic int
25452082Sbostic lfs_balloc(vp, iosize, lbn, bpp)
25552082Sbostic 	struct vnode *vp;
25652082Sbostic 	u_long iosize;
25752082Sbostic 	daddr_t lbn;
25852082Sbostic 	struct buf **bpp;
25952082Sbostic {
260*53528Sheideman 	USES_VOP_BMAP;
26152082Sbostic 	struct buf *bp;
26252082Sbostic 	struct inode *ip;
26352082Sbostic 	struct lfs *fs;
26452082Sbostic 	daddr_t daddr;
26552082Sbostic 	int error, newblock;
26652082Sbostic 
26752082Sbostic 	ip = VTOI(vp);
26852082Sbostic 	fs = ip->i_lfs;
26952082Sbostic 
27052082Sbostic 	/*
27152082Sbostic 	 * Three cases: it's a block beyond the end of file, it's a block in
27252082Sbostic 	 * the file that may or may not have been assigned a disk address or
27352082Sbostic 	 * we're writing an entire block.  Note, if the daddr is unassigned,
27452082Sbostic 	 * the block might still have existed in the cache.  If it did, make
27552082Sbostic 	 * sure we don't count it as a new block or zero out its contents.
27652082Sbostic 	 */
27752082Sbostic 	newblock = ip->i_size <= lbn << fs->lfs_bshift;
278*53528Sheideman 	if (!newblock && (error = VOP_BMAP(vp, lbn, NULL, &daddr)))
27952994Sbostic 		return (error);
28052082Sbostic 
28152172Sbostic 	if (newblock || daddr == UNASSIGNED || iosize == fs->lfs_bsize) {
28252082Sbostic 		*bpp = bp = getblk(vp, lbn, fs->lfs_bsize);
28352082Sbostic 		if (newblock ||
28452172Sbostic 		    daddr == UNASSIGNED && !(bp->b_flags & B_CACHE)) {
28552082Sbostic 			++ip->i_blocks;
28652082Sbostic 			if (iosize != fs->lfs_bsize)
28752082Sbostic 				clrbuf(bp);
28852082Sbostic 		}
28952994Sbostic 		return (0);
29052082Sbostic 	}
29152994Sbostic 	return (bread(vp, lbn, fs->lfs_bsize, NOCRED, bpp));
29252082Sbostic 
29352082Sbostic }
294