1 /* $OpenBSD: ext2fs_bmap.c,v 1.28 2021/12/12 09:14:59 visa 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/specdev.h> 49 50 #include <ufs/ufs/quota.h> 51 #include <ufs/ufs/inode.h> 52 #include <ufs/ufs/ufsmount.h> 53 #include <ufs/ufs/ufs_extern.h> 54 #include <ufs/ext2fs/ext2fs.h> 55 #include <ufs/ext2fs/ext2fs_extents.h> 56 #include <ufs/ext2fs/ext2fs_extern.h> 57 58 static int ext4_bmapext(struct vnode *, daddr_t, daddr_t *, struct indir *, 59 int *, int *); 60 static int ext2fs_bmaparray(struct vnode *, daddr_t, daddr_t *, struct indir *, 61 int *, int *); 62 63 /* 64 * Bmap converts a the logical block number of a file to its physical block 65 * number on the disk. The conversion is done by using the logical block 66 * number to index into the array of block pointers described by the dinode. 67 */ 68 int 69 ext2fs_bmap(void *v) 70 { 71 struct vop_bmap_args *ap = v; 72 73 /* 74 * Check for underlying vnode requests and ensure that logical 75 * to physical mapping is requested. 76 */ 77 if (ap->a_vpp != NULL) 78 *ap->a_vpp = VTOI(ap->a_vp)->i_devvp; 79 if (ap->a_bnp == NULL) 80 return (0); 81 82 if (VTOI(ap->a_vp)->i_e2din->e2di_flags & EXT4_EXTENTS) { 83 return (ext4_bmapext(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL, 84 ap->a_runp)); 85 } 86 return (ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL, 87 ap->a_runp)); 88 } 89 90 /* 91 * Logical block number of a file -> physical block number on disk within ext4 extents. 92 */ 93 int 94 ext4_bmapext(struct vnode *vp, daddr_t bn, daddr_t *bnp, struct indir *ap, int *nump, int *runp) 95 { 96 struct inode *ip; 97 struct m_ext2fs *fs; 98 struct ext4_extent *ep; 99 struct ext4_extent_path path; 100 daddr_t pos; 101 102 ip = VTOI(vp); 103 fs = ip->i_e2fs; 104 105 if (runp != NULL) 106 *runp = 0; 107 if (nump != NULL) 108 *nump = 0; 109 110 ext4_ext_find_extent(fs, ip, bn, &path); 111 if ((ep = path.ep_ext) == NULL) 112 return (EIO); 113 114 pos = bn - ep->e_blk + (((daddr_t)ep->e_start_hi << 32) | ep->e_start_lo); 115 if ((*bnp = fsbtodb(fs, pos)) == 0) 116 *bnp = -1; 117 return (0); 118 } 119 120 /* 121 * Indirect blocks are now on the vnode for the file. They are given negative 122 * logical block numbers. Indirect blocks are addressed by the negative 123 * address of the first data block to which they point. Double indirect blocks 124 * are addressed by one less than the address of the first indirect block to 125 * which they point. Triple indirect blocks are addressed by one less than 126 * the address of the first double indirect block to which they point. 127 * 128 * ext2fs_bmaparray does the bmap conversion, and if requested returns the 129 * array of logical blocks which must be traversed to get to a block. 130 * Each entry contains the offset into that block that gets you to the 131 * next block and the disk address of the block (if it is assigned). 132 */ 133 134 int 135 ext2fs_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, 136 struct indir *ap, int *nump, int *runp) 137 { 138 struct inode *ip; 139 struct buf *bp; 140 struct ufsmount *ump; 141 struct mount *mp; 142 struct vnode *devvp; 143 struct indir a[NIADDR+1], *xap; 144 int32_t daddr; 145 long metalbn; 146 int error, maxrun = 0, num; 147 148 ip = VTOI(vp); 149 mp = vp->v_mount; 150 ump = VFSTOUFS(mp); 151 #ifdef DIAGNOSTIC 152 if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL)) 153 panic("ext2fs_bmaparray: invalid arguments"); 154 #endif 155 156 if (runp) { 157 /* 158 * XXX 159 * If MAXBSIZE is the largest transfer the disks can handle, 160 * we probably want maxrun to be 1 block less so that we 161 * don't create a block larger than the device can handle. 162 */ 163 *runp = 0; 164 maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1; 165 } 166 167 xap = ap == NULL ? a : ap; 168 if (!nump) 169 nump = # 170 if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0) 171 return (error); 172 173 num = *nump; 174 if (num == 0) { 175 *bnp = blkptrtodb(ump, letoh32(ip->i_e2fs_blocks[bn])); 176 if (*bnp == 0) 177 *bnp = -1; 178 else if (runp) 179 for (++bn; bn < NDADDR && *runp < maxrun && 180 is_sequential(ump, 181 letoh32(ip->i_e2fs_blocks[bn - 1]), 182 letoh32(ip->i_e2fs_blocks[bn])); 183 ++bn, ++*runp) 184 /* nothing */; 185 return (0); 186 } 187 188 189 /* Get disk address out of indirect block array */ 190 daddr = letoh32(ip->i_e2fs_blocks[NDADDR + xap->in_off]); 191 192 devvp = VFSTOUFS(vp->v_mount)->um_devvp; 193 194 #ifdef DIAGNOSTIC 195 if (num > NIADDR + 1 || num < 1) { 196 printf("ext2fs_bmaparray: num=%d\n", num); 197 panic("ext2fs_bmaparray: num"); 198 } 199 #endif 200 for (bp = NULL, ++xap; --num; ++xap) { 201 /* 202 * Exit the loop if there is no disk address assigned yet and 203 * the indirect block isn't in the cache, or if we were 204 * looking for an indirect block and we've found it. 205 */ 206 207 metalbn = xap->in_lbn; 208 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn) 209 break; 210 /* 211 * If we get here, we've either got the block in the cache 212 * or we have a disk address for it, go fetch it. 213 */ 214 if (bp) 215 brelse(bp); 216 217 xap->in_exists = 1; 218 bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, INFSLP); 219 if (bp->b_flags & (B_DONE | B_DELWRI)) { 220 ; 221 } 222 #ifdef DIAGNOSTIC 223 else if (!daddr) 224 panic("ext2fs_bmaparry: indirect block not in cache"); 225 #endif 226 else { 227 bp->b_blkno = blkptrtodb(ump, daddr); 228 bp->b_flags |= B_READ; 229 VOP_STRATEGY(bp->b_vp, bp); 230 curproc->p_ru.ru_inblock++; /* XXX */ 231 bcstats.pendingreads++; 232 if ((error = biowait(bp)) != 0) { 233 brelse(bp); 234 return (error); 235 } 236 } 237 238 daddr = letoh32(((int32_t *)bp->b_data)[xap->in_off]); 239 if (num == 1 && daddr && runp) 240 for (bn = xap->in_off + 1; 241 bn < MNINDIR(ump) && *runp < maxrun && 242 is_sequential(ump, ((u_int32_t *)bp->b_data)[bn - 1], 243 ((u_int32_t *)bp->b_data)[bn]); 244 ++bn, ++*runp) 245 /* nothing */; 246 } 247 if (bp) 248 brelse(bp); 249 250 daddr = blkptrtodb(ump, daddr); 251 *bnp = daddr == 0 ? -1 : daddr; 252 return (0); 253 } 254