xref: /dragonfly/sys/vfs/ufs/ufs_bmap.c (revision 8accc937)
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  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/buf.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/resourcevar.h>
49 #include <sys/conf.h>
50 
51 #include "quota.h"
52 #include "inode.h"
53 #include "ufsmount.h"
54 #include "ufs_extern.h"
55 #include "fs.h"
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  * BMAP must return the contiguous before and after run in bytes, inclusive
63  * of the returned block.
64  *
65  * ufs_bmap(struct vnode *a_vp, off_t a_loffset,
66  *	    off_t *a_doffsetp, int *a_runp, int *a_runb)
67  */
68 int
69 ufs_bmap(struct vop_bmap_args *ap)
70 {
71 	struct fs *fs;
72 	ufs_daddr_t lbn;
73 	ufs_daddr_t dbn;
74 	int error;
75 
76 	/*
77 	 * Check for underlying vnode requests and ensure that logical
78 	 * to physical mapping is requested.
79 	 */
80 	if (ap->a_doffsetp == NULL)
81 		return (0);
82 
83 	fs = VTOI(ap->a_vp)->i_fs;
84 	KKASSERT(((int)ap->a_loffset & ((1 << fs->fs_bshift) - 1)) == 0);
85 	lbn = ap->a_loffset >> fs->fs_bshift;
86 
87 	error = ufs_bmaparray(ap->a_vp, lbn, &dbn, NULL, NULL,
88 			      ap->a_runp, ap->a_runb);
89 
90 	if (error || dbn == (ufs_daddr_t)-1) {
91 		*ap->a_doffsetp = NOOFFSET;
92 	} else {
93 		*ap->a_doffsetp = dbtodoff(fs, dbn);
94 		if (ap->a_runp)
95 			*ap->a_runp = (*ap->a_runp + 1) << fs->fs_bshift;
96 		if (ap->a_runb)
97 			*ap->a_runb = *ap->a_runb << fs->fs_bshift;
98 	}
99 	return (error);
100 }
101 
102 /*
103  * Indirect blocks are now on the vnode for the file.  They are given negative
104  * logical block numbers.  Indirect blocks are addressed by the negative
105  * address of the first data block to which they point.  Double indirect blocks
106  * are addressed by one less than the address of the first indirect block to
107  * which they point.  Triple indirect blocks are addressed by one less than
108  * the address of the first double indirect block to which they point.
109  *
110  * ufs_bmaparray does the bmap conversion, and if requested returns the
111  * array of logical blocks which must be traversed to get to a block.
112  * Each entry contains the offset into that block that gets you to the
113  * next block and the disk address of the block (if it is assigned).
114  */
115 int
116 ufs_bmaparray(struct vnode *vp, ufs_daddr_t bn, ufs_daddr_t *bnp,
117 	      struct indir *ap, int *nump, int *runp, int *runb)
118 {
119 	struct inode *ip;
120 	struct buf *bp;
121 	struct ufsmount *ump;
122 	struct mount *mp;
123 	struct fs *fs;
124 	struct indir a[NIADDR+1], *xap;
125 	ufs_daddr_t daddr;
126 	long metalbn;
127 	int error, maxrun, num;
128 
129 	ip = VTOI(vp);
130 	mp = vp->v_mount;
131 	ump = VFSTOUFS(mp);
132 	fs = ip->i_fs;
133 #ifdef DIAGNOSTIC
134 	if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
135 		panic("ufs_bmaparray: invalid arguments");
136 #endif
137 
138 	if (runp) {
139 		*runp = 0;
140 	}
141 
142 	if (runb) {
143 		*runb = 0;
144 	}
145 
146 	maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1;
147 
148 	xap = ap == NULL ? a : ap;
149 	if (!nump)
150 		nump = &num;
151 	error = ufs_getlbns(vp, bn, xap, nump);
152 	if (error)
153 		return (error);
154 
155 	num = *nump;
156 	if (num == 0) {
157 		*bnp = blkptrtodb(ump, ip->i_db[bn]);
158 		if (*bnp == 0)
159 			*bnp = -1;
160 		else if (runp) {
161 			daddr_t bnb = bn;
162 			for (++bn; bn < NDADDR && *runp < maxrun &&
163 			    is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
164 			    ++bn, ++*runp);
165 			bn = bnb;
166 			if (runb && (bn > 0)) {
167 				for (--bn; (bn >= 0) && (*runb < maxrun) &&
168 					is_sequential(ump, ip->i_db[bn],
169 						ip->i_db[bn+1]);
170 						--bn, ++*runb);
171 			}
172 		}
173 		return (0);
174 	}
175 
176 
177 	/* Get disk address out of indirect block array */
178 	daddr = ip->i_ib[xap->in_off];
179 
180 	for (bp = NULL, ++xap; --num; ++xap) {
181 		/*
182 		 * Exit the loop if there is no disk address assigned yet and
183 		 * the indirect block isn't in the cache, or if we were
184 		 * looking for an indirect block and we've found it.
185 		 */
186 		metalbn = xap->in_lbn;
187 		if ((daddr == 0 &&
188 		     !findblk(vp, dbtodoff(fs, metalbn), FINDBLK_TEST)) ||
189 		    metalbn == bn) {
190 			break;
191 		}
192 		/*
193 		 * If we get here, we've either got the block in the cache
194 		 * or we have a disk address for it, go fetch it.
195 		 */
196 		if (bp)
197 			bqrelse(bp);
198 
199 		xap->in_exists = 1;
200 		bp = getblk(vp, lblktodoff(fs, metalbn),
201 			    mp->mnt_stat.f_iosize, 0, 0);
202 		if ((bp->b_flags & B_CACHE) == 0) {
203 #ifdef DIAGNOSTIC
204 			if (!daddr)
205 				panic("ufs_bmaparray: indirect block not in cache");
206 #endif
207 			/*
208 			 * cached disk addr in bio2, do I/O on bio1.  It
209 			 * will probably hit the vfs's strategy function
210 			 * which will then use the cached offset in bio2.
211 			 */
212 			bp->b_bio1.bio_done = biodone_sync;
213 			bp->b_bio1.bio_flags |= BIO_SYNC;
214 			bp->b_bio2.bio_offset = fsbtodoff(fs, daddr);
215 			bp->b_flags &= ~(B_INVAL|B_ERROR);
216 			bp->b_cmd = BUF_CMD_READ;
217 			vfs_busy_pages(bp->b_vp, bp);
218 			vn_strategy(bp->b_vp, &bp->b_bio1);
219 			error = biowait(&bp->b_bio1, "biord");
220 			if (error) {
221 				brelse(bp);
222 				return (error);
223 			}
224 		}
225 
226 		daddr = ((ufs_daddr_t *)bp->b_data)[xap->in_off];
227 		if (num == 1 && daddr && runp) {
228 			for (bn = xap->in_off + 1;
229 			    bn < MNINDIR(ump) && *runp < maxrun &&
230 			    is_sequential(ump,
231 			    ((ufs_daddr_t *)bp->b_data)[bn - 1],
232 			    ((ufs_daddr_t *)bp->b_data)[bn]);
233 			    ++bn, ++*runp);
234 			bn = xap->in_off;
235 			if (runb && bn) {
236 				for(--bn; bn >= 0 && *runb < maxrun &&
237 			    		is_sequential(ump, ((daddr_t *)bp->b_data)[bn],
238 					    ((daddr_t *)bp->b_data)[bn+1]);
239 			    		--bn, ++*runb);
240 			}
241 		}
242 	}
243 	if (bp)
244 		bqrelse(bp);
245 
246 	daddr = blkptrtodb(ump, daddr);
247 	*bnp = daddr == 0 ? -1 : daddr;
248 	return (0);
249 }
250 
251 /*
252  * Create an array of logical block number/offset pairs which represent the
253  * path of indirect blocks required to access a data block.  The first "pair"
254  * contains the logical block number of the appropriate single, double or
255  * triple indirect block and the offset into the inode indirect block array.
256  * Note, the logical block number of the inode single/double/triple indirect
257  * block appears twice in the array, once with the offset into the i_ib and
258  * once with the offset into the page itself.
259  */
260 int
261 ufs_getlbns(struct vnode *vp, ufs_daddr_t bn, struct indir *ap, int *nump)
262 {
263 	long blockcnt, metalbn, realbn;
264 	struct ufsmount *ump;
265 	int i, numlevels, off;
266 	int64_t qblockcnt;
267 
268 	ump = VFSTOUFS(vp->v_mount);
269 	if (nump)
270 		*nump = 0;
271 	numlevels = 0;
272 	realbn = bn;
273 	if ((long)bn < 0)
274 		bn = -(long)bn;
275 
276 	/* The first NDADDR blocks are direct blocks. */
277 	if (bn < NDADDR)
278 		return (0);
279 
280 	/*
281 	 * Determine the number of levels of indirection.  After this loop
282 	 * is done, blockcnt indicates the number of data blocks possible
283 	 * at the previous level of indirection, and NIADDR - i is the number
284 	 * of levels of indirection needed to locate the requested block.
285 	 */
286 	for (blockcnt = 1, i = NIADDR, bn -= NDADDR;; i--, bn -= blockcnt) {
287 		if (i == 0)
288 			return (EFBIG);
289 		/*
290 		 * Use int64_t's here to avoid overflow for triple indirect
291 		 * blocks when longs have 32 bits and the block size is more
292 		 * than 4K.
293 		 */
294 		qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
295 		if (bn < qblockcnt)
296 			break;
297 		blockcnt = qblockcnt;
298 	}
299 
300 	/* Calculate the address of the first meta-block. */
301 	if (realbn >= 0)
302 		metalbn = -(realbn - bn + NIADDR - i);
303 	else
304 		metalbn = -(-realbn - bn + NIADDR - i);
305 
306 	/*
307 	 * At each iteration, off is the offset into the bap array which is
308 	 * an array of disk addresses at the current level of indirection.
309 	 * The logical block number and the offset in that block are stored
310 	 * into the argument array.
311 	 */
312 	ap->in_lbn = metalbn;
313 	ap->in_off = off = NIADDR - i;
314 	ap->in_exists = 0;
315 	ap++;
316 	for (++numlevels; i <= NIADDR; i++) {
317 		/* If searching for a meta-data block, quit when found. */
318 		if (metalbn == realbn)
319 			break;
320 
321 		off = (bn / blockcnt) % MNINDIR(ump);
322 
323 		++numlevels;
324 		ap->in_lbn = metalbn;
325 		ap->in_off = off;
326 		ap->in_exists = 0;
327 		++ap;
328 
329 		metalbn -= -1 + off * blockcnt;
330 		blockcnt /= MNINDIR(ump);
331 	}
332 	if (nump)
333 		*nump = numlevels;
334 	return (0);
335 }
336