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