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
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/resourcevar.h>
50 #include <ufs/ufs/extattr.h>
51 #include <ufs/ufs/quota.h>
52 #include <ufs/ufs/inode.h>
53 #include <ufs/ufs/ufsmount.h>
54 #include <ufs/ufs/ufs_extern.h>
57 * Bmap converts the logical block number of a file to its physical block
58 * number on the disk. The conversion is done by using the logical block
59 * number to index into the array of block pointers described by the dinode.
63 struct vop_bmap_args /* {
66 struct bufobj **a_bop;
76 * Check for underlying vnode requests and ensure that logical
77 * to physical mapping is requested.
79 if (ap->a_bop != NULL)
80 *ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj;
81 if (ap->a_bnp == NULL)
84 error = ufs_bmaparray(ap->a_vp, ap->a_bn, &blkno, NULL,
85 ap->a_runp, ap->a_runb);
91 * Indirect blocks are now on the vnode for the file. They are given negative
92 * logical block numbers. Indirect blocks are addressed by the negative
93 * address of the first data block to which they point. Double indirect blocks
94 * are addressed by one less than the address of the first indirect block to
95 * which they point. Triple indirect blocks are addressed by one less than
96 * the address of the first double indirect block to which they point.
98 * ufs_bmaparray does the bmap conversion, and if requested returns the
99 * array of logical blocks which must be traversed to get to a block.
100 * Each entry contains the offset into that block that gets you to the
101 * next block and the disk address of the block (if it is assigned).
105 ufs_bmaparray(vp, bn, bnp, nbp, runp, runb)
115 struct ufsmount *ump;
117 struct indir a[NIADDR+1], *ap;
120 int error, num, maxrun = 0;
129 maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1;
140 error = ufs_getlbns(vp, bn, ap, nump);
146 if (bn >= 0 && bn < NDADDR) {
147 *bnp = blkptrtodb(ump, DIP(ip, i_db[bn]));
148 } else if (bn < 0 && bn >= -NXADDR) {
149 *bnp = blkptrtodb(ump, ip->i_din2->di_extb[-1 - bn]);
153 panic("ufs_bmaparray: mapping ext data");
154 nbp->b_xflags |= BX_ALTDATA;
157 panic("ufs_bmaparray: blkno out of range");
160 * Since this is FFS independent code, we are out of
161 * scope for the definitions of BLK_NOCOPY and
162 * BLK_SNAP, but we do know that they will fall in
163 * the range 1..um_seqinc, so we use that test and
164 * return a request for a zeroed out buffer if attempts
165 * are made to read a BLK_NOCOPY or BLK_SNAP block.
167 if ((ip->i_flags & SF_SNAPSHOT) && DIP(ip, i_db[bn]) > 0 &&
168 DIP(ip, i_db[bn]) < ump->um_seqinc) {
170 } else if (*bnp == 0) {
171 if (ip->i_flags & SF_SNAPSHOT)
172 *bnp = blkptrtodb(ump, bn * ump->um_seqinc);
176 ufs2_daddr_t bnb = bn;
177 for (++bn; bn < NDADDR && *runp < maxrun &&
178 is_sequential(ump, DIP(ip, i_db[bn - 1]),
182 if (runb && (bn > 0)) {
183 for (--bn; (bn >= 0) && (*runb < maxrun) &&
184 is_sequential(ump, DIP(ip, i_db[bn]),
185 DIP(ip, i_db[bn+1]));
193 /* Get disk address out of indirect block array */
194 daddr = DIP(ip, i_ib[ap->in_off]);
196 for (bp = NULL, ++ap; --num; ++ap) {
198 * Exit the loop if there is no disk address assigned yet and
199 * the indirect block isn't in the cache, or if we were
200 * looking for an indirect block and we've found it.
203 metalbn = ap->in_lbn;
204 if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
207 * If we get here, we've either got the block in the cache
208 * or we have a disk address for it, go fetch it.
213 bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0, 0);
214 if ((bp->b_flags & B_CACHE) == 0) {
217 panic("ufs_bmaparray: indirect block not in cache");
219 bp->b_blkno = blkptrtodb(ump, daddr);
220 bp->b_iocmd = BIO_READ;
221 bp->b_flags &= ~B_INVAL;
222 bp->b_ioflags &= ~BIO_ERROR;
223 vfs_busy_pages(bp, 0);
224 bp->b_iooffset = dbtob(bp->b_blkno);
226 curthread->td_ru.ru_inblock++;
234 if (ip->i_ump->um_fstype == UFS1) {
235 daddr = ((ufs1_daddr_t *)bp->b_data)[ap->in_off];
236 if (num == 1 && daddr && runp) {
237 for (bn = ap->in_off + 1;
238 bn < MNINDIR(ump) && *runp < maxrun &&
240 ((ufs1_daddr_t *)bp->b_data)[bn - 1],
241 ((ufs1_daddr_t *)bp->b_data)[bn]);
245 for (--bn; bn >= 0 && *runb < maxrun &&
247 ((ufs1_daddr_t *)bp->b_data)[bn],
248 ((ufs1_daddr_t *)bp->b_data)[bn+1]);
254 daddr = ((ufs2_daddr_t *)bp->b_data)[ap->in_off];
255 if (num == 1 && daddr && runp) {
256 for (bn = ap->in_off + 1;
257 bn < MNINDIR(ump) && *runp < maxrun &&
259 ((ufs2_daddr_t *)bp->b_data)[bn - 1],
260 ((ufs2_daddr_t *)bp->b_data)[bn]);
264 for (--bn; bn >= 0 && *runb < maxrun &&
266 ((ufs2_daddr_t *)bp->b_data)[bn],
267 ((ufs2_daddr_t *)bp->b_data)[bn + 1]);
276 * Since this is FFS independent code, we are out of scope for the
277 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
278 * will fall in the range 1..um_seqinc, so we use that test and
279 * return a request for a zeroed out buffer if attempts are made
280 * to read a BLK_NOCOPY or BLK_SNAP block.
282 if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc){
286 *bnp = blkptrtodb(ump, daddr);
288 if (ip->i_flags & SF_SNAPSHOT)
289 *bnp = blkptrtodb(ump, bn * ump->um_seqinc);
297 * Create an array of logical block number/offset pairs which represent the
298 * path of indirect blocks required to access a data block. The first "pair"
299 * contains the logical block number of the appropriate single, double or
300 * triple indirect block and the offset into the inode indirect block array.
301 * Note, the logical block number of the inode single/double/triple indirect
302 * block appears twice in the array, once with the offset into the i_ib and
303 * once with the offset into the page itself.
306 ufs_getlbns(vp, bn, ap, nump)
312 ufs2_daddr_t blockcnt;
313 ufs_lbn_t metalbn, realbn;
314 struct ufsmount *ump;
315 int i, numlevels, off;
317 ump = VFSTOUFS(vp->v_mount);
325 /* The first NDADDR blocks are direct blocks. */
330 * Determine the number of levels of indirection. After this loop
331 * is done, blockcnt indicates the number of data blocks possible
332 * at the previous level of indirection, and NIADDR - i is the number
333 * of levels of indirection needed to locate the requested block.
335 for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
338 blockcnt *= MNINDIR(ump);
343 /* Calculate the address of the first meta-block. */
345 metalbn = -(realbn - bn + NIADDR - i);
347 metalbn = -(-realbn - bn + NIADDR - i);
350 * At each iteration, off is the offset into the bap array which is
351 * an array of disk addresses at the current level of indirection.
352 * The logical block number and the offset in that block are stored
353 * into the argument array.
355 ap->in_lbn = metalbn;
356 ap->in_off = off = NIADDR - i;
358 for (++numlevels; i <= NIADDR; i++) {
359 /* If searching for a meta-data block, quit when found. */
360 if (metalbn == realbn)
363 blockcnt /= MNINDIR(ump);
364 off = (bn / blockcnt) % MNINDIR(ump);
367 ap->in_lbn = metalbn;
371 metalbn -= -1 + off * blockcnt;