ext2fs_bmap.c revision 1.5.6.2 1 /* $NetBSD: ext2fs_bmap.c,v 1.5.6.2 2001/11/14 19:18:53 nathanw 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.5.6.2 2001/11/14 19:18:53 nathanw Exp $");
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/buf.h>
51 #include <sys/lwp.h>
52 #include <sys/proc.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/resourcevar.h>
56 #include <sys/trace.h>
57
58 #include <miscfs/specfs/specdev.h>
59
60 #include <ufs/ufs/inode.h>
61 #include <ufs/ufs/ufsmount.h>
62 #include <ufs/ufs/ufs_extern.h>
63 #include <ufs/ext2fs/ext2fs.h>
64 #include <ufs/ext2fs/ext2fs_extern.h>
65
66 static int ext2fs_bmaparray __P((struct vnode *, ufs_daddr_t, ufs_daddr_t *,
67 struct indir *, int *, int *));
68
69 /*
70 * Bmap converts a the logical block number of a file to its physical block
71 * number on the disk. The conversion is done by using the logical block
72 * number to index into the array of block pointers described by the dinode.
73 */
74 int
75 ext2fs_bmap(v)
76 void *v;
77 {
78 struct vop_bmap_args /* {
79 struct vnode *a_vp;
80 daddr_t a_bn;
81 struct vnode **a_vpp;
82 daddr_t *a_bnp;
83 int *a_runp;
84 } */ *ap = v;
85 /*
86 * Check for underlying vnode requests and ensure that logical
87 * to physical mapping is requested.
88 */
89 if (ap->a_vpp != NULL)
90 *ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
91 if (ap->a_bnp == NULL)
92 return (0);
93
94 return (ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
95 ap->a_runp));
96 }
97
98 /*
99 * Indirect blocks are now on the vnode for the file. They are given negative
100 * logical block numbers. Indirect blocks are addressed by the negative
101 * address of the first data block to which they point. Double indirect blocks
102 * are addressed by one less than the address of the first indirect block to
103 * which they point. Triple indirect blocks are addressed by one less than
104 * the address of the first double indirect block to which they point.
105 *
106 * ext2fs_bmaparray does the bmap conversion, and if requested returns the
107 * array of logical blocks which must be traversed to get to a block.
108 * Each entry contains the offset into that block that gets you to the
109 * next block and the disk address of the block (if it is assigned).
110 */
111
112 int
113 ext2fs_bmaparray(vp, bn, bnp, ap, nump, runp)
114 struct vnode *vp;
115 ufs_daddr_t bn;
116 ufs_daddr_t *bnp;
117 struct indir *ap;
118 int *nump;
119 int *runp;
120 {
121 struct inode *ip;
122 struct buf *bp;
123 struct ufsmount *ump;
124 struct mount *mp;
125 struct indir a[NIADDR+1], *xap;
126 ufs_daddr_t daddr;
127 long metalbn;
128 int error, maxrun = 0, num;
129
130 ip = VTOI(vp);
131 mp = vp->v_mount;
132 ump = VFSTOUFS(mp);
133 #ifdef DIAGNOSTIC
134 if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
135 panic("ext2fs_bmaparray: invalid arguments");
136 #endif
137
138 if (runp) {
139 /*
140 * XXX
141 * If MAXBSIZE is the largest transfer the disks can handle,
142 * we probably want maxrun to be 1 block less so that we
143 * don't create a block larger than the device can handle.
144 */
145 *runp = 0;
146 maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
147 }
148
149 if (bn >= 0 && bn < NDADDR) {
150 *bnp = blkptrtodb(ump, fs2h32(ip->i_e2fs_blocks[bn]));
151 if (*bnp == 0)
152 *bnp = -1;
153 else if (runp)
154 for (++bn; bn < NDADDR && *runp < maxrun &&
155 is_sequential(ump, fs2h32(ip->i_e2fs_blocks[bn - 1]),
156 fs2h32(ip->i_e2fs_blocks[bn]));
157 ++bn, ++*runp);
158 return (0);
159 }
160
161 xap = ap == NULL ? a : ap;
162 if (!nump)
163 nump = #
164 if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0)
165 return (error);
166
167 num = *nump;
168
169 /* Get disk address out of indirect block array */
170 daddr = fs2h32(ip->i_e2fs_blocks[NDADDR + xap->in_off]);
171
172 #ifdef DIAGNOSTIC
173 if (num > NIADDR + 1 || num < 1) {
174 printf("ext2fs_bmaparray: num=%d\n", num);
175 panic("ext2fs_bmaparray: num");
176 }
177 #endif
178 for (bp = NULL, ++xap; --num; ++xap) {
179 /*
180 * Exit the loop if there is no disk address assigned yet and
181 * the indirect block isn't in the cache, or if we were
182 * looking for an indirect block and we've found it.
183 */
184
185 metalbn = xap->in_lbn;
186 if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
187 break;
188 /*
189 * If we get here, we've either got the block in the cache
190 * or we have a disk address for it, go fetch it.
191 */
192 if (bp)
193 brelse(bp);
194
195 xap->in_exists = 1;
196 bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
197 if (bp->b_flags & (B_DONE | B_DELWRI)) {
198 trace(TR_BREADHIT, pack(vp, size), metalbn);
199 }
200 #ifdef DIAGNOSTIC
201 else if (!daddr)
202 panic("ext2fs_bmaparry: indirect block not in cache");
203 #endif
204 else {
205 trace(TR_BREADMISS, pack(vp, size), metalbn);
206 bp->b_blkno = blkptrtodb(ump, daddr);
207 bp->b_flags |= B_READ;
208 VOP_STRATEGY(bp);
209 curproc->l_proc->p_stats->p_ru.ru_inblock++; /* XXX */
210 if ((error = biowait(bp)) != 0) {
211 brelse(bp);
212 return (error);
213 }
214 }
215
216 daddr = fs2h32(((ufs_daddr_t *)bp->b_data)[xap->in_off]);
217 if (num == 1 && daddr && runp)
218 for (bn = xap->in_off + 1;
219 bn < MNINDIR(ump) && *runp < maxrun &&
220 is_sequential(ump, ((ufs_daddr_t *)bp->b_data)[bn - 1],
221 ((ufs_daddr_t *)bp->b_data)[bn]);
222 ++bn, ++*runp);
223 }
224 if (bp)
225 brelse(bp);
226
227 daddr = blkptrtodb(ump, daddr);
228 *bnp = daddr == 0 ? -1 : daddr;
229 return (0);
230 }
231