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