xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 23394)
1*23394Smckusick /*
2*23394Smckusick  * Copyright (c) 1982 Regents of the University of California.
3*23394Smckusick  * All rights reserved.  The Berkeley software License Agreement
4*23394Smckusick  * specifies the terms and conditions for redistribution.
5*23394Smckusick  *
6*23394Smckusick  *	@(#)lfs_alloc.c	6.14 (Berkeley) 06/08/85
7*23394Smckusick  */
84359Smckusick 
917097Sbloom #include "param.h"
1017097Sbloom #include "systm.h"
1117097Sbloom #include "mount.h"
1217097Sbloom #include "fs.h"
1317097Sbloom #include "buf.h"
1417097Sbloom #include "inode.h"
1517097Sbloom #include "dir.h"
1617097Sbloom #include "user.h"
1717097Sbloom #include "quota.h"
1817097Sbloom #include "kernel.h"
1918307Sralph #include "syslog.h"
204359Smckusick 
215212Smckusic extern u_long		hashalloc();
229163Ssam extern ino_t		ialloccg();
239163Ssam extern daddr_t		alloccg();
244651Smckusic extern daddr_t		alloccgblk();
254651Smckusic extern daddr_t		fragextend();
264651Smckusic extern daddr_t		blkpref();
274651Smckusic extern daddr_t		mapsearch();
284607Smckusic extern int		inside[], around[];
295322Smckusic extern unsigned char	*fragtbl[];
304359Smckusick 
315375Smckusic /*
325375Smckusic  * Allocate a block in the file system.
335375Smckusic  *
345375Smckusic  * The size of the requested block is given, which must be some
355375Smckusic  * multiple of fs_fsize and <= fs_bsize.
365375Smckusic  * A preference may be optionally specified. If a preference is given
375375Smckusic  * the following hierarchy is used to allocate a block:
385375Smckusic  *   1) allocate the requested block.
395375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
405375Smckusic  *   3) allocate a block in the same cylinder group.
415375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
425375Smckusic  *      available block is located.
435375Smckusic  * If no block preference is given the following heirarchy is used
445375Smckusic  * to allocate a block:
455375Smckusic  *   1) allocate a block in the cylinder group that contains the
465375Smckusic  *      inode for the file.
475375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
485375Smckusic  *      available block is located.
495375Smckusic  */
504359Smckusick struct buf *
515965Smckusic alloc(ip, bpref, size)
524463Smckusic 	register struct inode *ip;
534359Smckusick 	daddr_t bpref;
544359Smckusick 	int size;
554359Smckusick {
564359Smckusick 	daddr_t bno;
574359Smckusick 	register struct fs *fs;
584463Smckusic 	register struct buf *bp;
594359Smckusick 	int cg;
604359Smckusick 
615965Smckusic 	fs = ip->i_fs;
626716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
636716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
646716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
654463Smckusic 		panic("alloc: bad size");
666716Smckusick 	}
675322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
684359Smckusick 		goto nospace;
6911638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
704792Smckusic 		goto nospace;
717650Ssam #ifdef QUOTA
7212643Ssam 	u.u_error = chkdq(ip, (long)btodb(size), 0);
7312643Ssam 	if (u.u_error)
7412643Ssam 		return (NULL);
757483Skre #endif
764948Smckusic 	if (bpref >= fs->fs_size)
774948Smckusic 		bpref = 0;
784359Smckusick 	if (bpref == 0)
795377Smckusic 		cg = itog(fs, ip->i_number);
804359Smckusick 	else
815377Smckusic 		cg = dtog(fs, bpref);
829163Ssam 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
839163Ssam 		(u_long (*)())alloccg);
846567Smckusic 	if (bno <= 0)
854359Smckusick 		goto nospace;
8612643Ssam 	ip->i_blocks += btodb(size);
8712643Ssam 	ip->i_flag |= IUPD|ICHG;
885965Smckusic 	bp = getblk(ip->i_dev, fsbtodb(fs, bno), size);
894359Smckusick 	clrbuf(bp);
904359Smckusick 	return (bp);
914359Smckusick nospace:
924359Smckusick 	fserr(fs, "file system full");
934359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
944359Smckusick 	u.u_error = ENOSPC;
954359Smckusick 	return (NULL);
964359Smckusick }
974359Smckusick 
985375Smckusic /*
995375Smckusic  * Reallocate a fragment to a bigger size
1005375Smckusic  *
1015375Smckusic  * The number and size of the old block is given, and a preference
1025375Smckusic  * and new size is also specified. The allocator attempts to extend
1035375Smckusic  * the original block. Failing that, the regular block allocator is
1045375Smckusic  * invoked to get an appropriate block.
1055375Smckusic  */
1064426Smckusic struct buf *
1075965Smckusic realloccg(ip, bprev, bpref, osize, nsize)
1085965Smckusic 	register struct inode *ip;
1094651Smckusic 	daddr_t bprev, bpref;
1104426Smckusic 	int osize, nsize;
1114426Smckusic {
1124426Smckusic 	daddr_t bno;
1134426Smckusic 	register struct fs *fs;
1144463Smckusic 	register struct buf *bp, *obp;
1154426Smckusic 	int cg;
1164426Smckusic 
1175965Smckusic 	fs = ip->i_fs;
1185960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1196716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1206716Smckusick 		printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1216716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
1224463Smckusic 		panic("realloccg: bad size");
1236716Smckusick 	}
12411638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1254792Smckusic 		goto nospace;
1266716Smckusick 	if (bprev == 0) {
1276716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1286716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1294463Smckusic 		panic("realloccg: bad bprev");
1306716Smckusick 	}
1317650Ssam #ifdef QUOTA
13212643Ssam 	u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0);
13312643Ssam 	if (u.u_error)
13412643Ssam 		return (NULL);
1357483Skre #endif
1366294Smckusick 	cg = dtog(fs, bprev);
1375965Smckusic 	bno = fragextend(ip, cg, (long)bprev, osize, nsize);
1384463Smckusic 	if (bno != 0) {
1397187Sroot 		do {
1407187Sroot 			bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
1417187Sroot 			if (bp->b_flags & B_ERROR) {
1427187Sroot 				brelse(bp);
1437187Sroot 				return (NULL);
1447187Sroot 			}
1457187Sroot 		} while (brealloc(bp, nsize) == 0);
1467187Sroot 		bp->b_flags |= B_DONE;
1479163Ssam 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
14812643Ssam 		ip->i_blocks += btodb(nsize - osize);
14912643Ssam 		ip->i_flag |= IUPD|ICHG;
1504463Smckusic 		return (bp);
1514463Smckusic 	}
1524948Smckusic 	if (bpref >= fs->fs_size)
1534948Smckusic 		bpref = 0;
15417709Smckusick 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, fs->fs_bsize,
1559163Ssam 		(u_long (*)())alloccg);
1566567Smckusic 	if (bno > 0) {
1575965Smckusic 		obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
1585960Smckusic 		if (obp->b_flags & B_ERROR) {
1595960Smckusic 			brelse(obp);
1606294Smckusick 			return (NULL);
1615960Smckusic 		}
16217658Smckusick 		bp = getblk(ip->i_dev, fsbtodb(fs, bno), nsize);
16317658Smckusick 		bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
16417658Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
16517658Smckusick 		if (obp->b_flags & B_DELWRI) {
16617658Smckusick 			obp->b_flags &= ~B_DELWRI;
16717658Smckusick 			u.u_ru.ru_oublock--;		/* delete charge */
16817658Smckusick 		}
1694463Smckusic 		brelse(obp);
1709163Ssam 		free(ip, bprev, (off_t)osize);
17117709Smckusick 		if (nsize < fs->fs_bsize)
17217709Smckusick 			free(ip, bno + numfrags(fs, nsize),
17317709Smckusick 				(off_t)(fs->fs_bsize - nsize));
17412643Ssam 		ip->i_blocks += btodb(nsize - osize);
17512643Ssam 		ip->i_flag |= IUPD|ICHG;
1766294Smckusick 		return (bp);
1774463Smckusic 	}
1784792Smckusic nospace:
1794463Smckusic 	/*
1804463Smckusic 	 * no space available
1814463Smckusic 	 */
1824426Smckusic 	fserr(fs, "file system full");
1834426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
1844426Smckusic 	u.u_error = ENOSPC;
1854426Smckusic 	return (NULL);
1864426Smckusic }
1874426Smckusic 
1885375Smckusic /*
1895375Smckusic  * Allocate an inode in the file system.
1905375Smckusic  *
1915375Smckusic  * A preference may be optionally specified. If a preference is given
1925375Smckusic  * the following hierarchy is used to allocate an inode:
1935375Smckusic  *   1) allocate the requested inode.
1945375Smckusic  *   2) allocate an inode in the same cylinder group.
1955375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
1965375Smckusic  *      available inode is located.
1975375Smckusic  * If no inode preference is given the following heirarchy is used
1985375Smckusic  * to allocate an inode:
1995375Smckusic  *   1) allocate an inode in cylinder group 0.
2005375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2015375Smckusic  *      available inode is located.
2025375Smckusic  */
2034359Smckusick struct inode *
2045965Smckusic ialloc(pip, ipref, mode)
2055965Smckusic 	register struct inode *pip;
2064359Smckusick 	ino_t ipref;
2074359Smckusick 	int mode;
2084359Smckusick {
2095212Smckusic 	ino_t ino;
2104359Smckusick 	register struct fs *fs;
2114359Smckusick 	register struct inode *ip;
2124359Smckusick 	int cg;
2134359Smckusick 
2145965Smckusic 	fs = pip->i_fs;
2154792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2164359Smckusick 		goto noinodes;
2177650Ssam #ifdef QUOTA
21812643Ssam 	u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
21912643Ssam 	if (u.u_error)
22012643Ssam 		return (NULL);
2217483Skre #endif
2224948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2234948Smckusic 		ipref = 0;
2245377Smckusic 	cg = itog(fs, ipref);
2255965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2264359Smckusick 	if (ino == 0)
2274359Smckusick 		goto noinodes;
2285965Smckusic 	ip = iget(pip->i_dev, pip->i_fs, ino);
2294359Smckusick 	if (ip == NULL) {
23015120Skarels 		ifree(pip, ino, 0);
2314359Smckusick 		return (NULL);
2324359Smckusick 	}
2336716Smckusick 	if (ip->i_mode) {
2346716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
2356716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2364359Smckusick 		panic("ialloc: dup alloc");
2376716Smckusick 	}
23812643Ssam 	if (ip->i_blocks) {				/* XXX */
23912643Ssam 		printf("free inode %s/%d had %d blocks\n",
24012643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
24112643Ssam 		ip->i_blocks = 0;
24212643Ssam 	}
2434359Smckusick 	return (ip);
2444359Smckusick noinodes:
2454359Smckusick 	fserr(fs, "out of inodes");
2466294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
2474359Smckusick 	u.u_error = ENOSPC;
2484359Smckusick 	return (NULL);
2494359Smckusick }
2504359Smckusick 
2514651Smckusic /*
2525375Smckusic  * Find a cylinder to place a directory.
2535375Smckusic  *
2545375Smckusic  * The policy implemented by this algorithm is to select from
2555375Smckusic  * among those cylinder groups with above the average number of
2565375Smckusic  * free inodes, the one with the smallest number of directories.
2574651Smckusic  */
2589163Ssam ino_t
2595965Smckusic dirpref(fs)
2605965Smckusic 	register struct fs *fs;
2614359Smckusick {
2624651Smckusic 	int cg, minndir, mincg, avgifree;
2634359Smckusick 
2644792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
2654651Smckusic 	minndir = fs->fs_ipg;
2664359Smckusick 	mincg = 0;
2674651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
2685322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
2695322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
2704359Smckusick 			mincg = cg;
2715322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
2724359Smckusick 		}
2739163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
2744359Smckusick }
2754359Smckusick 
2764651Smckusic /*
2779163Ssam  * Select the desired position for the next block in a file.  The file is
2789163Ssam  * logically divided into sections. The first section is composed of the
2799163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
2809163Ssam  *
2819163Ssam  * If no blocks have been allocated in the first section, the policy is to
2829163Ssam  * request a block in the same cylinder group as the inode that describes
2839163Ssam  * the file. If no blocks have been allocated in any other section, the
2849163Ssam  * policy is to place the section in a cylinder group with a greater than
2859163Ssam  * average number of free blocks.  An appropriate cylinder group is found
28617696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
28717696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
28817696Smckusick  * cylinder group from which the previous allocation was made. The sweep
28917696Smckusick  * continues until a cylinder group with greater than the average number
29017696Smckusick  * of free blocks is found. If the allocation is for the first block in an
29117696Smckusick  * indirect block, the information on the previous allocation is unavailable;
29217696Smckusick  * here a best guess is made based upon the logical block number being
29317696Smckusick  * allocated.
2949163Ssam  *
2959163Ssam  * If a section is already partially allocated, the policy is to
2969163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
2979163Ssam  * contiguous blocks and the beginning of the next is physically separated
2989163Ssam  * so that the disk head will be in transit between them for at least
2999163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3009163Ssam  * schedule another I/O transfer.
3014651Smckusic  */
3025212Smckusic daddr_t
3039163Ssam blkpref(ip, lbn, indx, bap)
3049163Ssam 	struct inode *ip;
3059163Ssam 	daddr_t lbn;
3069163Ssam 	int indx;
3079163Ssam 	daddr_t *bap;
3089163Ssam {
3095965Smckusic 	register struct fs *fs;
31017696Smckusick 	register int cg;
31117696Smckusick 	int avgbfree, startcg;
3129163Ssam 	daddr_t nextblk;
3134651Smckusic 
3149163Ssam 	fs = ip->i_fs;
3159163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3169163Ssam 		if (lbn < NDADDR) {
3179163Ssam 			cg = itog(fs, ip->i_number);
3185322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3194651Smckusic 		}
3209163Ssam 		/*
3219163Ssam 		 * Find a cylinder with greater than average number of
3229163Ssam 		 * unused data blocks.
3239163Ssam 		 */
32417696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
32517696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
32617696Smckusick 		else
32717696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
32817696Smckusick 		startcg %= fs->fs_ncg;
3299163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
33017696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
3319163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3329163Ssam 				fs->fs_cgrotor = cg;
3339163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3349163Ssam 			}
33517696Smckusick 		for (cg = 0; cg <= startcg; cg++)
3369163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3379163Ssam 				fs->fs_cgrotor = cg;
3389163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3399163Ssam 			}
3409163Ssam 		return (NULL);
3419163Ssam 	}
3429163Ssam 	/*
3439163Ssam 	 * One or more previous blocks have been laid out. If less
3449163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
3459163Ssam 	 * next block is requested contiguously, otherwise it is
3469163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
3479163Ssam 	 */
3489163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
3499163Ssam 	if (indx > fs->fs_maxcontig &&
35011638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
3519163Ssam 	    != nextblk)
3529163Ssam 		return (nextblk);
3539163Ssam 	if (fs->fs_rotdelay != 0)
3549163Ssam 		/*
3559163Ssam 		 * Here we convert ms of delay to frags as:
3569163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
3579163Ssam 		 *	((sect/frag) * (ms/sec))
3589163Ssam 		 * then round up to the next block.
3599163Ssam 		 */
3609163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
3619163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
3629163Ssam 	return (nextblk);
3634651Smckusic }
3644651Smckusic 
3655375Smckusic /*
3665375Smckusic  * Implement the cylinder overflow algorithm.
3675375Smckusic  *
3685375Smckusic  * The policy implemented by this algorithm is:
3695375Smckusic  *   1) allocate the block in its requested cylinder group.
3705375Smckusic  *   2) quadradically rehash on the cylinder group number.
3715375Smckusic  *   3) brute force search for a free block.
3725375Smckusic  */
3735212Smckusic /*VARARGS5*/
3745212Smckusic u_long
3755965Smckusic hashalloc(ip, cg, pref, size, allocator)
3765965Smckusic 	struct inode *ip;
3774359Smckusick 	int cg;
3784359Smckusick 	long pref;
3794359Smckusick 	int size;	/* size for data blocks, mode for inodes */
3805212Smckusic 	u_long (*allocator)();
3814359Smckusick {
3825965Smckusic 	register struct fs *fs;
3834359Smckusick 	long result;
3844359Smckusick 	int i, icg = cg;
3854359Smckusick 
3865965Smckusic 	fs = ip->i_fs;
3874359Smckusick 	/*
3884359Smckusick 	 * 1: preferred cylinder group
3894359Smckusick 	 */
3905965Smckusic 	result = (*allocator)(ip, cg, pref, size);
3914359Smckusick 	if (result)
3924359Smckusick 		return (result);
3934359Smckusick 	/*
3944359Smckusick 	 * 2: quadratic rehash
3954359Smckusick 	 */
3964359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
3974359Smckusick 		cg += i;
3984359Smckusick 		if (cg >= fs->fs_ncg)
3994359Smckusick 			cg -= fs->fs_ncg;
4005965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4014359Smckusick 		if (result)
4024359Smckusick 			return (result);
4034359Smckusick 	}
4044359Smckusick 	/*
4054359Smckusick 	 * 3: brute force search
40610847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
40710847Ssam 	 * and 1 is always checked in the quadratic rehash.
4084359Smckusick 	 */
40910848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
41010847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4115965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4124359Smckusick 		if (result)
4134359Smckusick 			return (result);
4144359Smckusick 		cg++;
4154359Smckusick 		if (cg == fs->fs_ncg)
4164359Smckusick 			cg = 0;
4174359Smckusick 	}
4186294Smckusick 	return (NULL);
4194359Smckusick }
4204359Smckusick 
4215375Smckusic /*
4225375Smckusic  * Determine whether a fragment can be extended.
4235375Smckusic  *
4245375Smckusic  * Check to see if the necessary fragments are available, and
4255375Smckusic  * if they are, allocate them.
4265375Smckusic  */
4274359Smckusick daddr_t
4285965Smckusic fragextend(ip, cg, bprev, osize, nsize)
4295965Smckusic 	struct inode *ip;
4304426Smckusic 	int cg;
4314463Smckusic 	long bprev;
4324426Smckusic 	int osize, nsize;
4334426Smckusic {
4345965Smckusic 	register struct fs *fs;
4354463Smckusic 	register struct buf *bp;
4364463Smckusic 	register struct cg *cgp;
4374463Smckusic 	long bno;
4384463Smckusic 	int frags, bbase;
4394426Smckusic 	int i;
4404426Smckusic 
4415965Smckusic 	fs = ip->i_fs;
44217224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
4436531Smckusick 		return (NULL);
4445960Smckusic 	frags = numfrags(fs, nsize);
44517224Smckusick 	bbase = fragnum(fs, bprev);
44617224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
4474463Smckusic 		/* cannot extend across a block boundry */
4486294Smckusick 		return (NULL);
4494463Smckusic 	}
45010278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
4516531Smckusick 	cgp = bp->b_un.b_cg;
4526531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
4535960Smckusic 		brelse(bp);
4546294Smckusick 		return (NULL);
4555960Smckusic 	}
4568105Sroot 	cgp->cg_time = time.tv_sec;
4575377Smckusic 	bno = dtogd(fs, bprev);
4585960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
4595361Smckusic 		if (isclr(cgp->cg_free, bno + i)) {
4605361Smckusic 			brelse(bp);
4616294Smckusick 			return (NULL);
4625361Smckusic 		}
4635361Smckusic 	/*
4645361Smckusic 	 * the current fragment can be extended
4655361Smckusic 	 * deduct the count on fragment being extended into
4665361Smckusic 	 * increase the count on the remaining fragment (if any)
4675361Smckusic 	 * allocate the extended piece
4685361Smckusic 	 */
4695361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
4704463Smckusic 		if (isclr(cgp->cg_free, bno + i))
4714463Smckusic 			break;
4725960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
4735361Smckusic 	if (i != frags)
4745361Smckusic 		cgp->cg_frsum[i - frags]++;
4755960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
4765361Smckusic 		clrbit(cgp->cg_free, bno + i);
4775361Smckusic 		cgp->cg_cs.cs_nffree--;
4785361Smckusic 		fs->fs_cstotal.cs_nffree--;
4795361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
4804463Smckusic 	}
4815361Smckusic 	fs->fs_fmod++;
4825361Smckusic 	bdwrite(bp);
4835361Smckusic 	return (bprev);
4844426Smckusic }
4854426Smckusic 
4865375Smckusic /*
4875375Smckusic  * Determine whether a block can be allocated.
4885375Smckusic  *
4895375Smckusic  * Check to see if a block of the apprpriate size is available,
4905375Smckusic  * and if it is, allocate it.
4915375Smckusic  */
4929163Ssam daddr_t
4935965Smckusic alloccg(ip, cg, bpref, size)
4945965Smckusic 	struct inode *ip;
4954359Smckusick 	int cg;
4964359Smckusick 	daddr_t bpref;
4974359Smckusick 	int size;
4984359Smckusick {
4995965Smckusic 	register struct fs *fs;
5004463Smckusic 	register struct buf *bp;
5014463Smckusic 	register struct cg *cgp;
5024463Smckusic 	int bno, frags;
5034463Smckusic 	int allocsiz;
5044463Smckusic 	register int i;
5054359Smckusick 
5065965Smckusic 	fs = ip->i_fs;
5075322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5086294Smckusick 		return (NULL);
50910278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
5106531Smckusick 	cgp = bp->b_un.b_cg;
51115950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
51215950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5135960Smckusic 		brelse(bp);
5146294Smckusick 		return (NULL);
5155960Smckusic 	}
5168105Sroot 	cgp->cg_time = time.tv_sec;
5175322Smckusic 	if (size == fs->fs_bsize) {
5185212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5194463Smckusic 		bdwrite(bp);
5204463Smckusic 		return (bno);
5214463Smckusic 	}
5224463Smckusic 	/*
5234463Smckusic 	 * check to see if any fragments are already available
5244463Smckusic 	 * allocsiz is the size which will be allocated, hacking
5254463Smckusic 	 * it down to a smaller size if necessary
5264463Smckusic 	 */
5275960Smckusic 	frags = numfrags(fs, size);
5285322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
5294463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
5304463Smckusic 			break;
5315322Smckusic 	if (allocsiz == fs->fs_frag) {
5324463Smckusic 		/*
5334463Smckusic 		 * no fragments were available, so a block will be
5344463Smckusic 		 * allocated, and hacked up
5354463Smckusic 		 */
5364792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
5374463Smckusic 			brelse(bp);
5386294Smckusick 			return (NULL);
5394463Smckusic 		}
5405212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5415377Smckusic 		bpref = dtogd(fs, bno);
5425322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
5434463Smckusic 			setbit(cgp->cg_free, bpref + i);
5445322Smckusic 		i = fs->fs_frag - frags;
5454792Smckusic 		cgp->cg_cs.cs_nffree += i;
5464792Smckusic 		fs->fs_cstotal.cs_nffree += i;
5475322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
5489762Ssam 		fs->fs_fmod++;
5494463Smckusic 		cgp->cg_frsum[i]++;
5504463Smckusic 		bdwrite(bp);
5514463Smckusic 		return (bno);
5524463Smckusic 	}
5534651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
55415950Smckusick 	if (bno < 0) {
55515950Smckusick 		brelse(bp);
5566294Smckusick 		return (NULL);
55715950Smckusick 	}
5584463Smckusic 	for (i = 0; i < frags; i++)
5594463Smckusic 		clrbit(cgp->cg_free, bno + i);
5604792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
5614792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
5625322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
5639762Ssam 	fs->fs_fmod++;
5644463Smckusic 	cgp->cg_frsum[allocsiz]--;
5654463Smckusic 	if (frags != allocsiz)
5664463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
5674463Smckusic 	bdwrite(bp);
5684463Smckusic 	return (cg * fs->fs_fpg + bno);
5694463Smckusic }
5704463Smckusic 
5715375Smckusic /*
5725375Smckusic  * Allocate a block in a cylinder group.
5735375Smckusic  *
5745375Smckusic  * This algorithm implements the following policy:
5755375Smckusic  *   1) allocate the requested block.
5765375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
5775375Smckusic  *   3) allocate the next available block on the block rotor for the
5785375Smckusic  *      specified cylinder group.
5795375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
5805375Smckusic  * blocks may be fragmented by the routine that allocates them.
5815375Smckusic  */
5824463Smckusic daddr_t
5835212Smckusic alloccgblk(fs, cgp, bpref)
5845965Smckusic 	register struct fs *fs;
5854463Smckusic 	register struct cg *cgp;
5864463Smckusic 	daddr_t bpref;
5874463Smckusic {
5884651Smckusic 	daddr_t bno;
5896294Smckusick 	int cylno, pos, delta;
5904651Smckusic 	short *cylbp;
5915361Smckusic 	register int i;
5924463Smckusic 
5934651Smckusic 	if (bpref == 0) {
5944651Smckusic 		bpref = cgp->cg_rotor;
5955361Smckusic 		goto norot;
5965361Smckusic 	}
59717224Smckusick 	bpref = blknum(fs, bpref);
5985377Smckusic 	bpref = dtogd(fs, bpref);
5995361Smckusic 	/*
6005361Smckusic 	 * if the requested block is available, use it
6015361Smckusic 	 */
60211638Ssam 	if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) {
6035361Smckusic 		bno = bpref;
6045361Smckusic 		goto gotit;
6055361Smckusic 	}
6065361Smckusic 	/*
6075361Smckusic 	 * check for a block available on the same cylinder
6085361Smckusic 	 */
6095361Smckusic 	cylno = cbtocylno(fs, bpref);
6105375Smckusic 	if (cgp->cg_btot[cylno] == 0)
6115375Smckusic 		goto norot;
6125375Smckusic 	if (fs->fs_cpc == 0) {
6135375Smckusic 		/*
6145375Smckusic 		 * block layout info is not available, so just have
6155375Smckusic 		 * to take any block in this cylinder.
6165375Smckusic 		 */
6175375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
6185375Smckusic 		goto norot;
6195375Smckusic 	}
6205375Smckusic 	/*
6215361Smckusic 	 * check the summary information to see if a block is
6225361Smckusic 	 * available in the requested cylinder starting at the
6239163Ssam 	 * requested rotational position and proceeding around.
6245361Smckusic 	 */
6259163Ssam 	cylbp = cgp->cg_b[cylno];
6269163Ssam 	pos = cbtorpos(fs, bpref);
6275361Smckusic 	for (i = pos; i < NRPOS; i++)
6285361Smckusic 		if (cylbp[i] > 0)
6295361Smckusic 			break;
6305361Smckusic 	if (i == NRPOS)
6315361Smckusic 		for (i = 0; i < pos; i++)
6325361Smckusic 			if (cylbp[i] > 0)
6335361Smckusic 				break;
6345361Smckusic 	if (cylbp[i] > 0) {
6354651Smckusic 		/*
6365361Smckusic 		 * found a rotational position, now find the actual
6375361Smckusic 		 * block. A panic if none is actually there.
6384651Smckusic 		 */
6395361Smckusic 		pos = cylno % fs->fs_cpc;
6405361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
6416716Smckusick 		if (fs->fs_postbl[pos][i] == -1) {
6426716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
6436716Smckusick 			    pos, i, fs->fs_fsmnt);
6445361Smckusic 			panic("alloccgblk: cyl groups corrupted");
6456716Smckusick 		}
6466294Smckusick 		for (i = fs->fs_postbl[pos][i];; ) {
6475361Smckusic 			if (isblock(fs, cgp->cg_free, bno + i)) {
64811638Ssam 				bno = blkstofrags(fs, (bno + i));
6495361Smckusic 				goto gotit;
6505361Smckusic 			}
6516294Smckusick 			delta = fs->fs_rotbl[i];
6526294Smckusick 			if (delta <= 0 || delta > MAXBPC - i)
6534651Smckusic 				break;
6546294Smckusick 			i += delta;
6554651Smckusic 		}
6566716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
6575361Smckusic 		panic("alloccgblk: can't find blk in cyl");
6584359Smckusick 	}
6595361Smckusic norot:
6605361Smckusic 	/*
6615361Smckusic 	 * no blocks in the requested cylinder, so take next
6625361Smckusic 	 * available one in this cylinder group.
6635361Smckusic 	 */
6648628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
6656567Smckusic 	if (bno < 0)
6666294Smckusick 		return (NULL);
6674651Smckusic 	cgp->cg_rotor = bno;
6684359Smckusick gotit:
66911638Ssam 	clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno));
6704792Smckusic 	cgp->cg_cs.cs_nbfree--;
6714792Smckusic 	fs->fs_cstotal.cs_nbfree--;
6725322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
6735375Smckusic 	cylno = cbtocylno(fs, bno);
6745375Smckusic 	cgp->cg_b[cylno][cbtorpos(fs, bno)]--;
6755375Smckusic 	cgp->cg_btot[cylno]--;
6764359Smckusick 	fs->fs_fmod++;
6774651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
6784359Smckusick }
6794359Smckusick 
6805375Smckusic /*
6815375Smckusic  * Determine whether an inode can be allocated.
6825375Smckusic  *
6835375Smckusic  * Check to see if an inode is available, and if it is,
6845375Smckusic  * allocate it using the following policy:
6855375Smckusic  *   1) allocate the requested inode.
6865375Smckusic  *   2) allocate the next available inode after the requested
6875375Smckusic  *      inode in the specified cylinder group.
6885375Smckusic  */
6899163Ssam ino_t
6905965Smckusic ialloccg(ip, cg, ipref, mode)
6915965Smckusic 	struct inode *ip;
6924359Smckusick 	int cg;
6934359Smckusick 	daddr_t ipref;
6944359Smckusick 	int mode;
6954359Smckusick {
6965965Smckusic 	register struct fs *fs;
6974463Smckusic 	register struct cg *cgp;
69816784Smckusick 	struct buf *bp;
69916784Smckusick 	int start, len, loc, map, i;
7004359Smckusick 
7015965Smckusic 	fs = ip->i_fs;
7025322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7036294Smckusick 		return (NULL);
70410278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
7056531Smckusick 	cgp = bp->b_un.b_cg;
70615950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
70715950Smckusick 	    cgp->cg_cs.cs_nifree == 0) {
7085960Smckusic 		brelse(bp);
7096294Smckusick 		return (NULL);
7105960Smckusic 	}
7118105Sroot 	cgp->cg_time = time.tv_sec;
7124359Smckusick 	if (ipref) {
7134359Smckusick 		ipref %= fs->fs_ipg;
7144359Smckusick 		if (isclr(cgp->cg_iused, ipref))
7154359Smckusick 			goto gotit;
71616784Smckusick 	}
71716784Smckusick 	start = cgp->cg_irotor / NBBY;
71816784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
71916784Smckusick 	loc = skpc(0xff, len, &cgp->cg_iused[start]);
72016784Smckusick 	if (loc == 0) {
72117697Smckusick 		len = start + 1;
72217697Smckusick 		start = 0;
72317697Smckusick 		loc = skpc(0xff, len, &cgp->cg_iused[0]);
72417697Smckusick 		if (loc == 0) {
72517697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
72617697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
72717697Smckusick 			panic("ialloccg: map corrupted");
72817697Smckusick 			/* NOTREACHED */
72917697Smckusick 		}
73016784Smckusick 	}
73116784Smckusick 	i = start + len - loc;
73216784Smckusick 	map = cgp->cg_iused[i];
73316784Smckusick 	ipref = i * NBBY;
73416784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
73516784Smckusick 		if ((map & i) == 0) {
7364359Smckusick 			cgp->cg_irotor = ipref;
7374359Smckusick 			goto gotit;
7384359Smckusick 		}
7394359Smckusick 	}
74016784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
74116784Smckusick 	panic("ialloccg: block not in map");
74216784Smckusick 	/* NOTREACHED */
7434359Smckusick gotit:
7444359Smckusick 	setbit(cgp->cg_iused, ipref);
7454792Smckusic 	cgp->cg_cs.cs_nifree--;
7464792Smckusic 	fs->fs_cstotal.cs_nifree--;
7475322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
7484359Smckusick 	fs->fs_fmod++;
7494359Smckusick 	if ((mode & IFMT) == IFDIR) {
7504792Smckusic 		cgp->cg_cs.cs_ndir++;
7514792Smckusic 		fs->fs_cstotal.cs_ndir++;
7525322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
7534359Smckusick 	}
7544359Smckusick 	bdwrite(bp);
7554359Smckusick 	return (cg * fs->fs_ipg + ipref);
7564359Smckusick }
7574359Smckusick 
7585375Smckusic /*
7595375Smckusic  * Free a block or fragment.
7605375Smckusic  *
7615375Smckusic  * The specified block or fragment is placed back in the
7625375Smckusic  * free map. If a fragment is deallocated, a possible
7635375Smckusic  * block reassembly is checked.
7645375Smckusic  */
7659163Ssam free(ip, bno, size)
7665965Smckusic 	register struct inode *ip;
7674359Smckusick 	daddr_t bno;
7685212Smckusic 	off_t size;
7694359Smckusick {
7704359Smckusick 	register struct fs *fs;
7714359Smckusick 	register struct cg *cgp;
7724359Smckusick 	register struct buf *bp;
7734463Smckusic 	int cg, blk, frags, bbase;
7744463Smckusic 	register int i;
7754359Smckusick 
7765965Smckusic 	fs = ip->i_fs;
7776716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
7786716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
7796716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7804426Smckusic 		panic("free: bad size");
7816716Smckusick 	}
7825377Smckusic 	cg = dtog(fs, bno);
7836567Smckusic 	if (badblock(fs, bno)) {
7846567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
7854359Smckusick 		return;
7866567Smckusic 	}
78710278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
7886531Smckusick 	cgp = bp->b_un.b_cg;
7896531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
7905960Smckusic 		brelse(bp);
7914359Smckusick 		return;
7925960Smckusic 	}
7938105Sroot 	cgp->cg_time = time.tv_sec;
7945377Smckusic 	bno = dtogd(fs, bno);
7955322Smckusic 	if (size == fs->fs_bsize) {
79611638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) {
7976716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
7986716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
7994426Smckusic 			panic("free: freeing free block");
8006567Smckusic 		}
80111638Ssam 		setblock(fs, cgp->cg_free, fragstoblks(fs, bno));
8024792Smckusic 		cgp->cg_cs.cs_nbfree++;
8034792Smckusic 		fs->fs_cstotal.cs_nbfree++;
8045322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
8055375Smckusic 		i = cbtocylno(fs, bno);
8065375Smckusic 		cgp->cg_b[i][cbtorpos(fs, bno)]++;
8075375Smckusic 		cgp->cg_btot[i]++;
8084426Smckusic 	} else {
80917224Smckusick 		bbase = bno - fragnum(fs, bno);
8104463Smckusic 		/*
8114463Smckusic 		 * decrement the counts associated with the old frags
8124463Smckusic 		 */
8136294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
8145322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
8154463Smckusic 		/*
8164463Smckusic 		 * deallocate the fragment
8174463Smckusic 		 */
8185960Smckusic 		frags = numfrags(fs, size);
8194463Smckusic 		for (i = 0; i < frags; i++) {
8206716Smckusick 			if (isset(cgp->cg_free, bno + i)) {
8216716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
8226716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
8234426Smckusic 				panic("free: freeing free frag");
8246716Smckusick 			}
8254426Smckusic 			setbit(cgp->cg_free, bno + i);
8264426Smckusic 		}
8276294Smckusick 		cgp->cg_cs.cs_nffree += i;
8286294Smckusick 		fs->fs_cstotal.cs_nffree += i;
8296294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
8304463Smckusic 		/*
8314463Smckusic 		 * add back in counts associated with the new frags
8324463Smckusic 		 */
8336294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
8345322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
8354463Smckusic 		/*
8364463Smckusic 		 * if a complete block has been reassembled, account for it
8374463Smckusic 		 */
83811638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) {
8395322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
8405322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
8415322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
8424792Smckusic 			cgp->cg_cs.cs_nbfree++;
8434792Smckusic 			fs->fs_cstotal.cs_nbfree++;
8445322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
8455375Smckusic 			i = cbtocylno(fs, bbase);
8465375Smckusic 			cgp->cg_b[i][cbtorpos(fs, bbase)]++;
8475375Smckusic 			cgp->cg_btot[i]++;
8484426Smckusic 		}
8494426Smckusic 	}
8504359Smckusick 	fs->fs_fmod++;
8514359Smckusick 	bdwrite(bp);
8524359Smckusick }
8534359Smckusick 
8545375Smckusic /*
8555375Smckusic  * Free an inode.
8565375Smckusic  *
8575375Smckusic  * The specified inode is placed back in the free map.
8585375Smckusic  */
8595965Smckusic ifree(ip, ino, mode)
8605965Smckusic 	struct inode *ip;
8614359Smckusick 	ino_t ino;
8624359Smckusick 	int mode;
8634359Smckusick {
8644359Smckusick 	register struct fs *fs;
8654359Smckusick 	register struct cg *cgp;
8664359Smckusick 	register struct buf *bp;
8674359Smckusick 	int cg;
8684359Smckusick 
8695965Smckusic 	fs = ip->i_fs;
8706716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
8716716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
8726716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
8734359Smckusick 		panic("ifree: range");
8746716Smckusick 	}
8755377Smckusic 	cg = itog(fs, ino);
87610278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
8776531Smckusick 	cgp = bp->b_un.b_cg;
8786531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
8795960Smckusic 		brelse(bp);
8804359Smckusick 		return;
8815960Smckusic 	}
8828105Sroot 	cgp->cg_time = time.tv_sec;
8834359Smckusick 	ino %= fs->fs_ipg;
8846716Smckusick 	if (isclr(cgp->cg_iused, ino)) {
8856716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
8866716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
8874359Smckusick 		panic("ifree: freeing free inode");
8886716Smckusick 	}
8894359Smckusick 	clrbit(cgp->cg_iused, ino);
89016784Smckusick 	if (ino < cgp->cg_irotor)
89116784Smckusick 		cgp->cg_irotor = ino;
8924792Smckusic 	cgp->cg_cs.cs_nifree++;
8934792Smckusic 	fs->fs_cstotal.cs_nifree++;
8945322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
8954359Smckusick 	if ((mode & IFMT) == IFDIR) {
8964792Smckusic 		cgp->cg_cs.cs_ndir--;
8974792Smckusic 		fs->fs_cstotal.cs_ndir--;
8985322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
8994359Smckusick 	}
9004359Smckusick 	fs->fs_fmod++;
9014359Smckusick 	bdwrite(bp);
9024359Smckusick }
9034359Smckusick 
9044463Smckusic /*
9055375Smckusic  * Find a block of the specified size in the specified cylinder group.
9065375Smckusic  *
9074651Smckusic  * It is a panic if a request is made to find a block if none are
9084651Smckusic  * available.
9094651Smckusic  */
9104651Smckusic daddr_t
9114651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
9124651Smckusic 	register struct fs *fs;
9134651Smckusic 	register struct cg *cgp;
9144651Smckusic 	daddr_t bpref;
9154651Smckusic 	int allocsiz;
9164651Smckusic {
9174651Smckusic 	daddr_t bno;
9184651Smckusic 	int start, len, loc, i;
9194651Smckusic 	int blk, field, subfield, pos;
9204651Smckusic 
9214651Smckusic 	/*
9224651Smckusic 	 * find the fragment by searching through the free block
9234651Smckusic 	 * map for an appropriate bit pattern
9244651Smckusic 	 */
9254651Smckusic 	if (bpref)
9265377Smckusic 		start = dtogd(fs, bpref) / NBBY;
9274651Smckusic 	else
9284651Smckusic 		start = cgp->cg_frotor / NBBY;
9295398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
93012755Ssam 	loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start],
93112755Ssam 		(caddr_t)fragtbl[fs->fs_frag],
93212755Ssam 		(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
9334651Smckusic 	if (loc == 0) {
9346531Smckusick 		len = start + 1;
9356531Smckusick 		start = 0;
93617697Smckusick 		loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[0],
93712755Ssam 			(caddr_t)fragtbl[fs->fs_frag],
93812755Ssam 			(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
93916784Smckusick 		if (loc == 0) {
94016784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
94116784Smckusick 			    start, len, fs->fs_fsmnt);
94216784Smckusick 			panic("alloccg: map corrupted");
94317697Smckusick 			/* NOTREACHED */
94416784Smckusick 		}
9454651Smckusic 	}
9464651Smckusic 	bno = (start + len - loc) * NBBY;
9474651Smckusic 	cgp->cg_frotor = bno;
9484651Smckusic 	/*
9494651Smckusic 	 * found the byte in the map
9504651Smckusic 	 * sift through the bits to find the selected frag
9514651Smckusic 	 */
9526294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
9536294Smckusick 		blk = blkmap(fs, cgp->cg_free, bno);
9544651Smckusic 		blk <<= 1;
9554651Smckusic 		field = around[allocsiz];
9564651Smckusic 		subfield = inside[allocsiz];
9575322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
9586294Smckusick 			if ((blk & field) == subfield)
9596294Smckusick 				return (bno + pos);
9604651Smckusic 			field <<= 1;
9614651Smckusic 			subfield <<= 1;
9624651Smckusic 		}
9634651Smckusic 	}
9646716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
9654651Smckusic 	panic("alloccg: block not in map");
9666531Smckusick 	return (-1);
9674651Smckusic }
9684651Smckusic 
9694651Smckusic /*
9705375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
9715375Smckusic  *
9725375Smckusic  * The form of the error message is:
9734359Smckusick  *	fs: error message
9744359Smckusick  */
9754359Smckusick fserr(fs, cp)
9764359Smckusick 	struct fs *fs;
9774359Smckusick 	char *cp;
9784359Smckusick {
9794359Smckusick 
98018307Sralph 	log(KERN_FAIL, "%s: %s\n", fs->fs_fsmnt, cp);
9814359Smckusick }
982