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

Subversion Repositories eco32

[/] [eco32/] [tags/] [eco32-0.22/] [disk/] [tools/] [fs-NetBSD/] [makefs/] [ffs_alloc.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_alloc.c,v 1.18 2011/03/06 17:08:42 bouyer Exp $    */
/*      $NetBSD: ffs_alloc.c,v 1.18 2011/03/06 17:08:42 bouyer Exp $    */
/* From: NetBSD: ffs_alloc.c,v 1.50 2001/09/06 02:16:01 lukem Exp */
/* From: NetBSD: ffs_alloc.c,v 1.50 2001/09/06 02:16:01 lukem Exp */
 
 
/*
/*
 * Copyright (c) 2002 Networks Associates Technology, Inc.
 * Copyright (c) 2002 Networks Associates Technology, Inc.
 * All rights reserved.
 * All rights reserved.
 *
 *
 * This software was developed for the FreeBSD Project by Marshall
 * This software was developed for the FreeBSD Project by Marshall
 * Kirk McKusick and Network Associates Laboratories, the Security
 * Kirk McKusick and Network Associates Laboratories, the Security
 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
 * research program
 * research program
 *
 *
 * 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_alloc.c 8.19 (Berkeley) 7/13/95
 *      @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/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_alloc.c,v 1.18 2011/03/06 17:08:42 bouyer Exp $");
__RCSID("$NetBSD: ffs_alloc.c,v 1.18 2011/03/06 17:08:42 bouyer Exp $");
#endif  /* !__lint */
#endif  /* !__lint */
 
 
#include <sys/param.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/time.h>
 
 
#include <errno.h>
#include <errno.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 scanc(u_int, const u_char *, const u_char *, int);
static int scanc(u_int, const u_char *, const u_char *, int);
 
 
static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int);
static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int);
static daddr_t ffs_alloccgblk(struct inode *, struct buf *, daddr_t);
static daddr_t ffs_alloccgblk(struct inode *, struct buf *, daddr_t);
static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int,
static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int,
                     daddr_t (*)(struct inode *, int, daddr_t, int));
                     daddr_t (*)(struct inode *, int, daddr_t, int));
static int32_t ffs_mapsearch(struct fs *, struct cg *, daddr_t, int);
static int32_t ffs_mapsearch(struct fs *, struct cg *, daddr_t, int);
 
 
/* in ffs_tables.c */
/* in ffs_tables.c */
extern const int inside[], around[];
extern const int inside[], around[];
extern const u_char * const fragtbl[];
extern const u_char * const fragtbl[];
 
 
/*
/*
 * Allocate a block in the file system.
 * Allocate a block in the file system.
 *
 *
 * The size of the requested block is given, which must be some
 * The size of the requested block is given, which must be some
 * multiple of fs_fsize and <= fs_bsize.
 * multiple of fs_fsize and <= fs_bsize.
 * A preference may be optionally specified. If a preference is given
 * A preference may be optionally specified. If a preference is given
 * the following hierarchy is used to allocate a block:
 * the following hierarchy is used to allocate a block:
 *   1) allocate the requested block.
 *   1) allocate the requested block.
 *   2) allocate a rotationally optimal block in the same cylinder.
 *   2) allocate a rotationally optimal block in the same cylinder.
 *   3) allocate a block in the same cylinder group.
 *   3) allocate a block in the same cylinder group.
 *   4) quadradically rehash into other cylinder groups, until an
 *   4) quadradically rehash into other cylinder groups, until an
 *      available block is located.
 *      available block is located.
 * If no block preference is given the following hierarchy is used
 * If no block preference is given the following hierarchy is used
 * to allocate a block:
 * to allocate a block:
 *   1) allocate a block in the cylinder group that contains the
 *   1) allocate a block in the cylinder group that contains the
 *      inode for the file.
 *      inode for the file.
 *   2) quadradically rehash into other cylinder groups, until an
 *   2) quadradically rehash into other cylinder groups, until an
 *      available block is located.
 *      available block is located.
 */
 */
