OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.22/] [disk/] [tools/] [fs-NetBSD/] [makefs/] [ffs_balloc.c] - Diff between revs 17 and 21

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 17 Rev 21
/*      $NetBSD: ffs_balloc.c,v 1.13 2004/06/20 22:20:18 jmc Exp $      */
/*      $NetBSD: ffs_balloc.c,v 1.13 2004/06/20 22:20:18 jmc Exp $      */
/* From NetBSD: ffs_balloc.c,v 1.25 2001/08/08 08:36:36 lukem Exp */
/* From NetBSD: ffs_balloc.c,v 1.25 2001/08/08 08:36:36 lukem Exp */
 
 
/*
/*
 * Copyright (c) 1982, 1986, 1989, 1993
 * Copyright (c) 1982, 1986, 1989, 1993
 *      The Regents of the University of California.  All rights reserved.
 *      The Regents of the University of California.  All rights reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * modification, are permitted provided that the following conditions
 * are met:
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *    without specific prior written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * SUCH DAMAGE.
 *
 *
 *      @(#)ffs_balloc.c        8.8 (Berkeley) 6/16/95
 *      @(#)ffs_balloc.c        8.8 (Berkeley) 6/16/95
 */
 */
 
 
#if HAVE_NBTOOL_CONFIG_H
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#include "nbtool_config.h"
#endif
#endif
 
 
#include <sys/cdefs.h>
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(__lint)
#if defined(__RCSID) && !defined(__lint)
__RCSID("$NetBSD: ffs_balloc.c,v 1.13 2004/06/20 22:20:18 jmc Exp $");
__RCSID("$NetBSD: ffs_balloc.c,v 1.13 2004/06/20 22:20:18 jmc Exp $");
#endif  /* !__lint */
#endif  /* !__lint */
 
 
#include <sys/param.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/time.h>
 
 
#include <assert.h>
#include <assert.h>
#include <errno.h>
#include <errno.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
 
 
#include "common.h"
#include "common.h"
#include "makefs.h"
#include "makefs.h"
 
 
#include "dinode.h"
#include "dinode.h"
#include "ufs_bswap.h"
#include "ufs_bswap.h"
#include "fs.h"
#include "fs.h"
 
 
#include "buf.h"
#include "buf.h"
#include "ufs_inode.h"
#include "ufs_inode.h"
#include "ffs_extern.h"
#include "ffs_extern.h"
 
 
static int ffs_balloc_ufs1(struct inode *, off_t, int, struct buf **);
static int ffs_balloc_ufs1(struct inode *, off_t, int, struct buf **);
static int ffs_balloc_ufs2(struct inode *, off_t, int, struct buf **);
static int ffs_balloc_ufs2(struct inode *, off_t, int, struct buf **);
 
 
/*
/*
 * Balloc defines the structure of file system storage
 * Balloc defines the structure of file system storage
 * by allocating the physical blocks on a device given
 * by allocating the physical blocks on a device given
 * the inode and the logical block number in a file.
 * the inode and the logical block number in a file.
 *
 *
 * Assume: flags == B_SYNC | B_CLRBUF
 * Assume: flags == B_SYNC | B_CLRBUF
 */
 */
 
 
