ext2_bmap.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
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 * 4. 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.7 (Berkeley) 3/21/95
37 * $FreeBSD: stable/11/sys/fs/ext2fs/ext2_bmap.c 330897 2018-03-14 03:19:51Z eadler $
38 */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/bio.h>
43#include <sys/buf.h>
44#include <sys/proc.h>
45#include <sys/vnode.h>
46#include <sys/mount.h>
47#include <sys/racct.h>
48#include <sys/resourcevar.h>
49#include <sys/stat.h>
50
51#include <fs/ext2fs/inode.h>
52#include <fs/ext2fs/fs.h>
53#include <fs/ext2fs/ext2fs.h>
54#include <fs/ext2fs/ext2_dinode.h>
55#include <fs/ext2fs/ext2_extern.h>
56#include <fs/ext2fs/ext2_mount.h>
57
58static int ext4_bmapext(struct vnode *, int32_t, int64_t *, int *, int *);
59
60/*
61 * Bmap converts the logical block number of a file to its physical block
62 * number on the disk. The conversion is done by using the logical block
63 * number to index into the array of block pointers described by the dinode.
64 */
65int
66ext2_bmap(struct vop_bmap_args *ap)
67{
68	daddr_t blkno;
69	int error;
70
71	/*
72	 * Check for underlying vnode requests and ensure that logical
73	 * to physical mapping is requested.
74	 */
75	if (ap->a_bop != NULL)
76		*ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj;
77	if (ap->a_bnp == NULL)
78		return (0);
79
80	if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
81		error = ext4_bmapext(ap->a_vp, ap->a_bn, &blkno,
82		    ap->a_runp, ap->a_runb);
83	else
84		error = ext2_bmaparray(ap->a_vp, ap->a_bn, &blkno,
85		    ap->a_runp, ap->a_runb);
86	*ap->a_bnp = blkno;
87	return (error);
88}
89
90/*
91 * Convert the logical block number of a file to its physical block number
92 * on the disk within ext4 extents.
93 */
94static int
95ext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb)
96{
97	struct inode *ip;
98	struct m_ext2fs *fs;
99	struct ext4_extent *ep;
100	struct ext4_extent_path path = {.ep_bp = NULL};
101	daddr_t lbn;
102	int error;
103
104	ip = VTOI(vp);
105	fs = ip->i_e2fs;
106	lbn = bn;
107
108	if (runp != NULL)
109		*runp = 0;
110	if (runb != NULL)
111		*runb = 0;
112	error = 0;
113
114	ext4_ext_find_extent(fs, ip, lbn, &path);
115	if (path.ep_is_sparse) {
116		*bnp = -1;
117		if (runp != NULL)
118			*runp = path.ep_sparse_ext.e_len -
119			    (lbn - path.ep_sparse_ext.e_blk) - 1;
120		if (runb != NULL)
121			*runb = lbn - path.ep_sparse_ext.e_blk;
122	} else {
123		if (path.ep_ext == NULL) {
124			error = EIO;
125			goto out;
126		}
127		ep = path.ep_ext;
128		*bnp = fsbtodb(fs, lbn - ep->e_blk +
129		    (ep->e_start_lo | (daddr_t)ep->e_start_hi << 32));
130
131		if (*bnp == 0)
132			*bnp = -1;
133
134		if (runp != NULL)
135			*runp = ep->e_len - (lbn - ep->e_blk) - 1;
136		if (runb != NULL)
137			*runb = lbn - ep->e_blk;
138	}
139
140out:
141	if (path.ep_bp != NULL)
142		brelse(path.ep_bp);
143
144	return (error);
145}
146
147/*
148 * Indirect blocks are now on the vnode for the file.  They are given negative
149 * logical block numbers.  Indirect blocks are addressed by the negative
150 * address of the first data block to which they point.  Double indirect blocks
151 * are addressed by one less than the address of the first indirect block to
152 * which they point.  Triple indirect blocks are addressed by one less than
153 * the address of the first double indirect block to which they point.
154 *
155 * ext2_bmaparray does the bmap conversion, and if requested returns the
156 * array of logical blocks which must be traversed to get to a block.
157 * Each entry contains the offset into that block that gets you to the
158 * next block and the disk address of the block (if it is assigned).
159 */
160
161int
162ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb)
163{
164	struct inode *ip;
165	struct buf *bp;
166	struct ext2mount *ump;
167	struct mount *mp;
168	struct indir a[NIADDR + 1], *ap;
169	daddr_t daddr;
170	e2fs_lbn_t metalbn;
171	int error, num, maxrun = 0, bsize;
172	int *nump;
173
174	ap = NULL;
175	ip = VTOI(vp);
176	mp = vp->v_mount;
177	ump = VFSTOEXT2(mp);
178
179	bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
180
181	if (runp) {
182		maxrun = mp->mnt_iosize_max / bsize - 1;
183		*runp = 0;
184	}
185	if (runb)
186		*runb = 0;
187
188
189	ap = a;
190	nump = &num;
191	error = ext2_getlbns(vp, bn, ap, nump);
192	if (error)
193		return (error);
194
195	num = *nump;
196	if (num == 0) {
197		*bnp = blkptrtodb(ump, ip->i_db[bn]);
198		if (*bnp == 0) {
199			*bnp = -1;
200		} else if (runp) {
201			daddr_t bnb = bn;
202
203			for (++bn; bn < NDADDR && *runp < maxrun &&
204			    is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
205			    ++bn, ++*runp);
206			bn = bnb;
207			if (runb && (bn > 0)) {
208				for (--bn; (bn >= 0) && (*runb < maxrun) &&
209					is_sequential(ump, ip->i_db[bn],
210						ip->i_db[bn + 1]);
211						--bn, ++*runb);
212			}
213		}
214		return (0);
215	}
216
217	/* Get disk address out of indirect block array */
218	daddr = ip->i_ib[ap->in_off];
219
220	for (bp = NULL, ++ap; --num; ++ap) {
221		/*
222		 * Exit the loop if there is no disk address assigned yet and
223		 * the indirect block isn't in the cache, or if we were
224		 * looking for an indirect block and we've found it.
225		 */
226
227		metalbn = ap->in_lbn;
228		if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
229			break;
230		/*
231		 * If we get here, we've either got the block in the cache
232		 * or we have a disk address for it, go fetch it.
233		 */
234		if (bp)
235			bqrelse(bp);
236
237		bp = getblk(vp, metalbn, bsize, 0, 0, 0);
238		if ((bp->b_flags & B_CACHE) == 0) {
239#ifdef INVARIANTS
240			if (!daddr)
241				panic("ext2_bmaparray: indirect block not in cache");
242#endif
243			bp->b_blkno = blkptrtodb(ump, daddr);
244			bp->b_iocmd = BIO_READ;
245			bp->b_flags &= ~B_INVAL;
246			bp->b_ioflags &= ~BIO_ERROR;
247			vfs_busy_pages(bp, 0);
248			bp->b_iooffset = dbtob(bp->b_blkno);
249			bstrategy(bp);
250#ifdef RACCT
251			if (racct_enable) {
252				PROC_LOCK(curproc);
253				racct_add_buf(curproc, bp, 0);
254				PROC_UNLOCK(curproc);
255			}
256#endif
257			curthread->td_ru.ru_inblock++;
258			error = bufwait(bp);
259			if (error) {
260				brelse(bp);
261				return (error);
262			}
263		}
264
265		daddr = ((e2fs_daddr_t *)bp->b_data)[ap->in_off];
266		if (num == 1 && daddr && runp) {
267			for (bn = ap->in_off + 1;
268			    bn < MNINDIR(ump) && *runp < maxrun &&
269			    is_sequential(ump,
270			    ((e2fs_daddr_t *)bp->b_data)[bn - 1],
271			    ((e2fs_daddr_t *)bp->b_data)[bn]);
272			    ++bn, ++*runp);
273			bn = ap->in_off;
274			if (runb && bn) {
275				for (--bn; bn >= 0 && *runb < maxrun &&
276					is_sequential(ump,
277					((e2fs_daddr_t *)bp->b_data)[bn],
278					((e2fs_daddr_t *)bp->b_data)[bn + 1]);
279					--bn, ++*runb);
280			}
281		}
282	}
283	if (bp)
284		bqrelse(bp);
285
286	/*
287	 * Since this is FFS independent code, we are out of scope for the
288	 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
289	 * will fall in the range 1..um_seqinc, so we use that test and
290	 * return a request for a zeroed out buffer if attempts are made
291	 * to read a BLK_NOCOPY or BLK_SNAP block.
292	 */
293	if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc) {
294		*bnp = -1;
295		return (0);
296	}
297	*bnp = blkptrtodb(ump, daddr);
298	if (*bnp == 0) {
299		*bnp = -1;
300	}
301	return (0);
302}
303
304/*
305 * Create an array of logical block number/offset pairs which represent the
306 * path of indirect blocks required to access a data block.  The first "pair"
307 * contains the logical block number of the appropriate single, double or
308 * triple indirect block and the offset into the inode indirect block array.
309 * Note, the logical block number of the inode single/double/triple indirect
310 * block appears twice in the array, once with the offset into the i_ib and
311 * once with the offset into the page itself.
312 */
313int
314ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
315{
316	long blockcnt;
317	e2fs_lbn_t metalbn, realbn;
318	struct ext2mount *ump;
319	int i, numlevels, off;
320	int64_t qblockcnt;
321
322	ump = VFSTOEXT2(vp->v_mount);
323	if (nump)
324		*nump = 0;
325	numlevels = 0;
326	realbn = bn;
327	if ((long)bn < 0)
328		bn = -(long)bn;
329
330	/* The first NDADDR blocks are direct blocks. */
331	if (bn < NDADDR)
332		return (0);
333
334	/*
335	 * Determine the number of levels of indirection.  After this loop
336	 * is done, blockcnt indicates the number of data blocks possible
337	 * at the previous level of indirection, and NIADDR - i is the number
338	 * of levels of indirection needed to locate the requested block.
339	 */
340	for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
341		if (i == 0)
342			return (EFBIG);
343		/*
344		 * Use int64_t's here to avoid overflow for triple indirect
345		 * blocks when longs have 32 bits and the block size is more
346		 * than 4K.
347		 */
348		qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
349		if (bn < qblockcnt)
350			break;
351		blockcnt = qblockcnt;
352	}
353
354	/* Calculate the address of the first meta-block. */
355	if (realbn >= 0)
356		metalbn = -(realbn - bn + NIADDR - i);
357	else
358		metalbn = -(-realbn - bn + NIADDR - i);
359
360	/*
361	 * At each iteration, off is the offset into the bap array which is
362	 * an array of disk addresses at the current level of indirection.
363	 * The logical block number and the offset in that block are stored
364	 * into the argument array.
365	 */
366	ap->in_lbn = metalbn;
367	ap->in_off = off = NIADDR - i;
368	ap++;
369	for (++numlevels; i <= NIADDR; i++) {
370		/* If searching for a meta-data block, quit when found. */
371		if (metalbn == realbn)
372			break;
373
374		off = (bn / blockcnt) % MNINDIR(ump);
375
376		++numlevels;
377		ap->in_lbn = metalbn;
378		ap->in_off = off;
379		++ap;
380
381		metalbn -= -1 + off * blockcnt;
382		blockcnt /= MNINDIR(ump);
383	}
384	if (nump)
385		*nump = numlevels;
386	return (0);
387}
388