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