Home | History | Annotate | Line # | Download | only in ffs
ffs_alloc.c revision 1.26.2.2
      1 /*	$NetBSD: ffs_alloc.c,v 1.26.2.2 1999/02/25 04:00:12 chs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)ffs_alloc.c	8.19 (Berkeley) 7/13/95
     36  */
     37 
     38 #if defined(_KERNEL) && !defined(_LKM)
     39 #include "opt_quota.h"
     40 #include "opt_uvm.h"
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/buf.h>
     46 #include <sys/proc.h>
     47 #include <sys/vnode.h>
     48 #include <sys/mount.h>
     49 #include <sys/kernel.h>
     50 #include <sys/syslog.h>
     51 
     52 #include <vm/vm.h>
     53 #ifdef UBC
     54 #include <uvm/uvm.h>
     55 #endif
     56 
     57 #include <ufs/ufs/quota.h>
     58 #include <ufs/ufs/ufsmount.h>
     59 #include <ufs/ufs/inode.h>
     60 #include <ufs/ufs/ufs_extern.h>
     61 #include <ufs/ufs/ufs_bswap.h>
     62 
     63 #include <ufs/ffs/fs.h>
     64 #include <ufs/ffs/ffs_extern.h>
     65 
     66 static ufs_daddr_t	ffs_alloccg __P((struct inode *, int, ufs_daddr_t, int));
     67 static ufs_daddr_t	ffs_alloccgblk __P((struct mount *, struct fs *,
     68 	struct cg *, ufs_daddr_t));
     69 static ufs_daddr_t	ffs_clusteralloc __P((struct inode *, int, ufs_daddr_t, int));
     70 static ino_t	ffs_dirpref __P((struct fs *));
     71 static ufs_daddr_t	ffs_fragextend __P((struct inode *, int, long, int, int));
     72 static void	ffs_fserr __P((struct fs *, u_int, char *));
     73 static u_long	ffs_hashalloc __P((struct inode *, int, long, int,
     74 				   ufs_daddr_t (*)(struct inode *, int, ufs_daddr_t,
     75 					       int)));
     76 static ufs_daddr_t	ffs_nodealloccg __P((struct inode *, int, ufs_daddr_t, int));
     77 static ufs_daddr_t	ffs_mapsearch __P((int, struct fs *, struct cg *,
     78 					ufs_daddr_t, int));
     79 #if defined(DIAGNOSTIC) || defined(DEBUG)
     80 static int ffs_checkblk __P((struct inode *, ufs_daddr_t, long size));
     81 #endif
     82 
     83 /* in ffs_tables.c */
     84 extern int inside[], around[];
     85 extern u_char *fragtbl[];
     86 
     87 /*
     88  * Allocate a block in the file system.
     89  *
     90  * The size of the requested block is given, which must be some
     91  * multiple of fs_fsize and <= fs_bsize.
     92  * A preference may be optionally specified. If a preference is given
     93  * the following hierarchy is used to allocate a block:
     94  *   1) allocate the requested block.
     95  *   2) allocate a rotationally optimal block in the same cylinder.
     96  *   3) allocate a block in the same cylinder group.
     97  *   4) quadradically rehash into other cylinder groups, until an
     98  *      available block is located.
     99  * If no block preference is given the following heirarchy is used
    100  * to allocate a block:
    101  *   1) allocate a block in the cylinder group that contains the
    102  *      inode for the file.
    103  *   2) quadradically rehash into other cylinder groups, until an
    104  *      available block is located.
    105  */
    106 int
    107 ffs_alloc(ip, lbn, bpref, size, cred, bnp)
    108 	register struct inode *ip;
    109 	ufs_daddr_t lbn, bpref;
    110 	int size;
    111 	struct ucred *cred;
    112 	ufs_daddr_t *bnp;
    113 {
    114 	register struct fs *fs;
    115 	ufs_daddr_t bno;
    116 	int cg;
    117 #ifdef QUOTA
    118 	int error;
    119 #endif
    120 
    121 	*bnp = 0;
    122 	fs = ip->i_fs;
    123 #ifdef DIAGNOSTIC
    124 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
    125 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
    126 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
    127 		panic("ffs_alloc: bad size");
    128 	}
    129 	if (cred == NOCRED)
    130 		panic("ffs_alloc: missing credential\n");
    131 #endif /* DIAGNOSTIC */
    132 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
    133 		goto nospace;
    134 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
    135 		goto nospace;
    136 #ifdef QUOTA
    137 	if ((error = chkdq(ip, (long)btodb(size), cred, 0)) != 0)
    138 		return (error);
    139 #endif
    140 	if (bpref >= fs->fs_size)
    141 		bpref = 0;
    142 	if (bpref == 0)
    143 		cg = ino_to_cg(fs, ip->i_number);
    144 	else
    145 		cg = dtog(fs, bpref);
    146 	bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
    147 	    			     ffs_alloccg);
    148 	if (bno > 0) {
    149 		ip->i_ffs_blocks += btodb(size);
    150 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    151 		*bnp = bno;
    152 		return (0);
    153 	}
    154 #ifdef QUOTA
    155 	/*
    156 	 * Restore user's disk quota because allocation failed.
    157 	 */
    158 	(void) chkdq(ip, (long)-btodb(size), cred, FORCE);
    159 #endif
    160 nospace:
    161 	ffs_fserr(fs, cred->cr_uid, "file system full");
    162 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
    163 	return (ENOSPC);
    164 }
    165 
    166 /*
    167  * Reallocate a fragment to a bigger size
    168  *
    169  * The number and size of the old block is given, and a preference
    170  * and new size is also specified. The allocator attempts to extend
    171  * the original block. Failing that, the regular block allocator is
    172  * invoked to get an appropriate block.
    173  */
    174 int
    175 ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp, blknop)
    176 	register struct inode *ip;
    177 	ufs_daddr_t lbprev;
    178 	ufs_daddr_t bpref;
    179 	int osize, nsize;
    180 	struct ucred *cred;
    181 	struct buf **bpp;
    182 	ufs_daddr_t *blknop;
    183 {
    184 	register struct fs *fs;
    185 	struct buf *bp;
    186 	int cg, request, error;
    187 	ufs_daddr_t bprev, bno;
    188 
    189 	*bpp = 0;
    190 	fs = ip->i_fs;
    191 #ifdef DIAGNOSTIC
    192 	if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
    193 	    (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
    194 		printf(
    195 		    "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
    196 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
    197 		panic("ffs_realloccg: bad size");
    198 	}
    199 	if (cred == NOCRED)
    200 		panic("ffs_realloccg: missing credential\n");
    201 #endif /* DIAGNOSTIC */
    202 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
    203 		goto nospace;
    204 	if ((bprev = ufs_rw32(ip->i_ffs_db[lbprev], UFS_IPNEEDSWAP(ip))) == 0) {
    205 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
    206 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
    207 		panic("ffs_realloccg: bad bprev");
    208 	}
    209 
    210 #ifdef QUOTA
    211 	if ((error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) != 0) {
    212 		return (error);
    213 	}
    214 #endif
    215 
    216 	/*
    217 	 * Allocate the extra space in the buffer.
    218 	 */
    219 	if (bpp != NULL &&
    220 	    (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) != 0) {
    221 		brelse(bp);
    222 		return (error);
    223 	}
    224 
    225 	/*
    226 	 * Check for extension in the existing location.
    227 	 */
    228 	cg = dtog(fs, bprev);
    229 	if ((bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) != 0) {
    230 		ip->i_ffs_blocks += btodb(nsize - osize);
    231 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    232 
    233 		if (bpp != NULL) {
    234 			if (bp->b_blkno != fsbtodb(fs, bno))
    235 				panic("bad blockno");
    236 			allocbuf(bp, nsize);
    237 			bp->b_flags |= B_DONE;
    238 			memset(bp->b_data + osize, 0, nsize - osize);
    239 			*bpp = bp;
    240 		}
    241 		else {
    242 			/*
    243 			 * XXX do page-cache stuff.
    244 			 * I think we don't need to do anything,
    245 			 * assuming pages are always zeroed when alloc'd.
    246 			 */
    247 		}
    248 
    249 		*blknop = bno;
    250 		return (0);
    251 	}
    252 	/*
    253 	 * Allocate a new disk location.
    254 	 */
    255 	if (bpref >= fs->fs_size)
    256 		bpref = 0;
    257 	switch ((int)fs->fs_optim) {
    258 	case FS_OPTSPACE:
    259 		/*
    260 		 * Allocate an exact sized fragment. Although this makes
    261 		 * best use of space, we will waste time relocating it if
    262 		 * the file continues to grow. If the fragmentation is
    263 		 * less than half of the minimum free reserve, we choose
    264 		 * to begin optimizing for time.
    265 		 */
    266 		request = nsize;
    267 		if (fs->fs_minfree < 5 ||
    268 		    fs->fs_cstotal.cs_nffree >
    269 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
    270 			break;
    271 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
    272 			fs->fs_fsmnt);
    273 		fs->fs_optim = FS_OPTTIME;
    274 		break;
    275 	case FS_OPTTIME:
    276 		/*
    277 		 * At this point we have discovered a file that is trying to
    278 		 * grow a small fragment to a larger fragment. To save time,
    279 		 * we allocate a full sized block, then free the unused portion.
    280 		 * If the file continues to grow, the `ffs_fragextend' call
    281 		 * above will be able to grow it in place without further
    282 		 * copying. If aberrant programs cause disk fragmentation to
    283 		 * grow within 2% of the free reserve, we choose to begin
    284 		 * optimizing for space.
    285 		 */
    286 		request = fs->fs_bsize;
    287 		if (fs->fs_cstotal.cs_nffree <
    288 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
    289 			break;
    290 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
    291 			fs->fs_fsmnt);
    292 		fs->fs_optim = FS_OPTSPACE;
    293 		break;
    294 	default:
    295 		printf("dev = 0x%x, optim = %d, fs = %s\n",
    296 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
    297 		panic("ffs_realloccg: bad optim");
    298 		/* NOTREACHED */
    299 	}
    300 	bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
    301 	    			     ffs_alloccg);
    302 	if (bno > 0) {
    303 
    304 		if (bpp != NULL) {
    305 			bp->b_blkno = fsbtodb(fs, bno);
    306 		}
    307 		else {
    308 			/*
    309 			 * re-label cached pages, if any
    310 			 */
    311 			uvm_vnp_setpageblknos(ITOV(ip), lblktosize(fs, lbprev),
    312 					      blkoff(fs, ip->i_ffs_size),
    313 					      fsbtodb(fs, bno), UFP_NOALLOC,
    314 					      FALSE);
    315 		}
    316 
    317 #if defined(UVM)
    318 		(void) uvm_vnp_uncache(ITOV(ip));
    319 #else
    320 		(void) vnode_pager_uncache(ITOV(ip));
    321 #endif
    322 
    323 		ffs_blkfree(ip, bprev, (long)osize);
    324 		if (nsize < request)
    325 			ffs_blkfree(ip, bno + numfrags(fs, nsize),
    326 			    (long)(request - nsize));
    327 		ip->i_ffs_blocks += btodb(nsize - osize);
    328 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    329 
    330 		if (bpp != NULL) {
    331 			allocbuf(bp, nsize);
    332 			bp->b_flags |= B_DONE;
    333 			memset(bp->b_data + osize, 0, (u_int)nsize - osize);
    334 			*bpp = bp;
    335 		}
    336 
    337 		*blknop = bno;
    338 		return (0);
    339 	}
    340 #ifdef QUOTA
    341 	/*
    342 	 * Restore user's disk quota because allocation failed.
    343 	 */
    344 	(void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
    345 #endif
    346 
    347 	if (bpp != NULL) {
    348 		brelse(bp);
    349 	}
    350 
    351 nospace:
    352 	/*
    353 	 * no space available
    354 	 */
    355 	ffs_fserr(fs, cred->cr_uid, "file system full");
    356 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
    357 	return (ENOSPC);
    358 }
    359 
    360 /*
    361  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
    362  *
    363  * The vnode and an array of buffer pointers for a range of sequential
    364  * logical blocks to be made contiguous is given. The allocator attempts
    365  * to find a range of sequential blocks starting as close as possible to
    366  * an fs_rotdelay offset from the end of the allocation for the logical
    367  * block immediately preceeding the current range. If successful, the
    368  * physical block numbers in the buffer pointers and in the inode are
    369  * changed to reflect the new allocation. If unsuccessful, the allocation
    370  * is left unchanged. The success in doing the reallocation is returned.
    371  * Note that the error return is not reflected back to the user. Rather
    372  * the previous block allocation will be used.
    373  */
    374 #ifdef DEBUG
    375 #include <sys/sysctl.h>
    376 int prtrealloc = 0;
    377 struct ctldebug debug15 = { "prtrealloc", &prtrealloc };
    378 #endif
    379 
    380 int doasyncfree = 1;
    381 extern int doreallocblks;
    382 
    383 int
    384 ffs_reallocblks(v)
    385 	void *v;
    386 {
    387 	struct vop_reallocblks_args /* {
    388 		struct vnode *a_vp;
    389 		struct cluster_save *a_buflist;
    390 	} */ *ap = v;
    391 	struct fs *fs;
    392 	struct inode *ip;
    393 	struct vnode *vp;
    394 	struct buf *sbp, *ebp;
    395 	ufs_daddr_t *bap, *sbap, *ebap = NULL;
    396 	struct cluster_save *buflist;
    397 	ufs_daddr_t start_lbn, end_lbn, soff, newblk, blkno;
    398 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
    399 	int i, len, start_lvl, end_lvl, pref, ssize;
    400 	struct timespec ts;
    401 
    402 #ifdef UBC
    403 	/* XXX don't do this for now */
    404 	return ENOSPC;
    405 #endif
    406 
    407 	vp = ap->a_vp;
    408 	ip = VTOI(vp);
    409 	fs = ip->i_fs;
    410 	if (fs->fs_contigsumsize <= 0)
    411 		return (ENOSPC);
    412 	buflist = ap->a_buflist;
    413 	len = buflist->bs_nchildren;
    414 	start_lbn = buflist->bs_children[0]->b_lblkno;
    415 	end_lbn = start_lbn + len - 1;
    416 #ifdef DIAGNOSTIC
    417 	for (i = 0; i < len; i++)
    418 		if (!ffs_checkblk(ip,
    419 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
    420 			panic("ffs_reallocblks: unallocated block 1");
    421 	for (i = 1; i < len; i++)
    422 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
    423 			panic("ffs_reallocblks: non-logical cluster");
    424 	blkno = buflist->bs_children[0]->b_blkno;
    425 	ssize = fsbtodb(fs, fs->fs_frag);
    426 	for (i = 1; i < len - 1; i++)
    427 		if (buflist->bs_children[i]->b_blkno != blkno + (i * ssize))
    428 			panic("ffs_reallocblks: non-physical cluster %d", i);
    429 #endif
    430 	/*
    431 	 * If the latest allocation is in a new cylinder group, assume that
    432 	 * the filesystem has decided to move and do not force it back to
    433 	 * the previous cylinder group.
    434 	 */
    435 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
    436 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
    437 		return (ENOSPC);
    438 	if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
    439 	    ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
    440 		return (ENOSPC);
    441 	/*
    442 	 * Get the starting offset and block map for the first block.
    443 	 */
    444 	if (start_lvl == 0) {
    445 		sbap = &ip->i_ffs_db[0];
    446 		soff = start_lbn;
    447 	} else {
    448 		idp = &start_ap[start_lvl - 1];
    449 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
    450 			brelse(sbp);
    451 			return (ENOSPC);
    452 		}
    453 		sbap = (ufs_daddr_t *)sbp->b_data;
    454 		soff = idp->in_off;
    455 	}
    456 	/*
    457 	 * Find the preferred location for the cluster.
    458 	 */
    459 	pref = ffs_blkpref(ip, start_lbn, soff, sbap);
    460 	/*
    461 	 * If the block range spans two block maps, get the second map.
    462 	 */
    463 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
    464 		ssize = len;
    465 	} else {
    466 #ifdef DIAGNOSTIC
    467 		if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
    468 			panic("ffs_reallocblk: start == end");
    469 #endif
    470 		ssize = len - (idp->in_off + 1);
    471 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
    472 			goto fail;
    473 		ebap = (ufs_daddr_t *)ebp->b_data;
    474 	}
    475 	/*
    476 	 * Search the block map looking for an allocation of the desired size.
    477 	 */
    478 	if ((newblk = (ufs_daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
    479 	    len, ffs_clusteralloc)) == 0)
    480 		goto fail;
    481 	/*
    482 	 * We have found a new contiguous block.
    483 	 *
    484 	 * First we have to replace the old block pointers with the new
    485 	 * block pointers in the inode and indirect blocks associated
    486 	 * with the file.
    487 	 */
    488 #ifdef DEBUG
    489 	if (prtrealloc)
    490 		printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
    491 		    start_lbn, end_lbn);
    492 #endif
    493 	blkno = newblk;
    494 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
    495 		if (i == ssize)
    496 			bap = ebap;
    497 #ifdef DIAGNOSTIC
    498 		if (!ffs_checkblk(ip,
    499 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
    500 			panic("ffs_reallocblks: unallocated block 2");
    501 		if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) !=
    502 			ufs_rw32(*bap, UFS_MPNEEDSWAP(vp->v_mount)))
    503 			panic("ffs_reallocblks: alloc mismatch");
    504 #endif
    505 #ifdef DEBUG
    506 		if (prtrealloc)
    507 			printf(" %d,", ufs_rw32(*bap, UFS_MPNEEDSWAP(vp->v_mount)));
    508 #endif
    509 		*bap++ = ufs_rw32(blkno, UFS_MPNEEDSWAP(vp->v_mount));
    510 	}
    511 	/*
    512 	 * Next we must write out the modified inode and indirect blocks.
    513 	 * For strict correctness, the writes should be synchronous since
    514 	 * the old block values may have been written to disk. In practise
    515 	 * they are almost never written, but if we are concerned about
    516 	 * strict correctness, the `doasyncfree' flag should be set to zero.
    517 	 *
    518 	 * The test on `doasyncfree' should be changed to test a flag
    519 	 * that shows whether the associated buffers and inodes have
    520 	 * been written. The flag should be set when the cluster is
    521 	 * started and cleared whenever the buffer or inode is flushed.
    522 	 * We can then check below to see if it is set, and do the
    523 	 * synchronous write only when it has been cleared.
    524 	 */
    525 	if (sbap != &ip->i_ffs_db[0]) {
    526 		if (doasyncfree)
    527 			bdwrite(sbp);
    528 		else
    529 			bwrite(sbp);
    530 	} else {
    531 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    532 		if (!doasyncfree) {
    533 			TIMEVAL_TO_TIMESPEC(&time, &ts);
    534 			VOP_UPDATE(vp, &ts, &ts, 1);
    535 		}
    536 	}
    537 	if (ssize < len) {
    538 		if (doasyncfree)
    539 			bdwrite(ebp);
    540 		else
    541 			bwrite(ebp);
    542 	}
    543 	/*
    544 	 * Last, free the old blocks and assign the new blocks to the buffers.
    545 	 */
    546 #ifdef DEBUG
    547 	if (prtrealloc)
    548 		printf("\n\tnew:");
    549 #endif
    550 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
    551 		ffs_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
    552 		    fs->fs_bsize);
    553 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
    554 #ifdef DEBUG
    555 		if (!ffs_checkblk(ip,
    556 		   dbtofsb(fs, buflist->bs_children[i]->b_blkno), fs->fs_bsize))
    557 			panic("ffs_reallocblks: unallocated block 3");
    558 		if (prtrealloc)
    559 			printf(" %d,", blkno);
    560 #endif
    561 	}
    562 #ifdef DEBUG
    563 	if (prtrealloc) {
    564 		prtrealloc--;
    565 		printf("\n");
    566 	}
    567 #endif
    568 	return (0);
    569 
    570 fail:
    571 	if (ssize < len)
    572 		brelse(ebp);
    573 	if (sbap != &ip->i_ffs_db[0])
    574 		brelse(sbp);
    575 	return (ENOSPC);
    576 }
    577 
    578 /*
    579  * Allocate an inode in the file system.
    580  *
    581  * If allocating a directory, use ffs_dirpref to select the inode.
    582  * If allocating in a directory, the following hierarchy is followed:
    583  *   1) allocate the preferred inode.
    584  *   2) allocate an inode in the same cylinder group.
    585  *   3) quadradically rehash into other cylinder groups, until an
    586  *      available inode is located.
    587  * If no inode preference is given the following heirarchy is used
    588  * to allocate an inode:
    589  *   1) allocate an inode in cylinder group 0.
    590  *   2) quadradically rehash into other cylinder groups, until an
    591  *      available inode is located.
    592  */
    593 int
    594 ffs_valloc(v)
    595 	void *v;
    596 {
    597 	struct vop_valloc_args /* {
    598 		struct vnode *a_pvp;
    599 		int a_mode;
    600 		struct ucred *a_cred;
    601 		struct vnode **a_vpp;
    602 	} */ *ap = v;
    603 	register struct vnode *pvp = ap->a_pvp;
    604 	register struct inode *pip;
    605 	register struct fs *fs;
    606 	register struct inode *ip;
    607 	mode_t mode = ap->a_mode;
    608 	ino_t ino, ipref;
    609 	int cg, error;
    610 
    611 	*ap->a_vpp = NULL;
    612 	pip = VTOI(pvp);
    613 	fs = pip->i_fs;
    614 	if (fs->fs_cstotal.cs_nifree == 0)
    615 		goto noinodes;
    616 
    617 	if ((mode & IFMT) == IFDIR)
    618 		ipref = ffs_dirpref(fs);
    619 	else
    620 		ipref = pip->i_number;
    621 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
    622 		ipref = 0;
    623 	cg = ino_to_cg(fs, ipref);
    624 	ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
    625 	if (ino == 0)
    626 		goto noinodes;
    627 	error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
    628 	if (error) {
    629 		VOP_VFREE(pvp, ino, mode);
    630 		return (error);
    631 	}
    632 	ip = VTOI(*ap->a_vpp);
    633 	if (ip->i_ffs_mode) {
    634 		printf("mode = 0%o, inum = %d, fs = %s\n",
    635 		    ip->i_ffs_mode, ip->i_number, fs->fs_fsmnt);
    636 		panic("ffs_valloc: dup alloc");
    637 	}
    638 	if (ip->i_ffs_blocks) {				/* XXX */
    639 		printf("free inode %s/%d had %d blocks\n",
    640 		    fs->fs_fsmnt, ino, ip->i_ffs_blocks);
    641 		ip->i_ffs_blocks = 0;
    642 	}
    643 	ip->i_ffs_flags = 0;
    644 	/*
    645 	 * Set up a new generation number for this inode.
    646 	 */
    647 	ip->i_ffs_gen++;
    648 	return (0);
    649 noinodes:
    650 	ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
    651 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
    652 	return (ENOSPC);
    653 }
    654 
    655 /*
    656  * Find a cylinder to place a directory.
    657  *
    658  * The policy implemented by this algorithm is to select from
    659  * among those cylinder groups with above the average number of
    660  * free inodes, the one with the smallest number of directories.
    661  */
    662 static ino_t
    663 ffs_dirpref(fs)
    664 	register struct fs *fs;
    665 {
    666 	int cg, minndir, mincg, avgifree;
    667 
    668 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
    669 	minndir = fs->fs_ipg;
    670 	mincg = 0;
    671 	for (cg = 0; cg < fs->fs_ncg; cg++)
    672 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
    673 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
    674 			mincg = cg;
    675 			minndir = fs->fs_cs(fs, cg).cs_ndir;
    676 		}
    677 	return ((ino_t)(fs->fs_ipg * mincg));
    678 }
    679 
    680 /*
    681  * Select the desired position for the next block in a file.  The file is
    682  * logically divided into sections. The first section is composed of the
    683  * direct blocks. Each additional section contains fs_maxbpg blocks.
    684  *
    685  * If no blocks have been allocated in the first section, the policy is to
    686  * request a block in the same cylinder group as the inode that describes
    687  * the file. If no blocks have been allocated in any other section, the
    688  * policy is to place the section in a cylinder group with a greater than
    689  * average number of free blocks.  An appropriate cylinder group is found
    690  * by using a rotor that sweeps the cylinder groups. When a new group of
    691  * blocks is needed, the sweep begins in the cylinder group following the
    692  * cylinder group from which the previous allocation was made. The sweep
    693  * continues until a cylinder group with greater than the average number
    694  * of free blocks is found. If the allocation is for the first block in an
    695  * indirect block, the information on the previous allocation is unavailable;
    696  * here a best guess is made based upon the logical block number being
    697  * allocated.
    698  *
    699  * If a section is already partially allocated, the policy is to
    700  * contiguously allocate fs_maxcontig blocks.  The end of one of these
    701  * contiguous blocks and the beginning of the next is physically separated
    702  * so that the disk head will be in transit between them for at least
    703  * fs_rotdelay milliseconds.  This is to allow time for the processor to
    704  * schedule another I/O transfer.
    705  */
    706 ufs_daddr_t
    707 ffs_blkpref(ip, lbn, indx, bap)
    708 	struct inode *ip;
    709 	ufs_daddr_t lbn;
    710 	int indx;
    711 	ufs_daddr_t *bap;
    712 {
    713 	register struct fs *fs;
    714 	register int cg;
    715 	int avgbfree, startcg;
    716 	ufs_daddr_t nextblk;
    717 
    718 	fs = ip->i_fs;
    719 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
    720 		if (lbn < NDADDR) {
    721 			cg = ino_to_cg(fs, ip->i_number);
    722 			return (fs->fs_fpg * cg + fs->fs_frag);
    723 		}
    724 		/*
    725 		 * Find a cylinder with greater than average number of
    726 		 * unused data blocks.
    727 		 */
    728 		if (indx == 0 || bap[indx - 1] == 0)
    729 			startcg =
    730 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
    731 		else
    732 			startcg = dtog(fs,
    733 				ufs_rw32(bap[indx - 1], UFS_IPNEEDSWAP(ip)) + 1);
    734 		startcg %= fs->fs_ncg;
    735 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
    736 		for (cg = startcg; cg < fs->fs_ncg; cg++)
    737 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
    738 				fs->fs_cgrotor = cg;
    739 				return (fs->fs_fpg * cg + fs->fs_frag);
    740 			}
    741 		for (cg = 0; cg <= startcg; cg++)
    742 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
    743 				fs->fs_cgrotor = cg;
    744 				return (fs->fs_fpg * cg + fs->fs_frag);
    745 			}
    746 		return (NULL);
    747 	}
    748 	/*
    749 	 * One or more previous blocks have been laid out. If less
    750 	 * than fs_maxcontig previous blocks are contiguous, the
    751 	 * next block is requested contiguously, otherwise it is
    752 	 * requested rotationally delayed by fs_rotdelay milliseconds.
    753 	 */
    754 	nextblk = ufs_rw32(bap[indx - 1], UFS_IPNEEDSWAP(ip)) + fs->fs_frag;
    755 	if (indx < fs->fs_maxcontig ||
    756 		ufs_rw32(bap[indx - fs->fs_maxcontig], UFS_IPNEEDSWAP(ip)) +
    757 	    blkstofrags(fs, fs->fs_maxcontig) != nextblk)
    758 		return (nextblk);
    759 	if (fs->fs_rotdelay != 0)
    760 		/*
    761 		 * Here we convert ms of delay to frags as:
    762 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
    763 		 *	((sect/frag) * (ms/sec))
    764 		 * then round up to the next block.
    765 		 */
    766 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
    767 		    (NSPF(fs) * 1000), fs->fs_frag);
    768 	return (nextblk);
    769 }
    770 
    771 /*
    772  * Implement the cylinder overflow algorithm.
    773  *
    774  * The policy implemented by this algorithm is:
    775  *   1) allocate the block in its requested cylinder group.
    776  *   2) quadradically rehash on the cylinder group number.
    777  *   3) brute force search for a free block.
    778  */
    779 /*VARARGS5*/
    780 static u_long
    781 ffs_hashalloc(ip, cg, pref, size, allocator)
    782 	struct inode *ip;
    783 	int cg;
    784 	long pref;
    785 	int size;	/* size for data blocks, mode for inodes */
    786 	ufs_daddr_t (*allocator) __P((struct inode *, int, ufs_daddr_t, int));
    787 {
    788 	register struct fs *fs;
    789 	long result;
    790 	int i, icg = cg;
    791 
    792 	fs = ip->i_fs;
    793 	/*
    794 	 * 1: preferred cylinder group
    795 	 */
    796 	result = (*allocator)(ip, cg, pref, size);
    797 	if (result)
    798 		return (result);
    799 	/*
    800 	 * 2: quadratic rehash
    801 	 */
    802 	for (i = 1; i < fs->fs_ncg; i *= 2) {
    803 		cg += i;
    804 		if (cg >= fs->fs_ncg)
    805 			cg -= fs->fs_ncg;
    806 		result = (*allocator)(ip, cg, 0, size);
    807 		if (result)
    808 			return (result);
    809 	}
    810 	/*
    811 	 * 3: brute force search
    812 	 * Note that we start at i == 2, since 0 was checked initially,
    813 	 * and 1 is always checked in the quadratic rehash.
    814 	 */
    815 	cg = (icg + 2) % fs->fs_ncg;
    816 	for (i = 2; i < fs->fs_ncg; i++) {
    817 		result = (*allocator)(ip, cg, 0, size);
    818 		if (result)
    819 			return (result);
    820 		cg++;
    821 		if (cg == fs->fs_ncg)
    822 			cg = 0;
    823 	}
    824 	return (NULL);
    825 }
    826 
    827 /*
    828  * Determine whether a fragment can be extended.
    829  *
    830  * Check to see if the necessary fragments are available, and
    831  * if they are, allocate them.
    832  */
    833 static ufs_daddr_t
    834 ffs_fragextend(ip, cg, bprev, osize, nsize)
    835 	struct inode *ip;
    836 	int cg;
    837 	long bprev;
    838 	int osize, nsize;
    839 {
    840 	register struct fs *fs;
    841 	register struct cg *cgp;
    842 	struct buf *bp;
    843 	long bno;
    844 	int frags, bbase;
    845 	int i, error;
    846 
    847 	fs = ip->i_fs;
    848 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
    849 		return (NULL);
    850 	frags = numfrags(fs, nsize);
    851 	bbase = fragnum(fs, bprev);
    852 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
    853 		/* cannot extend across a block boundary */
    854 		return (NULL);
    855 	}
    856 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
    857 		(int)fs->fs_cgsize, NOCRED, &bp);
    858 	if (error) {
    859 		brelse(bp);
    860 		return (NULL);
    861 	}
    862 	cgp = (struct cg *)bp->b_data;
    863 	if (!cg_chkmagic(cgp, UFS_IPNEEDSWAP(ip))) {
    864 		brelse(bp);
    865 		return (NULL);
    866 	}
    867 	cgp->cg_time = ufs_rw32(time.tv_sec, UFS_IPNEEDSWAP(ip));
    868 	bno = dtogd(fs, bprev);
    869 	for (i = numfrags(fs, osize); i < frags; i++)
    870 		if (isclr(cg_blksfree(cgp, UFS_IPNEEDSWAP(ip)), bno + i)) {
    871 			brelse(bp);
    872 			return (NULL);
    873 		}
    874 	/*
    875 	 * the current fragment can be extended
    876 	 * deduct the count on fragment being extended into
    877 	 * increase the count on the remaining fragment (if any)
    878 	 * allocate the extended piece
    879 	 */
    880 	for (i = frags; i < fs->fs_frag - bbase; i++)
    881 		if (isclr(cg_blksfree(cgp, UFS_IPNEEDSWAP(ip)), bno + i))
    882 			break;
    883 	ufs_add32(cgp->cg_frsum[i - numfrags(fs, osize)], -1, UFS_IPNEEDSWAP(ip));
    884 	if (i != frags)
    885 		ufs_add32(cgp->cg_frsum[i - frags], 1, UFS_IPNEEDSWAP(ip));
    886 	for (i = numfrags(fs, osize); i < frags; i++) {
    887 		clrbit(cg_blksfree(cgp, UFS_IPNEEDSWAP(ip)), bno + i);
    888 		ufs_add32(cgp->cg_cs.cs_nffree, -1, UFS_IPNEEDSWAP(ip));
    889 		fs->fs_cstotal.cs_nffree--;
    890 		fs->fs_cs(fs, cg).cs_nffree--;
    891 	}
    892 	fs->fs_fmod = 1;
    893 	bdwrite(bp);
    894 	return (bprev);
    895 }
    896 
    897 /*
    898  * Determine whether a block can be allocated.
    899  *
    900  * Check to see if a block of the appropriate size is available,
    901  * and if it is, allocate it.
    902  */
    903 static ufs_daddr_t
    904 ffs_alloccg(ip, cg, bpref, size)
    905 	struct inode *ip;
    906 	int cg;
    907 	ufs_daddr_t bpref;
    908 	int size;
    909 {
    910 	register struct fs *fs;
    911 	register struct cg *cgp;
    912 	struct buf *bp;
    913 	register int i;
    914 	int error, bno, frags, allocsiz;
    915 	const int needswap = UFS_IPNEEDSWAP(ip);
    916 
    917 	fs = ip->i_fs;
    918 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
    919 		return (NULL);
    920 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
    921 		(int)fs->fs_cgsize, NOCRED, &bp);
    922 	if (error) {
    923 		brelse(bp);
    924 		return (NULL);
    925 	}
    926 	cgp = (struct cg *)bp->b_data;
    927 	if (!cg_chkmagic(cgp, needswap) ||
    928 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
    929 		brelse(bp);
    930 		return (NULL);
    931 	}
    932 	cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
    933 	if (size == fs->fs_bsize) {
    934 		bno = ffs_alloccgblk(ITOV(ip)->v_mount, fs, cgp, bpref);
    935 		bdwrite(bp);
    936 		return (bno);
    937 	}
    938 	/*
    939 	 * check to see if any fragments are already available
    940 	 * allocsiz is the size which will be allocated, hacking
    941 	 * it down to a smaller size if necessary
    942 	 */
    943 	frags = numfrags(fs, size);
    944 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
    945 		if (cgp->cg_frsum[allocsiz] != 0)
    946 			break;
    947 	if (allocsiz == fs->fs_frag) {
    948 		/*
    949 		 * no fragments were available, so a block will be
    950 		 * allocated, and hacked up
    951 		 */
    952 		if (cgp->cg_cs.cs_nbfree == 0) {
    953 			brelse(bp);
    954 			return (NULL);
    955 		}
    956 		bno = ffs_alloccgblk(ITOV(ip)->v_mount, fs, cgp, bpref);
    957 		bpref = dtogd(fs, bno);
    958 		for (i = frags; i < fs->fs_frag; i++)
    959 			setbit(cg_blksfree(cgp, needswap), bpref + i);
    960 		i = fs->fs_frag - frags;
    961 		ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
    962 		fs->fs_cstotal.cs_nffree += i;
    963 		fs->fs_cs(fs, cg).cs_nffree +=i;
    964 		fs->fs_fmod = 1;
    965 		ufs_add32(cgp->cg_frsum[i], 1, needswap);
    966 		bdwrite(bp);
    967 		return (bno);
    968 	}
    969 	bno = ffs_mapsearch(needswap, fs, cgp, bpref, allocsiz);
    970 	if (bno < 0) {
    971 		brelse(bp);
    972 		return (NULL);
    973 	}
    974 	for (i = 0; i < frags; i++)
    975 		clrbit(cg_blksfree(cgp, needswap), bno + i);
    976 	ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
    977 	fs->fs_cstotal.cs_nffree -= frags;
    978 	fs->fs_cs(fs, cg).cs_nffree -= frags;
    979 	fs->fs_fmod = 1;
    980 	ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
    981 	if (frags != allocsiz)
    982 		ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
    983 	bdwrite(bp);
    984 	return (cg * fs->fs_fpg + bno);
    985 }
    986 
    987 /*
    988  * Allocate a block in a cylinder group.
    989  *
    990  * This algorithm implements the following policy:
    991  *   1) allocate the requested block.
    992  *   2) allocate a rotationally optimal block in the same cylinder.
    993  *   3) allocate the next available block on the block rotor for the
    994  *      specified cylinder group.
    995  * Note that this routine only allocates fs_bsize blocks; these
    996  * blocks may be fragmented by the routine that allocates them.
    997  */
    998 static ufs_daddr_t
    999 ffs_alloccgblk(mp, fs, cgp, bpref)
   1000 	struct  mount *mp;
   1001 	register struct fs *fs;
   1002 	register struct cg *cgp;
   1003 	ufs_daddr_t bpref;
   1004 {
   1005 	ufs_daddr_t bno, blkno;
   1006 	int cylno, pos, delta;
   1007 	short *cylbp;
   1008 	register int i;
   1009 	const int needswap = UFS_MPNEEDSWAP(mp);
   1010 
   1011 	if (bpref == 0 ||
   1012 		dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
   1013 		bpref = ufs_rw32(cgp->cg_rotor, needswap);
   1014 		goto norot;
   1015 	}
   1016 	bpref = blknum(fs, bpref);
   1017 	bpref = dtogd(fs, bpref);
   1018 	/*
   1019 	 * if the requested block is available, use it
   1020 	 */
   1021 	if (ffs_isblock(fs, cg_blksfree(cgp, needswap),
   1022 		fragstoblks(fs, bpref))) {
   1023 		bno = bpref;
   1024 		goto gotit;
   1025 	}
   1026 	if (fs->fs_nrpos <= 1 || fs->fs_cpc == 0) {
   1027 		/*
   1028 		 * Block layout information is not available.
   1029 		 * Leaving bpref unchanged means we take the
   1030 		 * next available free block following the one
   1031 		 * we just allocated. Hopefully this will at
   1032 		 * least hit a track cache on drives of unknown
   1033 		 * geometry (e.g. SCSI).
   1034 		 */
   1035 		goto norot;
   1036 	}
   1037 	/*
   1038 	 * check for a block available on the same cylinder
   1039 	 */
   1040 	cylno = cbtocylno(fs, bpref);
   1041 	if (cg_blktot(cgp, needswap)[cylno] == 0)
   1042 		goto norot;
   1043 	/*
   1044 	 * check the summary information to see if a block is
   1045 	 * available in the requested cylinder starting at the
   1046 	 * requested rotational position and proceeding around.
   1047 	 */
   1048 	cylbp = cg_blks(fs, cgp, cylno, needswap);
   1049 	pos = cbtorpos(fs, bpref);
   1050 	for (i = pos; i < fs->fs_nrpos; i++)
   1051 		if (ufs_rw16(cylbp[i], needswap) > 0)
   1052 			break;
   1053 	if (i == fs->fs_nrpos)
   1054 		for (i = 0; i < pos; i++)
   1055 			if (ufs_rw16(cylbp[i], needswap) > 0)
   1056 				break;
   1057 	if (ufs_rw16(cylbp[i], needswap) > 0) {
   1058 		/*
   1059 		 * found a rotational position, now find the actual
   1060 		 * block. A panic if none is actually there.
   1061 		 */
   1062 		pos = cylno % fs->fs_cpc;
   1063 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
   1064 		if (fs_postbl(fs, pos)[i] == -1) {
   1065 			printf("pos = %d, i = %d, fs = %s\n",
   1066 			    pos, i, fs->fs_fsmnt);
   1067 			panic("ffs_alloccgblk: cyl groups corrupted");
   1068 		}
   1069 		for (i = fs_postbl(fs, pos)[i];; ) {
   1070 			if (ffs_isblock(fs, cg_blksfree(cgp, needswap), bno + i)) {
   1071 				bno = blkstofrags(fs, (bno + i));
   1072 				goto gotit;
   1073 			}
   1074 			delta = fs_rotbl(fs)[i];
   1075 			if (delta <= 0 ||
   1076 			    delta + i > fragstoblks(fs, fs->fs_fpg))
   1077 				break;
   1078 			i += delta;
   1079 		}
   1080 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
   1081 		panic("ffs_alloccgblk: can't find blk in cyl");
   1082 	}
   1083 norot:
   1084 	/*
   1085 	 * no blocks in the requested cylinder, so take next
   1086 	 * available one in this cylinder group.
   1087 	 */
   1088 	bno = ffs_mapsearch(needswap, fs, cgp, bpref, (int)fs->fs_frag);
   1089 	if (bno < 0)
   1090 		return (NULL);
   1091 	cgp->cg_rotor = ufs_rw32(bno, needswap);
   1092 gotit:
   1093 	blkno = fragstoblks(fs, bno);
   1094 	ffs_clrblock(fs, cg_blksfree(cgp, needswap), (long)blkno);
   1095 	ffs_clusteracct(needswap, fs, cgp, blkno, -1);
   1096 	ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
   1097 	fs->fs_cstotal.cs_nbfree--;
   1098 	fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
   1099 	cylno = cbtocylno(fs, bno);
   1100 	ufs_add16(cg_blks(fs, cgp, cylno, needswap)[cbtorpos(fs, bno)], -1,
   1101 		needswap);
   1102 	ufs_add32(cg_blktot(cgp, needswap)[cylno], -1, needswap);
   1103 	fs->fs_fmod = 1;
   1104 	return (ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno);
   1105 }
   1106 
   1107 /*
   1108  * Determine whether a cluster can be allocated.
   1109  *
   1110  * We do not currently check for optimal rotational layout if there
   1111  * are multiple choices in the same cylinder group. Instead we just
   1112  * take the first one that we find following bpref.
   1113  */
   1114 static ufs_daddr_t
   1115 ffs_clusteralloc(ip, cg, bpref, len)
   1116 	struct inode *ip;
   1117 	int cg;
   1118 	ufs_daddr_t bpref;
   1119 	int len;
   1120 {
   1121 	register struct fs *fs;
   1122 	register struct cg *cgp;
   1123 	struct buf *bp;
   1124 	int i, got, run, bno, bit, map;
   1125 	u_char *mapp;
   1126 	int32_t *lp;
   1127 
   1128 	fs = ip->i_fs;
   1129 	if (fs->fs_maxcluster[cg] < len)
   1130 		return (NULL);
   1131 	if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
   1132 	    NOCRED, &bp))
   1133 		goto fail;
   1134 	cgp = (struct cg *)bp->b_data;
   1135 	if (!cg_chkmagic(cgp, UFS_IPNEEDSWAP(ip)))
   1136 		goto fail;
   1137 	/*
   1138 	 * Check to see if a cluster of the needed size (or bigger) is
   1139 	 * available in this cylinder group.
   1140 	 */
   1141 	lp = &cg_clustersum(cgp, UFS_IPNEEDSWAP(ip))[len];
   1142 	for (i = len; i <= fs->fs_contigsumsize; i++)
   1143 		if (ufs_rw32(*lp++, UFS_IPNEEDSWAP(ip)) > 0)
   1144 			break;
   1145 	if (i > fs->fs_contigsumsize) {
   1146 		/*
   1147 		 * This is the first time looking for a cluster in this
   1148 		 * cylinder group. Update the cluster summary information
   1149 		 * to reflect the true maximum sized cluster so that
   1150 		 * future cluster allocation requests can avoid reading
   1151 		 * the cylinder group map only to find no clusters.
   1152 		 */
   1153 		lp = &cg_clustersum(cgp, UFS_IPNEEDSWAP(ip))[len - 1];
   1154 		for (i = len - 1; i > 0; i--)
   1155 			if (ufs_rw32(*lp--, UFS_IPNEEDSWAP(ip)) > 0)
   1156 				break;
   1157 		fs->fs_maxcluster[cg] = i;
   1158 		goto fail;
   1159 	}
   1160 	/*
   1161 	 * Search the cluster map to find a big enough cluster.
   1162 	 * We take the first one that we find, even if it is larger
   1163 	 * than we need as we prefer to get one close to the previous
   1164 	 * block allocation. We do not search before the current
   1165 	 * preference point as we do not want to allocate a block
   1166 	 * that is allocated before the previous one (as we will
   1167 	 * then have to wait for another pass of the elevator
   1168 	 * algorithm before it will be read). We prefer to fail and
   1169 	 * be recalled to try an allocation in the next cylinder group.
   1170 	 */
   1171 	if (dtog(fs, bpref) != cg)
   1172 		bpref = 0;
   1173 	else
   1174 		bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
   1175 	mapp = &cg_clustersfree(cgp, UFS_IPNEEDSWAP(ip))[bpref / NBBY];
   1176 	map = *mapp++;
   1177 	bit = 1 << (bpref % NBBY);
   1178 	for (run = 0, got = bpref;
   1179 		got < ufs_rw32(cgp->cg_nclusterblks, UFS_IPNEEDSWAP(ip)); got++) {
   1180 		if ((map & bit) == 0) {
   1181 			run = 0;
   1182 		} else {
   1183 			run++;
   1184 			if (run == len)
   1185 				break;
   1186 		}
   1187 		if ((got & (NBBY - 1)) != (NBBY - 1)) {
   1188 			bit <<= 1;
   1189 		} else {
   1190 			map = *mapp++;
   1191 			bit = 1;
   1192 		}
   1193 	}
   1194 	if (got == ufs_rw32(cgp->cg_nclusterblks, UFS_IPNEEDSWAP(ip)))
   1195 		goto fail;
   1196 	/*
   1197 	 * Allocate the cluster that we have found.
   1198 	 */
   1199 	for (i = 1; i <= len; i++)
   1200 		if (!ffs_isblock(fs, cg_blksfree(cgp, UFS_IPNEEDSWAP(ip)),
   1201 			got - run + i))
   1202 			panic("ffs_clusteralloc: map mismatch");
   1203 	bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
   1204 	if (dtog(fs, bno) != cg)
   1205 		panic("ffs_clusteralloc: allocated out of group");
   1206 	len = blkstofrags(fs, len);
   1207 	for (i = 0; i < len; i += fs->fs_frag)
   1208 		if ((got = ffs_alloccgblk(ITOV(ip)->v_mount, fs, cgp, bno + i))
   1209 			!= bno + i)
   1210 			panic("ffs_clusteralloc: lost block");
   1211 	bdwrite(bp);
   1212 	return (bno);
   1213 
   1214 fail:
   1215 	brelse(bp);
   1216 	return (0);
   1217 }
   1218 
   1219 /*
   1220  * Determine whether an inode can be allocated.
   1221  *
   1222  * Check to see if an inode is available, and if it is,
   1223  * allocate it using the following policy:
   1224  *   1) allocate the requested inode.
   1225  *   2) allocate the next available inode after the requested
   1226  *      inode in the specified cylinder group.
   1227  */
   1228 static ufs_daddr_t
   1229 ffs_nodealloccg(ip, cg, ipref, mode)
   1230 	struct inode *ip;
   1231 	int cg;
   1232 	ufs_daddr_t ipref;
   1233 	int mode;
   1234 {
   1235 	register struct fs *fs;
   1236 	register struct cg *cgp;
   1237 	struct buf *bp;
   1238 	int error, start, len, loc, map, i;
   1239 #ifdef FFS_EI
   1240 	const int needswap = UFS_IPNEEDSWAP(ip);
   1241 #endif
   1242 
   1243 	fs = ip->i_fs;
   1244 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
   1245 		return (NULL);
   1246 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
   1247 		(int)fs->fs_cgsize, NOCRED, &bp);
   1248 	if (error) {
   1249 		brelse(bp);
   1250 		return (NULL);
   1251 	}
   1252 	cgp = (struct cg *)bp->b_data;
   1253 	if (!cg_chkmagic(cgp, needswap) || cgp->cg_cs.cs_nifree == 0) {
   1254 		brelse(bp);
   1255 		return (NULL);
   1256 	}
   1257 	cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
   1258 	if (ipref) {
   1259 		ipref %= fs->fs_ipg;
   1260 		if (isclr(cg_inosused(cgp, needswap), ipref))
   1261 			goto gotit;
   1262 	}
   1263 	start = ufs_rw32(cgp->cg_irotor, needswap) / NBBY;
   1264 	len = howmany(fs->fs_ipg - ufs_rw32(cgp->cg_irotor, needswap),
   1265 		NBBY);
   1266 	loc = skpc(0xff, len, &cg_inosused(cgp, needswap)[start]);
   1267 	if (loc == 0) {
   1268 		len = start + 1;
   1269 		start = 0;
   1270 		loc = skpc(0xff, len, &cg_inosused(cgp, needswap)[0]);
   1271 		if (loc == 0) {
   1272 			printf("cg = %d, irotor = %d, fs = %s\n",
   1273 			    cg, ufs_rw32(cgp->cg_irotor, needswap),
   1274 				fs->fs_fsmnt);
   1275 			panic("ffs_nodealloccg: map corrupted");
   1276 			/* NOTREACHED */
   1277 		}
   1278 	}
   1279 	i = start + len - loc;
   1280 	map = cg_inosused(cgp, needswap)[i];
   1281 	ipref = i * NBBY;
   1282 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
   1283 		if ((map & i) == 0) {
   1284 			cgp->cg_irotor = ufs_rw32(ipref, needswap);
   1285 			goto gotit;
   1286 		}
   1287 	}
   1288 	printf("fs = %s\n", fs->fs_fsmnt);
   1289 	panic("ffs_nodealloccg: block not in map");
   1290 	/* NOTREACHED */
   1291 gotit:
   1292 	setbit(cg_inosused(cgp, needswap), ipref);
   1293 	ufs_add32(cgp->cg_cs.cs_nifree, -1, needswap);
   1294 	fs->fs_cstotal.cs_nifree--;
   1295 	fs->fs_cs(fs, cg).cs_nifree --;
   1296 	fs->fs_fmod = 1;
   1297 	if ((mode & IFMT) == IFDIR) {
   1298 		ufs_add32(cgp->cg_cs.cs_ndir, 1, needswap);
   1299 		fs->fs_cstotal.cs_ndir++;
   1300 		fs->fs_cs(fs, cg).cs_ndir++;
   1301 	}
   1302 	bdwrite(bp);
   1303 	return (cg * fs->fs_ipg + ipref);
   1304 }
   1305 
   1306 /*
   1307  * Free a block or fragment.
   1308  *
   1309  * The specified block or fragment is placed back in the
   1310  * free map. If a fragment is deallocated, a possible
   1311  * block reassembly is checked.
   1312  */
   1313 void
   1314 ffs_blkfree(ip, bno, size)
   1315 	register struct inode *ip;
   1316 	ufs_daddr_t bno;
   1317 	long size;
   1318 {
   1319 	register struct fs *fs;
   1320 	register struct cg *cgp;
   1321 	struct buf *bp;
   1322 	ufs_daddr_t blkno;
   1323 	int i, error, cg, blk, frags, bbase;
   1324 	const int needswap = UFS_MPNEEDSWAP(ITOV(ip)->v_mount);
   1325 
   1326 	fs = ip->i_fs;
   1327 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
   1328 		printf("dev = 0x%x, bsize = %d, size = %ld, fs = %s\n",
   1329 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
   1330 		panic("blkfree: bad size");
   1331 	}
   1332 	cg = dtog(fs, bno);
   1333 	if ((u_int)bno >= fs->fs_size) {
   1334 		printf("bad block %d, ino %d\n", bno, ip->i_number);
   1335 		ffs_fserr(fs, ip->i_ffs_uid, "bad block");
   1336 		return;
   1337 	}
   1338 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
   1339 		(int)fs->fs_cgsize, NOCRED, &bp);
   1340 	if (error) {
   1341 		brelse(bp);
   1342 		return;
   1343 	}
   1344 	cgp = (struct cg *)bp->b_data;
   1345 	if (!cg_chkmagic(cgp, needswap)) {
   1346 		brelse(bp);
   1347 		return;
   1348 	}
   1349 	cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
   1350 	bno = dtogd(fs, bno);
   1351 	if (size == fs->fs_bsize) {
   1352 		blkno = fragstoblks(fs, bno);
   1353 		if (ffs_isblock(fs, cg_blksfree(cgp, needswap), blkno)) {
   1354 			printf("dev = 0x%x, block = %d, fs = %s\n",
   1355 			    ip->i_dev, bno, fs->fs_fsmnt);
   1356 			panic("blkfree: freeing free block");
   1357 		}
   1358 		ffs_setblock(fs, cg_blksfree(cgp, needswap), blkno);
   1359 		ffs_clusteracct(needswap, fs, cgp, blkno, 1);
   1360 		ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
   1361 		fs->fs_cstotal.cs_nbfree++;
   1362 		fs->fs_cs(fs, cg).cs_nbfree++;
   1363 		i = cbtocylno(fs, bno);
   1364 		ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs, bno)], 1,
   1365 			needswap);
   1366 		ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
   1367 	} else {
   1368 		bbase = bno - fragnum(fs, bno);
   1369 		/*
   1370 		 * decrement the counts associated with the old frags
   1371 		 */
   1372 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
   1373 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
   1374 		/*
   1375 		 * deallocate the fragment
   1376 		 */
   1377 		frags = numfrags(fs, size);
   1378 		for (i = 0; i < frags; i++) {
   1379 			if (isset(cg_blksfree(cgp, needswap), bno + i)) {
   1380 				printf("dev = 0x%x, block = %d, fs = %s\n",
   1381 				    ip->i_dev, bno + i, fs->fs_fsmnt);
   1382 				panic("blkfree: freeing free frag");
   1383 			}
   1384 			setbit(cg_blksfree(cgp, needswap), bno + i);
   1385 		}
   1386 		ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
   1387 		fs->fs_cstotal.cs_nffree += i;
   1388 		fs->fs_cs(fs, cg).cs_nffree +=i;
   1389 		/*
   1390 		 * add back in counts associated with the new frags
   1391 		 */
   1392 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
   1393 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
   1394 		/*
   1395 		 * if a complete block has been reassembled, account for it
   1396 		 */
   1397 		blkno = fragstoblks(fs, bbase);
   1398 		if (ffs_isblock(fs, cg_blksfree(cgp, needswap), blkno)) {
   1399 			ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
   1400 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
   1401 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
   1402 			ffs_clusteracct(needswap, fs, cgp, blkno, 1);
   1403 			ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
   1404 			fs->fs_cstotal.cs_nbfree++;
   1405 			fs->fs_cs(fs, cg).cs_nbfree++;
   1406 			i = cbtocylno(fs, bbase);
   1407 			ufs_add16(cg_blks(fs, cgp, i, needswap)[cbtorpos(fs, bbase)], 1,
   1408 				needswap);
   1409 			ufs_add32(cg_blktot(cgp, needswap)[i], 1, needswap);
   1410 		}
   1411 	}
   1412 	fs->fs_fmod = 1;
   1413 	bdwrite(bp);
   1414 }
   1415 
   1416 #if defined(DIAGNOSTIC) || defined(DEBUG)
   1417 /*
   1418  * Verify allocation of a block or fragment. Returns true if block or
   1419  * fragment is allocated, false if it is free.
   1420  */
   1421 static int
   1422 ffs_checkblk(ip, bno, size)
   1423 	struct inode *ip;
   1424 	ufs_daddr_t bno;
   1425 	long size;
   1426 {
   1427 	struct fs *fs;
   1428 	struct cg *cgp;
   1429 	struct buf *bp;
   1430 	int i, error, frags, free;
   1431 
   1432 	fs = ip->i_fs;
   1433 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
   1434 		printf("bsize = %d, size = %ld, fs = %s\n",
   1435 		    fs->fs_bsize, size, fs->fs_fsmnt);
   1436 		panic("checkblk: bad size");
   1437 	}
   1438 	if ((u_int)bno >= fs->fs_size)
   1439 		panic("checkblk: bad block %d", bno);
   1440 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, dtog(fs, bno))),
   1441 		(int)fs->fs_cgsize, NOCRED, &bp);
   1442 	if (error) {
   1443 		brelse(bp);
   1444 		return 0;
   1445 	}
   1446 	cgp = (struct cg *)bp->b_data;
   1447 	if (!cg_chkmagic(cgp, UFS_IPNEEDSWAP(ip))) {
   1448 		brelse(bp);
   1449 		return 0;
   1450 	}
   1451 	bno = dtogd(fs, bno);
   1452 	if (size == fs->fs_bsize) {
   1453 		free = ffs_isblock(fs, cg_blksfree(cgp, UFS_IPNEEDSWAP(ip)),
   1454 			fragstoblks(fs, bno));
   1455 	} else {
   1456 		frags = numfrags(fs, size);
   1457 		for (free = 0, i = 0; i < frags; i++)
   1458 			if (isset(cg_blksfree(cgp, UFS_IPNEEDSWAP(ip)), bno + i))
   1459 				free++;
   1460 		if (free != 0 && free != frags)
   1461 			panic("checkblk: partially free fragment");
   1462 	}
   1463 	brelse(bp);
   1464 	return (!free);
   1465 }
   1466 #endif /* DIAGNOSTIC */
   1467 
   1468 /*
   1469  * Free an inode.
   1470  *
   1471  * The specified inode is placed back in the free map.
   1472  */
   1473 int
   1474 ffs_vfree(v)
   1475 	void *v;
   1476 {
   1477 	struct vop_vfree_args /* {
   1478 		struct vnode *a_pvp;
   1479 		ino_t a_ino;
   1480 		int a_mode;
   1481 	} */ *ap = v;
   1482 	register struct fs *fs;
   1483 	register struct cg *cgp;
   1484 	register struct inode *pip;
   1485 	ino_t ino = ap->a_ino;
   1486 	struct buf *bp;
   1487 	int error, cg;
   1488 #ifdef FFS_EI
   1489 	const int needswap = UFS_MPNEEDSWAP(ap->a_pvp->v_mount);
   1490 #endif
   1491 
   1492 	pip = VTOI(ap->a_pvp);
   1493 	fs = pip->i_fs;
   1494 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
   1495 		panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
   1496 		    pip->i_dev, ino, fs->fs_fsmnt);
   1497 	cg = ino_to_cg(fs, ino);
   1498 	error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
   1499 		(int)fs->fs_cgsize, NOCRED, &bp);
   1500 	if (error) {
   1501 		brelse(bp);
   1502 		return (0);
   1503 	}
   1504 	cgp = (struct cg *)bp->b_data;
   1505 	if (!cg_chkmagic(cgp, needswap)) {
   1506 		brelse(bp);
   1507 		return (0);
   1508 	}
   1509 	cgp->cg_time = ufs_rw32(time.tv_sec, needswap);
   1510 	ino %= fs->fs_ipg;
   1511 	if (isclr(cg_inosused(cgp, needswap), ino)) {
   1512 		printf("dev = 0x%x, ino = %d, fs = %s\n",
   1513 		    pip->i_dev, ino, fs->fs_fsmnt);
   1514 		if (fs->fs_ronly == 0)
   1515 			panic("ifree: freeing free inode");
   1516 	}
   1517 	clrbit(cg_inosused(cgp, needswap), ino);
   1518 	if (ino < ufs_rw32(cgp->cg_irotor, needswap))
   1519 		cgp->cg_irotor = ufs_rw32(ino, needswap);
   1520 	ufs_add32(cgp->cg_cs.cs_nifree, 1, needswap);
   1521 	fs->fs_cstotal.cs_nifree++;
   1522 	fs->fs_cs(fs, cg).cs_nifree++;
   1523 	if ((ap->a_mode & IFMT) == IFDIR) {
   1524 		ufs_add32(cgp->cg_cs.cs_ndir, -1, needswap);
   1525 		fs->fs_cstotal.cs_ndir--;
   1526 		fs->fs_cs(fs, cg).cs_ndir--;
   1527 	}
   1528 	fs->fs_fmod = 1;
   1529 	bdwrite(bp);
   1530 	return (0);
   1531 }
   1532 
   1533 /*
   1534  * Find a block of the specified size in the specified cylinder group.
   1535  *
   1536  * It is a panic if a request is made to find a block if none are
   1537  * available.
   1538  */
   1539 static ufs_daddr_t
   1540 ffs_mapsearch(needswap, fs, cgp, bpref, allocsiz)
   1541 	int needswap;
   1542 	register struct fs *fs;
   1543 	register struct cg *cgp;
   1544 	ufs_daddr_t bpref;
   1545 	int allocsiz;
   1546 {
   1547 	ufs_daddr_t bno;
   1548 	int start, len, loc, i;
   1549 	int blk, field, subfield, pos;
   1550 	int ostart, olen;
   1551 
   1552 	/*
   1553 	 * find the fragment by searching through the free block
   1554 	 * map for an appropriate bit pattern
   1555 	 */
   1556 	if (bpref)
   1557 		start = dtogd(fs, bpref) / NBBY;
   1558 	else
   1559 		start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
   1560 	len = howmany(fs->fs_fpg, NBBY) - start;
   1561 	ostart = start;
   1562 	olen = len;
   1563 	loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp, needswap)[start],
   1564 		(u_char *)fragtbl[fs->fs_frag],
   1565 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
   1566 	if (loc == 0) {
   1567 		len = start + 1;
   1568 		start = 0;
   1569 		loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp, needswap)[0],
   1570 			(u_char *)fragtbl[fs->fs_frag],
   1571 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
   1572 		if (loc == 0) {
   1573 			printf("start = %d, len = %d, fs = %s\n",
   1574 			    ostart, olen, fs->fs_fsmnt);
   1575 			printf("offset=%d %ld\n",
   1576 				ufs_rw32(cgp->cg_freeoff, needswap),
   1577 				(long)cg_blksfree(cgp, needswap) - (long)cgp);
   1578 			panic("ffs_alloccg: map corrupted");
   1579 			/* NOTREACHED */
   1580 		}
   1581 	}
   1582 	bno = (start + len - loc) * NBBY;
   1583 	cgp->cg_frotor = ufs_rw32(bno, needswap);
   1584 	/*
   1585 	 * found the byte in the map
   1586 	 * sift through the bits to find the selected frag
   1587 	 */
   1588 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
   1589 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bno);
   1590 		blk <<= 1;
   1591 		field = around[allocsiz];
   1592 		subfield = inside[allocsiz];
   1593 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
   1594 			if ((blk & field) == subfield)
   1595 				return (bno + pos);
   1596 			field <<= 1;
   1597 			subfield <<= 1;
   1598 		}
   1599 	}
   1600 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
   1601 	panic("ffs_alloccg: block not in map");
   1602 	return (-1);
   1603 }
   1604 
   1605 /*
   1606  * Update the cluster map because of an allocation or free.
   1607  *
   1608  * Cnt == 1 means free; cnt == -1 means allocating.
   1609  */
   1610 void
   1611 ffs_clusteracct(needswap, fs, cgp, blkno, cnt)
   1612 	int needswap;
   1613 	struct fs *fs;
   1614 	struct cg *cgp;
   1615 	ufs_daddr_t blkno;
   1616 	int cnt;
   1617 {
   1618 	int32_t *sump;
   1619 	int32_t *lp;
   1620 	u_char *freemapp, *mapp;
   1621 	int i, start, end, forw, back, map, bit;
   1622 
   1623 	if (fs->fs_contigsumsize <= 0)
   1624 		return;
   1625 	freemapp = cg_clustersfree(cgp, needswap);
   1626 	sump = cg_clustersum(cgp, needswap);
   1627 	/*
   1628 	 * Allocate or clear the actual block.
   1629 	 */
   1630 	if (cnt > 0)
   1631 		setbit(freemapp, blkno);
   1632 	else
   1633 		clrbit(freemapp, blkno);
   1634 	/*
   1635 	 * Find the size of the cluster going forward.
   1636 	 */
   1637 	start = blkno + 1;
   1638 	end = start + fs->fs_contigsumsize;
   1639 	if (end >= ufs_rw32(cgp->cg_nclusterblks, needswap))
   1640 		end = ufs_rw32(cgp->cg_nclusterblks, needswap);
   1641 	mapp = &freemapp[start / NBBY];
   1642 	map = *mapp++;
   1643 	bit = 1 << (start % NBBY);
   1644 	for (i = start; i < end; i++) {
   1645 		if ((map & bit) == 0)
   1646 			break;
   1647 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
   1648 			bit <<= 1;
   1649 		} else {
   1650 			map = *mapp++;
   1651 			bit = 1;
   1652 		}
   1653 	}
   1654 	forw = i - start;
   1655 	/*
   1656 	 * Find the size of the cluster going backward.
   1657 	 */
   1658 	start = blkno - 1;
   1659 	end = start - fs->fs_contigsumsize;
   1660 	if (end < 0)
   1661 		end = -1;
   1662 	mapp = &freemapp[start / NBBY];
   1663 	map = *mapp--;
   1664 	bit = 1 << (start % NBBY);
   1665 	for (i = start; i > end; i--) {
   1666 		if ((map & bit) == 0)
   1667 			break;
   1668 		if ((i & (NBBY - 1)) != 0) {
   1669 			bit >>= 1;
   1670 		} else {
   1671 			map = *mapp--;
   1672 			bit = 1 << (NBBY - 1);
   1673 		}
   1674 	}
   1675 	back = start - i;
   1676 	/*
   1677 	 * Account for old cluster and the possibly new forward and
   1678 	 * back clusters.
   1679 	 */
   1680 	i = back + forw + 1;
   1681 	if (i > fs->fs_contigsumsize)
   1682 		i = fs->fs_contigsumsize;
   1683 	ufs_add32(sump[i], cnt, needswap);
   1684 	if (back > 0)
   1685 		ufs_add32(sump[back], -cnt, needswap);
   1686 	if (forw > 0)
   1687 		ufs_add32(sump[forw], -cnt, needswap);
   1688 
   1689 	/*
   1690 	 * Update cluster summary information.
   1691 	 */
   1692 	lp = &sump[fs->fs_contigsumsize];
   1693 	for (i = fs->fs_contigsumsize; i > 0; i--)
   1694 		if (ufs_rw32(*lp--, needswap) > 0)
   1695 			break;
   1696 	fs->fs_maxcluster[ufs_rw32(cgp->cg_cgx, needswap)] = i;
   1697 }
   1698 
   1699 /*
   1700  * Fserr prints the name of a file system with an error diagnostic.
   1701  *
   1702  * The form of the error message is:
   1703  *	fs: error message
   1704  */
   1705 static void
   1706 ffs_fserr(fs, uid, cp)
   1707 	struct fs *fs;
   1708 	u_int uid;
   1709 	char *cp;
   1710 {
   1711 
   1712 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
   1713 }
   1714