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