1 /* $OpenBSD: ext2fs_bmap.c,v 1.5 2000/06/23 02:14:39 mickey Exp $ */ 2 /* $NetBSD: ext2fs_bmap.c,v 1.1 1997/06/11 09:33:46 bouyer 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. All advertising materials mentioning features or use of this software 23 * must display the following acknowledgement: 24 * This product includes software developed by the University of 25 * California, Berkeley and its contributors. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * @(#)ufs_bmap.c 8.6 (Berkeley) 1/21/94 43 * Modified for ext2fs by Manuel Bouyer. 44 */ 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/buf.h> 49 #include <sys/proc.h> 50 #include <sys/vnode.h> 51 #include <sys/mount.h> 52 #include <sys/resourcevar.h> 53 54 #include <miscfs/specfs/specdev.h> 55 56 #include <ufs/ufs/quota.h> 57 #include <ufs/ufs/inode.h> 58 #include <ufs/ufs/ufsmount.h> 59 #include <ufs/ufs/ufs_extern.h> 60 #include <ufs/ext2fs/ext2fs_extern.h> 61 62 static int ext2fs_bmaparray __P((struct vnode *, daddr_t, daddr_t *, 63 struct indir *, int *, int *)); 64 65 /* 66 * Bmap converts a the logical block number of a file to its physical block 67 * number on the disk. The conversion is done by using the logical block 68 * number to index into the array of block pointers described by the dinode. 69 */ 70 int 71 ext2fs_bmap(v) 72 void *v; 73 { 74 struct vop_bmap_args /* { 75 struct vnode *a_vp; 76 daddr_t a_bn; 77 struct vnode **a_vpp; 78 daddr_t *a_bnp; 79 int *a_runp; 80 } */ *ap = v; 81 /* 82 * Check for underlying vnode requests and ensure that logical 83 * to physical mapping is requested. 84 */ 85 if (ap->a_vpp != NULL) 86 *ap->a_vpp = VTOI(ap->a_vp)->i_devvp; 87 if (ap->a_bnp == NULL) 88 return (0); 89 90 return (ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL, 91 ap->a_runp)); 92 } 93 94 /* 95 * Indirect blocks are now on the vnode for the file. They are given negative 96 * logical block numbers. Indirect blocks are addressed by the negative 97 * address of the first data block to which they point. Double indirect blocks 98 * are addressed by one less than the address of the first indirect block to 99 * which they point. Triple indirect blocks are addressed by one less than 100 * the address of the first double indirect block to which they point. 101 * 102 * ext2fs_bmaparray does the bmap conversion, and if requested returns the 103 * array of logical blocks which must be traversed to get to a block. 104 * Each entry contains the offset into that block that gets you to the 105 * next block and the disk address of the block (if it is assigned). 106 */ 107 108 int 109 ext2fs_bmaparray(vp, bn, bnp, ap, nump, runp) 110 struct vnode *vp; 111 register daddr_t bn; 112 daddr_t *bnp; 113 struct indir *ap; 114 int *nump; 115 int *runp; 116 { 117 register struct inode *ip; 118 struct buf *bp; 119 struct ufsmount *ump; 120 struct mount *mp; 121 struct vnode *devvp; 122 struct indir a[NIADDR+1], *xap; 123 daddr_t daddr; 124 long metalbn; 125 int error, maxrun = 0, num; 126 127 ip = VTOI(vp); 128 mp = vp->v_mount; 129 ump = VFSTOUFS(mp); 130 #ifdef DIAGNOSTIC 131 if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL)) 132 panic("ext2fs_bmaparray: invalid arguments"); 133 #endif 134 135 if (runp) { 136 /* 137 * XXX 138 * If MAXBSIZE is the largest transfer the disks can handle, 139 * we probably want maxrun to be 1 block less so that we 140 * don't create a block larger than the device can handle. 141 */ 142 *runp = 0; 143 maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1; 144 } 145 146 xap = ap == NULL ? a : ap; 147 if (!nump) 148 nump = # 149 if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0) 150 return (error); 151 152 num = *nump; 153 if (num == 0) { 154 *bnp = blkptrtodb(ump, ip->i_e2fs_blocks[bn]); 155 if (*bnp == 0) 156 *bnp = -1; 157 else if (runp) 158 for (++bn; bn < NDADDR && *runp < maxrun && 159 is_sequential(ump, ip->i_e2fs_blocks[bn - 1], 160 ip->i_e2fs_blocks[bn]); 161 ++bn, ++*runp); 162 return (0); 163 } 164 165 166 /* Get disk address out of indirect block array */ 167 daddr = ip->i_e2fs_blocks[NDADDR + xap->in_off]; 168 169 devvp = VFSTOUFS(vp->v_mount)->um_devvp; 170 for (bp = NULL, ++xap; --num; ++xap) { 171 /* 172 * Exit the loop if there is no disk address assigned yet and 173 * the indirect block isn't in the cache, or if we were 174 * looking for an indirect block and we've found it. 175 */ 176 177 metalbn = xap->in_lbn; 178 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn) 179 break; 180 /* 181 * If we get here, we've either got the block in the cache 182 * or we have a disk address for it, go fetch it. 183 */ 184 if (bp) 185 brelse(bp); 186 187 xap->in_exists = 1; 188 bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0); 189 if (bp->b_flags & (B_DONE | B_DELWRI)) { 190 ; 191 } 192 #ifdef DIAGNOSTIC 193 else if (!daddr) 194 panic("ext2fs_bmaparry: indirect block not in cache"); 195 #endif 196 else { 197 bp->b_blkno = blkptrtodb(ump, daddr); 198 bp->b_flags |= B_READ; 199 VOP_STRATEGY(bp); 200 curproc->p_stats->p_ru.ru_inblock++; /* XXX */ 201 if ((error = biowait(bp)) != 0) { 202 brelse(bp); 203 return (error); 204 } 205 } 206 207 daddr = ((daddr_t *)bp->b_data)[xap->in_off]; 208 if (num == 1 && daddr && runp) 209 for (bn = xap->in_off + 1; 210 bn < MNINDIR(ump) && *runp < maxrun && 211 is_sequential(ump, ((daddr_t *)bp->b_data)[bn - 1], 212 ((daddr_t *)bp->b_data)[bn]); 213 ++bn, ++*runp); 214 } 215 if (bp) 216 brelse(bp); 217 218 daddr = blkptrtodb(ump, daddr); 219 *bnp = daddr == 0 ? -1 : daddr; 220 return (0); 221 } 222