1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/systm.h>
38 #include <sys/bio.h>
39 #include <sys/buf.h>
40 #include <sys/proc.h>
41 #include <sys/rwlock.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
44 #include <sys/racct.h>
45 #include <sys/resourcevar.h>
46 #include <sys/sched.h>
47 #include <sys/sf_buf.h>
48 #include <sys/stat.h>
49
50 #include <vm/vm.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vnode_pager.h>
54
55 #include <ufs/ufs/extattr.h>
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/inode.h>
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/ufs_extern.h>
60
61 static ufs_lbn_t lbn_count(struct ufsmount *, int);
62 static int readindir(struct vnode *, ufs_lbn_t, ufs2_daddr_t, bool,
63 struct buf **);
64
65 static int ufs_bmap_use_unmapped = 1;
66
67 SYSCTL_INT(_vfs_ufs, OID_AUTO, bmap_use_unmapped, CTLFLAG_RWTUN,
68 &ufs_bmap_use_unmapped, 0, "UFS bmap uses unmapped bufs");
69
70 /*
71 * Bmap converts the logical block number of a file to its physical block
72 * number on the disk. The conversion is done by using the logical block
73 * number to index into the array of block pointers described by the dinode.
74 */
75 int
ufs_bmap(struct vop_bmap_args * ap)76 ufs_bmap(
77 struct vop_bmap_args /* {
78 struct vnode *a_vp;
79 daddr_t a_bn;
80 struct bufobj **a_bop;
81 daddr_t *a_bnp;
82 int *a_runp;
83 int *a_runb;
84 } */ *ap)
85 {
86 ufs2_daddr_t blkno;
87 int error;
88
89 /*
90 * Check for underlying vnode requests and ensure that logical
91 * to physical mapping is requested.
92 */
93 if (ap->a_bop != NULL)
94 *ap->a_bop = &VFSTOUFS(ap->a_vp->v_mount)->um_devvp->v_bufobj;
95 if (ap->a_bnp == NULL)
96 return (0);
97
98 error = ufs_bmaparray(ap->a_vp, ap->a_bn, &blkno, NULL,
99 ap->a_runp, ap->a_runb);
100 *ap->a_bnp = blkno;
101 return (error);
102 }
103
104 static int
readindir(struct vnode * vp,ufs_lbn_t lbn,ufs2_daddr_t daddr,bool allow_unmapped,struct buf ** bpp)105 readindir(struct vnode *vp,
106 ufs_lbn_t lbn,
107 ufs2_daddr_t daddr,
108 bool allow_unmapped,
109 struct buf **bpp)
110 {
111 struct buf *bp;
112 struct mount *mp;
113 struct ufsmount *ump;
114 struct inode *ip;
115 int error, gbflags;
116
117 mp = vp->v_mount;
118 ump = VFSTOUFS(mp);
119 ip = VTOI(vp);
120
121 gbflags = allow_unmapped && !I_IS_UFS1(ip) &&
122 ufs_bmap_use_unmapped ? GB_UNMAPPED : 0;
123 bp = getblk(vp, lbn, mp->mnt_stat.f_iosize, 0, 0, gbflags);
124 if ((bp->b_flags & B_CACHE) == 0) {
125 KASSERT(daddr != 0,
126 ("readindir: indirect block not in cache"));
127
128 bp->b_blkno = blkptrtodb(ump, daddr);
129 bp->b_iocmd = BIO_READ;
130 bp->b_flags &= ~B_INVAL;
131 bp->b_ioflags &= ~BIO_ERROR;
132 vfs_busy_pages(bp, 0);
133 bp->b_iooffset = dbtob(bp->b_blkno);
134 bstrategy(bp);
135 #ifdef RACCT
136 if (racct_enable) {
137 PROC_LOCK(curproc);
138 racct_add_buf(curproc, bp, 0);
139 PROC_UNLOCK(curproc);
140 }
141 #endif
142 curthread->td_ru.ru_inblock++;
143 error = bufwait(bp);
144 if (error != 0) {
145 brelse(bp);
146 return (error);
147 }
148 }
149 *bpp = bp;
150 return (0);
151 }
152
153 /*
154 * Indirect blocks are now on the vnode for the file. They are given negative
155 * logical block numbers. Indirect blocks are addressed by the negative
156 * address of the first data block to which they point. Double indirect blocks
157 * are addressed by one less than the address of the first indirect block to
158 * which they point. Triple indirect blocks are addressed by one less than
159 * the address of the first double indirect block to which they point.
160 *
161 * ufs_bmaparray does the bmap conversion, and if requested returns the
162 * array of logical blocks which must be traversed to get to a block.
163 * Each entry contains the offset into that block that gets you to the
164 * next block and the disk address of the block (if it is assigned).
165 */
166
167 static void *
ufs_bm_sf_get(struct buf * bp,int32_t pgidx,struct sf_buf ** sfp)168 ufs_bm_sf_get(struct buf *bp, int32_t pgidx, struct sf_buf **sfp)
169 {
170 struct sf_buf *sf;
171
172 sched_pin();
173 sf = sf_buf_alloc(bp->b_pages[pgidx], SFB_CPUPRIVATE);
174 *sfp = sf;
175 return (sf_buf_kva(sf));
176 }
177
178 static void
ufs_bm_sf_put(struct sf_buf * sf)179 ufs_bm_sf_put(struct sf_buf *sf)
180 {
181 sf_buf_free(sf);
182 sched_unpin();
183 }
184
185 int
ufs_bmaparray(struct vnode * vp,ufs2_daddr_t bn,ufs2_daddr_t * bnp,struct buf * nbp,int * runp,int * runb)186 ufs_bmaparray(struct vnode *vp,
187 ufs2_daddr_t bn,
188 ufs2_daddr_t *bnp,
189 struct buf *nbp,
190 int *runp,
191 int *runb)
192 {
193 struct inode *ip;
194 struct buf *bp;
195 struct ufsmount *ump;
196 struct mount *mp;
197 struct indir a[UFS_NIADDR+1], *ap;
198 struct sf_buf *sf;
199 ufs2_daddr_t daddr;
200 ufs_lbn_t metalbn;
201 int error, num, maxrun = 0;
202 int *nump;
203 ufs1_daddr_t *daddr1p;
204 ufs2_daddr_t pgbn, daddrppg, prevdaddr, *daddr2p;
205 int32_t daddrsz, boff, pgidx, pgoff;
206 void *pgaddr;
207 bool isseq;
208
209 ap = NULL;
210 ip = VTOI(vp);
211 mp = vp->v_mount;
212 ump = VFSTOUFS(mp);
213
214 if (runp) {
215 maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1;
216 *runp = 0;
217 }
218
219 if (runb) {
220 *runb = 0;
221 }
222
223 ap = a;
224 nump = #
225 error = ufs_getlbns(vp, bn, ap, nump);
226 if (error)
227 return (error);
228
229 num = *nump;
230 if (num == 0) {
231 if (bn >= 0 && bn < UFS_NDADDR) {
232 *bnp = blkptrtodb(ump, DIP(ip, i_db[bn]));
233 } else if (bn < 0 && bn >= -UFS_NXADDR) {
234 *bnp = blkptrtodb(ump, ip->i_din2->di_extb[-1 - bn]);
235 if (*bnp == 0)
236 *bnp = -1;
237 if (nbp == NULL) {
238 /* indirect block not found */
239 return (EINVAL);
240 }
241 nbp->b_xflags |= BX_ALTDATA;
242 return (0);
243 } else {
244 /* blkno out of range */
245 return (EINVAL);
246 }
247 /*
248 * Since this is FFS independent code, we are out of
249 * scope for the definitions of BLK_NOCOPY and
250 * BLK_SNAP, but we do know that they will fall in
251 * the range 1..um_seqinc, so we use that test and
252 * return a request for a zeroed out buffer if attempts
253 * are made to read a BLK_NOCOPY or BLK_SNAP block.
254 */
255 if (IS_SNAPSHOT(ip) && DIP(ip, i_db[bn]) > 0 &&
256 DIP(ip, i_db[bn]) < ump->um_seqinc) {
257 *bnp = -1;
258 } else if (*bnp == 0) {
259 *bnp = IS_SNAPSHOT(ip) ? blkptrtodb(ump,
260 bn * ump->um_seqinc) : -1;
261 } else if (runp) {
262 ufs2_daddr_t bnb = bn;
263 for (++bn; bn < UFS_NDADDR && *runp < maxrun &&
264 is_sequential(ump, DIP(ip, i_db[bn - 1]),
265 DIP(ip, i_db[bn]));
266 ++bn, ++*runp);
267 bn = bnb;
268 if (runb && (bn > 0)) {
269 for (--bn; (bn >= 0) && (*runb < maxrun) &&
270 is_sequential(ump, DIP(ip, i_db[bn]),
271 DIP(ip, i_db[bn+1]));
272 --bn, ++*runb);
273 }
274 }
275 return (0);
276 }
277
278 /* Get disk address out of indirect block array */
279 daddr = DIP(ip, i_ib[ap->in_off]);
280
281 for (bp = NULL, ++ap; --num; ++ap) {
282 /*
283 * Exit the loop if there is no disk address assigned yet and
284 * the indirect block isn't in the cache, or if we were
285 * looking for an indirect block and we've found it.
286 */
287
288 metalbn = ap->in_lbn;
289 if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
290 break;
291 /*
292 * If we get here, we've either got the block in the cache
293 * or we have a disk address for it, go fetch it.
294 */
295 if (bp)
296 bqrelse(bp);
297 error = readindir(vp, metalbn, daddr, true, &bp);
298 if (error != 0)
299 return (error);
300
301 daddrsz = I_IS_UFS1(ip) ? sizeof(ufs1_daddr_t) : sizeof(ufs2_daddr_t);
302 if (!buf_mapped(bp)) {
303 boff = ap->in_off * daddrsz;
304 pgidx = boff / PAGE_SIZE;
305 pgoff = (boff & PAGE_MASK) / daddrsz;
306 pgaddr = ufs_bm_sf_get(bp, pgidx, &sf);
307 if (I_IS_UFS1(ip))
308 daddr = ((ufs1_daddr_t *)pgaddr)[pgoff];
309 else
310 daddr = ((ufs2_daddr_t *)pgaddr)[pgoff];
311 ufs_bm_sf_put(sf);
312 } else {
313 if (I_IS_UFS1(ip))
314 daddr = ((ufs1_daddr_t *)bp->b_data)[ap->in_off];
315 else
316 daddr = ((ufs2_daddr_t *)bp->b_data)[ap->in_off];
317 }
318
319 if ((error = UFS_CHECK_BLKNO(mp, ip->i_number, daddr,
320 mp->mnt_stat.f_iosize)) != 0) {
321 bqrelse(bp);
322 return (error);
323 }
324 if (num > 1 || daddr == 0 || runp == NULL)
325 continue;
326
327 daddrppg = PAGE_SIZE / daddrsz;
328 if (I_IS_UFS1(ip)) {
329 if (!buf_mapped(bp)) {
330 prevdaddr = daddr;
331 isseq = true;
332 for (bn = ap->in_off + 1;
333 bn < MNINDIR(ump) && *runp < maxrun && isseq; ) {
334 boff = bn * daddrsz;
335 pgidx = boff / PAGE_SIZE;
336 pgoff = (boff & PAGE_MASK) / daddrsz;
337 KASSERT(pgidx >= 0 && pgidx < bp->b_npages,
338 ("pgidx %d vs b_npages %d", pgidx, bp->b_npages));
339 pgaddr = ufs_bm_sf_get(bp, pgidx, &sf);
340 daddr1p = (ufs1_daddr_t *)pgaddr;
341 for (pgbn = pgoff;
342 pgbn < daddrppg && *runp < maxrun &&
343 (isseq = is_sequential(ump, prevdaddr, daddr1p[pgbn]));
344 prevdaddr = daddr1p[pgbn], ++pgbn, ++bn, ++*runp);
345 ufs_bm_sf_put(sf);
346 }
347 prevdaddr = daddr;
348 bn = ap->in_off;
349 if (runb && bn) {
350 isseq = true;
351 for (--bn; bn >= 0 && *runb < maxrun && isseq; ) {
352 boff = bn * daddrsz;
353 pgidx = boff / PAGE_SIZE;
354 pgoff = (boff & PAGE_MASK) / daddrsz;
355 KASSERT(pgidx >= 0 && pgidx < bp->b_npages,
356 ("pgidx %d vs b_npages %d", pgidx, bp->b_npages));
357 pgaddr = ufs_bm_sf_get(bp, pgidx, &sf);
358 daddr1p = (ufs1_daddr_t *)pgaddr;
359 for (pgbn = pgoff; pgbn >= 0 && *runb < maxrun &&
360 (isseq = is_sequential(ump, daddr1p[pgbn], prevdaddr));
361 prevdaddr = daddr1p[pgbn], --pgbn, --bn, ++*runb);
362 ufs_bm_sf_put(sf);
363 }
364 }
365 } else {
366 for (bn = ap->in_off + 1;
367 bn < MNINDIR(ump) && *runp < maxrun &&
368 is_sequential(ump,
369 ((ufs1_daddr_t *)bp->b_data)[bn - 1],
370 ((ufs1_daddr_t *)bp->b_data)[bn]);
371 ++bn, ++*runp);
372 bn = ap->in_off;
373 if (runb && bn) {
374 for (--bn; bn >= 0 && *runb < maxrun &&
375 is_sequential(ump,
376 ((ufs1_daddr_t *)bp->b_data)[bn],
377 ((ufs1_daddr_t *)bp->b_data)[bn+1]);
378 --bn, ++*runb);
379 }
380 }
381 continue;
382 }
383
384 if (!buf_mapped(bp)) {
385 prevdaddr = daddr;
386 isseq = true;
387 for (bn = ap->in_off + 1;
388 bn < MNINDIR(ump) && *runp < maxrun && isseq; ) {
389 boff = bn * daddrsz;
390 pgidx = boff / PAGE_SIZE;
391 pgoff = (boff & PAGE_MASK) / daddrsz;
392 KASSERT(pgidx >= 0 && pgidx < bp->b_npages,
393 ("pgidx %d vs b_npages %d", pgidx, bp->b_npages));
394 pgaddr = ufs_bm_sf_get(bp, pgidx, &sf);
395 daddr2p = (ufs2_daddr_t *)pgaddr;
396 for (pgbn = pgoff;
397 pgbn < daddrppg && *runp < maxrun &&
398 (isseq = is_sequential(ump, prevdaddr, daddr2p[pgbn]));
399 prevdaddr = daddr2p[pgbn], ++pgbn, ++bn, ++*runp);
400 ufs_bm_sf_put(sf);
401 }
402 prevdaddr = daddr;
403 bn = ap->in_off;
404 if (runb && bn) {
405 isseq = true;
406 for (--bn; bn >= 0 && *runb < maxrun && isseq; ) {
407 boff = bn * daddrsz;
408 pgidx = boff / PAGE_SIZE;
409 pgoff = (boff & PAGE_MASK) / daddrsz;
410 KASSERT(pgidx >= 0 && pgidx < bp->b_npages,
411 ("pgidx %d vs b_npages %d", pgidx, bp->b_npages));
412 pgaddr = ufs_bm_sf_get(bp, pgidx, &sf);
413 daddr2p = (ufs2_daddr_t *)pgaddr;
414 for (pgbn = pgoff; pgbn >= 0 && *runb < maxrun &&
415 (isseq = is_sequential(ump, daddr2p[pgbn], prevdaddr));
416 prevdaddr = daddr2p[pgbn], --pgbn, --bn, ++*runb);
417 ufs_bm_sf_put(sf);
418 }
419 }
420 } else {
421 for (bn = ap->in_off + 1;
422 bn < MNINDIR(ump) && *runp < maxrun &&
423 is_sequential(ump,
424 ((ufs2_daddr_t *)bp->b_data)[bn - 1],
425 ((ufs2_daddr_t *)bp->b_data)[bn]);
426 ++bn, ++*runp);
427 bn = ap->in_off;
428 if (runb && bn) {
429 for (--bn; bn >= 0 && *runb < maxrun &&
430 is_sequential(ump,
431 ((ufs2_daddr_t *)bp->b_data)[bn],
432 ((ufs2_daddr_t *)bp->b_data)[bn + 1]);
433 --bn, ++*runb);
434 }
435 }
436 }
437 if (bp)
438 bqrelse(bp);
439
440 /*
441 * Since this is FFS independent code, we are out of scope for the
442 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
443 * will fall in the range 1..um_seqinc, so we use that test and
444 * return a request for a zeroed out buffer if attempts are made
445 * to read a BLK_NOCOPY or BLK_SNAP block.
446 */
447 if (IS_SNAPSHOT(ip) && daddr > 0 && daddr < ump->um_seqinc){
448 *bnp = -1;
449 return (0);
450 }
451 *bnp = blkptrtodb(ump, daddr);
452 if (*bnp == 0) {
453 if (IS_SNAPSHOT(ip))
454 *bnp = blkptrtodb(ump, bn * ump->um_seqinc);
455 else
456 *bnp = -1;
457 }
458 return (0);
459 }
460
461 static ufs_lbn_t
lbn_count(struct ufsmount * ump,int level)462 lbn_count(struct ufsmount *ump, int level)
463 {
464 ufs_lbn_t blockcnt;
465
466 for (blockcnt = 1; level > 0; level--)
467 blockcnt *= MNINDIR(ump);
468 return (blockcnt);
469 }
470
471 int
ufs_bmap_seekdata(struct vnode * vp,off_t * offp)472 ufs_bmap_seekdata(struct vnode *vp, off_t *offp)
473 {
474 struct buf *bp;
475 struct indir a[UFS_NIADDR + 1], *ap;
476 struct inode *ip;
477 struct mount *mp;
478 struct ufsmount *ump;
479 ufs2_daddr_t bn, daddr, nextbn;
480 uint64_t bsize;
481 off_t numblks;
482 int error, num, num1, off;
483
484 bp = NULL;
485 error = 0;
486 ip = VTOI(vp);
487 mp = vp->v_mount;
488 ump = VFSTOUFS(mp);
489
490 if (vp->v_type != VREG || IS_SNAPSHOT(ip))
491 return (EINVAL);
492 if (*offp < 0 || *offp >= ip->i_size)
493 return (ENXIO);
494
495 /*
496 * We could have pages on the vnode' object queue which still
497 * do not have the data blocks allocated. Convert all dirty
498 * pages into buffer writes to ensure that we see all
499 * allocated data.
500 */
501 vnode_pager_clean_sync(vp);
502
503 bsize = mp->mnt_stat.f_iosize;
504 for (bn = *offp / bsize, numblks = howmany(ip->i_size, bsize);
505 bn < numblks; bn = nextbn) {
506 if (bn < UFS_NDADDR) {
507 daddr = DIP(ip, i_db[bn]);
508 if (daddr != 0)
509 break;
510 nextbn = bn + 1;
511 continue;
512 }
513
514 ap = a;
515 error = ufs_getlbns(vp, bn, ap, &num);
516 if (error != 0)
517 break;
518 MPASS(num >= 2);
519 daddr = DIP(ip, i_ib[ap->in_off]);
520 ap++, num--;
521 for (nextbn = UFS_NDADDR, num1 = num - 1; num1 > 0; num1--)
522 nextbn += lbn_count(ump, num1);
523 if (daddr == 0) {
524 nextbn += lbn_count(ump, num);
525 continue;
526 }
527
528 for (; daddr != 0 && num > 0; ap++, num--) {
529 if (bp != NULL)
530 bqrelse(bp);
531 error = readindir(vp, ap->in_lbn, daddr, false, &bp);
532 if (error != 0)
533 return (error);
534
535 /*
536 * Scan the indirect block until we find a non-zero
537 * pointer.
538 */
539 off = ap->in_off;
540 do {
541 daddr = I_IS_UFS1(ip) ?
542 ((ufs1_daddr_t *)bp->b_data)[off] :
543 ((ufs2_daddr_t *)bp->b_data)[off];
544 } while (daddr == 0 && ++off < MNINDIR(ump));
545 nextbn += off * lbn_count(ump, num - 1);
546
547 /*
548 * We need to recompute the LBNs of indirect
549 * blocks, so restart with the updated block offset.
550 */
551 if (off != ap->in_off)
552 break;
553 }
554 if (num == 0) {
555 /*
556 * We found a data block.
557 */
558 bn = nextbn;
559 break;
560 }
561 }
562 if (bp != NULL)
563 bqrelse(bp);
564 if (bn >= numblks)
565 error = ENXIO;
566 if (error == 0 && *offp < bn * bsize)
567 *offp = bn * bsize;
568 return (error);
569 }
570
571 /*
572 * Create an array of logical block number/offset pairs which represent the
573 * path of indirect blocks required to access a data block. The first "pair"
574 * contains the logical block number of the appropriate single, double or
575 * triple indirect block and the offset into the inode indirect block array.
576 * Note, the logical block number of the inode single/double/triple indirect
577 * block appears twice in the array, once with the offset into the i_ib and
578 * once with the offset into the page itself.
579 */
580 int
ufs_getlbns(struct vnode * vp,ufs2_daddr_t bn,struct indir * ap,int * nump)581 ufs_getlbns(struct vnode *vp,
582 ufs2_daddr_t bn,
583 struct indir *ap,
584 int *nump)
585 {
586 ufs2_daddr_t blockcnt;
587 ufs_lbn_t metalbn, realbn;
588 struct ufsmount *ump;
589 int i, numlevels, off;
590
591 ump = VFSTOUFS(vp->v_mount);
592 if (nump)
593 *nump = 0;
594 numlevels = 0;
595 realbn = bn;
596 if (bn < 0)
597 bn = -bn;
598
599 /* The first UFS_NDADDR blocks are direct blocks. */
600 if (bn < UFS_NDADDR)
601 return (0);
602
603 /*
604 * Determine the number of levels of indirection. After this loop
605 * is done, blockcnt indicates the number of data blocks possible
606 * at the previous level of indirection, and UFS_NIADDR - i is the
607 * number of levels of indirection needed to locate the requested block.
608 */
609 for (blockcnt = 1, i = UFS_NIADDR, bn -= UFS_NDADDR; ;
610 i--, bn -= blockcnt) {
611 if (i == 0)
612 return (EFBIG);
613 blockcnt *= MNINDIR(ump);
614 if (bn < blockcnt)
615 break;
616 }
617
618 /* Calculate the address of the first meta-block. */
619 if (realbn >= 0)
620 metalbn = -(realbn - bn + UFS_NIADDR - i);
621 else
622 metalbn = -(-realbn - bn + UFS_NIADDR - i);
623
624 /*
625 * At each iteration, off is the offset into the bap array which is
626 * an array of disk addresses at the current level of indirection.
627 * The logical block number and the offset in that block are stored
628 * into the argument array.
629 */
630 ap->in_lbn = metalbn;
631 ap->in_off = off = UFS_NIADDR - i;
632 ap++;
633 for (++numlevels; i <= UFS_NIADDR; i++) {
634 /* If searching for a meta-data block, quit when found. */
635 if (metalbn == realbn)
636 break;
637
638 blockcnt /= MNINDIR(ump);
639 off = (bn / blockcnt) % MNINDIR(ump);
640
641 ++numlevels;
642 ap->in_lbn = metalbn;
643 ap->in_off = off;
644 ++ap;
645
646 metalbn -= -1 + off * blockcnt;
647 }
648 if (nump)
649 *nump = numlevels;
650 return (0);
651 }
652