xref: /netbsd-src/sys/ufs/ext2fs/ext2fs_bmap.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: ext2fs_bmap.c,v 1.16 2004/08/15 07:19:56 mycroft 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. 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.6 (Berkeley) 1/21/94
37  * Modified for ext2fs by Manuel Bouyer.
38  */
39 
40 /*
41  * Copyright (c) 1997 Manuel Bouyer.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by Manuel Bouyer.
54  * 4. The name of the author may not be used to endorse or promote products
55  *    derived from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  *	@(#)ufs_bmap.c	8.6 (Berkeley) 1/21/94
69  * Modified for ext2fs by Manuel Bouyer.
70  */
71 
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: ext2fs_bmap.c,v 1.16 2004/08/15 07:19:56 mycroft Exp $");
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/buf.h>
78 #include <sys/proc.h>
79 #include <sys/vnode.h>
80 #include <sys/mount.h>
81 #include <sys/resourcevar.h>
82 #include <sys/trace.h>
83 
84 #include <miscfs/specfs/specdev.h>
85 
86 #include <ufs/ufs/inode.h>
87 #include <ufs/ufs/ufsmount.h>
88 #include <ufs/ufs/ufs_extern.h>
89 #include <ufs/ext2fs/ext2fs.h>
90 #include <ufs/ext2fs/ext2fs_extern.h>
91 
92 static int ext2fs_bmaparray __P((struct vnode *, daddr_t, daddr_t *,
93 								struct indir *, int *, int *));
94 
95 #define	is_sequential(ump, a, b)	((b) == (a) + ump->um_seqinc)
96 
97 /*
98  * Bmap converts a the logical block number of a file to its physical block
99  * number on the disk. The conversion is done by using the logical block
100  * number to index into the array of block pointers described by the dinode.
101  */
102 int
103 ext2fs_bmap(v)
104 	void *v;
105 {
106 	struct vop_bmap_args /* {
107 		struct vnode *a_vp;
108 		daddr_t  a_bn;
109 		struct vnode **a_vpp;
110 		daddr_t *a_bnp;
111 		int *a_runp;
112 	} */ *ap = v;
113 	/*
114 	 * Check for underlying vnode requests and ensure that logical
115 	 * to physical mapping is requested.
116 	 */
117 	if (ap->a_vpp != NULL)
118 		*ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
119 	if (ap->a_bnp == NULL)
120 		return (0);
121 
122 	return (ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
123 		ap->a_runp));
124 }
125 
126 /*
127  * Indirect blocks are now on the vnode for the file.  They are given negative
128  * logical block numbers.  Indirect blocks are addressed by the negative
129  * address of the first data block to which they point.  Double indirect blocks
130  * are addressed by one less than the address of the first indirect block to
131  * which they point.  Triple indirect blocks are addressed by one less than
132  * the address of the first double indirect block to which they point.
133  *
134  * ext2fs_bmaparray does the bmap conversion, and if requested returns the
135  * array of logical blocks which must be traversed to get to a block.
136  * Each entry contains the offset into that block that gets you to the
137  * next block and the disk address of the block (if it is assigned).
138  */
139 
140 int
141 ext2fs_bmaparray(vp, bn, bnp, ap, nump, runp)
142 	struct vnode *vp;
143 	daddr_t bn;
144 	daddr_t *bnp;
145 	struct indir *ap;
146 	int *nump;
147 	int *runp;
148 {
149 	struct inode *ip;
150 	struct buf *bp;
151 	struct ufsmount *ump;
152 	struct mount *mp;
153 	struct indir a[NIADDR+1], *xap;
154 	daddr_t daddr;
155 	daddr_t metalbn;
156 	int error, maxrun = 0, num;
157 
158 	ip = VTOI(vp);
159 	mp = vp->v_mount;
160 	ump = ip->i_ump;
161 #ifdef DIAGNOSTIC
162 	if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
163 		panic("ext2fs_bmaparray: invalid arguments");
164 #endif
165 
166 	if (runp) {
167 		/*
168 		 * XXX
169 		 * If MAXBSIZE is the largest transfer the disks can handle,
170 		 * we probably want maxrun to be 1 block less so that we
171 		 * don't create a block larger than the device can handle.
172 		 */
173 		*runp = 0;
174 		maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
175 	}
176 
177 	if (bn >= 0 && bn < NDADDR) {
178 		/* XXX ondisk32 */
179 		*bnp = blkptrtodb(ump, (daddr_t)fs2h32(ip->i_e2fs_blocks[bn]));
180 		if (*bnp == 0)
181 			*bnp = -1;
182 		else if (runp)
183 			/* XXX ondisk32 */
184 			for (++bn; bn < NDADDR && *runp < maxrun &&
185 				is_sequential(ump, (daddr_t)fs2h32(ip->i_e2fs_blocks[bn - 1]),
186 							  (daddr_t)fs2h32(ip->i_e2fs_blocks[bn]));
187 				++bn, ++*runp);
188 		return (0);
189 	}
190 
191 	xap = ap == NULL ? a : ap;
192 	if (!nump)
193 		nump = &num;
194 	if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0)
195 		return (error);
196 
197 	num = *nump;
198 
199 	/* Get disk address out of indirect block array */
200 	/* XXX ondisk32 */
201 	daddr = fs2h32(ip->i_e2fs_blocks[NDADDR + xap->in_off]);
202 
203 #ifdef DIAGNOSTIC
204     if (num > NIADDR + 1 || num < 1) {
205 		printf("ext2fs_bmaparray: num=%d\n", num);
206 		panic("ext2fs_bmaparray: num");
207 	}
208 #endif
209 	for (bp = NULL, ++xap; --num; ++xap) {
210 		/*
211 		 * Exit the loop if there is no disk address assigned yet and
212 		 * the indirect block isn't in the cache, or if we were
213 		 * looking for an indirect block and we've found it.
214 		 */
215 
216 		metalbn = xap->in_lbn;
217 		if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
218 			break;
219 		/*
220 		 * If we get here, we've either got the block in the cache
221 		 * or we have a disk address for it, go fetch it.
222 		 */
223 		if (bp)
224 			brelse(bp);
225 
226 		xap->in_exists = 1;
227 		bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
228 		if (bp->b_flags & (B_DONE | B_DELWRI)) {
229 			trace(TR_BREADHIT, pack(vp, size), metalbn);
230 		}
231 #ifdef DIAGNOSTIC
232 		else if (!daddr)
233 			panic("ext2fs_bmaparry: indirect block not in cache");
234 #endif
235 		else {
236 			trace(TR_BREADMISS, pack(vp, size), metalbn);
237 			bp->b_blkno = blkptrtodb(ump, daddr);
238 			bp->b_flags |= B_READ;
239 			VOP_STRATEGY(vp, bp);
240 			curproc->p_stats->p_ru.ru_inblock++;	/* XXX */
241 			if ((error = biowait(bp)) != 0) {
242 				brelse(bp);
243 				return (error);
244 			}
245 		}
246 
247 		/* XXX ondisk32 */
248 		daddr = fs2h32(((int32_t *)bp->b_data)[xap->in_off]);
249 		if (num == 1 && daddr && runp)
250 			/* XXX ondisk32 */
251 			for (bn = xap->in_off + 1;
252 				bn < MNINDIR(ump) && *runp < maxrun &&
253 				is_sequential(ump, ((int32_t *)bp->b_data)[bn - 1],
254 				((int32_t *)bp->b_data)[bn]);
255 				++bn, ++*runp);
256 	}
257 	if (bp)
258 		brelse(bp);
259 
260 	daddr = blkptrtodb(ump, daddr);
261 	*bnp = daddr == 0 ? -1 : daddr;
262 	return (0);
263 }
264