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