xref: /csrg-svn/sys/ufs/lfs/lfs_balloc.c (revision 51348)
123396Smckusick /*
237736Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
337736Smckusick  * All rights reserved.
423396Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*51348Sroot  *	@(#)lfs_balloc.c	7.17 (Berkeley) 10/09/91
823396Smckusick  */
97443Sroot 
1051215Sbostic #ifdef LOGFS
1117099Sbloom #include "param.h"
1217099Sbloom #include "systm.h"
1317099Sbloom #include "buf.h"
1451183Sbostic #include "time.h"
1551183Sbostic #include "resource.h"
1651183Sbostic #include "resourcevar.h"
1717099Sbloom #include "proc.h"
1837736Smckusick #include "file.h"
1937736Smckusick #include "vnode.h"
2051183Sbostic #include "mount.h"
2151183Sbostic #include "specdev.h"
227443Sroot 
2351155Sbostic #include "../ufs/quota.h"
2451155Sbostic #include "../ufs/inode.h"
2551183Sbostic #include "../ufs/ufsmount.h"
2651183Sbostic #include "trace.h"
2751155Sbostic #include "lfs.h"
2851155Sbostic #include "lfs_extern.h"
2947571Skarels 
307443Sroot /*
3149450Smckusick  * Bmap converts a the logical block number of a file
3249450Smckusick  * to its physical block number on the disk. The conversion
3349450Smckusick  * is done by using the logical block number to index into
3449450Smckusick  * the array of block pointers described by the dinode.
357443Sroot  */
36*51348Sroot int
3751155Sbostic lfs_bmap(ip, bn, bnp)
387443Sroot 	register struct inode *ip;
3937736Smckusick 	register daddr_t bn;
4037736Smckusick 	daddr_t	*bnp;
417443Sroot {
4251155Sbostic 	register LFS *fs;					/* LFS */
4337736Smckusick 	register daddr_t nb;
4451183Sbostic 	struct vnode *devvp, *vp;
4537736Smckusick 	struct buf *bp;
4651183Sbostic 	daddr_t *bap, daddr;
4751183Sbostic 	daddr_t lbn_ind;
48*51348Sroot 	int j, off, sh;
4937736Smckusick 	int error;
5037736Smckusick 
5151155Sbostic printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
5251155Sbostic 	fs = ip->i_lfs;						/* LFS */
5337736Smckusick 
5437736Smckusick 	/*
5551183Sbostic 	 * We access all blocks in the cache, even indirect blocks by means of
5651183Sbostic 	 * a logical address. Indirect blocks (single, double, triple) all have
5751183Sbostic 	 * negative block numbers. The first NDADDR blocks are direct blocks,
5851183Sbostic 	 * the first NIADDR negative blocks are the indirect block pointers.
5951183Sbostic 	 * The single, double and triple indirect blocks in the inode
6051183Sbostic 	 * are addressed: -1, -2 and -3 respectively.
6151183Sbostic 	 * XXX we don't handle triple indirect at all.
6251183Sbostic 	 */
6351183Sbostic 	if (bn < 0) {
6451183Sbostic 		/* Shouldn't be here -- we don't think */
6551183Sbostic 		printf("lfs_bmap: NEGATIVE indirect block number %d\n", bn);
6651183Sbostic 		panic("negative indirect block number");
6751183Sbostic 	}
6851183Sbostic 
6951183Sbostic 	/*
7037736Smckusick 	 * The first NDADDR blocks are direct blocks
7137736Smckusick 	 */
7237736Smckusick 	if (bn < NDADDR) {
7337736Smckusick 		nb = ip->i_db[bn];
7437736Smckusick 		if (nb == 0) {
75*51348Sroot 			*bnp = UNASSIGNED;
7637736Smckusick 			return (0);
7737736Smckusick 		}
7851155Sbostic 		*bnp = nb;
7937736Smckusick 		return (0);
8037736Smckusick 	}
8137736Smckusick 	/*
8239679Smckusick 	 * Determine the number of levels of indirection.
8337736Smckusick 	 */
8437736Smckusick 	sh = 1;
8537736Smckusick 	bn -= NDADDR;
8651183Sbostic 	lbn_ind = 0;
8737736Smckusick 	for (j = NIADDR; j > 0; j--) {
8851183Sbostic 		lbn_ind--;
8937736Smckusick 		sh *= NINDIR(fs);
9037736Smckusick 		if (bn < sh)
9137736Smckusick 			break;
9237736Smckusick 		bn -= sh;
9337736Smckusick 	}
9437736Smckusick 	if (j == 0)
9537736Smckusick 		return (EFBIG);
9637736Smckusick 	/*
9739679Smckusick 	 * Fetch through the indirect blocks.
9837736Smckusick 	 */
9951183Sbostic 
10051183Sbostic 	vp = ITOV(ip);
10151183Sbostic 	devvp = VFSTOUFS(vp->v_mount)->um_devvp;
10251183Sbostic 	for (off = NIADDR - j, bap = ip->i_ib; j <= NIADDR; j++) {
10351183Sbostic 		if((daddr = bap[off]) == 0) {
104*51348Sroot 			daddr = UNASSIGNED;
10551183Sbostic 			break;
10651183Sbostic 		}
10751183Sbostic 		if (bp)
10837736Smckusick 			brelse(bp);
10951183Sbostic 		bp = getblk(vp, lbn_ind, fs->lfs_bsize);
11051183Sbostic 		if (bp->b_flags & (B_DONE | B_DELWRI)) {
11151183Sbostic 			trace(TR_BREADHIT, pack(vp, size), lbn_ind);
11251183Sbostic 		} else {
11351183Sbostic 			trace(TR_BREADMISS, pack(vp, size), lbn_ind);
11451183Sbostic 			bp->b_blkno = daddr;
11551183Sbostic 			bp->b_flags |= B_READ;
11651183Sbostic 			bp->b_dev = devvp->v_rdev;
11751215Sbostic 			(devvp->v_op->vop_strategy)(bp);
11851183Sbostic 			curproc->p_stats->p_ru.ru_inblock++;	/* XXX */
11951183Sbostic 			if (error = biowait(bp)) {
12051183Sbostic 				brelse(bp);
12151183Sbostic 				return (error);
12251183Sbostic 			}
12337736Smckusick 		}
12437736Smckusick 		bap = bp->b_un.b_daddr;
12537736Smckusick 		sh /= NINDIR(fs);
12651183Sbostic 		off = (bn / sh) % NINDIR(fs);
12751183Sbostic 		lbn_ind  = -(NIADDR + 1 + off);
12851183Sbostic 	}
12951183Sbostic 	if (bp)
13039679Smckusick 		brelse(bp);
13151183Sbostic 
13251183Sbostic 	*bnp = daddr;
13337736Smckusick 	return (0);
13437736Smckusick }
13551215Sbostic #endif /* LOGFS */
136