2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)ufs_bmap.c 8.7 (Berkeley) 3/21/95
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/racct.h>
50 #include <sys/resourcevar.h>
53 #include <ufs/ufs/extattr.h>
54 #include <ufs/ufs/quota.h>
55 #include <ufs/ufs/inode.h>
56 #include <ufs/ufs/ufsmount.h>
57 #include <ufs/ufs/ufs_extern.h>
59 static ufs_lbn_t lbn_count(struct ufsmount *, int);
60 static int readindir(struct vnode *, ufs_lbn_t, ufs2_daddr_t, struct buf **);
63 * Bmap converts the logical block number of a file to its physical block
64 * number on the disk. The conversion is done by using the logical block
65 * number to index into the array of block pointers described by the dinode.
69 struct vop_bmap_args /* {
72 struct bufobj **a_bop;
82 * Check for underlying vnode requests and ensure that logical
83 * to physical mapping is requested.
85 if (ap->a_bop != NULL)
86 *ap->a_bop = &VFSTOUFS(ap->a_vp->v_mount)->um_devvp->v_bufobj;
87 if (ap->a_bnp == NULL)
90 error = ufs_bmaparray(ap->a_vp, ap->a_bn, &blkno, NULL,
91 ap->a_runp, ap->a_runb);
97 readindir(vp, lbn, daddr, bpp)
105 struct ufsmount *ump;
111 bp = getblk(vp, lbn, mp->mnt_stat.f_iosize, 0, 0, 0);
112 if ((bp->b_flags & B_CACHE) == 0) {
114 ("readindir: indirect block not in cache"));
116 bp->b_blkno = blkptrtodb(ump, daddr);
117 bp->b_iocmd = BIO_READ;
118 bp->b_flags &= ~B_INVAL;
119 bp->b_ioflags &= ~BIO_ERROR;
120 vfs_busy_pages(bp, 0);
121 bp->b_iooffset = dbtob(bp->b_blkno);
126 racct_add_buf(curproc, bp, 0);
127 PROC_UNLOCK(curproc);
130 curthread->td_ru.ru_inblock++;
142 * Indirect blocks are now on the vnode for the file. They are given negative
143 * logical block numbers. Indirect blocks are addressed by the negative
144 * address of the first data block to which they point. Double indirect blocks
145 * are addressed by one less than the address of the first indirect block to
146 * which they point. Triple indirect blocks are addressed by one less than
147 * the address of the first double indirect block to which they point.
149 * ufs_bmaparray does the bmap conversion, and if requested returns the
150 * array of logical blocks which must be traversed to get to a block.
151 * Each entry contains the offset into that block that gets you to the
152 * next block and the disk address of the block (if it is assigned).
156 ufs_bmaparray(vp, bn, bnp, nbp, runp, runb)
166 struct ufsmount *ump;
168 struct indir a[UFS_NIADDR+1], *ap;
171 int error, num, maxrun = 0;
180 maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1;
191 error = ufs_getlbns(vp, bn, ap, nump);
197 if (bn >= 0 && bn < UFS_NDADDR) {
198 *bnp = blkptrtodb(ump, DIP(ip, i_db[bn]));
199 } else if (bn < 0 && bn >= -UFS_NXADDR) {
200 *bnp = blkptrtodb(ump, ip->i_din2->di_extb[-1 - bn]);
204 panic("ufs_bmaparray: mapping ext data");
205 nbp->b_xflags |= BX_ALTDATA;
208 panic("ufs_bmaparray: blkno out of range");
211 * Since this is FFS independent code, we are out of
212 * scope for the definitions of BLK_NOCOPY and
213 * BLK_SNAP, but we do know that they will fall in
214 * the range 1..um_seqinc, so we use that test and
215 * return a request for a zeroed out buffer if attempts
216 * are made to read a BLK_NOCOPY or BLK_SNAP block.
218 if ((ip->i_flags & SF_SNAPSHOT) && DIP(ip, i_db[bn]) > 0 &&
219 DIP(ip, i_db[bn]) < ump->um_seqinc) {
221 } else if (*bnp == 0) {
222 if (ip->i_flags & SF_SNAPSHOT)
223 *bnp = blkptrtodb(ump, bn * ump->um_seqinc);
227 ufs2_daddr_t bnb = bn;
228 for (++bn; bn < UFS_NDADDR && *runp < maxrun &&
229 is_sequential(ump, DIP(ip, i_db[bn - 1]),
233 if (runb && (bn > 0)) {
234 for (--bn; (bn >= 0) && (*runb < maxrun) &&
235 is_sequential(ump, DIP(ip, i_db[bn]),
236 DIP(ip, i_db[bn+1]));
244 /* Get disk address out of indirect block array */
245 daddr = DIP(ip, i_ib[ap->in_off]);
247 for (bp = NULL, ++ap; --num; ++ap) {
249 * Exit the loop if there is no disk address assigned yet and
250 * the indirect block isn't in the cache, or if we were
251 * looking for an indirect block and we've found it.
254 metalbn = ap->in_lbn;
255 if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
258 * If we get here, we've either got the block in the cache
259 * or we have a disk address for it, go fetch it.
263 error = readindir(vp, metalbn, daddr, &bp);
268 daddr = ((ufs1_daddr_t *)bp->b_data)[ap->in_off];
269 if (num == 1 && daddr && runp) {
270 for (bn = ap->in_off + 1;
271 bn < MNINDIR(ump) && *runp < maxrun &&
273 ((ufs1_daddr_t *)bp->b_data)[bn - 1],
274 ((ufs1_daddr_t *)bp->b_data)[bn]);
278 for (--bn; bn >= 0 && *runb < maxrun &&
280 ((ufs1_daddr_t *)bp->b_data)[bn],
281 ((ufs1_daddr_t *)bp->b_data)[bn+1]);
287 daddr = ((ufs2_daddr_t *)bp->b_data)[ap->in_off];
288 if (num == 1 && daddr && runp) {
289 for (bn = ap->in_off + 1;
290 bn < MNINDIR(ump) && *runp < maxrun &&
292 ((ufs2_daddr_t *)bp->b_data)[bn - 1],
293 ((ufs2_daddr_t *)bp->b_data)[bn]);
297 for (--bn; bn >= 0 && *runb < maxrun &&
299 ((ufs2_daddr_t *)bp->b_data)[bn],
300 ((ufs2_daddr_t *)bp->b_data)[bn + 1]);
309 * Since this is FFS independent code, we are out of scope for the
310 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
311 * will fall in the range 1..um_seqinc, so we use that test and
312 * return a request for a zeroed out buffer if attempts are made
313 * to read a BLK_NOCOPY or BLK_SNAP block.
315 if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc){
319 *bnp = blkptrtodb(ump, daddr);
321 if (ip->i_flags & SF_SNAPSHOT)
322 *bnp = blkptrtodb(ump, bn * ump->um_seqinc);
330 lbn_count(ump, level)
331 struct ufsmount *ump;
336 for (blockcnt = 1; level > 0; level--)
337 blockcnt *= MNINDIR(ump);
342 ufs_bmap_seekdata(vp, offp)
347 struct indir a[UFS_NIADDR + 1], *ap;
350 struct ufsmount *ump;
351 ufs2_daddr_t bn, daddr, nextbn;
354 int error, num, num1, off;
362 if (vp->v_type != VREG || (ip->i_flags & SF_SNAPSHOT) != 0)
364 if (*offp < 0 || *offp >= ip->i_size)
367 bsize = mp->mnt_stat.f_iosize;
368 for (bn = *offp / bsize, numblks = howmany(ip->i_size, bsize);
369 bn < numblks; bn = nextbn) {
370 if (bn < UFS_NDADDR) {
371 daddr = DIP(ip, i_db[bn]);
379 error = ufs_getlbns(vp, bn, ap, &num);
383 daddr = DIP(ip, i_ib[ap->in_off]);
385 for (nextbn = UFS_NDADDR, num1 = num - 1; num1 > 0; num1--)
386 nextbn += lbn_count(ump, num1);
388 nextbn += lbn_count(ump, num);
392 for (; daddr != 0 && num > 0; ap++, num--) {
395 error = readindir(vp, ap->in_lbn, daddr, &bp);
400 * Scan the indirect block until we find a non-zero
405 daddr = I_IS_UFS1(ip) ?
406 ((ufs1_daddr_t *)bp->b_data)[off] :
407 ((ufs2_daddr_t *)bp->b_data)[off];
408 } while (daddr == 0 && ++off < MNINDIR(ump));
409 nextbn += off * lbn_count(ump, num - 1);
412 * We need to recompute the LBNs of indirect
413 * blocks, so restart with the updated block offset.
415 if (off != ap->in_off)
420 * We found a data block.
430 if (error == 0 && *offp < bn * bsize)
436 * Create an array of logical block number/offset pairs which represent the
437 * path of indirect blocks required to access a data block. The first "pair"
438 * contains the logical block number of the appropriate single, double or
439 * triple indirect block and the offset into the inode indirect block array.
440 * Note, the logical block number of the inode single/double/triple indirect
441 * block appears twice in the array, once with the offset into the i_ib and
442 * once with the offset into the page itself.
445 ufs_getlbns(vp, bn, ap, nump)
451 ufs2_daddr_t blockcnt;
452 ufs_lbn_t metalbn, realbn;
453 struct ufsmount *ump;
454 int i, numlevels, off;
456 ump = VFSTOUFS(vp->v_mount);
464 /* The first UFS_NDADDR blocks are direct blocks. */
469 * Determine the number of levels of indirection. After this loop
470 * is done, blockcnt indicates the number of data blocks possible
471 * at the previous level of indirection, and UFS_NIADDR - i is the
472 * number of levels of indirection needed to locate the requested block.
474 for (blockcnt = 1, i = UFS_NIADDR, bn -= UFS_NDADDR; ;
475 i--, bn -= blockcnt) {
478 blockcnt *= MNINDIR(ump);
483 /* Calculate the address of the first meta-block. */
485 metalbn = -(realbn - bn + UFS_NIADDR - i);
487 metalbn = -(-realbn - bn + UFS_NIADDR - i);
490 * At each iteration, off is the offset into the bap array which is
491 * an array of disk addresses at the current level of indirection.
492 * The logical block number and the offset in that block are stored
493 * into the argument array.
495 ap->in_lbn = metalbn;
496 ap->in_off = off = UFS_NIADDR - i;
498 for (++numlevels; i <= UFS_NIADDR; i++) {
499 /* If searching for a meta-data block, quit when found. */
500 if (metalbn == realbn)
503 blockcnt /= MNINDIR(ump);
504 off = (bn / blockcnt) % MNINDIR(ump);
507 ap->in_lbn = metalbn;
511 metalbn -= -1 + off * blockcnt;