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