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