xref: /csrg-svn/sys/ufs/ffs/ffs_alloc.c (revision 48948)
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*48948Smckusick  *	@(#)ffs_alloc.c	7.26 (Berkeley) 05/02/91
823394Smckusick  */
94359Smckusick 
1017097Sbloom #include "param.h"
1117097Sbloom #include "systm.h"
1217097Sbloom #include "buf.h"
1347571Skarels #include "proc.h"
1437735Smckusick #include "vnode.h"
1517097Sbloom #include "kernel.h"
1618307Sralph #include "syslog.h"
174359Smckusick 
1847571Skarels #include "quota.h"
1947571Skarels #include "inode.h"
2047571Skarels #include "fs.h"
2147571Skarels 
225212Smckusic extern u_long		hashalloc();
239163Ssam extern ino_t		ialloccg();
249163Ssam extern daddr_t		alloccg();
254651Smckusic extern daddr_t		alloccgblk();
264651Smckusic extern daddr_t		fragextend();
274651Smckusic extern daddr_t		blkpref();
284651Smckusic extern daddr_t		mapsearch();
294607Smckusic extern int		inside[], around[];
305322Smckusic extern unsigned char	*fragtbl[];
314359Smckusick 
325375Smckusic /*
335375Smckusic  * Allocate a block in the file system.
345375Smckusic  *
355375Smckusic  * The size of the requested block is given, which must be some
365375Smckusic  * multiple of fs_fsize and <= fs_bsize.
375375Smckusic  * A preference may be optionally specified. If a preference is given
385375Smckusic  * the following hierarchy is used to allocate a block:
395375Smckusic  *   1) allocate the requested block.
405375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
415375Smckusic  *   3) allocate a block in the same cylinder group.
425375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
435375Smckusic  *      available block is located.
445375Smckusic  * If no block preference is given the following heirarchy is used
455375Smckusic  * to allocate a block:
465375Smckusic  *   1) allocate a block in the cylinder group that contains the
475375Smckusic  *      inode for the file.
485375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
495375Smckusic  *      available block is located.
505375Smckusic  */
5139678Smckusick alloc(ip, lbn, bpref, size, bnp)
524463Smckusic 	register struct inode *ip;
5339678Smckusick 	daddr_t lbn, bpref;
544359Smckusick 	int size;
5539678Smckusick 	daddr_t *bnp;
564359Smckusick {
574359Smckusick 	daddr_t bno;
584359Smckusick 	register struct fs *fs;
594463Smckusic 	register struct buf *bp;
6037735Smckusick 	int cg, error;
6147571Skarels 	struct ucred *cred = curproc->p_ucred;		/* XXX */
624359Smckusick 
6339678Smckusick 	*bnp = 0;
645965Smckusic 	fs = ip->i_fs;
656716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
666716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
676716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
684463Smckusic 		panic("alloc: bad size");
696716Smckusick 	}
705322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
714359Smckusick 		goto nospace;
7241309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
734792Smckusic 		goto nospace;
747650Ssam #ifdef QUOTA
7541309Smckusick 	if (error = chkdq(ip, (long)btodb(size), cred, 0))
7637735Smckusick 		return (error);
777483Skre #endif
784948Smckusic 	if (bpref >= fs->fs_size)
794948Smckusic 		bpref = 0;
804359Smckusick 	if (bpref == 0)
815377Smckusic 		cg = itog(fs, ip->i_number);
824359Smckusick 	else
835377Smckusic 		cg = dtog(fs, bpref);
849163Ssam 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
859163Ssam 		(u_long (*)())alloccg);
8639678Smckusick 	if (bno > 0) {
8739678Smckusick 		ip->i_blocks += btodb(size);
8839678Smckusick 		ip->i_flag |= IUPD|ICHG;
8939678Smckusick 		*bnp = bno;
9039678Smckusick 		return (0);
9139678Smckusick 	}
9245173Smckusick #ifdef QUOTA
9345173Smckusick 	/*
9445173Smckusick 	 * Restore user's disk quota because allocation failed.
9545173Smckusick 	 */
9645173Smckusick 	(void) chkdq(ip, (long)-btodb(size), cred, FORCE);
9745173Smckusick #endif
984359Smckusick nospace:
9942318Smckusick 	fserr(fs, cred->cr_uid, "file system full");
1004359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
10137735Smckusick 	return (ENOSPC);
1024359Smckusick }
1034359Smckusick 
1045375Smckusic /*
1055375Smckusic  * Reallocate a fragment to a bigger size
1065375Smckusic  *
1075375Smckusic  * The number and size of the old block is given, and a preference
1085375Smckusic  * and new size is also specified. The allocator attempts to extend
1095375Smckusic  * the original block. Failing that, the regular block allocator is
1105375Smckusic  * invoked to get an appropriate block.
1115375Smckusic  */
11239678Smckusick realloccg(ip, lbprev, bpref, osize, nsize, bpp)
1135965Smckusic 	register struct inode *ip;
11439678Smckusick 	off_t lbprev;
11539678Smckusick 	daddr_t bpref;
1164426Smckusic 	int osize, nsize;
11737735Smckusick 	struct buf **bpp;
1184426Smckusic {
1194426Smckusic 	register struct fs *fs;
12037735Smckusick 	struct buf *bp, *obp;
12145719Smckusick 	int cg, request, error;
12245719Smckusick 	daddr_t bprev, bno;
12347571Skarels 	struct ucred *cred = curproc->p_ucred;		/* XXX */
1244426Smckusic 
12537735Smckusick 	*bpp = 0;
1265965Smckusic 	fs = ip->i_fs;
1275960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1286716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1296716Smckusick 		printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1306716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
1314463Smckusic 		panic("realloccg: bad size");
1326716Smckusick 	}
13341309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1344792Smckusic 		goto nospace;
13539678Smckusick 	if ((bprev = ip->i_db[lbprev]) == 0) {
1366716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1376716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1384463Smckusic 		panic("realloccg: bad bprev");
1396716Smckusick 	}
14039678Smckusick 	/*
14139678Smckusick 	 * Allocate the extra space in the buffer.
14239678Smckusick 	 */
14339678Smckusick 	if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
14439678Smckusick 		brelse(bp);
14539678Smckusick 		return (error);
14639678Smckusick 	}
14745173Smckusick #ifdef QUOTA
14845173Smckusick 	if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) {
14945173Smckusick 		brelse(bp);
15045173Smckusick 		return (error);
15145173Smckusick 	}
15245173Smckusick #endif
15339678Smckusick 	/*
15439678Smckusick 	 * Check for extension in the existing location.
15539678Smckusick 	 */
1566294Smckusick 	cg = dtog(fs, bprev);
15739678Smckusick 	if (bno = fragextend(ip, cg, (long)bprev, osize, nsize)) {
15839887Smckusick 		if (bp->b_blkno != fsbtodb(fs, bno))
15939678Smckusick 			panic("bad blockno");
16039762Smckusick 		ip->i_blocks += btodb(nsize - osize);
16139762Smckusick 		ip->i_flag |= IUPD|ICHG;
162*48948Smckusick 		allocbuf(bp, nsize);
163*48948Smckusick 		bp->b_flags |= B_DONE;
164*48948Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
16537735Smckusick 		*bpp = bp;
16637735Smckusick 		return (0);
1674463Smckusic 	}
16839678Smckusick 	/*
16939678Smckusick 	 * Allocate a new disk location.
17039678Smckusick 	 */
1714948Smckusic 	if (bpref >= fs->fs_size)
1724948Smckusic 		bpref = 0;
17326253Skarels 	switch ((int)fs->fs_optim) {
17425256Smckusick 	case FS_OPTSPACE:
17525256Smckusick 		/*
17625256Smckusick 		 * Allocate an exact sized fragment. Although this makes
17725256Smckusick 		 * best use of space, we will waste time relocating it if
17825256Smckusick 		 * the file continues to grow. If the fragmentation is
17925256Smckusick 		 * less than half of the minimum free reserve, we choose
18025256Smckusick 		 * to begin optimizing for time.
18125256Smckusick 		 */
18224698Smckusick 		request = nsize;
18325256Smckusick 		if (fs->fs_minfree < 5 ||
18425256Smckusick 		    fs->fs_cstotal.cs_nffree >
18525256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
18625256Smckusick 			break;
18725256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
18825256Smckusick 			fs->fs_fsmnt);
18925256Smckusick 		fs->fs_optim = FS_OPTTIME;
19025256Smckusick 		break;
19125256Smckusick 	case FS_OPTTIME:
19225256Smckusick 		/*
19325256Smckusick 		 * At this point we have discovered a file that is trying
19425256Smckusick 		 * to grow a small fragment to a larger fragment. To save
19525256Smckusick 		 * time, we allocate a full sized block, then free the
19625256Smckusick 		 * unused portion. If the file continues to grow, the
19725256Smckusick 		 * `fragextend' call above will be able to grow it in place
19825256Smckusick 		 * without further copying. If aberrant programs cause
19925256Smckusick 		 * disk fragmentation to grow within 2% of the free reserve,
20025256Smckusick 		 * we choose to begin optimizing for space.
20125256Smckusick 		 */
20224698Smckusick 		request = fs->fs_bsize;
20325256Smckusick 		if (fs->fs_cstotal.cs_nffree <
20425256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
20525256Smckusick 			break;
20625256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
20725256Smckusick 			fs->fs_fsmnt);
20825256Smckusick 		fs->fs_optim = FS_OPTSPACE;
20925256Smckusick 		break;
21025256Smckusick 	default:
21125256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
21225256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
21325256Smckusick 		panic("realloccg: bad optim");
21425256Smckusick 		/* NOTREACHED */
21525256Smckusick 	}
21624698Smckusick 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
2179163Ssam 		(u_long (*)())alloccg);
2186567Smckusic 	if (bno > 0) {
21945719Smckusick 		bp->b_blkno = fsbtodb(fs, bno);
22045719Smckusick 		(void) vnode_pager_uncache(ITOV(ip));
22131402Smckusick 		blkfree(ip, bprev, (off_t)osize);
22224698Smckusick 		if (nsize < request)
22331402Smckusick 			blkfree(ip, bno + numfrags(fs, nsize),
22424698Smckusick 				(off_t)(request - nsize));
22512643Ssam 		ip->i_blocks += btodb(nsize - osize);
22612643Ssam 		ip->i_flag |= IUPD|ICHG;
227*48948Smckusick 		allocbuf(bp, nsize);
228*48948Smckusick 		bp->b_flags |= B_DONE;
229*48948Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
23037735Smckusick 		*bpp = bp;
23137735Smckusick 		return (0);
2324463Smckusic 	}
23345173Smckusick #ifdef QUOTA
23445173Smckusick 	/*
23545173Smckusick 	 * Restore user's disk quota because allocation failed.
23645173Smckusick 	 */
23745173Smckusick 	(void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
23845173Smckusick #endif
23939678Smckusick 	brelse(bp);
2404792Smckusic nospace:
2414463Smckusic 	/*
2424463Smckusic 	 * no space available
2434463Smckusic 	 */
24442318Smckusick 	fserr(fs, cred->cr_uid, "file system full");
2454426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
24637735Smckusick 	return (ENOSPC);
2474426Smckusic }
2484426Smckusic 
2495375Smckusic /*
2505375Smckusic  * Allocate an inode in the file system.
2515375Smckusic  *
2525375Smckusic  * A preference may be optionally specified. If a preference is given
2535375Smckusic  * the following hierarchy is used to allocate an inode:
2545375Smckusic  *   1) allocate the requested inode.
2555375Smckusic  *   2) allocate an inode in the same cylinder group.
2565375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2575375Smckusic  *      available inode is located.
2585375Smckusic  * If no inode preference is given the following heirarchy is used
2595375Smckusic  * to allocate an inode:
2605375Smckusic  *   1) allocate an inode in cylinder group 0.
2615375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2625375Smckusic  *      available inode is located.
2635375Smckusic  */
26441309Smckusick ialloc(pip, ipref, mode, cred, ipp)
2655965Smckusic 	register struct inode *pip;
2664359Smckusick 	ino_t ipref;
2674359Smckusick 	int mode;
26841309Smckusick 	struct ucred *cred;
26937735Smckusick 	struct inode **ipp;
2704359Smckusick {
2715212Smckusic 	ino_t ino;
2724359Smckusick 	register struct fs *fs;
2734359Smckusick 	register struct inode *ip;
27437735Smckusick 	int cg, error;
2754359Smckusick 
27637735Smckusick 	*ipp = 0;
2775965Smckusic 	fs = pip->i_fs;
2784792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2794359Smckusick 		goto noinodes;
2804948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2814948Smckusic 		ipref = 0;
2825377Smckusic 	cg = itog(fs, ipref);
2835965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2844359Smckusick 	if (ino == 0)
2854359Smckusick 		goto noinodes;
28637735Smckusick 	error = iget(pip, ino, ipp);
28737735Smckusick 	if (error) {
28841309Smckusick 		ifree(pip, ino, mode);
28937735Smckusick 		return (error);
2904359Smckusick 	}
29138255Smckusick 	ip = *ipp;
2926716Smckusick 	if (ip->i_mode) {
2936716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
2946716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2954359Smckusick 		panic("ialloc: dup alloc");
2966716Smckusick 	}
29712643Ssam 	if (ip->i_blocks) {				/* XXX */
29812643Ssam 		printf("free inode %s/%d had %d blocks\n",
29912643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
30012643Ssam 		ip->i_blocks = 0;
30112643Ssam 	}
30239516Smckusick 	ip->i_flags = 0;
30338255Smckusick 	/*
30438255Smckusick 	 * Set up a new generation number for this inode.
30538255Smckusick 	 */
30638255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
30738255Smckusick 		nextgennumber = time.tv_sec;
30838255Smckusick 	ip->i_gen = nextgennumber;
30937735Smckusick 	return (0);
3104359Smckusick noinodes:
31142318Smckusick 	fserr(fs, cred->cr_uid, "out of inodes");
3126294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
31337735Smckusick 	return (ENOSPC);
3144359Smckusick }
3154359Smckusick 
3164651Smckusic /*
3175375Smckusic  * Find a cylinder to place a directory.
3185375Smckusic  *
3195375Smckusic  * The policy implemented by this algorithm is to select from
3205375Smckusic  * among those cylinder groups with above the average number of
3215375Smckusic  * free inodes, the one with the smallest number of directories.
3224651Smckusic  */
3239163Ssam ino_t
3245965Smckusic dirpref(fs)
3255965Smckusic 	register struct fs *fs;
3264359Smckusick {
3274651Smckusic 	int cg, minndir, mincg, avgifree;
3284359Smckusick 
3294792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3304651Smckusic 	minndir = fs->fs_ipg;
3314359Smckusick 	mincg = 0;
3324651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3335322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3345322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3354359Smckusick 			mincg = cg;
3365322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3374359Smckusick 		}
3389163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3394359Smckusick }
3404359Smckusick 
3414651Smckusic /*
3429163Ssam  * Select the desired position for the next block in a file.  The file is
3439163Ssam  * logically divided into sections. The first section is composed of the
3449163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3459163Ssam  *
3469163Ssam  * If no blocks have been allocated in the first section, the policy is to
3479163Ssam  * request a block in the same cylinder group as the inode that describes
3489163Ssam  * the file. If no blocks have been allocated in any other section, the
3499163Ssam  * policy is to place the section in a cylinder group with a greater than
3509163Ssam  * average number of free blocks.  An appropriate cylinder group is found
35117696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
35217696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
35317696Smckusick  * cylinder group from which the previous allocation was made. The sweep
35417696Smckusick  * continues until a cylinder group with greater than the average number
35517696Smckusick  * of free blocks is found. If the allocation is for the first block in an
35617696Smckusick  * indirect block, the information on the previous allocation is unavailable;
35717696Smckusick  * here a best guess is made based upon the logical block number being
35817696Smckusick  * allocated.
3599163Ssam  *
3609163Ssam  * If a section is already partially allocated, the policy is to
3619163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3629163Ssam  * contiguous blocks and the beginning of the next is physically separated
3639163Ssam  * so that the disk head will be in transit between them for at least
3649163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3659163Ssam  * schedule another I/O transfer.
3664651Smckusic  */
3675212Smckusic daddr_t
3689163Ssam blkpref(ip, lbn, indx, bap)
3699163Ssam 	struct inode *ip;
3709163Ssam 	daddr_t lbn;
3719163Ssam 	int indx;
3729163Ssam 	daddr_t *bap;
3739163Ssam {
3745965Smckusic 	register struct fs *fs;
37517696Smckusick 	register int cg;
37617696Smckusick 	int avgbfree, startcg;
3779163Ssam 	daddr_t nextblk;
3784651Smckusic 
3799163Ssam 	fs = ip->i_fs;
3809163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3819163Ssam 		if (lbn < NDADDR) {
3829163Ssam 			cg = itog(fs, ip->i_number);
3835322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3844651Smckusic 		}
3859163Ssam 		/*
3869163Ssam 		 * Find a cylinder with greater than average number of
3879163Ssam 		 * unused data blocks.
3889163Ssam 		 */
38917696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
39017696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
39117696Smckusick 		else
39217696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
39317696Smckusick 		startcg %= fs->fs_ncg;
3949163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
39517696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
3969163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3979163Ssam 				fs->fs_cgrotor = cg;
3989163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3999163Ssam 			}
40017696Smckusick 		for (cg = 0; cg <= startcg; cg++)
4019163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4029163Ssam 				fs->fs_cgrotor = cg;
4039163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4049163Ssam 			}
4059163Ssam 		return (NULL);
4069163Ssam 	}
4079163Ssam 	/*
4089163Ssam 	 * One or more previous blocks have been laid out. If less
4099163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
4109163Ssam 	 * next block is requested contiguously, otherwise it is
4119163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
4129163Ssam 	 */
4139163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
4149163Ssam 	if (indx > fs->fs_maxcontig &&
41511638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4169163Ssam 	    != nextblk)
4179163Ssam 		return (nextblk);
4189163Ssam 	if (fs->fs_rotdelay != 0)
4199163Ssam 		/*
4209163Ssam 		 * Here we convert ms of delay to frags as:
4219163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4229163Ssam 		 *	((sect/frag) * (ms/sec))
4239163Ssam 		 * then round up to the next block.
4249163Ssam 		 */
4259163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4269163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4279163Ssam 	return (nextblk);
4284651Smckusic }
4294651Smckusic 
4305375Smckusic /*
4315375Smckusic  * Implement the cylinder overflow algorithm.
4325375Smckusic  *
4335375Smckusic  * The policy implemented by this algorithm is:
4345375Smckusic  *   1) allocate the block in its requested cylinder group.
4355375Smckusic  *   2) quadradically rehash on the cylinder group number.
4365375Smckusic  *   3) brute force search for a free block.
4375375Smckusic  */
4385212Smckusic /*VARARGS5*/
4395212Smckusic u_long
4405965Smckusic hashalloc(ip, cg, pref, size, allocator)
4415965Smckusic 	struct inode *ip;
4424359Smckusick 	int cg;
4434359Smckusick 	long pref;
4444359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4455212Smckusic 	u_long (*allocator)();
4464359Smckusick {
4475965Smckusic 	register struct fs *fs;
4484359Smckusick 	long result;
4494359Smckusick 	int i, icg = cg;
4504359Smckusick 
4515965Smckusic 	fs = ip->i_fs;
4524359Smckusick 	/*
4534359Smckusick 	 * 1: preferred cylinder group
4544359Smckusick 	 */
4555965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4564359Smckusick 	if (result)
4574359Smckusick 		return (result);
4584359Smckusick 	/*
4594359Smckusick 	 * 2: quadratic rehash
4604359Smckusick 	 */
4614359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4624359Smckusick 		cg += i;
4634359Smckusick 		if (cg >= fs->fs_ncg)
4644359Smckusick 			cg -= fs->fs_ncg;
4655965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4664359Smckusick 		if (result)
4674359Smckusick 			return (result);
4684359Smckusick 	}
4694359Smckusick 	/*
4704359Smckusick 	 * 3: brute force search
47110847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
47210847Ssam 	 * and 1 is always checked in the quadratic rehash.
4734359Smckusick 	 */
47410848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
47510847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4765965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4774359Smckusick 		if (result)
4784359Smckusick 			return (result);
4794359Smckusick 		cg++;
4804359Smckusick 		if (cg == fs->fs_ncg)
4814359Smckusick 			cg = 0;
4824359Smckusick 	}
4836294Smckusick 	return (NULL);
4844359Smckusick }
4854359Smckusick 
4865375Smckusic /*
4875375Smckusic  * Determine whether a fragment can be extended.
4885375Smckusic  *
4895375Smckusic  * Check to see if the necessary fragments are available, and
4905375Smckusic  * if they are, allocate them.
4915375Smckusic  */
4924359Smckusick daddr_t
4935965Smckusic fragextend(ip, cg, bprev, osize, nsize)
4945965Smckusic 	struct inode *ip;
4954426Smckusic 	int cg;
4964463Smckusic 	long bprev;
4974426Smckusic 	int osize, nsize;
4984426Smckusic {
4995965Smckusic 	register struct fs *fs;
5004463Smckusic 	register struct cg *cgp;
50137735Smckusick 	struct buf *bp;
5024463Smckusic 	long bno;
5034463Smckusic 	int frags, bbase;
50437735Smckusick 	int i, error;
5054426Smckusic 
5065965Smckusic 	fs = ip->i_fs;
50717224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
5086531Smckusick 		return (NULL);
5095960Smckusic 	frags = numfrags(fs, nsize);
51017224Smckusick 	bbase = fragnum(fs, bprev);
51117224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
51230749Skarels 		/* cannot extend across a block boundary */
5136294Smckusick 		return (NULL);
5144463Smckusic 	}
51537735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
51638776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
51737735Smckusick 	if (error) {
51837735Smckusick 		brelse(bp);
51937735Smckusick 		return (NULL);
52037735Smckusick 	}
5216531Smckusick 	cgp = bp->b_un.b_cg;
52237735Smckusick 	if (!cg_chkmagic(cgp)) {
5235960Smckusic 		brelse(bp);
5246294Smckusick 		return (NULL);
5255960Smckusic 	}
5268105Sroot 	cgp->cg_time = time.tv_sec;
5275377Smckusic 	bno = dtogd(fs, bprev);
5285960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
52934143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
5305361Smckusic 			brelse(bp);
5316294Smckusick 			return (NULL);
5325361Smckusic 		}
5335361Smckusic 	/*
5345361Smckusic 	 * the current fragment can be extended
5355361Smckusic 	 * deduct the count on fragment being extended into
5365361Smckusic 	 * increase the count on the remaining fragment (if any)
5375361Smckusic 	 * allocate the extended piece
5385361Smckusic 	 */
5395361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
54034143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
5414463Smckusic 			break;
5425960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5435361Smckusic 	if (i != frags)
5445361Smckusic 		cgp->cg_frsum[i - frags]++;
5455960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
54634143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
5475361Smckusic 		cgp->cg_cs.cs_nffree--;
5485361Smckusic 		fs->fs_cstotal.cs_nffree--;
5495361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5504463Smckusic 	}
5515361Smckusic 	fs->fs_fmod++;
5525361Smckusic 	bdwrite(bp);
5535361Smckusic 	return (bprev);
5544426Smckusic }
5554426Smckusic 
5565375Smckusic /*
5575375Smckusic  * Determine whether a block can be allocated.
5585375Smckusic  *
5595375Smckusic  * Check to see if a block of the apprpriate size is available,
5605375Smckusic  * and if it is, allocate it.
5615375Smckusic  */
5629163Ssam daddr_t
5635965Smckusic alloccg(ip, cg, bpref, size)
5645965Smckusic 	struct inode *ip;
5654359Smckusick 	int cg;
5664359Smckusick 	daddr_t bpref;
5674359Smckusick 	int size;
5684359Smckusick {
5695965Smckusic 	register struct fs *fs;
5704463Smckusic 	register struct cg *cgp;
57137735Smckusick 	struct buf *bp;
5724463Smckusic 	register int i;
57337735Smckusick 	int error, bno, frags, allocsiz;
5744359Smckusick 
5755965Smckusic 	fs = ip->i_fs;
5765322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5776294Smckusick 		return (NULL);
57837735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
57938776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
58037735Smckusick 	if (error) {
58137735Smckusick 		brelse(bp);
58237735Smckusick 		return (NULL);
58337735Smckusick 	}
5846531Smckusick 	cgp = bp->b_un.b_cg;
58537735Smckusick 	if (!cg_chkmagic(cgp) ||
58615950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5875960Smckusic 		brelse(bp);
5886294Smckusick 		return (NULL);
5895960Smckusic 	}
5908105Sroot 	cgp->cg_time = time.tv_sec;
5915322Smckusic 	if (size == fs->fs_bsize) {
5925212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5934463Smckusic 		bdwrite(bp);
5944463Smckusic 		return (bno);
5954463Smckusic 	}
5964463Smckusic 	/*
5974463Smckusic 	 * check to see if any fragments are already available
5984463Smckusic 	 * allocsiz is the size which will be allocated, hacking
5994463Smckusic 	 * it down to a smaller size if necessary
6004463Smckusic 	 */
6015960Smckusic 	frags = numfrags(fs, size);
6025322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
6034463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
6044463Smckusic 			break;
6055322Smckusic 	if (allocsiz == fs->fs_frag) {
6064463Smckusic 		/*
6074463Smckusic 		 * no fragments were available, so a block will be
6084463Smckusic 		 * allocated, and hacked up
6094463Smckusic 		 */
6104792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
6114463Smckusic 			brelse(bp);
6126294Smckusick 			return (NULL);
6134463Smckusic 		}
6145212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
6155377Smckusic 		bpref = dtogd(fs, bno);
6165322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
61734143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
6185322Smckusic 		i = fs->fs_frag - frags;
6194792Smckusic 		cgp->cg_cs.cs_nffree += i;
6204792Smckusic 		fs->fs_cstotal.cs_nffree += i;
6215322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
6229762Ssam 		fs->fs_fmod++;
6234463Smckusic 		cgp->cg_frsum[i]++;
6244463Smckusic 		bdwrite(bp);
6254463Smckusic 		return (bno);
6264463Smckusic 	}
6274651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
62815950Smckusick 	if (bno < 0) {
62915950Smckusick 		brelse(bp);
6306294Smckusick 		return (NULL);
63115950Smckusick 	}
6324463Smckusic 	for (i = 0; i < frags; i++)
63334143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
6344792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6354792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6365322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
6379762Ssam 	fs->fs_fmod++;
6384463Smckusic 	cgp->cg_frsum[allocsiz]--;
6394463Smckusic 	if (frags != allocsiz)
6404463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6414463Smckusic 	bdwrite(bp);
6424463Smckusic 	return (cg * fs->fs_fpg + bno);
6434463Smckusic }
6444463Smckusic 
6455375Smckusic /*
6465375Smckusic  * Allocate a block in a cylinder group.
6475375Smckusic  *
6485375Smckusic  * This algorithm implements the following policy:
6495375Smckusic  *   1) allocate the requested block.
6505375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6515375Smckusic  *   3) allocate the next available block on the block rotor for the
6525375Smckusic  *      specified cylinder group.
6535375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6545375Smckusic  * blocks may be fragmented by the routine that allocates them.
6555375Smckusic  */
6564463Smckusic daddr_t
6575212Smckusic alloccgblk(fs, cgp, bpref)
6585965Smckusic 	register struct fs *fs;
6594463Smckusic 	register struct cg *cgp;
6604463Smckusic 	daddr_t bpref;
6614463Smckusic {
6624651Smckusic 	daddr_t bno;
6636294Smckusick 	int cylno, pos, delta;
6644651Smckusic 	short *cylbp;
6655361Smckusic 	register int i;
6664463Smckusic 
6674651Smckusic 	if (bpref == 0) {
6684651Smckusic 		bpref = cgp->cg_rotor;
6695361Smckusic 		goto norot;
6705361Smckusic 	}
67117224Smckusick 	bpref = blknum(fs, bpref);
6725377Smckusic 	bpref = dtogd(fs, bpref);
6735361Smckusic 	/*
6745361Smckusic 	 * if the requested block is available, use it
6755361Smckusic 	 */
67634143Smckusick 	if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
6775361Smckusic 		bno = bpref;
6785361Smckusic 		goto gotit;
6795361Smckusic 	}
6805361Smckusic 	/*
6815361Smckusic 	 * check for a block available on the same cylinder
6825361Smckusic 	 */
6835361Smckusic 	cylno = cbtocylno(fs, bpref);
68434143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
6855375Smckusic 		goto norot;
6865375Smckusic 	if (fs->fs_cpc == 0) {
6875375Smckusic 		/*
6885375Smckusic 		 * block layout info is not available, so just have
6895375Smckusic 		 * to take any block in this cylinder.
6905375Smckusic 		 */
6915375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
6925375Smckusic 		goto norot;
6935375Smckusic 	}
6945375Smckusic 	/*
6955361Smckusic 	 * check the summary information to see if a block is
6965361Smckusic 	 * available in the requested cylinder starting at the
6979163Ssam 	 * requested rotational position and proceeding around.
6985361Smckusic 	 */
69934143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
7009163Ssam 	pos = cbtorpos(fs, bpref);
70134143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
7025361Smckusic 		if (cylbp[i] > 0)
7035361Smckusic 			break;
70434143Smckusick 	if (i == fs->fs_nrpos)
7055361Smckusic 		for (i = 0; i < pos; i++)
7065361Smckusic 			if (cylbp[i] > 0)
7075361Smckusic 				break;
7085361Smckusic 	if (cylbp[i] > 0) {
7094651Smckusic 		/*
7105361Smckusic 		 * found a rotational position, now find the actual
7115361Smckusic 		 * block. A panic if none is actually there.
7124651Smckusic 		 */
7135361Smckusic 		pos = cylno % fs->fs_cpc;
7145361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
71534143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
7166716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
7176716Smckusick 			    pos, i, fs->fs_fsmnt);
7185361Smckusic 			panic("alloccgblk: cyl groups corrupted");
7196716Smckusick 		}
72034143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
72134143Smckusick 			if (isblock(fs, cg_blksfree(cgp), bno + i)) {
72211638Ssam 				bno = blkstofrags(fs, (bno + i));
7235361Smckusic 				goto gotit;
7245361Smckusic 			}
72534143Smckusick 			delta = fs_rotbl(fs)[i];
72634143Smckusick 			if (delta <= 0 ||
72734143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
7284651Smckusic 				break;
7296294Smckusick 			i += delta;
7304651Smckusic 		}
7316716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
7325361Smckusic 		panic("alloccgblk: can't find blk in cyl");
7334359Smckusick 	}
7345361Smckusic norot:
7355361Smckusic 	/*
7365361Smckusic 	 * no blocks in the requested cylinder, so take next
7375361Smckusic 	 * available one in this cylinder group.
7385361Smckusic 	 */
7398628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7406567Smckusic 	if (bno < 0)
7416294Smckusick 		return (NULL);
7424651Smckusic 	cgp->cg_rotor = bno;
7434359Smckusick gotit:
74434143Smckusick 	clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno));
7454792Smckusic 	cgp->cg_cs.cs_nbfree--;
7464792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7475322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7485375Smckusic 	cylno = cbtocylno(fs, bno);
74934143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
75034143Smckusick 	cg_blktot(cgp)[cylno]--;
7514359Smckusick 	fs->fs_fmod++;
7524651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7534359Smckusick }
75434143Smckusick 
7555375Smckusic /*
7565375Smckusic  * Determine whether an inode can be allocated.
7575375Smckusic  *
7585375Smckusic  * Check to see if an inode is available, and if it is,
7595375Smckusic  * allocate it using the following policy:
7605375Smckusic  *   1) allocate the requested inode.
7615375Smckusic  *   2) allocate the next available inode after the requested
7625375Smckusic  *      inode in the specified cylinder group.
7635375Smckusic  */
7649163Ssam ino_t
7655965Smckusic ialloccg(ip, cg, ipref, mode)
7665965Smckusic 	struct inode *ip;
7674359Smckusick 	int cg;
7684359Smckusick 	daddr_t ipref;
7694359Smckusick 	int mode;
7704359Smckusick {
7715965Smckusic 	register struct fs *fs;
7724463Smckusic 	register struct cg *cgp;
77316784Smckusick 	struct buf *bp;
77437735Smckusick 	int error, start, len, loc, map, i;
7754359Smckusick 
7765965Smckusic 	fs = ip->i_fs;
7775322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7786294Smckusick 		return (NULL);
77937735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
78038776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
78137735Smckusick 	if (error) {
78237735Smckusick 		brelse(bp);
78337735Smckusick 		return (NULL);
78437735Smckusick 	}
7856531Smckusick 	cgp = bp->b_un.b_cg;
78637735Smckusick 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
7875960Smckusic 		brelse(bp);
7886294Smckusick 		return (NULL);
7895960Smckusic 	}
7908105Sroot 	cgp->cg_time = time.tv_sec;
7914359Smckusick 	if (ipref) {
7924359Smckusick 		ipref %= fs->fs_ipg;
79334143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
7944359Smckusick 			goto gotit;
79516784Smckusick 	}
79616784Smckusick 	start = cgp->cg_irotor / NBBY;
79716784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
79834143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
79916784Smckusick 	if (loc == 0) {
80017697Smckusick 		len = start + 1;
80117697Smckusick 		start = 0;
80234143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
80317697Smckusick 		if (loc == 0) {
80417697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
80517697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
80617697Smckusick 			panic("ialloccg: map corrupted");
80717697Smckusick 			/* NOTREACHED */
80817697Smckusick 		}
80916784Smckusick 	}
81016784Smckusick 	i = start + len - loc;
81134143Smckusick 	map = cg_inosused(cgp)[i];
81216784Smckusick 	ipref = i * NBBY;
81316784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
81416784Smckusick 		if ((map & i) == 0) {
8154359Smckusick 			cgp->cg_irotor = ipref;
8164359Smckusick 			goto gotit;
8174359Smckusick 		}
8184359Smckusick 	}
81916784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
82016784Smckusick 	panic("ialloccg: block not in map");
82116784Smckusick 	/* NOTREACHED */
8224359Smckusick gotit:
82334143Smckusick 	setbit(cg_inosused(cgp), ipref);
8244792Smckusic 	cgp->cg_cs.cs_nifree--;
8254792Smckusic 	fs->fs_cstotal.cs_nifree--;
8265322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
8274359Smckusick 	fs->fs_fmod++;
8284359Smckusick 	if ((mode & IFMT) == IFDIR) {
8294792Smckusic 		cgp->cg_cs.cs_ndir++;
8304792Smckusic 		fs->fs_cstotal.cs_ndir++;
8315322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8324359Smckusick 	}
8334359Smckusick 	bdwrite(bp);
8344359Smckusick 	return (cg * fs->fs_ipg + ipref);
8354359Smckusick }
8364359Smckusick 
8375375Smckusic /*
8385375Smckusic  * Free a block or fragment.
8395375Smckusic  *
8405375Smckusic  * The specified block or fragment is placed back in the
8415375Smckusic  * free map. If a fragment is deallocated, a possible
8425375Smckusic  * block reassembly is checked.
8435375Smckusic  */
84431402Smckusick blkfree(ip, bno, size)
8455965Smckusic 	register struct inode *ip;
8464359Smckusick 	daddr_t bno;
8475212Smckusic 	off_t size;
8484359Smckusick {
8494359Smckusick 	register struct fs *fs;
8504359Smckusick 	register struct cg *cgp;
85137735Smckusick 	struct buf *bp;
85237735Smckusick 	int error, cg, blk, frags, bbase;
8534463Smckusic 	register int i;
85447571Skarels 	struct ucred *cred = curproc->p_ucred;	/* XXX */
8554359Smckusick 
8565965Smckusic 	fs = ip->i_fs;
8576716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8586716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8596716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
86031402Smckusick 		panic("blkfree: bad size");
8616716Smckusick 	}
8625377Smckusic 	cg = dtog(fs, bno);
86342318Smckusick 	if ((unsigned)bno >= fs->fs_size) {
8646567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
86542318Smckusick 		fserr(fs, cred->cr_uid, "bad block");
8664359Smckusick 		return;
8676567Smckusic 	}
86837735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
86938776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
87037735Smckusick 	if (error) {
87137735Smckusick 		brelse(bp);
87237735Smckusick 		return;
87337735Smckusick 	}
8746531Smckusick 	cgp = bp->b_un.b_cg;
87537735Smckusick 	if (!cg_chkmagic(cgp)) {
8765960Smckusic 		brelse(bp);
8774359Smckusick 		return;
8785960Smckusic 	}
8798105Sroot 	cgp->cg_time = time.tv_sec;
8805377Smckusic 	bno = dtogd(fs, bno);
8815322Smckusic 	if (size == fs->fs_bsize) {
88234143Smckusick 		if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) {
8836716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
8846716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
88531402Smckusick 			panic("blkfree: freeing free block");
8866567Smckusic 		}
88734143Smckusick 		setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
8884792Smckusic 		cgp->cg_cs.cs_nbfree++;
8894792Smckusic 		fs->fs_cstotal.cs_nbfree++;
8905322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
8915375Smckusic 		i = cbtocylno(fs, bno);
89234143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
89334143Smckusick 		cg_blktot(cgp)[i]++;
8944426Smckusic 	} else {
89517224Smckusick 		bbase = bno - fragnum(fs, bno);
8964463Smckusic 		/*
8974463Smckusic 		 * decrement the counts associated with the old frags
8984463Smckusic 		 */
89934143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
9005322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
9014463Smckusic 		/*
9024463Smckusic 		 * deallocate the fragment
9034463Smckusic 		 */
9045960Smckusic 		frags = numfrags(fs, size);
9054463Smckusic 		for (i = 0; i < frags; i++) {
90634143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
9076716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
9086716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
90931402Smckusick 				panic("blkfree: freeing free frag");
9106716Smckusick 			}
91134143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
9124426Smckusic 		}
9136294Smckusick 		cgp->cg_cs.cs_nffree += i;
9146294Smckusick 		fs->fs_cstotal.cs_nffree += i;
9156294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
9164463Smckusic 		/*
9174463Smckusic 		 * add back in counts associated with the new frags
9184463Smckusic 		 */
91934143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
9205322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
9214463Smckusic 		/*
9224463Smckusic 		 * if a complete block has been reassembled, account for it
9234463Smckusic 		 */
92434476Smckusick 		if (isblock(fs, cg_blksfree(cgp),
92534476Smckusick 		    (daddr_t)fragstoblks(fs, bbase))) {
9265322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
9275322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
9285322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
9294792Smckusic 			cgp->cg_cs.cs_nbfree++;
9304792Smckusic 			fs->fs_cstotal.cs_nbfree++;
9315322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
9325375Smckusic 			i = cbtocylno(fs, bbase);
93334143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
93434143Smckusick 			cg_blktot(cgp)[i]++;
9354426Smckusic 		}
9364426Smckusic 	}
9374359Smckusick 	fs->fs_fmod++;
9384359Smckusick 	bdwrite(bp);
9394359Smckusick }
9404359Smckusick 
9415375Smckusic /*
9425375Smckusic  * Free an inode.
9435375Smckusic  *
9445375Smckusic  * The specified inode is placed back in the free map.
9455375Smckusic  */
9465965Smckusic ifree(ip, ino, mode)
9475965Smckusic 	struct inode *ip;
9484359Smckusick 	ino_t ino;
9494359Smckusick 	int mode;
9504359Smckusick {
9514359Smckusick 	register struct fs *fs;
9524359Smckusick 	register struct cg *cgp;
95337735Smckusick 	struct buf *bp;
95437735Smckusick 	int error, cg;
9554359Smckusick 
9565965Smckusic 	fs = ip->i_fs;
9576716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
9586716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9596716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9604359Smckusick 		panic("ifree: range");
9616716Smckusick 	}
9625377Smckusic 	cg = itog(fs, ino);
96337735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
96438776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
96537735Smckusick 	if (error) {
96637735Smckusick 		brelse(bp);
96737735Smckusick 		return;
96837735Smckusick 	}
9696531Smckusick 	cgp = bp->b_un.b_cg;
97037735Smckusick 	if (!cg_chkmagic(cgp)) {
9715960Smckusic 		brelse(bp);
9724359Smckusick 		return;
9735960Smckusic 	}
9748105Sroot 	cgp->cg_time = time.tv_sec;
9754359Smckusick 	ino %= fs->fs_ipg;
97634143Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
9776716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9786716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
97948057Smckusick 		if (fs->fs_ronly == 0)
98048057Smckusick 			panic("ifree: freeing free inode");
9816716Smckusick 	}
98234143Smckusick 	clrbit(cg_inosused(cgp), ino);
98316784Smckusick 	if (ino < cgp->cg_irotor)
98416784Smckusick 		cgp->cg_irotor = ino;
9854792Smckusic 	cgp->cg_cs.cs_nifree++;
9864792Smckusic 	fs->fs_cstotal.cs_nifree++;
9875322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
9884359Smckusick 	if ((mode & IFMT) == IFDIR) {
9894792Smckusic 		cgp->cg_cs.cs_ndir--;
9904792Smckusic 		fs->fs_cstotal.cs_ndir--;
9915322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
9924359Smckusick 	}
9934359Smckusick 	fs->fs_fmod++;
9944359Smckusick 	bdwrite(bp);
9954359Smckusick }
9964359Smckusick 
9974463Smckusic /*
9985375Smckusic  * Find a block of the specified size in the specified cylinder group.
9995375Smckusic  *
10004651Smckusic  * It is a panic if a request is made to find a block if none are
10014651Smckusic  * available.
10024651Smckusic  */
10034651Smckusic daddr_t
10044651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
10054651Smckusic 	register struct fs *fs;
10064651Smckusic 	register struct cg *cgp;
10074651Smckusic 	daddr_t bpref;
10084651Smckusic 	int allocsiz;
10094651Smckusic {
10104651Smckusic 	daddr_t bno;
10114651Smckusic 	int start, len, loc, i;
10124651Smckusic 	int blk, field, subfield, pos;
10134651Smckusic 
10144651Smckusic 	/*
10154651Smckusic 	 * find the fragment by searching through the free block
10164651Smckusic 	 * map for an appropriate bit pattern
10174651Smckusic 	 */
10184651Smckusic 	if (bpref)
10195377Smckusic 		start = dtogd(fs, bpref) / NBBY;
10204651Smckusic 	else
10214651Smckusic 		start = cgp->cg_frotor / NBBY;
10225398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
102334476Smckusick 	loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start],
102434476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
102534476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
10264651Smckusic 	if (loc == 0) {
10276531Smckusick 		len = start + 1;
10286531Smckusick 		start = 0;
102934476Smckusick 		loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0],
103034476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
103134476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
103216784Smckusick 		if (loc == 0) {
103316784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
103416784Smckusick 			    start, len, fs->fs_fsmnt);
103516784Smckusick 			panic("alloccg: map corrupted");
103617697Smckusick 			/* NOTREACHED */
103716784Smckusick 		}
10384651Smckusic 	}
10394651Smckusic 	bno = (start + len - loc) * NBBY;
10404651Smckusic 	cgp->cg_frotor = bno;
10414651Smckusic 	/*
10424651Smckusic 	 * found the byte in the map
10434651Smckusic 	 * sift through the bits to find the selected frag
10444651Smckusic 	 */
10456294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
104634143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
10474651Smckusic 		blk <<= 1;
10484651Smckusic 		field = around[allocsiz];
10494651Smckusic 		subfield = inside[allocsiz];
10505322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10516294Smckusick 			if ((blk & field) == subfield)
10526294Smckusick 				return (bno + pos);
10534651Smckusic 			field <<= 1;
10544651Smckusic 			subfield <<= 1;
10554651Smckusic 		}
10564651Smckusic 	}
10576716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
10584651Smckusic 	panic("alloccg: block not in map");
10596531Smckusick 	return (-1);
10604651Smckusic }
10614651Smckusic 
10624651Smckusic /*
10635375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10645375Smckusic  *
10655375Smckusic  * The form of the error message is:
10664359Smckusick  *	fs: error message
10674359Smckusick  */
106842318Smckusick fserr(fs, uid, cp)
10694359Smckusick 	struct fs *fs;
107042318Smckusick 	uid_t uid;
10714359Smckusick 	char *cp;
10724359Smckusick {
10734359Smckusick 
107442318Smckusick 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
10754359Smckusick }
1076