1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)ufs_bmap.c 8.7 (Berkeley) 3/21/95 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/bio.h> 42 #include <sys/buf.h> 43 #include <sys/endian.h> 44 #include <sys/proc.h> 45 #include <sys/vnode.h> 46 #include <sys/mount.h> 47 #include <sys/racct.h> 48 #include <sys/resourcevar.h> 49 #include <sys/stat.h> 50 51 #include <fs/ext2fs/fs.h> 52 #include <fs/ext2fs/inode.h> 53 #include <fs/ext2fs/ext2fs.h> 54 #include <fs/ext2fs/ext2_dinode.h> 55 #include <fs/ext2fs/ext2_extern.h> 56 #include <fs/ext2fs/ext2_mount.h> 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 * Convert the logical block number of a file to its physical block number 90 * on the disk within ext4 extents. 91 */ 92 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 mount *mp; 98 struct ext2mount *ump; 99 struct ext4_extent_header *ehp; 100 struct ext4_extent *ep; 101 struct ext4_extent_path *path = NULL; 102 daddr_t lbn; 103 int error, depth, maxrun = 0, bsize; 104 105 ip = VTOI(vp); 106 fs = ip->i_e2fs; 107 mp = vp->v_mount; 108 ump = VFSTOEXT2(mp); 109 lbn = bn; 110 ehp = (struct ext4_extent_header *)ip->i_data; 111 depth = le16toh(ehp->eh_depth); 112 bsize = EXT2_BLOCK_SIZE(ump->um_e2fs); 113 114 *bnp = -1; 115 if (runp != NULL) { 116 maxrun = mp->mnt_iosize_max / bsize - 1; 117 *runp = 0; 118 } 119 if (runb != NULL) 120 *runb = 0; 121 122 error = ext4_ext_find_extent(ip, lbn, &path); 123 if (error) 124 return (error); 125 126 ep = path[depth].ep_ext; 127 if(ep) { 128 if (lbn < le32toh(ep->e_blk)) { 129 if (runp != NULL) { 130 *runp = min(maxrun, le32toh(ep->e_blk) - lbn - 1); 131 } 132 } else if (le32toh(ep->e_blk) <= lbn && 133 lbn < le32toh(ep->e_blk) + le16toh(ep->e_len)) { 134 *bnp = fsbtodb(fs, lbn - le32toh(ep->e_blk) + 135 (le32toh(ep->e_start_lo) | 136 (daddr_t)le16toh(ep->e_start_hi) << 32)); 137 if (runp != NULL) { 138 *runp = min(maxrun, 139 le16toh(ep->e_len) - 140 (lbn - le32toh(ep->e_blk)) - 1); 141 } 142 if (runb != NULL) 143 *runb = min(maxrun, lbn - le32toh(ep->e_blk)); 144 } else { 145 if (runb != NULL) 146 *runb = min(maxrun, le32toh(ep->e_blk) + lbn - 147 le16toh(ep->e_len)); 148 } 149 } 150 151 ext4_ext_path_free(path); 152 153 return (error); 154 } 155 156 static int 157 readindir(struct vnode *vp, e2fs_lbn_t lbn, e2fs_daddr_t daddr, struct buf **bpp) 158 { 159 struct buf *bp; 160 struct mount *mp; 161 struct ext2mount *ump; 162 int error; 163 164 mp = vp->v_mount; 165 ump = VFSTOEXT2(mp); 166 167 bp = getblk(vp, lbn, mp->mnt_stat.f_iosize, 0, 0, 0); 168 if ((bp->b_flags & B_CACHE) == 0) { 169 KASSERT(daddr != 0, 170 ("readindir: indirect block not in cache")); 171 172 bp->b_blkno = blkptrtodb(ump, daddr); 173 bp->b_iocmd = BIO_READ; 174 bp->b_flags &= ~B_INVAL; 175 bp->b_ioflags &= ~BIO_ERROR; 176 vfs_busy_pages(bp, 0); 177 bp->b_iooffset = dbtob(bp->b_blkno); 178 bstrategy(bp); 179 #ifdef RACCT 180 if (racct_enable) { 181 PROC_LOCK(curproc); 182 racct_add_buf(curproc, bp, 0); 183 PROC_UNLOCK(curproc); 184 } 185 #endif 186 curthread->td_ru.ru_inblock++; 187 error = bufwait(bp); 188 if (error != 0) { 189 brelse(bp); 190 return (error); 191 } 192 } 193 *bpp = bp; 194 return (0); 195 } 196 197 /* 198 * Indirect blocks are now on the vnode for the file. They are given negative 199 * logical block numbers. Indirect blocks are addressed by the negative 200 * address of the first data block to which they point. Double indirect blocks 201 * are addressed by one less than the address of the first indirect block to 202 * which they point. Triple indirect blocks are addressed by one less than 203 * the address of the first double indirect block to which they point. 204 * 205 * ext2_bmaparray does the bmap conversion, and if requested returns the 206 * array of logical blocks which must be traversed to get to a block. 207 * Each entry contains the offset into that block that gets you to the 208 * next block and the disk address of the block (if it is assigned). 209 */ 210 211 int 212 ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb) 213 { 214 struct inode *ip; 215 struct buf *bp; 216 struct ext2mount *ump; 217 struct mount *mp; 218 struct indir a[EXT2_NIADDR + 1], *ap; 219 daddr_t daddr; 220 e2fs_lbn_t metalbn; 221 int error, num, maxrun = 0, bsize; 222 int *nump; 223 224 ap = NULL; 225 ip = VTOI(vp); 226 mp = vp->v_mount; 227 ump = VFSTOEXT2(mp); 228 229 bsize = EXT2_BLOCK_SIZE(ump->um_e2fs); 230 231 if (runp) { 232 maxrun = mp->mnt_iosize_max / bsize - 1; 233 *runp = 0; 234 } 235 if (runb) 236 *runb = 0; 237 238 ap = a; 239 nump = # 240 error = ext2_getlbns(vp, bn, ap, nump); 241 if (error) 242 return (error); 243 244 num = *nump; 245 if (num == 0) { 246 *bnp = blkptrtodb(ump, ip->i_db[bn]); 247 if (*bnp == 0) { 248 *bnp = -1; 249 } else if (runp) { 250 daddr_t bnb = bn; 251 252 for (++bn; bn < EXT2_NDADDR && *runp < maxrun && 253 is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]); 254 ++bn, ++*runp); 255 bn = bnb; 256 if (runb && (bn > 0)) { 257 for (--bn; (bn >= 0) && (*runb < maxrun) && 258 is_sequential(ump, ip->i_db[bn], 259 ip->i_db[bn + 1]); 260 --bn, ++*runb); 261 } 262 } 263 return (0); 264 } 265 266 /* Get disk address out of indirect block array */ 267 daddr = ip->i_ib[ap->in_off]; 268 269 for (bp = NULL, ++ap; --num; ++ap) { 270 /* 271 * Exit the loop if there is no disk address assigned yet and 272 * the indirect block isn't in the cache, or if we were 273 * looking for an indirect block and we've found it. 274 */ 275 276 metalbn = ap->in_lbn; 277 if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn) 278 break; 279 /* 280 * If we get here, we've either got the block in the cache 281 * or we have a disk address for it, go fetch it. 282 */ 283 if (bp) 284 bqrelse(bp); 285 error = readindir(vp, metalbn, daddr, &bp); 286 if (error != 0) 287 return (error); 288 289 daddr = le32toh(((e2fs_daddr_t *)bp->b_data)[ap->in_off]); 290 if (num == 1 && daddr && runp) { 291 for (bn = ap->in_off + 1; 292 bn < MNINDIR(ump) && *runp < maxrun && 293 is_sequential(ump, 294 ((e2fs_daddr_t *)bp->b_data)[bn - 1], 295 ((e2fs_daddr_t *)bp->b_data)[bn]); 296 ++bn, ++*runp); 297 bn = ap->in_off; 298 if (runb && bn) { 299 for (--bn; bn >= 0 && *runb < maxrun && 300 is_sequential(ump, 301 ((e2fs_daddr_t *)bp->b_data)[bn], 302 ((e2fs_daddr_t *)bp->b_data)[bn + 1]); 303 --bn, ++*runb); 304 } 305 } 306 } 307 if (bp) 308 bqrelse(bp); 309 310 *bnp = blkptrtodb(ump, daddr); 311 if (*bnp == 0) { 312 *bnp = -1; 313 } 314 return (0); 315 } 316 317 static e2fs_lbn_t 318 lbn_count(struct ext2mount *ump, int level) 319 320 { 321 e2fs_lbn_t blockcnt; 322 323 for (blockcnt = 1; level > 0; level--) 324 blockcnt *= MNINDIR(ump); 325 return (blockcnt); 326 } 327 328 int 329 ext2_bmap_seekdata(struct vnode *vp, off_t *offp) 330 { 331 struct buf *bp; 332 struct indir a[EXT2_NIADDR + 1], *ap; 333 struct inode *ip; 334 struct mount *mp; 335 struct ext2mount *ump; 336 e2fs_daddr_t bn, daddr, nextbn; 337 uint64_t bsize; 338 off_t numblks; 339 int error, num, num1, off; 340 341 bp = NULL; 342 error = 0; 343 ip = VTOI(vp); 344 mp = vp->v_mount; 345 ump = VFSTOEXT2(mp); 346 347 if (vp->v_type != VREG) 348 return (EINVAL); 349 if (*offp < 0 || *offp >= ip->i_size) 350 return (ENXIO); 351 352 bsize = mp->mnt_stat.f_iosize; 353 for (bn = *offp / bsize, numblks = howmany(ip->i_size, bsize); 354 bn < numblks; bn = nextbn) { 355 if (bn < EXT2_NDADDR) { 356 daddr = ip->i_db[bn]; 357 if (daddr != 0) 358 break; 359 nextbn = bn + 1; 360 continue; 361 } 362 363 ap = a; 364 error = ext2_getlbns(vp, bn, ap, &num); 365 if (error != 0) 366 break; 367 MPASS(num >= 2); 368 daddr = ip->i_ib[ap->in_off]; 369 ap++, num--; 370 for (nextbn = EXT2_NDADDR, num1 = num - 1; num1 > 0; num1--) 371 nextbn += lbn_count(ump, num1); 372 if (daddr == 0) { 373 nextbn += lbn_count(ump, num); 374 continue; 375 } 376 377 for (; daddr != 0 && num > 0; ap++, num--) { 378 if (bp != NULL) 379 bqrelse(bp); 380 error = readindir(vp, ap->in_lbn, daddr, &bp); 381 if (error != 0) 382 return (error); 383 384 /* 385 * Scan the indirect block until we find a non-zero 386 * pointer. 387 */ 388 off = ap->in_off; 389 do { 390 daddr = le32toh(((e2fs_daddr_t *)bp->b_data)[off]); 391 } while (daddr == 0 && ++off < MNINDIR(ump)); 392 nextbn += off * lbn_count(ump, num - 1); 393 394 /* 395 * We need to recompute the LBNs of indirect 396 * blocks, so restart with the updated block offset. 397 */ 398 if (off != ap->in_off) 399 break; 400 } 401 if (num == 0) { 402 /* 403 * We found a data block. 404 */ 405 bn = nextbn; 406 break; 407 } 408 } 409 if (bp != NULL) 410 bqrelse(bp); 411 if (bn >= numblks) 412 error = ENXIO; 413 if (error == 0 && *offp < bn * bsize) 414 *offp = bn * bsize; 415 return (error); 416 } 417 418 /* 419 * Create an array of logical block number/offset pairs which represent the 420 * path of indirect blocks required to access a data block. The first "pair" 421 * contains the logical block number of the appropriate single, double or 422 * triple indirect block and the offset into the inode indirect block array. 423 * Note, the logical block number of the inode single/double/triple indirect 424 * block appears twice in the array, once with the offset into the i_ib and 425 * once with the offset into the page itself. 426 */ 427 int 428 ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump) 429 { 430 long blockcnt; 431 e2fs_lbn_t metalbn, realbn; 432 struct ext2mount *ump; 433 int i, numlevels, off; 434 int64_t qblockcnt; 435 436 ump = VFSTOEXT2(vp->v_mount); 437 if (nump) 438 *nump = 0; 439 numlevels = 0; 440 realbn = bn; 441 if ((long)bn < 0) 442 bn = -(long)bn; 443 444 /* The first EXT2_NDADDR blocks are direct blocks. */ 445 if (bn < EXT2_NDADDR) 446 return (0); 447 448 /* 449 * Determine the number of levels of indirection. After this loop 450 * is done, blockcnt indicates the number of data blocks possible 451 * at the previous level of indirection, and EXT2_NIADDR - i is the 452 * number of levels of indirection needed to locate the requested block. 453 */ 454 for (blockcnt = 1, i = EXT2_NIADDR, bn -= EXT2_NDADDR; ; 455 i--, bn -= blockcnt) { 456 if (i == 0) 457 return (EFBIG); 458 /* 459 * Use int64_t's here to avoid overflow for triple indirect 460 * blocks when longs have 32 bits and the block size is more 461 * than 4K. 462 */ 463 qblockcnt = (int64_t)blockcnt * MNINDIR(ump); 464 if (bn < qblockcnt) 465 break; 466 blockcnt = qblockcnt; 467 } 468 469 /* Calculate the address of the first meta-block. */ 470 if (realbn >= 0) 471 metalbn = -(realbn - bn + EXT2_NIADDR - i); 472 else 473 metalbn = -(-realbn - bn + EXT2_NIADDR - i); 474 475 /* 476 * At each iteration, off is the offset into the bap array which is 477 * an array of disk addresses at the current level of indirection. 478 * The logical block number and the offset in that block are stored 479 * into the argument array. 480 */ 481 ap->in_lbn = metalbn; 482 ap->in_off = off = EXT2_NIADDR - i; 483 ap++; 484 for (++numlevels; i <= EXT2_NIADDR; i++) { 485 /* If searching for a meta-data block, quit when found. */ 486 if (metalbn == realbn) 487 break; 488 489 off = (bn / blockcnt) % MNINDIR(ump); 490 491 ++numlevels; 492 ap->in_lbn = metalbn; 493 ap->in_off = off; 494 ++ap; 495 496 metalbn -= -1 + off * blockcnt; 497 blockcnt /= MNINDIR(ump); 498 } 499 if (nump) 500 *nump = numlevels; 501 return (0); 502 } 503