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/racct.h> 46 #include <sys/resourcevar.h> 47 #include <sys/stat.h> 48 49 #include <fs/ext2fs/inode.h> 50 #include <fs/ext2fs/fs.h> 51 #include <fs/ext2fs/ext2fs.h> 52 #include <fs/ext2fs/ext2_dinode.h> 53 #include <fs/ext2fs/ext2_extern.h> 54 #include <fs/ext2fs/ext2_mount.h> 55 56 static int ext4_bmapext(struct vnode *, int32_t, int64_t *, int *, int *); 57 58 /* 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. 62 */ 63 int 64 ext2_bmap(struct vop_bmap_args *ap) 65 { 66 daddr_t blkno; 67 int error; 68 69 /* 70 * Check for underlying vnode requests and ensure that logical 71 * to physical mapping is requested. 72 */ 73 if (ap->a_bop != NULL) 74 *ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj; 75 if (ap->a_bnp == NULL) 76 return (0); 77 78 if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS) 79 error = ext4_bmapext(ap->a_vp, ap->a_bn, &blkno, 80 ap->a_runp, ap->a_runb); 81 else 82 error = ext2_bmaparray(ap->a_vp, ap->a_bn, &blkno, 83 ap->a_runp, ap->a_runb); 84 *ap->a_bnp = blkno; 85 return (error); 86 } 87 88 /* 89 * This function converts the logical block number of a file to 90 * its physical block number on the disk within ext4 extents. 91 */ 92 static int 93 ext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb) 94 { 95 struct inode *ip; 96 struct m_ext2fs *fs; 97 struct ext4_extent *ep; 98 struct ext4_extent_path path = { .ep_bp = NULL }; 99 daddr_t lbn; 100 int ret = 0; 101 102 ip = VTOI(vp); 103 fs = ip->i_e2fs; 104 lbn = bn; 105 106 if (runp != NULL) 107 *runp = 0; 108 109 if (runb != NULL) 110 *runb = 0; 111 112 ext4_ext_find_extent(fs, ip, lbn, &path); 113 if (path.ep_is_sparse) { 114 *bnp = -1; 115 if (runp != NULL) 116 *runp = path.ep_sparse_ext.e_len - 117 (lbn - path.ep_sparse_ext.e_blk) - 1; 118 if (runb != NULL) 119 *runb = lbn - path.ep_sparse_ext.e_blk; 120 } else { 121 ep = path.ep_ext; 122 if (ep == NULL) 123 ret = EIO; 124 else { 125 *bnp = fsbtodb(fs, lbn - ep->e_blk + 126 (ep->e_start_lo | (daddr_t)ep->e_start_hi << 32)); 127 128 if (*bnp == 0) 129 *bnp = -1; 130 131 if (runp != NULL) 132 *runp = ep->e_len - (lbn - ep->e_blk) - 1; 133 if (runb != NULL) 134 *runb = lbn - ep->e_blk; 135 } 136 } 137 138 if (path.ep_bp != NULL) { 139 brelse(path.ep_bp); 140 path.ep_bp = NULL; 141 } 142 143 return (ret); 144 } 145 146 /* 147 * Indirect blocks are now on the vnode for the file. They are given negative 148 * logical block numbers. Indirect blocks are addressed by the negative 149 * address of the first data block to which they point. Double indirect blocks 150 * are addressed by one less than the address of the first indirect block to 151 * which they point. Triple indirect blocks are addressed by one less than 152 * the address of the first double indirect block to which they point. 153 * 154 * ext2_bmaparray does the bmap conversion, and if requested returns the 155 * array of logical blocks which must be traversed to get to a block. 156 * Each entry contains the offset into that block that gets you to the 157 * next block and the disk address of the block (if it is assigned). 158 */ 159 160 int 161 ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb) 162 { 163 struct inode *ip; 164 struct buf *bp; 165 struct ext2mount *ump; 166 struct mount *mp; 167 struct indir a[NIADDR+1], *ap; 168 daddr_t daddr; 169 e2fs_lbn_t metalbn; 170 int error, num, maxrun = 0, bsize; 171 int *nump; 172 173 ap = NULL; 174 ip = VTOI(vp); 175 mp = vp->v_mount; 176 ump = VFSTOEXT2(mp); 177 178 bsize = EXT2_BLOCK_SIZE(ump->um_e2fs); 179 180 if (runp) { 181 maxrun = mp->mnt_iosize_max / bsize - 1; 182 *runp = 0; 183 } 184 185 if (runb) { 186 *runb = 0; 187 } 188 189 190 ap = a; 191 nump = # 192 error = ext2_getlbns(vp, bn, ap, nump); 193 if (error) 194 return (error); 195 196 num = *nump; 197 if (num == 0) { 198 *bnp = blkptrtodb(ump, ip->i_db[bn]); 199 if (*bnp == 0) { 200 *bnp = -1; 201 } else if (runp) { 202 daddr_t bnb = bn; 203 for (++bn; bn < NDADDR && *runp < maxrun && 204 is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]); 205 ++bn, ++*runp); 206 bn = bnb; 207 if (runb && (bn > 0)) { 208 for (--bn; (bn >= 0) && (*runb < maxrun) && 209 is_sequential(ump, ip->i_db[bn], 210 ip->i_db[bn + 1]); 211 --bn, ++*runb); 212 } 213 } 214 return (0); 215 } 216 217 218 /* Get disk address out of indirect block array */ 219 daddr = ip->i_ib[ap->in_off]; 220 221 for (bp = NULL, ++ap; --num; ++ap) { 222 /* 223 * Exit the loop if there is no disk address assigned yet and 224 * the indirect block isn't in the cache, or if we were 225 * looking for an indirect block and we've found it. 226 */ 227 228 metalbn = ap->in_lbn; 229 if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn) 230 break; 231 /* 232 * If we get here, we've either got the block in the cache 233 * or we have a disk address for it, go fetch it. 234 */ 235 if (bp) 236 bqrelse(bp); 237 238 bp = getblk(vp, metalbn, bsize, 0, 0, 0); 239 if ((bp->b_flags & B_CACHE) == 0) { 240 #ifdef INVARIANTS 241 if (!daddr) 242 panic("ext2_bmaparray: indirect block not in cache"); 243 #endif 244 bp->b_blkno = blkptrtodb(ump, daddr); 245 bp->b_iocmd = BIO_READ; 246 bp->b_flags &= ~B_INVAL; 247 bp->b_ioflags &= ~BIO_ERROR; 248 vfs_busy_pages(bp, 0); 249 bp->b_iooffset = dbtob(bp->b_blkno); 250 bstrategy(bp); 251 #ifdef RACCT 252 if (racct_enable) { 253 PROC_LOCK(curproc); 254 racct_add_buf(curproc, bp, 0); 255 PROC_UNLOCK(curproc); 256 } 257 #endif 258 curthread->td_ru.ru_inblock++; 259 error = bufwait(bp); 260 if (error) { 261 brelse(bp); 262 return (error); 263 } 264 } 265 266 daddr = ((e2fs_daddr_t *)bp->b_data)[ap->in_off]; 267 if (num == 1 && daddr && runp) { 268 for (bn = ap->in_off + 1; 269 bn < MNINDIR(ump) && *runp < maxrun && 270 is_sequential(ump, 271 ((e2fs_daddr_t *)bp->b_data)[bn - 1], 272 ((e2fs_daddr_t *)bp->b_data)[bn]); 273 ++bn, ++*runp); 274 bn = ap->in_off; 275 if (runb && bn) { 276 for (--bn; bn >= 0 && *runb < maxrun && 277 is_sequential(ump, 278 ((e2fs_daddr_t *)bp->b_data)[bn], 279 ((e2fs_daddr_t *)bp->b_data)[bn + 1]); 280 --bn, ++*runb); 281 } 282 } 283 } 284 if (bp) 285 bqrelse(bp); 286 287 /* 288 * Since this is FFS independent code, we are out of scope for the 289 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they 290 * will fall in the range 1..um_seqinc, so we use that test and 291 * return a request for a zeroed out buffer if attempts are made 292 * to read a BLK_NOCOPY or BLK_SNAP block. 293 */ 294 if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc){ 295 *bnp = -1; 296 return (0); 297 } 298 *bnp = blkptrtodb(ump, daddr); 299 if (*bnp == 0) { 300 *bnp = -1; 301 } 302 return (0); 303 } 304 305 /* 306 * Create an array of logical block number/offset pairs which represent the 307 * path of indirect blocks required to access a data block. The first "pair" 308 * contains the logical block number of the appropriate single, double or 309 * triple indirect block and the offset into the inode indirect block array. 310 * Note, the logical block number of the inode single/double/triple indirect 311 * block appears twice in the array, once with the offset into the i_ib and 312 * once with the offset into the page itself. 313 */ 314 int 315 ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump) 316 { 317 long blockcnt; 318 e2fs_lbn_t metalbn, realbn; 319 struct ext2mount *ump; 320 int i, numlevels, off; 321 int64_t qblockcnt; 322 323 ump = VFSTOEXT2(vp->v_mount); 324 if (nump) 325 *nump = 0; 326 numlevels = 0; 327 realbn = bn; 328 if ((long)bn < 0) 329 bn = -(long)bn; 330 331 /* The first NDADDR blocks are direct blocks. */ 332 if (bn < NDADDR) 333 return (0); 334 335 /* 336 * Determine the number of levels of indirection. After this loop 337 * is done, blockcnt indicates the number of data blocks possible 338 * at the previous level of indirection, and NIADDR - i is the number 339 * of levels of indirection needed to locate the requested block. 340 */ 341 for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) { 342 if (i == 0) 343 return (EFBIG); 344 /* 345 * Use int64_t's here to avoid overflow for triple indirect 346 * blocks when longs have 32 bits and the block size is more 347 * than 4K. 348 */ 349 qblockcnt = (int64_t)blockcnt * MNINDIR(ump); 350 if (bn < qblockcnt) 351 break; 352 blockcnt = qblockcnt; 353 } 354 355 /* Calculate the address of the first meta-block. */ 356 if (realbn >= 0) 357 metalbn = -(realbn - bn + NIADDR - i); 358 else 359 metalbn = -(-realbn - bn + NIADDR - i); 360 361 /* 362 * At each iteration, off is the offset into the bap array which is 363 * an array of disk addresses at the current level of indirection. 364 * The logical block number and the offset in that block are stored 365 * into the argument array. 366 */ 367 ap->in_lbn = metalbn; 368 ap->in_off = off = NIADDR - i; 369 ap++; 370 for (++numlevels; i <= NIADDR; i++) { 371 /* If searching for a meta-data block, quit when found. */ 372 if (metalbn == realbn) 373 break; 374 375 off = (bn / blockcnt) % MNINDIR(ump); 376 377 ++numlevels; 378 ap->in_lbn = metalbn; 379 ap->in_off = off; 380 ++ap; 381 382 metalbn -= -1 + off * blockcnt; 383 blockcnt /= MNINDIR(ump); 384 } 385 if (nump) 386 *nump = numlevels; 387 return (0); 388 } 389