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