xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 26253)
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*26253Skarels  *	@(#)lfs_alloc.c	6.19 (Berkeley) 02/19/86
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"
20*26253Skarels #include "cmap.h"
214359Smckusick 
225212Smckusic extern u_long		hashalloc();
239163Ssam extern ino_t		ialloccg();
249163Ssam extern daddr_t		alloccg();
254651Smckusic extern daddr_t		alloccgblk();
264651Smckusic extern daddr_t		fragextend();
274651Smckusic extern daddr_t		blkpref();
284651Smckusic extern daddr_t		mapsearch();
294607Smckusic extern int		inside[], around[];
305322Smckusic extern unsigned char	*fragtbl[];
314359Smckusick 
325375Smckusic /*
335375Smckusic  * Allocate a block in the file system.
345375Smckusic  *
355375Smckusic  * The size of the requested block is given, which must be some
365375Smckusic  * multiple of fs_fsize and <= fs_bsize.
375375Smckusic  * A preference may be optionally specified. If a preference is given
385375Smckusic  * the following hierarchy is used to allocate a block:
395375Smckusic  *   1) allocate the requested block.
405375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
415375Smckusic  *   3) allocate a block in the same cylinder group.
425375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
435375Smckusic  *      available block is located.
445375Smckusic  * If no block preference is given the following heirarchy is used
455375Smckusic  * to allocate a block:
465375Smckusic  *   1) allocate a block in the cylinder group that contains the
475375Smckusic  *      inode for the file.
485375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
495375Smckusic  *      available block is located.
505375Smckusic  */
514359Smckusick struct buf *
525965Smckusic alloc(ip, bpref, size)
534463Smckusic 	register struct inode *ip;
544359Smckusick 	daddr_t bpref;
554359Smckusick 	int size;
564359Smckusick {
574359Smckusick 	daddr_t bno;
584359Smckusick 	register struct fs *fs;
594463Smckusic 	register struct buf *bp;
604359Smckusick 	int cg;
614359Smckusick 
625965Smckusic 	fs = ip->i_fs;
636716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
646716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
656716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
664463Smckusic 		panic("alloc: bad size");
676716Smckusick 	}
685322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
694359Smckusick 		goto nospace;
7011638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
714792Smckusic 		goto nospace;
727650Ssam #ifdef QUOTA
7312643Ssam 	u.u_error = chkdq(ip, (long)btodb(size), 0);
7412643Ssam 	if (u.u_error)
7512643Ssam 		return (NULL);
767483Skre #endif
774948Smckusic 	if (bpref >= fs->fs_size)
784948Smckusic 		bpref = 0;
794359Smckusick 	if (bpref == 0)
805377Smckusic 		cg = itog(fs, ip->i_number);
814359Smckusick 	else
825377Smckusic 		cg = dtog(fs, bpref);
839163Ssam 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
849163Ssam 		(u_long (*)())alloccg);
856567Smckusic 	if (bno <= 0)
864359Smckusick 		goto nospace;
8712643Ssam 	ip->i_blocks += btodb(size);
8812643Ssam 	ip->i_flag |= IUPD|ICHG;
895965Smckusic 	bp = getblk(ip->i_dev, fsbtodb(fs, bno), size);
904359Smckusick 	clrbuf(bp);
914359Smckusick 	return (bp);
924359Smckusick nospace:
934359Smckusick 	fserr(fs, "file system full");
944359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
954359Smckusick 	u.u_error = ENOSPC;
964359Smckusick 	return (NULL);
974359Smckusick }
984359Smckusick 
995375Smckusic /*
1005375Smckusic  * Reallocate a fragment to a bigger size
1015375Smckusic  *
1025375Smckusic  * The number and size of the old block is given, and a preference
1035375Smckusic  * and new size is also specified. The allocator attempts to extend
1045375Smckusic  * the original block. Failing that, the regular block allocator is
1055375Smckusic  * invoked to get an appropriate block.
1065375Smckusic  */
1074426Smckusic struct buf *
1085965Smckusic realloccg(ip, bprev, bpref, osize, nsize)
1095965Smckusic 	register struct inode *ip;
1104651Smckusic 	daddr_t bprev, bpref;
1114426Smckusic 	int osize, nsize;
1124426Smckusic {
1134426Smckusic 	register struct fs *fs;
1144463Smckusic 	register struct buf *bp, *obp;
11524698Smckusick 	int cg, request;
11625471Smckusick 	daddr_t bno, bn;
11725471Smckusick 	int i, count, s;
11825471Smckusick 	extern struct cmap *mfind();
1194426Smckusic 
1205965Smckusic 	fs = ip->i_fs;
1215960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1226716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1236716Smckusick 		printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1246716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
1254463Smckusic 		panic("realloccg: bad size");
1266716Smckusick 	}
12711638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1284792Smckusic 		goto nospace;
1296716Smckusick 	if (bprev == 0) {
1306716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1316716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1324463Smckusic 		panic("realloccg: bad bprev");
1336716Smckusick 	}
1347650Ssam #ifdef QUOTA
13512643Ssam 	u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0);
13612643Ssam 	if (u.u_error)
13712643Ssam 		return (NULL);
1387483Skre #endif
1396294Smckusick 	cg = dtog(fs, bprev);
1405965Smckusic 	bno = fragextend(ip, cg, (long)bprev, osize, nsize);
1414463Smckusic 	if (bno != 0) {
1427187Sroot 		do {
1437187Sroot 			bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
1447187Sroot 			if (bp->b_flags & B_ERROR) {
1457187Sroot 				brelse(bp);
1467187Sroot 				return (NULL);
1477187Sroot 			}
1487187Sroot 		} while (brealloc(bp, nsize) == 0);
1497187Sroot 		bp->b_flags |= B_DONE;
1509163Ssam 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
15112643Ssam 		ip->i_blocks += btodb(nsize - osize);
15212643Ssam 		ip->i_flag |= IUPD|ICHG;
1534463Smckusic 		return (bp);
1544463Smckusic 	}
1554948Smckusic 	if (bpref >= fs->fs_size)
1564948Smckusic 		bpref = 0;
157*26253Skarels 	switch ((int)fs->fs_optim) {
15825256Smckusick 	case FS_OPTSPACE:
15925256Smckusick 		/*
16025256Smckusick 		 * Allocate an exact sized fragment. Although this makes
16125256Smckusick 		 * best use of space, we will waste time relocating it if
16225256Smckusick 		 * the file continues to grow. If the fragmentation is
16325256Smckusick 		 * less than half of the minimum free reserve, we choose
16425256Smckusick 		 * to begin optimizing for time.
16525256Smckusick 		 */
16624698Smckusick 		request = nsize;
16725256Smckusick 		if (fs->fs_minfree < 5 ||
16825256Smckusick 		    fs->fs_cstotal.cs_nffree >
16925256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
17025256Smckusick 			break;
17125256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
17225256Smckusick 			fs->fs_fsmnt);
17325256Smckusick 		fs->fs_optim = FS_OPTTIME;
17425256Smckusick 		break;
17525256Smckusick 	case FS_OPTTIME:
17625256Smckusick 		/*
17725256Smckusick 		 * At this point we have discovered a file that is trying
17825256Smckusick 		 * to grow a small fragment to a larger fragment. To save
17925256Smckusick 		 * time, we allocate a full sized block, then free the
18025256Smckusick 		 * unused portion. If the file continues to grow, the
18125256Smckusick 		 * `fragextend' call above will be able to grow it in place
18225256Smckusick 		 * without further copying. If aberrant programs cause
18325256Smckusick 		 * disk fragmentation to grow within 2% of the free reserve,
18425256Smckusick 		 * we choose to begin optimizing for space.
18525256Smckusick 		 */
18624698Smckusick 		request = fs->fs_bsize;
18725256Smckusick 		if (fs->fs_cstotal.cs_nffree <
18825256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
18925256Smckusick 			break;
19025256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
19125256Smckusick 			fs->fs_fsmnt);
19225256Smckusick 		fs->fs_optim = FS_OPTSPACE;
19325256Smckusick 		break;
19425256Smckusick 	default:
19525256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
19625256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
19725256Smckusick 		panic("realloccg: bad optim");
19825256Smckusick 		/* NOTREACHED */
19925256Smckusick 	}
20024698Smckusick 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
2019163Ssam 		(u_long (*)())alloccg);
2026567Smckusic 	if (bno > 0) {
2035965Smckusic 		obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
2045960Smckusic 		if (obp->b_flags & B_ERROR) {
2055960Smckusic 			brelse(obp);
2066294Smckusick 			return (NULL);
2075960Smckusic 		}
20825471Smckusick 		bn = fsbtodb(fs, bno);
20925471Smckusick 		bp = getblk(ip->i_dev, bn, nsize);
21017658Smckusick 		bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
21125471Smckusick 		count = howmany(osize, DEV_BSIZE);
21225471Smckusick 		s = splimp();
21325471Smckusick 		for (i = 0; i < count; i += CLBYTES / DEV_BSIZE)
21425471Smckusick 			if (mfind(ip->i_dev, bn + i))
21525471Smckusick 				munhash(ip->i_dev, bn + i);
21625471Smckusick 		splx(s);
21717658Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
21817658Smckusick 		if (obp->b_flags & B_DELWRI) {
21917658Smckusick 			obp->b_flags &= ~B_DELWRI;
22017658Smckusick 			u.u_ru.ru_oublock--;		/* delete charge */
22117658Smckusick 		}
2224463Smckusic 		brelse(obp);
2239163Ssam 		free(ip, bprev, (off_t)osize);
22424698Smckusick 		if (nsize < request)
22517709Smckusick 			free(ip, bno + numfrags(fs, nsize),
22624698Smckusick 				(off_t)(request - nsize));
22712643Ssam 		ip->i_blocks += btodb(nsize - osize);
22812643Ssam 		ip->i_flag |= IUPD|ICHG;
2296294Smckusick 		return (bp);
2304463Smckusic 	}
2314792Smckusic nospace:
2324463Smckusic 	/*
2334463Smckusic 	 * no space available
2344463Smckusic 	 */
2354426Smckusic 	fserr(fs, "file system full");
2364426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
2374426Smckusic 	u.u_error = ENOSPC;
2384426Smckusic 	return (NULL);
2394426Smckusic }
2404426Smckusic 
2415375Smckusic /*
2425375Smckusic  * Allocate an inode in the file system.
2435375Smckusic  *
2445375Smckusic  * A preference may be optionally specified. If a preference is given
2455375Smckusic  * the following hierarchy is used to allocate an inode:
2465375Smckusic  *   1) allocate the requested inode.
2475375Smckusic  *   2) allocate an inode in the same cylinder group.
2485375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2495375Smckusic  *      available inode is located.
2505375Smckusic  * If no inode preference is given the following heirarchy is used
2515375Smckusic  * to allocate an inode:
2525375Smckusic  *   1) allocate an inode in cylinder group 0.
2535375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2545375Smckusic  *      available inode is located.
2555375Smckusic  */
2564359Smckusick struct inode *
2575965Smckusic ialloc(pip, ipref, mode)
2585965Smckusic 	register struct inode *pip;
2594359Smckusick 	ino_t ipref;
2604359Smckusick 	int mode;
2614359Smckusick {
2625212Smckusic 	ino_t ino;
2634359Smckusick 	register struct fs *fs;
2644359Smckusick 	register struct inode *ip;
2654359Smckusick 	int cg;
2664359Smckusick 
2675965Smckusic 	fs = pip->i_fs;
2684792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2694359Smckusick 		goto noinodes;
2707650Ssam #ifdef QUOTA
27112643Ssam 	u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
27212643Ssam 	if (u.u_error)
27312643Ssam 		return (NULL);
2747483Skre #endif
2754948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2764948Smckusic 		ipref = 0;
2775377Smckusic 	cg = itog(fs, ipref);
2785965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2794359Smckusick 	if (ino == 0)
2804359Smckusick 		goto noinodes;
2815965Smckusic 	ip = iget(pip->i_dev, pip->i_fs, ino);
2824359Smckusick 	if (ip == NULL) {
28315120Skarels 		ifree(pip, ino, 0);
2844359Smckusick 		return (NULL);
2854359Smckusick 	}
2866716Smckusick 	if (ip->i_mode) {
2876716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
2886716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2894359Smckusick 		panic("ialloc: dup alloc");
2906716Smckusick 	}
29112643Ssam 	if (ip->i_blocks) {				/* XXX */
29212643Ssam 		printf("free inode %s/%d had %d blocks\n",
29312643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
29412643Ssam 		ip->i_blocks = 0;
29512643Ssam 	}
2964359Smckusick 	return (ip);
2974359Smckusick noinodes:
2984359Smckusick 	fserr(fs, "out of inodes");
2996294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
3004359Smckusick 	u.u_error = ENOSPC;
3014359Smckusick 	return (NULL);
3024359Smckusick }
3034359Smckusick 
3044651Smckusic /*
3055375Smckusic  * Find a cylinder to place a directory.
3065375Smckusic  *
3075375Smckusic  * The policy implemented by this algorithm is to select from
3085375Smckusic  * among those cylinder groups with above the average number of
3095375Smckusic  * free inodes, the one with the smallest number of directories.
3104651Smckusic  */
3119163Ssam ino_t
3125965Smckusic dirpref(fs)
3135965Smckusic 	register struct fs *fs;
3144359Smckusick {
3154651Smckusic 	int cg, minndir, mincg, avgifree;
3164359Smckusick 
3174792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3184651Smckusic 	minndir = fs->fs_ipg;
3194359Smckusick 	mincg = 0;
3204651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3215322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3225322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3234359Smckusick 			mincg = cg;
3245322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3254359Smckusick 		}
3269163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3274359Smckusick }
3284359Smckusick 
3294651Smckusic /*
3309163Ssam  * Select the desired position for the next block in a file.  The file is
3319163Ssam  * logically divided into sections. The first section is composed of the
3329163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3339163Ssam  *
3349163Ssam  * If no blocks have been allocated in the first section, the policy is to
3359163Ssam  * request a block in the same cylinder group as the inode that describes
3369163Ssam  * the file. If no blocks have been allocated in any other section, the
3379163Ssam  * policy is to place the section in a cylinder group with a greater than
3389163Ssam  * average number of free blocks.  An appropriate cylinder group is found
33917696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
34017696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
34117696Smckusick  * cylinder group from which the previous allocation was made. The sweep
34217696Smckusick  * continues until a cylinder group with greater than the average number
34317696Smckusick  * of free blocks is found. If the allocation is for the first block in an
34417696Smckusick  * indirect block, the information on the previous allocation is unavailable;
34517696Smckusick  * here a best guess is made based upon the logical block number being
34617696Smckusick  * allocated.
3479163Ssam  *
3489163Ssam  * If a section is already partially allocated, the policy is to
3499163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3509163Ssam  * contiguous blocks and the beginning of the next is physically separated
3519163Ssam  * so that the disk head will be in transit between them for at least
3529163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3539163Ssam  * schedule another I/O transfer.
3544651Smckusic  */
3555212Smckusic daddr_t
3569163Ssam blkpref(ip, lbn, indx, bap)
3579163Ssam 	struct inode *ip;
3589163Ssam 	daddr_t lbn;
3599163Ssam 	int indx;
3609163Ssam 	daddr_t *bap;
3619163Ssam {
3625965Smckusic 	register struct fs *fs;
36317696Smckusick 	register int cg;
36417696Smckusick 	int avgbfree, startcg;
3659163Ssam 	daddr_t nextblk;
3664651Smckusic 
3679163Ssam 	fs = ip->i_fs;
3689163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3699163Ssam 		if (lbn < NDADDR) {
3709163Ssam 			cg = itog(fs, ip->i_number);
3715322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3724651Smckusic 		}
3739163Ssam 		/*
3749163Ssam 		 * Find a cylinder with greater than average number of
3759163Ssam 		 * unused data blocks.
3769163Ssam 		 */
37717696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
37817696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
37917696Smckusick 		else
38017696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
38117696Smckusick 		startcg %= fs->fs_ncg;
3829163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
38317696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
3849163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3859163Ssam 				fs->fs_cgrotor = cg;
3869163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3879163Ssam 			}
38817696Smckusick 		for (cg = 0; cg <= startcg; cg++)
3899163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3909163Ssam 				fs->fs_cgrotor = cg;
3919163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3929163Ssam 			}
3939163Ssam 		return (NULL);
3949163Ssam 	}
3959163Ssam 	/*
3969163Ssam 	 * One or more previous blocks have been laid out. If less
3979163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
3989163Ssam 	 * next block is requested contiguously, otherwise it is
3999163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
4009163Ssam 	 */
4019163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
4029163Ssam 	if (indx > fs->fs_maxcontig &&
40311638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4049163Ssam 	    != nextblk)
4059163Ssam 		return (nextblk);
4069163Ssam 	if (fs->fs_rotdelay != 0)
4079163Ssam 		/*
4089163Ssam 		 * Here we convert ms of delay to frags as:
4099163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4109163Ssam 		 *	((sect/frag) * (ms/sec))
4119163Ssam 		 * then round up to the next block.
4129163Ssam 		 */
4139163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4149163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4159163Ssam 	return (nextblk);
4164651Smckusic }
4174651Smckusic 
4185375Smckusic /*
4195375Smckusic  * Implement the cylinder overflow algorithm.
4205375Smckusic  *
4215375Smckusic  * The policy implemented by this algorithm is:
4225375Smckusic  *   1) allocate the block in its requested cylinder group.
4235375Smckusic  *   2) quadradically rehash on the cylinder group number.
4245375Smckusic  *   3) brute force search for a free block.
4255375Smckusic  */
4265212Smckusic /*VARARGS5*/
4275212Smckusic u_long
4285965Smckusic hashalloc(ip, cg, pref, size, allocator)
4295965Smckusic 	struct inode *ip;
4304359Smckusick 	int cg;
4314359Smckusick 	long pref;
4324359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4335212Smckusic 	u_long (*allocator)();
4344359Smckusick {
4355965Smckusic 	register struct fs *fs;
4364359Smckusick 	long result;
4374359Smckusick 	int i, icg = cg;
4384359Smckusick 
4395965Smckusic 	fs = ip->i_fs;
4404359Smckusick 	/*
4414359Smckusick 	 * 1: preferred cylinder group
4424359Smckusick 	 */
4435965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4444359Smckusick 	if (result)
4454359Smckusick 		return (result);
4464359Smckusick 	/*
4474359Smckusick 	 * 2: quadratic rehash
4484359Smckusick 	 */
4494359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4504359Smckusick 		cg += i;
4514359Smckusick 		if (cg >= fs->fs_ncg)
4524359Smckusick 			cg -= fs->fs_ncg;
4535965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4544359Smckusick 		if (result)
4554359Smckusick 			return (result);
4564359Smckusick 	}
4574359Smckusick 	/*
4584359Smckusick 	 * 3: brute force search
45910847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
46010847Ssam 	 * and 1 is always checked in the quadratic rehash.
4614359Smckusick 	 */
46210848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
46310847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4645965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4654359Smckusick 		if (result)
4664359Smckusick 			return (result);
4674359Smckusick 		cg++;
4684359Smckusick 		if (cg == fs->fs_ncg)
4694359Smckusick 			cg = 0;
4704359Smckusick 	}
4716294Smckusick 	return (NULL);
4724359Smckusick }
4734359Smckusick 
4745375Smckusic /*
4755375Smckusic  * Determine whether a fragment can be extended.
4765375Smckusic  *
4775375Smckusic  * Check to see if the necessary fragments are available, and
4785375Smckusic  * if they are, allocate them.
4795375Smckusic  */
4804359Smckusick daddr_t
4815965Smckusic fragextend(ip, cg, bprev, osize, nsize)
4825965Smckusic 	struct inode *ip;
4834426Smckusic 	int cg;
4844463Smckusic 	long bprev;
4854426Smckusic 	int osize, nsize;
4864426Smckusic {
4875965Smckusic 	register struct fs *fs;
4884463Smckusic 	register struct buf *bp;
4894463Smckusic 	register struct cg *cgp;
4904463Smckusic 	long bno;
4914463Smckusic 	int frags, bbase;
4924426Smckusic 	int i;
4934426Smckusic 
4945965Smckusic 	fs = ip->i_fs;
49517224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
4966531Smckusick 		return (NULL);
4975960Smckusic 	frags = numfrags(fs, nsize);
49817224Smckusick 	bbase = fragnum(fs, bprev);
49917224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
5004463Smckusic 		/* cannot extend across a block boundry */
5016294Smckusick 		return (NULL);
5024463Smckusic 	}
50310278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
5046531Smckusick 	cgp = bp->b_un.b_cg;
5056531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
5065960Smckusic 		brelse(bp);
5076294Smckusick 		return (NULL);
5085960Smckusic 	}
5098105Sroot 	cgp->cg_time = time.tv_sec;
5105377Smckusic 	bno = dtogd(fs, bprev);
5115960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
5125361Smckusic 		if (isclr(cgp->cg_free, bno + i)) {
5135361Smckusic 			brelse(bp);
5146294Smckusick 			return (NULL);
5155361Smckusic 		}
5165361Smckusic 	/*
5175361Smckusic 	 * the current fragment can be extended
5185361Smckusic 	 * deduct the count on fragment being extended into
5195361Smckusic 	 * increase the count on the remaining fragment (if any)
5205361Smckusic 	 * allocate the extended piece
5215361Smckusic 	 */
5225361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
5234463Smckusic 		if (isclr(cgp->cg_free, bno + i))
5244463Smckusic 			break;
5255960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5265361Smckusic 	if (i != frags)
5275361Smckusic 		cgp->cg_frsum[i - frags]++;
5285960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
5295361Smckusic 		clrbit(cgp->cg_free, bno + i);
5305361Smckusic 		cgp->cg_cs.cs_nffree--;
5315361Smckusic 		fs->fs_cstotal.cs_nffree--;
5325361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5334463Smckusic 	}
5345361Smckusic 	fs->fs_fmod++;
5355361Smckusic 	bdwrite(bp);
5365361Smckusic 	return (bprev);
5374426Smckusic }
5384426Smckusic 
5395375Smckusic /*
5405375Smckusic  * Determine whether a block can be allocated.
5415375Smckusic  *
5425375Smckusic  * Check to see if a block of the apprpriate size is available,
5435375Smckusic  * and if it is, allocate it.
5445375Smckusic  */
5459163Ssam daddr_t
5465965Smckusic alloccg(ip, cg, bpref, size)
5475965Smckusic 	struct inode *ip;
5484359Smckusick 	int cg;
5494359Smckusick 	daddr_t bpref;
5504359Smckusick 	int size;
5514359Smckusick {
5525965Smckusic 	register struct fs *fs;
5534463Smckusic 	register struct buf *bp;
5544463Smckusic 	register struct cg *cgp;
5554463Smckusic 	int bno, frags;
5564463Smckusic 	int allocsiz;
5574463Smckusic 	register int i;
5584359Smckusick 
5595965Smckusic 	fs = ip->i_fs;
5605322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5616294Smckusick 		return (NULL);
56210278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
5636531Smckusick 	cgp = bp->b_un.b_cg;
56415950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
56515950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5665960Smckusic 		brelse(bp);
5676294Smckusick 		return (NULL);
5685960Smckusic 	}
5698105Sroot 	cgp->cg_time = time.tv_sec;
5705322Smckusic 	if (size == fs->fs_bsize) {
5715212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5724463Smckusic 		bdwrite(bp);
5734463Smckusic 		return (bno);
5744463Smckusic 	}
5754463Smckusic 	/*
5764463Smckusic 	 * check to see if any fragments are already available
5774463Smckusic 	 * allocsiz is the size which will be allocated, hacking
5784463Smckusic 	 * it down to a smaller size if necessary
5794463Smckusic 	 */
5805960Smckusic 	frags = numfrags(fs, size);
5815322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
5824463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
5834463Smckusic 			break;
5845322Smckusic 	if (allocsiz == fs->fs_frag) {
5854463Smckusic 		/*
5864463Smckusic 		 * no fragments were available, so a block will be
5874463Smckusic 		 * allocated, and hacked up
5884463Smckusic 		 */
5894792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
5904463Smckusic 			brelse(bp);
5916294Smckusick 			return (NULL);
5924463Smckusic 		}
5935212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5945377Smckusic 		bpref = dtogd(fs, bno);
5955322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
5964463Smckusic 			setbit(cgp->cg_free, bpref + i);
5975322Smckusic 		i = fs->fs_frag - frags;
5984792Smckusic 		cgp->cg_cs.cs_nffree += i;
5994792Smckusic 		fs->fs_cstotal.cs_nffree += i;
6005322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
6019762Ssam 		fs->fs_fmod++;
6024463Smckusic 		cgp->cg_frsum[i]++;
6034463Smckusic 		bdwrite(bp);
6044463Smckusic 		return (bno);
6054463Smckusic 	}
6064651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
60715950Smckusick 	if (bno < 0) {
60815950Smckusick 		brelse(bp);
6096294Smckusick 		return (NULL);
61015950Smckusick 	}
6114463Smckusic 	for (i = 0; i < frags; i++)
6124463Smckusic 		clrbit(cgp->cg_free, bno + i);
6134792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6144792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6155322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
6169762Ssam 	fs->fs_fmod++;
6174463Smckusic 	cgp->cg_frsum[allocsiz]--;
6184463Smckusic 	if (frags != allocsiz)
6194463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6204463Smckusic 	bdwrite(bp);
6214463Smckusic 	return (cg * fs->fs_fpg + bno);
6224463Smckusic }
6234463Smckusic 
6245375Smckusic /*
6255375Smckusic  * Allocate a block in a cylinder group.
6265375Smckusic  *
6275375Smckusic  * This algorithm implements the following policy:
6285375Smckusic  *   1) allocate the requested block.
6295375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6305375Smckusic  *   3) allocate the next available block on the block rotor for the
6315375Smckusic  *      specified cylinder group.
6325375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6335375Smckusic  * blocks may be fragmented by the routine that allocates them.
6345375Smckusic  */
6354463Smckusic daddr_t
6365212Smckusic alloccgblk(fs, cgp, bpref)
6375965Smckusic 	register struct fs *fs;
6384463Smckusic 	register struct cg *cgp;
6394463Smckusic 	daddr_t bpref;
6404463Smckusic {
6414651Smckusic 	daddr_t bno;
6426294Smckusick 	int cylno, pos, delta;
6434651Smckusic 	short *cylbp;
6445361Smckusic 	register int i;
6454463Smckusic 
6464651Smckusic 	if (bpref == 0) {
6474651Smckusic 		bpref = cgp->cg_rotor;
6485361Smckusic 		goto norot;
6495361Smckusic 	}
65017224Smckusick 	bpref = blknum(fs, bpref);
6515377Smckusic 	bpref = dtogd(fs, bpref);
6525361Smckusic 	/*
6535361Smckusic 	 * if the requested block is available, use it
6545361Smckusic 	 */
65511638Ssam 	if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) {
6565361Smckusic 		bno = bpref;
6575361Smckusic 		goto gotit;
6585361Smckusic 	}
6595361Smckusic 	/*
6605361Smckusic 	 * check for a block available on the same cylinder
6615361Smckusic 	 */
6625361Smckusic 	cylno = cbtocylno(fs, bpref);
6635375Smckusic 	if (cgp->cg_btot[cylno] == 0)
6645375Smckusic 		goto norot;
6655375Smckusic 	if (fs->fs_cpc == 0) {
6665375Smckusic 		/*
6675375Smckusic 		 * block layout info is not available, so just have
6685375Smckusic 		 * to take any block in this cylinder.
6695375Smckusic 		 */
6705375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
6715375Smckusic 		goto norot;
6725375Smckusic 	}
6735375Smckusic 	/*
6745361Smckusic 	 * check the summary information to see if a block is
6755361Smckusic 	 * available in the requested cylinder starting at the
6769163Ssam 	 * requested rotational position and proceeding around.
6775361Smckusic 	 */
6789163Ssam 	cylbp = cgp->cg_b[cylno];
6799163Ssam 	pos = cbtorpos(fs, bpref);
6805361Smckusic 	for (i = pos; i < NRPOS; i++)
6815361Smckusic 		if (cylbp[i] > 0)
6825361Smckusic 			break;
6835361Smckusic 	if (i == NRPOS)
6845361Smckusic 		for (i = 0; i < pos; i++)
6855361Smckusic 			if (cylbp[i] > 0)
6865361Smckusic 				break;
6875361Smckusic 	if (cylbp[i] > 0) {
6884651Smckusic 		/*
6895361Smckusic 		 * found a rotational position, now find the actual
6905361Smckusic 		 * block. A panic if none is actually there.
6914651Smckusic 		 */
6925361Smckusic 		pos = cylno % fs->fs_cpc;
6935361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
6946716Smckusick 		if (fs->fs_postbl[pos][i] == -1) {
6956716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
6966716Smckusick 			    pos, i, fs->fs_fsmnt);
6975361Smckusic 			panic("alloccgblk: cyl groups corrupted");
6986716Smckusick 		}
6996294Smckusick 		for (i = fs->fs_postbl[pos][i];; ) {
7005361Smckusic 			if (isblock(fs, cgp->cg_free, bno + i)) {
70111638Ssam 				bno = blkstofrags(fs, (bno + i));
7025361Smckusic 				goto gotit;
7035361Smckusic 			}
7046294Smckusick 			delta = fs->fs_rotbl[i];
7056294Smckusick 			if (delta <= 0 || delta > MAXBPC - i)
7064651Smckusic 				break;
7076294Smckusick 			i += delta;
7084651Smckusic 		}
7096716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
7105361Smckusic 		panic("alloccgblk: can't find blk in cyl");
7114359Smckusick 	}
7125361Smckusic norot:
7135361Smckusic 	/*
7145361Smckusic 	 * no blocks in the requested cylinder, so take next
7155361Smckusic 	 * available one in this cylinder group.
7165361Smckusic 	 */
7178628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7186567Smckusic 	if (bno < 0)
7196294Smckusick 		return (NULL);
7204651Smckusic 	cgp->cg_rotor = bno;
7214359Smckusick gotit:
72211638Ssam 	clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno));
7234792Smckusic 	cgp->cg_cs.cs_nbfree--;
7244792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7255322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7265375Smckusic 	cylno = cbtocylno(fs, bno);
7275375Smckusic 	cgp->cg_b[cylno][cbtorpos(fs, bno)]--;
7285375Smckusic 	cgp->cg_btot[cylno]--;
7294359Smckusick 	fs->fs_fmod++;
7304651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7314359Smckusick }
7324359Smckusick 
7335375Smckusic /*
7345375Smckusic  * Determine whether an inode can be allocated.
7355375Smckusic  *
7365375Smckusic  * Check to see if an inode is available, and if it is,
7375375Smckusic  * allocate it using the following policy:
7385375Smckusic  *   1) allocate the requested inode.
7395375Smckusic  *   2) allocate the next available inode after the requested
7405375Smckusic  *      inode in the specified cylinder group.
7415375Smckusic  */
7429163Ssam ino_t
7435965Smckusic ialloccg(ip, cg, ipref, mode)
7445965Smckusic 	struct inode *ip;
7454359Smckusick 	int cg;
7464359Smckusick 	daddr_t ipref;
7474359Smckusick 	int mode;
7484359Smckusick {
7495965Smckusic 	register struct fs *fs;
7504463Smckusic 	register struct cg *cgp;
75116784Smckusick 	struct buf *bp;
75216784Smckusick 	int start, len, loc, map, i;
7534359Smckusick 
7545965Smckusic 	fs = ip->i_fs;
7555322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7566294Smckusick 		return (NULL);
75710278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
7586531Smckusick 	cgp = bp->b_un.b_cg;
75915950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
76015950Smckusick 	    cgp->cg_cs.cs_nifree == 0) {
7615960Smckusic 		brelse(bp);
7626294Smckusick 		return (NULL);
7635960Smckusic 	}
7648105Sroot 	cgp->cg_time = time.tv_sec;
7654359Smckusick 	if (ipref) {
7664359Smckusick 		ipref %= fs->fs_ipg;
7674359Smckusick 		if (isclr(cgp->cg_iused, ipref))
7684359Smckusick 			goto gotit;
76916784Smckusick 	}
77016784Smckusick 	start = cgp->cg_irotor / NBBY;
77116784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
77216784Smckusick 	loc = skpc(0xff, len, &cgp->cg_iused[start]);
77316784Smckusick 	if (loc == 0) {
77417697Smckusick 		len = start + 1;
77517697Smckusick 		start = 0;
77617697Smckusick 		loc = skpc(0xff, len, &cgp->cg_iused[0]);
77717697Smckusick 		if (loc == 0) {
77817697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
77917697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
78017697Smckusick 			panic("ialloccg: map corrupted");
78117697Smckusick 			/* NOTREACHED */
78217697Smckusick 		}
78316784Smckusick 	}
78416784Smckusick 	i = start + len - loc;
78516784Smckusick 	map = cgp->cg_iused[i];
78616784Smckusick 	ipref = i * NBBY;
78716784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
78816784Smckusick 		if ((map & i) == 0) {
7894359Smckusick 			cgp->cg_irotor = ipref;
7904359Smckusick 			goto gotit;
7914359Smckusick 		}
7924359Smckusick 	}
79316784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
79416784Smckusick 	panic("ialloccg: block not in map");
79516784Smckusick 	/* NOTREACHED */
7964359Smckusick gotit:
7974359Smckusick 	setbit(cgp->cg_iused, ipref);
7984792Smckusic 	cgp->cg_cs.cs_nifree--;
7994792Smckusic 	fs->fs_cstotal.cs_nifree--;
8005322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
8014359Smckusick 	fs->fs_fmod++;
8024359Smckusick 	if ((mode & IFMT) == IFDIR) {
8034792Smckusic 		cgp->cg_cs.cs_ndir++;
8044792Smckusic 		fs->fs_cstotal.cs_ndir++;
8055322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8064359Smckusick 	}
8074359Smckusick 	bdwrite(bp);
8084359Smckusick 	return (cg * fs->fs_ipg + ipref);
8094359Smckusick }
8104359Smckusick 
8115375Smckusic /*
8125375Smckusic  * Free a block or fragment.
8135375Smckusic  *
8145375Smckusic  * The specified block or fragment is placed back in the
8155375Smckusic  * free map. If a fragment is deallocated, a possible
8165375Smckusic  * block reassembly is checked.
8175375Smckusic  */
8189163Ssam free(ip, bno, size)
8195965Smckusic 	register struct inode *ip;
8204359Smckusick 	daddr_t bno;
8215212Smckusic 	off_t size;
8224359Smckusick {
8234359Smckusick 	register struct fs *fs;
8244359Smckusick 	register struct cg *cgp;
8254359Smckusick 	register struct buf *bp;
8264463Smckusic 	int cg, blk, frags, bbase;
8274463Smckusic 	register int i;
8284359Smckusick 
8295965Smckusic 	fs = ip->i_fs;
8306716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8316716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8326716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
8334426Smckusic 		panic("free: bad size");
8346716Smckusick 	}
8355377Smckusic 	cg = dtog(fs, bno);
8366567Smckusic 	if (badblock(fs, bno)) {
8376567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
8384359Smckusick 		return;
8396567Smckusic 	}
84010278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
8416531Smckusick 	cgp = bp->b_un.b_cg;
8426531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
8435960Smckusic 		brelse(bp);
8444359Smckusick 		return;
8455960Smckusic 	}
8468105Sroot 	cgp->cg_time = time.tv_sec;
8475377Smckusic 	bno = dtogd(fs, bno);
8485322Smckusic 	if (size == fs->fs_bsize) {
84911638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) {
8506716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
8516716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
8524426Smckusic 			panic("free: freeing free block");
8536567Smckusic 		}
85411638Ssam 		setblock(fs, cgp->cg_free, fragstoblks(fs, bno));
8554792Smckusic 		cgp->cg_cs.cs_nbfree++;
8564792Smckusic 		fs->fs_cstotal.cs_nbfree++;
8575322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
8585375Smckusic 		i = cbtocylno(fs, bno);
8595375Smckusic 		cgp->cg_b[i][cbtorpos(fs, bno)]++;
8605375Smckusic 		cgp->cg_btot[i]++;
8614426Smckusic 	} else {
86217224Smckusick 		bbase = bno - fragnum(fs, bno);
8634463Smckusic 		/*
8644463Smckusic 		 * decrement the counts associated with the old frags
8654463Smckusic 		 */
8666294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
8675322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
8684463Smckusic 		/*
8694463Smckusic 		 * deallocate the fragment
8704463Smckusic 		 */
8715960Smckusic 		frags = numfrags(fs, size);
8724463Smckusic 		for (i = 0; i < frags; i++) {
8736716Smckusick 			if (isset(cgp->cg_free, bno + i)) {
8746716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
8756716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
8764426Smckusic 				panic("free: freeing free frag");
8776716Smckusick 			}
8784426Smckusic 			setbit(cgp->cg_free, bno + i);
8794426Smckusic 		}
8806294Smckusick 		cgp->cg_cs.cs_nffree += i;
8816294Smckusick 		fs->fs_cstotal.cs_nffree += i;
8826294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
8834463Smckusic 		/*
8844463Smckusic 		 * add back in counts associated with the new frags
8854463Smckusic 		 */
8866294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
8875322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
8884463Smckusic 		/*
8894463Smckusic 		 * if a complete block has been reassembled, account for it
8904463Smckusic 		 */
89111638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) {
8925322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
8935322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
8945322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
8954792Smckusic 			cgp->cg_cs.cs_nbfree++;
8964792Smckusic 			fs->fs_cstotal.cs_nbfree++;
8975322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
8985375Smckusic 			i = cbtocylno(fs, bbase);
8995375Smckusic 			cgp->cg_b[i][cbtorpos(fs, bbase)]++;
9005375Smckusic 			cgp->cg_btot[i]++;
9014426Smckusic 		}
9024426Smckusic 	}
9034359Smckusick 	fs->fs_fmod++;
9044359Smckusick 	bdwrite(bp);
9054359Smckusick }
9064359Smckusick 
9075375Smckusic /*
9085375Smckusic  * Free an inode.
9095375Smckusic  *
9105375Smckusic  * The specified inode is placed back in the free map.
9115375Smckusic  */
9125965Smckusic ifree(ip, ino, mode)
9135965Smckusic 	struct inode *ip;
9144359Smckusick 	ino_t ino;
9154359Smckusick 	int mode;
9164359Smckusick {
9174359Smckusick 	register struct fs *fs;
9184359Smckusick 	register struct cg *cgp;
9194359Smckusick 	register struct buf *bp;
9204359Smckusick 	int cg;
9214359Smckusick 
9225965Smckusic 	fs = ip->i_fs;
9236716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
9246716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9256716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9264359Smckusick 		panic("ifree: range");
9276716Smckusick 	}
9285377Smckusic 	cg = itog(fs, ino);
92910278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
9306531Smckusick 	cgp = bp->b_un.b_cg;
9316531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
9325960Smckusic 		brelse(bp);
9334359Smckusick 		return;
9345960Smckusic 	}
9358105Sroot 	cgp->cg_time = time.tv_sec;
9364359Smckusick 	ino %= fs->fs_ipg;
9376716Smckusick 	if (isclr(cgp->cg_iused, ino)) {
9386716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9396716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9404359Smckusick 		panic("ifree: freeing free inode");
9416716Smckusick 	}
9424359Smckusick 	clrbit(cgp->cg_iused, ino);
94316784Smckusick 	if (ino < cgp->cg_irotor)
94416784Smckusick 		cgp->cg_irotor = ino;
9454792Smckusic 	cgp->cg_cs.cs_nifree++;
9464792Smckusic 	fs->fs_cstotal.cs_nifree++;
9475322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
9484359Smckusick 	if ((mode & IFMT) == IFDIR) {
9494792Smckusic 		cgp->cg_cs.cs_ndir--;
9504792Smckusic 		fs->fs_cstotal.cs_ndir--;
9515322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
9524359Smckusick 	}
9534359Smckusick 	fs->fs_fmod++;
9544359Smckusick 	bdwrite(bp);
9554359Smckusick }
9564359Smckusick 
9574463Smckusic /*
9585375Smckusic  * Find a block of the specified size in the specified cylinder group.
9595375Smckusic  *
9604651Smckusic  * It is a panic if a request is made to find a block if none are
9614651Smckusic  * available.
9624651Smckusic  */
9634651Smckusic daddr_t
9644651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
9654651Smckusic 	register struct fs *fs;
9664651Smckusic 	register struct cg *cgp;
9674651Smckusic 	daddr_t bpref;
9684651Smckusic 	int allocsiz;
9694651Smckusic {
9704651Smckusic 	daddr_t bno;
9714651Smckusic 	int start, len, loc, i;
9724651Smckusic 	int blk, field, subfield, pos;
9734651Smckusic 
9744651Smckusic 	/*
9754651Smckusic 	 * find the fragment by searching through the free block
9764651Smckusic 	 * map for an appropriate bit pattern
9774651Smckusic 	 */
9784651Smckusic 	if (bpref)
9795377Smckusic 		start = dtogd(fs, bpref) / NBBY;
9804651Smckusic 	else
9814651Smckusic 		start = cgp->cg_frotor / NBBY;
9825398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
98312755Ssam 	loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start],
98412755Ssam 		(caddr_t)fragtbl[fs->fs_frag],
98512755Ssam 		(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
9864651Smckusic 	if (loc == 0) {
9876531Smckusick 		len = start + 1;
9886531Smckusick 		start = 0;
98917697Smckusick 		loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[0],
99012755Ssam 			(caddr_t)fragtbl[fs->fs_frag],
99112755Ssam 			(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
99216784Smckusick 		if (loc == 0) {
99316784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
99416784Smckusick 			    start, len, fs->fs_fsmnt);
99516784Smckusick 			panic("alloccg: map corrupted");
99617697Smckusick 			/* NOTREACHED */
99716784Smckusick 		}
9984651Smckusic 	}
9994651Smckusic 	bno = (start + len - loc) * NBBY;
10004651Smckusic 	cgp->cg_frotor = bno;
10014651Smckusic 	/*
10024651Smckusic 	 * found the byte in the map
10034651Smckusic 	 * sift through the bits to find the selected frag
10044651Smckusic 	 */
10056294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
10066294Smckusick 		blk = blkmap(fs, cgp->cg_free, bno);
10074651Smckusic 		blk <<= 1;
10084651Smckusic 		field = around[allocsiz];
10094651Smckusic 		subfield = inside[allocsiz];
10105322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10116294Smckusick 			if ((blk & field) == subfield)
10126294Smckusick 				return (bno + pos);
10134651Smckusic 			field <<= 1;
10144651Smckusic 			subfield <<= 1;
10154651Smckusic 		}
10164651Smckusic 	}
10176716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
10184651Smckusic 	panic("alloccg: block not in map");
10196531Smckusick 	return (-1);
10204651Smckusic }
10214651Smckusic 
10224651Smckusic /*
10235375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10245375Smckusic  *
10255375Smckusic  * The form of the error message is:
10264359Smckusick  *	fs: error message
10274359Smckusick  */
10284359Smckusick fserr(fs, cp)
10294359Smckusick 	struct fs *fs;
10304359Smckusick 	char *cp;
10314359Smckusick {
10324359Smckusick 
103324839Seric 	log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp);
10344359Smckusick }
1035