int
int
ffs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref, int size,
ffs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref, int size,
    daddr_t *bnp)
    daddr_t *bnp)
{
{
        struct fs *fs = ip->i_fs;
        struct fs *fs = ip->i_fs;
        daddr_t bno;
        daddr_t bno;
        int cg;
        int cg;
 
 
        *bnp = 0;
        *bnp = 0;
        if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
        if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
                errx(1, "ffs_alloc: bad size: bsize %d size %d",
                errx(1, "ffs_alloc: bad size: bsize %d size %d",
                    fs->fs_bsize, size);
                    fs->fs_bsize, size);
        }
        }
        if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
        if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
                goto nospace;
                goto nospace;
        if (bpref >= fs->fs_size)
        if (bpref >= fs->fs_size)
                bpref = 0;
                bpref = 0;
        if (bpref == 0)
        if (bpref == 0)
                cg = ino_to_cg(fs, ip->i_number);
                cg = ino_to_cg(fs, ip->i_number);
        else
        else
                cg = dtog(fs, bpref);
                cg = dtog(fs, bpref);
        bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
        bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
        if (bno > 0) {
        if (bno > 0) {
                DIP_ADD(ip, blocks, size / DEV_BSIZE);
                DIP_ADD(ip, blocks, size / DEV_BSIZE);
                *bnp = bno;
                *bnp = bno;
                return (0);
                return (0);
        }
        }
nospace:
nospace:
        return (ENOSPC);
        return (ENOSPC);
}
}
 
 
/*
/*
 * Select the desired position for the next block in a file.  The file is
 * Select the desired position for the next block in a file.  The file is
 * logically divided into sections. The first section is composed of the
 * logically divided into sections. The first section is composed of the
 * direct blocks. Each additional section contains fs_maxbpg blocks.
 * direct blocks. Each additional section contains fs_maxbpg blocks.
 *
 *
 * If no blocks have been allocated in the first section, the policy is to
 * If no blocks have been allocated in the first section, the policy is to
 * request a block in the same cylinder group as the inode that describes
 * request a block in the same cylinder group as the inode that describes
 * the file. If no blocks have been allocated in any other section, the
 * the file. If no blocks have been allocated in any other section, the
 * policy is to place the section in a cylinder group with a greater than
 * policy is to place the section in a cylinder group with a greater than
 * average number of free blocks.  An appropriate cylinder group is found
 * average number of free blocks.  An appropriate cylinder group is found
 * by using a rotor that sweeps the cylinder groups. When a new group of
 * by using a rotor that sweeps the cylinder groups. When a new group of
 * blocks is needed, the sweep begins in the cylinder group following the
 * blocks is needed, the sweep begins in the cylinder group following the
 * cylinder group from which the previous allocation was made. The sweep
 * cylinder group from which the previous allocation was made. The sweep
 * continues until a cylinder group with greater than the average number
 * continues until a cylinder group with greater than the average number
 * of free blocks is found. If the allocation is for the first block in an
 * of free blocks is found. If the allocation is for the first block in an
 * indirect block, the information on the previous allocation is unavailable;
 * indirect block, the information on the previous allocation is unavailable;
 * here a best guess is made based upon the logical block number being
 * here a best guess is made based upon the logical block number being
 * allocated.
 * allocated.
 *
 *
 * If a section is already partially allocated, the policy is to
 * If a section is already partially allocated, the policy is to
 * contiguously allocate fs_maxcontig blocks.  The end of one of these
 * contiguously allocate fs_maxcontig blocks.  The end of one of these
 * contiguous blocks and the beginning of the next is physically separated
 * contiguous blocks and the beginning of the next is physically separated
 * so that the disk head will be in transit between them for at least
 * so that the disk head will be in transit between them for at least
 * fs_rotdelay milliseconds.  This is to allow time for the processor to
 * fs_rotdelay milliseconds.  This is to allow time for the processor to
 * schedule another I/O transfer.
 * schedule another I/O transfer.
 */
 */
