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