[BACK]Return to ffs_alloc.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / ufs / ffs

Annotation of src/sys/ufs/ffs/ffs_alloc.c, Revision 1.99.6.1

1.99.6.1! jmcneill    1: /*     $NetBSD: ffs_alloc.c,v 1.100 2007/08/09 07:34:28 hannken Exp $  */
1.2       cgd         2:
1.1       mycroft     3: /*
1.60      fvdl        4:  * Copyright (c) 2002 Networks Associates Technology, Inc.
                      5:  * All rights reserved.
                      6:  *
                      7:  * This software was developed for the FreeBSD Project by Marshall
                      8:  * Kirk McKusick and Network Associates Laboratories, the Security
                      9:  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
                     10:  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
                     11:  * research program
                     12:  *
1.1       mycroft    13:  * Copyright (c) 1982, 1986, 1989, 1993
                     14:  *     The Regents of the University of California.  All rights reserved.
                     15:  *
                     16:  * Redistribution and use in source and binary forms, with or without
                     17:  * modification, are permitted provided that the following conditions
                     18:  * are met:
                     19:  * 1. Redistributions of source code must retain the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer.
                     21:  * 2. Redistributions in binary form must reproduce the above copyright
                     22:  *    notice, this list of conditions and the following disclaimer in the
                     23:  *    documentation and/or other materials provided with the distribution.
1.69      agc        24:  * 3. Neither the name of the University nor the names of its contributors
1.1       mycroft    25:  *    may be used to endorse or promote products derived from this software
                     26:  *    without specific prior written permission.
                     27:  *
                     28:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     29:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     30:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     31:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     32:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     33:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     34:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     35:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     36:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     37:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     38:  * SUCH DAMAGE.
                     39:  *
1.18      fvdl       40:  *     @(#)ffs_alloc.c 8.19 (Berkeley) 7/13/95
1.1       mycroft    41:  */
1.53      lukem      42:
                     43: #include <sys/cdefs.h>
1.99.6.1! jmcneill   44: __KERNEL_RCSID(0, "$NetBSD: ffs_alloc.c,v 1.100 2007/08/09 07:34:28 hannken Exp $");
1.17      mrg        45:
1.43      mrg        46: #if defined(_KERNEL_OPT)
1.27      thorpej    47: #include "opt_ffs.h"
1.21      scottr     48: #include "opt_quota.h"
1.22      scottr     49: #endif
1.1       mycroft    50:
                     51: #include <sys/param.h>
                     52: #include <sys/systm.h>
                     53: #include <sys/buf.h>
                     54: #include <sys/proc.h>
                     55: #include <sys/vnode.h>
                     56: #include <sys/mount.h>
                     57: #include <sys/kernel.h>
                     58: #include <sys/syslog.h>
1.91      elad       59: #include <sys/kauth.h>
1.29      mrg        60:
1.76      hannken    61: #include <miscfs/specfs/specdev.h>
1.1       mycroft    62: #include <ufs/ufs/quota.h>
1.19      bouyer     63: #include <ufs/ufs/ufsmount.h>
1.1       mycroft    64: #include <ufs/ufs/inode.h>
1.9       christos   65: #include <ufs/ufs/ufs_extern.h>
1.19      bouyer     66: #include <ufs/ufs/ufs_bswap.h>
1.1       mycroft    67:
                     68: #include <ufs/ffs/fs.h>
                     69: #include <ufs/ffs/ffs_extern.h>
                     70:
1.85      thorpej    71: static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int);
                     72: static daddr_t ffs_alloccgblk(struct inode *, struct buf *, daddr_t);
1.55      matt       73: #ifdef XXXUBC
1.85      thorpej    74: static daddr_t ffs_clusteralloc(struct inode *, int, daddr_t, int);
1.55      matt       75: #endif
1.85      thorpej    76: static ino_t ffs_dirpref(struct inode *);
                     77: static daddr_t ffs_fragextend(struct inode *, int, daddr_t, int, int);
                     78: static void ffs_fserr(struct fs *, u_int, const char *);
                     79: static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int,
                     80:     daddr_t (*)(struct inode *, int, daddr_t, int));
                     81: static daddr_t ffs_nodealloccg(struct inode *, int, daddr_t, int);
                     82: static int32_t ffs_mapsearch(struct fs *, struct cg *,
                     83:                                      daddr_t, int);
1.18      fvdl       84: #if defined(DIAGNOSTIC) || defined(DEBUG)
1.55      matt       85: #ifdef XXXUBC
1.85      thorpej    86: static int ffs_checkblk(struct inode *, daddr_t, long size);
1.18      fvdl       87: #endif
1.55      matt       88: #endif
1.23      drochner   89:
1.34      jdolecek   90: /* if 1, changes in optimalization strategy are logged */
                     91: int ffs_log_changeopt = 0;
                     92:
1.23      drochner   93: /* in ffs_tables.c */
1.40      jdolecek   94: extern const int inside[], around[];
                     95: extern const u_char * const fragtbl[];
1.1       mycroft    96:
                     97: /*
                     98:  * Allocate a block in the file system.
1.81      perry      99:  *
1.1       mycroft   100:  * The size of the requested block is given, which must be some
                    101:  * multiple of fs_fsize and <= fs_bsize.
                    102:  * A preference may be optionally specified. If a preference is given
                    103:  * the following hierarchy is used to allocate a block:
                    104:  *   1) allocate the requested block.
                    105:  *   2) allocate a rotationally optimal block in the same cylinder.
                    106:  *   3) allocate a block in the same cylinder group.
                    107:  *   4) quadradically rehash into other cylinder groups, until an
                    108:  *      available block is located.
1.47      wiz       109:  * If no block preference is given the following hierarchy is used
1.1       mycroft   110:  * to allocate a block:
                    111:  *   1) allocate a block in the cylinder group that contains the
                    112:  *      inode for the file.
                    113:  *   2) quadradically rehash into other cylinder groups, until an
                    114:  *      available block is located.
                    115:  */
1.9       christos  116: int
1.96      christos  117: ffs_alloc(struct inode *ip, daddr_t lbn, daddr_t bpref, int size,
1.91      elad      118:     kauth_cred_t cred, daddr_t *bnp)
1.1       mycroft   119: {
1.62      fvdl      120:        struct fs *fs;
1.58      fvdl      121:        daddr_t bno;
1.9       christos  122:        int cg;
                    123: #ifdef QUOTA
                    124:        int error;
                    125: #endif
1.81      perry     126:
1.62      fvdl      127:        fs = ip->i_fs;
                    128:
1.37      chs       129: #ifdef UVM_PAGE_TRKOWN
1.51      chs       130:        if (ITOV(ip)->v_type == VREG &&
                    131:            lblktosize(fs, (voff_t)lbn) < round_page(ITOV(ip)->v_size)) {
1.37      chs       132:                struct vm_page *pg;
1.51      chs       133:                struct uvm_object *uobj = &ITOV(ip)->v_uobj;
1.49      lukem     134:                voff_t off = trunc_page(lblktosize(fs, lbn));
                    135:                voff_t endoff = round_page(lblktosize(fs, lbn) + size);
1.37      chs       136:
                    137:                simple_lock(&uobj->vmobjlock);
                    138:                while (off < endoff) {
                    139:                        pg = uvm_pagelookup(uobj, off);
                    140:                        KASSERT(pg != NULL);
                    141:                        KASSERT(pg->owner == curproc->p_pid);
                    142:                        off += PAGE_SIZE;
                    143:                }
                    144:                simple_unlock(&uobj->vmobjlock);
                    145:        }
                    146: #endif
                    147:
1.1       mycroft   148:        *bnp = 0;
                    149: #ifdef DIAGNOSTIC
                    150:        if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
1.13      christos  151:                printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
1.1       mycroft   152:                    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
                    153:                panic("ffs_alloc: bad size");
                    154:        }
                    155:        if (cred == NOCRED)
1.56      provos    156:                panic("ffs_alloc: missing credential");
1.1       mycroft   157: #endif /* DIAGNOSTIC */
                    158:        if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
                    159:                goto nospace;
1.99      pooka     160:        if (freespace(fs, fs->fs_minfree) <= 0 &&
                    161:            kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0)
1.1       mycroft   162:                goto nospace;
                    163: #ifdef QUOTA
1.60      fvdl      164:        if ((error = chkdq(ip, btodb(size), cred, 0)) != 0)
1.1       mycroft   165:                return (error);
                    166: #endif
                    167:        if (bpref >= fs->fs_size)
                    168:                bpref = 0;
                    169:        if (bpref == 0)
                    170:                cg = ino_to_cg(fs, ip->i_number);
                    171:        else
                    172:                cg = dtog(fs, bpref);
1.84      dbj       173:        bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
1.1       mycroft   174:        if (bno > 0) {
1.65      kristerw  175:                DIP_ADD(ip, blocks, btodb(size));
1.1       mycroft   176:                ip->i_flag |= IN_CHANGE | IN_UPDATE;
                    177:                *bnp = bno;
                    178:                return (0);
                    179:        }
                    180: #ifdef QUOTA
                    181:        /*
                    182:         * Restore user's disk quota because allocation failed.
                    183:         */
1.60      fvdl      184:        (void) chkdq(ip, -btodb(size), cred, FORCE);
1.1       mycroft   185: #endif
                    186: nospace:
1.91      elad      187:        ffs_fserr(fs, kauth_cred_geteuid(cred), "file system full");
1.1       mycroft   188:        uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
                    189:        return (ENOSPC);
                    190: }
                    191:
                    192: /*
                    193:  * Reallocate a fragment to a bigger size
                    194:  *
                    195:  * The number and size of the old block is given, and a preference
                    196:  * and new size is also specified. The allocator attempts to extend
                    197:  * the original block. Failing that, the regular block allocator is
                    198:  * invoked to get an appropriate block.
                    199:  */
