ext2fs_balloc.c revision 1.33 1 1.33 hannken /* $NetBSD: ext2fs_balloc.c,v 1.33 2008/05/16 09:22:00 hannken 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.33 hannken __KERNEL_RCSID(0, "$NetBSD: ext2fs_balloc.c,v 1.33 2008/05/16 09:22:00 hannken 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.29 elad #include <sys/kauth.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.31 christos ext2fs_balloc(struct inode *ip, daddr_t bn, int size,
98 1.30 christos kauth_cred_t cred, struct buf **bpp, int flags)
99 1.1 bouyer {
100 1.4 augustss struct m_ext2fs *fs;
101 1.19 fvdl daddr_t nb;
102 1.1 bouyer struct buf *bp, *nbp;
103 1.1 bouyer struct vnode *vp = ITOV(ip);
104 1.1 bouyer struct indir indirs[NIADDR + 2];
105 1.19 fvdl daddr_t newb, lbn, pref;
106 1.19 fvdl int32_t *bap; /* XXX ondisk32 */
107 1.1 bouyer int num, i, error;
108 1.3 fvdl u_int deallocated;
109 1.19 fvdl daddr_t *blkp, *allocblk, allociblk[NIADDR + 1];
110 1.19 fvdl int32_t *allocib; /* XXX ondisk32 */
111 1.5 mycroft int unwindidx = -1;
112 1.7 chs UVMHIST_FUNC("ext2fs_balloc"); UVMHIST_CALLED(ubchist);
113 1.1 bouyer
114 1.7 chs UVMHIST_LOG(ubchist, "bn 0x%x", bn,0,0,0);
115 1.7 chs
116 1.7 chs if (bpp != NULL) {
117 1.7 chs *bpp = NULL;
118 1.7 chs }
119 1.1 bouyer if (bn < 0)
120 1.1 bouyer return (EFBIG);
121 1.1 bouyer fs = ip->i_e2fs;
122 1.1 bouyer lbn = bn;
123 1.1 bouyer
124 1.1 bouyer /*
125 1.1 bouyer * The first NDADDR blocks are direct blocks
126 1.1 bouyer */
127 1.1 bouyer if (bn < NDADDR) {
128 1.19 fvdl /* XXX ondisk32 */
129 1.2 bouyer nb = fs2h32(ip->i_e2fs_blocks[bn]);
130 1.1 bouyer if (nb != 0) {
131 1.7 chs
132 1.7 chs /*
133 1.7 chs * the block is already allocated, just read it.
134 1.7 chs */
135 1.7 chs
136 1.7 chs if (bpp != NULL) {
137 1.7 chs error = bread(vp, bn, fs->e2fs_bsize, NOCRED,
138 1.33 hannken B_MODIFY, &bp);
139 1.7 chs if (error) {
140 1.32 ad brelse(bp, 0);
141 1.7 chs return (error);
142 1.7 chs }
143 1.7 chs *bpp = bp;
144 1.1 bouyer }
145 1.1 bouyer return (0);
146 1.7 chs }
147 1.7 chs
148 1.7 chs /*
149 1.7 chs * allocate a new direct block.
150 1.7 chs */
151 1.7 chs
152 1.7 chs error = ext2fs_alloc(ip, bn,
153 1.7 chs ext2fs_blkpref(ip, bn, bn, &ip->i_e2fs_blocks[0]),
154 1.7 chs cred, &newb);
155 1.7 chs if (error)
156 1.7 chs return (error);
157 1.7 chs ip->i_e2fs_last_lblk = lbn;
158 1.7 chs ip->i_e2fs_last_blk = newb;
159 1.19 fvdl /* XXX ondisk32 */
160 1.19 fvdl ip->i_e2fs_blocks[bn] = h2fs32((int32_t)newb);
161 1.7 chs ip->i_flag |= IN_CHANGE | IN_UPDATE;
162 1.7 chs if (bpp != NULL) {
163 1.1 bouyer bp = getblk(vp, bn, fs->e2fs_bsize, 0, 0);
164 1.1 bouyer bp->b_blkno = fsbtodb(fs, newb);
165 1.1 bouyer if (flags & B_CLRBUF)
166 1.1 bouyer clrbuf(bp);
167 1.7 chs *bpp = bp;
168 1.1 bouyer }
169 1.1 bouyer return (0);
170 1.1 bouyer }
171 1.1 bouyer /*
172 1.1 bouyer * Determine the number of levels of indirection.
173 1.1 bouyer */
174 1.1 bouyer pref = 0;
175 1.1 bouyer if ((error = ufs_getlbns(vp, bn, indirs, &num)) != 0)
176 1.1 bouyer return(error);
177 1.1 bouyer #ifdef DIAGNOSTIC
178 1.1 bouyer if (num < 1)
179 1.1 bouyer panic ("ext2fs_balloc: ufs_getlbns returned indirect block\n");
180 1.1 bouyer #endif
181 1.1 bouyer /*
182 1.1 bouyer * Fetch the first indirect block allocating if necessary.
183 1.1 bouyer */
184 1.1 bouyer --num;
185 1.19 fvdl /* XXX ondisk32 */
186 1.2 bouyer nb = fs2h32(ip->i_e2fs_blocks[NDADDR + indirs[0].in_off]);
187 1.3 fvdl allocib = NULL;
188 1.3 fvdl allocblk = allociblk;
189 1.1 bouyer if (nb == 0) {
190 1.19 fvdl pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
191 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
192 1.1 bouyer if (error)
193 1.1 bouyer return (error);
194 1.1 bouyer nb = newb;
195 1.3 fvdl *allocblk++ = nb;
196 1.1 bouyer ip->i_e2fs_last_blk = newb;
197 1.1 bouyer bp = getblk(vp, indirs[1].in_lbn, fs->e2fs_bsize, 0, 0);
198 1.1 bouyer bp->b_blkno = fsbtodb(fs, newb);
199 1.1 bouyer clrbuf(bp);
200 1.1 bouyer /*
201 1.1 bouyer * Write synchronously so that indirect blocks
202 1.1 bouyer * never point at garbage.
203 1.1 bouyer */
204 1.3 fvdl if ((error = bwrite(bp)) != 0)
205 1.3 fvdl goto fail;
206 1.5 mycroft unwindidx = 0;
207 1.3 fvdl allocib = &ip->i_e2fs_blocks[NDADDR + indirs[0].in_off];
208 1.19 fvdl /* XXX ondisk32 */
209 1.19 fvdl *allocib = h2fs32((int32_t)newb);
210 1.1 bouyer ip->i_flag |= IN_CHANGE | IN_UPDATE;
211 1.1 bouyer }
212 1.1 bouyer /*
213 1.1 bouyer * Fetch through the indirect blocks, allocating as necessary.
214 1.1 bouyer */
215 1.1 bouyer for (i = 1;;) {
216 1.1 bouyer error = bread(vp,
217 1.33 hannken indirs[i].in_lbn, (int)fs->e2fs_bsize, NOCRED, 0, &bp);
218 1.1 bouyer if (error) {
219 1.32 ad brelse(bp, 0);
220 1.3 fvdl goto fail;
221 1.1 bouyer }
222 1.19 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
223 1.2 bouyer nb = fs2h32(bap[indirs[i].in_off]);
224 1.1 bouyer if (i == num)
225 1.1 bouyer break;
226 1.5 mycroft i++;
227 1.1 bouyer if (nb != 0) {
228 1.32 ad brelse(bp, 0);
229 1.1 bouyer continue;
230 1.1 bouyer }
231 1.19 fvdl pref = ext2fs_blkpref(ip, lbn, 0, (int32_t *)0);
232 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
233 1.1 bouyer if (error) {
234 1.32 ad brelse(bp, 0);
235 1.3 fvdl goto fail;
236 1.1 bouyer }
237 1.1 bouyer nb = newb;
238 1.3 fvdl *allocblk++ = nb;
239 1.1 bouyer ip->i_e2fs_last_blk = newb;
240 1.1 bouyer nbp = getblk(vp, indirs[i].in_lbn, fs->e2fs_bsize, 0, 0);
241 1.1 bouyer nbp->b_blkno = fsbtodb(fs, nb);
242 1.1 bouyer clrbuf(nbp);
243 1.1 bouyer /*
244 1.1 bouyer * Write synchronously so that indirect blocks
245 1.1 bouyer * never point at garbage.
246 1.1 bouyer */
247 1.1 bouyer if ((error = bwrite(nbp)) != 0) {
248 1.32 ad brelse(bp, 0);
249 1.3 fvdl goto fail;
250 1.1 bouyer }
251 1.5 mycroft if (unwindidx < 0)
252 1.5 mycroft unwindidx = i - 1;
253 1.19 fvdl /* XXX ondisk32 */
254 1.19 fvdl bap[indirs[i - 1].in_off] = h2fs32((int32_t)nb);
255 1.1 bouyer /*
256 1.1 bouyer * If required, write synchronously, otherwise use
257 1.1 bouyer * delayed write.
258 1.1 bouyer */
259 1.1 bouyer if (flags & B_SYNC) {
260 1.1 bouyer bwrite(bp);
261 1.1 bouyer } else {
262 1.1 bouyer bdwrite(bp);
263 1.1 bouyer }
264 1.1 bouyer }
265 1.1 bouyer /*
266 1.1 bouyer * Get the data block, allocating if necessary.
267 1.1 bouyer */
268 1.1 bouyer if (nb == 0) {
269 1.5 mycroft pref = ext2fs_blkpref(ip, lbn, indirs[num].in_off, &bap[0]);
270 1.5 mycroft error = ext2fs_alloc(ip, lbn, pref, cred, &newb);
271 1.1 bouyer if (error) {
272 1.32 ad brelse(bp, 0);
273 1.3 fvdl goto fail;
274 1.1 bouyer }
275 1.1 bouyer nb = newb;
276 1.3 fvdl *allocblk++ = nb;
277 1.1 bouyer ip->i_e2fs_last_lblk = lbn;
278 1.1 bouyer ip->i_e2fs_last_blk = newb;
279 1.19 fvdl /* XXX ondisk32 */
280 1.19 fvdl bap[indirs[num].in_off] = h2fs32((int32_t)nb);
281 1.1 bouyer /*
282 1.1 bouyer * If required, write synchronously, otherwise use
283 1.1 bouyer * delayed write.
284 1.1 bouyer */
285 1.1 bouyer if (flags & B_SYNC) {
286 1.1 bouyer bwrite(bp);
287 1.1 bouyer } else {
288 1.1 bouyer bdwrite(bp);
289 1.1 bouyer }
290 1.7 chs if (bpp != NULL) {
291 1.7 chs nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
292 1.7 chs nbp->b_blkno = fsbtodb(fs, nb);
293 1.7 chs if (flags & B_CLRBUF)
294 1.7 chs clrbuf(nbp);
295 1.7 chs *bpp = nbp;
296 1.7 chs }
297 1.1 bouyer return (0);
298 1.1 bouyer }
299 1.32 ad brelse(bp, 0);
300 1.7 chs if (bpp != NULL) {
301 1.7 chs if (flags & B_CLRBUF) {
302 1.7 chs error = bread(vp, lbn, (int)fs->e2fs_bsize, NOCRED,
303 1.33 hannken B_MODIFY, &nbp);
304 1.7 chs if (error) {
305 1.32 ad brelse(nbp, 0);
306 1.7 chs goto fail;
307 1.7 chs }
308 1.7 chs } else {
309 1.7 chs nbp = getblk(vp, lbn, fs->e2fs_bsize, 0, 0);
310 1.7 chs nbp->b_blkno = fsbtodb(fs, nb);
311 1.1 bouyer }
312 1.7 chs *bpp = nbp;
313 1.1 bouyer }
314 1.1 bouyer return (0);
315 1.3 fvdl fail:
316 1.3 fvdl /*
317 1.3 fvdl * If we have failed part way through block allocation, we
318 1.3 fvdl * have to deallocate any indirect blocks that we have allocated.
319 1.3 fvdl */
320 1.3 fvdl for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
321 1.3 fvdl ext2fs_blkfree(ip, *blkp);
322 1.3 fvdl deallocated += fs->e2fs_bsize;
323 1.3 fvdl }
324 1.5 mycroft if (unwindidx >= 0) {
325 1.5 mycroft if (unwindidx == 0) {
326 1.5 mycroft *allocib = 0;
327 1.5 mycroft } else {
328 1.5 mycroft int r;
329 1.25 perry
330 1.25 perry r = bread(vp, indirs[unwindidx].in_lbn,
331 1.33 hannken (int)fs->e2fs_bsize, NOCRED, B_MODIFY, &bp);
332 1.5 mycroft if (r) {
333 1.5 mycroft panic("Could not unwind indirect block, error %d", r);
334 1.32 ad brelse(bp, 0);
335 1.5 mycroft } else {
336 1.19 fvdl bap = (int32_t *)bp->b_data; /* XXX ondisk32 */
337 1.5 mycroft bap[indirs[unwindidx].in_off] = 0;
338 1.5 mycroft if (flags & B_SYNC)
339 1.5 mycroft bwrite(bp);
340 1.5 mycroft else
341 1.5 mycroft bdwrite(bp);
342 1.5 mycroft }
343 1.5 mycroft }
344 1.5 mycroft for (i = unwindidx + 1; i <= num; i++) {
345 1.5 mycroft bp = getblk(vp, indirs[i].in_lbn, (int)fs->e2fs_bsize,
346 1.5 mycroft 0, 0);
347 1.32 ad brelse(bp, BC_INVAL);
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.29 elad kauth_cred_t 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