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 ap = a; 203 nump = # 204 error = ext2_getlbns(vp, bn, ap, nump); 205 if (error) 206 return (error); 207 208 num = *nump; 209 if (num == 0) { 210 *bnp = blkptrtodb(ump, ip->i_db[bn]); 211 if (*bnp == 0) { 212 *bnp = -1; 213 } else if (runp) { 214 daddr_t bnb = bn; 215 216 for (++bn; bn < EXT2_NDADDR && *runp < maxrun && 217 is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]); 218 ++bn, ++*runp); 219 bn = bnb; 220 if (runb && (bn > 0)) { 221 for (--bn; (bn >= 0) && (*runb < maxrun) && 222 is_sequential(ump, ip->i_db[bn], 223 ip->i_db[bn + 1]); 224 --bn, ++*runb); 225 } 226 } 227 return (0); 228 } 229 230 /* Get disk address out of indirect block array */ 231 daddr = ip->i_ib[ap->in_off]; 232 233 for (bp = NULL, ++ap; --num; ++ap) { 234 /* 235 * Exit the loop if there is no disk address assigned yet and 236 * the indirect block isn't in the cache, or if we were 237 * looking for an indirect block and we've found it. 238 */ 239 240 metalbn = ap->in_lbn; 241 if ((daddr == 0 && 242 !findblk(vp, dbtodoff(fs, metalbn), FINDBLK_TEST)) || 243 metalbn == bn) 244 break; 245 /* 246 * If we get here, we've either got the block in the cache 247 * or we have a disk address for it, go fetch it. 248 */ 249 if (bp) 250 bqrelse(bp); 251 error = readindir(vp, metalbn, daddr, &bp); 252 if (error != 0) 253 return (error); 254 255 daddr = le32toh(((e2fs_daddr_t *)bp->b_data)[ap->in_off]); 256 if (num == 1 && daddr && runp) { 257 for (bn = ap->in_off + 1; 258 bn < MNINDIR(ump) && *runp < maxrun && 259 is_sequential(ump, 260 ((e2fs_daddr_t *)bp->b_data)[bn - 1], 261 ((e2fs_daddr_t *)bp->b_data)[bn]); 262 ++bn, ++*runp); 263 bn = ap->in_off; 264 if (runb && bn) { 265 for (--bn; bn >= 0 && *runb < maxrun && 266 is_sequential(ump, 267 ((e2fs_daddr_t *)bp->b_data)[bn], 268 ((e2fs_daddr_t *)bp->b_data)[bn + 1]); 269 --bn, ++*runb); 270 } 271 } 272 } 273 if (bp) 274 bqrelse(bp); 275 276 *bnp = blkptrtodb(ump, daddr); 277 if (*bnp == 0) { 278 *bnp = -1; 279 } 280 return (0); 281 } 282 283 /* 284 * Create an array of logical block number/offset pairs which represent the 285 * path of indirect blocks required to access a data block. The first "pair" 286 * contains the logical block number of the appropriate single, double or 287 * triple indirect block and the offset into the inode indirect block array. 288 * Note, the logical block number of the inode single/double/triple indirect 289 * block appears twice in the array, once with the offset into the i_ib and 290 * once with the offset into the page itself. 291 */ 292 int 293 ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump) 294 { 295 long blockcnt; 296 e2fs_lbn_t metalbn, realbn; 297 struct ext2mount *ump; 298 int i, numlevels, off; 299 int64_t qblockcnt; 300 301 ump = VFSTOEXT2(vp->v_mount); 302 if (nump) 303 *nump = 0; 304 numlevels = 0; 305 realbn = bn; 306 if ((long)bn < 0) 307 bn = -(long)bn; 308 309 /* The first EXT2_NDADDR blocks are direct blocks. */ 310 if (bn < EXT2_NDADDR) 311 return (0); 312 313 /* 314 * Determine the number of levels of indirection. After this loop 315 * is done, blockcnt indicates the number of data blocks possible 316 * at the previous level of indirection, and EXT2_NIADDR - i is the 317 * number of levels of indirection needed to locate the requested block. 318 */ 319 for (blockcnt = 1, i = EXT2_NIADDR, bn -= EXT2_NDADDR; ; 320 i--, bn -= blockcnt) { 321 if (i == 0) 322 return (EFBIG); 323 /* 324 * Use int64_t's here to avoid overflow for triple indirect 325 * blocks when longs have 32 bits and the block size is more 326 * than 4K. 327 */ 328 qblockcnt = (int64_t)blockcnt * MNINDIR(ump); 329 if (bn < qblockcnt) 330 break; 331 blockcnt = qblockcnt; 332 } 333 334 /* Calculate the address of the first meta-block. */ 335 if (realbn >= 0) 336 metalbn = -(realbn - bn + EXT2_NIADDR - i); 337 else 338 metalbn = -(-realbn - bn + EXT2_NIADDR - i); 339 340 /* 341 * At each iteration, off is the offset into the bap array which is 342 * an array of disk addresses at the current level of indirection. 343 * The logical block number and the offset in that block are stored 344 * into the argument array. 345 */ 346 ap->in_lbn = metalbn; 347 ap->in_off = off = EXT2_NIADDR - i; 348 ap++; 349 for (++numlevels; i <= EXT2_NIADDR; i++) { 350 /* If searching for a meta-data block, quit when found. */ 351 if (metalbn == realbn) 352 break; 353 354 off = (bn / blockcnt) % MNINDIR(ump); 355 356 ++numlevels; 357 ap->in_lbn = metalbn; 358 ap->in_off = off; 359 ++ap; 360 361 metalbn -= -1 + off * blockcnt; 362 blockcnt /= MNINDIR(ump); 363 } 364 if (nump) 365 *nump = numlevels; 366 return (0); 367 } 368