xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 34143)
123394Smckusick /*
229113Smckusick  * Copyright (c) 1982, 1986 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*34143Smckusick  *	@(#)lfs_alloc.c	7.5 (Berkeley) 05/01/88
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"
2026253Skarels #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;
11732664Smckusick 	int i, count;
1184426Smckusic 
1195965Smckusic 	fs = ip->i_fs;
1205960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1216716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1226716Smckusick 		printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1236716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
1244463Smckusic 		panic("realloccg: bad size");
1256716Smckusick 	}
12611638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1274792Smckusic 		goto nospace;
1286716Smckusick 	if (bprev == 0) {
1296716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1306716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1314463Smckusic 		panic("realloccg: bad bprev");
1326716Smckusick 	}
1337650Ssam #ifdef QUOTA
13412643Ssam 	u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0);
13512643Ssam 	if (u.u_error)
13612643Ssam 		return (NULL);
1377483Skre #endif
1386294Smckusick 	cg = dtog(fs, bprev);
1395965Smckusic 	bno = fragextend(ip, cg, (long)bprev, osize, nsize);
1404463Smckusic 	if (bno != 0) {
1417187Sroot 		do {
1427187Sroot 			bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
1437187Sroot 			if (bp->b_flags & B_ERROR) {
1447187Sroot 				brelse(bp);
1457187Sroot 				return (NULL);
1467187Sroot 			}
1477187Sroot 		} while (brealloc(bp, nsize) == 0);
1487187Sroot 		bp->b_flags |= B_DONE;
1499163Ssam 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
15012643Ssam 		ip->i_blocks += btodb(nsize - osize);
15112643Ssam 		ip->i_flag |= IUPD|ICHG;
1524463Smckusic 		return (bp);
1534463Smckusic 	}
1544948Smckusic 	if (bpref >= fs->fs_size)
1554948Smckusic 		bpref = 0;
15626253Skarels 	switch ((int)fs->fs_optim) {
15725256Smckusick 	case FS_OPTSPACE:
15825256Smckusick 		/*
15925256Smckusick 		 * Allocate an exact sized fragment. Although this makes
16025256Smckusick 		 * best use of space, we will waste time relocating it if
16125256Smckusick 		 * the file continues to grow. If the fragmentation is
16225256Smckusick 		 * less than half of the minimum free reserve, we choose
16325256Smckusick 		 * to begin optimizing for time.
16425256Smckusick 		 */
16524698Smckusick 		request = nsize;
16625256Smckusick 		if (fs->fs_minfree < 5 ||
16725256Smckusick 		    fs->fs_cstotal.cs_nffree >
16825256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
16925256Smckusick 			break;
17025256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
17125256Smckusick 			fs->fs_fsmnt);
17225256Smckusick 		fs->fs_optim = FS_OPTTIME;
17325256Smckusick 		break;
17425256Smckusick 	case FS_OPTTIME:
17525256Smckusick 		/*
17625256Smckusick 		 * At this point we have discovered a file that is trying
17725256Smckusick 		 * to grow a small fragment to a larger fragment. To save
17825256Smckusick 		 * time, we allocate a full sized block, then free the
17925256Smckusick 		 * unused portion. If the file continues to grow, the
18025256Smckusick 		 * `fragextend' call above will be able to grow it in place
18125256Smckusick 		 * without further copying. If aberrant programs cause
18225256Smckusick 		 * disk fragmentation to grow within 2% of the free reserve,
18325256Smckusick 		 * we choose to begin optimizing for space.
18425256Smckusick 		 */
18524698Smckusick 		request = fs->fs_bsize;
18625256Smckusick 		if (fs->fs_cstotal.cs_nffree <
18725256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
18825256Smckusick 			break;
18925256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
19025256Smckusick 			fs->fs_fsmnt);
19125256Smckusick 		fs->fs_optim = FS_OPTSPACE;
19225256Smckusick 		break;
19325256Smckusick 	default:
19425256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
19525256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
19625256Smckusick 		panic("realloccg: bad optim");
19725256Smckusick 		/* NOTREACHED */
19825256Smckusick 	}
19924698Smckusick 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
2009163Ssam 		(u_long (*)())alloccg);
2016567Smckusic 	if (bno > 0) {
2025965Smckusic 		obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
2035960Smckusic 		if (obp->b_flags & B_ERROR) {
2045960Smckusic 			brelse(obp);
2056294Smckusick 			return (NULL);
2065960Smckusic 		}
20725471Smckusick 		bn = fsbtodb(fs, bno);
20825471Smckusick 		bp = getblk(ip->i_dev, bn, nsize);
20917658Smckusick 		bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
21030749Skarels 		count = howmany(osize, CLBYTES);
21130749Skarels 		for (i = 0; i < count; i++)
21230749Skarels 			munhash(ip->i_dev, bn + i * CLBYTES / DEV_BSIZE);
21317658Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
21417658Smckusick 		if (obp->b_flags & B_DELWRI) {
21517658Smckusick 			obp->b_flags &= ~B_DELWRI;
21617658Smckusick 			u.u_ru.ru_oublock--;		/* delete charge */
21717658Smckusick 		}
2184463Smckusic 		brelse(obp);
21931402Smckusick 		blkfree(ip, bprev, (off_t)osize);
22024698Smckusick 		if (nsize < request)
22131402Smckusick 			blkfree(ip, bno + numfrags(fs, nsize),
22224698Smckusick 				(off_t)(request - nsize));
22312643Ssam 		ip->i_blocks += btodb(nsize - osize);
22412643Ssam 		ip->i_flag |= IUPD|ICHG;
2256294Smckusick 		return (bp);
2264463Smckusic 	}
2274792Smckusic nospace:
2284463Smckusic 	/*
2294463Smckusic 	 * no space available
2304463Smckusic 	 */
2314426Smckusic 	fserr(fs, "file system full");
2324426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
2334426Smckusic 	u.u_error = ENOSPC;
2344426Smckusic 	return (NULL);
2354426Smckusic }
2364426Smckusic 
2375375Smckusic /*
2385375Smckusic  * Allocate an inode in the file system.
2395375Smckusic  *
2405375Smckusic  * A preference may be optionally specified. If a preference is given
2415375Smckusic  * the following hierarchy is used to allocate an inode:
2425375Smckusic  *   1) allocate the requested inode.
2435375Smckusic  *   2) allocate an inode in the same cylinder group.
2445375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2455375Smckusic  *      available inode is located.
2465375Smckusic  * If no inode preference is given the following heirarchy is used
2475375Smckusic  * to allocate an inode:
2485375Smckusic  *   1) allocate an inode in cylinder group 0.
2495375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2505375Smckusic  *      available inode is located.
2515375Smckusic  */
2524359Smckusick struct inode *
2535965Smckusic ialloc(pip, ipref, mode)
2545965Smckusic 	register struct inode *pip;
2554359Smckusick 	ino_t ipref;
2564359Smckusick 	int mode;
2574359Smckusick {
2585212Smckusic 	ino_t ino;
2594359Smckusick 	register struct fs *fs;
2604359Smckusick 	register struct inode *ip;
2614359Smckusick 	int cg;
2624359Smckusick 
2635965Smckusic 	fs = pip->i_fs;
2644792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2654359Smckusick 		goto noinodes;
2667650Ssam #ifdef QUOTA
26712643Ssam 	u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
26812643Ssam 	if (u.u_error)
26912643Ssam 		return (NULL);
2707483Skre #endif
2714948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2724948Smckusic 		ipref = 0;
2735377Smckusic 	cg = itog(fs, ipref);
2745965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2754359Smckusick 	if (ino == 0)
2764359Smckusick 		goto noinodes;
2775965Smckusic 	ip = iget(pip->i_dev, pip->i_fs, ino);
2784359Smckusick 	if (ip == NULL) {
27915120Skarels 		ifree(pip, ino, 0);
2804359Smckusick 		return (NULL);
2814359Smckusick 	}
2826716Smckusick 	if (ip->i_mode) {
2836716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
2846716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2854359Smckusick 		panic("ialloc: dup alloc");
2866716Smckusick 	}
28712643Ssam 	if (ip->i_blocks) {				/* XXX */
28812643Ssam 		printf("free inode %s/%d had %d blocks\n",
28912643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
29012643Ssam 		ip->i_blocks = 0;
29112643Ssam 	}
2924359Smckusick 	return (ip);
2934359Smckusick noinodes:
2944359Smckusick 	fserr(fs, "out of inodes");
2956294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
2964359Smckusick 	u.u_error = ENOSPC;
2974359Smckusick 	return (NULL);
2984359Smckusick }
2994359Smckusick 
3004651Smckusic /*
3015375Smckusic  * Find a cylinder to place a directory.
3025375Smckusic  *
3035375Smckusic  * The policy implemented by this algorithm is to select from
3045375Smckusic  * among those cylinder groups with above the average number of
3055375Smckusic  * free inodes, the one with the smallest number of directories.
3064651Smckusic  */
3079163Ssam ino_t
3085965Smckusic dirpref(fs)
3095965Smckusic 	register struct fs *fs;
3104359Smckusick {
3114651Smckusic 	int cg, minndir, mincg, avgifree;
3124359Smckusick 
3134792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3144651Smckusic 	minndir = fs->fs_ipg;
3154359Smckusick 	mincg = 0;
3164651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3175322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3185322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3194359Smckusick 			mincg = cg;
3205322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3214359Smckusick 		}
3229163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3234359Smckusick }
3244359Smckusick 
3254651Smckusic /*
3269163Ssam  * Select the desired position for the next block in a file.  The file is
3279163Ssam  * logically divided into sections. The first section is composed of the
3289163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3299163Ssam  *
3309163Ssam  * If no blocks have been allocated in the first section, the policy is to
3319163Ssam  * request a block in the same cylinder group as the inode that describes
3329163Ssam  * the file. If no blocks have been allocated in any other section, the
3339163Ssam  * policy is to place the section in a cylinder group with a greater than
3349163Ssam  * average number of free blocks.  An appropriate cylinder group is found
33517696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
33617696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
33717696Smckusick  * cylinder group from which the previous allocation was made. The sweep
33817696Smckusick  * continues until a cylinder group with greater than the average number
33917696Smckusick  * of free blocks is found. If the allocation is for the first block in an
34017696Smckusick  * indirect block, the information on the previous allocation is unavailable;
34117696Smckusick  * here a best guess is made based upon the logical block number being
34217696Smckusick  * allocated.
3439163Ssam  *
3449163Ssam  * If a section is already partially allocated, the policy is to
3459163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3469163Ssam  * contiguous blocks and the beginning of the next is physically separated
3479163Ssam  * so that the disk head will be in transit between them for at least
3489163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3499163Ssam  * schedule another I/O transfer.
3504651Smckusic  */
3515212Smckusic daddr_t
3529163Ssam blkpref(ip, lbn, indx, bap)
3539163Ssam 	struct inode *ip;
3549163Ssam 	daddr_t lbn;
3559163Ssam 	int indx;
3569163Ssam 	daddr_t *bap;
3579163Ssam {
3585965Smckusic 	register struct fs *fs;
35917696Smckusick 	register int cg;
36017696Smckusick 	int avgbfree, startcg;
3619163Ssam 	daddr_t nextblk;
3624651Smckusic 
3639163Ssam 	fs = ip->i_fs;
3649163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3659163Ssam 		if (lbn < NDADDR) {
3669163Ssam 			cg = itog(fs, ip->i_number);
3675322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3684651Smckusic 		}
3699163Ssam 		/*
3709163Ssam 		 * Find a cylinder with greater than average number of
3719163Ssam 		 * unused data blocks.
3729163Ssam 		 */
37317696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
37417696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
37517696Smckusick 		else
37617696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
37717696Smckusick 		startcg %= fs->fs_ncg;
3789163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
37917696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
3809163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3819163Ssam 				fs->fs_cgrotor = cg;
3829163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3839163Ssam 			}
38417696Smckusick 		for (cg = 0; cg <= startcg; cg++)
3859163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3869163Ssam 				fs->fs_cgrotor = cg;
3879163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3889163Ssam 			}
3899163Ssam 		return (NULL);
3909163Ssam 	}
3919163Ssam 	/*
3929163Ssam 	 * One or more previous blocks have been laid out. If less
3939163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
3949163Ssam 	 * next block is requested contiguously, otherwise it is
3959163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
3969163Ssam 	 */
3979163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
3989163Ssam 	if (indx > fs->fs_maxcontig &&
39911638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4009163Ssam 	    != nextblk)
4019163Ssam 		return (nextblk);
4029163Ssam 	if (fs->fs_rotdelay != 0)
4039163Ssam 		/*
4049163Ssam 		 * Here we convert ms of delay to frags as:
4059163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4069163Ssam 		 *	((sect/frag) * (ms/sec))
4079163Ssam 		 * then round up to the next block.
4089163Ssam 		 */
4099163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4109163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4119163Ssam 	return (nextblk);
4124651Smckusic }
4134651Smckusic 
4145375Smckusic /*
4155375Smckusic  * Implement the cylinder overflow algorithm.
4165375Smckusic  *
4175375Smckusic  * The policy implemented by this algorithm is:
4185375Smckusic  *   1) allocate the block in its requested cylinder group.
4195375Smckusic  *   2) quadradically rehash on the cylinder group number.
4205375Smckusic  *   3) brute force search for a free block.
4215375Smckusic  */
4225212Smckusic /*VARARGS5*/
4235212Smckusic u_long
4245965Smckusic hashalloc(ip, cg, pref, size, allocator)
4255965Smckusic 	struct inode *ip;
4264359Smckusick 	int cg;
4274359Smckusick 	long pref;
4284359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4295212Smckusic 	u_long (*allocator)();
4304359Smckusick {
4315965Smckusic 	register struct fs *fs;
4324359Smckusick 	long result;
4334359Smckusick 	int i, icg = cg;
4344359Smckusick 
4355965Smckusic 	fs = ip->i_fs;
4364359Smckusick 	/*
4374359Smckusick 	 * 1: preferred cylinder group
4384359Smckusick 	 */
4395965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4404359Smckusick 	if (result)
4414359Smckusick 		return (result);
4424359Smckusick 	/*
4434359Smckusick 	 * 2: quadratic rehash
4444359Smckusick 	 */
4454359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4464359Smckusick 		cg += i;
4474359Smckusick 		if (cg >= fs->fs_ncg)
4484359Smckusick 			cg -= fs->fs_ncg;
4495965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4504359Smckusick 		if (result)
4514359Smckusick 			return (result);
4524359Smckusick 	}
4534359Smckusick 	/*
4544359Smckusick 	 * 3: brute force search
45510847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
45610847Ssam 	 * and 1 is always checked in the quadratic rehash.
4574359Smckusick 	 */
45810848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
45910847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4605965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4614359Smckusick 		if (result)
4624359Smckusick 			return (result);
4634359Smckusick 		cg++;
4644359Smckusick 		if (cg == fs->fs_ncg)
4654359Smckusick 			cg = 0;
4664359Smckusick 	}
4676294Smckusick 	return (NULL);
4684359Smckusick }
4694359Smckusick 
4705375Smckusic /*
4715375Smckusic  * Determine whether a fragment can be extended.
4725375Smckusic  *
4735375Smckusic  * Check to see if the necessary fragments are available, and
4745375Smckusic  * if they are, allocate them.
4755375Smckusic  */
4764359Smckusick daddr_t
4775965Smckusic fragextend(ip, cg, bprev, osize, nsize)
4785965Smckusic 	struct inode *ip;
4794426Smckusic 	int cg;
4804463Smckusic 	long bprev;
4814426Smckusic 	int osize, nsize;
4824426Smckusic {
4835965Smckusic 	register struct fs *fs;
4844463Smckusic 	register struct buf *bp;
4854463Smckusic 	register struct cg *cgp;
4864463Smckusic 	long bno;
4874463Smckusic 	int frags, bbase;
4884426Smckusic 	int i;
4894426Smckusic 
4905965Smckusic 	fs = ip->i_fs;
49117224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
4926531Smckusick 		return (NULL);
4935960Smckusic 	frags = numfrags(fs, nsize);
49417224Smckusick 	bbase = fragnum(fs, bprev);
49517224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
49630749Skarels 		/* cannot extend across a block boundary */
4976294Smckusick 		return (NULL);
4984463Smckusic 	}
49910278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
5006531Smckusick 	cgp = bp->b_un.b_cg;
501*34143Smckusick 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
5025960Smckusic 		brelse(bp);
5036294Smckusick 		return (NULL);
5045960Smckusic 	}
5058105Sroot 	cgp->cg_time = time.tv_sec;
5065377Smckusic 	bno = dtogd(fs, bprev);
5075960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
508*34143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
5095361Smckusic 			brelse(bp);
5106294Smckusick 			return (NULL);
5115361Smckusic 		}
5125361Smckusic 	/*
5135361Smckusic 	 * the current fragment can be extended
5145361Smckusic 	 * deduct the count on fragment being extended into
5155361Smckusic 	 * increase the count on the remaining fragment (if any)
5165361Smckusic 	 * allocate the extended piece
5175361Smckusic 	 */
5185361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
519*34143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
5204463Smckusic 			break;
5215960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5225361Smckusic 	if (i != frags)
5235361Smckusic 		cgp->cg_frsum[i - frags]++;
5245960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
525*34143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
5265361Smckusic 		cgp->cg_cs.cs_nffree--;
5275361Smckusic 		fs->fs_cstotal.cs_nffree--;
5285361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5294463Smckusic 	}
5305361Smckusic 	fs->fs_fmod++;
5315361Smckusic 	bdwrite(bp);
5325361Smckusic 	return (bprev);
5334426Smckusic }
5344426Smckusic 
5355375Smckusic /*
5365375Smckusic  * Determine whether a block can be allocated.
5375375Smckusic  *
5385375Smckusic  * Check to see if a block of the apprpriate size is available,
5395375Smckusic  * and if it is, allocate it.
5405375Smckusic  */
5419163Ssam daddr_t
5425965Smckusic alloccg(ip, cg, bpref, size)
5435965Smckusic 	struct inode *ip;
5444359Smckusick 	int cg;
5454359Smckusick 	daddr_t bpref;
5464359Smckusick 	int size;
5474359Smckusick {
5485965Smckusic 	register struct fs *fs;
5494463Smckusic 	register struct buf *bp;
5504463Smckusic 	register struct cg *cgp;
5514463Smckusic 	int bno, frags;
5524463Smckusic 	int allocsiz;
5534463Smckusic 	register int i;
5544359Smckusick 
5555965Smckusic 	fs = ip->i_fs;
5565322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5576294Smckusick 		return (NULL);
55810278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
5596531Smckusick 	cgp = bp->b_un.b_cg;
560*34143Smckusick 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
56115950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5625960Smckusic 		brelse(bp);
5636294Smckusick 		return (NULL);
5645960Smckusic 	}
5658105Sroot 	cgp->cg_time = time.tv_sec;
5665322Smckusic 	if (size == fs->fs_bsize) {
5675212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5684463Smckusic 		bdwrite(bp);
5694463Smckusic 		return (bno);
5704463Smckusic 	}
5714463Smckusic 	/*
5724463Smckusic 	 * check to see if any fragments are already available
5734463Smckusic 	 * allocsiz is the size which will be allocated, hacking
5744463Smckusic 	 * it down to a smaller size if necessary
5754463Smckusic 	 */
5765960Smckusic 	frags = numfrags(fs, size);
5775322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
5784463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
5794463Smckusic 			break;
5805322Smckusic 	if (allocsiz == fs->fs_frag) {
5814463Smckusic 		/*
5824463Smckusic 		 * no fragments were available, so a block will be
5834463Smckusic 		 * allocated, and hacked up
5844463Smckusic 		 */
5854792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
5864463Smckusic 			brelse(bp);
5876294Smckusick 			return (NULL);
5884463Smckusic 		}
5895212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5905377Smckusic 		bpref = dtogd(fs, bno);
5915322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
592*34143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
5935322Smckusic 		i = fs->fs_frag - frags;
5944792Smckusic 		cgp->cg_cs.cs_nffree += i;
5954792Smckusic 		fs->fs_cstotal.cs_nffree += i;
5965322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
5979762Ssam 		fs->fs_fmod++;
5984463Smckusic 		cgp->cg_frsum[i]++;
5994463Smckusic 		bdwrite(bp);
6004463Smckusic 		return (bno);
6014463Smckusic 	}
6024651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
60315950Smckusick 	if (bno < 0) {
60415950Smckusick 		brelse(bp);
6056294Smckusick 		return (NULL);
60615950Smckusick 	}
6074463Smckusic 	for (i = 0; i < frags; i++)
608*34143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
6094792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6104792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6115322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
6129762Ssam 	fs->fs_fmod++;
6134463Smckusic 	cgp->cg_frsum[allocsiz]--;
6144463Smckusic 	if (frags != allocsiz)
6154463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6164463Smckusic 	bdwrite(bp);
6174463Smckusic 	return (cg * fs->fs_fpg + bno);
6184463Smckusic }
6194463Smckusic 
6205375Smckusic /*
6215375Smckusic  * Allocate a block in a cylinder group.
6225375Smckusic  *
6235375Smckusic  * This algorithm implements the following policy:
6245375Smckusic  *   1) allocate the requested block.
6255375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6265375Smckusic  *   3) allocate the next available block on the block rotor for the
6275375Smckusic  *      specified cylinder group.
6285375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6295375Smckusic  * blocks may be fragmented by the routine that allocates them.
6305375Smckusic  */
6314463Smckusic daddr_t
6325212Smckusic alloccgblk(fs, cgp, bpref)
6335965Smckusic 	register struct fs *fs;
6344463Smckusic 	register struct cg *cgp;
6354463Smckusic 	daddr_t bpref;
6364463Smckusic {
6374651Smckusic 	daddr_t bno;
6386294Smckusick 	int cylno, pos, delta;
6394651Smckusic 	short *cylbp;
6405361Smckusic 	register int i;
6414463Smckusic 
6424651Smckusic 	if (bpref == 0) {
6434651Smckusic 		bpref = cgp->cg_rotor;
6445361Smckusic 		goto norot;
6455361Smckusic 	}
64617224Smckusick 	bpref = blknum(fs, bpref);
6475377Smckusic 	bpref = dtogd(fs, bpref);
6485361Smckusic 	/*
6495361Smckusic 	 * if the requested block is available, use it
6505361Smckusic 	 */
651*34143Smckusick 	if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
6525361Smckusic 		bno = bpref;
6535361Smckusic 		goto gotit;
6545361Smckusic 	}
6555361Smckusic 	/*
6565361Smckusic 	 * check for a block available on the same cylinder
6575361Smckusic 	 */
6585361Smckusic 	cylno = cbtocylno(fs, bpref);
659*34143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
6605375Smckusic 		goto norot;
6615375Smckusic 	if (fs->fs_cpc == 0) {
6625375Smckusic 		/*
6635375Smckusic 		 * block layout info is not available, so just have
6645375Smckusic 		 * to take any block in this cylinder.
6655375Smckusic 		 */
6665375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
6675375Smckusic 		goto norot;
6685375Smckusic 	}
6695375Smckusic 	/*
6705361Smckusic 	 * check the summary information to see if a block is
6715361Smckusic 	 * available in the requested cylinder starting at the
6729163Ssam 	 * requested rotational position and proceeding around.
6735361Smckusic 	 */
674*34143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
6759163Ssam 	pos = cbtorpos(fs, bpref);
676*34143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
6775361Smckusic 		if (cylbp[i] > 0)
6785361Smckusic 			break;
679*34143Smckusick 	if (i == fs->fs_nrpos)
6805361Smckusic 		for (i = 0; i < pos; i++)
6815361Smckusic 			if (cylbp[i] > 0)
6825361Smckusic 				break;
6835361Smckusic 	if (cylbp[i] > 0) {
6844651Smckusic 		/*
6855361Smckusic 		 * found a rotational position, now find the actual
6865361Smckusic 		 * block. A panic if none is actually there.
6874651Smckusic 		 */
6885361Smckusic 		pos = cylno % fs->fs_cpc;
6895361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
690*34143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
6916716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
6926716Smckusick 			    pos, i, fs->fs_fsmnt);
6935361Smckusic 			panic("alloccgblk: cyl groups corrupted");
6946716Smckusick 		}
695*34143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
696*34143Smckusick 			if (isblock(fs, cg_blksfree(cgp), bno + i)) {
69711638Ssam 				bno = blkstofrags(fs, (bno + i));
6985361Smckusic 				goto gotit;
6995361Smckusic 			}
700*34143Smckusick 			delta = fs_rotbl(fs)[i];
701*34143Smckusick 			if (delta <= 0 ||
702*34143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
7034651Smckusic 				break;
7046294Smckusick 			i += delta;
7054651Smckusic 		}
7066716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
7075361Smckusic 		panic("alloccgblk: can't find blk in cyl");
7084359Smckusick 	}
7095361Smckusic norot:
7105361Smckusic 	/*
7115361Smckusic 	 * no blocks in the requested cylinder, so take next
7125361Smckusic 	 * available one in this cylinder group.
7135361Smckusic 	 */
7148628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7156567Smckusic 	if (bno < 0)
7166294Smckusick 		return (NULL);
7174651Smckusic 	cgp->cg_rotor = bno;
7184359Smckusick gotit:
719*34143Smckusick 	clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno));
7204792Smckusic 	cgp->cg_cs.cs_nbfree--;
7214792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7225322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7235375Smckusic 	cylno = cbtocylno(fs, bno);
724*34143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
725*34143Smckusick 	cg_blktot(cgp)[cylno]--;
7264359Smckusick 	fs->fs_fmod++;
7274651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7284359Smckusick }
729*34143Smckusick 
7305375Smckusic /*
7315375Smckusic  * Determine whether an inode can be allocated.
7325375Smckusic  *
7335375Smckusic  * Check to see if an inode is available, and if it is,
7345375Smckusic  * allocate it using the following policy:
7355375Smckusic  *   1) allocate the requested inode.
7365375Smckusic  *   2) allocate the next available inode after the requested
7375375Smckusic  *      inode in the specified cylinder group.
7385375Smckusic  */
7399163Ssam ino_t
7405965Smckusic ialloccg(ip, cg, ipref, mode)
7415965Smckusic 	struct inode *ip;
7424359Smckusick 	int cg;
7434359Smckusick 	daddr_t ipref;
7444359Smckusick 	int mode;
7454359Smckusick {
7465965Smckusic 	register struct fs *fs;
7474463Smckusic 	register struct cg *cgp;
74816784Smckusick 	struct buf *bp;
74916784Smckusick 	int start, len, loc, map, i;
7504359Smckusick 
7515965Smckusic 	fs = ip->i_fs;
7525322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7536294Smckusick 		return (NULL);
75410278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
7556531Smckusick 	cgp = bp->b_un.b_cg;
756*34143Smckusick 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
75715950Smckusick 	    cgp->cg_cs.cs_nifree == 0) {
7585960Smckusic 		brelse(bp);
7596294Smckusick 		return (NULL);
7605960Smckusic 	}
7618105Sroot 	cgp->cg_time = time.tv_sec;
7624359Smckusick 	if (ipref) {
7634359Smckusick 		ipref %= fs->fs_ipg;
764*34143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
7654359Smckusick 			goto gotit;
76616784Smckusick 	}
76716784Smckusick 	start = cgp->cg_irotor / NBBY;
76816784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
769*34143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
77016784Smckusick 	if (loc == 0) {
77117697Smckusick 		len = start + 1;
77217697Smckusick 		start = 0;
773*34143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
77417697Smckusick 		if (loc == 0) {
77517697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
77617697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
77717697Smckusick 			panic("ialloccg: map corrupted");
77817697Smckusick 			/* NOTREACHED */
77917697Smckusick 		}
78016784Smckusick 	}
78116784Smckusick 	i = start + len - loc;
782*34143Smckusick 	map = cg_inosused(cgp)[i];
78316784Smckusick 	ipref = i * NBBY;
78416784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
78516784Smckusick 		if ((map & i) == 0) {
7864359Smckusick 			cgp->cg_irotor = ipref;
7874359Smckusick 			goto gotit;
7884359Smckusick 		}
7894359Smckusick 	}
79016784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
79116784Smckusick 	panic("ialloccg: block not in map");
79216784Smckusick 	/* NOTREACHED */
7934359Smckusick gotit:
794*34143Smckusick 	setbit(cg_inosused(cgp), ipref);
7954792Smckusic 	cgp->cg_cs.cs_nifree--;
7964792Smckusic 	fs->fs_cstotal.cs_nifree--;
7975322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
7984359Smckusick 	fs->fs_fmod++;
7994359Smckusick 	if ((mode & IFMT) == IFDIR) {
8004792Smckusic 		cgp->cg_cs.cs_ndir++;
8014792Smckusic 		fs->fs_cstotal.cs_ndir++;
8025322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8034359Smckusick 	}
8044359Smckusick 	bdwrite(bp);
8054359Smckusick 	return (cg * fs->fs_ipg + ipref);
8064359Smckusick }
8074359Smckusick 
8085375Smckusic /*
8095375Smckusic  * Free a block or fragment.
8105375Smckusic  *
8115375Smckusic  * The specified block or fragment is placed back in the
8125375Smckusic  * free map. If a fragment is deallocated, a possible
8135375Smckusic  * block reassembly is checked.
8145375Smckusic  */
81531402Smckusick blkfree(ip, bno, size)
8165965Smckusic 	register struct inode *ip;
8174359Smckusick 	daddr_t bno;
8185212Smckusic 	off_t size;
8194359Smckusick {
8204359Smckusick 	register struct fs *fs;
8214359Smckusick 	register struct cg *cgp;
8224359Smckusick 	register struct buf *bp;
8234463Smckusic 	int cg, blk, frags, bbase;
8244463Smckusic 	register int i;
8254359Smckusick 
8265965Smckusic 	fs = ip->i_fs;
8276716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8286716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8296716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
83031402Smckusick 		panic("blkfree: bad size");
8316716Smckusick 	}
8325377Smckusic 	cg = dtog(fs, bno);
8336567Smckusic 	if (badblock(fs, bno)) {
8346567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
8354359Smckusick 		return;
8366567Smckusic 	}
83710278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
8386531Smckusick 	cgp = bp->b_un.b_cg;
839*34143Smckusick 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
8405960Smckusic 		brelse(bp);
8414359Smckusick 		return;
8425960Smckusic 	}
8438105Sroot 	cgp->cg_time = time.tv_sec;
8445377Smckusic 	bno = dtogd(fs, bno);
8455322Smckusic 	if (size == fs->fs_bsize) {
846*34143Smckusick 		if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) {
8476716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
8486716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
84931402Smckusick 			panic("blkfree: freeing free block");
8506567Smckusic 		}
851*34143Smckusick 		setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
8524792Smckusic 		cgp->cg_cs.cs_nbfree++;
8534792Smckusic 		fs->fs_cstotal.cs_nbfree++;
8545322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
8555375Smckusic 		i = cbtocylno(fs, bno);
856*34143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
857*34143Smckusick 		cg_blktot(cgp)[i]++;
8584426Smckusic 	} else {
85917224Smckusick 		bbase = bno - fragnum(fs, bno);
8604463Smckusic 		/*
8614463Smckusic 		 * decrement the counts associated with the old frags
8624463Smckusic 		 */
863*34143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
8645322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
8654463Smckusic 		/*
8664463Smckusic 		 * deallocate the fragment
8674463Smckusic 		 */
8685960Smckusic 		frags = numfrags(fs, size);
8694463Smckusic 		for (i = 0; i < frags; i++) {
870*34143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
8716716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
8726716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
87331402Smckusick 				panic("blkfree: freeing free frag");
8746716Smckusick 			}
875*34143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
8764426Smckusic 		}
8776294Smckusick 		cgp->cg_cs.cs_nffree += i;
8786294Smckusick 		fs->fs_cstotal.cs_nffree += i;
8796294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
8804463Smckusic 		/*
8814463Smckusic 		 * add back in counts associated with the new frags
8824463Smckusic 		 */
883*34143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
8845322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
8854463Smckusic 		/*
8864463Smckusic 		 * if a complete block has been reassembled, account for it
8874463Smckusic 		 */
888*34143Smckusick 		if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bbase))) {
8895322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
8905322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
8915322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
8924792Smckusic 			cgp->cg_cs.cs_nbfree++;
8934792Smckusic 			fs->fs_cstotal.cs_nbfree++;
8945322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
8955375Smckusic 			i = cbtocylno(fs, bbase);
896*34143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
897*34143Smckusick 			cg_blktot(cgp)[i]++;
8984426Smckusic 		}
8994426Smckusic 	}
9004359Smckusick 	fs->fs_fmod++;
9014359Smckusick 	bdwrite(bp);
9024359Smckusick }
9034359Smckusick 
9045375Smckusic /*
9055375Smckusic  * Free an inode.
9065375Smckusic  *
9075375Smckusic  * The specified inode is placed back in the free map.
9085375Smckusic  */
9095965Smckusic ifree(ip, ino, mode)
9105965Smckusic 	struct inode *ip;
9114359Smckusick 	ino_t ino;
9124359Smckusick 	int mode;
9134359Smckusick {
9144359Smckusick 	register struct fs *fs;
9154359Smckusick 	register struct cg *cgp;
9164359Smckusick 	register struct buf *bp;
9174359Smckusick 	int cg;
9184359Smckusick 
9195965Smckusic 	fs = ip->i_fs;
9206716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
9216716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9226716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9234359Smckusick 		panic("ifree: range");
9246716Smckusick 	}
9255377Smckusic 	cg = itog(fs, ino);
92610278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
9276531Smckusick 	cgp = bp->b_un.b_cg;
928*34143Smckusick 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
9295960Smckusic 		brelse(bp);
9304359Smckusick 		return;
9315960Smckusic 	}
9328105Sroot 	cgp->cg_time = time.tv_sec;
9334359Smckusick 	ino %= fs->fs_ipg;
934*34143Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
9356716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9366716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9374359Smckusick 		panic("ifree: freeing free inode");
9386716Smckusick 	}
939*34143Smckusick 	clrbit(cg_inosused(cgp), ino);
94016784Smckusick 	if (ino < cgp->cg_irotor)
94116784Smckusick 		cgp->cg_irotor = ino;
9424792Smckusic 	cgp->cg_cs.cs_nifree++;
9434792Smckusic 	fs->fs_cstotal.cs_nifree++;
9445322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
9454359Smckusick 	if ((mode & IFMT) == IFDIR) {
9464792Smckusic 		cgp->cg_cs.cs_ndir--;
9474792Smckusic 		fs->fs_cstotal.cs_ndir--;
9485322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
9494359Smckusick 	}
9504359Smckusick 	fs->fs_fmod++;
9514359Smckusick 	bdwrite(bp);
9524359Smckusick }
9534359Smckusick 
9544463Smckusic /*
9555375Smckusic  * Find a block of the specified size in the specified cylinder group.
9565375Smckusic  *
9574651Smckusic  * It is a panic if a request is made to find a block if none are
9584651Smckusic  * available.
9594651Smckusic  */
9604651Smckusic daddr_t
9614651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
9624651Smckusic 	register struct fs *fs;
9634651Smckusic 	register struct cg *cgp;
9644651Smckusic 	daddr_t bpref;
9654651Smckusic 	int allocsiz;
9664651Smckusic {
9674651Smckusic 	daddr_t bno;
9684651Smckusic 	int start, len, loc, i;
9694651Smckusic 	int blk, field, subfield, pos;
9704651Smckusic 
9714651Smckusic 	/*
9724651Smckusic 	 * find the fragment by searching through the free block
9734651Smckusic 	 * map for an appropriate bit pattern
9744651Smckusic 	 */
9754651Smckusic 	if (bpref)
9765377Smckusic 		start = dtogd(fs, bpref) / NBBY;
9774651Smckusic 	else
9784651Smckusic 		start = cgp->cg_frotor / NBBY;
9795398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
980*34143Smckusick 	loc = scanc((unsigned)len, (caddr_t)&cg_blksfree(cgp)[start],
98112755Ssam 		(caddr_t)fragtbl[fs->fs_frag],
98212755Ssam 		(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
9834651Smckusic 	if (loc == 0) {
9846531Smckusick 		len = start + 1;
9856531Smckusick 		start = 0;
986*34143Smckusick 		loc = scanc((unsigned)len, (caddr_t)&cg_blksfree(cgp)[0],
98712755Ssam 			(caddr_t)fragtbl[fs->fs_frag],
98812755Ssam 			(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
98916784Smckusick 		if (loc == 0) {
99016784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
99116784Smckusick 			    start, len, fs->fs_fsmnt);
99216784Smckusick 			panic("alloccg: map corrupted");
99317697Smckusick 			/* NOTREACHED */
99416784Smckusick 		}
9954651Smckusic 	}
9964651Smckusic 	bno = (start + len - loc) * NBBY;
9974651Smckusic 	cgp->cg_frotor = bno;
9984651Smckusic 	/*
9994651Smckusic 	 * found the byte in the map
10004651Smckusic 	 * sift through the bits to find the selected frag
10014651Smckusic 	 */
10026294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
1003*34143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
10044651Smckusic 		blk <<= 1;
10054651Smckusic 		field = around[allocsiz];
10064651Smckusic 		subfield = inside[allocsiz];
10075322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10086294Smckusick 			if ((blk & field) == subfield)
10096294Smckusick 				return (bno + pos);
10104651Smckusic 			field <<= 1;
10114651Smckusic 			subfield <<= 1;
10124651Smckusic 		}
10134651Smckusic 	}
10146716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
10154651Smckusic 	panic("alloccg: block not in map");
10166531Smckusick 	return (-1);
10174651Smckusic }
10184651Smckusic 
10194651Smckusic /*
10205375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10215375Smckusic  *
10225375Smckusic  * The form of the error message is:
10234359Smckusick  *	fs: error message
10244359Smckusick  */
10254359Smckusick fserr(fs, cp)
10264359Smckusick 	struct fs *fs;
10274359Smckusick 	char *cp;
10284359Smckusick {
10294359Smckusick 
103024839Seric 	log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp);
10314359Smckusick }
1032