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
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/endian.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/resourcevar.h>
51 #include <vfs/ext2fs/fs.h>
52 #include <vfs/ext2fs/inode.h>
53 #include <vfs/ext2fs/ext2fs.h>
54 #include <vfs/ext2fs/ext2_dinode.h>
55 #include <vfs/ext2fs/ext2_extern.h>
56 #include <vfs/ext2fs/ext2_mount.h>
59 * Bmap converts the logical block number of a file to its physical block
60 * number on the disk. The conversion is done by using the logical block
61 * number to index into the array of block pointers described by the dinode.
63 * BMAP must return the contiguous before and after run in bytes, inclusive
64 * of the returned block.
67 ext2_bmap(struct vop_bmap_args *ap)
74 * Check for underlying vnode requests and ensure that logical
75 * to physical mapping is requested.
77 if (ap->a_doffsetp == NULL)
80 fs = VTOI(ap->a_vp)->i_e2fs;
81 KKASSERT(((int)ap->a_loffset & ((1 << fs->e2fs_bshift) - 1)) == 0);
82 lbn = ap->a_loffset >> fs->e2fs_bshift;
84 if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
85 error = ext4_bmapext(ap->a_vp, lbn, &dbn, ap->a_runp,
88 error = ext2_bmaparray(ap->a_vp, lbn, &dbn, ap->a_runp,
91 if (error || dbn == (daddr_t)-1) {
92 *ap->a_doffsetp = NOOFFSET;
94 *ap->a_doffsetp = dbtodoff(fs, dbn);
96 *ap->a_runp = (*ap->a_runp + 1) << fs->e2fs_bshift;
98 *ap->a_runb = *ap->a_runb << fs->e2fs_bshift;
104 * Convert the logical block number of a file to its physical block number
105 * on the disk within ext4 extents.
108 ext4_bmapext(struct vnode *vp, int32_t bn, daddr_t/*int64_t*/ *bnp, int *runp, int *runb)
114 readindir(struct vnode *vp, e2fs_lbn_t lbn, e2fs_daddr_t daddr, struct buf **bpp)
118 struct ext2mount *ump;
124 fs = VTOI(vp)->i_e2fs;
126 bp = getblk(vp, lblktodoff(fs, lbn), mp->mnt_stat.f_iosize, 0, 0);
127 if ((bp->b_flags & B_CACHE) == 0) {
129 ("readindir: indirect block not in cache"));
131 * This runs through ext2_strategy using bio2 to
132 * cache the disk offset, then comes back through
133 * bio1. So we want to wait on bio1.
135 bp->b_bio1.bio_done = biodone_sync;
136 bp->b_bio1.bio_flags |= BIO_SYNC;
137 bp->b_bio2.bio_offset = fsbtodoff(fs, daddr);
138 bp->b_flags &= ~(B_INVAL | B_ERROR);
139 bp->b_cmd = BUF_CMD_READ;
140 vfs_busy_pages(bp->b_vp, bp);
141 vn_strategy(bp->b_vp, &bp->b_bio1);
145 racct_add_buf(curproc, bp, 0);
146 PROC_UNLOCK(curproc);
149 error = biowait(&bp->b_bio1, "biord");
160 * Indirect blocks are now on the vnode for the file. They are given negative
161 * logical block numbers. Indirect blocks are addressed by the negative
162 * address of the first data block to which they point. Double indirect blocks
163 * are addressed by one less than the address of the first indirect block to
164 * which they point. Triple indirect blocks are addressed by one less than
165 * the address of the first double indirect block to which they point.
167 * ext2_bmaparray does the bmap conversion, and if requested returns the
168 * array of logical blocks which must be traversed to get to a block.
169 * Each entry contains the offset into that block that gets you to the
170 * next block and the disk address of the block (if it is assigned).
174 ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb)
178 struct ext2mount *ump;
181 struct indir a[EXT2_NIADDR + 1], *ap;
184 int error, num, maxrun = 0, bsize;
193 bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
196 maxrun = mp->mnt_iosize_max / bsize - 1;
204 error = ext2_getlbns(vp, bn, ap, nump);
210 *bnp = blkptrtodb(ump, ip->i_db[bn]);
216 for (++bn; bn < EXT2_NDADDR && *runp < maxrun &&
217 is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
220 if (runb && (bn > 0)) {
221 for (--bn; (bn >= 0) && (*runb < maxrun) &&
222 is_sequential(ump, ip->i_db[bn],
230 /* Get disk address out of indirect block array */
231 daddr = ip->i_ib[ap->in_off];
233 for (bp = NULL, ++ap; --num; ++ap) {
235 * Exit the loop if there is no disk address assigned yet and
236 * the indirect block isn't in the cache, or if we were
237 * looking for an indirect block and we've found it.
240 metalbn = ap->in_lbn;
242 !findblk(vp, dbtodoff(fs, metalbn), FINDBLK_TEST)) ||
246 * If we get here, we've either got the block in the cache
247 * or we have a disk address for it, go fetch it.
251 error = readindir(vp, metalbn, daddr, &bp);
255 daddr = le32toh(((e2fs_daddr_t *)bp->b_data)[ap->in_off]);
256 if (num == 1 && daddr && runp) {
257 for (bn = ap->in_off + 1;
258 bn < MNINDIR(ump) && *runp < maxrun &&
260 ((e2fs_daddr_t *)bp->b_data)[bn - 1],
261 ((e2fs_daddr_t *)bp->b_data)[bn]);
265 for (--bn; bn >= 0 && *runb < maxrun &&
267 ((e2fs_daddr_t *)bp->b_data)[bn],
268 ((e2fs_daddr_t *)bp->b_data)[bn + 1]);
276 *bnp = blkptrtodb(ump, daddr);
284 * Create an array of logical block number/offset pairs which represent the
285 * path of indirect blocks required to access a data block. The first "pair"
286 * contains the logical block number of the appropriate single, double or
287 * triple indirect block and the offset into the inode indirect block array.
288 * Note, the logical block number of the inode single/double/triple indirect
289 * block appears twice in the array, once with the offset into the i_ib and
290 * once with the offset into the page itself.
293 ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
296 e2fs_lbn_t metalbn, realbn;
297 struct ext2mount *ump;
298 int i, numlevels, off;
301 ump = VFSTOEXT2(vp->v_mount);
309 /* The first EXT2_NDADDR blocks are direct blocks. */
310 if (bn < EXT2_NDADDR)
314 * Determine the number of levels of indirection. After this loop
315 * is done, blockcnt indicates the number of data blocks possible
316 * at the previous level of indirection, and EXT2_NIADDR - i is the
317 * number of levels of indirection needed to locate the requested block.
319 for (blockcnt = 1, i = EXT2_NIADDR, bn -= EXT2_NDADDR; ;
320 i--, bn -= blockcnt) {
324 * Use int64_t's here to avoid overflow for triple indirect
325 * blocks when longs have 32 bits and the block size is more
328 qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
331 blockcnt = qblockcnt;
334 /* Calculate the address of the first meta-block. */
336 metalbn = -(realbn - bn + EXT2_NIADDR - i);
338 metalbn = -(-realbn - bn + EXT2_NIADDR - i);
341 * At each iteration, off is the offset into the bap array which is
342 * an array of disk addresses at the current level of indirection.
343 * The logical block number and the offset in that block are stored
344 * into the argument array.
346 ap->in_lbn = metalbn;
347 ap->in_off = off = EXT2_NIADDR - i;
349 for (++numlevels; i <= EXT2_NIADDR; i++) {
350 /* If searching for a meta-data block, quit when found. */
351 if (metalbn == realbn)
354 off = (bn / blockcnt) % MNINDIR(ump);
357 ap->in_lbn = metalbn;
361 metalbn -= -1 + off * blockcnt;
362 blockcnt /= MNINDIR(ump);