xref: /csrg-svn/sys/ufs/ffs/ffs_alloc.c (revision 66086)
123394Smckusick /*
263371Sbostic  * Copyright (c) 1982, 1986, 1989, 1993
363371Sbostic  *	The Regents of the University of California.  All rights reserved.
423394Smckusick  *
544536Sbostic  * %sccs.include.redist.c%
634432Sbostic  *
7*66086Smckusick  *	@(#)ffs_alloc.c	8.7 (Berkeley) 02/14/94
823394Smckusick  */
94359Smckusick 
1051469Sbostic #include <sys/param.h>
1151469Sbostic #include <sys/systm.h>
1251469Sbostic #include <sys/buf.h>
1351469Sbostic #include <sys/proc.h>
1451469Sbostic #include <sys/vnode.h>
1554653Smckusick #include <sys/mount.h>
1651469Sbostic #include <sys/kernel.h>
1751469Sbostic #include <sys/syslog.h>
184359Smckusick 
1953317Smckusick #include <vm/vm.h>
2053317Smckusick 
2151469Sbostic #include <ufs/ufs/quota.h>
2251469Sbostic #include <ufs/ufs/inode.h>
2347571Skarels 
2451469Sbostic #include <ufs/ffs/fs.h>
2551469Sbostic #include <ufs/ffs/ffs_extern.h>
264359Smckusick 
2751469Sbostic extern u_long nextgennumber;
2851469Sbostic 
2951469Sbostic static daddr_t	ffs_alloccg __P((struct inode *, int, daddr_t, int));
3051469Sbostic static daddr_t	ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t));
3165999Smckusick static daddr_t	ffs_clusteralloc __P((struct inode *, int, daddr_t, int));
3251469Sbostic static ino_t	ffs_dirpref __P((struct fs *));
3351469Sbostic static daddr_t	ffs_fragextend __P((struct inode *, int, long, int, int));
3451469Sbostic static void	ffs_fserr __P((struct fs *, u_int, char *));
3551469Sbostic static u_long	ffs_hashalloc
3651469Sbostic 		    __P((struct inode *, int, long, int, u_long (*)()));
3764603Sbostic static ino_t	ffs_nodealloccg __P((struct inode *, int, daddr_t, int));
3851469Sbostic static daddr_t	ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int));
3951469Sbostic 
405375Smckusic /*
415375Smckusic  * Allocate a block in the file system.
425375Smckusic  *
435375Smckusic  * The size of the requested block is given, which must be some
445375Smckusic  * multiple of fs_fsize and <= fs_bsize.
455375Smckusic  * A preference may be optionally specified. If a preference is given
465375Smckusic  * the following hierarchy is used to allocate a block:
475375Smckusic  *   1) allocate the requested block.
485375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
495375Smckusic  *   3) allocate a block in the same cylinder group.
505375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
515375Smckusic  *      available block is located.
525375Smckusic  * If no block preference is given the following heirarchy is used
535375Smckusic  * to allocate a block:
545375Smckusic  *   1) allocate a block in the cylinder group that contains the
555375Smckusic  *      inode for the file.
565375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
575375Smckusic  *      available block is located.
585375Smckusic  */
5953244Smckusick ffs_alloc(ip, lbn, bpref, size, cred, bnp)
604463Smckusic 	register struct inode *ip;
6139678Smckusick 	daddr_t lbn, bpref;
624359Smckusick 	int size;
6353244Smckusick 	struct ucred *cred;
6439678Smckusick 	daddr_t *bnp;
654359Smckusick {
6665468Sbostic 	register struct fs *fs;
674359Smckusick 	daddr_t bno;
6837735Smckusick 	int cg, error;
694359Smckusick 
7039678Smckusick 	*bnp = 0;
715965Smckusic 	fs = ip->i_fs;
7253244Smckusick #ifdef DIAGNOSTIC
7364508Sbostic 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
746716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
756716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7651469Sbostic 		panic("ffs_alloc: bad size");
776716Smckusick 	}
7853244Smckusick 	if (cred == NOCRED)
7953244Smckusick 		panic("ffs_alloc: missing credential\n");
8053244Smckusick #endif /* DIAGNOSTIC */
815322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
824359Smckusick 		goto nospace;
8341309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
844792Smckusic 		goto nospace;
857650Ssam #ifdef QUOTA
8641309Smckusick 	if (error = chkdq(ip, (long)btodb(size), cred, 0))
8737735Smckusick 		return (error);
887483Skre #endif
894948Smckusic 	if (bpref >= fs->fs_size)
904948Smckusic 		bpref = 0;
914359Smckusick 	if (bpref == 0)
9264603Sbostic 		cg = ino_to_cg(fs, ip->i_number);
934359Smckusick 	else
945377Smckusic 		cg = dtog(fs, bpref);
9551469Sbostic 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
9651469Sbostic 	    (u_long (*)())ffs_alloccg);
9739678Smckusick 	if (bno > 0) {
9839678Smckusick 		ip->i_blocks += btodb(size);
9964603Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
10039678Smckusick 		*bnp = bno;
10139678Smckusick 		return (0);
10239678Smckusick 	}
10345173Smckusick #ifdef QUOTA
10445173Smckusick 	/*
10545173Smckusick 	 * Restore user's disk quota because allocation failed.
10645173Smckusick 	 */
10745173Smckusick 	(void) chkdq(ip, (long)-btodb(size), cred, FORCE);
10845173Smckusick #endif
1094359Smckusick nospace:
11051469Sbostic 	ffs_fserr(fs, cred->cr_uid, "file system full");
1114359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
11237735Smckusick 	return (ENOSPC);
1134359Smckusick }
1144359Smckusick 
1155375Smckusic /*
1165375Smckusic  * Reallocate a fragment to a bigger size
1175375Smckusic  *
1185375Smckusic  * The number and size of the old block is given, and a preference
1195375Smckusic  * and new size is also specified. The allocator attempts to extend
1205375Smckusic  * the original block. Failing that, the regular block allocator is
1215375Smckusic  * invoked to get an appropriate block.
1225375Smckusic  */
12353244Smckusick ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
1245965Smckusic 	register struct inode *ip;
12553059Smckusick 	daddr_t lbprev;
12639678Smckusick 	daddr_t bpref;
1274426Smckusic 	int osize, nsize;
12853244Smckusick 	struct ucred *cred;
12937735Smckusick 	struct buf **bpp;
1304426Smckusic {
1314426Smckusic 	register struct fs *fs;
13265468Sbostic 	struct buf *bp;
13345719Smckusick 	int cg, request, error;
13445719Smckusick 	daddr_t bprev, bno;
1354426Smckusic 
13637735Smckusick 	*bpp = 0;
1375965Smckusic 	fs = ip->i_fs;
13853244Smckusick #ifdef DIAGNOSTIC
13964508Sbostic 	if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
14064508Sbostic 	    (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
14151469Sbostic 		printf(
14251469Sbostic 		    "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1436716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
14451469Sbostic 		panic("ffs_realloccg: bad size");
1456716Smckusick 	}
14653244Smckusick 	if (cred == NOCRED)
14753244Smckusick 		panic("ffs_realloccg: missing credential\n");
14853244Smckusick #endif /* DIAGNOSTIC */
14941309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1504792Smckusic 		goto nospace;
15139678Smckusick 	if ((bprev = ip->i_db[lbprev]) == 0) {
1526716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1536716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
15451469Sbostic 		panic("ffs_realloccg: bad bprev");
1556716Smckusick 	}
15639678Smckusick 	/*
15739678Smckusick 	 * Allocate the extra space in the buffer.
15839678Smckusick 	 */
15939678Smckusick 	if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
16039678Smckusick 		brelse(bp);
16139678Smckusick 		return (error);
16239678Smckusick 	}
16345173Smckusick #ifdef QUOTA
16445173Smckusick 	if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) {
16545173Smckusick 		brelse(bp);
16645173Smckusick 		return (error);
16745173Smckusick 	}
16845173Smckusick #endif
16939678Smckusick 	/*
17039678Smckusick 	 * Check for extension in the existing location.
17139678Smckusick 	 */
1726294Smckusick 	cg = dtog(fs, bprev);
17351469Sbostic 	if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) {
17439887Smckusick 		if (bp->b_blkno != fsbtodb(fs, bno))
17539678Smckusick 			panic("bad blockno");
17639762Smckusick 		ip->i_blocks += btodb(nsize - osize);
17764603Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
17848948Smckusick 		allocbuf(bp, nsize);
17948948Smckusick 		bp->b_flags |= B_DONE;
18064508Sbostic 		bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
18137735Smckusick 		*bpp = bp;
18237735Smckusick 		return (0);
1834463Smckusic 	}
18439678Smckusick 	/*
18539678Smckusick 	 * Allocate a new disk location.
18639678Smckusick 	 */
1874948Smckusic 	if (bpref >= fs->fs_size)
1884948Smckusic 		bpref = 0;
18926253Skarels 	switch ((int)fs->fs_optim) {
19025256Smckusick 	case FS_OPTSPACE:
19125256Smckusick 		/*
19225256Smckusick 		 * Allocate an exact sized fragment. Although this makes
19325256Smckusick 		 * best use of space, we will waste time relocating it if
19425256Smckusick 		 * the file continues to grow. If the fragmentation is
19525256Smckusick 		 * less than half of the minimum free reserve, we choose
19625256Smckusick 		 * to begin optimizing for time.
19725256Smckusick 		 */
19824698Smckusick 		request = nsize;
19925256Smckusick 		if (fs->fs_minfree < 5 ||
20025256Smckusick 		    fs->fs_cstotal.cs_nffree >
20125256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
20225256Smckusick 			break;
20325256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
20425256Smckusick 			fs->fs_fsmnt);
20525256Smckusick 		fs->fs_optim = FS_OPTTIME;
20625256Smckusick 		break;
20725256Smckusick 	case FS_OPTTIME:
20825256Smckusick 		/*
20951469Sbostic 		 * At this point we have discovered a file that is trying to
21051469Sbostic 		 * grow a small fragment to a larger fragment. To save time,
21151469Sbostic 		 * we allocate a full sized block, then free the unused portion.
21251469Sbostic 		 * If the file continues to grow, the `ffs_fragextend' call
21351469Sbostic 		 * above will be able to grow it in place without further
21451469Sbostic 		 * copying. If aberrant programs cause disk fragmentation to
21551469Sbostic 		 * grow within 2% of the free reserve, we choose to begin
21651469Sbostic 		 * optimizing for space.
21725256Smckusick 		 */
21824698Smckusick 		request = fs->fs_bsize;
21925256Smckusick 		if (fs->fs_cstotal.cs_nffree <
22025256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
22125256Smckusick 			break;
22225256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
22325256Smckusick 			fs->fs_fsmnt);
22425256Smckusick 		fs->fs_optim = FS_OPTSPACE;
22525256Smckusick 		break;
22625256Smckusick 	default:
22725256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
22825256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
22951469Sbostic 		panic("ffs_realloccg: bad optim");
23025256Smckusick 		/* NOTREACHED */
23125256Smckusick 	}
23251469Sbostic 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
23351469Sbostic 	    (u_long (*)())ffs_alloccg);
2346567Smckusic 	if (bno > 0) {
23545719Smckusick 		bp->b_blkno = fsbtodb(fs, bno);
23645719Smckusick 		(void) vnode_pager_uncache(ITOV(ip));
23753059Smckusick 		ffs_blkfree(ip, bprev, (long)osize);
23824698Smckusick 		if (nsize < request)
23951469Sbostic 			ffs_blkfree(ip, bno + numfrags(fs, nsize),
24053059Smckusick 			    (long)(request - nsize));
24112643Ssam 		ip->i_blocks += btodb(nsize - osize);
24264603Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
24348948Smckusick 		allocbuf(bp, nsize);
24448948Smckusick 		bp->b_flags |= B_DONE;
24564508Sbostic 		bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
24637735Smckusick 		*bpp = bp;
24737735Smckusick 		return (0);
2484463Smckusic 	}
24945173Smckusick #ifdef QUOTA
25045173Smckusick 	/*
25145173Smckusick 	 * Restore user's disk quota because allocation failed.
25245173Smckusick 	 */
25345173Smckusick 	(void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
25445173Smckusick #endif
25539678Smckusick 	brelse(bp);
2564792Smckusic nospace:
2574463Smckusic 	/*
2584463Smckusic 	 * no space available
2594463Smckusic 	 */
26051469Sbostic 	ffs_fserr(fs, cred->cr_uid, "file system full");
2614426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
26237735Smckusick 	return (ENOSPC);
2634426Smckusic }
2644426Smckusic 
2655375Smckusic /*
26665999Smckusick  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
26765999Smckusick  *
26865999Smckusick  * The vnode and an array of buffer pointers for a range of sequential
26965999Smckusick  * logical blocks to be made contiguous is given. The allocator attempts
27065999Smckusick  * to find a range of sequential blocks starting as close as possible to
27165999Smckusick  * an fs_rotdelay offset from the end of the allocation for the logical
27265999Smckusick  * block immediately preceeding the current range. If successful, the
27365999Smckusick  * physical block numbers in the buffer pointers and in the inode are
27465999Smckusick  * changed to reflect the new allocation. If unsuccessful, the allocation
27565999Smckusick  * is left unchanged. The success in doing the reallocation is returned.
27665999Smckusick  * Note that the error return is not reflected back to the user. Rather
27765999Smckusick  * the previous block allocation will be used.
27865999Smckusick  */
27965999Smckusick int
28065999Smckusick ffs_reallocblks(ap)
28165999Smckusick 	struct vop_reallocblks_args /* {
28265999Smckusick 		struct vnode *a_vp;
28365999Smckusick 		struct cluster_save *a_buflist;
28465999Smckusick 	} */ *ap;
28565999Smckusick {
28665999Smckusick 	struct fs *fs;
28765999Smckusick 	struct inode *ip;
28865999Smckusick 	struct vnode *vp;
28965999Smckusick 	struct buf *sbp, *ebp;
29065999Smckusick 	daddr_t *bap, *sbap, *ebap;
29165999Smckusick 	struct cluster_save *buflist;
29265999Smckusick 	daddr_t start_lbn, end_lbn, soff, eoff, newblk, blkno;
29365999Smckusick 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
29465999Smckusick 	int i, len, start_lvl, end_lvl, pref, ssize;
29565999Smckusick 
29665999Smckusick 	vp = ap->a_vp;
29765999Smckusick 	ip = VTOI(vp);
29865999Smckusick 	fs = ip->i_fs;
29965999Smckusick 	if (fs->fs_contigsumsize <= 0)
30065999Smckusick 		return (ENOSPC);
30165999Smckusick 	buflist = ap->a_buflist;
30265999Smckusick 	len = buflist->bs_nchildren;
30365999Smckusick 	start_lbn = buflist->bs_children[0]->b_lblkno;
30465999Smckusick 	end_lbn = start_lbn + len - 1;
30565999Smckusick #ifdef DIAGNOSTIC
30665999Smckusick 	for (i = 1; i < len; i++)
30765999Smckusick 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
30865999Smckusick 			panic("ffs_reallocblks: non-cluster");
30965999Smckusick #endif
31065999Smckusick 	/*
31165999Smckusick 	 * If the latest allocation is in a new cylinder group, assume that
31265999Smckusick 	 * the filesystem has decided to move and do not force it back to
31365999Smckusick 	 * the previous cylinder group.
31465999Smckusick 	 */
31565999Smckusick 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
31665999Smckusick 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
31765999Smckusick 		return (ENOSPC);
31865999Smckusick 	if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
31965999Smckusick 	    ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
32065999Smckusick 		return (ENOSPC);
32165999Smckusick 	/*
32265999Smckusick 	 * Get the starting offset and block map for the first block.
32365999Smckusick 	 */
32465999Smckusick 	if (start_lvl == 0) {
32565999Smckusick 		sbap = &ip->i_db[0];
32665999Smckusick 		soff = start_lbn;
32765999Smckusick 	} else {
32865999Smckusick 		idp = &start_ap[start_lvl - 1];
32965999Smckusick 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
33065999Smckusick 			brelse(sbp);
33165999Smckusick 			return (ENOSPC);
33265999Smckusick 		}
33365999Smckusick 		sbap = (daddr_t *)sbp->b_data;
33465999Smckusick 		soff = idp->in_off;
33565999Smckusick 	}
33665999Smckusick 	/*
33765999Smckusick 	 * Find the preferred location for the cluster.
33865999Smckusick 	 */
33965999Smckusick 	pref = ffs_blkpref(ip, start_lbn, soff, sbap);
34065999Smckusick 	/*
34165999Smckusick 	 * If the block range spans two block maps, get the second map.
34265999Smckusick 	 */
343*66086Smckusick 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
34465999Smckusick 		ssize = len;
34565999Smckusick 	} else {
346*66086Smckusick #ifdef DIAGNOSTIC
347*66086Smckusick 		if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
348*66086Smckusick 			panic("ffs_reallocblk: start == end");
349*66086Smckusick #endif
35065999Smckusick 		ssize = len - (idp->in_off + 1);
35165999Smckusick 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
35265999Smckusick 			goto fail;
35365999Smckusick 		ebap = (daddr_t *)ebp->b_data;
35465999Smckusick 	}
35565999Smckusick 	/*
35665999Smckusick 	 * Search the block map looking for an allocation of the desired size.
35765999Smckusick 	 */
35865999Smckusick 	if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
35965999Smckusick 	    len, (u_long (*)())ffs_clusteralloc)) == 0)
36065999Smckusick 		goto fail;
36165999Smckusick 	/*
36265999Smckusick 	 * We have found a new contiguous block.
36365999Smckusick 	 *
36465999Smckusick 	 * First we have to replace the old block pointers with the new
36565999Smckusick 	 * block pointers in the inode and indirect blocks associated
36665999Smckusick 	 * with the file.
36765999Smckusick 	 */
36865999Smckusick 	blkno = newblk;
36965999Smckusick 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
37065999Smckusick 		if (i == ssize)
37165999Smckusick 			bap = ebap;
37265999Smckusick #ifdef DIAGNOSTIC
37365999Smckusick 		if (buflist->bs_children[i]->b_blkno != fsbtodb(fs, *bap))
37465999Smckusick 			panic("ffs_reallocblks: alloc mismatch");
37565999Smckusick #endif
37665999Smckusick 		*bap++ = blkno;
37765999Smckusick 	}
37865999Smckusick 	/*
37965999Smckusick 	 * Next we must write out the modified inode and indirect blocks.
38065999Smckusick 	 * The writes are synchronous since the old block values may have
38165999Smckusick 	 * been written to disk.
38265999Smckusick 	 *
38365999Smckusick 	 * These writes should be changed to be bdwrites (and the VOP_UPDATE
38465999Smckusick 	 * dropped) when a flag has been added to the buffers and inodes
38565999Smckusick 	 * to detect when they have been written. It should be set when the
38665999Smckusick 	 * cluster is started and cleared whenever the buffer or inode is
38765999Smckusick 	 * flushed. We can then check below to see if it is set, and do
38865999Smckusick 	 * the synchronous write only when it has been cleared.
38965999Smckusick 	 */
39065999Smckusick 	if (sbap != &ip->i_db[0]) {
39165999Smckusick 		bwrite(sbp);
39265999Smckusick 	} else {
39365999Smckusick 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
39465999Smckusick 		VOP_UPDATE(vp, &time, &time, MNT_WAIT);
39565999Smckusick 	}
39665999Smckusick 	if (ssize < len)
39765999Smckusick 		bwrite(ebp);
39865999Smckusick 	/*
39965999Smckusick 	 * Last, free the old blocks and assign the new blocks to the buffers.
40065999Smckusick 	 */
40165999Smckusick 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
40265999Smckusick 		ffs_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
40365999Smckusick 		    fs->fs_bsize);
40465999Smckusick 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
40565999Smckusick 	}
40665999Smckusick 	return (0);
40765999Smckusick 
40865999Smckusick fail:
40965999Smckusick 	if (ssize < len)
41065999Smckusick 		brelse(ebp);
41165999Smckusick 	if (sbap != &ip->i_db[0])
41265999Smckusick 		brelse(sbp);
41365999Smckusick 	return (ENOSPC);
41465999Smckusick }
41565999Smckusick 
41665999Smckusick /*
4175375Smckusic  * Allocate an inode in the file system.
4185375Smckusic  *
41951469Sbostic  * If allocating a directory, use ffs_dirpref to select the inode.
42051469Sbostic  * If allocating in a directory, the following hierarchy is followed:
42151469Sbostic  *   1) allocate the preferred inode.
4225375Smckusic  *   2) allocate an inode in the same cylinder group.
4235375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
4245375Smckusic  *      available inode is located.
4255375Smckusic  * If no inode preference is given the following heirarchy is used
4265375Smckusic  * to allocate an inode:
4275375Smckusic  *   1) allocate an inode in cylinder group 0.
4285375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
4295375Smckusic  *      available inode is located.
4305375Smckusic  */
43154653Smckusick ffs_valloc(ap)
43254653Smckusick 	struct vop_valloc_args /* {
43354653Smckusick 		struct vnode *a_pvp;
43454653Smckusick 		int a_mode;
43554653Smckusick 		struct ucred *a_cred;
43654653Smckusick 		struct vnode **a_vpp;
43754653Smckusick 	} */ *ap;
4384359Smckusick {
43953865Sheideman 	register struct vnode *pvp = ap->a_pvp;
44051541Smckusick 	register struct inode *pip;
4414359Smckusick 	register struct fs *fs;
4424359Smckusick 	register struct inode *ip;
44354653Smckusick 	mode_t mode = ap->a_mode;
44451469Sbostic 	ino_t ino, ipref;
44537735Smckusick 	int cg, error;
4464359Smckusick 
44753583Sheideman 	*ap->a_vpp = NULL;
44853865Sheideman 	pip = VTOI(pvp);
4495965Smckusic 	fs = pip->i_fs;
4504792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
4514359Smckusick 		goto noinodes;
45251469Sbostic 
45354653Smckusick 	if ((mode & IFMT) == IFDIR)
45451541Smckusick 		ipref = ffs_dirpref(fs);
45551469Sbostic 	else
45651469Sbostic 		ipref = pip->i_number;
4574948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
4584948Smckusic 		ipref = 0;
45964603Sbostic 	cg = ino_to_cg(fs, ipref);
46064603Sbostic 	ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
4614359Smckusick 	if (ino == 0)
4624359Smckusick 		goto noinodes;
46354653Smckusick 	error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
46437735Smckusick 	if (error) {
46554653Smckusick 		VOP_VFREE(pvp, ino, mode);
46637735Smckusick 		return (error);
4674359Smckusick 	}
46853583Sheideman 	ip = VTOI(*ap->a_vpp);
4696716Smckusick 	if (ip->i_mode) {
47054653Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
4716716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
47251541Smckusick 		panic("ffs_valloc: dup alloc");
4736716Smckusick 	}
47412643Ssam 	if (ip->i_blocks) {				/* XXX */
47512643Ssam 		printf("free inode %s/%d had %d blocks\n",
47612643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
47712643Ssam 		ip->i_blocks = 0;
47812643Ssam 	}
47939516Smckusick 	ip->i_flags = 0;
48038255Smckusick 	/*
48138255Smckusick 	 * Set up a new generation number for this inode.
48238255Smckusick 	 */
48338255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
48438255Smckusick 		nextgennumber = time.tv_sec;
48538255Smckusick 	ip->i_gen = nextgennumber;
48637735Smckusick 	return (0);
4874359Smckusick noinodes:
48853583Sheideman 	ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
4896294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
49037735Smckusick 	return (ENOSPC);
4914359Smckusick }
4924359Smckusick 
4934651Smckusic /*
4945375Smckusic  * Find a cylinder to place a directory.
4955375Smckusic  *
4965375Smckusic  * The policy implemented by this algorithm is to select from
4975375Smckusic  * among those cylinder groups with above the average number of
4985375Smckusic  * free inodes, the one with the smallest number of directories.
4994651Smckusic  */
50051469Sbostic static ino_t
50151469Sbostic ffs_dirpref(fs)
5025965Smckusic 	register struct fs *fs;
5034359Smckusick {
5044651Smckusic 	int cg, minndir, mincg, avgifree;
5054359Smckusick 
5064792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
5074651Smckusic 	minndir = fs->fs_ipg;
5084359Smckusick 	mincg = 0;
5094651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
5105322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
5115322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
5124359Smckusick 			mincg = cg;
5135322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
5144359Smckusick 		}
5159163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
5164359Smckusick }
5174359Smckusick 
5184651Smckusic /*
5199163Ssam  * Select the desired position for the next block in a file.  The file is
5209163Ssam  * logically divided into sections. The first section is composed of the
5219163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
5229163Ssam  *
5239163Ssam  * If no blocks have been allocated in the first section, the policy is to
5249163Ssam  * request a block in the same cylinder group as the inode that describes
5259163Ssam  * the file. If no blocks have been allocated in any other section, the
5269163Ssam  * policy is to place the section in a cylinder group with a greater than
5279163Ssam  * average number of free blocks.  An appropriate cylinder group is found
52817696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
52917696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
53017696Smckusick  * cylinder group from which the previous allocation was made. The sweep
53117696Smckusick  * continues until a cylinder group with greater than the average number
53217696Smckusick  * of free blocks is found. If the allocation is for the first block in an
53317696Smckusick  * indirect block, the information on the previous allocation is unavailable;
53417696Smckusick  * here a best guess is made based upon the logical block number being
53517696Smckusick  * allocated.
5369163Ssam  *
5379163Ssam  * If a section is already partially allocated, the policy is to
5389163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
5399163Ssam  * contiguous blocks and the beginning of the next is physically separated
5409163Ssam  * so that the disk head will be in transit between them for at least
5419163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
5429163Ssam  * schedule another I/O transfer.
5434651Smckusic  */
5445212Smckusic daddr_t
54551469Sbostic ffs_blkpref(ip, lbn, indx, bap)
5469163Ssam 	struct inode *ip;
5479163Ssam 	daddr_t lbn;
5489163Ssam 	int indx;
5499163Ssam 	daddr_t *bap;
5509163Ssam {
5515965Smckusic 	register struct fs *fs;
55217696Smckusick 	register int cg;
55317696Smckusick 	int avgbfree, startcg;
5549163Ssam 	daddr_t nextblk;
5554651Smckusic 
5569163Ssam 	fs = ip->i_fs;
5579163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
5589163Ssam 		if (lbn < NDADDR) {
55964603Sbostic 			cg = ino_to_cg(fs, ip->i_number);
5605322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
5614651Smckusic 		}
5629163Ssam 		/*
5639163Ssam 		 * Find a cylinder with greater than average number of
5649163Ssam 		 * unused data blocks.
5659163Ssam 		 */
56617696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
56764603Sbostic 			startcg =
56864603Sbostic 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
56917696Smckusick 		else
57017696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
57117696Smckusick 		startcg %= fs->fs_ncg;
5729163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
57317696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
5749163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
5759163Ssam 				fs->fs_cgrotor = cg;
5769163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
5779163Ssam 			}
57817696Smckusick 		for (cg = 0; cg <= startcg; cg++)
5799163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
5809163Ssam 				fs->fs_cgrotor = cg;
5819163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
5829163Ssam 			}
5839163Ssam 		return (NULL);
5849163Ssam 	}
5859163Ssam 	/*
5869163Ssam 	 * One or more previous blocks have been laid out. If less
5879163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
5889163Ssam 	 * next block is requested contiguously, otherwise it is
5899163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
5909163Ssam 	 */
5919163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
59256665Smargo 	if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] +
59356665Smargo 	    blkstofrags(fs, fs->fs_maxcontig) != nextblk)
5949163Ssam 		return (nextblk);
5959163Ssam 	if (fs->fs_rotdelay != 0)
5969163Ssam 		/*
5979163Ssam 		 * Here we convert ms of delay to frags as:
5989163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
5999163Ssam 		 *	((sect/frag) * (ms/sec))
6009163Ssam 		 * then round up to the next block.
6019163Ssam 		 */
6029163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
6039163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
6049163Ssam 	return (nextblk);
6054651Smckusic }
6064651Smckusic 
6075375Smckusic /*
6085375Smckusic  * Implement the cylinder overflow algorithm.
6095375Smckusic  *
6105375Smckusic  * The policy implemented by this algorithm is:
6115375Smckusic  *   1) allocate the block in its requested cylinder group.
6125375Smckusic  *   2) quadradically rehash on the cylinder group number.
6135375Smckusic  *   3) brute force search for a free block.
6145375Smckusic  */
6155212Smckusic /*VARARGS5*/
61651469Sbostic static u_long
61751469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator)
6185965Smckusic 	struct inode *ip;
6194359Smckusick 	int cg;
6204359Smckusick 	long pref;
6214359Smckusick 	int size;	/* size for data blocks, mode for inodes */
6225212Smckusic 	u_long (*allocator)();
6234359Smckusick {
6245965Smckusic 	register struct fs *fs;
6254359Smckusick 	long result;
6264359Smckusick 	int i, icg = cg;
6274359Smckusick 
6285965Smckusic 	fs = ip->i_fs;
6294359Smckusick 	/*
6304359Smckusick 	 * 1: preferred cylinder group
6314359Smckusick 	 */
6325965Smckusic 	result = (*allocator)(ip, cg, pref, size);
6334359Smckusick 	if (result)
6344359Smckusick 		return (result);
6354359Smckusick 	/*
6364359Smckusick 	 * 2: quadratic rehash
6374359Smckusick 	 */
6384359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
6394359Smckusick 		cg += i;
6404359Smckusick 		if (cg >= fs->fs_ncg)
6414359Smckusick 			cg -= fs->fs_ncg;
6425965Smckusic 		result = (*allocator)(ip, cg, 0, size);
6434359Smckusick 		if (result)
6444359Smckusick 			return (result);
6454359Smckusick 	}
6464359Smckusick 	/*
6474359Smckusick 	 * 3: brute force search
64810847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
64910847Ssam 	 * and 1 is always checked in the quadratic rehash.
6504359Smckusick 	 */
65110848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
65210847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
6535965Smckusic 		result = (*allocator)(ip, cg, 0, size);
6544359Smckusick 		if (result)
6554359Smckusick 			return (result);
6564359Smckusick 		cg++;
6574359Smckusick 		if (cg == fs->fs_ncg)
6584359Smckusick 			cg = 0;
6594359Smckusick 	}
6606294Smckusick 	return (NULL);
6614359Smckusick }
6624359Smckusick 
6635375Smckusic /*
6645375Smckusic  * Determine whether a fragment can be extended.
6655375Smckusic  *
6665375Smckusic  * Check to see if the necessary fragments are available, and
6675375Smckusic  * if they are, allocate them.
6685375Smckusic  */
66951469Sbostic static daddr_t
67051469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize)
6715965Smckusic 	struct inode *ip;
6724426Smckusic 	int cg;
6734463Smckusic 	long bprev;
6744426Smckusic 	int osize, nsize;
6754426Smckusic {
6765965Smckusic 	register struct fs *fs;
6774463Smckusic 	register struct cg *cgp;
67837735Smckusick 	struct buf *bp;
6794463Smckusic 	long bno;
6804463Smckusic 	int frags, bbase;
68137735Smckusick 	int i, error;
6824426Smckusic 
6835965Smckusic 	fs = ip->i_fs;
68417224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
6856531Smckusick 		return (NULL);
6865960Smckusic 	frags = numfrags(fs, nsize);
68717224Smckusick 	bbase = fragnum(fs, bprev);
68817224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
68930749Skarels 		/* cannot extend across a block boundary */
6906294Smckusick 		return (NULL);
6914463Smckusic 	}
69237735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
69338776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
69437735Smckusick 	if (error) {
69537735Smckusick 		brelse(bp);
69637735Smckusick 		return (NULL);
69737735Smckusick 	}
69864508Sbostic 	cgp = (struct cg *)bp->b_data;
69937735Smckusick 	if (!cg_chkmagic(cgp)) {
7005960Smckusic 		brelse(bp);
7016294Smckusick 		return (NULL);
7025960Smckusic 	}
7038105Sroot 	cgp->cg_time = time.tv_sec;
7045377Smckusic 	bno = dtogd(fs, bprev);
7055960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
70634143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
7075361Smckusic 			brelse(bp);
7086294Smckusick 			return (NULL);
7095361Smckusic 		}
7105361Smckusic 	/*
7115361Smckusic 	 * the current fragment can be extended
7125361Smckusic 	 * deduct the count on fragment being extended into
7135361Smckusic 	 * increase the count on the remaining fragment (if any)
7145361Smckusic 	 * allocate the extended piece
7155361Smckusic 	 */
7165361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
71734143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
7184463Smckusic 			break;
7195960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
7205361Smckusic 	if (i != frags)
7215361Smckusic 		cgp->cg_frsum[i - frags]++;
7225960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
72334143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
7245361Smckusic 		cgp->cg_cs.cs_nffree--;
7255361Smckusic 		fs->fs_cstotal.cs_nffree--;
7265361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
7274463Smckusic 	}
72850893Smckusick 	fs->fs_fmod = 1;
7295361Smckusic 	bdwrite(bp);
7305361Smckusic 	return (bprev);
7314426Smckusic }
7324426Smckusic 
7335375Smckusic /*
7345375Smckusic  * Determine whether a block can be allocated.
7355375Smckusic  *
73664603Sbostic  * Check to see if a block of the appropriate size is available,
7375375Smckusic  * and if it is, allocate it.
7385375Smckusic  */
73951779Smarc static daddr_t
74051469Sbostic ffs_alloccg(ip, cg, bpref, size)
7415965Smckusic 	struct inode *ip;
7424359Smckusick 	int cg;
7434359Smckusick 	daddr_t bpref;
7444359Smckusick 	int size;
7454359Smckusick {
7465965Smckusic 	register struct fs *fs;
7474463Smckusic 	register struct cg *cgp;
74837735Smckusick 	struct buf *bp;
7494463Smckusic 	register int i;
75037735Smckusick 	int error, bno, frags, allocsiz;
7514359Smckusick 
7525965Smckusic 	fs = ip->i_fs;
7535322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
7546294Smckusick 		return (NULL);
75537735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
75638776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
75737735Smckusick 	if (error) {
75837735Smckusick 		brelse(bp);
75937735Smckusick 		return (NULL);
76037735Smckusick 	}
76164508Sbostic 	cgp = (struct cg *)bp->b_data;
76237735Smckusick 	if (!cg_chkmagic(cgp) ||
76315950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
7645960Smckusic 		brelse(bp);
7656294Smckusick 		return (NULL);
7665960Smckusic 	}
7678105Sroot 	cgp->cg_time = time.tv_sec;
7685322Smckusic 	if (size == fs->fs_bsize) {
76951469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
7704463Smckusic 		bdwrite(bp);
7714463Smckusic 		return (bno);
7724463Smckusic 	}
7734463Smckusic 	/*
7744463Smckusic 	 * check to see if any fragments are already available
7754463Smckusic 	 * allocsiz is the size which will be allocated, hacking
7764463Smckusic 	 * it down to a smaller size if necessary
7774463Smckusic 	 */
7785960Smckusic 	frags = numfrags(fs, size);
7795322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
7804463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
7814463Smckusic 			break;
7825322Smckusic 	if (allocsiz == fs->fs_frag) {
7834463Smckusic 		/*
7844463Smckusic 		 * no fragments were available, so a block will be
7854463Smckusic 		 * allocated, and hacked up
7864463Smckusic 		 */
7874792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
7884463Smckusic 			brelse(bp);
7896294Smckusick 			return (NULL);
7904463Smckusic 		}
79151469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
7925377Smckusic 		bpref = dtogd(fs, bno);
7935322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
79434143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
7955322Smckusic 		i = fs->fs_frag - frags;
7964792Smckusic 		cgp->cg_cs.cs_nffree += i;
7974792Smckusic 		fs->fs_cstotal.cs_nffree += i;
7985322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
79950893Smckusick 		fs->fs_fmod = 1;
8004463Smckusic 		cgp->cg_frsum[i]++;
8014463Smckusic 		bdwrite(bp);
8024463Smckusic 		return (bno);
8034463Smckusic 	}
80451469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
80515950Smckusick 	if (bno < 0) {
80615950Smckusick 		brelse(bp);
8076294Smckusick 		return (NULL);
80815950Smckusick 	}
8094463Smckusic 	for (i = 0; i < frags; i++)
81034143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
8114792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
8124792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
8135322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
81450893Smckusick 	fs->fs_fmod = 1;
8154463Smckusic 	cgp->cg_frsum[allocsiz]--;
8164463Smckusic 	if (frags != allocsiz)
8174463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
8184463Smckusic 	bdwrite(bp);
8194463Smckusic 	return (cg * fs->fs_fpg + bno);
8204463Smckusic }
8214463Smckusic 
8225375Smckusic /*
8235375Smckusic  * Allocate a block in a cylinder group.
8245375Smckusic  *
8255375Smckusic  * This algorithm implements the following policy:
8265375Smckusic  *   1) allocate the requested block.
8275375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
8285375Smckusic  *   3) allocate the next available block on the block rotor for the
8295375Smckusic  *      specified cylinder group.
8305375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
8315375Smckusic  * blocks may be fragmented by the routine that allocates them.
8325375Smckusic  */
83351469Sbostic static daddr_t
83451469Sbostic ffs_alloccgblk(fs, cgp, bpref)
8355965Smckusic 	register struct fs *fs;
8364463Smckusic 	register struct cg *cgp;
8374463Smckusic 	daddr_t bpref;
8384463Smckusic {
83965999Smckusick 	daddr_t bno, blkno;
8406294Smckusick 	int cylno, pos, delta;
8414651Smckusic 	short *cylbp;
8425361Smckusic 	register int i;
8434463Smckusic 
84465999Smckusick 	if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
8454651Smckusic 		bpref = cgp->cg_rotor;
8465361Smckusic 		goto norot;
8475361Smckusic 	}
84817224Smckusick 	bpref = blknum(fs, bpref);
8495377Smckusic 	bpref = dtogd(fs, bpref);
8505361Smckusic 	/*
8515361Smckusic 	 * if the requested block is available, use it
8525361Smckusic 	 */
85351469Sbostic 	if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
8545361Smckusic 		bno = bpref;
8555361Smckusic 		goto gotit;
8565361Smckusic 	}
8575361Smckusic 	/*
8585361Smckusic 	 * check for a block available on the same cylinder
8595361Smckusic 	 */
8605361Smckusic 	cylno = cbtocylno(fs, bpref);
86134143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
8625375Smckusic 		goto norot;
8635375Smckusic 	if (fs->fs_cpc == 0) {
8645375Smckusic 		/*
86557000Smckusick 		 * Block layout information is not available.
86657000Smckusick 		 * Leaving bpref unchanged means we take the
86757000Smckusick 		 * next available free block following the one
86857000Smckusick 		 * we just allocated. Hopefully this will at
86957000Smckusick 		 * least hit a track cache on drives of unknown
87057000Smckusick 		 * geometry (e.g. SCSI).
8715375Smckusic 		 */
8725375Smckusic 		goto norot;
8735375Smckusic 	}
8745375Smckusic 	/*
8755361Smckusic 	 * check the summary information to see if a block is
8765361Smckusic 	 * available in the requested cylinder starting at the
8779163Ssam 	 * requested rotational position and proceeding around.
8785361Smckusic 	 */
87934143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
8809163Ssam 	pos = cbtorpos(fs, bpref);
88134143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
8825361Smckusic 		if (cylbp[i] > 0)
8835361Smckusic 			break;
88434143Smckusick 	if (i == fs->fs_nrpos)
8855361Smckusic 		for (i = 0; i < pos; i++)
8865361Smckusic 			if (cylbp[i] > 0)
8875361Smckusic 				break;
8885361Smckusic 	if (cylbp[i] > 0) {
8894651Smckusic 		/*
8905361Smckusic 		 * found a rotational position, now find the actual
8915361Smckusic 		 * block. A panic if none is actually there.
8924651Smckusic 		 */
8935361Smckusic 		pos = cylno % fs->fs_cpc;
8945361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
89534143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
8966716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
8976716Smckusick 			    pos, i, fs->fs_fsmnt);
89851469Sbostic 			panic("ffs_alloccgblk: cyl groups corrupted");
8996716Smckusick 		}
90034143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
90151469Sbostic 			if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
90211638Ssam 				bno = blkstofrags(fs, (bno + i));
9035361Smckusic 				goto gotit;
9045361Smckusic 			}
90534143Smckusick 			delta = fs_rotbl(fs)[i];
90634143Smckusick 			if (delta <= 0 ||
90734143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
9084651Smckusic 				break;
9096294Smckusick 			i += delta;
9104651Smckusic 		}
9116716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
91251469Sbostic 		panic("ffs_alloccgblk: can't find blk in cyl");
9134359Smckusick 	}
9145361Smckusic norot:
9155361Smckusic 	/*
9165361Smckusic 	 * no blocks in the requested cylinder, so take next
9175361Smckusic 	 * available one in this cylinder group.
9185361Smckusic 	 */
91951469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
9206567Smckusic 	if (bno < 0)
9216294Smckusick 		return (NULL);
9224651Smckusic 	cgp->cg_rotor = bno;
9234359Smckusick gotit:
92465999Smckusick 	blkno = fragstoblks(fs, bno);
92565999Smckusick 	ffs_clrblock(fs, cg_blksfree(cgp), (long)blkno);
92665999Smckusick 	ffs_clusteracct(fs, cgp, blkno, -1);
9274792Smckusic 	cgp->cg_cs.cs_nbfree--;
9284792Smckusic 	fs->fs_cstotal.cs_nbfree--;
9295322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
9305375Smckusic 	cylno = cbtocylno(fs, bno);
93134143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
93234143Smckusick 	cg_blktot(cgp)[cylno]--;
93350893Smckusick 	fs->fs_fmod = 1;
9344651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
9354359Smckusick }
93634143Smckusick 
9375375Smckusic /*
93865999Smckusick  * Determine whether a cluster can be allocated.
93965999Smckusick  *
94065999Smckusick  * We do not currently check for optimal rotational layout if there
94165999Smckusick  * are multiple choices in the same cylinder group. Instead we just
94265999Smckusick  * take the first one that we find following bpref.
94365999Smckusick  */
94465999Smckusick static daddr_t
94565999Smckusick ffs_clusteralloc(ip, cg, bpref, len)
94665999Smckusick 	struct inode *ip;
94765999Smckusick 	int cg;
94865999Smckusick 	daddr_t bpref;
94965999Smckusick 	int len;
95065999Smckusick {
95165999Smckusick 	register struct fs *fs;
95265999Smckusick 	register struct cg *cgp;
95365999Smckusick 	struct buf *bp;
95465999Smckusick 	int i, run, bno, bit, map;
95565999Smckusick 	u_char *mapp;
95665999Smckusick 
95765999Smckusick 	fs = ip->i_fs;
95865999Smckusick 	if (fs->fs_cs(fs, cg).cs_nbfree < len)
95965999Smckusick 		return (NULL);
96065999Smckusick 	if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
96165999Smckusick 	    NOCRED, &bp))
96265999Smckusick 		goto fail;
96365999Smckusick 	cgp = (struct cg *)bp->b_data;
96465999Smckusick 	if (!cg_chkmagic(cgp))
96565999Smckusick 		goto fail;
96665999Smckusick 	/*
96765999Smckusick 	 * Check to see if a cluster of the needed size (or bigger) is
96865999Smckusick 	 * available in this cylinder group.
96965999Smckusick 	 */
97065999Smckusick 	for (i = len; i <= fs->fs_contigsumsize; i++)
97165999Smckusick 		if (cg_clustersum(cgp)[i] > 0)
97265999Smckusick 			break;
97365999Smckusick 	if (i > fs->fs_contigsumsize)
97465999Smckusick 		goto fail;
97565999Smckusick 	/*
97665999Smckusick 	 * Search the cluster map to find a big enough cluster.
97765999Smckusick 	 * We take the first one that we find, even if it is larger
97865999Smckusick 	 * than we need as we prefer to get one close to the previous
97965999Smckusick 	 * block allocation. We do not search before the current
98065999Smckusick 	 * preference point as we do not want to allocate a block
98165999Smckusick 	 * that is allocated before the previous one (as we will
98265999Smckusick 	 * then have to wait for another pass of the elevator
98365999Smckusick 	 * algorithm before it will be read). We prefer to fail and
98465999Smckusick 	 * be recalled to try an allocation in the next cylinder group.
98565999Smckusick 	 */
98665999Smckusick 	if (dtog(fs, bpref) != cg)
98765999Smckusick 		bpref = 0;
98865999Smckusick 	else
98965999Smckusick 		bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
99065999Smckusick 	mapp = &cg_clustersfree(cgp)[bpref / NBBY];
99165999Smckusick 	map = *mapp++;
99265999Smckusick 	bit = 1 << (bpref % NBBY);
99365999Smckusick 	for (run = 0, i = bpref; i < cgp->cg_nclusterblks; i++) {
99465999Smckusick 		if ((map & bit) == 0) {
99565999Smckusick 			run = 0;
99665999Smckusick 		} else {
99765999Smckusick 			run++;
99865999Smckusick 			if (run == len)
99965999Smckusick 				break;
100065999Smckusick 		}
100165999Smckusick 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
100265999Smckusick 			bit <<= 1;
100365999Smckusick 		} else {
100465999Smckusick 			map = *mapp++;
100565999Smckusick 			bit = 1;
100665999Smckusick 		}
100765999Smckusick 	}
100865999Smckusick 	if (i == cgp->cg_nclusterblks)
100965999Smckusick 		goto fail;
101065999Smckusick 	/*
101165999Smckusick 	 * Allocate the cluster that we have found.
101265999Smckusick 	 */
101365999Smckusick 	bno = cg * fs->fs_fpg + blkstofrags(fs, i - run + 1);
101465999Smckusick 	len = blkstofrags(fs, len);
101565999Smckusick 	for (i = 0; i < len; i += fs->fs_frag)
101665999Smckusick 		if (ffs_alloccgblk(fs, cgp, bno + i) != bno + i)
101765999Smckusick 			panic("ffs_clusteralloc: lost block");
101865999Smckusick 	brelse(bp);
101965999Smckusick 	return (bno);
102065999Smckusick 
102165999Smckusick fail:
102265999Smckusick 	brelse(bp);
102365999Smckusick 	return (0);
102465999Smckusick }
102565999Smckusick 
102665999Smckusick /*
10275375Smckusic  * Determine whether an inode can be allocated.
10285375Smckusic  *
10295375Smckusic  * Check to see if an inode is available, and if it is,
10305375Smckusic  * allocate it using the following policy:
10315375Smckusic  *   1) allocate the requested inode.
10325375Smckusic  *   2) allocate the next available inode after the requested
10335375Smckusic  *      inode in the specified cylinder group.
10345375Smckusic  */
103551469Sbostic static ino_t
103664603Sbostic ffs_nodealloccg(ip, cg, ipref, mode)
10375965Smckusic 	struct inode *ip;
10384359Smckusick 	int cg;
10394359Smckusick 	daddr_t ipref;
10404359Smckusick 	int mode;
10414359Smckusick {
10425965Smckusic 	register struct fs *fs;
10434463Smckusic 	register struct cg *cgp;
104416784Smckusick 	struct buf *bp;
104537735Smckusick 	int error, start, len, loc, map, i;
10464359Smckusick 
10475965Smckusic 	fs = ip->i_fs;
10485322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
10496294Smckusick 		return (NULL);
105037735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
105138776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
105237735Smckusick 	if (error) {
105337735Smckusick 		brelse(bp);
105437735Smckusick 		return (NULL);
105537735Smckusick 	}
105664508Sbostic 	cgp = (struct cg *)bp->b_data;
105737735Smckusick 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
10585960Smckusic 		brelse(bp);
10596294Smckusick 		return (NULL);
10605960Smckusic 	}
10618105Sroot 	cgp->cg_time = time.tv_sec;
10624359Smckusick 	if (ipref) {
10634359Smckusick 		ipref %= fs->fs_ipg;
106434143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
10654359Smckusick 			goto gotit;
106616784Smckusick 	}
106716784Smckusick 	start = cgp->cg_irotor / NBBY;
106816784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
106934143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
107016784Smckusick 	if (loc == 0) {
107117697Smckusick 		len = start + 1;
107217697Smckusick 		start = 0;
107334143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
107417697Smckusick 		if (loc == 0) {
107565718Sbostic 			printf("cg = %d, irotor = %d, fs = %s\n",
107617697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
107764603Sbostic 			panic("ffs_nodealloccg: map corrupted");
107817697Smckusick 			/* NOTREACHED */
107917697Smckusick 		}
108016784Smckusick 	}
108116784Smckusick 	i = start + len - loc;
108234143Smckusick 	map = cg_inosused(cgp)[i];
108316784Smckusick 	ipref = i * NBBY;
108416784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
108516784Smckusick 		if ((map & i) == 0) {
10864359Smckusick 			cgp->cg_irotor = ipref;
10874359Smckusick 			goto gotit;
10884359Smckusick 		}
10894359Smckusick 	}
109016784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
109164603Sbostic 	panic("ffs_nodealloccg: block not in map");
109216784Smckusick 	/* NOTREACHED */
10934359Smckusick gotit:
109434143Smckusick 	setbit(cg_inosused(cgp), ipref);
10954792Smckusic 	cgp->cg_cs.cs_nifree--;
10964792Smckusic 	fs->fs_cstotal.cs_nifree--;
10975322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
109850893Smckusick 	fs->fs_fmod = 1;
10994359Smckusick 	if ((mode & IFMT) == IFDIR) {
11004792Smckusic 		cgp->cg_cs.cs_ndir++;
11014792Smckusic 		fs->fs_cstotal.cs_ndir++;
11025322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
11034359Smckusick 	}
11044359Smckusick 	bdwrite(bp);
11054359Smckusick 	return (cg * fs->fs_ipg + ipref);
11064359Smckusick }
11074359Smckusick 
11085375Smckusic /*
11095375Smckusic  * Free a block or fragment.
11105375Smckusic  *
11115375Smckusic  * The specified block or fragment is placed back in the
11125375Smckusic  * free map. If a fragment is deallocated, a possible
11135375Smckusic  * block reassembly is checked.
11145375Smckusic  */
111551469Sbostic ffs_blkfree(ip, bno, size)
11165965Smckusic 	register struct inode *ip;
11174359Smckusick 	daddr_t bno;
111853059Smckusick 	long size;
11194359Smckusick {
11204359Smckusick 	register struct fs *fs;
11214359Smckusick 	register struct cg *cgp;
112237735Smckusick 	struct buf *bp;
112365999Smckusick 	daddr_t blkno;
112465999Smckusick 	int i, error, cg, blk, frags, bbase;
11254359Smckusick 
11265965Smckusic 	fs = ip->i_fs;
112764508Sbostic 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
11286716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
11296716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
113031402Smckusick 		panic("blkfree: bad size");
11316716Smckusick 	}
11325377Smckusic 	cg = dtog(fs, bno);
113364508Sbostic 	if ((u_int)bno >= fs->fs_size) {
11346567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
113553244Smckusick 		ffs_fserr(fs, ip->i_uid, "bad block");
11364359Smckusick 		return;
11376567Smckusic 	}
113837735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
113938776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
114037735Smckusick 	if (error) {
114137735Smckusick 		brelse(bp);
114237735Smckusick 		return;
114337735Smckusick 	}
114464508Sbostic 	cgp = (struct cg *)bp->b_data;
114537735Smckusick 	if (!cg_chkmagic(cgp)) {
11465960Smckusic 		brelse(bp);
11474359Smckusick 		return;
11485960Smckusic 	}
11498105Sroot 	cgp->cg_time = time.tv_sec;
11505377Smckusic 	bno = dtogd(fs, bno);
11515322Smckusic 	if (size == fs->fs_bsize) {
115265999Smckusick 		blkno = fragstoblks(fs, bno);
115365999Smckusick 		if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
11546716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
11556716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
115631402Smckusick 			panic("blkfree: freeing free block");
11576567Smckusic 		}
115865999Smckusick 		ffs_setblock(fs, cg_blksfree(cgp), blkno);
115965999Smckusick 		ffs_clusteracct(fs, cgp, blkno, 1);
11604792Smckusic 		cgp->cg_cs.cs_nbfree++;
11614792Smckusic 		fs->fs_cstotal.cs_nbfree++;
11625322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
11635375Smckusic 		i = cbtocylno(fs, bno);
116434143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
116534143Smckusick 		cg_blktot(cgp)[i]++;
11664426Smckusic 	} else {
116717224Smckusick 		bbase = bno - fragnum(fs, bno);
11684463Smckusic 		/*
11694463Smckusic 		 * decrement the counts associated with the old frags
11704463Smckusic 		 */
117134143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
117251469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11734463Smckusic 		/*
11744463Smckusic 		 * deallocate the fragment
11754463Smckusic 		 */
11765960Smckusic 		frags = numfrags(fs, size);
11774463Smckusic 		for (i = 0; i < frags; i++) {
117834143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
11796716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
11806716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
118131402Smckusick 				panic("blkfree: freeing free frag");
11826716Smckusick 			}
118334143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
11844426Smckusic 		}
11856294Smckusick 		cgp->cg_cs.cs_nffree += i;
11866294Smckusick 		fs->fs_cstotal.cs_nffree += i;
11876294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
11884463Smckusic 		/*
11894463Smckusic 		 * add back in counts associated with the new frags
11904463Smckusic 		 */
119134143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
119251469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11934463Smckusic 		/*
11944463Smckusic 		 * if a complete block has been reassembled, account for it
11954463Smckusic 		 */
119665999Smckusick 		blkno = fragstoblks(fs, bbase);
119765999Smckusick 		if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
11985322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
11995322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
12005322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
120165999Smckusick 			ffs_clusteracct(fs, cgp, blkno, 1);
12024792Smckusic 			cgp->cg_cs.cs_nbfree++;
12034792Smckusic 			fs->fs_cstotal.cs_nbfree++;
12045322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
12055375Smckusic 			i = cbtocylno(fs, bbase);
120634143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
120734143Smckusick 			cg_blktot(cgp)[i]++;
12084426Smckusic 		}
12094426Smckusic 	}
121050893Smckusick 	fs->fs_fmod = 1;
12114359Smckusick 	bdwrite(bp);
12124359Smckusick }
12134359Smckusick 
12145375Smckusic /*
12155375Smckusic  * Free an inode.
12165375Smckusic  *
12175375Smckusic  * The specified inode is placed back in the free map.
12185375Smckusic  */
121953577Sheideman int
122054653Smckusick ffs_vfree(ap)
122154653Smckusick 	struct vop_vfree_args /* {
122254653Smckusick 		struct vnode *a_pvp;
122354653Smckusick 		ino_t a_ino;
122454653Smckusick 		int a_mode;
122554653Smckusick 	} */ *ap;
12264359Smckusick {
12274359Smckusick 	register struct fs *fs;
12284359Smckusick 	register struct cg *cgp;
122951541Smckusick 	register struct inode *pip;
123054653Smckusick 	ino_t ino = ap->a_ino;
123137735Smckusick 	struct buf *bp;
123237735Smckusick 	int error, cg;
12334359Smckusick 
123453583Sheideman 	pip = VTOI(ap->a_pvp);
123551469Sbostic 	fs = pip->i_fs;
123654653Smckusick 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
123754653Smckusick 		panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
123854653Smckusick 		    pip->i_dev, ino, fs->fs_fsmnt);
123964603Sbostic 	cg = ino_to_cg(fs, ino);
124051469Sbostic 	error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
124138776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
124237735Smckusick 	if (error) {
124337735Smckusick 		brelse(bp);
124453577Sheideman 		return (0);
124537735Smckusick 	}
124664508Sbostic 	cgp = (struct cg *)bp->b_data;
124737735Smckusick 	if (!cg_chkmagic(cgp)) {
12485960Smckusic 		brelse(bp);
124953577Sheideman 		return (0);
12505960Smckusic 	}
12518105Sroot 	cgp->cg_time = time.tv_sec;
125254653Smckusick 	ino %= fs->fs_ipg;
125354653Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
125454653Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
125554653Smckusick 		    pip->i_dev, ino, fs->fs_fsmnt);
125648057Smckusick 		if (fs->fs_ronly == 0)
125748057Smckusick 			panic("ifree: freeing free inode");
12586716Smckusick 	}
125954653Smckusick 	clrbit(cg_inosused(cgp), ino);
126054653Smckusick 	if (ino < cgp->cg_irotor)
126154653Smckusick 		cgp->cg_irotor = ino;
12624792Smckusic 	cgp->cg_cs.cs_nifree++;
12634792Smckusic 	fs->fs_cstotal.cs_nifree++;
12645322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
126553583Sheideman 	if ((ap->a_mode & IFMT) == IFDIR) {
12664792Smckusic 		cgp->cg_cs.cs_ndir--;
12674792Smckusic 		fs->fs_cstotal.cs_ndir--;
12685322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
12694359Smckusick 	}
127050893Smckusick 	fs->fs_fmod = 1;
12714359Smckusick 	bdwrite(bp);
127253577Sheideman 	return (0);
12734359Smckusick }
12744359Smckusick 
12754463Smckusic /*
12765375Smckusic  * Find a block of the specified size in the specified cylinder group.
12775375Smckusic  *
12784651Smckusic  * It is a panic if a request is made to find a block if none are
12794651Smckusic  * available.
12804651Smckusic  */
128151469Sbostic static daddr_t
128251469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz)
12834651Smckusic 	register struct fs *fs;
12844651Smckusic 	register struct cg *cgp;
12854651Smckusic 	daddr_t bpref;
12864651Smckusic 	int allocsiz;
12874651Smckusic {
12884651Smckusic 	daddr_t bno;
12894651Smckusic 	int start, len, loc, i;
12904651Smckusic 	int blk, field, subfield, pos;
12914651Smckusic 
12924651Smckusic 	/*
12934651Smckusic 	 * find the fragment by searching through the free block
12944651Smckusic 	 * map for an appropriate bit pattern
12954651Smckusic 	 */
12964651Smckusic 	if (bpref)
12975377Smckusic 		start = dtogd(fs, bpref) / NBBY;
12984651Smckusic 	else
12994651Smckusic 		start = cgp->cg_frotor / NBBY;
13005398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
130164508Sbostic 	loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[start],
130234476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
130334476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
13044651Smckusic 	if (loc == 0) {
13056531Smckusick 		len = start + 1;
13066531Smckusick 		start = 0;
130764508Sbostic 		loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[0],
130834476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
130934476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
131016784Smckusick 		if (loc == 0) {
131116784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
131216784Smckusick 			    start, len, fs->fs_fsmnt);
131351469Sbostic 			panic("ffs_alloccg: map corrupted");
131417697Smckusick 			/* NOTREACHED */
131516784Smckusick 		}
13164651Smckusic 	}
13174651Smckusic 	bno = (start + len - loc) * NBBY;
13184651Smckusic 	cgp->cg_frotor = bno;
13194651Smckusic 	/*
13204651Smckusic 	 * found the byte in the map
13214651Smckusic 	 * sift through the bits to find the selected frag
13224651Smckusic 	 */
13236294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
132434143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
13254651Smckusic 		blk <<= 1;
13264651Smckusic 		field = around[allocsiz];
13274651Smckusic 		subfield = inside[allocsiz];
13285322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
13296294Smckusick 			if ((blk & field) == subfield)
13306294Smckusick 				return (bno + pos);
13314651Smckusic 			field <<= 1;
13324651Smckusic 			subfield <<= 1;
13334651Smckusic 		}
13344651Smckusic 	}
13356716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
133651469Sbostic 	panic("ffs_alloccg: block not in map");
13376531Smckusick 	return (-1);
13384651Smckusic }
13394651Smckusic 
13404651Smckusic /*
134165999Smckusick  * Update the cluster map because of an allocation or free.
134265999Smckusick  *
134365999Smckusick  * Cnt == 1 means free; cnt == -1 means allocating.
134465999Smckusick  */
134565999Smckusick ffs_clusteracct(fs, cgp, blkno, cnt)
134665999Smckusick 	struct fs *fs;
134765999Smckusick 	struct cg *cgp;
134865999Smckusick 	daddr_t blkno;
134965999Smckusick 	int cnt;
135065999Smckusick {
135165999Smckusick 	long *sump;
135265999Smckusick 	u_char *freemapp, *mapp;
135365999Smckusick 	int i, start, end, forw, back, map, bit;
135465999Smckusick 
135565999Smckusick 	if (fs->fs_contigsumsize <= 0)
135665999Smckusick 		return;
135765999Smckusick 	freemapp = cg_clustersfree(cgp);
135865999Smckusick 	sump = cg_clustersum(cgp);
135965999Smckusick 	/*
136065999Smckusick 	 * Allocate or clear the actual block.
136165999Smckusick 	 */
136265999Smckusick 	if (cnt > 0)
136365999Smckusick 		setbit(freemapp, blkno);
136465999Smckusick 	else
136565999Smckusick 		clrbit(freemapp, blkno);
136665999Smckusick 	/*
136765999Smckusick 	 * Find the size of the cluster going forward.
136865999Smckusick 	 */
136965999Smckusick 	start = blkno + 1;
137065999Smckusick 	end = start + fs->fs_contigsumsize;
137165999Smckusick 	if (end >= cgp->cg_nclusterblks)
137265999Smckusick 		end = cgp->cg_nclusterblks;
137365999Smckusick 	mapp = &freemapp[start / NBBY];
137465999Smckusick 	map = *mapp++;
137565999Smckusick 	bit = 1 << (start % NBBY);
137665999Smckusick 	for (i = start; i < end; i++) {
137765999Smckusick 		if ((map & bit) == 0)
137865999Smckusick 			break;
137965999Smckusick 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
138065999Smckusick 			bit <<= 1;
138165999Smckusick 		} else {
138265999Smckusick 			map = *mapp++;
138365999Smckusick 			bit = 1;
138465999Smckusick 		}
138565999Smckusick 	}
138665999Smckusick 	forw = i - start;
138765999Smckusick 	/*
138865999Smckusick 	 * Find the size of the cluster going backward.
138965999Smckusick 	 */
139065999Smckusick 	start = blkno - 1;
139165999Smckusick 	end = start - fs->fs_contigsumsize;
139265999Smckusick 	if (end < 0)
139365999Smckusick 		end = -1;
139465999Smckusick 	mapp = &freemapp[start / NBBY];
139565999Smckusick 	map = *mapp--;
139665999Smckusick 	bit = 1 << (start % NBBY);
139765999Smckusick 	for (i = start; i > end; i--) {
139865999Smckusick 		if ((map & bit) == 0)
139965999Smckusick 			break;
140065999Smckusick 		if ((i & (NBBY - 1)) != 0) {
140165999Smckusick 			bit >>= 1;
140265999Smckusick 		} else {
140365999Smckusick 			map = *mapp--;
140465999Smckusick 			bit = 1 << (NBBY - 1);
140565999Smckusick 		}
140665999Smckusick 	}
140765999Smckusick 	back = start - i;
140865999Smckusick 	/*
140965999Smckusick 	 * Account for old cluster and the possibly new forward and
141065999Smckusick 	 * back clusters.
141165999Smckusick 	 */
141265999Smckusick 	i = back + forw + 1;
141365999Smckusick 	if (i > fs->fs_contigsumsize)
141465999Smckusick 		i = fs->fs_contigsumsize;
141565999Smckusick 	sump[i] += cnt;
141665999Smckusick 	if (back > 0)
141765999Smckusick 		sump[back] -= cnt;
141865999Smckusick 	if (forw > 0)
141965999Smckusick 		sump[forw] -= cnt;
142065999Smckusick }
142165999Smckusick 
142265999Smckusick /*
14235375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
14245375Smckusic  *
14255375Smckusic  * The form of the error message is:
14264359Smckusick  *	fs: error message
14274359Smckusick  */
142851469Sbostic static void
142951469Sbostic ffs_fserr(fs, uid, cp)
14304359Smckusick 	struct fs *fs;
143151469Sbostic 	u_int uid;
14324359Smckusick 	char *cp;
14334359Smckusick {
14344359Smckusick 
143542318Smckusick 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
14364359Smckusick }
1437