xref: /csrg-svn/sys/ufs/lfs/lfs_balloc.c (revision 52082)
123396Smckusick /*
251494Sbostic  * Copyright (c) 1989, 1991 Regents of the University of California.
337736Smckusick  * All rights reserved.
423396Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*52082Sbostic  *	@(#)lfs_balloc.c	7.25 (Berkeley) 12/31/91
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
3451855Sbostic lfs_bmap(vp, bn, vpp, bnp)
3551855Sbostic 	struct vnode *vp;
3651855Sbostic 	register daddr_t bn;
3751855Sbostic 	struct vnode **vpp;
3851855Sbostic 	daddr_t *bnp;
3951855Sbostic {
4051855Sbostic #ifdef VERBOSE
4151855Sbostic 	printf("lfs_bmap\n");
4251855Sbostic #endif
4351855Sbostic 	/*
4451855Sbostic 	 * Check for underlying vnode requests and ensure that logical
4551855Sbostic 	 * to physical mapping is requested.
4651855Sbostic 	 */
4751855Sbostic 	if (vpp != NULL)
4851855Sbostic 		*vpp = VTOI(vp)->i_devvp;
4951855Sbostic 	if (bnp == NULL)
5051855Sbostic 		return (0);
5151855Sbostic 
5251855Sbostic 	return (lfs_bmaparray(vp, bn, bnp, NULL, NULL));
5351855Sbostic }
5451855Sbostic 
5551855Sbostic /*
5651846Sbostic  * LFS has a different version of bmap from FFS because of a naming conflict.
5751846Sbostic  * In FFS, meta blocks are given real disk addresses at allocation time, and
5851846Sbostic  * are linked into the device vnode, using a logical block number which is
5951846Sbostic  * the same as the physical block number.  This can't be done by LFS because
6051846Sbostic  * blocks aren't given disk addresses until they're written, so there's no
6151846Sbostic  * way to distinguish the meta-data blocks for one file from any other file.
6251846Sbostic  * This means that meta-data blocks have to be on the vnode for the file so
6351846Sbostic  * they can be found, and have to have "names" different from the standard
6451846Sbostic  * data blocks.  To do this, we divide the name space into positive and
6551846Sbostic  * negative block numbers, and give the meta-data blocks negative logical
6651847Sbostic  * numbers.  Indirect blocks are addressed by the negative address of the
6751847Sbostic  * first data block to which they point.  Double indirect blocks are addressed
6851847Sbostic  * by one less than the address of the first indirect block to which they
6951847Sbostic  * point.  Triple indirect blocks are addressed by one less than the address
7051847Sbostic  * of the first double indirect block to which they point.
717443Sroot  */
7251348Sroot int
7351855Sbostic lfs_bmaparray(vp, bn, bnp, ap, nump)
7451561Smckusick 	struct vnode *vp;
7537736Smckusick 	register daddr_t bn;
7651561Smckusick 	daddr_t *bnp;
7751855Sbostic 	INDIR *ap;
7851855Sbostic 	int *nump;
797443Sroot {
8051561Smckusick 	register struct inode *ip;
8151846Sbostic 	struct buf *bp;
8251855Sbostic 	struct lfs *fs;
8351561Smckusick 	struct vnode *devvp;
8451855Sbostic 	INDIR a[NIADDR], *xap;
8551855Sbostic 	daddr_t *bap, daddr;
8651855Sbostic 	long metalbn;
8751855Sbostic 	int error, num, off;
8837736Smckusick 
8951855Sbostic 
9051561Smckusick 	ip = VTOI(vp);
9151846Sbostic #ifdef VERBOSE
9251855Sbostic 	printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
9351846Sbostic #endif
9451855Sbostic #ifdef DIAGNOSTIC
9551855Sbostic 	if (ap != NULL && nump == NULL || ap == NULL && nump != NULL)
9651855Sbostic 		panic("lfs_bmaparray: invalid arguments");
9751855Sbostic #endif
9851847Sbostic 
9951855Sbostic 	xap = ap == NULL ? a : ap;
10051855Sbostic 	if (error = lfs_getlbns(vp, bn, xap, nump))
10151855Sbostic 		return (error);
10251855Sbostic 
10351855Sbostic 	num = *nump;
10451855Sbostic 	fs = ip->i_lfs;
10551855Sbostic 	if (num == 0) {
10651855Sbostic 		*bnp = ip->i_db[bn];
10751855Sbostic 		if (*bnp == 0)
10851348Sroot 			*bnp = UNASSIGNED;
10937736Smckusick 		return (0);
11037736Smckusick 	}
11151486Sbostic 
11251855Sbostic 	/* Fetch through the indirect blocks. */
11351846Sbostic 	bp = NULL;
11451183Sbostic 	devvp = VFSTOUFS(vp->v_mount)->um_devvp;
11551855Sbostic 	for (bap = ip->i_ib; num--; off = xap->in_off, ++xap) {
11651855Sbostic 		off = xap->in_off;
11751855Sbostic 		metalbn = xap->in_lbn;
11851855Sbostic 
11951846Sbostic 		/*
12051846Sbostic 		 * In LFS, it's possible to have a block appended to a file
12151846Sbostic 		 * for which the meta-blocks have not yet been allocated.
12251846Sbostic 		 * This is a win if the file never gets written or if the
12351846Sbostic 		 * file's growing.
12451846Sbostic 		 */
12551846Sbostic 		if ((daddr = bap[off]) == 0) {
12651348Sroot 			daddr = UNASSIGNED;
12751183Sbostic 			break;
12851183Sbostic 		}
12951847Sbostic 
13051847Sbostic 		/* If searching for a meta-data block, quit when found. */
13151855Sbostic 		if (metalbn == bn)
13251847Sbostic 			break;
13351847Sbostic 
13451846Sbostic 		/*
13551846Sbostic 		 * Read in the appropriate indirect block.  LFS can't do a
13651846Sbostic 		 * bread because bread knows that FFS will hand it the device
13751846Sbostic 		 * vnode, not the file vnode, so the b_dev and b_blkno would
13851846Sbostic 		 * be wrong.
13951846Sbostic 		 *
14051846Sbostic 		 * XXX
14151846Sbostic 		 * This REALLY needs to be fixed, at the very least it needs
14251855Sbostic 		 * to be rethought when the buffer cache goes away.  When it's
14351855Sbostic 		 * fixed, change lfs_bmaparray and lfs_getlbns to take an ip,
14451855Sbostic 		 * not a vp.
14551846Sbostic 		 */
14651183Sbostic 		if (bp)
14737736Smckusick 			brelse(bp);
14851847Sbostic 		bp = getblk(vp, metalbn, fs->lfs_bsize);
14951183Sbostic 		if (bp->b_flags & (B_DONE | B_DELWRI)) {
15051847Sbostic 			trace(TR_BREADHIT, pack(vp, size), metalbn);
15151183Sbostic 		} else {
15251847Sbostic 			trace(TR_BREADMISS, pack(vp, size), metalbn);
15351847Sbostic 			bp->b_blkno = daddr;
15451846Sbostic 			bp->b_flags |= B_READ;
15551183Sbostic 			bp->b_dev = devvp->v_rdev;
15651215Sbostic 			(devvp->v_op->vop_strategy)(bp);
15751183Sbostic 			curproc->p_stats->p_ru.ru_inblock++;	/* XXX */
15851183Sbostic 			if (error = biowait(bp)) {
15951183Sbostic 				brelse(bp);
16051183Sbostic 				return (error);
16151183Sbostic 			}
16237736Smckusick 		}
16337736Smckusick 		bap = bp->b_un.b_daddr;
16451183Sbostic 	}
16551183Sbostic 	if (bp)
16639679Smckusick 		brelse(bp);
16751183Sbostic 
16851183Sbostic 	*bnp = 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;
19051855Sbostic 	int j, off, sh;
19151855Sbostic 
19251855Sbostic #ifdef VERBOSE
19351855Sbostic 	printf("lfs_getlbns: bn %d, inode %d\n", bn, VTOI(vp)->i_number);
19451855Sbostic #endif
19551855Sbostic 	*nump = 0;
19651855Sbostic 	realbn = bn;
19751855Sbostic 	if ((long)bn < 0)
19851855Sbostic 		bn = -(long)bn;
19951855Sbostic 
20051855Sbostic 	/* The first NDADDR blocks are direct blocks. */
20151855Sbostic 	if (bn < NDADDR)
20251855Sbostic 		return(0);
20351855Sbostic 
20451855Sbostic 	/*
20551855Sbostic 	 * Determine the number of levels of indirection.  After this loop
20651855Sbostic 	 * is done, sh indicates the number of data blocks possible at the
20751855Sbostic 	 * given level of indirection, and NIADDR - j is the number of levels
20851855Sbostic 	 * of indirection needed to locate the requested block.
20951855Sbostic 	 */
21051855Sbostic 	bn -= NDADDR;
21151855Sbostic 	fs = VTOI(vp)->i_lfs;
21251855Sbostic 	sh = 1;
21351855Sbostic 	for (j = NIADDR; j > 0; j--) {
21451855Sbostic 		sh *= NINDIR(fs);
21551855Sbostic 		if (bn < sh)
21651855Sbostic 			break;
21751855Sbostic 		bn -= sh;
21851855Sbostic 	}
21951855Sbostic 	if (j == 0)
22051855Sbostic 		return (EFBIG);
22151855Sbostic 
22251855Sbostic 	/* Calculate the address of the first meta-block. */
22351855Sbostic 	if (realbn >= 0)
22451855Sbostic 		metalbn = -(realbn - bn + NIADDR - j);
22551855Sbostic 	else
22651855Sbostic 		metalbn = -(-realbn - bn + NIADDR - j);
22751855Sbostic 
22851855Sbostic 	/*
22951855Sbostic 	 * At each iteration, off is the offset into the bap array which is
23051855Sbostic 	 * an array of disk addresses at the current level of indirection.
23151855Sbostic 	 * The logical block number and the offset in that block are stored
23251855Sbostic 	 * into the argument array.
23351855Sbostic 	 */
23451855Sbostic 	++*nump;
23551855Sbostic 	ap->in_lbn = metalbn;
23651855Sbostic 	ap->in_off = off = NIADDR - j;
23751855Sbostic 	ap++;
23851855Sbostic 	for (; j <= NIADDR; j++) {
23951855Sbostic 		/* If searching for a meta-data block, quit when found. */
24051855Sbostic 		if (metalbn == realbn)
24151855Sbostic 			break;
24251855Sbostic 
24351855Sbostic 		sh /= NINDIR(fs);
24451855Sbostic 		off = (bn / sh) % NINDIR(fs);
24551855Sbostic 
24651855Sbostic 		++*nump;
24751855Sbostic 		ap->in_lbn = metalbn;
24851855Sbostic 		ap->in_off = off;
24951855Sbostic 		++ap;
25051855Sbostic 
25151855Sbostic 		metalbn -= -1 + off * sh;
25251855Sbostic 	}
25351855Sbostic 	return (0);
25451855Sbostic }
255*52082Sbostic 
256*52082Sbostic int
257*52082Sbostic lfs_balloc(vp, iosize, lbn, bpp)
258*52082Sbostic 	struct vnode *vp;
259*52082Sbostic 	u_long iosize;
260*52082Sbostic 	daddr_t lbn;
261*52082Sbostic 	struct buf **bpp;
262*52082Sbostic {
263*52082Sbostic 	struct buf *bp;
264*52082Sbostic 	struct inode *ip;
265*52082Sbostic 	struct lfs *fs;
266*52082Sbostic 	daddr_t daddr;
267*52082Sbostic 	int error, newblock;
268*52082Sbostic 
269*52082Sbostic 	ip = VTOI(vp);
270*52082Sbostic 	fs = ip->i_lfs;
271*52082Sbostic 
272*52082Sbostic 	/*
273*52082Sbostic 	 * Three cases: it's a block beyond the end of file, it's a block in
274*52082Sbostic 	 * the file that may or may not have been assigned a disk address or
275*52082Sbostic 	 * we're writing an entire block.  Note, if the daddr is unassigned,
276*52082Sbostic 	 * the block might still have existed in the cache.  If it did, make
277*52082Sbostic 	 * sure we don't count it as a new block or zero out its contents.
278*52082Sbostic 	 */
279*52082Sbostic 	newblock = ip->i_size <= lbn << fs->lfs_bshift;
280*52082Sbostic 	if (!newblock && (error = lfs_bmap(vp, lbn, NULL, &daddr)))
281*52082Sbostic 		return(error);
282*52082Sbostic 
283*52082Sbostic 	if (newblock || daddr == LFS_UNUSED_DADDR || iosize == fs->lfs_bsize) {
284*52082Sbostic 		*bpp = bp = getblk(vp, lbn, fs->lfs_bsize);
285*52082Sbostic 		if (newblock ||
286*52082Sbostic 		    daddr == LFS_UNUSED_DADDR && !(bp->b_flags & B_CACHE)) {
287*52082Sbostic 			++ip->i_blocks;
288*52082Sbostic 			if (iosize != fs->lfs_bsize)
289*52082Sbostic 				clrbuf(bp);
290*52082Sbostic 		}
291*52082Sbostic 		return(0);
292*52082Sbostic 	}
293*52082Sbostic 	return(bread(vp, lbn, fs->lfs_bsize, NOCRED, bpp));
294*52082Sbostic 
295*52082Sbostic }
296