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