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