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