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