1.9       christos  200: int
1.85      thorpej   201: ffs_realloccg(struct inode *ip, daddr_t lbprev, daddr_t bpref, int osize,
1.91      elad      202:     int nsize, kauth_cred_t cred, struct buf **bpp, daddr_t *blknop)
1.1       mycroft   203: {
1.62      fvdl      204:        struct fs *fs;
1.1       mycroft   205:        struct buf *bp;
                    206:        int cg, request, error;
1.58      fvdl      207:        daddr_t bprev, bno;
1.25      thorpej   208:
1.62      fvdl      209:        fs = ip->i_fs;
1.37      chs       210: #ifdef UVM_PAGE_TRKOWN
                    211:        if (ITOV(ip)->v_type == VREG) {
                    212:                struct vm_page *pg;
1.51      chs       213:                struct uvm_object *uobj = &ITOV(ip)->v_uobj;
1.49      lukem     214:                voff_t off = trunc_page(lblktosize(fs, lbprev));
                    215:                voff_t endoff = round_page(lblktosize(fs, lbprev) + osize);
1.37      chs       216:
                    217:                simple_lock(&uobj->vmobjlock);
                    218:                while (off < endoff) {
                    219:                        pg = uvm_pagelookup(uobj, off);
                    220:                        KASSERT(pg != NULL);
                    221:                        KASSERT(pg->owner == curproc->p_pid);
                    222:                        KASSERT((pg->flags & PG_CLEAN) == 0);
                    223:                        off += PAGE_SIZE;
                    224:                }
                    225:                simple_unlock(&uobj->vmobjlock);
                    226:        }
                    227: #endif
                    228:
1.1       mycroft   229: #ifdef DIAGNOSTIC
                    230:        if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
                    231:            (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1.13      christos  232:                printf(
1.1       mycroft   233:                    "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
                    234:                    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
                    235:                panic("ffs_realloccg: bad size");
                    236:        }
                    237:        if (cred == NOCRED)
1.56      provos    238:                panic("ffs_realloccg: missing credential");
1.1       mycroft   239: #endif /* DIAGNOSTIC */
1.99      pooka     240:        if (freespace(fs, fs->fs_minfree) <= 0 &&
                    241:            kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL) != 0)
1.1       mycroft   242:                goto nospace;
1.60      fvdl      243:        if (fs->fs_magic == FS_UFS2_MAGIC)
                    244:                bprev = ufs_rw64(ip->i_ffs2_db[lbprev], UFS_FSNEEDSWAP(fs));
                    245:        else
                    246:                bprev = ufs_rw32(ip->i_ffs1_db[lbprev], UFS_FSNEEDSWAP(fs));
                    247:
                    248:        if (bprev == 0) {
1.59      tsutsui   249:                printf("dev = 0x%x, bsize = %d, bprev = %" PRId64 ", fs = %s\n",
                    250:                    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1.1       mycroft   251:                panic("ffs_realloccg: bad bprev");
                    252:        }
                    253:        /*
                    254:         * Allocate the extra space in the buffer.
                    255:         */
1.37      chs       256:        if (bpp != NULL &&
                    257:            (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) != 0) {
1.1       mycroft   258:                brelse(bp);
                    259:                return (error);
                    260:        }
                    261: #ifdef QUOTA
1.60      fvdl      262:        if ((error = chkdq(ip, btodb(nsize - osize), cred, 0)) != 0) {
1.44      chs       263:                if (bpp != NULL) {
                    264:                        brelse(bp);
                    265:                }
1.1       mycroft   266:                return (error);
                    267:        }
                    268: #endif
                    269:        /*
                    270:         * Check for extension in the existing location.
                    271:         */
                    272:        cg = dtog(fs, bprev);
1.60      fvdl      273:        if ((bno = ffs_fragextend(ip, cg, bprev, osize, nsize)) != 0) {
1.65      kristerw  274:                DIP_ADD(ip, blocks, btodb(nsize - osize));
1.1       mycroft   275:                ip->i_flag |= IN_CHANGE | IN_UPDATE;
1.37      chs       276:
                    277:                if (bpp != NULL) {
                    278:                        if (bp->b_blkno != fsbtodb(fs, bno))
                    279:                                panic("bad blockno");
1.72      pk        280:                        allocbuf(bp, nsize, 1);
1.37      chs       281:                        bp->b_flags |= B_DONE;
1.98      christos  282:                        memset((char *)bp->b_data + osize, 0, nsize - osize);
1.37      chs       283:                        *bpp = bp;
                    284:                }
                    285:                if (blknop != NULL) {
                    286:                        *blknop = bno;
                    287:                }
1.1       mycroft   288:                return (0);
                    289:        }
                    290:        /*
                    291:         * Allocate a new disk location.
                    292:         */
                    293:        if (bpref >= fs->fs_size)
                    294:                bpref = 0;
                    295:        switch ((int)fs->fs_optim) {
                    296:        case FS_OPTSPACE:
                    297:                /*
1.81      perry     298:                 * Allocate an exact sized fragment. Although this makes
                    299:                 * best use of space, we will waste time relocating it if
1.1       mycroft   300:                 * the file continues to grow. If the fragmentation is
                    301:                 * less than half of the minimum free reserve, we choose
                    302:                 * to begin optimizing for time.
                    303:                 */
                    304:                request = nsize;
                    305:                if (fs->fs_minfree < 5 ||
                    306:                    fs->fs_cstotal.cs_nffree >
                    307:                    fs->fs_dsize * fs->fs_minfree / (2 * 100))
                    308:                        break;
1.34      jdolecek  309:
                    310:                if (ffs_log_changeopt) {
                    311:                        log(LOG_NOTICE,
                    312:                                "%s: optimization changed from SPACE to TIME\n",
                    313:                                fs->fs_fsmnt);
                    314:                }
                    315:
1.1       mycroft   316:                fs->fs_optim = FS_OPTTIME;
                    317:                break;
                    318:        case FS_OPTTIME:
                    319:                /*
                    320:                 * At this point we have discovered a file that is trying to
                    321:                 * grow a small fragment to a larger fragment. To save time,
                    322:                 * we allocate a full sized block, then free the unused portion.
                    323:                 * If the file continues to grow, the `ffs_fragextend' call
                    324:                 * above will be able to grow it in place without further
                    325:                 * copying. If aberrant programs cause disk fragmentation to
                    326:                 * grow within 2% of the free reserve, we choose to begin
                    327:                 * optimizing for space.
                    328:                 */
                    329:                request = fs->fs_bsize;
                    330:                if (fs->fs_cstotal.cs_nffree <
                    331:                    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
                    332:                        break;
1.34      jdolecek  333:
                    334:                if (ffs_log_changeopt) {
                    335:                        log(LOG_NOTICE,
                    336:                                "%s: optimization changed from TIME to SPACE\n",
                    337:                                fs->fs_fsmnt);
                    338:                }
                    339:
1.1       mycroft   340:                fs->fs_optim = FS_OPTSPACE;
                    341:                break;
                    342:        default:
1.13      christos  343:                printf("dev = 0x%x, optim = %d, fs = %s\n",
1.1       mycroft   344:                    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
                    345:                panic("ffs_realloccg: bad optim");
                    346:                /* NOTREACHED */
                    347:        }
1.58      fvdl      348:        bno = ffs_hashalloc(ip, cg, bpref, request, ffs_alloccg);
1.1       mycroft   349:        if (bno > 0) {
1.30      fvdl      350:                if (!DOINGSOFTDEP(ITOV(ip)))
1.76      hannken   351:                        ffs_blkfree(fs, ip->i_devvp, bprev, (long)osize,
                    352:                            ip->i_number);
1.1       mycroft   353:                if (nsize < request)
1.76      hannken   354:                        ffs_blkfree(fs, ip->i_devvp, bno + numfrags(fs, nsize),
                    355:                            (long)(request - nsize), ip->i_number);
1.65      kristerw  356:                DIP_ADD(ip, blocks, btodb(nsize - osize));
1.1       mycroft   357:                ip->i_flag |= IN_CHANGE | IN_UPDATE;
1.37      chs       358:                if (bpp != NULL) {
                    359:                        bp->b_blkno = fsbtodb(fs, bno);
1.72      pk        360:                        allocbuf(bp, nsize, 1);
1.37      chs       361:                        bp->b_flags |= B_DONE;
1.98      christos  362:                        memset((char *)bp->b_data + osize, 0, (u_int)nsize - osize);
1.37      chs       363:                        *bpp = bp;
                    364:                }
                    365:                if (blknop != NULL) {
                    366:                        *blknop = bno;
                    367:                }
1.1       mycroft   368:                return (0);
                    369:        }
                    370: #ifdef QUOTA
                    371:        /*
                    372:         * Restore user's disk quota because allocation failed.
                    373:         */
1.60      fvdl      374:        (void) chkdq(ip, -btodb(nsize - osize), cred, FORCE);
1.1       mycroft   375: #endif
1.37      chs       376:        if (bpp != NULL) {
                    377:                brelse(bp);
                    378:        }
                    379:
1.1       mycroft   380: nospace:
                    381:        /*
                    382:         * no space available
                    383:         */
1.91      elad      384:        ffs_fserr(fs, kauth_cred_geteuid(cred), "file system full");
1.1       mycroft   385:        uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
                    386:        return (ENOSPC);
                    387: }
                    388:
1.88      yamt      389: #if 0
1.1       mycroft   390: /*
                    391:  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
                    392:  *
                    393:  * The vnode and an array of buffer pointers for a range of sequential
                    394:  * logical blocks to be made contiguous is given. The allocator attempts
1.60      fvdl      395:  * to find a range of sequential blocks starting as close as possible
                    396:  * from the end of the allocation for the logical block immediately
                    397:  * preceding the current range. If successful, the physical block numbers
                    398:  * in the buffer pointers and in the inode are changed to reflect the new
                    399:  * allocation. If unsuccessful, the allocation is left unchanged. The
                    400:  * success in doing the reallocation is returned. Note that the error
                    401:  * return is not reflected back to the user. Rather the previous block
                    402:  * allocation will be used.
                    403:
1.1       mycroft   404:  */
1.55      matt      405: #ifdef XXXUBC
1.3       mycroft   406: #ifdef DEBUG
1.1       mycroft   407: #include <sys/sysctl.h>
1.5       mycroft   408: int prtrealloc = 0;
                    409: struct ctldebug debug15 = { "prtrealloc", &prtrealloc };
1.1       mycroft   410: #endif
1.55      matt      411: #endif
1.1       mycroft   412:
1.60      fvdl      413: /*
                    414:  * NOTE: when re-enabling this, it must be updated for UFS2.
                    415:  */
                    416:
1.18      fvdl      417: int doasyncfree = 1;
                    418:
1.1       mycroft   419: int
1.85      thorpej   420: ffs_reallocblks(void *v)
1.9       christos  421: {
1.55      matt      422: #ifdef XXXUBC
1.1       mycroft   423:        struct vop_reallocblks_args /* {
                    424:                struct vnode *a_vp;
                    425:                struct cluster_save *a_buflist;
1.9       christos  426:        } */ *ap = v;
1.1       mycroft   427:        struct fs *fs;
                    428:        struct inode *ip;
                    429:        struct vnode *vp;
                    430:        struct buf *sbp, *ebp;
1.58      fvdl      431:        int32_t *bap, *ebap = NULL, *sbap;      /* XXX ondisk32 */
1.1       mycroft   432:        struct cluster_save *buflist;
1.58      fvdl      433:        daddr_t start_lbn, end_lbn, soff, newblk, blkno;
1.1       mycroft   434:        struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
                    435:        int i, len, start_lvl, end_lvl, pref, ssize;
1.55      matt      436: #endif /* XXXUBC */
1.1       mycroft   437:
1.37      chs       438:        /* XXXUBC don't reallocblks for now */
                    439:        return ENOSPC;
                    440:
1.55      matt      441: #ifdef XXXUBC
1.1       mycroft   442:        vp = ap->a_vp;
                    443:        ip = VTOI(vp);
                    444:        fs = ip->i_fs;
                    445:        if (fs->fs_contigsumsize <= 0)
                    446:                return (ENOSPC);
                    447:        buflist = ap->a_buflist;
                    448:        len = buflist->bs_nchildren;
                    449:        start_lbn = buflist->bs_children[0]->b_lblkno;
                    450:        end_lbn = start_lbn + len - 1;
                    451: #ifdef DIAGNOSTIC
1.18      fvdl      452:        for (i = 0; i < len; i++)
                    453:                if (!ffs_checkblk(ip,
                    454:                   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
                    455:                        panic("ffs_reallocblks: unallocated block 1");
1.1       mycroft   456:        for (i = 1; i < len; i++)
                    457:                if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
1.18      fvdl      458:                        panic("ffs_reallocblks: non-logical cluster");
                    459:        blkno = buflist->bs_children[0]->b_blkno;
                    460:        ssize = fsbtodb(fs, fs->fs_frag);
                    461:        for (i = 1; i < len - 1; i++)
                    462:                if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
                    463:                        panic("ffs_reallocblks: non-physical cluster %d", i);
1.1       mycroft   464: #endif
                    465:        /*
                    466:         * If the latest allocation is in a new cylinder group, assume that
                    467:         * the filesystem has decided to move and do not force it back to
                    468:         * the previous cylinder group.
                    469:         */
                    470:        if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
                    471:            dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
                    472:                return (ENOSPC);
                    473:        if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
                    474:            ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
                    475:                return (ENOSPC);
                    476:        /*
                    477:         * Get the starting offset and block map for the first block.
                    478:         */
                    479:        if (start_lvl == 0) {
1.60      fvdl      480:                sbap = &ip->i_ffs1_db[0];
1.1       mycroft   481:                soff = start_lbn;
                    482:        } else {
                    483:                idp = &start_ap[start_lvl - 1];
                    484:                if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
                    485:                        brelse(sbp);
                    486:                        return (ENOSPC);
                    487:                }
1.60      fvdl      488:                sbap = (int32_t *)sbp->b_data;
1.1       mycroft   489:                soff = idp->in_off;
                    490:        }
                    491:        /*
                    492:         * Find the preferred location for the cluster.
                    493:         */
                    494:        pref = ffs_blkpref(ip, start_lbn, soff, sbap);
                    495:        /*
                    496:         * If the block range spans two block maps, get the second map.
                    497:         */
                    498:        if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
                    499:                ssize = len;
                    500:        } else {
                    501: #ifdef DIAGNOSTIC
                    502:                if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
                    503:                        panic("ffs_reallocblk: start == end");
                    504: #endif
                    505:                ssize = len - (idp->in_off + 1);
                    506:                if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
                    507:                        goto fail;
1.58      fvdl      508:                ebap = (int32_t *)ebp->b_data;  /* XXX ondisk32 */
1.1       mycroft   509:        }
                    510:        /*
                    511:         * Search the block map looking for an allocation of the desired size.
                    512:         */
1.58      fvdl      513:        if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
1.9       christos  514:            len, ffs_clusteralloc)) == 0)
1.1       mycroft   515:                goto fail;
                    516:        /*
                    517:         * We have found a new contiguous block.
                    518:         *
                    519:         * First we have to replace the old block pointers with the new
                    520:         * block pointers in the inode and indirect blocks associated
                    521:         * with the file.
                    522:         */
1.5       mycroft   523: #ifdef DEBUG
                    524:        if (prtrealloc)
1.13      christos  525:                printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
1.5       mycroft   526:                    start_lbn, end_lbn);
                    527: #endif
1.1       mycroft   528:        blkno = newblk;
                    529:        for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
