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