xref: /freebsd/sys/ufs/ufs/ufs_bmap.c (revision 4f52dfbb)
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 /*
60  * Bmap converts the logical block number of a file to its physical block
61  * number on the disk. The conversion is done by using the logical block
62  * number to index into the array of block pointers described by the dinode.
63  */
64 int
65 ufs_bmap(ap)
66 	struct vop_bmap_args /* {
67 		struct vnode *a_vp;
68 		daddr_t a_bn;
69 		struct bufobj **a_bop;
70 		daddr_t *a_bnp;
71 		int *a_runp;
72 		int *a_runb;
73 	} */ *ap;
74 {
75 	ufs2_daddr_t blkno;
76 	int error;
77 
78 	/*
79 	 * Check for underlying vnode requests and ensure that logical
80 	 * to physical mapping is requested.
81 	 */
82 	if (ap->a_bop != NULL)
83 		*ap->a_bop = &VFSTOUFS(ap->a_vp->v_mount)->um_devvp->v_bufobj;
84 	if (ap->a_bnp == NULL)
85 		return (0);
86 
87 	error = ufs_bmaparray(ap->a_vp, ap->a_bn, &blkno, NULL,
88 	    ap->a_runp, ap->a_runb);
89 	*ap->a_bnp = blkno;
90 	return (error);
91 }
92 
93 /*
94  * Indirect blocks are now on the vnode for the file.  They are given negative
95  * logical block numbers.  Indirect blocks are addressed by the negative
96  * address of the first data block to which they point.  Double indirect blocks
97  * are addressed by one less than the address of the first indirect block to
98  * which they point.  Triple indirect blocks are addressed by one less than
99  * the address of the first double indirect block to which they point.
100  *
101  * ufs_bmaparray does the bmap conversion, and if requested returns the
102  * array of logical blocks which must be traversed to get to a block.
103  * Each entry contains the offset into that block that gets you to the
104  * next block and the disk address of the block (if it is assigned).
105  */
106 
107 int
108 ufs_bmaparray(vp, bn, bnp, nbp, runp, runb)
109 	struct vnode *vp;
110 	ufs2_daddr_t bn;
111 	ufs2_daddr_t *bnp;
112 	struct buf *nbp;
113 	int *runp;
114 	int *runb;
115 {
116 	struct inode *ip;
117 	struct buf *bp;
118 	struct ufsmount *ump;
119 	struct mount *mp;
120 	struct indir a[UFS_NIADDR+1], *ap;
121 	ufs2_daddr_t daddr;
122 	ufs_lbn_t metalbn;
123 	int error, num, maxrun = 0;
124 	int *nump;
125 
126 	ap = NULL;
127 	ip = VTOI(vp);
128 	mp = vp->v_mount;
129 	ump = VFSTOUFS(mp);
130 
131 	if (runp) {
132 		maxrun = mp->mnt_iosize_max / mp->mnt_stat.f_iosize - 1;
133 		*runp = 0;
134 	}
135 
136 	if (runb) {
137 		*runb = 0;
138 	}
139 
140 
141 	ap = a;
142 	nump = &num;
143 	error = ufs_getlbns(vp, bn, ap, nump);
144 	if (error)
145 		return (error);
146 
147 	num = *nump;
148 	if (num == 0) {
149 		if (bn >= 0 && bn < UFS_NDADDR) {
150 			*bnp = blkptrtodb(ump, DIP(ip, i_db[bn]));
151 		} else if (bn < 0 && bn >= -UFS_NXADDR) {
152 			*bnp = blkptrtodb(ump, ip->i_din2->di_extb[-1 - bn]);
153 			if (*bnp == 0)
154 				*bnp = -1;
155 			if (nbp == NULL)
156 				panic("ufs_bmaparray: mapping ext data");
157 			nbp->b_xflags |= BX_ALTDATA;
158 			return (0);
159 		} else {
160 			panic("ufs_bmaparray: blkno out of range");
161 		}
162 		/*
163 		 * Since this is FFS independent code, we are out of
164 		 * scope for the definitions of BLK_NOCOPY and
165 		 * BLK_SNAP, but we do know that they will fall in
166 		 * the range 1..um_seqinc, so we use that test and
167 		 * return a request for a zeroed out buffer if attempts
168 		 * are made to read a BLK_NOCOPY or BLK_SNAP block.
169 		 */
170 		if ((ip->i_flags & SF_SNAPSHOT) && DIP(ip, i_db[bn]) > 0 &&
171 		    DIP(ip, i_db[bn]) < ump->um_seqinc) {
172 			*bnp = -1;
173 		} else if (*bnp == 0) {
174 			if (ip->i_flags & SF_SNAPSHOT)
175 				*bnp = blkptrtodb(ump, bn * ump->um_seqinc);
176 			else
177 				*bnp = -1;
178 		} else if (runp) {
179 			ufs2_daddr_t bnb = bn;
180 			for (++bn; bn < UFS_NDADDR && *runp < maxrun &&
181 			    is_sequential(ump, DIP(ip, i_db[bn - 1]),
182 			    DIP(ip, i_db[bn]));
183 			    ++bn, ++*runp);
184 			bn = bnb;
185 			if (runb && (bn > 0)) {
186 				for (--bn; (bn >= 0) && (*runb < maxrun) &&
187 					is_sequential(ump, DIP(ip, i_db[bn]),
188 						DIP(ip, i_db[bn+1]));
189 						--bn, ++*runb);
190 			}
191 		}
192 		return (0);
193 	}
194 
195 
196 	/* Get disk address out of indirect block array */
197 	daddr = DIP(ip, i_ib[ap->in_off]);
198 
199 	for (bp = NULL, ++ap; --num; ++ap) {
200 		/*
201 		 * Exit the loop if there is no disk address assigned yet and
202 		 * the indirect block isn't in the cache, or if we were
203 		 * looking for an indirect block and we've found it.
204 		 */
205 
206 		metalbn = ap->in_lbn;
207 		if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
208 			break;
209 		/*
210 		 * If we get here, we've either got the block in the cache
211 		 * or we have a disk address for it, go fetch it.
212 		 */
213 		if (bp)
214 			bqrelse(bp);
215 
216 		bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0, 0);
217 		if ((bp->b_flags & B_CACHE) == 0) {
218 #ifdef INVARIANTS
219 			if (!daddr)
220 				panic("ufs_bmaparray: indirect block not in cache");
221 #endif
222 			bp->b_blkno = blkptrtodb(ump, daddr);
223 			bp->b_iocmd = BIO_READ;
224 			bp->b_flags &= ~B_INVAL;
225 			bp->b_ioflags &= ~BIO_ERROR;
226 			vfs_busy_pages(bp, 0);
227 			bp->b_iooffset = dbtob(bp->b_blkno);
228 			bstrategy(bp);
229 #ifdef RACCT
230 			if (racct_enable) {
231 				PROC_LOCK(curproc);
232 				racct_add_buf(curproc, bp, 0);
233 				PROC_UNLOCK(curproc);
234 			}
235 #endif /* RACCT */
236 			curthread->td_ru.ru_inblock++;
237 			error = bufwait(bp);
238 			if (error) {
239 				brelse(bp);
240 				return (error);
241 			}
242 		}
243 
244 		if (I_IS_UFS1(ip)) {
245 			daddr = ((ufs1_daddr_t *)bp->b_data)[ap->in_off];
246 			if (num == 1 && daddr && runp) {
247 				for (bn = ap->in_off + 1;
248 				    bn < MNINDIR(ump) && *runp < maxrun &&
249 				    is_sequential(ump,
250 				    ((ufs1_daddr_t *)bp->b_data)[bn - 1],
251 				    ((ufs1_daddr_t *)bp->b_data)[bn]);
252 				    ++bn, ++*runp);
253 				bn = ap->in_off;
254 				if (runb && bn) {
255 					for (--bn; bn >= 0 && *runb < maxrun &&
256 					    is_sequential(ump,
257 					    ((ufs1_daddr_t *)bp->b_data)[bn],
258 					    ((ufs1_daddr_t *)bp->b_data)[bn+1]);
259 					    --bn, ++*runb);
260 				}
261 			}
262 			continue;
263 		}
264 		daddr = ((ufs2_daddr_t *)bp->b_data)[ap->in_off];
265 		if (num == 1 && daddr && runp) {
266 			for (bn = ap->in_off + 1;
267 			    bn < MNINDIR(ump) && *runp < maxrun &&
268 			    is_sequential(ump,
269 			    ((ufs2_daddr_t *)bp->b_data)[bn - 1],
270 			    ((ufs2_daddr_t *)bp->b_data)[bn]);
271 			    ++bn, ++*runp);
272 			bn = ap->in_off;
273 			if (runb && bn) {
274 				for (--bn; bn >= 0 && *runb < maxrun &&
275 				    is_sequential(ump,
276 				    ((ufs2_daddr_t *)bp->b_data)[bn],
277 				    ((ufs2_daddr_t *)bp->b_data)[bn + 1]);
278 				    --bn, ++*runb);
279 			}
280 		}
281 	}
282 	if (bp)
283 		bqrelse(bp);
284 
285 	/*
286 	 * Since this is FFS independent code, we are out of scope for the
287 	 * definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
288 	 * will fall in the range 1..um_seqinc, so we use that test and
289 	 * return a request for a zeroed out buffer if attempts are made
290 	 * to read a BLK_NOCOPY or BLK_SNAP block.
291 	 */
292 	if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc){
293 		*bnp = -1;
294 		return (0);
295 	}
296 	*bnp = blkptrtodb(ump, daddr);
297 	if (*bnp == 0) {
298 		if (ip->i_flags & SF_SNAPSHOT)
299 			*bnp = blkptrtodb(ump, bn * ump->um_seqinc);
300 		else
301 			*bnp = -1;
302 	}
303 	return (0);
304 }
305 
306 /*
307  * Create an array of logical block number/offset pairs which represent the
308  * path of indirect blocks required to access a data block.  The first "pair"
309  * contains the logical block number of the appropriate single, double or
310  * triple indirect block and the offset into the inode indirect block array.
311  * Note, the logical block number of the inode single/double/triple indirect
312  * block appears twice in the array, once with the offset into the i_ib and
313  * once with the offset into the page itself.
314  */
315 int
316 ufs_getlbns(vp, bn, ap, nump)
317 	struct vnode *vp;
318 	ufs2_daddr_t bn;
319 	struct indir *ap;
320 	int *nump;
321 {
322 	ufs2_daddr_t blockcnt;
323 	ufs_lbn_t metalbn, realbn;
324 	struct ufsmount *ump;
325 	int i, numlevels, off;
326 
327 	ump = VFSTOUFS(vp->v_mount);
328 	if (nump)
329 		*nump = 0;
330 	numlevels = 0;
331 	realbn = bn;
332 	if (bn < 0)
333 		bn = -bn;
334 
335 	/* The first UFS_NDADDR blocks are direct blocks. */
336 	if (bn < UFS_NDADDR)
337 		return (0);
338 
339 	/*
340 	 * Determine the number of levels of indirection.  After this loop
341 	 * is done, blockcnt indicates the number of data blocks possible
342 	 * at the previous level of indirection, and UFS_NIADDR - i is the
343 	 * number of levels of indirection needed to locate the requested block.
344 	 */
345 	for (blockcnt = 1, i = UFS_NIADDR, bn -= UFS_NDADDR; ;
346 	    i--, bn -= blockcnt) {
347 		if (i == 0)
348 			return (EFBIG);
349 		blockcnt *= MNINDIR(ump);
350 		if (bn < blockcnt)
351 			break;
352 	}
353 
354 	/* Calculate the address of the first meta-block. */
355 	if (realbn >= 0)
356 		metalbn = -(realbn - bn + UFS_NIADDR - i);
357 	else
358 		metalbn = -(-realbn - bn + UFS_NIADDR - i);
359 
360 	/*
361 	 * At each iteration, off is the offset into the bap array which is
362 	 * an array of disk addresses at the current level of indirection.
363 	 * The logical block number and the offset in that block are stored
364 	 * into the argument array.
365 	 */
366 	ap->in_lbn = metalbn;
367 	ap->in_off = off = UFS_NIADDR - i;
368 	ap++;
369 	for (++numlevels; i <= UFS_NIADDR; i++) {
370 		/* If searching for a meta-data block, quit when found. */
371 		if (metalbn == realbn)
372 			break;
373 
374 		blockcnt /= MNINDIR(ump);
375 		off = (bn / blockcnt) % MNINDIR(ump);
376 
377 		++numlevels;
378 		ap->in_lbn = metalbn;
379 		ap->in_off = off;
380 		++ap;
381 
382 		metalbn -= -1 + off * blockcnt;
383 	}
384 	if (nump)
385 		*nump = numlevels;
386 	return (0);
387 }
388