1 /*- 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. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 * 34 * @(#)ufs_bmap.c 8.7 (Berkeley) 3/21/95 35 * $FreeBSD$ 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/bio.h> 41 #include <sys/buf.h> 42 #include <sys/proc.h> 43 #include <sys/vnode.h> 44 #include <sys/mount.h> 45 #include <sys/resourcevar.h> 46 #include <sys/stat.h> 47 48 #include <fs/ext2fs/inode.h> 49 #include <fs/ext2fs/ext2fs.h> 50 #include <fs/ext2fs/ext2_mount.h> 51 #include <fs/ext2fs/ext2_extern.h> 52 53 /* 54 * Bmap converts the logical block number of a file to its physical block 55 * number on the disk. The conversion is done by using the logical block 56 * number to index into the array of block pointers described by the dinode. 57 */ 58 int 59 ext2_bmap(ap) 60 struct vop_bmap_args /* { 61 struct vnode *a_vp; 62 daddr_t a_bn; 63 struct bufobj **a_bop; 64 daddr_t *a_bnp; 65 int *a_runp; 66 int *a_runb; 67 } */ *ap; 68 { 69 int32_t blkno; 70 int error; 71 72 /* 73 * Check for underlying vnode requests and ensure that logical 74 * to physical mapping is requested. 75 */ 76 if (ap->a_bop != NULL) 77 *ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj; 78 if (ap->a_bnp == NULL) 79 return (0); 80 81 error = ext2_bmaparray(ap->a_vp, ap->a_bn, &blkno, 82 ap->a_runp, ap->a_runb); 83 *ap->a_bnp = blkno; 84 return (error); 85 } 86 87 /* 88 * Indirect blocks are now on the vnode for the file. They are given negative 89 * logical block numbers. Indirect blocks are addressed by the negative 90 * address of the first data block to which they point. Double indirect blocks 91 * are addressed by one less than the address of the first indirect block to 92 * which they point. Triple indirect blocks are addressed by one less than 93 * the address of the first double indirect block to which they point. 94 * 95 * ext2_bmaparray does the bmap conversion, and if requested returns the 96 * array of logical blocks which must be traversed to get to a block. 97 * Each entry contains the offset into that block that gets you to the 98 * next block and the disk address of the block (if it is assigned). 99 */ 100 101 int 102 ext2_bmaparray(vp, bn, bnp, runp, runb) 103 struct vnode *vp; 104 int32_t bn; 105 int32_t *bnp; 106 int *runp; 107 int *runb; 108 { 109 struct inode *ip; 110 struct buf *bp; 111 struct ext2mount *ump; 112 struct mount *mp; 113 struct vnode *devvp; 114 struct indir a[NIADDR+1], *ap; 115 int32_t daddr; 116 long metalbn; 117 int error, num, maxrun = 0, bsize; 118 int *nump; 119 120 ap = NULL; 121 ip = VTOI(vp); 122 mp = vp->v_mount; 123 ump = VFSTOEXT2(mp); 124 devvp = ump->um_devvp; 125 126 bsize = EXT2_BLOCK_SIZE(ump->um_e2fs); 127 128 if (runp) { 129 maxrun = mp->mnt_iosize_max / bsize - 1; 130 *runp = 0; 131 } 132 133 if (runb) { 134 *runb = 0; 135 } 136 137 138 ap = a; 139 nump = # 140 error = ext2_getlbns(vp, bn, ap, nump); 141 if (error) 142 return (error); 143 144 num = *nump; 145 if (num == 0) { 146 *bnp = blkptrtodb(ump, ip->i_db[bn]); 147 if (*bnp == 0) { 148 *bnp = -1; 149 } else if (runp) { 150 int32_t bnb = bn; 151 for (++bn; bn < NDADDR && *runp < maxrun && 152 is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]); 153 ++bn, ++*runp); 154 bn = bnb; 155 if (runb && (bn > 0)) { 156 for (--bn; (bn >= 0) && (*runb < maxrun) && 157 is_sequential(ump, ip->i_db[bn], 158 ip->i_db[bn+1]); 159 --bn, ++*runb); 160 } 161 } 162 return (0); 163 } 164 165 166 /* Get disk address out of indirect block array */ 167 daddr = ip->i_ib[ap->in_off]; 168 169 for (bp = NULL, ++ap; --num; ++ap) { 170 /* 171 * Exit the loop if there is no disk address assigned yet and 172 * the indirect block isn't in the cache, or if we were 173 * looking for an indirect block and we've found it. 174 */ 175 176 metalbn = ap->in_lbn; 177 if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn) 178 break; 179 /* 180 * If we get here, we've either got the block in the cache 181 * or we have a disk address for it, go fetch it. 182 */ 183 if (bp) 184 bqrelse(bp); 185 186 bp = getblk(vp, metalbn, bsize, 0, 0, 0); 187 if ((bp->b_flags & B_CACHE) == 0) { 188 #ifdef DIAGNOSTIC 189 if (!daddr) 190 panic("ext2_bmaparray: indirect block not in cache"); 191 #endif 192 bp->b_blkno = blkptrtodb(ump, daddr); 193 bp->b_iocmd = BIO_READ; 194 bp->b_flags &= ~B_INVAL; 195 bp->b_ioflags &= ~BIO_ERROR; 196 vfs_busy_pages(bp, 0); 197 bp->b_iooffset = dbtob(bp->b_blkno); 198 bstrategy(bp); 199 curthread->td_ru.ru_inblock++; 200 error = bufwait(bp); 201 if (error) { 202 brelse(bp); 203 return (error); 204 } 205 } 206 207 daddr = ((int32_t *)bp->b_data)[ap->in_off]; 208 if (num == 1 && daddr && runp) { 209 for (bn = ap->in_off + 1; 210 bn < MNINDIR(ump) && *runp < maxrun && 211 is_sequential(ump, 212 ((int32_t *)bp->b_data)[bn - 1], 213 ((int32_t *)bp->b_data)[bn]); 214 ++bn, ++*runp); 215 bn = ap->in_off; 216 if (runb && bn) { 217 for (--bn; bn >= 0 && *runb < maxrun && 218 is_sequential(ump, ((int32_t *)bp->b_data)[bn], 219 ((int32_t *)bp->b_data)[bn+1]); 220 --bn, ++*runb); 221 } 222 } 223 } 224 if (bp) 225 bqrelse(bp); 226 227 /* 228 * Since this is FFS independent code, we are out of scope for the 229 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they 230 * will fall in the range 1..um_seqinc, so we use that test and 231 * return a request for a zeroed out buffer if attempts are made 232 * to read a BLK_NOCOPY or BLK_SNAP block. 233 */ 234 if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc){ 235 *bnp = -1; 236 return (0); 237 } 238 *bnp = blkptrtodb(ump, daddr); 239 if (*bnp == 0) { 240 *bnp = -1; 241 } 242 return (0); 243 } 244 245 /* 246 * Create an array of logical block number/offset pairs which represent the 247 * path of indirect blocks required to access a data block. The first "pair" 248 * contains the logical block number of the appropriate single, double or 249 * triple indirect block and the offset into the inode indirect block array. 250 * Note, the logical block number of the inode single/double/triple indirect 251 * block appears twice in the array, once with the offset into the i_ib and 252 * once with the offset into the page itself. 253 */ 254 int 255 ext2_getlbns(vp, bn, ap, nump) 256 struct vnode *vp; 257 int32_t bn; 258 struct indir *ap; 259 int *nump; 260 { 261 long blockcnt, metalbn, realbn; 262 struct ext2mount *ump; 263 int i, numlevels, off; 264 int64_t qblockcnt; 265 266 ump = VFSTOEXT2(vp->v_mount); 267 if (nump) 268 *nump = 0; 269 numlevels = 0; 270 realbn = bn; 271 if ((long)bn < 0) 272 bn = -(long)bn; 273 274 /* The first NDADDR blocks are direct blocks. */ 275 if (bn < NDADDR) 276 return (0); 277 278 /* 279 * Determine the number of levels of indirection. After this loop 280 * is done, blockcnt indicates the number of data blocks possible 281 * at the previous level of indirection, and NIADDR - i is the number 282 * of levels of indirection needed to locate the requested block. 283 */ 284 for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) { 285 if (i == 0) 286 return (EFBIG); 287 /* 288 * Use int64_t's here to avoid overflow for triple indirect 289 * blocks when longs have 32 bits and the block size is more 290 * than 4K. 291 */ 292 qblockcnt = (int64_t)blockcnt * MNINDIR(ump); 293 if (bn < qblockcnt) 294 break; 295 blockcnt = qblockcnt; 296 } 297 298 /* Calculate the address of the first meta-block. */ 299 if (realbn >= 0) 300 metalbn = -(realbn - bn + NIADDR - i); 301 else 302 metalbn = -(-realbn - bn + NIADDR - i); 303 304 /* 305 * At each iteration, off is the offset into the bap array which is 306 * an array of disk addresses at the current level of indirection. 307 * The logical block number and the offset in that block are stored 308 * into the argument array. 309 */ 310 ap->in_lbn = metalbn; 311 ap->in_off = off = NIADDR - i; 312 ap++; 313 for (++numlevels; i <= NIADDR; i++) { 314 /* If searching for a meta-data block, quit when found. */ 315 if (metalbn == realbn) 316 break; 317 318 off = (bn / blockcnt) % MNINDIR(ump); 319 320 ++numlevels; 321 ap->in_lbn = metalbn; 322 ap->in_off = off; 323 ++ap; 324 325 metalbn -= -1 + off * blockcnt; 326 blockcnt /= MNINDIR(ump); 327 } 328 if (nump) 329 *nump = numlevels; 330 return (0); 331 } 332