xref: /netbsd-src/sys/ufs/ext2fs/ext2fs_bmap.c (revision 9bd4ba417d045dba399c191a33f736de4600c34b)
1*9bd4ba41Sriastradh /*	$NetBSD: ext2fs_bmap.c,v 1.31 2023/08/26 05:22:50 riastradh Exp $	*/
29e3c291dSbouyer 
39e3c291dSbouyer /*
49e3c291dSbouyer  * Copyright (c) 1989, 1991, 1993
59e3c291dSbouyer  *	The Regents of the University of California.  All rights reserved.
69e3c291dSbouyer  * (c) UNIX System Laboratories, Inc.
79e3c291dSbouyer  * All or some portions of this file are derived from material licensed
89e3c291dSbouyer  * to the University of California by American Telephone and Telegraph
99e3c291dSbouyer  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
109e3c291dSbouyer  * the permission of UNIX System Laboratories, Inc.
119e3c291dSbouyer  *
129e3c291dSbouyer  * Redistribution and use in source and binary forms, with or without
139e3c291dSbouyer  * modification, are permitted provided that the following conditions
149e3c291dSbouyer  * are met:
159e3c291dSbouyer  * 1. Redistributions of source code must retain the above copyright
169e3c291dSbouyer  *    notice, this list of conditions and the following disclaimer.
179e3c291dSbouyer  * 2. Redistributions in binary form must reproduce the above copyright
189e3c291dSbouyer  *    notice, this list of conditions and the following disclaimer in the
199e3c291dSbouyer  *    documentation and/or other materials provided with the distribution.
20aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
21aad01611Sagc  *    may be used to endorse or promote products derived from this software
22aad01611Sagc  *    without specific prior written permission.
23aad01611Sagc  *
24aad01611Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25aad01611Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26aad01611Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27aad01611Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28aad01611Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29aad01611Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30aad01611Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31aad01611Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32aad01611Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33aad01611Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34aad01611Sagc  * SUCH DAMAGE.
35aad01611Sagc  *
36aad01611Sagc  *	@(#)ufs_bmap.c	8.6 (Berkeley) 1/21/94
37aad01611Sagc  * Modified for ext2fs by Manuel Bouyer.
38aad01611Sagc  */
39aad01611Sagc 
40aad01611Sagc /*
41aad01611Sagc  * Copyright (c) 1997 Manuel Bouyer.
42aad01611Sagc  *
43aad01611Sagc  * Redistribution and use in source and binary forms, with or without
44aad01611Sagc  * modification, are permitted provided that the following conditions
45aad01611Sagc  * are met:
46aad01611Sagc  * 1. Redistributions of source code must retain the above copyright
47aad01611Sagc  *    notice, this list of conditions and the following disclaimer.
48aad01611Sagc  * 2. Redistributions in binary form must reproduce the above copyright
49aad01611Sagc  *    notice, this list of conditions and the following disclaimer in the
50aad01611Sagc  *    documentation and/or other materials provided with the distribution.
519e3c291dSbouyer  *
5235decc6eSbouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
5335decc6eSbouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5435decc6eSbouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5535decc6eSbouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
5635decc6eSbouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5735decc6eSbouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5835decc6eSbouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5935decc6eSbouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6035decc6eSbouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
6135decc6eSbouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
629e3c291dSbouyer  *
639e3c291dSbouyer  *	@(#)ufs_bmap.c	8.6 (Berkeley) 1/21/94
649e3c291dSbouyer  * Modified for ext2fs by Manuel Bouyer.
659e3c291dSbouyer  */
669e3c291dSbouyer 
67ec624546Slukem #include <sys/cdefs.h>
68*9bd4ba41Sriastradh __KERNEL_RCSID(0, "$NetBSD: ext2fs_bmap.c,v 1.31 2023/08/26 05:22:50 riastradh Exp $");
69ec624546Slukem 
709e3c291dSbouyer #include <sys/param.h>
719e3c291dSbouyer #include <sys/systm.h>
729e3c291dSbouyer #include <sys/buf.h>
739e3c291dSbouyer #include <sys/proc.h>
749e3c291dSbouyer #include <sys/vnode.h>
759e3c291dSbouyer #include <sys/mount.h>
769e3c291dSbouyer #include <sys/resourcevar.h>
779e3c291dSbouyer #include <sys/trace.h>
789e3c291dSbouyer 
799e3c291dSbouyer #include <miscfs/specfs/specdev.h>
809e3c291dSbouyer 
819e3c291dSbouyer #include <ufs/ufs/inode.h>
829e3c291dSbouyer #include <ufs/ufs/ufsmount.h>
839e3c291dSbouyer #include <ufs/ufs/ufs_extern.h>
846ab3092bSbouyer #include <ufs/ext2fs/ext2fs.h>
859e3c291dSbouyer #include <ufs/ext2fs/ext2fs_extern.h>
869e3c291dSbouyer 
874e815995Schristos 
884e815995Schristos static int ext4_bmapext(struct vnode *, int32_t, int64_t *, int *, int *);
894e815995Schristos static int ext2fs_bmaparray(struct vnode *, daddr_t, daddr_t *, struct indir *,
904e815995Schristos     int *, int *);
919e3c291dSbouyer 
92ab2238cfSyamt #define	is_sequential(ump, a, b)	((b) == (a) + ump->um_seqinc)
93ab2238cfSyamt 
949e3c291dSbouyer /*
959e3c291dSbouyer  * Bmap converts a the logical block number of a file to its physical block
969e3c291dSbouyer  * number on the disk. The conversion is done by using the logical block
979e3c291dSbouyer  * number to index into the array of block pointers described by the dinode.
989e3c291dSbouyer  */
999e3c291dSbouyer int
ext2fs_bmap(void * v)10023ebf62dSxtraeme ext2fs_bmap(void *v)
1019e3c291dSbouyer {
1029e3c291dSbouyer 	struct vop_bmap_args /* {
1039e3c291dSbouyer 		struct vnode *a_vp;
1049e3c291dSbouyer 		daddr_t  a_bn;
1059e3c291dSbouyer 		struct vnode **a_vpp;
1069e3c291dSbouyer 		daddr_t *a_bnp;
1079e3c291dSbouyer 		int *a_runp;
1089e3c291dSbouyer 	} */ *ap = v;
109437b8a43Sjdolecek 
1109e3c291dSbouyer 	/*
1119e3c291dSbouyer 	 * Check for underlying vnode requests and ensure that logical
1129e3c291dSbouyer 	 * to physical mapping is requested.
1139e3c291dSbouyer 	 */
1149e3c291dSbouyer 	if (ap->a_vpp != NULL)
1159e3c291dSbouyer 		*ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
1169e3c291dSbouyer 	if (ap->a_bnp == NULL)
11701d303e7Schristos 		return 0;
1189e3c291dSbouyer 
1199a22ef9eSjdolecek 	if (VTOI(ap->a_vp)->i_din.e2fs_din->e2di_flags & EXT2_EXTENTS)
1204e815995Schristos 		return ext4_bmapext(ap->a_vp, ap->a_bn, ap->a_bnp,
1214e815995Schristos 		    ap->a_runp, NULL);
1224e815995Schristos 	else
1234e815995Schristos 		return ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL,
1244e815995Schristos 		    NULL, ap->a_runp);
1259e3c291dSbouyer }
1269e3c291dSbouyer 
1279e3c291dSbouyer /*
1284e815995Schristos  * Convert the logical block number of a file to its physical block number
1294e815995Schristos  * on the disk within ext4 extents.
1304e815995Schristos  */
1314e815995Schristos static int
ext4_bmapext(struct vnode * vp,int32_t bn,int64_t * bnp,int * runp,int * runb)1324e815995Schristos ext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb)
1334e815995Schristos {
1344e815995Schristos 	struct inode *ip;
1354e815995Schristos 	struct m_ext2fs	 *fs;
1364e815995Schristos 	struct ext4_extent *ep;
1374e815995Schristos 	struct ext4_extent_path path = { .ep_bp = NULL };
1384e815995Schristos 	daddr_t lbn;
1394e815995Schristos 	int error = 0;
1404e815995Schristos 
1414e815995Schristos 	ip = VTOI(vp);
1424e815995Schristos 	fs = ip->i_e2fs;
1434e815995Schristos 	lbn = bn;
1444e815995Schristos 
1454e815995Schristos 	/* XXX: Should not initialize on error? */
1464e815995Schristos 	if (runp != NULL)
1474e815995Schristos 		*runp = 0;
1484e815995Schristos 
1494e815995Schristos 	if (runb != NULL)
1504e815995Schristos 		*runb = 0;
1514e815995Schristos 
1524e815995Schristos 	ext4_ext_find_extent(fs, ip, lbn, &path);
1534e815995Schristos 	if (path.ep_is_sparse) {
1544e815995Schristos 		*bnp = -1;
1554e815995Schristos 		if (runp != NULL)
1564e815995Schristos 			*runp = path.ep_sparse_ext.e_len -
1574e815995Schristos 			    (lbn - path.ep_sparse_ext.e_blk) - 1;
1584e815995Schristos 		if (runb != NULL)
1594e815995Schristos 			*runb = lbn - path.ep_sparse_ext.e_blk;
1604e815995Schristos 	} else {
1614e815995Schristos 		if (path.ep_ext == NULL) {
1624e815995Schristos 			error = EIO;
1634e815995Schristos 			goto out;
1644e815995Schristos 		}
1654e815995Schristos 		ep = path.ep_ext;
1664e815995Schristos 		*bnp = fsbtodb(fs, lbn - ep->e_blk
1674e815995Schristos 		    + (ep->e_start_lo | (daddr_t)ep->e_start_hi << 32));
1684e815995Schristos 
1694e815995Schristos 		if (*bnp == 0)
1704e815995Schristos 			*bnp = -1;
1714e815995Schristos 
1724e815995Schristos 		if (runp != NULL)
1734e815995Schristos 			*runp = ep->e_len - (lbn - ep->e_blk) - 1;
1744e815995Schristos 		if (runb != NULL)
1754e815995Schristos 			*runb = lbn - ep->e_blk;
1764e815995Schristos 	}
1774e815995Schristos 
1784e815995Schristos out:
1794e815995Schristos 	if (path.ep_bp != NULL) {
1804e815995Schristos 		brelse(path.ep_bp, 0);
1814e815995Schristos 	}
1824e815995Schristos 
1834e815995Schristos 	return error;
1844e815995Schristos }
1854e815995Schristos 
1864e815995Schristos 
1874e815995Schristos 
1884e815995Schristos /*
1899e3c291dSbouyer  * Indirect blocks are now on the vnode for the file.  They are given negative
1909e3c291dSbouyer  * logical block numbers.  Indirect blocks are addressed by the negative
1919e3c291dSbouyer  * address of the first data block to which they point.  Double indirect blocks
1929e3c291dSbouyer  * are addressed by one less than the address of the first indirect block to
1939e3c291dSbouyer  * which they point.  Triple indirect blocks are addressed by one less than
1949e3c291dSbouyer  * the address of the first double indirect block to which they point.
1959e3c291dSbouyer  *
1969e3c291dSbouyer  * ext2fs_bmaparray does the bmap conversion, and if requested returns the
1979e3c291dSbouyer  * array of logical blocks which must be traversed to get to a block.
1989e3c291dSbouyer  * Each entry contains the offset into that block that gets you to the
1999e3c291dSbouyer  * next block and the disk address of the block (if it is assigned).
2009e3c291dSbouyer  */
2019e3c291dSbouyer 
2029e3c291dSbouyer int
ext2fs_bmaparray(struct vnode * vp,daddr_t bn,daddr_t * bnp,struct indir * ap,int * nump,int * runp)20323ebf62dSxtraeme ext2fs_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, struct indir *ap,
20423ebf62dSxtraeme 		int *nump, int *runp)
2059e3c291dSbouyer {
206169ac5b3Saugustss 	struct inode *ip;
2074a780c9aSad 	struct buf *bp, *cbp;
2089e3c291dSbouyer 	struct ufsmount *ump;
2099e3c291dSbouyer 	struct mount *mp;
210dcd34a91Sdholland 	struct indir a[EXT2FS_NIADDR+1], *xap;
211a3ff3a30Sfvdl 	daddr_t daddr;
212a3ff3a30Sfvdl 	daddr_t metalbn;
2139e3c291dSbouyer 	int error, maxrun = 0, num;
2149e3c291dSbouyer 
2159e3c291dSbouyer 	ip = VTOI(vp);
2169e3c291dSbouyer 	mp = vp->v_mount;
21745a21b76Smycroft 	ump = ip->i_ump;
2189e3c291dSbouyer #ifdef DIAGNOSTIC
2199e3c291dSbouyer 	if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
2209e3c291dSbouyer 		panic("ext2fs_bmaparray: invalid arguments");
2219e3c291dSbouyer #endif
2229e3c291dSbouyer 
2239e3c291dSbouyer 	if (runp) {
2249e3c291dSbouyer 		/*
2259e3c291dSbouyer 		 * XXX
2269e3c291dSbouyer 		 * If MAXBSIZE is the largest transfer the disks can handle,
2279e3c291dSbouyer 		 * we probably want maxrun to be 1 block less so that we
2289e3c291dSbouyer 		 * don't create a block larger than the device can handle.
2299e3c291dSbouyer 		 */
2309e3c291dSbouyer 		*runp = 0;
2319e3c291dSbouyer 		maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
2329e3c291dSbouyer 	}
2339e3c291dSbouyer 
234dcd34a91Sdholland 	if (bn >= 0 && bn < EXT2FS_NDADDR) {
235a3ff3a30Sfvdl 		/* XXX ondisk32 */
2365ac91d48Smycroft 		*bnp = blkptrtodb(ump, fs2h32(ip->i_e2fs_blocks[bn]));
2379e3c291dSbouyer 		if (*bnp == 0)
2389e3c291dSbouyer 			*bnp = -1;
2399e3c291dSbouyer 		else if (runp)
240a3ff3a30Sfvdl 			/* XXX ondisk32 */
241dcd34a91Sdholland 			for (++bn; bn < EXT2FS_NDADDR && *runp < maxrun &&
242a3ff3a30Sfvdl 				is_sequential(ump, (daddr_t)fs2h32(ip->i_e2fs_blocks[bn - 1]),
243a3ff3a30Sfvdl 							  (daddr_t)fs2h32(ip->i_e2fs_blocks[bn]));
2449e3c291dSbouyer 				++bn, ++*runp);
24501d303e7Schristos 		return 0;
2469e3c291dSbouyer 	}
2479e3c291dSbouyer 
248c237f810Schs 	xap = ap == NULL ? a : ap;
249c237f810Schs 	if (!nump)
250c237f810Schs 		nump = &num;
251c237f810Schs 	if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0)
25201d303e7Schristos 		return error;
253c237f810Schs 
254c237f810Schs 	num = *nump;
2559e3c291dSbouyer 
2569e3c291dSbouyer 	/* Get disk address out of indirect block array */
257a3ff3a30Sfvdl 	/* XXX ondisk32 */
258dcd34a91Sdholland 	daddr = fs2h32(ip->i_e2fs_blocks[EXT2FS_NDADDR + xap->in_off]);
2599e3c291dSbouyer 
2603bf1a6adSbouyer #ifdef DIAGNOSTIC
261dcd34a91Sdholland     if (num > EXT2FS_NIADDR + 1 || num < 1) {
2623bf1a6adSbouyer 		printf("ext2fs_bmaparray: num=%d\n", num);
2633bf1a6adSbouyer 		panic("ext2fs_bmaparray: num");
2643bf1a6adSbouyer 	}
2653bf1a6adSbouyer #endif
2669e3c291dSbouyer 	for (bp = NULL, ++xap; --num; ++xap) {
2679e3c291dSbouyer 		/*
2689e3c291dSbouyer 		 * Exit the loop if there is no disk address assigned yet and
2699e3c291dSbouyer 		 * the indirect block isn't in the cache, or if we were
2709e3c291dSbouyer 		 * looking for an indirect block and we've found it.
2719e3c291dSbouyer 		 */
2729e3c291dSbouyer 
2739e3c291dSbouyer 		metalbn = xap->in_lbn;
2744a780c9aSad 		if (metalbn == bn)
2759e3c291dSbouyer 			break;
2764a780c9aSad 		if (daddr == 0) {
2774a780c9aSad 			mutex_enter(&bufcache_lock);
2784a780c9aSad 			cbp = incore(vp, metalbn);
2794a780c9aSad 			mutex_exit(&bufcache_lock);
2804a780c9aSad 			if (cbp == NULL)
2814a780c9aSad 				break;
2824a780c9aSad 		}
2839e3c291dSbouyer 		/*
2849e3c291dSbouyer 		 * If we get here, we've either got the block in the cache
2859e3c291dSbouyer 		 * or we have a disk address for it, go fetch it.
2869e3c291dSbouyer 		 */
2879e3c291dSbouyer 		if (bp)
2885c3b2b3fSad 			brelse(bp, 0);
2899e3c291dSbouyer 
2909e3c291dSbouyer 		xap->in_exists = 1;
2919e3c291dSbouyer 		bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
292303cafe4Sbouyer 		if (bp == NULL) {
293303cafe4Sbouyer 
294303cafe4Sbouyer 			/*
295303cafe4Sbouyer 			 * getblk() above returns NULL only iff we are
296303cafe4Sbouyer 			 * pagedaemon.  See the implementation of getblk
297303cafe4Sbouyer 			 * for detail.
298303cafe4Sbouyer 			 */
299303cafe4Sbouyer 
30001d303e7Schristos 			 return ENOMEM;
301303cafe4Sbouyer 		}
3024a780c9aSad 		if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
3039e3c291dSbouyer 			trace(TR_BREADHIT, pack(vp, size), metalbn);
3049e3c291dSbouyer 		}
3059e3c291dSbouyer #ifdef DIAGNOSTIC
3069e3c291dSbouyer 		else if (!daddr)
3079e3c291dSbouyer 			panic("ext2fs_bmaparry: indirect block not in cache");
3089e3c291dSbouyer #endif
3099e3c291dSbouyer 		else {
3109e3c291dSbouyer 			trace(TR_BREADMISS, pack(vp, size), metalbn);
3119e3c291dSbouyer 			bp->b_blkno = blkptrtodb(ump, daddr);
3129e3c291dSbouyer 			bp->b_flags |= B_READ;
3133db4e2acShannken 			VOP_STRATEGY(vp, bp);
314be04ac48Sad 			curlwp->l_ru.ru_inblock++;	/* XXX */
3159e3c291dSbouyer 			if ((error = biowait(bp)) != 0) {
3165c3b2b3fSad 				brelse(bp, 0);
31701d303e7Schristos 				return error;
3189e3c291dSbouyer 			}
3199e3c291dSbouyer 		}
3209e3c291dSbouyer 
321a3ff3a30Sfvdl 		/* XXX ondisk32 */
322a3ff3a30Sfvdl 		daddr = fs2h32(((int32_t *)bp->b_data)[xap->in_off]);
3239e3c291dSbouyer 		if (num == 1 && daddr && runp)
324a3ff3a30Sfvdl 			/* XXX ondisk32 */
3259e3c291dSbouyer 			for (bn = xap->in_off + 1;
3269e3c291dSbouyer 				bn < MNINDIR(ump) && *runp < maxrun &&
327a3ff3a30Sfvdl 				is_sequential(ump, ((int32_t *)bp->b_data)[bn - 1],
328a3ff3a30Sfvdl 				((int32_t *)bp->b_data)[bn]);
3299e3c291dSbouyer 				++bn, ++*runp);
3309e3c291dSbouyer 	}
3319e3c291dSbouyer 	if (bp)
3325c3b2b3fSad 		brelse(bp, 0);
3339e3c291dSbouyer 
3349e3c291dSbouyer 	daddr = blkptrtodb(ump, daddr);
3359e3c291dSbouyer 	*bnp = daddr == 0 ? -1 : daddr;
33601d303e7Schristos 	return 0;
3379e3c291dSbouyer }
338