/* XXX ondisk32 */
/* XXX ondisk32 */
daddr_t
daddr_t
ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int32_t *bap)
ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int32_t *bap)
{
{
        struct fs *fs;
        struct fs *fs;
        int cg;
        int cg;
        int avgbfree, startcg;
        int avgbfree, startcg;
 
 
        fs = ip->i_fs;
        fs = ip->i_fs;
        if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
        if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
                if (lbn < NDADDR + NINDIR(fs)) {
                if (lbn < NDADDR + NINDIR(fs)) {
                        cg = ino_to_cg(fs, ip->i_number);
                        cg = ino_to_cg(fs, ip->i_number);
                        return (fs->fs_fpg * cg + fs->fs_frag);
                        return (fs->fs_fpg * cg + fs->fs_frag);
                }
                }
                /*
                /*
                 * Find a cylinder with greater than average number of
                 * Find a cylinder with greater than average number of
                 * unused data blocks.
                 * unused data blocks.
                 */
                 */
                if (indx == 0 || bap[indx - 1] == 0)
                if (indx == 0 || bap[indx - 1] == 0)
                        startcg =
                        startcg =
                            ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
                            ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
                else
                else
                        startcg = dtog(fs,
                        startcg = dtog(fs,
                                ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
                                ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
                startcg %= fs->fs_ncg;
                startcg %= fs->fs_ncg;
                avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
                avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
                for (cg = startcg; cg < fs->fs_ncg; cg++)
                for (cg = startcg; cg < fs->fs_ncg; cg++)
                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
                                return (fs->fs_fpg * cg + fs->fs_frag);
                                return (fs->fs_fpg * cg + fs->fs_frag);
                for (cg = 0; cg <= startcg; cg++)
                for (cg = 0; cg <= startcg; cg++)
                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
                                return (fs->fs_fpg * cg + fs->fs_frag);
                                return (fs->fs_fpg * cg + fs->fs_frag);
                return (0);
                return (0);
        }
        }
        /*
        /*
         * We just always try to lay things out contiguously.
         * We just always try to lay things out contiguously.
         */
         */
        return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
        return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
}
}
 
 
daddr_t
daddr_t
ffs_blkpref_ufs2(ip, lbn, indx, bap)
ffs_blkpref_ufs2(ip, lbn, indx, bap)
        struct inode *ip;
        struct inode *ip;
        daddr_t lbn;
        daddr_t lbn;
        int indx;
        int indx;
        int64_t *bap;
        int64_t *bap;
{
{
        struct fs *fs;
        struct fs *fs;
        int cg;
        int cg;
        int avgbfree, startcg;
        int avgbfree, startcg;
 
 
        fs = ip->i_fs;
        fs = ip->i_fs;
        if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
        if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
                if (lbn < NDADDR + NINDIR(fs)) {
                if (lbn < NDADDR + NINDIR(fs)) {
                        cg = ino_to_cg(fs, ip->i_number);
                        cg = ino_to_cg(fs, ip->i_number);
                        return (fs->fs_fpg * cg + fs->fs_frag);
                        return (fs->fs_fpg * cg + fs->fs_frag);
                }
                }
                /*
                /*
                 * Find a cylinder with greater than average number of
                 * Find a cylinder with greater than average number of
                 * unused data blocks.
                 * unused data blocks.
                 */
                 */
                if (indx == 0 || bap[indx - 1] == 0)
                if (indx == 0 || bap[indx - 1] == 0)
                        startcg =
                        startcg =
                            ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
                            ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
                else
                else
                        startcg = dtog(fs,
                        startcg = dtog(fs,
                                ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
                                ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
                startcg %= fs->fs_ncg;
                startcg %= fs->fs_ncg;
                avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
                avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
                for (cg = startcg; cg < fs->fs_ncg; cg++)
                for (cg = startcg; cg < fs->fs_ncg; cg++)
                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
                                return (fs->fs_fpg * cg + fs->fs_frag);
                                return (fs->fs_fpg * cg + fs->fs_frag);
                        }
                        }
                for (cg = 0; cg < startcg; cg++)
                for (cg = 0; cg < startcg; cg++)
                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
                                return (fs->fs_fpg * cg + fs->fs_frag);
                                return (fs->fs_fpg * cg + fs->fs_frag);
                        }
                        }
                return (0);
                return (0);
        }
        }
        /*
        /*
         * We just always try to lay things out contiguously.
         * We just always try to lay things out contiguously.
         */
         */
        return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
        return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
}
}
 
 
/*
/*
 * Implement the cylinder overflow algorithm.
 * Implement the cylinder overflow algorithm.
 *
 *
 * The policy implemented by this algorithm is:
 * The policy implemented by this algorithm is:
 *   1) allocate the block in its requested cylinder group.
 *   1) allocate the block in its requested cylinder group.
 *   2) quadradically rehash on the cylinder group number.
 *   2) quadradically rehash on the cylinder group number.
 *   3) brute force search for a free block.
 *   3) brute force search for a free block.
 *
 *
 * `size':      size for data blocks, mode for inodes
 * `size':      size for data blocks, mode for inodes
 */
 */
