xref: /csrg-svn/sys/ufs/ffs/ffs_alloc.c (revision 51541)
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*51541Smckusick  *	@(#)ffs_alloc.c	7.29 (Berkeley) 11/05/91
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 
1851469Sbostic #include <ufs/ufs/quota.h>
1951469Sbostic #include <ufs/ufs/inode.h>
2047571Skarels 
2151469Sbostic #include <ufs/ffs/fs.h>
2251469Sbostic #include <ufs/ffs/ffs_extern.h>
234359Smckusick 
2451469Sbostic extern u_long nextgennumber;
2551469Sbostic 
2651469Sbostic static daddr_t	ffs_alloccg __P((struct inode *, int, daddr_t, int));
2751469Sbostic static daddr_t	ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t));
2851469Sbostic static ino_t	ffs_dirpref __P((struct fs *));
2951469Sbostic static daddr_t	ffs_fragextend __P((struct inode *, int, long, int, int));
3051469Sbostic static void	ffs_fserr __P((struct fs *, u_int, char *));
3151469Sbostic static u_long	ffs_hashalloc
3251469Sbostic 		    __P((struct inode *, int, long, int, u_long (*)()));
3351469Sbostic static ino_t	ffs_ialloccg __P((struct inode *, int, daddr_t, int));
3451469Sbostic static daddr_t	ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int));
3551469Sbostic 
365375Smckusic /*
375375Smckusic  * Allocate a block in the file system.
385375Smckusic  *
395375Smckusic  * The size of the requested block is given, which must be some
405375Smckusic  * multiple of fs_fsize and <= fs_bsize.
415375Smckusic  * A preference may be optionally specified. If a preference is given
425375Smckusic  * the following hierarchy is used to allocate a block:
435375Smckusic  *   1) allocate the requested block.
445375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
455375Smckusic  *   3) allocate a block in the same cylinder group.
465375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
475375Smckusic  *      available block is located.
485375Smckusic  * If no block preference is given the following heirarchy is used
495375Smckusic  * to allocate a block:
505375Smckusic  *   1) allocate a block in the cylinder group that contains the
515375Smckusic  *      inode for the file.
525375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
535375Smckusic  *      available block is located.
545375Smckusic  */
5551469Sbostic ffs_alloc(ip, lbn, bpref, size, bnp)
564463Smckusic 	register struct inode *ip;
5739678Smckusick 	daddr_t lbn, bpref;
584359Smckusick 	int size;
5939678Smckusick 	daddr_t *bnp;
604359Smckusick {
614359Smckusick 	daddr_t bno;
624359Smckusick 	register struct fs *fs;
634463Smckusic 	register struct buf *bp;
6437735Smckusick 	int cg, error;
6547571Skarels 	struct ucred *cred = curproc->p_ucred;		/* XXX */
664359Smckusick 
6739678Smckusick 	*bnp = 0;
685965Smckusic 	fs = ip->i_fs;
696716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
706716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
716716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7251469Sbostic 		panic("ffs_alloc: bad size");
736716Smckusick 	}
745322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
754359Smckusick 		goto nospace;
7641309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
774792Smckusic 		goto nospace;
787650Ssam #ifdef QUOTA
7941309Smckusick 	if (error = chkdq(ip, (long)btodb(size), cred, 0))
8037735Smckusick 		return (error);
817483Skre #endif
824948Smckusic 	if (bpref >= fs->fs_size)
834948Smckusic 		bpref = 0;
844359Smckusick 	if (bpref == 0)
855377Smckusic 		cg = itog(fs, ip->i_number);
864359Smckusick 	else
875377Smckusic 		cg = dtog(fs, bpref);
8851469Sbostic 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
8951469Sbostic 	    (u_long (*)())ffs_alloccg);
9039678Smckusick 	if (bno > 0) {
9139678Smckusick 		ip->i_blocks += btodb(size);
9239678Smckusick 		ip->i_flag |= IUPD|ICHG;
9339678Smckusick 		*bnp = bno;
9439678Smckusick 		return (0);
9539678Smckusick 	}
9645173Smckusick #ifdef QUOTA
9745173Smckusick 	/*
9845173Smckusick 	 * Restore user's disk quota because allocation failed.
9945173Smckusick 	 */
10045173Smckusick 	(void) chkdq(ip, (long)-btodb(size), cred, FORCE);
10145173Smckusick #endif
1024359Smckusick nospace:
10351469Sbostic 	ffs_fserr(fs, cred->cr_uid, "file system full");
1044359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
10537735Smckusick 	return (ENOSPC);
1064359Smckusick }
1074359Smckusick 
1085375Smckusic /*
1095375Smckusic  * Reallocate a fragment to a bigger size
1105375Smckusic  *
1115375Smckusic  * The number and size of the old block is given, and a preference
1125375Smckusic  * and new size is also specified. The allocator attempts to extend
1135375Smckusic  * the original block. Failing that, the regular block allocator is
1145375Smckusic  * invoked to get an appropriate block.
1155375Smckusic  */
11651469Sbostic ffs_realloccg(ip, lbprev, bpref, osize, nsize, bpp)
1175965Smckusic 	register struct inode *ip;
11839678Smckusick 	off_t lbprev;
11939678Smckusick 	daddr_t bpref;
1204426Smckusic 	int osize, nsize;
12137735Smckusick 	struct buf **bpp;
1224426Smckusic {
1234426Smckusic 	register struct fs *fs;
12437735Smckusick 	struct buf *bp, *obp;
12545719Smckusick 	int cg, request, error;
12645719Smckusick 	daddr_t bprev, bno;
12747571Skarels 	struct ucred *cred = curproc->p_ucred;		/* XXX */
1284426Smckusic 
12937735Smckusick 	*bpp = 0;
1305965Smckusic 	fs = ip->i_fs;
1315960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1326716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
13351469Sbostic 		printf(
13451469Sbostic 		    "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1356716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
13651469Sbostic 		panic("ffs_realloccg: bad size");
1376716Smckusick 	}
13841309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1394792Smckusic 		goto nospace;
14039678Smckusick 	if ((bprev = ip->i_db[lbprev]) == 0) {
1416716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1426716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
14351469Sbostic 		panic("ffs_realloccg: bad bprev");
1446716Smckusick 	}
14539678Smckusick 	/*
14639678Smckusick 	 * Allocate the extra space in the buffer.
14739678Smckusick 	 */
14839678Smckusick 	if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
14939678Smckusick 		brelse(bp);
15039678Smckusick 		return (error);
15139678Smckusick 	}
15245173Smckusick #ifdef QUOTA
15345173Smckusick 	if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) {
15445173Smckusick 		brelse(bp);
15545173Smckusick 		return (error);
15645173Smckusick 	}
15745173Smckusick #endif
15839678Smckusick 	/*
15939678Smckusick 	 * Check for extension in the existing location.
16039678Smckusick 	 */
1616294Smckusick 	cg = dtog(fs, bprev);
16251469Sbostic 	if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) {
16339887Smckusick 		if (bp->b_blkno != fsbtodb(fs, bno))
16439678Smckusick 			panic("bad blockno");
16539762Smckusick 		ip->i_blocks += btodb(nsize - osize);
16639762Smckusick 		ip->i_flag |= IUPD|ICHG;
16748948Smckusick 		allocbuf(bp, nsize);
16848948Smckusick 		bp->b_flags |= B_DONE;
16948948Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
17037735Smckusick 		*bpp = bp;
17137735Smckusick 		return (0);
1724463Smckusic 	}
17339678Smckusick 	/*
17439678Smckusick 	 * Allocate a new disk location.
17539678Smckusick 	 */
1764948Smckusic 	if (bpref >= fs->fs_size)
1774948Smckusic 		bpref = 0;
17826253Skarels 	switch ((int)fs->fs_optim) {
17925256Smckusick 	case FS_OPTSPACE:
18025256Smckusick 		/*
18125256Smckusick 		 * Allocate an exact sized fragment. Although this makes
18225256Smckusick 		 * best use of space, we will waste time relocating it if
18325256Smckusick 		 * the file continues to grow. If the fragmentation is
18425256Smckusick 		 * less than half of the minimum free reserve, we choose
18525256Smckusick 		 * to begin optimizing for time.
18625256Smckusick 		 */
18724698Smckusick 		request = nsize;
18825256Smckusick 		if (fs->fs_minfree < 5 ||
18925256Smckusick 		    fs->fs_cstotal.cs_nffree >
19025256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
19125256Smckusick 			break;
19225256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
19325256Smckusick 			fs->fs_fsmnt);
19425256Smckusick 		fs->fs_optim = FS_OPTTIME;
19525256Smckusick 		break;
19625256Smckusick 	case FS_OPTTIME:
19725256Smckusick 		/*
19851469Sbostic 		 * At this point we have discovered a file that is trying to
19951469Sbostic 		 * grow a small fragment to a larger fragment. To save time,
20051469Sbostic 		 * we allocate a full sized block, then free the unused portion.
20151469Sbostic 		 * If the file continues to grow, the `ffs_fragextend' call
20251469Sbostic 		 * above will be able to grow it in place without further
20351469Sbostic 		 * copying. If aberrant programs cause disk fragmentation to
20451469Sbostic 		 * grow within 2% of the free reserve, we choose to begin
20551469Sbostic 		 * optimizing for space.
20625256Smckusick 		 */
20724698Smckusick 		request = fs->fs_bsize;
20825256Smckusick 		if (fs->fs_cstotal.cs_nffree <
20925256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
21025256Smckusick 			break;
21125256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
21225256Smckusick 			fs->fs_fsmnt);
21325256Smckusick 		fs->fs_optim = FS_OPTSPACE;
21425256Smckusick 		break;
21525256Smckusick 	default:
21625256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
21725256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
21851469Sbostic 		panic("ffs_realloccg: bad optim");
21925256Smckusick 		/* NOTREACHED */
22025256Smckusick 	}
22151469Sbostic 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
22251469Sbostic 	    (u_long (*)())ffs_alloccg);
2236567Smckusic 	if (bno > 0) {
22445719Smckusick 		bp->b_blkno = fsbtodb(fs, bno);
22545719Smckusick 		(void) vnode_pager_uncache(ITOV(ip));
22651469Sbostic 		ffs_blkfree(ip, bprev, (off_t)osize);
22724698Smckusick 		if (nsize < request)
22851469Sbostic 			ffs_blkfree(ip, bno + numfrags(fs, nsize),
22951469Sbostic 			    (off_t)(request - nsize));
23012643Ssam 		ip->i_blocks += btodb(nsize - osize);
23112643Ssam 		ip->i_flag |= IUPD|ICHG;
23248948Smckusick 		allocbuf(bp, nsize);
23348948Smckusick 		bp->b_flags |= B_DONE;
23448948Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
23537735Smckusick 		*bpp = bp;
23637735Smckusick 		return (0);
2374463Smckusic 	}
23845173Smckusick #ifdef QUOTA
23945173Smckusick 	/*
24045173Smckusick 	 * Restore user's disk quota because allocation failed.
24145173Smckusick 	 */
24245173Smckusick 	(void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
24345173Smckusick #endif
24439678Smckusick 	brelse(bp);
2454792Smckusic nospace:
2464463Smckusic 	/*
2474463Smckusic 	 * no space available
2484463Smckusic 	 */
24951469Sbostic 	ffs_fserr(fs, cred->cr_uid, "file system full");
2504426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
25137735Smckusick 	return (ENOSPC);
2524426Smckusic }
2534426Smckusic 
2545375Smckusic /*
2555375Smckusic  * Allocate an inode in the file system.
2565375Smckusic  *
25751469Sbostic  * If allocating a directory, use ffs_dirpref to select the inode.
25851469Sbostic  * If allocating in a directory, the following hierarchy is followed:
25951469Sbostic  *   1) allocate the preferred inode.
2605375Smckusic  *   2) allocate an inode in the same cylinder group.
2615375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2625375Smckusic  *      available inode is located.
2635375Smckusic  * If no inode preference is given the following heirarchy is used
2645375Smckusic  * to allocate an inode:
2655375Smckusic  *   1) allocate an inode in cylinder group 0.
2665375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2675375Smckusic  *      available inode is located.
2685375Smckusic  */
269*51541Smckusick ffs_valloc(pvp, mode, cred, vpp)
270*51541Smckusick 	register struct vnode *pvp;
2714359Smckusick 	int mode;
27241309Smckusick 	struct ucred *cred;
273*51541Smckusick 	struct vnode **vpp;
2744359Smckusick {
275*51541Smckusick 	register struct inode *pip;
2764359Smckusick 	register struct fs *fs;
2774359Smckusick 	register struct inode *ip;
27851469Sbostic 	ino_t ino, ipref;
27937735Smckusick 	int cg, error;
2804359Smckusick 
281*51541Smckusick 	*vpp = NULL;
282*51541Smckusick 	pip = VTOI(pvp);
2835965Smckusic 	fs = pip->i_fs;
2844792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2854359Smckusick 		goto noinodes;
28651469Sbostic 
28751469Sbostic 	if ((mode & IFMT) == IFDIR)
288*51541Smckusick 		ipref = ffs_dirpref(fs);
28951469Sbostic 	else
29051469Sbostic 		ipref = pip->i_number;
2914948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2924948Smckusic 		ipref = 0;
2935377Smckusic 	cg = itog(fs, ipref);
29451469Sbostic 	ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_ialloccg);
2954359Smckusick 	if (ino == 0)
2964359Smckusick 		goto noinodes;
297*51541Smckusick 	error = ffs_vget(pvp->v_mount, ino, vpp);
29837735Smckusick 	if (error) {
299*51541Smckusick 		ffs_vfree(pvp, ino, mode);
30037735Smckusick 		return (error);
3014359Smckusick 	}
302*51541Smckusick 	ip = VTOI(*vpp);
3036716Smckusick 	if (ip->i_mode) {
3046716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
3056716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
306*51541Smckusick 		panic("ffs_valloc: dup alloc");
3076716Smckusick 	}
30812643Ssam 	if (ip->i_blocks) {				/* XXX */
30912643Ssam 		printf("free inode %s/%d had %d blocks\n",
31012643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
31112643Ssam 		ip->i_blocks = 0;
31212643Ssam 	}
31339516Smckusick 	ip->i_flags = 0;
31438255Smckusick 	/*
31538255Smckusick 	 * Set up a new generation number for this inode.
31638255Smckusick 	 */
31738255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
31838255Smckusick 		nextgennumber = time.tv_sec;
31938255Smckusick 	ip->i_gen = nextgennumber;
32037735Smckusick 	return (0);
3214359Smckusick noinodes:
32251469Sbostic 	ffs_fserr(fs, cred->cr_uid, "out of inodes");
3236294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
32437735Smckusick 	return (ENOSPC);
3254359Smckusick }
3264359Smckusick 
3274651Smckusic /*
3285375Smckusic  * Find a cylinder to place a directory.
3295375Smckusic  *
3305375Smckusic  * The policy implemented by this algorithm is to select from
3315375Smckusic  * among those cylinder groups with above the average number of
3325375Smckusic  * free inodes, the one with the smallest number of directories.
3334651Smckusic  */
33451469Sbostic static ino_t
33551469Sbostic ffs_dirpref(fs)
3365965Smckusic 	register struct fs *fs;
3374359Smckusick {
3384651Smckusic 	int cg, minndir, mincg, avgifree;
3394359Smckusick 
3404792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3414651Smckusic 	minndir = fs->fs_ipg;
3424359Smckusick 	mincg = 0;
3434651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3445322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3455322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3464359Smckusick 			mincg = cg;
3475322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3484359Smckusick 		}
3499163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3504359Smckusick }
3514359Smckusick 
3524651Smckusic /*
3539163Ssam  * Select the desired position for the next block in a file.  The file is
3549163Ssam  * logically divided into sections. The first section is composed of the
3559163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3569163Ssam  *
3579163Ssam  * If no blocks have been allocated in the first section, the policy is to
3589163Ssam  * request a block in the same cylinder group as the inode that describes
3599163Ssam  * the file. If no blocks have been allocated in any other section, the
3609163Ssam  * policy is to place the section in a cylinder group with a greater than
3619163Ssam  * average number of free blocks.  An appropriate cylinder group is found
36217696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
36317696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
36417696Smckusick  * cylinder group from which the previous allocation was made. The sweep
36517696Smckusick  * continues until a cylinder group with greater than the average number
36617696Smckusick  * of free blocks is found. If the allocation is for the first block in an
36717696Smckusick  * indirect block, the information on the previous allocation is unavailable;
36817696Smckusick  * here a best guess is made based upon the logical block number being
36917696Smckusick  * allocated.
3709163Ssam  *
3719163Ssam  * If a section is already partially allocated, the policy is to
3729163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3739163Ssam  * contiguous blocks and the beginning of the next is physically separated
3749163Ssam  * so that the disk head will be in transit between them for at least
3759163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3769163Ssam  * schedule another I/O transfer.
3774651Smckusic  */
3785212Smckusic daddr_t
37951469Sbostic ffs_blkpref(ip, lbn, indx, bap)
3809163Ssam 	struct inode *ip;
3819163Ssam 	daddr_t lbn;
3829163Ssam 	int indx;
3839163Ssam 	daddr_t *bap;
3849163Ssam {
3855965Smckusic 	register struct fs *fs;
38617696Smckusick 	register int cg;
38717696Smckusick 	int avgbfree, startcg;
3889163Ssam 	daddr_t nextblk;
3894651Smckusic 
3909163Ssam 	fs = ip->i_fs;
3919163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3929163Ssam 		if (lbn < NDADDR) {
3939163Ssam 			cg = itog(fs, ip->i_number);
3945322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3954651Smckusic 		}
3969163Ssam 		/*
3979163Ssam 		 * Find a cylinder with greater than average number of
3989163Ssam 		 * unused data blocks.
3999163Ssam 		 */
40017696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
40117696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
40217696Smckusick 		else
40317696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
40417696Smckusick 		startcg %= fs->fs_ncg;
4059163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
40617696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
4079163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4089163Ssam 				fs->fs_cgrotor = cg;
4099163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4109163Ssam 			}
41117696Smckusick 		for (cg = 0; cg <= startcg; cg++)
4129163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4139163Ssam 				fs->fs_cgrotor = cg;
4149163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4159163Ssam 			}
4169163Ssam 		return (NULL);
4179163Ssam 	}
4189163Ssam 	/*
4199163Ssam 	 * One or more previous blocks have been laid out. If less
4209163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
4219163Ssam 	 * next block is requested contiguously, otherwise it is
4229163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
4239163Ssam 	 */
4249163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
4259163Ssam 	if (indx > fs->fs_maxcontig &&
42611638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4279163Ssam 	    != nextblk)
4289163Ssam 		return (nextblk);
4299163Ssam 	if (fs->fs_rotdelay != 0)
4309163Ssam 		/*
4319163Ssam 		 * Here we convert ms of delay to frags as:
4329163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4339163Ssam 		 *	((sect/frag) * (ms/sec))
4349163Ssam 		 * then round up to the next block.
4359163Ssam 		 */
4369163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4379163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4389163Ssam 	return (nextblk);
4394651Smckusic }
4404651Smckusic 
4415375Smckusic /*
4425375Smckusic  * Implement the cylinder overflow algorithm.
4435375Smckusic  *
4445375Smckusic  * The policy implemented by this algorithm is:
4455375Smckusic  *   1) allocate the block in its requested cylinder group.
4465375Smckusic  *   2) quadradically rehash on the cylinder group number.
4475375Smckusic  *   3) brute force search for a free block.
4485375Smckusic  */
4495212Smckusic /*VARARGS5*/
45051469Sbostic static u_long
45151469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator)
4525965Smckusic 	struct inode *ip;
4534359Smckusick 	int cg;
4544359Smckusick 	long pref;
4554359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4565212Smckusic 	u_long (*allocator)();
4574359Smckusick {
4585965Smckusic 	register struct fs *fs;
4594359Smckusick 	long result;
4604359Smckusick 	int i, icg = cg;
4614359Smckusick 
4625965Smckusic 	fs = ip->i_fs;
4634359Smckusick 	/*
4644359Smckusick 	 * 1: preferred cylinder group
4654359Smckusick 	 */
4665965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4674359Smckusick 	if (result)
4684359Smckusick 		return (result);
4694359Smckusick 	/*
4704359Smckusick 	 * 2: quadratic rehash
4714359Smckusick 	 */
4724359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4734359Smckusick 		cg += i;
4744359Smckusick 		if (cg >= fs->fs_ncg)
4754359Smckusick 			cg -= fs->fs_ncg;
4765965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4774359Smckusick 		if (result)
4784359Smckusick 			return (result);
4794359Smckusick 	}
4804359Smckusick 	/*
4814359Smckusick 	 * 3: brute force search
48210847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
48310847Ssam 	 * and 1 is always checked in the quadratic rehash.
4844359Smckusick 	 */
48510848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
48610847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4875965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4884359Smckusick 		if (result)
4894359Smckusick 			return (result);
4904359Smckusick 		cg++;
4914359Smckusick 		if (cg == fs->fs_ncg)
4924359Smckusick 			cg = 0;
4934359Smckusick 	}
4946294Smckusick 	return (NULL);
4954359Smckusick }
4964359Smckusick 
4975375Smckusic /*
4985375Smckusic  * Determine whether a fragment can be extended.
4995375Smckusic  *
5005375Smckusic  * Check to see if the necessary fragments are available, and
5015375Smckusic  * if they are, allocate them.
5025375Smckusic  */
50351469Sbostic static daddr_t
50451469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize)
5055965Smckusic 	struct inode *ip;
5064426Smckusic 	int cg;
5074463Smckusic 	long bprev;
5084426Smckusic 	int osize, nsize;
5094426Smckusic {
5105965Smckusic 	register struct fs *fs;
5114463Smckusic 	register struct cg *cgp;
51237735Smckusick 	struct buf *bp;
5134463Smckusic 	long bno;
5144463Smckusic 	int frags, bbase;
51537735Smckusick 	int i, error;
5164426Smckusic 
5175965Smckusic 	fs = ip->i_fs;
51817224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
5196531Smckusick 		return (NULL);
5205960Smckusic 	frags = numfrags(fs, nsize);
52117224Smckusick 	bbase = fragnum(fs, bprev);
52217224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
52330749Skarels 		/* cannot extend across a block boundary */
5246294Smckusick 		return (NULL);
5254463Smckusic 	}
52637735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
52738776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
52837735Smckusick 	if (error) {
52937735Smckusick 		brelse(bp);
53037735Smckusick 		return (NULL);
53137735Smckusick 	}
5326531Smckusick 	cgp = bp->b_un.b_cg;
53337735Smckusick 	if (!cg_chkmagic(cgp)) {
5345960Smckusic 		brelse(bp);
5356294Smckusick 		return (NULL);
5365960Smckusic 	}
5378105Sroot 	cgp->cg_time = time.tv_sec;
5385377Smckusic 	bno = dtogd(fs, bprev);
5395960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
54034143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
5415361Smckusic 			brelse(bp);
5426294Smckusick 			return (NULL);
5435361Smckusic 		}
5445361Smckusic 	/*
5455361Smckusic 	 * the current fragment can be extended
5465361Smckusic 	 * deduct the count on fragment being extended into
5475361Smckusic 	 * increase the count on the remaining fragment (if any)
5485361Smckusic 	 * allocate the extended piece
5495361Smckusic 	 */
5505361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
55134143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
5524463Smckusic 			break;
5535960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5545361Smckusic 	if (i != frags)
5555361Smckusic 		cgp->cg_frsum[i - frags]++;
5565960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
55734143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
5585361Smckusic 		cgp->cg_cs.cs_nffree--;
5595361Smckusic 		fs->fs_cstotal.cs_nffree--;
5605361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5614463Smckusic 	}
56250893Smckusick 	fs->fs_fmod = 1;
5635361Smckusic 	bdwrite(bp);
5645361Smckusic 	return (bprev);
5654426Smckusic }
5664426Smckusic 
5675375Smckusic /*
5685375Smckusic  * Determine whether a block can be allocated.
5695375Smckusic  *
5705375Smckusic  * Check to see if a block of the apprpriate size is available,
5715375Smckusic  * and if it is, allocate it.
5725375Smckusic  */
5739163Ssam daddr_t
57451469Sbostic ffs_alloccg(ip, cg, bpref, size)
5755965Smckusic 	struct inode *ip;
5764359Smckusick 	int cg;
5774359Smckusick 	daddr_t bpref;
5784359Smckusick 	int size;
5794359Smckusick {
5805965Smckusic 	register struct fs *fs;
5814463Smckusic 	register struct cg *cgp;
58237735Smckusick 	struct buf *bp;
5834463Smckusic 	register int i;
58437735Smckusick 	int error, bno, frags, allocsiz;
5854359Smckusick 
5865965Smckusic 	fs = ip->i_fs;
5875322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5886294Smckusick 		return (NULL);
58937735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
59038776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
59137735Smckusick 	if (error) {
59237735Smckusick 		brelse(bp);
59337735Smckusick 		return (NULL);
59437735Smckusick 	}
5956531Smckusick 	cgp = bp->b_un.b_cg;
59637735Smckusick 	if (!cg_chkmagic(cgp) ||
59715950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5985960Smckusic 		brelse(bp);
5996294Smckusick 		return (NULL);
6005960Smckusic 	}
6018105Sroot 	cgp->cg_time = time.tv_sec;
6025322Smckusic 	if (size == fs->fs_bsize) {
60351469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
6044463Smckusic 		bdwrite(bp);
6054463Smckusic 		return (bno);
6064463Smckusic 	}
6074463Smckusic 	/*
6084463Smckusic 	 * check to see if any fragments are already available
6094463Smckusic 	 * allocsiz is the size which will be allocated, hacking
6104463Smckusic 	 * it down to a smaller size if necessary
6114463Smckusic 	 */
6125960Smckusic 	frags = numfrags(fs, size);
6135322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
6144463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
6154463Smckusic 			break;
6165322Smckusic 	if (allocsiz == fs->fs_frag) {
6174463Smckusic 		/*
6184463Smckusic 		 * no fragments were available, so a block will be
6194463Smckusic 		 * allocated, and hacked up
6204463Smckusic 		 */
6214792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
6224463Smckusic 			brelse(bp);
6236294Smckusick 			return (NULL);
6244463Smckusic 		}
62551469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
6265377Smckusic 		bpref = dtogd(fs, bno);
6275322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
62834143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
6295322Smckusic 		i = fs->fs_frag - frags;
6304792Smckusic 		cgp->cg_cs.cs_nffree += i;
6314792Smckusic 		fs->fs_cstotal.cs_nffree += i;
6325322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
63350893Smckusick 		fs->fs_fmod = 1;
6344463Smckusic 		cgp->cg_frsum[i]++;
6354463Smckusic 		bdwrite(bp);
6364463Smckusic 		return (bno);
6374463Smckusic 	}
63851469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
63915950Smckusick 	if (bno < 0) {
64015950Smckusick 		brelse(bp);
6416294Smckusick 		return (NULL);
64215950Smckusick 	}
6434463Smckusic 	for (i = 0; i < frags; i++)
64434143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
6454792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6464792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6475322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
64850893Smckusick 	fs->fs_fmod = 1;
6494463Smckusic 	cgp->cg_frsum[allocsiz]--;
6504463Smckusic 	if (frags != allocsiz)
6514463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6524463Smckusic 	bdwrite(bp);
6534463Smckusic 	return (cg * fs->fs_fpg + bno);
6544463Smckusic }
6554463Smckusic 
6565375Smckusic /*
6575375Smckusic  * Allocate a block in a cylinder group.
6585375Smckusic  *
6595375Smckusic  * This algorithm implements the following policy:
6605375Smckusic  *   1) allocate the requested block.
6615375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6625375Smckusic  *   3) allocate the next available block on the block rotor for the
6635375Smckusic  *      specified cylinder group.
6645375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6655375Smckusic  * blocks may be fragmented by the routine that allocates them.
6665375Smckusic  */
66751469Sbostic static daddr_t
66851469Sbostic ffs_alloccgblk(fs, cgp, bpref)
6695965Smckusic 	register struct fs *fs;
6704463Smckusic 	register struct cg *cgp;
6714463Smckusic 	daddr_t bpref;
6724463Smckusic {
6734651Smckusic 	daddr_t bno;
6746294Smckusick 	int cylno, pos, delta;
6754651Smckusic 	short *cylbp;
6765361Smckusic 	register int i;
6774463Smckusic 
6784651Smckusic 	if (bpref == 0) {
6794651Smckusic 		bpref = cgp->cg_rotor;
6805361Smckusic 		goto norot;
6815361Smckusic 	}
68217224Smckusick 	bpref = blknum(fs, bpref);
6835377Smckusic 	bpref = dtogd(fs, bpref);
6845361Smckusic 	/*
6855361Smckusic 	 * if the requested block is available, use it
6865361Smckusic 	 */
68751469Sbostic 	if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
6885361Smckusic 		bno = bpref;
6895361Smckusic 		goto gotit;
6905361Smckusic 	}
6915361Smckusic 	/*
6925361Smckusic 	 * check for a block available on the same cylinder
6935361Smckusic 	 */
6945361Smckusic 	cylno = cbtocylno(fs, bpref);
69534143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
6965375Smckusic 		goto norot;
6975375Smckusic 	if (fs->fs_cpc == 0) {
6985375Smckusic 		/*
6995375Smckusic 		 * block layout info is not available, so just have
7005375Smckusic 		 * to take any block in this cylinder.
7015375Smckusic 		 */
7025375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
7035375Smckusic 		goto norot;
7045375Smckusic 	}
7055375Smckusic 	/*
7065361Smckusic 	 * check the summary information to see if a block is
7075361Smckusic 	 * available in the requested cylinder starting at the
7089163Ssam 	 * requested rotational position and proceeding around.
7095361Smckusic 	 */
71034143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
7119163Ssam 	pos = cbtorpos(fs, bpref);
71234143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
7135361Smckusic 		if (cylbp[i] > 0)
7145361Smckusic 			break;
71534143Smckusick 	if (i == fs->fs_nrpos)
7165361Smckusic 		for (i = 0; i < pos; i++)
7175361Smckusic 			if (cylbp[i] > 0)
7185361Smckusic 				break;
7195361Smckusic 	if (cylbp[i] > 0) {
7204651Smckusic 		/*
7215361Smckusic 		 * found a rotational position, now find the actual
7225361Smckusic 		 * block. A panic if none is actually there.
7234651Smckusic 		 */
7245361Smckusic 		pos = cylno % fs->fs_cpc;
7255361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
72634143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
7276716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
7286716Smckusick 			    pos, i, fs->fs_fsmnt);
72951469Sbostic 			panic("ffs_alloccgblk: cyl groups corrupted");
7306716Smckusick 		}
73134143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
73251469Sbostic 			if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
73311638Ssam 				bno = blkstofrags(fs, (bno + i));
7345361Smckusic 				goto gotit;
7355361Smckusic 			}
73634143Smckusick 			delta = fs_rotbl(fs)[i];
73734143Smckusick 			if (delta <= 0 ||
73834143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
7394651Smckusic 				break;
7406294Smckusick 			i += delta;
7414651Smckusic 		}
7426716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
74351469Sbostic 		panic("ffs_alloccgblk: can't find blk in cyl");
7444359Smckusick 	}
7455361Smckusic norot:
7465361Smckusic 	/*
7475361Smckusic 	 * no blocks in the requested cylinder, so take next
7485361Smckusic 	 * available one in this cylinder group.
7495361Smckusic 	 */
75051469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7516567Smckusic 	if (bno < 0)
7526294Smckusick 		return (NULL);
7534651Smckusic 	cgp->cg_rotor = bno;
7544359Smckusick gotit:
75551469Sbostic 	ffs_clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno));
7564792Smckusic 	cgp->cg_cs.cs_nbfree--;
7574792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7585322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7595375Smckusic 	cylno = cbtocylno(fs, bno);
76034143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
76134143Smckusick 	cg_blktot(cgp)[cylno]--;
76250893Smckusick 	fs->fs_fmod = 1;
7634651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7644359Smckusick }
76534143Smckusick 
7665375Smckusic /*
7675375Smckusic  * Determine whether an inode can be allocated.
7685375Smckusic  *
7695375Smckusic  * Check to see if an inode is available, and if it is,
7705375Smckusic  * allocate it using the following policy:
7715375Smckusic  *   1) allocate the requested inode.
7725375Smckusic  *   2) allocate the next available inode after the requested
7735375Smckusic  *      inode in the specified cylinder group.
7745375Smckusic  */
77551469Sbostic static ino_t
77651469Sbostic ffs_ialloccg(ip, cg, ipref, mode)
7775965Smckusic 	struct inode *ip;
7784359Smckusick 	int cg;
7794359Smckusick 	daddr_t ipref;
7804359Smckusick 	int mode;
7814359Smckusick {
7825965Smckusic 	register struct fs *fs;
7834463Smckusic 	register struct cg *cgp;
78416784Smckusick 	struct buf *bp;
78537735Smckusick 	int error, start, len, loc, map, i;
7864359Smckusick 
7875965Smckusic 	fs = ip->i_fs;
7885322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7896294Smckusick 		return (NULL);
79037735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
79138776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
79237735Smckusick 	if (error) {
79337735Smckusick 		brelse(bp);
79437735Smckusick 		return (NULL);
79537735Smckusick 	}
7966531Smckusick 	cgp = bp->b_un.b_cg;
79737735Smckusick 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
7985960Smckusic 		brelse(bp);
7996294Smckusick 		return (NULL);
8005960Smckusic 	}
8018105Sroot 	cgp->cg_time = time.tv_sec;
8024359Smckusick 	if (ipref) {
8034359Smckusick 		ipref %= fs->fs_ipg;
80434143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
8054359Smckusick 			goto gotit;
80616784Smckusick 	}
80716784Smckusick 	start = cgp->cg_irotor / NBBY;
80816784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
80934143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
81016784Smckusick 	if (loc == 0) {
81117697Smckusick 		len = start + 1;
81217697Smckusick 		start = 0;
81334143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
81417697Smckusick 		if (loc == 0) {
81517697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
81617697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
81751469Sbostic 			panic("ffs_ialloccg: map corrupted");
81817697Smckusick 			/* NOTREACHED */
81917697Smckusick 		}
82016784Smckusick 	}
82116784Smckusick 	i = start + len - loc;
82234143Smckusick 	map = cg_inosused(cgp)[i];
82316784Smckusick 	ipref = i * NBBY;
82416784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
82516784Smckusick 		if ((map & i) == 0) {
8264359Smckusick 			cgp->cg_irotor = ipref;
8274359Smckusick 			goto gotit;
8284359Smckusick 		}
8294359Smckusick 	}
83016784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
83151469Sbostic 	panic("ffs_ialloccg: block not in map");
83216784Smckusick 	/* NOTREACHED */
8334359Smckusick gotit:
83434143Smckusick 	setbit(cg_inosused(cgp), ipref);
8354792Smckusic 	cgp->cg_cs.cs_nifree--;
8364792Smckusic 	fs->fs_cstotal.cs_nifree--;
8375322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
83850893Smckusick 	fs->fs_fmod = 1;
8394359Smckusick 	if ((mode & IFMT) == IFDIR) {
8404792Smckusic 		cgp->cg_cs.cs_ndir++;
8414792Smckusic 		fs->fs_cstotal.cs_ndir++;
8425322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8434359Smckusick 	}
8444359Smckusick 	bdwrite(bp);
8454359Smckusick 	return (cg * fs->fs_ipg + ipref);
8464359Smckusick }
8474359Smckusick 
8485375Smckusic /*
8495375Smckusic  * Free a block or fragment.
8505375Smckusic  *
8515375Smckusic  * The specified block or fragment is placed back in the
8525375Smckusic  * free map. If a fragment is deallocated, a possible
8535375Smckusic  * block reassembly is checked.
8545375Smckusic  */
85551469Sbostic ffs_blkfree(ip, bno, size)
8565965Smckusic 	register struct inode *ip;
8574359Smckusick 	daddr_t bno;
8585212Smckusic 	off_t size;
8594359Smckusick {
8604359Smckusick 	register struct fs *fs;
8614359Smckusick 	register struct cg *cgp;
86237735Smckusick 	struct buf *bp;
86337735Smckusick 	int error, cg, blk, frags, bbase;
8644463Smckusic 	register int i;
86547571Skarels 	struct ucred *cred = curproc->p_ucred;	/* XXX */
8664359Smckusick 
8675965Smckusic 	fs = ip->i_fs;
8686716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8696716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8706716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
87131402Smckusick 		panic("blkfree: bad size");
8726716Smckusick 	}
8735377Smckusic 	cg = dtog(fs, bno);
87442318Smckusick 	if ((unsigned)bno >= fs->fs_size) {
8756567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
87651469Sbostic 		ffs_fserr(fs, cred->cr_uid, "bad block");
8774359Smckusick 		return;
8786567Smckusic 	}
87937735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
88038776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
88137735Smckusick 	if (error) {
88237735Smckusick 		brelse(bp);
88337735Smckusick 		return;
88437735Smckusick 	}
8856531Smckusick 	cgp = bp->b_un.b_cg;
88637735Smckusick 	if (!cg_chkmagic(cgp)) {
8875960Smckusic 		brelse(bp);
8884359Smckusick 		return;
8895960Smckusic 	}
8908105Sroot 	cgp->cg_time = time.tv_sec;
8915377Smckusic 	bno = dtogd(fs, bno);
8925322Smckusic 	if (size == fs->fs_bsize) {
89351469Sbostic 		if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) {
8946716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
8956716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
89631402Smckusick 			panic("blkfree: freeing free block");
8976567Smckusic 		}
89851469Sbostic 		ffs_setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
8994792Smckusic 		cgp->cg_cs.cs_nbfree++;
9004792Smckusic 		fs->fs_cstotal.cs_nbfree++;
9015322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
9025375Smckusic 		i = cbtocylno(fs, bno);
90334143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
90434143Smckusick 		cg_blktot(cgp)[i]++;
9054426Smckusic 	} else {
90617224Smckusick 		bbase = bno - fragnum(fs, bno);
9074463Smckusic 		/*
9084463Smckusic 		 * decrement the counts associated with the old frags
9094463Smckusic 		 */
91034143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
91151469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
9124463Smckusic 		/*
9134463Smckusic 		 * deallocate the fragment
9144463Smckusic 		 */
9155960Smckusic 		frags = numfrags(fs, size);
9164463Smckusic 		for (i = 0; i < frags; i++) {
91734143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
9186716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
9196716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
92031402Smckusick 				panic("blkfree: freeing free frag");
9216716Smckusick 			}
92234143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
9234426Smckusic 		}
9246294Smckusick 		cgp->cg_cs.cs_nffree += i;
9256294Smckusick 		fs->fs_cstotal.cs_nffree += i;
9266294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
9274463Smckusic 		/*
9284463Smckusic 		 * add back in counts associated with the new frags
9294463Smckusic 		 */
93034143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
93151469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
9324463Smckusic 		/*
9334463Smckusic 		 * if a complete block has been reassembled, account for it
9344463Smckusic 		 */
93551469Sbostic 		if (ffs_isblock(fs, cg_blksfree(cgp),
93634476Smckusick 		    (daddr_t)fragstoblks(fs, bbase))) {
9375322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
9385322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
9395322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
9404792Smckusic 			cgp->cg_cs.cs_nbfree++;
9414792Smckusic 			fs->fs_cstotal.cs_nbfree++;
9425322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
9435375Smckusic 			i = cbtocylno(fs, bbase);
94434143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
94534143Smckusick 			cg_blktot(cgp)[i]++;
9464426Smckusic 		}
9474426Smckusic 	}
94850893Smckusick 	fs->fs_fmod = 1;
9494359Smckusick 	bdwrite(bp);
9504359Smckusick }
9514359Smckusick 
9525375Smckusic /*
9535375Smckusic  * Free an inode.
9545375Smckusic  *
9555375Smckusic  * The specified inode is placed back in the free map.
9565375Smckusic  */
95751469Sbostic void
958*51541Smckusick ffs_vfree(pvp, ino, mode)
959*51541Smckusick 	struct vnode *pvp;
9604359Smckusick 	ino_t ino;
9614359Smckusick 	int mode;
9624359Smckusick {
9634359Smckusick 	register struct fs *fs;
9644359Smckusick 	register struct cg *cgp;
965*51541Smckusick 	register struct inode *pip;
96637735Smckusick 	struct buf *bp;
96737735Smckusick 	int error, cg;
9684359Smckusick 
969*51541Smckusick 	pip = VTOI(pvp);
97051469Sbostic 	fs = pip->i_fs;
97151469Sbostic 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
97251469Sbostic 		panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
97351469Sbostic 		    pip->i_dev, ino, fs->fs_fsmnt);
9745377Smckusic 	cg = itog(fs, ino);
97551469Sbostic 	error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
97638776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
97737735Smckusick 	if (error) {
97837735Smckusick 		brelse(bp);
97937735Smckusick 		return;
98037735Smckusick 	}
9816531Smckusick 	cgp = bp->b_un.b_cg;
98237735Smckusick 	if (!cg_chkmagic(cgp)) {
9835960Smckusic 		brelse(bp);
9844359Smckusick 		return;
9855960Smckusic 	}
9868105Sroot 	cgp->cg_time = time.tv_sec;
9874359Smckusick 	ino %= fs->fs_ipg;
98834143Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
9896716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
99051469Sbostic 		    pip->i_dev, ino, fs->fs_fsmnt);
99148057Smckusick 		if (fs->fs_ronly == 0)
99248057Smckusick 			panic("ifree: freeing free inode");
9936716Smckusick 	}
99434143Smckusick 	clrbit(cg_inosused(cgp), ino);
99516784Smckusick 	if (ino < cgp->cg_irotor)
99616784Smckusick 		cgp->cg_irotor = ino;
9974792Smckusic 	cgp->cg_cs.cs_nifree++;
9984792Smckusic 	fs->fs_cstotal.cs_nifree++;
9995322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
10004359Smckusick 	if ((mode & IFMT) == IFDIR) {
10014792Smckusic 		cgp->cg_cs.cs_ndir--;
10024792Smckusic 		fs->fs_cstotal.cs_ndir--;
10035322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
10044359Smckusick 	}
100550893Smckusick 	fs->fs_fmod = 1;
10064359Smckusick 	bdwrite(bp);
10074359Smckusick }
10084359Smckusick 
10094463Smckusic /*
10105375Smckusic  * Find a block of the specified size in the specified cylinder group.
10115375Smckusic  *
10124651Smckusic  * It is a panic if a request is made to find a block if none are
10134651Smckusic  * available.
10144651Smckusic  */
101551469Sbostic static daddr_t
101651469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz)
10174651Smckusic 	register struct fs *fs;
10184651Smckusic 	register struct cg *cgp;
10194651Smckusic 	daddr_t bpref;
10204651Smckusic 	int allocsiz;
10214651Smckusic {
10224651Smckusic 	daddr_t bno;
10234651Smckusic 	int start, len, loc, i;
10244651Smckusic 	int blk, field, subfield, pos;
10254651Smckusic 
10264651Smckusic 	/*
10274651Smckusic 	 * find the fragment by searching through the free block
10284651Smckusic 	 * map for an appropriate bit pattern
10294651Smckusic 	 */
10304651Smckusic 	if (bpref)
10315377Smckusic 		start = dtogd(fs, bpref) / NBBY;
10324651Smckusic 	else
10334651Smckusic 		start = cgp->cg_frotor / NBBY;
10345398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
103534476Smckusick 	loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start],
103634476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
103734476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
10384651Smckusic 	if (loc == 0) {
10396531Smckusick 		len = start + 1;
10406531Smckusick 		start = 0;
104134476Smckusick 		loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0],
104234476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
104334476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
104416784Smckusick 		if (loc == 0) {
104516784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
104616784Smckusick 			    start, len, fs->fs_fsmnt);
104751469Sbostic 			panic("ffs_alloccg: map corrupted");
104817697Smckusick 			/* NOTREACHED */
104916784Smckusick 		}
10504651Smckusic 	}
10514651Smckusic 	bno = (start + len - loc) * NBBY;
10524651Smckusic 	cgp->cg_frotor = bno;
10534651Smckusic 	/*
10544651Smckusic 	 * found the byte in the map
10554651Smckusic 	 * sift through the bits to find the selected frag
10564651Smckusic 	 */
10576294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
105834143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
10594651Smckusic 		blk <<= 1;
10604651Smckusic 		field = around[allocsiz];
10614651Smckusic 		subfield = inside[allocsiz];
10625322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10636294Smckusick 			if ((blk & field) == subfield)
10646294Smckusick 				return (bno + pos);
10654651Smckusic 			field <<= 1;
10664651Smckusic 			subfield <<= 1;
10674651Smckusic 		}
10684651Smckusic 	}
10696716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
107051469Sbostic 	panic("ffs_alloccg: block not in map");
10716531Smckusick 	return (-1);
10724651Smckusic }
10734651Smckusic 
10744651Smckusic /*
10755375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10765375Smckusic  *
10775375Smckusic  * The form of the error message is:
10784359Smckusick  *	fs: error message
10794359Smckusick  */
108051469Sbostic static void
108151469Sbostic ffs_fserr(fs, uid, cp)
10824359Smckusick 	struct fs *fs;
108351469Sbostic 	u_int uid;
10844359Smckusick 	char *cp;
10854359Smckusick {
10864359Smckusick 
108742318Smckusick 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
10884359Smckusick }
1089