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