/*VARARGS5*/
/*VARARGS5*/
static daddr_t
static daddr_t
ffs_hashalloc(struct inode *ip, int cg, daddr_t pref, int size,
ffs_hashalloc(struct inode *ip, int cg, daddr_t pref, int size,
    daddr_t (*allocator)(struct inode *, int, daddr_t, int))
    daddr_t (*allocator)(struct inode *, int, daddr_t, int))
{
{
        struct fs *fs;
        struct fs *fs;
        daddr_t result;
        daddr_t result;
        int i, icg = cg;
        int i, icg = cg;
 
 
        fs = ip->i_fs;
        fs = ip->i_fs;
        /*
        /*
         * 1: preferred cylinder group
         * 1: preferred cylinder group
         */
         */
        result = (*allocator)(ip, cg, pref, size);
        result = (*allocator)(ip, cg, pref, size);
        if (result)
        if (result)
                return (result);
                return (result);
        /*
        /*
         * 2: quadratic rehash
         * 2: quadratic rehash
         */
         */
        for (i = 1; i < fs->fs_ncg; i *= 2) {
        for (i = 1; i < fs->fs_ncg; i *= 2) {
                cg += i;
                cg += i;
                if (cg >= fs->fs_ncg)
                if (cg >= fs->fs_ncg)
                        cg -= fs->fs_ncg;
                        cg -= fs->fs_ncg;
                result = (*allocator)(ip, cg, 0, size);
                result = (*allocator)(ip, cg, 0, size);
                if (result)
                if (result)
                        return (result);
                        return (result);
        }
        }
        /*
        /*
         * 3: brute force search
         * 3: brute force search
         * Note that we start at i == 2, since 0 was checked initially,
         * Note that we start at i == 2, since 0 was checked initially,
         * and 1 is always checked in the quadratic rehash.
         * and 1 is always checked in the quadratic rehash.
         */
         */
        cg = (icg + 2) % fs->fs_ncg;
        cg = (icg + 2) % fs->fs_ncg;
        for (i = 2; i < fs->fs_ncg; i++) {
        for (i = 2; i < fs->fs_ncg; i++) {
                result = (*allocator)(ip, cg, 0, size);
                result = (*allocator)(ip, cg, 0, size);
                if (result)
                if (result)
                        return (result);
                        return (result);
                cg++;
                cg++;
                if (cg == fs->fs_ncg)
                if (cg == fs->fs_ncg)
                        cg = 0;
                        cg = 0;
        }
        }
        return (0);
        return (0);
}
}
 
 
/*
/*
 * Determine whether a block can be allocated.
 * Determine whether a block can be allocated.
 *
 *
 * Check to see if a block of the appropriate size is available,
 * Check to see if a block of the appropriate size is available,
 * and if it is, allocate it.
 * and if it is, allocate it.
 */
 */