1.58      fvdl      530:                daddr_t ba;
1.30      fvdl      531:
                    532:                if (i == ssize) {
1.1       mycroft   533:                        bap = ebap;
1.30      fvdl      534:                        soff = -i;
                    535:                }
1.58      fvdl      536:                /* XXX ondisk32 */
1.30      fvdl      537:                ba = ufs_rw32(*bap, UFS_FSNEEDSWAP(fs));
1.1       mycroft   538: #ifdef DIAGNOSTIC
1.18      fvdl      539:                if (!ffs_checkblk(ip,
                    540:                   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
                    541:                        panic("ffs_reallocblks: unallocated block 2");
1.30      fvdl      542:                if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != ba)
1.1       mycroft   543:                        panic("ffs_reallocblks: alloc mismatch");
                    544: #endif
1.5       mycroft   545: #ifdef DEBUG
                    546:                if (prtrealloc)
1.30      fvdl      547:                        printf(" %d,", ba);
1.5       mycroft   548: #endif
1.30      fvdl      549:                if (DOINGSOFTDEP(vp)) {
1.60      fvdl      550:                        if (sbap == &ip->i_ffs1_db[0] && i < ssize)
1.30      fvdl      551:                                softdep_setup_allocdirect(ip, start_lbn + i,
                    552:                                    blkno, ba, fs->fs_bsize, fs->fs_bsize,
                    553:                                    buflist->bs_children[i]);
                    554:                        else
                    555:                                softdep_setup_allocindir_page(ip, start_lbn + i,
                    556:                                    i < ssize ? sbp : ebp, soff + i, blkno,
                    557:                                    ba, buflist->bs_children[i]);
                    558:                }
1.58      fvdl      559:                /* XXX ondisk32 */
1.80      mycroft   560:                *bap++ = ufs_rw32((u_int32_t)blkno, UFS_FSNEEDSWAP(fs));
1.1       mycroft   561:        }
                    562:        /*
                    563:         * Next we must write out the modified inode and indirect blocks.
                    564:         * For strict correctness, the writes should be synchronous since
                    565:         * the old block values may have been written to disk. In practise
1.81      perry     566:         * they are almost never written, but if we are concerned about
1.1       mycroft   567:         * strict correctness, the `doasyncfree' flag should be set to zero.
                    568:         *
                    569:         * The test on `doasyncfree' should be changed to test a flag
                    570:         * that shows whether the associated buffers and inodes have
                    571:         * been written. The flag should be set when the cluster is
                    572:         * started and cleared whenever the buffer or inode is flushed.
                    573:         * We can then check below to see if it is set, and do the
                    574:         * synchronous write only when it has been cleared.
                    575:         */
1.60      fvdl      576:        if (sbap != &ip->i_ffs1_db[0]) {
1.1       mycroft   577:                if (doasyncfree)
                    578:                        bdwrite(sbp);
                    579:                else
                    580:                        bwrite(sbp);
                    581:        } else {
                    582:                ip->i_flag |= IN_CHANGE | IN_UPDATE;
1.28      mycroft   583:                if (!doasyncfree)
1.88      yamt      584:                        ffs_update(vp, NULL, NULL, 1);
1.1       mycroft   585:        }
1.25      thorpej   586:        if (ssize < len) {
1.1       mycroft   587:                if (doasyncfree)
                    588:                        bdwrite(ebp);
                    589:                else
                    590:                        bwrite(ebp);
1.25      thorpej   591:        }
1.1       mycroft   592:        /*
                    593:         * Last, free the old blocks and assign the new blocks to the buffers.
                    594:         */
1.5       mycroft   595: #ifdef DEBUG
                    596:        if (prtrealloc)
1.13      christos  597:                printf("\n\tnew:");
1.5       mycroft   598: #endif
1.1       mycroft   599:        for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
1.30      fvdl      600:                if (!DOINGSOFTDEP(vp))
1.76      hannken   601:                        ffs_blkfree(fs, ip->i_devvp,
1.30      fvdl      602:                            dbtofsb(fs, buflist->bs_children[i]->b_blkno),
1.76      hannken   603:                            fs->fs_bsize, ip->i_number);
1.1       mycroft   604:                buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
1.5       mycroft   605: #ifdef DEBUG
1.18      fvdl      606:                if (!ffs_checkblk(ip,
                    607:                   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
                    608:                        panic("ffs_reallocblks: unallocated block 3");
1.5       mycroft   609:                if (prtrealloc)
1.13      christos  610:                        printf(" %d,", blkno);
1.5       mycroft   611: #endif
                    612:        }
                    613: #ifdef DEBUG
                    614:        if (prtrealloc) {
                    615:                prtrealloc--;
1.13      christos  616:                printf("\n");
1.1       mycroft   617:        }
1.5       mycroft   618: #endif
1.1       mycroft   619:        return (0);
                    620:
                    621: fail:
                    622:        if (ssize < len)
                    623:                brelse(ebp);
1.60      fvdl      624:        if (sbap != &ip->i_ffs1_db[0])
1.1       mycroft   625:                brelse(sbp);
                    626:        return (ENOSPC);
1.55      matt      627: #endif /* XXXUBC */
1.1       mycroft   628: }
1.88      yamt      629: #endif /* 0 */
1.1       mycroft   630:
                    631: /*
                    632:  * Allocate an inode in the file system.
1.81      perry     633:  *
1.1       mycroft   634:  * If allocating a directory, use ffs_dirpref to select the inode.
                    635:  * If allocating in a directory, the following hierarchy is followed:
                    636:  *   1) allocate the preferred inode.
                    637:  *   2) allocate an inode in the same cylinder group.
                    638:  *   3) quadradically rehash into other cylinder groups, until an
                    639:  *      available inode is located.
1.47      wiz       640:  * If no inode preference is given the following hierarchy is used
1.1       mycroft   641:  * to allocate an inode:
                    642:  *   1) allocate an inode in cylinder group 0.
                    643:  *   2) quadradically rehash into other cylinder groups, until an
                    644:  *      available inode is located.
                    645:  */
1.9       christos  646: int
1.91      elad      647: ffs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
1.88      yamt      648:     struct vnode **vpp)
1.9       christos  649: {
1.33      augustss  650:        struct inode *pip;
                    651:        struct fs *fs;
                    652:        struct inode *ip;
1.60      fvdl      653:        struct timespec ts;
1.1       mycroft   654:        ino_t ino, ipref;
                    655:        int cg, error;
1.81      perry     656:
1.88      yamt      657:        *vpp = NULL;
1.1       mycroft   658:        pip = VTOI(pvp);
                    659:        fs = pip->i_fs;
                    660:        if (fs->fs_cstotal.cs_nifree == 0)
                    661:                goto noinodes;
                    662:
                    663:        if ((mode & IFMT) == IFDIR)
1.50      lukem     664:                ipref = ffs_dirpref(pip);
                    665:        else
                    666:                ipref = pip->i_number;
1.1       mycroft   667:        if (ipref >= fs->fs_ncg * fs->fs_ipg)
                    668:                ipref = 0;
                    669:        cg = ino_to_cg(fs, ipref);
1.50      lukem     670:        /*
                    671:         * Track number of dirs created one after another
                    672:         * in a same cg without intervening by files.
                    673:         */
                    674:        if ((mode & IFMT) == IFDIR) {
1.63      fvdl      675:                if (fs->fs_contigdirs[cg] < 255)
1.50      lukem     676:                        fs->fs_contigdirs[cg]++;
                    677:        } else {
                    678:                if (fs->fs_contigdirs[cg] > 0)
                    679:                        fs->fs_contigdirs[cg]--;
                    680:        }
1.60      fvdl      681:        ino = (ino_t)ffs_hashalloc(pip, cg, ipref, mode, ffs_nodealloccg);
1.1       mycroft   682:        if (ino == 0)
                    683:                goto noinodes;
1.88      yamt      684:        error = VFS_VGET(pvp->v_mount, ino, vpp);
1.1       mycroft   685:        if (error) {
1.88      yamt      686:                ffs_vfree(pvp, ino, mode);
1.1       mycroft   687:                return (error);
                    688:        }
1.90      yamt      689:        KASSERT((*vpp)->v_type == VNON);
1.88      yamt      690:        ip = VTOI(*vpp);
1.60      fvdl      691:        if (ip->i_mode) {
                    692: #if 0
1.13      christos  693:                printf("mode = 0%o, inum = %d, fs = %s\n",
1.60      fvdl      694:                    ip->i_mode, ip->i_number, fs->fs_fsmnt);
                    695: #else
                    696:                printf("dmode %x mode %x dgen %x gen %x\n",
                    697:                    DIP(ip, mode), ip->i_mode,
                    698:                    DIP(ip, gen), ip->i_gen);
                    699:                printf("size %llx blocks %llx\n",
                    700:                    (long long)DIP(ip, size), (long long)DIP(ip, blocks));
1.86      christos  701:                printf("ino %llu ipref %llu\n", (unsigned long long)ino,
                    702:                    (unsigned long long)ipref);
1.60      fvdl      703: #if 0
                    704:                error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
                    705:                    (int)fs->fs_bsize, NOCRED, &bp);
                    706: #endif
                    707:
                    708: #endif
1.1       mycroft   709:                panic("ffs_valloc: dup alloc");
                    710:        }
1.60      fvdl      711:        if (DIP(ip, blocks)) {                          /* XXX */
1.86      christos  712:                printf("free inode %s/%llu had %" PRId64 " blocks\n",
                    713:                    fs->fs_fsmnt, (unsigned long long)ino, DIP(ip, blocks));
1.65      kristerw  714:                DIP_ASSIGN(ip, blocks, 0);
1.1       mycroft   715:        }
1.57      hannken   716:        ip->i_flag &= ~IN_SPACECOUNTED;
1.61      fvdl      717:        ip->i_flags = 0;
1.65      kristerw  718:        DIP_ASSIGN(ip, flags, 0);
1.1       mycroft   719:        /*
                    720:         * Set up a new generation number for this inode.
                    721:         */
1.60      fvdl      722:        ip->i_gen++;
1.65      kristerw  723:        DIP_ASSIGN(ip, gen, ip->i_gen);
1.60      fvdl      724:        if (fs->fs_magic == FS_UFS2_MAGIC) {
1.93      yamt      725:                vfs_timestamp(&ts);
1.60      fvdl      726:                ip->i_ffs2_birthtime = ts.tv_sec;
                    727:                ip->i_ffs2_birthnsec = ts.tv_nsec;
                    728:        }
1.1       mycroft   729:        return (0);
                    730: noinodes:
1.91      elad      731:        ffs_fserr(fs, kauth_cred_geteuid(cred), "out of inodes");
1.1       mycroft   732:        uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
                    733:        return (ENOSPC);
                    734: }
                    735:
                    736: /*
1.50      lukem     737:  * Find a cylinder group in which to place a directory.
1.42      sommerfe  738:  *
1.50      lukem     739:  * The policy implemented by this algorithm is to allocate a
                    740:  * directory inode in the same cylinder group as its parent
                    741:  * directory, but also to reserve space for its files inodes
                    742:  * and data. Restrict the number of directories which may be
                    743:  * allocated one after another in the same cylinder group
                    744:  * without intervening allocation of files.
1.42      sommerfe  745:  *
1.50      lukem     746:  * If we allocate a first level directory then force allocation
                    747:  * in another cylinder group.
1.1       mycroft   748:  */
                    749: static ino_t
1.85      thorpej   750: ffs_dirpref(struct inode *pip)
1.1       mycroft   751: {
1.50      lukem     752:        register struct fs *fs;
1.74      soren     753:        int cg, prefcg;
1.89      dsl       754:        int64_t dirsize, cgsize, curdsz;
                    755:        int avgifree, avgbfree, avgndir;
1.50      lukem     756:        int minifree, minbfree, maxndir;
                    757:        int mincg, minndir;
                    758:        int maxcontigdirs;
                    759:
                    760:        fs = pip->i_fs;
1.1       mycroft   761:
                    762:        avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
1.50      lukem     763:        avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
                    764:        avgndir = fs->fs_cstotal.cs_ndir / fs->fs_ncg;
                    765:
                    766:        /*
                    767:         * Force allocation in another cg if creating a first level dir.
                    768:         */
                    769:        if (ITOV(pip)->v_flag & VROOT) {
1.71      mycroft   770:                prefcg = random() % fs->fs_ncg;
1.50      lukem     771:                mincg = prefcg;
                    772:                minndir = fs->fs_ipg;
                    773:                for (cg = prefcg; cg < fs->fs_ncg; cg++)
                    774:                        if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
                    775:                            fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
                    776:                            fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
1.42      sommerfe  777:                                mincg = cg;
1.50      lukem     778:                                minndir = fs->fs_cs(fs, cg).cs_ndir;
1.42      sommerfe  779:                        }
1.50      lukem     780:                for (cg = 0; cg < prefcg; cg++)
                    781:                        if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
                    782:                            fs->fs_cs(fs, cg).cs_nifree >= avgifree &&
                    783:                            fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
                    784:                                mincg = cg;
                    785:                                minndir = fs->fs_cs(fs, cg).cs_ndir;
1.42      sommerfe  786:                        }
1.50      lukem     787:                return ((ino_t)(fs->fs_ipg * mincg));
1.42      sommerfe  788:        }
1.50      lukem     789:
                    790:        /*
                    791:         * Count various limits which used for
                    792:         * optimal allocation of a directory inode.
                    793:         */
                    794:        maxndir = min(avgndir + fs->fs_ipg / 16, fs->fs_ipg);
                    795:        minifree = avgifree - fs->fs_ipg / 4;
                    796:        if (minifree < 0)
                    797:                minifree = 0;
