xref: /csrg-svn/sys/ufs/ffs/ffs_alloc.c (revision 57000)
123394Smckusick /*
237735Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
334432Sbostic  * All rights reserved.
423394Smckusick  *
544536Sbostic  * %sccs.include.redist.c%
634432Sbostic  *
7*57000Smckusick  *	@(#)ffs_alloc.c	7.40 (Berkeley) 12/04/92
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));
3151469Sbostic static ino_t	ffs_dirpref __P((struct fs *));
3251469Sbostic static daddr_t	ffs_fragextend __P((struct inode *, int, long, int, int));
3351469Sbostic static void	ffs_fserr __P((struct fs *, u_int, char *));
3451469Sbostic static u_long	ffs_hashalloc
3551469Sbostic 		    __P((struct inode *, int, long, int, u_long (*)()));
3651469Sbostic static ino_t	ffs_ialloccg __P((struct inode *, int, daddr_t, int));
3751469Sbostic static daddr_t	ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int));
3851469Sbostic 
395375Smckusic /*
405375Smckusic  * Allocate a block in the file system.
415375Smckusic  *
425375Smckusic  * The size of the requested block is given, which must be some
435375Smckusic  * multiple of fs_fsize and <= fs_bsize.
445375Smckusic  * A preference may be optionally specified. If a preference is given
455375Smckusic  * the following hierarchy is used to allocate a block:
465375Smckusic  *   1) allocate the requested block.
475375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
485375Smckusic  *   3) allocate a block in the same cylinder group.
495375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
505375Smckusic  *      available block is located.
515375Smckusic  * If no block preference is given the following heirarchy is used
525375Smckusic  * to allocate a block:
535375Smckusic  *   1) allocate a block in the cylinder group that contains the
545375Smckusic  *      inode for the file.
555375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
565375Smckusic  *      available block is located.
575375Smckusic  */
5853244Smckusick ffs_alloc(ip, lbn, bpref, size, cred, bnp)
594463Smckusic 	register struct inode *ip;
6039678Smckusick 	daddr_t lbn, bpref;
614359Smckusick 	int size;
6253244Smckusick 	struct ucred *cred;
6339678Smckusick 	daddr_t *bnp;
644359Smckusick {
654359Smckusick 	daddr_t bno;
664359Smckusick 	register struct fs *fs;
674463Smckusic 	register struct buf *bp;
6837735Smckusick 	int cg, error;
694359Smckusick 
7039678Smckusick 	*bnp = 0;
715965Smckusic 	fs = ip->i_fs;
7253244Smckusick #ifdef DIAGNOSTIC
736716Smckusick 	if ((unsigned)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)
925377Smckusic 		cg = itog(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);
9939678Smckusick 		ip->i_flag |= IUPD|ICHG;
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;
13237735Smckusick 	struct buf *bp, *obp;
13345719Smckusick 	int cg, request, error;
13445719Smckusick 	daddr_t bprev, bno;
1354426Smckusic 
13637735Smckusick 	*bpp = 0;
1375965Smckusic 	fs = ip->i_fs;
13853244Smckusick #ifdef DIAGNOSTIC
1395960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1406716Smckusick 	    (unsigned)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);
17739762Smckusick 		ip->i_flag |= IUPD|ICHG;
17848948Smckusick 		allocbuf(bp, nsize);
17948948Smckusick 		bp->b_flags |= B_DONE;
18048948Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)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);
24212643Ssam 		ip->i_flag |= IUPD|ICHG;
24348948Smckusick 		allocbuf(bp, nsize);
24448948Smckusick 		bp->b_flags |= B_DONE;
24548948Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)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 /*
2665375Smckusic  * Allocate an inode in the file system.
2675375Smckusic  *
26851469Sbostic  * If allocating a directory, use ffs_dirpref to select the inode.
26951469Sbostic  * If allocating in a directory, the following hierarchy is followed:
27051469Sbostic  *   1) allocate the preferred inode.
2715375Smckusic  *   2) allocate an inode in the same cylinder group.
2725375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2735375Smckusic  *      available inode is located.
2745375Smckusic  * If no inode preference is given the following heirarchy is used
2755375Smckusic  * to allocate an inode:
2765375Smckusic  *   1) allocate an inode in cylinder group 0.
2775375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2785375Smckusic  *      available inode is located.
2795375Smckusic  */
28054653Smckusick ffs_valloc(ap)
28154653Smckusick 	struct vop_valloc_args /* {
28254653Smckusick 		struct vnode *a_pvp;
28354653Smckusick 		int a_mode;
28454653Smckusick 		struct ucred *a_cred;
28554653Smckusick 		struct vnode **a_vpp;
28654653Smckusick 	} */ *ap;
2874359Smckusick {
28853865Sheideman 	register struct vnode *pvp = ap->a_pvp;
28951541Smckusick 	register struct inode *pip;
2904359Smckusick 	register struct fs *fs;
2914359Smckusick 	register struct inode *ip;
29254653Smckusick 	mode_t mode = ap->a_mode;
29351469Sbostic 	ino_t ino, ipref;
29437735Smckusick 	int cg, error;
2954359Smckusick 
29653583Sheideman 	*ap->a_vpp = NULL;
29753865Sheideman 	pip = VTOI(pvp);
2985965Smckusic 	fs = pip->i_fs;
2994792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
3004359Smckusick 		goto noinodes;
30151469Sbostic 
30254653Smckusick 	if ((mode & IFMT) == IFDIR)
30351541Smckusick 		ipref = ffs_dirpref(fs);
30451469Sbostic 	else
30551469Sbostic 		ipref = pip->i_number;
3064948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
3074948Smckusic 		ipref = 0;
3085377Smckusic 	cg = itog(fs, ipref);
30954653Smckusick 	ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_ialloccg);
3104359Smckusick 	if (ino == 0)
3114359Smckusick 		goto noinodes;
31254653Smckusick 	error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
31337735Smckusick 	if (error) {
31454653Smckusick 		VOP_VFREE(pvp, ino, mode);
31537735Smckusick 		return (error);
3164359Smckusick 	}
31753583Sheideman 	ip = VTOI(*ap->a_vpp);
3186716Smckusick 	if (ip->i_mode) {
31954653Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
3206716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
32151541Smckusick 		panic("ffs_valloc: dup alloc");
3226716Smckusick 	}
32312643Ssam 	if (ip->i_blocks) {				/* XXX */
32412643Ssam 		printf("free inode %s/%d had %d blocks\n",
32512643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
32612643Ssam 		ip->i_blocks = 0;
32712643Ssam 	}
32839516Smckusick 	ip->i_flags = 0;
32938255Smckusick 	/*
33038255Smckusick 	 * Set up a new generation number for this inode.
33138255Smckusick 	 */
33238255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
33338255Smckusick 		nextgennumber = time.tv_sec;
33438255Smckusick 	ip->i_gen = nextgennumber;
33537735Smckusick 	return (0);
3364359Smckusick noinodes:
33753583Sheideman 	ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
3386294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
33937735Smckusick 	return (ENOSPC);
3404359Smckusick }
3414359Smckusick 
3424651Smckusic /*
3435375Smckusic  * Find a cylinder to place a directory.
3445375Smckusic  *
3455375Smckusic  * The policy implemented by this algorithm is to select from
3465375Smckusic  * among those cylinder groups with above the average number of
3475375Smckusic  * free inodes, the one with the smallest number of directories.
3484651Smckusic  */
34951469Sbostic static ino_t
35051469Sbostic ffs_dirpref(fs)
3515965Smckusic 	register struct fs *fs;
3524359Smckusick {
3534651Smckusic 	int cg, minndir, mincg, avgifree;
3544359Smckusick 
3554792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3564651Smckusic 	minndir = fs->fs_ipg;
3574359Smckusick 	mincg = 0;
3584651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3595322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3605322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3614359Smckusick 			mincg = cg;
3625322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3634359Smckusick 		}
3649163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3654359Smckusick }
3664359Smckusick 
3674651Smckusic /*
3689163Ssam  * Select the desired position for the next block in a file.  The file is
3699163Ssam  * logically divided into sections. The first section is composed of the
3709163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3719163Ssam  *
3729163Ssam  * If no blocks have been allocated in the first section, the policy is to
3739163Ssam  * request a block in the same cylinder group as the inode that describes
3749163Ssam  * the file. If no blocks have been allocated in any other section, the
3759163Ssam  * policy is to place the section in a cylinder group with a greater than
3769163Ssam  * average number of free blocks.  An appropriate cylinder group is found
37717696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
37817696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
37917696Smckusick  * cylinder group from which the previous allocation was made. The sweep
38017696Smckusick  * continues until a cylinder group with greater than the average number
38117696Smckusick  * of free blocks is found. If the allocation is for the first block in an
38217696Smckusick  * indirect block, the information on the previous allocation is unavailable;
38317696Smckusick  * here a best guess is made based upon the logical block number being
38417696Smckusick  * allocated.
3859163Ssam  *
3869163Ssam  * If a section is already partially allocated, the policy is to
3879163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3889163Ssam  * contiguous blocks and the beginning of the next is physically separated
3899163Ssam  * so that the disk head will be in transit between them for at least
3909163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3919163Ssam  * schedule another I/O transfer.
3924651Smckusic  */
3935212Smckusic daddr_t
39451469Sbostic ffs_blkpref(ip, lbn, indx, bap)
3959163Ssam 	struct inode *ip;
3969163Ssam 	daddr_t lbn;
3979163Ssam 	int indx;
3989163Ssam 	daddr_t *bap;
3999163Ssam {
4005965Smckusic 	register struct fs *fs;
40117696Smckusick 	register int cg;
40217696Smckusick 	int avgbfree, startcg;
4039163Ssam 	daddr_t nextblk;
4044651Smckusic 
4059163Ssam 	fs = ip->i_fs;
4069163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
4079163Ssam 		if (lbn < NDADDR) {
4089163Ssam 			cg = itog(fs, ip->i_number);
4095322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
4104651Smckusic 		}
4119163Ssam 		/*
4129163Ssam 		 * Find a cylinder with greater than average number of
4139163Ssam 		 * unused data blocks.
4149163Ssam 		 */
41517696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
41617696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
41717696Smckusick 		else
41817696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
41917696Smckusick 		startcg %= fs->fs_ncg;
4209163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
42117696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
4229163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4239163Ssam 				fs->fs_cgrotor = cg;
4249163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4259163Ssam 			}
42617696Smckusick 		for (cg = 0; cg <= startcg; cg++)
4279163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4289163Ssam 				fs->fs_cgrotor = cg;
4299163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4309163Ssam 			}
4319163Ssam 		return (NULL);
4329163Ssam 	}
4339163Ssam 	/*
4349163Ssam 	 * One or more previous blocks have been laid out. If less
4359163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
4369163Ssam 	 * next block is requested contiguously, otherwise it is
4379163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
4389163Ssam 	 */
4399163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
44056665Smargo 	if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] +
44156665Smargo 	    blkstofrags(fs, fs->fs_maxcontig) != nextblk)
4429163Ssam 		return (nextblk);
4439163Ssam 	if (fs->fs_rotdelay != 0)
4449163Ssam 		/*
4459163Ssam 		 * Here we convert ms of delay to frags as:
4469163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4479163Ssam 		 *	((sect/frag) * (ms/sec))
4489163Ssam 		 * then round up to the next block.
4499163Ssam 		 */
4509163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4519163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4529163Ssam 	return (nextblk);
4534651Smckusic }
4544651Smckusic 
4555375Smckusic /*
4565375Smckusic  * Implement the cylinder overflow algorithm.
4575375Smckusic  *
4585375Smckusic  * The policy implemented by this algorithm is:
4595375Smckusic  *   1) allocate the block in its requested cylinder group.
4605375Smckusic  *   2) quadradically rehash on the cylinder group number.
4615375Smckusic  *   3) brute force search for a free block.
4625375Smckusic  */
4635212Smckusic /*VARARGS5*/
46451469Sbostic static u_long
46551469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator)
4665965Smckusic 	struct inode *ip;
4674359Smckusick 	int cg;
4684359Smckusick 	long pref;
4694359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4705212Smckusic 	u_long (*allocator)();
4714359Smckusick {
4725965Smckusic 	register struct fs *fs;
4734359Smckusick 	long result;
4744359Smckusick 	int i, icg = cg;
4754359Smckusick 
4765965Smckusic 	fs = ip->i_fs;
4774359Smckusick 	/*
4784359Smckusick 	 * 1: preferred cylinder group
4794359Smckusick 	 */
4805965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4814359Smckusick 	if (result)
4824359Smckusick 		return (result);
4834359Smckusick 	/*
4844359Smckusick 	 * 2: quadratic rehash
4854359Smckusick 	 */
4864359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4874359Smckusick 		cg += i;
4884359Smckusick 		if (cg >= fs->fs_ncg)
4894359Smckusick 			cg -= fs->fs_ncg;
4905965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4914359Smckusick 		if (result)
4924359Smckusick 			return (result);
4934359Smckusick 	}
4944359Smckusick 	/*
4954359Smckusick 	 * 3: brute force search
49610847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
49710847Ssam 	 * and 1 is always checked in the quadratic rehash.
4984359Smckusick 	 */
49910848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
50010847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
5015965Smckusic 		result = (*allocator)(ip, cg, 0, size);
5024359Smckusick 		if (result)
5034359Smckusick 			return (result);
5044359Smckusick 		cg++;
5054359Smckusick 		if (cg == fs->fs_ncg)
5064359Smckusick 			cg = 0;
5074359Smckusick 	}
5086294Smckusick 	return (NULL);
5094359Smckusick }
5104359Smckusick 
5115375Smckusic /*
5125375Smckusic  * Determine whether a fragment can be extended.
5135375Smckusic  *
5145375Smckusic  * Check to see if the necessary fragments are available, and
5155375Smckusic  * if they are, allocate them.
5165375Smckusic  */
51751469Sbostic static daddr_t
51851469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize)
5195965Smckusic 	struct inode *ip;
5204426Smckusic 	int cg;
5214463Smckusic 	long bprev;
5224426Smckusic 	int osize, nsize;
5234426Smckusic {
5245965Smckusic 	register struct fs *fs;
5254463Smckusic 	register struct cg *cgp;
52637735Smckusick 	struct buf *bp;
5274463Smckusic 	long bno;
5284463Smckusic 	int frags, bbase;
52937735Smckusick 	int i, error;
5304426Smckusic 
5315965Smckusic 	fs = ip->i_fs;
53217224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
5336531Smckusick 		return (NULL);
5345960Smckusic 	frags = numfrags(fs, nsize);
53517224Smckusick 	bbase = fragnum(fs, bprev);
53617224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
53730749Skarels 		/* cannot extend across a block boundary */
5386294Smckusick 		return (NULL);
5394463Smckusic 	}
54037735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
54138776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
54237735Smckusick 	if (error) {
54337735Smckusick 		brelse(bp);
54437735Smckusick 		return (NULL);
54537735Smckusick 	}
5466531Smckusick 	cgp = bp->b_un.b_cg;
54737735Smckusick 	if (!cg_chkmagic(cgp)) {
5485960Smckusic 		brelse(bp);
5496294Smckusick 		return (NULL);
5505960Smckusic 	}
5518105Sroot 	cgp->cg_time = time.tv_sec;
5525377Smckusic 	bno = dtogd(fs, bprev);
5535960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
55434143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
5555361Smckusic 			brelse(bp);
5566294Smckusick 			return (NULL);
5575361Smckusic 		}
5585361Smckusic 	/*
5595361Smckusic 	 * the current fragment can be extended
5605361Smckusic 	 * deduct the count on fragment being extended into
5615361Smckusic 	 * increase the count on the remaining fragment (if any)
5625361Smckusic 	 * allocate the extended piece
5635361Smckusic 	 */
5645361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
56534143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
5664463Smckusic 			break;
5675960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5685361Smckusic 	if (i != frags)
5695361Smckusic 		cgp->cg_frsum[i - frags]++;
5705960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
57134143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
5725361Smckusic 		cgp->cg_cs.cs_nffree--;
5735361Smckusic 		fs->fs_cstotal.cs_nffree--;
5745361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5754463Smckusic 	}
57650893Smckusick 	fs->fs_fmod = 1;
5775361Smckusic 	bdwrite(bp);
5785361Smckusic 	return (bprev);
5794426Smckusic }
5804426Smckusic 
5815375Smckusic /*
5825375Smckusic  * Determine whether a block can be allocated.
5835375Smckusic  *
5845375Smckusic  * Check to see if a block of the apprpriate size is available,
5855375Smckusic  * and if it is, allocate it.
5865375Smckusic  */
58751779Smarc static daddr_t
58851469Sbostic ffs_alloccg(ip, cg, bpref, size)
5895965Smckusic 	struct inode *ip;
5904359Smckusick 	int cg;
5914359Smckusick 	daddr_t bpref;
5924359Smckusick 	int size;
5934359Smckusick {
5945965Smckusic 	register struct fs *fs;
5954463Smckusic 	register struct cg *cgp;
59637735Smckusick 	struct buf *bp;
5974463Smckusic 	register int i;
59837735Smckusick 	int error, bno, frags, allocsiz;
5994359Smckusick 
6005965Smckusic 	fs = ip->i_fs;
6015322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
6026294Smckusick 		return (NULL);
60337735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
60438776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
60537735Smckusick 	if (error) {
60637735Smckusick 		brelse(bp);
60737735Smckusick 		return (NULL);
60837735Smckusick 	}
6096531Smckusick 	cgp = bp->b_un.b_cg;
61037735Smckusick 	if (!cg_chkmagic(cgp) ||
61115950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
6125960Smckusic 		brelse(bp);
6136294Smckusick 		return (NULL);
6145960Smckusic 	}
6158105Sroot 	cgp->cg_time = time.tv_sec;
6165322Smckusic 	if (size == fs->fs_bsize) {
61751469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
6184463Smckusic 		bdwrite(bp);
6194463Smckusic 		return (bno);
6204463Smckusic 	}
6214463Smckusic 	/*
6224463Smckusic 	 * check to see if any fragments are already available
6234463Smckusic 	 * allocsiz is the size which will be allocated, hacking
6244463Smckusic 	 * it down to a smaller size if necessary
6254463Smckusic 	 */
6265960Smckusic 	frags = numfrags(fs, size);
6275322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
6284463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
6294463Smckusic 			break;
6305322Smckusic 	if (allocsiz == fs->fs_frag) {
6314463Smckusic 		/*
6324463Smckusic 		 * no fragments were available, so a block will be
6334463Smckusic 		 * allocated, and hacked up
6344463Smckusic 		 */
6354792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
6364463Smckusic 			brelse(bp);
6376294Smckusick 			return (NULL);
6384463Smckusic 		}
63951469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
6405377Smckusic 		bpref = dtogd(fs, bno);
6415322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
64234143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
6435322Smckusic 		i = fs->fs_frag - frags;
6444792Smckusic 		cgp->cg_cs.cs_nffree += i;
6454792Smckusic 		fs->fs_cstotal.cs_nffree += i;
6465322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
64750893Smckusick 		fs->fs_fmod = 1;
6484463Smckusic 		cgp->cg_frsum[i]++;
6494463Smckusic 		bdwrite(bp);
6504463Smckusic 		return (bno);
6514463Smckusic 	}
65251469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
65315950Smckusick 	if (bno < 0) {
65415950Smckusick 		brelse(bp);
6556294Smckusick 		return (NULL);
65615950Smckusick 	}
6574463Smckusic 	for (i = 0; i < frags; i++)
65834143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
6594792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6604792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6615322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
66250893Smckusick 	fs->fs_fmod = 1;
6634463Smckusic 	cgp->cg_frsum[allocsiz]--;
6644463Smckusic 	if (frags != allocsiz)
6654463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6664463Smckusic 	bdwrite(bp);
6674463Smckusic 	return (cg * fs->fs_fpg + bno);
6684463Smckusic }
6694463Smckusic 
6705375Smckusic /*
6715375Smckusic  * Allocate a block in a cylinder group.
6725375Smckusic  *
6735375Smckusic  * This algorithm implements the following policy:
6745375Smckusic  *   1) allocate the requested block.
6755375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6765375Smckusic  *   3) allocate the next available block on the block rotor for the
6775375Smckusic  *      specified cylinder group.
6785375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6795375Smckusic  * blocks may be fragmented by the routine that allocates them.
6805375Smckusic  */
68151469Sbostic static daddr_t
68251469Sbostic ffs_alloccgblk(fs, cgp, bpref)
6835965Smckusic 	register struct fs *fs;
6844463Smckusic 	register struct cg *cgp;
6854463Smckusic 	daddr_t bpref;
6864463Smckusic {
6874651Smckusic 	daddr_t bno;
6886294Smckusick 	int cylno, pos, delta;
6894651Smckusic 	short *cylbp;
6905361Smckusic 	register int i;
6914463Smckusic 
6924651Smckusic 	if (bpref == 0) {
6934651Smckusic 		bpref = cgp->cg_rotor;
6945361Smckusic 		goto norot;
6955361Smckusic 	}
69617224Smckusick 	bpref = blknum(fs, bpref);
6975377Smckusic 	bpref = dtogd(fs, bpref);
6985361Smckusic 	/*
6995361Smckusic 	 * if the requested block is available, use it
7005361Smckusic 	 */
70151469Sbostic 	if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
7025361Smckusic 		bno = bpref;
7035361Smckusic 		goto gotit;
7045361Smckusic 	}
7055361Smckusic 	/*
7065361Smckusic 	 * check for a block available on the same cylinder
7075361Smckusic 	 */
7085361Smckusic 	cylno = cbtocylno(fs, bpref);
70934143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
7105375Smckusic 		goto norot;
7115375Smckusic 	if (fs->fs_cpc == 0) {
7125375Smckusic 		/*
713*57000Smckusick 		 * Block layout information is not available.
714*57000Smckusick 		 * Leaving bpref unchanged means we take the
715*57000Smckusick 		 * next available free block following the one
716*57000Smckusick 		 * we just allocated. Hopefully this will at
717*57000Smckusick 		 * least hit a track cache on drives of unknown
718*57000Smckusick 		 * geometry (e.g. SCSI).
7195375Smckusic 		 */
7205375Smckusic 		goto norot;
7215375Smckusic 	}
7225375Smckusic 	/*
7235361Smckusic 	 * check the summary information to see if a block is
7245361Smckusic 	 * available in the requested cylinder starting at the
7259163Ssam 	 * requested rotational position and proceeding around.
7265361Smckusic 	 */
72734143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
7289163Ssam 	pos = cbtorpos(fs, bpref);
72934143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
7305361Smckusic 		if (cylbp[i] > 0)
7315361Smckusic 			break;
73234143Smckusick 	if (i == fs->fs_nrpos)
7335361Smckusic 		for (i = 0; i < pos; i++)
7345361Smckusic 			if (cylbp[i] > 0)
7355361Smckusic 				break;
7365361Smckusic 	if (cylbp[i] > 0) {
7374651Smckusic 		/*
7385361Smckusic 		 * found a rotational position, now find the actual
7395361Smckusic 		 * block. A panic if none is actually there.
7404651Smckusic 		 */
7415361Smckusic 		pos = cylno % fs->fs_cpc;
7425361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
74334143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
7446716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
7456716Smckusick 			    pos, i, fs->fs_fsmnt);
74651469Sbostic 			panic("ffs_alloccgblk: cyl groups corrupted");
7476716Smckusick 		}
74834143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
74951469Sbostic 			if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
75011638Ssam 				bno = blkstofrags(fs, (bno + i));
7515361Smckusic 				goto gotit;
7525361Smckusic 			}
75334143Smckusick 			delta = fs_rotbl(fs)[i];
75434143Smckusick 			if (delta <= 0 ||
75534143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
7564651Smckusic 				break;
7576294Smckusick 			i += delta;
7584651Smckusic 		}
7596716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
76051469Sbostic 		panic("ffs_alloccgblk: can't find blk in cyl");
7614359Smckusick 	}
7625361Smckusic norot:
7635361Smckusic 	/*
7645361Smckusic 	 * no blocks in the requested cylinder, so take next
7655361Smckusic 	 * available one in this cylinder group.
7665361Smckusic 	 */
76751469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7686567Smckusic 	if (bno < 0)
7696294Smckusick 		return (NULL);
7704651Smckusic 	cgp->cg_rotor = bno;
7714359Smckusick gotit:
77251469Sbostic 	ffs_clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno));
7734792Smckusic 	cgp->cg_cs.cs_nbfree--;
7744792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7755322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7765375Smckusic 	cylno = cbtocylno(fs, bno);
77734143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
77834143Smckusick 	cg_blktot(cgp)[cylno]--;
77950893Smckusick 	fs->fs_fmod = 1;
7804651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7814359Smckusick }
78234143Smckusick 
7835375Smckusic /*
7845375Smckusic  * Determine whether an inode can be allocated.
7855375Smckusic  *
7865375Smckusic  * Check to see if an inode is available, and if it is,
7875375Smckusic  * allocate it using the following policy:
7885375Smckusic  *   1) allocate the requested inode.
7895375Smckusic  *   2) allocate the next available inode after the requested
7905375Smckusic  *      inode in the specified cylinder group.
7915375Smckusic  */
79251469Sbostic static ino_t
79351469Sbostic ffs_ialloccg(ip, cg, ipref, mode)
7945965Smckusic 	struct inode *ip;
7954359Smckusick 	int cg;
7964359Smckusick 	daddr_t ipref;
7974359Smckusick 	int mode;
7984359Smckusick {
7995965Smckusic 	register struct fs *fs;
8004463Smckusic 	register struct cg *cgp;
80116784Smckusick 	struct buf *bp;
80237735Smckusick 	int error, start, len, loc, map, i;
8034359Smckusick 
8045965Smckusic 	fs = ip->i_fs;
8055322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
8066294Smckusick 		return (NULL);
80737735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
80838776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
80937735Smckusick 	if (error) {
81037735Smckusick 		brelse(bp);
81137735Smckusick 		return (NULL);
81237735Smckusick 	}
8136531Smckusick 	cgp = bp->b_un.b_cg;
81437735Smckusick 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
8155960Smckusic 		brelse(bp);
8166294Smckusick 		return (NULL);
8175960Smckusic 	}
8188105Sroot 	cgp->cg_time = time.tv_sec;
8194359Smckusick 	if (ipref) {
8204359Smckusick 		ipref %= fs->fs_ipg;
82134143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
8224359Smckusick 			goto gotit;
82316784Smckusick 	}
82416784Smckusick 	start = cgp->cg_irotor / NBBY;
82516784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
82634143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
82716784Smckusick 	if (loc == 0) {
82817697Smckusick 		len = start + 1;
82917697Smckusick 		start = 0;
83034143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
83117697Smckusick 		if (loc == 0) {
83217697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
83317697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
83451469Sbostic 			panic("ffs_ialloccg: map corrupted");
83517697Smckusick 			/* NOTREACHED */
83617697Smckusick 		}
83716784Smckusick 	}
83816784Smckusick 	i = start + len - loc;
83934143Smckusick 	map = cg_inosused(cgp)[i];
84016784Smckusick 	ipref = i * NBBY;
84116784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
84216784Smckusick 		if ((map & i) == 0) {
8434359Smckusick 			cgp->cg_irotor = ipref;
8444359Smckusick 			goto gotit;
8454359Smckusick 		}
8464359Smckusick 	}
84716784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
84851469Sbostic 	panic("ffs_ialloccg: block not in map");
84916784Smckusick 	/* NOTREACHED */
8504359Smckusick gotit:
85134143Smckusick 	setbit(cg_inosused(cgp), ipref);
8524792Smckusic 	cgp->cg_cs.cs_nifree--;
8534792Smckusic 	fs->fs_cstotal.cs_nifree--;
8545322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
85550893Smckusick 	fs->fs_fmod = 1;
8564359Smckusick 	if ((mode & IFMT) == IFDIR) {
8574792Smckusic 		cgp->cg_cs.cs_ndir++;
8584792Smckusic 		fs->fs_cstotal.cs_ndir++;
8595322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8604359Smckusick 	}
8614359Smckusick 	bdwrite(bp);
8624359Smckusick 	return (cg * fs->fs_ipg + ipref);
8634359Smckusick }
8644359Smckusick 
8655375Smckusic /*
8665375Smckusic  * Free a block or fragment.
8675375Smckusic  *
8685375Smckusic  * The specified block or fragment is placed back in the
8695375Smckusic  * free map. If a fragment is deallocated, a possible
8705375Smckusic  * block reassembly is checked.
8715375Smckusic  */
87251469Sbostic ffs_blkfree(ip, bno, size)
8735965Smckusic 	register struct inode *ip;
8744359Smckusick 	daddr_t bno;
87553059Smckusick 	long size;
8764359Smckusick {
8774359Smckusick 	register struct fs *fs;
8784359Smckusick 	register struct cg *cgp;
87937735Smckusick 	struct buf *bp;
88037735Smckusick 	int error, cg, blk, frags, bbase;
8814463Smckusic 	register int i;
8824359Smckusick 
8835965Smckusic 	fs = ip->i_fs;
8846716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8856716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8866716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
88731402Smckusick 		panic("blkfree: bad size");
8886716Smckusick 	}
8895377Smckusic 	cg = dtog(fs, bno);
89042318Smckusick 	if ((unsigned)bno >= fs->fs_size) {
8916567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
89253244Smckusick 		ffs_fserr(fs, ip->i_uid, "bad block");
8934359Smckusick 		return;
8946567Smckusic 	}
89537735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
89638776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
89737735Smckusick 	if (error) {
89837735Smckusick 		brelse(bp);
89937735Smckusick 		return;
90037735Smckusick 	}
9016531Smckusick 	cgp = bp->b_un.b_cg;
90237735Smckusick 	if (!cg_chkmagic(cgp)) {
9035960Smckusic 		brelse(bp);
9044359Smckusick 		return;
9055960Smckusic 	}
9068105Sroot 	cgp->cg_time = time.tv_sec;
9075377Smckusic 	bno = dtogd(fs, bno);
9085322Smckusic 	if (size == fs->fs_bsize) {
90951469Sbostic 		if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) {
9106716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
9116716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
91231402Smckusick 			panic("blkfree: freeing free block");
9136567Smckusic 		}
91451469Sbostic 		ffs_setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
9154792Smckusic 		cgp->cg_cs.cs_nbfree++;
9164792Smckusic 		fs->fs_cstotal.cs_nbfree++;
9175322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
9185375Smckusic 		i = cbtocylno(fs, bno);
91934143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
92034143Smckusick 		cg_blktot(cgp)[i]++;
9214426Smckusic 	} else {
92217224Smckusick 		bbase = bno - fragnum(fs, bno);
9234463Smckusic 		/*
9244463Smckusic 		 * decrement the counts associated with the old frags
9254463Smckusic 		 */
92634143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
92751469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
9284463Smckusic 		/*
9294463Smckusic 		 * deallocate the fragment
9304463Smckusic 		 */
9315960Smckusic 		frags = numfrags(fs, size);
9324463Smckusic 		for (i = 0; i < frags; i++) {
93334143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
9346716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
9356716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
93631402Smckusick 				panic("blkfree: freeing free frag");
9376716Smckusick 			}
93834143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
9394426Smckusic 		}
9406294Smckusick 		cgp->cg_cs.cs_nffree += i;
9416294Smckusick 		fs->fs_cstotal.cs_nffree += i;
9426294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
9434463Smckusic 		/*
9444463Smckusic 		 * add back in counts associated with the new frags
9454463Smckusic 		 */
94634143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
94751469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
9484463Smckusic 		/*
9494463Smckusic 		 * if a complete block has been reassembled, account for it
9504463Smckusic 		 */
95151469Sbostic 		if (ffs_isblock(fs, cg_blksfree(cgp),
95234476Smckusick 		    (daddr_t)fragstoblks(fs, bbase))) {
9535322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
9545322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
9555322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
9564792Smckusic 			cgp->cg_cs.cs_nbfree++;
9574792Smckusic 			fs->fs_cstotal.cs_nbfree++;
9585322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
9595375Smckusic 			i = cbtocylno(fs, bbase);
96034143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
96134143Smckusick 			cg_blktot(cgp)[i]++;
9624426Smckusic 		}
9634426Smckusic 	}
96450893Smckusick 	fs->fs_fmod = 1;
9654359Smckusick 	bdwrite(bp);
9664359Smckusick }
9674359Smckusick 
9685375Smckusic /*
9695375Smckusic  * Free an inode.
9705375Smckusic  *
9715375Smckusic  * The specified inode is placed back in the free map.
9725375Smckusic  */
97353577Sheideman int
97454653Smckusick ffs_vfree(ap)
97554653Smckusick 	struct vop_vfree_args /* {
97654653Smckusick 		struct vnode *a_pvp;
97754653Smckusick 		ino_t a_ino;
97854653Smckusick 		int a_mode;
97954653Smckusick 	} */ *ap;
9804359Smckusick {
9814359Smckusick 	register struct fs *fs;
9824359Smckusick 	register struct cg *cgp;
98351541Smckusick 	register struct inode *pip;
98454653Smckusick 	ino_t ino = ap->a_ino;
98537735Smckusick 	struct buf *bp;
98637735Smckusick 	int error, cg;
9874359Smckusick 
98853583Sheideman 	pip = VTOI(ap->a_pvp);
98951469Sbostic 	fs = pip->i_fs;
99054653Smckusick 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
99154653Smckusick 		panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
99254653Smckusick 		    pip->i_dev, ino, fs->fs_fsmnt);
99354653Smckusick 	cg = itog(fs, ino);
99451469Sbostic 	error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
99538776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
99637735Smckusick 	if (error) {
99737735Smckusick 		brelse(bp);
99853577Sheideman 		return (0);
99937735Smckusick 	}
10006531Smckusick 	cgp = bp->b_un.b_cg;
100137735Smckusick 	if (!cg_chkmagic(cgp)) {
10025960Smckusic 		brelse(bp);
100353577Sheideman 		return (0);
10045960Smckusic 	}
10058105Sroot 	cgp->cg_time = time.tv_sec;
100654653Smckusick 	ino %= fs->fs_ipg;
100754653Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
100854653Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
100954653Smckusick 		    pip->i_dev, ino, fs->fs_fsmnt);
101048057Smckusick 		if (fs->fs_ronly == 0)
101148057Smckusick 			panic("ifree: freeing free inode");
10126716Smckusick 	}
101354653Smckusick 	clrbit(cg_inosused(cgp), ino);
101454653Smckusick 	if (ino < cgp->cg_irotor)
101554653Smckusick 		cgp->cg_irotor = ino;
10164792Smckusic 	cgp->cg_cs.cs_nifree++;
10174792Smckusic 	fs->fs_cstotal.cs_nifree++;
10185322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
101953583Sheideman 	if ((ap->a_mode & IFMT) == IFDIR) {
10204792Smckusic 		cgp->cg_cs.cs_ndir--;
10214792Smckusic 		fs->fs_cstotal.cs_ndir--;
10225322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
10234359Smckusick 	}
102450893Smckusick 	fs->fs_fmod = 1;
10254359Smckusick 	bdwrite(bp);
102653577Sheideman 	return (0);
10274359Smckusick }
10284359Smckusick 
10294463Smckusic /*
10305375Smckusic  * Find a block of the specified size in the specified cylinder group.
10315375Smckusic  *
10324651Smckusic  * It is a panic if a request is made to find a block if none are
10334651Smckusic  * available.
10344651Smckusic  */
103551469Sbostic static daddr_t
103651469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz)
10374651Smckusic 	register struct fs *fs;
10384651Smckusic 	register struct cg *cgp;
10394651Smckusic 	daddr_t bpref;
10404651Smckusic 	int allocsiz;
10414651Smckusic {
10424651Smckusic 	daddr_t bno;
10434651Smckusic 	int start, len, loc, i;
10444651Smckusic 	int blk, field, subfield, pos;
10454651Smckusic 
10464651Smckusic 	/*
10474651Smckusic 	 * find the fragment by searching through the free block
10484651Smckusic 	 * map for an appropriate bit pattern
10494651Smckusic 	 */
10504651Smckusic 	if (bpref)
10515377Smckusic 		start = dtogd(fs, bpref) / NBBY;
10524651Smckusic 	else
10534651Smckusic 		start = cgp->cg_frotor / NBBY;
10545398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
105534476Smckusick 	loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start],
105634476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
105734476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
10584651Smckusic 	if (loc == 0) {
10596531Smckusick 		len = start + 1;
10606531Smckusick 		start = 0;
106134476Smckusick 		loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0],
106234476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
106334476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
106416784Smckusick 		if (loc == 0) {
106516784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
106616784Smckusick 			    start, len, fs->fs_fsmnt);
106751469Sbostic 			panic("ffs_alloccg: map corrupted");
106817697Smckusick 			/* NOTREACHED */
106916784Smckusick 		}
10704651Smckusic 	}
10714651Smckusic 	bno = (start + len - loc) * NBBY;
10724651Smckusic 	cgp->cg_frotor = bno;
10734651Smckusic 	/*
10744651Smckusic 	 * found the byte in the map
10754651Smckusic 	 * sift through the bits to find the selected frag
10764651Smckusic 	 */
10776294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
107834143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
10794651Smckusic 		blk <<= 1;
10804651Smckusic 		field = around[allocsiz];
10814651Smckusic 		subfield = inside[allocsiz];
10825322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10836294Smckusick 			if ((blk & field) == subfield)
10846294Smckusick 				return (bno + pos);
10854651Smckusic 			field <<= 1;
10864651Smckusic 			subfield <<= 1;
10874651Smckusic 		}
10884651Smckusic 	}
10896716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
109051469Sbostic 	panic("ffs_alloccg: block not in map");
10916531Smckusick 	return (-1);
10924651Smckusic }
10934651Smckusic 
10944651Smckusic /*
10955375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10965375Smckusic  *
10975375Smckusic  * The form of the error message is:
10984359Smckusick  *	fs: error message
10994359Smckusick  */
110051469Sbostic static void
110151469Sbostic ffs_fserr(fs, uid, cp)
11024359Smckusick 	struct fs *fs;
110351469Sbostic 	u_int uid;
11044359Smckusick 	char *cp;
11054359Smckusick {
11064359Smckusick 
110742318Smckusick 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
11084359Smckusick }
1109