xref: /freebsd-13.1/sys/ufs/ufs/ufs_bmap.c (revision 8fcc0d7b)
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  * 3. 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  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/bio.h>
45 #include <sys/buf.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/racct.h>
50 #include <sys/resourcevar.h>
51 #include <sys/stat.h>
52 
53 #include <ufs/ufs/extattr.h>
54 #include <ufs/ufs/quota.h>
55 #include <ufs/ufs/inode.h>
56 #include <ufs/ufs/ufsmount.h>
57 #include <ufs/ufs/ufs_extern.h>
58 
59 static ufs_lbn_t lbn_count(struct ufsmount *, int);
60 static int readindir(struct vnode *, ufs_lbn_t, ufs2_daddr_t, struct buf **);
61 
62 /*
63  * Bmap converts the logical block number of a file to its physical block
64  * number on the disk. The conversion is done by using the logical block
65  * number to index into the array of block pointers described by the dinode.
66  */
67 int
ufs_bmap(ap)68 ufs_bmap(ap)
69 	struct vop_bmap_args /* {
70 		struct vnode *a_vp;
71 		daddr_t a_bn;
72 		struct bufobj **a_bop;
73 		daddr_t *a_bnp;
74 		int *a_runp;
75 		int *a_runb;
76 	} */ *ap;
77 {
78 	ufs2_daddr_t blkno;
79 	int error;
80 
81 	/*
82 	 * Check for underlying vnode requests and ensure that logical
83 	 * to physical mapping is requested.
84 	 */
85 	if (ap->a_bop != NULL)
86 		*ap->a_bop = &VFSTOUFS(ap->a_vp->v_mount)->um_devvp->v_bufobj;
87 	if (ap->a_bnp == NULL)
88 		return (0);
89 
90 	error = ufs_bmaparray(ap->a_vp, ap->a_bn, &blkno, NULL,
91 	    ap->a_runp, ap->a_runb);
92 	*ap->a_bnp = blkno;
93 	return (error);
94 }
95 
96 static int
readindir(vp,lbn,daddr,bpp)97 readindir(vp, lbn, daddr, bpp)
98 	struct vnode *vp;
99 	ufs_lbn_t lbn;
100 	ufs2_daddr_t daddr;
101 	struct buf **bpp;
102 {
103 	struct buf *bp;
104 	struct mount *mp;
105 	struct ufsmount *ump;
106 	int error;
107 
108 	mp = vp->v_mount;
109 	ump = VFSTOUFS(mp);
110 
111 	bp = getblk(vp, lbn, mp->mnt_stat.f_iosize, 0, 0, 0);
112 	if ((bp->b_flags & B_CACHE) == 0) {
113 		KASSERT(daddr != 0,
114 		    ("readindir: indirect block not in cache"));
115 
116 		bp->b_blkno = blkptrtodb(ump, daddr);
117 		bp->b_iocmd = BIO_READ;
118 		bp->b_flags &= ~B_INVAL;
119 		bp->b_ioflags &= ~BIO_ERROR;
120 		vfs_busy_pages(bp, 0);
121 		bp->b_iooffset = dbtob(bp->b_blkno);
122 		bstrategy(bp);
123 #ifdef RACCT
124 		if (racct_enable) {
125 			PROC_LOCK(curproc);
126 			racct_add_buf(curproc, bp, 0);
127 			PROC_UNLOCK(curproc);
128 		}
129 #endif
130 		curthread->td_ru.ru_inblock++;
131 		error = bufwait(bp);
132 		if (error != 0) {
133 			brelse(bp);
134 			return (error);
135 		}
136 	}
137 	*bpp = bp;
138 	return (0);
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  * ufs_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 
155 int
ufs_bmaparray(vp,bn,bnp,nbp,runp,runb)156 ufs_bmaparray(vp, bn, bnp, nbp, runp, runb)
157 	struct vnode *vp;
158 	ufs2_daddr_t bn;
159 	ufs2_daddr_t *bnp;
160 	struct buf *nbp;
161 	int *runp;
162 	int *runb;
163 {
164 	struct inode *ip;
165 	struct buf *bp;
166 	struct ufsmount *ump;
167 	struct mount *mp;
168 	struct indir a[UFS_NIADDR+1], *ap;
169 	ufs2_daddr_t daddr;
170 	ufs_lbn_t metalbn;
171 	int error, num, maxrun = 0;
172 	int *nump;
173 
174 	ap = NULL;
175 	ip = VTOI(vp);
176 	mp = vp->v_mount;
177 	ump = VFSTOUFS(mp);
178 
179 	if (runp) {
180 		maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1;
181 		*runp = 0;
182 	}
183 
184 	if (runb) {
185 		*runb = 0;
186 	}
187 
188 	ap = a;
189 	nump = &num;
190 	error = ufs_getlbns(vp, bn, ap, nump);
191 	if (error)
192 		return (error);
193 
194 	num = *nump;
195 	if (num == 0) {
196 		if (bn >= 0 && bn < UFS_NDADDR) {
197 			*bnp = blkptrtodb(ump, DIP(ip, i_db[bn]));
198 		} else if (bn < 0 && bn >= -UFS_NXADDR) {
199 			*bnp = blkptrtodb(ump, ip->i_din2->di_extb[-1 - bn]);
200 			if (*bnp == 0)
201 				*bnp = -1;
202 			if (nbp == NULL) {
203 				/* indirect block not found */
204 				return (EINVAL);
205 			}
206 			nbp->b_xflags |= BX_ALTDATA;
207 			return (0);
208 		} else {
209 			/* blkno out of range */
210 			return (EINVAL);
211 		}
212 		/*
213 		 * Since this is FFS independent code, we are out of
214 		 * scope for the definitions of BLK_NOCOPY and
215 		 * BLK_SNAP, but we do know that they will fall in
216 		 * the range 1..um_seqinc, so we use that test and
217 		 * return a request for a zeroed out buffer if attempts
218 		 * are made to read a BLK_NOCOPY or BLK_SNAP block.
219 		 */
220 		if (IS_SNAPSHOT(ip) && DIP(ip, i_db[bn]) > 0 &&
221 		    DIP(ip, i_db[bn]) < ump->um_seqinc) {
222 			*bnp = -1;
223 		} else if (*bnp == 0) {
224 			*bnp = IS_SNAPSHOT(ip) ? blkptrtodb(ump,
225 			    bn * ump->um_seqinc) : -1;
226 		} else if (runp) {
227 			ufs2_daddr_t bnb = bn;
228 			for (++bn; bn < UFS_NDADDR && *runp < maxrun &&
229 			    is_sequential(ump, DIP(ip, i_db[bn - 1]),
230 			    DIP(ip, i_db[bn]));
231 			    ++bn, ++*runp);
232 			bn = bnb;
233 			if (runb && (bn > 0)) {
234 				for (--bn; (bn >= 0) && (*runb < maxrun) &&
235 					is_sequential(ump, DIP(ip, i_db[bn]),
236 						DIP(ip, i_db[bn+1]));
237 						--bn, ++*runb);
238 			}
239 		}
240 		return (0);
241 	}
242 
243 	/* Get disk address out of indirect block array */
244 	daddr = DIP(ip, i_ib[ap->in_off]);
245 
246 	for (bp = NULL, ++ap; --num; ++ap) {
247 		/*
248 		 * Exit the loop if there is no disk address assigned yet and
249 		 * the indirect block isn't in the cache, or if we were
250 		 * looking for an indirect block and we've found it.
251 		 */
252 
253 		metalbn = ap->in_lbn;
254 		if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
255 			break;
256 		/*
257 		 * If we get here, we've either got the block in the cache
258 		 * or we have a disk address for it, go fetch it.
259 		 */
260 		if (bp)
261 			bqrelse(bp);
262 		error = readindir(vp, metalbn, daddr, &bp);
263 		if (error != 0)
264 			return (error);
265 
266 		if (I_IS_UFS1(ip))
267 			daddr = ((ufs1_daddr_t *)bp->b_data)[ap->in_off];
268 		else
269 			daddr = ((ufs2_daddr_t *)bp->b_data)[ap->in_off];
270 		if ((error = UFS_CHECK_BLKNO(mp, ip->i_number, daddr,
271 		     mp->mnt_stat.f_iosize)) != 0) {
272 			bqrelse(bp);
273 			return (error);
274 		}
275 		if (I_IS_UFS1(ip)) {
276 			if (num == 1 && daddr && runp) {
277 				for (bn = ap->in_off + 1;
278 				    bn < MNINDIR(ump) && *runp < maxrun &&
279 				    is_sequential(ump,
280 				    ((ufs1_daddr_t *)bp->b_data)[bn - 1],
281 				    ((ufs1_daddr_t *)bp->b_data)[bn]);
282 				    ++bn, ++*runp);
283 				bn = ap->in_off;
284 				if (runb && bn) {
285 					for (--bn; bn >= 0 && *runb < maxrun &&
286 					    is_sequential(ump,
287 					    ((ufs1_daddr_t *)bp->b_data)[bn],
288 					    ((ufs1_daddr_t *)bp->b_data)[bn+1]);
289 					    --bn, ++*runb);
290 				}
291 			}
292 			continue;
293 		}
294 		if (num == 1 && daddr && runp) {
295 			for (bn = ap->in_off + 1;
296 			    bn < MNINDIR(ump) && *runp < maxrun &&
297 			    is_sequential(ump,
298 			    ((ufs2_daddr_t *)bp->b_data)[bn - 1],
299 			    ((ufs2_daddr_t *)bp->b_data)[bn]);
300 			    ++bn, ++*runp);
301 			bn = ap->in_off;
302 			if (runb && bn) {
303 				for (--bn; bn >= 0 && *runb < maxrun &&
304 				    is_sequential(ump,
305 				    ((ufs2_daddr_t *)bp->b_data)[bn],
306 				    ((ufs2_daddr_t *)bp->b_data)[bn + 1]);
307 				    --bn, ++*runb);
308 			}
309 		}
310 	}
311 	if (bp)
312 		bqrelse(bp);
313 
314 	/*
315 	 * Since this is FFS independent code, we are out of scope for the
316 	 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
317 	 * will fall in the range 1..um_seqinc, so we use that test and
318 	 * return a request for a zeroed out buffer if attempts are made
319 	 * to read a BLK_NOCOPY or BLK_SNAP block.
320 	 */
321 	if (IS_SNAPSHOT(ip) && daddr > 0 && daddr < ump->um_seqinc){
322 		*bnp = -1;
323 		return (0);
324 	}
325 	*bnp = blkptrtodb(ump, daddr);
326 	if (*bnp == 0) {
327 		if (IS_SNAPSHOT(ip))
328 			*bnp = blkptrtodb(ump, bn * ump->um_seqinc);
329 		else
330 			*bnp = -1;
331 	}
332 	return (0);
333 }
334 
335 static ufs_lbn_t
lbn_count(ump,level)336 lbn_count(ump, level)
337 	struct ufsmount *ump;
338 	int level;
339 {
340 	ufs_lbn_t blockcnt;
341 
342 	for (blockcnt = 1; level > 0; level--)
343 		blockcnt *= MNINDIR(ump);
344 	return (blockcnt);
345 }
346 
347 int
ufs_bmap_seekdata(vp,offp)348 ufs_bmap_seekdata(vp, offp)
349 	struct vnode *vp;
350 	off_t *offp;
351 {
352 	struct buf *bp;
353 	struct indir a[UFS_NIADDR + 1], *ap;
354 	struct inode *ip;
355 	struct mount *mp;
356 	struct ufsmount *ump;
357 	ufs2_daddr_t bn, daddr, nextbn;
358 	uint64_t bsize;
359 	off_t numblks;
360 	int error, num, num1, off;
361 
362 	bp = NULL;
363 	error = 0;
364 	ip = VTOI(vp);
365 	mp = vp->v_mount;
366 	ump = VFSTOUFS(mp);
367 
368 	if (vp->v_type != VREG || IS_SNAPSHOT(ip))
369 		return (EINVAL);
370 	if (*offp < 0 || *offp >= ip->i_size)
371 		return (ENXIO);
372 
373 	bsize = mp->mnt_stat.f_iosize;
374 	for (bn = *offp / bsize, numblks = howmany(ip->i_size, bsize);
375 	    bn < numblks; bn = nextbn) {
376 		if (bn < UFS_NDADDR) {
377 			daddr = DIP(ip, i_db[bn]);
378 			if (daddr != 0)
379 				break;
380 			nextbn = bn + 1;
381 			continue;
382 		}
383 
384 		ap = a;
385 		error = ufs_getlbns(vp, bn, ap, &num);
386 		if (error != 0)
387 			break;
388 		MPASS(num >= 2);
389 		daddr = DIP(ip, i_ib[ap->in_off]);
390 		ap++, num--;
391 		for (nextbn = UFS_NDADDR, num1 = num - 1; num1 > 0; num1--)
392 			nextbn += lbn_count(ump, num1);
393 		if (daddr == 0) {
394 			nextbn += lbn_count(ump, num);
395 			continue;
396 		}
397 
398 		for (; daddr != 0 && num > 0; ap++, num--) {
399 			if (bp != NULL)
400 				bqrelse(bp);
401 			error = readindir(vp, ap->in_lbn, daddr, &bp);
402 			if (error != 0)
403 				return (error);
404 
405 			/*
406 			 * Scan the indirect block until we find a non-zero
407 			 * pointer.
408 			 */
409 			off = ap->in_off;
410 			do {
411 				daddr = I_IS_UFS1(ip) ?
412 				    ((ufs1_daddr_t *)bp->b_data)[off] :
413 				    ((ufs2_daddr_t *)bp->b_data)[off];
414 			} while (daddr == 0 && ++off < MNINDIR(ump));
415 			nextbn += off * lbn_count(ump, num - 1);
416 
417 			/*
418 			 * We need to recompute the LBNs of indirect
419 			 * blocks, so restart with the updated block offset.
420 			 */
421 			if (off != ap->in_off)
422 				break;
423 		}
424 		if (num == 0) {
425 			/*
426 			 * We found a data block.
427 			 */
428 			bn = nextbn;
429 			break;
430 		}
431 	}
432 	if (bp != NULL)
433 		bqrelse(bp);
434 	if (bn >= numblks)
435 		error = ENXIO;
436 	if (error == 0 && *offp < bn * bsize)
437 		*offp = bn * bsize;
438 	return (error);
439 }
440 
441 /*
442  * Create an array of logical block number/offset pairs which represent the
443  * path of indirect blocks required to access a data block.  The first "pair"
444  * contains the logical block number of the appropriate single, double or
445  * triple indirect block and the offset into the inode indirect block array.
446  * Note, the logical block number of the inode single/double/triple indirect
447  * block appears twice in the array, once with the offset into the i_ib and
448  * once with the offset into the page itself.
449  */
450 int
ufs_getlbns(vp,bn,ap,nump)451 ufs_getlbns(vp, bn, ap, nump)
452 	struct vnode *vp;
453 	ufs2_daddr_t bn;
454 	struct indir *ap;
455 	int *nump;
456 {
457 	ufs2_daddr_t blockcnt;
458 	ufs_lbn_t metalbn, realbn;
459 	struct ufsmount *ump;
460 	int i, numlevels, off;
461 
462 	ump = VFSTOUFS(vp->v_mount);
463 	if (nump)
464 		*nump = 0;
465 	numlevels = 0;
466 	realbn = bn;
467 	if (bn < 0)
468 		bn = -bn;
469 
470 	/* The first UFS_NDADDR blocks are direct blocks. */
471 	if (bn < UFS_NDADDR)
472 		return (0);
473 
474 	/*
475 	 * Determine the number of levels of indirection.  After this loop
476 	 * is done, blockcnt indicates the number of data blocks possible
477 	 * at the previous level of indirection, and UFS_NIADDR - i is the
478 	 * number of levels of indirection needed to locate the requested block.
479 	 */
480 	for (blockcnt = 1, i = UFS_NIADDR, bn -= UFS_NDADDR; ;
481 	    i--, bn -= blockcnt) {
482 		if (i == 0)
483 			return (EFBIG);
484 		blockcnt *= MNINDIR(ump);
485 		if (bn < blockcnt)
486 			break;
487 	}
488 
489 	/* Calculate the address of the first meta-block. */
490 	if (realbn >= 0)
491 		metalbn = -(realbn - bn + UFS_NIADDR - i);
492 	else
493 		metalbn = -(-realbn - bn + UFS_NIADDR - i);
494 
495 	/*
496 	 * At each iteration, off is the offset into the bap array which is
497 	 * an array of disk addresses at the current level of indirection.
498 	 * The logical block number and the offset in that block are stored
499 	 * into the argument array.
500 	 */
501 	ap->in_lbn = metalbn;
502 	ap->in_off = off = UFS_NIADDR - i;
503 	ap++;
504 	for (++numlevels; i <= UFS_NIADDR; i++) {
505 		/* If searching for a meta-data block, quit when found. */
506 		if (metalbn == realbn)
507 			break;
508 
509 		blockcnt /= MNINDIR(ump);
510 		off = (bn / blockcnt) % MNINDIR(ump);
511 
512 		++numlevels;
513 		ap->in_lbn = metalbn;
514 		ap->in_off = off;
515 		++ap;
516 
517 		metalbn -= -1 + off * blockcnt;
518 	}
519 	if (nump)
520 		*nump = numlevels;
521 	return (0);
522 }
523