156610Smargo /*
263376Sbostic * Copyright (c) 1989, 1991, 1993
363376Sbostic * The Regents of the University of California. All rights reserved.
4*65774Sbostic * (c) UNIX System Laboratories, Inc.
5*65774Sbostic * All or some portions of this file are derived from material licensed
6*65774Sbostic * to the University of California by American Telephone and Telegraph
7*65774Sbostic * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8*65774Sbostic * the permission of UNIX System Laboratories, Inc.
956610Smargo *
1056610Smargo * %sccs.include.redist.c%
1156610Smargo *
12*65774Sbostic * @(#)ufs_bmap.c 8.6 (Berkeley) 01/21/94
1356610Smargo */
1456610Smargo
1556610Smargo #include <sys/param.h>
1656610Smargo #include <sys/buf.h>
1756610Smargo #include <sys/proc.h>
1856610Smargo #include <sys/vnode.h>
1956610Smargo #include <sys/mount.h>
2056610Smargo #include <sys/resourcevar.h>
2156610Smargo #include <sys/trace.h>
2256610Smargo
2356610Smargo #include <miscfs/specfs/specdev.h>
2456610Smargo
2556610Smargo #include <ufs/ufs/quota.h>
2656610Smargo #include <ufs/ufs/inode.h>
2756610Smargo #include <ufs/ufs/ufsmount.h>
2856610Smargo #include <ufs/ufs/ufs_extern.h>
2956610Smargo
3056610Smargo /*
3156610Smargo * Bmap converts a the logical block number of a file to its physical block
3256610Smargo * number on the disk. The conversion is done by using the logical block
3356610Smargo * number to index into the array of block pointers described by the dinode.
3456610Smargo */
3556610Smargo int
ufs_bmap(ap)3656610Smargo ufs_bmap(ap)
3756610Smargo struct vop_bmap_args /* {
3856610Smargo struct vnode *a_vp;
3956610Smargo daddr_t a_bn;
4056610Smargo struct vnode **a_vpp;
4156610Smargo daddr_t *a_bnp;
4256610Smargo int *a_runp;
4356610Smargo } */ *ap;
4456610Smargo {
4556610Smargo /*
4656610Smargo * Check for underlying vnode requests and ensure that logical
4756610Smargo * to physical mapping is requested.
4856610Smargo */
4956610Smargo if (ap->a_vpp != NULL)
5056610Smargo *ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
5156610Smargo if (ap->a_bnp == NULL)
5256610Smargo return (0);
5356610Smargo
5456610Smargo return (ufs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
5556610Smargo ap->a_runp));
5656610Smargo }
5756610Smargo
5856610Smargo /*
5956610Smargo * Indirect blocks are now on the vnode for the file. They are given negative
6056610Smargo * logical block numbers. Indirect blocks are addressed by the negative
6156610Smargo * address of the first data block to which they point. Double indirect blocks
6256610Smargo * are addressed by one less than the address of the first indirect block to
6356610Smargo * which they point. Triple indirect blocks are addressed by one less than
6456610Smargo * the address of the first double indirect block to which they point.
6556610Smargo *
6656610Smargo * ufs_bmaparray does the bmap conversion, and if requested returns the
6756610Smargo * array of logical blocks which must be traversed to get to a block.
6856610Smargo * Each entry contains the offset into that block that gets you to the
6956610Smargo * next block and the disk address of the block (if it is assigned).
7056610Smargo */
7156610Smargo
7256610Smargo int
ufs_bmaparray(vp,bn,bnp,ap,nump,runp)7356610Smargo ufs_bmaparray(vp, bn, bnp, ap, nump, runp)
7456610Smargo struct vnode *vp;
7556610Smargo register daddr_t bn;
7656610Smargo daddr_t *bnp;
7756610Smargo struct indir *ap;
7856610Smargo int *nump;
7956610Smargo int *runp;
8056610Smargo {
8156610Smargo register struct inode *ip;
8256610Smargo struct buf *bp;
8356610Smargo struct ufsmount *ump;
8456610Smargo struct mount *mp;
8556610Smargo struct vnode *devvp;
8656610Smargo struct indir a[NIADDR], *xap;
8765475Sbostic daddr_t daddr;
8856610Smargo long metalbn;
8965475Sbostic int error, maxrun, num;
9056610Smargo
9156610Smargo ip = VTOI(vp);
9256610Smargo mp = vp->v_mount;
9356610Smargo ump = VFSTOUFS(mp);
9456610Smargo #ifdef DIAGNOSTIC
9556610Smargo if (ap != NULL && nump == NULL || ap == NULL && nump != NULL)
9656610Smargo panic("ufs_bmaparray: invalid arguments");
9756610Smargo #endif
9856610Smargo
9956610Smargo if (runp) {
10056610Smargo /*
10164425Sbostic * XXX
10264425Sbostic * If MAXBSIZE is the largest transfer the disks can handle,
10364425Sbostic * we probably want maxrun to be 1 block less so that we
10464425Sbostic * don't create a block larger than the device can handle.
10556610Smargo */
10656610Smargo *runp = 0;
10757036Smargo maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
10856610Smargo }
10956610Smargo
11056610Smargo xap = ap == NULL ? a : ap;
11156610Smargo if (!nump)
11256610Smargo nump = #
11356610Smargo if (error = ufs_getlbns(vp, bn, xap, nump))
11456610Smargo return (error);
11556610Smargo
11656610Smargo num = *nump;
11756610Smargo if (num == 0) {
11856610Smargo *bnp = blkptrtodb(ump, ip->i_db[bn]);
11956610Smargo if (*bnp == 0)
12056610Smargo *bnp = -1;
12157036Smargo else if (runp)
12256610Smargo for (++bn; bn < NDADDR && *runp < maxrun &&
12356610Smargo is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
12456610Smargo ++bn, ++*runp);
12556610Smargo return (0);
12656610Smargo }
12756610Smargo
12856610Smargo
12956610Smargo /* Get disk address out of indirect block array */
13056610Smargo daddr = ip->i_ib[xap->in_off];
13156610Smargo
13256610Smargo devvp = VFSTOUFS(vp->v_mount)->um_devvp;
13356610Smargo for (bp = NULL, ++xap; --num; ++xap) {
13456610Smargo /*
13556610Smargo * Exit the loop if there is no disk address assigned yet and
13656610Smargo * the indirect block isn't in the cache, or if we were
13756610Smargo * looking for an indirect block and we've found it.
13856610Smargo */
13956610Smargo
14056610Smargo metalbn = xap->in_lbn;
14156610Smargo if (daddr == 0 && !incore(vp, metalbn) || metalbn == bn)
14256610Smargo break;
14356610Smargo /*
14456610Smargo * If we get here, we've either got the block in the cache
14556610Smargo * or we have a disk address for it, go fetch it.
14656610Smargo */
14756610Smargo if (bp)
14856610Smargo brelse(bp);
14956610Smargo
15056610Smargo xap->in_exists = 1;
15157799Smckusick bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
15256610Smargo if (bp->b_flags & (B_DONE | B_DELWRI)) {
15356610Smargo trace(TR_BREADHIT, pack(vp, size), metalbn);
15456610Smargo }
15556610Smargo #ifdef DIAGNOSTIC
15656610Smargo else if (!daddr)
15756610Smargo panic("ufs_bmaparry: indirect block not in cache");
15856610Smargo #endif
15956610Smargo else {
16056610Smargo trace(TR_BREADMISS, pack(vp, size), metalbn);
16156610Smargo bp->b_blkno = blkptrtodb(ump, daddr);
16256610Smargo bp->b_flags |= B_READ;
16356610Smargo VOP_STRATEGY(bp);
16456610Smargo curproc->p_stats->p_ru.ru_inblock++; /* XXX */
16556610Smargo if (error = biowait(bp)) {
16656610Smargo brelse(bp);
16756610Smargo return (error);
16856610Smargo }
16956610Smargo }
17056610Smargo
17164518Sbostic daddr = ((daddr_t *)bp->b_data)[xap->in_off];
17257036Smargo if (num == 1 && daddr && runp)
17356610Smargo for (bn = xap->in_off + 1;
17456610Smargo bn < MNINDIR(ump) && *runp < maxrun &&
17564518Sbostic is_sequential(ump, ((daddr_t *)bp->b_data)[bn - 1],
17664518Sbostic ((daddr_t *)bp->b_data)[bn]);
17756610Smargo ++bn, ++*runp);
17856610Smargo }
17956610Smargo if (bp)
18056610Smargo brelse(bp);
18156610Smargo
18256610Smargo daddr = blkptrtodb(ump, daddr);
18356610Smargo *bnp = daddr == 0 ? -1 : daddr;
18456610Smargo return (0);
18556610Smargo }
18656610Smargo
18756610Smargo /*
18856610Smargo * Create an array of logical block number/offset pairs which represent the
18956610Smargo * path of indirect blocks required to access a data block. The first "pair"
19056610Smargo * contains the logical block number of the appropriate single, double or
19156610Smargo * triple indirect block and the offset into the inode indirect block array.
19256610Smargo * Note, the logical block number of the inode single/double/triple indirect
19356610Smargo * block appears twice in the array, once with the offset into the i_ib and
19456610Smargo * once with the offset into the page itself.
19556610Smargo */
19656610Smargo int
ufs_getlbns(vp,bn,ap,nump)19756610Smargo ufs_getlbns(vp, bn, ap, nump)
19856610Smargo struct vnode *vp;
19956610Smargo register daddr_t bn;
20056610Smargo struct indir *ap;
20156610Smargo int *nump;
20256610Smargo {
20356610Smargo long metalbn, realbn;
20456610Smargo struct ufsmount *ump;
20564425Sbostic int blockcnt, i, numlevels, off;
20656610Smargo
20756610Smargo ump = VFSTOUFS(vp->v_mount);
20856610Smargo if (nump)
20956610Smargo *nump = 0;
21056610Smargo numlevels = 0;
21156610Smargo realbn = bn;
21256610Smargo if ((long)bn < 0)
21356610Smargo bn = -(long)bn;
21456610Smargo
21556610Smargo /* The first NDADDR blocks are direct blocks. */
21656610Smargo if (bn < NDADDR)
21756610Smargo return (0);
21856610Smargo
21956610Smargo /*
22056610Smargo * Determine the number of levels of indirection. After this loop
22164425Sbostic * is done, blockcnt indicates the number of data blocks possible
22264425Sbostic * at the given level of indirection, and NIADDR - i is the number
22364425Sbostic * of levels of indirection needed to locate the requested block.
22456610Smargo */
22564425Sbostic for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
22664425Sbostic if (i == 0)
22764425Sbostic return (EFBIG);
22864425Sbostic blockcnt *= MNINDIR(ump);
22964425Sbostic if (bn < blockcnt)
23056610Smargo break;
23156610Smargo }
23256610Smargo
23356610Smargo /* Calculate the address of the first meta-block. */
23456610Smargo if (realbn >= 0)
23564425Sbostic metalbn = -(realbn - bn + NIADDR - i);
23656610Smargo else
23764425Sbostic metalbn = -(-realbn - bn + NIADDR - i);
23856610Smargo
23956610Smargo /*
24056610Smargo * At each iteration, off is the offset into the bap array which is
24156610Smargo * an array of disk addresses at the current level of indirection.
24256610Smargo * The logical block number and the offset in that block are stored
24356610Smargo * into the argument array.
24456610Smargo */
24556610Smargo ap->in_lbn = metalbn;
24664425Sbostic ap->in_off = off = NIADDR - i;
24756610Smargo ap->in_exists = 0;
24856610Smargo ap++;
24964425Sbostic for (++numlevels; i <= NIADDR; i++) {
25056610Smargo /* If searching for a meta-data block, quit when found. */
25156610Smargo if (metalbn == realbn)
25256610Smargo break;
25356610Smargo
25464425Sbostic blockcnt /= MNINDIR(ump);
25564425Sbostic off = (bn / blockcnt) % MNINDIR(ump);
25656610Smargo
25756610Smargo ++numlevels;
25856610Smargo ap->in_lbn = metalbn;
25956610Smargo ap->in_off = off;
26056610Smargo ap->in_exists = 0;
26156610Smargo ++ap;
26256610Smargo
26364425Sbostic metalbn -= -1 + off * blockcnt;
26456610Smargo }
26556610Smargo if (nump)
26656610Smargo *nump = numlevels;
26756610Smargo return (0);
26856610Smargo }
269