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