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