xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 34857)
123394Smckusick /*
229113Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
334432Sbostic  * All rights reserved.
423394Smckusick  *
534432Sbostic  * Redistribution and use in source and binary forms are permitted
6*34857Sbostic  * provided that the above copyright notice and this paragraph are
7*34857Sbostic  * duplicated in all such forms and that any documentation,
8*34857Sbostic  * advertising materials, and other materials related to such
9*34857Sbostic  * distribution and use acknowledge that the software was developed
10*34857Sbostic  * by the University of California, Berkeley.  The name of the
11*34857Sbostic  * University may not be used to endorse or promote products derived
12*34857Sbostic  * from this software without specific prior written permission.
13*34857Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34857Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34857Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1634432Sbostic  *
17*34857Sbostic  *	@(#)lfs_alloc.c	7.8 (Berkeley) 06/29/88
1823394Smckusick  */
194359Smckusick 
2017097Sbloom #include "param.h"
2117097Sbloom #include "systm.h"
2217097Sbloom #include "mount.h"
2317097Sbloom #include "fs.h"
2417097Sbloom #include "buf.h"
2517097Sbloom #include "inode.h"
2617097Sbloom #include "dir.h"
2717097Sbloom #include "user.h"
2817097Sbloom #include "quota.h"
2917097Sbloom #include "kernel.h"
3018307Sralph #include "syslog.h"
3126253Skarels #include "cmap.h"
324359Smckusick 
335212Smckusic extern u_long		hashalloc();
349163Ssam extern ino_t		ialloccg();
359163Ssam extern daddr_t		alloccg();
364651Smckusic extern daddr_t		alloccgblk();
374651Smckusic extern daddr_t		fragextend();
384651Smckusic extern daddr_t		blkpref();
394651Smckusic extern daddr_t		mapsearch();
404607Smckusic extern int		inside[], around[];
415322Smckusic extern unsigned char	*fragtbl[];
424359Smckusick 
435375Smckusic /*
445375Smckusic  * Allocate a block in the file system.
455375Smckusic  *
465375Smckusic  * The size of the requested block is given, which must be some
475375Smckusic  * multiple of fs_fsize and <= fs_bsize.
485375Smckusic  * A preference may be optionally specified. If a preference is given
495375Smckusic  * the following hierarchy is used to allocate a block:
505375Smckusic  *   1) allocate the requested block.
515375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
525375Smckusic  *   3) allocate a block in the same cylinder group.
535375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
545375Smckusic  *      available block is located.
555375Smckusic  * If no block preference is given the following heirarchy is used
565375Smckusic  * to allocate a block:
575375Smckusic  *   1) allocate a block in the cylinder group that contains the
585375Smckusic  *      inode for the file.
595375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
605375Smckusic  *      available block is located.
615375Smckusic  */
624359Smckusick struct buf *
635965Smckusic alloc(ip, bpref, size)
644463Smckusic 	register struct inode *ip;
654359Smckusick 	daddr_t bpref;
664359Smckusick 	int size;
674359Smckusick {
684359Smckusick 	daddr_t bno;
694359Smckusick 	register struct fs *fs;
704463Smckusic 	register struct buf *bp;
714359Smckusick 	int cg;
724359Smckusick 
735965Smckusic 	fs = ip->i_fs;
746716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
756716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
766716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
774463Smckusic 		panic("alloc: bad size");
786716Smckusick 	}
795322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
804359Smckusick 		goto nospace;
8111638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
824792Smckusic 		goto nospace;
837650Ssam #ifdef QUOTA
8412643Ssam 	u.u_error = chkdq(ip, (long)btodb(size), 0);
8512643Ssam 	if (u.u_error)
8612643Ssam 		return (NULL);
877483Skre #endif
884948Smckusic 	if (bpref >= fs->fs_size)
894948Smckusic 		bpref = 0;
904359Smckusick 	if (bpref == 0)
915377Smckusic 		cg = itog(fs, ip->i_number);
924359Smckusick 	else
935377Smckusic 		cg = dtog(fs, bpref);
949163Ssam 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
959163Ssam 		(u_long (*)())alloccg);
966567Smckusic 	if (bno <= 0)
974359Smckusick 		goto nospace;
9812643Ssam 	ip->i_blocks += btodb(size);
9912643Ssam 	ip->i_flag |= IUPD|ICHG;
1005965Smckusic 	bp = getblk(ip->i_dev, fsbtodb(fs, bno), size);
1014359Smckusick 	clrbuf(bp);
1024359Smckusick 	return (bp);
1034359Smckusick nospace:
1044359Smckusick 	fserr(fs, "file system full");
1054359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
1064359Smckusick 	u.u_error = ENOSPC;
1074359Smckusick 	return (NULL);
1084359Smckusick }
1094359Smckusick 
1105375Smckusic /*
1115375Smckusic  * Reallocate a fragment to a bigger size
1125375Smckusic  *
1135375Smckusic  * The number and size of the old block is given, and a preference
1145375Smckusic  * and new size is also specified. The allocator attempts to extend
1155375Smckusic  * the original block. Failing that, the regular block allocator is
1165375Smckusic  * invoked to get an appropriate block.
1175375Smckusic  */
1184426Smckusic struct buf *
1195965Smckusic realloccg(ip, bprev, bpref, osize, nsize)
1205965Smckusic 	register struct inode *ip;
1214651Smckusic 	daddr_t bprev, bpref;
1224426Smckusic 	int osize, nsize;
1234426Smckusic {
1244426Smckusic 	register struct fs *fs;
1254463Smckusic 	register struct buf *bp, *obp;
12624698Smckusick 	int cg, request;
12725471Smckusick 	daddr_t bno, bn;
12832664Smckusick 	int i, count;
1294426Smckusic 
1305965Smckusic 	fs = ip->i_fs;
1315960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1326716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1336716Smckusick 		printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1346716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
1354463Smckusic 		panic("realloccg: bad size");
1366716Smckusick 	}
13711638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1384792Smckusic 		goto nospace;
1396716Smckusick 	if (bprev == 0) {
1406716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1416716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1424463Smckusic 		panic("realloccg: bad bprev");
1436716Smckusick 	}
1447650Ssam #ifdef QUOTA
14512643Ssam 	u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0);
14612643Ssam 	if (u.u_error)
14712643Ssam 		return (NULL);
1487483Skre #endif
1496294Smckusick 	cg = dtog(fs, bprev);
1505965Smckusic 	bno = fragextend(ip, cg, (long)bprev, osize, nsize);
1514463Smckusic 	if (bno != 0) {
1527187Sroot 		do {
1537187Sroot 			bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
1547187Sroot 			if (bp->b_flags & B_ERROR) {
1557187Sroot 				brelse(bp);
1567187Sroot 				return (NULL);
1577187Sroot 			}
1587187Sroot 		} while (brealloc(bp, nsize) == 0);
1597187Sroot 		bp->b_flags |= B_DONE;
1609163Ssam 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
16112643Ssam 		ip->i_blocks += btodb(nsize - osize);
16212643Ssam 		ip->i_flag |= IUPD|ICHG;
1634463Smckusic 		return (bp);
1644463Smckusic 	}
1654948Smckusic 	if (bpref >= fs->fs_size)
1664948Smckusic 		bpref = 0;
16726253Skarels 	switch ((int)fs->fs_optim) {
16825256Smckusick 	case FS_OPTSPACE:
16925256Smckusick 		/*
17025256Smckusick 		 * Allocate an exact sized fragment. Although this makes
17125256Smckusick 		 * best use of space, we will waste time relocating it if
17225256Smckusick 		 * the file continues to grow. If the fragmentation is
17325256Smckusick 		 * less than half of the minimum free reserve, we choose
17425256Smckusick 		 * to begin optimizing for time.
17525256Smckusick 		 */
17624698Smckusick 		request = nsize;
17725256Smckusick 		if (fs->fs_minfree < 5 ||
17825256Smckusick 		    fs->fs_cstotal.cs_nffree >
17925256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
18025256Smckusick 			break;
18125256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
18225256Smckusick 			fs->fs_fsmnt);
18325256Smckusick 		fs->fs_optim = FS_OPTTIME;
18425256Smckusick 		break;
18525256Smckusick 	case FS_OPTTIME:
18625256Smckusick 		/*
18725256Smckusick 		 * At this point we have discovered a file that is trying
18825256Smckusick 		 * to grow a small fragment to a larger fragment. To save
18925256Smckusick 		 * time, we allocate a full sized block, then free the
19025256Smckusick 		 * unused portion. If the file continues to grow, the
19125256Smckusick 		 * `fragextend' call above will be able to grow it in place
19225256Smckusick 		 * without further copying. If aberrant programs cause
19325256Smckusick 		 * disk fragmentation to grow within 2% of the free reserve,
19425256Smckusick 		 * we choose to begin optimizing for space.
19525256Smckusick 		 */
19624698Smckusick 		request = fs->fs_bsize;
19725256Smckusick 		if (fs->fs_cstotal.cs_nffree <
19825256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
19925256Smckusick 			break;
20025256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
20125256Smckusick 			fs->fs_fsmnt);
20225256Smckusick 		fs->fs_optim = FS_OPTSPACE;
20325256Smckusick 		break;
20425256Smckusick 	default:
20525256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
20625256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
20725256Smckusick 		panic("realloccg: bad optim");
20825256Smckusick 		/* NOTREACHED */
20925256Smckusick 	}
21024698Smckusick 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
2119163Ssam 		(u_long (*)())alloccg);
2126567Smckusic 	if (bno > 0) {
2135965Smckusic 		obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
2145960Smckusic 		if (obp->b_flags & B_ERROR) {
2155960Smckusic 			brelse(obp);
2166294Smckusick 			return (NULL);
2175960Smckusic 		}
21825471Smckusick 		bn = fsbtodb(fs, bno);
21925471Smckusick 		bp = getblk(ip->i_dev, bn, nsize);
22017658Smckusick 		bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
22130749Skarels 		count = howmany(osize, CLBYTES);
22230749Skarels 		for (i = 0; i < count; i++)
22330749Skarels 			munhash(ip->i_dev, bn + i * CLBYTES / DEV_BSIZE);
22417658Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
22517658Smckusick 		if (obp->b_flags & B_DELWRI) {
22617658Smckusick 			obp->b_flags &= ~B_DELWRI;
22717658Smckusick 			u.u_ru.ru_oublock--;		/* delete charge */
22817658Smckusick 		}
2294463Smckusic 		brelse(obp);
23031402Smckusick 		blkfree(ip, bprev, (off_t)osize);
23124698Smckusick 		if (nsize < request)
23231402Smckusick 			blkfree(ip, bno + numfrags(fs, nsize),
23324698Smckusick 				(off_t)(request - nsize));
23412643Ssam 		ip->i_blocks += btodb(nsize - osize);
23512643Ssam 		ip->i_flag |= IUPD|ICHG;
2366294Smckusick 		return (bp);
2374463Smckusic 	}
2384792Smckusic nospace:
2394463Smckusic 	/*
2404463Smckusic 	 * no space available
2414463Smckusic 	 */
2424426Smckusic 	fserr(fs, "file system full");
2434426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
2444426Smckusic 	u.u_error = ENOSPC;
2454426Smckusic 	return (NULL);
2464426Smckusic }
2474426Smckusic 
2485375Smckusic /*
2495375Smckusic  * Allocate an inode in the file system.
2505375Smckusic  *
2515375Smckusic  * A preference may be optionally specified. If a preference is given
2525375Smckusic  * the following hierarchy is used to allocate an inode:
2535375Smckusic  *   1) allocate the requested inode.
2545375Smckusic  *   2) allocate an inode in the same cylinder group.
2555375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2565375Smckusic  *      available inode is located.
2575375Smckusic  * If no inode preference is given the following heirarchy is used
2585375Smckusic  * to allocate an inode:
2595375Smckusic  *   1) allocate an inode in cylinder group 0.
2605375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2615375Smckusic  *      available inode is located.
2625375Smckusic  */
2634359Smckusick struct inode *
2645965Smckusic ialloc(pip, ipref, mode)
2655965Smckusic 	register struct inode *pip;
2664359Smckusick 	ino_t ipref;
2674359Smckusick 	int mode;
2684359Smckusick {
2695212Smckusic 	ino_t ino;
2704359Smckusick 	register struct fs *fs;
2714359Smckusick 	register struct inode *ip;
2724359Smckusick 	int cg;
2734359Smckusick 
2745965Smckusic 	fs = pip->i_fs;
2754792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2764359Smckusick 		goto noinodes;
2777650Ssam #ifdef QUOTA
27812643Ssam 	u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
27912643Ssam 	if (u.u_error)
28012643Ssam 		return (NULL);
2817483Skre #endif
2824948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2834948Smckusic 		ipref = 0;
2845377Smckusic 	cg = itog(fs, ipref);
2855965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2864359Smckusick 	if (ino == 0)
2874359Smckusick 		goto noinodes;
2885965Smckusic 	ip = iget(pip->i_dev, pip->i_fs, ino);
2894359Smckusick 	if (ip == NULL) {
29015120Skarels 		ifree(pip, ino, 0);
2914359Smckusick 		return (NULL);
2924359Smckusick 	}
2936716Smckusick 	if (ip->i_mode) {
2946716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
2956716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
2964359Smckusick 		panic("ialloc: dup alloc");
2976716Smckusick 	}
29812643Ssam 	if (ip->i_blocks) {				/* XXX */
29912643Ssam 		printf("free inode %s/%d had %d blocks\n",
30012643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
30112643Ssam 		ip->i_blocks = 0;
30212643Ssam 	}
3034359Smckusick 	return (ip);
3044359Smckusick noinodes:
3054359Smckusick 	fserr(fs, "out of inodes");
3066294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
3074359Smckusick 	u.u_error = ENOSPC;
3084359Smckusick 	return (NULL);
3094359Smckusick }
3104359Smckusick 
3114651Smckusic /*
3125375Smckusic  * Find a cylinder to place a directory.
3135375Smckusic  *
3145375Smckusic  * The policy implemented by this algorithm is to select from
3155375Smckusic  * among those cylinder groups with above the average number of
3165375Smckusic  * free inodes, the one with the smallest number of directories.
3174651Smckusic  */
3189163Ssam ino_t
3195965Smckusic dirpref(fs)
3205965Smckusic 	register struct fs *fs;
3214359Smckusick {
3224651Smckusic 	int cg, minndir, mincg, avgifree;
3234359Smckusick 
3244792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3254651Smckusic 	minndir = fs->fs_ipg;
3264359Smckusick 	mincg = 0;
3274651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3285322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3295322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3304359Smckusick 			mincg = cg;
3315322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3324359Smckusick 		}
3339163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3344359Smckusick }
3354359Smckusick 
3364651Smckusic /*
3379163Ssam  * Select the desired position for the next block in a file.  The file is
3389163Ssam  * logically divided into sections. The first section is composed of the
3399163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3409163Ssam  *
3419163Ssam  * If no blocks have been allocated in the first section, the policy is to
3429163Ssam  * request a block in the same cylinder group as the inode that describes
3439163Ssam  * the file. If no blocks have been allocated in any other section, the
3449163Ssam  * policy is to place the section in a cylinder group with a greater than
3459163Ssam  * average number of free blocks.  An appropriate cylinder group is found
34617696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
34717696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
34817696Smckusick  * cylinder group from which the previous allocation was made. The sweep
34917696Smckusick  * continues until a cylinder group with greater than the average number
35017696Smckusick  * of free blocks is found. If the allocation is for the first block in an
35117696Smckusick  * indirect block, the information on the previous allocation is unavailable;
35217696Smckusick  * here a best guess is made based upon the logical block number being
35317696Smckusick  * allocated.
3549163Ssam  *
3559163Ssam  * If a section is already partially allocated, the policy is to
3569163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3579163Ssam  * contiguous blocks and the beginning of the next is physically separated
3589163Ssam  * so that the disk head will be in transit between them for at least
3599163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3609163Ssam  * schedule another I/O transfer.
3614651Smckusic  */
3625212Smckusic daddr_t
3639163Ssam blkpref(ip, lbn, indx, bap)
3649163Ssam 	struct inode *ip;
3659163Ssam 	daddr_t lbn;
3669163Ssam 	int indx;
3679163Ssam 	daddr_t *bap;
3689163Ssam {
3695965Smckusic 	register struct fs *fs;
37017696Smckusick 	register int cg;
37117696Smckusick 	int avgbfree, startcg;
3729163Ssam 	daddr_t nextblk;
3734651Smckusic 
3749163Ssam 	fs = ip->i_fs;
3759163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3769163Ssam 		if (lbn < NDADDR) {
3779163Ssam 			cg = itog(fs, ip->i_number);
3785322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3794651Smckusic 		}
3809163Ssam 		/*
3819163Ssam 		 * Find a cylinder with greater than average number of
3829163Ssam 		 * unused data blocks.
3839163Ssam 		 */
38417696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
38517696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
38617696Smckusick 		else
38717696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
38817696Smckusick 		startcg %= fs->fs_ncg;
3899163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
39017696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
3919163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3929163Ssam 				fs->fs_cgrotor = cg;
3939163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3949163Ssam 			}
39517696Smckusick 		for (cg = 0; cg <= startcg; cg++)
3969163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
3979163Ssam 				fs->fs_cgrotor = cg;
3989163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
3999163Ssam 			}
4009163Ssam 		return (NULL);
4019163Ssam 	}
4029163Ssam 	/*
4039163Ssam 	 * One or more previous blocks have been laid out. If less
4049163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
4059163Ssam 	 * next block is requested contiguously, otherwise it is
4069163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
4079163Ssam 	 */
4089163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
4099163Ssam 	if (indx > fs->fs_maxcontig &&
41011638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4119163Ssam 	    != nextblk)
4129163Ssam 		return (nextblk);
4139163Ssam 	if (fs->fs_rotdelay != 0)
4149163Ssam 		/*
4159163Ssam 		 * Here we convert ms of delay to frags as:
4169163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4179163Ssam 		 *	((sect/frag) * (ms/sec))
4189163Ssam 		 * then round up to the next block.
4199163Ssam 		 */
4209163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4219163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4229163Ssam 	return (nextblk);
4234651Smckusic }
4244651Smckusic 
4255375Smckusic /*
4265375Smckusic  * Implement the cylinder overflow algorithm.
4275375Smckusic  *
4285375Smckusic  * The policy implemented by this algorithm is:
4295375Smckusic  *   1) allocate the block in its requested cylinder group.
4305375Smckusic  *   2) quadradically rehash on the cylinder group number.
4315375Smckusic  *   3) brute force search for a free block.
4325375Smckusic  */
4335212Smckusic /*VARARGS5*/
4345212Smckusic u_long
4355965Smckusic hashalloc(ip, cg, pref, size, allocator)
4365965Smckusic 	struct inode *ip;
4374359Smckusick 	int cg;
4384359Smckusick 	long pref;
4394359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4405212Smckusic 	u_long (*allocator)();
4414359Smckusick {
4425965Smckusic 	register struct fs *fs;
4434359Smckusick 	long result;
4444359Smckusick 	int i, icg = cg;
4454359Smckusick 
4465965Smckusic 	fs = ip->i_fs;
4474359Smckusick 	/*
4484359Smckusick 	 * 1: preferred cylinder group
4494359Smckusick 	 */
4505965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4514359Smckusick 	if (result)
4524359Smckusick 		return (result);
4534359Smckusick 	/*
4544359Smckusick 	 * 2: quadratic rehash
4554359Smckusick 	 */
4564359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4574359Smckusick 		cg += i;
4584359Smckusick 		if (cg >= fs->fs_ncg)
4594359Smckusick 			cg -= fs->fs_ncg;
4605965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4614359Smckusick 		if (result)
4624359Smckusick 			return (result);
4634359Smckusick 	}
4644359Smckusick 	/*
4654359Smckusick 	 * 3: brute force search
46610847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
46710847Ssam 	 * and 1 is always checked in the quadratic rehash.
4684359Smckusick 	 */
46910848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
47010847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4715965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4724359Smckusick 		if (result)
4734359Smckusick 			return (result);
4744359Smckusick 		cg++;
4754359Smckusick 		if (cg == fs->fs_ncg)
4764359Smckusick 			cg = 0;
4774359Smckusick 	}
4786294Smckusick 	return (NULL);
4794359Smckusick }
4804359Smckusick 
4815375Smckusic /*
4825375Smckusic  * Determine whether a fragment can be extended.
4835375Smckusic  *
4845375Smckusic  * Check to see if the necessary fragments are available, and
4855375Smckusic  * if they are, allocate them.
4865375Smckusic  */
4874359Smckusick daddr_t
4885965Smckusic fragextend(ip, cg, bprev, osize, nsize)
4895965Smckusic 	struct inode *ip;
4904426Smckusic 	int cg;
4914463Smckusic 	long bprev;
4924426Smckusic 	int osize, nsize;
4934426Smckusic {
4945965Smckusic 	register struct fs *fs;
4954463Smckusic 	register struct buf *bp;
4964463Smckusic 	register struct cg *cgp;
4974463Smckusic 	long bno;
4984463Smckusic 	int frags, bbase;
4994426Smckusic 	int i;
5004426Smckusic 
5015965Smckusic 	fs = ip->i_fs;
50217224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
5036531Smckusick 		return (NULL);
5045960Smckusic 	frags = numfrags(fs, nsize);
50517224Smckusick 	bbase = fragnum(fs, bprev);
50617224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
50730749Skarels 		/* cannot extend across a block boundary */
5086294Smckusick 		return (NULL);
5094463Smckusic 	}
51010278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
5116531Smckusick 	cgp = bp->b_un.b_cg;
51234143Smckusick 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
5135960Smckusic 		brelse(bp);
5146294Smckusick 		return (NULL);
5155960Smckusic 	}
5168105Sroot 	cgp->cg_time = time.tv_sec;
5175377Smckusic 	bno = dtogd(fs, bprev);
5185960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
51934143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
5205361Smckusic 			brelse(bp);
5216294Smckusick 			return (NULL);
5225361Smckusic 		}
5235361Smckusic 	/*
5245361Smckusic 	 * the current fragment can be extended
5255361Smckusic 	 * deduct the count on fragment being extended into
5265361Smckusic 	 * increase the count on the remaining fragment (if any)
5275361Smckusic 	 * allocate the extended piece
5285361Smckusic 	 */
5295361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
53034143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
5314463Smckusic 			break;
5325960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5335361Smckusic 	if (i != frags)
5345361Smckusic 		cgp->cg_frsum[i - frags]++;
5355960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
53634143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
5375361Smckusic 		cgp->cg_cs.cs_nffree--;
5385361Smckusic 		fs->fs_cstotal.cs_nffree--;
5395361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5404463Smckusic 	}
5415361Smckusic 	fs->fs_fmod++;
5425361Smckusic 	bdwrite(bp);
5435361Smckusic 	return (bprev);
5444426Smckusic }
5454426Smckusic 
5465375Smckusic /*
5475375Smckusic  * Determine whether a block can be allocated.
5485375Smckusic  *
5495375Smckusic  * Check to see if a block of the apprpriate size is available,
5505375Smckusic  * and if it is, allocate it.
5515375Smckusic  */
5529163Ssam daddr_t
5535965Smckusic alloccg(ip, cg, bpref, size)
5545965Smckusic 	struct inode *ip;
5554359Smckusick 	int cg;
5564359Smckusick 	daddr_t bpref;
5574359Smckusick 	int size;
5584359Smckusick {
5595965Smckusic 	register struct fs *fs;
5604463Smckusic 	register struct buf *bp;
5614463Smckusic 	register struct cg *cgp;
5624463Smckusic 	int bno, frags;
5634463Smckusic 	int allocsiz;
5644463Smckusic 	register int i;
5654359Smckusick 
5665965Smckusic 	fs = ip->i_fs;
5675322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5686294Smckusick 		return (NULL);
56910278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
5706531Smckusick 	cgp = bp->b_un.b_cg;
57134143Smckusick 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
57215950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5735960Smckusic 		brelse(bp);
5746294Smckusick 		return (NULL);
5755960Smckusic 	}
5768105Sroot 	cgp->cg_time = time.tv_sec;
5775322Smckusic 	if (size == fs->fs_bsize) {
5785212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5794463Smckusic 		bdwrite(bp);
5804463Smckusic 		return (bno);
5814463Smckusic 	}
5824463Smckusic 	/*
5834463Smckusic 	 * check to see if any fragments are already available
5844463Smckusic 	 * allocsiz is the size which will be allocated, hacking
5854463Smckusic 	 * it down to a smaller size if necessary
5864463Smckusic 	 */
5875960Smckusic 	frags = numfrags(fs, size);
5885322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
5894463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
5904463Smckusic 			break;
5915322Smckusic 	if (allocsiz == fs->fs_frag) {
5924463Smckusic 		/*
5934463Smckusic 		 * no fragments were available, so a block will be
5944463Smckusic 		 * allocated, and hacked up
5954463Smckusic 		 */
5964792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
5974463Smckusic 			brelse(bp);
5986294Smckusick 			return (NULL);
5994463Smckusic 		}
6005212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
6015377Smckusic 		bpref = dtogd(fs, bno);
6025322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
60334143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
6045322Smckusic 		i = fs->fs_frag - frags;
6054792Smckusic 		cgp->cg_cs.cs_nffree += i;
6064792Smckusic 		fs->fs_cstotal.cs_nffree += i;
6075322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
6089762Ssam 		fs->fs_fmod++;
6094463Smckusic 		cgp->cg_frsum[i]++;
6104463Smckusic 		bdwrite(bp);
6114463Smckusic 		return (bno);
6124463Smckusic 	}
6134651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
61415950Smckusick 	if (bno < 0) {
61515950Smckusick 		brelse(bp);
6166294Smckusick 		return (NULL);
61715950Smckusick 	}
6184463Smckusic 	for (i = 0; i < frags; i++)
61934143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
6204792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6214792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6225322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
6239762Ssam 	fs->fs_fmod++;
6244463Smckusic 	cgp->cg_frsum[allocsiz]--;
6254463Smckusic 	if (frags != allocsiz)
6264463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6274463Smckusic 	bdwrite(bp);
6284463Smckusic 	return (cg * fs->fs_fpg + bno);
6294463Smckusic }
6304463Smckusic 
6315375Smckusic /*
6325375Smckusic  * Allocate a block in a cylinder group.
6335375Smckusic  *
6345375Smckusic  * This algorithm implements the following policy:
6355375Smckusic  *   1) allocate the requested block.
6365375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6375375Smckusic  *   3) allocate the next available block on the block rotor for the
6385375Smckusic  *      specified cylinder group.
6395375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6405375Smckusic  * blocks may be fragmented by the routine that allocates them.
6415375Smckusic  */
6424463Smckusic daddr_t
6435212Smckusic alloccgblk(fs, cgp, bpref)
6445965Smckusic 	register struct fs *fs;
6454463Smckusic 	register struct cg *cgp;
6464463Smckusic 	daddr_t bpref;
6474463Smckusic {
6484651Smckusic 	daddr_t bno;
6496294Smckusick 	int cylno, pos, delta;
6504651Smckusic 	short *cylbp;
6515361Smckusic 	register int i;
6524463Smckusic 
6534651Smckusic 	if (bpref == 0) {
6544651Smckusic 		bpref = cgp->cg_rotor;
6555361Smckusic 		goto norot;
6565361Smckusic 	}
65717224Smckusick 	bpref = blknum(fs, bpref);
6585377Smckusic 	bpref = dtogd(fs, bpref);
6595361Smckusic 	/*
6605361Smckusic 	 * if the requested block is available, use it
6615361Smckusic 	 */
66234143Smckusick 	if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
6635361Smckusic 		bno = bpref;
6645361Smckusic 		goto gotit;
6655361Smckusic 	}
6665361Smckusic 	/*
6675361Smckusic 	 * check for a block available on the same cylinder
6685361Smckusic 	 */
6695361Smckusic 	cylno = cbtocylno(fs, bpref);
67034143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
6715375Smckusic 		goto norot;
6725375Smckusic 	if (fs->fs_cpc == 0) {
6735375Smckusic 		/*
6745375Smckusic 		 * block layout info is not available, so just have
6755375Smckusic 		 * to take any block in this cylinder.
6765375Smckusic 		 */
6775375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
6785375Smckusic 		goto norot;
6795375Smckusic 	}
6805375Smckusic 	/*
6815361Smckusic 	 * check the summary information to see if a block is
6825361Smckusic 	 * available in the requested cylinder starting at the
6839163Ssam 	 * requested rotational position and proceeding around.
6845361Smckusic 	 */
68534143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
6869163Ssam 	pos = cbtorpos(fs, bpref);
68734143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
6885361Smckusic 		if (cylbp[i] > 0)
6895361Smckusic 			break;
69034143Smckusick 	if (i == fs->fs_nrpos)
6915361Smckusic 		for (i = 0; i < pos; i++)
6925361Smckusic 			if (cylbp[i] > 0)
6935361Smckusic 				break;
6945361Smckusic 	if (cylbp[i] > 0) {
6954651Smckusic 		/*
6965361Smckusic 		 * found a rotational position, now find the actual
6975361Smckusic 		 * block. A panic if none is actually there.
6984651Smckusic 		 */
6995361Smckusic 		pos = cylno % fs->fs_cpc;
7005361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
70134143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
7026716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
7036716Smckusick 			    pos, i, fs->fs_fsmnt);
7045361Smckusic 			panic("alloccgblk: cyl groups corrupted");
7056716Smckusick 		}
70634143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
70734143Smckusick 			if (isblock(fs, cg_blksfree(cgp), bno + i)) {
70811638Ssam 				bno = blkstofrags(fs, (bno + i));
7095361Smckusic 				goto gotit;
7105361Smckusic 			}
71134143Smckusick 			delta = fs_rotbl(fs)[i];
71234143Smckusick 			if (delta <= 0 ||
71334143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
7144651Smckusic 				break;
7156294Smckusick 			i += delta;
7164651Smckusic 		}
7176716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
7185361Smckusic 		panic("alloccgblk: can't find blk in cyl");
7194359Smckusick 	}
7205361Smckusic norot:
7215361Smckusic 	/*
7225361Smckusic 	 * no blocks in the requested cylinder, so take next
7235361Smckusic 	 * available one in this cylinder group.
7245361Smckusic 	 */
7258628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7266567Smckusic 	if (bno < 0)
7276294Smckusick 		return (NULL);
7284651Smckusic 	cgp->cg_rotor = bno;
7294359Smckusick gotit:
73034143Smckusick 	clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno));
7314792Smckusic 	cgp->cg_cs.cs_nbfree--;
7324792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7335322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7345375Smckusic 	cylno = cbtocylno(fs, bno);
73534143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
73634143Smckusick 	cg_blktot(cgp)[cylno]--;
7374359Smckusick 	fs->fs_fmod++;
7384651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7394359Smckusick }
74034143Smckusick 
7415375Smckusic /*
7425375Smckusic  * Determine whether an inode can be allocated.
7435375Smckusic  *
7445375Smckusic  * Check to see if an inode is available, and if it is,
7455375Smckusic  * allocate it using the following policy:
7465375Smckusic  *   1) allocate the requested inode.
7475375Smckusic  *   2) allocate the next available inode after the requested
7485375Smckusic  *      inode in the specified cylinder group.
7495375Smckusic  */
7509163Ssam ino_t
7515965Smckusic ialloccg(ip, cg, ipref, mode)
7525965Smckusic 	struct inode *ip;
7534359Smckusick 	int cg;
7544359Smckusick 	daddr_t ipref;
7554359Smckusick 	int mode;
7564359Smckusick {
7575965Smckusic 	register struct fs *fs;
7584463Smckusic 	register struct cg *cgp;
75916784Smckusick 	struct buf *bp;
76016784Smckusick 	int start, len, loc, map, i;
7614359Smckusick 
7625965Smckusic 	fs = ip->i_fs;
7635322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7646294Smckusick 		return (NULL);
76510278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
7666531Smckusick 	cgp = bp->b_un.b_cg;
76734143Smckusick 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
76815950Smckusick 	    cgp->cg_cs.cs_nifree == 0) {
7695960Smckusic 		brelse(bp);
7706294Smckusick 		return (NULL);
7715960Smckusic 	}
7728105Sroot 	cgp->cg_time = time.tv_sec;
7734359Smckusick 	if (ipref) {
7744359Smckusick 		ipref %= fs->fs_ipg;
77534143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
7764359Smckusick 			goto gotit;
77716784Smckusick 	}
77816784Smckusick 	start = cgp->cg_irotor / NBBY;
77916784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
78034143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
78116784Smckusick 	if (loc == 0) {
78217697Smckusick 		len = start + 1;
78317697Smckusick 		start = 0;
78434143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
78517697Smckusick 		if (loc == 0) {
78617697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
78717697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
78817697Smckusick 			panic("ialloccg: map corrupted");
78917697Smckusick 			/* NOTREACHED */
79017697Smckusick 		}
79116784Smckusick 	}
79216784Smckusick 	i = start + len - loc;
79334143Smckusick 	map = cg_inosused(cgp)[i];
79416784Smckusick 	ipref = i * NBBY;
79516784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
79616784Smckusick 		if ((map & i) == 0) {
7974359Smckusick 			cgp->cg_irotor = ipref;
7984359Smckusick 			goto gotit;
7994359Smckusick 		}
8004359Smckusick 	}
80116784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
80216784Smckusick 	panic("ialloccg: block not in map");
80316784Smckusick 	/* NOTREACHED */
8044359Smckusick gotit:
80534143Smckusick 	setbit(cg_inosused(cgp), ipref);
8064792Smckusic 	cgp->cg_cs.cs_nifree--;
8074792Smckusic 	fs->fs_cstotal.cs_nifree--;
8085322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
8094359Smckusick 	fs->fs_fmod++;
8104359Smckusick 	if ((mode & IFMT) == IFDIR) {
8114792Smckusic 		cgp->cg_cs.cs_ndir++;
8124792Smckusic 		fs->fs_cstotal.cs_ndir++;
8135322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8144359Smckusick 	}
8154359Smckusick 	bdwrite(bp);
8164359Smckusick 	return (cg * fs->fs_ipg + ipref);
8174359Smckusick }
8184359Smckusick 
8195375Smckusic /*
8205375Smckusic  * Free a block or fragment.
8215375Smckusic  *
8225375Smckusic  * The specified block or fragment is placed back in the
8235375Smckusic  * free map. If a fragment is deallocated, a possible
8245375Smckusic  * block reassembly is checked.
8255375Smckusic  */
82631402Smckusick blkfree(ip, bno, size)
8275965Smckusic 	register struct inode *ip;
8284359Smckusick 	daddr_t bno;
8295212Smckusic 	off_t size;
8304359Smckusick {
8314359Smckusick 	register struct fs *fs;
8324359Smckusick 	register struct cg *cgp;
8334359Smckusick 	register struct buf *bp;
8344463Smckusic 	int cg, blk, frags, bbase;
8354463Smckusic 	register int i;
8364359Smckusick 
8375965Smckusic 	fs = ip->i_fs;
8386716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8396716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8406716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
84131402Smckusick 		panic("blkfree: bad size");
8426716Smckusick 	}
8435377Smckusic 	cg = dtog(fs, bno);
8446567Smckusic 	if (badblock(fs, bno)) {
8456567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
8464359Smckusick 		return;
8476567Smckusic 	}
84810278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
8496531Smckusick 	cgp = bp->b_un.b_cg;
85034143Smckusick 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
8515960Smckusic 		brelse(bp);
8524359Smckusick 		return;
8535960Smckusic 	}
8548105Sroot 	cgp->cg_time = time.tv_sec;
8555377Smckusic 	bno = dtogd(fs, bno);
8565322Smckusic 	if (size == fs->fs_bsize) {
85734143Smckusick 		if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) {
8586716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
8596716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
86031402Smckusick 			panic("blkfree: freeing free block");
8616567Smckusic 		}
86234143Smckusick 		setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
8634792Smckusic 		cgp->cg_cs.cs_nbfree++;
8644792Smckusic 		fs->fs_cstotal.cs_nbfree++;
8655322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
8665375Smckusic 		i = cbtocylno(fs, bno);
86734143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
86834143Smckusick 		cg_blktot(cgp)[i]++;
8694426Smckusic 	} else {
87017224Smckusick 		bbase = bno - fragnum(fs, bno);
8714463Smckusic 		/*
8724463Smckusic 		 * decrement the counts associated with the old frags
8734463Smckusic 		 */
87434143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
8755322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
8764463Smckusic 		/*
8774463Smckusic 		 * deallocate the fragment
8784463Smckusic 		 */
8795960Smckusic 		frags = numfrags(fs, size);
8804463Smckusic 		for (i = 0; i < frags; i++) {
88134143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
8826716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
8836716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
88431402Smckusick 				panic("blkfree: freeing free frag");
8856716Smckusick 			}
88634143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
8874426Smckusic 		}
8886294Smckusick 		cgp->cg_cs.cs_nffree += i;
8896294Smckusick 		fs->fs_cstotal.cs_nffree += i;
8906294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
8914463Smckusic 		/*
8924463Smckusic 		 * add back in counts associated with the new frags
8934463Smckusic 		 */
89434143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
8955322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
8964463Smckusic 		/*
8974463Smckusic 		 * if a complete block has been reassembled, account for it
8984463Smckusic 		 */
89934476Smckusick 		if (isblock(fs, cg_blksfree(cgp),
90034476Smckusick 		    (daddr_t)fragstoblks(fs, bbase))) {
9015322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
9025322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
9035322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
9044792Smckusic 			cgp->cg_cs.cs_nbfree++;
9054792Smckusic 			fs->fs_cstotal.cs_nbfree++;
9065322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
9075375Smckusic 			i = cbtocylno(fs, bbase);
90834143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
90934143Smckusick 			cg_blktot(cgp)[i]++;
9104426Smckusic 		}
9114426Smckusic 	}
9124359Smckusick 	fs->fs_fmod++;
9134359Smckusick 	bdwrite(bp);
9144359Smckusick }
9154359Smckusick 
9165375Smckusic /*
9175375Smckusic  * Free an inode.
9185375Smckusic  *
9195375Smckusic  * The specified inode is placed back in the free map.
9205375Smckusic  */
9215965Smckusic ifree(ip, ino, mode)
9225965Smckusic 	struct inode *ip;
9234359Smckusick 	ino_t ino;
9244359Smckusick 	int mode;
9254359Smckusick {
9264359Smckusick 	register struct fs *fs;
9274359Smckusick 	register struct cg *cgp;
9284359Smckusick 	register struct buf *bp;
9294359Smckusick 	int cg;
9304359Smckusick 
9315965Smckusic 	fs = ip->i_fs;
9326716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
9336716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9346716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9354359Smckusick 		panic("ifree: range");
9366716Smckusick 	}
9375377Smckusic 	cg = itog(fs, ino);
93810278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
9396531Smckusick 	cgp = bp->b_un.b_cg;
94034143Smckusick 	if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
9415960Smckusic 		brelse(bp);
9424359Smckusick 		return;
9435960Smckusic 	}
9448105Sroot 	cgp->cg_time = time.tv_sec;
9454359Smckusick 	ino %= fs->fs_ipg;
94634143Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
9476716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9486716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9494359Smckusick 		panic("ifree: freeing free inode");
9506716Smckusick 	}
95134143Smckusick 	clrbit(cg_inosused(cgp), ino);
95216784Smckusick 	if (ino < cgp->cg_irotor)
95316784Smckusick 		cgp->cg_irotor = ino;
9544792Smckusic 	cgp->cg_cs.cs_nifree++;
9554792Smckusic 	fs->fs_cstotal.cs_nifree++;
9565322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
9574359Smckusick 	if ((mode & IFMT) == IFDIR) {
9584792Smckusic 		cgp->cg_cs.cs_ndir--;
9594792Smckusic 		fs->fs_cstotal.cs_ndir--;
9605322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
9614359Smckusick 	}
9624359Smckusick 	fs->fs_fmod++;
9634359Smckusick 	bdwrite(bp);
9644359Smckusick }
9654359Smckusick 
9664463Smckusic /*
9675375Smckusic  * Find a block of the specified size in the specified cylinder group.
9685375Smckusic  *
9694651Smckusic  * It is a panic if a request is made to find a block if none are
9704651Smckusic  * available.
9714651Smckusic  */
9724651Smckusic daddr_t
9734651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
9744651Smckusic 	register struct fs *fs;
9754651Smckusic 	register struct cg *cgp;
9764651Smckusic 	daddr_t bpref;
9774651Smckusic 	int allocsiz;
9784651Smckusic {
9794651Smckusic 	daddr_t bno;
9804651Smckusic 	int start, len, loc, i;
9814651Smckusic 	int blk, field, subfield, pos;
9824651Smckusic 
9834651Smckusic 	/*
9844651Smckusic 	 * find the fragment by searching through the free block
9854651Smckusic 	 * map for an appropriate bit pattern
9864651Smckusic 	 */
9874651Smckusic 	if (bpref)
9885377Smckusic 		start = dtogd(fs, bpref) / NBBY;
9894651Smckusic 	else
9904651Smckusic 		start = cgp->cg_frotor / NBBY;
9915398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
99234476Smckusick 	loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start],
99334476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
99434476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
9954651Smckusic 	if (loc == 0) {
9966531Smckusick 		len = start + 1;
9976531Smckusick 		start = 0;
99834476Smckusick 		loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0],
99934476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
100034476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
100116784Smckusick 		if (loc == 0) {
100216784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
100316784Smckusick 			    start, len, fs->fs_fsmnt);
100416784Smckusick 			panic("alloccg: map corrupted");
100517697Smckusick 			/* NOTREACHED */
100616784Smckusick 		}
10074651Smckusic 	}
10084651Smckusic 	bno = (start + len - loc) * NBBY;
10094651Smckusic 	cgp->cg_frotor = bno;
10104651Smckusic 	/*
10114651Smckusic 	 * found the byte in the map
10124651Smckusic 	 * sift through the bits to find the selected frag
10134651Smckusic 	 */
10146294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
101534143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
10164651Smckusic 		blk <<= 1;
10174651Smckusic 		field = around[allocsiz];
10184651Smckusic 		subfield = inside[allocsiz];
10195322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10206294Smckusick 			if ((blk & field) == subfield)
10216294Smckusick 				return (bno + pos);
10224651Smckusic 			field <<= 1;
10234651Smckusic 			subfield <<= 1;
10244651Smckusic 		}
10254651Smckusic 	}
10266716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
10274651Smckusic 	panic("alloccg: block not in map");
10286531Smckusick 	return (-1);
10294651Smckusic }
10304651Smckusic 
10314651Smckusic /*
10325375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10335375Smckusic  *
10345375Smckusic  * The form of the error message is:
10354359Smckusick  *	fs: error message
10364359Smckusick  */
10374359Smckusick fserr(fs, cp)
10384359Smckusick 	struct fs *fs;
10394359Smckusick 	char *cp;
10404359Smckusick {
10414359Smckusick 
104224839Seric 	log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp);
10434359Smckusick }
1044