ext2_bmap.c revision 314937
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: stable/10/sys/fs/ext2fs/ext2_bmap.c 314937 2017-03-09 02:47:01Z pfg $
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
55static 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 */
62int
63ext2_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 */
91static int
92ext4_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
155int
156ext2_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	if (runb)
180		*runb = 0;
181
182
183	ap = a;
184	nump = &num;
185	error = ext2_getlbns(vp, bn, ap, nump);
186	if (error)
187		return (error);
188
189	num = *nump;
190	if (num == 0) {
191		*bnp = blkptrtodb(ump, ip->i_db[bn]);
192		if (*bnp == 0) {
193			*bnp = -1;
194		} else if (runp) {
195			daddr_t bnb = bn;
196
197			for (++bn; bn < NDADDR && *runp < maxrun &&
198			    is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
199			    ++bn, ++*runp);
200			bn = bnb;
201			if (runb && (bn > 0)) {
202				for (--bn; (bn >= 0) && (*runb < maxrun) &&
203					is_sequential(ump, ip->i_db[bn],
204						ip->i_db[bn + 1]);
205						--bn, ++*runb);
206			}
207		}
208		return (0);
209	}
210
211	/* Get disk address out of indirect block array */
212	daddr = ip->i_ib[ap->in_off];
213
214	for (bp = NULL, ++ap; --num; ++ap) {
215		/*
216		 * Exit the loop if there is no disk address assigned yet and
217		 * the indirect block isn't in the cache, or if we were
218		 * looking for an indirect block and we've found it.
219		 */
220
221		metalbn = ap->in_lbn;
222		if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
223			break;
224		/*
225		 * If we get here, we've either got the block in the cache
226		 * or we have a disk address for it, go fetch it.
227		 */
228		if (bp)
229			bqrelse(bp);
230
231		bp = getblk(vp, metalbn, bsize, 0, 0, 0);
232		if ((bp->b_flags & B_CACHE) == 0) {
233#ifdef INVARIANTS
234			if (!daddr)
235				panic("ext2_bmaparray: indirect block not in cache");
236#endif
237			bp->b_blkno = blkptrtodb(ump, daddr);
238			bp->b_iocmd = BIO_READ;
239			bp->b_flags &= ~B_INVAL;
240			bp->b_ioflags &= ~BIO_ERROR;
241			vfs_busy_pages(bp, 0);
242			bp->b_iooffset = dbtob(bp->b_blkno);
243			bstrategy(bp);
244			curthread->td_ru.ru_inblock++;
245			error = bufwait(bp);
246			if (error) {
247				brelse(bp);
248				return (error);
249			}
250		}
251
252		daddr = ((e2fs_daddr_t *)bp->b_data)[ap->in_off];
253		if (num == 1 && daddr && runp) {
254			for (bn = ap->in_off + 1;
255			    bn < MNINDIR(ump) && *runp < maxrun &&
256			    is_sequential(ump,
257			    ((e2fs_daddr_t *)bp->b_data)[bn - 1],
258			    ((e2fs_daddr_t *)bp->b_data)[bn]);
259			    ++bn, ++*runp);
260			bn = ap->in_off;
261			if (runb && bn) {
262				for (--bn; bn >= 0 && *runb < maxrun &&
263					is_sequential(ump,
264					((e2fs_daddr_t *)bp->b_data)[bn],
265					((e2fs_daddr_t *)bp->b_data)[bn + 1]);
266					--bn, ++*runb);
267			}
268		}
269	}
270	if (bp)
271		bqrelse(bp);
272
273	/*
274	 * Since this is FFS independent code, we are out of scope for the
275	 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
276	 * will fall in the range 1..um_seqinc, so we use that test and
277	 * return a request for a zeroed out buffer if attempts are made
278	 * to read a BLK_NOCOPY or BLK_SNAP block.
279	 */
280	if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc) {
281		*bnp = -1;
282		return (0);
283	}
284	*bnp = blkptrtodb(ump, daddr);
285	if (*bnp == 0) {
286		*bnp = -1;
287	}
288	return (0);
289}
290
291/*
292 * Create an array of logical block number/offset pairs which represent the
293 * path of indirect blocks required to access a data block.  The first "pair"
294 * contains the logical block number of the appropriate single, double or
295 * triple indirect block and the offset into the inode indirect block array.
296 * Note, the logical block number of the inode single/double/triple indirect
297 * block appears twice in the array, once with the offset into the i_ib and
298 * once with the offset into the page itself.
299 */
300int
301ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
302{
303	long blockcnt;
304	e2fs_lbn_t metalbn, realbn;
305	struct ext2mount *ump;
306	int i, numlevels, off;
307	int64_t qblockcnt;
308
309	ump = VFSTOEXT2(vp->v_mount);
310	if (nump)
311		*nump = 0;
312	numlevels = 0;
313	realbn = bn;
314	if ((long)bn < 0)
315		bn = -(long)bn;
316
317	/* The first NDADDR blocks are direct blocks. */
318	if (bn < NDADDR)
319		return (0);
320
321	/*
322	 * Determine the number of levels of indirection.  After this loop
323	 * is done, blockcnt indicates the number of data blocks possible
324	 * at the previous level of indirection, and NIADDR - i is the number
325	 * of levels of indirection needed to locate the requested block.
326	 */
327	for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
328		if (i == 0)
329			return (EFBIG);
330		/*
331		 * Use int64_t's here to avoid overflow for triple indirect
332		 * blocks when longs have 32 bits and the block size is more
333		 * than 4K.
334		 */
335		qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
336		if (bn < qblockcnt)
337			break;
338		blockcnt = qblockcnt;
339	}
340
341	/* Calculate the address of the first meta-block. */
342	if (realbn >= 0)
343		metalbn = -(realbn - bn + NIADDR - i);
344	else
345		metalbn = -(-realbn - bn + NIADDR - i);
346
347	/*
348	 * At each iteration, off is the offset into the bap array which is
349	 * an array of disk addresses at the current level of indirection.
350	 * The logical block number and the offset in that block are stored
351	 * into the argument array.
352	 */
353	ap->in_lbn = metalbn;
354	ap->in_off = off = NIADDR - i;
355	ap++;
356	for (++numlevels; i <= NIADDR; i++) {
357		/* If searching for a meta-data block, quit when found. */
358		if (metalbn == realbn)
359			break;
360
361		off = (bn / blockcnt) % MNINDIR(ump);
362
363		++numlevels;
364		ap->in_lbn = metalbn;
365		ap->in_off = off;
366		++ap;
367
368		metalbn -= -1 + off * blockcnt;
369		blockcnt /= MNINDIR(ump);
370	}
371	if (nump)
372		*nump = numlevels;
373	return (0);
374}
375