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/endian.h> 45 #include <sys/proc.h> 46 #include <sys/vnode.h> 47 #include <sys/mount.h> 48 #include <sys/resourcevar.h> 49 #include <sys/stat.h> 50 51 #include <vfs/ext2fs/fs.h> 52 #include <vfs/ext2fs/inode.h> 53 #include <vfs/ext2fs/ext2fs.h> 54 #include <vfs/ext2fs/ext2_dinode.h> 55 #include <vfs/ext2fs/ext2_extern.h> 56 #include <vfs/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 * BMAP must return the contiguous before and after run in bytes, inclusive 64 * of the returned block. 65 */ 66 int 67 ext2_bmap(struct vop_bmap_args *ap) 68 { 69 struct m_ext2fs *fs; 70 daddr_t lbn, dbn; 71 int error; 72 73 /* 74 * Check for underlying vnode requests and ensure that logical 75 * to physical mapping is requested. 76 */ 77 if (ap->a_doffsetp == NULL) 78 return (0); 79 80 fs = VTOI(ap->a_vp)->i_e2fs; 81 KKASSERT(((int)ap->a_loffset & ((1 << fs->e2fs_bshift) - 1)) == 0); 82 lbn = ap->a_loffset >> fs->e2fs_bshift; 83 84 if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS) 85 error = ext4_bmapext(ap->a_vp, lbn, &dbn, ap->a_runp, 86 ap->a_runb); 87 else 88 error = ext2_bmaparray(ap->a_vp, lbn, &dbn, ap->a_runp, 89 ap->a_runb); 90 91 if (error || dbn == (daddr_t)-1) { 92 *ap->a_doffsetp = NOOFFSET; 93 } else { 94 *ap->a_doffsetp = dbtodoff(fs, dbn); 95 if (ap->a_runp) 96 *ap->a_runp = (*ap->a_runp + 1) << fs->e2fs_bshift; 97 if (ap->a_runb) 98 *ap->a_runb = *ap->a_runb << fs->e2fs_bshift; 99 } 100 return (error); 101 } 102 103 /* 104 * Convert the logical block number of a file to its physical block number 105 * on the disk within ext4 extents. 106 */ 107 int 108 ext4_bmapext(struct vnode *vp, int32_t bn, daddr_t/*int64_t*/ *bnp, int *runp, int *runb) 109 { 110 return (EINVAL); 111 } 112 113 static int 114 readindir(struct vnode *vp, e2fs_lbn_t lbn, e2fs_daddr_t daddr, struct buf **bpp) 115 { 116 struct buf *bp; 117 struct mount *mp; 118 struct ext2mount *ump; 119 struct m_ext2fs *fs; 120 int error; 121 122 mp = vp->v_mount; 123 ump = VFSTOEXT2(mp); 124 fs = VTOI(vp)->i_e2fs; 125 126 bp = getblk(vp, lblktodoff(fs, lbn), mp->mnt_stat.f_iosize, 0, 0); 127 if ((bp->b_flags & B_CACHE) == 0) { 128 KASSERT(daddr != 0, 129 ("readindir: indirect block not in cache")); 130 /* 131 * This runs through ext2_strategy using bio2 to 132 * cache the disk offset, then comes back through 133 * bio1. So we want to wait on bio1. 134 */ 135 bp->b_bio1.bio_done = biodone_sync; 136 bp->b_bio1.bio_flags |= BIO_SYNC; 137 bp->b_bio2.bio_offset = fsbtodoff(fs, daddr); 138 bp->b_flags &= ~(B_INVAL | B_ERROR); 139 bp->b_cmd = BUF_CMD_READ; 140 vfs_busy_pages(bp->b_vp, bp); 141 vn_strategy(bp->b_vp, &bp->b_bio1); 142 #ifdef RACCT 143 if (racct_enable) { 144 PROC_LOCK(curproc); 145 racct_add_buf(curproc, bp, 0); 146 PROC_UNLOCK(curproc); 147 } 148 #endif 149 error = biowait(&bp->b_bio1, "biord"); 150 if (error) { 151 brelse(bp); 152 return (error); 153 } 154 } 155 *bpp = bp; 156 return (0); 157 } 158 159 /* 160 * Indirect blocks are now on the vnode for the file. They are given negative 161 * logical block numbers. Indirect blocks are addressed by the negative 162 * address of the first data block to which they point. Double indirect blocks 163 * are addressed by one less than the address of the first indirect block to 164 * which they point. Triple indirect blocks are addressed by one less than 165 * the address of the first double indirect block to which they point. 166 * 167 * ext2_bmaparray does the bmap conversion, and if requested returns the 168 * array of logical blocks which must be traversed to get to a block. 169 * Each entry contains the offset into that block that gets you to the 170 * next block and the disk address of the block (if it is assigned). 171 */ 172 173 int 174 ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb) 175 { 176 struct inode *ip; 177 struct buf *bp; 178 struct ext2mount *ump; 179 struct mount *mp; 180 struct m_ext2fs *fs; 181 struct indir a[EXT2_NIADDR + 1], *ap; 182 daddr_t daddr; 183 e2fs_lbn_t metalbn; 184 int error, num, maxrun = 0, bsize; 185 int *nump; 186 187 ap = NULL; 188 ip = VTOI(vp); 189 mp = vp->v_mount; 190 ump = VFSTOEXT2(mp); 191 fs = ip->i_e2fs; 192 193 bsize = EXT2_BLOCK_SIZE(ump->um_e2fs); 194 195 if (runp) { 196 maxrun = mp->mnt_iosize_max / bsize - 1; 197 *runp = 0; 198 } 199 if (runb) 200 *runb = 0; 201 202 203 ap = a; 204 nump = # 205 error = ext2_getlbns(vp, bn, ap, nump); 206 if (error) 207 return (error); 208 209 num = *nump; 210 if (num == 0) { 211 *bnp = blkptrtodb(ump, ip->i_db[bn]); 212 if (*bnp == 0) { 213 *bnp = -1; 214 } else if (runp) { 215 daddr_t bnb = bn; 216 217 for (++bn; bn < EXT2_NDADDR && *runp < maxrun && 218 is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]); 219 ++bn, ++*runp); 220 bn = bnb; 221 if (runb && (bn > 0)) { 222 for (--bn; (bn >= 0) && (*runb < maxrun) && 223 is_sequential(ump, ip->i_db[bn], 224 ip->i_db[bn + 1]); 225 --bn, ++*runb); 226 } 227 } 228 return (0); 229 } 230 231 /* Get disk address out of indirect block array */ 232 daddr = ip->i_ib[ap->in_off]; 233 234 for (bp = NULL, ++ap; --num; ++ap) { 235 /* 236 * Exit the loop if there is no disk address assigned yet and 237 * the indirect block isn't in the cache, or if we were 238 * looking for an indirect block and we've found it. 239 */ 240 241 metalbn = ap->in_lbn; 242 if ((daddr == 0 && 243 !findblk(vp, dbtodoff(fs, metalbn), FINDBLK_TEST)) || 244 metalbn == bn) 245 break; 246 /* 247 * If we get here, we've either got the block in the cache 248 * or we have a disk address for it, go fetch it. 249 */ 250 if (bp) 251 bqrelse(bp); 252 error = readindir(vp, metalbn, daddr, &bp); 253 if (error != 0) 254 return (error); 255 256 daddr = le32toh(((e2fs_daddr_t *)bp->b_data)[ap->in_off]); 257 if (num == 1 && daddr && runp) { 258 for (bn = ap->in_off + 1; 259 bn < MNINDIR(ump) && *runp < maxrun && 260 is_sequential(ump, 261 ((e2fs_daddr_t *)bp->b_data)[bn - 1], 262 ((e2fs_daddr_t *)bp->b_data)[bn]); 263 ++bn, ++*runp); 264 bn = ap->in_off; 265 if (runb && bn) { 266 for (--bn; bn >= 0 && *runb < maxrun && 267 is_sequential(ump, 268 ((e2fs_daddr_t *)bp->b_data)[bn], 269 ((e2fs_daddr_t *)bp->b_data)[bn + 1]); 270 --bn, ++*runb); 271 } 272 } 273 } 274 if (bp) 275 bqrelse(bp); 276 277 *bnp = blkptrtodb(ump, daddr); 278 if (*bnp == 0) { 279 *bnp = -1; 280 } 281 return (0); 282 } 283 284 /* 285 * Create an array of logical block number/offset pairs which represent the 286 * path of indirect blocks required to access a data block. The first "pair" 287 * contains the logical block number of the appropriate single, double or 288 * triple indirect block and the offset into the inode indirect block array. 289 * Note, the logical block number of the inode single/double/triple indirect 290 * block appears twice in the array, once with the offset into the i_ib and 291 * once with the offset into the page itself. 292 */ 293 int 294 ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump) 295 { 296 long blockcnt; 297 e2fs_lbn_t metalbn, realbn; 298 struct ext2mount *ump; 299 int i, numlevels, off; 300 int64_t qblockcnt; 301 302 ump = VFSTOEXT2(vp->v_mount); 303 if (nump) 304 *nump = 0; 305 numlevels = 0; 306 realbn = bn; 307 if ((long)bn < 0) 308 bn = -(long)bn; 309 310 /* The first EXT2_NDADDR blocks are direct blocks. */ 311 if (bn < EXT2_NDADDR) 312 return (0); 313 314 /* 315 * Determine the number of levels of indirection. After this loop 316 * is done, blockcnt indicates the number of data blocks possible 317 * at the previous level of indirection, and EXT2_NIADDR - i is the 318 * number of levels of indirection needed to locate the requested block. 319 */ 320 for (blockcnt = 1, i = EXT2_NIADDR, bn -= EXT2_NDADDR; ; 321 i--, bn -= blockcnt) { 322 if (i == 0) 323 return (EFBIG); 324 /* 325 * Use int64_t's here to avoid overflow for triple indirect 326 * blocks when longs have 32 bits and the block size is more 327 * than 4K. 328 */ 329 qblockcnt = (int64_t)blockcnt * MNINDIR(ump); 330 if (bn < qblockcnt) 331 break; 332 blockcnt = qblockcnt; 333 } 334 335 /* Calculate the address of the first meta-block. */ 336 if (realbn >= 0) 337 metalbn = -(realbn - bn + EXT2_NIADDR - i); 338 else 339 metalbn = -(-realbn - bn + EXT2_NIADDR - i); 340 341 /* 342 * At each iteration, off is the offset into the bap array which is 343 * an array of disk addresses at the current level of indirection. 344 * The logical block number and the offset in that block are stored 345 * into the argument array. 346 */ 347 ap->in_lbn = metalbn; 348 ap->in_off = off = EXT2_NIADDR - i; 349 ap++; 350 for (++numlevels; i <= EXT2_NIADDR; i++) { 351 /* If searching for a meta-data block, quit when found. */ 352 if (metalbn == realbn) 353 break; 354 355 off = (bn / blockcnt) % MNINDIR(ump); 356 357 ++numlevels; 358 ap->in_lbn = metalbn; 359 ap->in_off = off; 360 ++ap; 361 362 metalbn -= -1 + off * blockcnt; 363 blockcnt /= MNINDIR(ump); 364 } 365 if (nump) 366 *nump = numlevels; 367 return (0); 368 } 369