[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.106.6.3

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

CVSweb <webmaster@jp.NetBSD.org>