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