1.54      mycroft   798:        minbfree = avgbfree - fragstoblks(fs, fs->fs_fpg) / 4;
1.50      lukem     799:        if (minbfree < 0)
                    800:                minbfree = 0;
1.89      dsl       801:        cgsize = (int64_t)fs->fs_fsize * fs->fs_fpg;
                    802:        dirsize = (int64_t)fs->fs_avgfilesize * fs->fs_avgfpdir;
                    803:        if (avgndir != 0) {
                    804:                curdsz = (cgsize - (int64_t)avgbfree * fs->fs_bsize) / avgndir;
                    805:                if (dirsize < curdsz)
                    806:                        dirsize = curdsz;
                    807:        }
                    808:        if (cgsize < dirsize * 255)
                    809:                maxcontigdirs = cgsize / dirsize;
                    810:        else
                    811:                maxcontigdirs = 255;
1.50      lukem     812:        if (fs->fs_avgfpdir > 0)
                    813:                maxcontigdirs = min(maxcontigdirs,
                    814:                                    fs->fs_ipg / fs->fs_avgfpdir);
                    815:        if (maxcontigdirs == 0)
                    816:                maxcontigdirs = 1;
                    817:
                    818:        /*
1.81      perry     819:         * Limit number of dirs in one cg and reserve space for
1.50      lukem     820:         * regular files, but only if we have no deficit in
                    821:         * inodes or space.
                    822:         */
                    823:        prefcg = ino_to_cg(fs, pip->i_number);
                    824:        for (cg = prefcg; cg < fs->fs_ncg; cg++)
                    825:                if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
                    826:                    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
                    827:                    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
                    828:                        if (fs->fs_contigdirs[cg] < maxcontigdirs)
                    829:                                return ((ino_t)(fs->fs_ipg * cg));
                    830:                }
                    831:        for (cg = 0; cg < prefcg; cg++)
                    832:                if (fs->fs_cs(fs, cg).cs_ndir < maxndir &&
                    833:                    fs->fs_cs(fs, cg).cs_nifree >= minifree &&
                    834:                    fs->fs_cs(fs, cg).cs_nbfree >= minbfree) {
                    835:                        if (fs->fs_contigdirs[cg] < maxcontigdirs)
                    836:                                return ((ino_t)(fs->fs_ipg * cg));
                    837:                }
                    838:        /*
                    839:         * This is a backstop when we are deficient in space.
                    840:         */
                    841:        for (cg = prefcg; cg < fs->fs_ncg; cg++)
                    842:                if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
                    843:                        return ((ino_t)(fs->fs_ipg * cg));
                    844:        for (cg = 0; cg < prefcg; cg++)
                    845:                if (fs->fs_cs(fs, cg).cs_nifree >= avgifree)
                    846:                        break;
                    847:        return ((ino_t)(fs->fs_ipg * cg));
1.1       mycroft   848: }
                    849:
                    850: /*
                    851:  * Select the desired position for the next block in a file.  The file is
                    852:  * logically divided into sections. The first section is composed of the
                    853:  * direct blocks. Each additional section contains fs_maxbpg blocks.
1.81      perry     854:  *
1.1       mycroft   855:  * If no blocks have been allocated in the first section, the policy is to
                    856:  * request a block in the same cylinder group as the inode that describes
                    857:  * the file. If no blocks have been allocated in any other section, the
                    858:  * policy is to place the section in a cylinder group with a greater than
                    859:  * average number of free blocks.  An appropriate cylinder group is found
                    860:  * by using a rotor that sweeps the cylinder groups. When a new group of
                    861:  * blocks is needed, the sweep begins in the cylinder group following the
                    862:  * cylinder group from which the previous allocation was made. The sweep
                    863:  * continues until a cylinder group with greater than the average number
                    864:  * of free blocks is found. If the allocation is for the first block in an
                    865:  * indirect block, the information on the previous allocation is unavailable;
                    866:  * here a best guess is made based upon the logical block number being
                    867:  * allocated.
1.81      perry     868:  *
1.1       mycroft   869:  * If a section is already partially allocated, the policy is to
                    870:  * contiguously allocate fs_maxcontig blocks.  The end of one of these
1.60      fvdl      871:  * contiguous blocks and the beginning of the next is laid out
                    872:  * contigously if possible.
1.1       mycroft   873:  */
1.58      fvdl      874: daddr_t
1.85      thorpej   875: ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx,
                    876:     int32_t *bap /* XXX ondisk32 */)
1.1       mycroft   877: {
1.33      augustss  878:        struct fs *fs;
                    879:        int cg;
1.1       mycroft   880:        int avgbfree, startcg;
                    881:
                    882:        fs = ip->i_fs;
                    883:        if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1.31      fvdl      884:                if (lbn < NDADDR + NINDIR(fs)) {
1.1       mycroft   885:                        cg = ino_to_cg(fs, ip->i_number);
                    886:                        return (fs->fs_fpg * cg + fs->fs_frag);
                    887:                }
                    888:                /*
                    889:                 * Find a cylinder with greater than average number of
                    890:                 * unused data blocks.
                    891:                 */
                    892:                if (indx == 0 || bap[indx - 1] == 0)
                    893:                        startcg =
                    894:                            ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
                    895:                else
1.19      bouyer    896:                        startcg = dtog(fs,
1.30      fvdl      897:                                ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
1.1       mycroft   898:                startcg %= fs->fs_ncg;
                    899:                avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
                    900:                for (cg = startcg; cg < fs->fs_ncg; cg++)
                    901:                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
                    902:                                return (fs->fs_fpg * cg + fs->fs_frag);
                    903:                        }
1.52      lukem     904:                for (cg = 0; cg < startcg; cg++)
1.1       mycroft   905:                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
                    906:                                return (fs->fs_fpg * cg + fs->fs_frag);
                    907:                        }
1.35      thorpej   908:                return (0);
1.1       mycroft   909:        }
                    910:        /*
1.60      fvdl      911:         * We just always try to lay things out contiguously.
                    912:         */
                    913:        return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
                    914: }
                    915:
                    916: daddr_t
1.85      thorpej   917: ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int64_t *bap)
1.60      fvdl      918: {
                    919:        struct fs *fs;
                    920:        int cg;
                    921:        int avgbfree, startcg;
                    922:
                    923:        fs = ip->i_fs;
                    924:        if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
                    925:                if (lbn < NDADDR + NINDIR(fs)) {
                    926:                        cg = ino_to_cg(fs, ip->i_number);
                    927:                        return (fs->fs_fpg * cg + fs->fs_frag);
                    928:                }
1.1       mycroft   929:                /*
1.60      fvdl      930:                 * Find a cylinder with greater than average number of
                    931:                 * unused data blocks.
1.1       mycroft   932:                 */
1.60      fvdl      933:                if (indx == 0 || bap[indx - 1] == 0)
                    934:                        startcg =
                    935:                            ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
                    936:                else
                    937:                        startcg = dtog(fs,
                    938:                                ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
                    939:                startcg %= fs->fs_ncg;
                    940:                avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
                    941:                for (cg = startcg; cg < fs->fs_ncg; cg++)
                    942:                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
                    943:                                return (fs->fs_fpg * cg + fs->fs_frag);
                    944:                        }
                    945:                for (cg = 0; cg < startcg; cg++)
                    946:                        if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
                    947:                                return (fs->fs_fpg * cg + fs->fs_frag);
                    948:                        }
                    949:                return (0);
                    950:        }
                    951:        /*
                    952:         * We just always try to lay things out contiguously.
                    953:         */
                    954:        return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
1.1       mycroft   955: }
                    956:
1.60      fvdl      957:
1.1       mycroft   958: /*
                    959:  * Implement the cylinder overflow algorithm.
                    960:  *
                    961:  * The policy implemented by this algorithm is:
                    962:  *   1) allocate the block in its requested cylinder group.
                    963:  *   2) quadradically rehash on the cylinder group number.
                    964:  *   3) brute force search for a free block.
                    965:  */
                    966: /*VARARGS5*/
1.58      fvdl      967: static daddr_t
1.85      thorpej   968: ffs_hashalloc(struct inode *ip, int cg, daddr_t pref,
                    969:     int size /* size for data blocks, mode for inodes */,
                    970:     daddr_t (*allocator)(struct inode *, int, daddr_t, int))
1.1       mycroft   971: {
1.33      augustss  972:        struct fs *fs;
1.58      fvdl      973:        daddr_t result;
1.1       mycroft   974:        int i, icg = cg;
                    975:
                    976:        fs = ip->i_fs;
                    977:        /*
                    978:         * 1: preferred cylinder group
                    979:         */
                    980:        result = (*allocator)(ip, cg, pref, size);
                    981:        if (result)
                    982:                return (result);
                    983:        /*
                    984:         * 2: quadratic rehash
                    985:         */
                    986:        for (i = 1; i < fs->fs_ncg; i *= 2) {
                    987:                cg += i;
                    988:                if (cg >= fs->fs_ncg)
                    989:                        cg -= fs->fs_ncg;
                    990:                result = (*allocator)(ip, cg, 0, size);
                    991:                if (result)
                    992:                        return (result);
                    993:        }
                    994:        /*
                    995:         * 3: brute force search
                    996:         * Note that we start at i == 2, since 0 was checked initially,
                    997:         * and 1 is always checked in the quadratic rehash.
                    998:         */
                    999:        cg = (icg + 2) % fs->fs_ncg;
                   1000:        for (i = 2; i < fs->fs_ncg; i++) {
                   1001:                result = (*allocator)(ip, cg, 0, size);
                   1002:                if (result)
                   1003:                        return (result);
                   1004:                cg++;
                   1005:                if (cg == fs->fs_ncg)
                   1006:                        cg = 0;
                   1007:        }
1.35      thorpej  1008:        return (0);
1.1       mycroft  1009: }
                   1010:
                   1011: /*
                   1012:  * Determine whether a fragment can be extended.
                   1013:  *
1.81      perry    1014:  * Check to see if the necessary fragments are available, and
1.1       mycroft  1015:  * if they are, allocate them.
                   1016:  */
1.58      fvdl     1017: static daddr_t
1.85      thorpej  1018: ffs_fragextend(struct inode *ip, int cg, daddr_t bprev, int osize, int nsize)
1.1       mycroft  1019: {
1.33      augustss 1020:        struct fs *fs;
                   1021:        struct cg *cgp;
1.1       mycroft  1022:        struct buf *bp;
1.58      fvdl     1023:        daddr_t bno;
1.1       mycroft  1024:        int frags, bbase;
                   1025:        int i, error;
1.62      fvdl     1026:        u_int8_t *blksfree;
1.1       mycroft  1027:
                   1028:        fs = ip->i_fs;
                   1029:        if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
1.35      thorpej  1030:                return (0);
1.1       mycroft  1031:        frags = numfrags(fs, nsize);
                   1032:        bbase = fragnum(fs, bprev);
                   1033:        if (bbase > fragnum(fs, (bprev + frags - 1))) {
                   1034:                /* cannot extend across a block boundary */
1.35      thorpej  1035:                return (0);
1.1       mycroft  1036:        }
                   1037:        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
                   1038:                (int)fs->fs_cgsize, NOCRED, &bp);
                   1039:        if (error) {
                   1040:                brelse(bp);
1.35      thorpej  1041:                return (0);
1.1       mycroft  1042:        }
                   1043:        cgp = (struct cg *)bp->b_data;
1.30      fvdl     1044:        if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
1.1       mycroft  1045:                brelse(bp);
1.35      thorpej  1046:                return (0);
1.1       mycroft  1047:        }
1.92      kardel   1048:        cgp->cg_old_time = ufs_rw32(time_second, UFS_FSNEEDSWAP(fs));
1.73      dbj      1049:        if ((fs->fs_magic != FS_UFS1_MAGIC) ||
                   1050:            (fs->fs_old_flags & FS_FLAGS_UPDATED))
