xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 38776)
123394Smckusick /*
237735Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
334432Sbostic  * All rights reserved.
423394Smckusick  *
534432Sbostic  * Redistribution and use in source and binary forms are permitted
634857Sbostic  * provided that the above copyright notice and this paragraph are
734857Sbostic  * duplicated in all such forms and that any documentation,
834857Sbostic  * advertising materials, and other materials related to such
934857Sbostic  * distribution and use acknowledge that the software was developed
1034857Sbostic  * by the University of California, Berkeley.  The name of the
1134857Sbostic  * University may not be used to endorse or promote products derived
1234857Sbostic  * from this software without specific prior written permission.
1334857Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434857Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534857Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1634432Sbostic  *
17*38776Smckusick  *	@(#)lfs_alloc.c	7.11 (Berkeley) 08/26/89
1823394Smckusick  */
194359Smckusick 
2017097Sbloom #include "param.h"
2117097Sbloom #include "systm.h"
2217097Sbloom #include "mount.h"
2317097Sbloom #include "buf.h"
2417097Sbloom #include "user.h"
2537735Smckusick #include "vnode.h"
2617097Sbloom #include "kernel.h"
2718307Sralph #include "syslog.h"
2826253Skarels #include "cmap.h"
2937735Smckusick #include "../ufs/quota.h"
3037735Smckusick #include "../ufs/inode.h"
3137735Smckusick #include "../ufs/fs.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  */
6237735Smckusick alloc(ip, bpref, size, bpp, flags)
634463Smckusic 	register struct inode *ip;
644359Smckusick 	daddr_t bpref;
654359Smckusick 	int size;
6637735Smckusick 	struct buf **bpp;
6737735Smckusick 	int flags;
684359Smckusick {
694359Smckusick 	daddr_t bno;
704359Smckusick 	register struct fs *fs;
714463Smckusic 	register struct buf *bp;
7237735Smckusick 	int cg, error;
734359Smckusick 
7437735Smckusick 	*bpp = 0;
755965Smckusic 	fs = ip->i_fs;
766716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
776716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
786716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
794463Smckusic 		panic("alloc: bad size");
806716Smckusick 	}
815322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
824359Smckusick 		goto nospace;
8311638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
844792Smckusic 		goto nospace;
857650Ssam #ifdef QUOTA
8637735Smckusick 	if (error = chkdq(ip, (long)btodb(size), 0))
8737735Smckusick 		return (error);
887483Skre #endif
894948Smckusic 	if (bpref >= fs->fs_size)
904948Smckusic 		bpref = 0;
914359Smckusick 	if (bpref == 0)
925377Smckusic 		cg = itog(fs, ip->i_number);
934359Smckusick 	else
945377Smckusic 		cg = dtog(fs, bpref);
959163Ssam 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
969163Ssam 		(u_long (*)())alloccg);
976567Smckusic 	if (bno <= 0)
984359Smckusick 		goto nospace;
9912643Ssam 	ip->i_blocks += btodb(size);
10012643Ssam 	ip->i_flag |= IUPD|ICHG;
10137735Smckusick 	bp = getblk(ip->i_devvp, fsbtodb(fs, bno), size);
10237735Smckusick 	if (flags & B_CLRBUF)
10337735Smckusick 		clrbuf(bp);
10437735Smckusick 	*bpp = bp;
10537735Smckusick 	return (0);
1064359Smckusick nospace:
1074359Smckusick 	fserr(fs, "file system full");
1084359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
10937735Smckusick 	return (ENOSPC);
1104359Smckusick }
1114359Smckusick 
1125375Smckusic /*
1135375Smckusic  * Reallocate a fragment to a bigger size
1145375Smckusic  *
1155375Smckusic  * The number and size of the old block is given, and a preference
1165375Smckusic  * and new size is also specified. The allocator attempts to extend
1175375Smckusic  * the original block. Failing that, the regular block allocator is
1185375Smckusic  * invoked to get an appropriate block.
1195375Smckusic  */
12037735Smckusick realloccg(ip, bprev, bpref, osize, nsize, bpp)
1215965Smckusic 	register struct inode *ip;
1224651Smckusic 	daddr_t bprev, bpref;
1234426Smckusic 	int osize, nsize;
12437735Smckusick 	struct buf **bpp;
1254426Smckusic {
1264426Smckusic 	register struct fs *fs;
12737735Smckusick 	struct buf *bp, *obp;
12824698Smckusick 	int cg, request;
12925471Smckusick 	daddr_t bno, bn;
13037735Smckusick 	int i, error, count;
1314426Smckusic 
13237735Smckusick 	*bpp = 0;
1335965Smckusic 	fs = ip->i_fs;
1345960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1356716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1366716Smckusick 		printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1376716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
1384463Smckusic 		panic("realloccg: bad size");
1396716Smckusick 	}
14011638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1414792Smckusic 		goto nospace;
1426716Smckusick 	if (bprev == 0) {
1436716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1446716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1454463Smckusic 		panic("realloccg: bad bprev");
1466716Smckusick 	}
1477650Ssam #ifdef QUOTA
14837735Smckusick 	if (error = chkdq(ip, (long)btodb(nsize - osize), 0))
14937735Smckusick 		return (error);
1507483Skre #endif
1516294Smckusick 	cg = dtog(fs, bprev);
1525965Smckusic 	bno = fragextend(ip, cg, (long)bprev, osize, nsize);
1534463Smckusic 	if (bno != 0) {
1547187Sroot 		do {
15537735Smckusick 			error = bread(ip->i_devvp, fsbtodb(fs, bno),
156*38776Smckusick 				osize, NOCRED, &bp);
15737735Smckusick 			if (error) {
1587187Sroot 				brelse(bp);
15937735Smckusick 				return (error);
1607187Sroot 			}
1617187Sroot 		} while (brealloc(bp, nsize) == 0);
1627187Sroot 		bp->b_flags |= B_DONE;
1639163Ssam 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
16412643Ssam 		ip->i_blocks += btodb(nsize - osize);
16512643Ssam 		ip->i_flag |= IUPD|ICHG;
16637735Smckusick 		*bpp = bp;
16737735Smckusick 		return (0);
1684463Smckusic 	}
1694948Smckusic 	if (bpref >= fs->fs_size)
1704948Smckusic 		bpref = 0;
17126253Skarels 	switch ((int)fs->fs_optim) {
17225256Smckusick 	case FS_OPTSPACE:
17325256Smckusick 		/*
17425256Smckusick 		 * Allocate an exact sized fragment. Although this makes
17525256Smckusick 		 * best use of space, we will waste time relocating it if
17625256Smckusick 		 * the file continues to grow. If the fragmentation is
17725256Smckusick 		 * less than half of the minimum free reserve, we choose
17825256Smckusick 		 * to begin optimizing for time.
17925256Smckusick 		 */
18024698Smckusick 		request = nsize;
18125256Smckusick 		if (fs->fs_minfree < 5 ||
18225256Smckusick 		    fs->fs_cstotal.cs_nffree >
18325256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
18425256Smckusick 			break;
18525256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
18625256Smckusick 			fs->fs_fsmnt);
18725256Smckusick 		fs->fs_optim = FS_OPTTIME;
18825256Smckusick 		break;
18925256Smckusick 	case FS_OPTTIME:
19025256Smckusick 		/*
19125256Smckusick 		 * At this point we have discovered a file that is trying
19225256Smckusick 		 * to grow a small fragment to a larger fragment. To save
19325256Smckusick 		 * time, we allocate a full sized block, then free the
19425256Smckusick 		 * unused portion. If the file continues to grow, the
19525256Smckusick 		 * `fragextend' call above will be able to grow it in place
19625256Smckusick 		 * without further copying. If aberrant programs cause
19725256Smckusick 		 * disk fragmentation to grow within 2% of the free reserve,
19825256Smckusick 		 * we choose to begin optimizing for space.
19925256Smckusick 		 */
20024698Smckusick 		request = fs->fs_bsize;
20125256Smckusick 		if (fs->fs_cstotal.cs_nffree <
20225256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
20325256Smckusick 			break;
20425256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
20525256Smckusick 			fs->fs_fsmnt);
20625256Smckusick 		fs->fs_optim = FS_OPTSPACE;
20725256Smckusick 		break;
20825256Smckusick 	default:
20925256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
21025256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
21125256Smckusick 		panic("realloccg: bad optim");
21225256Smckusick 		/* NOTREACHED */
21325256Smckusick 	}
21424698Smckusick 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
2159163Ssam 		(u_long (*)())alloccg);
2166567Smckusic 	if (bno > 0) {
217*38776Smckusick 		error = bread(ip->i_devvp, fsbtodb(fs, bprev),
218*38776Smckusick 			osize, NOCRED, &obp);
21937735Smckusick 		if (error) {
2205960Smckusic 			brelse(obp);
22137735Smckusick 			return (error);
2225960Smckusic 		}
22325471Smckusick 		bn = fsbtodb(fs, bno);
22437735Smckusick 		bp = getblk(ip->i_devvp, bn, nsize);
22517658Smckusick 		bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
22630749Skarels 		count = howmany(osize, CLBYTES);
22730749Skarels 		for (i = 0; i < count; i++)
22837735Smckusick 			munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
22917658Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
23017658Smckusick 		if (obp->b_flags & B_DELWRI) {
23117658Smckusick 			obp->b_flags &= ~B_DELWRI;
23217658Smckusick 			u.u_ru.ru_oublock--;		/* delete charge */
23317658Smckusick 		}
2344463Smckusic 		brelse(obp);
23531402Smckusick 		blkfree(ip, bprev, (off_t)osize);
23624698Smckusick 		if (nsize < request)
23731402Smckusick 			blkfree(ip, bno + numfrags(fs, nsize),
23824698Smckusick 				(off_t)(request - nsize));
23912643Ssam 		ip->i_blocks += btodb(nsize - osize);
24012643Ssam 		ip->i_flag |= IUPD|ICHG;
24137735Smckusick 		*bpp = bp;
24237735Smckusick 		return (0);
2434463Smckusic 	}
2444792Smckusic nospace:
2454463Smckusic 	/*
2464463Smckusic 	 * no space available
2474463Smckusic 	 */
2484426Smckusic 	fserr(fs, "file system full");
2494426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
25037735Smckusick 	return (ENOSPC);
2514426Smckusic }
2524426Smckusic 
2535375Smckusic /*
2545375Smckusic  * Allocate an inode in the file system.
2555375Smckusic  *
2565375Smckusic  * A preference may be optionally specified. If a preference is given
2575375Smckusic  * the following hierarchy is used to allocate an inode:
2585375Smckusic  *   1) allocate the requested inode.
2595375Smckusic  *   2) allocate an inode in the same cylinder group.
2605375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2615375Smckusic  *      available inode is located.
2625375Smckusic  * If no inode preference is given the following heirarchy is used
2635375Smckusic  * to allocate an inode:
2645375Smckusic  *   1) allocate an inode in cylinder group 0.
2655375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2665375Smckusic  *      available inode is located.
2675375Smckusic  */
26837735Smckusick ialloc(pip, ipref, mode, ipp)
2695965Smckusic 	register struct inode *pip;
2704359Smckusick 	ino_t ipref;
2714359Smckusick 	int mode;
27237735Smckusick 	struct inode **ipp;
2734359Smckusick {
2745212Smckusic 	ino_t ino;
2754359Smckusick 	register struct fs *fs;
2764359Smckusick 	register struct inode *ip;
27737735Smckusick 	int cg, error;
2784359Smckusick 
27937735Smckusick 	*ipp = 0;
2805965Smckusic 	fs = pip->i_fs;
2814792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2824359Smckusick 		goto noinodes;
2837650Ssam #ifdef QUOTA
28437735Smckusick 	if (error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0))
28537735Smckusick 		return (error);
2867483Skre #endif
2874948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2884948Smckusic 		ipref = 0;
2895377Smckusic 	cg = itog(fs, ipref);
2905965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2914359Smckusick 	if (ino == 0)
2924359Smckusick 		goto noinodes;
29337735Smckusick 	error = iget(pip, ino, ipp);
29437735Smckusick 	if (error) {
29515120Skarels 		ifree(pip, ino, 0);
29637735Smckusick 		return (error);
2974359Smckusick 	}
29838255Smckusick 	ip = *ipp;
2996716Smckusick 	if (ip->i_mode) {
3006716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
3016716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
3024359Smckusick 		panic("ialloc: dup alloc");
3036716Smckusick 	}
30412643Ssam 	if (ip->i_blocks) {				/* XXX */
30512643Ssam 		printf("free inode %s/%d had %d blocks\n",
30612643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
30712643Ssam 		ip->i_blocks = 0;
30812643Ssam 	}
30938255Smckusick 	/*
31038255Smckusick 	 * Set up a new generation number for this inode.
31138255Smckusick 	 */
31238255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
31338255Smckusick 		nextgennumber = time.tv_sec;
31438255Smckusick 	ip->i_gen = nextgennumber;
31537735Smckusick 	return (0);
3164359Smckusick noinodes:
3174359Smckusick 	fserr(fs, "out of inodes");
3186294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
31937735Smckusick 	return (ENOSPC);
3204359Smckusick }
3214359Smckusick 
3224651Smckusic /*
3235375Smckusic  * Find a cylinder to place a directory.
3245375Smckusic  *
3255375Smckusic  * The policy implemented by this algorithm is to select from
3265375Smckusic  * among those cylinder groups with above the average number of
3275375Smckusic  * free inodes, the one with the smallest number of directories.
3284651Smckusic  */
3299163Ssam ino_t
3305965Smckusic dirpref(fs)
3315965Smckusic 	register struct fs *fs;
3324359Smckusick {
3334651Smckusic 	int cg, minndir, mincg, avgifree;
3344359Smckusick 
3354792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3364651Smckusic 	minndir = fs->fs_ipg;
3374359Smckusick 	mincg = 0;
3384651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3395322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3405322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3414359Smckusick 			mincg = cg;
3425322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3434359Smckusick 		}
3449163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3454359Smckusick }
3464359Smckusick 
3474651Smckusic /*
3489163Ssam  * Select the desired position for the next block in a file.  The file is
3499163Ssam  * logically divided into sections. The first section is composed of the
3509163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3519163Ssam  *
3529163Ssam  * If no blocks have been allocated in the first section, the policy is to
3539163Ssam  * request a block in the same cylinder group as the inode that describes
3549163Ssam  * the file. If no blocks have been allocated in any other section, the
3559163Ssam  * policy is to place the section in a cylinder group with a greater than
3569163Ssam  * average number of free blocks.  An appropriate cylinder group is found
35717696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
35817696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
35917696Smckusick  * cylinder group from which the previous allocation was made. The sweep
36017696Smckusick  * continues until a cylinder group with greater than the average number
36117696Smckusick  * of free blocks is found. If the allocation is for the first block in an
36217696Smckusick  * indirect block, the information on the previous allocation is unavailable;
36317696Smckusick  * here a best guess is made based upon the logical block number being
36417696Smckusick  * allocated.
3659163Ssam  *
3669163Ssam  * If a section is already partially allocated, the policy is to
3679163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3689163Ssam  * contiguous blocks and the beginning of the next is physically separated
3699163Ssam  * so that the disk head will be in transit between them for at least
3709163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3719163Ssam  * schedule another I/O transfer.
3724651Smckusic  */
3735212Smckusic daddr_t
3749163Ssam blkpref(ip, lbn, indx, bap)
3759163Ssam 	struct inode *ip;
3769163Ssam 	daddr_t lbn;
3779163Ssam 	int indx;
3789163Ssam 	daddr_t *bap;
3799163Ssam {
3805965Smckusic 	register struct fs *fs;
38117696Smckusick 	register int cg;
38217696Smckusick 	int avgbfree, startcg;
3839163Ssam 	daddr_t nextblk;
3844651Smckusic 
3859163Ssam 	fs = ip->i_fs;
3869163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3879163Ssam 		if (lbn < NDADDR) {
3889163Ssam 			cg = itog(fs, ip->i_number);
3895322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3904651Smckusic 		}
3919163Ssam 		/*
3929163Ssam 		 * Find a cylinder with greater than average number of
3939163Ssam 		 * unused data blocks.
3949163Ssam 		 */
39517696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
39617696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
39717696Smckusick 		else
39817696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
39917696Smckusick 		startcg %= fs->fs_ncg;
4009163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
40117696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
4029163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4039163Ssam 				fs->fs_cgrotor = cg;
4049163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4059163Ssam 			}
40617696Smckusick 		for (cg = 0; cg <= startcg; cg++)
4079163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4089163Ssam 				fs->fs_cgrotor = cg;
4099163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4109163Ssam 			}
4119163Ssam 		return (NULL);
4129163Ssam 	}
4139163Ssam 	/*
4149163Ssam 	 * One or more previous blocks have been laid out. If less
4159163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
4169163Ssam 	 * next block is requested contiguously, otherwise it is
4179163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
4189163Ssam 	 */
4199163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
4209163Ssam 	if (indx > fs->fs_maxcontig &&
42111638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4229163Ssam 	    != nextblk)
4239163Ssam 		return (nextblk);
4249163Ssam 	if (fs->fs_rotdelay != 0)
4259163Ssam 		/*
4269163Ssam 		 * Here we convert ms of delay to frags as:
4279163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4289163Ssam 		 *	((sect/frag) * (ms/sec))
4299163Ssam 		 * then round up to the next block.
4309163Ssam 		 */
4319163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4329163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4339163Ssam 	return (nextblk);
4344651Smckusic }
4354651Smckusic 
4365375Smckusic /*
4375375Smckusic  * Implement the cylinder overflow algorithm.
4385375Smckusic  *
4395375Smckusic  * The policy implemented by this algorithm is:
4405375Smckusic  *   1) allocate the block in its requested cylinder group.
4415375Smckusic  *   2) quadradically rehash on the cylinder group number.
4425375Smckusic  *   3) brute force search for a free block.
4435375Smckusic  */
4445212Smckusic /*VARARGS5*/
4455212Smckusic u_long
4465965Smckusic hashalloc(ip, cg, pref, size, allocator)
4475965Smckusic 	struct inode *ip;
4484359Smckusick 	int cg;
4494359Smckusick 	long pref;
4504359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4515212Smckusic 	u_long (*allocator)();
4524359Smckusick {
4535965Smckusic 	register struct fs *fs;
4544359Smckusick 	long result;
4554359Smckusick 	int i, icg = cg;
4564359Smckusick 
4575965Smckusic 	fs = ip->i_fs;
4584359Smckusick 	/*
4594359Smckusick 	 * 1: preferred cylinder group
4604359Smckusick 	 */
4615965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4624359Smckusick 	if (result)
4634359Smckusick 		return (result);
4644359Smckusick 	/*
4654359Smckusick 	 * 2: quadratic rehash
4664359Smckusick 	 */
4674359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4684359Smckusick 		cg += i;
4694359Smckusick 		if (cg >= fs->fs_ncg)
4704359Smckusick 			cg -= fs->fs_ncg;
4715965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4724359Smckusick 		if (result)
4734359Smckusick 			return (result);
4744359Smckusick 	}
4754359Smckusick 	/*
4764359Smckusick 	 * 3: brute force search
47710847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
47810847Ssam 	 * and 1 is always checked in the quadratic rehash.
4794359Smckusick 	 */
48010848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
48110847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4825965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4834359Smckusick 		if (result)
4844359Smckusick 			return (result);
4854359Smckusick 		cg++;
4864359Smckusick 		if (cg == fs->fs_ncg)
4874359Smckusick 			cg = 0;
4884359Smckusick 	}
4896294Smckusick 	return (NULL);
4904359Smckusick }
4914359Smckusick 
4925375Smckusic /*
4935375Smckusic  * Determine whether a fragment can be extended.
4945375Smckusic  *
4955375Smckusic  * Check to see if the necessary fragments are available, and
4965375Smckusic  * if they are, allocate them.
4975375Smckusic  */
4984359Smckusick daddr_t
4995965Smckusic fragextend(ip, cg, bprev, osize, nsize)
5005965Smckusic 	struct inode *ip;
5014426Smckusic 	int cg;
5024463Smckusic 	long bprev;
5034426Smckusic 	int osize, nsize;
5044426Smckusic {
5055965Smckusic 	register struct fs *fs;
5064463Smckusic 	register struct cg *cgp;
50737735Smckusick 	struct buf *bp;
5084463Smckusic 	long bno;
5094463Smckusic 	int frags, bbase;
51037735Smckusick 	int i, error;
5114426Smckusic 
5125965Smckusic 	fs = ip->i_fs;
51317224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
5146531Smckusick 		return (NULL);
5155960Smckusic 	frags = numfrags(fs, nsize);
51617224Smckusick 	bbase = fragnum(fs, bprev);
51717224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
51830749Skarels 		/* cannot extend across a block boundary */
5196294Smckusick 		return (NULL);
5204463Smckusic 	}
52137735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
522*38776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
52337735Smckusick 	if (error) {
52437735Smckusick 		brelse(bp);
52537735Smckusick 		return (NULL);
52637735Smckusick 	}
5276531Smckusick 	cgp = bp->b_un.b_cg;
52837735Smckusick 	if (!cg_chkmagic(cgp)) {
5295960Smckusic 		brelse(bp);
5306294Smckusick 		return (NULL);
5315960Smckusic 	}
5328105Sroot 	cgp->cg_time = time.tv_sec;
5335377Smckusic 	bno = dtogd(fs, bprev);
5345960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
53534143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
5365361Smckusic 			brelse(bp);
5376294Smckusick 			return (NULL);
5385361Smckusic 		}
5395361Smckusic 	/*
5405361Smckusic 	 * the current fragment can be extended
5415361Smckusic 	 * deduct the count on fragment being extended into
5425361Smckusic 	 * increase the count on the remaining fragment (if any)
5435361Smckusic 	 * allocate the extended piece
5445361Smckusic 	 */
5455361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
54634143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
5474463Smckusic 			break;
5485960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5495361Smckusic 	if (i != frags)
5505361Smckusic 		cgp->cg_frsum[i - frags]++;
5515960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
55234143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
5535361Smckusic 		cgp->cg_cs.cs_nffree--;
5545361Smckusic 		fs->fs_cstotal.cs_nffree--;
5555361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5564463Smckusic 	}
5575361Smckusic 	fs->fs_fmod++;
5585361Smckusic 	bdwrite(bp);
5595361Smckusic 	return (bprev);
5604426Smckusic }
5614426Smckusic 
5625375Smckusic /*
5635375Smckusic  * Determine whether a block can be allocated.
5645375Smckusic  *
5655375Smckusic  * Check to see if a block of the apprpriate size is available,
5665375Smckusic  * and if it is, allocate it.
5675375Smckusic  */
5689163Ssam daddr_t
5695965Smckusic alloccg(ip, cg, bpref, size)
5705965Smckusic 	struct inode *ip;
5714359Smckusick 	int cg;
5724359Smckusick 	daddr_t bpref;
5734359Smckusick 	int size;
5744359Smckusick {
5755965Smckusic 	register struct fs *fs;
5764463Smckusic 	register struct cg *cgp;
57737735Smckusick 	struct buf *bp;
5784463Smckusic 	register int i;
57937735Smckusick 	int error, bno, frags, allocsiz;
5804359Smckusick 
5815965Smckusic 	fs = ip->i_fs;
5825322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5836294Smckusick 		return (NULL);
58437735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
585*38776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
58637735Smckusick 	if (error) {
58737735Smckusick 		brelse(bp);
58837735Smckusick 		return (NULL);
58937735Smckusick 	}
5906531Smckusick 	cgp = bp->b_un.b_cg;
59137735Smckusick 	if (!cg_chkmagic(cgp) ||
59215950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5935960Smckusic 		brelse(bp);
5946294Smckusick 		return (NULL);
5955960Smckusic 	}
5968105Sroot 	cgp->cg_time = time.tv_sec;
5975322Smckusic 	if (size == fs->fs_bsize) {
5985212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
5994463Smckusic 		bdwrite(bp);
6004463Smckusic 		return (bno);
6014463Smckusic 	}
6024463Smckusic 	/*
6034463Smckusic 	 * check to see if any fragments are already available
6044463Smckusic 	 * allocsiz is the size which will be allocated, hacking
6054463Smckusic 	 * it down to a smaller size if necessary
6064463Smckusic 	 */
6075960Smckusic 	frags = numfrags(fs, size);
6085322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
6094463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
6104463Smckusic 			break;
6115322Smckusic 	if (allocsiz == fs->fs_frag) {
6124463Smckusic 		/*
6134463Smckusic 		 * no fragments were available, so a block will be
6144463Smckusic 		 * allocated, and hacked up
6154463Smckusic 		 */
6164792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
6174463Smckusic 			brelse(bp);
6186294Smckusick 			return (NULL);
6194463Smckusic 		}
6205212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
6215377Smckusic 		bpref = dtogd(fs, bno);
6225322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
62334143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
6245322Smckusic 		i = fs->fs_frag - frags;
6254792Smckusic 		cgp->cg_cs.cs_nffree += i;
6264792Smckusic 		fs->fs_cstotal.cs_nffree += i;
6275322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
6289762Ssam 		fs->fs_fmod++;
6294463Smckusic 		cgp->cg_frsum[i]++;
6304463Smckusic 		bdwrite(bp);
6314463Smckusic 		return (bno);
6324463Smckusic 	}
6334651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
63415950Smckusick 	if (bno < 0) {
63515950Smckusick 		brelse(bp);
6366294Smckusick 		return (NULL);
63715950Smckusick 	}
6384463Smckusic 	for (i = 0; i < frags; i++)
63934143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
6404792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6414792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6425322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
6439762Ssam 	fs->fs_fmod++;
6444463Smckusic 	cgp->cg_frsum[allocsiz]--;
6454463Smckusic 	if (frags != allocsiz)
6464463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6474463Smckusic 	bdwrite(bp);
6484463Smckusic 	return (cg * fs->fs_fpg + bno);
6494463Smckusic }
6504463Smckusic 
6515375Smckusic /*
6525375Smckusic  * Allocate a block in a cylinder group.
6535375Smckusic  *
6545375Smckusic  * This algorithm implements the following policy:
6555375Smckusic  *   1) allocate the requested block.
6565375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6575375Smckusic  *   3) allocate the next available block on the block rotor for the
6585375Smckusic  *      specified cylinder group.
6595375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6605375Smckusic  * blocks may be fragmented by the routine that allocates them.
6615375Smckusic  */
6624463Smckusic daddr_t
6635212Smckusic alloccgblk(fs, cgp, bpref)
6645965Smckusic 	register struct fs *fs;
6654463Smckusic 	register struct cg *cgp;
6664463Smckusic 	daddr_t bpref;
6674463Smckusic {
6684651Smckusic 	daddr_t bno;
6696294Smckusick 	int cylno, pos, delta;
6704651Smckusic 	short *cylbp;
6715361Smckusic 	register int i;
6724463Smckusic 
6734651Smckusic 	if (bpref == 0) {
6744651Smckusic 		bpref = cgp->cg_rotor;
6755361Smckusic 		goto norot;
6765361Smckusic 	}
67717224Smckusick 	bpref = blknum(fs, bpref);
6785377Smckusic 	bpref = dtogd(fs, bpref);
6795361Smckusic 	/*
6805361Smckusic 	 * if the requested block is available, use it
6815361Smckusic 	 */
68234143Smckusick 	if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
6835361Smckusic 		bno = bpref;
6845361Smckusic 		goto gotit;
6855361Smckusic 	}
6865361Smckusic 	/*
6875361Smckusic 	 * check for a block available on the same cylinder
6885361Smckusic 	 */
6895361Smckusic 	cylno = cbtocylno(fs, bpref);
69034143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
6915375Smckusic 		goto norot;
6925375Smckusic 	if (fs->fs_cpc == 0) {
6935375Smckusic 		/*
6945375Smckusic 		 * block layout info is not available, so just have
6955375Smckusic 		 * to take any block in this cylinder.
6965375Smckusic 		 */
6975375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
6985375Smckusic 		goto norot;
6995375Smckusic 	}
7005375Smckusic 	/*
7015361Smckusic 	 * check the summary information to see if a block is
7025361Smckusic 	 * available in the requested cylinder starting at the
7039163Ssam 	 * requested rotational position and proceeding around.
7045361Smckusic 	 */
70534143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
7069163Ssam 	pos = cbtorpos(fs, bpref);
70734143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
7085361Smckusic 		if (cylbp[i] > 0)
7095361Smckusic 			break;
71034143Smckusick 	if (i == fs->fs_nrpos)
7115361Smckusic 		for (i = 0; i < pos; i++)
7125361Smckusic 			if (cylbp[i] > 0)
7135361Smckusic 				break;
7145361Smckusic 	if (cylbp[i] > 0) {
7154651Smckusic 		/*
7165361Smckusic 		 * found a rotational position, now find the actual
7175361Smckusic 		 * block. A panic if none is actually there.
7184651Smckusic 		 */
7195361Smckusic 		pos = cylno % fs->fs_cpc;
7205361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
72134143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
7226716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
7236716Smckusick 			    pos, i, fs->fs_fsmnt);
7245361Smckusic 			panic("alloccgblk: cyl groups corrupted");
7256716Smckusick 		}
72634143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
72734143Smckusick 			if (isblock(fs, cg_blksfree(cgp), bno + i)) {
72811638Ssam 				bno = blkstofrags(fs, (bno + i));
7295361Smckusic 				goto gotit;
7305361Smckusic 			}
73134143Smckusick 			delta = fs_rotbl(fs)[i];
73234143Smckusick 			if (delta <= 0 ||
73334143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
7344651Smckusic 				break;
7356294Smckusick 			i += delta;
7364651Smckusic 		}
7376716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
7385361Smckusic 		panic("alloccgblk: can't find blk in cyl");
7394359Smckusick 	}
7405361Smckusic norot:
7415361Smckusic 	/*
7425361Smckusic 	 * no blocks in the requested cylinder, so take next
7435361Smckusic 	 * available one in this cylinder group.
7445361Smckusic 	 */
7458628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7466567Smckusic 	if (bno < 0)
7476294Smckusick 		return (NULL);
7484651Smckusic 	cgp->cg_rotor = bno;
7494359Smckusick gotit:
75034143Smckusick 	clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno));
7514792Smckusic 	cgp->cg_cs.cs_nbfree--;
7524792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7535322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7545375Smckusic 	cylno = cbtocylno(fs, bno);
75534143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
75634143Smckusick 	cg_blktot(cgp)[cylno]--;
7574359Smckusick 	fs->fs_fmod++;
7584651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7594359Smckusick }
76034143Smckusick 
7615375Smckusic /*
7625375Smckusic  * Determine whether an inode can be allocated.
7635375Smckusic  *
7645375Smckusic  * Check to see if an inode is available, and if it is,
7655375Smckusic  * allocate it using the following policy:
7665375Smckusic  *   1) allocate the requested inode.
7675375Smckusic  *   2) allocate the next available inode after the requested
7685375Smckusic  *      inode in the specified cylinder group.
7695375Smckusic  */
7709163Ssam ino_t
7715965Smckusic ialloccg(ip, cg, ipref, mode)
7725965Smckusic 	struct inode *ip;
7734359Smckusick 	int cg;
7744359Smckusick 	daddr_t ipref;
7754359Smckusick 	int mode;
7764359Smckusick {
7775965Smckusic 	register struct fs *fs;
7784463Smckusic 	register struct cg *cgp;
77916784Smckusick 	struct buf *bp;
78037735Smckusick 	int error, start, len, loc, map, i;
7814359Smckusick 
7825965Smckusic 	fs = ip->i_fs;
7835322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7846294Smckusick 		return (NULL);
78537735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
786*38776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
78737735Smckusick 	if (error) {
78837735Smckusick 		brelse(bp);
78937735Smckusick 		return (NULL);
79037735Smckusick 	}
7916531Smckusick 	cgp = bp->b_un.b_cg;
79237735Smckusick 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
7935960Smckusic 		brelse(bp);
7946294Smckusick 		return (NULL);
7955960Smckusic 	}
7968105Sroot 	cgp->cg_time = time.tv_sec;
7974359Smckusick 	if (ipref) {
7984359Smckusick 		ipref %= fs->fs_ipg;
79934143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
8004359Smckusick 			goto gotit;
80116784Smckusick 	}
80216784Smckusick 	start = cgp->cg_irotor / NBBY;
80316784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
80434143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
80516784Smckusick 	if (loc == 0) {
80617697Smckusick 		len = start + 1;
80717697Smckusick 		start = 0;
80834143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
80917697Smckusick 		if (loc == 0) {
81017697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
81117697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
81217697Smckusick 			panic("ialloccg: map corrupted");
81317697Smckusick 			/* NOTREACHED */
81417697Smckusick 		}
81516784Smckusick 	}
81616784Smckusick 	i = start + len - loc;
81734143Smckusick 	map = cg_inosused(cgp)[i];
81816784Smckusick 	ipref = i * NBBY;
81916784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
82016784Smckusick 		if ((map & i) == 0) {
8214359Smckusick 			cgp->cg_irotor = ipref;
8224359Smckusick 			goto gotit;
8234359Smckusick 		}
8244359Smckusick 	}
82516784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
82616784Smckusick 	panic("ialloccg: block not in map");
82716784Smckusick 	/* NOTREACHED */
8284359Smckusick gotit:
82934143Smckusick 	setbit(cg_inosused(cgp), ipref);
8304792Smckusic 	cgp->cg_cs.cs_nifree--;
8314792Smckusic 	fs->fs_cstotal.cs_nifree--;
8325322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
8334359Smckusick 	fs->fs_fmod++;
8344359Smckusick 	if ((mode & IFMT) == IFDIR) {
8354792Smckusic 		cgp->cg_cs.cs_ndir++;
8364792Smckusic 		fs->fs_cstotal.cs_ndir++;
8375322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8384359Smckusick 	}
8394359Smckusick 	bdwrite(bp);
8404359Smckusick 	return (cg * fs->fs_ipg + ipref);
8414359Smckusick }
8424359Smckusick 
8435375Smckusic /*
8445375Smckusic  * Free a block or fragment.
8455375Smckusic  *
8465375Smckusic  * The specified block or fragment is placed back in the
8475375Smckusic  * free map. If a fragment is deallocated, a possible
8485375Smckusic  * block reassembly is checked.
8495375Smckusic  */
85031402Smckusick blkfree(ip, bno, size)
8515965Smckusic 	register struct inode *ip;
8524359Smckusick 	daddr_t bno;
8535212Smckusic 	off_t size;
8544359Smckusick {
8554359Smckusick 	register struct fs *fs;
8564359Smckusick 	register struct cg *cgp;
85737735Smckusick 	struct buf *bp;
85837735Smckusick 	int error, cg, blk, frags, bbase;
8594463Smckusic 	register int i;
8604359Smckusick 
8615965Smckusic 	fs = ip->i_fs;
8626716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8636716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8646716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
86531402Smckusick 		panic("blkfree: bad size");
8666716Smckusick 	}
8675377Smckusic 	cg = dtog(fs, bno);
8686567Smckusic 	if (badblock(fs, bno)) {
8696567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
8704359Smckusick 		return;
8716567Smckusic 	}
87237735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
873*38776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
87437735Smckusick 	if (error) {
87537735Smckusick 		brelse(bp);
87637735Smckusick 		return;
87737735Smckusick 	}
8786531Smckusick 	cgp = bp->b_un.b_cg;
87937735Smckusick 	if (!cg_chkmagic(cgp)) {
8805960Smckusic 		brelse(bp);
8814359Smckusick 		return;
8825960Smckusic 	}
8838105Sroot 	cgp->cg_time = time.tv_sec;
8845377Smckusic 	bno = dtogd(fs, bno);
8855322Smckusic 	if (size == fs->fs_bsize) {
88634143Smckusick 		if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) {
8876716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
8886716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
88931402Smckusick 			panic("blkfree: freeing free block");
8906567Smckusic 		}
89134143Smckusick 		setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
8924792Smckusic 		cgp->cg_cs.cs_nbfree++;
8934792Smckusic 		fs->fs_cstotal.cs_nbfree++;
8945322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
8955375Smckusic 		i = cbtocylno(fs, bno);
89634143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
89734143Smckusick 		cg_blktot(cgp)[i]++;
8984426Smckusic 	} else {
89917224Smckusick 		bbase = bno - fragnum(fs, bno);
9004463Smckusic 		/*
9014463Smckusic 		 * decrement the counts associated with the old frags
9024463Smckusic 		 */
90334143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
9045322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
9054463Smckusic 		/*
9064463Smckusic 		 * deallocate the fragment
9074463Smckusic 		 */
9085960Smckusic 		frags = numfrags(fs, size);
9094463Smckusic 		for (i = 0; i < frags; i++) {
91034143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
9116716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
9126716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
91331402Smckusick 				panic("blkfree: freeing free frag");
9146716Smckusick 			}
91534143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
9164426Smckusic 		}
9176294Smckusick 		cgp->cg_cs.cs_nffree += i;
9186294Smckusick 		fs->fs_cstotal.cs_nffree += i;
9196294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
9204463Smckusic 		/*
9214463Smckusic 		 * add back in counts associated with the new frags
9224463Smckusic 		 */
92334143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
9245322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
9254463Smckusic 		/*
9264463Smckusic 		 * if a complete block has been reassembled, account for it
9274463Smckusic 		 */
92834476Smckusick 		if (isblock(fs, cg_blksfree(cgp),
92934476Smckusick 		    (daddr_t)fragstoblks(fs, bbase))) {
9305322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
9315322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
9325322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
9334792Smckusic 			cgp->cg_cs.cs_nbfree++;
9344792Smckusic 			fs->fs_cstotal.cs_nbfree++;
9355322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
9365375Smckusic 			i = cbtocylno(fs, bbase);
93734143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
93834143Smckusick 			cg_blktot(cgp)[i]++;
9394426Smckusic 		}
9404426Smckusic 	}
9414359Smckusick 	fs->fs_fmod++;
9424359Smckusick 	bdwrite(bp);
9434359Smckusick }
9444359Smckusick 
9455375Smckusic /*
9465375Smckusic  * Free an inode.
9475375Smckusic  *
9485375Smckusic  * The specified inode is placed back in the free map.
9495375Smckusic  */
9505965Smckusic ifree(ip, ino, mode)
9515965Smckusic 	struct inode *ip;
9524359Smckusick 	ino_t ino;
9534359Smckusick 	int mode;
9544359Smckusick {
9554359Smckusick 	register struct fs *fs;
9564359Smckusick 	register struct cg *cgp;
95737735Smckusick 	struct buf *bp;
95837735Smckusick 	int error, cg;
9594359Smckusick 
9605965Smckusic 	fs = ip->i_fs;
9616716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
9626716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9636716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9644359Smckusick 		panic("ifree: range");
9656716Smckusick 	}
9665377Smckusic 	cg = itog(fs, ino);
96737735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
968*38776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
96937735Smckusick 	if (error) {
97037735Smckusick 		brelse(bp);
97137735Smckusick 		return;
97237735Smckusick 	}
9736531Smckusick 	cgp = bp->b_un.b_cg;
97437735Smckusick 	if (!cg_chkmagic(cgp)) {
9755960Smckusic 		brelse(bp);
9764359Smckusick 		return;
9775960Smckusic 	}
9788105Sroot 	cgp->cg_time = time.tv_sec;
9794359Smckusick 	ino %= fs->fs_ipg;
98034143Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
9816716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9826716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9834359Smckusick 		panic("ifree: freeing free inode");
9846716Smckusick 	}
98534143Smckusick 	clrbit(cg_inosused(cgp), ino);
98616784Smckusick 	if (ino < cgp->cg_irotor)
98716784Smckusick 		cgp->cg_irotor = ino;
9884792Smckusic 	cgp->cg_cs.cs_nifree++;
9894792Smckusic 	fs->fs_cstotal.cs_nifree++;
9905322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
9914359Smckusick 	if ((mode & IFMT) == IFDIR) {
9924792Smckusic 		cgp->cg_cs.cs_ndir--;
9934792Smckusic 		fs->fs_cstotal.cs_ndir--;
9945322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
9954359Smckusick 	}
9964359Smckusick 	fs->fs_fmod++;
9974359Smckusick 	bdwrite(bp);
9984359Smckusick }
9994359Smckusick 
10004463Smckusic /*
10015375Smckusic  * Find a block of the specified size in the specified cylinder group.
10025375Smckusic  *
10034651Smckusic  * It is a panic if a request is made to find a block if none are
10044651Smckusic  * available.
10054651Smckusic  */
10064651Smckusic daddr_t
10074651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
10084651Smckusic 	register struct fs *fs;
10094651Smckusic 	register struct cg *cgp;
10104651Smckusic 	daddr_t bpref;
10114651Smckusic 	int allocsiz;
10124651Smckusic {
10134651Smckusic 	daddr_t bno;
10144651Smckusic 	int start, len, loc, i;
10154651Smckusic 	int blk, field, subfield, pos;
10164651Smckusic 
10174651Smckusic 	/*
10184651Smckusic 	 * find the fragment by searching through the free block
10194651Smckusic 	 * map for an appropriate bit pattern
10204651Smckusic 	 */
10214651Smckusic 	if (bpref)
10225377Smckusic 		start = dtogd(fs, bpref) / NBBY;
10234651Smckusic 	else
10244651Smckusic 		start = cgp->cg_frotor / NBBY;
10255398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
102634476Smckusick 	loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start],
102734476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
102834476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
10294651Smckusic 	if (loc == 0) {
10306531Smckusick 		len = start + 1;
10316531Smckusick 		start = 0;
103234476Smckusick 		loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0],
103334476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
103434476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
103516784Smckusick 		if (loc == 0) {
103616784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
103716784Smckusick 			    start, len, fs->fs_fsmnt);
103816784Smckusick 			panic("alloccg: map corrupted");
103917697Smckusick 			/* NOTREACHED */
104016784Smckusick 		}
10414651Smckusic 	}
10424651Smckusic 	bno = (start + len - loc) * NBBY;
10434651Smckusic 	cgp->cg_frotor = bno;
10444651Smckusic 	/*
10454651Smckusic 	 * found the byte in the map
10464651Smckusic 	 * sift through the bits to find the selected frag
10474651Smckusic 	 */
10486294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
104934143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
10504651Smckusic 		blk <<= 1;
10514651Smckusic 		field = around[allocsiz];
10524651Smckusic 		subfield = inside[allocsiz];
10535322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10546294Smckusick 			if ((blk & field) == subfield)
10556294Smckusick 				return (bno + pos);
10564651Smckusic 			field <<= 1;
10574651Smckusic 			subfield <<= 1;
10584651Smckusic 		}
10594651Smckusic 	}
10606716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
10614651Smckusic 	panic("alloccg: block not in map");
10626531Smckusick 	return (-1);
10634651Smckusic }
10644651Smckusic 
10654651Smckusic /*
10665375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10675375Smckusic  *
10685375Smckusic  * The form of the error message is:
10694359Smckusick  *	fs: error message
10704359Smckusick  */
10714359Smckusick fserr(fs, cp)
10724359Smckusick 	struct fs *fs;
10734359Smckusick 	char *cp;
10744359Smckusick {
10754359Smckusick 
107624839Seric 	log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp);
10774359Smckusick }
1078