xref: /csrg-svn/sys/ufs/ffs/ffs_alloc.c (revision 53865)
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*53865Sheideman  *	@(#)ffs_alloc.c	7.37 (Berkeley) 06/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>
1551469Sbostic #include <sys/kernel.h>
1651469Sbostic #include <sys/syslog.h>
174359Smckusick 
1853317Smckusick #include <vm/vm.h>
1953317Smckusick 
2051469Sbostic #include <ufs/ufs/quota.h>
2151469Sbostic #include <ufs/ufs/inode.h>
2247571Skarels 
2351469Sbostic #include <ufs/ffs/fs.h>
2451469Sbostic #include <ufs/ffs/ffs_extern.h>
254359Smckusick 
2651469Sbostic extern u_long nextgennumber;
2751469Sbostic 
2851469Sbostic static daddr_t	ffs_alloccg __P((struct inode *, int, daddr_t, int));
2951469Sbostic static daddr_t	ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t));
3051469Sbostic static ino_t	ffs_dirpref __P((struct fs *));
3151469Sbostic static daddr_t	ffs_fragextend __P((struct inode *, int, long, int, int));
3251469Sbostic static void	ffs_fserr __P((struct fs *, u_int, char *));
3351469Sbostic static u_long	ffs_hashalloc
3451469Sbostic 		    __P((struct inode *, int, long, int, u_long (*)()));
3551469Sbostic static ino_t	ffs_ialloccg __P((struct inode *, int, daddr_t, int));
3651469Sbostic static daddr_t	ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int));
3751469Sbostic 
385375Smckusic /*
395375Smckusic  * Allocate a block in the file system.
405375Smckusic  *
415375Smckusic  * The size of the requested block is given, which must be some
425375Smckusic  * multiple of fs_fsize and <= fs_bsize.
435375Smckusic  * A preference may be optionally specified. If a preference is given
445375Smckusic  * the following hierarchy is used to allocate a block:
455375Smckusic  *   1) allocate the requested block.
465375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
475375Smckusic  *   3) allocate a block in the same cylinder group.
485375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
495375Smckusic  *      available block is located.
505375Smckusic  * If no block preference is given the following heirarchy is used
515375Smckusic  * to allocate a block:
525375Smckusic  *   1) allocate a block in the cylinder group that contains the
535375Smckusic  *      inode for the file.
545375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
555375Smckusic  *      available block is located.
565375Smckusic  */
5753244Smckusick ffs_alloc(ip, lbn, bpref, size, cred, bnp)
584463Smckusic 	register struct inode *ip;
5939678Smckusick 	daddr_t lbn, bpref;
604359Smckusick 	int size;
6153244Smckusick 	struct ucred *cred;
6239678Smckusick 	daddr_t *bnp;
634359Smckusick {
644359Smckusick 	daddr_t bno;
654359Smckusick 	register struct fs *fs;
664463Smckusic 	register struct buf *bp;
6737735Smckusick 	int cg, error;
684359Smckusick 
6939678Smckusick 	*bnp = 0;
705965Smckusic 	fs = ip->i_fs;
7153244Smckusick #ifdef DIAGNOSTIC
726716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
736716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
746716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7551469Sbostic 		panic("ffs_alloc: bad size");
766716Smckusick 	}
7753244Smckusick 	if (cred == NOCRED)
7853244Smckusick 		panic("ffs_alloc: missing credential\n");
7953244Smckusick #endif /* DIAGNOSTIC */
805322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
814359Smckusick 		goto nospace;
8241309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
834792Smckusic 		goto nospace;
847650Ssam #ifdef QUOTA
8541309Smckusick 	if (error = chkdq(ip, (long)btodb(size), cred, 0))
8637735Smckusick 		return (error);
877483Skre #endif
884948Smckusic 	if (bpref >= fs->fs_size)
894948Smckusic 		bpref = 0;
904359Smckusick 	if (bpref == 0)
915377Smckusic 		cg = itog(fs, ip->i_number);
924359Smckusick 	else
935377Smckusic 		cg = dtog(fs, bpref);
9451469Sbostic 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
9551469Sbostic 	    (u_long (*)())ffs_alloccg);
9639678Smckusick 	if (bno > 0) {
9739678Smckusick 		ip->i_blocks += btodb(size);
9839678Smckusick 		ip->i_flag |= IUPD|ICHG;
9939678Smckusick 		*bnp = bno;
10039678Smckusick 		return (0);
10139678Smckusick 	}
10245173Smckusick #ifdef QUOTA
10345173Smckusick 	/*
10445173Smckusick 	 * Restore user's disk quota because allocation failed.
10545173Smckusick 	 */
10645173Smckusick 	(void) chkdq(ip, (long)-btodb(size), cred, FORCE);
10745173Smckusick #endif
1084359Smckusick nospace:
10951469Sbostic 	ffs_fserr(fs, cred->cr_uid, "file system full");
1104359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
11137735Smckusick 	return (ENOSPC);
1124359Smckusick }
1134359Smckusick 
1145375Smckusic /*
1155375Smckusic  * Reallocate a fragment to a bigger size
1165375Smckusic  *
1175375Smckusic  * The number and size of the old block is given, and a preference
1185375Smckusic  * and new size is also specified. The allocator attempts to extend
1195375Smckusic  * the original block. Failing that, the regular block allocator is
1205375Smckusic  * invoked to get an appropriate block.
1215375Smckusic  */
12253244Smckusick ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
1235965Smckusic 	register struct inode *ip;
12453059Smckusick 	daddr_t lbprev;
12539678Smckusick 	daddr_t bpref;
1264426Smckusic 	int osize, nsize;
12753244Smckusick 	struct ucred *cred;
12837735Smckusick 	struct buf **bpp;
1294426Smckusic {
1304426Smckusic 	register struct fs *fs;
13137735Smckusick 	struct buf *bp, *obp;
13245719Smckusick 	int cg, request, error;
13345719Smckusick 	daddr_t bprev, bno;
1344426Smckusic 
13537735Smckusick 	*bpp = 0;
1365965Smckusic 	fs = ip->i_fs;
13753244Smckusick #ifdef DIAGNOSTIC
1385960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1396716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
14051469Sbostic 		printf(
14151469Sbostic 		    "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1426716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
14351469Sbostic 		panic("ffs_realloccg: bad size");
1446716Smckusick 	}
14553244Smckusick 	if (cred == NOCRED)
14653244Smckusick 		panic("ffs_realloccg: missing credential\n");
14753244Smckusick #endif /* DIAGNOSTIC */
14841309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1494792Smckusic 		goto nospace;
15039678Smckusick 	if ((bprev = ip->i_db[lbprev]) == 0) {
1516716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1526716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
15351469Sbostic 		panic("ffs_realloccg: bad bprev");
1546716Smckusick 	}
15539678Smckusick 	/*
15639678Smckusick 	 * Allocate the extra space in the buffer.
15739678Smckusick 	 */
15839678Smckusick 	if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
15939678Smckusick 		brelse(bp);
16039678Smckusick 		return (error);
16139678Smckusick 	}
16245173Smckusick #ifdef QUOTA
16345173Smckusick 	if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) {
16445173Smckusick 		brelse(bp);
16545173Smckusick 		return (error);
16645173Smckusick 	}
16745173Smckusick #endif
16839678Smckusick 	/*
16939678Smckusick 	 * Check for extension in the existing location.
17039678Smckusick 	 */
1716294Smckusick 	cg = dtog(fs, bprev);
17251469Sbostic 	if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) {
17339887Smckusick 		if (bp->b_blkno != fsbtodb(fs, bno))
17439678Smckusick 			panic("bad blockno");
17539762Smckusick 		ip->i_blocks += btodb(nsize - osize);
17639762Smckusick 		ip->i_flag |= IUPD|ICHG;
17748948Smckusick 		allocbuf(bp, nsize);
17848948Smckusick 		bp->b_flags |= B_DONE;
17948948Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
18037735Smckusick 		*bpp = bp;
18137735Smckusick 		return (0);
1824463Smckusic 	}
18339678Smckusick 	/*
18439678Smckusick 	 * Allocate a new disk location.
18539678Smckusick 	 */
1864948Smckusic 	if (bpref >= fs->fs_size)
1874948Smckusic 		bpref = 0;
18826253Skarels 	switch ((int)fs->fs_optim) {
18925256Smckusick 	case FS_OPTSPACE:
19025256Smckusick 		/*
19125256Smckusick 		 * Allocate an exact sized fragment. Although this makes
19225256Smckusick 		 * best use of space, we will waste time relocating it if
19325256Smckusick 		 * the file continues to grow. If the fragmentation is
19425256Smckusick 		 * less than half of the minimum free reserve, we choose
19525256Smckusick 		 * to begin optimizing for time.
19625256Smckusick 		 */
19724698Smckusick 		request = nsize;
19825256Smckusick 		if (fs->fs_minfree < 5 ||
19925256Smckusick 		    fs->fs_cstotal.cs_nffree >
20025256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
20125256Smckusick 			break;
20225256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
20325256Smckusick 			fs->fs_fsmnt);
20425256Smckusick 		fs->fs_optim = FS_OPTTIME;
20525256Smckusick 		break;
20625256Smckusick 	case FS_OPTTIME:
20725256Smckusick 		/*
20851469Sbostic 		 * At this point we have discovered a file that is trying to
20951469Sbostic 		 * grow a small fragment to a larger fragment. To save time,
21051469Sbostic 		 * we allocate a full sized block, then free the unused portion.
21151469Sbostic 		 * If the file continues to grow, the `ffs_fragextend' call
21251469Sbostic 		 * above will be able to grow it in place without further
21351469Sbostic 		 * copying. If aberrant programs cause disk fragmentation to
21451469Sbostic 		 * grow within 2% of the free reserve, we choose to begin
21551469Sbostic 		 * optimizing for space.
21625256Smckusick 		 */
21724698Smckusick 		request = fs->fs_bsize;
21825256Smckusick 		if (fs->fs_cstotal.cs_nffree <
21925256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
22025256Smckusick 			break;
22125256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
22225256Smckusick 			fs->fs_fsmnt);
22325256Smckusick 		fs->fs_optim = FS_OPTSPACE;
22425256Smckusick 		break;
22525256Smckusick 	default:
22625256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
22725256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
22851469Sbostic 		panic("ffs_realloccg: bad optim");
22925256Smckusick 		/* NOTREACHED */
23025256Smckusick 	}
23151469Sbostic 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
23251469Sbostic 	    (u_long (*)())ffs_alloccg);
2336567Smckusic 	if (bno > 0) {
23445719Smckusick 		bp->b_blkno = fsbtodb(fs, bno);
23545719Smckusick 		(void) vnode_pager_uncache(ITOV(ip));
23653059Smckusick 		ffs_blkfree(ip, bprev, (long)osize);
23724698Smckusick 		if (nsize < request)
23851469Sbostic 			ffs_blkfree(ip, bno + numfrags(fs, nsize),
23953059Smckusick 			    (long)(request - nsize));
24012643Ssam 		ip->i_blocks += btodb(nsize - osize);
24112643Ssam 		ip->i_flag |= IUPD|ICHG;
24248948Smckusick 		allocbuf(bp, nsize);
24348948Smckusick 		bp->b_flags |= B_DONE;
24448948Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
24537735Smckusick 		*bpp = bp;
24637735Smckusick 		return (0);
2474463Smckusic 	}
24845173Smckusick #ifdef QUOTA
24945173Smckusick 	/*
25045173Smckusick 	 * Restore user's disk quota because allocation failed.
25145173Smckusick 	 */
25245173Smckusick 	(void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
25345173Smckusick #endif
25439678Smckusick 	brelse(bp);
2554792Smckusic nospace:
2564463Smckusic 	/*
2574463Smckusic 	 * no space available
2584463Smckusic 	 */
25951469Sbostic 	ffs_fserr(fs, cred->cr_uid, "file system full");
2604426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
26137735Smckusick 	return (ENOSPC);
2624426Smckusic }
2634426Smckusic 
2645375Smckusic /*
2655375Smckusic  * Allocate an inode in the file system.
2665375Smckusic  *
26751469Sbostic  * If allocating a directory, use ffs_dirpref to select the inode.
26851469Sbostic  * If allocating in a directory, the following hierarchy is followed:
26951469Sbostic  *   1) allocate the preferred inode.
2705375Smckusic  *   2) allocate an inode in the same cylinder group.
2715375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2725375Smckusic  *      available inode is located.
2735375Smckusic  * If no inode preference is given the following heirarchy is used
2745375Smckusic  * to allocate an inode:
2755375Smckusic  *   1) allocate an inode in cylinder group 0.
2765375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2775375Smckusic  *      available inode is located.
2785375Smckusic  */
27953519Sheideman ffs_valloc (ap)
28053519Sheideman 	struct vop_valloc_args *ap;
2814359Smckusick {
28253519Sheideman 	USES_VOP_VFREE;
28353519Sheideman 	USES_VOP_VGET;
284*53865Sheideman 	register struct vnode *pvp = ap->a_pvp;
28551541Smckusick 	register struct inode *pip;
2864359Smckusick 	register struct fs *fs;
2874359Smckusick 	register struct inode *ip;
28851469Sbostic 	ino_t ino, ipref;
28937735Smckusick 	int cg, error;
2904359Smckusick 
29153583Sheideman 	*ap->a_vpp = NULL;
292*53865Sheideman 	pip = VTOI(pvp);
2935965Smckusic 	fs = pip->i_fs;
2944792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2954359Smckusick 		goto noinodes;
29651469Sbostic 
29753583Sheideman 	if ((ap->a_mode & IFMT) == IFDIR)
29851541Smckusick 		ipref = ffs_dirpref(fs);
29951469Sbostic 	else
30051469Sbostic 		ipref = pip->i_number;
3014948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
3024948Smckusic 		ipref = 0;
3035377Smckusic 	cg = itog(fs, ipref);
30453583Sheideman 	ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, ap->a_mode, ffs_ialloccg);
3054359Smckusick 	if (ino == 0)
3064359Smckusick 		goto noinodes;
307*53865Sheideman 	error = FFS_VGET(pvp->v_mount, ino, ap->a_vpp);
30837735Smckusick 	if (error) {
309*53865Sheideman 		VOP_VFREE(pvp, ino, ap->a_mode);
31037735Smckusick 		return (error);
3114359Smckusick 	}
31253583Sheideman 	ip = VTOI(*ap->a_vpp);
3136716Smckusick 	if (ip->i_mode) {
31453583Sheideman 		printf("ap->a_mode = 0%o, inum = %d, fs = %s\n",
3156716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
31651541Smckusick 		panic("ffs_valloc: dup alloc");
3176716Smckusick 	}
31812643Ssam 	if (ip->i_blocks) {				/* XXX */
31912643Ssam 		printf("free inode %s/%d had %d blocks\n",
32012643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
32112643Ssam 		ip->i_blocks = 0;
32212643Ssam 	}
32339516Smckusick 	ip->i_flags = 0;
32438255Smckusick 	/*
32538255Smckusick 	 * Set up a new generation number for this inode.
32638255Smckusick 	 */
32738255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
32838255Smckusick 		nextgennumber = time.tv_sec;
32938255Smckusick 	ip->i_gen = nextgennumber;
33037735Smckusick 	return (0);
3314359Smckusick noinodes:
33253583Sheideman 	ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
3336294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
33437735Smckusick 	return (ENOSPC);
3354359Smckusick }
3364359Smckusick 
3374651Smckusic /*
3385375Smckusic  * Find a cylinder to place a directory.
3395375Smckusic  *
3405375Smckusic  * The policy implemented by this algorithm is to select from
3415375Smckusic  * among those cylinder groups with above the average number of
3425375Smckusic  * free inodes, the one with the smallest number of directories.
3434651Smckusic  */
34451469Sbostic static ino_t
34551469Sbostic ffs_dirpref(fs)
3465965Smckusic 	register struct fs *fs;
3474359Smckusick {
3484651Smckusic 	int cg, minndir, mincg, avgifree;
3494359Smckusick 
3504792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3514651Smckusic 	minndir = fs->fs_ipg;
3524359Smckusick 	mincg = 0;
3534651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3545322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3555322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3564359Smckusick 			mincg = cg;
3575322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3584359Smckusick 		}
3599163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3604359Smckusick }
3614359Smckusick 
3624651Smckusic /*
3639163Ssam  * Select the desired position for the next block in a file.  The file is
3649163Ssam  * logically divided into sections. The first section is composed of the
3659163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3669163Ssam  *
3679163Ssam  * If no blocks have been allocated in the first section, the policy is to
3689163Ssam  * request a block in the same cylinder group as the inode that describes
3699163Ssam  * the file. If no blocks have been allocated in any other section, the
3709163Ssam  * policy is to place the section in a cylinder group with a greater than
3719163Ssam  * average number of free blocks.  An appropriate cylinder group is found
37217696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
37317696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
37417696Smckusick  * cylinder group from which the previous allocation was made. The sweep
37517696Smckusick  * continues until a cylinder group with greater than the average number
37617696Smckusick  * of free blocks is found. If the allocation is for the first block in an
37717696Smckusick  * indirect block, the information on the previous allocation is unavailable;
37817696Smckusick  * here a best guess is made based upon the logical block number being
37917696Smckusick  * allocated.
3809163Ssam  *
3819163Ssam  * If a section is already partially allocated, the policy is to
3829163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3839163Ssam  * contiguous blocks and the beginning of the next is physically separated
3849163Ssam  * so that the disk head will be in transit between them for at least
3859163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3869163Ssam  * schedule another I/O transfer.
3874651Smckusic  */
3885212Smckusic daddr_t
38951469Sbostic ffs_blkpref(ip, lbn, indx, bap)
3909163Ssam 	struct inode *ip;
3919163Ssam 	daddr_t lbn;
3929163Ssam 	int indx;
3939163Ssam 	daddr_t *bap;
3949163Ssam {
3955965Smckusic 	register struct fs *fs;
39617696Smckusick 	register int cg;
39717696Smckusick 	int avgbfree, startcg;
3989163Ssam 	daddr_t nextblk;
3994651Smckusic 
4009163Ssam 	fs = ip->i_fs;
4019163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
4029163Ssam 		if (lbn < NDADDR) {
4039163Ssam 			cg = itog(fs, ip->i_number);
4045322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
4054651Smckusic 		}
4069163Ssam 		/*
4079163Ssam 		 * Find a cylinder with greater than average number of
4089163Ssam 		 * unused data blocks.
4099163Ssam 		 */
41017696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
41117696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
41217696Smckusick 		else
41317696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
41417696Smckusick 		startcg %= fs->fs_ncg;
4159163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
41617696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
4179163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4189163Ssam 				fs->fs_cgrotor = cg;
4199163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4209163Ssam 			}
42117696Smckusick 		for (cg = 0; cg <= startcg; 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 			}
4269163Ssam 		return (NULL);
4279163Ssam 	}
4289163Ssam 	/*
4299163Ssam 	 * One or more previous blocks have been laid out. If less
4309163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
4319163Ssam 	 * next block is requested contiguously, otherwise it is
4329163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
4339163Ssam 	 */
4349163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
4359163Ssam 	if (indx > fs->fs_maxcontig &&
43611638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4379163Ssam 	    != nextblk)
4389163Ssam 		return (nextblk);
4399163Ssam 	if (fs->fs_rotdelay != 0)
4409163Ssam 		/*
4419163Ssam 		 * Here we convert ms of delay to frags as:
4429163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4439163Ssam 		 *	((sect/frag) * (ms/sec))
4449163Ssam 		 * then round up to the next block.
4459163Ssam 		 */
4469163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4479163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4489163Ssam 	return (nextblk);
4494651Smckusic }
4504651Smckusic 
4515375Smckusic /*
4525375Smckusic  * Implement the cylinder overflow algorithm.
4535375Smckusic  *
4545375Smckusic  * The policy implemented by this algorithm is:
4555375Smckusic  *   1) allocate the block in its requested cylinder group.
4565375Smckusic  *   2) quadradically rehash on the cylinder group number.
4575375Smckusic  *   3) brute force search for a free block.
4585375Smckusic  */
4595212Smckusic /*VARARGS5*/
46051469Sbostic static u_long
46151469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator)
4625965Smckusic 	struct inode *ip;
4634359Smckusick 	int cg;
4644359Smckusick 	long pref;
4654359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4665212Smckusic 	u_long (*allocator)();
4674359Smckusick {
4685965Smckusic 	register struct fs *fs;
4694359Smckusick 	long result;
4704359Smckusick 	int i, icg = cg;
4714359Smckusick 
4725965Smckusic 	fs = ip->i_fs;
4734359Smckusick 	/*
4744359Smckusick 	 * 1: preferred cylinder group
4754359Smckusick 	 */
4765965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4774359Smckusick 	if (result)
4784359Smckusick 		return (result);
4794359Smckusick 	/*
4804359Smckusick 	 * 2: quadratic rehash
4814359Smckusick 	 */
4824359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4834359Smckusick 		cg += i;
4844359Smckusick 		if (cg >= fs->fs_ncg)
4854359Smckusick 			cg -= fs->fs_ncg;
4865965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4874359Smckusick 		if (result)
4884359Smckusick 			return (result);
4894359Smckusick 	}
4904359Smckusick 	/*
4914359Smckusick 	 * 3: brute force search
49210847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
49310847Ssam 	 * and 1 is always checked in the quadratic rehash.
4944359Smckusick 	 */
49510848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
49610847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4975965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4984359Smckusick 		if (result)
4994359Smckusick 			return (result);
5004359Smckusick 		cg++;
5014359Smckusick 		if (cg == fs->fs_ncg)
5024359Smckusick 			cg = 0;
5034359Smckusick 	}
5046294Smckusick 	return (NULL);
5054359Smckusick }
5064359Smckusick 
5075375Smckusic /*
5085375Smckusic  * Determine whether a fragment can be extended.
5095375Smckusic  *
5105375Smckusic  * Check to see if the necessary fragments are available, and
5115375Smckusic  * if they are, allocate them.
5125375Smckusic  */
51351469Sbostic static daddr_t
51451469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize)
5155965Smckusic 	struct inode *ip;
5164426Smckusic 	int cg;
5174463Smckusic 	long bprev;
5184426Smckusic 	int osize, nsize;
5194426Smckusic {
5205965Smckusic 	register struct fs *fs;
5214463Smckusic 	register struct cg *cgp;
52237735Smckusick 	struct buf *bp;
5234463Smckusic 	long bno;
5244463Smckusic 	int frags, bbase;
52537735Smckusick 	int i, error;
5264426Smckusic 
5275965Smckusic 	fs = ip->i_fs;
52817224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
5296531Smckusick 		return (NULL);
5305960Smckusic 	frags = numfrags(fs, nsize);
53117224Smckusick 	bbase = fragnum(fs, bprev);
53217224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
53330749Skarels 		/* cannot extend across a block boundary */
5346294Smckusick 		return (NULL);
5354463Smckusic 	}
53637735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
53738776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
53837735Smckusick 	if (error) {
53937735Smckusick 		brelse(bp);
54037735Smckusick 		return (NULL);
54137735Smckusick 	}
5426531Smckusick 	cgp = bp->b_un.b_cg;
54337735Smckusick 	if (!cg_chkmagic(cgp)) {
5445960Smckusic 		brelse(bp);
5456294Smckusick 		return (NULL);
5465960Smckusic 	}
5478105Sroot 	cgp->cg_time = time.tv_sec;
5485377Smckusic 	bno = dtogd(fs, bprev);
5495960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
55034143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
5515361Smckusic 			brelse(bp);
5526294Smckusick 			return (NULL);
5535361Smckusic 		}
5545361Smckusic 	/*
5555361Smckusic 	 * the current fragment can be extended
5565361Smckusic 	 * deduct the count on fragment being extended into
5575361Smckusic 	 * increase the count on the remaining fragment (if any)
5585361Smckusic 	 * allocate the extended piece
5595361Smckusic 	 */
5605361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
56134143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
5624463Smckusic 			break;
5635960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5645361Smckusic 	if (i != frags)
5655361Smckusic 		cgp->cg_frsum[i - frags]++;
5665960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
56734143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
5685361Smckusic 		cgp->cg_cs.cs_nffree--;
5695361Smckusic 		fs->fs_cstotal.cs_nffree--;
5705361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5714463Smckusic 	}
57250893Smckusick 	fs->fs_fmod = 1;
5735361Smckusic 	bdwrite(bp);
5745361Smckusic 	return (bprev);
5754426Smckusic }
5764426Smckusic 
5775375Smckusic /*
5785375Smckusic  * Determine whether a block can be allocated.
5795375Smckusic  *
5805375Smckusic  * Check to see if a block of the apprpriate size is available,
5815375Smckusic  * and if it is, allocate it.
5825375Smckusic  */
58351779Smarc static daddr_t
58451469Sbostic ffs_alloccg(ip, cg, bpref, size)
5855965Smckusic 	struct inode *ip;
5864359Smckusick 	int cg;
5874359Smckusick 	daddr_t bpref;
5884359Smckusick 	int size;
5894359Smckusick {
5905965Smckusic 	register struct fs *fs;
5914463Smckusic 	register struct cg *cgp;
59237735Smckusick 	struct buf *bp;
5934463Smckusic 	register int i;
59437735Smckusick 	int error, bno, frags, allocsiz;
5954359Smckusick 
5965965Smckusic 	fs = ip->i_fs;
5975322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5986294Smckusick 		return (NULL);
59937735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
60038776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
60137735Smckusick 	if (error) {
60237735Smckusick 		brelse(bp);
60337735Smckusick 		return (NULL);
60437735Smckusick 	}
6056531Smckusick 	cgp = bp->b_un.b_cg;
60637735Smckusick 	if (!cg_chkmagic(cgp) ||
60715950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
6085960Smckusic 		brelse(bp);
6096294Smckusick 		return (NULL);
6105960Smckusic 	}
6118105Sroot 	cgp->cg_time = time.tv_sec;
6125322Smckusic 	if (size == fs->fs_bsize) {
61351469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
6144463Smckusic 		bdwrite(bp);
6154463Smckusic 		return (bno);
6164463Smckusic 	}
6174463Smckusic 	/*
6184463Smckusic 	 * check to see if any fragments are already available
6194463Smckusic 	 * allocsiz is the size which will be allocated, hacking
6204463Smckusic 	 * it down to a smaller size if necessary
6214463Smckusic 	 */
6225960Smckusic 	frags = numfrags(fs, size);
6235322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
6244463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
6254463Smckusic 			break;
6265322Smckusic 	if (allocsiz == fs->fs_frag) {
6274463Smckusic 		/*
6284463Smckusic 		 * no fragments were available, so a block will be
6294463Smckusic 		 * allocated, and hacked up
6304463Smckusic 		 */
6314792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
6324463Smckusic 			brelse(bp);
6336294Smckusick 			return (NULL);
6344463Smckusic 		}
63551469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
6365377Smckusic 		bpref = dtogd(fs, bno);
6375322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
63834143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
6395322Smckusic 		i = fs->fs_frag - frags;
6404792Smckusic 		cgp->cg_cs.cs_nffree += i;
6414792Smckusic 		fs->fs_cstotal.cs_nffree += i;
6425322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
64350893Smckusick 		fs->fs_fmod = 1;
6444463Smckusic 		cgp->cg_frsum[i]++;
6454463Smckusic 		bdwrite(bp);
6464463Smckusic 		return (bno);
6474463Smckusic 	}
64851469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
64915950Smckusick 	if (bno < 0) {
65015950Smckusick 		brelse(bp);
6516294Smckusick 		return (NULL);
65215950Smckusick 	}
6534463Smckusic 	for (i = 0; i < frags; i++)
65434143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
6554792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6564792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6575322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
65850893Smckusick 	fs->fs_fmod = 1;
6594463Smckusic 	cgp->cg_frsum[allocsiz]--;
6604463Smckusic 	if (frags != allocsiz)
6614463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6624463Smckusic 	bdwrite(bp);
6634463Smckusic 	return (cg * fs->fs_fpg + bno);
6644463Smckusic }
6654463Smckusic 
6665375Smckusic /*
6675375Smckusic  * Allocate a block in a cylinder group.
6685375Smckusic  *
6695375Smckusic  * This algorithm implements the following policy:
6705375Smckusic  *   1) allocate the requested block.
6715375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6725375Smckusic  *   3) allocate the next available block on the block rotor for the
6735375Smckusic  *      specified cylinder group.
6745375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6755375Smckusic  * blocks may be fragmented by the routine that allocates them.
6765375Smckusic  */
67751469Sbostic static daddr_t
67851469Sbostic ffs_alloccgblk(fs, cgp, bpref)
6795965Smckusic 	register struct fs *fs;
6804463Smckusic 	register struct cg *cgp;
6814463Smckusic 	daddr_t bpref;
6824463Smckusic {
6834651Smckusic 	daddr_t bno;
6846294Smckusick 	int cylno, pos, delta;
6854651Smckusic 	short *cylbp;
6865361Smckusic 	register int i;
6874463Smckusic 
6884651Smckusic 	if (bpref == 0) {
6894651Smckusic 		bpref = cgp->cg_rotor;
6905361Smckusic 		goto norot;
6915361Smckusic 	}
69217224Smckusick 	bpref = blknum(fs, bpref);
6935377Smckusic 	bpref = dtogd(fs, bpref);
6945361Smckusic 	/*
6955361Smckusic 	 * if the requested block is available, use it
6965361Smckusic 	 */
69751469Sbostic 	if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
6985361Smckusic 		bno = bpref;
6995361Smckusic 		goto gotit;
7005361Smckusic 	}
7015361Smckusic 	/*
7025361Smckusic 	 * check for a block available on the same cylinder
7035361Smckusic 	 */
7045361Smckusic 	cylno = cbtocylno(fs, bpref);
70534143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
7065375Smckusic 		goto norot;
7075375Smckusic 	if (fs->fs_cpc == 0) {
7085375Smckusic 		/*
7095375Smckusic 		 * block layout info is not available, so just have
7105375Smckusic 		 * to take any block in this cylinder.
7115375Smckusic 		 */
7125375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
7135375Smckusic 		goto norot;
7145375Smckusic 	}
7155375Smckusic 	/*
7165361Smckusic 	 * check the summary information to see if a block is
7175361Smckusic 	 * available in the requested cylinder starting at the
7189163Ssam 	 * requested rotational position and proceeding around.
7195361Smckusic 	 */
72034143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
7219163Ssam 	pos = cbtorpos(fs, bpref);
72234143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
7235361Smckusic 		if (cylbp[i] > 0)
7245361Smckusic 			break;
72534143Smckusick 	if (i == fs->fs_nrpos)
7265361Smckusic 		for (i = 0; i < pos; i++)
7275361Smckusic 			if (cylbp[i] > 0)
7285361Smckusic 				break;
7295361Smckusic 	if (cylbp[i] > 0) {
7304651Smckusic 		/*
7315361Smckusic 		 * found a rotational position, now find the actual
7325361Smckusic 		 * block. A panic if none is actually there.
7334651Smckusic 		 */
7345361Smckusic 		pos = cylno % fs->fs_cpc;
7355361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
73634143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
7376716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
7386716Smckusick 			    pos, i, fs->fs_fsmnt);
73951469Sbostic 			panic("ffs_alloccgblk: cyl groups corrupted");
7406716Smckusick 		}
74134143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
74251469Sbostic 			if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
74311638Ssam 				bno = blkstofrags(fs, (bno + i));
7445361Smckusic 				goto gotit;
7455361Smckusic 			}
74634143Smckusick 			delta = fs_rotbl(fs)[i];
74734143Smckusick 			if (delta <= 0 ||
74834143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
7494651Smckusic 				break;
7506294Smckusick 			i += delta;
7514651Smckusic 		}
7526716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
75351469Sbostic 		panic("ffs_alloccgblk: can't find blk in cyl");
7544359Smckusick 	}
7555361Smckusic norot:
7565361Smckusic 	/*
7575361Smckusic 	 * no blocks in the requested cylinder, so take next
7585361Smckusic 	 * available one in this cylinder group.
7595361Smckusic 	 */
76051469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7616567Smckusic 	if (bno < 0)
7626294Smckusick 		return (NULL);
7634651Smckusic 	cgp->cg_rotor = bno;
7644359Smckusick gotit:
76551469Sbostic 	ffs_clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno));
7664792Smckusic 	cgp->cg_cs.cs_nbfree--;
7674792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7685322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7695375Smckusic 	cylno = cbtocylno(fs, bno);
77034143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
77134143Smckusick 	cg_blktot(cgp)[cylno]--;
77250893Smckusick 	fs->fs_fmod = 1;
7734651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7744359Smckusick }
77534143Smckusick 
7765375Smckusic /*
7775375Smckusic  * Determine whether an inode can be allocated.
7785375Smckusic  *
7795375Smckusic  * Check to see if an inode is available, and if it is,
7805375Smckusic  * allocate it using the following policy:
7815375Smckusic  *   1) allocate the requested inode.
7825375Smckusic  *   2) allocate the next available inode after the requested
7835375Smckusic  *      inode in the specified cylinder group.
7845375Smckusic  */
78551469Sbostic static ino_t
78651469Sbostic ffs_ialloccg(ip, cg, ipref, mode)
7875965Smckusic 	struct inode *ip;
7884359Smckusick 	int cg;
7894359Smckusick 	daddr_t ipref;
7904359Smckusick 	int mode;
7914359Smckusick {
7925965Smckusic 	register struct fs *fs;
7934463Smckusic 	register struct cg *cgp;
79416784Smckusick 	struct buf *bp;
79537735Smckusick 	int error, start, len, loc, map, i;
7964359Smckusick 
7975965Smckusic 	fs = ip->i_fs;
7985322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7996294Smckusick 		return (NULL);
80037735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
80138776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
80237735Smckusick 	if (error) {
80337735Smckusick 		brelse(bp);
80437735Smckusick 		return (NULL);
80537735Smckusick 	}
8066531Smckusick 	cgp = bp->b_un.b_cg;
80737735Smckusick 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
8085960Smckusic 		brelse(bp);
8096294Smckusick 		return (NULL);
8105960Smckusic 	}
8118105Sroot 	cgp->cg_time = time.tv_sec;
8124359Smckusick 	if (ipref) {
8134359Smckusick 		ipref %= fs->fs_ipg;
81434143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
8154359Smckusick 			goto gotit;
81616784Smckusick 	}
81716784Smckusick 	start = cgp->cg_irotor / NBBY;
81816784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
81934143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
82016784Smckusick 	if (loc == 0) {
82117697Smckusick 		len = start + 1;
82217697Smckusick 		start = 0;
82334143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
82417697Smckusick 		if (loc == 0) {
82517697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
82617697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
82751469Sbostic 			panic("ffs_ialloccg: map corrupted");
82817697Smckusick 			/* NOTREACHED */
82917697Smckusick 		}
83016784Smckusick 	}
83116784Smckusick 	i = start + len - loc;
83234143Smckusick 	map = cg_inosused(cgp)[i];
83316784Smckusick 	ipref = i * NBBY;
83416784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
83516784Smckusick 		if ((map & i) == 0) {
8364359Smckusick 			cgp->cg_irotor = ipref;
8374359Smckusick 			goto gotit;
8384359Smckusick 		}
8394359Smckusick 	}
84016784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
84151469Sbostic 	panic("ffs_ialloccg: block not in map");
84216784Smckusick 	/* NOTREACHED */
8434359Smckusick gotit:
84434143Smckusick 	setbit(cg_inosused(cgp), ipref);
8454792Smckusic 	cgp->cg_cs.cs_nifree--;
8464792Smckusic 	fs->fs_cstotal.cs_nifree--;
8475322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
84850893Smckusick 	fs->fs_fmod = 1;
8494359Smckusick 	if ((mode & IFMT) == IFDIR) {
8504792Smckusic 		cgp->cg_cs.cs_ndir++;
8514792Smckusic 		fs->fs_cstotal.cs_ndir++;
8525322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8534359Smckusick 	}
8544359Smckusick 	bdwrite(bp);
8554359Smckusick 	return (cg * fs->fs_ipg + ipref);
8564359Smckusick }
8574359Smckusick 
8585375Smckusic /*
8595375Smckusic  * Free a block or fragment.
8605375Smckusic  *
8615375Smckusic  * The specified block or fragment is placed back in the
8625375Smckusic  * free map. If a fragment is deallocated, a possible
8635375Smckusic  * block reassembly is checked.
8645375Smckusic  */
86551469Sbostic ffs_blkfree(ip, bno, size)
8665965Smckusic 	register struct inode *ip;
8674359Smckusick 	daddr_t bno;
86853059Smckusick 	long size;
8694359Smckusick {
8704359Smckusick 	register struct fs *fs;
8714359Smckusick 	register struct cg *cgp;
87237735Smckusick 	struct buf *bp;
87337735Smckusick 	int error, cg, blk, frags, bbase;
8744463Smckusic 	register int i;
8754359Smckusick 
8765965Smckusic 	fs = ip->i_fs;
8776716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8786716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8796716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
88031402Smckusick 		panic("blkfree: bad size");
8816716Smckusick 	}
8825377Smckusic 	cg = dtog(fs, bno);
88342318Smckusick 	if ((unsigned)bno >= fs->fs_size) {
8846567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
88553244Smckusick 		ffs_fserr(fs, ip->i_uid, "bad block");
8864359Smckusick 		return;
8876567Smckusic 	}
88837735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
88938776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
89037735Smckusick 	if (error) {
89137735Smckusick 		brelse(bp);
89237735Smckusick 		return;
89337735Smckusick 	}
8946531Smckusick 	cgp = bp->b_un.b_cg;
89537735Smckusick 	if (!cg_chkmagic(cgp)) {
8965960Smckusic 		brelse(bp);
8974359Smckusick 		return;
8985960Smckusic 	}
8998105Sroot 	cgp->cg_time = time.tv_sec;
9005377Smckusic 	bno = dtogd(fs, bno);
9015322Smckusic 	if (size == fs->fs_bsize) {
90251469Sbostic 		if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) {
9036716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
9046716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
90531402Smckusick 			panic("blkfree: freeing free block");
9066567Smckusic 		}
90751469Sbostic 		ffs_setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
9084792Smckusic 		cgp->cg_cs.cs_nbfree++;
9094792Smckusic 		fs->fs_cstotal.cs_nbfree++;
9105322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
9115375Smckusic 		i = cbtocylno(fs, bno);
91234143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
91334143Smckusick 		cg_blktot(cgp)[i]++;
9144426Smckusic 	} else {
91517224Smckusick 		bbase = bno - fragnum(fs, bno);
9164463Smckusic 		/*
9174463Smckusic 		 * decrement the counts associated with the old frags
9184463Smckusic 		 */
91934143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
92051469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
9214463Smckusic 		/*
9224463Smckusic 		 * deallocate the fragment
9234463Smckusic 		 */
9245960Smckusic 		frags = numfrags(fs, size);
9254463Smckusic 		for (i = 0; i < frags; i++) {
92634143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
9276716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
9286716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
92931402Smckusick 				panic("blkfree: freeing free frag");
9306716Smckusick 			}
93134143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
9324426Smckusic 		}
9336294Smckusick 		cgp->cg_cs.cs_nffree += i;
9346294Smckusick 		fs->fs_cstotal.cs_nffree += i;
9356294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
9364463Smckusic 		/*
9374463Smckusic 		 * add back in counts associated with the new frags
9384463Smckusic 		 */
93934143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
94051469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
9414463Smckusic 		/*
9424463Smckusic 		 * if a complete block has been reassembled, account for it
9434463Smckusic 		 */
94451469Sbostic 		if (ffs_isblock(fs, cg_blksfree(cgp),
94534476Smckusick 		    (daddr_t)fragstoblks(fs, bbase))) {
9465322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
9475322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
9485322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
9494792Smckusic 			cgp->cg_cs.cs_nbfree++;
9504792Smckusic 			fs->fs_cstotal.cs_nbfree++;
9515322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
9525375Smckusic 			i = cbtocylno(fs, bbase);
95334143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
95434143Smckusick 			cg_blktot(cgp)[i]++;
9554426Smckusic 		}
9564426Smckusic 	}
95750893Smckusick 	fs->fs_fmod = 1;
9584359Smckusick 	bdwrite(bp);
9594359Smckusick }
9604359Smckusick 
9615375Smckusic /*
9625375Smckusic  * Free an inode.
9635375Smckusic  *
9645375Smckusic  * The specified inode is placed back in the free map.
9655375Smckusic  */
96653577Sheideman int
96753519Sheideman ffs_vfree (ap)
96853519Sheideman 	struct vop_vfree_args *ap;
9694359Smckusick {
9704359Smckusick 	register struct fs *fs;
9714359Smckusick 	register struct cg *cgp;
97251541Smckusick 	register struct inode *pip;
97337735Smckusick 	struct buf *bp;
97437735Smckusick 	int error, cg;
9754359Smckusick 
97653583Sheideman 	pip = VTOI(ap->a_pvp);
97751469Sbostic 	fs = pip->i_fs;
97853583Sheideman 	if ((u_int)ap->a_ino >= fs->fs_ipg * fs->fs_ncg)
97953583Sheideman 		panic("ifree: range: dev = 0x%x, ap->a_ino = %d, fs = %s\n",
98053583Sheideman 		    pip->i_dev, ap->a_ino, fs->fs_fsmnt);
98153583Sheideman 	cg = itog(fs, ap->a_ino);
98251469Sbostic 	error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
98338776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
98437735Smckusick 	if (error) {
98537735Smckusick 		brelse(bp);
98653577Sheideman 		return (0);
98737735Smckusick 	}
9886531Smckusick 	cgp = bp->b_un.b_cg;
98937735Smckusick 	if (!cg_chkmagic(cgp)) {
9905960Smckusic 		brelse(bp);
99153577Sheideman 		return (0);
9925960Smckusic 	}
9938105Sroot 	cgp->cg_time = time.tv_sec;
99453583Sheideman 	ap->a_ino %= fs->fs_ipg;
99553583Sheideman 	if (isclr(cg_inosused(cgp), ap->a_ino)) {
99653583Sheideman 		printf("dev = 0x%x, ap->a_ino = %d, fs = %s\n",
99753583Sheideman 		    pip->i_dev, ap->a_ino, fs->fs_fsmnt);
99848057Smckusick 		if (fs->fs_ronly == 0)
99948057Smckusick 			panic("ifree: freeing free inode");
10006716Smckusick 	}
100153583Sheideman 	clrbit(cg_inosused(cgp), ap->a_ino);
100253583Sheideman 	if (ap->a_ino < cgp->cg_irotor)
100353583Sheideman 		cgp->cg_irotor = ap->a_ino;
10044792Smckusic 	cgp->cg_cs.cs_nifree++;
10054792Smckusic 	fs->fs_cstotal.cs_nifree++;
10065322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
100753583Sheideman 	if ((ap->a_mode & IFMT) == IFDIR) {
10084792Smckusic 		cgp->cg_cs.cs_ndir--;
10094792Smckusic 		fs->fs_cstotal.cs_ndir--;
10105322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
10114359Smckusick 	}
101250893Smckusick 	fs->fs_fmod = 1;
10134359Smckusick 	bdwrite(bp);
101453577Sheideman 	return (0);
10154359Smckusick }
10164359Smckusick 
10174463Smckusic /*
10185375Smckusic  * Find a block of the specified size in the specified cylinder group.
10195375Smckusic  *
10204651Smckusic  * It is a panic if a request is made to find a block if none are
10214651Smckusic  * available.
10224651Smckusic  */
102351469Sbostic static daddr_t
102451469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz)
10254651Smckusic 	register struct fs *fs;
10264651Smckusic 	register struct cg *cgp;
10274651Smckusic 	daddr_t bpref;
10284651Smckusic 	int allocsiz;
10294651Smckusic {
10304651Smckusic 	daddr_t bno;
10314651Smckusic 	int start, len, loc, i;
10324651Smckusic 	int blk, field, subfield, pos;
10334651Smckusic 
10344651Smckusic 	/*
10354651Smckusic 	 * find the fragment by searching through the free block
10364651Smckusic 	 * map for an appropriate bit pattern
10374651Smckusic 	 */
10384651Smckusic 	if (bpref)
10395377Smckusic 		start = dtogd(fs, bpref) / NBBY;
10404651Smckusic 	else
10414651Smckusic 		start = cgp->cg_frotor / NBBY;
10425398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
104334476Smckusick 	loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start],
104434476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
104534476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
10464651Smckusic 	if (loc == 0) {
10476531Smckusick 		len = start + 1;
10486531Smckusick 		start = 0;
104934476Smckusick 		loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0],
105034476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
105134476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
105216784Smckusick 		if (loc == 0) {
105316784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
105416784Smckusick 			    start, len, fs->fs_fsmnt);
105551469Sbostic 			panic("ffs_alloccg: map corrupted");
105617697Smckusick 			/* NOTREACHED */
105716784Smckusick 		}
10584651Smckusic 	}
10594651Smckusic 	bno = (start + len - loc) * NBBY;
10604651Smckusic 	cgp->cg_frotor = bno;
10614651Smckusic 	/*
10624651Smckusic 	 * found the byte in the map
10634651Smckusic 	 * sift through the bits to find the selected frag
10644651Smckusic 	 */
10656294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
106634143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
10674651Smckusic 		blk <<= 1;
10684651Smckusic 		field = around[allocsiz];
10694651Smckusic 		subfield = inside[allocsiz];
10705322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10716294Smckusick 			if ((blk & field) == subfield)
10726294Smckusick 				return (bno + pos);
10734651Smckusic 			field <<= 1;
10744651Smckusic 			subfield <<= 1;
10754651Smckusic 		}
10764651Smckusic 	}
10776716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
107851469Sbostic 	panic("ffs_alloccg: block not in map");
10796531Smckusick 	return (-1);
10804651Smckusic }
10814651Smckusic 
10824651Smckusic /*
10835375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10845375Smckusic  *
10855375Smckusic  * The form of the error message is:
10864359Smckusick  *	fs: error message
10874359Smckusick  */
108851469Sbostic static void
108951469Sbostic ffs_fserr(fs, uid, cp)
10904359Smckusick 	struct fs *fs;
109151469Sbostic 	u_int uid;
10924359Smckusick 	char *cp;
10934359Smckusick {
10944359Smckusick 
109542318Smckusick 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
10964359Smckusick }
1097