1.92      kardel   1051:                cgp->cg_time = ufs_rw64(time_second, UFS_FSNEEDSWAP(fs));
1.1       mycroft  1052:        bno = dtogd(fs, bprev);
1.62      fvdl     1053:        blksfree = cg_blksfree(cgp, UFS_FSNEEDSWAP(fs));
1.1       mycroft  1054:        for (i = numfrags(fs, osize); i < frags; i++)
1.62      fvdl     1055:                if (isclr(blksfree, bno + i)) {
1.1       mycroft  1056:                        brelse(bp);
1.35      thorpej  1057:                        return (0);
1.1       mycroft  1058:                }
                   1059:        /*
                   1060:         * the current fragment can be extended
                   1061:         * deduct the count on fragment being extended into
                   1062:         * increase the count on the remaining fragment (if any)
                   1063:         * allocate the extended piece
                   1064:         */
                   1065:        for (i = frags; i < fs->fs_frag - bbase; i++)
1.62      fvdl     1066:                if (isclr(blksfree, bno + i))
1.1       mycroft  1067:                        break;
1.30      fvdl     1068:        ufs_add32(cgp->cg_frsum[i - numfrags(fs, osize)], -1, UFS_FSNEEDSWAP(fs));
1.1       mycroft  1069:        if (i != frags)
1.30      fvdl     1070:                ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_FSNEEDSWAP(fs));
1.1       mycroft  1071:        for (i = numfrags(fs, osize); i < frags; i++) {
1.62      fvdl     1072:                clrbit(blksfree, bno + i);
1.30      fvdl     1073:                ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_FSNEEDSWAP(fs));
1.1       mycroft  1074:                fs->fs_cstotal.cs_nffree--;
                   1075:                fs->fs_cs(fs, cg).cs_nffree--;
                   1076:        }
                   1077:        fs->fs_fmod = 1;
1.30      fvdl     1078:        if (DOINGSOFTDEP(ITOV(ip)))
                   1079:                softdep_setup_blkmapdep(bp, fs, bprev);
1.76      hannken  1080:        ACTIVECG_CLR(fs, cg);
1.1       mycroft  1081:        bdwrite(bp);
                   1082:        return (bprev);
                   1083: }
                   1084:
                   1085: /*
                   1086:  * Determine whether a block can be allocated.
                   1087:  *
                   1088:  * Check to see if a block of the appropriate size is available,
                   1089:  * and if it is, allocate it.
                   1090:  */
1.58      fvdl     1091: static daddr_t
1.85      thorpej  1092: ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
1.1       mycroft  1093: {
1.62      fvdl     1094:        struct fs *fs = ip->i_fs;
1.30      fvdl     1095:        struct cg *cgp;
1.1       mycroft  1096:        struct buf *bp;
1.60      fvdl     1097:        int32_t bno;
                   1098:        daddr_t blkno;
1.30      fvdl     1099:        int error, frags, allocsiz, i;
1.62      fvdl     1100:        u_int8_t *blksfree;
1.30      fvdl     1101: #ifdef FFS_EI
                   1102:        const int needswap = UFS_FSNEEDSWAP(fs);
                   1103: #endif
1.1       mycroft  1104:
                   1105:        if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
1.35      thorpej  1106:                return (0);
1.1       mycroft  1107:        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
                   1108:                (int)fs->fs_cgsize, NOCRED, &bp);
                   1109:        if (error) {
                   1110:                brelse(bp);
1.35      thorpej  1111:                return (0);
1.1       mycroft  1112:        }
                   1113:        cgp = (struct cg *)bp->b_data;
1.19      bouyer   1114:        if (!cg_chkmagic(cgp, needswap) ||
1.1       mycroft  1115:            (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
                   1116:                brelse(bp);
1.35      thorpej  1117:                return (0);
1.1       mycroft  1118:        }
1.92      kardel   1119:        cgp->cg_old_time = ufs_rw32(time_second, needswap);
1.73      dbj      1120:        if ((fs->fs_magic != FS_UFS1_MAGIC) ||
                   1121:            (fs->fs_old_flags & FS_FLAGS_UPDATED))
1.92      kardel   1122:                cgp->cg_time = ufs_rw64(time_second, needswap);
1.1       mycroft  1123:        if (size == fs->fs_bsize) {
1.60      fvdl     1124:                blkno = ffs_alloccgblk(ip, bp, bpref);
1.76      hannken  1125:                ACTIVECG_CLR(fs, cg);
1.1       mycroft  1126:                bdwrite(bp);
1.60      fvdl     1127:                return (blkno);
1.1       mycroft  1128:        }
                   1129:        /*
                   1130:         * check to see if any fragments are already available
                   1131:         * allocsiz is the size which will be allocated, hacking
                   1132:         * it down to a smaller size if necessary
                   1133:         */
1.62      fvdl     1134:        blksfree = cg_blksfree(cgp, needswap);
1.1       mycroft  1135:        frags = numfrags(fs, size);
                   1136:        for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
                   1137:                if (cgp->cg_frsum[allocsiz] != 0)
                   1138:                        break;
                   1139:        if (allocsiz == fs->fs_frag) {
                   1140:                /*
1.81      perry    1141:                 * no fragments were available, so a block will be
1.1       mycroft  1142:                 * allocated, and hacked up
                   1143:                 */
                   1144:                if (cgp->cg_cs.cs_nbfree == 0) {
                   1145:                        brelse(bp);
1.35      thorpej  1146:                        return (0);
1.1       mycroft  1147:                }
1.60      fvdl     1148:                blkno = ffs_alloccgblk(ip, bp, bpref);
                   1149:                bno = dtogd(fs, blkno);
1.1       mycroft  1150:                for (i = frags; i < fs->fs_frag; i++)
1.62      fvdl     1151:                        setbit(blksfree, bno + i);
1.1       mycroft  1152:                i = fs->fs_frag - frags;
1.19      bouyer   1153:                ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1.1       mycroft  1154:                fs->fs_cstotal.cs_nffree += i;
1.30      fvdl     1155:                fs->fs_cs(fs, cg).cs_nffree += i;
1.1       mycroft  1156:                fs->fs_fmod = 1;
1.19      bouyer   1157:                ufs_add32(cgp->cg_frsum[i], 1, needswap);
1.76      hannken  1158:                ACTIVECG_CLR(fs, cg);
1.1       mycroft  1159:                bdwrite(bp);
1.60      fvdl     1160:                return (blkno);
1.1       mycroft  1161:        }
1.30      fvdl     1162:        bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
                   1163: #if 0
                   1164:        /*
                   1165:         * XXX fvdl mapsearch will panic, and never return -1
1.58      fvdl     1166:         *          also: returning NULL as daddr_t ?
1.30      fvdl     1167:         */
1.1       mycroft  1168:        if (bno < 0) {
                   1169:                brelse(bp);
1.35      thorpej  1170:                return (0);
1.1       mycroft  1171:        }
1.30      fvdl     1172: #endif
1.1       mycroft  1173:        for (i = 0; i < frags; i++)
1.62      fvdl     1174:                clrbit(blksfree, bno + i);
1.19      bouyer   1175:        ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
1.1       mycroft  1176:        fs->fs_cstotal.cs_nffree -= frags;
                   1177:        fs->fs_cs(fs, cg).cs_nffree -= frags;
                   1178:        fs->fs_fmod = 1;
1.19      bouyer   1179:        ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
1.1       mycroft  1180:        if (frags != allocsiz)
1.19      bouyer   1181:                ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
1.30      fvdl     1182:        blkno = cg * fs->fs_fpg + bno;
                   1183:        if (DOINGSOFTDEP(ITOV(ip)))
                   1184:                softdep_setup_blkmapdep(bp, fs, blkno);
1.76      hannken  1185:        ACTIVECG_CLR(fs, cg);
1.1       mycroft  1186:        bdwrite(bp);
1.30      fvdl     1187:        return blkno;
1.1       mycroft  1188: }
                   1189:
                   1190: /*
                   1191:  * Allocate a block in a cylinder group.
                   1192:  *
                   1193:  * This algorithm implements the following policy:
                   1194:  *   1) allocate the requested block.
                   1195:  *   2) allocate a rotationally optimal block in the same cylinder.
                   1196:  *   3) allocate the next available block on the block rotor for the
                   1197:  *      specified cylinder group.
                   1198:  * Note that this routine only allocates fs_bsize blocks; these
                   1199:  * blocks may be fragmented by the routine that allocates them.
                   1200:  */
1.58      fvdl     1201: static daddr_t
1.85      thorpej  1202: ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref)
1.1       mycroft  1203: {
1.62      fvdl     1204:        struct fs *fs = ip->i_fs;
1.30      fvdl     1205:        struct cg *cgp;
1.60      fvdl     1206:        daddr_t blkno;
                   1207:        int32_t bno;
                   1208:        u_int8_t *blksfree;
1.30      fvdl     1209: #ifdef FFS_EI
                   1210:        const int needswap = UFS_FSNEEDSWAP(fs);
                   1211: #endif
1.1       mycroft  1212:
1.30      fvdl     1213:        cgp = (struct cg *)bp->b_data;
1.60      fvdl     1214:        blksfree = cg_blksfree(cgp, needswap);
1.30      fvdl     1215:        if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
1.19      bouyer   1216:                bpref = ufs_rw32(cgp->cg_rotor, needswap);
1.60      fvdl     1217:        } else {
                   1218:                bpref = blknum(fs, bpref);
                   1219:                bno = dtogd(fs, bpref);
1.1       mycroft  1220:                /*
1.60      fvdl     1221:                 * if the requested block is available, use it
1.1       mycroft  1222:                 */
1.60      fvdl     1223:                if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
                   1224:                        goto gotit;
1.1       mycroft  1225:        }
                   1226:        /*
1.60      fvdl     1227:         * Take the next available block in this cylinder group.
1.1       mycroft  1228:         */
1.30      fvdl     1229:        bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
1.1       mycroft  1230:        if (bno < 0)
1.35      thorpej  1231:                return (0);
1.60      fvdl     1232:        cgp->cg_rotor = ufs_rw32(bno, needswap);
1.1       mycroft  1233: gotit:
                   1234:        blkno = fragstoblks(fs, bno);
1.60      fvdl     1235:        ffs_clrblock(fs, blksfree, blkno);
1.30      fvdl     1236:        ffs_clusteracct(fs, cgp, blkno, -1);
1.19      bouyer   1237:        ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
1.1       mycroft  1238:        fs->fs_cstotal.cs_nbfree--;
1.19      bouyer   1239:        fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
1.73      dbj      1240:        if ((fs->fs_magic == FS_UFS1_MAGIC) &&
                   1241:            ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
                   1242:                int cylno;
                   1243:                cylno = old_cbtocylno(fs, bno);
1.75      dbj      1244:                KASSERT(cylno >= 0);
                   1245:                KASSERT(cylno < fs->fs_old_ncyl);
                   1246:                KASSERT(old_cbtorpos(fs, bno) >= 0);
                   1247:                KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bno) < fs->fs_old_nrpos);
1.73      dbj      1248:                ufs_add16(old_cg_blks(fs, cgp, cylno, needswap)[old_cbtorpos(fs, bno)], -1,
                   1249:                    needswap);
                   1250:                ufs_add32(old_cg_blktot(cgp, needswap)[cylno], -1, needswap);
                   1251:        }
1.1       mycroft  1252:        fs->fs_fmod = 1;
1.30      fvdl     1253:        blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
                   1254:        if (DOINGSOFTDEP(ITOV(ip)))
                   1255:                softdep_setup_blkmapdep(bp, fs, blkno);
                   1256:        return (blkno);
1.1       mycroft  1257: }
                   1258:
1.55      matt     1259: #ifdef XXXUBC
1.1       mycroft  1260: /*
                   1261:  * Determine whether a cluster can be allocated.
                   1262:  *
                   1263:  * We do not currently check for optimal rotational layout if there
                   1264:  * are multiple choices in the same cylinder group. Instead we just
                   1265:  * take the first one that we find following bpref.
                   1266:  */
1.60      fvdl     1267:
                   1268: /*
                   1269:  * This function must be fixed for UFS2 if re-enabled.
                   1270:  */
1.58      fvdl     1271: static daddr_t
1.85      thorpej  1272: ffs_clusteralloc(struct inode *ip, int cg, daddr_t bpref, int len)
1.1       mycroft  1273: {
1.33      augustss 1274:        struct fs *fs;
                   1275:        struct cg *cgp;
1.1       mycroft  1276:        struct buf *bp;
1.18      fvdl     1277:        int i, got, run, bno, bit, map;
1.1       mycroft  1278:        u_char *mapp;
1.5       mycroft  1279:        int32_t *lp;
1.1       mycroft  1280:
                   1281:        fs = ip->i_fs;
1.5       mycroft  1282:        if (fs->fs_maxcluster[cg] < len)
1.35      thorpej  1283:                return (0);
1.1       mycroft  1284:        if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
                   1285:            NOCRED, &bp))
                   1286:                goto fail;
                   1287:        cgp = (struct cg *)bp->b_data;
