ext2fs_balloc.c revision 1.19 1 1.19 fvdl /* $NetBSD: ext2fs_balloc.c,v 1.19 2003/01/24 21:55:19 fvdl Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1997 Manuel Bouyer.
5 1.1 bouyer * Copyright (c) 1982, 1986, 1989, 1993
6 1.1 bouyer * The Regents of the University of California. All rights reserved.
7 1.1 bouyer *
8 1.1 bouyer * Redistribution and use in source and binary forms, with or without
9 1.1 bouyer * modification, are permitted provided that the following conditions
10 1.1 bouyer * are met:
11 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
12 1.1 bouyer * notice, this list of conditions and the following disclaimer.
13 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
15 1.1 bouyer * documentation and/or other materials provided with the distribution.
16 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
17 1.1 bouyer * must display the following acknowledgement:
18 1.1 bouyer * This product includes software developed by the University of
19 1.1 bouyer * California, Berkeley and its contributors.
20 1.1 bouyer * 4. 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 * @(#)ffs_balloc.c 8.4 (Berkeley) 9/23/93
37 1.1 bouyer * Modified for ext2fs by Manuel Bouyer.
38 1.1 bouyer */
39 1.13 lukem
40 1.13 lukem #include <sys/cdefs.h>
41 1.19 fvdl __KERNEL_RCSID(0, "$NetBSD: ext2fs_balloc.c,v 1.19 2003/01/24 21:55:19 fvdl Exp $");
42 1.1 bouyer
43 1.9 mrg #if defined(_KERNEL_OPT)
44 1.7 chs #include "opt_uvmhist.h"
45 1.7 chs #endif
46 1.7 chs
47 1.1 bouyer #include <sys/param.h>
48 1.1 bouyer #include <sys/systm.h>
49 1.1 bouyer #include <sys/buf.h>
50 1.1 bouyer #include <sys/proc.h>
51 1.1 bouyer #include <sys/file.h>
52 1.1 bouyer #include <sys/vnode.h>
53 1.7 chs #include <sys/mount.h>
54 1.7 chs
55 1.7 chs #include <uvm/uvm.h>
56 1.1 bouyer
57 1.1 bouyer #include <ufs/ufs/inode.h>
58 1.1 bouyer #include <ufs/ufs/ufs_extern.h>
59 1.1 bouyer
60 1.1 bouyer #include <ufs/ext2fs/ext2fs.h>
61 1.1 bouyer #include <ufs/ext2fs/ext2fs_extern.h>
62 1.1 bouyer
63 1.1 bouyer /*
64 1.1 bouyer * Balloc defines the structure of file system storage
65 1.1 bouyer * by allocating the physical blocks on a device given
66 1.1 bouyer * the inode and the logical block number in a file.
67 1.1 bouyer */
68 1.1 bouyer int
69 1.1 bouyer ext2fs_balloc(ip, bn, size, cred, bpp, flags)
70 1.4 augustss struct inode *ip;
71 1.19 fvdl daddr_t bn;
72 1.1 bouyer int size;
73 1.1 bouyer struct ucred *cred;
74 1.1 bouyer struct buf **bpp;
75 1.1 bouyer int flags;
76 1.1 bouyer {
77 1.4 augustss struct m_ext2fs *fs;
78 1.19 fvdl daddr_t nb;
79 1.1 bouyer struct buf *bp, *nbp;
80 1.1 bouyer struct vnode *vp = ITOV(ip);
81 1.1 bouyer struct indir indirs[NIADDR + 2];
82 1.19 fvdl daddr_t newb, lbn, pref;
83 1.19 fvdl int32_t *bap; /* XXX ondisk32 */
84 1.1 bouyer int num, i, error;
85 1.3 fvdl u_int deallocated;
86 1.19 fvdl daddr_t *blkp, *allocblk, allociblk[NIADDR + 1];
87 1.19 fvdl int32_t *allocib; /* XXX ondisk32 */
88 1.5 mycroft int unwindidx = -1;
89 1.7 chs UVMHIST_FUNC("ext2fs_balloc"); UVMHIST_CALLED(ubchist);
90 1.1 bouyer
91 1.7 chs UVMHIST_LOG(ubchist, "bn 0x%x", bn,0,0,0);
92 1.7 chs
93 1.7 chs if (bpp != NULL) {
94 1.7 chs *bpp = NULL;
95 1.7 chs }
96 1.1 bouyer if (bn < 0)
97 1.1 bouyer return (EFBIG);
98 1.1 bouyer fs = ip->i_e2fs;
99 1.1 bouyer lbn = bn;
100 1.1 bouyer
101 1.1 bouyer /*
102 1.1 bouyer * The first NDADDR blocks are direct blocks
103 1.1 bouyer */
104 1.1 bouyer if (bn < NDADDR) {
105 1.19 fvdl /* XXX ondisk32 */
106 1.2 bouyer nb = fs2h32(ip->i_e2fs_blocks[bn]);
107 1.1 bouyer if (nb != 0) {
108 1.7 chs
109 1.7 chs /*
110 1.7 chs * the block is already allocated, just read it.
111 1.7 chs */
112 1.7 chs
113 1.7 chs if (bpp != NULL) {
114 1.7 chs error = bread(vp, bn, fs->e2fs_bsize, NOCRED,
115 1.7 chs &bp);
116 1.7 chs if (error) {
117 1.7 chs brelse(bp);
118 1.7 chs return (error);
119 1.7 chs }
120 1.7 chs *bpp = bp;
121 1.1 bouyer }
122 1.1 bouyer return (0);
123 1.7 chs }
124 1.7 chs
125 1.7 chs /*
126 1.7 chs * allocate a new direct block.
127 1.7 chs */
128 1.7 chs
129 1.7 chs error = ext2fs_alloc(ip, bn,
130 1.7 chs ext2fs_blkpref(ip, bn, bn, &ip->i_e2fs_blocks[0]),
131 1.7 chs cred, &newb);
132 1.7 chs if (error)
133 1.7 chs return (error);
134 1.7 chs ip->i_e2fs_last_lblk = lbn;
135 1.7 chs ip->i_e2fs_last_blk = newb;
136 1.19 fvdl /* XXX ondisk32 */
137 1.19 fvdl ip->i_e2fs_blocks[bn] = h2fs32((int32_t)newb);
138 1.7 chs ip->i_flag |= IN_CHANGE | IN_UPDATE;
139 1.7 chs if (bpp != NULL) {
140 1.1 bouyer bp = getblk(vp, bn, fs->e2fs_bsize, 0, 0);
141 1.1 bouyer bp->b_blkno = fsbtodb(fs, newb);
142 1.1 bouyer if (flags & B_CLRBUF)
143 1.1 bouyer clrbuf(bp);
144 1.7 chs *bpp = bp;
145 1.1 bouyer }
146 1.1 bouyer return (0);
147 1.1 bouyer }
148 1.1 bouyer /*
149 1.1 bouyer * Determine the number of levels of indirection.
150 1.1 bouyer */
151 1.1 bouyer pref = 0;
152 1.1 bouyer if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
153 1.1 bouyer return(error);
154 1.1 bouyer #ifdef DIAGNOSTIC
155 1.1 bouyer if (num < 1)
156 1.1 bouyer panic ("ext2fs_balloc: ufs_getlbns returned indirect block\n");
157 1.1 bouyer #endif
158 1.1 bouyer /*
159 1.1 bouyer * Fetch the first indirect block allocating if necessary.
160 1.1 bouyer */
161 1.1 bouyer --num;
162 1.19 fvdl /* XXX ondisk32 */
163 1.2 bouyer nb = fs2h32(ip->i_e2fs_blocks[NDADDR + indirs[0].in_off]);
164 1.3 fvdl allocib = NULL;
165 1.3 fvdl allocblk = allociblk;
166 1.1 bouyer if (nb == 0) {
167 1.19 fvdl pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
168 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
169 1.1 bouyer if (error)
170 1.1 bouyer return (error);
171 1.1 bouyer nb = newb;
172 1.3 fvdl *allocblk++ = nb;
173 1.1 bouyer ip->i_e2fs_last_blk = newb;
174 1.1 bouyer bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0);
175 1.1 bouyer bp->b_blkno = fsbtodb(fs, newb);
176 1.1 bouyer clrbuf(bp);
177 1.1 bouyer /*
178 1.1 bouyer * Write synchronously so that indirect blocks
179 1.1 bouyer * never point at garbage.
180 1.1 bouyer */
181 1.3 fvdl if ((error = bwrite(bp)) != 0)
182 1.3 fvdl goto fail;
183 1.5 mycroft unwindidx = 0;
184 1.3 fvdl allocib = &ip->i_e2fs_blocks[NDADDR + indirs[0].in_off];
185 1.19 fvdl /* XXX ondisk32 */
186 1.19 fvdl *allocib = h2fs32((int32_t)newb);
187 1.1 bouyer ip->i_flag |= IN_CHANGE | IN_UPDATE;
188 1.1 bouyer }
189 1.1 bouyer /*
190 1.1 bouyer * Fetch through the indirect blocks, allocating as necessary.
191 1.1 bouyer */
192 1.1 bouyer for (i = 1;;) {
193 1.1 bouyer error = bread(vp,
194 1.5 mycroft indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
195 1.1 bouyer if (error) {
196 1.1 bouyer brelse(bp);
197 1.3 fvdl goto fail;
198 1.1 bouyer }
199 1.19 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
200 1.2 bouyer nb = fs2h32(bap[indirs[i].in_off]);
201 1.1 bouyer if (i == num)
202 1.1 bouyer break;
203 1.5 mycroft i++;
204 1.1 bouyer if (nb != 0) {
205 1.1 bouyer brelse(bp);
206 1.1 bouyer continue;
207 1.1 bouyer }
208 1.19 fvdl pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
209 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
210 1.1 bouyer if (error) {
211 1.1 bouyer brelse(bp);
212 1.3 fvdl goto fail;
213 1.1 bouyer }
214 1.1 bouyer nb = newb;
215 1.3 fvdl *allocblk++ = nb;
216 1.1 bouyer ip->i_e2fs_last_blk = newb;
217 1.1 bouyer nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0);
218 1.1 bouyer nbp->b_blkno = fsbtodb(fs, nb);
219 1.1 bouyer clrbuf(nbp);
220 1.1 bouyer /*
221 1.1 bouyer * Write synchronously so that indirect blocks
222 1.1 bouyer * never point at garbage.
223 1.1 bouyer */
224 1.1 bouyer if ((error = bwrite(nbp)) != 0) {
225 1.1 bouyer brelse(bp);
226 1.3 fvdl goto fail;
227 1.1 bouyer }
228 1.5 mycroft if (unwindidx < 0)
229 1.5 mycroft unwindidx = i - 1;
230 1.19 fvdl /* XXX ondisk32 */
231 1.19 fvdl bap[indirs[i - 1].in_off] = h2fs32((int32_t)nb);
232 1.1 bouyer /*
233 1.1 bouyer * If required, write synchronously, otherwise use
234 1.1 bouyer * delayed write.
235 1.1 bouyer */
236 1.1 bouyer if (flags & B_SYNC) {
237 1.1 bouyer bwrite(bp);
238 1.1 bouyer } else {
239 1.1 bouyer bdwrite(bp);
240 1.1 bouyer }
241 1.1 bouyer }
242 1.1 bouyer /*
243 1.1 bouyer * Get the data block, allocating if necessary.
244 1.1 bouyer */
245 1.1 bouyer if (nb == 0) {
246 1.5 mycroft pref = ext2fs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
247 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
248 1.1 bouyer if (error) {
249 1.1 bouyer brelse(bp);
250 1.3 fvdl goto fail;
251 1.1 bouyer }
252 1.1 bouyer nb = newb;
253 1.3 fvdl *allocblk++ = nb;
254 1.1 bouyer ip->i_e2fs_last_lblk = lbn;
255 1.1 bouyer ip->i_e2fs_last_blk = newb;
256 1.19 fvdl /* XXX ondisk32 */
257 1.19 fvdl bap[indirs[num].in_off] = h2fs32((int32_t)nb);
258 1.1 bouyer /*
259 1.1 bouyer * If required, write synchronously, otherwise use
260 1.1 bouyer * delayed write.
261 1.1 bouyer */
262 1.1 bouyer if (flags & B_SYNC) {
263 1.1 bouyer bwrite(bp);
264 1.1 bouyer } else {
265 1.1 bouyer bdwrite(bp);
266 1.1 bouyer }
267 1.7 chs if (bpp != NULL) {
268 1.7 chs nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
269 1.7 chs nbp->b_blkno = fsbtodb(fs, nb);
270 1.7 chs if (flags & B_CLRBUF)
271 1.7 chs clrbuf(nbp);
272 1.7 chs *bpp = nbp;
273 1.7 chs }
274 1.1 bouyer return (0);
275 1.1 bouyer }
276 1.1 bouyer brelse(bp);
277 1.7 chs if (bpp != NULL) {
278 1.7 chs if (flags & B_CLRBUF) {
279 1.7 chs error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED,
280 1.7 chs &nbp);
281 1.7 chs if (error) {
282 1.7 chs brelse(nbp);
283 1.7 chs goto fail;
284 1.7 chs }
285 1.7 chs } else {
286 1.7 chs nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
287 1.7 chs nbp->b_blkno = fsbtodb(fs, nb);
288 1.1 bouyer }
289 1.7 chs *bpp = nbp;
290 1.1 bouyer }
291 1.1 bouyer return (0);
292 1.3 fvdl fail:
293 1.3 fvdl /*
294 1.3 fvdl * If we have failed part way through block allocation, we
295 1.3 fvdl * have to deallocate any indirect blocks that we have allocated.
296 1.3 fvdl */
297 1.3 fvdl for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
298 1.3 fvdl ext2fs_blkfree(ip, *blkp);
299 1.3 fvdl deallocated += fs->e2fs_bsize;
300 1.3 fvdl }
301 1.5 mycroft if (unwindidx >= 0) {
302 1.5 mycroft if (unwindidx == 0) {
303 1.5 mycroft *allocib = 0;
304 1.5 mycroft } else {
305 1.5 mycroft int r;
306 1.5 mycroft
307 1.5 mycroft r = bread(vp, indirs[unwindidx].in_lbn,
308 1.5 mycroft (int)fs->e2fs_bsize, NOCRED, &bp);
309 1.5 mycroft if (r) {
310 1.5 mycroft panic("Could not unwind indirect block, error %d", r);
311 1.5 mycroft brelse(bp);
312 1.5 mycroft } else {
313 1.19 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
314 1.5 mycroft bap[indirs[unwindidx].in_off] = 0;
315 1.5 mycroft if (flags & B_SYNC)
316 1.5 mycroft bwrite(bp);
317 1.5 mycroft else
318 1.5 mycroft bdwrite(bp);
319 1.5 mycroft }
320 1.5 mycroft }
321 1.5 mycroft for (i = unwindidx + 1; i <= num; i++) {
322 1.5 mycroft bp = getblk(vp, indirs[i].in_lbn, (int)fs->e2fs_bsize,
323 1.5 mycroft 0, 0);
324 1.5 mycroft bp->b_flags |= B_INVAL;
325 1.5 mycroft brelse(bp);
326 1.5 mycroft }
327 1.5 mycroft }
328 1.3 fvdl if (deallocated) {
329 1.3 fvdl ip->i_e2fs_nblock -= btodb(deallocated);
330 1.3 fvdl ip->i_e2fs_flags |= IN_CHANGE | IN_UPDATE;
331 1.3 fvdl }
332 1.7 chs return error;
333 1.7 chs }
334 1.7 chs
335 1.7 chs int
336 1.11 chs ext2fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
337 1.11 chs struct ucred *cred)
338 1.7 chs {
339 1.7 chs struct inode *ip = VTOI(vp);
340 1.7 chs struct m_ext2fs *fs = ip->i_e2fs;
341 1.7 chs int error, delta, bshift, bsize;
342 1.11 chs UVMHIST_FUNC("ext2fs_gop_alloc"); UVMHIST_CALLED(ubchist);
343 1.7 chs
344 1.7 chs bshift = fs->e2fs_bshift;
345 1.7 chs bsize = 1 << bshift;
346 1.7 chs
347 1.7 chs delta = off & (bsize - 1);
348 1.7 chs off -= delta;
349 1.7 chs len += delta;
350 1.7 chs
351 1.7 chs while (len > 0) {
352 1.7 chs bsize = min(bsize, len);
353 1.7 chs UVMHIST_LOG(ubchist, "off 0x%x len 0x%x bsize 0x%x",
354 1.7 chs off, len, bsize, 0);
355 1.7 chs
356 1.11 chs error = ext2fs_balloc(ip, lblkno(fs, off), bsize, cred,
357 1.11 chs NULL, flags);
358 1.7 chs if (error) {
359 1.7 chs UVMHIST_LOG(ubchist, "error %d", error, 0,0,0);
360 1.7 chs return error;
361 1.7 chs }
362 1.7 chs
363 1.7 chs /*
364 1.7 chs * increase file size now, VOP_BALLOC() requires that
365 1.7 chs * EOF be up-to-date before each call.
366 1.7 chs */
367 1.7 chs
368 1.7 chs if (ip->i_e2fs_size < off + bsize) {
369 1.7 chs UVMHIST_LOG(ubchist, "old 0x%x new 0x%x",
370 1.7 chs ip->i_e2fs_size, off + bsize,0,0);
371 1.7 chs ip->i_e2fs_size = off + bsize;
372 1.7 chs }
373 1.7 chs
374 1.7 chs off += bsize;
375 1.7 chs len -= bsize;
376 1.7 chs }
377 1.7 chs return 0;
378 1.1 bouyer }
379