xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 17709)
1*17709Smckusick /*	lfs_alloc.c	6.11	85/01/14	*/
24359Smckusick 
317097Sbloom #include "param.h"
417097Sbloom #include "systm.h"
517097Sbloom #include "mount.h"
617097Sbloom #include "fs.h"
717097Sbloom #include "conf.h"
817097Sbloom #include "buf.h"
917097Sbloom #include "inode.h"
1017097Sbloom #include "dir.h"
1117097Sbloom #include "user.h"
1217097Sbloom #include "quota.h"
1317097Sbloom #include "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;
6311638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
644792Smckusic 		goto nospace;
657650Ssam #ifdef QUOTA
6612643Ssam 	u.u_error = chkdq(ip, (long)btodb(size), 0);
6712643Ssam 	if (u.u_error)
6812643Ssam 		return (NULL);
697483Skre #endif
704948Smckusic 	if (bpref >= fs->fs_size)
714948Smckusic 		bpref = 0;
724359Smckusick 	if (bpref == 0)
735377Smckusic 		cg = itog(fs, ip->i_number);
744359Smckusick 	else
755377Smckusic 		cg = dtog(fs, bpref);
769163Ssam 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
779163Ssam 		(u_long (*)())alloccg);
786567Smckusic 	if (bno <= 0)
794359Smckusick 		goto nospace;
8012643Ssam 	ip->i_blocks += btodb(size);
8112643Ssam 	ip->i_flag |= IUPD|ICHG;
825965Smckusic 	bp = getblk(ip->i_dev, fsbtodb(fs, bno), size);
834359Smckusick 	clrbuf(bp);
844359Smckusick 	return (bp);
854359Smckusick nospace:
864359Smckusick 	fserr(fs, "file system full");
874359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
884359Smckusick 	u.u_error = ENOSPC;
894359Smckusick 	return (NULL);
904359Smckusick }
914359Smckusick 
925375Smckusic /*
935375Smckusic  * Reallocate a fragment to a bigger size
945375Smckusic  *
955375Smckusic  * The number and size of the old block is given, and a preference
965375Smckusic  * and new size is also specified. The allocator attempts to extend
975375Smckusic  * the original block. Failing that, the regular block allocator is
985375Smckusic  * invoked to get an appropriate block.
995375Smckusic  */
1004426Smckusic struct buf *
1015965Smckusic realloccg(ip, bprev, bpref, osize, nsize)
1025965Smckusic 	register struct inode *ip;
1034651Smckusic 	daddr_t bprev, bpref;
1044426Smckusic 	int osize, nsize;
1054426Smckusic {
1064426Smckusic 	daddr_t bno;
1074426Smckusic 	register struct fs *fs;
1084463Smckusic 	register struct buf *bp, *obp;
1094426Smckusic 	int cg;
1104426Smckusic 
1115965Smckusic 	fs = ip->i_fs;
1125960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1136716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1146716Smckusick 		printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1156716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
1164463Smckusic 		panic("realloccg: bad size");
1176716Smckusick 	}
11811638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1194792Smckusic 		goto nospace;
1206716Smckusick 	if (bprev == 0) {
1216716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1226716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1234463Smckusic 		panic("realloccg: bad bprev");
1246716Smckusick 	}
1257650Ssam #ifdef QUOTA
12612643Ssam 	u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0);
12712643Ssam 	if (u.u_error)
12812643Ssam 		return (NULL);
1297483Skre #endif
1306294Smckusick 	cg = dtog(fs, bprev);
1315965Smckusic 	bno = fragextend(ip, cg, (long)bprev, osize, nsize);
1324463Smckusic 	if (bno != 0) {
1337187Sroot 		do {
1347187Sroot 			bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
1357187Sroot 			if (bp->b_flags & B_ERROR) {
1367187Sroot 				brelse(bp);
1377187Sroot 				return (NULL);
1387187Sroot 			}
1397187Sroot 		} while (brealloc(bp, nsize) == 0);
1407187Sroot 		bp->b_flags |= B_DONE;
1419163Ssam 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
14212643Ssam 		ip->i_blocks += btodb(nsize - osize);
14312643Ssam 		ip->i_flag |= IUPD|ICHG;
1444463Smckusic 		return (bp);
1454463Smckusic 	}
1464948Smckusic 	if (bpref >= fs->fs_size)
1474948Smckusic 		bpref = 0;
148*17709Smckusick 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, fs->fs_bsize,
1499163Ssam 		(u_long (*)())alloccg);
1506567Smckusic 	if (bno > 0) {
1515965Smckusic 		obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
1525960Smckusic 		if (obp->b_flags & B_ERROR) {
1535960Smckusic 			brelse(obp);
1546294Smckusick 			return (NULL);
1555960Smckusic 		}
15617658Smckusick 		bp = getblk(ip->i_dev, fsbtodb(fs, bno), nsize);
15717658Smckusick 		bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
15817658Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
15917658Smckusick 		if (obp->b_flags & B_DELWRI) {
16017658Smckusick 			obp->b_flags &= ~B_DELWRI;
16117658Smckusick 			u.u_ru.ru_oublock--;		/* delete charge */
16217658Smckusick 		}
1634463Smckusic 		brelse(obp);
1649163Ssam 		free(ip, bprev, (off_t)osize);
165*17709Smckusick 		if (nsize < fs->fs_bsize)
166*17709Smckusick 			free(ip, bno + numfrags(fs, nsize),
167*17709Smckusick 				(off_t)(fs->fs_bsize - nsize));
16812643Ssam 		ip->i_blocks += btodb(nsize - osize);
16912643Ssam 		ip->i_flag |= IUPD|ICHG;
1706294Smckusick 		return (bp);
1714463Smckusic 	}
1724792Smckusic nospace:
1734463Smckusic 	/*
1744463Smckusic 	 * no space available
1754463Smckusic 	 */
1764426Smckusic 	fserr(fs, "file system full");
1774426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
1784426Smckusic 	u.u_error = ENOSPC;
1794426Smckusic 	return (NULL);
1804426Smckusic }
1814426Smckusic 
1825375Smckusic /*
1835375Smckusic  * Allocate an inode in the file system.
1845375Smckusic  *
1855375Smckusic  * A preference may be optionally specified. If a preference is given
1865375Smckusic  * the following hierarchy is used to allocate an inode:
1875375Smckusic  *   1) allocate the requested inode.
1885375Smckusic  *   2) allocate an inode in the same cylinder group.
1895375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
1905375Smckusic  *      available inode is located.
1915375Smckusic  * If no inode preference is given the following heirarchy is used
1925375Smckusic  * to allocate an inode:
1935375Smckusic  *   1) allocate an inode in cylinder group 0.
1945375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
1955375Smckusic  *      available inode is located.
1965375Smckusic  */
1974359Smckusick struct inode *
1985965Smckusic ialloc(pip, ipref, mode)
1995965Smckusic 	register struct inode *pip;
2004359Smckusick 	ino_t ipref;
2014359Smckusick 	int mode;
2024359Smckusick {
2035212Smckusic 	ino_t ino;
2044359Smckusick 	register struct fs *fs;
2054359Smckusick 	register struct inode *ip;
2064359Smckusick 	int cg;
2074359Smckusick 
2085965Smckusic 	fs = pip->i_fs;
2094792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2104359Smckusick 		goto noinodes;
2117650Ssam #ifdef QUOTA
21212643Ssam 	u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
21312643Ssam 	if (u.u_error)
21412643Ssam 		return (NULL);
2157483Skre #endif
2164948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2174948Smckusic 		ipref = 0;
2185377Smckusic 	cg = itog(fs, ipref);
2195965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2204359Smckusick 	if (ino == 0)
2214359Smckusick 		goto noinodes;
2225965Smckusic 	ip = iget(pip->i_dev, pip->i_fs, ino);
2234359Smckusick 	if (ip == NULL) {
22415120Skarels 		ifree(pip, ino, 0);
2254359Smckusick 		return (NULL);
2264359Smckusick 	}
2276716Smckusick 	if (ip->i_mode) {
2286716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
2296716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2304359Smckusick 		panic("ialloc: dup alloc");
2316716Smckusick 	}
23212643Ssam 	if (ip->i_blocks) {				/* XXX */
23312643Ssam 		printf("free inode %s/%d had %d blocks\n",
23412643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
23512643Ssam 		ip->i_blocks = 0;
23612643Ssam 	}
2374359Smckusick 	return (ip);
2384359Smckusick noinodes:
2394359Smckusick 	fserr(fs, "out of inodes");
2406294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
2414359Smckusick 	u.u_error = ENOSPC;
2424359Smckusick 	return (NULL);
2434359Smckusick }
2444359Smckusick 
2454651Smckusic /*
2465375Smckusic  * Find a cylinder to place a directory.
2475375Smckusic  *
2485375Smckusic  * The policy implemented by this algorithm is to select from
2495375Smckusic  * among those cylinder groups with above the average number of
2505375Smckusic  * free inodes, the one with the smallest number of directories.
2514651Smckusic  */
2529163Ssam ino_t
2535965Smckusic dirpref(fs)
2545965Smckusic 	register struct fs *fs;
2554359Smckusick {
2564651Smckusic 	int cg, minndir, mincg, avgifree;
2574359Smckusick 
2584792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
2594651Smckusic 	minndir = fs->fs_ipg;
2604359Smckusick 	mincg = 0;
2614651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
2625322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
2635322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
2644359Smckusick 			mincg = cg;
2655322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
2664359Smckusick 		}
2679163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
2684359Smckusick }
2694359Smckusick 
2704651Smckusic /*
2719163Ssam  * Select the desired position for the next block in a file.  The file is
2729163Ssam  * logically divided into sections. The first section is composed of the
2739163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
2749163Ssam  *
2759163Ssam  * If no blocks have been allocated in the first section, the policy is to
2769163Ssam  * request a block in the same cylinder group as the inode that describes
2779163Ssam  * the file. If no blocks have been allocated in any other section, the
2789163Ssam  * policy is to place the section in a cylinder group with a greater than
2799163Ssam  * average number of free blocks.  An appropriate cylinder group is found
28017696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
28117696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
28217696Smckusick  * cylinder group from which the previous allocation was made. The sweep
28317696Smckusick  * continues until a cylinder group with greater than the average number
28417696Smckusick  * of free blocks is found. If the allocation is for the first block in an
28517696Smckusick  * indirect block, the information on the previous allocation is unavailable;
28617696Smckusick  * here a best guess is made based upon the logical block number being
28717696Smckusick  * allocated.
2889163Ssam  *
2899163Ssam  * If a section is already partially allocated, the policy is to
2909163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
2919163Ssam  * contiguous blocks and the beginning of the next is physically separated
2929163Ssam  * so that the disk head will be in transit between them for at least
2939163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
2949163Ssam  * schedule another I/O transfer.
2954651Smckusic  */
2965212Smckusic daddr_t
2979163Ssam blkpref(ip, lbn, indx, bap)
2989163Ssam 	struct inode *ip;
2999163Ssam 	daddr_t lbn;
3009163Ssam 	int indx;
3019163Ssam 	daddr_t *bap;
3029163Ssam {
3035965Smckusic 	register struct fs *fs;
30417696Smckusick 	register int cg;
30517696Smckusick 	int avgbfree, startcg;
3069163Ssam 	daddr_t nextblk;
3074651Smckusic 
3089163Ssam 	fs = ip->i_fs;
3099163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3109163Ssam 		if (lbn < NDADDR) {
3119163Ssam 			cg = itog(fs, ip->i_number);
3125322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3134651Smckusic 		}
3149163Ssam 		/*
3159163Ssam 		 * Find a cylinder with greater than average number of
3169163Ssam 		 * unused data blocks.
3179163Ssam 		 */
31817696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
31917696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
32017696Smckusick 		else
32117696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
32217696Smckusick 		startcg %= fs->fs_ncg;
3239163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
32417696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
3259163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3269163Ssam 				fs->fs_cgrotor = cg;
3279163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3289163Ssam 			}
32917696Smckusick 		for (cg = 0; cg <= startcg; cg++)
3309163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3319163Ssam 				fs->fs_cgrotor = cg;
3329163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3339163Ssam 			}
3349163Ssam 		return (NULL);
3359163Ssam 	}
3369163Ssam 	/*
3379163Ssam 	 * One or more previous blocks have been laid out. If less
3389163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
3399163Ssam 	 * next block is requested contiguously, otherwise it is
3409163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
3419163Ssam 	 */
3429163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
3439163Ssam 	if (indx > fs->fs_maxcontig &&
34411638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
3459163Ssam 	    != nextblk)
3469163Ssam 		return (nextblk);
3479163Ssam 	if (fs->fs_rotdelay != 0)
3489163Ssam 		/*
3499163Ssam 		 * Here we convert ms of delay to frags as:
3509163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
3519163Ssam 		 *	((sect/frag) * (ms/sec))
3529163Ssam 		 * then round up to the next block.
3539163Ssam 		 */
3549163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
3559163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
3569163Ssam 	return (nextblk);
3574651Smckusic }
3584651Smckusic 
3595375Smckusic /*
3605375Smckusic  * Implement the cylinder overflow algorithm.
3615375Smckusic  *
3625375Smckusic  * The policy implemented by this algorithm is:
3635375Smckusic  *   1) allocate the block in its requested cylinder group.
3645375Smckusic  *   2) quadradically rehash on the cylinder group number.
3655375Smckusic  *   3) brute force search for a free block.
3665375Smckusic  */
3675212Smckusic /*VARARGS5*/
3685212Smckusic u_long
3695965Smckusic hashalloc(ip, cg, pref, size, allocator)
3705965Smckusic 	struct inode *ip;
3714359Smckusick 	int cg;
3724359Smckusick 	long pref;
3734359Smckusick 	int size;	/* size for data blocks, mode for inodes */
3745212Smckusic 	u_long (*allocator)();
3754359Smckusick {
3765965Smckusic 	register struct fs *fs;
3774359Smckusick 	long result;
3784359Smckusick 	int i, icg = cg;
3794359Smckusick 
3805965Smckusic 	fs = ip->i_fs;
3814359Smckusick 	/*
3824359Smckusick 	 * 1: preferred cylinder group
3834359Smckusick 	 */
3845965Smckusic 	result = (*allocator)(ip, cg, pref, size);
3854359Smckusick 	if (result)
3864359Smckusick 		return (result);
3874359Smckusick 	/*
3884359Smckusick 	 * 2: quadratic rehash
3894359Smckusick 	 */
3904359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
3914359Smckusick 		cg += i;
3924359Smckusick 		if (cg >= fs->fs_ncg)
3934359Smckusick 			cg -= fs->fs_ncg;
3945965Smckusic 		result = (*allocator)(ip, cg, 0, size);
3954359Smckusick 		if (result)
3964359Smckusick 			return (result);
3974359Smckusick 	}
3984359Smckusick 	/*
3994359Smckusick 	 * 3: brute force search
40010847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
40110847Ssam 	 * and 1 is always checked in the quadratic rehash.
4024359Smckusick 	 */
40310848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
40410847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4055965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4064359Smckusick 		if (result)
4074359Smckusick 			return (result);
4084359Smckusick 		cg++;
4094359Smckusick 		if (cg == fs->fs_ncg)
4104359Smckusick 			cg = 0;
4114359Smckusick 	}
4126294Smckusick 	return (NULL);
4134359Smckusick }
4144359Smckusick 
4155375Smckusic /*
4165375Smckusic  * Determine whether a fragment can be extended.
4175375Smckusic  *
4185375Smckusic  * Check to see if the necessary fragments are available, and
4195375Smckusic  * if they are, allocate them.
4205375Smckusic  */
4214359Smckusick daddr_t
4225965Smckusic fragextend(ip, cg, bprev, osize, nsize)
4235965Smckusic 	struct inode *ip;
4244426Smckusic 	int cg;
4254463Smckusic 	long bprev;
4264426Smckusic 	int osize, nsize;
4274426Smckusic {
4285965Smckusic 	register struct fs *fs;
4294463Smckusic 	register struct buf *bp;
4304463Smckusic 	register struct cg *cgp;
4314463Smckusic 	long bno;
4324463Smckusic 	int frags, bbase;
4334426Smckusic 	int i;
4344426Smckusic 
4355965Smckusic 	fs = ip->i_fs;
43617224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
4376531Smckusick 		return (NULL);
4385960Smckusic 	frags = numfrags(fs, nsize);
43917224Smckusick 	bbase = fragnum(fs, bprev);
44017224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
4414463Smckusic 		/* cannot extend across a block boundry */
4426294Smckusick 		return (NULL);
4434463Smckusic 	}
44410278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
4456531Smckusick 	cgp = bp->b_un.b_cg;
4466531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
4475960Smckusic 		brelse(bp);
4486294Smckusick 		return (NULL);
4495960Smckusic 	}
4508105Sroot 	cgp->cg_time = time.tv_sec;
4515377Smckusic 	bno = dtogd(fs, bprev);
4525960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
4535361Smckusic 		if (isclr(cgp->cg_free, bno + i)) {
4545361Smckusic 			brelse(bp);
4556294Smckusick 			return (NULL);
4565361Smckusic 		}
4575361Smckusic 	/*
4585361Smckusic 	 * the current fragment can be extended
4595361Smckusic 	 * deduct the count on fragment being extended into
4605361Smckusic 	 * increase the count on the remaining fragment (if any)
4615361Smckusic 	 * allocate the extended piece
4625361Smckusic 	 */
4635361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
4644463Smckusic 		if (isclr(cgp->cg_free, bno + i))
4654463Smckusic 			break;
4665960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
4675361Smckusic 	if (i != frags)
4685361Smckusic 		cgp->cg_frsum[i - frags]++;
4695960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
4705361Smckusic 		clrbit(cgp->cg_free, bno + i);
4715361Smckusic 		cgp->cg_cs.cs_nffree--;
4725361Smckusic 		fs->fs_cstotal.cs_nffree--;
4735361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
4744463Smckusic 	}
4755361Smckusic 	fs->fs_fmod++;
4765361Smckusic 	bdwrite(bp);
4775361Smckusic 	return (bprev);
4784426Smckusic }
4794426Smckusic 
4805375Smckusic /*
4815375Smckusic  * Determine whether a block can be allocated.
4825375Smckusic  *
4835375Smckusic  * Check to see if a block of the apprpriate size is available,
4845375Smckusic  * and if it is, allocate it.
4855375Smckusic  */
4869163Ssam daddr_t
4875965Smckusic alloccg(ip, cg, bpref, size)
4885965Smckusic 	struct inode *ip;
4894359Smckusick 	int cg;
4904359Smckusick 	daddr_t bpref;
4914359Smckusick 	int size;
4924359Smckusick {
4935965Smckusic 	register struct fs *fs;
4944463Smckusic 	register struct buf *bp;
4954463Smckusic 	register struct cg *cgp;
4964463Smckusic 	int bno, frags;
4974463Smckusic 	int allocsiz;
4984463Smckusic 	register int i;
4994359Smckusick 
5005965Smckusic 	fs = ip->i_fs;
5015322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5026294Smckusick 		return (NULL);
50310278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
5046531Smckusick 	cgp = bp->b_un.b_cg;
50515950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
50615950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5075960Smckusic 		brelse(bp);
5086294Smckusick 		return (NULL);
5095960Smckusic 	}
5108105Sroot 	cgp->cg_time = time.tv_sec;
5115322Smckusic 	if (size == fs->fs_bsize) {
5125212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5134463Smckusic 		bdwrite(bp);
5144463Smckusic 		return (bno);
5154463Smckusic 	}
5164463Smckusic 	/*
5174463Smckusic 	 * check to see if any fragments are already available
5184463Smckusic 	 * allocsiz is the size which will be allocated, hacking
5194463Smckusic 	 * it down to a smaller size if necessary
5204463Smckusic 	 */
5215960Smckusic 	frags = numfrags(fs, size);
5225322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
5234463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
5244463Smckusic 			break;
5255322Smckusic 	if (allocsiz == fs->fs_frag) {
5264463Smckusic 		/*
5274463Smckusic 		 * no fragments were available, so a block will be
5284463Smckusic 		 * allocated, and hacked up
5294463Smckusic 		 */
5304792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
5314463Smckusic 			brelse(bp);
5326294Smckusick 			return (NULL);
5334463Smckusic 		}
5345212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5355377Smckusic 		bpref = dtogd(fs, bno);
5365322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
5374463Smckusic 			setbit(cgp->cg_free, bpref + i);
5385322Smckusic 		i = fs->fs_frag - frags;
5394792Smckusic 		cgp->cg_cs.cs_nffree += i;
5404792Smckusic 		fs->fs_cstotal.cs_nffree += i;
5415322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
5429762Ssam 		fs->fs_fmod++;
5434463Smckusic 		cgp->cg_frsum[i]++;
5444463Smckusic 		bdwrite(bp);
5454463Smckusic 		return (bno);
5464463Smckusic 	}
5474651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
54815950Smckusick 	if (bno < 0) {
54915950Smckusick 		brelse(bp);
5506294Smckusick 		return (NULL);
55115950Smckusick 	}
5524463Smckusic 	for (i = 0; i < frags; i++)
5534463Smckusic 		clrbit(cgp->cg_free, bno + i);
5544792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
5554792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
5565322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
5579762Ssam 	fs->fs_fmod++;
5584463Smckusic 	cgp->cg_frsum[allocsiz]--;
5594463Smckusic 	if (frags != allocsiz)
5604463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
5614463Smckusic 	bdwrite(bp);
5624463Smckusic 	return (cg * fs->fs_fpg + bno);
5634463Smckusic }
5644463Smckusic 
5655375Smckusic /*
5665375Smckusic  * Allocate a block in a cylinder group.
5675375Smckusic  *
5685375Smckusic  * This algorithm implements the following policy:
5695375Smckusic  *   1) allocate the requested block.
5705375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
5715375Smckusic  *   3) allocate the next available block on the block rotor for the
5725375Smckusic  *      specified cylinder group.
5735375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
5745375Smckusic  * blocks may be fragmented by the routine that allocates them.
5755375Smckusic  */
5764463Smckusic daddr_t
5775212Smckusic alloccgblk(fs, cgp, bpref)
5785965Smckusic 	register struct fs *fs;
5794463Smckusic 	register struct cg *cgp;
5804463Smckusic 	daddr_t bpref;
5814463Smckusic {
5824651Smckusic 	daddr_t bno;
5836294Smckusick 	int cylno, pos, delta;
5844651Smckusic 	short *cylbp;
5855361Smckusic 	register int i;
5864463Smckusic 
5874651Smckusic 	if (bpref == 0) {
5884651Smckusic 		bpref = cgp->cg_rotor;
5895361Smckusic 		goto norot;
5905361Smckusic 	}
59117224Smckusick 	bpref = blknum(fs, bpref);
5925377Smckusic 	bpref = dtogd(fs, bpref);
5935361Smckusic 	/*
5945361Smckusic 	 * if the requested block is available, use it
5955361Smckusic 	 */
59611638Ssam 	if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) {
5975361Smckusic 		bno = bpref;
5985361Smckusic 		goto gotit;
5995361Smckusic 	}
6005361Smckusic 	/*
6015361Smckusic 	 * check for a block available on the same cylinder
6025361Smckusic 	 */
6035361Smckusic 	cylno = cbtocylno(fs, bpref);
6045375Smckusic 	if (cgp->cg_btot[cylno] == 0)
6055375Smckusic 		goto norot;
6065375Smckusic 	if (fs->fs_cpc == 0) {
6075375Smckusic 		/*
6085375Smckusic 		 * block layout info is not available, so just have
6095375Smckusic 		 * to take any block in this cylinder.
6105375Smckusic 		 */
6115375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
6125375Smckusic 		goto norot;
6135375Smckusic 	}
6145375Smckusic 	/*
6155361Smckusic 	 * check the summary information to see if a block is
6165361Smckusic 	 * available in the requested cylinder starting at the
6179163Ssam 	 * requested rotational position and proceeding around.
6185361Smckusic 	 */
6199163Ssam 	cylbp = cgp->cg_b[cylno];
6209163Ssam 	pos = cbtorpos(fs, bpref);
6215361Smckusic 	for (i = pos; i < NRPOS; i++)
6225361Smckusic 		if (cylbp[i] > 0)
6235361Smckusic 			break;
6245361Smckusic 	if (i == NRPOS)
6255361Smckusic 		for (i = 0; i < pos; i++)
6265361Smckusic 			if (cylbp[i] > 0)
6275361Smckusic 				break;
6285361Smckusic 	if (cylbp[i] > 0) {
6294651Smckusic 		/*
6305361Smckusic 		 * found a rotational position, now find the actual
6315361Smckusic 		 * block. A panic if none is actually there.
6324651Smckusic 		 */
6335361Smckusic 		pos = cylno % fs->fs_cpc;
6345361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
6356716Smckusick 		if (fs->fs_postbl[pos][i] == -1) {
6366716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
6376716Smckusick 			    pos, i, fs->fs_fsmnt);
6385361Smckusic 			panic("alloccgblk: cyl groups corrupted");
6396716Smckusick 		}
6406294Smckusick 		for (i = fs->fs_postbl[pos][i];; ) {
6415361Smckusic 			if (isblock(fs, cgp->cg_free, bno + i)) {
64211638Ssam 				bno = blkstofrags(fs, (bno + i));
6435361Smckusic 				goto gotit;
6445361Smckusic 			}
6456294Smckusick 			delta = fs->fs_rotbl[i];
6466294Smckusick 			if (delta <= 0 || delta > MAXBPC - i)
6474651Smckusic 				break;
6486294Smckusick 			i += delta;
6494651Smckusic 		}
6506716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
6515361Smckusic 		panic("alloccgblk: can't find blk in cyl");
6524359Smckusick 	}
6535361Smckusic norot:
6545361Smckusic 	/*
6555361Smckusic 	 * no blocks in the requested cylinder, so take next
6565361Smckusic 	 * available one in this cylinder group.
6575361Smckusic 	 */
6588628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
6596567Smckusic 	if (bno < 0)
6606294Smckusick 		return (NULL);
6614651Smckusic 	cgp->cg_rotor = bno;
6624359Smckusick gotit:
66311638Ssam 	clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno));
6644792Smckusic 	cgp->cg_cs.cs_nbfree--;
6654792Smckusic 	fs->fs_cstotal.cs_nbfree--;
6665322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
6675375Smckusic 	cylno = cbtocylno(fs, bno);
6685375Smckusic 	cgp->cg_b[cylno][cbtorpos(fs, bno)]--;
6695375Smckusic 	cgp->cg_btot[cylno]--;
6704359Smckusick 	fs->fs_fmod++;
6714651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
6724359Smckusick }
6734359Smckusick 
6745375Smckusic /*
6755375Smckusic  * Determine whether an inode can be allocated.
6765375Smckusic  *
6775375Smckusic  * Check to see if an inode is available, and if it is,
6785375Smckusic  * allocate it using the following policy:
6795375Smckusic  *   1) allocate the requested inode.
6805375Smckusic  *   2) allocate the next available inode after the requested
6815375Smckusic  *      inode in the specified cylinder group.
6825375Smckusic  */
6839163Ssam ino_t
6845965Smckusic ialloccg(ip, cg, ipref, mode)
6855965Smckusic 	struct inode *ip;
6864359Smckusick 	int cg;
6874359Smckusick 	daddr_t ipref;
6884359Smckusick 	int mode;
6894359Smckusick {
6905965Smckusic 	register struct fs *fs;
6914463Smckusic 	register struct cg *cgp;
69216784Smckusick 	struct buf *bp;
69316784Smckusick 	int start, len, loc, map, i;
6944359Smckusick 
6955965Smckusic 	fs = ip->i_fs;
6965322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
6976294Smckusick 		return (NULL);
69810278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
6996531Smckusick 	cgp = bp->b_un.b_cg;
70015950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
70115950Smckusick 	    cgp->cg_cs.cs_nifree == 0) {
7025960Smckusic 		brelse(bp);
7036294Smckusick 		return (NULL);
7045960Smckusic 	}
7058105Sroot 	cgp->cg_time = time.tv_sec;
7064359Smckusick 	if (ipref) {
7074359Smckusick 		ipref %= fs->fs_ipg;
7084359Smckusick 		if (isclr(cgp->cg_iused, ipref))
7094359Smckusick 			goto gotit;
71016784Smckusick 	}
71116784Smckusick 	start = cgp->cg_irotor / NBBY;
71216784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
71316784Smckusick 	loc = skpc(0xff, len, &cgp->cg_iused[start]);
71416784Smckusick 	if (loc == 0) {
71517697Smckusick 		len = start + 1;
71617697Smckusick 		start = 0;
71717697Smckusick 		loc = skpc(0xff, len, &cgp->cg_iused[0]);
71817697Smckusick 		if (loc == 0) {
71917697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
72017697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
72117697Smckusick 			panic("ialloccg: map corrupted");
72217697Smckusick 			/* NOTREACHED */
72317697Smckusick 		}
72416784Smckusick 	}
72516784Smckusick 	i = start + len - loc;
72616784Smckusick 	map = cgp->cg_iused[i];
72716784Smckusick 	ipref = i * NBBY;
72816784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
72916784Smckusick 		if ((map & i) == 0) {
7304359Smckusick 			cgp->cg_irotor = ipref;
7314359Smckusick 			goto gotit;
7324359Smckusick 		}
7334359Smckusick 	}
73416784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
73516784Smckusick 	panic("ialloccg: block not in map");
73616784Smckusick 	/* NOTREACHED */
7374359Smckusick gotit:
7384359Smckusick 	setbit(cgp->cg_iused, ipref);
7394792Smckusic 	cgp->cg_cs.cs_nifree--;
7404792Smckusic 	fs->fs_cstotal.cs_nifree--;
7415322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
7424359Smckusick 	fs->fs_fmod++;
7434359Smckusick 	if ((mode & IFMT) == IFDIR) {
7444792Smckusic 		cgp->cg_cs.cs_ndir++;
7454792Smckusic 		fs->fs_cstotal.cs_ndir++;
7465322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
7474359Smckusick 	}
7484359Smckusick 	bdwrite(bp);
7494359Smckusick 	return (cg * fs->fs_ipg + ipref);
7504359Smckusick }
7514359Smckusick 
7525375Smckusic /*
7535375Smckusic  * Free a block or fragment.
7545375Smckusic  *
7555375Smckusic  * The specified block or fragment is placed back in the
7565375Smckusic  * free map. If a fragment is deallocated, a possible
7575375Smckusic  * block reassembly is checked.
7585375Smckusic  */
7599163Ssam free(ip, bno, size)
7605965Smckusic 	register struct inode *ip;
7614359Smckusick 	daddr_t bno;
7625212Smckusic 	off_t size;
7634359Smckusick {
7644359Smckusick 	register struct fs *fs;
7654359Smckusick 	register struct cg *cgp;
7664359Smckusick 	register struct buf *bp;
7674463Smckusic 	int cg, blk, frags, bbase;
7684463Smckusic 	register int i;
7694359Smckusick 
7705965Smckusic 	fs = ip->i_fs;
7716716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
7726716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
7736716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7744426Smckusic 		panic("free: bad size");
7756716Smckusick 	}
7765377Smckusic 	cg = dtog(fs, bno);
7776567Smckusic 	if (badblock(fs, bno)) {
7786567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
7794359Smckusick 		return;
7806567Smckusic 	}
78110278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
7826531Smckusick 	cgp = bp->b_un.b_cg;
7836531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
7845960Smckusic 		brelse(bp);
7854359Smckusick 		return;
7865960Smckusic 	}
7878105Sroot 	cgp->cg_time = time.tv_sec;
7885377Smckusic 	bno = dtogd(fs, bno);
7895322Smckusic 	if (size == fs->fs_bsize) {
79011638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) {
7916716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
7926716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
7934426Smckusic 			panic("free: freeing free block");
7946567Smckusic 		}
79511638Ssam 		setblock(fs, cgp->cg_free, fragstoblks(fs, bno));
7964792Smckusic 		cgp->cg_cs.cs_nbfree++;
7974792Smckusic 		fs->fs_cstotal.cs_nbfree++;
7985322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
7995375Smckusic 		i = cbtocylno(fs, bno);
8005375Smckusic 		cgp->cg_b[i][cbtorpos(fs, bno)]++;
8015375Smckusic 		cgp->cg_btot[i]++;
8024426Smckusic 	} else {
80317224Smckusick 		bbase = bno - fragnum(fs, bno);
8044463Smckusic 		/*
8054463Smckusic 		 * decrement the counts associated with the old frags
8064463Smckusic 		 */
8076294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
8085322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
8094463Smckusic 		/*
8104463Smckusic 		 * deallocate the fragment
8114463Smckusic 		 */
8125960Smckusic 		frags = numfrags(fs, size);
8134463Smckusic 		for (i = 0; i < frags; i++) {
8146716Smckusick 			if (isset(cgp->cg_free, bno + i)) {
8156716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
8166716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
8174426Smckusic 				panic("free: freeing free frag");
8186716Smckusick 			}
8194426Smckusic 			setbit(cgp->cg_free, bno + i);
8204426Smckusic 		}
8216294Smckusick 		cgp->cg_cs.cs_nffree += i;
8226294Smckusick 		fs->fs_cstotal.cs_nffree += i;
8236294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
8244463Smckusic 		/*
8254463Smckusic 		 * add back in counts associated with the new frags
8264463Smckusic 		 */
8276294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
8285322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
8294463Smckusic 		/*
8304463Smckusic 		 * if a complete block has been reassembled, account for it
8314463Smckusic 		 */
83211638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) {
8335322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
8345322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
8355322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
8364792Smckusic 			cgp->cg_cs.cs_nbfree++;
8374792Smckusic 			fs->fs_cstotal.cs_nbfree++;
8385322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
8395375Smckusic 			i = cbtocylno(fs, bbase);
8405375Smckusic 			cgp->cg_b[i][cbtorpos(fs, bbase)]++;
8415375Smckusic 			cgp->cg_btot[i]++;
8424426Smckusic 		}
8434426Smckusic 	}
8444359Smckusick 	fs->fs_fmod++;
8454359Smckusick 	bdwrite(bp);
8464359Smckusick }
8474359Smckusick 
8485375Smckusic /*
8495375Smckusic  * Free an inode.
8505375Smckusic  *
8515375Smckusic  * The specified inode is placed back in the free map.
8525375Smckusic  */
8535965Smckusic ifree(ip, ino, mode)
8545965Smckusic 	struct inode *ip;
8554359Smckusick 	ino_t ino;
8564359Smckusick 	int mode;
8574359Smckusick {
8584359Smckusick 	register struct fs *fs;
8594359Smckusick 	register struct cg *cgp;
8604359Smckusick 	register struct buf *bp;
8614359Smckusick 	int cg;
8624359Smckusick 
8635965Smckusic 	fs = ip->i_fs;
8646716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
8656716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
8666716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
8674359Smckusick 		panic("ifree: range");
8686716Smckusick 	}
8695377Smckusic 	cg = itog(fs, ino);
87010278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
8716531Smckusick 	cgp = bp->b_un.b_cg;
8726531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
8735960Smckusic 		brelse(bp);
8744359Smckusick 		return;
8755960Smckusic 	}
8768105Sroot 	cgp->cg_time = time.tv_sec;
8774359Smckusick 	ino %= fs->fs_ipg;
8786716Smckusick 	if (isclr(cgp->cg_iused, ino)) {
8796716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
8806716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
8814359Smckusick 		panic("ifree: freeing free inode");
8826716Smckusick 	}
8834359Smckusick 	clrbit(cgp->cg_iused, ino);
88416784Smckusick 	if (ino < cgp->cg_irotor)
88516784Smckusick 		cgp->cg_irotor = ino;
8864792Smckusic 	cgp->cg_cs.cs_nifree++;
8874792Smckusic 	fs->fs_cstotal.cs_nifree++;
8885322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
8894359Smckusick 	if ((mode & IFMT) == IFDIR) {
8904792Smckusic 		cgp->cg_cs.cs_ndir--;
8914792Smckusic 		fs->fs_cstotal.cs_ndir--;
8925322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
8934359Smckusick 	}
8944359Smckusick 	fs->fs_fmod++;
8954359Smckusick 	bdwrite(bp);
8964359Smckusick }
8974359Smckusick 
8984463Smckusic /*
8995375Smckusic  * Find a block of the specified size in the specified cylinder group.
9005375Smckusic  *
9014651Smckusic  * It is a panic if a request is made to find a block if none are
9024651Smckusic  * available.
9034651Smckusic  */
9044651Smckusic daddr_t
9054651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
9064651Smckusic 	register struct fs *fs;
9074651Smckusic 	register struct cg *cgp;
9084651Smckusic 	daddr_t bpref;
9094651Smckusic 	int allocsiz;
9104651Smckusic {
9114651Smckusic 	daddr_t bno;
9124651Smckusic 	int start, len, loc, i;
9134651Smckusic 	int blk, field, subfield, pos;
9144651Smckusic 
9154651Smckusic 	/*
9164651Smckusic 	 * find the fragment by searching through the free block
9174651Smckusic 	 * map for an appropriate bit pattern
9184651Smckusic 	 */
9194651Smckusic 	if (bpref)
9205377Smckusic 		start = dtogd(fs, bpref) / NBBY;
9214651Smckusic 	else
9224651Smckusic 		start = cgp->cg_frotor / NBBY;
9235398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
92412755Ssam 	loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start],
92512755Ssam 		(caddr_t)fragtbl[fs->fs_frag],
92612755Ssam 		(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
9274651Smckusic 	if (loc == 0) {
9286531Smckusick 		len = start + 1;
9296531Smckusick 		start = 0;
93017697Smckusick 		loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[0],
93112755Ssam 			(caddr_t)fragtbl[fs->fs_frag],
93212755Ssam 			(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
93316784Smckusick 		if (loc == 0) {
93416784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
93516784Smckusick 			    start, len, fs->fs_fsmnt);
93616784Smckusick 			panic("alloccg: map corrupted");
93717697Smckusick 			/* NOTREACHED */
93816784Smckusick 		}
9394651Smckusic 	}
9404651Smckusic 	bno = (start + len - loc) * NBBY;
9414651Smckusic 	cgp->cg_frotor = bno;
9424651Smckusic 	/*
9434651Smckusic 	 * found the byte in the map
9444651Smckusic 	 * sift through the bits to find the selected frag
9454651Smckusic 	 */
9466294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
9476294Smckusick 		blk = blkmap(fs, cgp->cg_free, bno);
9484651Smckusic 		blk <<= 1;
9494651Smckusic 		field = around[allocsiz];
9504651Smckusic 		subfield = inside[allocsiz];
9515322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
9526294Smckusick 			if ((blk & field) == subfield)
9536294Smckusick 				return (bno + pos);
9544651Smckusic 			field <<= 1;
9554651Smckusic 			subfield <<= 1;
9564651Smckusic 		}
9574651Smckusic 	}
9586716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
9594651Smckusic 	panic("alloccg: block not in map");
9606531Smckusick 	return (-1);
9614651Smckusic }
9624651Smckusic 
9634651Smckusic /*
9645375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
9655375Smckusic  *
9665375Smckusic  * The form of the error message is:
9674359Smckusick  *	fs: error message
9684359Smckusick  */
9694359Smckusick fserr(fs, cp)
9704359Smckusick 	struct fs *fs;
9714359Smckusick 	char *cp;
9724359Smckusick {
9734359Smckusick 
9744359Smckusick 	printf("%s: %s\n", fs->fs_fsmnt, cp);
9754359Smckusick }
976