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