1.30      fvdl     1288:        if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs)))
1.1       mycroft  1289:                goto fail;
                   1290:        /*
                   1291:         * Check to see if a cluster of the needed size (or bigger) is
                   1292:         * available in this cylinder group.
                   1293:         */
1.30      fvdl     1294:        lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len];
1.1       mycroft  1295:        for (i = len; i <= fs->fs_contigsumsize; i++)
1.30      fvdl     1296:                if (ufs_rw32(*lp++, UFS_FSNEEDSWAP(fs)) > 0)
1.1       mycroft  1297:                        break;
1.5       mycroft  1298:        if (i > fs->fs_contigsumsize) {
                   1299:                /*
                   1300:                 * This is the first time looking for a cluster in this
                   1301:                 * cylinder group. Update the cluster summary information
                   1302:                 * to reflect the true maximum sized cluster so that
                   1303:                 * future cluster allocation requests can avoid reading
                   1304:                 * the cylinder group map only to find no clusters.
                   1305:                 */
1.30      fvdl     1306:                lp = &cg_clustersum(cgp, UFS_FSNEEDSWAP(fs))[len - 1];
1.5       mycroft  1307:                for (i = len - 1; i > 0; i--)
1.30      fvdl     1308:                        if (ufs_rw32(*lp--, UFS_FSNEEDSWAP(fs)) > 0)
1.5       mycroft  1309:                                break;
                   1310:                fs->fs_maxcluster[cg] = i;
1.1       mycroft  1311:                goto fail;
1.5       mycroft  1312:        }
1.1       mycroft  1313:        /*
                   1314:         * Search the cluster map to find a big enough cluster.
                   1315:         * We take the first one that we find, even if it is larger
                   1316:         * than we need as we prefer to get one close to the previous
                   1317:         * block allocation. We do not search before the current
                   1318:         * preference point as we do not want to allocate a block
                   1319:         * that is allocated before the previous one (as we will
                   1320:         * then have to wait for another pass of the elevator
                   1321:         * algorithm before it will be read). We prefer to fail and
                   1322:         * be recalled to try an allocation in the next cylinder group.
                   1323:         */
                   1324:        if (dtog(fs, bpref) != cg)
                   1325:                bpref = 0;
                   1326:        else
                   1327:                bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
1.30      fvdl     1328:        mapp = &cg_clustersfree(cgp, UFS_FSNEEDSWAP(fs))[bpref / NBBY];
1.1       mycroft  1329:        map = *mapp++;
                   1330:        bit = 1 << (bpref % NBBY);
1.19      bouyer   1331:        for (run = 0, got = bpref;
1.30      fvdl     1332:                got < ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)); got++) {
1.1       mycroft  1333:                if ((map & bit) == 0) {
                   1334:                        run = 0;
                   1335:                } else {
                   1336:                        run++;
                   1337:                        if (run == len)
                   1338:                                break;
                   1339:                }
1.18      fvdl     1340:                if ((got & (NBBY - 1)) != (NBBY - 1)) {
1.1       mycroft  1341:                        bit <<= 1;
                   1342:                } else {
                   1343:                        map = *mapp++;
                   1344:                        bit = 1;
                   1345:                }
                   1346:        }
1.30      fvdl     1347:        if (got == ufs_rw32(cgp->cg_nclusterblks, UFS_FSNEEDSWAP(fs)))
1.1       mycroft  1348:                goto fail;
                   1349:        /*
                   1350:         * Allocate the cluster that we have found.
                   1351:         */
1.30      fvdl     1352: #ifdef DIAGNOSTIC
1.18      fvdl     1353:        for (i = 1; i <= len; i++)
1.30      fvdl     1354:                if (!ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
                   1355:                    got - run + i))
1.18      fvdl     1356:                        panic("ffs_clusteralloc: map mismatch");
1.30      fvdl     1357: #endif
1.18      fvdl     1358:        bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
                   1359:        if (dtog(fs, bno) != cg)
                   1360:                panic("ffs_clusteralloc: allocated out of group");
1.1       mycroft  1361:        len = blkstofrags(fs, len);
                   1362:        for (i = 0; i < len; i += fs->fs_frag)
1.30      fvdl     1363:                if ((got = ffs_alloccgblk(ip, bp, bno + i)) != bno + i)
1.1       mycroft  1364:                        panic("ffs_clusteralloc: lost block");
1.76      hannken  1365:        ACTIVECG_CLR(fs, cg);
1.8       cgd      1366:        bdwrite(bp);
1.1       mycroft  1367:        return (bno);
                   1368:
                   1369: fail:
                   1370:        brelse(bp);
                   1371:        return (0);
                   1372: }
1.55      matt     1373: #endif /* XXXUBC */
1.1       mycroft  1374:
                   1375: /*
                   1376:  * Determine whether an inode can be allocated.
                   1377:  *
                   1378:  * Check to see if an inode is available, and if it is,
                   1379:  * allocate it using the following policy:
                   1380:  *   1) allocate the requested inode.
                   1381:  *   2) allocate the next available inode after the requested
                   1382:  *      inode in the specified cylinder group.
                   1383:  */
1.58      fvdl     1384: static daddr_t
1.85      thorpej  1385: ffs_nodealloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
1.1       mycroft  1386: {
1.62      fvdl     1387:        struct fs *fs = ip->i_fs;
1.33      augustss 1388:        struct cg *cgp;
1.60      fvdl     1389:        struct buf *bp, *ibp;
                   1390:        u_int8_t *inosused;
1.1       mycroft  1391:        int error, start, len, loc, map, i;
1.60      fvdl     1392:        int32_t initediblk;
                   1393:        struct ufs2_dinode *dp2;
1.19      bouyer   1394: #ifdef FFS_EI
1.30      fvdl     1395:        const int needswap = UFS_FSNEEDSWAP(fs);
1.19      bouyer   1396: #endif
1.1       mycroft  1397:
                   1398:        if (fs->fs_cs(fs, cg).cs_nifree == 0)
1.35      thorpej  1399:                return (0);
1.1       mycroft  1400:        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
                   1401:                (int)fs->fs_cgsize, NOCRED, &bp);
                   1402:        if (error) {
                   1403:                brelse(bp);
1.35      thorpej  1404:                return (0);
1.1       mycroft  1405:        }
                   1406:        cgp = (struct cg *)bp->b_data;
1.19      bouyer   1407:        if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0) {
1.1       mycroft  1408:                brelse(bp);
1.35      thorpej  1409:                return (0);
1.1       mycroft  1410:        }
1.92      kardel   1411:        cgp->cg_old_time = ufs_rw32(time_second, needswap);
1.73      dbj      1412:        if ((fs->fs_magic != FS_UFS1_MAGIC) ||
                   1413:            (fs->fs_old_flags & FS_FLAGS_UPDATED))
1.92      kardel   1414:                cgp->cg_time = ufs_rw64(time_second, needswap);
1.60      fvdl     1415:        inosused = cg_inosused(cgp, needswap);
1.1       mycroft  1416:        if (ipref) {
                   1417:                ipref %= fs->fs_ipg;
1.60      fvdl     1418:                if (isclr(inosused, ipref))
1.1       mycroft  1419:                        goto gotit;
                   1420:        }
1.19      bouyer   1421:        start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
                   1422:        len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
                   1423:                NBBY);
1.60      fvdl     1424:        loc = skpc(0xff, len, &inosused[start]);
1.1       mycroft  1425:        if (loc == 0) {
                   1426:                len = start + 1;
                   1427:                start = 0;
1.60      fvdl     1428:                loc = skpc(0xff, len, &inosused[0]);
1.1       mycroft  1429:                if (loc == 0) {
1.13      christos 1430:                        printf("cg = %d, irotor = %d, fs = %s\n",
1.19      bouyer   1431:                            cg, ufs_rw32(cgp->cg_irotor, needswap),
                   1432:                                fs->fs_fsmnt);
1.1       mycroft  1433:                        panic("ffs_nodealloccg: map corrupted");
                   1434:                        /* NOTREACHED */
                   1435:                }
                   1436:        }
                   1437:        i = start + len - loc;
1.60      fvdl     1438:        map = inosused[i];
1.1       mycroft  1439:        ipref = i * NBBY;
                   1440:        for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
                   1441:                if ((map & i) == 0) {
1.19      bouyer   1442:                        cgp->cg_irotor = ufs_rw32(ipref, needswap);
1.1       mycroft  1443:                        goto gotit;
                   1444:                }
                   1445:        }
1.13      christos 1446:        printf("fs = %s\n", fs->fs_fsmnt);
1.1       mycroft  1447:        panic("ffs_nodealloccg: block not in map");
                   1448:        /* NOTREACHED */
                   1449: gotit:
1.30      fvdl     1450:        if (DOINGSOFTDEP(ITOV(ip)))
                   1451:                softdep_setup_inomapdep(bp, ip, cg * fs->fs_ipg + ipref);
1.60      fvdl     1452:        setbit(inosused, ipref);
1.19      bouyer   1453:        ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
1.1       mycroft  1454:        fs->fs_cstotal.cs_nifree--;
1.30      fvdl     1455:        fs->fs_cs(fs, cg).cs_nifree--;
1.1       mycroft  1456:        fs->fs_fmod = 1;
                   1457:        if ((mode & IFMT) == IFDIR) {
1.19      bouyer   1458:                ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
1.1       mycroft  1459:                fs->fs_cstotal.cs_ndir++;
                   1460:                fs->fs_cs(fs, cg).cs_ndir++;
                   1461:        }
1.60      fvdl     1462:        /*
                   1463:         * Check to see if we need to initialize more inodes.
                   1464:         */
                   1465:        initediblk = ufs_rw32(cgp->cg_initediblk, needswap);
                   1466:        if (fs->fs_magic == FS_UFS2_MAGIC &&
                   1467:            ipref + INOPB(fs) > initediblk &&
                   1468:            initediblk < ufs_rw32(cgp->cg_niblk, needswap)) {
                   1469:                ibp = getblk(ip->i_devvp, fsbtodb(fs,
                   1470:                    ino_to_fsba(fs, cg * fs->fs_ipg + initediblk)),
                   1471:                    (int)fs->fs_bsize, 0, 0);
                   1472:                    memset(ibp->b_data, 0, fs->fs_bsize);
                   1473:                    dp2 = (struct ufs2_dinode *)(ibp->b_data);
                   1474:                    for (i = 0; i < INOPB(fs); i++) {
                   1475:                        /*
                   1476:                         * Don't bother to swap, it's supposed to be
                   1477:                         * random, after all.
                   1478:                         */
1.70      itojun   1479:                        dp2->di_gen = (arc4random() & INT32_MAX) / 2 + 1;
1.60      fvdl     1480:                        dp2++;
                   1481:                }
                   1482:                bawrite(ibp);
                   1483:                initediblk += INOPB(fs);
                   1484:                cgp->cg_initediblk = ufs_rw32(initediblk, needswap);
                   1485:        }
                   1486:
1.76      hannken  1487:        ACTIVECG_CLR(fs, cg);
1.1       mycroft  1488:        bdwrite(bp);
                   1489:        return (cg * fs->fs_ipg + ipref);
                   1490: }
                   1491:
                   1492: /*
                   1493:  * Free a block or fragment.
                   1494:  *
                   1495:  * The specified block or fragment is placed back in the
1.81      perry    1496:  * free map. If a fragment is deallocated, a possible
1.1       mycroft  1497:  * block reassembly is checked.
                   1498:  */
1.9       christos 1499: void
1.85      thorpej  1500: ffs_blkfree(struct fs *fs, struct vnode *devvp, daddr_t bno, long size,
                   1501:     ino_t inum)
