ext2fs_balloc.c revision 1.27 1 1.27 yamt /* $NetBSD: ext2fs_balloc.c,v 1.27 2005/11/02 12:39:00 yamt 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.27 yamt __KERNEL_RCSID(0, "$NetBSD: ext2fs_balloc.c,v 1.27 2005/11/02 12:39:00 yamt 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.26 xtraeme ext2fs_balloc(struct inode *ip, daddr_t bn, int size, struct ucred *cred,
97 1.26 xtraeme struct buf **bpp, int flags)
98 1.1 bouyer {
99 1.4 augustss struct m_ext2fs *fs;
100 1.19 fvdl daddr_t nb;
101 1.1 bouyer struct buf *bp, *nbp;
102 1.1 bouyer struct vnode *vp = ITOV(ip);
103 1.1 bouyer struct indir indirs[NIADDR + 2];
104 1.19 fvdl daddr_t newb, lbn, pref;
105 1.19 fvdl int32_t *bap; /* XXX ondisk32 */
106 1.1 bouyer int num, i, error;
107 1.3 fvdl u_int deallocated;
108 1.19 fvdl daddr_t *blkp, *allocblk, allociblk[NIADDR + 1];
109 1.19 fvdl int32_t *allocib; /* XXX ondisk32 */
110 1.5 mycroft int unwindidx = -1;
111 1.7 chs UVMHIST_FUNC("ext2fs_balloc"); UVMHIST_CALLED(ubchist);
112 1.1 bouyer
113 1.7 chs UVMHIST_LOG(ubchist, "bn 0x%x", bn,0,0,0);
114 1.7 chs
115 1.7 chs if (bpp != NULL) {
116 1.7 chs *bpp = NULL;
117 1.7 chs }
118 1.1 bouyer if (bn < 0)
119 1.1 bouyer return (EFBIG);
120 1.1 bouyer fs = ip->i_e2fs;
121 1.1 bouyer lbn = bn;
122 1.1 bouyer
123 1.1 bouyer /*
124 1.1 bouyer * The first NDADDR blocks are direct blocks
125 1.1 bouyer */
126 1.1 bouyer if (bn < NDADDR) {
127 1.19 fvdl /* XXX ondisk32 */
128 1.2 bouyer nb = fs2h32(ip->i_e2fs_blocks[bn]);
129 1.1 bouyer if (nb != 0) {
130 1.7 chs
131 1.7 chs /*
132 1.7 chs * the block is already allocated, just read it.
133 1.7 chs */
134 1.7 chs
135 1.7 chs if (bpp != NULL) {
136 1.7 chs error = bread(vp, bn, fs->e2fs_bsize, NOCRED,
137 1.7 chs &bp);
138 1.7 chs if (error) {
139 1.7 chs brelse(bp);
140 1.7 chs return (error);
141 1.7 chs }
142 1.7 chs *bpp = bp;
143 1.1 bouyer }
144 1.1 bouyer return (0);
145 1.7 chs }
146 1.7 chs
147 1.7 chs /*
148 1.7 chs * allocate a new direct block.
149 1.7 chs */
150 1.7 chs
151 1.7 chs error = ext2fs_alloc(ip, bn,
152 1.7 chs ext2fs_blkpref(ip, bn, bn, &ip->i_e2fs_blocks[0]),
153 1.7 chs cred, &newb);
154 1.7 chs if (error)
155 1.7 chs return (error);
156 1.7 chs ip->i_e2fs_last_lblk = lbn;
157 1.7 chs ip->i_e2fs_last_blk = newb;
158 1.19 fvdl /* XXX ondisk32 */
159 1.19 fvdl ip->i_e2fs_blocks[bn] = h2fs32((int32_t)newb);
160 1.7 chs ip->i_flag |= IN_CHANGE | IN_UPDATE;
161 1.7 chs if (bpp != NULL) {
162 1.1 bouyer bp = getblk(vp, bn, fs->e2fs_bsize, 0, 0);
163 1.1 bouyer bp->b_blkno = fsbtodb(fs, newb);
164 1.1 bouyer if (flags & B_CLRBUF)
165 1.1 bouyer clrbuf(bp);
166 1.7 chs *bpp = bp;
167 1.1 bouyer }
168 1.1 bouyer return (0);
169 1.1 bouyer }
170 1.1 bouyer /*
171 1.1 bouyer * Determine the number of levels of indirection.
172 1.1 bouyer */
173 1.1 bouyer pref = 0;
174 1.1 bouyer if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
175 1.1 bouyer return(error);
176 1.1 bouyer #ifdef DIAGNOSTIC
177 1.1 bouyer if (num < 1)
178 1.1 bouyer panic ("ext2fs_balloc: ufs_getlbns returned indirect block\n");
179 1.1 bouyer #endif
180 1.1 bouyer /*
181 1.1 bouyer * Fetch the first indirect block allocating if necessary.
182 1.1 bouyer */
183 1.1 bouyer --num;
184 1.19 fvdl /* XXX ondisk32 */
185 1.2 bouyer nb = fs2h32(ip->i_e2fs_blocks[NDADDR + indirs[0].in_off]);
186 1.3 fvdl allocib = NULL;
187 1.3 fvdl allocblk = allociblk;
188 1.1 bouyer if (nb == 0) {
189 1.19 fvdl pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
190 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
191 1.1 bouyer if (error)
192 1.1 bouyer return (error);
193 1.1 bouyer nb = newb;
194 1.3 fvdl *allocblk++ = nb;
195 1.1 bouyer ip->i_e2fs_last_blk = newb;
196 1.1 bouyer bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0);
197 1.1 bouyer bp->b_blkno = fsbtodb(fs, newb);
198 1.1 bouyer clrbuf(bp);
199 1.1 bouyer /*
200 1.1 bouyer * Write synchronously so that indirect blocks
201 1.1 bouyer * never point at garbage.
202 1.1 bouyer */
203 1.3 fvdl if ((error = bwrite(bp)) != 0)
204 1.3 fvdl goto fail;
205 1.5 mycroft unwindidx = 0;
206 1.3 fvdl allocib = &ip->i_e2fs_blocks[NDADDR + indirs[0].in_off];
207 1.19 fvdl /* XXX ondisk32 */
208 1.19 fvdl *allocib = h2fs32((int32_t)newb);
209 1.1 bouyer ip->i_flag |= IN_CHANGE | IN_UPDATE;
210 1.1 bouyer }
211 1.1 bouyer /*
212 1.1 bouyer * Fetch through the indirect blocks, allocating as necessary.
213 1.1 bouyer */
214 1.1 bouyer for (i = 1;;) {
215 1.1 bouyer error = bread(vp,
216 1.5 mycroft indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, &bp);
217 1.1 bouyer if (error) {
218 1.1 bouyer brelse(bp);
219 1.3 fvdl goto fail;
220 1.1 bouyer }
221 1.19 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
222 1.2 bouyer nb = fs2h32(bap[indirs[i].in_off]);
223 1.1 bouyer if (i == num)
224 1.1 bouyer break;
225 1.5 mycroft i++;
226 1.1 bouyer if (nb != 0) {
227 1.1 bouyer brelse(bp);
228 1.1 bouyer continue;
229 1.1 bouyer }
230 1.19 fvdl pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
231 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
232 1.1 bouyer if (error) {
233 1.1 bouyer brelse(bp);
234 1.3 fvdl goto fail;
235 1.1 bouyer }
236 1.1 bouyer nb = newb;
237 1.3 fvdl *allocblk++ = nb;
238 1.1 bouyer ip->i_e2fs_last_blk = newb;
239 1.1 bouyer nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0);
240 1.1 bouyer nbp->b_blkno = fsbtodb(fs, nb);
241 1.1 bouyer clrbuf(nbp);
242 1.1 bouyer /*
243 1.1 bouyer * Write synchronously so that indirect blocks
244 1.1 bouyer * never point at garbage.
245 1.1 bouyer */
246 1.1 bouyer if ((error = bwrite(nbp)) != 0) {
247 1.1 bouyer brelse(bp);
248 1.3 fvdl goto fail;
249 1.1 bouyer }
250 1.5 mycroft if (unwindidx < 0)
251 1.5 mycroft unwindidx = i - 1;
252 1.19 fvdl /* XXX ondisk32 */
253 1.19 fvdl bap[indirs[i - 1].in_off] = h2fs32((int32_t)nb);
254 1.1 bouyer /*
255 1.1 bouyer * If required, write synchronously, otherwise use
256 1.1 bouyer * delayed write.
257 1.1 bouyer */
258 1.1 bouyer if (flags & B_SYNC) {
259 1.1 bouyer bwrite(bp);
260 1.1 bouyer } else {
261 1.1 bouyer bdwrite(bp);
262 1.1 bouyer }
263 1.1 bouyer }
264 1.1 bouyer /*
265 1.1 bouyer * Get the data block, allocating if necessary.
266 1.1 bouyer */
267 1.1 bouyer if (nb == 0) {
268 1.5 mycroft pref = ext2fs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
269 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
270 1.1 bouyer if (error) {
271 1.1 bouyer brelse(bp);
272 1.3 fvdl goto fail;
273 1.1 bouyer }
274 1.1 bouyer nb = newb;
275 1.3 fvdl *allocblk++ = nb;
276 1.1 bouyer ip->i_e2fs_last_lblk = lbn;
277 1.1 bouyer ip->i_e2fs_last_blk = newb;
278 1.19 fvdl /* XXX ondisk32 */
279 1.19 fvdl bap[indirs[num].in_off] = h2fs32((int32_t)nb);
280 1.1 bouyer /*
281 1.1 bouyer * If required, write synchronously, otherwise use
282 1.1 bouyer * delayed write.
283 1.1 bouyer */
284 1.1 bouyer if (flags & B_SYNC) {
285 1.1 bouyer bwrite(bp);
286 1.1 bouyer } else {
287 1.1 bouyer bdwrite(bp);
288 1.1 bouyer }
289 1.7 chs if (bpp != NULL) {
290 1.7 chs nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
291 1.7 chs nbp->b_blkno = fsbtodb(fs, nb);
292 1.7 chs if (flags & B_CLRBUF)
293 1.7 chs clrbuf(nbp);
294 1.7 chs *bpp = nbp;
295 1.7 chs }
296 1.1 bouyer return (0);
297 1.1 bouyer }
298 1.1 bouyer brelse(bp);
299 1.7 chs if (bpp != NULL) {
300 1.7 chs if (flags & B_CLRBUF) {
301 1.7 chs error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED,
302 1.7 chs &nbp);
303 1.7 chs if (error) {
304 1.7 chs brelse(nbp);
305 1.7 chs goto fail;
306 1.7 chs }
307 1.7 chs } else {
308 1.7 chs nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
309 1.7 chs nbp->b_blkno = fsbtodb(fs, nb);
310 1.1 bouyer }
311 1.7 chs *bpp = nbp;
312 1.1 bouyer }
313 1.1 bouyer return (0);
314 1.3 fvdl fail:
315 1.3 fvdl /*
316 1.3 fvdl * If we have failed part way through block allocation, we
317 1.3 fvdl * have to deallocate any indirect blocks that we have allocated.
318 1.3 fvdl */
319 1.3 fvdl for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
320 1.3 fvdl ext2fs_blkfree(ip, *blkp);
321 1.3 fvdl deallocated += fs->e2fs_bsize;
322 1.3 fvdl }
323 1.5 mycroft if (unwindidx >= 0) {
324 1.5 mycroft if (unwindidx == 0) {
325 1.5 mycroft *allocib = 0;
326 1.5 mycroft } else {
327 1.5 mycroft int r;
328 1.25 perry
329 1.25 perry r = bread(vp, indirs[unwindidx].in_lbn,
330 1.5 mycroft (int)fs->e2fs_bsize, NOCRED, &bp);
331 1.5 mycroft if (r) {
332 1.5 mycroft panic("Could not unwind indirect block, error %d", r);
333 1.5 mycroft brelse(bp);
334 1.5 mycroft } else {
335 1.19 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
336 1.5 mycroft bap[indirs[unwindidx].in_off] = 0;
337 1.5 mycroft if (flags & B_SYNC)
338 1.5 mycroft bwrite(bp);
339 1.5 mycroft else
340 1.5 mycroft bdwrite(bp);
341 1.5 mycroft }
342 1.5 mycroft }
343 1.5 mycroft for (i = unwindidx + 1; i <= num; i++) {
344 1.5 mycroft bp = getblk(vp, indirs[i].in_lbn, (int)fs->e2fs_bsize,
345 1.5 mycroft 0, 0);
346 1.5 mycroft bp->b_flags |= B_INVAL;
347 1.5 mycroft brelse(bp);
348 1.5 mycroft }
349 1.5 mycroft }
350 1.3 fvdl if (deallocated) {
351 1.3 fvdl ip->i_e2fs_nblock -= btodb(deallocated);
352 1.3 fvdl ip->i_e2fs_flags |= IN_CHANGE | IN_UPDATE;
353 1.3 fvdl }
354 1.7 chs return error;
355 1.7 chs }
356 1.7 chs
357 1.7 chs int
358 1.11 chs ext2fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
359 1.11 chs struct ucred *cred)
360 1.7 chs {
361 1.7 chs struct inode *ip = VTOI(vp);
362 1.7 chs struct m_ext2fs *fs = ip->i_e2fs;
363 1.7 chs int error, delta, bshift, bsize;
364 1.11 chs UVMHIST_FUNC("ext2fs_gop_alloc"); UVMHIST_CALLED(ubchist);
365 1.7 chs
366 1.7 chs bshift = fs->e2fs_bshift;
367 1.7 chs bsize = 1 << bshift;
368 1.7 chs
369 1.7 chs delta = off & (bsize - 1);
370 1.7 chs off -= delta;
371 1.7 chs len += delta;
372 1.7 chs
373 1.7 chs while (len > 0) {
374 1.7 chs bsize = min(bsize, len);
375 1.7 chs UVMHIST_LOG(ubchist, "off 0x%x len 0x%x bsize 0x%x",
376 1.7 chs off, len, bsize, 0);
377 1.7 chs
378 1.11 chs error = ext2fs_balloc(ip, lblkno(fs, off), bsize, cred,
379 1.11 chs NULL, flags);
380 1.7 chs if (error) {
381 1.7 chs UVMHIST_LOG(ubchist, "error %d", error, 0,0,0);
382 1.7 chs return error;
383 1.7 chs }
384 1.7 chs
385 1.7 chs /*
386 1.27 yamt * increase file size now, ext2fs_balloc() requires that
387 1.7 chs * EOF be up-to-date before each call.
388 1.7 chs */
389 1.7 chs
390 1.23 ws if (ext2fs_size(ip) < off + bsize) {
391 1.23 ws UVMHIST_LOG(ubchist, "old 0x%lx%8lx new 0x%lx%8lx",
392 1.23 ws /* Note that arguments are always cast to u_long. */
393 1.23 ws ext2fs_size(ip) >> 32,
394 1.23 ws ext2fs_size(ip) & 0xffffffff,
395 1.23 ws (off + bsize) >> 32,
396 1.23 ws (off + bsize) & 0xffffffff);
397 1.23 ws error = ext2fs_setsize(ip, off + bsize);
398 1.23 ws if (error) {
399 1.24 chs UVMHIST_LOG(ubchist, "error %d", error, 0,0,0);
400 1.23 ws return error;
401 1.23 ws }
402 1.7 chs }
403 1.7 chs
404 1.7 chs off += bsize;
405 1.7 chs len -= bsize;
406 1.7 chs }
407 1.7 chs return 0;
408 1.1 bouyer }
409