static daddr_t
static daddr_t
ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
{
{
        struct cg *cgp;
        struct cg *cgp;
        struct buf *bp;
        struct buf *bp;
        daddr_t bno, blkno;
        daddr_t bno, blkno;
        int error, frags, allocsiz, i;
        int error, frags, allocsiz, i;
        struct fs *fs = ip->i_fs;
        struct fs *fs = ip->i_fs;
        const int needswap = UFS_FSNEEDSWAP(fs);
        const int needswap = UFS_FSNEEDSWAP(fs);
 
 
        if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
        if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
                return (0);
                return (0);
        error = bread(ip->i_fd, ip->i_fs, fsbtodb(fs, cgtod(fs, cg)),
        error = bread(ip->i_fd, ip->i_fs, fsbtodb(fs, cgtod(fs, cg)),
                (int)fs->fs_cgsize, &bp);
                (int)fs->fs_cgsize, &bp);
        if (error) {
        if (error) {
                brelse(bp);
                brelse(bp);
                return (0);
                return (0);
        }
        }
        cgp = (struct cg *)bp->b_data;
        cgp = (struct cg *)bp->b_data;
        if (!cg_chkmagic(cgp, needswap) ||
        if (!cg_chkmagic(cgp, needswap) ||
            (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
            (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
                brelse(bp);
                brelse(bp);
                return (0);
                return (0);
        }
        }
        if (size == fs->fs_bsize) {
        if (size == fs->fs_bsize) {
                bno = ffs_alloccgblk(ip, bp, bpref);
                bno = ffs_alloccgblk(ip, bp, bpref);
                bdwrite(bp);
                bdwrite(bp);
                return (bno);
                return (bno);
        }
        }
        /*
        /*
         * check to see if any fragments are already available
         * check to see if any fragments are already available
         * allocsiz is the size which will be allocated, hacking
         * allocsiz is the size which will be allocated, hacking
         * it down to a smaller size if necessary
         * it down to a smaller size if necessary
         */
         */
        frags = numfrags(fs, size);
        frags = numfrags(fs, size);
        for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
        for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
                if (cgp->cg_frsum[allocsiz] != 0)
                if (cgp->cg_frsum[allocsiz] != 0)
                        break;
                        break;
        if (allocsiz == fs->fs_frag) {
        if (allocsiz == fs->fs_frag) {
                /*
                /*
                 * no fragments were available, so a block will be
                 * no fragments were available, so a block will be
                 * allocated, and hacked up
                 * allocated, and hacked up
                 */
                 */
                if (cgp->cg_cs.cs_nbfree == 0) {
                if (cgp->cg_cs.cs_nbfree == 0) {
                        brelse(bp);
                        brelse(bp);
                        return (0);
                        return (0);
                }
                }
                bno = ffs_alloccgblk(ip, bp, bpref);
                bno = ffs_alloccgblk(ip, bp, bpref);
                bpref = dtogd(fs, bno);
                bpref = dtogd(fs, bno);
                for (i = frags; i < fs->fs_frag; i++)
                for (i = frags; i < fs->fs_frag; i++)
                        setbit(cg_blksfree(cgp, needswap), bpref + i);
                        setbit(cg_blksfree(cgp, needswap), bpref + i);
                i = fs->fs_frag - frags;
                i = fs->fs_frag - frags;
                ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
                ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
                fs->fs_cstotal.cs_nffree += i;
                fs->fs_cstotal.cs_nffree += i;
                fs->fs_cs(fs, cg).cs_nffree += i;
                fs->fs_cs(fs, cg).cs_nffree += i;
                fs->fs_fmod = 1;
                fs->fs_fmod = 1;
                ufs_add32(cgp->cg_frsum[i], 1, needswap);
                ufs_add32(cgp->cg_frsum[i], 1, needswap);
                bdwrite(bp);
                bdwrite(bp);
                return (bno);
                return (bno);
        }
        }
        bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
        bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
        for (i = 0; i < frags; i++)
        for (i = 0; i < frags; i++)
                clrbit(cg_blksfree(cgp, needswap), bno + i);
                clrbit(cg_blksfree(cgp, needswap), bno + i);
        ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
        ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
        fs->fs_cstotal.cs_nffree -= frags;
        fs->fs_cstotal.cs_nffree -= frags;
        fs->fs_cs(fs, cg).cs_nffree -= frags;
        fs->fs_cs(fs, cg).cs_nffree -= frags;
        fs->fs_fmod = 1;
        fs->fs_fmod = 1;
        ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
        ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
        if (frags != allocsiz)
        if (frags != allocsiz)
                ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
                ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
        blkno = cg * fs->fs_fpg + bno;
        blkno = cg * fs->fs_fpg + bno;
        bdwrite(bp);
        bdwrite(bp);
        return blkno;
        return blkno;
}
}
 
 
/*
/*
 * Allocate a block in a cylinder group.
 * Allocate a block in a cylinder group.
 *
 *
 * This algorithm implements the following policy:
 * This algorithm implements the following policy:
 *   1) allocate the requested block.
 *   1) allocate the requested block.
 *   2) allocate a rotationally optimal block in the same cylinder.
 *   2) allocate a rotationally optimal block in the same cylinder.
 *   3) allocate the next available block on the block rotor for the
 *   3) allocate the next available block on the block rotor for the
 *      specified cylinder group.
 *      specified cylinder group.
 * Note that this routine only allocates fs_bsize blocks; these
 * Note that this routine only allocates fs_bsize blocks; these
 * blocks may be fragmented by the routine that allocates them.
 * blocks may be fragmented by the routine that allocates them.
 */
 */
static daddr_t
static daddr_t
ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref)
ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref)
{
{
        struct cg *cgp;
        struct cg *cgp;
        daddr_t blkno;
        daddr_t blkno;
        int32_t bno;
        int32_t bno;
        struct fs *fs = ip->i_fs;
        struct fs *fs = ip->i_fs;
        const int needswap = UFS_FSNEEDSWAP(fs);
        const int needswap = UFS_FSNEEDSWAP(fs);
        u_int8_t *blksfree;
        u_int8_t *blksfree;
 
 
        cgp = (struct cg *)bp->b_data;
        cgp = (struct cg *)bp->b_data;
        blksfree = cg_blksfree(cgp, needswap);
        blksfree = cg_blksfree(cgp, needswap);
        if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
        if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
                bpref = ufs_rw32(cgp->cg_rotor, needswap);
                bpref = ufs_rw32(cgp->cg_rotor, needswap);
        } else {
        } else {
                bpref = blknum(fs, bpref);
                bpref = blknum(fs, bpref);
                bno = dtogd(fs, bpref);
                bno = dtogd(fs, bpref);
                /*
                /*
                 * if the requested block is available, use it
                 * if the requested block is available, use it
                 */
                 */
                if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
                if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
                        goto gotit;
                        goto gotit;
        }
        }
        /*
        /*
         * Take the next available one in this cylinder group.
         * Take the next available one in this cylinder group.
         */
         */
        bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
        bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
        if (bno < 0)
        if (bno < 0)
                return (0);
                return (0);
        cgp->cg_rotor = ufs_rw32(bno, needswap);
        cgp->cg_rotor = ufs_rw32(bno, needswap);
