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 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 1982, 1986, 1989, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)ffs_balloc.c 8.8 (Berkeley) 6/16/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ffs/fs.h>
54 #include "ffs/ufs_bswap.h"
56 #include "ffs/ufs_inode.h"
57 #include "ffs/ffs_extern.h"
59 static int ffs_balloc_ufs1(struct inode *, off_t, int, struct buf **);
60 static int ffs_balloc_ufs2(struct inode *, off_t, int, struct buf **);
63 * Balloc defines the structure of file system storage
64 * by allocating the physical blocks on a device given
65 * the inode and the logical block number in a file.
67 * Assume: flags == B_SYNC | B_CLRBUF
71 ffs_balloc(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
73 if (ip->i_fs->fs_magic == FS_UFS2_MAGIC)
74 return ffs_balloc_ufs2(ip, offset, bufsize, bpp);
76 return ffs_balloc_ufs1(ip, offset, bufsize, bpp);
80 ffs_balloc_ufs1(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
86 struct fs *fs = ip->i_fs;
87 struct indir indirs[UFS_NIADDR + 2];
90 int osize, nsize, num, i, error;
91 int32_t *allocblk, allociblk[UFS_NIADDR + 1];
93 const int needswap = UFS_FSNEEDSWAP(fs);
95 lbn = lblkno(fs, offset);
96 size = blkoff(fs, offset) + bufsize;
101 assert(size <= fs->fs_bsize);
106 * If the next write will extend the file into a new block,
107 * and the file is currently composed of a fragment
108 * this fragment has to be extended to be a full block.
111 lastlbn = lblkno(fs, ip->i_ffs1_size);
112 if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
114 osize = blksize(fs, ip, nb);
115 if (osize < fs->fs_bsize && osize > 0) {
116 warnx("need to ffs_realloccg; not supported!");
122 * The first UFS_NDADDR blocks are direct blocks
125 if (lbn < UFS_NDADDR) {
126 nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap);
127 if (nb != 0 && ip->i_ffs1_size >=
128 (uint64_t)lblktosize(fs, lbn + 1)) {
131 * The block is an already-allocated direct block
132 * and the file already extends past this block,
133 * thus this must be a whole block.
134 * Just read the block (if requested).
138 error = bread(ip->i_devvp, lbn, fs->fs_bsize,
150 * Consider need to reallocate a fragment.
153 osize = fragroundup(fs, blkoff(fs, ip->i_ffs1_size));
154 nsize = fragroundup(fs, size);
155 if (nsize <= osize) {
158 * The existing block is already
159 * at least as big as we want.
160 * Just read the block (if requested).
164 error = bread(ip->i_devvp, lbn, osize,
173 warnx("need to ffs_realloccg; not supported!");
179 * the block was not previously allocated,
180 * allocate a new block or fragment.
183 if (ip->i_ffs1_size < (uint64_t)lblktosize(fs, lbn + 1))
184 nsize = fragroundup(fs, size);
186 nsize = fs->fs_bsize;
187 error = ffs_alloc(ip, lbn,
188 ffs_blkpref_ufs1(ip, lbn, (int)lbn,
194 bp = getblk(ip->i_devvp, lbn, nsize, 0, 0, 0);
195 bp->b_blkno = fsbtodb(fs, newb);
200 ip->i_ffs1_db[lbn] = ufs_rw32((int32_t)newb, needswap);
205 * Determine the number of levels of indirection.
209 if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
213 warnx("ffs_balloc: ufs_getlbns returned indirect block");
218 * Fetch the first indirect block allocating if necessary.
222 nb = ufs_rw32(ip->i_ffs1_ib[indirs[0].in_off], needswap);
224 allocblk = allociblk;
226 pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
227 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
232 bp = getblk(ip->i_devvp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, 0);
233 bp->b_blkno = fsbtodb(fs, nb);
236 * Write synchronously so that indirect blocks
237 * never point at garbage.
239 if ((error = bwrite(bp)) != 0)
241 allocib = &ip->i_ffs1_ib[indirs[0].in_off];
242 *allocib = ufs_rw32((int32_t)nb, needswap);
246 * Fetch through the indirect blocks, allocating as necessary.
250 error = bread(ip->i_devvp, indirs[i].in_lbn, fs->fs_bsize,
256 bap = (int32_t *)bp->b_data;
257 nb = ufs_rw32(bap[indirs[i].in_off], needswap);
266 pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
267 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
274 nbp = getblk(ip->i_devvp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
275 nbp->b_blkno = fsbtodb(fs, nb);
278 * Write synchronously so that indirect blocks
279 * never point at garbage.
282 if ((error = bwrite(nbp)) != 0) {
286 bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
292 * Get the data block, allocating if necessary.
296 pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, &bap[0]);
297 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
305 nbp = getblk(ip->i_devvp, lbn, fs->fs_bsize, 0, 0, 0);
306 nbp->b_blkno = fsbtodb(fs, nb);
310 bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
313 * If required, write synchronously, otherwise use
321 error = bread(ip->i_devvp, lbn, (int)fs->fs_bsize, NULL, &nbp);
332 ffs_balloc_ufs2(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
334 daddr_t lbn, lastlbn;
336 struct buf *bp, *nbp;
337 struct fs *fs = ip->i_fs;
338 struct indir indirs[UFS_NIADDR + 2];
339 daddr_t newb, pref, nb;
341 int osize, nsize, num, i, error;
342 int64_t *allocblk, allociblk[UFS_NIADDR + 1];
344 const int needswap = UFS_FSNEEDSWAP(fs);
346 lbn = lblkno(fs, offset);
347 size = blkoff(fs, offset) + bufsize;
352 assert(size <= fs->fs_bsize);
357 * If the next write will extend the file into a new block,
358 * and the file is currently composed of a fragment
359 * this fragment has to be extended to be a full block.
362 lastlbn = lblkno(fs, ip->i_ffs2_size);
363 if (lastlbn < UFS_NDADDR && lastlbn < lbn) {
365 osize = blksize(fs, ip, nb);
366 if (osize < fs->fs_bsize && osize > 0) {
367 warnx("need to ffs_realloccg; not supported!");
373 * The first UFS_NDADDR blocks are direct blocks
376 if (lbn < UFS_NDADDR) {
377 nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
378 if (nb != 0 && ip->i_ffs2_size >=
379 (uint64_t)lblktosize(fs, lbn + 1)) {
382 * The block is an already-allocated direct block
383 * and the file already extends past this block,
384 * thus this must be a whole block.
385 * Just read the block (if requested).
389 error = bread(ip->i_devvp, lbn, fs->fs_bsize,
401 * Consider need to reallocate a fragment.
404 osize = fragroundup(fs, blkoff(fs, ip->i_ffs2_size));
405 nsize = fragroundup(fs, size);
406 if (nsize <= osize) {
409 * The existing block is already
410 * at least as big as we want.
411 * Just read the block (if requested).
415 error = bread(ip->i_devvp, lbn, osize,
424 warnx("need to ffs_realloccg; not supported!");
430 * the block was not previously allocated,
431 * allocate a new block or fragment.
434 if (ip->i_ffs2_size < (uint64_t)lblktosize(fs, lbn + 1))
435 nsize = fragroundup(fs, size);
437 nsize = fs->fs_bsize;
438 error = ffs_alloc(ip, lbn,
439 ffs_blkpref_ufs2(ip, lbn, (int)lbn,
445 bp = getblk(ip->i_devvp, lbn, nsize, 0, 0, 0);
446 bp->b_blkno = fsbtodb(fs, newb);
451 ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap);
456 * Determine the number of levels of indirection.
460 if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
464 warnx("ffs_balloc: ufs_getlbns returned indirect block");
469 * Fetch the first indirect block allocating if necessary.
473 nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap);
475 allocblk = allociblk;
477 pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
478 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
483 bp = getblk(ip->i_devvp, indirs[1].in_lbn, fs->fs_bsize, 0, 0, 0);
484 bp->b_blkno = fsbtodb(fs, nb);
487 * Write synchronously so that indirect blocks
488 * never point at garbage.
490 if ((error = bwrite(bp)) != 0)
492 allocib = &ip->i_ffs2_ib[indirs[0].in_off];
493 *allocib = ufs_rw64(nb, needswap);
497 * Fetch through the indirect blocks, allocating as necessary.
501 error = bread(ip->i_devvp, indirs[i].in_lbn, fs->fs_bsize,
507 bap = (int64_t *)bp->b_data;
508 nb = ufs_rw64(bap[indirs[i].in_off], needswap);
517 pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
518 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
525 nbp = getblk(ip->i_devvp, indirs[i].in_lbn, fs->fs_bsize, 0, 0, 0);
526 nbp->b_blkno = fsbtodb(fs, nb);
529 * Write synchronously so that indirect blocks
530 * never point at garbage.
533 if ((error = bwrite(nbp)) != 0) {
537 bap[indirs[i - 1].in_off] = ufs_rw64(nb, needswap);
543 * Get the data block, allocating if necessary.
547 pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, &bap[0]);
548 error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
556 nbp = getblk(ip->i_devvp, lbn, fs->fs_bsize, 0, 0, 0);
557 nbp->b_blkno = fsbtodb(fs, nb);
561 bap[indirs[num].in_off] = ufs_rw64(nb, needswap);
564 * If required, write synchronously, otherwise use
572 error = bread(ip->i_devvp, lbn, (int)fs->fs_bsize, NULL, &nbp);