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