xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 17697)
1*17697Smckusick /*	lfs_alloc.c	6.10	85/01/10	*/
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;
1489163Ssam 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, nsize,
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);
16512643Ssam 		ip->i_blocks += btodb(nsize - osize);
16612643Ssam 		ip->i_flag |= IUPD|ICHG;
1676294Smckusick 		return (bp);
1684463Smckusic 	}
1694792Smckusic nospace:
1704463Smckusic 	/*
1714463Smckusic 	 * no space available
1724463Smckusic 	 */
1734426Smckusic 	fserr(fs, "file system full");
1744426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
1754426Smckusic 	u.u_error = ENOSPC;
1764426Smckusic 	return (NULL);
1774426Smckusic }
1784426Smckusic 
1795375Smckusic /*
1805375Smckusic  * Allocate an inode in the file system.
1815375Smckusic  *
1825375Smckusic  * A preference may be optionally specified. If a preference is given
1835375Smckusic  * the following hierarchy is used to allocate an inode:
1845375Smckusic  *   1) allocate the requested inode.
1855375Smckusic  *   2) allocate an inode in the same cylinder group.
1865375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
1875375Smckusic  *      available inode is located.
1885375Smckusic  * If no inode preference is given the following heirarchy is used
1895375Smckusic  * to allocate an inode:
1905375Smckusic  *   1) allocate an inode in cylinder group 0.
1915375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
1925375Smckusic  *      available inode is located.
1935375Smckusic  */
1944359Smckusick struct inode *
1955965Smckusic ialloc(pip, ipref, mode)
1965965Smckusic 	register struct inode *pip;
1974359Smckusick 	ino_t ipref;
1984359Smckusick 	int mode;
1994359Smckusick {
2005212Smckusic 	ino_t ino;
2014359Smckusick 	register struct fs *fs;
2024359Smckusick 	register struct inode *ip;
2034359Smckusick 	int cg;
2044359Smckusick 
2055965Smckusic 	fs = pip->i_fs;
2064792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2074359Smckusick 		goto noinodes;
2087650Ssam #ifdef QUOTA
20912643Ssam 	u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
21012643Ssam 	if (u.u_error)
21112643Ssam 		return (NULL);
2127483Skre #endif
2134948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2144948Smckusic 		ipref = 0;
2155377Smckusic 	cg = itog(fs, ipref);
2165965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2174359Smckusick 	if (ino == 0)
2184359Smckusick 		goto noinodes;
2195965Smckusic 	ip = iget(pip->i_dev, pip->i_fs, ino);
2204359Smckusick 	if (ip == NULL) {
22115120Skarels 		ifree(pip, ino, 0);
2224359Smckusick 		return (NULL);
2234359Smckusick 	}
2246716Smckusick 	if (ip->i_mode) {
2256716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
2266716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2274359Smckusick 		panic("ialloc: dup alloc");
2286716Smckusick 	}
22912643Ssam 	if (ip->i_blocks) {				/* XXX */
23012643Ssam 		printf("free inode %s/%d had %d blocks\n",
23112643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
23212643Ssam 		ip->i_blocks = 0;
23312643Ssam 	}
2344359Smckusick 	return (ip);
2354359Smckusick noinodes:
2364359Smckusick 	fserr(fs, "out of inodes");
2376294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
2384359Smckusick 	u.u_error = ENOSPC;
2394359Smckusick 	return (NULL);
2404359Smckusick }
2414359Smckusick 
2424651Smckusic /*
2435375Smckusic  * Find a cylinder to place a directory.
2445375Smckusic  *
2455375Smckusic  * The policy implemented by this algorithm is to select from
2465375Smckusic  * among those cylinder groups with above the average number of
2475375Smckusic  * free inodes, the one with the smallest number of directories.
2484651Smckusic  */
2499163Ssam ino_t
2505965Smckusic dirpref(fs)
2515965Smckusic 	register struct fs *fs;
2524359Smckusick {
2534651Smckusic 	int cg, minndir, mincg, avgifree;
2544359Smckusick 
2554792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
2564651Smckusic 	minndir = fs->fs_ipg;
2574359Smckusick 	mincg = 0;
2584651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
2595322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
2605322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
2614359Smckusick 			mincg = cg;
2625322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
2634359Smckusick 		}
2649163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
2654359Smckusick }
2664359Smckusick 
2674651Smckusic /*
2689163Ssam  * Select the desired position for the next block in a file.  The file is
2699163Ssam  * logically divided into sections. The first section is composed of the
2709163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
2719163Ssam  *
2729163Ssam  * If no blocks have been allocated in the first section, the policy is to
2739163Ssam  * request a block in the same cylinder group as the inode that describes
2749163Ssam  * the file. If no blocks have been allocated in any other section, the
2759163Ssam  * policy is to place the section in a cylinder group with a greater than
2769163Ssam  * average number of free blocks.  An appropriate cylinder group is found
27717696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
27817696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
27917696Smckusick  * cylinder group from which the previous allocation was made. The sweep
28017696Smckusick  * continues until a cylinder group with greater than the average number
28117696Smckusick  * of free blocks is found. If the allocation is for the first block in an
28217696Smckusick  * indirect block, the information on the previous allocation is unavailable;
28317696Smckusick  * here a best guess is made based upon the logical block number being
28417696Smckusick  * allocated.
2859163Ssam  *
2869163Ssam  * If a section is already partially allocated, the policy is to
2879163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
2889163Ssam  * contiguous blocks and the beginning of the next is physically separated
2899163Ssam  * so that the disk head will be in transit between them for at least
2909163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
2919163Ssam  * schedule another I/O transfer.
2924651Smckusic  */
2935212Smckusic daddr_t
2949163Ssam blkpref(ip, lbn, indx, bap)
2959163Ssam 	struct inode *ip;
2969163Ssam 	daddr_t lbn;
2979163Ssam 	int indx;
2989163Ssam 	daddr_t *bap;
2999163Ssam {
3005965Smckusic 	register struct fs *fs;
30117696Smckusick 	register int cg;
30217696Smckusick 	int avgbfree, startcg;
3039163Ssam 	daddr_t nextblk;
3044651Smckusic 
3059163Ssam 	fs = ip->i_fs;
3069163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3079163Ssam 		if (lbn < NDADDR) {
3089163Ssam 			cg = itog(fs, ip->i_number);
3095322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3104651Smckusic 		}
3119163Ssam 		/*
3129163Ssam 		 * Find a cylinder with greater than average number of
3139163Ssam 		 * unused data blocks.
3149163Ssam 		 */
31517696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
31617696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
31717696Smckusick 		else
31817696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
31917696Smckusick 		startcg %= fs->fs_ncg;
3209163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
32117696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
3229163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3239163Ssam 				fs->fs_cgrotor = cg;
3249163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3259163Ssam 			}
32617696Smckusick 		for (cg = 0; cg <= startcg; cg++)
3279163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3289163Ssam 				fs->fs_cgrotor = cg;
3299163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3309163Ssam 			}
3319163Ssam 		return (NULL);
3329163Ssam 	}
3339163Ssam 	/*
3349163Ssam 	 * One or more previous blocks have been laid out. If less
3359163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
3369163Ssam 	 * next block is requested contiguously, otherwise it is
3379163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
3389163Ssam 	 */
3399163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
3409163Ssam 	if (indx > fs->fs_maxcontig &&
34111638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
3429163Ssam 	    != nextblk)
3439163Ssam 		return (nextblk);
3449163Ssam 	if (fs->fs_rotdelay != 0)
3459163Ssam 		/*
3469163Ssam 		 * Here we convert ms of delay to frags as:
3479163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
3489163Ssam 		 *	((sect/frag) * (ms/sec))
3499163Ssam 		 * then round up to the next block.
3509163Ssam 		 */
3519163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
3529163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
3539163Ssam 	return (nextblk);
3544651Smckusic }
3554651Smckusic 
3565375Smckusic /*
3575375Smckusic  * Implement the cylinder overflow algorithm.
3585375Smckusic  *
3595375Smckusic  * The policy implemented by this algorithm is:
3605375Smckusic  *   1) allocate the block in its requested cylinder group.
3615375Smckusic  *   2) quadradically rehash on the cylinder group number.
3625375Smckusic  *   3) brute force search for a free block.
3635375Smckusic  */
3645212Smckusic /*VARARGS5*/
3655212Smckusic u_long
3665965Smckusic hashalloc(ip, cg, pref, size, allocator)
3675965Smckusic 	struct inode *ip;
3684359Smckusick 	int cg;
3694359Smckusick 	long pref;
3704359Smckusick 	int size;	/* size for data blocks, mode for inodes */
3715212Smckusic 	u_long (*allocator)();
3724359Smckusick {
3735965Smckusic 	register struct fs *fs;
3744359Smckusick 	long result;
3754359Smckusick 	int i, icg = cg;
3764359Smckusick 
3775965Smckusic 	fs = ip->i_fs;
3784359Smckusick 	/*
3794359Smckusick 	 * 1: preferred cylinder group
3804359Smckusick 	 */
3815965Smckusic 	result = (*allocator)(ip, cg, pref, size);
3824359Smckusick 	if (result)
3834359Smckusick 		return (result);
3844359Smckusick 	/*
3854359Smckusick 	 * 2: quadratic rehash
3864359Smckusick 	 */
3874359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
3884359Smckusick 		cg += i;
3894359Smckusick 		if (cg >= fs->fs_ncg)
3904359Smckusick 			cg -= fs->fs_ncg;
3915965Smckusic 		result = (*allocator)(ip, cg, 0, size);
3924359Smckusick 		if (result)
3934359Smckusick 			return (result);
3944359Smckusick 	}
3954359Smckusick 	/*
3964359Smckusick 	 * 3: brute force search
39710847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
39810847Ssam 	 * and 1 is always checked in the quadratic rehash.
3994359Smckusick 	 */
40010848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
40110847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4025965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4034359Smckusick 		if (result)
4044359Smckusick 			return (result);
4054359Smckusick 		cg++;
4064359Smckusick 		if (cg == fs->fs_ncg)
4074359Smckusick 			cg = 0;
4084359Smckusick 	}
4096294Smckusick 	return (NULL);
4104359Smckusick }
4114359Smckusick 
4125375Smckusic /*
4135375Smckusic  * Determine whether a fragment can be extended.
4145375Smckusic  *
4155375Smckusic  * Check to see if the necessary fragments are available, and
4165375Smckusic  * if they are, allocate them.
4175375Smckusic  */
4184359Smckusick daddr_t
4195965Smckusic fragextend(ip, cg, bprev, osize, nsize)
4205965Smckusic 	struct inode *ip;
4214426Smckusic 	int cg;
4224463Smckusic 	long bprev;
4234426Smckusic 	int osize, nsize;
4244426Smckusic {
4255965Smckusic 	register struct fs *fs;
4264463Smckusic 	register struct buf *bp;
4274463Smckusic 	register struct cg *cgp;
4284463Smckusic 	long bno;
4294463Smckusic 	int frags, bbase;
4304426Smckusic 	int i;
4314426Smckusic 
4325965Smckusic 	fs = ip->i_fs;
43317224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
4346531Smckusick 		return (NULL);
4355960Smckusic 	frags = numfrags(fs, nsize);
43617224Smckusick 	bbase = fragnum(fs, bprev);
43717224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
4384463Smckusic 		/* cannot extend across a block boundry */
4396294Smckusick 		return (NULL);
4404463Smckusic 	}
44110278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
4426531Smckusick 	cgp = bp->b_un.b_cg;
4436531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
4445960Smckusic 		brelse(bp);
4456294Smckusick 		return (NULL);
4465960Smckusic 	}
4478105Sroot 	cgp->cg_time = time.tv_sec;
4485377Smckusic 	bno = dtogd(fs, bprev);
4495960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
4505361Smckusic 		if (isclr(cgp->cg_free, bno + i)) {
4515361Smckusic 			brelse(bp);
4526294Smckusick 			return (NULL);
4535361Smckusic 		}
4545361Smckusic 	/*
4555361Smckusic 	 * the current fragment can be extended
4565361Smckusic 	 * deduct the count on fragment being extended into
4575361Smckusic 	 * increase the count on the remaining fragment (if any)
4585361Smckusic 	 * allocate the extended piece
4595361Smckusic 	 */
4605361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
4614463Smckusic 		if (isclr(cgp->cg_free, bno + i))
4624463Smckusic 			break;
4635960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
4645361Smckusic 	if (i != frags)
4655361Smckusic 		cgp->cg_frsum[i - frags]++;
4665960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
4675361Smckusic 		clrbit(cgp->cg_free, bno + i);
4685361Smckusic 		cgp->cg_cs.cs_nffree--;
4695361Smckusic 		fs->fs_cstotal.cs_nffree--;
4705361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
4714463Smckusic 	}
4725361Smckusic 	fs->fs_fmod++;
4735361Smckusic 	bdwrite(bp);
4745361Smckusic 	return (bprev);
4754426Smckusic }
4764426Smckusic 
4775375Smckusic /*
4785375Smckusic  * Determine whether a block can be allocated.
4795375Smckusic  *
4805375Smckusic  * Check to see if a block of the apprpriate size is available,
4815375Smckusic  * and if it is, allocate it.
4825375Smckusic  */
4839163Ssam daddr_t
4845965Smckusic alloccg(ip, cg, bpref, size)
4855965Smckusic 	struct inode *ip;
4864359Smckusick 	int cg;
4874359Smckusick 	daddr_t bpref;
4884359Smckusick 	int size;
4894359Smckusick {
4905965Smckusic 	register struct fs *fs;
4914463Smckusic 	register struct buf *bp;
4924463Smckusic 	register struct cg *cgp;
4934463Smckusic 	int bno, frags;
4944463Smckusic 	int allocsiz;
4954463Smckusic 	register int i;
4964359Smckusick 
4975965Smckusic 	fs = ip->i_fs;
4985322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
4996294Smckusick 		return (NULL);
50010278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
5016531Smckusick 	cgp = bp->b_un.b_cg;
50215950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
50315950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5045960Smckusic 		brelse(bp);
5056294Smckusick 		return (NULL);
5065960Smckusic 	}
5078105Sroot 	cgp->cg_time = time.tv_sec;
5085322Smckusic 	if (size == fs->fs_bsize) {
5095212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5104463Smckusic 		bdwrite(bp);
5114463Smckusic 		return (bno);
5124463Smckusic 	}
5134463Smckusic 	/*
5144463Smckusic 	 * check to see if any fragments are already available
5154463Smckusic 	 * allocsiz is the size which will be allocated, hacking
5164463Smckusic 	 * it down to a smaller size if necessary
5174463Smckusic 	 */
5185960Smckusic 	frags = numfrags(fs, size);
5195322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
5204463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
5214463Smckusic 			break;
5225322Smckusic 	if (allocsiz == fs->fs_frag) {
5234463Smckusic 		/*
5244463Smckusic 		 * no fragments were available, so a block will be
5254463Smckusic 		 * allocated, and hacked up
5264463Smckusic 		 */
5274792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
5284463Smckusic 			brelse(bp);
5296294Smckusick 			return (NULL);
5304463Smckusic 		}
5315212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5325377Smckusic 		bpref = dtogd(fs, bno);
5335322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
5344463Smckusic 			setbit(cgp->cg_free, bpref + i);
5355322Smckusic 		i = fs->fs_frag - frags;
5364792Smckusic 		cgp->cg_cs.cs_nffree += i;
5374792Smckusic 		fs->fs_cstotal.cs_nffree += i;
5385322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
5399762Ssam 		fs->fs_fmod++;
5404463Smckusic 		cgp->cg_frsum[i]++;
5414463Smckusic 		bdwrite(bp);
5424463Smckusic 		return (bno);
5434463Smckusic 	}
5444651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
54515950Smckusick 	if (bno < 0) {
54615950Smckusick 		brelse(bp);
5476294Smckusick 		return (NULL);
54815950Smckusick 	}
5494463Smckusic 	for (i = 0; i < frags; i++)
5504463Smckusic 		clrbit(cgp->cg_free, bno + i);
5514792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
5524792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
5535322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
5549762Ssam 	fs->fs_fmod++;
5554463Smckusic 	cgp->cg_frsum[allocsiz]--;
5564463Smckusic 	if (frags != allocsiz)
5574463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
5584463Smckusic 	bdwrite(bp);
5594463Smckusic 	return (cg * fs->fs_fpg + bno);
5604463Smckusic }
5614463Smckusic 
5625375Smckusic /*
5635375Smckusic  * Allocate a block in a cylinder group.
5645375Smckusic  *
5655375Smckusic  * This algorithm implements the following policy:
5665375Smckusic  *   1) allocate the requested block.
5675375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
5685375Smckusic  *   3) allocate the next available block on the block rotor for the
5695375Smckusic  *      specified cylinder group.
5705375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
5715375Smckusic  * blocks may be fragmented by the routine that allocates them.
5725375Smckusic  */
5734463Smckusic daddr_t
5745212Smckusic alloccgblk(fs, cgp, bpref)
5755965Smckusic 	register struct fs *fs;
5764463Smckusic 	register struct cg *cgp;
5774463Smckusic 	daddr_t bpref;
5784463Smckusic {
5794651Smckusic 	daddr_t bno;
5806294Smckusick 	int cylno, pos, delta;
5814651Smckusic 	short *cylbp;
5825361Smckusic 	register int i;
5834463Smckusic 
5844651Smckusic 	if (bpref == 0) {
5854651Smckusic 		bpref = cgp->cg_rotor;
5865361Smckusic 		goto norot;
5875361Smckusic 	}
58817224Smckusick 	bpref = blknum(fs, bpref);
5895377Smckusic 	bpref = dtogd(fs, bpref);
5905361Smckusic 	/*
5915361Smckusic 	 * if the requested block is available, use it
5925361Smckusic 	 */
59311638Ssam 	if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) {
5945361Smckusic 		bno = bpref;
5955361Smckusic 		goto gotit;
5965361Smckusic 	}
5975361Smckusic 	/*
5985361Smckusic 	 * check for a block available on the same cylinder
5995361Smckusic 	 */
6005361Smckusic 	cylno = cbtocylno(fs, bpref);
6015375Smckusic 	if (cgp->cg_btot[cylno] == 0)
6025375Smckusic 		goto norot;
6035375Smckusic 	if (fs->fs_cpc == 0) {
6045375Smckusic 		/*
6055375Smckusic 		 * block layout info is not available, so just have
6065375Smckusic 		 * to take any block in this cylinder.
6075375Smckusic 		 */
6085375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
6095375Smckusic 		goto norot;
6105375Smckusic 	}
6115375Smckusic 	/*
6125361Smckusic 	 * check the summary information to see if a block is
6135361Smckusic 	 * available in the requested cylinder starting at the
6149163Ssam 	 * requested rotational position and proceeding around.
6155361Smckusic 	 */
6169163Ssam 	cylbp = cgp->cg_b[cylno];
6179163Ssam 	pos = cbtorpos(fs, bpref);
6185361Smckusic 	for (i = pos; i < NRPOS; i++)
6195361Smckusic 		if (cylbp[i] > 0)
6205361Smckusic 			break;
6215361Smckusic 	if (i == NRPOS)
6225361Smckusic 		for (i = 0; i < pos; i++)
6235361Smckusic 			if (cylbp[i] > 0)
6245361Smckusic 				break;
6255361Smckusic 	if (cylbp[i] > 0) {
6264651Smckusic 		/*
6275361Smckusic 		 * found a rotational position, now find the actual
6285361Smckusic 		 * block. A panic if none is actually there.
6294651Smckusic 		 */
6305361Smckusic 		pos = cylno % fs->fs_cpc;
6315361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
6326716Smckusick 		if (fs->fs_postbl[pos][i] == -1) {
6336716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
6346716Smckusick 			    pos, i, fs->fs_fsmnt);
6355361Smckusic 			panic("alloccgblk: cyl groups corrupted");
6366716Smckusick 		}
6376294Smckusick 		for (i = fs->fs_postbl[pos][i];; ) {
6385361Smckusic 			if (isblock(fs, cgp->cg_free, bno + i)) {
63911638Ssam 				bno = blkstofrags(fs, (bno + i));
6405361Smckusic 				goto gotit;
6415361Smckusic 			}
6426294Smckusick 			delta = fs->fs_rotbl[i];
6436294Smckusick 			if (delta <= 0 || delta > MAXBPC - i)
6444651Smckusic 				break;
6456294Smckusick 			i += delta;
6464651Smckusic 		}
6476716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
6485361Smckusic 		panic("alloccgblk: can't find blk in cyl");
6494359Smckusick 	}
6505361Smckusic norot:
6515361Smckusic 	/*
6525361Smckusic 	 * no blocks in the requested cylinder, so take next
6535361Smckusic 	 * available one in this cylinder group.
6545361Smckusic 	 */
6558628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
6566567Smckusic 	if (bno < 0)
6576294Smckusick 		return (NULL);
6584651Smckusic 	cgp->cg_rotor = bno;
6594359Smckusick gotit:
66011638Ssam 	clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno));
6614792Smckusic 	cgp->cg_cs.cs_nbfree--;
6624792Smckusic 	fs->fs_cstotal.cs_nbfree--;
6635322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
6645375Smckusic 	cylno = cbtocylno(fs, bno);
6655375Smckusic 	cgp->cg_b[cylno][cbtorpos(fs, bno)]--;
6665375Smckusic 	cgp->cg_btot[cylno]--;
6674359Smckusick 	fs->fs_fmod++;
6684651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
6694359Smckusick }
6704359Smckusick 
6715375Smckusic /*
6725375Smckusic  * Determine whether an inode can be allocated.
6735375Smckusic  *
6745375Smckusic  * Check to see if an inode is available, and if it is,
6755375Smckusic  * allocate it using the following policy:
6765375Smckusic  *   1) allocate the requested inode.
6775375Smckusic  *   2) allocate the next available inode after the requested
6785375Smckusic  *      inode in the specified cylinder group.
6795375Smckusic  */
6809163Ssam ino_t
6815965Smckusic ialloccg(ip, cg, ipref, mode)
6825965Smckusic 	struct inode *ip;
6834359Smckusick 	int cg;
6844359Smckusick 	daddr_t ipref;
6854359Smckusick 	int mode;
6864359Smckusick {
6875965Smckusic 	register struct fs *fs;
6884463Smckusic 	register struct cg *cgp;
68916784Smckusick 	struct buf *bp;
69016784Smckusick 	int start, len, loc, map, i;
6914359Smckusick 
6925965Smckusic 	fs = ip->i_fs;
6935322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
6946294Smckusick 		return (NULL);
69510278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
6966531Smckusick 	cgp = bp->b_un.b_cg;
69715950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
69815950Smckusick 	    cgp->cg_cs.cs_nifree == 0) {
6995960Smckusic 		brelse(bp);
7006294Smckusick 		return (NULL);
7015960Smckusic 	}
7028105Sroot 	cgp->cg_time = time.tv_sec;
7034359Smckusick 	if (ipref) {
7044359Smckusick 		ipref %= fs->fs_ipg;
7054359Smckusick 		if (isclr(cgp->cg_iused, ipref))
7064359Smckusick 			goto gotit;
70716784Smckusick 	}
70816784Smckusick 	start = cgp->cg_irotor / NBBY;
70916784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
71016784Smckusick 	loc = skpc(0xff, len, &cgp->cg_iused[start]);
71116784Smckusick 	if (loc == 0) {
712*17697Smckusick 		len = start + 1;
713*17697Smckusick 		start = 0;
714*17697Smckusick 		loc = skpc(0xff, len, &cgp->cg_iused[0]);
715*17697Smckusick 		if (loc == 0) {
716*17697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
717*17697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
718*17697Smckusick 			panic("ialloccg: map corrupted");
719*17697Smckusick 			/* NOTREACHED */
720*17697Smckusick 		}
72116784Smckusick 	}
72216784Smckusick 	i = start + len - loc;
72316784Smckusick 	map = cgp->cg_iused[i];
72416784Smckusick 	ipref = i * NBBY;
72516784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
72616784Smckusick 		if ((map & i) == 0) {
7274359Smckusick 			cgp->cg_irotor = ipref;
7284359Smckusick 			goto gotit;
7294359Smckusick 		}
7304359Smckusick 	}
73116784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
73216784Smckusick 	panic("ialloccg: block not in map");
73316784Smckusick 	/* NOTREACHED */
7344359Smckusick gotit:
7354359Smckusick 	setbit(cgp->cg_iused, ipref);
7364792Smckusic 	cgp->cg_cs.cs_nifree--;
7374792Smckusic 	fs->fs_cstotal.cs_nifree--;
7385322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
7394359Smckusick 	fs->fs_fmod++;
7404359Smckusick 	if ((mode & IFMT) == IFDIR) {
7414792Smckusic 		cgp->cg_cs.cs_ndir++;
7424792Smckusic 		fs->fs_cstotal.cs_ndir++;
7435322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
7444359Smckusick 	}
7454359Smckusick 	bdwrite(bp);
7464359Smckusick 	return (cg * fs->fs_ipg + ipref);
7474359Smckusick }
7484359Smckusick 
7495375Smckusic /*
7505375Smckusic  * Free a block or fragment.
7515375Smckusic  *
7525375Smckusic  * The specified block or fragment is placed back in the
7535375Smckusic  * free map. If a fragment is deallocated, a possible
7545375Smckusic  * block reassembly is checked.
7555375Smckusic  */
7569163Ssam free(ip, bno, size)
7575965Smckusic 	register struct inode *ip;
7584359Smckusick 	daddr_t bno;
7595212Smckusic 	off_t size;
7604359Smckusick {
7614359Smckusick 	register struct fs *fs;
7624359Smckusick 	register struct cg *cgp;
7634359Smckusick 	register struct buf *bp;
7644463Smckusic 	int cg, blk, frags, bbase;
7654463Smckusic 	register int i;
7664359Smckusick 
7675965Smckusic 	fs = ip->i_fs;
7686716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
7696716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
7706716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7714426Smckusic 		panic("free: bad size");
7726716Smckusick 	}
7735377Smckusic 	cg = dtog(fs, bno);
7746567Smckusic 	if (badblock(fs, bno)) {
7756567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
7764359Smckusick 		return;
7776567Smckusic 	}
77810278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
7796531Smckusick 	cgp = bp->b_un.b_cg;
7806531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
7815960Smckusic 		brelse(bp);
7824359Smckusick 		return;
7835960Smckusic 	}
7848105Sroot 	cgp->cg_time = time.tv_sec;
7855377Smckusic 	bno = dtogd(fs, bno);
7865322Smckusic 	if (size == fs->fs_bsize) {
78711638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) {
7886716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
7896716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
7904426Smckusic 			panic("free: freeing free block");
7916567Smckusic 		}
79211638Ssam 		setblock(fs, cgp->cg_free, fragstoblks(fs, bno));
7934792Smckusic 		cgp->cg_cs.cs_nbfree++;
7944792Smckusic 		fs->fs_cstotal.cs_nbfree++;
7955322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
7965375Smckusic 		i = cbtocylno(fs, bno);
7975375Smckusic 		cgp->cg_b[i][cbtorpos(fs, bno)]++;
7985375Smckusic 		cgp->cg_btot[i]++;
7994426Smckusic 	} else {
80017224Smckusick 		bbase = bno - fragnum(fs, bno);
8014463Smckusic 		/*
8024463Smckusic 		 * decrement the counts associated with the old frags
8034463Smckusic 		 */
8046294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
8055322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
8064463Smckusic 		/*
8074463Smckusic 		 * deallocate the fragment
8084463Smckusic 		 */
8095960Smckusic 		frags = numfrags(fs, size);
8104463Smckusic 		for (i = 0; i < frags; i++) {
8116716Smckusick 			if (isset(cgp->cg_free, bno + i)) {
8126716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
8136716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
8144426Smckusic 				panic("free: freeing free frag");
8156716Smckusick 			}
8164426Smckusic 			setbit(cgp->cg_free, bno + i);
8174426Smckusic 		}
8186294Smckusick 		cgp->cg_cs.cs_nffree += i;
8196294Smckusick 		fs->fs_cstotal.cs_nffree += i;
8206294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
8214463Smckusic 		/*
8224463Smckusic 		 * add back in counts associated with the new frags
8234463Smckusic 		 */
8246294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
8255322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
8264463Smckusic 		/*
8274463Smckusic 		 * if a complete block has been reassembled, account for it
8284463Smckusic 		 */
82911638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) {
8305322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
8315322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
8325322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
8334792Smckusic 			cgp->cg_cs.cs_nbfree++;
8344792Smckusic 			fs->fs_cstotal.cs_nbfree++;
8355322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
8365375Smckusic 			i = cbtocylno(fs, bbase);
8375375Smckusic 			cgp->cg_b[i][cbtorpos(fs, bbase)]++;
8385375Smckusic 			cgp->cg_btot[i]++;
8394426Smckusic 		}
8404426Smckusic 	}
8414359Smckusick 	fs->fs_fmod++;
8424359Smckusick 	bdwrite(bp);
8434359Smckusick }
8444359Smckusick 
8455375Smckusic /*
8465375Smckusic  * Free an inode.
8475375Smckusic  *
8485375Smckusic  * The specified inode is placed back in the free map.
8495375Smckusic  */
8505965Smckusic ifree(ip, ino, mode)
8515965Smckusic 	struct inode *ip;
8524359Smckusick 	ino_t ino;
8534359Smckusick 	int mode;
8544359Smckusick {
8554359Smckusick 	register struct fs *fs;
8564359Smckusick 	register struct cg *cgp;
8574359Smckusick 	register struct buf *bp;
8584359Smckusick 	int cg;
8594359Smckusick 
8605965Smckusic 	fs = ip->i_fs;
8616716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
8626716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
8636716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
8644359Smckusick 		panic("ifree: range");
8656716Smckusick 	}
8665377Smckusic 	cg = itog(fs, ino);
86710278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
8686531Smckusick 	cgp = bp->b_un.b_cg;
8696531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
8705960Smckusic 		brelse(bp);
8714359Smckusick 		return;
8725960Smckusic 	}
8738105Sroot 	cgp->cg_time = time.tv_sec;
8744359Smckusick 	ino %= fs->fs_ipg;
8756716Smckusick 	if (isclr(cgp->cg_iused, ino)) {
8766716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
8776716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
8784359Smckusick 		panic("ifree: freeing free inode");
8796716Smckusick 	}
8804359Smckusick 	clrbit(cgp->cg_iused, ino);
88116784Smckusick 	if (ino < cgp->cg_irotor)
88216784Smckusick 		cgp->cg_irotor = ino;
8834792Smckusic 	cgp->cg_cs.cs_nifree++;
8844792Smckusic 	fs->fs_cstotal.cs_nifree++;
8855322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
8864359Smckusick 	if ((mode & IFMT) == IFDIR) {
8874792Smckusic 		cgp->cg_cs.cs_ndir--;
8884792Smckusic 		fs->fs_cstotal.cs_ndir--;
8895322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
8904359Smckusick 	}
8914359Smckusick 	fs->fs_fmod++;
8924359Smckusick 	bdwrite(bp);
8934359Smckusick }
8944359Smckusick 
8954463Smckusic /*
8965375Smckusic  * Find a block of the specified size in the specified cylinder group.
8975375Smckusic  *
8984651Smckusic  * It is a panic if a request is made to find a block if none are
8994651Smckusic  * available.
9004651Smckusic  */
9014651Smckusic daddr_t
9024651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
9034651Smckusic 	register struct fs *fs;
9044651Smckusic 	register struct cg *cgp;
9054651Smckusic 	daddr_t bpref;
9064651Smckusic 	int allocsiz;
9074651Smckusic {
9084651Smckusic 	daddr_t bno;
9094651Smckusic 	int start, len, loc, i;
9104651Smckusic 	int blk, field, subfield, pos;
9114651Smckusic 
9124651Smckusic 	/*
9134651Smckusic 	 * find the fragment by searching through the free block
9144651Smckusic 	 * map for an appropriate bit pattern
9154651Smckusic 	 */
9164651Smckusic 	if (bpref)
9175377Smckusic 		start = dtogd(fs, bpref) / NBBY;
9184651Smckusic 	else
9194651Smckusic 		start = cgp->cg_frotor / NBBY;
9205398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
92112755Ssam 	loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start],
92212755Ssam 		(caddr_t)fragtbl[fs->fs_frag],
92312755Ssam 		(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
9244651Smckusic 	if (loc == 0) {
9256531Smckusick 		len = start + 1;
9266531Smckusick 		start = 0;
927*17697Smckusick 		loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[0],
92812755Ssam 			(caddr_t)fragtbl[fs->fs_frag],
92912755Ssam 			(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
93016784Smckusick 		if (loc == 0) {
93116784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
93216784Smckusick 			    start, len, fs->fs_fsmnt);
93316784Smckusick 			panic("alloccg: map corrupted");
934*17697Smckusick 			/* NOTREACHED */
93516784Smckusick 		}
9364651Smckusic 	}
9374651Smckusic 	bno = (start + len - loc) * NBBY;
9384651Smckusic 	cgp->cg_frotor = bno;
9394651Smckusic 	/*
9404651Smckusic 	 * found the byte in the map
9414651Smckusic 	 * sift through the bits to find the selected frag
9424651Smckusic 	 */
9436294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
9446294Smckusick 		blk = blkmap(fs, cgp->cg_free, bno);
9454651Smckusic 		blk <<= 1;
9464651Smckusic 		field = around[allocsiz];
9474651Smckusic 		subfield = inside[allocsiz];
9485322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
9496294Smckusick 			if ((blk & field) == subfield)
9506294Smckusick 				return (bno + pos);
9514651Smckusic 			field <<= 1;
9524651Smckusic 			subfield <<= 1;
9534651Smckusic 		}
9544651Smckusic 	}
9556716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
9564651Smckusic 	panic("alloccg: block not in map");
9576531Smckusick 	return (-1);
9584651Smckusic }
9594651Smckusic 
9604651Smckusic /*
9615375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
9625375Smckusic  *
9635375Smckusic  * The form of the error message is:
9644359Smckusick  *	fs: error message
9654359Smckusick  */
9664359Smckusick fserr(fs, cp)
9674359Smckusick 	struct fs *fs;
9684359Smckusick 	char *cp;
9694359Smckusick {
9704359Smckusick 
9714359Smckusick 	printf("%s: %s\n", fs->fs_fsmnt, cp);
9724359Smckusick }
973