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