ext2_bmap.c revision 261311
1209920Snwhitehorn/*-
2209920Snwhitehorn * Copyright (c) 1989, 1991, 1993
3209920Snwhitehorn *	The Regents of the University of California.  All rights reserved.
4209920Snwhitehorn * (c) UNIX System Laboratories, Inc.
5209920Snwhitehorn * All or some portions of this file are derived from material licensed
6209920Snwhitehorn * to the University of California by American Telephone and Telegraph
7209920Snwhitehorn * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8209920Snwhitehorn * the permission of UNIX System Laboratories, Inc.
9209920Snwhitehorn *
10209920Snwhitehorn * Redistribution and use in source and binary forms, with or without
11209920Snwhitehorn * modification, are permitted provided that the following conditions
12209920Snwhitehorn * are met:
13209920Snwhitehorn * 1. Redistributions of source code must retain the above copyright
14209920Snwhitehorn *    notice, this list of conditions and the following disclaimer.
15209920Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
16209920Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
17209920Snwhitehorn *    documentation and/or other materials provided with the distribution.
18209920Snwhitehorn * 4. Neither the name of the University nor the names of its contributors
19209920Snwhitehorn *    may be used to endorse or promote products derived from this software
20209920Snwhitehorn *    without specific prior written permission.
21209920Snwhitehorn *
22209920Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23209920Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24209920Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25209920Snwhitehorn * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26209920Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27209920Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28209920Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29209920Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30209920Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31209920Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32209920Snwhitehorn * SUCH DAMAGE.
33209920Snwhitehorn *
34209920Snwhitehorn *	@(#)ufs_bmap.c	8.7 (Berkeley) 3/21/95
35209920Snwhitehorn * $FreeBSD: stable/10/sys/fs/ext2fs/ext2_bmap.c 261311 2014-01-31 03:58:36Z pfg $
36209920Snwhitehorn */
37209920Snwhitehorn
38209920Snwhitehorn#include <sys/param.h>
39209920Snwhitehorn#include <sys/systm.h>
40209920Snwhitehorn#include <sys/bio.h>
41209920Snwhitehorn#include <sys/buf.h>
42209920Snwhitehorn#include <sys/proc.h>
43209920Snwhitehorn#include <sys/vnode.h>
44209920Snwhitehorn#include <sys/mount.h>
45209920Snwhitehorn#include <sys/resourcevar.h>
46209920Snwhitehorn#include <sys/stat.h>
47209920Snwhitehorn
48209920Snwhitehorn#include <fs/ext2fs/inode.h>
49209920Snwhitehorn#include <fs/ext2fs/fs.h>
50209920Snwhitehorn#include <fs/ext2fs/ext2fs.h>
51209920Snwhitehorn#include <fs/ext2fs/ext2_dinode.h>
52209920Snwhitehorn#include <fs/ext2fs/ext2_extern.h>
53209920Snwhitehorn#include <fs/ext2fs/ext2_mount.h>
54209920Snwhitehorn
55209920Snwhitehornstatic int ext4_bmapext(struct vnode *, int32_t, int64_t *, int *, int *);
56209920Snwhitehorn
57209920Snwhitehorn/*
58209920Snwhitehorn * Bmap converts the logical block number of a file to its physical block
59209920Snwhitehorn * number on the disk. The conversion is done by using the logical block
60209920Snwhitehorn * number to index into the array of block pointers described by the dinode.
61209920Snwhitehorn */
62209920Snwhitehornint
63209920Snwhitehornext2_bmap(struct vop_bmap_args *ap)
64209920Snwhitehorn{
65209920Snwhitehorn	daddr_t blkno;
66209920Snwhitehorn	int error;
67209920Snwhitehorn
68209920Snwhitehorn	/*
69209920Snwhitehorn	 * Check for underlying vnode requests and ensure that logical
70209920Snwhitehorn	 * to physical mapping is requested.
71209920Snwhitehorn	 */
72209920Snwhitehorn	if (ap->a_bop != NULL)
73209920Snwhitehorn		*ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj;
74209920Snwhitehorn	if (ap->a_bnp == NULL)
75209920Snwhitehorn		return (0);
76209920Snwhitehorn
77209920Snwhitehorn	if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
78209920Snwhitehorn		error = ext4_bmapext(ap->a_vp, ap->a_bn, &blkno,
79209920Snwhitehorn		    ap->a_runp, ap->a_runb);
80209920Snwhitehorn	else
81209920Snwhitehorn		error = ext2_bmaparray(ap->a_vp, ap->a_bn, &blkno,
82209920Snwhitehorn		    ap->a_runp, ap->a_runb);
83209920Snwhitehorn	*ap->a_bnp = blkno;
84209920Snwhitehorn	return (error);
85209920Snwhitehorn}
86209920Snwhitehorn
87209920Snwhitehorn/*
88209920Snwhitehorn * This function converts the logical block number of a file to
89209920Snwhitehorn * its physical block number on the disk within ext4 extents.
90209920Snwhitehorn */
91209920Snwhitehornstatic int
92209920Snwhitehornext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb)
93209920Snwhitehorn{
94209920Snwhitehorn	struct inode *ip;
95209920Snwhitehorn	struct m_ext2fs *fs;
96209920Snwhitehorn	struct ext4_extent *ep;
97209920Snwhitehorn	struct ext4_extent_path path;
98209920Snwhitehorn	daddr_t lbn;
99209920Snwhitehorn
100209920Snwhitehorn	ip = VTOI(vp);
101209920Snwhitehorn	fs = ip->i_e2fs;
102	lbn = bn;
103
104	/*
105	 * TODO: need to implement read ahead to improve the performance.
106	 */
107	if (runp != NULL)
108		*runp = 0;
109
110	if (runb != NULL)
111		*runb = 0;
112
113	ext4_ext_find_extent(fs, ip, lbn, &path);
114	ep = path.ep_ext;
115	if (ep == NULL)
116		return (EIO);
117
118	*bnp = fsbtodb(fs, lbn - ep->e_blk +
119	    (ep->e_start_lo | (daddr_t)ep->e_start_hi << 32));
120
121	if (*bnp == 0)
122		*bnp = -1;
123
124	return (0);
125}
126
127/*
128 * Indirect blocks are now on the vnode for the file.  They are given negative
129 * logical block numbers.  Indirect blocks are addressed by the negative
130 * address of the first data block to which they point.  Double indirect blocks
131 * are addressed by one less than the address of the first indirect block to
132 * which they point.  Triple indirect blocks are addressed by one less than
133 * the address of the first double indirect block to which they point.
134 *
135 * ext2_bmaparray does the bmap conversion, and if requested returns the
136 * array of logical blocks which must be traversed to get to a block.
137 * Each entry contains the offset into that block that gets you to the
138 * next block and the disk address of the block (if it is assigned).
139 */
140
141int
142ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb)
143{
144	struct inode *ip;
145	struct buf *bp;
146	struct ext2mount *ump;
147	struct mount *mp;
148	struct vnode *devvp;
149	struct indir a[NIADDR+1], *ap;
150	daddr_t daddr;
151	e2fs_lbn_t metalbn;
152	int error, num, maxrun = 0, bsize;
153	int *nump;
154
155	ap = NULL;
156	ip = VTOI(vp);
157	mp = vp->v_mount;
158	ump = VFSTOEXT2(mp);
159	devvp = ump->um_devvp;
160
161	bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
162
163	if (runp) {
164		maxrun = mp->mnt_iosize_max / bsize - 1;
165		*runp = 0;
166	}
167
168	if (runb) {
169		*runb = 0;
170	}
171
172
173	ap = a;
174	nump = &num;
175	error = ext2_getlbns(vp, bn, ap, nump);
176	if (error)
177		return (error);
178
179	num = *nump;
180	if (num == 0) {
181		*bnp = blkptrtodb(ump, ip->i_db[bn]);
182		if (*bnp == 0) {
183			*bnp = -1;
184		} else if (runp) {
185			daddr_t bnb = bn;
186			for (++bn; bn < NDADDR && *runp < maxrun &&
187			    is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
188			    ++bn, ++*runp);
189			bn = bnb;
190			if (runb && (bn > 0)) {
191				for (--bn; (bn >= 0) && (*runb < maxrun) &&
192					is_sequential(ump, ip->i_db[bn],
193						ip->i_db[bn + 1]);
194						--bn, ++*runb);
195			}
196		}
197		return (0);
198	}
199
200
201	/* Get disk address out of indirect block array */
202	daddr = ip->i_ib[ap->in_off];
203
204	for (bp = NULL, ++ap; --num; ++ap) {
205		/*
206		 * Exit the loop if there is no disk address assigned yet and
207		 * the indirect block isn't in the cache, or if we were
208		 * looking for an indirect block and we've found it.
209		 */
210
211		metalbn = ap->in_lbn;
212		if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
213			break;
214		/*
215		 * If we get here, we've either got the block in the cache
216		 * or we have a disk address for it, go fetch it.
217		 */
218		if (bp)
219			bqrelse(bp);
220
221		bp = getblk(vp, metalbn, bsize, 0, 0, 0);
222		if ((bp->b_flags & B_CACHE) == 0) {
223#ifdef INVARIANTS
224			if (!daddr)
225				panic("ext2_bmaparray: indirect block not in cache");
226#endif
227			bp->b_blkno = blkptrtodb(ump, daddr);
228			bp->b_iocmd = BIO_READ;
229			bp->b_flags &= ~B_INVAL;
230			bp->b_ioflags &= ~BIO_ERROR;
231			vfs_busy_pages(bp, 0);
232			bp->b_iooffset = dbtob(bp->b_blkno);
233			bstrategy(bp);
234			curthread->td_ru.ru_inblock++;
235			error = bufwait(bp);
236			if (error) {
237				brelse(bp);
238				return (error);
239			}
240		}
241
242		daddr = ((e2fs_daddr_t *)bp->b_data)[ap->in_off];
243		if (num == 1 && daddr && runp) {
244			for (bn = ap->in_off + 1;
245			    bn < MNINDIR(ump) && *runp < maxrun &&
246			    is_sequential(ump,
247			    ((e2fs_daddr_t *)bp->b_data)[bn - 1],
248			    ((e2fs_daddr_t *)bp->b_data)[bn]);
249			    ++bn, ++*runp);
250			bn = ap->in_off;
251			if (runb && bn) {
252				for (--bn; bn >= 0 && *runb < maxrun &&
253			    		is_sequential(ump,
254					((e2fs_daddr_t *)bp->b_data)[bn],
255					((e2fs_daddr_t *)bp->b_data)[bn + 1]);
256			    		--bn, ++*runb);
257			}
258		}
259	}
260	if (bp)
261		bqrelse(bp);
262
263	/*
264	 * Since this is FFS independent code, we are out of scope for the
265	 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
266	 * will fall in the range 1..um_seqinc, so we use that test and
267	 * return a request for a zeroed out buffer if attempts are made
268	 * to read a BLK_NOCOPY or BLK_SNAP block.
269	 */
270	if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc){
271		*bnp = -1;
272		return (0);
273	}
274	*bnp = blkptrtodb(ump, daddr);
275	if (*bnp == 0) {
276		*bnp = -1;
277	}
278	return (0);
279}
280
281/*
282 * Create an array of logical block number/offset pairs which represent the
283 * path of indirect blocks required to access a data block.  The first "pair"
284 * contains the logical block number of the appropriate single, double or
285 * triple indirect block and the offset into the inode indirect block array.
286 * Note, the logical block number of the inode single/double/triple indirect
287 * block appears twice in the array, once with the offset into the i_ib and
288 * once with the offset into the page itself.
289 */
290int
291ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
292{
293	long blockcnt;
294	e2fs_lbn_t metalbn, realbn;
295	struct ext2mount *ump;
296	int i, numlevels, off;
297	int64_t qblockcnt;
298
299	ump = VFSTOEXT2(vp->v_mount);
300	if (nump)
301		*nump = 0;
302	numlevels = 0;
303	realbn = bn;
304	if ((long)bn < 0)
305		bn = -(long)bn;
306
307	/* The first NDADDR blocks are direct blocks. */
308	if (bn < NDADDR)
309		return (0);
310
311	/*
312	 * Determine the number of levels of indirection.  After this loop
313	 * is done, blockcnt indicates the number of data blocks possible
314	 * at the previous level of indirection, and NIADDR - i is the number
315	 * of levels of indirection needed to locate the requested block.
316	 */
317	for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
318		if (i == 0)
319			return (EFBIG);
320		/*
321		 * Use int64_t's here to avoid overflow for triple indirect
322		 * blocks when longs have 32 bits and the block size is more
323		 * than 4K.
324		 */
325		qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
326		if (bn < qblockcnt)
327			break;
328		blockcnt = qblockcnt;
329	}
330
331	/* Calculate the address of the first meta-block. */
332	if (realbn >= 0)
333		metalbn = -(realbn - bn + NIADDR - i);
334	else
335		metalbn = -(-realbn - bn + NIADDR - i);
336
337	/*
338	 * At each iteration, off is the offset into the bap array which is
339	 * an array of disk addresses at the current level of indirection.
340	 * The logical block number and the offset in that block are stored
341	 * into the argument array.
342	 */
343	ap->in_lbn = metalbn;
344	ap->in_off = off = NIADDR - i;
345	ap++;
346	for (++numlevels; i <= NIADDR; i++) {
347		/* If searching for a meta-data block, quit when found. */
348		if (metalbn == realbn)
349			break;
350
351		off = (bn / blockcnt) % MNINDIR(ump);
352
353		++numlevels;
354		ap->in_lbn = metalbn;
355		ap->in_off = off;
356		++ap;
357
358		metalbn -= -1 + off * blockcnt;
359		blockcnt /= MNINDIR(ump);
360	}
361	if (nump)
362		*nump = numlevels;
363	return (0);
364}
365