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