2 * Copyright (c) 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)ufs_bmap.c 8.7 (Berkeley) 3/21/95
38 #include <sys/param.h>
39 #include <sys/systm.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/resourcevar.h>
48 #include <fs/ext2fs/inode.h>
49 #include <fs/ext2fs/fs.h>
50 #include <fs/ext2fs/ext2fs.h>
51 #include <fs/ext2fs/ext2_dinode.h>
52 #include <fs/ext2fs/ext2_extern.h>
53 #include <fs/ext2fs/ext2_mount.h>
55 static int ext4_bmapext(struct vnode *, int32_t, int64_t *, int *, int *);
58 * Bmap converts the logical block number of a file to its physical block
59 * number on the disk. The conversion is done by using the logical block
60 * number to index into the array of block pointers described by the dinode.
63 ext2_bmap(struct vop_bmap_args *ap)
69 * Check for underlying vnode requests and ensure that logical
70 * to physical mapping is requested.
72 if (ap->a_bop != NULL)
73 *ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj;
74 if (ap->a_bnp == NULL)
77 if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
78 error = ext4_bmapext(ap->a_vp, ap->a_bn, &blkno,
79 ap->a_runp, ap->a_runb);
81 error = ext2_bmaparray(ap->a_vp, ap->a_bn, &blkno,
82 ap->a_runp, ap->a_runb);
88 * This function converts the logical block number of a file to
89 * its physical block number on the disk within ext4 extents.
92 ext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb)
96 struct ext4_extent *ep;
97 struct ext4_extent_path path;
105 * TODO: need to implement read ahead to improve the performance.
113 ext4_ext_find_extent(fs, ip, lbn, &path);
118 *bnp = fsbtodb(fs, lbn - ep->e_blk +
119 (ep->e_start_lo | (daddr_t)ep->e_start_hi << 32));
128 * Indirect blocks are now on the vnode for the file. They are given negative
129 * logical block numbers. Indirect blocks are addressed by the negative
130 * address of the first data block to which they point. Double indirect blocks
131 * are addressed by one less than the address of the first indirect block to
132 * which they point. Triple indirect blocks are addressed by one less than
133 * the address of the first double indirect block to which they point.
135 * ext2_bmaparray does the bmap conversion, and if requested returns the
136 * array of logical blocks which must be traversed to get to a block.
137 * Each entry contains the offset into that block that gets you to the
138 * next block and the disk address of the block (if it is assigned).
142 ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb)
146 struct ext2mount *ump;
149 struct indir a[NIADDR+1], *ap;
152 int error, num, maxrun = 0, bsize;
159 devvp = ump->um_devvp;
161 bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
164 maxrun = mp->mnt_iosize_max / bsize - 1;
175 error = ext2_getlbns(vp, bn, ap, nump);
181 *bnp = blkptrtodb(ump, ip->i_db[bn]);
186 for (++bn; bn < NDADDR && *runp < maxrun &&
187 is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
190 if (runb && (bn > 0)) {
191 for (--bn; (bn >= 0) && (*runb < maxrun) &&
192 is_sequential(ump, ip->i_db[bn],
201 /* Get disk address out of indirect block array */
202 daddr = ip->i_ib[ap->in_off];
204 for (bp = NULL, ++ap; --num; ++ap) {
206 * Exit the loop if there is no disk address assigned yet and
207 * the indirect block isn't in the cache, or if we were
208 * looking for an indirect block and we've found it.
211 metalbn = ap->in_lbn;
212 if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
215 * If we get here, we've either got the block in the cache
216 * or we have a disk address for it, go fetch it.
221 bp = getblk(vp, metalbn, bsize, 0, 0, 0);
222 if ((bp->b_flags & B_CACHE) == 0) {
225 panic("ext2_bmaparray: indirect block not in cache");
227 bp->b_blkno = blkptrtodb(ump, daddr);
228 bp->b_iocmd = BIO_READ;
229 bp->b_flags &= ~B_INVAL;
230 bp->b_ioflags &= ~BIO_ERROR;
231 vfs_busy_pages(bp, 0);
232 bp->b_iooffset = dbtob(bp->b_blkno);
234 curthread->td_ru.ru_inblock++;
242 daddr = ((e2fs_daddr_t *)bp->b_data)[ap->in_off];
243 if (num == 1 && daddr && runp) {
244 for (bn = ap->in_off + 1;
245 bn < MNINDIR(ump) && *runp < maxrun &&
247 ((e2fs_daddr_t *)bp->b_data)[bn - 1],
248 ((e2fs_daddr_t *)bp->b_data)[bn]);
252 for (--bn; bn >= 0 && *runb < maxrun &&
254 ((e2fs_daddr_t *)bp->b_data)[bn],
255 ((e2fs_daddr_t *)bp->b_data)[bn + 1]);
264 * Since this is FFS independent code, we are out of scope for the
265 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
266 * will fall in the range 1..um_seqinc, so we use that test and
267 * return a request for a zeroed out buffer if attempts are made
268 * to read a BLK_NOCOPY or BLK_SNAP block.
270 if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc){
274 *bnp = blkptrtodb(ump, daddr);
282 * Create an array of logical block number/offset pairs which represent the
283 * path of indirect blocks required to access a data block. The first "pair"
284 * contains the logical block number of the appropriate single, double or
285 * triple indirect block and the offset into the inode indirect block array.
286 * Note, the logical block number of the inode single/double/triple indirect
287 * block appears twice in the array, once with the offset into the i_ib and
288 * once with the offset into the page itself.
291 ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
294 e2fs_lbn_t metalbn, realbn;
295 struct ext2mount *ump;
296 int i, numlevels, off;
299 ump = VFSTOEXT2(vp->v_mount);
307 /* The first NDADDR blocks are direct blocks. */
312 * Determine the number of levels of indirection. After this loop
313 * is done, blockcnt indicates the number of data blocks possible
314 * at the previous level of indirection, and NIADDR - i is the number
315 * of levels of indirection needed to locate the requested block.
317 for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
321 * Use int64_t's here to avoid overflow for triple indirect
322 * blocks when longs have 32 bits and the block size is more
325 qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
328 blockcnt = qblockcnt;
331 /* Calculate the address of the first meta-block. */
333 metalbn = -(realbn - bn + NIADDR - i);
335 metalbn = -(-realbn - bn + NIADDR - i);
338 * At each iteration, off is the offset into the bap array which is
339 * an array of disk addresses at the current level of indirection.
340 * The logical block number and the offset in that block are stored
341 * into the argument array.
343 ap->in_lbn = metalbn;
344 ap->in_off = off = NIADDR - i;
346 for (++numlevels; i <= NIADDR; i++) {
347 /* If searching for a meta-data block, quit when found. */
348 if (metalbn == realbn)
351 off = (bn / blockcnt) % MNINDIR(ump);
354 ap->in_lbn = metalbn;
358 metalbn -= -1 + off * blockcnt;
359 blockcnt /= MNINDIR(ump);