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