Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_bmap.c revision 1.13
      1 /*	$NetBSD: ext2fs_bmap.c,v 1.13 2003/10/05 17:48:49 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)ufs_bmap.c	8.6 (Berkeley) 1/21/94
     37  * Modified for ext2fs by Manuel Bouyer.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1997 Manuel Bouyer.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Manuel Bouyer.
     54  * 4. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67  * SUCH DAMAGE.
     68  *
     69  *	@(#)ufs_bmap.c	8.6 (Berkeley) 1/21/94
     70  * Modified for ext2fs by Manuel Bouyer.
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: ext2fs_bmap.c,v 1.13 2003/10/05 17:48:49 bouyer Exp $");
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/buf.h>
     79 #include <sys/proc.h>
     80 #include <sys/vnode.h>
     81 #include <sys/mount.h>
     82 #include <sys/resourcevar.h>
     83 #include <sys/trace.h>
     84 
     85 #include <miscfs/specfs/specdev.h>
     86 
     87 #include <ufs/ufs/inode.h>
     88 #include <ufs/ufs/ufsmount.h>
     89 #include <ufs/ufs/ufs_extern.h>
     90 #include <ufs/ext2fs/ext2fs.h>
     91 #include <ufs/ext2fs/ext2fs_extern.h>
     92 
     93 static int ext2fs_bmaparray __P((struct vnode *, daddr_t, daddr_t *,
     94 								struct indir *, int *, int *));
     95 
     96 #define	is_sequential(ump, a, b)	((b) == (a) + ump->um_seqinc)
     97 
     98 /*
     99  * Bmap converts a the logical block number of a file to its physical block
    100  * number on the disk. The conversion is done by using the logical block
    101  * number to index into the array of block pointers described by the dinode.
    102  */
    103 int
    104 ext2fs_bmap(v)
    105 	void *v;
    106 {
    107 	struct vop_bmap_args /* {
    108 		struct vnode *a_vp;
    109 		daddr_t  a_bn;
    110 		struct vnode **a_vpp;
    111 		daddr_t *a_bnp;
    112 		int *a_runp;
    113 	} */ *ap = v;
    114 	/*
    115 	 * Check for underlying vnode requests and ensure that logical
    116 	 * to physical mapping is requested.
    117 	 */
    118 	if (ap->a_vpp != NULL)
    119 		*ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
    120 	if (ap->a_bnp == NULL)
    121 		return (0);
    122 
    123 	return (ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
    124 		ap->a_runp));
    125 }
    126 
    127 /*
    128  * Indirect blocks are now on the vnode for the file.  They are given negative
    129  * logical block numbers.  Indirect blocks are addressed by the negative
    130  * address of the first data block to which they point.  Double indirect blocks
    131  * are addressed by one less than the address of the first indirect block to
    132  * which they point.  Triple indirect blocks are addressed by one less than
    133  * the address of the first double indirect block to which they point.
    134  *
    135  * ext2fs_bmaparray does the bmap conversion, and if requested returns the
    136  * array of logical blocks which must be traversed to get to a block.
    137  * Each entry contains the offset into that block that gets you to the
    138  * next block and the disk address of the block (if it is assigned).
    139  */
    140 
    141 int
    142 ext2fs_bmaparray(vp, bn, bnp, ap, nump, runp)
    143 	struct vnode *vp;
    144 	daddr_t bn;
    145 	daddr_t *bnp;
    146 	struct indir *ap;
    147 	int *nump;
    148 	int *runp;
    149 {
    150 	struct inode *ip;
    151 	struct buf *bp;
    152 	struct ufsmount *ump;
    153 	struct mount *mp;
    154 	struct indir a[NIADDR+1], *xap;
    155 	daddr_t daddr;
    156 	daddr_t metalbn;
    157 	int error, maxrun = 0, num;
    158 
    159 	ip = VTOI(vp);
    160 	mp = vp->v_mount;
    161 	ump = VFSTOUFS(mp);
    162 #ifdef DIAGNOSTIC
    163 	if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
    164 		panic("ext2fs_bmaparray: invalid arguments");
    165 #endif
    166 
    167 	if (runp) {
    168 		/*
    169 		 * XXX
    170 		 * If MAXBSIZE is the largest transfer the disks can handle,
    171 		 * we probably want maxrun to be 1 block less so that we
    172 		 * don't create a block larger than the device can handle.
    173 		 */
    174 		*runp = 0;
    175 		maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
    176 	}
    177 
    178 	if (bn >= 0 && bn < NDADDR) {
    179 		/* XXX ondisk32 */
    180 		*bnp = blkptrtodb(ump, (daddr_t)fs2h32(ip->i_e2fs_blocks[bn]));
    181 		if (*bnp == 0)
    182 			*bnp = -1;
    183 		else if (runp)
    184 			/* XXX ondisk32 */
    185 			for (++bn; bn < NDADDR && *runp < maxrun &&
    186 				is_sequential(ump, (daddr_t)fs2h32(ip->i_e2fs_blocks[bn - 1]),
    187 							  (daddr_t)fs2h32(ip->i_e2fs_blocks[bn]));
    188 				++bn, ++*runp);
    189 		return (0);
    190 	}
    191 
    192 	xap = ap == NULL ? a : ap;
    193 	if (!nump)
    194 		nump = &num;
    195 	if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0)
    196 		return (error);
    197 
    198 	num = *nump;
    199 
    200 	/* Get disk address out of indirect block array */
    201 	/* XXX ondisk32 */
    202 	daddr = fs2h32(ip->i_e2fs_blocks[NDADDR + xap->in_off]);
    203 
    204 #ifdef DIAGNOSTIC
    205     if (num > NIADDR + 1 || num < 1) {
    206 		printf("ext2fs_bmaparray: num=%d\n", num);
    207 		panic("ext2fs_bmaparray: num");
    208 	}
    209 #endif
    210 	for (bp = NULL, ++xap; --num; ++xap) {
    211 		/*
    212 		 * Exit the loop if there is no disk address assigned yet and
    213 		 * the indirect block isn't in the cache, or if we were
    214 		 * looking for an indirect block and we've found it.
    215 		 */
    216 
    217 		metalbn = xap->in_lbn;
    218 		if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
    219 			break;
    220 		/*
    221 		 * If we get here, we've either got the block in the cache
    222 		 * or we have a disk address for it, go fetch it.
    223 		 */
    224 		if (bp)
    225 			brelse(bp);
    226 
    227 		xap->in_exists = 1;
    228 		bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
    229 		if (bp->b_flags & (B_DONE | B_DELWRI)) {
    230 			trace(TR_BREADHIT, pack(vp, size), metalbn);
    231 		}
    232 #ifdef DIAGNOSTIC
    233 		else if (!daddr)
    234 			panic("ext2fs_bmaparry: indirect block not in cache");
    235 #endif
    236 		else {
    237 			trace(TR_BREADMISS, pack(vp, size), metalbn);
    238 			bp->b_blkno = blkptrtodb(ump, daddr);
    239 			bp->b_flags |= B_READ;
    240 			VOP_STRATEGY(bp);
    241 			curproc->p_stats->p_ru.ru_inblock++;	/* XXX */
    242 			if ((error = biowait(bp)) != 0) {
    243 				brelse(bp);
    244 				return (error);
    245 			}
    246 		}
    247 
    248 		/* XXX ondisk32 */
    249 		daddr = fs2h32(((int32_t *)bp->b_data)[xap->in_off]);
    250 		if (num == 1 && daddr && runp)
    251 			/* XXX ondisk32 */
    252 			for (bn = xap->in_off + 1;
    253 				bn < MNINDIR(ump) && *runp < maxrun &&
    254 				is_sequential(ump, ((int32_t *)bp->b_data)[bn - 1],
    255 				((int32_t *)bp->b_data)[bn]);
    256 				++bn, ++*runp);
    257 	}
    258 	if (bp)
    259 		brelse(bp);
    260 
    261 	daddr = blkptrtodb(ump, daddr);
    262 	*bnp = daddr == 0 ? -1 : daddr;
    263 	return (0);
    264 }
    265