xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 24839)
123394Smckusick /*
223394Smckusick  * Copyright (c) 1982 Regents of the University of California.
323394Smckusick  * All rights reserved.  The Berkeley software License Agreement
423394Smckusick  * specifies the terms and conditions for redistribution.
523394Smckusick  *
6*24839Seric  *	@(#)lfs_alloc.c	6.16 (Berkeley) 09/17/85
723394Smckusick  */
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;
11524698Smckusick 	int cg, request;
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;
15424698Smckusick 	if (fs->fs_optim == FS_OPTSPACE)
15524698Smckusick 		request = nsize;
15624698Smckusick 	else /* if (fs->fs_optim == FS_OPTTIME) */
15724698Smckusick 		request = fs->fs_bsize;
15824698Smckusick 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
1599163Ssam 		(u_long (*)())alloccg);
1606567Smckusic 	if (bno > 0) {
1615965Smckusic 		obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
1625960Smckusic 		if (obp->b_flags & B_ERROR) {
1635960Smckusic 			brelse(obp);
1646294Smckusick 			return (NULL);
1655960Smckusic 		}
16617658Smckusick 		bp = getblk(ip->i_dev, fsbtodb(fs, bno), nsize);
16717658Smckusick 		bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
16817658Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
16917658Smckusick 		if (obp->b_flags & B_DELWRI) {
17017658Smckusick 			obp->b_flags &= ~B_DELWRI;
17117658Smckusick 			u.u_ru.ru_oublock--;		/* delete charge */
17217658Smckusick 		}
1734463Smckusic 		brelse(obp);
1749163Ssam 		free(ip, bprev, (off_t)osize);
17524698Smckusick 		if (nsize < request)
17617709Smckusick 			free(ip, bno + numfrags(fs, nsize),
17724698Smckusick 				(off_t)(request - nsize));
17812643Ssam 		ip->i_blocks += btodb(nsize - osize);
17912643Ssam 		ip->i_flag |= IUPD|ICHG;
1806294Smckusick 		return (bp);
1814463Smckusic 	}
1824792Smckusic nospace:
1834463Smckusic 	/*
1844463Smckusic 	 * no space available
1854463Smckusic 	 */
1864426Smckusic 	fserr(fs, "file system full");
1874426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
1884426Smckusic 	u.u_error = ENOSPC;
1894426Smckusic 	return (NULL);
1904426Smckusic }
1914426Smckusic 
1925375Smckusic /*
1935375Smckusic  * Allocate an inode in the file system.
1945375Smckusic  *
1955375Smckusic  * A preference may be optionally specified. If a preference is given
1965375Smckusic  * the following hierarchy is used to allocate an inode:
1975375Smckusic  *   1) allocate the requested inode.
1985375Smckusic  *   2) allocate an inode in the same cylinder group.
1995375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2005375Smckusic  *      available inode is located.
2015375Smckusic  * If no inode preference is given the following heirarchy is used
2025375Smckusic  * to allocate an inode:
2035375Smckusic  *   1) allocate an inode in cylinder group 0.
2045375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2055375Smckusic  *      available inode is located.
2065375Smckusic  */
2074359Smckusick struct inode *
2085965Smckusic ialloc(pip, ipref, mode)
2095965Smckusic 	register struct inode *pip;
2104359Smckusick 	ino_t ipref;
2114359Smckusick 	int mode;
2124359Smckusick {
2135212Smckusic 	ino_t ino;
2144359Smckusick 	register struct fs *fs;
2154359Smckusick 	register struct inode *ip;
2164359Smckusick 	int cg;
2174359Smckusick 
2185965Smckusic 	fs = pip->i_fs;
2194792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2204359Smckusick 		goto noinodes;
2217650Ssam #ifdef QUOTA
22212643Ssam 	u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
22312643Ssam 	if (u.u_error)
22412643Ssam 		return (NULL);
2257483Skre #endif
2264948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2274948Smckusic 		ipref = 0;
2285377Smckusic 	cg = itog(fs, ipref);
2295965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2304359Smckusick 	if (ino == 0)
2314359Smckusick 		goto noinodes;
2325965Smckusic 	ip = iget(pip->i_dev, pip->i_fs, ino);
2334359Smckusick 	if (ip == NULL) {
23415120Skarels 		ifree(pip, ino, 0);
2354359Smckusick 		return (NULL);
2364359Smckusick 	}
2376716Smckusick 	if (ip->i_mode) {
2386716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
2396716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2404359Smckusick 		panic("ialloc: dup alloc");
2416716Smckusick 	}
24212643Ssam 	if (ip->i_blocks) {				/* XXX */
24312643Ssam 		printf("free inode %s/%d had %d blocks\n",
24412643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
24512643Ssam 		ip->i_blocks = 0;
24612643Ssam 	}
2474359Smckusick 	return (ip);
2484359Smckusick noinodes:
2494359Smckusick 	fserr(fs, "out of inodes");
2506294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
2514359Smckusick 	u.u_error = ENOSPC;
2524359Smckusick 	return (NULL);
2534359Smckusick }
2544359Smckusick 
2554651Smckusic /*
2565375Smckusic  * Find a cylinder to place a directory.
2575375Smckusic  *
2585375Smckusic  * The policy implemented by this algorithm is to select from
2595375Smckusic  * among those cylinder groups with above the average number of
2605375Smckusic  * free inodes, the one with the smallest number of directories.
2614651Smckusic  */
2629163Ssam ino_t
2635965Smckusic dirpref(fs)
2645965Smckusic 	register struct fs *fs;
2654359Smckusick {
2664651Smckusic 	int cg, minndir, mincg, avgifree;
2674359Smckusick 
2684792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
2694651Smckusic 	minndir = fs->fs_ipg;
2704359Smckusick 	mincg = 0;
2714651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
2725322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
2735322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
2744359Smckusick 			mincg = cg;
2755322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
2764359Smckusick 		}
2779163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
2784359Smckusick }
2794359Smckusick 
2804651Smckusic /*
2819163Ssam  * Select the desired position for the next block in a file.  The file is
2829163Ssam  * logically divided into sections. The first section is composed of the
2839163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
2849163Ssam  *
2859163Ssam  * If no blocks have been allocated in the first section, the policy is to
2869163Ssam  * request a block in the same cylinder group as the inode that describes
2879163Ssam  * the file. If no blocks have been allocated in any other section, the
2889163Ssam  * policy is to place the section in a cylinder group with a greater than
2899163Ssam  * average number of free blocks.  An appropriate cylinder group is found
29017696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
29117696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
29217696Smckusick  * cylinder group from which the previous allocation was made. The sweep
29317696Smckusick  * continues until a cylinder group with greater than the average number
29417696Smckusick  * of free blocks is found. If the allocation is for the first block in an
29517696Smckusick  * indirect block, the information on the previous allocation is unavailable;
29617696Smckusick  * here a best guess is made based upon the logical block number being
29717696Smckusick  * allocated.
2989163Ssam  *
2999163Ssam  * If a section is already partially allocated, the policy is to
3009163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3019163Ssam  * contiguous blocks and the beginning of the next is physically separated
3029163Ssam  * so that the disk head will be in transit between them for at least
3039163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3049163Ssam  * schedule another I/O transfer.
3054651Smckusic  */
3065212Smckusic daddr_t
3079163Ssam blkpref(ip, lbn, indx, bap)
3089163Ssam 	struct inode *ip;
3099163Ssam 	daddr_t lbn;
3109163Ssam 	int indx;
3119163Ssam 	daddr_t *bap;
3129163Ssam {
3135965Smckusic 	register struct fs *fs;
31417696Smckusick 	register int cg;
31517696Smckusick 	int avgbfree, startcg;
3169163Ssam 	daddr_t nextblk;
3174651Smckusic 
3189163Ssam 	fs = ip->i_fs;
3199163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3209163Ssam 		if (lbn < NDADDR) {
3219163Ssam 			cg = itog(fs, ip->i_number);
3225322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3234651Smckusic 		}
3249163Ssam 		/*
3259163Ssam 		 * Find a cylinder with greater than average number of
3269163Ssam 		 * unused data blocks.
3279163Ssam 		 */
32817696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
32917696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
33017696Smckusick 		else
33117696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
33217696Smckusick 		startcg %= fs->fs_ncg;
3339163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
33417696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
3359163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3369163Ssam 				fs->fs_cgrotor = cg;
3379163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3389163Ssam 			}
33917696Smckusick 		for (cg = 0; cg <= startcg; cg++)
3409163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3419163Ssam 				fs->fs_cgrotor = cg;
3429163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3439163Ssam 			}
3449163Ssam 		return (NULL);
3459163Ssam 	}
3469163Ssam 	/*
3479163Ssam 	 * One or more previous blocks have been laid out. If less
3489163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
3499163Ssam 	 * next block is requested contiguously, otherwise it is
3509163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
3519163Ssam 	 */
3529163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
3539163Ssam 	if (indx > fs->fs_maxcontig &&
35411638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
3559163Ssam 	    != nextblk)
3569163Ssam 		return (nextblk);
3579163Ssam 	if (fs->fs_rotdelay != 0)
3589163Ssam 		/*
3599163Ssam 		 * Here we convert ms of delay to frags as:
3609163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
3619163Ssam 		 *	((sect/frag) * (ms/sec))
3629163Ssam 		 * then round up to the next block.
3639163Ssam 		 */
3649163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
3659163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
3669163Ssam 	return (nextblk);
3674651Smckusic }
3684651Smckusic 
3695375Smckusic /*
3705375Smckusic  * Implement the cylinder overflow algorithm.
3715375Smckusic  *
3725375Smckusic  * The policy implemented by this algorithm is:
3735375Smckusic  *   1) allocate the block in its requested cylinder group.
3745375Smckusic  *   2) quadradically rehash on the cylinder group number.
3755375Smckusic  *   3) brute force search for a free block.
3765375Smckusic  */
3775212Smckusic /*VARARGS5*/
3785212Smckusic u_long
3795965Smckusic hashalloc(ip, cg, pref, size, allocator)
3805965Smckusic 	struct inode *ip;
3814359Smckusick 	int cg;
3824359Smckusick 	long pref;
3834359Smckusick 	int size;	/* size for data blocks, mode for inodes */
3845212Smckusic 	u_long (*allocator)();
3854359Smckusick {
3865965Smckusic 	register struct fs *fs;
3874359Smckusick 	long result;
3884359Smckusick 	int i, icg = cg;
3894359Smckusick 
3905965Smckusic 	fs = ip->i_fs;
3914359Smckusick 	/*
3924359Smckusick 	 * 1: preferred cylinder group
3934359Smckusick 	 */
3945965Smckusic 	result = (*allocator)(ip, cg, pref, size);
3954359Smckusick 	if (result)
3964359Smckusick 		return (result);
3974359Smckusick 	/*
3984359Smckusick 	 * 2: quadratic rehash
3994359Smckusick 	 */
4004359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4014359Smckusick 		cg += i;
4024359Smckusick 		if (cg >= fs->fs_ncg)
4034359Smckusick 			cg -= fs->fs_ncg;
4045965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4054359Smckusick 		if (result)
4064359Smckusick 			return (result);
4074359Smckusick 	}
4084359Smckusick 	/*
4094359Smckusick 	 * 3: brute force search
41010847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
41110847Ssam 	 * and 1 is always checked in the quadratic rehash.
4124359Smckusick 	 */
41310848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
41410847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4155965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4164359Smckusick 		if (result)
4174359Smckusick 			return (result);
4184359Smckusick 		cg++;
4194359Smckusick 		if (cg == fs->fs_ncg)
4204359Smckusick 			cg = 0;
4214359Smckusick 	}
4226294Smckusick 	return (NULL);
4234359Smckusick }
4244359Smckusick 
4255375Smckusic /*
4265375Smckusic  * Determine whether a fragment can be extended.
4275375Smckusic  *
4285375Smckusic  * Check to see if the necessary fragments are available, and
4295375Smckusic  * if they are, allocate them.
4305375Smckusic  */
4314359Smckusick daddr_t
4325965Smckusic fragextend(ip, cg, bprev, osize, nsize)
4335965Smckusic 	struct inode *ip;
4344426Smckusic 	int cg;
4354463Smckusic 	long bprev;
4364426Smckusic 	int osize, nsize;
4374426Smckusic {
4385965Smckusic 	register struct fs *fs;
4394463Smckusic 	register struct buf *bp;
4404463Smckusic 	register struct cg *cgp;
4414463Smckusic 	long bno;
4424463Smckusic 	int frags, bbase;
4434426Smckusic 	int i;
4444426Smckusic 
4455965Smckusic 	fs = ip->i_fs;
44617224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
4476531Smckusick 		return (NULL);
4485960Smckusic 	frags = numfrags(fs, nsize);
44917224Smckusick 	bbase = fragnum(fs, bprev);
45017224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
4514463Smckusic 		/* cannot extend across a block boundry */
4526294Smckusick 		return (NULL);
4534463Smckusic 	}
45410278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
4556531Smckusick 	cgp = bp->b_un.b_cg;
4566531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
4575960Smckusic 		brelse(bp);
4586294Smckusick 		return (NULL);
4595960Smckusic 	}
4608105Sroot 	cgp->cg_time = time.tv_sec;
4615377Smckusic 	bno = dtogd(fs, bprev);
4625960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
4635361Smckusic 		if (isclr(cgp->cg_free, bno + i)) {
4645361Smckusic 			brelse(bp);
4656294Smckusick 			return (NULL);
4665361Smckusic 		}
4675361Smckusic 	/*
4685361Smckusic 	 * the current fragment can be extended
4695361Smckusic 	 * deduct the count on fragment being extended into
4705361Smckusic 	 * increase the count on the remaining fragment (if any)
4715361Smckusic 	 * allocate the extended piece
4725361Smckusic 	 */
4735361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
4744463Smckusic 		if (isclr(cgp->cg_free, bno + i))
4754463Smckusic 			break;
4765960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
4775361Smckusic 	if (i != frags)
4785361Smckusic 		cgp->cg_frsum[i - frags]++;
4795960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
4805361Smckusic 		clrbit(cgp->cg_free, bno + i);
4815361Smckusic 		cgp->cg_cs.cs_nffree--;
4825361Smckusic 		fs->fs_cstotal.cs_nffree--;
4835361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
4844463Smckusic 	}
4855361Smckusic 	fs->fs_fmod++;
4865361Smckusic 	bdwrite(bp);
4875361Smckusic 	return (bprev);
4884426Smckusic }
4894426Smckusic 
4905375Smckusic /*
4915375Smckusic  * Determine whether a block can be allocated.
4925375Smckusic  *
4935375Smckusic  * Check to see if a block of the apprpriate size is available,
4945375Smckusic  * and if it is, allocate it.
4955375Smckusic  */
4969163Ssam daddr_t
4975965Smckusic alloccg(ip, cg, bpref, size)
4985965Smckusic 	struct inode *ip;
4994359Smckusick 	int cg;
5004359Smckusick 	daddr_t bpref;
5014359Smckusick 	int size;
5024359Smckusick {
5035965Smckusic 	register struct fs *fs;
5044463Smckusic 	register struct buf *bp;
5054463Smckusic 	register struct cg *cgp;
5064463Smckusic 	int bno, frags;
5074463Smckusic 	int allocsiz;
5084463Smckusic 	register int i;
5094359Smckusick 
5105965Smckusic 	fs = ip->i_fs;
5115322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5126294Smckusick 		return (NULL);
51310278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
5146531Smckusick 	cgp = bp->b_un.b_cg;
51515950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
51615950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5175960Smckusic 		brelse(bp);
5186294Smckusick 		return (NULL);
5195960Smckusic 	}
5208105Sroot 	cgp->cg_time = time.tv_sec;
5215322Smckusic 	if (size == fs->fs_bsize) {
5225212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5234463Smckusic 		bdwrite(bp);
5244463Smckusic 		return (bno);
5254463Smckusic 	}
5264463Smckusic 	/*
5274463Smckusic 	 * check to see if any fragments are already available
5284463Smckusic 	 * allocsiz is the size which will be allocated, hacking
5294463Smckusic 	 * it down to a smaller size if necessary
5304463Smckusic 	 */
5315960Smckusic 	frags = numfrags(fs, size);
5325322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
5334463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
5344463Smckusic 			break;
5355322Smckusic 	if (allocsiz == fs->fs_frag) {
5364463Smckusic 		/*
5374463Smckusic 		 * no fragments were available, so a block will be
5384463Smckusic 		 * allocated, and hacked up
5394463Smckusic 		 */
5404792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
5414463Smckusic 			brelse(bp);
5426294Smckusick 			return (NULL);
5434463Smckusic 		}
5445212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5455377Smckusic 		bpref = dtogd(fs, bno);
5465322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
5474463Smckusic 			setbit(cgp->cg_free, bpref + i);
5485322Smckusic 		i = fs->fs_frag - frags;
5494792Smckusic 		cgp->cg_cs.cs_nffree += i;
5504792Smckusic 		fs->fs_cstotal.cs_nffree += i;
5515322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
5529762Ssam 		fs->fs_fmod++;
5534463Smckusic 		cgp->cg_frsum[i]++;
5544463Smckusic 		bdwrite(bp);
5554463Smckusic 		return (bno);
5564463Smckusic 	}
5574651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
55815950Smckusick 	if (bno < 0) {
55915950Smckusick 		brelse(bp);
5606294Smckusick 		return (NULL);
56115950Smckusick 	}
5624463Smckusic 	for (i = 0; i < frags; i++)
5634463Smckusic 		clrbit(cgp->cg_free, bno + i);
5644792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
5654792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
5665322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
5679762Ssam 	fs->fs_fmod++;
5684463Smckusic 	cgp->cg_frsum[allocsiz]--;
5694463Smckusic 	if (frags != allocsiz)
5704463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
5714463Smckusic 	bdwrite(bp);
5724463Smckusic 	return (cg * fs->fs_fpg + bno);
5734463Smckusic }
5744463Smckusic 
5755375Smckusic /*
5765375Smckusic  * Allocate a block in a cylinder group.
5775375Smckusic  *
5785375Smckusic  * This algorithm implements the following policy:
5795375Smckusic  *   1) allocate the requested block.
5805375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
5815375Smckusic  *   3) allocate the next available block on the block rotor for the
5825375Smckusic  *      specified cylinder group.
5835375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
5845375Smckusic  * blocks may be fragmented by the routine that allocates them.
5855375Smckusic  */
5864463Smckusic daddr_t
5875212Smckusic alloccgblk(fs, cgp, bpref)
5885965Smckusic 	register struct fs *fs;
5894463Smckusic 	register struct cg *cgp;
5904463Smckusic 	daddr_t bpref;
5914463Smckusic {
5924651Smckusic 	daddr_t bno;
5936294Smckusick 	int cylno, pos, delta;
5944651Smckusic 	short *cylbp;
5955361Smckusic 	register int i;
5964463Smckusic 
5974651Smckusic 	if (bpref == 0) {
5984651Smckusic 		bpref = cgp->cg_rotor;
5995361Smckusic 		goto norot;
6005361Smckusic 	}
60117224Smckusick 	bpref = blknum(fs, bpref);
6025377Smckusic 	bpref = dtogd(fs, bpref);
6035361Smckusic 	/*
6045361Smckusic 	 * if the requested block is available, use it
6055361Smckusic 	 */
60611638Ssam 	if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) {
6075361Smckusic 		bno = bpref;
6085361Smckusic 		goto gotit;
6095361Smckusic 	}
6105361Smckusic 	/*
6115361Smckusic 	 * check for a block available on the same cylinder
6125361Smckusic 	 */
6135361Smckusic 	cylno = cbtocylno(fs, bpref);
6145375Smckusic 	if (cgp->cg_btot[cylno] == 0)
6155375Smckusic 		goto norot;
6165375Smckusic 	if (fs->fs_cpc == 0) {
6175375Smckusic 		/*
6185375Smckusic 		 * block layout info is not available, so just have
6195375Smckusic 		 * to take any block in this cylinder.
6205375Smckusic 		 */
6215375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
6225375Smckusic 		goto norot;
6235375Smckusic 	}
6245375Smckusic 	/*
6255361Smckusic 	 * check the summary information to see if a block is
6265361Smckusic 	 * available in the requested cylinder starting at the
6279163Ssam 	 * requested rotational position and proceeding around.
6285361Smckusic 	 */
6299163Ssam 	cylbp = cgp->cg_b[cylno];
6309163Ssam 	pos = cbtorpos(fs, bpref);
6315361Smckusic 	for (i = pos; i < NRPOS; i++)
6325361Smckusic 		if (cylbp[i] > 0)
6335361Smckusic 			break;
6345361Smckusic 	if (i == NRPOS)
6355361Smckusic 		for (i = 0; i < pos; i++)
6365361Smckusic 			if (cylbp[i] > 0)
6375361Smckusic 				break;
6385361Smckusic 	if (cylbp[i] > 0) {
6394651Smckusic 		/*
6405361Smckusic 		 * found a rotational position, now find the actual
6415361Smckusic 		 * block. A panic if none is actually there.
6424651Smckusic 		 */
6435361Smckusic 		pos = cylno % fs->fs_cpc;
6445361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
6456716Smckusick 		if (fs->fs_postbl[pos][i] == -1) {
6466716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
6476716Smckusick 			    pos, i, fs->fs_fsmnt);
6485361Smckusic 			panic("alloccgblk: cyl groups corrupted");
6496716Smckusick 		}
6506294Smckusick 		for (i = fs->fs_postbl[pos][i];; ) {
6515361Smckusic 			if (isblock(fs, cgp->cg_free, bno + i)) {
65211638Ssam 				bno = blkstofrags(fs, (bno + i));
6535361Smckusic 				goto gotit;
6545361Smckusic 			}
6556294Smckusick 			delta = fs->fs_rotbl[i];
6566294Smckusick 			if (delta <= 0 || delta > MAXBPC - i)
6574651Smckusic 				break;
6586294Smckusick 			i += delta;
6594651Smckusic 		}
6606716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
6615361Smckusic 		panic("alloccgblk: can't find blk in cyl");
6624359Smckusick 	}
6635361Smckusic norot:
6645361Smckusic 	/*
6655361Smckusic 	 * no blocks in the requested cylinder, so take next
6665361Smckusic 	 * available one in this cylinder group.
6675361Smckusic 	 */
6688628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
6696567Smckusic 	if (bno < 0)
6706294Smckusick 		return (NULL);
6714651Smckusic 	cgp->cg_rotor = bno;
6724359Smckusick gotit:
67311638Ssam 	clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno));
6744792Smckusic 	cgp->cg_cs.cs_nbfree--;
6754792Smckusic 	fs->fs_cstotal.cs_nbfree--;
6765322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
6775375Smckusic 	cylno = cbtocylno(fs, bno);
6785375Smckusic 	cgp->cg_b[cylno][cbtorpos(fs, bno)]--;
6795375Smckusic 	cgp->cg_btot[cylno]--;
6804359Smckusick 	fs->fs_fmod++;
6814651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
6824359Smckusick }
6834359Smckusick 
6845375Smckusic /*
6855375Smckusic  * Determine whether an inode can be allocated.
6865375Smckusic  *
6875375Smckusic  * Check to see if an inode is available, and if it is,
6885375Smckusic  * allocate it using the following policy:
6895375Smckusic  *   1) allocate the requested inode.
6905375Smckusic  *   2) allocate the next available inode after the requested
6915375Smckusic  *      inode in the specified cylinder group.
6925375Smckusic  */
6939163Ssam ino_t
6945965Smckusic ialloccg(ip, cg, ipref, mode)
6955965Smckusic 	struct inode *ip;
6964359Smckusick 	int cg;
6974359Smckusick 	daddr_t ipref;
6984359Smckusick 	int mode;
6994359Smckusick {
7005965Smckusic 	register struct fs *fs;
7014463Smckusic 	register struct cg *cgp;
70216784Smckusick 	struct buf *bp;
70316784Smckusick 	int start, len, loc, map, i;
7044359Smckusick 
7055965Smckusic 	fs = ip->i_fs;
7065322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7076294Smckusick 		return (NULL);
70810278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
7096531Smckusick 	cgp = bp->b_un.b_cg;
71015950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
71115950Smckusick 	    cgp->cg_cs.cs_nifree == 0) {
7125960Smckusic 		brelse(bp);
7136294Smckusick 		return (NULL);
7145960Smckusic 	}
7158105Sroot 	cgp->cg_time = time.tv_sec;
7164359Smckusick 	if (ipref) {
7174359Smckusick 		ipref %= fs->fs_ipg;
7184359Smckusick 		if (isclr(cgp->cg_iused, ipref))
7194359Smckusick 			goto gotit;
72016784Smckusick 	}
72116784Smckusick 	start = cgp->cg_irotor / NBBY;
72216784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
72316784Smckusick 	loc = skpc(0xff, len, &cgp->cg_iused[start]);
72416784Smckusick 	if (loc == 0) {
72517697Smckusick 		len = start + 1;
72617697Smckusick 		start = 0;
72717697Smckusick 		loc = skpc(0xff, len, &cgp->cg_iused[0]);
72817697Smckusick 		if (loc == 0) {
72917697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
73017697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
73117697Smckusick 			panic("ialloccg: map corrupted");
73217697Smckusick 			/* NOTREACHED */
73317697Smckusick 		}
73416784Smckusick 	}
73516784Smckusick 	i = start + len - loc;
73616784Smckusick 	map = cgp->cg_iused[i];
73716784Smckusick 	ipref = i * NBBY;
73816784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
73916784Smckusick 		if ((map & i) == 0) {
7404359Smckusick 			cgp->cg_irotor = ipref;
7414359Smckusick 			goto gotit;
7424359Smckusick 		}
7434359Smckusick 	}
74416784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
74516784Smckusick 	panic("ialloccg: block not in map");
74616784Smckusick 	/* NOTREACHED */
7474359Smckusick gotit:
7484359Smckusick 	setbit(cgp->cg_iused, ipref);
7494792Smckusic 	cgp->cg_cs.cs_nifree--;
7504792Smckusic 	fs->fs_cstotal.cs_nifree--;
7515322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
7524359Smckusick 	fs->fs_fmod++;
7534359Smckusick 	if ((mode & IFMT) == IFDIR) {
7544792Smckusic 		cgp->cg_cs.cs_ndir++;
7554792Smckusic 		fs->fs_cstotal.cs_ndir++;
7565322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
7574359Smckusick 	}
7584359Smckusick 	bdwrite(bp);
7594359Smckusick 	return (cg * fs->fs_ipg + ipref);
7604359Smckusick }
7614359Smckusick 
7625375Smckusic /*
7635375Smckusic  * Free a block or fragment.
7645375Smckusic  *
7655375Smckusic  * The specified block or fragment is placed back in the
7665375Smckusic  * free map. If a fragment is deallocated, a possible
7675375Smckusic  * block reassembly is checked.
7685375Smckusic  */
7699163Ssam free(ip, bno, size)
7705965Smckusic 	register struct inode *ip;
7714359Smckusick 	daddr_t bno;
7725212Smckusic 	off_t size;
7734359Smckusick {
7744359Smckusick 	register struct fs *fs;
7754359Smckusick 	register struct cg *cgp;
7764359Smckusick 	register struct buf *bp;
7774463Smckusic 	int cg, blk, frags, bbase;
7784463Smckusic 	register int i;
7794359Smckusick 
7805965Smckusic 	fs = ip->i_fs;
7816716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
7826716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
7836716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7844426Smckusic 		panic("free: bad size");
7856716Smckusick 	}
7865377Smckusic 	cg = dtog(fs, bno);
7876567Smckusic 	if (badblock(fs, bno)) {
7886567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
7894359Smckusick 		return;
7906567Smckusic 	}
79110278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
7926531Smckusick 	cgp = bp->b_un.b_cg;
7936531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
7945960Smckusic 		brelse(bp);
7954359Smckusick 		return;
7965960Smckusic 	}
7978105Sroot 	cgp->cg_time = time.tv_sec;
7985377Smckusic 	bno = dtogd(fs, bno);
7995322Smckusic 	if (size == fs->fs_bsize) {
80011638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) {
8016716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
8026716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
8034426Smckusic 			panic("free: freeing free block");
8046567Smckusic 		}
80511638Ssam 		setblock(fs, cgp->cg_free, fragstoblks(fs, bno));
8064792Smckusic 		cgp->cg_cs.cs_nbfree++;
8074792Smckusic 		fs->fs_cstotal.cs_nbfree++;
8085322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
8095375Smckusic 		i = cbtocylno(fs, bno);
8105375Smckusic 		cgp->cg_b[i][cbtorpos(fs, bno)]++;
8115375Smckusic 		cgp->cg_btot[i]++;
8124426Smckusic 	} else {
81317224Smckusick 		bbase = bno - fragnum(fs, bno);
8144463Smckusic 		/*
8154463Smckusic 		 * decrement the counts associated with the old frags
8164463Smckusic 		 */
8176294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
8185322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
8194463Smckusic 		/*
8204463Smckusic 		 * deallocate the fragment
8214463Smckusic 		 */
8225960Smckusic 		frags = numfrags(fs, size);
8234463Smckusic 		for (i = 0; i < frags; i++) {
8246716Smckusick 			if (isset(cgp->cg_free, bno + i)) {
8256716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
8266716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
8274426Smckusic 				panic("free: freeing free frag");
8286716Smckusick 			}
8294426Smckusic 			setbit(cgp->cg_free, bno + i);
8304426Smckusic 		}
8316294Smckusick 		cgp->cg_cs.cs_nffree += i;
8326294Smckusick 		fs->fs_cstotal.cs_nffree += i;
8336294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
8344463Smckusic 		/*
8354463Smckusic 		 * add back in counts associated with the new frags
8364463Smckusic 		 */
8376294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
8385322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
8394463Smckusic 		/*
8404463Smckusic 		 * if a complete block has been reassembled, account for it
8414463Smckusic 		 */
84211638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) {
8435322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
8445322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
8455322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
8464792Smckusic 			cgp->cg_cs.cs_nbfree++;
8474792Smckusic 			fs->fs_cstotal.cs_nbfree++;
8485322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
8495375Smckusic 			i = cbtocylno(fs, bbase);
8505375Smckusic 			cgp->cg_b[i][cbtorpos(fs, bbase)]++;
8515375Smckusic 			cgp->cg_btot[i]++;
8524426Smckusic 		}
8534426Smckusic 	}
8544359Smckusick 	fs->fs_fmod++;
8554359Smckusick 	bdwrite(bp);
8564359Smckusick }
8574359Smckusick 
8585375Smckusic /*
8595375Smckusic  * Free an inode.
8605375Smckusic  *
8615375Smckusic  * The specified inode is placed back in the free map.
8625375Smckusic  */
8635965Smckusic ifree(ip, ino, mode)
8645965Smckusic 	struct inode *ip;
8654359Smckusick 	ino_t ino;
8664359Smckusick 	int mode;
8674359Smckusick {
8684359Smckusick 	register struct fs *fs;
8694359Smckusick 	register struct cg *cgp;
8704359Smckusick 	register struct buf *bp;
8714359Smckusick 	int cg;
8724359Smckusick 
8735965Smckusic 	fs = ip->i_fs;
8746716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
8756716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
8766716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
8774359Smckusick 		panic("ifree: range");
8786716Smckusick 	}
8795377Smckusic 	cg = itog(fs, ino);
88010278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
8816531Smckusick 	cgp = bp->b_un.b_cg;
8826531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
8835960Smckusic 		brelse(bp);
8844359Smckusick 		return;
8855960Smckusic 	}
8868105Sroot 	cgp->cg_time = time.tv_sec;
8874359Smckusick 	ino %= fs->fs_ipg;
8886716Smckusick 	if (isclr(cgp->cg_iused, ino)) {
8896716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
8906716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
8914359Smckusick 		panic("ifree: freeing free inode");
8926716Smckusick 	}
8934359Smckusick 	clrbit(cgp->cg_iused, ino);
89416784Smckusick 	if (ino < cgp->cg_irotor)
89516784Smckusick 		cgp->cg_irotor = ino;
8964792Smckusic 	cgp->cg_cs.cs_nifree++;
8974792Smckusic 	fs->fs_cstotal.cs_nifree++;
8985322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
8994359Smckusick 	if ((mode & IFMT) == IFDIR) {
9004792Smckusic 		cgp->cg_cs.cs_ndir--;
9014792Smckusic 		fs->fs_cstotal.cs_ndir--;
9025322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
9034359Smckusick 	}
9044359Smckusick 	fs->fs_fmod++;
9054359Smckusick 	bdwrite(bp);
9064359Smckusick }
9074359Smckusick 
9084463Smckusic /*
9095375Smckusic  * Find a block of the specified size in the specified cylinder group.
9105375Smckusic  *
9114651Smckusic  * It is a panic if a request is made to find a block if none are
9124651Smckusic  * available.
9134651Smckusic  */
9144651Smckusic daddr_t
9154651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
9164651Smckusic 	register struct fs *fs;
9174651Smckusic 	register struct cg *cgp;
9184651Smckusic 	daddr_t bpref;
9194651Smckusic 	int allocsiz;
9204651Smckusic {
9214651Smckusic 	daddr_t bno;
9224651Smckusic 	int start, len, loc, i;
9234651Smckusic 	int blk, field, subfield, pos;
9244651Smckusic 
9254651Smckusic 	/*
9264651Smckusic 	 * find the fragment by searching through the free block
9274651Smckusic 	 * map for an appropriate bit pattern
9284651Smckusic 	 */
9294651Smckusic 	if (bpref)
9305377Smckusic 		start = dtogd(fs, bpref) / NBBY;
9314651Smckusic 	else
9324651Smckusic 		start = cgp->cg_frotor / NBBY;
9335398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
93412755Ssam 	loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start],
93512755Ssam 		(caddr_t)fragtbl[fs->fs_frag],
93612755Ssam 		(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
9374651Smckusic 	if (loc == 0) {
9386531Smckusick 		len = start + 1;
9396531Smckusick 		start = 0;
94017697Smckusick 		loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[0],
94112755Ssam 			(caddr_t)fragtbl[fs->fs_frag],
94212755Ssam 			(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
94316784Smckusick 		if (loc == 0) {
94416784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
94516784Smckusick 			    start, len, fs->fs_fsmnt);
94616784Smckusick 			panic("alloccg: map corrupted");
94717697Smckusick 			/* NOTREACHED */
94816784Smckusick 		}
9494651Smckusic 	}
9504651Smckusic 	bno = (start + len - loc) * NBBY;
9514651Smckusic 	cgp->cg_frotor = bno;
9524651Smckusic 	/*
9534651Smckusic 	 * found the byte in the map
9544651Smckusic 	 * sift through the bits to find the selected frag
9554651Smckusic 	 */
9566294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
9576294Smckusick 		blk = blkmap(fs, cgp->cg_free, bno);
9584651Smckusic 		blk <<= 1;
9594651Smckusic 		field = around[allocsiz];
9604651Smckusic 		subfield = inside[allocsiz];
9615322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
9626294Smckusick 			if ((blk & field) == subfield)
9636294Smckusick 				return (bno + pos);
9644651Smckusic 			field <<= 1;
9654651Smckusic 			subfield <<= 1;
9664651Smckusic 		}
9674651Smckusic 	}
9686716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
9694651Smckusic 	panic("alloccg: block not in map");
9706531Smckusick 	return (-1);
9714651Smckusic }
9724651Smckusic 
9734651Smckusic /*
9745375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
9755375Smckusic  *
9765375Smckusic  * The form of the error message is:
9774359Smckusick  *	fs: error message
9784359Smckusick  */
9794359Smckusick fserr(fs, cp)
9804359Smckusick 	struct fs *fs;
9814359Smckusick 	char *cp;
9824359Smckusick {
9834359Smckusick 
984*24839Seric 	log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp);
9854359Smckusick }
986