xref: /freebsd-src/sys/fs/ext2fs/ext2_bmap.c (revision 29363fb446372cb3f10bc98664e9767c53fbb457)
1e09c00caSUlf Lilleengen /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4e09c00caSUlf Lilleengen  * Copyright (c) 1989, 1991, 1993
5e09c00caSUlf Lilleengen  *	The Regents of the University of California.  All rights reserved.
6e09c00caSUlf Lilleengen  * (c) UNIX System Laboratories, Inc.
7e09c00caSUlf Lilleengen  * All or some portions of this file are derived from material licensed
8e09c00caSUlf Lilleengen  * to the University of California by American Telephone and Telegraph
9e09c00caSUlf Lilleengen  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10e09c00caSUlf Lilleengen  * the permission of UNIX System Laboratories, Inc.
11e09c00caSUlf Lilleengen  *
12e09c00caSUlf Lilleengen  * Redistribution and use in source and binary forms, with or without
13e09c00caSUlf Lilleengen  * modification, are permitted provided that the following conditions
14e09c00caSUlf Lilleengen  * are met:
15e09c00caSUlf Lilleengen  * 1. Redistributions of source code must retain the above copyright
16e09c00caSUlf Lilleengen  *    notice, this list of conditions and the following disclaimer.
17e09c00caSUlf Lilleengen  * 2. Redistributions in binary form must reproduce the above copyright
18e09c00caSUlf Lilleengen  *    notice, this list of conditions and the following disclaimer in the
19e09c00caSUlf Lilleengen  *    documentation and/or other materials provided with the distribution.
20fca15474SPedro F. Giffuni  * 3. Neither the name of the University nor the names of its contributors
21e09c00caSUlf Lilleengen  *    may be used to endorse or promote products derived from this software
22e09c00caSUlf Lilleengen  *    without specific prior written permission.
23e09c00caSUlf Lilleengen  *
24e09c00caSUlf Lilleengen  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25e09c00caSUlf Lilleengen  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26e09c00caSUlf Lilleengen  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27e09c00caSUlf Lilleengen  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28e09c00caSUlf Lilleengen  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29e09c00caSUlf Lilleengen  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30e09c00caSUlf Lilleengen  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31e09c00caSUlf Lilleengen  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32e09c00caSUlf Lilleengen  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33e09c00caSUlf Lilleengen  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34e09c00caSUlf Lilleengen  * SUCH DAMAGE.
35e09c00caSUlf Lilleengen  */
36e09c00caSUlf Lilleengen 
37e09c00caSUlf Lilleengen #include <sys/param.h>
38e09c00caSUlf Lilleengen #include <sys/systm.h>
39e09c00caSUlf Lilleengen #include <sys/bio.h>
40e09c00caSUlf Lilleengen #include <sys/buf.h>
41cd3acfe7SFedor Uporov #include <sys/endian.h>
42e09c00caSUlf Lilleengen #include <sys/proc.h>
43e09c00caSUlf Lilleengen #include <sys/vnode.h>
44e09c00caSUlf Lilleengen #include <sys/mount.h>
45ae34b6ffSEdward Tomasz Napierala #include <sys/racct.h>
46e09c00caSUlf Lilleengen #include <sys/resourcevar.h>
47e09c00caSUlf Lilleengen #include <sys/stat.h>
48e09c00caSUlf Lilleengen 
49d7511a40SPedro F. Giffuni #include <fs/ext2fs/fs.h>
50e06e5241SFedor Uporov #include <fs/ext2fs/inode.h>
51e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2fs.h>
52d7511a40SPedro F. Giffuni #include <fs/ext2fs/ext2_dinode.h>
53e09c00caSUlf Lilleengen #include <fs/ext2fs/ext2_extern.h>
54fc3ea958SPedro F. Giffuni #include <fs/ext2fs/ext2_mount.h>
55e09c00caSUlf Lilleengen 
56e09c00caSUlf Lilleengen /*
57e09c00caSUlf Lilleengen  * Bmap converts the logical block number of a file to its physical block
58e09c00caSUlf Lilleengen  * number on the disk. The conversion is done by using the logical block
59e09c00caSUlf Lilleengen  * number to index into the array of block pointers described by the dinode.
60e09c00caSUlf Lilleengen  */
61e09c00caSUlf Lilleengen int
ext2_bmap(struct vop_bmap_args * ap)62a9d1b299SPedro F. Giffuni ext2_bmap(struct vop_bmap_args *ap)
63e09c00caSUlf Lilleengen {
6470097aacSPedro F. Giffuni 	daddr_t blkno;
65e09c00caSUlf Lilleengen 	int error;
66e09c00caSUlf Lilleengen 
67e09c00caSUlf Lilleengen 	/*
68e09c00caSUlf Lilleengen 	 * Check for underlying vnode requests and ensure that logical
69e09c00caSUlf Lilleengen 	 * to physical mapping is requested.
70e09c00caSUlf Lilleengen 	 */
71e09c00caSUlf Lilleengen 	if (ap->a_bop != NULL)
72e09c00caSUlf Lilleengen 		*ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj;
73e09c00caSUlf Lilleengen 	if (ap->a_bnp == NULL)
74e09c00caSUlf Lilleengen 		return (0);
75e09c00caSUlf Lilleengen 
76ad3d96a7SPedro F. Giffuni 	if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
77d7511a40SPedro F. Giffuni 		error = ext4_bmapext(ap->a_vp, ap->a_bn, &blkno,
78d7511a40SPedro F. Giffuni 		    ap->a_runp, ap->a_runb);
79d7511a40SPedro F. Giffuni 	else
80e09c00caSUlf Lilleengen 		error = ext2_bmaparray(ap->a_vp, ap->a_bn, &blkno,
81e09c00caSUlf Lilleengen 		    ap->a_runp, ap->a_runb);
82e09c00caSUlf Lilleengen 	*ap->a_bnp = blkno;
83e09c00caSUlf Lilleengen 	return (error);
84e09c00caSUlf Lilleengen }
85e09c00caSUlf Lilleengen 
86e09c00caSUlf Lilleengen /*
8796e9f467SPedro F. Giffuni  * Convert the logical block number of a file to its physical block number
8896e9f467SPedro F. Giffuni  * on the disk within ext4 extents.
89d7511a40SPedro F. Giffuni  */
90b394cd1eSFedor Uporov int
ext4_bmapext(struct vnode * vp,int32_t bn,int64_t * bnp,int * runp,int * runb)91d7511a40SPedro F. Giffuni ext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb)
92d7511a40SPedro F. Giffuni {
93d7511a40SPedro F. Giffuni 	struct inode *ip;
94d7511a40SPedro F. Giffuni 	struct m_ext2fs *fs;
954bd6d63dSFedor Uporov 	struct mount *mp;
964bd6d63dSFedor Uporov 	struct ext2mount *ump;
97b394cd1eSFedor Uporov 	struct ext4_extent_header *ehp;
98d7511a40SPedro F. Giffuni 	struct ext4_extent *ep;
99b394cd1eSFedor Uporov 	struct ext4_extent_path *path = NULL;
100d7511a40SPedro F. Giffuni 	daddr_t lbn;
1014bd6d63dSFedor Uporov 	int error, depth, maxrun = 0, bsize;
102d7511a40SPedro F. Giffuni 
103d7511a40SPedro F. Giffuni 	ip = VTOI(vp);
104d7511a40SPedro F. Giffuni 	fs = ip->i_e2fs;
1054bd6d63dSFedor Uporov 	mp = vp->v_mount;
1064bd6d63dSFedor Uporov 	ump = VFSTOEXT2(mp);
107d7511a40SPedro F. Giffuni 	lbn = bn;
108b394cd1eSFedor Uporov 	ehp = (struct ext4_extent_header *)ip->i_data;
109cd3acfe7SFedor Uporov 	depth = le16toh(ehp->eh_depth);
1104bd6d63dSFedor Uporov 	bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
111d7511a40SPedro F. Giffuni 
112b394cd1eSFedor Uporov 	*bnp = -1;
1134bd6d63dSFedor Uporov 	if (runp != NULL) {
1144bd6d63dSFedor Uporov 		maxrun = mp->mnt_iosize_max / bsize - 1;
115d7511a40SPedro F. Giffuni 		*runp = 0;
1164bd6d63dSFedor Uporov 	}
117d7511a40SPedro F. Giffuni 	if (runb != NULL)
118d7511a40SPedro F. Giffuni 		*runb = 0;
119d7511a40SPedro F. Giffuni 
120b394cd1eSFedor Uporov 	error = ext4_ext_find_extent(ip, lbn, &path);
121b394cd1eSFedor Uporov 	if (error)
122b394cd1eSFedor Uporov 		return (error);
123b394cd1eSFedor Uporov 
124b394cd1eSFedor Uporov 	ep = path[depth].ep_ext;
125b394cd1eSFedor Uporov 	if(ep) {
126cd3acfe7SFedor Uporov 		if (lbn < le32toh(ep->e_blk)) {
1274bd6d63dSFedor Uporov 			if (runp != NULL) {
128cd3acfe7SFedor Uporov 				*runp = min(maxrun, le32toh(ep->e_blk) - lbn - 1);
1294bd6d63dSFedor Uporov 			}
130cd3acfe7SFedor Uporov 		} else if (le32toh(ep->e_blk) <= lbn &&
131cd3acfe7SFedor Uporov 			    lbn < le32toh(ep->e_blk) + le16toh(ep->e_len)) {
132cd3acfe7SFedor Uporov 			*bnp = fsbtodb(fs, lbn - le32toh(ep->e_blk) +
133cd3acfe7SFedor Uporov 			    (le32toh(ep->e_start_lo) |
134cd3acfe7SFedor Uporov 			    (daddr_t)le16toh(ep->e_start_hi) << 32));
1354bd6d63dSFedor Uporov 			if (runp != NULL) {
1364bd6d63dSFedor Uporov 				*runp = min(maxrun,
137cd3acfe7SFedor Uporov 				    le16toh(ep->e_len) -
138cd3acfe7SFedor Uporov 				    (lbn - le32toh(ep->e_blk)) - 1);
1394bd6d63dSFedor Uporov 			}
14078f6ea54SPedro F. Giffuni 			if (runb != NULL)
141cd3acfe7SFedor Uporov 				*runb = min(maxrun, lbn - le32toh(ep->e_blk));
142b394cd1eSFedor Uporov 		} else {
143b394cd1eSFedor Uporov 			if (runb != NULL)
144cd3acfe7SFedor Uporov 				*runb = min(maxrun, le32toh(ep->e_blk) + lbn -
145cd3acfe7SFedor Uporov 				    le16toh(ep->e_len));
146b394cd1eSFedor Uporov 		}
147e813d9d7SPedro F. Giffuni 	}
148d7511a40SPedro F. Giffuni 
149b394cd1eSFedor Uporov 	ext4_ext_path_free(path);
1507135ca50SPedro F. Giffuni 
15196e9f467SPedro F. Giffuni 	return (error);
152d7511a40SPedro F. Giffuni }
153d7511a40SPedro F. Giffuni 
1543767ed5bSFedor Uporov static int
readindir(struct vnode * vp,e2fs_lbn_t lbn,e2fs_daddr_t daddr,struct buf ** bpp)1553767ed5bSFedor Uporov readindir(struct vnode *vp, e2fs_lbn_t lbn, e2fs_daddr_t daddr, struct buf **bpp)
1563767ed5bSFedor Uporov {
1573767ed5bSFedor Uporov 	struct buf *bp;
1583767ed5bSFedor Uporov 	struct mount *mp;
1593767ed5bSFedor Uporov 	struct ext2mount *ump;
1603767ed5bSFedor Uporov 	int error;
1613767ed5bSFedor Uporov 
1623767ed5bSFedor Uporov 	mp = vp->v_mount;
1633767ed5bSFedor Uporov 	ump = VFSTOEXT2(mp);
1643767ed5bSFedor Uporov 
1653767ed5bSFedor Uporov 	bp = getblk(vp, lbn, mp->mnt_stat.f_iosize, 0, 0, 0);
1663767ed5bSFedor Uporov 	if ((bp->b_flags & B_CACHE) == 0) {
1673767ed5bSFedor Uporov 		KASSERT(daddr != 0,
1683767ed5bSFedor Uporov 		    ("readindir: indirect block not in cache"));
1693767ed5bSFedor Uporov 
1703767ed5bSFedor Uporov 		bp->b_blkno = blkptrtodb(ump, daddr);
1713767ed5bSFedor Uporov 		bp->b_iocmd = BIO_READ;
1723767ed5bSFedor Uporov 		bp->b_flags &= ~B_INVAL;
1733767ed5bSFedor Uporov 		bp->b_ioflags &= ~BIO_ERROR;
1743767ed5bSFedor Uporov 		vfs_busy_pages(bp, 0);
1753767ed5bSFedor Uporov 		bp->b_iooffset = dbtob(bp->b_blkno);
1763767ed5bSFedor Uporov 		bstrategy(bp);
1773767ed5bSFedor Uporov #ifdef RACCT
1783767ed5bSFedor Uporov 		if (racct_enable) {
1793767ed5bSFedor Uporov 			PROC_LOCK(curproc);
1803767ed5bSFedor Uporov 			racct_add_buf(curproc, bp, 0);
1813767ed5bSFedor Uporov 			PROC_UNLOCK(curproc);
1823767ed5bSFedor Uporov 		}
1833767ed5bSFedor Uporov #endif
1843767ed5bSFedor Uporov 		curthread->td_ru.ru_inblock++;
1853767ed5bSFedor Uporov 		error = bufwait(bp);
1863767ed5bSFedor Uporov 		if (error != 0) {
1873767ed5bSFedor Uporov 			brelse(bp);
1883767ed5bSFedor Uporov 			return (error);
1893767ed5bSFedor Uporov 		}
1903767ed5bSFedor Uporov 	}
1913767ed5bSFedor Uporov 	*bpp = bp;
1923767ed5bSFedor Uporov 	return (0);
1933767ed5bSFedor Uporov }
1943767ed5bSFedor Uporov 
195d7511a40SPedro F. Giffuni /*
196e09c00caSUlf Lilleengen  * Indirect blocks are now on the vnode for the file.  They are given negative
197e09c00caSUlf Lilleengen  * logical block numbers.  Indirect blocks are addressed by the negative
198e09c00caSUlf Lilleengen  * address of the first data block to which they point.  Double indirect blocks
199e09c00caSUlf Lilleengen  * are addressed by one less than the address of the first indirect block to
200e09c00caSUlf Lilleengen  * which they point.  Triple indirect blocks are addressed by one less than
201e09c00caSUlf Lilleengen  * the address of the first double indirect block to which they point.
202e09c00caSUlf Lilleengen  *
203e09c00caSUlf Lilleengen  * ext2_bmaparray does the bmap conversion, and if requested returns the
204e09c00caSUlf Lilleengen  * array of logical blocks which must be traversed to get to a block.
205e09c00caSUlf Lilleengen  * Each entry contains the offset into that block that gets you to the
206e09c00caSUlf Lilleengen  * next block and the disk address of the block (if it is assigned).
207e09c00caSUlf Lilleengen  */
208e09c00caSUlf Lilleengen 
209e09c00caSUlf Lilleengen int
ext2_bmaparray(struct vnode * vp,daddr_t bn,daddr_t * bnp,int * runp,int * runb)21070097aacSPedro F. Giffuni ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb)
211e09c00caSUlf Lilleengen {
212e09c00caSUlf Lilleengen 	struct inode *ip;
213e09c00caSUlf Lilleengen 	struct buf *bp;
214e09c00caSUlf Lilleengen 	struct ext2mount *ump;
215e09c00caSUlf Lilleengen 	struct mount *mp;
2161dc349abSEd Maste 	struct indir a[EXT2_NIADDR + 1], *ap;
217da057ed2SPedro F. Giffuni 	daddr_t daddr;
218da057ed2SPedro F. Giffuni 	e2fs_lbn_t metalbn;
219e09c00caSUlf Lilleengen 	int error, num, maxrun = 0, bsize;
220e09c00caSUlf Lilleengen 	int *nump;
221e09c00caSUlf Lilleengen 
222e09c00caSUlf Lilleengen 	ap = NULL;
223e09c00caSUlf Lilleengen 	ip = VTOI(vp);
224e09c00caSUlf Lilleengen 	mp = vp->v_mount;
225e09c00caSUlf Lilleengen 	ump = VFSTOEXT2(mp);
226e09c00caSUlf Lilleengen 
227e09c00caSUlf Lilleengen 	bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
228e09c00caSUlf Lilleengen 
229e09c00caSUlf Lilleengen 	if (runp) {
230e09c00caSUlf Lilleengen 		maxrun = mp->mnt_iosize_max / bsize - 1;
231e09c00caSUlf Lilleengen 		*runp = 0;
232e09c00caSUlf Lilleengen 	}
233bf9a211dSPedro F. Giffuni 	if (runb)
234e09c00caSUlf Lilleengen 		*runb = 0;
235e09c00caSUlf Lilleengen 
236e09c00caSUlf Lilleengen 	ap = a;
237e09c00caSUlf Lilleengen 	nump = &num;
238e09c00caSUlf Lilleengen 	error = ext2_getlbns(vp, bn, ap, nump);
239e09c00caSUlf Lilleengen 	if (error)
240e09c00caSUlf Lilleengen 		return (error);
241e09c00caSUlf Lilleengen 
242e09c00caSUlf Lilleengen 	num = *nump;
243e09c00caSUlf Lilleengen 	if (num == 0) {
244e09c00caSUlf Lilleengen 		*bnp = blkptrtodb(ump, ip->i_db[bn]);
245e09c00caSUlf Lilleengen 		if (*bnp == 0) {
246e09c00caSUlf Lilleengen 			*bnp = -1;
247e09c00caSUlf Lilleengen 		} else if (runp) {
24870097aacSPedro F. Giffuni 			daddr_t bnb = bn;
249bf9a211dSPedro F. Giffuni 
2501dc349abSEd Maste 			for (++bn; bn < EXT2_NDADDR && *runp < maxrun &&
251e09c00caSUlf Lilleengen 			    is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
252e09c00caSUlf Lilleengen 			    ++bn, ++*runp);
253e09c00caSUlf Lilleengen 			bn = bnb;
254e09c00caSUlf Lilleengen 			if (runb && (bn > 0)) {
255e09c00caSUlf Lilleengen 				for (--bn; (bn >= 0) && (*runb < maxrun) &&
256e09c00caSUlf Lilleengen 					is_sequential(ump, ip->i_db[bn],
257e09c00caSUlf Lilleengen 						ip->i_db[bn + 1]);
258e09c00caSUlf Lilleengen 						--bn, ++*runb);
259e09c00caSUlf Lilleengen 			}
260e09c00caSUlf Lilleengen 		}
261e09c00caSUlf Lilleengen 		return (0);
262e09c00caSUlf Lilleengen 	}
263e09c00caSUlf Lilleengen 
264e09c00caSUlf Lilleengen 	/* Get disk address out of indirect block array */
265e09c00caSUlf Lilleengen 	daddr = ip->i_ib[ap->in_off];
266e09c00caSUlf Lilleengen 
267e09c00caSUlf Lilleengen 	for (bp = NULL, ++ap; --num; ++ap) {
268e09c00caSUlf Lilleengen 		/*
269e09c00caSUlf Lilleengen 		 * Exit the loop if there is no disk address assigned yet and
270e09c00caSUlf Lilleengen 		 * the indirect block isn't in the cache, or if we were
271e09c00caSUlf Lilleengen 		 * looking for an indirect block and we've found it.
272e09c00caSUlf Lilleengen 		 */
273e09c00caSUlf Lilleengen 
274e09c00caSUlf Lilleengen 		metalbn = ap->in_lbn;
275e09c00caSUlf Lilleengen 		if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
276e09c00caSUlf Lilleengen 			break;
277e09c00caSUlf Lilleengen 		/*
278e09c00caSUlf Lilleengen 		 * If we get here, we've either got the block in the cache
279e09c00caSUlf Lilleengen 		 * or we have a disk address for it, go fetch it.
280e09c00caSUlf Lilleengen 		 */
281e09c00caSUlf Lilleengen 		if (bp)
282e09c00caSUlf Lilleengen 			bqrelse(bp);
2833767ed5bSFedor Uporov 		error = readindir(vp, metalbn, daddr, &bp);
2843767ed5bSFedor Uporov 		if (error != 0)
285e09c00caSUlf Lilleengen 			return (error);
286e09c00caSUlf Lilleengen 
287cd3acfe7SFedor Uporov 		daddr = le32toh(((e2fs_daddr_t *)bp->b_data)[ap->in_off]);
288e09c00caSUlf Lilleengen 		if (num == 1 && daddr && runp) {
289e09c00caSUlf Lilleengen 			for (bn = ap->in_off + 1;
290e09c00caSUlf Lilleengen 			    bn < MNINDIR(ump) && *runp < maxrun &&
291e09c00caSUlf Lilleengen 			    is_sequential(ump,
29270097aacSPedro F. Giffuni 			    ((e2fs_daddr_t *)bp->b_data)[bn - 1],
29370097aacSPedro F. Giffuni 			    ((e2fs_daddr_t *)bp->b_data)[bn]);
294e09c00caSUlf Lilleengen 			    ++bn, ++*runp);
295e09c00caSUlf Lilleengen 			bn = ap->in_off;
296e09c00caSUlf Lilleengen 			if (runb && bn) {
297e09c00caSUlf Lilleengen 				for (--bn; bn >= 0 && *runb < maxrun &&
29870097aacSPedro F. Giffuni 					is_sequential(ump,
29970097aacSPedro F. Giffuni 					((e2fs_daddr_t *)bp->b_data)[bn],
30070097aacSPedro F. Giffuni 					((e2fs_daddr_t *)bp->b_data)[bn + 1]);
301e09c00caSUlf Lilleengen 					--bn, ++*runb);
302e09c00caSUlf Lilleengen 			}
303e09c00caSUlf Lilleengen 		}
304e09c00caSUlf Lilleengen 	}
305e09c00caSUlf Lilleengen 	if (bp)
306e09c00caSUlf Lilleengen 		bqrelse(bp);
307e09c00caSUlf Lilleengen 
308e09c00caSUlf Lilleengen 	*bnp = blkptrtodb(ump, daddr);
309e09c00caSUlf Lilleengen 	if (*bnp == 0) {
310e09c00caSUlf Lilleengen 		*bnp = -1;
311e09c00caSUlf Lilleengen 	}
312e09c00caSUlf Lilleengen 	return (0);
313e09c00caSUlf Lilleengen }
314e09c00caSUlf Lilleengen 
3153767ed5bSFedor Uporov static e2fs_lbn_t
lbn_count(struct ext2mount * ump,int level)3163767ed5bSFedor Uporov lbn_count(struct ext2mount *ump, int level)
3173767ed5bSFedor Uporov 
3183767ed5bSFedor Uporov {
3193767ed5bSFedor Uporov 	e2fs_lbn_t blockcnt;
3203767ed5bSFedor Uporov 
3213767ed5bSFedor Uporov 	for (blockcnt = 1; level > 0; level--)
3223767ed5bSFedor Uporov 		blockcnt *= MNINDIR(ump);
3233767ed5bSFedor Uporov 	return (blockcnt);
3243767ed5bSFedor Uporov }
3253767ed5bSFedor Uporov 
3263767ed5bSFedor Uporov int
ext2_bmap_seekdata(struct vnode * vp,off_t * offp)3273767ed5bSFedor Uporov ext2_bmap_seekdata(struct vnode *vp, off_t *offp)
3283767ed5bSFedor Uporov {
3293767ed5bSFedor Uporov 	struct buf *bp;
3303767ed5bSFedor Uporov 	struct indir a[EXT2_NIADDR + 1], *ap;
3313767ed5bSFedor Uporov 	struct inode *ip;
3323767ed5bSFedor Uporov 	struct mount *mp;
3333767ed5bSFedor Uporov 	struct ext2mount *ump;
3343767ed5bSFedor Uporov 	e2fs_daddr_t bn, daddr, nextbn;
3353767ed5bSFedor Uporov 	uint64_t bsize;
3363767ed5bSFedor Uporov 	off_t numblks;
3373767ed5bSFedor Uporov 	int error, num, num1, off;
3383767ed5bSFedor Uporov 
3393767ed5bSFedor Uporov 	bp = NULL;
3403767ed5bSFedor Uporov 	error = 0;
3413767ed5bSFedor Uporov 	ip = VTOI(vp);
3423767ed5bSFedor Uporov 	mp = vp->v_mount;
3433767ed5bSFedor Uporov 	ump = VFSTOEXT2(mp);
3443767ed5bSFedor Uporov 
345*9cd59de2SKonstantin Belousov 	if (vp->v_type != VREG)
3463767ed5bSFedor Uporov 		return (EINVAL);
3473767ed5bSFedor Uporov 	if (*offp < 0 || *offp >= ip->i_size)
3483767ed5bSFedor Uporov 		return (ENXIO);
3493767ed5bSFedor Uporov 
3503767ed5bSFedor Uporov 	bsize = mp->mnt_stat.f_iosize;
3513767ed5bSFedor Uporov 	for (bn = *offp / bsize, numblks = howmany(ip->i_size, bsize);
3523767ed5bSFedor Uporov 	    bn < numblks; bn = nextbn) {
3533767ed5bSFedor Uporov 		if (bn < EXT2_NDADDR) {
3543767ed5bSFedor Uporov 			daddr = ip->i_db[bn];
3553767ed5bSFedor Uporov 			if (daddr != 0)
3563767ed5bSFedor Uporov 				break;
3573767ed5bSFedor Uporov 			nextbn = bn + 1;
3583767ed5bSFedor Uporov 			continue;
3593767ed5bSFedor Uporov 		}
3603767ed5bSFedor Uporov 
3613767ed5bSFedor Uporov 		ap = a;
3623767ed5bSFedor Uporov 		error = ext2_getlbns(vp, bn, ap, &num);
3633767ed5bSFedor Uporov 		if (error != 0)
3643767ed5bSFedor Uporov 			break;
3653767ed5bSFedor Uporov 		MPASS(num >= 2);
3663767ed5bSFedor Uporov 		daddr = ip->i_ib[ap->in_off];
3673767ed5bSFedor Uporov 		ap++, num--;
3683767ed5bSFedor Uporov 		for (nextbn = EXT2_NDADDR, num1 = num - 1; num1 > 0; num1--)
3693767ed5bSFedor Uporov 			nextbn += lbn_count(ump, num1);
3703767ed5bSFedor Uporov 		if (daddr == 0) {
3713767ed5bSFedor Uporov 			nextbn += lbn_count(ump, num);
3723767ed5bSFedor Uporov 			continue;
3733767ed5bSFedor Uporov 		}
3743767ed5bSFedor Uporov 
3753767ed5bSFedor Uporov 		for (; daddr != 0 && num > 0; ap++, num--) {
3763767ed5bSFedor Uporov 			if (bp != NULL)
3773767ed5bSFedor Uporov 				bqrelse(bp);
3783767ed5bSFedor Uporov 			error = readindir(vp, ap->in_lbn, daddr, &bp);
3793767ed5bSFedor Uporov 			if (error != 0)
3803767ed5bSFedor Uporov 				return (error);
3813767ed5bSFedor Uporov 
3823767ed5bSFedor Uporov 			/*
3833767ed5bSFedor Uporov 			 * Scan the indirect block until we find a non-zero
3843767ed5bSFedor Uporov 			 * pointer.
3853767ed5bSFedor Uporov 			 */
3863767ed5bSFedor Uporov 			off = ap->in_off;
3873767ed5bSFedor Uporov 			do {
388cd3acfe7SFedor Uporov 				daddr = le32toh(((e2fs_daddr_t *)bp->b_data)[off]);
3893767ed5bSFedor Uporov 			} while (daddr == 0 && ++off < MNINDIR(ump));
3903767ed5bSFedor Uporov 			nextbn += off * lbn_count(ump, num - 1);
3913767ed5bSFedor Uporov 
3923767ed5bSFedor Uporov 			/*
3933767ed5bSFedor Uporov 			 * We need to recompute the LBNs of indirect
3943767ed5bSFedor Uporov 			 * blocks, so restart with the updated block offset.
3953767ed5bSFedor Uporov 			 */
3963767ed5bSFedor Uporov 			if (off != ap->in_off)
3973767ed5bSFedor Uporov 				break;
3983767ed5bSFedor Uporov 		}
3993767ed5bSFedor Uporov 		if (num == 0) {
4003767ed5bSFedor Uporov 			/*
4013767ed5bSFedor Uporov 			 * We found a data block.
4023767ed5bSFedor Uporov 			 */
4033767ed5bSFedor Uporov 			bn = nextbn;
4043767ed5bSFedor Uporov 			break;
4053767ed5bSFedor Uporov 		}
4063767ed5bSFedor Uporov 	}
4073767ed5bSFedor Uporov 	if (bp != NULL)
4083767ed5bSFedor Uporov 		bqrelse(bp);
4093767ed5bSFedor Uporov 	if (bn >= numblks)
4103767ed5bSFedor Uporov 		error = ENXIO;
4113767ed5bSFedor Uporov 	if (error == 0 && *offp < bn * bsize)
4123767ed5bSFedor Uporov 		*offp = bn * bsize;
4133767ed5bSFedor Uporov 	return (error);
4143767ed5bSFedor Uporov }
4153767ed5bSFedor Uporov 
416e09c00caSUlf Lilleengen /*
417e09c00caSUlf Lilleengen  * Create an array of logical block number/offset pairs which represent the
418e09c00caSUlf Lilleengen  * path of indirect blocks required to access a data block.  The first "pair"
419e09c00caSUlf Lilleengen  * contains the logical block number of the appropriate single, double or
420e09c00caSUlf Lilleengen  * triple indirect block and the offset into the inode indirect block array.
421e09c00caSUlf Lilleengen  * Note, the logical block number of the inode single/double/triple indirect
422e09c00caSUlf Lilleengen  * block appears twice in the array, once with the offset into the i_ib and
423e09c00caSUlf Lilleengen  * once with the offset into the page itself.
424e09c00caSUlf Lilleengen  */
425e09c00caSUlf Lilleengen int
ext2_getlbns(struct vnode * vp,daddr_t bn,struct indir * ap,int * nump)42670097aacSPedro F. Giffuni ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
427e09c00caSUlf Lilleengen {
428da057ed2SPedro F. Giffuni 	long blockcnt;
429da057ed2SPedro F. Giffuni 	e2fs_lbn_t metalbn, realbn;
430e09c00caSUlf Lilleengen 	struct ext2mount *ump;
431e09c00caSUlf Lilleengen 	int i, numlevels, off;
432e09c00caSUlf Lilleengen 	int64_t qblockcnt;
433e09c00caSUlf Lilleengen 
434e09c00caSUlf Lilleengen 	ump = VFSTOEXT2(vp->v_mount);
435e09c00caSUlf Lilleengen 	if (nump)
436e09c00caSUlf Lilleengen 		*nump = 0;
437e09c00caSUlf Lilleengen 	numlevels = 0;
438e09c00caSUlf Lilleengen 	realbn = bn;
439e09c00caSUlf Lilleengen 	if ((long)bn < 0)
440e09c00caSUlf Lilleengen 		bn = -(long)bn;
441e09c00caSUlf Lilleengen 
4421dc349abSEd Maste 	/* The first EXT2_NDADDR blocks are direct blocks. */
4431dc349abSEd Maste 	if (bn < EXT2_NDADDR)
444e09c00caSUlf Lilleengen 		return (0);
445e09c00caSUlf Lilleengen 
446e09c00caSUlf Lilleengen 	/*
447e09c00caSUlf Lilleengen 	 * Determine the number of levels of indirection.  After this loop
448e09c00caSUlf Lilleengen 	 * is done, blockcnt indicates the number of data blocks possible
4491dc349abSEd Maste 	 * at the previous level of indirection, and EXT2_NIADDR - i is the
4501dc349abSEd Maste 	 * number of levels of indirection needed to locate the requested block.
451e09c00caSUlf Lilleengen 	 */
4521dc349abSEd Maste 	for (blockcnt = 1, i = EXT2_NIADDR, bn -= EXT2_NDADDR; ;
4531dc349abSEd Maste 	    i--, bn -= blockcnt) {
454e09c00caSUlf Lilleengen 		if (i == 0)
455e09c00caSUlf Lilleengen 			return (EFBIG);
456e09c00caSUlf Lilleengen 		/*
457e09c00caSUlf Lilleengen 		 * Use int64_t's here to avoid overflow for triple indirect
458e09c00caSUlf Lilleengen 		 * blocks when longs have 32 bits and the block size is more
459e09c00caSUlf Lilleengen 		 * than 4K.
460e09c00caSUlf Lilleengen 		 */
461e09c00caSUlf Lilleengen 		qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
462e09c00caSUlf Lilleengen 		if (bn < qblockcnt)
463e09c00caSUlf Lilleengen 			break;
464e09c00caSUlf Lilleengen 		blockcnt = qblockcnt;
465e09c00caSUlf Lilleengen 	}
466e09c00caSUlf Lilleengen 
467e09c00caSUlf Lilleengen 	/* Calculate the address of the first meta-block. */
468e09c00caSUlf Lilleengen 	if (realbn >= 0)
4691dc349abSEd Maste 		metalbn = -(realbn - bn + EXT2_NIADDR - i);
470e09c00caSUlf Lilleengen 	else
4711dc349abSEd Maste 		metalbn = -(-realbn - bn + EXT2_NIADDR - i);
472e09c00caSUlf Lilleengen 
473e09c00caSUlf Lilleengen 	/*
474e09c00caSUlf Lilleengen 	 * At each iteration, off is the offset into the bap array which is
475e09c00caSUlf Lilleengen 	 * an array of disk addresses at the current level of indirection.
476e09c00caSUlf Lilleengen 	 * The logical block number and the offset in that block are stored
477e09c00caSUlf Lilleengen 	 * into the argument array.
478e09c00caSUlf Lilleengen 	 */
479e09c00caSUlf Lilleengen 	ap->in_lbn = metalbn;
4801dc349abSEd Maste 	ap->in_off = off = EXT2_NIADDR - i;
481e09c00caSUlf Lilleengen 	ap++;
4821dc349abSEd Maste 	for (++numlevels; i <= EXT2_NIADDR; i++) {
483e09c00caSUlf Lilleengen 		/* If searching for a meta-data block, quit when found. */
484e09c00caSUlf Lilleengen 		if (metalbn == realbn)
485e09c00caSUlf Lilleengen 			break;
486e09c00caSUlf Lilleengen 
487e09c00caSUlf Lilleengen 		off = (bn / blockcnt) % MNINDIR(ump);
488e09c00caSUlf Lilleengen 
489e09c00caSUlf Lilleengen 		++numlevels;
490e09c00caSUlf Lilleengen 		ap->in_lbn = metalbn;
491e09c00caSUlf Lilleengen 		ap->in_off = off;
492e09c00caSUlf Lilleengen 		++ap;
493e09c00caSUlf Lilleengen 
494e09c00caSUlf Lilleengen 		metalbn -= -1 + off * blockcnt;
495e09c00caSUlf Lilleengen 		blockcnt /= MNINDIR(ump);
496e09c00caSUlf Lilleengen 	}
497e09c00caSUlf Lilleengen 	if (nump)
498e09c00caSUlf Lilleengen 		*nump = numlevels;
499e09c00caSUlf Lilleengen 	return (0);
500e09c00caSUlf Lilleengen }
501