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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)ufs_bmap.c 8.7 (Berkeley) 3/21/95
39 * $FreeBSD: src/sys/ufs/ufs/ufs_bmap.c,v 1.34.2.1 2000/03/17 10:12:14 ps Exp $
40 * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_bmap.c,v 1.2 2006/04/28 16:34:01 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/resourcevar.h>
55 #include "ext2_fs_sb.h"
56 #include "ext2mount.h"
57 #include "ext2_extern.h"
60 static int ext2_bmaparray(struct vnode *vp, ext2_daddr_t bn,
61 ext2_daddr_t *bnp, struct indir *ap, int *nump,
62 int *runp, int *runb);
65 * Bmap converts the logical block number of a file to its physical block
66 * number on the disk. The conversion is done by using the logical block
67 * number to index into the array of block pointers described by the dinode.
69 * BMAP must return the contiguous before and after run in bytes, inclusive
70 * of the returned block.
72 * ext2_bmap(struct vnode *a_vp, off_t a_loffset, struct vnode **a_vpp,
73 * off_t *a_doffsetp, int *a_runp, int *a_runb)
76 ext2_bmap(struct vop_bmap_args *ap)
78 struct ext2_sb_info *fs;
84 * Check for underlying vnode requests and ensure that logical
85 * to physical mapping is requested.
87 if (ap->a_vpp != NULL)
88 *ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
89 if (ap->a_doffsetp == NULL)
92 fs = VTOI(ap->a_vp)->i_e2fs;
93 KKASSERT(((int)ap->a_loffset & ((1 << fs->s_bshift) - 1)) == 0);
94 lbn = ap->a_loffset >> fs->s_bshift;
96 error = ext2_bmaparray(ap->a_vp, lbn, &dbn, NULL, NULL,
97 ap->a_runp, ap->a_runb);
99 if (error || dbn == (ext2_daddr_t)-1) {
100 *ap->a_doffsetp = NOOFFSET;
102 *ap->a_doffsetp = dbtodoff(fs, dbn);
104 *ap->a_runp = (*ap->a_runp + 1) << fs->s_bshift;
106 *ap->a_runb = *ap->a_runb << fs->s_bshift;
112 * Indirect blocks are now on the vnode for the file. They are given negative
113 * logical block numbers. Indirect blocks are addressed by the negative
114 * address of the first data block to which they point. Double indirect blocks
115 * are addressed by one less than the address of the first indirect block to
116 * which they point. Triple indirect blocks are addressed by one less than
117 * the address of the first double indirect block to which they point.
119 * ext2_bmaparray does the bmap conversion, and if requested returns the
120 * array of logical blocks which must be traversed to get to a block.
121 * Each entry contains the offset into that block that gets you to the
122 * next block and the disk address of the block (if it is assigned).
126 ext2_bmaparray(struct vnode *vp, ext2_daddr_t bn, ext2_daddr_t *bnp,
127 struct indir *ap, int *nump, int *runp, int *runb)
131 struct ext2mount *ump;
134 struct ext2_sb_info *fs;
135 struct indir a[NIADDR+1], *xap;
138 int error, maxrun, num;
143 devvp = ump->um_devvp;
146 if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
147 panic("ext2_bmaparray: invalid arguments");
158 maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1;
160 xap = ap == NULL ? a : ap;
163 error = ext2_getlbns(vp, bn, xap, nump);
169 *bnp = blkptrtodb(ump, ip->i_db[bn]);
174 for (++bn; bn < NDADDR && *runp < maxrun &&
175 is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
178 if (runb && (bn > 0)) {
179 for (--bn; (bn >= 0) && (*runb < maxrun) &&
180 is_sequential(ump, ip->i_db[bn],
189 /* Get disk address out of indirect block array */
190 daddr = ip->i_ib[xap->in_off];
192 for (bp = NULL, ++xap; --num; ++xap) {
194 * Exit the loop if there is no disk address assigned yet and
195 * the indirect block isn't in the cache, or if we were
196 * looking for an indirect block and we've found it.
199 metalbn = xap->in_lbn;
200 if ((daddr == 0 && !findblk(vp, dbtodoff(fs, metalbn))) || metalbn == bn)
203 * If we get here, we've either got the block in the cache
204 * or we have a disk address for it, go fetch it.
210 bp = getblk(vp, lblktodoff(fs, metalbn),
211 mp->mnt_stat.f_iosize, 0, 0);
212 if ((bp->b_flags & B_CACHE) == 0) {
215 panic("ext2_bmaparray: indirect block not in cache");
217 bp->b_bio2.bio_offset = fsbtodoff(fs, daddr);
218 bp->b_flags |= B_READ;
219 bp->b_flags &= ~(B_INVAL|B_ERROR);
220 vfs_busy_pages(bp->b_vp, bp, 0);
221 vn_strategy(bp->b_vp, &bp->b_bio1);
229 daddr = ((ext2_daddr_t *)bp->b_data)[xap->in_off];
230 if (num == 1 && daddr && runp) {
231 for (bn = xap->in_off + 1;
232 bn < MNINDIR(ump) && *runp < maxrun &&
234 ((ext2_daddr_t *)bp->b_data)[bn - 1],
235 ((ext2_daddr_t *)bp->b_data)[bn]);
239 for(--bn; bn >= 0 && *runb < maxrun &&
240 is_sequential(ump, ((daddr_t *)bp->b_data)[bn],
241 ((daddr_t *)bp->b_data)[bn+1]);
249 daddr = blkptrtodb(ump, daddr);
250 *bnp = daddr == 0 ? -1 : daddr;
255 * Create an array of logical block number/offset pairs which represent the
256 * path of indirect blocks required to access a data block. The first "pair"
257 * contains the logical block number of the appropriate single, double or
258 * triple indirect block and the offset into the inode indirect block array.
259 * Note, the logical block number of the inode single/double/triple indirect
260 * block appears twice in the array, once with the offset into the i_ib and
261 * once with the offset into the page itself.
264 ext2_getlbns(struct vnode *vp, ext2_daddr_t bn, struct indir *ap, int *nump)
266 long blockcnt, metalbn, realbn;
267 struct ext2mount *ump;
268 int i, numlevels, off;
271 ump = VFSTOEXT2(vp->v_mount);
279 /* The first NDADDR blocks are direct blocks. */
284 * Determine the number of levels of indirection. After this loop
285 * is done, blockcnt indicates the number of data blocks possible
286 * at the previous level of indirection, and NIADDR - i is the number
287 * of levels of indirection needed to locate the requested block.
289 for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
293 * Use int64_t's here to avoid overflow for triple indirect
294 * blocks when longs have 32 bits and the block size is more
297 qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
300 blockcnt = qblockcnt;
303 /* Calculate the address of the first meta-block. */
305 metalbn = -(realbn - bn + NIADDR - i);
307 metalbn = -(-realbn - bn + NIADDR - i);
310 * At each iteration, off is the offset into the bap array which is
311 * an array of disk addresses at the current level of indirection.
312 * The logical block number and the offset in that block are stored
313 * into the argument array.
315 ap->in_lbn = metalbn;
316 ap->in_off = off = NIADDR - i;
319 for (++numlevels; i <= NIADDR; i++) {
320 /* If searching for a meta-data block, quit when found. */
321 if (metalbn == realbn)
324 off = (bn / blockcnt) % MNINDIR(ump);
327 ap->in_lbn = metalbn;
332 metalbn -= -1 + off * blockcnt;
333 blockcnt /= MNINDIR(ump);