xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 44536)
123394Smckusick /*
237735Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
334432Sbostic  * All rights reserved.
423394Smckusick  *
5*44536Sbostic  * %sccs.include.redist.c%
634432Sbostic  *
7*44536Sbostic  *	@(#)lfs_alloc.c	7.20 (Berkeley) 06/28/90
823394Smckusick  */
94359Smckusick 
1017097Sbloom #include "param.h"
1117097Sbloom #include "systm.h"
1217097Sbloom #include "buf.h"
1317097Sbloom #include "user.h"
1437735Smckusick #include "vnode.h"
1517097Sbloom #include "kernel.h"
1618307Sralph #include "syslog.h"
1726253Skarels #include "cmap.h"
1837735Smckusick #include "../ufs/quota.h"
1937735Smckusick #include "../ufs/inode.h"
2037735Smckusick #include "../ufs/fs.h"
214359Smckusick 
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;
6141309Smckusick 	struct ucred *cred = u.u_cred;		/* 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 	}
924359Smckusick nospace:
9342318Smckusick 	fserr(fs, cred->cr_uid, "file system full");
944359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
9537735Smckusick 	return (ENOSPC);
964359Smckusick }
974359Smckusick 
985375Smckusic /*
995375Smckusic  * Reallocate a fragment to a bigger size
1005375Smckusic  *
1015375Smckusic  * The number and size of the old block is given, and a preference
1025375Smckusic  * and new size is also specified. The allocator attempts to extend
1035375Smckusic  * the original block. Failing that, the regular block allocator is
1045375Smckusic  * invoked to get an appropriate block.
1055375Smckusic  */
10639678Smckusick realloccg(ip, lbprev, bpref, osize, nsize, bpp)
1075965Smckusic 	register struct inode *ip;
10839678Smckusick 	off_t lbprev;
10939678Smckusick 	daddr_t bpref;
1104426Smckusic 	int osize, nsize;
11137735Smckusick 	struct buf **bpp;
1124426Smckusic {
1134426Smckusic 	register struct fs *fs;
11437735Smckusick 	struct buf *bp, *obp;
11524698Smckusick 	int cg, request;
11639678Smckusick 	daddr_t bprev, bno, bn;
11737735Smckusick 	int i, error, count;
11841309Smckusick 	struct ucred *cred = u.u_cred;		/* XXX */
1194426Smckusic 
12037735Smckusick 	*bpp = 0;
1215965Smckusic 	fs = ip->i_fs;
1225960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1236716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1246716Smckusick 		printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1256716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
1264463Smckusic 		panic("realloccg: bad size");
1276716Smckusick 	}
12841309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1294792Smckusic 		goto nospace;
13039678Smckusick 	if ((bprev = ip->i_db[lbprev]) == 0) {
1316716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1326716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1334463Smckusic 		panic("realloccg: bad bprev");
1346716Smckusick 	}
1357650Ssam #ifdef QUOTA
13641309Smckusick 	if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0))
13737735Smckusick 		return (error);
1387483Skre #endif
13939678Smckusick 	/*
14039678Smckusick 	 * Allocate the extra space in the buffer.
14139678Smckusick 	 */
14239678Smckusick 	if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
14339678Smckusick 		brelse(bp);
14439678Smckusick 		return (error);
14539678Smckusick 	}
14639678Smckusick 	brealloc(bp, nsize);
14739678Smckusick 	bp->b_flags |= B_DONE;
14839678Smckusick 	bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
14939678Smckusick 	/*
15039678Smckusick 	 * Check for extension in the existing location.
15139678Smckusick 	 */
1526294Smckusick 	cg = dtog(fs, bprev);
15339678Smckusick 	if (bno = fragextend(ip, cg, (long)bprev, osize, nsize)) {
15439887Smckusick 		if (bp->b_blkno != fsbtodb(fs, bno))
15539678Smckusick 			panic("bad blockno");
15639762Smckusick 		ip->i_blocks += btodb(nsize - osize);
15739762Smckusick 		ip->i_flag |= IUPD|ICHG;
15837735Smckusick 		*bpp = bp;
15937735Smckusick 		return (0);
1604463Smckusic 	}
16139678Smckusick 	/*
16239678Smckusick 	 * Allocate a new disk location.
16339678Smckusick 	 */
1644948Smckusic 	if (bpref >= fs->fs_size)
1654948Smckusic 		bpref = 0;
16626253Skarels 	switch ((int)fs->fs_optim) {
16725256Smckusick 	case FS_OPTSPACE:
16825256Smckusick 		/*
16925256Smckusick 		 * Allocate an exact sized fragment. Although this makes
17025256Smckusick 		 * best use of space, we will waste time relocating it if
17125256Smckusick 		 * the file continues to grow. If the fragmentation is
17225256Smckusick 		 * less than half of the minimum free reserve, we choose
17325256Smckusick 		 * to begin optimizing for time.
17425256Smckusick 		 */
17524698Smckusick 		request = nsize;
17625256Smckusick 		if (fs->fs_minfree < 5 ||
17725256Smckusick 		    fs->fs_cstotal.cs_nffree >
17825256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
17925256Smckusick 			break;
18025256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
18125256Smckusick 			fs->fs_fsmnt);
18225256Smckusick 		fs->fs_optim = FS_OPTTIME;
18325256Smckusick 		break;
18425256Smckusick 	case FS_OPTTIME:
18525256Smckusick 		/*
18625256Smckusick 		 * At this point we have discovered a file that is trying
18725256Smckusick 		 * to grow a small fragment to a larger fragment. To save
18825256Smckusick 		 * time, we allocate a full sized block, then free the
18925256Smckusick 		 * unused portion. If the file continues to grow, the
19025256Smckusick 		 * `fragextend' call above will be able to grow it in place
19125256Smckusick 		 * without further copying. If aberrant programs cause
19225256Smckusick 		 * disk fragmentation to grow within 2% of the free reserve,
19325256Smckusick 		 * we choose to begin optimizing for space.
19425256Smckusick 		 */
19524698Smckusick 		request = fs->fs_bsize;
19625256Smckusick 		if (fs->fs_cstotal.cs_nffree <
19725256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
19825256Smckusick 			break;
19925256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
20025256Smckusick 			fs->fs_fsmnt);
20125256Smckusick 		fs->fs_optim = FS_OPTSPACE;
20225256Smckusick 		break;
20325256Smckusick 	default:
20425256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
20525256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
20625256Smckusick 		panic("realloccg: bad optim");
20725256Smckusick 		/* NOTREACHED */
20825256Smckusick 	}
20924698Smckusick 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
2109163Ssam 		(u_long (*)())alloccg);
2116567Smckusic 	if (bno > 0) {
21239678Smckusick 		bp->b_blkno = bn = fsbtodb(fs, bno);
21330749Skarels 		count = howmany(osize, CLBYTES);
21430749Skarels 		for (i = 0; i < count; i++)
21537735Smckusick 			munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
21631402Smckusick 		blkfree(ip, bprev, (off_t)osize);
21724698Smckusick 		if (nsize < request)
21831402Smckusick 			blkfree(ip, bno + numfrags(fs, nsize),
21924698Smckusick 				(off_t)(request - nsize));
22012643Ssam 		ip->i_blocks += btodb(nsize - osize);
22112643Ssam 		ip->i_flag |= IUPD|ICHG;
22237735Smckusick 		*bpp = bp;
22337735Smckusick 		return (0);
2244463Smckusic 	}
22539678Smckusick 	brelse(bp);
2264792Smckusic nospace:
2274463Smckusic 	/*
2284463Smckusic 	 * no space available
2294463Smckusic 	 */
23042318Smckusick 	fserr(fs, cred->cr_uid, "file system full");
2314426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
23237735Smckusick 	return (ENOSPC);
2334426Smckusic }
2344426Smckusic 
2355375Smckusic /*
2365375Smckusic  * Allocate an inode in the file system.
2375375Smckusic  *
2385375Smckusic  * A preference may be optionally specified. If a preference is given
2395375Smckusic  * the following hierarchy is used to allocate an inode:
2405375Smckusic  *   1) allocate the requested inode.
2415375Smckusic  *   2) allocate an inode in the same cylinder group.
2425375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2435375Smckusic  *      available inode is located.
2445375Smckusic  * If no inode preference is given the following heirarchy is used
2455375Smckusic  * to allocate an inode:
2465375Smckusic  *   1) allocate an inode in cylinder group 0.
2475375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2485375Smckusic  *      available inode is located.
2495375Smckusic  */
25041309Smckusick ialloc(pip, ipref, mode, cred, ipp)
2515965Smckusic 	register struct inode *pip;
2524359Smckusick 	ino_t ipref;
2534359Smckusick 	int mode;
25441309Smckusick 	struct ucred *cred;
25537735Smckusick 	struct inode **ipp;
2564359Smckusick {
2575212Smckusic 	ino_t ino;
2584359Smckusick 	register struct fs *fs;
2594359Smckusick 	register struct inode *ip;
26037735Smckusick 	int cg, error;
2614359Smckusick 
26237735Smckusick 	*ipp = 0;
2635965Smckusic 	fs = pip->i_fs;
2644792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2654359Smckusick 		goto noinodes;
2664948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2674948Smckusic 		ipref = 0;
2685377Smckusic 	cg = itog(fs, ipref);
2695965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2704359Smckusick 	if (ino == 0)
2714359Smckusick 		goto noinodes;
27237735Smckusick 	error = iget(pip, ino, ipp);
27337735Smckusick 	if (error) {
27441309Smckusick 		ifree(pip, ino, mode);
27537735Smckusick 		return (error);
2764359Smckusick 	}
27738255Smckusick 	ip = *ipp;
2786716Smckusick 	if (ip->i_mode) {
2796716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
2806716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2814359Smckusick 		panic("ialloc: dup alloc");
2826716Smckusick 	}
28312643Ssam 	if (ip->i_blocks) {				/* XXX */
28412643Ssam 		printf("free inode %s/%d had %d blocks\n",
28512643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
28612643Ssam 		ip->i_blocks = 0;
28712643Ssam 	}
28839516Smckusick 	ip->i_flags = 0;
28938255Smckusick 	/*
29038255Smckusick 	 * Set up a new generation number for this inode.
29138255Smckusick 	 */
29238255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
29338255Smckusick 		nextgennumber = time.tv_sec;
29438255Smckusick 	ip->i_gen = nextgennumber;
29537735Smckusick 	return (0);
2964359Smckusick noinodes:
29742318Smckusick 	fserr(fs, cred->cr_uid, "out of inodes");
2986294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
29937735Smckusick 	return (ENOSPC);
3004359Smckusick }
3014359Smckusick 
3024651Smckusic /*
3035375Smckusic  * Find a cylinder to place a directory.
3045375Smckusic  *
3055375Smckusic  * The policy implemented by this algorithm is to select from
3065375Smckusic  * among those cylinder groups with above the average number of
3075375Smckusic  * free inodes, the one with the smallest number of directories.
3084651Smckusic  */
3099163Ssam ino_t
3105965Smckusic dirpref(fs)
3115965Smckusic 	register struct fs *fs;
3124359Smckusick {
3134651Smckusic 	int cg, minndir, mincg, avgifree;
3144359Smckusick 
3154792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3164651Smckusic 	minndir = fs->fs_ipg;
3174359Smckusick 	mincg = 0;
3184651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3195322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3205322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3214359Smckusick 			mincg = cg;
3225322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3234359Smckusick 		}
3249163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3254359Smckusick }
3264359Smckusick 
3274651Smckusic /*
3289163Ssam  * Select the desired position for the next block in a file.  The file is
3299163Ssam  * logically divided into sections. The first section is composed of the
3309163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3319163Ssam  *
3329163Ssam  * If no blocks have been allocated in the first section, the policy is to
3339163Ssam  * request a block in the same cylinder group as the inode that describes
3349163Ssam  * the file. If no blocks have been allocated in any other section, the
3359163Ssam  * policy is to place the section in a cylinder group with a greater than
3369163Ssam  * average number of free blocks.  An appropriate cylinder group is found
33717696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
33817696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
33917696Smckusick  * cylinder group from which the previous allocation was made. The sweep
34017696Smckusick  * continues until a cylinder group with greater than the average number
34117696Smckusick  * of free blocks is found. If the allocation is for the first block in an
34217696Smckusick  * indirect block, the information on the previous allocation is unavailable;
34317696Smckusick  * here a best guess is made based upon the logical block number being
34417696Smckusick  * allocated.
3459163Ssam  *
3469163Ssam  * If a section is already partially allocated, the policy is to
3479163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3489163Ssam  * contiguous blocks and the beginning of the next is physically separated
3499163Ssam  * so that the disk head will be in transit between them for at least
3509163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3519163Ssam  * schedule another I/O transfer.
3524651Smckusic  */
3535212Smckusic daddr_t
3549163Ssam blkpref(ip, lbn, indx, bap)
3559163Ssam 	struct inode *ip;
3569163Ssam 	daddr_t lbn;
3579163Ssam 	int indx;
3589163Ssam 	daddr_t *bap;
3599163Ssam {
3605965Smckusic 	register struct fs *fs;
36117696Smckusick 	register int cg;
36217696Smckusick 	int avgbfree, startcg;
3639163Ssam 	daddr_t nextblk;
3644651Smckusic 
3659163Ssam 	fs = ip->i_fs;
3669163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3679163Ssam 		if (lbn < NDADDR) {
3689163Ssam 			cg = itog(fs, ip->i_number);
3695322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3704651Smckusic 		}
3719163Ssam 		/*
3729163Ssam 		 * Find a cylinder with greater than average number of
3739163Ssam 		 * unused data blocks.
3749163Ssam 		 */
37517696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
37617696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
37717696Smckusick 		else
37817696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
37917696Smckusick 		startcg %= fs->fs_ncg;
3809163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
38117696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
3829163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3839163Ssam 				fs->fs_cgrotor = cg;
3849163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3859163Ssam 			}
38617696Smckusick 		for (cg = 0; cg <= startcg; cg++)
3879163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3889163Ssam 				fs->fs_cgrotor = cg;
3899163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3909163Ssam 			}
3919163Ssam 		return (NULL);
3929163Ssam 	}
3939163Ssam 	/*
3949163Ssam 	 * One or more previous blocks have been laid out. If less
3959163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
3969163Ssam 	 * next block is requested contiguously, otherwise it is
3979163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
3989163Ssam 	 */
3999163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
4009163Ssam 	if (indx > fs->fs_maxcontig &&
40111638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4029163Ssam 	    != nextblk)
4039163Ssam 		return (nextblk);
4049163Ssam 	if (fs->fs_rotdelay != 0)
4059163Ssam 		/*
4069163Ssam 		 * Here we convert ms of delay to frags as:
4079163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4089163Ssam 		 *	((sect/frag) * (ms/sec))
4099163Ssam 		 * then round up to the next block.
4109163Ssam 		 */
4119163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4129163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4139163Ssam 	return (nextblk);
4144651Smckusic }
4154651Smckusic 
4165375Smckusic /*
4175375Smckusic  * Implement the cylinder overflow algorithm.
4185375Smckusic  *
4195375Smckusic  * The policy implemented by this algorithm is:
4205375Smckusic  *   1) allocate the block in its requested cylinder group.
4215375Smckusic  *   2) quadradically rehash on the cylinder group number.
4225375Smckusic  *   3) brute force search for a free block.
4235375Smckusic  */
4245212Smckusic /*VARARGS5*/
4255212Smckusic u_long
4265965Smckusic hashalloc(ip, cg, pref, size, allocator)
4275965Smckusic 	struct inode *ip;
4284359Smckusick 	int cg;
4294359Smckusick 	long pref;
4304359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4315212Smckusic 	u_long (*allocator)();
4324359Smckusick {
4335965Smckusic 	register struct fs *fs;
4344359Smckusick 	long result;
4354359Smckusick 	int i, icg = cg;
4364359Smckusick 
4375965Smckusic 	fs = ip->i_fs;
4384359Smckusick 	/*
4394359Smckusick 	 * 1: preferred cylinder group
4404359Smckusick 	 */
4415965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4424359Smckusick 	if (result)
4434359Smckusick 		return (result);
4444359Smckusick 	/*
4454359Smckusick 	 * 2: quadratic rehash
4464359Smckusick 	 */
4474359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4484359Smckusick 		cg += i;
4494359Smckusick 		if (cg >= fs->fs_ncg)
4504359Smckusick 			cg -= fs->fs_ncg;
4515965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4524359Smckusick 		if (result)
4534359Smckusick 			return (result);
4544359Smckusick 	}
4554359Smckusick 	/*
4564359Smckusick 	 * 3: brute force search
45710847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
45810847Ssam 	 * and 1 is always checked in the quadratic rehash.
4594359Smckusick 	 */
46010848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
46110847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4625965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4634359Smckusick 		if (result)
4644359Smckusick 			return (result);
4654359Smckusick 		cg++;
4664359Smckusick 		if (cg == fs->fs_ncg)
4674359Smckusick 			cg = 0;
4684359Smckusick 	}
4696294Smckusick 	return (NULL);
4704359Smckusick }
4714359Smckusick 
4725375Smckusic /*
4735375Smckusic  * Determine whether a fragment can be extended.
4745375Smckusic  *
4755375Smckusic  * Check to see if the necessary fragments are available, and
4765375Smckusic  * if they are, allocate them.
4775375Smckusic  */
4784359Smckusick daddr_t
4795965Smckusic fragextend(ip, cg, bprev, osize, nsize)
4805965Smckusic 	struct inode *ip;
4814426Smckusic 	int cg;
4824463Smckusic 	long bprev;
4834426Smckusic 	int osize, nsize;
4844426Smckusic {
4855965Smckusic 	register struct fs *fs;
4864463Smckusic 	register struct cg *cgp;
48737735Smckusick 	struct buf *bp;
4884463Smckusic 	long bno;
4894463Smckusic 	int frags, bbase;
49037735Smckusick 	int i, error;
4914426Smckusic 
4925965Smckusic 	fs = ip->i_fs;
49317224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
4946531Smckusick 		return (NULL);
4955960Smckusic 	frags = numfrags(fs, nsize);
49617224Smckusick 	bbase = fragnum(fs, bprev);
49717224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
49830749Skarels 		/* cannot extend across a block boundary */
4996294Smckusick 		return (NULL);
5004463Smckusic 	}
50137735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
50238776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
50337735Smckusick 	if (error) {
50437735Smckusick 		brelse(bp);
50537735Smckusick 		return (NULL);
50637735Smckusick 	}
5076531Smckusick 	cgp = bp->b_un.b_cg;
50837735Smckusick 	if (!cg_chkmagic(cgp)) {
5095960Smckusic 		brelse(bp);
5106294Smckusick 		return (NULL);
5115960Smckusic 	}
5128105Sroot 	cgp->cg_time = time.tv_sec;
5135377Smckusic 	bno = dtogd(fs, bprev);
5145960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
51534143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
5165361Smckusic 			brelse(bp);
5176294Smckusick 			return (NULL);
5185361Smckusic 		}
5195361Smckusic 	/*
5205361Smckusic 	 * the current fragment can be extended
5215361Smckusic 	 * deduct the count on fragment being extended into
5225361Smckusic 	 * increase the count on the remaining fragment (if any)
5235361Smckusic 	 * allocate the extended piece
5245361Smckusic 	 */
5255361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
52634143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
5274463Smckusic 			break;
5285960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5295361Smckusic 	if (i != frags)
5305361Smckusic 		cgp->cg_frsum[i - frags]++;
5315960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
53234143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
5335361Smckusic 		cgp->cg_cs.cs_nffree--;
5345361Smckusic 		fs->fs_cstotal.cs_nffree--;
5355361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5364463Smckusic 	}
5375361Smckusic 	fs->fs_fmod++;
5385361Smckusic 	bdwrite(bp);
5395361Smckusic 	return (bprev);
5404426Smckusic }
5414426Smckusic 
5425375Smckusic /*
5435375Smckusic  * Determine whether a block can be allocated.
5445375Smckusic  *
5455375Smckusic  * Check to see if a block of the apprpriate size is available,
5465375Smckusic  * and if it is, allocate it.
5475375Smckusic  */
5489163Ssam daddr_t
5495965Smckusic alloccg(ip, cg, bpref, size)
5505965Smckusic 	struct inode *ip;
5514359Smckusick 	int cg;
5524359Smckusick 	daddr_t bpref;
5534359Smckusick 	int size;
5544359Smckusick {
5555965Smckusic 	register struct fs *fs;
5564463Smckusic 	register struct cg *cgp;
55737735Smckusick 	struct buf *bp;
5584463Smckusic 	register int i;
55937735Smckusick 	int error, bno, frags, allocsiz;
5604359Smckusick 
5615965Smckusic 	fs = ip->i_fs;
5625322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5636294Smckusick 		return (NULL);
56437735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
56538776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
56637735Smckusick 	if (error) {
56737735Smckusick 		brelse(bp);
56837735Smckusick 		return (NULL);
56937735Smckusick 	}
5706531Smckusick 	cgp = bp->b_un.b_cg;
57137735Smckusick 	if (!cg_chkmagic(cgp) ||
57215950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5735960Smckusic 		brelse(bp);
5746294Smckusick 		return (NULL);
5755960Smckusic 	}
5768105Sroot 	cgp->cg_time = time.tv_sec;
5775322Smckusic 	if (size == fs->fs_bsize) {
5785212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5794463Smckusic 		bdwrite(bp);
5804463Smckusic 		return (bno);
5814463Smckusic 	}
5824463Smckusic 	/*
5834463Smckusic 	 * check to see if any fragments are already available
5844463Smckusic 	 * allocsiz is the size which will be allocated, hacking
5854463Smckusic 	 * it down to a smaller size if necessary
5864463Smckusic 	 */
5875960Smckusic 	frags = numfrags(fs, size);
5885322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
5894463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
5904463Smckusic 			break;
5915322Smckusic 	if (allocsiz == fs->fs_frag) {
5924463Smckusic 		/*
5934463Smckusic 		 * no fragments were available, so a block will be
5944463Smckusic 		 * allocated, and hacked up
5954463Smckusic 		 */
5964792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
5974463Smckusic 			brelse(bp);
5986294Smckusick 			return (NULL);
5994463Smckusic 		}
6005212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
6015377Smckusic 		bpref = dtogd(fs, bno);
6025322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
60334143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
6045322Smckusic 		i = fs->fs_frag - frags;
6054792Smckusic 		cgp->cg_cs.cs_nffree += i;
6064792Smckusic 		fs->fs_cstotal.cs_nffree += i;
6075322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
6089762Ssam 		fs->fs_fmod++;
6094463Smckusic 		cgp->cg_frsum[i]++;
6104463Smckusic 		bdwrite(bp);
6114463Smckusic 		return (bno);
6124463Smckusic 	}
6134651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
61415950Smckusick 	if (bno < 0) {
61515950Smckusick 		brelse(bp);
6166294Smckusick 		return (NULL);
61715950Smckusick 	}
6184463Smckusic 	for (i = 0; i < frags; i++)
61934143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
6204792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6214792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6225322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
6239762Ssam 	fs->fs_fmod++;
6244463Smckusic 	cgp->cg_frsum[allocsiz]--;
6254463Smckusic 	if (frags != allocsiz)
6264463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6274463Smckusic 	bdwrite(bp);
6284463Smckusic 	return (cg * fs->fs_fpg + bno);
6294463Smckusic }
6304463Smckusic 
6315375Smckusic /*
6325375Smckusic  * Allocate a block in a cylinder group.
6335375Smckusic  *
6345375Smckusic  * This algorithm implements the following policy:
6355375Smckusic  *   1) allocate the requested block.
6365375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6375375Smckusic  *   3) allocate the next available block on the block rotor for the
6385375Smckusic  *      specified cylinder group.
6395375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6405375Smckusic  * blocks may be fragmented by the routine that allocates them.
6415375Smckusic  */
6424463Smckusic daddr_t
6435212Smckusic alloccgblk(fs, cgp, bpref)
6445965Smckusic 	register struct fs *fs;
6454463Smckusic 	register struct cg *cgp;
6464463Smckusic 	daddr_t bpref;
6474463Smckusic {
6484651Smckusic 	daddr_t bno;
6496294Smckusick 	int cylno, pos, delta;
6504651Smckusic 	short *cylbp;
6515361Smckusic 	register int i;
6524463Smckusic 
6534651Smckusic 	if (bpref == 0) {
6544651Smckusic 		bpref = cgp->cg_rotor;
6555361Smckusic 		goto norot;
6565361Smckusic 	}
65717224Smckusick 	bpref = blknum(fs, bpref);
6585377Smckusic 	bpref = dtogd(fs, bpref);
6595361Smckusic 	/*
6605361Smckusic 	 * if the requested block is available, use it
6615361Smckusic 	 */
66234143Smckusick 	if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
6635361Smckusic 		bno = bpref;
6645361Smckusic 		goto gotit;
6655361Smckusic 	}
6665361Smckusic 	/*
6675361Smckusic 	 * check for a block available on the same cylinder
6685361Smckusic 	 */
6695361Smckusic 	cylno = cbtocylno(fs, bpref);
67034143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
6715375Smckusic 		goto norot;
6725375Smckusic 	if (fs->fs_cpc == 0) {
6735375Smckusic 		/*
6745375Smckusic 		 * block layout info is not available, so just have
6755375Smckusic 		 * to take any block in this cylinder.
6765375Smckusic 		 */
6775375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
6785375Smckusic 		goto norot;
6795375Smckusic 	}
6805375Smckusic 	/*
6815361Smckusic 	 * check the summary information to see if a block is
6825361Smckusic 	 * available in the requested cylinder starting at the
6839163Ssam 	 * requested rotational position and proceeding around.
6845361Smckusic 	 */
68534143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
6869163Ssam 	pos = cbtorpos(fs, bpref);
68734143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
6885361Smckusic 		if (cylbp[i] > 0)
6895361Smckusic 			break;
69034143Smckusick 	if (i == fs->fs_nrpos)
6915361Smckusic 		for (i = 0; i < pos; i++)
6925361Smckusic 			if (cylbp[i] > 0)
6935361Smckusic 				break;
6945361Smckusic 	if (cylbp[i] > 0) {
6954651Smckusic 		/*
6965361Smckusic 		 * found a rotational position, now find the actual
6975361Smckusic 		 * block. A panic if none is actually there.
6984651Smckusic 		 */
6995361Smckusic 		pos = cylno % fs->fs_cpc;
7005361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
70134143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
7026716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
7036716Smckusick 			    pos, i, fs->fs_fsmnt);
7045361Smckusic 			panic("alloccgblk: cyl groups corrupted");
7056716Smckusick 		}
70634143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
70734143Smckusick 			if (isblock(fs, cg_blksfree(cgp), bno + i)) {
70811638Ssam 				bno = blkstofrags(fs, (bno + i));
7095361Smckusic 				goto gotit;
7105361Smckusic 			}
71134143Smckusick 			delta = fs_rotbl(fs)[i];
71234143Smckusick 			if (delta <= 0 ||
71334143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
7144651Smckusic 				break;
7156294Smckusick 			i += delta;
7164651Smckusic 		}
7176716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
7185361Smckusic 		panic("alloccgblk: can't find blk in cyl");
7194359Smckusick 	}
7205361Smckusic norot:
7215361Smckusic 	/*
7225361Smckusic 	 * no blocks in the requested cylinder, so take next
7235361Smckusic 	 * available one in this cylinder group.
7245361Smckusic 	 */
7258628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7266567Smckusic 	if (bno < 0)
7276294Smckusick 		return (NULL);
7284651Smckusic 	cgp->cg_rotor = bno;
7294359Smckusick gotit:
73034143Smckusick 	clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno));
7314792Smckusic 	cgp->cg_cs.cs_nbfree--;
7324792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7335322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7345375Smckusic 	cylno = cbtocylno(fs, bno);
73534143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
73634143Smckusick 	cg_blktot(cgp)[cylno]--;
7374359Smckusick 	fs->fs_fmod++;
7384651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7394359Smckusick }
74034143Smckusick 
7415375Smckusic /*
7425375Smckusic  * Determine whether an inode can be allocated.
7435375Smckusic  *
7445375Smckusic  * Check to see if an inode is available, and if it is,
7455375Smckusic  * allocate it using the following policy:
7465375Smckusic  *   1) allocate the requested inode.
7475375Smckusic  *   2) allocate the next available inode after the requested
7485375Smckusic  *      inode in the specified cylinder group.
7495375Smckusic  */
7509163Ssam ino_t
7515965Smckusic ialloccg(ip, cg, ipref, mode)
7525965Smckusic 	struct inode *ip;
7534359Smckusick 	int cg;
7544359Smckusick 	daddr_t ipref;
7554359Smckusick 	int mode;
7564359Smckusick {
7575965Smckusic 	register struct fs *fs;
7584463Smckusic 	register struct cg *cgp;
75916784Smckusick 	struct buf *bp;
76037735Smckusick 	int error, start, len, loc, map, i;
7614359Smckusick 
7625965Smckusic 	fs = ip->i_fs;
7635322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7646294Smckusick 		return (NULL);
76537735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
76638776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
76737735Smckusick 	if (error) {
76837735Smckusick 		brelse(bp);
76937735Smckusick 		return (NULL);
77037735Smckusick 	}
7716531Smckusick 	cgp = bp->b_un.b_cg;
77237735Smckusick 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
7735960Smckusic 		brelse(bp);
7746294Smckusick 		return (NULL);
7755960Smckusic 	}
7768105Sroot 	cgp->cg_time = time.tv_sec;
7774359Smckusick 	if (ipref) {
7784359Smckusick 		ipref %= fs->fs_ipg;
77934143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
7804359Smckusick 			goto gotit;
78116784Smckusick 	}
78216784Smckusick 	start = cgp->cg_irotor / NBBY;
78316784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
78434143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
78516784Smckusick 	if (loc == 0) {
78617697Smckusick 		len = start + 1;
78717697Smckusick 		start = 0;
78834143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
78917697Smckusick 		if (loc == 0) {
79017697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
79117697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
79217697Smckusick 			panic("ialloccg: map corrupted");
79317697Smckusick 			/* NOTREACHED */
79417697Smckusick 		}
79516784Smckusick 	}
79616784Smckusick 	i = start + len - loc;
79734143Smckusick 	map = cg_inosused(cgp)[i];
79816784Smckusick 	ipref = i * NBBY;
79916784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
80016784Smckusick 		if ((map & i) == 0) {
8014359Smckusick 			cgp->cg_irotor = ipref;
8024359Smckusick 			goto gotit;
8034359Smckusick 		}
8044359Smckusick 	}
80516784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
80616784Smckusick 	panic("ialloccg: block not in map");
80716784Smckusick 	/* NOTREACHED */
8084359Smckusick gotit:
80934143Smckusick 	setbit(cg_inosused(cgp), ipref);
8104792Smckusic 	cgp->cg_cs.cs_nifree--;
8114792Smckusic 	fs->fs_cstotal.cs_nifree--;
8125322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
8134359Smckusick 	fs->fs_fmod++;
8144359Smckusick 	if ((mode & IFMT) == IFDIR) {
8154792Smckusic 		cgp->cg_cs.cs_ndir++;
8164792Smckusic 		fs->fs_cstotal.cs_ndir++;
8175322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8184359Smckusick 	}
8194359Smckusick 	bdwrite(bp);
8204359Smckusick 	return (cg * fs->fs_ipg + ipref);
8214359Smckusick }
8224359Smckusick 
8235375Smckusic /*
8245375Smckusic  * Free a block or fragment.
8255375Smckusic  *
8265375Smckusic  * The specified block or fragment is placed back in the
8275375Smckusic  * free map. If a fragment is deallocated, a possible
8285375Smckusic  * block reassembly is checked.
8295375Smckusic  */
83031402Smckusick blkfree(ip, bno, size)
8315965Smckusic 	register struct inode *ip;
8324359Smckusick 	daddr_t bno;
8335212Smckusic 	off_t size;
8344359Smckusick {
8354359Smckusick 	register struct fs *fs;
8364359Smckusick 	register struct cg *cgp;
83737735Smckusick 	struct buf *bp;
83837735Smckusick 	int error, cg, blk, frags, bbase;
8394463Smckusic 	register int i;
84042318Smckusick 	struct ucred *cred = u.u_cred;	/* XXX */
8414359Smckusick 
8425965Smckusic 	fs = ip->i_fs;
8436716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8446716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8456716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
84631402Smckusick 		panic("blkfree: bad size");
8476716Smckusick 	}
8485377Smckusic 	cg = dtog(fs, bno);
84942318Smckusick 	if ((unsigned)bno >= fs->fs_size) {
8506567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
85142318Smckusick 		fserr(fs, cred->cr_uid, "bad block");
8524359Smckusick 		return;
8536567Smckusic 	}
85437735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
85538776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
85637735Smckusick 	if (error) {
85737735Smckusick 		brelse(bp);
85837735Smckusick 		return;
85937735Smckusick 	}
8606531Smckusick 	cgp = bp->b_un.b_cg;
86137735Smckusick 	if (!cg_chkmagic(cgp)) {
8625960Smckusic 		brelse(bp);
8634359Smckusick 		return;
8645960Smckusic 	}
8658105Sroot 	cgp->cg_time = time.tv_sec;
8665377Smckusic 	bno = dtogd(fs, bno);
8675322Smckusic 	if (size == fs->fs_bsize) {
86834143Smckusick 		if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) {
8696716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
8706716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
87131402Smckusick 			panic("blkfree: freeing free block");
8726567Smckusic 		}
87334143Smckusick 		setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
8744792Smckusic 		cgp->cg_cs.cs_nbfree++;
8754792Smckusic 		fs->fs_cstotal.cs_nbfree++;
8765322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
8775375Smckusic 		i = cbtocylno(fs, bno);
87834143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
87934143Smckusick 		cg_blktot(cgp)[i]++;
8804426Smckusic 	} else {
88117224Smckusick 		bbase = bno - fragnum(fs, bno);
8824463Smckusic 		/*
8834463Smckusic 		 * decrement the counts associated with the old frags
8844463Smckusic 		 */
88534143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
8865322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
8874463Smckusic 		/*
8884463Smckusic 		 * deallocate the fragment
8894463Smckusic 		 */
8905960Smckusic 		frags = numfrags(fs, size);
8914463Smckusic 		for (i = 0; i < frags; i++) {
89234143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
8936716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
8946716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
89531402Smckusick 				panic("blkfree: freeing free frag");
8966716Smckusick 			}
89734143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
8984426Smckusic 		}
8996294Smckusick 		cgp->cg_cs.cs_nffree += i;
9006294Smckusick 		fs->fs_cstotal.cs_nffree += i;
9016294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
9024463Smckusic 		/*
9034463Smckusic 		 * add back in counts associated with the new frags
9044463Smckusic 		 */
90534143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
9065322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
9074463Smckusic 		/*
9084463Smckusic 		 * if a complete block has been reassembled, account for it
9094463Smckusic 		 */
91034476Smckusick 		if (isblock(fs, cg_blksfree(cgp),
91134476Smckusick 		    (daddr_t)fragstoblks(fs, bbase))) {
9125322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
9135322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
9145322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
9154792Smckusic 			cgp->cg_cs.cs_nbfree++;
9164792Smckusic 			fs->fs_cstotal.cs_nbfree++;
9175322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
9185375Smckusic 			i = cbtocylno(fs, bbase);
91934143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
92034143Smckusick 			cg_blktot(cgp)[i]++;
9214426Smckusic 		}
9224426Smckusic 	}
9234359Smckusick 	fs->fs_fmod++;
9244359Smckusick 	bdwrite(bp);
9254359Smckusick }
9264359Smckusick 
9275375Smckusic /*
9285375Smckusic  * Free an inode.
9295375Smckusic  *
9305375Smckusic  * The specified inode is placed back in the free map.
9315375Smckusic  */
9325965Smckusic ifree(ip, ino, mode)
9335965Smckusic 	struct inode *ip;
9344359Smckusick 	ino_t ino;
9354359Smckusick 	int mode;
9364359Smckusick {
9374359Smckusick 	register struct fs *fs;
9384359Smckusick 	register struct cg *cgp;
93937735Smckusick 	struct buf *bp;
94037735Smckusick 	int error, cg;
9414359Smckusick 
9425965Smckusic 	fs = ip->i_fs;
9436716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
9446716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9456716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9464359Smckusick 		panic("ifree: range");
9476716Smckusick 	}
9485377Smckusic 	cg = itog(fs, ino);
94937735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
95038776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
95137735Smckusick 	if (error) {
95237735Smckusick 		brelse(bp);
95337735Smckusick 		return;
95437735Smckusick 	}
9556531Smckusick 	cgp = bp->b_un.b_cg;
95637735Smckusick 	if (!cg_chkmagic(cgp)) {
9575960Smckusic 		brelse(bp);
9584359Smckusick 		return;
9595960Smckusic 	}
9608105Sroot 	cgp->cg_time = time.tv_sec;
9614359Smckusick 	ino %= fs->fs_ipg;
96234143Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
9636716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9646716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9654359Smckusick 		panic("ifree: freeing free inode");
9666716Smckusick 	}
96734143Smckusick 	clrbit(cg_inosused(cgp), ino);
96816784Smckusick 	if (ino < cgp->cg_irotor)
96916784Smckusick 		cgp->cg_irotor = ino;
9704792Smckusic 	cgp->cg_cs.cs_nifree++;
9714792Smckusic 	fs->fs_cstotal.cs_nifree++;
9725322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
9734359Smckusick 	if ((mode & IFMT) == IFDIR) {
9744792Smckusic 		cgp->cg_cs.cs_ndir--;
9754792Smckusic 		fs->fs_cstotal.cs_ndir--;
9765322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
9774359Smckusick 	}
9784359Smckusick 	fs->fs_fmod++;
9794359Smckusick 	bdwrite(bp);
9804359Smckusick }
9814359Smckusick 
9824463Smckusic /*
9835375Smckusic  * Find a block of the specified size in the specified cylinder group.
9845375Smckusic  *
9854651Smckusic  * It is a panic if a request is made to find a block if none are
9864651Smckusic  * available.
9874651Smckusic  */
9884651Smckusic daddr_t
9894651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
9904651Smckusic 	register struct fs *fs;
9914651Smckusic 	register struct cg *cgp;
9924651Smckusic 	daddr_t bpref;
9934651Smckusic 	int allocsiz;
9944651Smckusic {
9954651Smckusic 	daddr_t bno;
9964651Smckusic 	int start, len, loc, i;
9974651Smckusic 	int blk, field, subfield, pos;
9984651Smckusic 
9994651Smckusic 	/*
10004651Smckusic 	 * find the fragment by searching through the free block
10014651Smckusic 	 * map for an appropriate bit pattern
10024651Smckusic 	 */
10034651Smckusic 	if (bpref)
10045377Smckusic 		start = dtogd(fs, bpref) / NBBY;
10054651Smckusic 	else
10064651Smckusic 		start = cgp->cg_frotor / NBBY;
10075398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
100834476Smckusick 	loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start],
100934476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
101034476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
10114651Smckusic 	if (loc == 0) {
10126531Smckusick 		len = start + 1;
10136531Smckusick 		start = 0;
101434476Smckusick 		loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0],
101534476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
101634476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
101716784Smckusick 		if (loc == 0) {
101816784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
101916784Smckusick 			    start, len, fs->fs_fsmnt);
102016784Smckusick 			panic("alloccg: map corrupted");
102117697Smckusick 			/* NOTREACHED */
102216784Smckusick 		}
10234651Smckusic 	}
10244651Smckusic 	bno = (start + len - loc) * NBBY;
10254651Smckusic 	cgp->cg_frotor = bno;
10264651Smckusic 	/*
10274651Smckusic 	 * found the byte in the map
10284651Smckusic 	 * sift through the bits to find the selected frag
10294651Smckusic 	 */
10306294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
103134143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
10324651Smckusic 		blk <<= 1;
10334651Smckusic 		field = around[allocsiz];
10344651Smckusic 		subfield = inside[allocsiz];
10355322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10366294Smckusick 			if ((blk & field) == subfield)
10376294Smckusick 				return (bno + pos);
10384651Smckusic 			field <<= 1;
10394651Smckusic 			subfield <<= 1;
10404651Smckusic 		}
10414651Smckusic 	}
10426716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
10434651Smckusic 	panic("alloccg: block not in map");
10446531Smckusick 	return (-1);
10454651Smckusic }
10464651Smckusic 
10474651Smckusic /*
10485375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10495375Smckusic  *
10505375Smckusic  * The form of the error message is:
10514359Smckusick  *	fs: error message
10524359Smckusick  */
105342318Smckusick fserr(fs, uid, cp)
10544359Smckusick 	struct fs *fs;
105542318Smckusick 	uid_t uid;
10564359Smckusick 	char *cp;
10574359Smckusick {
10584359Smckusick 
105942318Smckusick 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
10604359Smckusick }
1061