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