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