gotit:
gotit:
        blkno = fragstoblks(fs, bno);
        blkno = fragstoblks(fs, bno);
        ffs_clrblock(fs, blksfree, (long)blkno);
        ffs_clrblock(fs, blksfree, (long)blkno);
        ffs_clusteracct(fs, cgp, blkno, -1);
        ffs_clusteracct(fs, cgp, blkno, -1);
        ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
        ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
        fs->fs_cstotal.cs_nbfree--;
        fs->fs_cstotal.cs_nbfree--;
        fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
        fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
        fs->fs_fmod = 1;
        fs->fs_fmod = 1;
        blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
        blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
        return (blkno);
        return (blkno);
}
}
 
 
/*
/*
 * Free a block or fragment.
 * Free a block or fragment.
 *
 *
 * The specified block or fragment is placed back in the
 * The specified block or fragment is placed back in the
 * free map. If a fragment is deallocated, a possible
 * free map. If a fragment is deallocated, a possible
 * block reassembly is checked.
 * block reassembly is checked.
 */
 */
void
void
ffs_blkfree(struct inode *ip, daddr_t bno, long size)
ffs_blkfree(struct inode *ip, daddr_t bno, long size)
{
{
        struct cg *cgp;
        struct cg *cgp;
        struct buf *bp;
        struct buf *bp;
        int32_t fragno, cgbno;
        int32_t fragno, cgbno;
        int i, error, cg, blk, frags, bbase;
        int i, error, cg, blk, frags, bbase;
        struct fs *fs = ip->i_fs;
        struct fs *fs = ip->i_fs;
        const int needswap = UFS_FSNEEDSWAP(fs);
        const int needswap = UFS_FSNEEDSWAP(fs);
 
 
        if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
        if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
            fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
            fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
                errx(1, "blkfree: bad size: bno %lld bsize %d size %ld",
                errx(1, "blkfree: bad size: bno %lld bsize %d size %ld",
                    (long long)bno, fs->fs_bsize, size);
                    (long long)bno, fs->fs_bsize, size);
        }
        }
        cg = dtog(fs, bno);
        cg = dtog(fs, bno);
        if (bno >= fs->fs_size) {
        if (bno >= fs->fs_size) {
                warnx("bad block %lld, ino %llu", (long long)bno,
                warnx("bad block %lld, ino %llu", (long long)bno,
                    (unsigned long long)ip->i_number);
                    (unsigned long long)ip->i_number);
                return;
                return;
        }
        }
        error = bread(ip->i_fd, ip->i_fs, fsbtodb(fs, cgtod(fs, cg)),
        error = bread(ip->i_fd, ip->i_fs, fsbtodb(fs, cgtod(fs, cg)),
                (int)fs->fs_cgsize, &bp);
                (int)fs->fs_cgsize, &bp);
        if (error) {
        if (error) {
                brelse(bp);
                brelse(bp);
                return;
                return;
        }
        }
        cgp = (struct cg *)bp->b_data;
        cgp = (struct cg *)bp->b_data;
        if (!cg_chkmagic(cgp, needswap)) {
        if (!cg_chkmagic(cgp, needswap)) {
                brelse(bp);
                brelse(bp);
                return;
                return;
        }
        }
        cgbno = dtogd(fs, bno);
        cgbno = dtogd(fs, bno);
        if (size == fs->fs_bsize) {
        if (size == fs->fs_bsize) {
                fragno = fragstoblks(fs, cgbno);
                fragno = fragstoblks(fs, cgbno);
                if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), fragno)) {
                if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), fragno)) {
                        errx(1, "blkfree: freeing free block %lld",
                        errx(1, "blkfree: freeing free block %lld",
                            (long long)bno);
                            (long long)bno);
                }
                }
                ffs_setblock(fs, cg_blksfree(cgp, needswap), fragno);
                ffs_setblock(fs, cg_blksfree(cgp, needswap), fragno);
                ffs_clusteracct(fs, cgp, fragno, 1);
                ffs_clusteracct(fs, cgp, fragno, 1);
                ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
                ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
                fs->fs_cstotal.cs_nbfree++;
                fs->fs_cstotal.cs_nbfree++;
                fs->fs_cs(fs, cg).cs_nbfree++;
                fs->fs_cs(fs, cg).cs_nbfree++;
        } else {
        } else {
                bbase = cgbno - fragnum(fs, cgbno);
                bbase = cgbno - fragnum(fs, cgbno);
                /*
                /*
                 * decrement the counts associated with the old frags
                 * decrement the counts associated with the old frags
                 */
                 */
                blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
                blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
                ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
                ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
                /*
                /*
                 * deallocate the fragment
                 * deallocate the fragment
                 */
                 */
                frags = numfrags(fs, size);
                frags = numfrags(fs, size);
                for (i = 0; i < frags; i++) {
                for (i = 0; i < frags; i++) {
                        if (isset(cg_blksfree(cgp, needswap), cgbno + i)) {
                        if (isset(cg_blksfree(cgp, needswap), cgbno + i)) {
                                errx(1, "blkfree: freeing free frag: block %lld",
                                errx(1, "blkfree: freeing free frag: block %lld",
                                    (long long)(cgbno + i));
                                    (long long)(cgbno + i));
                        }
                        }
                        setbit(cg_blksfree(cgp, needswap), cgbno + i);
                        setbit(cg_blksfree(cgp, needswap), cgbno + i);
                }
                }
                ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
                ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
                fs->fs_cstotal.cs_nffree += i;
                fs->fs_cstotal.cs_nffree += i;
                fs->fs_cs(fs, cg).cs_nffree += i;
                fs->fs_cs(fs, cg).cs_nffree += i;
                /*
                /*
                 * add back in counts associated with the new frags
                 * add back in counts associated with the new frags
                 */
                 */
                blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
                blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
                ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
                ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
                /*
                /*
                 * if a complete block has been reassembled, account for it
                 * if a complete block has been reassembled, account for it
                 */
                 */
                fragno = fragstoblks(fs, bbase);
                fragno = fragstoblks(fs, bbase);
                if (ffs_isblock(fs, cg_blksfree(cgp, needswap), fragno)) {
                if (ffs_isblock(fs, cg_blksfree(cgp, needswap), fragno)) {
                        ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
                        ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
                        fs->fs_cstotal.cs_nffree -= fs->fs_frag;
                        fs->fs_cstotal.cs_nffree -= fs->fs_frag;
                        fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
                        fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
                        ffs_clusteracct(fs, cgp, fragno, 1);
                        ffs_clusteracct(fs, cgp, fragno, 1);
                        ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
                        ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
                        fs->fs_cstotal.cs_nbfree++;
                        fs->fs_cstotal.cs_nbfree++;
                        fs->fs_cs(fs, cg).cs_nbfree++;
                        fs->fs_cs(fs, cg).cs_nbfree++;
                }
                }
        }
        }
        fs->fs_fmod = 1;
        fs->fs_fmod = 1;
        bdwrite(bp);
        bdwrite(bp);
}
}
 
 
 
 
static int
static int
scanc(u_int size, const u_char *cp, const u_char table[], int mask)
scanc(u_int size, const u_char *cp, const u_char table[], int mask)
{
{
        const u_char *end = &cp[size];
        const u_char *end = &cp[size];
 
 
        while (cp < end && (table[*cp] & mask) == 0)
        while (cp < end && (table[*cp] & mask) == 0)
                cp++;
                cp++;
        return (end - cp);
        return (end - cp);
}
}
 
 
/*
/*
 * Find a block of the specified size in the specified cylinder group.
 * Find a block of the specified size in the specified cylinder group.
 *
 *
 * It is a panic if a request is made to find a block if none are
 * It is a panic if a request is made to find a block if none are
 * available.
 * available.
 */
 */
