1 /* $NetBSD: ffs_balloc.c,v 1.13 2004/06/20 22:20:18 jmc Exp $ */
2 /* From NetBSD: ffs_balloc.c,v 1.25 2001/08/08 08:36:36 lukem Exp */
5 * Copyright (c) 1982, 1986, 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ffs/fs.h>
52 #include "ffs/ufs_bswap.h"
54 #include "ffs/ufs_inode.h"
55 #include "ffs/ffs_extern.h"
57 static int ffs_balloc_ufs1(struct inode *, off_t, int, struct buf **);
58 static int ffs_balloc_ufs2(struct inode *, off_t, int, struct buf **);
61 * Balloc defines the structure of file system storage
62 * by allocating the physical blocks on a device given
63 * the inode and the logical block number in a file.
65 * Assume: flags == B_SYNC | B_CLRBUF
69 ffs_balloc(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
71 if (ip->i_fs->fs_magic == FS_UFS2_MAGIC)
72 return ffs_balloc_ufs2(ip, offset, bufsize, bpp);
74 return ffs_balloc_ufs1(ip, offset, bufsize, bpp);
78 ffs_balloc_ufs1(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
84 struct fs *fs = ip->i_fs;
85 struct indir indirs[NIADDR + 2];
88 int osize, nsize, num, i, error;
89 int32_t *allocblk, allociblk[NIADDR + 1];
91 const int needswap = UFS_FSNEEDSWAP(fs);
93 lbn = lblkno(fs, offset);
94 size = blkoff(fs, offset) + bufsize;
99 assert(size <= fs->fs_bsize);
104 * If the next write will extend the file into a new block,
105 * and the file is currently composed of a fragment
106 * this fragment has to be extended to be a full block.
109 lastlbn = lblkno(fs, ip->i_ffs1_size);
110 if (lastlbn < NDADDR && lastlbn < lbn) {
112 osize = blksize(fs, ip, nb);
113 if (osize < fs->fs_bsize && osize > 0) {
114 warnx("need to ffs_realloccg; not supported!");
120 * The first NDADDR blocks are direct blocks
124 nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap);
125 if (nb != 0 && ip->i_ffs1_size >= lblktosize(fs, lbn + 1)) {
128 * The block is an already-allocated direct block
129 * and the file already extends past this block,
130 * thus this must be a whole block.
131 * Just read the block (if requested).
135 error = bread(ip->i_fd, ip->i_fs, lbn,
147 * Consider need to reallocate a fragment.
150 osize = fragroundup(fs, blkoff(fs, ip->i_ffs1_size));
151 nsize = fragroundup(fs, size);
152 if (nsize <= osize) {
155 * The existing block is already
156 * at least as big as we want.
157 * Just read the block (if requested).
161 error = bread(ip->i_fd, ip->i_fs, lbn,
170 warnx("need to ffs_realloccg; not supported!");
176 * the block was not previously allocated,
177 * allocate a new block or fragment.
180 if (ip->i_ffs1_size < lblktosize(fs, lbn + 1))
181 nsize = fragroundup(fs, size);
183 nsize = fs->fs_bsize;
184 error = ffs_alloc(ip, lbn,
185 ffs_blkpref_ufs1(ip, lbn, (int)lbn,
191 bp = getblk(ip->i_fd, ip->i_fs, lbn, nsize);
192 bp->b_blkno = fsbtodb(fs, newb);
197 ip->i_ffs1_db[lbn] = ufs_rw32((int32_t)newb, needswap);
202 * Determine the number of levels of indirection.
206 if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
210 warnx("ffs_balloc: ufs_getlbns returned indirect block");
215 * Fetch the first indirect block allocating if necessary.
219 nb = ufs_rw32(ip->i_ffs1_ib[indirs[0].in_off], needswap);
221 allocblk = allociblk;
223 pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
224 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
229 bp = getblk(ip->i_fd, ip->i_fs, indirs[1].in_lbn, fs->fs_bsize);
230 bp->b_blkno = fsbtodb(fs, nb);
233 * Write synchronously so that indirect blocks
234 * never point at garbage.
236 if ((error = bwrite(bp)) != 0)
238 allocib = &ip->i_ffs1_ib[indirs[0].in_off];
239 *allocib = ufs_rw32((int32_t)nb, needswap);
243 * Fetch through the indirect blocks, allocating as necessary.
247 error = bread(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
253 bap = (int32_t *)bp->b_data;
254 nb = ufs_rw32(bap[indirs[i].in_off], needswap);
263 pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
264 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
271 nbp = getblk(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
273 nbp->b_blkno = fsbtodb(fs, nb);
276 * Write synchronously so that indirect blocks
277 * never point at garbage.
280 if ((error = bwrite(nbp)) != 0) {
284 bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
290 * Get the data block, allocating if necessary.
294 pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, &bap[0]);
295 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
303 nbp = getblk(ip->i_fd, ip->i_fs, lbn, fs->fs_bsize);
304 nbp->b_blkno = fsbtodb(fs, nb);
308 bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
311 * If required, write synchronously, otherwise use
319 error = bread(ip->i_fd, ip->i_fs, lbn, (int)fs->fs_bsize, &nbp);
330 ffs_balloc_ufs2(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
332 daddr_t lbn, lastlbn;
334 struct buf *bp, *nbp;
335 struct fs *fs = ip->i_fs;
336 struct indir indirs[NIADDR + 2];
337 daddr_t newb, pref, nb;
339 int osize, nsize, num, i, error;
340 int64_t *allocblk, allociblk[NIADDR + 1];
342 const int needswap = UFS_FSNEEDSWAP(fs);
344 lbn = lblkno(fs, offset);
345 size = blkoff(fs, offset) + bufsize;
350 assert(size <= fs->fs_bsize);
355 * If the next write will extend the file into a new block,
356 * and the file is currently composed of a fragment
357 * this fragment has to be extended to be a full block.
360 lastlbn = lblkno(fs, ip->i_ffs2_size);
361 if (lastlbn < NDADDR && lastlbn < lbn) {
363 osize = blksize(fs, ip, nb);
364 if (osize < fs->fs_bsize && osize > 0) {
365 warnx("need to ffs_realloccg; not supported!");
371 * The first NDADDR blocks are direct blocks
375 nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
376 if (nb != 0 && ip->i_ffs2_size >= lblktosize(fs, lbn + 1)) {
379 * The block is an already-allocated direct block
380 * and the file already extends past this block,
381 * thus this must be a whole block.
382 * Just read the block (if requested).
386 error = bread(ip->i_fd, ip->i_fs, lbn,
398 * Consider need to reallocate a fragment.
401 osize = fragroundup(fs, blkoff(fs, ip->i_ffs2_size));
402 nsize = fragroundup(fs, size);
403 if (nsize <= osize) {
406 * The existing block is already
407 * at least as big as we want.
408 * Just read the block (if requested).
412 error = bread(ip->i_fd, ip->i_fs, lbn,
421 warnx("need to ffs_realloccg; not supported!");
427 * the block was not previously allocated,
428 * allocate a new block or fragment.
431 if (ip->i_ffs2_size < lblktosize(fs, lbn + 1))
432 nsize = fragroundup(fs, size);
434 nsize = fs->fs_bsize;
435 error = ffs_alloc(ip, lbn,
436 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
442 bp = getblk(ip->i_fd, ip->i_fs, lbn, nsize);
443 bp->b_blkno = fsbtodb(fs, newb);
448 ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap);
453 * Determine the number of levels of indirection.
457 if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
461 warnx("ffs_balloc: ufs_getlbns returned indirect block");
466 * Fetch the first indirect block allocating if necessary.
470 nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap);
472 allocblk = allociblk;
474 pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
475 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
480 bp = getblk(ip->i_fd, ip->i_fs, indirs[1].in_lbn, fs->fs_bsize);
481 bp->b_blkno = fsbtodb(fs, nb);
484 * Write synchronously so that indirect blocks
485 * never point at garbage.
487 if ((error = bwrite(bp)) != 0)
489 allocib = &ip->i_ffs2_ib[indirs[0].in_off];
490 *allocib = ufs_rw64(nb, needswap);
494 * Fetch through the indirect blocks, allocating as necessary.
498 error = bread(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
504 bap = (int64_t *)bp->b_data;
505 nb = ufs_rw64(bap[indirs[i].in_off], needswap);
514 pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
515 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
522 nbp = getblk(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
524 nbp->b_blkno = fsbtodb(fs, nb);
527 * Write synchronously so that indirect blocks
528 * never point at garbage.
531 if ((error = bwrite(nbp)) != 0) {
535 bap[indirs[i - 1].in_off] = ufs_rw64(nb, needswap);
541 * Get the data block, allocating if necessary.
545 pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, &bap[0]);
546 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
554 nbp = getblk(ip->i_fd, ip->i_fs, lbn, fs->fs_bsize);
555 nbp->b_blkno = fsbtodb(fs, nb);
559 bap[indirs[num].in_off] = ufs_rw64(nb, needswap);
562 * If required, write synchronously, otherwise use
570 error = bread(ip->i_fd, ip->i_fs, lbn, (int)fs->fs_bsize, &nbp);