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