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