int
int
ffs_balloc(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
ffs_balloc(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
{
{
        if (ip->i_fs->fs_magic == FS_UFS2_MAGIC)
        if (ip->i_fs->fs_magic == FS_UFS2_MAGIC)
                return ffs_balloc_ufs2(ip, offset, bufsize, bpp);
                return ffs_balloc_ufs2(ip, offset, bufsize, bpp);
        else
        else
                return ffs_balloc_ufs1(ip, offset, bufsize, bpp);
                return ffs_balloc_ufs1(ip, offset, bufsize, bpp);
}
}
 
 
static int
static int
ffs_balloc_ufs1(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
ffs_balloc_ufs1(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
{
{
        daddr_t lbn, lastlbn;
        daddr_t lbn, lastlbn;
        int size;
        int size;
        int32_t nb;
        int32_t nb;
        struct buf *bp, *nbp;
        struct buf *bp, *nbp;
        struct fs *fs = ip->i_fs;
        struct fs *fs = ip->i_fs;
        struct indir indirs[NIADDR + 2];
        struct indir indirs[NIADDR + 2];
        daddr_t newb, pref;
        daddr_t newb, pref;
        int32_t *bap;
        int32_t *bap;
        int osize, nsize, num, i, error;
        int osize, nsize, num, i, error;
        int32_t *allocblk, allociblk[NIADDR + 1];
        int32_t *allocblk, allociblk[NIADDR + 1];
        int32_t *allocib;
        int32_t *allocib;
        const int needswap = UFS_FSNEEDSWAP(fs);
        const int needswap = UFS_FSNEEDSWAP(fs);
 
 
        lbn = lblkno(fs, offset);
        lbn = lblkno(fs, offset);
        size = blkoff(fs, offset) + bufsize;
        size = blkoff(fs, offset) + bufsize;
        if (bpp != NULL) {
        if (bpp != NULL) {
                *bpp = NULL;
                *bpp = NULL;
        }
        }
 
 
        assert(size <= fs->fs_bsize);
        assert(size <= fs->fs_bsize);
        if (lbn < 0)
        if (lbn < 0)
                return (EFBIG);
                return (EFBIG);
 
 
        /*
        /*
         * If the next write will extend the file into a new block,
         * If the next write will extend the file into a new block,
         * and the file is currently composed of a fragment
         * and the file is currently composed of a fragment
         * this fragment has to be extended to be a full block.
         * this fragment has to be extended to be a full block.
         */
         */
 
 
        lastlbn = lblkno(fs, ip->i_ffs1_size);
        lastlbn = lblkno(fs, ip->i_ffs1_size);
        if (lastlbn < NDADDR && lastlbn < lbn) {
        if (lastlbn < NDADDR && lastlbn < lbn) {
                nb = lastlbn;
                nb = lastlbn;
                osize = blksize(fs, ip, nb);
                osize = blksize(fs, ip, nb);
                if (osize < fs->fs_bsize && osize > 0) {
                if (osize < fs->fs_bsize && osize > 0) {
                        warnx("need to ffs_realloccg; not supported!");
                        warnx("need to ffs_realloccg; not supported!");
                        abort();
                        abort();
                }
                }
        }
        }
 
 
        /*
        /*
         * The first NDADDR blocks are direct blocks
         * The first NDADDR blocks are direct blocks
         */
         */
 
 
        if (lbn < NDADDR) {
        if (lbn < NDADDR) {
                nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap);
                nb = ufs_rw32(ip->i_ffs1_db[lbn], needswap);
                if (nb != 0 && ip->i_ffs1_size >= lblktosize(fs, lbn + 1)) {
                if (nb != 0 && ip->i_ffs1_size >= lblktosize(fs, lbn + 1)) {
 
 
                        /*
                        /*
                         * The block is an already-allocated direct block
                         * The block is an already-allocated direct block
                         * and the file already extends past this block,
                         * and the file already extends past this block,
                         * thus this must be a whole block.
                         * thus this must be a whole block.
                         * Just read the block (if requested).
                         * Just read the block (if requested).
                         */
                         */
 
 
                        if (bpp != NULL) {
                        if (bpp != NULL) {
                                error = bread(ip->i_fd, ip->i_fs, lbn,
                                error = bread(ip->i_fd, ip->i_fs, lbn,
                                    fs->fs_bsize, bpp);
                                    fs->fs_bsize, bpp);
                                if (error) {
                                if (error) {
                                        brelse(*bpp);
                                        brelse(*bpp);
                                        return (error);
                                        return (error);
                                }
                                }
                        }
                        }
                        return (0);
                        return (0);
                }
                }
                if (nb != 0) {
                if (nb != 0) {
 
 
                        /*
                        /*
                         * Consider need to reallocate a fragment.
                         * Consider need to reallocate a fragment.
                         */
                         */
 
 
                        osize = fragroundup(fs, blkoff(fs, ip->i_ffs1_size));
                        osize = fragroundup(fs, blkoff(fs, ip->i_ffs1_size));
                        nsize = fragroundup(fs, size);
                        nsize = fragroundup(fs, size);
                        if (nsize <= osize) {
                        if (nsize <= osize) {
 
 
                                /*
                                /*
                                 * The existing block is already
                                 * The existing block is already
                                 * at least as big as we want.
                                 * at least as big as we want.
                                 * Just read the block (if requested).
                                 * Just read the block (if requested).
                                 */
                                 */
 
 
                                if (bpp != NULL) {
                                if (bpp != NULL) {
                                        error = bread(ip->i_fd, ip->i_fs, lbn,
                                        error = bread(ip->i_fd, ip->i_fs, lbn,
                                            osize, bpp);
                                            osize, bpp);
                                        if (error) {
                                        if (error) {
                                                brelse(*bpp);
                                                brelse(*bpp);
                                                return (error);
                                                return (error);
                                        }
                                        }
                                }
                                }
                                return 0;
                                return 0;
                        } else {
                        } else {
                                warnx("need to ffs_realloccg; not supported!");
                                warnx("need to ffs_realloccg; not supported!");
                                abort();
                                abort();
                        }
                        }
                } else {
                } else {
 
 
                        /*
                        /*
                         * the block was not previously allocated,
                         * the block was not previously allocated,
                         * allocate a new block or fragment.
                         * allocate a new block or fragment.
                         */
                         */
 
 
                        if (ip->i_ffs1_size < lblktosize(fs, lbn + 1))
                        if (ip->i_ffs1_size < lblktosize(fs, lbn + 1))
                                nsize = fragroundup(fs, size);
                                nsize = fragroundup(fs, size);
                        else
                        else
                                nsize = fs->fs_bsize;
                                nsize = fs->fs_bsize;
                        error = ffs_alloc(ip, lbn,
                        error = ffs_alloc(ip, lbn,
                            ffs_blkpref_ufs1(ip, lbn, (int)lbn,
                            ffs_blkpref_ufs1(ip, lbn, (int)lbn,
                                &ip->i_ffs1_db[0]),
                                &ip->i_ffs1_db[0]),
                                nsize, &newb);
                                nsize, &newb);
                        if (error)
                        if (error)
                                return (error);
                                return (error);
                        if (bpp != NULL) {
                        if (bpp != NULL) {
                                bp = getblk(ip->i_fd, ip->i_fs, lbn, nsize);
                                bp = getblk(ip->i_fd, ip->i_fs, lbn, nsize);
                                bp->b_blkno = fsbtodb(fs, newb);
                                bp->b_blkno = fsbtodb(fs, newb);
                                clrbuf(bp);
                                clrbuf(bp);
                                *bpp = bp;
                                *bpp = bp;
                        }
                        }
                }
                }
                ip->i_ffs1_db[lbn] = ufs_rw32((int32_t)newb, needswap);
                ip->i_ffs1_db[lbn] = ufs_rw32((int32_t)newb, needswap);
                return (0);
                return (0);
        }
        }
 
 
        /*
        /*
         * Determine the number of levels of indirection.
         * Determine the number of levels of indirection.
         */
         */
 
 
        pref = 0;
        pref = 0;
        if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
        if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
                return (error);
                return (error);
 
 
        if (num < 1) {
        if (num < 1) {
                warnx("ffs_balloc: ufs_getlbns returned indirect block");
                warnx("ffs_balloc: ufs_getlbns returned indirect block");
                abort();
                abort();
        }
        }
 
 
        /*
        /*
         * Fetch the first indirect block allocating if necessary.
         * Fetch the first indirect block allocating if necessary.
         */
         */
 
 
        --num;
        --num;
        nb = ufs_rw32(ip->i_ffs1_ib[indirs[0].in_off], needswap);
        nb = ufs_rw32(ip->i_ffs1_ib[indirs[0].in_off], needswap);
        allocib = NULL;
        allocib = NULL;
        allocblk = allociblk;
        allocblk = allociblk;
        if (nb == 0) {
        if (nb == 0) {
                pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
                pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                if (error)
                if (error)
                        return error;
                        return error;
                nb = newb;
                nb = newb;
                *allocblk++ = nb;
                *allocblk++ = nb;
                bp = getblk(ip->i_fd, ip->i_fs, indirs[1].in_lbn, fs->fs_bsize);
                bp = getblk(ip->i_fd, ip->i_fs, indirs[1].in_lbn, fs->fs_bsize);
                bp->b_blkno = fsbtodb(fs, nb);
                bp->b_blkno = fsbtodb(fs, nb);
                clrbuf(bp);
                clrbuf(bp);
                /*
                /*
                 * Write synchronously so that indirect blocks
                 * Write synchronously so that indirect blocks
                 * never point at garbage.
                 * never point at garbage.
                 */
                 */
                if ((error = bwrite(bp)) != 0)
                if ((error = bwrite(bp)) != 0)
                        return error;
                        return error;
                allocib = &ip->i_ffs1_ib[indirs[0].in_off];
                allocib = &ip->i_ffs1_ib[indirs[0].in_off];
                *allocib = ufs_rw32((int32_t)nb, needswap);
                *allocib = ufs_rw32((int32_t)nb, needswap);
        }
        }
 
 
        /*
        /*
         * Fetch through the indirect blocks, allocating as necessary.
         * Fetch through the indirect blocks, allocating as necessary.
         */
         */
 
 
        for (i = 1;;) {
        for (i = 1;;) {
                error = bread(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
                error = bread(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
                    fs->fs_bsize, &bp);
                    fs->fs_bsize, &bp);
                if (error) {
                if (error) {
                        brelse(bp);
                        brelse(bp);
                        return error;
                        return error;
                }
                }
                bap = (int32_t *)bp->b_data;
                bap = (int32_t *)bp->b_data;
                nb = ufs_rw32(bap[indirs[i].in_off], needswap);
                nb = ufs_rw32(bap[indirs[i].in_off], needswap);
                if (i == num)
                if (i == num)
                        break;
                        break;
                i++;
                i++;
                if (nb != 0) {
                if (nb != 0) {
                        brelse(bp);
                        brelse(bp);
                        continue;
                        continue;
                }
                }
                if (pref == 0)
                if (pref == 0)
                        pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
                        pref = ffs_blkpref_ufs1(ip, lbn, 0, (int32_t *)0);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                if (error) {
                if (error) {
                        brelse(bp);
                        brelse(bp);
                        return error;
                        return error;
                }
                }
                nb = newb;
                nb = newb;
                *allocblk++ = nb;
                *allocblk++ = nb;
                nbp = getblk(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
                nbp = getblk(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
                    fs->fs_bsize);
                    fs->fs_bsize);
                nbp->b_blkno = fsbtodb(fs, nb);
                nbp->b_blkno = fsbtodb(fs, nb);
                clrbuf(nbp);
                clrbuf(nbp);
                /*
                /*
                 * Write synchronously so that indirect blocks
                 * Write synchronously so that indirect blocks
                 * never point at garbage.
                 * never point at garbage.
                 */
                 */
 
 
                if ((error = bwrite(nbp)) != 0) {
                if ((error = bwrite(nbp)) != 0) {
                        brelse(bp);
                        brelse(bp);
                        return error;
                        return error;
                }
                }
                bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
                bap[indirs[i - 1].in_off] = ufs_rw32(nb, needswap);
 
 
                bwrite(bp);
                bwrite(bp);
        }
        }
 
 
        /*
        /*
         * Get the data block, allocating if necessary.
         * Get the data block, allocating if necessary.
         */
         */
 
 
        if (nb == 0) {
        if (nb == 0) {
                pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, &bap[0]);
                pref = ffs_blkpref_ufs1(ip, lbn, indirs[num].in_off, &bap[0]);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                if (error) {
                if (error) {
                        brelse(bp);
                        brelse(bp);
                        return error;
                        return error;
                }
                }
                nb = newb;
                nb = newb;
                *allocblk++ = nb;
                *allocblk++ = nb;
                if (bpp != NULL) {
                if (bpp != NULL) {
                        nbp = getblk(ip->i_fd, ip->i_fs, lbn, fs->fs_bsize);
                        nbp = getblk(ip->i_fd, ip->i_fs, lbn, fs->fs_bsize);
                        nbp->b_blkno = fsbtodb(fs, nb);
                        nbp->b_blkno = fsbtodb(fs, nb);
                        clrbuf(nbp);
                        clrbuf(nbp);
                        *bpp = nbp;
                        *bpp = nbp;
                }
                }
                bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
                bap[indirs[num].in_off] = ufs_rw32(nb, needswap);
 
 
                /*
                /*
                 * If required, write synchronously, otherwise use
                 * If required, write synchronously, otherwise use
                 * delayed write.
                 * delayed write.
                 */
                 */
                bwrite(bp);
                bwrite(bp);
                return (0);
                return (0);
        }
        }
        brelse(bp);
        brelse(bp);
        if (bpp != NULL) {
        if (bpp != NULL) {
                error = bread(ip->i_fd, ip->i_fs, lbn, (int)fs->fs_bsize, &nbp);
                error = bread(ip->i_fd, ip->i_fs, lbn, (int)fs->fs_bsize, &nbp);
                if (error) {
                if (error) {
                        brelse(nbp);
                        brelse(nbp);
                        return error;
                        return error;
                }
                }
                *bpp = nbp;
                *bpp = nbp;
        }
        }
        return (0);
        return (0);
}
}
 
 
static int
static int
ffs_balloc_ufs2(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
ffs_balloc_ufs2(struct inode *ip, off_t offset, int bufsize, struct buf **bpp)
{
{
        daddr_t lbn, lastlbn;
        daddr_t lbn, lastlbn;
        int size;
        int size;
        struct buf *bp, *nbp;
        struct buf *bp, *nbp;
        struct fs *fs = ip->i_fs;
        struct fs *fs = ip->i_fs;
        struct indir indirs[NIADDR + 2];
        struct indir indirs[NIADDR + 2];
        daddr_t newb, pref, nb;
        daddr_t newb, pref, nb;
        int64_t *bap;
        int64_t *bap;
        int osize, nsize, num, i, error;
        int osize, nsize, num, i, error;
        int64_t *allocblk, allociblk[NIADDR + 1];
        int64_t *allocblk, allociblk[NIADDR + 1];
        int64_t *allocib;
        int64_t *allocib;
        const int needswap = UFS_FSNEEDSWAP(fs);
        const int needswap = UFS_FSNEEDSWAP(fs);
 
 
        lbn = lblkno(fs, offset);
        lbn = lblkno(fs, offset);
        size = blkoff(fs, offset) + bufsize;
        size = blkoff(fs, offset) + bufsize;
        if (bpp != NULL) {
        if (bpp != NULL) {
                *bpp = NULL;
                *bpp = NULL;
        }
        }
 
 
        assert(size <= fs->fs_bsize);
        assert(size <= fs->fs_bsize);
        if (lbn < 0)
        if (lbn < 0)
                return (EFBIG);
                return (EFBIG);
 
 
        /*
        /*
         * If the next write will extend the file into a new block,
         * If the next write will extend the file into a new block,
         * and the file is currently composed of a fragment
         * and the file is currently composed of a fragment
         * this fragment has to be extended to be a full block.
         * this fragment has to be extended to be a full block.
         */
         */
 
 
        lastlbn = lblkno(fs, ip->i_ffs2_size);
        lastlbn = lblkno(fs, ip->i_ffs2_size);
        if (lastlbn < NDADDR && lastlbn < lbn) {
        if (lastlbn < NDADDR && lastlbn < lbn) {
                nb = lastlbn;
                nb = lastlbn;
                osize = blksize(fs, ip, nb);
                osize = blksize(fs, ip, nb);
                if (osize < fs->fs_bsize && osize > 0) {
                if (osize < fs->fs_bsize && osize > 0) {
                        warnx("need to ffs_realloccg; not supported!");
                        warnx("need to ffs_realloccg; not supported!");
                        abort();
                        abort();
                }
                }
        }
        }
 
 
        /*
        /*
         * The first NDADDR blocks are direct blocks
         * The first NDADDR blocks are direct blocks
         */
         */
 
 
        if (lbn < NDADDR) {
        if (lbn < NDADDR) {
                nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
                nb = ufs_rw64(ip->i_ffs2_db[lbn], needswap);
                if (nb != 0 && ip->i_ffs2_size >= lblktosize(fs, lbn + 1)) {
                if (nb != 0 && ip->i_ffs2_size >= lblktosize(fs, lbn + 1)) {
 
 
                        /*
                        /*
                         * The block is an already-allocated direct block
                         * The block is an already-allocated direct block
                         * and the file already extends past this block,
                         * and the file already extends past this block,
                         * thus this must be a whole block.
                         * thus this must be a whole block.
                         * Just read the block (if requested).
                         * Just read the block (if requested).
                         */
                         */
 
 
                        if (bpp != NULL) {
                        if (bpp != NULL) {
                                error = bread(ip->i_fd, ip->i_fs, lbn,
                                error = bread(ip->i_fd, ip->i_fs, lbn,
                                    fs->fs_bsize, bpp);
                                    fs->fs_bsize, bpp);
                                if (error) {
                                if (error) {
                                        brelse(*bpp);
                                        brelse(*bpp);
                                        return (error);
                                        return (error);
                                }
                                }
                        }
                        }
                        return (0);
                        return (0);
                }
                }
                if (nb != 0) {
                if (nb != 0) {
 
 
                        /*
                        /*
                         * Consider need to reallocate a fragment.
                         * Consider need to reallocate a fragment.
                         */
                         */
 
 
                        osize = fragroundup(fs, blkoff(fs, ip->i_ffs2_size));
                        osize = fragroundup(fs, blkoff(fs, ip->i_ffs2_size));
                        nsize = fragroundup(fs, size);
                        nsize = fragroundup(fs, size);
                        if (nsize <= osize) {
                        if (nsize <= osize) {
 
 
                                /*
                                /*
                                 * The existing block is already
                                 * The existing block is already
                                 * at least as big as we want.
                                 * at least as big as we want.
                                 * Just read the block (if requested).
                                 * Just read the block (if requested).
                                 */
                                 */
 
 
                                if (bpp != NULL) {
                                if (bpp != NULL) {
                                        error = bread(ip->i_fd, ip->i_fs, lbn,
                                        error = bread(ip->i_fd, ip->i_fs, lbn,
                                            osize, bpp);
                                            osize, bpp);
                                        if (error) {
                                        if (error) {
                                                brelse(*bpp);
                                                brelse(*bpp);
                                                return (error);
                                                return (error);
                                        }
                                        }
                                }
                                }
                                return 0;
                                return 0;
                        } else {
                        } else {
                                warnx("need to ffs_realloccg; not supported!");
                                warnx("need to ffs_realloccg; not supported!");
                                abort();
                                abort();
                        }
                        }
                } else {
                } else {
 
 
                        /*
                        /*
                         * the block was not previously allocated,
                         * the block was not previously allocated,
                         * allocate a new block or fragment.
                         * allocate a new block or fragment.
                         */
                         */
 
 
                        if (ip->i_ffs2_size < lblktosize(fs, lbn + 1))
                        if (ip->i_ffs2_size < lblktosize(fs, lbn + 1))
                                nsize = fragroundup(fs, size);
                                nsize = fragroundup(fs, size);
                        else
                        else
                                nsize = fs->fs_bsize;
                                nsize = fs->fs_bsize;
                        error = ffs_alloc(ip, lbn,
                        error = ffs_alloc(ip, lbn,
                            ffs_blkpref_ufs2(ip, lbn, (int)lbn,
                            ffs_blkpref_ufs2(ip, lbn, (int)lbn,
                                &ip->i_ffs2_db[0]),
                                &ip->i_ffs2_db[0]),
                                nsize, &newb);
                                nsize, &newb);
                        if (error)
                        if (error)
                                return (error);
                                return (error);
                        if (bpp != NULL) {
                        if (bpp != NULL) {
                                bp = getblk(ip->i_fd, ip->i_fs, lbn, nsize);
                                bp = getblk(ip->i_fd, ip->i_fs, lbn, nsize);
                                bp->b_blkno = fsbtodb(fs, newb);
                                bp->b_blkno = fsbtodb(fs, newb);
                                clrbuf(bp);
                                clrbuf(bp);
                                *bpp = bp;
                                *bpp = bp;
                        }
                        }
                }
                }
                ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap);
                ip->i_ffs2_db[lbn] = ufs_rw64(newb, needswap);
                return (0);
                return (0);
        }
        }
 
 
        /*
        /*
         * Determine the number of levels of indirection.
         * Determine the number of levels of indirection.
         */
         */
 
 
        pref = 0;
        pref = 0;
        if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
        if ((error = ufs_getlbns(ip, lbn, indirs, &num)) != 0)
                return (error);
                return (error);
 
 
        if (num < 1) {
        if (num < 1) {
                warnx("ffs_balloc: ufs_getlbns returned indirect block");
                warnx("ffs_balloc: ufs_getlbns returned indirect block");
                abort();
                abort();
        }
        }
 
 
        /*
        /*
         * Fetch the first indirect block allocating if necessary.
         * Fetch the first indirect block allocating if necessary.
         */
         */
 
 
        --num;
        --num;
        nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap);
        nb = ufs_rw64(ip->i_ffs2_ib[indirs[0].in_off], needswap);
        allocib = NULL;
        allocib = NULL;
        allocblk = allociblk;
        allocblk = allociblk;
        if (nb == 0) {
        if (nb == 0) {
                pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
                pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                if (error)
                if (error)
                        return error;
                        return error;
                nb = newb;
                nb = newb;
                *allocblk++ = nb;
                *allocblk++ = nb;
                bp = getblk(ip->i_fd, ip->i_fs, indirs[1].in_lbn, fs->fs_bsize);
                bp = getblk(ip->i_fd, ip->i_fs, indirs[1].in_lbn, fs->fs_bsize);
                bp->b_blkno = fsbtodb(fs, nb);
                bp->b_blkno = fsbtodb(fs, nb);
                clrbuf(bp);
                clrbuf(bp);
                /*
                /*
                 * Write synchronously so that indirect blocks
                 * Write synchronously so that indirect blocks
                 * never point at garbage.
                 * never point at garbage.
                 */
                 */
                if ((error = bwrite(bp)) != 0)
                if ((error = bwrite(bp)) != 0)
                        return error;
                        return error;
                allocib = &ip->i_ffs2_ib[indirs[0].in_off];
                allocib = &ip->i_ffs2_ib[indirs[0].in_off];
                *allocib = ufs_rw64(nb, needswap);
                *allocib = ufs_rw64(nb, needswap);
        }
        }
 
 
        /*
        /*
         * Fetch through the indirect blocks, allocating as necessary.
         * Fetch through the indirect blocks, allocating as necessary.
         */
         */
 
 
        for (i = 1;;) {
        for (i = 1;;) {
                error = bread(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
                error = bread(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
                    fs->fs_bsize, &bp);
                    fs->fs_bsize, &bp);
                if (error) {
                if (error) {
                        brelse(bp);
                        brelse(bp);
                        return error;
                        return error;
                }
                }
                bap = (int64_t *)bp->b_data;
                bap = (int64_t *)bp->b_data;
                nb = ufs_rw64(bap[indirs[i].in_off], needswap);
                nb = ufs_rw64(bap[indirs[i].in_off], needswap);
                if (i == num)
                if (i == num)
                        break;
                        break;
                i++;
                i++;
                if (nb != 0) {
                if (nb != 0) {
                        brelse(bp);
                        brelse(bp);
                        continue;
                        continue;
                }
                }
                if (pref == 0)
                if (pref == 0)
                        pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
                        pref = ffs_blkpref_ufs2(ip, lbn, 0, (int64_t *)0);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                if (error) {
                if (error) {
                        brelse(bp);
                        brelse(bp);
                        return error;
                        return error;
                }
                }
                nb = newb;
                nb = newb;
                *allocblk++ = nb;
                *allocblk++ = nb;
                nbp = getblk(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
                nbp = getblk(ip->i_fd, ip->i_fs, indirs[i].in_lbn,
                    fs->fs_bsize);
                    fs->fs_bsize);
                nbp->b_blkno = fsbtodb(fs, nb);
                nbp->b_blkno = fsbtodb(fs, nb);
                clrbuf(nbp);
                clrbuf(nbp);
                /*
                /*
                 * Write synchronously so that indirect blocks
                 * Write synchronously so that indirect blocks
                 * never point at garbage.
                 * never point at garbage.
                 */
                 */
 
 
                if ((error = bwrite(nbp)) != 0) {
                if ((error = bwrite(nbp)) != 0) {
                        brelse(bp);
                        brelse(bp);
                        return error;
                        return error;
                }
                }
                bap[indirs[i - 1].in_off] = ufs_rw64(nb, needswap);
                bap[indirs[i - 1].in_off] = ufs_rw64(nb, needswap);
 
 
                bwrite(bp);
                bwrite(bp);
        }
        }
 
 
        /*
        /*
         * Get the data block, allocating if necessary.
         * Get the data block, allocating if necessary.
         */
         */
 
 
        if (nb == 0) {
        if (nb == 0) {
                pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, &bap[0]);
                pref = ffs_blkpref_ufs2(ip, lbn, indirs[num].in_off, &bap[0]);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                error = ffs_alloc(ip, lbn, pref, (int)fs->fs_bsize, &newb);
                if (error) {
                if (error) {
                        brelse(bp);
                        brelse(bp);
                        return error;
                        return error;
                }
                }
                nb = newb;
                nb = newb;
                *allocblk++ = nb;
                *allocblk++ = nb;
                if (bpp != NULL) {
                if (bpp != NULL) {
                        nbp = getblk(ip->i_fd, ip->i_fs, lbn, fs->fs_bsize);
                        nbp = getblk(ip->i_fd, ip->i_fs, lbn, fs->fs_bsize);
                        nbp->b_blkno = fsbtodb(fs, nb);
                        nbp->b_blkno = fsbtodb(fs, nb);
                        clrbuf(nbp);
                        clrbuf(nbp);
                        *bpp = nbp;
                        *bpp = nbp;
                }
                }
                bap[indirs[num].in_off] = ufs_rw64(nb, needswap);
                bap[indirs[num].in_off] = ufs_rw64(nb, needswap);
 
 
                /*
                /*
                 * If required, write synchronously, otherwise use
                 * If required, write synchronously, otherwise use
                 * delayed write.
                 * delayed write.
                 */
                 */
                bwrite(bp);
                bwrite(bp);
                return (0);
                return (0);
        }
        }
        brelse(bp);
        brelse(bp);
        if (bpp != NULL) {
        if (bpp != NULL) {
                error = bread(ip->i_fd, ip->i_fs, lbn, (int)fs->fs_bsize, &nbp);
                error = bread(ip->i_fd, ip->i_fs, lbn, (int)fs->fs_bsize, &nbp);
                if (error) {
                if (error) {
                        brelse(nbp);
                        brelse(nbp);
                        return error;
                        return error;
                }
                }
                *bpp = nbp;
                *bpp = nbp;
        }
        }
        return (0);
        return (0);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.