xref: /dragonfly/sys/vfs/ufs/ufs_bmap.c (revision b7367ef6)
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)ufs_bmap.c	8.7 (Berkeley) 3/21/95
39  * $FreeBSD: src/sys/ufs/ufs/ufs_bmap.c,v 1.34.2.1 2000/03/17 10:12:14 ps Exp $
40  * $DragonFly: src/sys/vfs/ufs/ufs_bmap.c,v 1.13 2007/08/13 17:31:57 dillon Exp $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/resourcevar.h>
50 #include <sys/conf.h>
51 
52 #include "quota.h"
53 #include "inode.h"
54 #include "ufsmount.h"
55 #include "ufs_extern.h"
56 #include "fs.h"
57 
58 /*
59  * Bmap converts the logical block number of a file to its physical block
60  * number on the disk. The conversion is done by using the logical block
61  * number to index into the array of block pointers described by the dinode.
62  *
63  * BMAP must return the contiguous before and after run in bytes, inclusive
64  * of the returned block.
65  *
66  * ufs_bmap(struct vnode *a_vp, off_t a_loffset,
67  *	    off_t *a_doffsetp, int *a_runp, int *a_runb)
68  */
69 int
70 ufs_bmap(struct vop_bmap_args *ap)
71 {
72 	struct fs *fs;
73 	ufs_daddr_t lbn;
74 	ufs_daddr_t dbn;
75 	int error;
76 
77 	/*
78 	 * Check for underlying vnode requests and ensure that logical
79 	 * to physical mapping is requested.
80 	 */
81 	if (ap->a_doffsetp == NULL)
82 		return (0);
83 
84 	fs = VTOI(ap->a_vp)->i_fs;
85 	KKASSERT(((int)ap->a_loffset & ((1 << fs->fs_bshift) - 1)) == 0);
86 	lbn = ap->a_loffset >> fs->fs_bshift;
87 
88 	error = ufs_bmaparray(ap->a_vp, lbn, &dbn, NULL, NULL,
89 			      ap->a_runp, ap->a_runb);
90 
91 	if (error || dbn == (ufs_daddr_t)-1) {
92 		*ap->a_doffsetp = NOOFFSET;
93 	} else {
94 		*ap->a_doffsetp = dbtodoff(fs, dbn);
95 		if (ap->a_runp)
96 			*ap->a_runp = (*ap->a_runp + 1) << fs->fs_bshift;
97 		if (ap->a_runb)
98 			*ap->a_runb = *ap->a_runb << fs->fs_bshift;
99 	}
100 	return (error);
101 }
102 
103 /*
104  * Indirect blocks are now on the vnode for the file.  They are given negative
105  * logical block numbers.  Indirect blocks are addressed by the negative
106  * address of the first data block to which they point.  Double indirect blocks
107  * are addressed by one less than the address of the first indirect block to
108  * which they point.  Triple indirect blocks are addressed by one less than
109  * the address of the first double indirect block to which they point.
110  *
111  * ufs_bmaparray does the bmap conversion, and if requested returns the
112  * array of logical blocks which must be traversed to get to a block.
113  * Each entry contains the offset into that block that gets you to the
114  * next block and the disk address of the block (if it is assigned).
115  */
116 int
117 ufs_bmaparray(struct vnode *vp, ufs_daddr_t bn, ufs_daddr_t *bnp,
118 	      struct indir *ap, int *nump, int *runp, int *runb)
119 {
120 	struct inode *ip;
121 	struct buf *bp;
122 	struct ufsmount *ump;
123 	struct mount *mp;
124 	struct vnode *devvp;
125 	struct fs *fs;
126 	struct indir a[NIADDR+1], *xap;
127 	ufs_daddr_t daddr;
128 	long metalbn;
129 	int error, maxrun, num;
130 
131 	ip = VTOI(vp);
132 	mp = vp->v_mount;
133 	ump = VFSTOUFS(mp);
134 	devvp = ump->um_devvp;
135 	fs = ip->i_fs;
136 #ifdef DIAGNOSTIC
137 	if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
138 		panic("ufs_bmaparray: invalid arguments");
139 #endif
140 
141 	if (runp) {
142 		*runp = 0;
143 	}
144 
145 	if (runb) {
146 		*runb = 0;
147 	}
148 
149 	maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1;
150 
151 	xap = ap == NULL ? a : ap;
152 	if (!nump)
153 		nump = &num;
154 	error = ufs_getlbns(vp, bn, xap, nump);
155 	if (error)
156 		return (error);
157 
158 	num = *nump;
159 	if (num == 0) {
160 		*bnp = blkptrtodb(ump, ip->i_db[bn]);
161 		if (*bnp == 0)
162 			*bnp = -1;
163 		else if (runp) {
164 			daddr_t bnb = bn;
165 			for (++bn; bn < NDADDR && *runp < maxrun &&
166 			    is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
167 			    ++bn, ++*runp);
168 			bn = bnb;
169 			if (runb && (bn > 0)) {
170 				for (--bn; (bn >= 0) && (*runb < maxrun) &&
171 					is_sequential(ump, ip->i_db[bn],
172 						ip->i_db[bn+1]);
173 						--bn, ++*runb);
174 			}
175 		}
176 		return (0);
177 	}
178 
179 
180 	/* Get disk address out of indirect block array */
181 	daddr = ip->i_ib[xap->in_off];
182 
183 	for (bp = NULL, ++xap; --num; ++xap) {
184 		/*
185 		 * Exit the loop if there is no disk address assigned yet and
186 		 * the indirect block isn't in the cache, or if we were
187 		 * looking for an indirect block and we've found it.
188 		 */
189 
190 		metalbn = xap->in_lbn;
191 		if ((daddr == 0 && !findblk(vp, dbtodoff(fs, metalbn))) || metalbn == bn)
192 			break;
193 		/*
194 		 * If we get here, we've either got the block in the cache
195 		 * or we have a disk address for it, go fetch it.
196 		 */
197 		if (bp)
198 			bqrelse(bp);
199 
200 		xap->in_exists = 1;
201 		bp = getblk(vp, lblktodoff(fs, metalbn),
202 			    mp->mnt_stat.f_iosize, 0, 0);
203 		if ((bp->b_flags & B_CACHE) == 0) {
204 #ifdef DIAGNOSTIC
205 			if (!daddr)
206 				panic("ufs_bmaparray: indirect block not in cache");
207 #endif
208 			bp->b_bio2.bio_offset = fsbtodoff(fs, daddr);
209 			bp->b_flags &= ~(B_INVAL|B_ERROR);
210 			bp->b_cmd = BUF_CMD_READ;
211 			vfs_busy_pages(bp->b_vp, bp);
212 			vn_strategy(bp->b_vp, &bp->b_bio1);
213 			error = biowait(bp);
214 			if (error) {
215 				brelse(bp);
216 				return (error);
217 			}
218 		}
219 
220 		daddr = ((ufs_daddr_t *)bp->b_data)[xap->in_off];
221 		if (num == 1 && daddr && runp) {
222 			for (bn = xap->in_off + 1;
223 			    bn < MNINDIR(ump) && *runp < maxrun &&
224 			    is_sequential(ump,
225 			    ((ufs_daddr_t *)bp->b_data)[bn - 1],
226 			    ((ufs_daddr_t *)bp->b_data)[bn]);
227 			    ++bn, ++*runp);
228 			bn = xap->in_off;
229 			if (runb && bn) {
230 				for(--bn; bn >= 0 && *runb < maxrun &&
231 			    		is_sequential(ump, ((daddr_t *)bp->b_data)[bn],
232 					    ((daddr_t *)bp->b_data)[bn+1]);
233 			    		--bn, ++*runb);
234 			}
235 		}
236 	}
237 	if (bp)
238 		bqrelse(bp);
239 
240 	daddr = blkptrtodb(ump, daddr);
241 	*bnp = daddr == 0 ? -1 : daddr;
242 	return (0);
243 }
244 
245 /*
246  * Create an array of logical block number/offset pairs which represent the
247  * path of indirect blocks required to access a data block.  The first "pair"
248  * contains the logical block number of the appropriate single, double or
249  * triple indirect block and the offset into the inode indirect block array.
250  * Note, the logical block number of the inode single/double/triple indirect
251  * block appears twice in the array, once with the offset into the i_ib and
252  * once with the offset into the page itself.
253  */
254 int
255 ufs_getlbns(struct vnode *vp, ufs_daddr_t bn, struct indir *ap, int *nump)
256 {
257 	long blockcnt, metalbn, realbn;
258 	struct ufsmount *ump;
259 	int i, numlevels, off;
260 	int64_t qblockcnt;
261 
262 	ump = VFSTOUFS(vp->v_mount);
263 	if (nump)
264 		*nump = 0;
265 	numlevels = 0;
266 	realbn = bn;
267 	if ((long)bn < 0)
268 		bn = -(long)bn;
269 
270 	/* The first NDADDR blocks are direct blocks. */
271 	if (bn < NDADDR)
272 		return (0);
273 
274 	/*
275 	 * Determine the number of levels of indirection.  After this loop
276 	 * is done, blockcnt indicates the number of data blocks possible
277 	 * at the previous level of indirection, and NIADDR - i is the number
278 	 * of levels of indirection needed to locate the requested block.
279 	 */
280 	for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
281 		if (i == 0)
282 			return (EFBIG);
283 		/*
284 		 * Use int64_t's here to avoid overflow for triple indirect
285 		 * blocks when longs have 32 bits and the block size is more
286 		 * than 4K.
287 		 */
288 		qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
289 		if (bn < qblockcnt)
290 			break;
291 		blockcnt = qblockcnt;
292 	}
293 
294 	/* Calculate the address of the first meta-block. */
295 	if (realbn >= 0)
296 		metalbn = -(realbn - bn + NIADDR - i);
297 	else
298 		metalbn = -(-realbn - bn + NIADDR - i);
299 
300 	/*
301 	 * At each iteration, off is the offset into the bap array which is
302 	 * an array of disk addresses at the current level of indirection.
303 	 * The logical block number and the offset in that block are stored
304 	 * into the argument array.
305 	 */
306 	ap->in_lbn = metalbn;
307 	ap->in_off = off = NIADDR - i;
308 	ap->in_exists = 0;
309 	ap++;
310 	for (++numlevels; i <= NIADDR; i++) {
311 		/* If searching for a meta-data block, quit when found. */
312 		if (metalbn == realbn)
313 			break;
314 
315 		off = (bn / blockcnt) % MNINDIR(ump);
316 
317 		++numlevels;
318 		ap->in_lbn = metalbn;
319 		ap->in_off = off;
320 		ap->in_exists = 0;
321 		++ap;
322 
323 		metalbn -= -1 + off * blockcnt;
324 		blockcnt /= MNINDIR(ump);
325 	}
326 	if (nump)
327 		*nump = numlevels;
328 	return (0);
329 }
330