Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_bmap.c revision 1.25.22.2
      1  1.25.22.2  jdolecek /*	$NetBSD: ext2fs_bmap.c,v 1.25.22.2 2017/12/03 11:39:21 jdolecek Exp $	*/
      2        1.1    bouyer 
      3        1.1    bouyer /*
      4        1.1    bouyer  * Copyright (c) 1989, 1991, 1993
      5        1.1    bouyer  *	The Regents of the University of California.  All rights reserved.
      6        1.1    bouyer  * (c) UNIX System Laboratories, Inc.
      7        1.1    bouyer  * All or some portions of this file are derived from material licensed
      8        1.1    bouyer  * to the University of California by American Telephone and Telegraph
      9        1.1    bouyer  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10        1.1    bouyer  * the permission of UNIX System Laboratories, Inc.
     11        1.1    bouyer  *
     12        1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     13        1.1    bouyer  * modification, are permitted provided that the following conditions
     14        1.1    bouyer  * are met:
     15        1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     16        1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     17        1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     18        1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     19        1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     20       1.12       agc  * 3. Neither the name of the University nor the names of its contributors
     21       1.12       agc  *    may be used to endorse or promote products derived from this software
     22       1.12       agc  *    without specific prior written permission.
     23       1.12       agc  *
     24       1.12       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25       1.12       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26       1.12       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27       1.12       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28       1.12       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29       1.12       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30       1.12       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31       1.12       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32       1.12       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33       1.12       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34       1.12       agc  * SUCH DAMAGE.
     35       1.12       agc  *
     36       1.12       agc  *	@(#)ufs_bmap.c	8.6 (Berkeley) 1/21/94
     37       1.12       agc  * Modified for ext2fs by Manuel Bouyer.
     38       1.12       agc  */
     39       1.12       agc 
     40       1.12       agc /*
     41       1.12       agc  * Copyright (c) 1997 Manuel Bouyer.
     42       1.12       agc  *
     43       1.12       agc  * Redistribution and use in source and binary forms, with or without
     44       1.12       agc  * modification, are permitted provided that the following conditions
     45       1.12       agc  * are met:
     46       1.12       agc  * 1. Redistributions of source code must retain the above copyright
     47       1.12       agc  *    notice, this list of conditions and the following disclaimer.
     48       1.12       agc  * 2. Redistributions in binary form must reproduce the above copyright
     49       1.12       agc  *    notice, this list of conditions and the following disclaimer in the
     50       1.12       agc  *    documentation and/or other materials provided with the distribution.
     51        1.1    bouyer  *
     52       1.15    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53       1.15    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     54       1.15    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     55       1.15    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     56       1.15    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     57       1.15    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     58       1.15    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     59       1.15    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     60       1.15    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     61       1.15    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62        1.1    bouyer  *
     63        1.1    bouyer  *	@(#)ufs_bmap.c	8.6 (Berkeley) 1/21/94
     64        1.1    bouyer  * Modified for ext2fs by Manuel Bouyer.
     65        1.1    bouyer  */
     66        1.8     lukem 
     67        1.8     lukem #include <sys/cdefs.h>
     68  1.25.22.2  jdolecek __KERNEL_RCSID(0, "$NetBSD: ext2fs_bmap.c,v 1.25.22.2 2017/12/03 11:39:21 jdolecek Exp $");
     69        1.1    bouyer 
     70        1.1    bouyer #include <sys/param.h>
     71        1.1    bouyer #include <sys/systm.h>
     72        1.1    bouyer #include <sys/buf.h>
     73        1.1    bouyer #include <sys/proc.h>
     74        1.1    bouyer #include <sys/vnode.h>
     75        1.1    bouyer #include <sys/mount.h>
     76        1.1    bouyer #include <sys/resourcevar.h>
     77        1.1    bouyer #include <sys/trace.h>
     78        1.1    bouyer 
     79        1.1    bouyer #include <miscfs/specfs/specdev.h>
     80        1.1    bouyer 
     81        1.1    bouyer #include <ufs/ufs/inode.h>
     82        1.1    bouyer #include <ufs/ufs/ufsmount.h>
     83        1.1    bouyer #include <ufs/ufs/ufs_extern.h>
     84        1.3    bouyer #include <ufs/ext2fs/ext2fs.h>
     85        1.1    bouyer #include <ufs/ext2fs/ext2fs_extern.h>
     86        1.1    bouyer 
     87  1.25.22.2  jdolecek 
     88  1.25.22.2  jdolecek static int ext4_bmapext(struct vnode *, int32_t, int64_t *, int *, int *);
     89  1.25.22.2  jdolecek static int ext2fs_bmaparray(struct vnode *, daddr_t, daddr_t *, struct indir *,
     90  1.25.22.2  jdolecek     int *, int *);
     91       1.11      yamt 
     92       1.11      yamt #define	is_sequential(ump, a, b)	((b) == (a) + ump->um_seqinc)
     93        1.1    bouyer 
     94        1.1    bouyer /*
     95        1.1    bouyer  * Bmap converts a the logical block number of a file to its physical block
     96        1.1    bouyer  * number on the disk. The conversion is done by using the logical block
     97        1.1    bouyer  * number to index into the array of block pointers described by the dinode.
     98        1.1    bouyer  */
     99        1.1    bouyer int
    100       1.20   xtraeme ext2fs_bmap(void *v)
    101        1.1    bouyer {
    102        1.1    bouyer 	struct vop_bmap_args /* {
    103        1.1    bouyer 		struct vnode *a_vp;
    104        1.1    bouyer 		daddr_t  a_bn;
    105        1.1    bouyer 		struct vnode **a_vpp;
    106        1.1    bouyer 		daddr_t *a_bnp;
    107        1.1    bouyer 		int *a_runp;
    108        1.1    bouyer 	} */ *ap = v;
    109  1.25.22.2  jdolecek 
    110        1.1    bouyer 	/*
    111        1.1    bouyer 	 * Check for underlying vnode requests and ensure that logical
    112        1.1    bouyer 	 * to physical mapping is requested.
    113        1.1    bouyer 	 */
    114        1.1    bouyer 	if (ap->a_vpp != NULL)
    115        1.1    bouyer 		*ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
    116        1.1    bouyer 	if (ap->a_bnp == NULL)
    117  1.25.22.2  jdolecek 		return 0;
    118  1.25.22.2  jdolecek 
    119  1.25.22.2  jdolecek 	if (VTOI(ap->a_vp)->i_din.e2fs_din->e2di_flags & EXT2_EXTENTS)
    120  1.25.22.2  jdolecek 		return ext4_bmapext(ap->a_vp, ap->a_bn, ap->a_bnp,
    121  1.25.22.2  jdolecek 		    ap->a_runp, NULL);
    122  1.25.22.2  jdolecek 	else
    123  1.25.22.2  jdolecek 		return ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL,
    124  1.25.22.2  jdolecek 		    NULL, ap->a_runp);
    125  1.25.22.2  jdolecek }
    126  1.25.22.2  jdolecek 
    127  1.25.22.2  jdolecek /*
    128  1.25.22.2  jdolecek  * Convert the logical block number of a file to its physical block number
    129  1.25.22.2  jdolecek  * on the disk within ext4 extents.
    130  1.25.22.2  jdolecek  */
    131  1.25.22.2  jdolecek static int
    132  1.25.22.2  jdolecek ext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb)
    133  1.25.22.2  jdolecek {
    134  1.25.22.2  jdolecek 	struct inode *ip;
    135  1.25.22.2  jdolecek 	struct m_ext2fs	 *fs;
    136  1.25.22.2  jdolecek 	struct ext4_extent *ep;
    137  1.25.22.2  jdolecek 	struct ext4_extent_path path = { .ep_bp = NULL };
    138  1.25.22.2  jdolecek 	daddr_t lbn;
    139  1.25.22.2  jdolecek 	int error = 0;
    140  1.25.22.2  jdolecek 
    141  1.25.22.2  jdolecek 	ip = VTOI(vp);
    142  1.25.22.2  jdolecek 	fs = ip->i_e2fs;
    143  1.25.22.2  jdolecek 	lbn = bn;
    144  1.25.22.2  jdolecek 
    145  1.25.22.2  jdolecek 	/* XXX: Should not initialize on error? */
    146  1.25.22.2  jdolecek 	if (runp != NULL)
    147  1.25.22.2  jdolecek 		*runp = 0;
    148        1.1    bouyer 
    149  1.25.22.2  jdolecek 	if (runb != NULL)
    150  1.25.22.2  jdolecek 		*runb = 0;
    151  1.25.22.2  jdolecek 
    152  1.25.22.2  jdolecek 	ext4_ext_find_extent(fs, ip, lbn, &path);
    153  1.25.22.2  jdolecek 	if (path.ep_is_sparse) {
    154  1.25.22.2  jdolecek 		*bnp = -1;
    155  1.25.22.2  jdolecek 		if (runp != NULL)
    156  1.25.22.2  jdolecek 			*runp = path.ep_sparse_ext.e_len -
    157  1.25.22.2  jdolecek 			    (lbn - path.ep_sparse_ext.e_blk) - 1;
    158  1.25.22.2  jdolecek 		if (runb != NULL)
    159  1.25.22.2  jdolecek 			*runb = lbn - path.ep_sparse_ext.e_blk;
    160  1.25.22.2  jdolecek 	} else {
    161  1.25.22.2  jdolecek 		if (path.ep_ext == NULL) {
    162  1.25.22.2  jdolecek 			error = EIO;
    163  1.25.22.2  jdolecek 			goto out;
    164  1.25.22.2  jdolecek 		}
    165  1.25.22.2  jdolecek 		ep = path.ep_ext;
    166  1.25.22.2  jdolecek 		*bnp = fsbtodb(fs, lbn - ep->e_blk
    167  1.25.22.2  jdolecek 		    + (ep->e_start_lo | (daddr_t)ep->e_start_hi << 32));
    168  1.25.22.2  jdolecek 
    169  1.25.22.2  jdolecek 		if (*bnp == 0)
    170  1.25.22.2  jdolecek 			*bnp = -1;
    171  1.25.22.2  jdolecek 
    172  1.25.22.2  jdolecek 		if (runp != NULL)
    173  1.25.22.2  jdolecek 			*runp = ep->e_len - (lbn - ep->e_blk) - 1;
    174  1.25.22.2  jdolecek 		if (runb != NULL)
    175  1.25.22.2  jdolecek 			*runb = lbn - ep->e_blk;
    176  1.25.22.2  jdolecek 	}
    177  1.25.22.2  jdolecek 
    178  1.25.22.2  jdolecek out:
    179  1.25.22.2  jdolecek 	if (path.ep_bp != NULL) {
    180  1.25.22.2  jdolecek 		brelse(path.ep_bp, 0);
    181  1.25.22.2  jdolecek 	}
    182  1.25.22.2  jdolecek 
    183  1.25.22.2  jdolecek 	return error;
    184        1.1    bouyer }
    185        1.1    bouyer 
    186  1.25.22.2  jdolecek 
    187  1.25.22.2  jdolecek 
    188        1.1    bouyer /*
    189        1.1    bouyer  * Indirect blocks are now on the vnode for the file.  They are given negative
    190        1.1    bouyer  * logical block numbers.  Indirect blocks are addressed by the negative
    191        1.1    bouyer  * address of the first data block to which they point.  Double indirect blocks
    192        1.1    bouyer  * are addressed by one less than the address of the first indirect block to
    193        1.1    bouyer  * which they point.  Triple indirect blocks are addressed by one less than
    194        1.1    bouyer  * the address of the first double indirect block to which they point.
    195        1.1    bouyer  *
    196        1.1    bouyer  * ext2fs_bmaparray does the bmap conversion, and if requested returns the
    197        1.1    bouyer  * array of logical blocks which must be traversed to get to a block.
    198        1.1    bouyer  * Each entry contains the offset into that block that gets you to the
    199        1.1    bouyer  * next block and the disk address of the block (if it is assigned).
    200        1.1    bouyer  */
    201        1.1    bouyer 
    202        1.1    bouyer int
    203       1.20   xtraeme ext2fs_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, struct indir *ap,
    204       1.20   xtraeme 		int *nump, int *runp)
    205        1.1    bouyer {
    206        1.5  augustss 	struct inode *ip;
    207       1.23        ad 	struct buf *bp, *cbp;
    208        1.1    bouyer 	struct ufsmount *ump;
    209        1.1    bouyer 	struct mount *mp;
    210  1.25.22.1       tls 	struct indir a[EXT2FS_NIADDR+1], *xap;
    211       1.10      fvdl 	daddr_t daddr;
    212       1.10      fvdl 	daddr_t metalbn;
    213        1.1    bouyer 	int error, maxrun = 0, num;
    214        1.1    bouyer 
    215        1.1    bouyer 	ip = VTOI(vp);
    216        1.1    bouyer 	mp = vp->v_mount;
    217       1.16   mycroft 	ump = ip->i_ump;
    218        1.1    bouyer #ifdef DIAGNOSTIC
    219        1.1    bouyer 	if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
    220        1.1    bouyer 		panic("ext2fs_bmaparray: invalid arguments");
    221        1.1    bouyer #endif
    222        1.1    bouyer 
    223        1.1    bouyer 	if (runp) {
    224        1.1    bouyer 		/*
    225        1.1    bouyer 		 * XXX
    226        1.1    bouyer 		 * If MAXBSIZE is the largest transfer the disks can handle,
    227        1.1    bouyer 		 * we probably want maxrun to be 1 block less so that we
    228        1.1    bouyer 		 * don't create a block larger than the device can handle.
    229        1.1    bouyer 		 */
    230        1.1    bouyer 		*runp = 0;
    231        1.1    bouyer 		maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
    232        1.1    bouyer 	}
    233        1.1    bouyer 
    234  1.25.22.1       tls 	if (bn >= 0 && bn < EXT2FS_NDADDR) {
    235       1.10      fvdl 		/* XXX ondisk32 */
    236       1.17   mycroft 		*bnp = blkptrtodb(ump, fs2h32(ip->i_e2fs_blocks[bn]));
    237        1.1    bouyer 		if (*bnp == 0)
    238        1.1    bouyer 			*bnp = -1;
    239        1.1    bouyer 		else if (runp)
    240       1.10      fvdl 			/* XXX ondisk32 */
    241  1.25.22.1       tls 			for (++bn; bn < EXT2FS_NDADDR && *runp < maxrun &&
    242       1.10      fvdl 				is_sequential(ump, (daddr_t)fs2h32(ip->i_e2fs_blocks[bn - 1]),
    243       1.10      fvdl 							  (daddr_t)fs2h32(ip->i_e2fs_blocks[bn]));
    244        1.1    bouyer 				++bn, ++*runp);
    245  1.25.22.2  jdolecek 		return 0;
    246        1.1    bouyer 	}
    247        1.1    bouyer 
    248        1.9       chs 	xap = ap == NULL ? a : ap;
    249        1.9       chs 	if (!nump)
    250        1.9       chs 		nump = &num;
    251        1.9       chs 	if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0)
    252  1.25.22.2  jdolecek 		return error;
    253        1.9       chs 
    254        1.9       chs 	num = *nump;
    255        1.1    bouyer 
    256        1.1    bouyer 	/* Get disk address out of indirect block array */
    257       1.10      fvdl 	/* XXX ondisk32 */
    258  1.25.22.1       tls 	daddr = fs2h32(ip->i_e2fs_blocks[EXT2FS_NDADDR + xap->in_off]);
    259        1.2    bouyer 
    260        1.2    bouyer #ifdef DIAGNOSTIC
    261  1.25.22.1       tls     if (num > EXT2FS_NIADDR + 1 || num < 1) {
    262        1.2    bouyer 		printf("ext2fs_bmaparray: num=%d\n", num);
    263        1.2    bouyer 		panic("ext2fs_bmaparray: num");
    264        1.2    bouyer 	}
    265        1.2    bouyer #endif
    266        1.1    bouyer 	for (bp = NULL, ++xap; --num; ++xap) {
    267       1.18     perry 		/*
    268        1.1    bouyer 		 * Exit the loop if there is no disk address assigned yet and
    269        1.1    bouyer 		 * the indirect block isn't in the cache, or if we were
    270        1.1    bouyer 		 * looking for an indirect block and we've found it.
    271        1.1    bouyer 		 */
    272        1.1    bouyer 
    273        1.1    bouyer 		metalbn = xap->in_lbn;
    274       1.23        ad 		if (metalbn == bn)
    275        1.1    bouyer 			break;
    276       1.23        ad 		if (daddr == 0) {
    277       1.23        ad 			mutex_enter(&bufcache_lock);
    278       1.23        ad 			cbp = incore(vp, metalbn);
    279       1.23        ad 			mutex_exit(&bufcache_lock);
    280       1.23        ad 			if (cbp == NULL)
    281       1.23        ad 				break;
    282       1.23        ad 		}
    283        1.1    bouyer 		/*
    284        1.1    bouyer 		 * If we get here, we've either got the block in the cache
    285        1.1    bouyer 		 * or we have a disk address for it, go fetch it.
    286        1.1    bouyer 		 */
    287        1.1    bouyer 		if (bp)
    288       1.22        ad 			brelse(bp, 0);
    289        1.1    bouyer 
    290        1.1    bouyer 		xap->in_exists = 1;
    291        1.1    bouyer 		bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
    292       1.19    bouyer 		if (bp == NULL) {
    293       1.19    bouyer 
    294       1.19    bouyer 			/*
    295       1.19    bouyer 			 * getblk() above returns NULL only iff we are
    296       1.19    bouyer 			 * pagedaemon.  See the implementation of getblk
    297       1.19    bouyer 			 * for detail.
    298       1.19    bouyer 			 */
    299       1.19    bouyer 
    300  1.25.22.2  jdolecek 			 return ENOMEM;
    301       1.19    bouyer 		}
    302       1.23        ad 		if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
    303        1.1    bouyer 			trace(TR_BREADHIT, pack(vp, size), metalbn);
    304        1.1    bouyer 		}
    305        1.1    bouyer #ifdef DIAGNOSTIC
    306        1.1    bouyer 		else if (!daddr)
    307        1.1    bouyer 			panic("ext2fs_bmaparry: indirect block not in cache");
    308        1.1    bouyer #endif
    309        1.1    bouyer 		else {
    310        1.1    bouyer 			trace(TR_BREADMISS, pack(vp, size), metalbn);
    311        1.1    bouyer 			bp->b_blkno = blkptrtodb(ump, daddr);
    312        1.1    bouyer 			bp->b_flags |= B_READ;
    313       1.14   hannken 			VOP_STRATEGY(vp, bp);
    314       1.24        ad 			curlwp->l_ru.ru_inblock++;	/* XXX */
    315        1.1    bouyer 			if ((error = biowait(bp)) != 0) {
    316       1.22        ad 				brelse(bp, 0);
    317  1.25.22.2  jdolecek 				return error;
    318        1.1    bouyer 			}
    319        1.1    bouyer 		}
    320        1.1    bouyer 
    321       1.10      fvdl 		/* XXX ondisk32 */
    322       1.10      fvdl 		daddr = fs2h32(((int32_t *)bp->b_data)[xap->in_off]);
    323        1.1    bouyer 		if (num == 1 && daddr && runp)
    324       1.10      fvdl 			/* XXX ondisk32 */
    325        1.1    bouyer 			for (bn = xap->in_off + 1;
    326        1.1    bouyer 				bn < MNINDIR(ump) && *runp < maxrun &&
    327       1.10      fvdl 				is_sequential(ump, ((int32_t *)bp->b_data)[bn - 1],
    328       1.10      fvdl 				((int32_t *)bp->b_data)[bn]);
    329        1.1    bouyer 				++bn, ++*runp);
    330        1.1    bouyer 	}
    331        1.1    bouyer 	if (bp)
    332       1.22        ad 		brelse(bp, 0);
    333        1.1    bouyer 
    334        1.1    bouyer 	daddr = blkptrtodb(ump, daddr);
    335        1.1    bouyer 	*bnp = daddr == 0 ? -1 : daddr;
    336  1.25.22.2  jdolecek 	return 0;
    337        1.1    bouyer }
    338