static int32_t
static int32_t
ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
{
{
        int32_t bno;
        int32_t bno;
        int start, len, loc, i;
        int start, len, loc, i;
        int blk, field, subfield, pos;
        int blk, field, subfield, pos;
        int ostart, olen;
        int ostart, olen;
        const int needswap = UFS_FSNEEDSWAP(fs);
        const int needswap = UFS_FSNEEDSWAP(fs);
 
 
        /*
        /*
         * find the fragment by searching through the free block
         * find the fragment by searching through the free block
         * map for an appropriate bit pattern
         * map for an appropriate bit pattern
         */
         */
        if (bpref)
        if (bpref)
                start = dtogd(fs, bpref) / NBBY;
                start = dtogd(fs, bpref) / NBBY;
        else
        else
                start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
                start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
        len = howmany(fs->fs_fpg, NBBY) - start;
        len = howmany(fs->fs_fpg, NBBY) - start;
        ostart = start;
        ostart = start;
        olen = len;
        olen = len;
        loc = scanc((u_int)len,
        loc = scanc((u_int)len,
                (const u_char *)&cg_blksfree(cgp, needswap)[start],
                (const u_char *)&cg_blksfree(cgp, needswap)[start],
                (const u_char *)fragtbl[fs->fs_frag],
                (const u_char *)fragtbl[fs->fs_frag],
                (1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
                (1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
        if (loc == 0) {
        if (loc == 0) {
                len = start + 1;
                len = start + 1;
                start = 0;
                start = 0;
                loc = scanc((u_int)len,
                loc = scanc((u_int)len,
                        (const u_char *)&cg_blksfree(cgp, needswap)[0],
                        (const u_char *)&cg_blksfree(cgp, needswap)[0],
                        (const u_char *)fragtbl[fs->fs_frag],
                        (const u_char *)fragtbl[fs->fs_frag],
                        (1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
                        (1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
                if (loc == 0) {
                if (loc == 0) {
                        errx(1,
                        errx(1,
    "ffs_alloccg: map corrupted: start %d len %d offset %d %ld",
    "ffs_alloccg: map corrupted: start %d len %d offset %d %ld",
                                ostart, olen,
                                ostart, olen,
                                ufs_rw32(cgp->cg_freeoff, needswap),
                                ufs_rw32(cgp->cg_freeoff, needswap),
                                (long)cg_blksfree(cgp, needswap) - (long)cgp);
                                (long)cg_blksfree(cgp, needswap) - (long)cgp);
                        /* NOTREACHED */
                        /* NOTREACHED */
                }
                }
        }
        }
        bno = (start + len - loc) * NBBY;
        bno = (start + len - loc) * NBBY;
        cgp->cg_frotor = ufs_rw32(bno, needswap);
        cgp->cg_frotor = ufs_rw32(bno, needswap);
        /*
        /*
         * found the byte in the map
         * found the byte in the map
         * sift through the bits to find the selected frag
         * sift through the bits to find the selected frag
         */
         */
        for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
        for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
                blk = blkmap(fs, cg_blksfree(cgp, needswap), bno);
                blk = blkmap(fs, cg_blksfree(cgp, needswap), bno);
                blk <<= 1;
                blk <<= 1;
                field = around[allocsiz];
                field = around[allocsiz];
                subfield = inside[allocsiz];
                subfield = inside[allocsiz];
                for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
                for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
                        if ((blk & field) == subfield)
                        if ((blk & field) == subfield)
                                return (bno + pos);
                                return (bno + pos);
                        field <<= 1;
                        field <<= 1;
                        subfield <<= 1;
                        subfield <<= 1;
                }
                }
        }
        }
        errx(1, "ffs_alloccg: block not in map: bno %lld", (long long)bno);
        errx(1, "ffs_alloccg: block not in map: bno %lld", (long long)bno);
        return (-1);
        return (-1);
}
}
 
 

powered by: WebSVN 2.1.0

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