ffs_balloc.c revision 1.13.2.2 1 /* $NetBSD: ffs_balloc.c,v 1.13.2.2 1999/02/25 04:01:57 chs Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
36 */
37
38 #if defined(_KERNEL) && !defined(_LKM)
39 #include "opt_quota.h"
40 #include "opt_uvm.h"
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/buf.h>
46 #include <sys/proc.h>
47 #include <sys/file.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50
51 #include <vm/vm.h>
52
53 #if defined(UVM)
54 #include <uvm/uvm.h>
55 #endif
56
57 #include <ufs/ufs/quota.h>
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/inode.h>
60 #include <ufs/ufs/ufs_extern.h>
61 #include <ufs/ufs/ufs_bswap.h>
62
63 #include <ufs/ffs/fs.h>
64 #include <ufs/ffs/ffs_extern.h>
65
66 /*
67 * Balloc defines the structure of file system storage
68 * by allocating the physical blocks on a device given
69 * the inode and the logical block number in a file.
70 */
71 int
72 ffs_balloc(ip, lbn, size, cred, bpp, blknop, flags, alloced)
73 struct inode *ip;
74 ufs_daddr_t lbn;
75 int size;
76 struct ucred *cred;
77 struct buf **bpp;
78 daddr_t *blknop;
79 int flags;
80 boolean_t *alloced;
81 {
82 struct fs *fs;
83 ufs_daddr_t nb;
84 struct buf *bp, *nbp;
85 struct vnode *vp = ITOV(ip);
86 struct indir indirs[NIADDR + 2];
87 ufs_daddr_t newb, *bap, pref;
88 int deallocated, osize, nsize, num, i, error;
89 ufs_daddr_t *allocib, *blkp, *allocblk, allociblk[NIADDR + 1];
90
91 if (bpp != NULL) {
92 *bpp = NULL;
93 }
94 if (blknop != NULL) {
95 *blknop = (daddr_t)-1;
96 }
97 if (alloced != NULL) {
98 *alloced = FALSE;
99 }
100
101 if (lbn < 0)
102 return (EFBIG);
103 fs = ip->i_fs;
104
105 /*
106 * If the file currently ends with a fragment and
107 * the block we're allocating now is after the current EOF,
108 * this fragment has to be extended to be a full block.
109 */
110 nb = lblkno(fs, ip->i_ffs_size);
111 if (nb < NDADDR && nb < lbn) {
112 osize = blksize(fs, ip, nb);
113 if (osize < fs->fs_bsize && osize > 0) {
114 error = ffs_realloccg(ip, nb,
115 ffs_blkpref(ip, nb, (int)nb, &ip->i_ffs_db[0]),
116 osize, (int)fs->fs_bsize, cred, bpp, &newb);
117 if (error)
118 return (error);
119 ip->i_ffs_size = lblktosize(fs, nb + 1);
120 #if defined(UVM)
121 uvm_vnp_setsize(vp, ip->i_ffs_size);
122 #else
123 vnode_pager_setsize(vp, ip->i_ffs_size);
124 #endif
125 ip->i_ffs_db[nb] = ufs_rw32(newb,
126 UFS_MPNEEDSWAP(vp->v_mount));
127 ip->i_flag |= IN_CHANGE | IN_UPDATE;
128
129 if (bpp) {
130 if (flags & B_SYNC)
131 bwrite(*bpp);
132 else
133 bawrite(*bpp);
134 }
135 else {
136 /*
137 * XXX the data in the frag might be
138 * moving to a new disk location.
139 * we need to flush pages to the
140 * new disk locations.
141 * XXX we could do this in realloccg
142 * except for the sync flag.
143 */
144 (vp->v_uvm.u_obj.pgops->pgo_flush)
145 (&vp->v_uvm.u_obj, lblktosize(fs, nb),
146 lblktosize(fs, nb + 1),
147 flags & B_SYNC ? PGO_SYNCIO : 0);
148 }
149 }
150 }
151 /*
152 * The first NDADDR blocks are direct blocks
153 */
154 if (lbn < NDADDR) {
155
156 nb = ufs_rw32(ip->i_ffs_db[lbn], UFS_MPNEEDSWAP(vp->v_mount));
157 if (nb != 0 && ip->i_ffs_size >= lblktosize(fs, lbn + 1)) {
158
159 /*
160 * the block is an already-allocated direct block
161 * and the file already extends past this block,
162 * thus this must be a whole block.
163 * just read the block (if requested).
164 */
165
166 justread:
167 if (bpp != NULL) {
168 error = bread(vp, lbn, fs->fs_bsize, NOCRED,
169 &bp);
170 if (error) {
171 brelse(bp);
172 return (error);
173 }
174 *bpp = bp;
175 }
176 return (0);
177 }
178 if (nb != 0) {
179 /*
180 * Consider need to reallocate a fragment.
181 */
182 osize = fragroundup(fs, blkoff(fs, ip->i_ffs_size));
183 nsize = fragroundup(fs, size);
184 if (nsize <= osize) {
185
186 /*
187 * the existing block is already
188 * at least as big as we want.
189 * just read the block (if requested).
190 */
191
192 goto justread;
193 } else {
194
195 /*
196 * the existing block is smaller than we want,
197 * grow it.
198 */
199
200 error = ffs_realloccg(ip, lbn,
201 ffs_blkpref(ip, lbn, (int)lbn,
202 &ip->i_ffs_db[0]), osize, nsize, cred,
203 bpp, &newb);
204 if (error)
205 return (error);
206 ip->i_ffs_db[lbn] = ufs_rw32(newb,
207 UFS_MPNEEDSWAP(vp->v_mount));
208 ip->i_flag |= IN_CHANGE | IN_UPDATE;
209 }
210 } else {
211
212 /*
213 * the block was not previously allocated,
214 * allocate a new block or fragment.
215 */
216
217 if (ip->i_ffs_size < lblktosize(fs, lbn + 1))
218 nsize = fragroundup(fs, size);
219 else
220 nsize = fs->fs_bsize;
221 error = ffs_alloc(ip, lbn,
222 ffs_blkpref(ip, lbn, (int)lbn, &ip->i_ffs_db[0]),
223 nsize, cred, &newb);
224 if (error)
225 return (error);
226
227 ip->i_ffs_db[lbn] = ufs_rw32(newb,
228 UFS_MPNEEDSWAP(vp->v_mount));
229 ip->i_flag |= IN_CHANGE | IN_UPDATE;
230
231 if (bpp != NULL) {
232 bp = getblk(vp, lbn, nsize, 0, 0);
233 bp->b_blkno = fsbtodb(fs, newb);
234 if (flags & B_CLRBUF)
235 clrbuf(bp);
236 *bpp = bp;
237 }
238 if (blknop != NULL) {
239 *blknop = fsbtodb(fs, newb);
240 }
241 if (alloced != NULL) {
242 *alloced = TRUE;
243 }
244 }
245 return (0);
246 }
247 /*
248 * Determine the number of levels of indirection.
249 */
250 pref = 0;
251 if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
252 return(error);
253 #ifdef DIAGNOSTIC
254 if (num < 1)
255 panic ("ffs_balloc: ufs_bmaparray returned indirect block\n");
256 #endif
257 /*
258 * Fetch the first indirect block allocating if necessary.
259 */
260 --num;
261 nb = ufs_rw32(ip->i_ffs_ib[indirs[0].in_off],
262 UFS_MPNEEDSWAP(vp->v_mount));
263 allocib = NULL;
264 allocblk = allociblk;
265 if (nb == 0) {
266 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
267 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize,
268 cred, &newb);
269 if (error)
270 return (error);
271 nb = newb;
272 *allocblk++ = nb;
273 bp = getblk(vp, indirs[1].in_lbn, fs->fs_bsize, 0, 0);
274 bp->b_blkno = fsbtodb(fs, nb);
275 clrbuf(bp);
276 /*
277 * Write synchronously so that indirect blocks
278 * never point at garbage.
279 */
280 if ((error = bwrite(bp)) != 0)
281 goto fail;
282 allocib = &ip->i_ffs_ib[indirs[0].in_off];
283 *allocib = ufs_rw32(nb, UFS_MPNEEDSWAP(vp->v_mount));
284 ip->i_flag |= IN_CHANGE | IN_UPDATE;
285 }
286 /*
287 * Fetch through the indirect blocks, allocating as necessary.
288 */
289 for (i = 1;;) {
290 error = bread(vp,
291 indirs[i].in_lbn, (int)fs->fs_bsize, NOCRED, &bp);
292 if (error) {
293 brelse(bp);
294 goto fail;
295 }
296 bap = (ufs_daddr_t *)bp->b_data;
297 nb = ufs_rw32(bap[indirs[i].in_off],
298 UFS_MPNEEDSWAP(vp->v_mount));
299 if (i == num)
300 break;
301 i += 1;
302 if (nb != 0) {
303 brelse(bp);
304 continue;
305 }
306 if (pref == 0)
307 pref = ffs_blkpref(ip, lbn, 0, (ufs_daddr_t *)0);
308 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
309 &newb);
310 if (error) {
311 brelse(bp);
312 goto fail;
313 }
314 nb = newb;
315 *allocblk++ = nb;
316 nbp = getblk(vp, indirs[i].in_lbn, fs->fs_bsize, 0, 0);
317 nbp->b_blkno = fsbtodb(fs, nb);
318 clrbuf(nbp);
319 /*
320 * Write synchronously so that indirect blocks
321 * never point at garbage.
322 */
323 if ((error = bwrite(nbp)) != 0) {
324 brelse(bp);
325 goto fail;
326 }
327 bap[indirs[i - 1].in_off] = ufs_rw32(nb,
328 UFS_MPNEEDSWAP(vp->v_mount));
329 /*
330 * If required, write synchronously, otherwise use
331 * delayed write.
332 */
333 if (flags & B_SYNC) {
334 bwrite(bp);
335 } else {
336 bdwrite(bp);
337 }
338 }
339 /*
340 * Get the data block, allocating if necessary.
341 */
342 if (nb == 0) {
343 pref = ffs_blkpref(ip, lbn, indirs[i].in_off, &bap[0]);
344 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, cred,
345 &newb);
346 if (error) {
347 brelse(bp);
348 goto fail;
349 }
350 nb = newb;
351 *allocblk++ = nb;
352 bap[indirs[i].in_off] = ufs_rw32(nb,
353 UFS_MPNEEDSWAP(vp->v_mount));
354 /*
355 * If required, write synchronously, otherwise use
356 * delayed write.
357 */
358 if (flags & B_SYNC) {
359 bwrite(bp);
360 } else {
361 bdwrite(bp);
362 }
363 if (bpp != NULL) {
364 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
365 nbp->b_blkno = fsbtodb(fs, nb);
366 if (flags & B_CLRBUF)
367 clrbuf(nbp);
368 *bpp = nbp;
369 }
370 if (blknop != NULL) {
371 *blknop = fsbtodb(fs, nb);
372 }
373 if (alloced != NULL) {
374 *alloced = TRUE;
375 }
376 return (0);
377 }
378
379 brelse(bp);
380
381 if (bpp != NULL) {
382 if (flags & B_CLRBUF) {
383 error = bread(vp, lbn, (int)fs->fs_bsize, NOCRED, &nbp);
384 if (error) {
385 brelse(nbp);
386 goto fail;
387 }
388 } else {
389 nbp = getblk(vp, lbn, fs->fs_bsize, 0, 0);
390 nbp->b_blkno = fsbtodb(fs, nb);
391 clrbuf(nbp);
392 }
393 *bpp = nbp;
394 }
395 if (blknop != NULL) {
396 *blknop = fsbtodb(fs, nb);
397 }
398 return (0);
399 fail:
400 /*
401 * If we have failed part way through block allocation, we
402 * have to deallocate any indirect blocks that we have allocated.
403 */
404 for (deallocated = 0, blkp = allociblk; blkp < allocblk; blkp++) {
405 ffs_blkfree(ip, *blkp, fs->fs_bsize);
406 deallocated += fs->fs_bsize;
407 }
408 if (allocib != NULL)
409 *allocib = 0;
410 if (deallocated) {
411 #ifdef QUOTA
412 /*
413 * Restore user's disk quota because allocation failed.
414 */
415 (void)chkdq(ip, (long)-btodb(deallocated), cred, FORCE);
416 #endif
417 ip->i_ffs_blocks -= btodb(deallocated);
418 ip->i_flag |= IN_CHANGE | IN_UPDATE;
419 }
420 return (error);
421 }
422
423
424
425 int
426 ffs_balloc_range(ip, off, len, cred, flags)
427 struct inode *ip;
428 off_t off;
429 off_t len;
430 struct ucred *cred;
431 int flags;
432 {
433 struct fs *fs = ip->i_fs;
434 int lbn, bsize, delta, error;
435 daddr_t blkno;
436 boolean_t alloced;
437 off_t pagestart, pageend;
438
439 /*
440 * pagestart and pageend describe the range of pages that are
441 * completely covered by the range of blocks being allocated.
442 */
443
444 pagestart = round_page(off);
445 pageend = trunc_page(off + len);
446
447 while (len > 0) {
448 lbn = lblkno(fs, off);
449 bsize = min(fs->fs_bsize, blkoff(fs, off) + len);
450
451
452 if ((error = ffs_balloc(ip, lbn, bsize, cred, NULL, &blkno,
453 flags, &alloced))) {
454 return error;
455 }
456
457 /*
458 * if the block was freshly allocated then we can
459 * allocate the pages now and set their blknos.
460 */
461
462 if (alloced) {
463 uvm_vnp_setpageblknos(ITOV(ip), off, len, blkno,
464 UFP_ALL, (off < pagestart ||
465 off + len > pageend));
466 }
467
468 delta = fs->fs_bsize - blkoff(fs, off);
469 len -= delta;
470 off += delta;
471 }
472 return 0;
473 }
474