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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)ufs_bmap.c 8.7 (Berkeley) 3/21/95 39 * $FreeBSD: src/sys/ufs/ufs/ufs_bmap.c,v 1.34.2.1 2000/03/17 10:12:14 ps Exp $ 40 * $DragonFly: src/sys/vfs/ufs/ufs_bmap.c,v 1.12 2006/04/30 17:22:18 dillon Exp $ 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/buf.h> 46 #include <sys/proc.h> 47 #include <sys/vnode.h> 48 #include <sys/mount.h> 49 #include <sys/resourcevar.h> 50 #include <sys/conf.h> 51 52 #include "quota.h" 53 #include "inode.h" 54 #include "ufsmount.h" 55 #include "ufs_extern.h" 56 #include "fs.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 * ufs_bmap(struct vnode *a_vp, off_t a_loffset, struct vnode **a_vpp, 67 * off_t *a_doffsetp, int *a_runp, int *a_runb) 68 */ 69 int 70 ufs_bmap(struct vop_bmap_args *ap) 71 { 72 struct fs *fs; 73 ufs_daddr_t lbn; 74 ufs_daddr_t dbn; 75 int error; 76 77 /* 78 * Check for underlying vnode requests and ensure that logical 79 * to physical mapping is requested. 80 */ 81 if (ap->a_vpp != NULL) 82 *ap->a_vpp = VTOI(ap->a_vp)->i_devvp; 83 if (ap->a_doffsetp == NULL) 84 return (0); 85 86 fs = VTOI(ap->a_vp)->i_fs; 87 KKASSERT(((int)ap->a_loffset & ((1 << fs->fs_bshift) - 1)) == 0); 88 lbn = ap->a_loffset >> fs->fs_bshift; 89 90 error = ufs_bmaparray(ap->a_vp, lbn, &dbn, NULL, NULL, 91 ap->a_runp, ap->a_runb); 92 93 if (error || dbn == (ufs_daddr_t)-1) { 94 *ap->a_doffsetp = NOOFFSET; 95 } else { 96 *ap->a_doffsetp = dbtodoff(fs, dbn); 97 if (ap->a_runp) 98 *ap->a_runp = (*ap->a_runp + 1) << fs->fs_bshift; 99 if (ap->a_runb) 100 *ap->a_runb = *ap->a_runb << fs->fs_bshift; 101 } 102 return (error); 103 } 104 105 /* 106 * Indirect blocks are now on the vnode for the file. They are given negative 107 * logical block numbers. Indirect blocks are addressed by the negative 108 * address of the first data block to which they point. Double indirect blocks 109 * are addressed by one less than the address of the first indirect block to 110 * which they point. Triple indirect blocks are addressed by one less than 111 * the address of the first double indirect block to which they point. 112 * 113 * ufs_bmaparray does the bmap conversion, and if requested returns the 114 * array of logical blocks which must be traversed to get to a block. 115 * Each entry contains the offset into that block that gets you to the 116 * next block and the disk address of the block (if it is assigned). 117 */ 118 int 119 ufs_bmaparray(struct vnode *vp, ufs_daddr_t bn, ufs_daddr_t *bnp, 120 struct indir *ap, int *nump, int *runp, int *runb) 121 { 122 struct inode *ip; 123 struct buf *bp; 124 struct ufsmount *ump; 125 struct mount *mp; 126 struct vnode *devvp; 127 struct fs *fs; 128 struct indir a[NIADDR+1], *xap; 129 ufs_daddr_t daddr; 130 long metalbn; 131 int error, maxrun, num; 132 133 ip = VTOI(vp); 134 mp = vp->v_mount; 135 ump = VFSTOUFS(mp); 136 devvp = ump->um_devvp; 137 fs = ip->i_fs; 138 #ifdef DIAGNOSTIC 139 if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL)) 140 panic("ufs_bmaparray: invalid arguments"); 141 #endif 142 143 if (runp) { 144 *runp = 0; 145 } 146 147 if (runb) { 148 *runb = 0; 149 } 150 151 maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1; 152 153 xap = ap == NULL ? a : ap; 154 if (!nump) 155 nump = # 156 error = ufs_getlbns(vp, bn, xap, nump); 157 if (error) 158 return (error); 159 160 num = *nump; 161 if (num == 0) { 162 *bnp = blkptrtodb(ump, ip->i_db[bn]); 163 if (*bnp == 0) 164 *bnp = -1; 165 else if (runp) { 166 daddr_t bnb = bn; 167 for (++bn; bn < NDADDR && *runp < maxrun && 168 is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]); 169 ++bn, ++*runp); 170 bn = bnb; 171 if (runb && (bn > 0)) { 172 for (--bn; (bn >= 0) && (*runb < maxrun) && 173 is_sequential(ump, ip->i_db[bn], 174 ip->i_db[bn+1]); 175 --bn, ++*runb); 176 } 177 } 178 return (0); 179 } 180 181 182 /* Get disk address out of indirect block array */ 183 daddr = ip->i_ib[xap->in_off]; 184 185 for (bp = NULL, ++xap; --num; ++xap) { 186 /* 187 * Exit the loop if there is no disk address assigned yet and 188 * the indirect block isn't in the cache, or if we were 189 * looking for an indirect block and we've found it. 190 */ 191 192 metalbn = xap->in_lbn; 193 if ((daddr == 0 && !findblk(vp, dbtodoff(fs, metalbn))) || metalbn == bn) 194 break; 195 /* 196 * If we get here, we've either got the block in the cache 197 * or we have a disk address for it, go fetch it. 198 */ 199 if (bp) 200 bqrelse(bp); 201 202 xap->in_exists = 1; 203 bp = getblk(vp, lblktodoff(fs, metalbn), 204 mp->mnt_stat.f_iosize, 0, 0); 205 if ((bp->b_flags & B_CACHE) == 0) { 206 #ifdef DIAGNOSTIC 207 if (!daddr) 208 panic("ufs_bmaparray: indirect block not in cache"); 209 #endif 210 bp->b_bio2.bio_offset = fsbtodoff(fs, daddr); 211 bp->b_flags &= ~(B_INVAL|B_ERROR); 212 bp->b_cmd = BUF_CMD_READ; 213 vfs_busy_pages(bp->b_vp, bp); 214 vn_strategy(bp->b_vp, &bp->b_bio1); 215 error = biowait(bp); 216 if (error) { 217 brelse(bp); 218 return (error); 219 } 220 } 221 222 daddr = ((ufs_daddr_t *)bp->b_data)[xap->in_off]; 223 if (num == 1 && daddr && runp) { 224 for (bn = xap->in_off + 1; 225 bn < MNINDIR(ump) && *runp < maxrun && 226 is_sequential(ump, 227 ((ufs_daddr_t *)bp->b_data)[bn - 1], 228 ((ufs_daddr_t *)bp->b_data)[bn]); 229 ++bn, ++*runp); 230 bn = xap->in_off; 231 if (runb && bn) { 232 for(--bn; bn >= 0 && *runb < maxrun && 233 is_sequential(ump, ((daddr_t *)bp->b_data)[bn], 234 ((daddr_t *)bp->b_data)[bn+1]); 235 --bn, ++*runb); 236 } 237 } 238 } 239 if (bp) 240 bqrelse(bp); 241 242 daddr = blkptrtodb(ump, daddr); 243 *bnp = daddr == 0 ? -1 : daddr; 244 return (0); 245 } 246 247 /* 248 * Create an array of logical block number/offset pairs which represent the 249 * path of indirect blocks required to access a data block. The first "pair" 250 * contains the logical block number of the appropriate single, double or 251 * triple indirect block and the offset into the inode indirect block array. 252 * Note, the logical block number of the inode single/double/triple indirect 253 * block appears twice in the array, once with the offset into the i_ib and 254 * once with the offset into the page itself. 255 */ 256 int 257 ufs_getlbns(struct vnode *vp, ufs_daddr_t bn, struct indir *ap, int *nump) 258 { 259 long blockcnt, metalbn, realbn; 260 struct ufsmount *ump; 261 int i, numlevels, off; 262 int64_t qblockcnt; 263 264 ump = VFSTOUFS(vp->v_mount); 265 if (nump) 266 *nump = 0; 267 numlevels = 0; 268 realbn = bn; 269 if ((long)bn < 0) 270 bn = -(long)bn; 271 272 /* The first NDADDR blocks are direct blocks. */ 273 if (bn < NDADDR) 274 return (0); 275 276 /* 277 * Determine the number of levels of indirection. After this loop 278 * is done, blockcnt indicates the number of data blocks possible 279 * at the previous level of indirection, and NIADDR - i is the number 280 * of levels of indirection needed to locate the requested block. 281 */ 282 for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) { 283 if (i == 0) 284 return (EFBIG); 285 /* 286 * Use int64_t's here to avoid overflow for triple indirect 287 * blocks when longs have 32 bits and the block size is more 288 * than 4K. 289 */ 290 qblockcnt = (int64_t)blockcnt * MNINDIR(ump); 291 if (bn < qblockcnt) 292 break; 293 blockcnt = qblockcnt; 294 } 295 296 /* Calculate the address of the first meta-block. */ 297 if (realbn >= 0) 298 metalbn = -(realbn - bn + NIADDR - i); 299 else 300 metalbn = -(-realbn - bn + NIADDR - i); 301 302 /* 303 * At each iteration, off is the offset into the bap array which is 304 * an array of disk addresses at the current level of indirection. 305 * The logical block number and the offset in that block are stored 306 * into the argument array. 307 */ 308 ap->in_lbn = metalbn; 309 ap->in_off = off = NIADDR - i; 310 ap->in_exists = 0; 311 ap++; 312 for (++numlevels; i <= NIADDR; i++) { 313 /* If searching for a meta-data block, quit when found. */ 314 if (metalbn == realbn) 315 break; 316 317 off = (bn / blockcnt) % MNINDIR(ump); 318 319 ++numlevels; 320 ap->in_lbn = metalbn; 321 ap->in_off = off; 322 ap->in_exists = 0; 323 ++ap; 324 325 metalbn -= -1 + off * blockcnt; 326 blockcnt /= MNINDIR(ump); 327 } 328 if (nump) 329 *nump = numlevels; 330 return (0); 331 } 332