1.1       mycroft  1502: {
1.33      augustss 1503:        struct cg *cgp;
1.1       mycroft  1504:        struct buf *bp;
1.76      hannken  1505:        struct ufsmount *ump;
1.60      fvdl     1506:        int32_t fragno, cgbno;
1.76      hannken  1507:        daddr_t cgblkno;
1.1       mycroft  1508:        int i, error, cg, blk, frags, bbase;
1.62      fvdl     1509:        u_int8_t *blksfree;
1.76      hannken  1510:        dev_t dev;
1.30      fvdl     1511:        const int needswap = UFS_FSNEEDSWAP(fs);
1.1       mycroft  1512:
1.76      hannken  1513:        cg = dtog(fs, bno);
1.77      hannken  1514:        if (devvp->v_type != VBLK) {
                   1515:                /* devvp is a snapshot */
1.76      hannken  1516:                dev = VTOI(devvp)->i_devvp->v_rdev;
                   1517:                cgblkno = fragstoblks(fs, cgtod(fs, cg));
                   1518:        } else {
                   1519:                dev = devvp->v_rdev;
                   1520:                ump = VFSTOUFS(devvp->v_specmountpoint);
                   1521:                cgblkno = fsbtodb(fs, cgtod(fs, cg));
1.99.6.1! jmcneill 1522:                if (ffs_snapblkfree(fs, devvp, bno, size, inum))
1.76      hannken  1523:                        return;
                   1524:        }
1.30      fvdl     1525:        if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0 ||
                   1526:            fragnum(fs, bno) + numfrags(fs, size) > fs->fs_frag) {
1.59      tsutsui  1527:                printf("dev = 0x%x, bno = %" PRId64 " bsize = %d, "
1.58      fvdl     1528:                       "size = %ld, fs = %s\n",
1.76      hannken  1529:                    dev, bno, fs->fs_bsize, size, fs->fs_fsmnt);
1.1       mycroft  1530:                panic("blkfree: bad size");
                   1531:        }
1.76      hannken  1532:
1.60      fvdl     1533:        if (bno >= fs->fs_size) {
1.86      christos 1534:                printf("bad block %" PRId64 ", ino %llu\n", bno,
                   1535:                    (unsigned long long)inum);
1.76      hannken  1536:                ffs_fserr(fs, inum, "bad block");
1.1       mycroft  1537:                return;
                   1538:        }
1.76      hannken  1539:        error = bread(devvp, cgblkno, (int)fs->fs_cgsize, NOCRED, &bp);
1.1       mycroft  1540:        if (error) {
                   1541:                brelse(bp);
                   1542:                return;
                   1543:        }
                   1544:        cgp = (struct cg *)bp->b_data;
1.19      bouyer   1545:        if (!cg_chkmagic(cgp, needswap)) {
1.1       mycroft  1546:                brelse(bp);
                   1547:                return;
                   1548:        }
1.92      kardel   1549:        cgp->cg_old_time = ufs_rw32(time_second, needswap);
1.73      dbj      1550:        if ((fs->fs_magic != FS_UFS1_MAGIC) ||
                   1551:            (fs->fs_old_flags & FS_FLAGS_UPDATED))
1.92      kardel   1552:                cgp->cg_time = ufs_rw64(time_second, needswap);
1.60      fvdl     1553:        cgbno = dtogd(fs, bno);
1.62      fvdl     1554:        blksfree = cg_blksfree(cgp, needswap);
1.1       mycroft  1555:        if (size == fs->fs_bsize) {
1.60      fvdl     1556:                fragno = fragstoblks(fs, cgbno);
1.62      fvdl     1557:                if (!ffs_isfreeblock(fs, blksfree, fragno)) {
1.77      hannken  1558:                        if (devvp->v_type != VBLK) {
                   1559:                                /* devvp is a snapshot */
1.76      hannken  1560:                                brelse(bp);
                   1561:                                return;
                   1562:                        }
1.59      tsutsui  1563:                        printf("dev = 0x%x, block = %" PRId64 ", fs = %s\n",
1.76      hannken  1564:                            dev, bno, fs->fs_fsmnt);
1.1       mycroft  1565:                        panic("blkfree: freeing free block");
                   1566:                }
1.62      fvdl     1567:                ffs_setblock(fs, blksfree, fragno);
1.60      fvdl     1568:                ffs_clusteracct(fs, cgp, fragno, 1);
1.19      bouyer   1569:                ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1.1       mycroft  1570:                fs->fs_cstotal.cs_nbfree++;
                   1571:                fs->fs_cs(fs, cg).cs_nbfree++;
1.73      dbj      1572:                if ((fs->fs_magic == FS_UFS1_MAGIC) &&
                   1573:                    ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
                   1574:                        i = old_cbtocylno(fs, cgbno);
1.75      dbj      1575:                        KASSERT(i >= 0);
                   1576:                        KASSERT(i < fs->fs_old_ncyl);
                   1577:                        KASSERT(old_cbtorpos(fs, cgbno) >= 0);
                   1578:                        KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, cgbno) < fs->fs_old_nrpos);
1.73      dbj      1579:                        ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs, cgbno)], 1,
                   1580:                            needswap);
                   1581:                        ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
                   1582:                }
1.1       mycroft  1583:        } else {
1.60      fvdl     1584:                bbase = cgbno - fragnum(fs, cgbno);
1.1       mycroft  1585:                /*
                   1586:                 * decrement the counts associated with the old frags
                   1587:                 */
1.62      fvdl     1588:                blk = blkmap(fs, blksfree, bbase);
1.19      bouyer   1589:                ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
1.1       mycroft  1590:                /*
                   1591:                 * deallocate the fragment
                   1592:                 */
                   1593:                frags = numfrags(fs, size);
                   1594:                for (i = 0; i < frags; i++) {
1.62      fvdl     1595:                        if (isset(blksfree, cgbno + i)) {
1.59      tsutsui  1596:                                printf("dev = 0x%x, block = %" PRId64
                   1597:                                       ", fs = %s\n",
1.76      hannken  1598:                                    dev, bno + i, fs->fs_fsmnt);
1.1       mycroft  1599:                                panic("blkfree: freeing free frag");
                   1600:                        }
1.62      fvdl     1601:                        setbit(blksfree, cgbno + i);
1.1       mycroft  1602:                }
1.19      bouyer   1603:                ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
1.1       mycroft  1604:                fs->fs_cstotal.cs_nffree += i;
1.30      fvdl     1605:                fs->fs_cs(fs, cg).cs_nffree += i;
1.1       mycroft  1606:                /*
                   1607:                 * add back in counts associated with the new frags
                   1608:                 */
1.62      fvdl     1609:                blk = blkmap(fs, blksfree, bbase);
1.19      bouyer   1610:                ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
1.1       mycroft  1611:                /*
                   1612:                 * if a complete block has been reassembled, account for it
                   1613:                 */
1.60      fvdl     1614:                fragno = fragstoblks(fs, bbase);
1.62      fvdl     1615:                if (ffs_isblock(fs, blksfree, fragno)) {
1.19      bouyer   1616:                        ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
1.1       mycroft  1617:                        fs->fs_cstotal.cs_nffree -= fs->fs_frag;
                   1618:                        fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1.60      fvdl     1619:                        ffs_clusteracct(fs, cgp, fragno, 1);
1.19      bouyer   1620:                        ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
1.1       mycroft  1621:                        fs->fs_cstotal.cs_nbfree++;
                   1622:                        fs->fs_cs(fs, cg).cs_nbfree++;
1.73      dbj      1623:                        if ((fs->fs_magic == FS_UFS1_MAGIC) &&
                   1624:                            ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
                   1625:                                i = old_cbtocylno(fs, bbase);
1.75      dbj      1626:                                KASSERT(i >= 0);
                   1627:                                KASSERT(i < fs->fs_old_ncyl);
                   1628:                                KASSERT(old_cbtorpos(fs, bbase) >= 0);
                   1629:                                KASSERT(fs->fs_old_nrpos == 0 || old_cbtorpos(fs, bbase) < fs->fs_old_nrpos);
1.73      dbj      1630:                                ufs_add16(old_cg_blks(fs, cgp, i, needswap)[old_cbtorpos(fs,
                   1631:                                    bbase)], 1, needswap);
                   1632:                                ufs_add32(old_cg_blktot(cgp, needswap)[i], 1, needswap);
                   1633:                        }
1.1       mycroft  1634:                }
                   1635:        }
                   1636:        fs->fs_fmod = 1;
1.76      hannken  1637:        ACTIVECG_CLR(fs, cg);
1.1       mycroft  1638:        bdwrite(bp);
                   1639: }
                   1640:
1.18      fvdl     1641: #if defined(DIAGNOSTIC) || defined(DEBUG)
1.55      matt     1642: #ifdef XXXUBC
1.18      fvdl     1643: /*
                   1644:  * Verify allocation of a block or fragment. Returns true if block or
                   1645:  * fragment is allocated, false if it is free.
                   1646:  */
                   1647: static int
1.85      thorpej  1648: ffs_checkblk(struct inode *ip, daddr_t bno, long size)
1.18      fvdl     1649: {
                   1650:        struct fs *fs;
                   1651:        struct cg *cgp;
                   1652:        struct buf *bp;
                   1653:        int i, error, frags, free;
                   1654:
                   1655:        fs = ip->i_fs;
                   1656:        if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
                   1657:                printf("bsize = %d, size = %ld, fs = %s\n",
                   1658:                    fs->fs_bsize, size, fs->fs_fsmnt);
                   1659:                panic("checkblk: bad size");
                   1660:        }
1.60      fvdl     1661:        if (bno >= fs->fs_size)
1.18      fvdl     1662:                panic("checkblk: bad block %d", bno);
                   1663:        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
                   1664:                (int)fs->fs_cgsize, NOCRED, &bp);
                   1665:        if (error) {
                   1666:                brelse(bp);
                   1667:                return 0;
                   1668:        }
                   1669:        cgp = (struct cg *)bp->b_data;
1.30      fvdl     1670:        if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
1.18      fvdl     1671:                brelse(bp);
                   1672:                return 0;
                   1673:        }
                   1674:        bno = dtogd(fs, bno);
                   1675:        if (size == fs->fs_bsize) {
1.30      fvdl     1676:                free = ffs_isblock(fs, cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)),
1.19      bouyer   1677:                        fragstoblks(fs, bno));
1.18      fvdl     1678:        } else {
                   1679:                frags = numfrags(fs, size);
                   1680:                for (free = 0, i = 0; i < frags; i++)
1.30      fvdl     1681:                        if (isset(cg_blksfree(cgp, UFS_FSNEEDSWAP(fs)), bno + i))
1.18      fvdl     1682:                                free++;
                   1683:                if (free != 0 && free != frags)
                   1684:                        panic("checkblk: partially free fragment");
                   1685:        }
                   1686:        brelse(bp);
                   1687:        return (!free);
                   1688: }
1.55      matt     1689: #endif /* XXXUBC */
1.18      fvdl     1690: #endif /* DIAGNOSTIC */
                   1691:
1.1       mycroft  1692: /*
                   1693:  * Free an inode.
1.30      fvdl     1694:  */
                   1695: int
1.88      yamt     1696: ffs_vfree(struct vnode *vp, ino_t ino, int mode)
1.30      fvdl     1697: {
                   1698:
1.88      yamt     1699:        if (DOINGSOFTDEP(vp)) {
                   1700:                softdep_freefile(vp, ino, mode);
1.30      fvdl     1701:                return (0);
                   1702:        }
1.88      yamt     1703:        return ffs_freefile(VTOI(vp)->i_fs, VTOI(vp)->i_devvp, ino, mode);
1.30      fvdl     1704: }
                   1705:
                   1706: /*
                   1707:  * Do the actual free operation.
1.1       mycroft  1708:  * The specified inode is placed back in the free map.
                   1709:  */
                   1710: int
1.85      thorpej  1711: ffs_freefile(struct fs *fs, struct vnode *devvp, ino_t ino, int mode)
1.9       christos 1712: {
1.33      augustss 1713:        struct cg *cgp;
1.1       mycroft  1714:        struct buf *bp;
                   1715:        int error, cg;
1.76      hannken  1716:        daddr_t cgbno;
1.62      fvdl     1717:        u_int8_t *inosused;
1.78      hannken  1718:        dev_t dev;
1.19      bouyer   1719: #ifdef FFS_EI
1.30      fvdl     1720:        const int needswap = UFS_FSNEEDSWAP(fs);
1.19      bouyer   1721: #endif
1.1       mycroft  1722:
1.76      hannken  1723:        cg = ino_to_cg(fs, ino);
1.78      hannken  1724:        if (devvp->v_type != VBLK) {
                   1725:                /* devvp is a snapshot */
                   1726:                dev = VTOI(devvp)->i_devvp->v_rdev;
1.76      hannken  1727:                cgbno = fragstoblks(fs, cgtod(fs, cg));
                   1728:        } else {
1.78      hannken  1729:                dev = devvp->v_rdev;
1.76      hannken  1730:                cgbno = fsbtodb(fs, cgtod(fs, cg));
                   1731:        }
1.1       mycroft  1732:        if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
1.86      christos 1733:                panic("ifree: range: dev = 0x%x, ino = %llu, fs = %s",
                   1734:                    dev, (unsigned long long)ino, fs->fs_fsmnt);
1.78      hannken  1735:        error = bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp);
1.1       mycroft  1736:        if (error) {
                   1737:                brelse(bp);
1.30      fvdl     1738:                return (error);
1.1       mycroft  1739:        }
                   1740:        cgp = (struct cg *)bp->b_data;
