ulfs_bmap.c revision 1.4.2.4 1 1.4.2.2 tls /* $NetBSD: ulfs_bmap.c,v 1.4.2.4 2017/12/03 11:39:22 jdolecek 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.4 2017/12/03 11:39:22 jdolecek 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
53 1.4.2.2 tls #include <miscfs/specfs/specdev.h>
54 1.4.2.2 tls
55 1.4.2.2 tls #include <ufs/lfs/ulfs_inode.h>
56 1.4.2.2 tls #include <ufs/lfs/ulfsmount.h>
57 1.4.2.2 tls #include <ufs/lfs/ulfs_extern.h>
58 1.4.2.2 tls #include <ufs/lfs/ulfs_bswap.h>
59 1.4.2.2 tls
60 1.4.2.2 tls static bool
61 1.4.2.3 tls ulfs_issequential(const struct lfs *fs, daddr_t daddr0, daddr_t daddr1)
62 1.4.2.2 tls {
63 1.4.2.2 tls
64 1.4.2.2 tls /* for ulfs, blocks in a hole is not 'contiguous'. */
65 1.4.2.2 tls if (daddr0 == 0)
66 1.4.2.2 tls return false;
67 1.4.2.2 tls
68 1.4.2.3 tls return (daddr0 + fs->um_seqinc == daddr1);
69 1.4.2.2 tls }
70 1.4.2.2 tls
71 1.4.2.2 tls /*
72 1.4.2.4 jdolecek * This is used for block pointers in inodes and elsewhere, which can
73 1.4.2.4 jdolecek * contain the magic value UNWRITTEN, which is -2. This is mishandled
74 1.4.2.4 jdolecek * by u32 -> u64 promotion unless special-cased.
75 1.4.2.4 jdolecek *
76 1.4.2.4 jdolecek * XXX this should be rolled into better inode accessors and go away.
77 1.4.2.4 jdolecek */
78 1.4.2.4 jdolecek static inline uint64_t
79 1.4.2.4 jdolecek ulfs_fix_unwritten(uint32_t val)
80 1.4.2.4 jdolecek {
81 1.4.2.4 jdolecek if (val == (uint32_t)UNWRITTEN) {
82 1.4.2.4 jdolecek return (uint64_t)(int64_t)UNWRITTEN;
83 1.4.2.4 jdolecek } else {
84 1.4.2.4 jdolecek return val;
85 1.4.2.4 jdolecek }
86 1.4.2.4 jdolecek }
87 1.4.2.4 jdolecek
88 1.4.2.4 jdolecek
89 1.4.2.4 jdolecek /*
90 1.4.2.2 tls * Bmap converts the logical block number of a file to its physical block
91 1.4.2.2 tls * number on the disk. The conversion is done by using the logical block
92 1.4.2.2 tls * number to index into the array of block pointers described by the dinode.
93 1.4.2.2 tls */
94 1.4.2.2 tls int
95 1.4.2.2 tls ulfs_bmap(void *v)
96 1.4.2.2 tls {
97 1.4.2.2 tls struct vop_bmap_args /* {
98 1.4.2.2 tls struct vnode *a_vp;
99 1.4.2.2 tls daddr_t a_bn;
100 1.4.2.2 tls struct vnode **a_vpp;
101 1.4.2.2 tls daddr_t *a_bnp;
102 1.4.2.2 tls int *a_runp;
103 1.4.2.2 tls } */ *ap = v;
104 1.4.2.2 tls int error;
105 1.4.2.2 tls
106 1.4.2.2 tls /*
107 1.4.2.2 tls * Check for underlying vnode requests and ensure that logical
108 1.4.2.2 tls * to physical mapping is requested.
109 1.4.2.2 tls */
110 1.4.2.2 tls if (ap->a_vpp != NULL)
111 1.4.2.2 tls *ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
112 1.4.2.2 tls if (ap->a_bnp == NULL)
113 1.4.2.2 tls return (0);
114 1.4.2.2 tls
115 1.4.2.2 tls error = ulfs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
116 1.4.2.2 tls ap->a_runp, ulfs_issequential);
117 1.4.2.2 tls return error;
118 1.4.2.2 tls }
119 1.4.2.2 tls
120 1.4.2.2 tls /*
121 1.4.2.2 tls * Indirect blocks are now on the vnode for the file. They are given negative
122 1.4.2.2 tls * logical block numbers. Indirect blocks are addressed by the negative
123 1.4.2.2 tls * address of the first data block to which they point. Double indirect blocks
124 1.4.2.2 tls * are addressed by one less than the address of the first indirect block to
125 1.4.2.2 tls * which they point. Triple indirect blocks are addressed by one less than
126 1.4.2.2 tls * the address of the first double indirect block to which they point.
127 1.4.2.2 tls *
128 1.4.2.2 tls * ulfs_bmaparray does the bmap conversion, and if requested returns the
129 1.4.2.2 tls * array of logical blocks which must be traversed to get to a block.
130 1.4.2.2 tls * Each entry contains the offset into that block that gets you to the
131 1.4.2.2 tls * next block and the disk address of the block (if it is assigned).
132 1.4.2.2 tls */
133 1.4.2.2 tls
134 1.4.2.2 tls int
135 1.4.2.2 tls ulfs_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, struct indir *ap,
136 1.4.2.2 tls int *nump, int *runp, ulfs_issequential_callback_t is_sequential)
137 1.4.2.2 tls {
138 1.4.2.2 tls struct inode *ip;
139 1.4.2.2 tls struct buf *bp, *cbp;
140 1.4.2.2 tls struct ulfsmount *ump;
141 1.4.2.3 tls struct lfs *fs;
142 1.4.2.2 tls struct mount *mp;
143 1.4.2.2 tls struct indir a[ULFS_NIADDR + 1], *xap;
144 1.4.2.2 tls daddr_t daddr;
145 1.4.2.2 tls daddr_t metalbn;
146 1.4.2.2 tls int error, maxrun = 0, num;
147 1.4.2.2 tls
148 1.4.2.2 tls ip = VTOI(vp);
149 1.4.2.2 tls mp = vp->v_mount;
150 1.4.2.2 tls ump = ip->i_ump;
151 1.4.2.3 tls fs = ip->i_lfs;
152 1.4.2.4 jdolecek KASSERTMSG(((ap == NULL) == (nump == NULL)),
153 1.4.2.4 jdolecek "ulfs_bmaparray: invalid arguments: ap=%p, nump=%p", ap, nump);
154 1.4.2.2 tls
155 1.4.2.2 tls if (runp) {
156 1.4.2.2 tls /*
157 1.4.2.2 tls * XXX
158 1.4.2.2 tls * If MAXBSIZE is the largest transfer the disks can handle,
159 1.4.2.2 tls * we probably want maxrun to be 1 block less so that we
160 1.4.2.2 tls * don't create a block larger than the device can handle.
161 1.4.2.2 tls */
162 1.4.2.2 tls *runp = 0;
163 1.4.2.2 tls maxrun = MAXPHYS / mp->mnt_stat.f_iosize - 1;
164 1.4.2.2 tls }
165 1.4.2.2 tls
166 1.4.2.2 tls if (bn >= 0 && bn < ULFS_NDADDR) {
167 1.4.2.2 tls if (nump != NULL)
168 1.4.2.2 tls *nump = 0;
169 1.4.2.2 tls if (ump->um_fstype == ULFS1)
170 1.4.2.4 jdolecek daddr = ulfs_fix_unwritten(ulfs_rw32(ip->i_din->u_32.di_db[bn],
171 1.4.2.4 jdolecek ULFS_MPNEEDSWAP(fs)));
172 1.4.2.2 tls else
173 1.4.2.4 jdolecek daddr = ulfs_rw64(ip->i_din->u_64.di_db[bn],
174 1.4.2.3 tls ULFS_MPNEEDSWAP(fs));
175 1.4.2.3 tls *bnp = blkptrtodb(fs, daddr);
176 1.4.2.2 tls /*
177 1.4.2.2 tls * Since this is FFS independent code, we are out of
178 1.4.2.2 tls * scope for the definitions of BLK_NOCOPY and
179 1.4.2.2 tls * BLK_SNAP, but we do know that they will fall in
180 1.4.2.2 tls * the range 1..um_seqinc, so we use that test and
181 1.4.2.2 tls * return a request for a zeroed out buffer if attempts
182 1.4.2.2 tls * are made to read a BLK_NOCOPY or BLK_SNAP block.
183 1.4.2.2 tls */
184 1.4.2.2 tls if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) == SF_SNAPSHOT
185 1.4.2.2 tls && daddr > 0 &&
186 1.4.2.3 tls daddr < fs->um_seqinc) {
187 1.4.2.2 tls *bnp = -1;
188 1.4.2.2 tls } else if (*bnp == 0) {
189 1.4.2.2 tls if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))
190 1.4.2.2 tls == SF_SNAPSHOT) {
191 1.4.2.3 tls *bnp = blkptrtodb(fs, bn * fs->um_seqinc);
192 1.4.2.2 tls } else {
193 1.4.2.2 tls *bnp = -1;
194 1.4.2.2 tls }
195 1.4.2.2 tls } else if (runp) {
196 1.4.2.2 tls if (ump->um_fstype == ULFS1) {
197 1.4.2.2 tls for (++bn; bn < ULFS_NDADDR && *runp < maxrun &&
198 1.4.2.3 tls is_sequential(fs,
199 1.4.2.4 jdolecek ulfs_fix_unwritten(ulfs_rw32(ip->i_din->u_32.di_db[bn - 1],
200 1.4.2.4 jdolecek ULFS_MPNEEDSWAP(fs))),
201 1.4.2.4 jdolecek ulfs_fix_unwritten(ulfs_rw32(ip->i_din->u_32.di_db[bn],
202 1.4.2.4 jdolecek ULFS_MPNEEDSWAP(fs))));
203 1.4.2.2 tls ++bn, ++*runp);
204 1.4.2.2 tls } else {
205 1.4.2.2 tls for (++bn; bn < ULFS_NDADDR && *runp < maxrun &&
206 1.4.2.3 tls is_sequential(fs,
207 1.4.2.4 jdolecek ulfs_rw64(ip->i_din->u_64.di_db[bn - 1],
208 1.4.2.3 tls ULFS_MPNEEDSWAP(fs)),
209 1.4.2.4 jdolecek ulfs_rw64(ip->i_din->u_64.di_db[bn],
210 1.4.2.3 tls ULFS_MPNEEDSWAP(fs)));
211 1.4.2.2 tls ++bn, ++*runp);
212 1.4.2.2 tls }
213 1.4.2.2 tls }
214 1.4.2.2 tls return (0);
215 1.4.2.2 tls }
216 1.4.2.2 tls
217 1.4.2.2 tls xap = ap == NULL ? a : ap;
218 1.4.2.2 tls if (!nump)
219 1.4.2.2 tls nump = #
220 1.4.2.2 tls if ((error = ulfs_getlbns(vp, bn, xap, nump)) != 0)
221 1.4.2.2 tls return (error);
222 1.4.2.2 tls
223 1.4.2.2 tls num = *nump;
224 1.4.2.2 tls
225 1.4.2.2 tls /* Get disk address out of indirect block array */
226 1.4.2.4 jdolecek // XXX clean this up
227 1.4.2.2 tls if (ump->um_fstype == ULFS1)
228 1.4.2.4 jdolecek daddr = ulfs_fix_unwritten(ulfs_rw32(ip->i_din->u_32.di_ib[xap->in_off],
229 1.4.2.4 jdolecek ULFS_MPNEEDSWAP(fs)));
230 1.4.2.2 tls else
231 1.4.2.4 jdolecek daddr = ulfs_rw64(ip->i_din->u_64.di_ib[xap->in_off],
232 1.4.2.3 tls ULFS_MPNEEDSWAP(fs));
233 1.4.2.2 tls
234 1.4.2.2 tls for (bp = NULL, ++xap; --num; ++xap) {
235 1.4.2.2 tls /*
236 1.4.2.2 tls * Exit the loop if there is no disk address assigned yet and
237 1.4.2.2 tls * the indirect block isn't in the cache, or if we were
238 1.4.2.2 tls * looking for an indirect block and we've found it.
239 1.4.2.2 tls */
240 1.4.2.2 tls
241 1.4.2.2 tls metalbn = xap->in_lbn;
242 1.4.2.2 tls if (metalbn == bn)
243 1.4.2.2 tls break;
244 1.4.2.2 tls if (daddr == 0) {
245 1.4.2.2 tls mutex_enter(&bufcache_lock);
246 1.4.2.2 tls cbp = incore(vp, metalbn);
247 1.4.2.2 tls mutex_exit(&bufcache_lock);
248 1.4.2.2 tls if (cbp == NULL)
249 1.4.2.2 tls break;
250 1.4.2.2 tls }
251 1.4.2.2 tls
252 1.4.2.2 tls /*
253 1.4.2.2 tls * If we get here, we've either got the block in the cache
254 1.4.2.2 tls * or we have a disk address for it, go fetch it.
255 1.4.2.2 tls */
256 1.4.2.2 tls if (bp)
257 1.4.2.2 tls brelse(bp, 0);
258 1.4.2.2 tls
259 1.4.2.2 tls xap->in_exists = 1;
260 1.4.2.2 tls bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
261 1.4.2.2 tls if (bp == NULL) {
262 1.4.2.2 tls
263 1.4.2.2 tls /*
264 1.4.2.2 tls * getblk() above returns NULL only iff we are
265 1.4.2.2 tls * pagedaemon. See the implementation of getblk
266 1.4.2.2 tls * for detail.
267 1.4.2.2 tls */
268 1.4.2.2 tls
269 1.4.2.2 tls return (ENOMEM);
270 1.4.2.2 tls }
271 1.4.2.2 tls if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
272 1.4.2.2 tls trace(TR_BREADHIT, pack(vp, size), metalbn);
273 1.4.2.4 jdolecek } else {
274 1.4.2.4 jdolecek KASSERTMSG(daddr,
275 1.4.2.4 jdolecek "ulfs_bmaparray: indirect block not in cache");
276 1.4.2.2 tls trace(TR_BREADMISS, pack(vp, size), metalbn);
277 1.4.2.3 tls bp->b_blkno = blkptrtodb(fs, daddr);
278 1.4.2.2 tls bp->b_flags |= B_READ;
279 1.4.2.2 tls BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
280 1.4.2.2 tls VOP_STRATEGY(vp, bp);
281 1.4.2.2 tls curlwp->l_ru.ru_inblock++; /* XXX */
282 1.4.2.2 tls if ((error = biowait(bp)) != 0) {
283 1.4.2.2 tls brelse(bp, 0);
284 1.4.2.2 tls return (error);
285 1.4.2.2 tls }
286 1.4.2.2 tls }
287 1.4.2.2 tls if (ump->um_fstype == ULFS1) {
288 1.4.2.4 jdolecek daddr = ulfs_fix_unwritten(ulfs_rw32(((u_int32_t *)bp->b_data)[xap->in_off],
289 1.4.2.4 jdolecek ULFS_MPNEEDSWAP(fs)));
290 1.4.2.2 tls if (num == 1 && daddr && runp) {
291 1.4.2.2 tls for (bn = xap->in_off + 1;
292 1.4.2.3 tls bn < MNINDIR(fs) && *runp < maxrun &&
293 1.4.2.3 tls is_sequential(fs,
294 1.4.2.4 jdolecek ulfs_fix_unwritten(ulfs_rw32(((int32_t *)bp->b_data)[bn-1],
295 1.4.2.4 jdolecek ULFS_MPNEEDSWAP(fs))),
296 1.4.2.4 jdolecek ulfs_fix_unwritten(ulfs_rw32(((int32_t *)bp->b_data)[bn],
297 1.4.2.4 jdolecek ULFS_MPNEEDSWAP(fs))));
298 1.4.2.2 tls ++bn, ++*runp);
299 1.4.2.2 tls }
300 1.4.2.2 tls } else {
301 1.4.2.2 tls daddr = ulfs_rw64(((u_int64_t *)bp->b_data)[xap->in_off],
302 1.4.2.3 tls ULFS_MPNEEDSWAP(fs));
303 1.4.2.2 tls if (num == 1 && daddr && runp) {
304 1.4.2.2 tls for (bn = xap->in_off + 1;
305 1.4.2.3 tls bn < MNINDIR(fs) && *runp < maxrun &&
306 1.4.2.3 tls is_sequential(fs,
307 1.4.2.2 tls ulfs_rw64(((int64_t *)bp->b_data)[bn-1],
308 1.4.2.3 tls ULFS_MPNEEDSWAP(fs)),
309 1.4.2.2 tls ulfs_rw64(((int64_t *)bp->b_data)[bn],
310 1.4.2.3 tls ULFS_MPNEEDSWAP(fs)));
311 1.4.2.2 tls ++bn, ++*runp);
312 1.4.2.2 tls }
313 1.4.2.2 tls }
314 1.4.2.2 tls }
315 1.4.2.2 tls if (bp)
316 1.4.2.2 tls brelse(bp, 0);
317 1.4.2.2 tls
318 1.4.2.2 tls /*
319 1.4.2.2 tls * Since this is FFS independent code, we are out of scope for the
320 1.4.2.2 tls * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
321 1.4.2.2 tls * will fall in the range 1..um_seqinc, so we use that test and
322 1.4.2.2 tls * return a request for a zeroed out buffer if attempts are made
323 1.4.2.2 tls * to read a BLK_NOCOPY or BLK_SNAP block.
324 1.4.2.2 tls */
325 1.4.2.2 tls if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) == SF_SNAPSHOT
326 1.4.2.3 tls && daddr > 0 && daddr < fs->um_seqinc) {
327 1.4.2.2 tls *bnp = -1;
328 1.4.2.2 tls return (0);
329 1.4.2.2 tls }
330 1.4.2.3 tls *bnp = blkptrtodb(fs, daddr);
331 1.4.2.2 tls if (*bnp == 0) {
332 1.4.2.2 tls if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL))
333 1.4.2.2 tls == SF_SNAPSHOT) {
334 1.4.2.3 tls *bnp = blkptrtodb(fs, bn * fs->um_seqinc);
335 1.4.2.2 tls } else {
336 1.4.2.2 tls *bnp = -1;
337 1.4.2.2 tls }
338 1.4.2.2 tls }
339 1.4.2.2 tls return (0);
340 1.4.2.2 tls }
341 1.4.2.2 tls
342 1.4.2.2 tls /*
343 1.4.2.2 tls * Create an array of logical block number/offset pairs which represent the
344 1.4.2.2 tls * path of indirect blocks required to access a data block. The first "pair"
345 1.4.2.2 tls * contains the logical block number of the appropriate single, double or
346 1.4.2.2 tls * triple indirect block and the offset into the inode indirect block array.
347 1.4.2.2 tls * Note, the logical block number of the inode single/double/triple indirect
348 1.4.2.2 tls * block appears twice in the array, once with the offset into the i_ffs1_ib and
349 1.4.2.2 tls * once with the offset into the page itself.
350 1.4.2.2 tls */
351 1.4.2.2 tls int
352 1.4.2.2 tls ulfs_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
353 1.4.2.2 tls {
354 1.4.2.2 tls daddr_t metalbn, realbn;
355 1.4.2.2 tls struct ulfsmount *ump;
356 1.4.2.3 tls struct lfs *fs;
357 1.4.2.2 tls int64_t blockcnt;
358 1.4.2.2 tls int lbc;
359 1.4.2.2 tls int i, numlevels, off;
360 1.4.2.2 tls
361 1.4.2.2 tls ump = VFSTOULFS(vp->v_mount);
362 1.4.2.3 tls fs = ump->um_lfs;
363 1.4.2.2 tls if (nump)
364 1.4.2.2 tls *nump = 0;
365 1.4.2.2 tls numlevels = 0;
366 1.4.2.2 tls realbn = bn;
367 1.4.2.2 tls if (bn < 0)
368 1.4.2.2 tls bn = -bn;
369 1.4.2.2 tls KASSERT(bn >= ULFS_NDADDR);
370 1.4.2.2 tls
371 1.4.2.2 tls /*
372 1.4.2.2 tls * Determine the number of levels of indirection. After this loop
373 1.4.2.2 tls * is done, blockcnt indicates the number of data blocks possible
374 1.4.2.2 tls * at the given level of indirection, and ULFS_NIADDR - i is the number
375 1.4.2.2 tls * of levels of indirection needed to locate the requested block.
376 1.4.2.2 tls */
377 1.4.2.2 tls
378 1.4.2.2 tls bn -= ULFS_NDADDR;
379 1.4.2.2 tls for (lbc = 0, i = ULFS_NIADDR;; i--, bn -= blockcnt) {
380 1.4.2.2 tls if (i == 0)
381 1.4.2.2 tls return (EFBIG);
382 1.4.2.2 tls
383 1.4.2.3 tls lbc += fs->um_lognindir;
384 1.4.2.2 tls blockcnt = (int64_t)1 << lbc;
385 1.4.2.2 tls
386 1.4.2.2 tls if (bn < blockcnt)
387 1.4.2.2 tls break;
388 1.4.2.2 tls }
389 1.4.2.2 tls
390 1.4.2.2 tls /* Calculate the address of the first meta-block. */
391 1.4.2.2 tls metalbn = -((realbn >= 0 ? realbn : -realbn) - bn + ULFS_NIADDR - i);
392 1.4.2.2 tls
393 1.4.2.2 tls /*
394 1.4.2.2 tls * At each iteration, off is the offset into the bap array which is
395 1.4.2.2 tls * an array of disk addresses at the current level of indirection.
396 1.4.2.2 tls * The logical block number and the offset in that block are stored
397 1.4.2.2 tls * into the argument array.
398 1.4.2.2 tls */
399 1.4.2.2 tls ap->in_lbn = metalbn;
400 1.4.2.2 tls ap->in_off = off = ULFS_NIADDR - i;
401 1.4.2.2 tls ap->in_exists = 0;
402 1.4.2.2 tls ap++;
403 1.4.2.2 tls for (++numlevels; i <= ULFS_NIADDR; i++) {
404 1.4.2.2 tls /* If searching for a meta-data block, quit when found. */
405 1.4.2.2 tls if (metalbn == realbn)
406 1.4.2.2 tls break;
407 1.4.2.2 tls
408 1.4.2.3 tls lbc -= fs->um_lognindir;
409 1.4.2.3 tls off = (bn >> lbc) & (MNINDIR(fs) - 1);
410 1.4.2.2 tls
411 1.4.2.2 tls ++numlevels;
412 1.4.2.2 tls ap->in_lbn = metalbn;
413 1.4.2.2 tls ap->in_off = off;
414 1.4.2.2 tls ap->in_exists = 0;
415 1.4.2.2 tls ++ap;
416 1.4.2.2 tls
417 1.4.2.2 tls metalbn -= -1 + ((int64_t)off << lbc);
418 1.4.2.2 tls }
419 1.4.2.2 tls if (nump)
420 1.4.2.2 tls *nump = numlevels;
421 1.4.2.2 tls return (0);
422 1.4.2.2 tls }
423