xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 47571)
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*47571Skarels  *	@(#)lfs_alloc.c	7.24 (Berkeley) 03/19/91
823394Smckusick  */
94359Smckusick 
1017097Sbloom #include "param.h"
1117097Sbloom #include "systm.h"
1217097Sbloom #include "buf.h"
13*47571Skarels #include "proc.h"
1437735Smckusick #include "vnode.h"
1517097Sbloom #include "kernel.h"
1618307Sralph #include "syslog.h"
174359Smckusick 
18*47571Skarels #include "quota.h"
19*47571Skarels #include "inode.h"
20*47571Skarels #include "fs.h"
21*47571Skarels 
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;
61*47571Skarels 	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;
123*47571Skarels 	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
15345112Smckusick 	allocbuf(bp, nsize);
15439678Smckusick 	bp->b_flags |= B_DONE;
15539678Smckusick 	bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
15639678Smckusick 	/*
15739678Smckusick 	 * Check for extension in the existing location.
15839678Smckusick 	 */
1596294Smckusick 	cg = dtog(fs, bprev);
16039678Smckusick 	if (bno = fragextend(ip, cg, (long)bprev, osize, nsize)) {
16139887Smckusick 		if (bp->b_blkno != fsbtodb(fs, bno))
16239678Smckusick 			panic("bad blockno");
16339762Smckusick 		ip->i_blocks += btodb(nsize - osize);
16439762Smckusick 		ip->i_flag |= IUPD|ICHG;
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;
22737735Smckusick 		*bpp = bp;
22837735Smckusick 		return (0);
2294463Smckusic 	}
23045173Smckusick #ifdef QUOTA
23145173Smckusick 	/*
23245173Smckusick 	 * Restore user's disk quota because allocation failed.
23345173Smckusick 	 */
23445173Smckusick 	(void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
23545173Smckusick #endif
23639678Smckusick 	brelse(bp);
2374792Smckusic nospace:
2384463Smckusic 	/*
2394463Smckusic 	 * no space available
2404463Smckusic 	 */
24142318Smckusick 	fserr(fs, cred->cr_uid, "file system full");
2424426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
24337735Smckusick 	return (ENOSPC);
2444426Smckusic }
2454426Smckusic 
2465375Smckusic /*
2475375Smckusic  * Allocate an inode in the file system.
2485375Smckusic  *
2495375Smckusic  * A preference may be optionally specified. If a preference is given
2505375Smckusic  * the following hierarchy is used to allocate an inode:
2515375Smckusic  *   1) allocate the requested inode.
2525375Smckusic  *   2) allocate an inode in the same cylinder group.
2535375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2545375Smckusic  *      available inode is located.
2555375Smckusic  * If no inode preference is given the following heirarchy is used
2565375Smckusic  * to allocate an inode:
2575375Smckusic  *   1) allocate an inode in cylinder group 0.
2585375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2595375Smckusic  *      available inode is located.
2605375Smckusic  */
26141309Smckusick ialloc(pip, ipref, mode, cred, ipp)
2625965Smckusic 	register struct inode *pip;
2634359Smckusick 	ino_t ipref;
2644359Smckusick 	int mode;
26541309Smckusick 	struct ucred *cred;
26637735Smckusick 	struct inode **ipp;
2674359Smckusick {
2685212Smckusic 	ino_t ino;
2694359Smckusick 	register struct fs *fs;
2704359Smckusick 	register struct inode *ip;
27137735Smckusick 	int cg, error;
2724359Smckusick 
27337735Smckusick 	*ipp = 0;
2745965Smckusic 	fs = pip->i_fs;
2754792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2764359Smckusick 		goto noinodes;
2774948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2784948Smckusic 		ipref = 0;
2795377Smckusic 	cg = itog(fs, ipref);
2805965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2814359Smckusick 	if (ino == 0)
2824359Smckusick 		goto noinodes;
28337735Smckusick 	error = iget(pip, ino, ipp);
28437735Smckusick 	if (error) {
28541309Smckusick 		ifree(pip, ino, mode);
28637735Smckusick 		return (error);
2874359Smckusick 	}
28838255Smckusick 	ip = *ipp;
2896716Smckusick 	if (ip->i_mode) {
2906716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
2916716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2924359Smckusick 		panic("ialloc: dup alloc");
2936716Smckusick 	}
29412643Ssam 	if (ip->i_blocks) {				/* XXX */
29512643Ssam 		printf("free inode %s/%d had %d blocks\n",
29612643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
29712643Ssam 		ip->i_blocks = 0;
29812643Ssam 	}
29939516Smckusick 	ip->i_flags = 0;
30038255Smckusick 	/*
30138255Smckusick 	 * Set up a new generation number for this inode.
30238255Smckusick 	 */
30338255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
30438255Smckusick 		nextgennumber = time.tv_sec;
30538255Smckusick 	ip->i_gen = nextgennumber;
30637735Smckusick 	return (0);
3074359Smckusick noinodes:
30842318Smckusick 	fserr(fs, cred->cr_uid, "out of inodes");
3096294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
31037735Smckusick 	return (ENOSPC);
3114359Smckusick }
3124359Smckusick 
3134651Smckusic /*
3145375Smckusic  * Find a cylinder to place a directory.
3155375Smckusic  *
3165375Smckusic  * The policy implemented by this algorithm is to select from
3175375Smckusic  * among those cylinder groups with above the average number of
3185375Smckusic  * free inodes, the one with the smallest number of directories.
3194651Smckusic  */
3209163Ssam ino_t
3215965Smckusic dirpref(fs)
3225965Smckusic 	register struct fs *fs;
3234359Smckusick {
3244651Smckusic 	int cg, minndir, mincg, avgifree;
3254359Smckusick 
3264792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3274651Smckusic 	minndir = fs->fs_ipg;
3284359Smckusick 	mincg = 0;
3294651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3305322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3315322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3324359Smckusick 			mincg = cg;
3335322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3344359Smckusick 		}
3359163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3364359Smckusick }
3374359Smckusick 
3384651Smckusic /*
3399163Ssam  * Select the desired position for the next block in a file.  The file is
3409163Ssam  * logically divided into sections. The first section is composed of the
3419163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3429163Ssam  *
3439163Ssam  * If no blocks have been allocated in the first section, the policy is to
3449163Ssam  * request a block in the same cylinder group as the inode that describes
3459163Ssam  * the file. If no blocks have been allocated in any other section, the
3469163Ssam  * policy is to place the section in a cylinder group with a greater than
3479163Ssam  * average number of free blocks.  An appropriate cylinder group is found
34817696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
34917696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
35017696Smckusick  * cylinder group from which the previous allocation was made. The sweep
35117696Smckusick  * continues until a cylinder group with greater than the average number
35217696Smckusick  * of free blocks is found. If the allocation is for the first block in an
35317696Smckusick  * indirect block, the information on the previous allocation is unavailable;
35417696Smckusick  * here a best guess is made based upon the logical block number being
35517696Smckusick  * allocated.
3569163Ssam  *
3579163Ssam  * If a section is already partially allocated, the policy is to
3589163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3599163Ssam  * contiguous blocks and the beginning of the next is physically separated
3609163Ssam  * so that the disk head will be in transit between them for at least
3619163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3629163Ssam  * schedule another I/O transfer.
3634651Smckusic  */
3645212Smckusic daddr_t
3659163Ssam blkpref(ip, lbn, indx, bap)
3669163Ssam 	struct inode *ip;
3679163Ssam 	daddr_t lbn;
3689163Ssam 	int indx;
3699163Ssam 	daddr_t *bap;
3709163Ssam {
3715965Smckusic 	register struct fs *fs;
37217696Smckusick 	register int cg;
37317696Smckusick 	int avgbfree, startcg;
3749163Ssam 	daddr_t nextblk;
3754651Smckusic 
3769163Ssam 	fs = ip->i_fs;
3779163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3789163Ssam 		if (lbn < NDADDR) {
3799163Ssam 			cg = itog(fs, ip->i_number);
3805322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3814651Smckusic 		}
3829163Ssam 		/*
3839163Ssam 		 * Find a cylinder with greater than average number of
3849163Ssam 		 * unused data blocks.
3859163Ssam 		 */
38617696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
38717696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
38817696Smckusick 		else
38917696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
39017696Smckusick 		startcg %= fs->fs_ncg;
3919163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
39217696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
3939163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3949163Ssam 				fs->fs_cgrotor = cg;
3959163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3969163Ssam 			}
39717696Smckusick 		for (cg = 0; cg <= startcg; cg++)
3989163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3999163Ssam 				fs->fs_cgrotor = cg;
4009163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4019163Ssam 			}
4029163Ssam 		return (NULL);
4039163Ssam 	}
4049163Ssam 	/*
4059163Ssam 	 * One or more previous blocks have been laid out. If less
4069163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
4079163Ssam 	 * next block is requested contiguously, otherwise it is
4089163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
4099163Ssam 	 */
4109163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
4119163Ssam 	if (indx > fs->fs_maxcontig &&
41211638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4139163Ssam 	    != nextblk)
4149163Ssam 		return (nextblk);
4159163Ssam 	if (fs->fs_rotdelay != 0)
4169163Ssam 		/*
4179163Ssam 		 * Here we convert ms of delay to frags as:
4189163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4199163Ssam 		 *	((sect/frag) * (ms/sec))
4209163Ssam 		 * then round up to the next block.
4219163Ssam 		 */
4229163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4239163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4249163Ssam 	return (nextblk);
4254651Smckusic }
4264651Smckusic 
4275375Smckusic /*
4285375Smckusic  * Implement the cylinder overflow algorithm.
4295375Smckusic  *
4305375Smckusic  * The policy implemented by this algorithm is:
4315375Smckusic  *   1) allocate the block in its requested cylinder group.
4325375Smckusic  *   2) quadradically rehash on the cylinder group number.
4335375Smckusic  *   3) brute force search for a free block.
4345375Smckusic  */
4355212Smckusic /*VARARGS5*/
4365212Smckusic u_long
4375965Smckusic hashalloc(ip, cg, pref, size, allocator)
4385965Smckusic 	struct inode *ip;
4394359Smckusick 	int cg;
4404359Smckusick 	long pref;
4414359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4425212Smckusic 	u_long (*allocator)();
4434359Smckusick {
4445965Smckusic 	register struct fs *fs;
4454359Smckusick 	long result;
4464359Smckusick 	int i, icg = cg;
4474359Smckusick 
4485965Smckusic 	fs = ip->i_fs;
4494359Smckusick 	/*
4504359Smckusick 	 * 1: preferred cylinder group
4514359Smckusick 	 */
4525965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4534359Smckusick 	if (result)
4544359Smckusick 		return (result);
4554359Smckusick 	/*
4564359Smckusick 	 * 2: quadratic rehash
4574359Smckusick 	 */
4584359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4594359Smckusick 		cg += i;
4604359Smckusick 		if (cg >= fs->fs_ncg)
4614359Smckusick 			cg -= fs->fs_ncg;
4625965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4634359Smckusick 		if (result)
4644359Smckusick 			return (result);
4654359Smckusick 	}
4664359Smckusick 	/*
4674359Smckusick 	 * 3: brute force search
46810847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
46910847Ssam 	 * and 1 is always checked in the quadratic rehash.
4704359Smckusick 	 */
47110848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
47210847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4735965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4744359Smckusick 		if (result)
4754359Smckusick 			return (result);
4764359Smckusick 		cg++;
4774359Smckusick 		if (cg == fs->fs_ncg)
4784359Smckusick 			cg = 0;
4794359Smckusick 	}
4806294Smckusick 	return (NULL);
4814359Smckusick }
4824359Smckusick 
4835375Smckusic /*
4845375Smckusic  * Determine whether a fragment can be extended.
4855375Smckusic  *
4865375Smckusic  * Check to see if the necessary fragments are available, and
4875375Smckusic  * if they are, allocate them.
4885375Smckusic  */
4894359Smckusick daddr_t
4905965Smckusic fragextend(ip, cg, bprev, osize, nsize)
4915965Smckusic 	struct inode *ip;
4924426Smckusic 	int cg;
4934463Smckusic 	long bprev;
4944426Smckusic 	int osize, nsize;
4954426Smckusic {
4965965Smckusic 	register struct fs *fs;
4974463Smckusic 	register struct cg *cgp;
49837735Smckusick 	struct buf *bp;
4994463Smckusic 	long bno;
5004463Smckusic 	int frags, bbase;
50137735Smckusick 	int i, error;
5024426Smckusic 
5035965Smckusic 	fs = ip->i_fs;
50417224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
5056531Smckusick 		return (NULL);
5065960Smckusic 	frags = numfrags(fs, nsize);
50717224Smckusick 	bbase = fragnum(fs, bprev);
50817224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
50930749Skarels 		/* cannot extend across a block boundary */
5106294Smckusick 		return (NULL);
5114463Smckusic 	}
51237735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
51338776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
51437735Smckusick 	if (error) {
51537735Smckusick 		brelse(bp);
51637735Smckusick 		return (NULL);
51737735Smckusick 	}
5186531Smckusick 	cgp = bp->b_un.b_cg;
51937735Smckusick 	if (!cg_chkmagic(cgp)) {
5205960Smckusic 		brelse(bp);
5216294Smckusick 		return (NULL);
5225960Smckusic 	}
5238105Sroot 	cgp->cg_time = time.tv_sec;
5245377Smckusic 	bno = dtogd(fs, bprev);
5255960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
52634143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
5275361Smckusic 			brelse(bp);
5286294Smckusick 			return (NULL);
5295361Smckusic 		}
5305361Smckusic 	/*
5315361Smckusic 	 * the current fragment can be extended
5325361Smckusic 	 * deduct the count on fragment being extended into
5335361Smckusic 	 * increase the count on the remaining fragment (if any)
5345361Smckusic 	 * allocate the extended piece
5355361Smckusic 	 */
5365361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
53734143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
5384463Smckusic 			break;
5395960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5405361Smckusic 	if (i != frags)
5415361Smckusic 		cgp->cg_frsum[i - frags]++;
5425960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
54334143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
5445361Smckusic 		cgp->cg_cs.cs_nffree--;
5455361Smckusic 		fs->fs_cstotal.cs_nffree--;
5465361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5474463Smckusic 	}
5485361Smckusic 	fs->fs_fmod++;
5495361Smckusic 	bdwrite(bp);
5505361Smckusic 	return (bprev);
5514426Smckusic }
5524426Smckusic 
5535375Smckusic /*
5545375Smckusic  * Determine whether a block can be allocated.
5555375Smckusic  *
5565375Smckusic  * Check to see if a block of the apprpriate size is available,
5575375Smckusic  * and if it is, allocate it.
5585375Smckusic  */
5599163Ssam daddr_t
5605965Smckusic alloccg(ip, cg, bpref, size)
5615965Smckusic 	struct inode *ip;
5624359Smckusick 	int cg;
5634359Smckusick 	daddr_t bpref;
5644359Smckusick 	int size;
5654359Smckusick {
5665965Smckusic 	register struct fs *fs;
5674463Smckusic 	register struct cg *cgp;
56837735Smckusick 	struct buf *bp;
5694463Smckusic 	register int i;
57037735Smckusick 	int error, bno, frags, allocsiz;
5714359Smckusick 
5725965Smckusic 	fs = ip->i_fs;
5735322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5746294Smckusick 		return (NULL);
57537735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
57638776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
57737735Smckusick 	if (error) {
57837735Smckusick 		brelse(bp);
57937735Smckusick 		return (NULL);
58037735Smckusick 	}
5816531Smckusick 	cgp = bp->b_un.b_cg;
58237735Smckusick 	if (!cg_chkmagic(cgp) ||
58315950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5845960Smckusic 		brelse(bp);
5856294Smckusick 		return (NULL);
5865960Smckusic 	}
5878105Sroot 	cgp->cg_time = time.tv_sec;
5885322Smckusic 	if (size == fs->fs_bsize) {
5895212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5904463Smckusic 		bdwrite(bp);
5914463Smckusic 		return (bno);
5924463Smckusic 	}
5934463Smckusic 	/*
5944463Smckusic 	 * check to see if any fragments are already available
5954463Smckusic 	 * allocsiz is the size which will be allocated, hacking
5964463Smckusic 	 * it down to a smaller size if necessary
5974463Smckusic 	 */
5985960Smckusic 	frags = numfrags(fs, size);
5995322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
6004463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
6014463Smckusic 			break;
6025322Smckusic 	if (allocsiz == fs->fs_frag) {
6034463Smckusic 		/*
6044463Smckusic 		 * no fragments were available, so a block will be
6054463Smckusic 		 * allocated, and hacked up
6064463Smckusic 		 */
6074792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
6084463Smckusic 			brelse(bp);
6096294Smckusick 			return (NULL);
6104463Smckusic 		}
6115212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
6125377Smckusic 		bpref = dtogd(fs, bno);
6135322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
61434143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
6155322Smckusic 		i = fs->fs_frag - frags;
6164792Smckusic 		cgp->cg_cs.cs_nffree += i;
6174792Smckusic 		fs->fs_cstotal.cs_nffree += i;
6185322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
6199762Ssam 		fs->fs_fmod++;
6204463Smckusic 		cgp->cg_frsum[i]++;
6214463Smckusic 		bdwrite(bp);
6224463Smckusic 		return (bno);
6234463Smckusic 	}
6244651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
62515950Smckusick 	if (bno < 0) {
62615950Smckusick 		brelse(bp);
6276294Smckusick 		return (NULL);
62815950Smckusick 	}
6294463Smckusic 	for (i = 0; i < frags; i++)
63034143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
6314792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6324792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6335322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
6349762Ssam 	fs->fs_fmod++;
6354463Smckusic 	cgp->cg_frsum[allocsiz]--;
6364463Smckusic 	if (frags != allocsiz)
6374463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6384463Smckusic 	bdwrite(bp);
6394463Smckusic 	return (cg * fs->fs_fpg + bno);
6404463Smckusic }
6414463Smckusic 
6425375Smckusic /*
6435375Smckusic  * Allocate a block in a cylinder group.
6445375Smckusic  *
6455375Smckusic  * This algorithm implements the following policy:
6465375Smckusic  *   1) allocate the requested block.
6475375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6485375Smckusic  *   3) allocate the next available block on the block rotor for the
6495375Smckusic  *      specified cylinder group.
6505375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6515375Smckusic  * blocks may be fragmented by the routine that allocates them.
6525375Smckusic  */
6534463Smckusic daddr_t
6545212Smckusic alloccgblk(fs, cgp, bpref)
6555965Smckusic 	register struct fs *fs;
6564463Smckusic 	register struct cg *cgp;
6574463Smckusic 	daddr_t bpref;
6584463Smckusic {
6594651Smckusic 	daddr_t bno;
6606294Smckusick 	int cylno, pos, delta;
6614651Smckusic 	short *cylbp;
6625361Smckusic 	register int i;
6634463Smckusic 
6644651Smckusic 	if (bpref == 0) {
6654651Smckusic 		bpref = cgp->cg_rotor;
6665361Smckusic 		goto norot;
6675361Smckusic 	}
66817224Smckusick 	bpref = blknum(fs, bpref);
6695377Smckusic 	bpref = dtogd(fs, bpref);
6705361Smckusic 	/*
6715361Smckusic 	 * if the requested block is available, use it
6725361Smckusic 	 */
67334143Smckusick 	if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
6745361Smckusic 		bno = bpref;
6755361Smckusic 		goto gotit;
6765361Smckusic 	}
6775361Smckusic 	/*
6785361Smckusic 	 * check for a block available on the same cylinder
6795361Smckusic 	 */
6805361Smckusic 	cylno = cbtocylno(fs, bpref);
68134143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
6825375Smckusic 		goto norot;
6835375Smckusic 	if (fs->fs_cpc == 0) {
6845375Smckusic 		/*
6855375Smckusic 		 * block layout info is not available, so just have
6865375Smckusic 		 * to take any block in this cylinder.
6875375Smckusic 		 */
6885375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
6895375Smckusic 		goto norot;
6905375Smckusic 	}
6915375Smckusic 	/*
6925361Smckusic 	 * check the summary information to see if a block is
6935361Smckusic 	 * available in the requested cylinder starting at the
6949163Ssam 	 * requested rotational position and proceeding around.
6955361Smckusic 	 */
69634143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
6979163Ssam 	pos = cbtorpos(fs, bpref);
69834143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
6995361Smckusic 		if (cylbp[i] > 0)
7005361Smckusic 			break;
70134143Smckusick 	if (i == fs->fs_nrpos)
7025361Smckusic 		for (i = 0; i < pos; i++)
7035361Smckusic 			if (cylbp[i] > 0)
7045361Smckusic 				break;
7055361Smckusic 	if (cylbp[i] > 0) {
7064651Smckusic 		/*
7075361Smckusic 		 * found a rotational position, now find the actual
7085361Smckusic 		 * block. A panic if none is actually there.
7094651Smckusic 		 */
7105361Smckusic 		pos = cylno % fs->fs_cpc;
7115361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
71234143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
7136716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
7146716Smckusick 			    pos, i, fs->fs_fsmnt);
7155361Smckusic 			panic("alloccgblk: cyl groups corrupted");
7166716Smckusick 		}
71734143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
71834143Smckusick 			if (isblock(fs, cg_blksfree(cgp), bno + i)) {
71911638Ssam 				bno = blkstofrags(fs, (bno + i));
7205361Smckusic 				goto gotit;
7215361Smckusic 			}
72234143Smckusick 			delta = fs_rotbl(fs)[i];
72334143Smckusick 			if (delta <= 0 ||
72434143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
7254651Smckusic 				break;
7266294Smckusick 			i += delta;
7274651Smckusic 		}
7286716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
7295361Smckusic 		panic("alloccgblk: can't find blk in cyl");
7304359Smckusick 	}
7315361Smckusic norot:
7325361Smckusic 	/*
7335361Smckusic 	 * no blocks in the requested cylinder, so take next
7345361Smckusic 	 * available one in this cylinder group.
7355361Smckusic 	 */
7368628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7376567Smckusic 	if (bno < 0)
7386294Smckusick 		return (NULL);
7394651Smckusic 	cgp->cg_rotor = bno;
7404359Smckusick gotit:
74134143Smckusick 	clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno));
7424792Smckusic 	cgp->cg_cs.cs_nbfree--;
7434792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7445322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7455375Smckusic 	cylno = cbtocylno(fs, bno);
74634143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
74734143Smckusick 	cg_blktot(cgp)[cylno]--;
7484359Smckusick 	fs->fs_fmod++;
7494651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7504359Smckusick }
75134143Smckusick 
7525375Smckusic /*
7535375Smckusic  * Determine whether an inode can be allocated.
7545375Smckusic  *
7555375Smckusic  * Check to see if an inode is available, and if it is,
7565375Smckusic  * allocate it using the following policy:
7575375Smckusic  *   1) allocate the requested inode.
7585375Smckusic  *   2) allocate the next available inode after the requested
7595375Smckusic  *      inode in the specified cylinder group.
7605375Smckusic  */
7619163Ssam ino_t
7625965Smckusic ialloccg(ip, cg, ipref, mode)
7635965Smckusic 	struct inode *ip;
7644359Smckusick 	int cg;
7654359Smckusick 	daddr_t ipref;
7664359Smckusick 	int mode;
7674359Smckusick {
7685965Smckusic 	register struct fs *fs;
7694463Smckusic 	register struct cg *cgp;
77016784Smckusick 	struct buf *bp;
77137735Smckusick 	int error, start, len, loc, map, i;
7724359Smckusick 
7735965Smckusic 	fs = ip->i_fs;
7745322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7756294Smckusick 		return (NULL);
77637735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
77738776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
77837735Smckusick 	if (error) {
77937735Smckusick 		brelse(bp);
78037735Smckusick 		return (NULL);
78137735Smckusick 	}
7826531Smckusick 	cgp = bp->b_un.b_cg;
78337735Smckusick 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
7845960Smckusic 		brelse(bp);
7856294Smckusick 		return (NULL);
7865960Smckusic 	}
7878105Sroot 	cgp->cg_time = time.tv_sec;
7884359Smckusick 	if (ipref) {
7894359Smckusick 		ipref %= fs->fs_ipg;
79034143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
7914359Smckusick 			goto gotit;
79216784Smckusick 	}
79316784Smckusick 	start = cgp->cg_irotor / NBBY;
79416784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
79534143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
79616784Smckusick 	if (loc == 0) {
79717697Smckusick 		len = start + 1;
79817697Smckusick 		start = 0;
79934143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
80017697Smckusick 		if (loc == 0) {
80117697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
80217697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
80317697Smckusick 			panic("ialloccg: map corrupted");
80417697Smckusick 			/* NOTREACHED */
80517697Smckusick 		}
80616784Smckusick 	}
80716784Smckusick 	i = start + len - loc;
80834143Smckusick 	map = cg_inosused(cgp)[i];
80916784Smckusick 	ipref = i * NBBY;
81016784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
81116784Smckusick 		if ((map & i) == 0) {
8124359Smckusick 			cgp->cg_irotor = ipref;
8134359Smckusick 			goto gotit;
8144359Smckusick 		}
8154359Smckusick 	}
81616784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
81716784Smckusick 	panic("ialloccg: block not in map");
81816784Smckusick 	/* NOTREACHED */
8194359Smckusick gotit:
82034143Smckusick 	setbit(cg_inosused(cgp), ipref);
8214792Smckusic 	cgp->cg_cs.cs_nifree--;
8224792Smckusic 	fs->fs_cstotal.cs_nifree--;
8235322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
8244359Smckusick 	fs->fs_fmod++;
8254359Smckusick 	if ((mode & IFMT) == IFDIR) {
8264792Smckusic 		cgp->cg_cs.cs_ndir++;
8274792Smckusic 		fs->fs_cstotal.cs_ndir++;
8285322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8294359Smckusick 	}
8304359Smckusick 	bdwrite(bp);
8314359Smckusick 	return (cg * fs->fs_ipg + ipref);
8324359Smckusick }
8334359Smckusick 
8345375Smckusic /*
8355375Smckusic  * Free a block or fragment.
8365375Smckusic  *
8375375Smckusic  * The specified block or fragment is placed back in the
8385375Smckusic  * free map. If a fragment is deallocated, a possible
8395375Smckusic  * block reassembly is checked.
8405375Smckusic  */
84131402Smckusick blkfree(ip, bno, size)
8425965Smckusic 	register struct inode *ip;
8434359Smckusick 	daddr_t bno;
8445212Smckusic 	off_t size;
8454359Smckusick {
8464359Smckusick 	register struct fs *fs;
8474359Smckusick 	register struct cg *cgp;
84837735Smckusick 	struct buf *bp;
84937735Smckusick 	int error, cg, blk, frags, bbase;
8504463Smckusic 	register int i;
851*47571Skarels 	struct ucred *cred = curproc->p_ucred;	/* XXX */
8524359Smckusick 
8535965Smckusic 	fs = ip->i_fs;
8546716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8556716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8566716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
85731402Smckusick 		panic("blkfree: bad size");
8586716Smckusick 	}
8595377Smckusic 	cg = dtog(fs, bno);
86042318Smckusick 	if ((unsigned)bno >= fs->fs_size) {
8616567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
86242318Smckusick 		fserr(fs, cred->cr_uid, "bad block");
8634359Smckusick 		return;
8646567Smckusic 	}
86537735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
86638776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
86737735Smckusick 	if (error) {
86837735Smckusick 		brelse(bp);
86937735Smckusick 		return;
87037735Smckusick 	}
8716531Smckusick 	cgp = bp->b_un.b_cg;
87237735Smckusick 	if (!cg_chkmagic(cgp)) {
8735960Smckusic 		brelse(bp);
8744359Smckusick 		return;
8755960Smckusic 	}
8768105Sroot 	cgp->cg_time = time.tv_sec;
8775377Smckusic 	bno = dtogd(fs, bno);
8785322Smckusic 	if (size == fs->fs_bsize) {
87934143Smckusick 		if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) {
8806716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
8816716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
88231402Smckusick 			panic("blkfree: freeing free block");
8836567Smckusic 		}
88434143Smckusick 		setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
8854792Smckusic 		cgp->cg_cs.cs_nbfree++;
8864792Smckusic 		fs->fs_cstotal.cs_nbfree++;
8875322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
8885375Smckusic 		i = cbtocylno(fs, bno);
88934143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
89034143Smckusick 		cg_blktot(cgp)[i]++;
8914426Smckusic 	} else {
89217224Smckusick 		bbase = bno - fragnum(fs, bno);
8934463Smckusic 		/*
8944463Smckusic 		 * decrement the counts associated with the old frags
8954463Smckusic 		 */
89634143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
8975322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
8984463Smckusic 		/*
8994463Smckusic 		 * deallocate the fragment
9004463Smckusic 		 */
9015960Smckusic 		frags = numfrags(fs, size);
9024463Smckusic 		for (i = 0; i < frags; i++) {
90334143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
9046716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
9056716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
90631402Smckusick 				panic("blkfree: freeing free frag");
9076716Smckusick 			}
90834143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
9094426Smckusic 		}
9106294Smckusick 		cgp->cg_cs.cs_nffree += i;
9116294Smckusick 		fs->fs_cstotal.cs_nffree += i;
9126294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
9134463Smckusic 		/*
9144463Smckusic 		 * add back in counts associated with the new frags
9154463Smckusic 		 */
91634143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
9175322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
9184463Smckusic 		/*
9194463Smckusic 		 * if a complete block has been reassembled, account for it
9204463Smckusic 		 */
92134476Smckusick 		if (isblock(fs, cg_blksfree(cgp),
92234476Smckusick 		    (daddr_t)fragstoblks(fs, bbase))) {
9235322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
9245322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
9255322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
9264792Smckusic 			cgp->cg_cs.cs_nbfree++;
9274792Smckusic 			fs->fs_cstotal.cs_nbfree++;
9285322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
9295375Smckusic 			i = cbtocylno(fs, bbase);
93034143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
93134143Smckusick 			cg_blktot(cgp)[i]++;
9324426Smckusic 		}
9334426Smckusic 	}
9344359Smckusick 	fs->fs_fmod++;
9354359Smckusick 	bdwrite(bp);
9364359Smckusick }
9374359Smckusick 
9385375Smckusic /*
9395375Smckusic  * Free an inode.
9405375Smckusic  *
9415375Smckusic  * The specified inode is placed back in the free map.
9425375Smckusic  */
9435965Smckusic ifree(ip, ino, mode)
9445965Smckusic 	struct inode *ip;
9454359Smckusick 	ino_t ino;
9464359Smckusick 	int mode;
9474359Smckusick {
9484359Smckusick 	register struct fs *fs;
9494359Smckusick 	register struct cg *cgp;
95037735Smckusick 	struct buf *bp;
95137735Smckusick 	int error, cg;
9524359Smckusick 
9535965Smckusic 	fs = ip->i_fs;
9546716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
9556716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9566716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9574359Smckusick 		panic("ifree: range");
9586716Smckusick 	}
9595377Smckusic 	cg = itog(fs, ino);
96037735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
96138776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
96237735Smckusick 	if (error) {
96337735Smckusick 		brelse(bp);
96437735Smckusick 		return;
96537735Smckusick 	}
9666531Smckusick 	cgp = bp->b_un.b_cg;
96737735Smckusick 	if (!cg_chkmagic(cgp)) {
9685960Smckusic 		brelse(bp);
9694359Smckusick 		return;
9705960Smckusic 	}
9718105Sroot 	cgp->cg_time = time.tv_sec;
9724359Smckusick 	ino %= fs->fs_ipg;
97334143Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
9746716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9756716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9764359Smckusick 		panic("ifree: freeing free inode");
9776716Smckusick 	}
97834143Smckusick 	clrbit(cg_inosused(cgp), ino);
97916784Smckusick 	if (ino < cgp->cg_irotor)
98016784Smckusick 		cgp->cg_irotor = ino;
9814792Smckusic 	cgp->cg_cs.cs_nifree++;
9824792Smckusic 	fs->fs_cstotal.cs_nifree++;
9835322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
9844359Smckusick 	if ((mode & IFMT) == IFDIR) {
9854792Smckusic 		cgp->cg_cs.cs_ndir--;
9864792Smckusic 		fs->fs_cstotal.cs_ndir--;
9875322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
9884359Smckusick 	}
9894359Smckusick 	fs->fs_fmod++;
9904359Smckusick 	bdwrite(bp);
9914359Smckusick }
9924359Smckusick 
9934463Smckusic /*
9945375Smckusic  * Find a block of the specified size in the specified cylinder group.
9955375Smckusic  *
9964651Smckusic  * It is a panic if a request is made to find a block if none are
9974651Smckusic  * available.
9984651Smckusic  */
9994651Smckusic daddr_t
10004651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
10014651Smckusic 	register struct fs *fs;
10024651Smckusic 	register struct cg *cgp;
10034651Smckusic 	daddr_t bpref;
10044651Smckusic 	int allocsiz;
10054651Smckusic {
10064651Smckusic 	daddr_t bno;
10074651Smckusic 	int start, len, loc, i;
10084651Smckusic 	int blk, field, subfield, pos;
10094651Smckusic 
10104651Smckusic 	/*
10114651Smckusic 	 * find the fragment by searching through the free block
10124651Smckusic 	 * map for an appropriate bit pattern
10134651Smckusic 	 */
10144651Smckusic 	if (bpref)
10155377Smckusic 		start = dtogd(fs, bpref) / NBBY;
10164651Smckusic 	else
10174651Smckusic 		start = cgp->cg_frotor / NBBY;
10185398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
101934476Smckusick 	loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start],
102034476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
102134476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
10224651Smckusic 	if (loc == 0) {
10236531Smckusick 		len = start + 1;
10246531Smckusick 		start = 0;
102534476Smckusick 		loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0],
102634476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
102734476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
102816784Smckusick 		if (loc == 0) {
102916784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
103016784Smckusick 			    start, len, fs->fs_fsmnt);
103116784Smckusick 			panic("alloccg: map corrupted");
103217697Smckusick 			/* NOTREACHED */
103316784Smckusick 		}
10344651Smckusic 	}
10354651Smckusic 	bno = (start + len - loc) * NBBY;
10364651Smckusic 	cgp->cg_frotor = bno;
10374651Smckusic 	/*
10384651Smckusic 	 * found the byte in the map
10394651Smckusic 	 * sift through the bits to find the selected frag
10404651Smckusic 	 */
10416294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
104234143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
10434651Smckusic 		blk <<= 1;
10444651Smckusic 		field = around[allocsiz];
10454651Smckusic 		subfield = inside[allocsiz];
10465322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10476294Smckusick 			if ((blk & field) == subfield)
10486294Smckusick 				return (bno + pos);
10494651Smckusic 			field <<= 1;
10504651Smckusic 			subfield <<= 1;
10514651Smckusic 		}
10524651Smckusic 	}
10536716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
10544651Smckusic 	panic("alloccg: block not in map");
10556531Smckusick 	return (-1);
10564651Smckusic }
10574651Smckusic 
10584651Smckusic /*
10595375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10605375Smckusic  *
10615375Smckusic  * The form of the error message is:
10624359Smckusick  *	fs: error message
10634359Smckusick  */
106442318Smckusick fserr(fs, uid, cp)
10654359Smckusick 	struct fs *fs;
106642318Smckusick 	uid_t uid;
10674359Smckusick 	char *cp;
10684359Smckusick {
10694359Smckusick 
107042318Smckusick 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
10714359Smckusick }
1072