Home | History | Annotate | Line # | Download | only in lfs
ulfs_bmap.c revision 1.4.2.3
      1  1.4.2.2  tls /*	$NetBSD: ulfs_bmap.c,v 1.4.2.3 2014/08/20 00:04:45 tls Exp $	*/
      2  1.4.2.2  tls /*  from NetBSD: ufs_bmap.c,v 1.50 2013/01/22 09:39:18 dholland Exp  */
      3  1.4.2.2  tls 
      4  1.4.2.2  tls /*
      5  1.4.2.2  tls  * Copyright (c) 1989, 1991, 1993
      6  1.4.2.2  tls  *	The Regents of the University of California.  All rights reserved.
      7  1.4.2.2  tls  * (c) UNIX System Laboratories, Inc.
      8  1.4.2.2  tls  * All or some portions of this file are derived from material licensed
      9  1.4.2.2  tls  * to the University of California by American Telephone and Telegraph
     10  1.4.2.2  tls  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     11  1.4.2.2  tls  * the permission of UNIX System Laboratories, Inc.
     12  1.4.2.2  tls  *
     13  1.4.2.2  tls  * Redistribution and use in source and binary forms, with or without
     14  1.4.2.2  tls  * modification, are permitted provided that the following conditions
     15  1.4.2.2  tls  * are met:
     16  1.4.2.2  tls  * 1. Redistributions of source code must retain the above copyright
     17  1.4.2.2  tls  *    notice, this list of conditions and the following disclaimer.
     18  1.4.2.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.4.2.2  tls  *    notice, this list of conditions and the following disclaimer in the
     20  1.4.2.2  tls  *    documentation and/or other materials provided with the distribution.
     21  1.4.2.2  tls  * 3. Neither the name of the University nor the names of its contributors
     22  1.4.2.2  tls  *    may be used to endorse or promote products derived from this software
     23  1.4.2.2  tls  *    without specific prior written permission.
     24  1.4.2.2  tls  *
     25  1.4.2.2  tls  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.4.2.2  tls  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.4.2.2  tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.4.2.2  tls  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.4.2.2  tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.4.2.2  tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.4.2.2  tls  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.4.2.2  tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.4.2.2  tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.4.2.2  tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.4.2.2  tls  * SUCH DAMAGE.
     36  1.4.2.2  tls  *
     37  1.4.2.2  tls  *	@(#)ufs_bmap.c	8.8 (Berkeley) 8/11/95
     38  1.4.2.2  tls  */
     39  1.4.2.2  tls 
     40  1.4.2.2  tls #include <sys/cdefs.h>
     41  1.4.2.2  tls __KERNEL_RCSID(0, "$NetBSD: ulfs_bmap.c,v 1.4.2.3 2014/08/20 00:04:45 tls Exp $");
     42  1.4.2.2  tls 
     43  1.4.2.2  tls #include <sys/param.h>
     44  1.4.2.2  tls #include <sys/systm.h>
     45  1.4.2.2  tls #include <sys/stat.h>
     46  1.4.2.2  tls #include <sys/buf.h>
     47  1.4.2.2  tls #include <sys/proc.h>
     48  1.4.2.2  tls #include <sys/vnode.h>
     49  1.4.2.2  tls #include <sys/mount.h>
     50  1.4.2.2  tls #include <sys/resourcevar.h>
     51  1.4.2.2  tls #include <sys/trace.h>
     52  1.4.2.2  tls #include <sys/fstrans.h>
     53  1.4.2.2  tls 
     54  1.4.2.2  tls #include <miscfs/specfs/specdev.h>
     55  1.4.2.2  tls 
     56  1.4.2.2  tls #include <ufs/lfs/ulfs_inode.h>
     57  1.4.2.2  tls #include <ufs/lfs/ulfsmount.h>
     58  1.4.2.2  tls #include <ufs/lfs/ulfs_extern.h>
     59  1.4.2.2  tls #include <ufs/lfs/ulfs_bswap.h>
     60  1.4.2.2  tls 
     61  1.4.2.2  tls static bool
     62  1.4.2.3  tls ulfs_issequential(const struct lfs *fs, daddr_t daddr0, daddr_t daddr1)
     63  1.4.2.2  tls {
     64  1.4.2.2  tls 
     65  1.4.2.2  tls 	/* for ulfs, blocks in a hole is not 'contiguous'. */
     66  1.4.2.2  tls 	if (daddr0 == 0)
     67  1.4.2.2  tls 		return false;
     68  1.4.2.2  tls 
     69  1.4.2.3  tls 	return (daddr0 + fs->um_seqinc == daddr1);
     70  1.4.2.2  tls }
     71  1.4.2.2  tls 
     72  1.4.2.2  tls /*
     73  1.4.2.2  tls  * Bmap converts the logical block number of a file to its physical block
     74  1.4.2.2  tls  * number on the disk. The conversion is done by using the logical block
     75  1.4.2.2  tls  * number to index into the array of block pointers described by the dinode.
     76  1.4.2.2  tls  */
     77  1.4.2.2  tls int
     78  1.4.2.2  tls ulfs_bmap(void *v)
     79  1.4.2.2  tls {
     80  1.4.2.2  tls 	struct vop_bmap_args /* {
     81  1.4.2.2  tls 		struct vnode *a_vp;
     82  1.4.2.2  tls 		daddr_t  a_bn;
     83  1.4.2.2  tls 		struct vnode **a_vpp;
     84  1.4.2.2  tls 		daddr_t *a_bnp;
     85  1.4.2.2  tls 		int *a_runp;
     86  1.4.2.2  tls 	} */ *ap = v;
     87  1.4.2.2  tls 	int error;
     88  1.4.2.2  tls 
     89  1.4.2.2  tls 	/*
     90  1.4.2.2  tls 	 * Check for underlying vnode requests and ensure that logical
     91  1.4.2.2  tls 	 * to physical mapping is requested.
     92  1.4.2.2  tls 	 */
     93  1.4.2.2  tls 	if (ap->a_vpp != NULL)
     94  1.4.2.2  tls 		*ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
     95  1.4.2.2  tls 	if (ap->a_bnp == NULL)
     96  1.4.2.2  tls 		return (0);
     97  1.4.2.2  tls 
     98  1.4.2.2  tls 	fstrans_start(ap->a_vp->v_mount, FSTRANS_SHARED);
     99  1.4.2.2  tls 	error = ulfs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
    100  1.4.2.2  tls 	    ap->a_runp, ulfs_issequential);
    101  1.4.2.2  tls 	fstrans_done(ap->a_vp->v_mount);
    102  1.4.2.2  tls 	return error;
    103  1.4.2.2  tls }
    104  1.4.2.2  tls 
    105  1.4.2.2  tls /*
    106  1.4.2.2  tls  * Indirect blocks are now on the vnode for the file.  They are given negative
    107  1.4.2.2  tls  * logical block numbers.  Indirect blocks are addressed by the negative
    108  1.4.2.2  tls  * address of the first data block to which they point.  Double indirect blocks
    109  1.4.2.2  tls  * are addressed by one less than the address of the first indirect block to
    110  1.4.2.2  tls  * which they point.  Triple indirect blocks are addressed by one less than
    111  1.4.2.2  tls  * the address of the first double indirect block to which they point.
    112  1.4.2.2  tls  *
    113  1.4.2.2  tls  * ulfs_bmaparray does the bmap conversion, and if requested returns the
    114  1.4.2.2  tls  * array of logical blocks which must be traversed to get to a block.
    115  1.4.2.2  tls  * Each entry contains the offset into that block that gets you to the
    116  1.4.2.2  tls  * next block and the disk address of the block (if it is assigned).
    117  1.4.2.2  tls  */
    118  1.4.2.2  tls 
    119  1.4.2.2  tls int
    120  1.4.2.2  tls ulfs_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, struct indir *ap,
    121  1.4.2.2  tls     int *nump, int *runp, ulfs_issequential_callback_t is_sequential)
    122  1.4.2.2  tls {
    123  1.4.2.2  tls 	struct inode *ip;
    124  1.4.2.2  tls 	struct buf *bp, *cbp;
    125  1.4.2.2  tls 	struct ulfsmount *ump;
    126  1.4.2.3  tls 	struct lfs *fs;
    127  1.4.2.2  tls 	struct mount *mp;
    128  1.4.2.2  tls 	struct indir a[ULFS_NIADDR + 1], *xap;
    129  1.4.2.2  tls 	daddr_t daddr;
    130  1.4.2.2  tls 	daddr_t metalbn;
    131  1.4.2.2  tls 	int error, maxrun = 0, num;
    132  1.4.2.2  tls 
    133  1.4.2.2  tls 	ip = VTOI(vp);
    134  1.4.2.2  tls 	mp = vp->v_mount;
    135  1.4.2.2  tls 	ump = ip->i_ump;
    136  1.4.2.3  tls 	fs = ip->i_lfs;
    137  1.4.2.2  tls #ifdef DIAGNOSTIC
    138  1.4.2.2  tls 	if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
    139  1.4.2.2  tls 		panic("ulfs_bmaparray: invalid arguments");
    140  1.4.2.2  tls #endif
    141  1.4.2.2  tls 
    142  1.4.2.2  tls 	if (runp) {
    143  1.4.2.2  tls 		/*
    144  1.4.2.2  tls 		 * XXX
    145  1.4.2.2  tls 		 * If MAXBSIZE is the largest transfer the disks can handle,
    146  1.4.2.2  tls 		 * we probably want maxrun to be 1 block less so that we
    147  1.4.2.2  tls 		 * don't create a block larger than the device can handle.
    148  1.4.2.2  tls 		 */
    149  1.4.2.2  tls 		*runp = 0;
    150  1.4.2.2  tls 		maxrun = MAXPHYS / mp->mnt_stat.f_iosize - 1;
    151  1.4.2.2  tls 	}
    152  1.4.2.2  tls 
    153  1.4.2.2  tls 	if (bn >= 0 && bn < ULFS_NDADDR) {
    154  1.4.2.2  tls 		if (nump != NULL)
    155  1.4.2.2  tls 			*nump = 0;
    156  1.4.2.2  tls 		if (ump->um_fstype == ULFS1)
    157  1.4.2.2  tls 			daddr = ulfs_rw32(ip->i_ffs1_db[bn],
    158  1.4.2.3  tls 			    ULFS_MPNEEDSWAP(fs));
    159  1.4.2.2  tls 		else
    160  1.4.2.2  tls 			daddr = ulfs_rw64(ip->i_ffs2_db[bn],
    161  1.4.2.3  tls 			    ULFS_MPNEEDSWAP(fs));
    162  1.4.2.3  tls 		*bnp = blkptrtodb(fs, daddr);
    163  1.4.2.2  tls 		/*
    164  1.4.2.2  tls 		 * Since this is FFS independent code, we are out of
    165  1.4.2.2  tls 		 * scope for the definitions of BLK_NOCOPY and
    166  1.4.2.2  tls 		 * BLK_SNAP, but we do know that they will fall in
    167  1.4.2.2  tls 		 * the range 1..um_seqinc, so we use that test and
    168  1.4.2.2  tls 		 * return a request for a zeroed out buffer if attempts
    169  1.4.2.2  tls 		 * are made to read a BLK_NOCOPY or BLK_SNAP block.
    170  1.4.2.2  tls 		 */
    171  1.4.2.2  tls 		if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) == SF_SNAPSHOT
    172  1.4.2.2  tls 		    && daddr > 0 &&
    173  1.4.2.3  tls 		    daddr < fs->um_seqinc) {
    174  1.4.2.2  tls 			*bnp = -1;
    175  1.4.2.2  tls 		} else if (*bnp == 0) {
    176  1.4.2.2  tls 			if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))
    177  1.4.2.2  tls 			    == SF_SNAPSHOT) {
    178  1.4.2.3  tls 				*bnp = blkptrtodb(fs, bn * fs->um_seqinc);
    179  1.4.2.2  tls 			} else {
    180  1.4.2.2  tls 				*bnp = -1;
    181  1.4.2.2  tls 			}
    182  1.4.2.2  tls 		} else if (runp) {
    183  1.4.2.2  tls 			if (ump->um_fstype == ULFS1) {
    184  1.4.2.2  tls 				for (++bn; bn < ULFS_NDADDR && *runp < maxrun &&
    185  1.4.2.3  tls 				    is_sequential(fs,
    186  1.4.2.2  tls 				        ulfs_rw32(ip->i_ffs1_db[bn - 1],
    187  1.4.2.3  tls 				            ULFS_MPNEEDSWAP(fs)),
    188  1.4.2.2  tls 				        ulfs_rw32(ip->i_ffs1_db[bn],
    189  1.4.2.3  tls 				            ULFS_MPNEEDSWAP(fs)));
    190  1.4.2.2  tls 				    ++bn, ++*runp);
    191  1.4.2.2  tls 			} else {
    192  1.4.2.2  tls 				for (++bn; bn < ULFS_NDADDR && *runp < maxrun &&
    193  1.4.2.3  tls 				    is_sequential(fs,
    194  1.4.2.2  tls 				        ulfs_rw64(ip->i_ffs2_db[bn - 1],
    195  1.4.2.3  tls 				            ULFS_MPNEEDSWAP(fs)),
    196  1.4.2.2  tls 				        ulfs_rw64(ip->i_ffs2_db[bn],
    197  1.4.2.3  tls 				            ULFS_MPNEEDSWAP(fs)));
    198  1.4.2.2  tls 				    ++bn, ++*runp);
    199  1.4.2.2  tls 			}
    200  1.4.2.2  tls 		}
    201  1.4.2.2  tls 		return (0);
    202  1.4.2.2  tls 	}
    203  1.4.2.2  tls 
    204  1.4.2.2  tls 	xap = ap == NULL ? a : ap;
    205  1.4.2.2  tls 	if (!nump)
    206  1.4.2.2  tls 		nump = &num;
    207  1.4.2.2  tls 	if ((error = ulfs_getlbns(vp, bn, xap, nump)) != 0)
    208  1.4.2.2  tls 		return (error);
    209  1.4.2.2  tls 
    210  1.4.2.2  tls 	num = *nump;
    211  1.4.2.2  tls 
    212  1.4.2.2  tls 	/* Get disk address out of indirect block array */
    213  1.4.2.2  tls 	if (ump->um_fstype == ULFS1)
    214  1.4.2.2  tls 		daddr = ulfs_rw32(ip->i_ffs1_ib[xap->in_off],
    215  1.4.2.3  tls 		    ULFS_MPNEEDSWAP(fs));
    216  1.4.2.2  tls 	else
    217  1.4.2.2  tls 		daddr = ulfs_rw64(ip->i_ffs2_ib[xap->in_off],
    218  1.4.2.3  tls 		    ULFS_MPNEEDSWAP(fs));
    219  1.4.2.2  tls 
    220  1.4.2.2  tls 	for (bp = NULL, ++xap; --num; ++xap) {
    221  1.4.2.2  tls 		/*
    222  1.4.2.2  tls 		 * Exit the loop if there is no disk address assigned yet and
    223  1.4.2.2  tls 		 * the indirect block isn't in the cache, or if we were
    224  1.4.2.2  tls 		 * looking for an indirect block and we've found it.
    225  1.4.2.2  tls 		 */
    226  1.4.2.2  tls 
    227  1.4.2.2  tls 		metalbn = xap->in_lbn;
    228  1.4.2.2  tls 		if (metalbn == bn)
    229  1.4.2.2  tls 			break;
    230  1.4.2.2  tls 		if (daddr == 0) {
    231  1.4.2.2  tls 			mutex_enter(&bufcache_lock);
    232  1.4.2.2  tls 			cbp = incore(vp, metalbn);
    233  1.4.2.2  tls 			mutex_exit(&bufcache_lock);
    234  1.4.2.2  tls 			if (cbp == NULL)
    235  1.4.2.2  tls 				break;
    236  1.4.2.2  tls 		}
    237  1.4.2.2  tls 
    238  1.4.2.2  tls 		/*
    239  1.4.2.2  tls 		 * If we get here, we've either got the block in the cache
    240  1.4.2.2  tls 		 * or we have a disk address for it, go fetch it.
    241  1.4.2.2  tls 		 */
    242  1.4.2.2  tls 		if (bp)
    243  1.4.2.2  tls 			brelse(bp, 0);
    244  1.4.2.2  tls 
    245  1.4.2.2  tls 		xap->in_exists = 1;
    246  1.4.2.2  tls 		bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
    247  1.4.2.2  tls 		if (bp == NULL) {
    248  1.4.2.2  tls 
    249  1.4.2.2  tls 			/*
    250  1.4.2.2  tls 			 * getblk() above returns NULL only iff we are
    251  1.4.2.2  tls 			 * pagedaemon.  See the implementation of getblk
    252  1.4.2.2  tls 			 * for detail.
    253  1.4.2.2  tls 			 */
    254  1.4.2.2  tls 
    255  1.4.2.2  tls 			return (ENOMEM);
    256  1.4.2.2  tls 		}
    257  1.4.2.2  tls 		if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
    258  1.4.2.2  tls 			trace(TR_BREADHIT, pack(vp, size), metalbn);
    259  1.4.2.2  tls 		}
    260  1.4.2.2  tls #ifdef DIAGNOSTIC
    261  1.4.2.2  tls 		else if (!daddr)
    262  1.4.2.2  tls 			panic("ulfs_bmaparray: indirect block not in cache");
    263  1.4.2.2  tls #endif
    264  1.4.2.2  tls 		else {
    265  1.4.2.2  tls 			trace(TR_BREADMISS, pack(vp, size), metalbn);
    266  1.4.2.3  tls 			bp->b_blkno = blkptrtodb(fs, daddr);
    267  1.4.2.2  tls 			bp->b_flags |= B_READ;
    268  1.4.2.2  tls 			BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
    269  1.4.2.2  tls 			VOP_STRATEGY(vp, bp);
    270  1.4.2.2  tls 			curlwp->l_ru.ru_inblock++;	/* XXX */
    271  1.4.2.2  tls 			if ((error = biowait(bp)) != 0) {
    272  1.4.2.2  tls 				brelse(bp, 0);
    273  1.4.2.2  tls 				return (error);
    274  1.4.2.2  tls 			}
    275  1.4.2.2  tls 		}
    276  1.4.2.2  tls 		if (ump->um_fstype == ULFS1) {
    277  1.4.2.2  tls 			daddr = ulfs_rw32(((u_int32_t *)bp->b_data)[xap->in_off],
    278  1.4.2.3  tls 			    ULFS_MPNEEDSWAP(fs));
    279  1.4.2.2  tls 			if (num == 1 && daddr && runp) {
    280  1.4.2.2  tls 				for (bn = xap->in_off + 1;
    281  1.4.2.3  tls 				    bn < MNINDIR(fs) && *runp < maxrun &&
    282  1.4.2.3  tls 				    is_sequential(fs,
    283  1.4.2.2  tls 				        ulfs_rw32(((int32_t *)bp->b_data)[bn-1],
    284  1.4.2.3  tls 				            ULFS_MPNEEDSWAP(fs)),
    285  1.4.2.2  tls 				        ulfs_rw32(((int32_t *)bp->b_data)[bn],
    286  1.4.2.3  tls 				            ULFS_MPNEEDSWAP(fs)));
    287  1.4.2.2  tls 				    ++bn, ++*runp);
    288  1.4.2.2  tls 			}
    289  1.4.2.2  tls 		} else {
    290  1.4.2.2  tls 			daddr = ulfs_rw64(((u_int64_t *)bp->b_data)[xap->in_off],
    291  1.4.2.3  tls 			    ULFS_MPNEEDSWAP(fs));
    292  1.4.2.2  tls 			if (num == 1 && daddr && runp) {
    293  1.4.2.2  tls 				for (bn = xap->in_off + 1;
    294  1.4.2.3  tls 				    bn < MNINDIR(fs) && *runp < maxrun &&
    295  1.4.2.3  tls 				    is_sequential(fs,
    296  1.4.2.2  tls 				        ulfs_rw64(((int64_t *)bp->b_data)[bn-1],
    297  1.4.2.3  tls 				            ULFS_MPNEEDSWAP(fs)),
    298  1.4.2.2  tls 				        ulfs_rw64(((int64_t *)bp->b_data)[bn],
    299  1.4.2.3  tls 				            ULFS_MPNEEDSWAP(fs)));
    300  1.4.2.2  tls 				    ++bn, ++*runp);
    301  1.4.2.2  tls 			}
    302  1.4.2.2  tls 		}
    303  1.4.2.2  tls 	}
    304  1.4.2.2  tls 	if (bp)
    305  1.4.2.2  tls 		brelse(bp, 0);
    306  1.4.2.2  tls 
    307  1.4.2.2  tls 	/*
    308  1.4.2.2  tls 	 * Since this is FFS independent code, we are out of scope for the
    309  1.4.2.2  tls 	 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
    310  1.4.2.2  tls 	 * will fall in the range 1..um_seqinc, so we use that test and
    311  1.4.2.2  tls 	 * return a request for a zeroed out buffer if attempts are made
    312  1.4.2.2  tls 	 * to read a BLK_NOCOPY or BLK_SNAP block.
    313  1.4.2.2  tls 	 */
    314  1.4.2.2  tls 	if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) == SF_SNAPSHOT
    315  1.4.2.3  tls 	    && daddr > 0 && daddr < fs->um_seqinc) {
    316  1.4.2.2  tls 		*bnp = -1;
    317  1.4.2.2  tls 		return (0);
    318  1.4.2.2  tls 	}
    319  1.4.2.3  tls 	*bnp = blkptrtodb(fs, daddr);
    320  1.4.2.2  tls 	if (*bnp == 0) {
    321  1.4.2.2  tls 		if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))
    322  1.4.2.2  tls 		    == SF_SNAPSHOT) {
    323  1.4.2.3  tls 			*bnp = blkptrtodb(fs, bn * fs->um_seqinc);
    324  1.4.2.2  tls 		} else {
    325  1.4.2.2  tls 			*bnp = -1;
    326  1.4.2.2  tls 		}
    327  1.4.2.2  tls 	}
    328  1.4.2.2  tls 	return (0);
    329  1.4.2.2  tls }
    330  1.4.2.2  tls 
    331  1.4.2.2  tls /*
    332  1.4.2.2  tls  * Create an array of logical block number/offset pairs which represent the
    333  1.4.2.2  tls  * path of indirect blocks required to access a data block.  The first "pair"
    334  1.4.2.2  tls  * contains the logical block number of the appropriate single, double or
    335  1.4.2.2  tls  * triple indirect block and the offset into the inode indirect block array.
    336  1.4.2.2  tls  * Note, the logical block number of the inode single/double/triple indirect
    337  1.4.2.2  tls  * block appears twice in the array, once with the offset into the i_ffs1_ib and
    338  1.4.2.2  tls  * once with the offset into the page itself.
    339  1.4.2.2  tls  */
    340  1.4.2.2  tls int
    341  1.4.2.2  tls ulfs_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
    342  1.4.2.2  tls {
    343  1.4.2.2  tls 	daddr_t metalbn, realbn;
    344  1.4.2.2  tls 	struct ulfsmount *ump;
    345  1.4.2.3  tls 	struct lfs *fs;
    346  1.4.2.2  tls 	int64_t blockcnt;
    347  1.4.2.2  tls 	int lbc;
    348  1.4.2.2  tls 	int i, numlevels, off;
    349  1.4.2.2  tls 
    350  1.4.2.2  tls 	ump = VFSTOULFS(vp->v_mount);
    351  1.4.2.3  tls 	fs = ump->um_lfs;
    352  1.4.2.2  tls 	if (nump)
    353  1.4.2.2  tls 		*nump = 0;
    354  1.4.2.2  tls 	numlevels = 0;
    355  1.4.2.2  tls 	realbn = bn;
    356  1.4.2.2  tls 	if (bn < 0)
    357  1.4.2.2  tls 		bn = -bn;
    358  1.4.2.2  tls 	KASSERT(bn >= ULFS_NDADDR);
    359  1.4.2.2  tls 
    360  1.4.2.2  tls 	/*
    361  1.4.2.2  tls 	 * Determine the number of levels of indirection.  After this loop
    362  1.4.2.2  tls 	 * is done, blockcnt indicates the number of data blocks possible
    363  1.4.2.2  tls 	 * at the given level of indirection, and ULFS_NIADDR - i is the number
    364  1.4.2.2  tls 	 * of levels of indirection needed to locate the requested block.
    365  1.4.2.2  tls 	 */
    366  1.4.2.2  tls 
    367  1.4.2.2  tls 	bn -= ULFS_NDADDR;
    368  1.4.2.2  tls 	for (lbc = 0, i = ULFS_NIADDR;; i--, bn -= blockcnt) {
    369  1.4.2.2  tls 		if (i == 0)
    370  1.4.2.2  tls 			return (EFBIG);
    371  1.4.2.2  tls 
    372  1.4.2.3  tls 		lbc += fs->um_lognindir;
    373  1.4.2.2  tls 		blockcnt = (int64_t)1 << lbc;
    374  1.4.2.2  tls 
    375  1.4.2.2  tls 		if (bn < blockcnt)
    376  1.4.2.2  tls 			break;
    377  1.4.2.2  tls 	}
    378  1.4.2.2  tls 
    379  1.4.2.2  tls 	/* Calculate the address of the first meta-block. */
    380  1.4.2.2  tls 	metalbn = -((realbn >= 0 ? realbn : -realbn) - bn + ULFS_NIADDR - i);
    381  1.4.2.2  tls 
    382  1.4.2.2  tls 	/*
    383  1.4.2.2  tls 	 * At each iteration, off is the offset into the bap array which is
    384  1.4.2.2  tls 	 * an array of disk addresses at the current level of indirection.
    385  1.4.2.2  tls 	 * The logical block number and the offset in that block are stored
    386  1.4.2.2  tls 	 * into the argument array.
    387  1.4.2.2  tls 	 */
    388  1.4.2.2  tls 	ap->in_lbn = metalbn;
    389  1.4.2.2  tls 	ap->in_off = off = ULFS_NIADDR - i;
    390  1.4.2.2  tls 	ap->in_exists = 0;
    391  1.4.2.2  tls 	ap++;
    392  1.4.2.2  tls 	for (++numlevels; i <= ULFS_NIADDR; i++) {
    393  1.4.2.2  tls 		/* If searching for a meta-data block, quit when found. */
    394  1.4.2.2  tls 		if (metalbn == realbn)
    395  1.4.2.2  tls 			break;
    396  1.4.2.2  tls 
    397  1.4.2.3  tls 		lbc -= fs->um_lognindir;
    398  1.4.2.3  tls 		off = (bn >> lbc) & (MNINDIR(fs) - 1);
    399  1.4.2.2  tls 
    400  1.4.2.2  tls 		++numlevels;
    401  1.4.2.2  tls 		ap->in_lbn = metalbn;
    402  1.4.2.2  tls 		ap->in_off = off;
    403  1.4.2.2  tls 		ap->in_exists = 0;
    404  1.4.2.2  tls 		++ap;
    405  1.4.2.2  tls 
    406  1.4.2.2  tls 		metalbn -= -1 + ((int64_t)off << lbc);
    407  1.4.2.2  tls 	}
    408  1.4.2.2  tls 	if (nump)
    409  1.4.2.2  tls 		*nump = numlevels;
    410  1.4.2.2  tls 	return (0);
    411  1.4.2.2  tls }
    412