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