xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 11638)
1*11638Ssam /*	lfs_alloc.c	2.24	83/03/21	*/
24359Smckusick 
34359Smckusick #include "../h/param.h"
44359Smckusick #include "../h/systm.h"
54359Smckusick #include "../h/mount.h"
64359Smckusick #include "../h/fs.h"
74359Smckusick #include "../h/conf.h"
84359Smckusick #include "../h/buf.h"
94359Smckusick #include "../h/inode.h"
106567Smckusic #include "../h/dir.h"
114359Smckusick #include "../h/user.h"
127483Skre #include "../h/quota.h"
138105Sroot #include "../h/kernel.h"
144359Smckusick 
155212Smckusic extern u_long		hashalloc();
169163Ssam extern ino_t		ialloccg();
179163Ssam extern daddr_t		alloccg();
184651Smckusic extern daddr_t		alloccgblk();
194651Smckusic extern daddr_t		fragextend();
204651Smckusic extern daddr_t		blkpref();
214651Smckusic extern daddr_t		mapsearch();
224607Smckusic extern int		inside[], around[];
235322Smckusic extern unsigned char	*fragtbl[];
244359Smckusick 
255375Smckusic /*
265375Smckusic  * Allocate a block in the file system.
275375Smckusic  *
285375Smckusic  * The size of the requested block is given, which must be some
295375Smckusic  * multiple of fs_fsize and <= fs_bsize.
305375Smckusic  * A preference may be optionally specified. If a preference is given
315375Smckusic  * the following hierarchy is used to allocate a block:
325375Smckusic  *   1) allocate the requested block.
335375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
345375Smckusic  *   3) allocate a block in the same cylinder group.
355375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
365375Smckusic  *      available block is located.
375375Smckusic  * If no block preference is given the following heirarchy is used
385375Smckusic  * to allocate a block:
395375Smckusic  *   1) allocate a block in the cylinder group that contains the
405375Smckusic  *      inode for the file.
415375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
425375Smckusic  *      available block is located.
435375Smckusic  */
444359Smckusick struct buf *
455965Smckusic alloc(ip, bpref, size)
464463Smckusic 	register struct inode *ip;
474359Smckusick 	daddr_t bpref;
484359Smckusick 	int size;
494359Smckusick {
504359Smckusick 	daddr_t bno;
514359Smckusick 	register struct fs *fs;
524463Smckusic 	register struct buf *bp;
534359Smckusick 	int cg;
544359Smckusick 
555965Smckusic 	fs = ip->i_fs;
566716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
576716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
586716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
594463Smckusic 		panic("alloc: bad size");
606716Smckusick 	}
615322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
624359Smckusick 		goto nospace;
63*11638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
644792Smckusic 		goto nospace;
657650Ssam #ifdef QUOTA
667483Skre 	if (chkdq(ip, (long)((unsigned)size/DEV_BSIZE), 0))
677483Skre 		return(NULL);
687483Skre #endif
694948Smckusic 	if (bpref >= fs->fs_size)
704948Smckusic 		bpref = 0;
714359Smckusick 	if (bpref == 0)
725377Smckusic 		cg = itog(fs, ip->i_number);
734359Smckusick 	else
745377Smckusic 		cg = dtog(fs, bpref);
759163Ssam 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
769163Ssam 		(u_long (*)())alloccg);
776567Smckusic 	if (bno <= 0)
784359Smckusick 		goto nospace;
795965Smckusic 	bp = getblk(ip->i_dev, fsbtodb(fs, bno), size);
804359Smckusick 	clrbuf(bp);
814359Smckusick 	return (bp);
824359Smckusick nospace:
834359Smckusick 	fserr(fs, "file system full");
844359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
854359Smckusick 	u.u_error = ENOSPC;
864359Smckusick 	return (NULL);
874359Smckusick }
884359Smckusick 
895375Smckusic /*
905375Smckusic  * Reallocate a fragment to a bigger size
915375Smckusic  *
925375Smckusic  * The number and size of the old block is given, and a preference
935375Smckusic  * and new size is also specified. The allocator attempts to extend
945375Smckusic  * the original block. Failing that, the regular block allocator is
955375Smckusic  * invoked to get an appropriate block.
965375Smckusic  */
974426Smckusic struct buf *
985965Smckusic realloccg(ip, bprev, bpref, osize, nsize)
995965Smckusic 	register struct inode *ip;
1004651Smckusic 	daddr_t bprev, bpref;
1014426Smckusic 	int osize, nsize;
1024426Smckusic {
1034426Smckusic 	daddr_t bno;
1044426Smckusic 	register struct fs *fs;
1054463Smckusic 	register struct buf *bp, *obp;
1064426Smckusic 	int cg;
1074426Smckusic 
1085965Smckusic 	fs = ip->i_fs;
1095960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1106716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1116716Smckusick 		printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1126716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
1134463Smckusic 		panic("realloccg: bad size");
1146716Smckusick 	}
115*11638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1164792Smckusic 		goto nospace;
1176716Smckusick 	if (bprev == 0) {
1186716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1196716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1204463Smckusic 		panic("realloccg: bad bprev");
1216716Smckusick 	}
1227650Ssam #ifdef QUOTA
1237483Skre 	if (chkdq(ip, (long)((unsigned)(nsize-osize)/DEV_BSIZE), 0))
1247483Skre 		return(NULL);
1257483Skre #endif
1266294Smckusick 	cg = dtog(fs, bprev);
1275965Smckusic 	bno = fragextend(ip, cg, (long)bprev, osize, nsize);
1284463Smckusic 	if (bno != 0) {
1297187Sroot 		do {
1307187Sroot 			bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
1317187Sroot 			if (bp->b_flags & B_ERROR) {
1327187Sroot 				brelse(bp);
1337187Sroot 				return (NULL);
1347187Sroot 			}
1357187Sroot 		} while (brealloc(bp, nsize) == 0);
1367187Sroot 		bp->b_flags |= B_DONE;
1379163Ssam 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
1384463Smckusic 		return (bp);
1394463Smckusic 	}
1404948Smckusic 	if (bpref >= fs->fs_size)
1414948Smckusic 		bpref = 0;
1429163Ssam 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, nsize,
1439163Ssam 		(u_long (*)())alloccg);
1446567Smckusic 	if (bno > 0) {
1455965Smckusic 		obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
1465960Smckusic 		if (obp->b_flags & B_ERROR) {
1475960Smckusic 			brelse(obp);
1486294Smckusick 			return (NULL);
1495960Smckusic 		}
1505965Smckusic 		bp = getblk(ip->i_dev, fsbtodb(fs, bno), nsize);
1518617Sroot 		bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
1529163Ssam 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
1534463Smckusic 		brelse(obp);
1549163Ssam 		free(ip, bprev, (off_t)osize);
1556294Smckusick 		return (bp);
1564463Smckusic 	}
1574792Smckusic nospace:
1584463Smckusic 	/*
1594463Smckusic 	 * no space available
1604463Smckusic 	 */
1614426Smckusic 	fserr(fs, "file system full");
1624426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
1634426Smckusic 	u.u_error = ENOSPC;
1644426Smckusic 	return (NULL);
1654426Smckusic }
1664426Smckusic 
1675375Smckusic /*
1685375Smckusic  * Allocate an inode in the file system.
1695375Smckusic  *
1705375Smckusic  * A preference may be optionally specified. If a preference is given
1715375Smckusic  * the following hierarchy is used to allocate an inode:
1725375Smckusic  *   1) allocate the requested inode.
1735375Smckusic  *   2) allocate an inode in the same cylinder group.
1745375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
1755375Smckusic  *      available inode is located.
1765375Smckusic  * If no inode preference is given the following heirarchy is used
1775375Smckusic  * to allocate an inode:
1785375Smckusic  *   1) allocate an inode in cylinder group 0.
1795375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
1805375Smckusic  *      available inode is located.
1815375Smckusic  */
1824359Smckusick struct inode *
1835965Smckusic ialloc(pip, ipref, mode)
1845965Smckusic 	register struct inode *pip;
1854359Smckusick 	ino_t ipref;
1864359Smckusick 	int mode;
1874359Smckusick {
1885212Smckusic 	ino_t ino;
1894359Smckusick 	register struct fs *fs;
1904359Smckusick 	register struct inode *ip;
1914359Smckusick 	int cg;
1924359Smckusick 
1935965Smckusic 	fs = pip->i_fs;
1944792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
1954359Smckusick 		goto noinodes;
1967650Ssam #ifdef QUOTA
1979163Ssam 	if (chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0))
1987483Skre 		return(NULL);
1997483Skre #endif
2004948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2014948Smckusic 		ipref = 0;
2025377Smckusic 	cg = itog(fs, ipref);
2035965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2044359Smckusick 	if (ino == 0)
2054359Smckusick 		goto noinodes;
2065965Smckusic 	ip = iget(pip->i_dev, pip->i_fs, ino);
2074359Smckusick 	if (ip == NULL) {
2085965Smckusic 		ifree(ip, ino, 0);
2094359Smckusick 		return (NULL);
2104359Smckusick 	}
2116716Smckusick 	if (ip->i_mode) {
2126716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
2136716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2144359Smckusick 		panic("ialloc: dup alloc");
2156716Smckusick 	}
2164359Smckusick 	return (ip);
2174359Smckusick noinodes:
2184359Smckusick 	fserr(fs, "out of inodes");
2196294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
2204359Smckusick 	u.u_error = ENOSPC;
2214359Smckusick 	return (NULL);
2224359Smckusick }
2234359Smckusick 
2244651Smckusic /*
2255375Smckusic  * Find a cylinder to place a directory.
2265375Smckusic  *
2275375Smckusic  * The policy implemented by this algorithm is to select from
2285375Smckusic  * among those cylinder groups with above the average number of
2295375Smckusic  * free inodes, the one with the smallest number of directories.
2304651Smckusic  */
2319163Ssam ino_t
2325965Smckusic dirpref(fs)
2335965Smckusic 	register struct fs *fs;
2344359Smckusick {
2354651Smckusic 	int cg, minndir, mincg, avgifree;
2364359Smckusick 
2374792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
2384651Smckusic 	minndir = fs->fs_ipg;
2394359Smckusick 	mincg = 0;
2404651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
2415322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
2425322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
2434359Smckusick 			mincg = cg;
2445322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
2454359Smckusick 		}
2469163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
2474359Smckusick }
2484359Smckusick 
2494651Smckusic /*
2509163Ssam  * Select the desired position for the next block in a file.  The file is
2519163Ssam  * logically divided into sections. The first section is composed of the
2529163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
2539163Ssam  *
2549163Ssam  * If no blocks have been allocated in the first section, the policy is to
2559163Ssam  * request a block in the same cylinder group as the inode that describes
2569163Ssam  * the file. If no blocks have been allocated in any other section, the
2579163Ssam  * policy is to place the section in a cylinder group with a greater than
2589163Ssam  * average number of free blocks.  An appropriate cylinder group is found
2599163Ssam  * by maintaining a rotor that sweeps the cylinder groups. When a new
2609163Ssam  * group of blocks is needed, the rotor is advanced until a cylinder group
2619163Ssam  * with greater than the average number of free blocks is found.
2629163Ssam  *
2639163Ssam  * If a section is already partially allocated, the policy is to
2649163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
2659163Ssam  * contiguous blocks and the beginning of the next is physically separated
2669163Ssam  * so that the disk head will be in transit between them for at least
2679163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
2689163Ssam  * schedule another I/O transfer.
2694651Smckusic  */
2705212Smckusic daddr_t
2719163Ssam blkpref(ip, lbn, indx, bap)
2729163Ssam 	struct inode *ip;
2739163Ssam 	daddr_t lbn;
2749163Ssam 	int indx;
2759163Ssam 	daddr_t *bap;
2769163Ssam {
2775965Smckusic 	register struct fs *fs;
2784651Smckusic 	int cg, avgbfree;
2799163Ssam 	daddr_t nextblk;
2804651Smckusic 
2819163Ssam 	fs = ip->i_fs;
2829163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
2839163Ssam 		if (lbn < NDADDR) {
2849163Ssam 			cg = itog(fs, ip->i_number);
2855322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
2864651Smckusic 		}
2879163Ssam 		/*
2889163Ssam 		 * Find a cylinder with greater than average number of
2899163Ssam 		 * unused data blocks.
2909163Ssam 		 */
2919163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
2929163Ssam 		for (cg = fs->fs_cgrotor + 1; cg < fs->fs_ncg; cg++)
2939163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
2949163Ssam 				fs->fs_cgrotor = cg;
2959163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
2969163Ssam 			}
2979163Ssam 		for (cg = 0; cg <= fs->fs_cgrotor; cg++)
2989163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
2999163Ssam 				fs->fs_cgrotor = cg;
3009163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3019163Ssam 			}
3029163Ssam 		return (NULL);
3039163Ssam 	}
3049163Ssam 	/*
3059163Ssam 	 * One or more previous blocks have been laid out. If less
3069163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
3079163Ssam 	 * next block is requested contiguously, otherwise it is
3089163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
3099163Ssam 	 */
3109163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
3119163Ssam 	if (indx > fs->fs_maxcontig &&
312*11638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
3139163Ssam 	    != nextblk)
3149163Ssam 		return (nextblk);
3159163Ssam 	if (fs->fs_rotdelay != 0)
3169163Ssam 		/*
3179163Ssam 		 * Here we convert ms of delay to frags as:
3189163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
3199163Ssam 		 *	((sect/frag) * (ms/sec))
3209163Ssam 		 * then round up to the next block.
3219163Ssam 		 */
3229163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
3239163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
3249163Ssam 	return (nextblk);
3254651Smckusic }
3264651Smckusic 
3275375Smckusic /*
3285375Smckusic  * Implement the cylinder overflow algorithm.
3295375Smckusic  *
3305375Smckusic  * The policy implemented by this algorithm is:
3315375Smckusic  *   1) allocate the block in its requested cylinder group.
3325375Smckusic  *   2) quadradically rehash on the cylinder group number.
3335375Smckusic  *   3) brute force search for a free block.
3345375Smckusic  */
3355212Smckusic /*VARARGS5*/
3365212Smckusic u_long
3375965Smckusic hashalloc(ip, cg, pref, size, allocator)
3385965Smckusic 	struct inode *ip;
3394359Smckusick 	int cg;
3404359Smckusick 	long pref;
3414359Smckusick 	int size;	/* size for data blocks, mode for inodes */
3425212Smckusic 	u_long (*allocator)();
3434359Smckusick {
3445965Smckusic 	register struct fs *fs;
3454359Smckusick 	long result;
3464359Smckusick 	int i, icg = cg;
3474359Smckusick 
3485965Smckusic 	fs = ip->i_fs;
3494359Smckusick 	/*
3504359Smckusick 	 * 1: preferred cylinder group
3514359Smckusick 	 */
3525965Smckusic 	result = (*allocator)(ip, cg, pref, size);
3534359Smckusick 	if (result)
3544359Smckusick 		return (result);
3554359Smckusick 	/*
3564359Smckusick 	 * 2: quadratic rehash
3574359Smckusick 	 */
3584359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
3594359Smckusick 		cg += i;
3604359Smckusick 		if (cg >= fs->fs_ncg)
3614359Smckusick 			cg -= fs->fs_ncg;
3625965Smckusic 		result = (*allocator)(ip, cg, 0, size);
3634359Smckusick 		if (result)
3644359Smckusick 			return (result);
3654359Smckusick 	}
3664359Smckusick 	/*
3674359Smckusick 	 * 3: brute force search
36810847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
36910847Ssam 	 * and 1 is always checked in the quadratic rehash.
3704359Smckusick 	 */
37110848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
37210847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
3735965Smckusic 		result = (*allocator)(ip, cg, 0, size);
3744359Smckusick 		if (result)
3754359Smckusick 			return (result);
3764359Smckusick 		cg++;
3774359Smckusick 		if (cg == fs->fs_ncg)
3784359Smckusick 			cg = 0;
3794359Smckusick 	}
3806294Smckusick 	return (NULL);
3814359Smckusick }
3824359Smckusick 
3835375Smckusic /*
3845375Smckusic  * Determine whether a fragment can be extended.
3855375Smckusic  *
3865375Smckusic  * Check to see if the necessary fragments are available, and
3875375Smckusic  * if they are, allocate them.
3885375Smckusic  */
3894359Smckusick daddr_t
3905965Smckusic fragextend(ip, cg, bprev, osize, nsize)
3915965Smckusic 	struct inode *ip;
3924426Smckusic 	int cg;
3934463Smckusic 	long bprev;
3944426Smckusic 	int osize, nsize;
3954426Smckusic {
3965965Smckusic 	register struct fs *fs;
3974463Smckusic 	register struct buf *bp;
3984463Smckusic 	register struct cg *cgp;
3994463Smckusic 	long bno;
4004463Smckusic 	int frags, bbase;
4014426Smckusic 	int i;
4024426Smckusic 
4035965Smckusic 	fs = ip->i_fs;
4046531Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < nsize - osize)
4056531Smckusick 		return (NULL);
4065960Smckusic 	frags = numfrags(fs, nsize);
4075960Smckusic 	bbase = fragoff(fs, bprev);
4085322Smckusic 	if (bbase > (bprev + frags - 1) % fs->fs_frag) {
4094463Smckusic 		/* cannot extend across a block boundry */
4106294Smckusick 		return (NULL);
4114463Smckusic 	}
41210278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
4136531Smckusick 	cgp = bp->b_un.b_cg;
4146531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
4155960Smckusic 		brelse(bp);
4166294Smckusick 		return (NULL);
4175960Smckusic 	}
4188105Sroot 	cgp->cg_time = time.tv_sec;
4195377Smckusic 	bno = dtogd(fs, bprev);
4205960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
4215361Smckusic 		if (isclr(cgp->cg_free, bno + i)) {
4225361Smckusic 			brelse(bp);
4236294Smckusick 			return (NULL);
4245361Smckusic 		}
4255361Smckusic 	/*
4265361Smckusic 	 * the current fragment can be extended
4275361Smckusic 	 * deduct the count on fragment being extended into
4285361Smckusic 	 * increase the count on the remaining fragment (if any)
4295361Smckusic 	 * allocate the extended piece
4305361Smckusic 	 */
4315361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
4324463Smckusic 		if (isclr(cgp->cg_free, bno + i))
4334463Smckusic 			break;
4345960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
4355361Smckusic 	if (i != frags)
4365361Smckusic 		cgp->cg_frsum[i - frags]++;
4375960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
4385361Smckusic 		clrbit(cgp->cg_free, bno + i);
4395361Smckusic 		cgp->cg_cs.cs_nffree--;
4405361Smckusic 		fs->fs_cstotal.cs_nffree--;
4415361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
4424463Smckusic 	}
4435361Smckusic 	fs->fs_fmod++;
4445361Smckusic 	bdwrite(bp);
4455361Smckusic 	return (bprev);
4464426Smckusic }
4474426Smckusic 
4485375Smckusic /*
4495375Smckusic  * Determine whether a block can be allocated.
4505375Smckusic  *
4515375Smckusic  * Check to see if a block of the apprpriate size is available,
4525375Smckusic  * and if it is, allocate it.
4535375Smckusic  */
4549163Ssam daddr_t
4555965Smckusic alloccg(ip, cg, bpref, size)
4565965Smckusic 	struct inode *ip;
4574359Smckusick 	int cg;
4584359Smckusick 	daddr_t bpref;
4594359Smckusick 	int size;
4604359Smckusick {
4615965Smckusic 	register struct fs *fs;
4624463Smckusic 	register struct buf *bp;
4634463Smckusic 	register struct cg *cgp;
4644463Smckusic 	int bno, frags;
4654463Smckusic 	int allocsiz;
4664463Smckusic 	register int i;
4674359Smckusick 
4685965Smckusic 	fs = ip->i_fs;
4695322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
4706294Smckusick 		return (NULL);
47110278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
4726531Smckusick 	cgp = bp->b_un.b_cg;
4736531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
4745960Smckusic 		brelse(bp);
4756294Smckusick 		return (NULL);
4765960Smckusic 	}
47710278Smckusick 	if (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)
47810278Smckusick 		return (NULL);
4798105Sroot 	cgp->cg_time = time.tv_sec;
4805322Smckusic 	if (size == fs->fs_bsize) {
4815212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
4824463Smckusic 		bdwrite(bp);
4834463Smckusic 		return (bno);
4844463Smckusic 	}
4854463Smckusic 	/*
4864463Smckusic 	 * check to see if any fragments are already available
4874463Smckusic 	 * allocsiz is the size which will be allocated, hacking
4884463Smckusic 	 * it down to a smaller size if necessary
4894463Smckusic 	 */
4905960Smckusic 	frags = numfrags(fs, size);
4915322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
4924463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
4934463Smckusic 			break;
4945322Smckusic 	if (allocsiz == fs->fs_frag) {
4954463Smckusic 		/*
4964463Smckusic 		 * no fragments were available, so a block will be
4974463Smckusic 		 * allocated, and hacked up
4984463Smckusic 		 */
4994792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
5004463Smckusic 			brelse(bp);
5016294Smckusick 			return (NULL);
5024463Smckusic 		}
5035212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5045377Smckusic 		bpref = dtogd(fs, bno);
5055322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
5064463Smckusic 			setbit(cgp->cg_free, bpref + i);
5075322Smckusic 		i = fs->fs_frag - frags;
5084792Smckusic 		cgp->cg_cs.cs_nffree += i;
5094792Smckusic 		fs->fs_cstotal.cs_nffree += i;
5105322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
5119762Ssam 		fs->fs_fmod++;
5124463Smckusic 		cgp->cg_frsum[i]++;
5134463Smckusic 		bdwrite(bp);
5144463Smckusic 		return (bno);
5154463Smckusic 	}
5164651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
5176567Smckusic 	if (bno < 0)
5186294Smckusick 		return (NULL);
5194463Smckusic 	for (i = 0; i < frags; i++)
5204463Smckusic 		clrbit(cgp->cg_free, bno + i);
5214792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
5224792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
5235322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
5249762Ssam 	fs->fs_fmod++;
5254463Smckusic 	cgp->cg_frsum[allocsiz]--;
5264463Smckusic 	if (frags != allocsiz)
5274463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
5284463Smckusic 	bdwrite(bp);
5294463Smckusic 	return (cg * fs->fs_fpg + bno);
5304463Smckusic }
5314463Smckusic 
5325375Smckusic /*
5335375Smckusic  * Allocate a block in a cylinder group.
5345375Smckusic  *
5355375Smckusic  * This algorithm implements the following policy:
5365375Smckusic  *   1) allocate the requested block.
5375375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
5385375Smckusic  *   3) allocate the next available block on the block rotor for the
5395375Smckusic  *      specified cylinder group.
5405375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
5415375Smckusic  * blocks may be fragmented by the routine that allocates them.
5425375Smckusic  */
5434463Smckusic daddr_t
5445212Smckusic alloccgblk(fs, cgp, bpref)
5455965Smckusic 	register struct fs *fs;
5464463Smckusic 	register struct cg *cgp;
5474463Smckusic 	daddr_t bpref;
5484463Smckusic {
5494651Smckusic 	daddr_t bno;
5506294Smckusick 	int cylno, pos, delta;
5514651Smckusic 	short *cylbp;
5525361Smckusic 	register int i;
5534463Smckusic 
5544651Smckusic 	if (bpref == 0) {
5554651Smckusic 		bpref = cgp->cg_rotor;
5565361Smckusic 		goto norot;
5575361Smckusic 	}
5585361Smckusic 	bpref &= ~(fs->fs_frag - 1);
5595377Smckusic 	bpref = dtogd(fs, bpref);
5605361Smckusic 	/*
5615361Smckusic 	 * if the requested block is available, use it
5625361Smckusic 	 */
563*11638Ssam 	if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) {
5645361Smckusic 		bno = bpref;
5655361Smckusic 		goto gotit;
5665361Smckusic 	}
5675361Smckusic 	/*
5685361Smckusic 	 * check for a block available on the same cylinder
5695361Smckusic 	 */
5705361Smckusic 	cylno = cbtocylno(fs, bpref);
5715375Smckusic 	if (cgp->cg_btot[cylno] == 0)
5725375Smckusic 		goto norot;
5735375Smckusic 	if (fs->fs_cpc == 0) {
5745375Smckusic 		/*
5755375Smckusic 		 * block layout info is not available, so just have
5765375Smckusic 		 * to take any block in this cylinder.
5775375Smckusic 		 */
5785375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
5795375Smckusic 		goto norot;
5805375Smckusic 	}
5815375Smckusic 	/*
5825361Smckusic 	 * check the summary information to see if a block is
5835361Smckusic 	 * available in the requested cylinder starting at the
5849163Ssam 	 * requested rotational position and proceeding around.
5855361Smckusic 	 */
5869163Ssam 	cylbp = cgp->cg_b[cylno];
5879163Ssam 	pos = cbtorpos(fs, bpref);
5885361Smckusic 	for (i = pos; i < NRPOS; i++)
5895361Smckusic 		if (cylbp[i] > 0)
5905361Smckusic 			break;
5915361Smckusic 	if (i == NRPOS)
5925361Smckusic 		for (i = 0; i < pos; i++)
5935361Smckusic 			if (cylbp[i] > 0)
5945361Smckusic 				break;
5955361Smckusic 	if (cylbp[i] > 0) {
5964651Smckusic 		/*
5975361Smckusic 		 * found a rotational position, now find the actual
5985361Smckusic 		 * block. A panic if none is actually there.
5994651Smckusic 		 */
6005361Smckusic 		pos = cylno % fs->fs_cpc;
6015361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
6026716Smckusick 		if (fs->fs_postbl[pos][i] == -1) {
6036716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
6046716Smckusick 			    pos, i, fs->fs_fsmnt);
6055361Smckusic 			panic("alloccgblk: cyl groups corrupted");
6066716Smckusick 		}
6076294Smckusick 		for (i = fs->fs_postbl[pos][i];; ) {
6085361Smckusic 			if (isblock(fs, cgp->cg_free, bno + i)) {
609*11638Ssam 				bno = blkstofrags(fs, (bno + i));
6105361Smckusic 				goto gotit;
6115361Smckusic 			}
6126294Smckusick 			delta = fs->fs_rotbl[i];
6136294Smckusick 			if (delta <= 0 || delta > MAXBPC - i)
6144651Smckusic 				break;
6156294Smckusick 			i += delta;
6164651Smckusic 		}
6176716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
6185361Smckusic 		panic("alloccgblk: can't find blk in cyl");
6194359Smckusick 	}
6205361Smckusic norot:
6215361Smckusic 	/*
6225361Smckusic 	 * no blocks in the requested cylinder, so take next
6235361Smckusic 	 * available one in this cylinder group.
6245361Smckusic 	 */
6258628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
6266567Smckusic 	if (bno < 0)
6276294Smckusick 		return (NULL);
6284651Smckusic 	cgp->cg_rotor = bno;
6294359Smckusick gotit:
630*11638Ssam 	clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno));
6314792Smckusic 	cgp->cg_cs.cs_nbfree--;
6324792Smckusic 	fs->fs_cstotal.cs_nbfree--;
6335322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
6345375Smckusic 	cylno = cbtocylno(fs, bno);
6355375Smckusic 	cgp->cg_b[cylno][cbtorpos(fs, bno)]--;
6365375Smckusic 	cgp->cg_btot[cylno]--;
6374359Smckusick 	fs->fs_fmod++;
6384651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
6394359Smckusick }
6404359Smckusick 
6415375Smckusic /*
6425375Smckusic  * Determine whether an inode can be allocated.
6435375Smckusic  *
6445375Smckusic  * Check to see if an inode is available, and if it is,
6455375Smckusic  * allocate it using the following policy:
6465375Smckusic  *   1) allocate the requested inode.
6475375Smckusic  *   2) allocate the next available inode after the requested
6485375Smckusic  *      inode in the specified cylinder group.
6495375Smckusic  */
6509163Ssam ino_t
6515965Smckusic ialloccg(ip, cg, ipref, mode)
6525965Smckusic 	struct inode *ip;
6534359Smckusick 	int cg;
6544359Smckusick 	daddr_t ipref;
6554359Smckusick 	int mode;
6564359Smckusick {
6575965Smckusic 	register struct fs *fs;
6584463Smckusic 	register struct buf *bp;
6594463Smckusic 	register struct cg *cgp;
6604359Smckusick 	int i;
6614359Smckusick 
6625965Smckusic 	fs = ip->i_fs;
6635322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
6646294Smckusick 		return (NULL);
66510278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
6666531Smckusick 	cgp = bp->b_un.b_cg;
6676531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
6685960Smckusic 		brelse(bp);
6696294Smckusick 		return (NULL);
6705960Smckusic 	}
67110278Smckusick 	if (cgp->cg_cs.cs_nifree == 0)
67210278Smckusick 		return (NULL);
6738105Sroot 	cgp->cg_time = time.tv_sec;
6744359Smckusick 	if (ipref) {
6754359Smckusick 		ipref %= fs->fs_ipg;
6764359Smckusick 		if (isclr(cgp->cg_iused, ipref))
6774359Smckusick 			goto gotit;
6784359Smckusick 	} else
6794359Smckusick 		ipref = cgp->cg_irotor;
6804359Smckusick 	for (i = 0; i < fs->fs_ipg; i++) {
6814359Smckusick 		ipref++;
6824359Smckusick 		if (ipref >= fs->fs_ipg)
6834359Smckusick 			ipref = 0;
6844359Smckusick 		if (isclr(cgp->cg_iused, ipref)) {
6854359Smckusick 			cgp->cg_irotor = ipref;
6864359Smckusick 			goto gotit;
6874359Smckusick 		}
6884359Smckusick 	}
6894359Smckusick 	brelse(bp);
6906294Smckusick 	return (NULL);
6914359Smckusick gotit:
6924359Smckusick 	setbit(cgp->cg_iused, ipref);
6934792Smckusic 	cgp->cg_cs.cs_nifree--;
6944792Smckusic 	fs->fs_cstotal.cs_nifree--;
6955322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
6964359Smckusick 	fs->fs_fmod++;
6974359Smckusick 	if ((mode & IFMT) == IFDIR) {
6984792Smckusic 		cgp->cg_cs.cs_ndir++;
6994792Smckusic 		fs->fs_cstotal.cs_ndir++;
7005322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
7014359Smckusick 	}
7024359Smckusick 	bdwrite(bp);
7034359Smckusick 	return (cg * fs->fs_ipg + ipref);
7044359Smckusick }
7054359Smckusick 
7065375Smckusic /*
7075375Smckusic  * Free a block or fragment.
7085375Smckusic  *
7095375Smckusic  * The specified block or fragment is placed back in the
7105375Smckusic  * free map. If a fragment is deallocated, a possible
7115375Smckusic  * block reassembly is checked.
7125375Smckusic  */
7139163Ssam free(ip, bno, size)
7145965Smckusic 	register struct inode *ip;
7154359Smckusick 	daddr_t bno;
7165212Smckusic 	off_t size;
7174359Smckusick {
7184359Smckusick 	register struct fs *fs;
7194359Smckusick 	register struct cg *cgp;
7204359Smckusick 	register struct buf *bp;
7214463Smckusic 	int cg, blk, frags, bbase;
7224463Smckusic 	register int i;
7234359Smckusick 
7245965Smckusic 	fs = ip->i_fs;
7256716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
7266716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
7276716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7284426Smckusic 		panic("free: bad size");
7296716Smckusick 	}
7305377Smckusic 	cg = dtog(fs, bno);
7316567Smckusic 	if (badblock(fs, bno)) {
7326567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
7334359Smckusick 		return;
7346567Smckusic 	}
73510278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
7366531Smckusick 	cgp = bp->b_un.b_cg;
7376531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
7385960Smckusic 		brelse(bp);
7394359Smckusick 		return;
7405960Smckusic 	}
7418105Sroot 	cgp->cg_time = time.tv_sec;
7425377Smckusic 	bno = dtogd(fs, bno);
7435322Smckusic 	if (size == fs->fs_bsize) {
744*11638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) {
7456716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
7466716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
7474426Smckusic 			panic("free: freeing free block");
7486567Smckusic 		}
749*11638Ssam 		setblock(fs, cgp->cg_free, fragstoblks(fs, bno));
7504792Smckusic 		cgp->cg_cs.cs_nbfree++;
7514792Smckusic 		fs->fs_cstotal.cs_nbfree++;
7525322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
7535375Smckusic 		i = cbtocylno(fs, bno);
7545375Smckusic 		cgp->cg_b[i][cbtorpos(fs, bno)]++;
7555375Smckusic 		cgp->cg_btot[i]++;
7564426Smckusic 	} else {
7575322Smckusic 		bbase = bno - (bno % fs->fs_frag);
7584463Smckusic 		/*
7594463Smckusic 		 * decrement the counts associated with the old frags
7604463Smckusic 		 */
7616294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
7625322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
7634463Smckusic 		/*
7644463Smckusic 		 * deallocate the fragment
7654463Smckusic 		 */
7665960Smckusic 		frags = numfrags(fs, size);
7674463Smckusic 		for (i = 0; i < frags; i++) {
7686716Smckusick 			if (isset(cgp->cg_free, bno + i)) {
7696716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
7706716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
7714426Smckusic 				panic("free: freeing free frag");
7726716Smckusick 			}
7734426Smckusic 			setbit(cgp->cg_free, bno + i);
7744426Smckusic 		}
7756294Smckusick 		cgp->cg_cs.cs_nffree += i;
7766294Smckusick 		fs->fs_cstotal.cs_nffree += i;
7776294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
7784463Smckusic 		/*
7794463Smckusic 		 * add back in counts associated with the new frags
7804463Smckusic 		 */
7816294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
7825322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
7834463Smckusic 		/*
7844463Smckusic 		 * if a complete block has been reassembled, account for it
7854463Smckusic 		 */
786*11638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) {
7875322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
7885322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
7895322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
7904792Smckusic 			cgp->cg_cs.cs_nbfree++;
7914792Smckusic 			fs->fs_cstotal.cs_nbfree++;
7925322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
7935375Smckusic 			i = cbtocylno(fs, bbase);
7945375Smckusic 			cgp->cg_b[i][cbtorpos(fs, bbase)]++;
7955375Smckusic 			cgp->cg_btot[i]++;
7964426Smckusic 		}
7974426Smckusic 	}
7984359Smckusick 	fs->fs_fmod++;
7994359Smckusick 	bdwrite(bp);
8004359Smckusick }
8014359Smckusick 
8025375Smckusic /*
8035375Smckusic  * Free an inode.
8045375Smckusic  *
8055375Smckusic  * The specified inode is placed back in the free map.
8065375Smckusic  */
8075965Smckusic ifree(ip, ino, mode)
8085965Smckusic 	struct inode *ip;
8094359Smckusick 	ino_t ino;
8104359Smckusick 	int mode;
8114359Smckusick {
8124359Smckusick 	register struct fs *fs;
8134359Smckusick 	register struct cg *cgp;
8144359Smckusick 	register struct buf *bp;
8154359Smckusick 	int cg;
8164359Smckusick 
8175965Smckusic 	fs = ip->i_fs;
8186716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
8196716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
8206716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
8214359Smckusick 		panic("ifree: range");
8226716Smckusick 	}
8235377Smckusic 	cg = itog(fs, ino);
82410278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
8256531Smckusick 	cgp = bp->b_un.b_cg;
8266531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
8275960Smckusic 		brelse(bp);
8284359Smckusick 		return;
8295960Smckusic 	}
8308105Sroot 	cgp->cg_time = time.tv_sec;
8314359Smckusick 	ino %= fs->fs_ipg;
8326716Smckusick 	if (isclr(cgp->cg_iused, ino)) {
8336716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
8346716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
8354359Smckusick 		panic("ifree: freeing free inode");
8366716Smckusick 	}
8374359Smckusick 	clrbit(cgp->cg_iused, ino);
8384792Smckusic 	cgp->cg_cs.cs_nifree++;
8394792Smckusic 	fs->fs_cstotal.cs_nifree++;
8405322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
8414359Smckusick 	if ((mode & IFMT) == IFDIR) {
8424792Smckusic 		cgp->cg_cs.cs_ndir--;
8434792Smckusic 		fs->fs_cstotal.cs_ndir--;
8445322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
8454359Smckusick 	}
8464359Smckusick 	fs->fs_fmod++;
8474359Smckusick 	bdwrite(bp);
8484359Smckusick }
8494359Smckusick 
8504463Smckusic /*
8515375Smckusic  * Find a block of the specified size in the specified cylinder group.
8525375Smckusic  *
8534651Smckusic  * It is a panic if a request is made to find a block if none are
8544651Smckusic  * available.
8554651Smckusic  */
8564651Smckusic daddr_t
8574651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
8584651Smckusic 	register struct fs *fs;
8594651Smckusic 	register struct cg *cgp;
8604651Smckusic 	daddr_t bpref;
8614651Smckusic 	int allocsiz;
8624651Smckusic {
8634651Smckusic 	daddr_t bno;
8644651Smckusic 	int start, len, loc, i;
8654651Smckusic 	int blk, field, subfield, pos;
8664651Smckusic 
8674651Smckusic 	/*
8684651Smckusic 	 * find the fragment by searching through the free block
8694651Smckusic 	 * map for an appropriate bit pattern
8704651Smckusic 	 */
8714651Smckusic 	if (bpref)
8725377Smckusic 		start = dtogd(fs, bpref) / NBBY;
8734651Smckusic 	else
8744651Smckusic 		start = cgp->cg_frotor / NBBY;
8755398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
8765322Smckusic 	loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag],
8776292Smckusick 		1 << (allocsiz - 1 + (fs->fs_frag % NBBY)));
8784651Smckusic 	if (loc == 0) {
8796531Smckusick 		len = start + 1;
8806531Smckusick 		start = 0;
8815322Smckusic 		loc = scanc(len, &cgp->cg_free[start], fragtbl[fs->fs_frag],
8826292Smckusick 			1 << (allocsiz - 1 + (fs->fs_frag % NBBY)));
8839762Ssam 		if (loc == 0)
8846531Smckusick 			return (-1);
8854651Smckusic 	}
8864651Smckusic 	bno = (start + len - loc) * NBBY;
8874651Smckusic 	cgp->cg_frotor = bno;
8884651Smckusic 	/*
8894651Smckusic 	 * found the byte in the map
8904651Smckusic 	 * sift through the bits to find the selected frag
8914651Smckusic 	 */
8926294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
8936294Smckusick 		blk = blkmap(fs, cgp->cg_free, bno);
8944651Smckusic 		blk <<= 1;
8954651Smckusic 		field = around[allocsiz];
8964651Smckusic 		subfield = inside[allocsiz];
8975322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
8986294Smckusick 			if ((blk & field) == subfield)
8996294Smckusick 				return (bno + pos);
9004651Smckusic 			field <<= 1;
9014651Smckusic 			subfield <<= 1;
9024651Smckusic 		}
9034651Smckusic 	}
9046716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
9054651Smckusic 	panic("alloccg: block not in map");
9066531Smckusick 	return (-1);
9074651Smckusic }
9084651Smckusic 
9094651Smckusic /*
9105375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
9115375Smckusic  *
9125375Smckusic  * The form of the error message is:
9134359Smckusick  *	fs: error message
9144359Smckusick  */
9154359Smckusick fserr(fs, cp)
9164359Smckusick 	struct fs *fs;
9174359Smckusick 	char *cp;
9184359Smckusick {
9194359Smckusick 
9204359Smckusick 	printf("%s: %s\n", fs->fs_fsmnt, cp);
9214359Smckusick }
922