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