1 /* $NetBSD: ulfs_bmap.c,v 1.5 2013/07/28 01:10:49 dholland Exp $ */
2 /* from NetBSD: ufs_bmap.c,v 1.50 2013/01/22 09:39:18 dholland Exp */
5 * Copyright (c) 1989, 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)ufs_bmap.c 8.8 (Berkeley) 8/11/95
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ulfs_bmap.c,v 1.5 2013/07/28 01:10:49 dholland Exp $");
43 #include <sys/param.h>
44 #include <sys/systm.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/resourcevar.h>
51 #include <sys/trace.h>
52 #include <sys/fstrans.h>
54 #include <miscfs/specfs/specdev.h>
56 #include <ufs/lfs/ulfs_inode.h>
57 #include <ufs/lfs/ulfsmount.h>
58 #include <ufs/lfs/ulfs_extern.h>
59 #include <ufs/lfs/ulfs_bswap.h>
62 ulfs_issequential(const struct lfs
*fs
, daddr_t daddr0
, daddr_t daddr1
)
65 /* for ulfs, blocks in a hole is not 'contiguous'. */
69 return (daddr0
+ fs
->um_seqinc
== daddr1
);
73 * Bmap converts the logical block number of a file to its physical block
74 * number on the disk. The conversion is done by using the logical block
75 * number to index into the array of block pointers described by the dinode.
80 struct vop_bmap_args
/* {
90 * Check for underlying vnode requests and ensure that logical
91 * to physical mapping is requested.
93 if (ap
->a_vpp
!= NULL
)
94 *ap
->a_vpp
= VTOI(ap
->a_vp
)->i_devvp
;
95 if (ap
->a_bnp
== NULL
)
98 fstrans_start(ap
->a_vp
->v_mount
, FSTRANS_SHARED
);
99 error
= ulfs_bmaparray(ap
->a_vp
, ap
->a_bn
, ap
->a_bnp
, NULL
, NULL
,
100 ap
->a_runp
, ulfs_issequential
);
101 fstrans_done(ap
->a_vp
->v_mount
);
106 * Indirect blocks are now on the vnode for the file. They are given negative
107 * logical block numbers. Indirect blocks are addressed by the negative
108 * address of the first data block to which they point. Double indirect blocks
109 * are addressed by one less than the address of the first indirect block to
110 * which they point. Triple indirect blocks are addressed by one less than
111 * the address of the first double indirect block to which they point.
113 * ulfs_bmaparray does the bmap conversion, and if requested returns the
114 * array of logical blocks which must be traversed to get to a block.
115 * Each entry contains the offset into that block that gets you to the
116 * next block and the disk address of the block (if it is assigned).
120 ulfs_bmaparray(struct vnode
*vp
, daddr_t bn
, daddr_t
*bnp
, struct indir
*ap
,
121 int *nump
, int *runp
, ulfs_issequential_callback_t is_sequential
)
124 struct buf
*bp
, *cbp
;
125 struct ulfsmount
*ump
;
128 struct indir a
[ULFS_NIADDR
+ 1], *xap
;
131 int error
, maxrun
= 0, num
;
138 if ((ap
!= NULL
&& nump
== NULL
) || (ap
== NULL
&& nump
!= NULL
))
139 panic("ulfs_bmaparray: invalid arguments");
145 * If MAXBSIZE is the largest transfer the disks can handle,
146 * we probably want maxrun to be 1 block less so that we
147 * don't create a block larger than the device can handle.
150 maxrun
= MAXPHYS
/ mp
->mnt_stat
.f_iosize
- 1;
153 if (bn
>= 0 && bn
< ULFS_NDADDR
) {
156 if (ump
->um_fstype
== ULFS1
)
157 daddr
= ulfs_rw32(ip
->i_ffs1_db
[bn
],
158 ULFS_MPNEEDSWAP(fs
));
160 daddr
= ulfs_rw64(ip
->i_ffs2_db
[bn
],
161 ULFS_MPNEEDSWAP(fs
));
162 *bnp
= blkptrtodb(fs
, daddr
);
164 * Since this is FFS independent code, we are out of
165 * scope for the definitions of BLK_NOCOPY and
166 * BLK_SNAP, but we do know that they will fall in
167 * the range 1..um_seqinc, so we use that test and
168 * return a request for a zeroed out buffer if attempts
169 * are made to read a BLK_NOCOPY or BLK_SNAP block.
171 if ((ip
->i_flags
& (SF_SNAPSHOT
| SF_SNAPINVAL
)) == SF_SNAPSHOT
173 daddr
< fs
->um_seqinc
) {
175 } else if (*bnp
== 0) {
176 if ((ip
->i_flags
& (SF_SNAPSHOT
| SF_SNAPINVAL
))
178 *bnp
= blkptrtodb(fs
, bn
* fs
->um_seqinc
);
183 if (ump
->um_fstype
== ULFS1
) {
184 for (++bn
; bn
< ULFS_NDADDR
&& *runp
< maxrun
&&
186 ulfs_rw32(ip
->i_ffs1_db
[bn
- 1],
187 ULFS_MPNEEDSWAP(fs
)),
188 ulfs_rw32(ip
->i_ffs1_db
[bn
],
189 ULFS_MPNEEDSWAP(fs
)));
192 for (++bn
; bn
< ULFS_NDADDR
&& *runp
< maxrun
&&
194 ulfs_rw64(ip
->i_ffs2_db
[bn
- 1],
195 ULFS_MPNEEDSWAP(fs
)),
196 ulfs_rw64(ip
->i_ffs2_db
[bn
],
197 ULFS_MPNEEDSWAP(fs
)));
204 xap
= ap
== NULL
? a
: ap
;
207 if ((error
= ulfs_getlbns(vp
, bn
, xap
, nump
)) != 0)
212 /* Get disk address out of indirect block array */
213 if (ump
->um_fstype
== ULFS1
)
214 daddr
= ulfs_rw32(ip
->i_ffs1_ib
[xap
->in_off
],
215 ULFS_MPNEEDSWAP(fs
));
217 daddr
= ulfs_rw64(ip
->i_ffs2_ib
[xap
->in_off
],
218 ULFS_MPNEEDSWAP(fs
));
220 for (bp
= NULL
, ++xap
; --num
; ++xap
) {
222 * Exit the loop if there is no disk address assigned yet and
223 * the indirect block isn't in the cache, or if we were
224 * looking for an indirect block and we've found it.
227 metalbn
= xap
->in_lbn
;
231 mutex_enter(&bufcache_lock
);
232 cbp
= incore(vp
, metalbn
);
233 mutex_exit(&bufcache_lock
);
239 * If we get here, we've either got the block in the cache
240 * or we have a disk address for it, go fetch it.
246 bp
= getblk(vp
, metalbn
, mp
->mnt_stat
.f_iosize
, 0, 0);
250 * getblk() above returns NULL only iff we are
251 * pagedaemon. See the implementation of getblk
257 if (bp
->b_oflags
& (BO_DONE
| BO_DELWRI
)) {
258 trace(TR_BREADHIT
, pack(vp
, size
), metalbn
);
262 panic("ulfs_bmaparray: indirect block not in cache");
265 trace(TR_BREADMISS
, pack(vp
, size
), metalbn
);
266 bp
->b_blkno
= blkptrtodb(fs
, daddr
);
267 bp
->b_flags
|= B_READ
;
268 BIO_SETPRIO(bp
, BPRIO_TIMECRITICAL
);
269 VOP_STRATEGY(vp
, bp
);
270 curlwp
->l_ru
.ru_inblock
++; /* XXX */
271 if ((error
= biowait(bp
)) != 0) {
276 if (ump
->um_fstype
== ULFS1
) {
277 daddr
= ulfs_rw32(((u_int32_t
*)bp
->b_data
)[xap
->in_off
],
278 ULFS_MPNEEDSWAP(fs
));
279 if (num
== 1 && daddr
&& runp
) {
280 for (bn
= xap
->in_off
+ 1;
281 bn
< MNINDIR(fs
) && *runp
< maxrun
&&
283 ulfs_rw32(((int32_t *)bp
->b_data
)[bn
-1],
284 ULFS_MPNEEDSWAP(fs
)),
285 ulfs_rw32(((int32_t *)bp
->b_data
)[bn
],
286 ULFS_MPNEEDSWAP(fs
)));
290 daddr
= ulfs_rw64(((u_int64_t
*)bp
->b_data
)[xap
->in_off
],
291 ULFS_MPNEEDSWAP(fs
));
292 if (num
== 1 && daddr
&& runp
) {
293 for (bn
= xap
->in_off
+ 1;
294 bn
< MNINDIR(fs
) && *runp
< maxrun
&&
296 ulfs_rw64(((int64_t *)bp
->b_data
)[bn
-1],
297 ULFS_MPNEEDSWAP(fs
)),
298 ulfs_rw64(((int64_t *)bp
->b_data
)[bn
],
299 ULFS_MPNEEDSWAP(fs
)));
308 * Since this is FFS independent code, we are out of scope for the
309 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
310 * will fall in the range 1..um_seqinc, so we use that test and
311 * return a request for a zeroed out buffer if attempts are made
312 * to read a BLK_NOCOPY or BLK_SNAP block.
314 if ((ip
->i_flags
& (SF_SNAPSHOT
| SF_SNAPINVAL
)) == SF_SNAPSHOT
315 && daddr
> 0 && daddr
< fs
->um_seqinc
) {
319 *bnp
= blkptrtodb(fs
, daddr
);
321 if ((ip
->i_flags
& (SF_SNAPSHOT
| SF_SNAPINVAL
))
323 *bnp
= blkptrtodb(fs
, bn
* fs
->um_seqinc
);
332 * Create an array of logical block number/offset pairs which represent the
333 * path of indirect blocks required to access a data block. The first "pair"
334 * contains the logical block number of the appropriate single, double or
335 * triple indirect block and the offset into the inode indirect block array.
336 * Note, the logical block number of the inode single/double/triple indirect
337 * block appears twice in the array, once with the offset into the i_ffs1_ib and
338 * once with the offset into the page itself.
341 ulfs_getlbns(struct vnode
*vp
, daddr_t bn
, struct indir
*ap
, int *nump
)
343 daddr_t metalbn
, realbn
;
344 struct ulfsmount
*ump
;
348 int i
, numlevels
, off
;
350 ump
= VFSTOULFS(vp
->v_mount
);
358 KASSERT(bn
>= ULFS_NDADDR
);
361 * Determine the number of levels of indirection. After this loop
362 * is done, blockcnt indicates the number of data blocks possible
363 * at the given level of indirection, and ULFS_NIADDR - i is the number
364 * of levels of indirection needed to locate the requested block.
368 for (lbc
= 0, i
= ULFS_NIADDR
;; i
--, bn
-= blockcnt
) {
372 lbc
+= fs
->um_lognindir
;
373 blockcnt
= (int64_t)1 << lbc
;
379 /* Calculate the address of the first meta-block. */
380 metalbn
= -((realbn
>= 0 ? realbn
: -realbn
) - bn
+ ULFS_NIADDR
- i
);
383 * At each iteration, off is the offset into the bap array which is
384 * an array of disk addresses at the current level of indirection.
385 * The logical block number and the offset in that block are stored
386 * into the argument array.
388 ap
->in_lbn
= metalbn
;
389 ap
->in_off
= off
= ULFS_NIADDR
- i
;
392 for (++numlevels
; i
<= ULFS_NIADDR
; i
++) {
393 /* If searching for a meta-data block, quit when found. */
394 if (metalbn
== realbn
)
397 lbc
-= fs
->um_lognindir
;
398 off
= (bn
>> lbc
) & (MNINDIR(fs
) - 1);
401 ap
->in_lbn
= metalbn
;
406 metalbn
-= -1 + ((int64_t)off
<< lbc
);