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