xref: /freebsd/sys/fs/ext2fs/ext2_bmap.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (c) 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)ufs_bmap.c	8.7 (Berkeley) 3/21/95
35  * $FreeBSD$
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bio.h>
41 #include <sys/buf.h>
42 #include <sys/proc.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/resourcevar.h>
46 #include <sys/stat.h>
47 
48 #include <fs/ext2fs/inode.h>
49 #include <fs/ext2fs/fs.h>
50 #include <fs/ext2fs/ext2fs.h>
51 #include <fs/ext2fs/ext2_dinode.h>
52 #include <fs/ext2fs/ext2_extern.h>
53 #include <fs/ext2fs/ext2_mount.h>
54 
55 static int ext4_bmapext(struct vnode *, int32_t, int64_t *, int *, int *);
56 
57 /*
58  * Bmap converts the logical block number of a file to its physical block
59  * number on the disk. The conversion is done by using the logical block
60  * number to index into the array of block pointers described by the dinode.
61  */
62 int
63 ext2_bmap(struct vop_bmap_args *ap)
64 {
65 	daddr_t blkno;
66 	int error;
67 
68 	/*
69 	 * Check for underlying vnode requests and ensure that logical
70 	 * to physical mapping is requested.
71 	 */
72 	if (ap->a_bop != NULL)
73 		*ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj;
74 	if (ap->a_bnp == NULL)
75 		return (0);
76 
77 	if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
78 		error = ext4_bmapext(ap->a_vp, ap->a_bn, &blkno,
79 		    ap->a_runp, ap->a_runb);
80 	else
81 		error = ext2_bmaparray(ap->a_vp, ap->a_bn, &blkno,
82 		    ap->a_runp, ap->a_runb);
83 	*ap->a_bnp = blkno;
84 	return (error);
85 }
86 
87 /*
88  * This function converts the logical block number of a file to
89  * its physical block number on the disk within ext4 extents.
90  */
91 static int
92 ext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb)
93 {
94 	struct inode *ip;
95 	struct m_ext2fs *fs;
96 	struct ext4_extent *ep;
97 	struct ext4_extent_path path = { .ep_bp = NULL };
98 	daddr_t lbn;
99 	int ret = 0;
100 
101 	ip = VTOI(vp);
102 	fs = ip->i_e2fs;
103 	lbn = bn;
104 
105 	if (runp != NULL)
106 		*runp = 0;
107 
108 	if (runb != NULL)
109 		*runb = 0;
110 
111 	ext4_ext_find_extent(fs, ip, lbn, &path);
112 	if (path.ep_is_sparse) {
113 		*bnp = -1;
114 		if (runp != NULL)
115 			*runp = path.ep_sparse_ext.e_len -
116 			    (lbn - path.ep_sparse_ext.e_blk) - 1;
117 	} else {
118 		ep = path.ep_ext;
119 		if (ep == NULL)
120 			ret = EIO;
121 		else {
122 			*bnp = fsbtodb(fs, lbn - ep->e_blk +
123 			    (ep->e_start_lo | (daddr_t)ep->e_start_hi << 32));
124 
125 			if (*bnp == 0)
126 				*bnp = -1;
127 
128 			if (runp != NULL)
129 				*runp = ep->e_len - (lbn - ep->e_blk) - 1;
130 		}
131 	}
132 
133 	if (path.ep_bp != NULL) {
134 		brelse(path.ep_bp);
135 		path.ep_bp = NULL;
136 	}
137 
138 	return (ret);
139 }
140 
141 /*
142  * Indirect blocks are now on the vnode for the file.  They are given negative
143  * logical block numbers.  Indirect blocks are addressed by the negative
144  * address of the first data block to which they point.  Double indirect blocks
145  * are addressed by one less than the address of the first indirect block to
146  * which they point.  Triple indirect blocks are addressed by one less than
147  * the address of the first double indirect block to which they point.
148  *
149  * ext2_bmaparray does the bmap conversion, and if requested returns the
150  * array of logical blocks which must be traversed to get to a block.
151  * Each entry contains the offset into that block that gets you to the
152  * next block and the disk address of the block (if it is assigned).
153  */
154 
155 int
156 ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb)
157 {
158 	struct inode *ip;
159 	struct buf *bp;
160 	struct ext2mount *ump;
161 	struct mount *mp;
162 	struct indir a[NIADDR+1], *ap;
163 	daddr_t daddr;
164 	e2fs_lbn_t metalbn;
165 	int error, num, maxrun = 0, bsize;
166 	int *nump;
167 
168 	ap = NULL;
169 	ip = VTOI(vp);
170 	mp = vp->v_mount;
171 	ump = VFSTOEXT2(mp);
172 
173 	bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
174 
175 	if (runp) {
176 		maxrun = mp->mnt_iosize_max / bsize - 1;
177 		*runp = 0;
178 	}
179 
180 	if (runb) {
181 		*runb = 0;
182 	}
183 
184 
185 	ap = a;
186 	nump = &num;
187 	error = ext2_getlbns(vp, bn, ap, nump);
188 	if (error)
189 		return (error);
190 
191 	num = *nump;
192 	if (num == 0) {
193 		*bnp = blkptrtodb(ump, ip->i_db[bn]);
194 		if (*bnp == 0) {
195 			*bnp = -1;
196 		} else if (runp) {
197 			daddr_t bnb = bn;
198 			for (++bn; bn < NDADDR && *runp < maxrun &&
199 			    is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
200 			    ++bn, ++*runp);
201 			bn = bnb;
202 			if (runb && (bn > 0)) {
203 				for (--bn; (bn >= 0) && (*runb < maxrun) &&
204 					is_sequential(ump, ip->i_db[bn],
205 						ip->i_db[bn + 1]);
206 						--bn, ++*runb);
207 			}
208 		}
209 		return (0);
210 	}
211 
212 
213 	/* Get disk address out of indirect block array */
214 	daddr = ip->i_ib[ap->in_off];
215 
216 	for (bp = NULL, ++ap; --num; ++ap) {
217 		/*
218 		 * Exit the loop if there is no disk address assigned yet and
219 		 * the indirect block isn't in the cache, or if we were
220 		 * looking for an indirect block and we've found it.
221 		 */
222 
223 		metalbn = ap->in_lbn;
224 		if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
225 			break;
226 		/*
227 		 * If we get here, we've either got the block in the cache
228 		 * or we have a disk address for it, go fetch it.
229 		 */
230 		if (bp)
231 			bqrelse(bp);
232 
233 		bp = getblk(vp, metalbn, bsize, 0, 0, 0);
234 		if ((bp->b_flags & B_CACHE) == 0) {
235 #ifdef INVARIANTS
236 			if (!daddr)
237 				panic("ext2_bmaparray: indirect block not in cache");
238 #endif
239 			bp->b_blkno = blkptrtodb(ump, daddr);
240 			bp->b_iocmd = BIO_READ;
241 			bp->b_flags &= ~B_INVAL;
242 			bp->b_ioflags &= ~BIO_ERROR;
243 			vfs_busy_pages(bp, 0);
244 			bp->b_iooffset = dbtob(bp->b_blkno);
245 			bstrategy(bp);
246 			curthread->td_ru.ru_inblock++;
247 			error = bufwait(bp);
248 			if (error) {
249 				brelse(bp);
250 				return (error);
251 			}
252 		}
253 
254 		daddr = ((e2fs_daddr_t *)bp->b_data)[ap->in_off];
255 		if (num == 1 && daddr && runp) {
256 			for (bn = ap->in_off + 1;
257 			    bn < MNINDIR(ump) && *runp < maxrun &&
258 			    is_sequential(ump,
259 			    ((e2fs_daddr_t *)bp->b_data)[bn - 1],
260 			    ((e2fs_daddr_t *)bp->b_data)[bn]);
261 			    ++bn, ++*runp);
262 			bn = ap->in_off;
263 			if (runb && bn) {
264 				for (--bn; bn >= 0 && *runb < maxrun &&
265 					is_sequential(ump,
266 					((e2fs_daddr_t *)bp->b_data)[bn],
267 					((e2fs_daddr_t *)bp->b_data)[bn + 1]);
268 					--bn, ++*runb);
269 			}
270 		}
271 	}
272 	if (bp)
273 		bqrelse(bp);
274 
275 	/*
276 	 * Since this is FFS independent code, we are out of scope for the
277 	 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
278 	 * will fall in the range 1..um_seqinc, so we use that test and
279 	 * return a request for a zeroed out buffer if attempts are made
280 	 * to read a BLK_NOCOPY or BLK_SNAP block.
281 	 */
282 	if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc){
283 		*bnp = -1;
284 		return (0);
285 	}
286 	*bnp = blkptrtodb(ump, daddr);
287 	if (*bnp == 0) {
288 		*bnp = -1;
289 	}
290 	return (0);
291 }
292 
293 /*
294  * Create an array of logical block number/offset pairs which represent the
295  * path of indirect blocks required to access a data block.  The first "pair"
296  * contains the logical block number of the appropriate single, double or
297  * triple indirect block and the offset into the inode indirect block array.
298  * Note, the logical block number of the inode single/double/triple indirect
299  * block appears twice in the array, once with the offset into the i_ib and
300  * once with the offset into the page itself.
301  */
302 int
303 ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
304 {
305 	long blockcnt;
306 	e2fs_lbn_t metalbn, realbn;
307 	struct ext2mount *ump;
308 	int i, numlevels, off;
309 	int64_t qblockcnt;
310 
311 	ump = VFSTOEXT2(vp->v_mount);
312 	if (nump)
313 		*nump = 0;
314 	numlevels = 0;
315 	realbn = bn;
316 	if ((long)bn < 0)
317 		bn = -(long)bn;
318 
319 	/* The first NDADDR blocks are direct blocks. */
320 	if (bn < NDADDR)
321 		return (0);
322 
323 	/*
324 	 * Determine the number of levels of indirection.  After this loop
325 	 * is done, blockcnt indicates the number of data blocks possible
326 	 * at the previous level of indirection, and NIADDR - i is the number
327 	 * of levels of indirection needed to locate the requested block.
328 	 */
329 	for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
330 		if (i == 0)
331 			return (EFBIG);
332 		/*
333 		 * Use int64_t's here to avoid overflow for triple indirect
334 		 * blocks when longs have 32 bits and the block size is more
335 		 * than 4K.
336 		 */
337 		qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
338 		if (bn < qblockcnt)
339 			break;
340 		blockcnt = qblockcnt;
341 	}
342 
343 	/* Calculate the address of the first meta-block. */
344 	if (realbn >= 0)
345 		metalbn = -(realbn - bn + NIADDR - i);
346 	else
347 		metalbn = -(-realbn - bn + NIADDR - i);
348 
349 	/*
350 	 * At each iteration, off is the offset into the bap array which is
351 	 * an array of disk addresses at the current level of indirection.
352 	 * The logical block number and the offset in that block are stored
353 	 * into the argument array.
354 	 */
355 	ap->in_lbn = metalbn;
356 	ap->in_off = off = NIADDR - i;
357 	ap++;
358 	for (++numlevels; i <= NIADDR; i++) {
359 		/* If searching for a meta-data block, quit when found. */
360 		if (metalbn == realbn)
361 			break;
362 
363 		off = (bn / blockcnt) % MNINDIR(ump);
364 
365 		++numlevels;
366 		ap->in_lbn = metalbn;
367 		ap->in_off = off;
368 		++ap;
369 
370 		metalbn -= -1 + off * blockcnt;
371 		blockcnt /= MNINDIR(ump);
372 	}
373 	if (nump)
374 		*nump = numlevels;
375 	return (0);
376 }
377