1 /* $OpenBSD: ext2fs_bmap.c,v 1.21 2013/06/11 16:42:18 deraadt Exp $ */ 2 /* $NetBSD: ext2fs_bmap.c,v 1.5 2000/03/30 12:41:11 augustss Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Manuel Bouyer. 6 * Copyright (c) 1989, 1991, 1993 7 * The Regents of the University of California. All rights reserved. 8 * (c) UNIX System Laboratories, Inc. 9 * All or some portions of this file are derived from material licensed 10 * to the University of California by American Telephone and Telegraph 11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 12 * the permission of UNIX System Laboratories, Inc. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. 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.6 (Berkeley) 1/21/94 39 * Modified for ext2fs by Manuel Bouyer. 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/buf.h> 45 #include <sys/proc.h> 46 #include <sys/vnode.h> 47 #include <sys/mount.h> 48 #include <sys/resourcevar.h> 49 #include <sys/specdev.h> 50 51 #include <ufs/ufs/quota.h> 52 #include <ufs/ufs/inode.h> 53 #include <ufs/ufs/ufsmount.h> 54 #include <ufs/ufs/ufs_extern.h> 55 #include <ufs/ext2fs/ext2fs.h> 56 #include <ufs/ext2fs/ext2fs_extern.h> 57 58 static int ext2fs_bmaparray(struct vnode *, int32_t, daddr_t *, 59 struct indir *, int *, int *); 60 61 /* 62 * Bmap converts a the logical block number of a file to its physical block 63 * number on the disk. The conversion is done by using the logical block 64 * number to index into the array of block pointers described by the dinode. 65 */ 66 int 67 ext2fs_bmap(void *v) 68 { 69 struct vop_bmap_args *ap = v; 70 /* 71 * Check for underlying vnode requests and ensure that logical 72 * to physical mapping is requested. 73 */ 74 if (ap->a_vpp != NULL) 75 *ap->a_vpp = VTOI(ap->a_vp)->i_devvp; 76 if (ap->a_bnp == NULL) 77 return (0); 78 79 return (ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL, 80 ap->a_runp)); 81 } 82 83 /* 84 * Indirect blocks are now on the vnode for the file. They are given negative 85 * logical block numbers. Indirect blocks are addressed by the negative 86 * address of the first data block to which they point. Double indirect blocks 87 * are addressed by one less than the address of the first indirect block to 88 * which they point. Triple indirect blocks are addressed by one less than 89 * the address of the first double indirect block to which they point. 90 * 91 * ext2fs_bmaparray does the bmap conversion, and if requested returns the 92 * array of logical blocks which must be traversed to get to a block. 93 * Each entry contains the offset into that block that gets you to the 94 * next block and the disk address of the block (if it is assigned). 95 */ 96 97 int 98 ext2fs_bmaparray(struct vnode *vp, int32_t bn, daddr_t *bnp, 99 struct indir *ap, int *nump, int *runp) 100 { 101 struct inode *ip; 102 struct buf *bp; 103 struct ufsmount *ump; 104 struct mount *mp; 105 struct vnode *devvp; 106 struct indir a[NIADDR+1], *xap; 107 int32_t daddr; 108 long metalbn; 109 int error, maxrun = 0, num; 110 111 ip = VTOI(vp); 112 mp = vp->v_mount; 113 ump = VFSTOUFS(mp); 114 #ifdef DIAGNOSTIC 115 if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL)) 116 panic("ext2fs_bmaparray: invalid arguments"); 117 #endif 118 119 if (runp) { 120 /* 121 * XXX 122 * If MAXBSIZE is the largest transfer the disks can handle, 123 * we probably want maxrun to be 1 block less so that we 124 * don't create a block larger than the device can handle. 125 */ 126 *runp = 0; 127 maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1; 128 } 129 130 xap = ap == NULL ? a : ap; 131 if (!nump) 132 nump = # 133 if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0) 134 return (error); 135 136 num = *nump; 137 if (num == 0) { 138 *bnp = blkptrtodb(ump, fs2h32(ip->i_e2fs_blocks[bn])); 139 if (*bnp == 0) 140 *bnp = -1; 141 else if (runp) 142 for (++bn; bn < NDADDR && *runp < maxrun && 143 is_sequential(ump, fs2h32(ip->i_e2fs_blocks[bn - 1]), 144 fs2h32(ip->i_e2fs_blocks[bn])); 145 ++bn, ++*runp); 146 return (0); 147 } 148 149 150 /* Get disk address out of indirect block array */ 151 daddr = fs2h32(ip->i_e2fs_blocks[NDADDR + xap->in_off]); 152 153 devvp = VFSTOUFS(vp->v_mount)->um_devvp; 154 155 #ifdef DIAGNOSTIC 156 if (num > NIADDR + 1 || num < 1) { 157 printf("ext2fs_bmaparray: num=%d\n", num); 158 panic("ext2fs_bmaparray: num"); 159 } 160 #endif 161 for (bp = NULL, ++xap; --num; ++xap) { 162 /* 163 * Exit the loop if there is no disk address assigned yet and 164 * the indirect block isn't in the cache, or if we were 165 * looking for an indirect block and we've found it. 166 */ 167 168 metalbn = xap->in_lbn; 169 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn) 170 break; 171 /* 172 * If we get here, we've either got the block in the cache 173 * or we have a disk address for it, go fetch it. 174 */ 175 if (bp) 176 brelse(bp); 177 178 xap->in_exists = 1; 179 bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0); 180 if (bp->b_flags & (B_DONE | B_DELWRI)) { 181 ; 182 } 183 #ifdef DIAGNOSTIC 184 else if (!daddr) 185 panic("ext2fs_bmaparry: indirect block not in cache"); 186 #endif 187 else { 188 bp->b_blkno = blkptrtodb(ump, daddr); 189 bp->b_flags |= B_READ; 190 VOP_STRATEGY(bp); 191 curproc->p_ru.ru_inblock++; /* XXX */ 192 bcstats.pendingreads++; 193 if ((error = biowait(bp)) != 0) { 194 brelse(bp); 195 return (error); 196 } 197 } 198 199 daddr = fs2h32(((int32_t *)bp->b_data)[xap->in_off]); 200 if (num == 1 && daddr && runp) 201 for (bn = xap->in_off + 1; 202 bn < MNINDIR(ump) && *runp < maxrun && 203 is_sequential(ump, ((int32_t *)bp->b_data)[bn - 1], 204 ((int32_t *)bp->b_data)[bn]); 205 ++bn, ++*runp); 206 } 207 if (bp) 208 brelse(bp); 209 210 daddr = blkptrtodb(ump, daddr); 211 *bnp = daddr == 0 ? -1 : daddr; 212 return (0); 213 } 214