1.19      bouyer   1741:        if (!cg_chkmagic(cgp, needswap)) {
1.1       mycroft  1742:                brelse(bp);
                   1743:                return (0);
                   1744:        }
1.92      kardel   1745:        cgp->cg_old_time = ufs_rw32(time_second, needswap);
1.73      dbj      1746:        if ((fs->fs_magic != FS_UFS1_MAGIC) ||
                   1747:            (fs->fs_old_flags & FS_FLAGS_UPDATED))
1.92      kardel   1748:                cgp->cg_time = ufs_rw64(time_second, needswap);
1.62      fvdl     1749:        inosused = cg_inosused(cgp, needswap);
1.1       mycroft  1750:        ino %= fs->fs_ipg;
1.62      fvdl     1751:        if (isclr(inosused, ino)) {
1.86      christos 1752:                printf("ifree: dev = 0x%x, ino = %llu, fs = %s\n",
                   1753:                    dev, (unsigned long long)ino + cg * fs->fs_ipg,
                   1754:                    fs->fs_fsmnt);
1.1       mycroft  1755:                if (fs->fs_ronly == 0)
                   1756:                        panic("ifree: freeing free inode");
                   1757:        }
1.62      fvdl     1758:        clrbit(inosused, ino);
1.19      bouyer   1759:        if (ino < ufs_rw32(cgp->cg_irotor, needswap))
                   1760:                cgp->cg_irotor = ufs_rw32(ino, needswap);
                   1761:        ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
1.1       mycroft  1762:        fs->fs_cstotal.cs_nifree++;
                   1763:        fs->fs_cs(fs, cg).cs_nifree++;
1.78      hannken  1764:        if ((mode & IFMT) == IFDIR) {
1.19      bouyer   1765:                ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
1.1       mycroft  1766:                fs->fs_cstotal.cs_ndir--;
                   1767:                fs->fs_cs(fs, cg).cs_ndir--;
                   1768:        }
                   1769:        fs->fs_fmod = 1;
1.82      hannken  1770:        ACTIVECG_CLR(fs, cg);
1.1       mycroft  1771:        bdwrite(bp);
                   1772:        return (0);
                   1773: }
                   1774:
                   1775: /*
1.76      hannken  1776:  * Check to see if a file is free.
                   1777:  */
                   1778: int
1.85      thorpej  1779: ffs_checkfreefile(struct fs *fs, struct vnode *devvp, ino_t ino)
1.76      hannken  1780: {
                   1781:        struct cg *cgp;
                   1782:        struct buf *bp;
                   1783:        daddr_t cgbno;
                   1784:        int ret, cg;
                   1785:        u_int8_t *inosused;
                   1786:
                   1787:        cg = ino_to_cg(fs, ino);
1.77      hannken  1788:        if (devvp->v_type != VBLK) {
                   1789:                /* devvp is a snapshot */
1.76      hannken  1790:                cgbno = fragstoblks(fs, cgtod(fs, cg));
1.77      hannken  1791:        } else
1.76      hannken  1792:                cgbno = fsbtodb(fs, cgtod(fs, cg));
                   1793:        if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
                   1794:                return 1;
                   1795:        if (bread(devvp, cgbno, (int)fs->fs_cgsize, NOCRED, &bp)) {
                   1796:                brelse(bp);
                   1797:                return 1;
                   1798:        }
                   1799:        cgp = (struct cg *)bp->b_data;
                   1800:        if (!cg_chkmagic(cgp, UFS_FSNEEDSWAP(fs))) {
                   1801:                brelse(bp);
                   1802:                return 1;
                   1803:        }
                   1804:        inosused = cg_inosused(cgp, UFS_FSNEEDSWAP(fs));
                   1805:        ino %= fs->fs_ipg;
                   1806:        ret = isclr(inosused, ino);
                   1807:        brelse(bp);
                   1808:        return ret;
                   1809: }
                   1810:
                   1811: /*
1.1       mycroft  1812:  * Find a block of the specified size in the specified cylinder group.
                   1813:  *
                   1814:  * It is a panic if a request is made to find a block if none are
                   1815:  * available.
                   1816:  */
1.60      fvdl     1817: static int32_t
1.85      thorpej  1818: ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
1.1       mycroft  1819: {
1.60      fvdl     1820:        int32_t bno;
1.1       mycroft  1821:        int start, len, loc, i;
                   1822:        int blk, field, subfield, pos;
1.19      bouyer   1823:        int ostart, olen;
1.62      fvdl     1824:        u_int8_t *blksfree;
1.30      fvdl     1825: #ifdef FFS_EI
                   1826:        const int needswap = UFS_FSNEEDSWAP(fs);
                   1827: #endif
1.1       mycroft  1828:
                   1829:        /*
                   1830:         * find the fragment by searching through the free block
                   1831:         * map for an appropriate bit pattern
                   1832:         */
                   1833:        if (bpref)
                   1834:                start = dtogd(fs, bpref) / NBBY;
                   1835:        else
1.19      bouyer   1836:                start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
1.62      fvdl     1837:        blksfree = cg_blksfree(cgp, needswap);
1.1       mycroft  1838:        len = howmany(fs->fs_fpg, NBBY) - start;
1.19      bouyer   1839:        ostart = start;
                   1840:        olen = len;
1.45      lukem    1841:        loc = scanc((u_int)len,
1.62      fvdl     1842:                (const u_char *)&blksfree[start],
1.45      lukem    1843:                (const u_char *)fragtbl[fs->fs_frag],
1.54      mycroft  1844:                (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
1.1       mycroft  1845:        if (loc == 0) {
                   1846:                len = start + 1;
                   1847:                start = 0;
1.45      lukem    1848:                loc = scanc((u_int)len,
1.62      fvdl     1849:                        (const u_char *)&blksfree[0],
1.45      lukem    1850:                        (const u_char *)fragtbl[fs->fs_frag],
1.54      mycroft  1851:                        (1 << (allocsiz - 1 + (fs->fs_frag & (NBBY - 1)))));
1.1       mycroft  1852:                if (loc == 0) {
1.13      christos 1853:                        printf("start = %d, len = %d, fs = %s\n",
1.19      bouyer   1854:                            ostart, olen, fs->fs_fsmnt);
1.20      ross     1855:                        printf("offset=%d %ld\n",
1.19      bouyer   1856:                                ufs_rw32(cgp->cg_freeoff, needswap),
1.62      fvdl     1857:                                (long)blksfree - (long)cgp);
                   1858:                        printf("cg %d\n", cgp->cg_cgx);
1.1       mycroft  1859:                        panic("ffs_alloccg: map corrupted");
                   1860:                        /* NOTREACHED */
                   1861:                }
                   1862:        }
                   1863:        bno = (start + len - loc) * NBBY;
1.19      bouyer   1864:        cgp->cg_frotor = ufs_rw32(bno, needswap);
1.1       mycroft  1865:        /*
                   1866:         * found the byte in the map
                   1867:         * sift through the bits to find the selected frag
                   1868:         */
                   1869:        for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1.62      fvdl     1870:                blk = blkmap(fs, blksfree, bno);
1.1       mycroft  1871:                blk <<= 1;
                   1872:                field = around[allocsiz];
                   1873:                subfield = inside[allocsiz];
                   1874:                for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
                   1875:                        if ((blk & field) == subfield)
                   1876:                                return (bno + pos);
                   1877:                        field <<= 1;
                   1878:                        subfield <<= 1;
                   1879:                }
                   1880:        }
1.60      fvdl     1881:        printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
1.1       mycroft  1882:        panic("ffs_alloccg: block not in map");
1.58      fvdl     1883:        /* return (-1); */
1.1       mycroft  1884: }
                   1885:
                   1886: /*
                   1887:  * Update the cluster map because of an allocation or free.
                   1888:  *
                   1889:  * Cnt == 1 means free; cnt == -1 means allocating.
                   1890:  */
1.9       christos 1891: void
1.85      thorpej  1892: ffs_clusteracct(struct fs *fs, struct cg *cgp, int32_t blkno, int cnt)
1.1       mycroft  1893: {
1.4       cgd      1894:        int32_t *sump;
1.5       mycroft  1895:        int32_t *lp;
1.1       mycroft  1896:        u_char *freemapp, *mapp;
                   1897:        int i, start, end, forw, back, map, bit;
1.30      fvdl     1898: #ifdef FFS_EI
                   1899:        const int needswap = UFS_FSNEEDSWAP(fs);
                   1900: #endif
1.1       mycroft  1901:
                   1902:        if (fs->fs_contigsumsize <= 0)
                   1903:                return;
1.19      bouyer   1904:        freemapp = cg_clustersfree(cgp, needswap);
                   1905:        sump = cg_clustersum(cgp, needswap);
1.1       mycroft  1906:        /*
                   1907:         * Allocate or clear the actual block.
                   1908:         */
                   1909:        if (cnt > 0)
                   1910:                setbit(freemapp, blkno);
                   1911:        else
                   1912:                clrbit(freemapp, blkno);
                   1913:        /*
                   1914:         * Find the size of the cluster going forward.
                   1915:         */
                   1916:        start = blkno + 1;
                   1917:        end = start + fs->fs_contigsumsize;
1.19      bouyer   1918:        if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
                   1919:                end = ufs_rw32(cgp->cg_nclusterblks, needswap);
1.1       mycroft  1920:        mapp = &freemapp[start / NBBY];
                   1921:        map = *mapp++;
                   1922:        bit = 1 << (start % NBBY);
                   1923:        for (i = start; i < end; i++) {
                   1924:                if ((map & bit) == 0)
                   1925:                        break;
                   1926:                if ((i & (NBBY - 1)) != (NBBY - 1)) {
                   1927:                        bit <<= 1;
                   1928:                } else {
                   1929:                        map = *mapp++;
                   1930:                        bit = 1;
                   1931:                }
                   1932:        }
                   1933:        forw = i - start;
                   1934:        /*
                   1935:         * Find the size of the cluster going backward.
                   1936:         */
                   1937:        start = blkno - 1;
                   1938:        end = start - fs->fs_contigsumsize;
                   1939:        if (end < 0)
                   1940:                end = -1;
                   1941:        mapp = &freemapp[start / NBBY];
                   1942:        map = *mapp--;
                   1943:        bit = 1 << (start % NBBY);
                   1944:        for (i = start; i > end; i--) {
                   1945:                if ((map & bit) == 0)
                   1946:                        break;
                   1947:                if ((i & (NBBY - 1)) != 0) {
                   1948:                        bit >>= 1;
                   1949:                } else {
                   1950:                        map = *mapp--;
                   1951:                        bit = 1 << (NBBY - 1);
                   1952:                }
                   1953:        }
                   1954:        back = start - i;
                   1955:        /*
                   1956:         * Account for old cluster and the possibly new forward and
                   1957:         * back clusters.
                   1958:         */
                   1959:        i = back + forw + 1;
                   1960:        if (i > fs->fs_contigsumsize)
                   1961:                i = fs->fs_contigsumsize;
1.19      bouyer   1962:        ufs_add32(sump[i], cnt, needswap);
1.1       mycroft  1963:        if (back > 0)
1.19      bouyer   1964:                ufs_add32(sump[back], -cnt, needswap);
1.1       mycroft  1965:        if (forw > 0)
1.19      bouyer   1966:                ufs_add32(sump[forw], -cnt, needswap);
                   1967:
1.5       mycroft  1968:        /*
                   1969:         * Update cluster summary information.
                   1970:         */
                   1971:        lp = &sump[fs->fs_contigsumsize];
                   1972:        for (i = fs->fs_contigsumsize; i > 0; i--)
1.19      bouyer   1973:                if (ufs_rw32(*lp--, needswap) > 0)
1.5       mycroft  1974:                        break;
1.19      bouyer   1975:        fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
1.1       mycroft  1976: }
                   1977:
                   1978: /*
                   1979:  * Fserr prints the name of a file system with an error diagnostic.
1.81      perry    1980:  *
1.1       mycroft  1981:  * The form of the error message is:
                   1982:  *     fs: error message
                   1983:  */
                   1984: static void
1.85      thorpej  1985: ffs_fserr(struct fs *fs, u_int uid, const char *cp)
1.1       mycroft  1986: {
                   1987:
1.64      gmcgarry 1988:        log(LOG_ERR, "uid %d, pid %d, command %s, on %s: %s\n",
                   1989:            uid, curproc->p_pid, curproc->p_comm, fs->fs_fsmnt, cp);
1.1       mycroft  1990: }

CVSweb <webmaster@jp.NetBSD.org>