1*0a6a1f1dSLionel Sambuc /* $NetBSD: ulfs_bmap.c,v 1.7 2015/09/01 06:08:37 dholland Exp $ */
284d9c625SLionel Sambuc /* from NetBSD: ufs_bmap.c,v 1.50 2013/01/22 09:39:18 dholland Exp */
384d9c625SLionel Sambuc
484d9c625SLionel Sambuc /*
584d9c625SLionel Sambuc * Copyright (c) 1989, 1991, 1993
684d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
784d9c625SLionel Sambuc * (c) UNIX System Laboratories, Inc.
884d9c625SLionel Sambuc * All or some portions of this file are derived from material licensed
984d9c625SLionel Sambuc * to the University of California by American Telephone and Telegraph
1084d9c625SLionel Sambuc * Co. or Unix System Laboratories, Inc. and are reproduced herein with
1184d9c625SLionel Sambuc * the permission of UNIX System Laboratories, Inc.
1284d9c625SLionel Sambuc *
1384d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1484d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
1584d9c625SLionel Sambuc * are met:
1684d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1784d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1884d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1984d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
2084d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
2184d9c625SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
2284d9c625SLionel Sambuc * may be used to endorse or promote products derived from this software
2384d9c625SLionel Sambuc * without specific prior written permission.
2484d9c625SLionel Sambuc *
2584d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2684d9c625SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2784d9c625SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2884d9c625SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2984d9c625SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3084d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3184d9c625SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3284d9c625SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3384d9c625SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3484d9c625SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3584d9c625SLionel Sambuc * SUCH DAMAGE.
3684d9c625SLionel Sambuc *
3784d9c625SLionel Sambuc * @(#)ufs_bmap.c 8.8 (Berkeley) 8/11/95
3884d9c625SLionel Sambuc */
3984d9c625SLionel Sambuc
4084d9c625SLionel Sambuc #include <sys/cdefs.h>
41*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: ulfs_bmap.c,v 1.7 2015/09/01 06:08:37 dholland Exp $");
4284d9c625SLionel Sambuc
4384d9c625SLionel Sambuc #include <sys/param.h>
4484d9c625SLionel Sambuc #include <sys/systm.h>
4584d9c625SLionel Sambuc #include <sys/stat.h>
4684d9c625SLionel Sambuc #include <sys/buf.h>
4784d9c625SLionel Sambuc #include <sys/proc.h>
4884d9c625SLionel Sambuc #include <sys/vnode.h>
4984d9c625SLionel Sambuc #include <sys/mount.h>
5084d9c625SLionel Sambuc #include <sys/resourcevar.h>
5184d9c625SLionel Sambuc #include <sys/trace.h>
5284d9c625SLionel Sambuc #include <sys/fstrans.h>
5384d9c625SLionel Sambuc
5484d9c625SLionel Sambuc #include <miscfs/specfs/specdev.h>
5584d9c625SLionel Sambuc
5684d9c625SLionel Sambuc #include <ufs/lfs/ulfs_inode.h>
5784d9c625SLionel Sambuc #include <ufs/lfs/ulfsmount.h>
5884d9c625SLionel Sambuc #include <ufs/lfs/ulfs_extern.h>
5984d9c625SLionel Sambuc #include <ufs/lfs/ulfs_bswap.h>
6084d9c625SLionel Sambuc
6184d9c625SLionel Sambuc static bool
ulfs_issequential(const struct lfs * fs,daddr_t daddr0,daddr_t daddr1)6284d9c625SLionel Sambuc ulfs_issequential(const struct lfs *fs, daddr_t daddr0, daddr_t daddr1)
6384d9c625SLionel Sambuc {
6484d9c625SLionel Sambuc
6584d9c625SLionel Sambuc /* for ulfs, blocks in a hole is not 'contiguous'. */
6684d9c625SLionel Sambuc if (daddr0 == 0)
6784d9c625SLionel Sambuc return false;
6884d9c625SLionel Sambuc
6984d9c625SLionel Sambuc return (daddr0 + fs->um_seqinc == daddr1);
7084d9c625SLionel Sambuc }
7184d9c625SLionel Sambuc
7284d9c625SLionel Sambuc /*
73*0a6a1f1dSLionel Sambuc * This is used for block pointers in inodes and elsewhere, which can
74*0a6a1f1dSLionel Sambuc * contain the magic value UNWRITTEN, which is -2. This is mishandled
75*0a6a1f1dSLionel Sambuc * by u32 -> u64 promotion unless special-cased.
76*0a6a1f1dSLionel Sambuc *
77*0a6a1f1dSLionel Sambuc * XXX this should be rolled into better inode accessors and go away.
78*0a6a1f1dSLionel Sambuc */
79*0a6a1f1dSLionel Sambuc static inline uint64_t
ulfs_fix_unwritten(uint32_t val)80*0a6a1f1dSLionel Sambuc ulfs_fix_unwritten(uint32_t val)
81*0a6a1f1dSLionel Sambuc {
82*0a6a1f1dSLionel Sambuc if (val == (uint32_t)UNWRITTEN) {
83*0a6a1f1dSLionel Sambuc return (uint64_t)(int64_t)UNWRITTEN;
84*0a6a1f1dSLionel Sambuc } else {
85*0a6a1f1dSLionel Sambuc return val;
86*0a6a1f1dSLionel Sambuc }
87*0a6a1f1dSLionel Sambuc }
88*0a6a1f1dSLionel Sambuc
89*0a6a1f1dSLionel Sambuc
90*0a6a1f1dSLionel Sambuc /*
9184d9c625SLionel Sambuc * Bmap converts the logical block number of a file to its physical block
9284d9c625SLionel Sambuc * number on the disk. The conversion is done by using the logical block
9384d9c625SLionel Sambuc * number to index into the array of block pointers described by the dinode.
9484d9c625SLionel Sambuc */
9584d9c625SLionel Sambuc int
ulfs_bmap(void * v)9684d9c625SLionel Sambuc ulfs_bmap(void *v)
9784d9c625SLionel Sambuc {
9884d9c625SLionel Sambuc struct vop_bmap_args /* {
9984d9c625SLionel Sambuc struct vnode *a_vp;
10084d9c625SLionel Sambuc daddr_t a_bn;
10184d9c625SLionel Sambuc struct vnode **a_vpp;
10284d9c625SLionel Sambuc daddr_t *a_bnp;
10384d9c625SLionel Sambuc int *a_runp;
10484d9c625SLionel Sambuc } */ *ap = v;
10584d9c625SLionel Sambuc int error;
10684d9c625SLionel Sambuc
10784d9c625SLionel Sambuc /*
10884d9c625SLionel Sambuc * Check for underlying vnode requests and ensure that logical
10984d9c625SLionel Sambuc * to physical mapping is requested.
11084d9c625SLionel Sambuc */
11184d9c625SLionel Sambuc if (ap->a_vpp != NULL)
11284d9c625SLionel Sambuc *ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
11384d9c625SLionel Sambuc if (ap->a_bnp == NULL)
11484d9c625SLionel Sambuc return (0);
11584d9c625SLionel Sambuc
11684d9c625SLionel Sambuc fstrans_start(ap->a_vp->v_mount, FSTRANS_SHARED);
11784d9c625SLionel Sambuc error = ulfs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
11884d9c625SLionel Sambuc ap->a_runp, ulfs_issequential);
11984d9c625SLionel Sambuc fstrans_done(ap->a_vp->v_mount);
12084d9c625SLionel Sambuc return error;
12184d9c625SLionel Sambuc }
12284d9c625SLionel Sambuc
12384d9c625SLionel Sambuc /*
12484d9c625SLionel Sambuc * Indirect blocks are now on the vnode for the file. They are given negative
12584d9c625SLionel Sambuc * logical block numbers. Indirect blocks are addressed by the negative
12684d9c625SLionel Sambuc * address of the first data block to which they point. Double indirect blocks
12784d9c625SLionel Sambuc * are addressed by one less than the address of the first indirect block to
12884d9c625SLionel Sambuc * which they point. Triple indirect blocks are addressed by one less than
12984d9c625SLionel Sambuc * the address of the first double indirect block to which they point.
13084d9c625SLionel Sambuc *
13184d9c625SLionel Sambuc * ulfs_bmaparray does the bmap conversion, and if requested returns the
13284d9c625SLionel Sambuc * array of logical blocks which must be traversed to get to a block.
13384d9c625SLionel Sambuc * Each entry contains the offset into that block that gets you to the
13484d9c625SLionel Sambuc * next block and the disk address of the block (if it is assigned).
13584d9c625SLionel Sambuc */
13684d9c625SLionel Sambuc
13784d9c625SLionel Sambuc int
ulfs_bmaparray(struct vnode * vp,daddr_t bn,daddr_t * bnp,struct indir * ap,int * nump,int * runp,ulfs_issequential_callback_t is_sequential)13884d9c625SLionel Sambuc ulfs_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, struct indir *ap,
13984d9c625SLionel Sambuc int *nump, int *runp, ulfs_issequential_callback_t is_sequential)
14084d9c625SLionel Sambuc {
14184d9c625SLionel Sambuc struct inode *ip;
14284d9c625SLionel Sambuc struct buf *bp, *cbp;
14384d9c625SLionel Sambuc struct ulfsmount *ump;
14484d9c625SLionel Sambuc struct lfs *fs;
14584d9c625SLionel Sambuc struct mount *mp;
14684d9c625SLionel Sambuc struct indir a[ULFS_NIADDR + 1], *xap;
14784d9c625SLionel Sambuc daddr_t daddr;
14884d9c625SLionel Sambuc daddr_t metalbn;
14984d9c625SLionel Sambuc int error, maxrun = 0, num;
15084d9c625SLionel Sambuc
15184d9c625SLionel Sambuc ip = VTOI(vp);
15284d9c625SLionel Sambuc mp = vp->v_mount;
15384d9c625SLionel Sambuc ump = ip->i_ump;
15484d9c625SLionel Sambuc fs = ip->i_lfs;
15584d9c625SLionel Sambuc #ifdef DIAGNOSTIC
15684d9c625SLionel Sambuc if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
15784d9c625SLionel Sambuc panic("ulfs_bmaparray: invalid arguments");
15884d9c625SLionel Sambuc #endif
15984d9c625SLionel Sambuc
16084d9c625SLionel Sambuc if (runp) {
16184d9c625SLionel Sambuc /*
16284d9c625SLionel Sambuc * XXX
16384d9c625SLionel Sambuc * If MAXBSIZE is the largest transfer the disks can handle,
16484d9c625SLionel Sambuc * we probably want maxrun to be 1 block less so that we
16584d9c625SLionel Sambuc * don't create a block larger than the device can handle.
16684d9c625SLionel Sambuc */
16784d9c625SLionel Sambuc *runp = 0;
16884d9c625SLionel Sambuc maxrun = MAXPHYS / mp->mnt_stat.f_iosize - 1;
16984d9c625SLionel Sambuc }
17084d9c625SLionel Sambuc
17184d9c625SLionel Sambuc if (bn >= 0 && bn < ULFS_NDADDR) {
17284d9c625SLionel Sambuc if (nump != NULL)
17384d9c625SLionel Sambuc *nump = 0;
17484d9c625SLionel Sambuc if (ump->um_fstype == ULFS1)
175*0a6a1f1dSLionel Sambuc daddr = ulfs_fix_unwritten(ulfs_rw32(ip->i_din->u_32.di_db[bn],
176*0a6a1f1dSLionel Sambuc ULFS_MPNEEDSWAP(fs)));
17784d9c625SLionel Sambuc else
178*0a6a1f1dSLionel Sambuc daddr = ulfs_rw64(ip->i_din->u_64.di_db[bn],
17984d9c625SLionel Sambuc ULFS_MPNEEDSWAP(fs));
18084d9c625SLionel Sambuc *bnp = blkptrtodb(fs, daddr);
18184d9c625SLionel Sambuc /*
18284d9c625SLionel Sambuc * Since this is FFS independent code, we are out of
18384d9c625SLionel Sambuc * scope for the definitions of BLK_NOCOPY and
18484d9c625SLionel Sambuc * BLK_SNAP, but we do know that they will fall in
18584d9c625SLionel Sambuc * the range 1..um_seqinc, so we use that test and
18684d9c625SLionel Sambuc * return a request for a zeroed out buffer if attempts
18784d9c625SLionel Sambuc * are made to read a BLK_NOCOPY or BLK_SNAP block.
18884d9c625SLionel Sambuc */
18984d9c625SLionel Sambuc if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) == SF_SNAPSHOT
19084d9c625SLionel Sambuc && daddr > 0 &&
19184d9c625SLionel Sambuc daddr < fs->um_seqinc) {
19284d9c625SLionel Sambuc *bnp = -1;
19384d9c625SLionel Sambuc } else if (*bnp == 0) {
19484d9c625SLionel Sambuc if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))
19584d9c625SLionel Sambuc == SF_SNAPSHOT) {
19684d9c625SLionel Sambuc *bnp = blkptrtodb(fs, bn * fs->um_seqinc);
19784d9c625SLionel Sambuc } else {
19884d9c625SLionel Sambuc *bnp = -1;
19984d9c625SLionel Sambuc }
20084d9c625SLionel Sambuc } else if (runp) {
20184d9c625SLionel Sambuc if (ump->um_fstype == ULFS1) {
20284d9c625SLionel Sambuc for (++bn; bn < ULFS_NDADDR && *runp < maxrun &&
20384d9c625SLionel Sambuc is_sequential(fs,
204*0a6a1f1dSLionel Sambuc ulfs_fix_unwritten(ulfs_rw32(ip->i_din->u_32.di_db[bn - 1],
205*0a6a1f1dSLionel Sambuc ULFS_MPNEEDSWAP(fs))),
206*0a6a1f1dSLionel Sambuc ulfs_fix_unwritten(ulfs_rw32(ip->i_din->u_32.di_db[bn],
207*0a6a1f1dSLionel Sambuc ULFS_MPNEEDSWAP(fs))));
20884d9c625SLionel Sambuc ++bn, ++*runp);
20984d9c625SLionel Sambuc } else {
21084d9c625SLionel Sambuc for (++bn; bn < ULFS_NDADDR && *runp < maxrun &&
21184d9c625SLionel Sambuc is_sequential(fs,
212*0a6a1f1dSLionel Sambuc ulfs_rw64(ip->i_din->u_64.di_db[bn - 1],
21384d9c625SLionel Sambuc ULFS_MPNEEDSWAP(fs)),
214*0a6a1f1dSLionel Sambuc ulfs_rw64(ip->i_din->u_64.di_db[bn],
21584d9c625SLionel Sambuc ULFS_MPNEEDSWAP(fs)));
21684d9c625SLionel Sambuc ++bn, ++*runp);
21784d9c625SLionel Sambuc }
21884d9c625SLionel Sambuc }
21984d9c625SLionel Sambuc return (0);
22084d9c625SLionel Sambuc }
22184d9c625SLionel Sambuc
22284d9c625SLionel Sambuc xap = ap == NULL ? a : ap;
22384d9c625SLionel Sambuc if (!nump)
22484d9c625SLionel Sambuc nump = #
22584d9c625SLionel Sambuc if ((error = ulfs_getlbns(vp, bn, xap, nump)) != 0)
22684d9c625SLionel Sambuc return (error);
22784d9c625SLionel Sambuc
22884d9c625SLionel Sambuc num = *nump;
22984d9c625SLionel Sambuc
23084d9c625SLionel Sambuc /* Get disk address out of indirect block array */
231*0a6a1f1dSLionel Sambuc // XXX clean this up
23284d9c625SLionel Sambuc if (ump->um_fstype == ULFS1)
233*0a6a1f1dSLionel Sambuc daddr = ulfs_fix_unwritten(ulfs_rw32(ip->i_din->u_32.di_ib[xap->in_off],
234*0a6a1f1dSLionel Sambuc ULFS_MPNEEDSWAP(fs)));
23584d9c625SLionel Sambuc else
236*0a6a1f1dSLionel Sambuc daddr = ulfs_rw64(ip->i_din->u_64.di_ib[xap->in_off],
23784d9c625SLionel Sambuc ULFS_MPNEEDSWAP(fs));
23884d9c625SLionel Sambuc
23984d9c625SLionel Sambuc for (bp = NULL, ++xap; --num; ++xap) {
24084d9c625SLionel Sambuc /*
24184d9c625SLionel Sambuc * Exit the loop if there is no disk address assigned yet and
24284d9c625SLionel Sambuc * the indirect block isn't in the cache, or if we were
24384d9c625SLionel Sambuc * looking for an indirect block and we've found it.
24484d9c625SLionel Sambuc */
24584d9c625SLionel Sambuc
24684d9c625SLionel Sambuc metalbn = xap->in_lbn;
24784d9c625SLionel Sambuc if (metalbn == bn)
24884d9c625SLionel Sambuc break;
24984d9c625SLionel Sambuc if (daddr == 0) {
25084d9c625SLionel Sambuc mutex_enter(&bufcache_lock);
25184d9c625SLionel Sambuc cbp = incore(vp, metalbn);
25284d9c625SLionel Sambuc mutex_exit(&bufcache_lock);
25384d9c625SLionel Sambuc if (cbp == NULL)
25484d9c625SLionel Sambuc break;
25584d9c625SLionel Sambuc }
25684d9c625SLionel Sambuc
25784d9c625SLionel Sambuc /*
25884d9c625SLionel Sambuc * If we get here, we've either got the block in the cache
25984d9c625SLionel Sambuc * or we have a disk address for it, go fetch it.
26084d9c625SLionel Sambuc */
26184d9c625SLionel Sambuc if (bp)
26284d9c625SLionel Sambuc brelse(bp, 0);
26384d9c625SLionel Sambuc
26484d9c625SLionel Sambuc xap->in_exists = 1;
26584d9c625SLionel Sambuc bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
26684d9c625SLionel Sambuc if (bp == NULL) {
26784d9c625SLionel Sambuc
26884d9c625SLionel Sambuc /*
26984d9c625SLionel Sambuc * getblk() above returns NULL only iff we are
27084d9c625SLionel Sambuc * pagedaemon. See the implementation of getblk
27184d9c625SLionel Sambuc * for detail.
27284d9c625SLionel Sambuc */
27384d9c625SLionel Sambuc
27484d9c625SLionel Sambuc return (ENOMEM);
27584d9c625SLionel Sambuc }
27684d9c625SLionel Sambuc if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
27784d9c625SLionel Sambuc trace(TR_BREADHIT, pack(vp, size), metalbn);
27884d9c625SLionel Sambuc }
27984d9c625SLionel Sambuc #ifdef DIAGNOSTIC
28084d9c625SLionel Sambuc else if (!daddr)
28184d9c625SLionel Sambuc panic("ulfs_bmaparray: indirect block not in cache");
28284d9c625SLionel Sambuc #endif
28384d9c625SLionel Sambuc else {
28484d9c625SLionel Sambuc trace(TR_BREADMISS, pack(vp, size), metalbn);
28584d9c625SLionel Sambuc bp->b_blkno = blkptrtodb(fs, daddr);
28684d9c625SLionel Sambuc bp->b_flags |= B_READ;
28784d9c625SLionel Sambuc BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
28884d9c625SLionel Sambuc VOP_STRATEGY(vp, bp);
28984d9c625SLionel Sambuc curlwp->l_ru.ru_inblock++; /* XXX */
29084d9c625SLionel Sambuc if ((error = biowait(bp)) != 0) {
29184d9c625SLionel Sambuc brelse(bp, 0);
29284d9c625SLionel Sambuc return (error);
29384d9c625SLionel Sambuc }
29484d9c625SLionel Sambuc }
29584d9c625SLionel Sambuc if (ump->um_fstype == ULFS1) {
296*0a6a1f1dSLionel Sambuc daddr = ulfs_fix_unwritten(ulfs_rw32(((u_int32_t *)bp->b_data)[xap->in_off],
297*0a6a1f1dSLionel Sambuc ULFS_MPNEEDSWAP(fs)));
29884d9c625SLionel Sambuc if (num == 1 && daddr && runp) {
29984d9c625SLionel Sambuc for (bn = xap->in_off + 1;
30084d9c625SLionel Sambuc bn < MNINDIR(fs) && *runp < maxrun &&
30184d9c625SLionel Sambuc is_sequential(fs,
302*0a6a1f1dSLionel Sambuc ulfs_fix_unwritten(ulfs_rw32(((int32_t *)bp->b_data)[bn-1],
303*0a6a1f1dSLionel Sambuc ULFS_MPNEEDSWAP(fs))),
304*0a6a1f1dSLionel Sambuc ulfs_fix_unwritten(ulfs_rw32(((int32_t *)bp->b_data)[bn],
305*0a6a1f1dSLionel Sambuc ULFS_MPNEEDSWAP(fs))));
30684d9c625SLionel Sambuc ++bn, ++*runp);
30784d9c625SLionel Sambuc }
30884d9c625SLionel Sambuc } else {
30984d9c625SLionel Sambuc daddr = ulfs_rw64(((u_int64_t *)bp->b_data)[xap->in_off],
31084d9c625SLionel Sambuc ULFS_MPNEEDSWAP(fs));
31184d9c625SLionel Sambuc if (num == 1 && daddr && runp) {
31284d9c625SLionel Sambuc for (bn = xap->in_off + 1;
31384d9c625SLionel Sambuc bn < MNINDIR(fs) && *runp < maxrun &&
31484d9c625SLionel Sambuc is_sequential(fs,
31584d9c625SLionel Sambuc ulfs_rw64(((int64_t *)bp->b_data)[bn-1],
31684d9c625SLionel Sambuc ULFS_MPNEEDSWAP(fs)),
31784d9c625SLionel Sambuc ulfs_rw64(((int64_t *)bp->b_data)[bn],
31884d9c625SLionel Sambuc ULFS_MPNEEDSWAP(fs)));
31984d9c625SLionel Sambuc ++bn, ++*runp);
32084d9c625SLionel Sambuc }
32184d9c625SLionel Sambuc }
32284d9c625SLionel Sambuc }
32384d9c625SLionel Sambuc if (bp)
32484d9c625SLionel Sambuc brelse(bp, 0);
32584d9c625SLionel Sambuc
32684d9c625SLionel Sambuc /*
32784d9c625SLionel Sambuc * Since this is FFS independent code, we are out of scope for the
32884d9c625SLionel Sambuc * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
32984d9c625SLionel Sambuc * will fall in the range 1..um_seqinc, so we use that test and
33084d9c625SLionel Sambuc * return a request for a zeroed out buffer if attempts are made
33184d9c625SLionel Sambuc * to read a BLK_NOCOPY or BLK_SNAP block.
33284d9c625SLionel Sambuc */
33384d9c625SLionel Sambuc if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) == SF_SNAPSHOT
33484d9c625SLionel Sambuc && daddr > 0 && daddr < fs->um_seqinc) {
33584d9c625SLionel Sambuc *bnp = -1;
33684d9c625SLionel Sambuc return (0);
33784d9c625SLionel Sambuc }
33884d9c625SLionel Sambuc *bnp = blkptrtodb(fs, daddr);
33984d9c625SLionel Sambuc if (*bnp == 0) {
34084d9c625SLionel Sambuc if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))
34184d9c625SLionel Sambuc == SF_SNAPSHOT) {
34284d9c625SLionel Sambuc *bnp = blkptrtodb(fs, bn * fs->um_seqinc);
34384d9c625SLionel Sambuc } else {
34484d9c625SLionel Sambuc *bnp = -1;
34584d9c625SLionel Sambuc }
34684d9c625SLionel Sambuc }
34784d9c625SLionel Sambuc return (0);
34884d9c625SLionel Sambuc }
34984d9c625SLionel Sambuc
35084d9c625SLionel Sambuc /*
35184d9c625SLionel Sambuc * Create an array of logical block number/offset pairs which represent the
35284d9c625SLionel Sambuc * path of indirect blocks required to access a data block. The first "pair"
35384d9c625SLionel Sambuc * contains the logical block number of the appropriate single, double or
35484d9c625SLionel Sambuc * triple indirect block and the offset into the inode indirect block array.
35584d9c625SLionel Sambuc * Note, the logical block number of the inode single/double/triple indirect
35684d9c625SLionel Sambuc * block appears twice in the array, once with the offset into the i_ffs1_ib and
35784d9c625SLionel Sambuc * once with the offset into the page itself.
35884d9c625SLionel Sambuc */
35984d9c625SLionel Sambuc int
ulfs_getlbns(struct vnode * vp,daddr_t bn,struct indir * ap,int * nump)36084d9c625SLionel Sambuc ulfs_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
36184d9c625SLionel Sambuc {
36284d9c625SLionel Sambuc daddr_t metalbn, realbn;
36384d9c625SLionel Sambuc struct ulfsmount *ump;
36484d9c625SLionel Sambuc struct lfs *fs;
36584d9c625SLionel Sambuc int64_t blockcnt;
36684d9c625SLionel Sambuc int lbc;
36784d9c625SLionel Sambuc int i, numlevels, off;
36884d9c625SLionel Sambuc
36984d9c625SLionel Sambuc ump = VFSTOULFS(vp->v_mount);
37084d9c625SLionel Sambuc fs = ump->um_lfs;
37184d9c625SLionel Sambuc if (nump)
37284d9c625SLionel Sambuc *nump = 0;
37384d9c625SLionel Sambuc numlevels = 0;
37484d9c625SLionel Sambuc realbn = bn;
37584d9c625SLionel Sambuc if (bn < 0)
37684d9c625SLionel Sambuc bn = -bn;
37784d9c625SLionel Sambuc KASSERT(bn >= ULFS_NDADDR);
37884d9c625SLionel Sambuc
37984d9c625SLionel Sambuc /*
38084d9c625SLionel Sambuc * Determine the number of levels of indirection. After this loop
38184d9c625SLionel Sambuc * is done, blockcnt indicates the number of data blocks possible
38284d9c625SLionel Sambuc * at the given level of indirection, and ULFS_NIADDR - i is the number
38384d9c625SLionel Sambuc * of levels of indirection needed to locate the requested block.
38484d9c625SLionel Sambuc */
38584d9c625SLionel Sambuc
38684d9c625SLionel Sambuc bn -= ULFS_NDADDR;
38784d9c625SLionel Sambuc for (lbc = 0, i = ULFS_NIADDR;; i--, bn -= blockcnt) {
38884d9c625SLionel Sambuc if (i == 0)
38984d9c625SLionel Sambuc return (EFBIG);
39084d9c625SLionel Sambuc
39184d9c625SLionel Sambuc lbc += fs->um_lognindir;
39284d9c625SLionel Sambuc blockcnt = (int64_t)1 << lbc;
39384d9c625SLionel Sambuc
39484d9c625SLionel Sambuc if (bn < blockcnt)
39584d9c625SLionel Sambuc break;
39684d9c625SLionel Sambuc }
39784d9c625SLionel Sambuc
39884d9c625SLionel Sambuc /* Calculate the address of the first meta-block. */
39984d9c625SLionel Sambuc metalbn = -((realbn >= 0 ? realbn : -realbn) - bn + ULFS_NIADDR - i);
40084d9c625SLionel Sambuc
40184d9c625SLionel Sambuc /*
40284d9c625SLionel Sambuc * At each iteration, off is the offset into the bap array which is
40384d9c625SLionel Sambuc * an array of disk addresses at the current level of indirection.
40484d9c625SLionel Sambuc * The logical block number and the offset in that block are stored
40584d9c625SLionel Sambuc * into the argument array.
40684d9c625SLionel Sambuc */
40784d9c625SLionel Sambuc ap->in_lbn = metalbn;
40884d9c625SLionel Sambuc ap->in_off = off = ULFS_NIADDR - i;
40984d9c625SLionel Sambuc ap->in_exists = 0;
41084d9c625SLionel Sambuc ap++;
41184d9c625SLionel Sambuc for (++numlevels; i <= ULFS_NIADDR; i++) {
41284d9c625SLionel Sambuc /* If searching for a meta-data block, quit when found. */
41384d9c625SLionel Sambuc if (metalbn == realbn)
41484d9c625SLionel Sambuc break;
41584d9c625SLionel Sambuc
41684d9c625SLionel Sambuc lbc -= fs->um_lognindir;
41784d9c625SLionel Sambuc off = (bn >> lbc) & (MNINDIR(fs) - 1);
41884d9c625SLionel Sambuc
41984d9c625SLionel Sambuc ++numlevels;
42084d9c625SLionel Sambuc ap->in_lbn = metalbn;
42184d9c625SLionel Sambuc ap->in_off = off;
42284d9c625SLionel Sambuc ap->in_exists = 0;
42384d9c625SLionel Sambuc ++ap;
42484d9c625SLionel Sambuc
42584d9c625SLionel Sambuc metalbn -= -1 + ((int64_t)off << lbc);
42684d9c625SLionel Sambuc }
42784d9c625SLionel Sambuc if (nump)
42884d9c625SLionel Sambuc *nump = numlevels;
42984d9c625SLionel Sambuc return (0);
43084d9c625SLionel Sambuc }
431