xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 30749)
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*30749Skarels  *	@(#)lfs_alloc.c	7.1.1.1 (Berkeley) 04/02/87
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;
89*30749Skarels #ifdef SECSIZE
90*30749Skarels 	bp = getblk(ip->i_dev, fsbtodb(fs, bno), size, fs->fs_dbsize);
91*30749Skarels #else SECSIZE
925965Smckusic 	bp = getblk(ip->i_dev, fsbtodb(fs, bno), size);
93*30749Skarels #endif SECSIZE
944359Smckusick 	clrbuf(bp);
954359Smckusick 	return (bp);
964359Smckusick nospace:
974359Smckusick 	fserr(fs, "file system full");
984359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
994359Smckusick 	u.u_error = ENOSPC;
1004359Smckusick 	return (NULL);
1014359Smckusick }
1024359Smckusick 
1035375Smckusic /*
1045375Smckusic  * Reallocate a fragment to a bigger size
1055375Smckusic  *
1065375Smckusic  * The number and size of the old block is given, and a preference
1075375Smckusic  * and new size is also specified. The allocator attempts to extend
1085375Smckusic  * the original block. Failing that, the regular block allocator is
1095375Smckusic  * invoked to get an appropriate block.
1105375Smckusic  */
1114426Smckusic struct buf *
1125965Smckusic realloccg(ip, bprev, bpref, osize, nsize)
1135965Smckusic 	register struct inode *ip;
1144651Smckusic 	daddr_t bprev, bpref;
1154426Smckusic 	int osize, nsize;
1164426Smckusic {
1174426Smckusic 	register struct fs *fs;
1184463Smckusic 	register struct buf *bp, *obp;
11924698Smckusick 	int cg, request;
12025471Smckusick 	daddr_t bno, bn;
12125471Smckusick 	int i, count, s;
12225471Smckusick 	extern struct cmap *mfind();
1234426Smckusic 
1245965Smckusic 	fs = ip->i_fs;
1255960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1266716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
1276716Smckusick 		printf("dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1286716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
1294463Smckusic 		panic("realloccg: bad size");
1306716Smckusick 	}
13111638Ssam 	if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1324792Smckusic 		goto nospace;
1336716Smckusick 	if (bprev == 0) {
1346716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1356716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
1364463Smckusic 		panic("realloccg: bad bprev");
1376716Smckusick 	}
1387650Ssam #ifdef QUOTA
13912643Ssam 	u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0);
14012643Ssam 	if (u.u_error)
14112643Ssam 		return (NULL);
1427483Skre #endif
1436294Smckusick 	cg = dtog(fs, bprev);
1445965Smckusic 	bno = fragextend(ip, cg, (long)bprev, osize, nsize);
1454463Smckusic 	if (bno != 0) {
1467187Sroot 		do {
147*30749Skarels #ifdef SECSIZE
148*30749Skarels 			bp = bread(ip->i_dev, fsbtodb(fs, bno), osize,
149*30749Skarels 			    fs->fs_dbsize);
150*30749Skarels #else SECSIZE
1517187Sroot 			bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
152*30749Skarels #endif SECSIZE
1537187Sroot 			if (bp->b_flags & B_ERROR) {
1547187Sroot 				brelse(bp);
1557187Sroot 				return (NULL);
1567187Sroot 			}
1577187Sroot 		} while (brealloc(bp, nsize) == 0);
1587187Sroot 		bp->b_flags |= B_DONE;
1599163Ssam 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
16012643Ssam 		ip->i_blocks += btodb(nsize - osize);
16112643Ssam 		ip->i_flag |= IUPD|ICHG;
1624463Smckusic 		return (bp);
1634463Smckusic 	}
1644948Smckusic 	if (bpref >= fs->fs_size)
1654948Smckusic 		bpref = 0;
16626253Skarels 	switch ((int)fs->fs_optim) {
16725256Smckusick 	case FS_OPTSPACE:
16825256Smckusick 		/*
16925256Smckusick 		 * Allocate an exact sized fragment. Although this makes
17025256Smckusick 		 * best use of space, we will waste time relocating it if
17125256Smckusick 		 * the file continues to grow. If the fragmentation is
17225256Smckusick 		 * less than half of the minimum free reserve, we choose
17325256Smckusick 		 * to begin optimizing for time.
17425256Smckusick 		 */
17524698Smckusick 		request = nsize;
17625256Smckusick 		if (fs->fs_minfree < 5 ||
17725256Smckusick 		    fs->fs_cstotal.cs_nffree >
17825256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
17925256Smckusick 			break;
18025256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
18125256Smckusick 			fs->fs_fsmnt);
18225256Smckusick 		fs->fs_optim = FS_OPTTIME;
18325256Smckusick 		break;
18425256Smckusick 	case FS_OPTTIME:
18525256Smckusick 		/*
18625256Smckusick 		 * At this point we have discovered a file that is trying
18725256Smckusick 		 * to grow a small fragment to a larger fragment. To save
18825256Smckusick 		 * time, we allocate a full sized block, then free the
18925256Smckusick 		 * unused portion. If the file continues to grow, the
19025256Smckusick 		 * `fragextend' call above will be able to grow it in place
19125256Smckusick 		 * without further copying. If aberrant programs cause
19225256Smckusick 		 * disk fragmentation to grow within 2% of the free reserve,
19325256Smckusick 		 * we choose to begin optimizing for space.
19425256Smckusick 		 */
19524698Smckusick 		request = fs->fs_bsize;
19625256Smckusick 		if (fs->fs_cstotal.cs_nffree <
19725256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
19825256Smckusick 			break;
19925256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
20025256Smckusick 			fs->fs_fsmnt);
20125256Smckusick 		fs->fs_optim = FS_OPTSPACE;
20225256Smckusick 		break;
20325256Smckusick 	default:
20425256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
20525256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
20625256Smckusick 		panic("realloccg: bad optim");
20725256Smckusick 		/* NOTREACHED */
20825256Smckusick 	}
20924698Smckusick 	bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
2109163Ssam 		(u_long (*)())alloccg);
2116567Smckusic 	if (bno > 0) {
212*30749Skarels #ifdef SECSIZE
213*30749Skarels 		obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize,
214*30749Skarels 		    fs->fs_dbsize);
215*30749Skarels #else SECSIZE
2165965Smckusic 		obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
217*30749Skarels #endif SECSIZE
2185960Smckusic 		if (obp->b_flags & B_ERROR) {
2195960Smckusic 			brelse(obp);
2206294Smckusick 			return (NULL);
2215960Smckusic 		}
22225471Smckusick 		bn = fsbtodb(fs, bno);
223*30749Skarels #ifdef SECSIZE
224*30749Skarels 		bp = getblk(ip->i_dev, bn, nsize, fs->fs_dbsize);
225*30749Skarels #else SECSIZE
22625471Smckusick 		bp = getblk(ip->i_dev, bn, nsize);
227*30749Skarels #endif SECSIZE
22817658Smckusick 		bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
229*30749Skarels 		count = howmany(osize, CLBYTES);
230*30749Skarels 		for (i = 0; i < count; i++)
231*30749Skarels #ifdef SECSIZE
232*30749Skarels 			munhash(ip->i_dev, bn + i * CLBYTES / fs->fs_dbsize);
233*30749Skarels #else SECSIZE
234*30749Skarels 			munhash(ip->i_dev, bn + i * CLBYTES / DEV_BSIZE);
235*30749Skarels #endif SECSIZE
23617658Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
23717658Smckusick 		if (obp->b_flags & B_DELWRI) {
23817658Smckusick 			obp->b_flags &= ~B_DELWRI;
23917658Smckusick 			u.u_ru.ru_oublock--;		/* delete charge */
24017658Smckusick 		}
2414463Smckusic 		brelse(obp);
2429163Ssam 		free(ip, bprev, (off_t)osize);
24324698Smckusick 		if (nsize < request)
24417709Smckusick 			free(ip, bno + numfrags(fs, nsize),
24524698Smckusick 				(off_t)(request - nsize));
24612643Ssam 		ip->i_blocks += btodb(nsize - osize);
24712643Ssam 		ip->i_flag |= IUPD|ICHG;
2486294Smckusick 		return (bp);
2494463Smckusic 	}
2504792Smckusic nospace:
2514463Smckusic 	/*
2524463Smckusic 	 * no space available
2534463Smckusic 	 */
2544426Smckusic 	fserr(fs, "file system full");
2554426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
2564426Smckusic 	u.u_error = ENOSPC;
2574426Smckusic 	return (NULL);
2584426Smckusic }
2594426Smckusic 
2605375Smckusic /*
2615375Smckusic  * Allocate an inode in the file system.
2625375Smckusic  *
2635375Smckusic  * A preference may be optionally specified. If a preference is given
2645375Smckusic  * the following hierarchy is used to allocate an inode:
2655375Smckusic  *   1) allocate the requested inode.
2665375Smckusic  *   2) allocate an inode in the same cylinder group.
2675375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2685375Smckusic  *      available inode is located.
2695375Smckusic  * If no inode preference is given the following heirarchy is used
2705375Smckusic  * to allocate an inode:
2715375Smckusic  *   1) allocate an inode in cylinder group 0.
2725375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2735375Smckusic  *      available inode is located.
2745375Smckusic  */
2754359Smckusick struct inode *
2765965Smckusic ialloc(pip, ipref, mode)
2775965Smckusic 	register struct inode *pip;
2784359Smckusick 	ino_t ipref;
2794359Smckusick 	int mode;
2804359Smckusick {
2815212Smckusic 	ino_t ino;
2824359Smckusick 	register struct fs *fs;
2834359Smckusick 	register struct inode *ip;
2844359Smckusick 	int cg;
2854359Smckusick 
2865965Smckusic 	fs = pip->i_fs;
2874792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2884359Smckusick 		goto noinodes;
2897650Ssam #ifdef QUOTA
29012643Ssam 	u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
29112643Ssam 	if (u.u_error)
29212643Ssam 		return (NULL);
2937483Skre #endif
2944948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
2954948Smckusic 		ipref = 0;
2965377Smckusic 	cg = itog(fs, ipref);
2975965Smckusic 	ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
2984359Smckusick 	if (ino == 0)
2994359Smckusick 		goto noinodes;
3005965Smckusic 	ip = iget(pip->i_dev, pip->i_fs, ino);
3014359Smckusick 	if (ip == NULL) {
30215120Skarels 		ifree(pip, ino, 0);
3034359Smckusick 		return (NULL);
3044359Smckusick 	}
3056716Smckusick 	if (ip->i_mode) {
3066716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
3076716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
3084359Smckusick 		panic("ialloc: dup alloc");
3096716Smckusick 	}
31012643Ssam 	if (ip->i_blocks) {				/* XXX */
31112643Ssam 		printf("free inode %s/%d had %d blocks\n",
31212643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
31312643Ssam 		ip->i_blocks = 0;
31412643Ssam 	}
3154359Smckusick 	return (ip);
3164359Smckusick noinodes:
3174359Smckusick 	fserr(fs, "out of inodes");
3186294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
3194359Smckusick 	u.u_error = ENOSPC;
3204359Smckusick 	return (NULL);
3214359Smckusick }
3224359Smckusick 
3234651Smckusic /*
3245375Smckusic  * Find a cylinder to place a directory.
3255375Smckusic  *
3265375Smckusic  * The policy implemented by this algorithm is to select from
3275375Smckusic  * among those cylinder groups with above the average number of
3285375Smckusic  * free inodes, the one with the smallest number of directories.
3294651Smckusic  */
3309163Ssam ino_t
3315965Smckusic dirpref(fs)
3325965Smckusic 	register struct fs *fs;
3334359Smckusick {
3344651Smckusic 	int cg, minndir, mincg, avgifree;
3354359Smckusick 
3364792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3374651Smckusic 	minndir = fs->fs_ipg;
3384359Smckusick 	mincg = 0;
3394651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3405322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3415322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3424359Smckusick 			mincg = cg;
3435322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3444359Smckusick 		}
3459163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3464359Smckusick }
3474359Smckusick 
3484651Smckusic /*
3499163Ssam  * Select the desired position for the next block in a file.  The file is
3509163Ssam  * logically divided into sections. The first section is composed of the
3519163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3529163Ssam  *
3539163Ssam  * If no blocks have been allocated in the first section, the policy is to
3549163Ssam  * request a block in the same cylinder group as the inode that describes
3559163Ssam  * the file. If no blocks have been allocated in any other section, the
3569163Ssam  * policy is to place the section in a cylinder group with a greater than
3579163Ssam  * average number of free blocks.  An appropriate cylinder group is found
35817696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
35917696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
36017696Smckusick  * cylinder group from which the previous allocation was made. The sweep
36117696Smckusick  * continues until a cylinder group with greater than the average number
36217696Smckusick  * of free blocks is found. If the allocation is for the first block in an
36317696Smckusick  * indirect block, the information on the previous allocation is unavailable;
36417696Smckusick  * here a best guess is made based upon the logical block number being
36517696Smckusick  * allocated.
3669163Ssam  *
3679163Ssam  * If a section is already partially allocated, the policy is to
3689163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3699163Ssam  * contiguous blocks and the beginning of the next is physically separated
3709163Ssam  * so that the disk head will be in transit between them for at least
3719163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3729163Ssam  * schedule another I/O transfer.
3734651Smckusic  */
3745212Smckusic daddr_t
3759163Ssam blkpref(ip, lbn, indx, bap)
3769163Ssam 	struct inode *ip;
3779163Ssam 	daddr_t lbn;
3789163Ssam 	int indx;
3799163Ssam 	daddr_t *bap;
3809163Ssam {
3815965Smckusic 	register struct fs *fs;
38217696Smckusick 	register int cg;
38317696Smckusick 	int avgbfree, startcg;
3849163Ssam 	daddr_t nextblk;
3854651Smckusic 
3869163Ssam 	fs = ip->i_fs;
3879163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
3889163Ssam 		if (lbn < NDADDR) {
3899163Ssam 			cg = itog(fs, ip->i_number);
3905322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
3914651Smckusic 		}
3929163Ssam 		/*
3939163Ssam 		 * Find a cylinder with greater than average number of
3949163Ssam 		 * unused data blocks.
3959163Ssam 		 */
39617696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
39717696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
39817696Smckusick 		else
39917696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
40017696Smckusick 		startcg %= fs->fs_ncg;
4019163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
40217696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
4039163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4049163Ssam 				fs->fs_cgrotor = cg;
4059163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4069163Ssam 			}
40717696Smckusick 		for (cg = 0; cg <= startcg; cg++)
4089163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4099163Ssam 				fs->fs_cgrotor = cg;
4109163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4119163Ssam 			}
4129163Ssam 		return (NULL);
4139163Ssam 	}
4149163Ssam 	/*
4159163Ssam 	 * One or more previous blocks have been laid out. If less
4169163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
4179163Ssam 	 * next block is requested contiguously, otherwise it is
4189163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
4199163Ssam 	 */
4209163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
4219163Ssam 	if (indx > fs->fs_maxcontig &&
42211638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4239163Ssam 	    != nextblk)
4249163Ssam 		return (nextblk);
4259163Ssam 	if (fs->fs_rotdelay != 0)
4269163Ssam 		/*
4279163Ssam 		 * Here we convert ms of delay to frags as:
4289163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4299163Ssam 		 *	((sect/frag) * (ms/sec))
4309163Ssam 		 * then round up to the next block.
4319163Ssam 		 */
4329163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4339163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4349163Ssam 	return (nextblk);
4354651Smckusic }
4364651Smckusic 
4375375Smckusic /*
4385375Smckusic  * Implement the cylinder overflow algorithm.
4395375Smckusic  *
4405375Smckusic  * The policy implemented by this algorithm is:
4415375Smckusic  *   1) allocate the block in its requested cylinder group.
4425375Smckusic  *   2) quadradically rehash on the cylinder group number.
4435375Smckusic  *   3) brute force search for a free block.
4445375Smckusic  */
4455212Smckusic /*VARARGS5*/
4465212Smckusic u_long
4475965Smckusic hashalloc(ip, cg, pref, size, allocator)
4485965Smckusic 	struct inode *ip;
4494359Smckusick 	int cg;
4504359Smckusick 	long pref;
4514359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4525212Smckusic 	u_long (*allocator)();
4534359Smckusick {
4545965Smckusic 	register struct fs *fs;
4554359Smckusick 	long result;
4564359Smckusick 	int i, icg = cg;
4574359Smckusick 
4585965Smckusic 	fs = ip->i_fs;
4594359Smckusick 	/*
4604359Smckusick 	 * 1: preferred cylinder group
4614359Smckusick 	 */
4625965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4634359Smckusick 	if (result)
4644359Smckusick 		return (result);
4654359Smckusick 	/*
4664359Smckusick 	 * 2: quadratic rehash
4674359Smckusick 	 */
4684359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4694359Smckusick 		cg += i;
4704359Smckusick 		if (cg >= fs->fs_ncg)
4714359Smckusick 			cg -= fs->fs_ncg;
4725965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4734359Smckusick 		if (result)
4744359Smckusick 			return (result);
4754359Smckusick 	}
4764359Smckusick 	/*
4774359Smckusick 	 * 3: brute force search
47810847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
47910847Ssam 	 * and 1 is always checked in the quadratic rehash.
4804359Smckusick 	 */
48110848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
48210847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4835965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4844359Smckusick 		if (result)
4854359Smckusick 			return (result);
4864359Smckusick 		cg++;
4874359Smckusick 		if (cg == fs->fs_ncg)
4884359Smckusick 			cg = 0;
4894359Smckusick 	}
4906294Smckusick 	return (NULL);
4914359Smckusick }
4924359Smckusick 
4935375Smckusic /*
4945375Smckusic  * Determine whether a fragment can be extended.
4955375Smckusic  *
4965375Smckusic  * Check to see if the necessary fragments are available, and
4975375Smckusic  * if they are, allocate them.
4985375Smckusic  */
4994359Smckusick daddr_t
5005965Smckusic fragextend(ip, cg, bprev, osize, nsize)
5015965Smckusic 	struct inode *ip;
5024426Smckusic 	int cg;
5034463Smckusic 	long bprev;
5044426Smckusic 	int osize, nsize;
5054426Smckusic {
5065965Smckusic 	register struct fs *fs;
5074463Smckusic 	register struct buf *bp;
5084463Smckusic 	register struct cg *cgp;
5094463Smckusic 	long bno;
5104463Smckusic 	int frags, bbase;
5114426Smckusic 	int i;
5124426Smckusic 
5135965Smckusic 	fs = ip->i_fs;
51417224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
5156531Smckusick 		return (NULL);
5165960Smckusic 	frags = numfrags(fs, nsize);
51717224Smckusick 	bbase = fragnum(fs, bprev);
51817224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
519*30749Skarels 		/* cannot extend across a block boundary */
5206294Smckusick 		return (NULL);
5214463Smckusic 	}
522*30749Skarels #ifdef SECSIZE
523*30749Skarels 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
524*30749Skarels 	    fs->fs_dbsize);
525*30749Skarels #else SECSIZE
52610278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
527*30749Skarels #endif SECSIZE
5286531Smckusick 	cgp = bp->b_un.b_cg;
5296531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
5305960Smckusic 		brelse(bp);
5316294Smckusick 		return (NULL);
5325960Smckusic 	}
5338105Sroot 	cgp->cg_time = time.tv_sec;
5345377Smckusic 	bno = dtogd(fs, bprev);
5355960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
5365361Smckusic 		if (isclr(cgp->cg_free, bno + i)) {
5375361Smckusic 			brelse(bp);
5386294Smckusick 			return (NULL);
5395361Smckusic 		}
5405361Smckusic 	/*
5415361Smckusic 	 * the current fragment can be extended
5425361Smckusic 	 * deduct the count on fragment being extended into
5435361Smckusic 	 * increase the count on the remaining fragment (if any)
5445361Smckusic 	 * allocate the extended piece
5455361Smckusic 	 */
5465361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
5474463Smckusic 		if (isclr(cgp->cg_free, bno + i))
5484463Smckusic 			break;
5495960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5505361Smckusic 	if (i != frags)
5515361Smckusic 		cgp->cg_frsum[i - frags]++;
5525960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
5535361Smckusic 		clrbit(cgp->cg_free, bno + i);
5545361Smckusic 		cgp->cg_cs.cs_nffree--;
5555361Smckusic 		fs->fs_cstotal.cs_nffree--;
5565361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5574463Smckusic 	}
5585361Smckusic 	fs->fs_fmod++;
5595361Smckusic 	bdwrite(bp);
5605361Smckusic 	return (bprev);
5614426Smckusic }
5624426Smckusic 
5635375Smckusic /*
5645375Smckusic  * Determine whether a block can be allocated.
5655375Smckusic  *
5665375Smckusic  * Check to see if a block of the apprpriate size is available,
5675375Smckusic  * and if it is, allocate it.
5685375Smckusic  */
5699163Ssam daddr_t
5705965Smckusic alloccg(ip, cg, bpref, size)
5715965Smckusic 	struct inode *ip;
5724359Smckusick 	int cg;
5734359Smckusick 	daddr_t bpref;
5744359Smckusick 	int size;
5754359Smckusick {
5765965Smckusic 	register struct fs *fs;
5774463Smckusic 	register struct buf *bp;
5784463Smckusic 	register struct cg *cgp;
5794463Smckusic 	int bno, frags;
5804463Smckusic 	int allocsiz;
5814463Smckusic 	register int i;
5824359Smckusick 
5835965Smckusic 	fs = ip->i_fs;
5845322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5856294Smckusick 		return (NULL);
586*30749Skarels #ifdef SECSIZE
587*30749Skarels 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
588*30749Skarels 	    fs->fs_dbsize);
589*30749Skarels #else SECSIZE
59010278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
591*30749Skarels #endif SECSIZE
5926531Smckusick 	cgp = bp->b_un.b_cg;
59315950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
59415950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
5955960Smckusic 		brelse(bp);
5966294Smckusick 		return (NULL);
5975960Smckusic 	}
5988105Sroot 	cgp->cg_time = time.tv_sec;
5995322Smckusic 	if (size == fs->fs_bsize) {
6005212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
6014463Smckusic 		bdwrite(bp);
6024463Smckusic 		return (bno);
6034463Smckusic 	}
6044463Smckusic 	/*
6054463Smckusic 	 * check to see if any fragments are already available
6064463Smckusic 	 * allocsiz is the size which will be allocated, hacking
6074463Smckusic 	 * it down to a smaller size if necessary
6084463Smckusic 	 */
6095960Smckusic 	frags = numfrags(fs, size);
6105322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
6114463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
6124463Smckusic 			break;
6135322Smckusic 	if (allocsiz == fs->fs_frag) {
6144463Smckusic 		/*
6154463Smckusic 		 * no fragments were available, so a block will be
6164463Smckusic 		 * allocated, and hacked up
6174463Smckusic 		 */
6184792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
6194463Smckusic 			brelse(bp);
6206294Smckusick 			return (NULL);
6214463Smckusic 		}
6225212Smckusic 		bno = alloccgblk(fs, cgp, bpref);
6235377Smckusic 		bpref = dtogd(fs, bno);
6245322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
6254463Smckusic 			setbit(cgp->cg_free, bpref + i);
6265322Smckusic 		i = fs->fs_frag - frags;
6274792Smckusic 		cgp->cg_cs.cs_nffree += i;
6284792Smckusic 		fs->fs_cstotal.cs_nffree += i;
6295322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
6309762Ssam 		fs->fs_fmod++;
6314463Smckusic 		cgp->cg_frsum[i]++;
6324463Smckusic 		bdwrite(bp);
6334463Smckusic 		return (bno);
6344463Smckusic 	}
6354651Smckusic 	bno = mapsearch(fs, cgp, bpref, allocsiz);
63615950Smckusick 	if (bno < 0) {
63715950Smckusick 		brelse(bp);
6386294Smckusick 		return (NULL);
63915950Smckusick 	}
6404463Smckusic 	for (i = 0; i < frags; i++)
6414463Smckusic 		clrbit(cgp->cg_free, bno + i);
6424792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6434792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6445322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
6459762Ssam 	fs->fs_fmod++;
6464463Smckusic 	cgp->cg_frsum[allocsiz]--;
6474463Smckusic 	if (frags != allocsiz)
6484463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6494463Smckusic 	bdwrite(bp);
6504463Smckusic 	return (cg * fs->fs_fpg + bno);
6514463Smckusic }
6524463Smckusic 
6535375Smckusic /*
6545375Smckusic  * Allocate a block in a cylinder group.
6555375Smckusic  *
6565375Smckusic  * This algorithm implements the following policy:
6575375Smckusic  *   1) allocate the requested block.
6585375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6595375Smckusic  *   3) allocate the next available block on the block rotor for the
6605375Smckusic  *      specified cylinder group.
6615375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6625375Smckusic  * blocks may be fragmented by the routine that allocates them.
6635375Smckusic  */
6644463Smckusic daddr_t
6655212Smckusic alloccgblk(fs, cgp, bpref)
6665965Smckusic 	register struct fs *fs;
6674463Smckusic 	register struct cg *cgp;
6684463Smckusic 	daddr_t bpref;
6694463Smckusic {
6704651Smckusic 	daddr_t bno;
6716294Smckusick 	int cylno, pos, delta;
6724651Smckusic 	short *cylbp;
6735361Smckusic 	register int i;
6744463Smckusic 
6754651Smckusic 	if (bpref == 0) {
6764651Smckusic 		bpref = cgp->cg_rotor;
6775361Smckusic 		goto norot;
6785361Smckusic 	}
67917224Smckusick 	bpref = blknum(fs, bpref);
6805377Smckusic 	bpref = dtogd(fs, bpref);
6815361Smckusic 	/*
6825361Smckusic 	 * if the requested block is available, use it
6835361Smckusic 	 */
68411638Ssam 	if (isblock(fs, cgp->cg_free, fragstoblks(fs, bpref))) {
6855361Smckusic 		bno = bpref;
6865361Smckusic 		goto gotit;
6875361Smckusic 	}
6885361Smckusic 	/*
6895361Smckusic 	 * check for a block available on the same cylinder
6905361Smckusic 	 */
6915361Smckusic 	cylno = cbtocylno(fs, bpref);
6925375Smckusic 	if (cgp->cg_btot[cylno] == 0)
6935375Smckusic 		goto norot;
6945375Smckusic 	if (fs->fs_cpc == 0) {
6955375Smckusic 		/*
6965375Smckusic 		 * block layout info is not available, so just have
6975375Smckusic 		 * to take any block in this cylinder.
6985375Smckusic 		 */
6995375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
7005375Smckusic 		goto norot;
7015375Smckusic 	}
7025375Smckusic 	/*
7035361Smckusic 	 * check the summary information to see if a block is
7045361Smckusic 	 * available in the requested cylinder starting at the
7059163Ssam 	 * requested rotational position and proceeding around.
7065361Smckusic 	 */
7079163Ssam 	cylbp = cgp->cg_b[cylno];
7089163Ssam 	pos = cbtorpos(fs, bpref);
7095361Smckusic 	for (i = pos; i < NRPOS; i++)
7105361Smckusic 		if (cylbp[i] > 0)
7115361Smckusic 			break;
7125361Smckusic 	if (i == NRPOS)
7135361Smckusic 		for (i = 0; i < pos; i++)
7145361Smckusic 			if (cylbp[i] > 0)
7155361Smckusic 				break;
7165361Smckusic 	if (cylbp[i] > 0) {
7174651Smckusic 		/*
7185361Smckusic 		 * found a rotational position, now find the actual
7195361Smckusic 		 * block. A panic if none is actually there.
7204651Smckusic 		 */
7215361Smckusic 		pos = cylno % fs->fs_cpc;
7225361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
7236716Smckusick 		if (fs->fs_postbl[pos][i] == -1) {
7246716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
7256716Smckusick 			    pos, i, fs->fs_fsmnt);
7265361Smckusic 			panic("alloccgblk: cyl groups corrupted");
7276716Smckusick 		}
7286294Smckusick 		for (i = fs->fs_postbl[pos][i];; ) {
7295361Smckusic 			if (isblock(fs, cgp->cg_free, bno + i)) {
73011638Ssam 				bno = blkstofrags(fs, (bno + i));
7315361Smckusic 				goto gotit;
7325361Smckusic 			}
7336294Smckusick 			delta = fs->fs_rotbl[i];
7346294Smckusick 			if (delta <= 0 || delta > MAXBPC - i)
7354651Smckusic 				break;
7366294Smckusick 			i += delta;
7374651Smckusic 		}
7386716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
7395361Smckusic 		panic("alloccgblk: can't find blk in cyl");
7404359Smckusick 	}
7415361Smckusic norot:
7425361Smckusic 	/*
7435361Smckusic 	 * no blocks in the requested cylinder, so take next
7445361Smckusic 	 * available one in this cylinder group.
7455361Smckusic 	 */
7468628Sroot 	bno = mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7476567Smckusic 	if (bno < 0)
7486294Smckusick 		return (NULL);
7494651Smckusic 	cgp->cg_rotor = bno;
7504359Smckusick gotit:
75111638Ssam 	clrblock(fs, cgp->cg_free, (long)fragstoblks(fs, bno));
7524792Smckusic 	cgp->cg_cs.cs_nbfree--;
7534792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7545322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7555375Smckusic 	cylno = cbtocylno(fs, bno);
7565375Smckusic 	cgp->cg_b[cylno][cbtorpos(fs, bno)]--;
7575375Smckusic 	cgp->cg_btot[cylno]--;
7584359Smckusick 	fs->fs_fmod++;
7594651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7604359Smckusick }
7614359Smckusick 
7625375Smckusic /*
7635375Smckusic  * Determine whether an inode can be allocated.
7645375Smckusic  *
7655375Smckusic  * Check to see if an inode is available, and if it is,
7665375Smckusic  * allocate it using the following policy:
7675375Smckusic  *   1) allocate the requested inode.
7685375Smckusic  *   2) allocate the next available inode after the requested
7695375Smckusic  *      inode in the specified cylinder group.
7705375Smckusic  */
7719163Ssam ino_t
7725965Smckusic ialloccg(ip, cg, ipref, mode)
7735965Smckusic 	struct inode *ip;
7744359Smckusick 	int cg;
7754359Smckusick 	daddr_t ipref;
7764359Smckusick 	int mode;
7774359Smckusick {
7785965Smckusic 	register struct fs *fs;
7794463Smckusic 	register struct cg *cgp;
78016784Smckusick 	struct buf *bp;
78116784Smckusick 	int start, len, loc, map, i;
7824359Smckusick 
7835965Smckusic 	fs = ip->i_fs;
7845322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7856294Smckusick 		return (NULL);
786*30749Skarels #ifdef SECSIZE
787*30749Skarels 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
788*30749Skarels 	    fs->fs_dbsize);
789*30749Skarels #else SECSIZE
79010278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
791*30749Skarels #endif SECSIZE
7926531Smckusick 	cgp = bp->b_un.b_cg;
79315950Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC ||
79415950Smckusick 	    cgp->cg_cs.cs_nifree == 0) {
7955960Smckusic 		brelse(bp);
7966294Smckusick 		return (NULL);
7975960Smckusic 	}
7988105Sroot 	cgp->cg_time = time.tv_sec;
7994359Smckusick 	if (ipref) {
8004359Smckusick 		ipref %= fs->fs_ipg;
8014359Smckusick 		if (isclr(cgp->cg_iused, ipref))
8024359Smckusick 			goto gotit;
80316784Smckusick 	}
80416784Smckusick 	start = cgp->cg_irotor / NBBY;
80516784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
80616784Smckusick 	loc = skpc(0xff, len, &cgp->cg_iused[start]);
80716784Smckusick 	if (loc == 0) {
80817697Smckusick 		len = start + 1;
80917697Smckusick 		start = 0;
81017697Smckusick 		loc = skpc(0xff, len, &cgp->cg_iused[0]);
81117697Smckusick 		if (loc == 0) {
81217697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
81317697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
81417697Smckusick 			panic("ialloccg: map corrupted");
81517697Smckusick 			/* NOTREACHED */
81617697Smckusick 		}
81716784Smckusick 	}
81816784Smckusick 	i = start + len - loc;
81916784Smckusick 	map = cgp->cg_iused[i];
82016784Smckusick 	ipref = i * NBBY;
82116784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
82216784Smckusick 		if ((map & i) == 0) {
8234359Smckusick 			cgp->cg_irotor = ipref;
8244359Smckusick 			goto gotit;
8254359Smckusick 		}
8264359Smckusick 	}
82716784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
82816784Smckusick 	panic("ialloccg: block not in map");
82916784Smckusick 	/* NOTREACHED */
8304359Smckusick gotit:
8314359Smckusick 	setbit(cgp->cg_iused, ipref);
8324792Smckusic 	cgp->cg_cs.cs_nifree--;
8334792Smckusic 	fs->fs_cstotal.cs_nifree--;
8345322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
8354359Smckusick 	fs->fs_fmod++;
8364359Smckusick 	if ((mode & IFMT) == IFDIR) {
8374792Smckusic 		cgp->cg_cs.cs_ndir++;
8384792Smckusic 		fs->fs_cstotal.cs_ndir++;
8395322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8404359Smckusick 	}
8414359Smckusick 	bdwrite(bp);
8424359Smckusick 	return (cg * fs->fs_ipg + ipref);
8434359Smckusick }
8444359Smckusick 
8455375Smckusic /*
8465375Smckusic  * Free a block or fragment.
8475375Smckusic  *
8485375Smckusic  * The specified block or fragment is placed back in the
8495375Smckusic  * free map. If a fragment is deallocated, a possible
8505375Smckusic  * block reassembly is checked.
8515375Smckusic  */
8529163Ssam free(ip, bno, size)
8535965Smckusic 	register struct inode *ip;
8544359Smckusick 	daddr_t bno;
8555212Smckusic 	off_t size;
8564359Smckusick {
8574359Smckusick 	register struct fs *fs;
8584359Smckusick 	register struct cg *cgp;
8594359Smckusick 	register struct buf *bp;
8604463Smckusic 	int cg, blk, frags, bbase;
8614463Smckusic 	register int i;
8624359Smckusick 
8635965Smckusic 	fs = ip->i_fs;
8646716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8656716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8666716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
8674426Smckusic 		panic("free: bad size");
8686716Smckusick 	}
8695377Smckusic 	cg = dtog(fs, bno);
8706567Smckusic 	if (badblock(fs, bno)) {
8716567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
8724359Smckusick 		return;
8736567Smckusic 	}
874*30749Skarels #ifdef SECSIZE
875*30749Skarels 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
876*30749Skarels 	    fs->fs_dbsize);
877*30749Skarels #else SECSIZE
87810278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
879*30749Skarels #endif SECSIZE
8806531Smckusick 	cgp = bp->b_un.b_cg;
8816531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
8825960Smckusic 		brelse(bp);
8834359Smckusick 		return;
8845960Smckusic 	}
8858105Sroot 	cgp->cg_time = time.tv_sec;
8865377Smckusic 	bno = dtogd(fs, bno);
8875322Smckusic 	if (size == fs->fs_bsize) {
88811638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bno))) {
8896716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
8906716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
8914426Smckusic 			panic("free: freeing free block");
8926567Smckusic 		}
89311638Ssam 		setblock(fs, cgp->cg_free, fragstoblks(fs, bno));
8944792Smckusic 		cgp->cg_cs.cs_nbfree++;
8954792Smckusic 		fs->fs_cstotal.cs_nbfree++;
8965322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
8975375Smckusic 		i = cbtocylno(fs, bno);
8985375Smckusic 		cgp->cg_b[i][cbtorpos(fs, bno)]++;
8995375Smckusic 		cgp->cg_btot[i]++;
9004426Smckusic 	} else {
90117224Smckusick 		bbase = bno - fragnum(fs, bno);
9024463Smckusic 		/*
9034463Smckusic 		 * decrement the counts associated with the old frags
9044463Smckusic 		 */
9056294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
9065322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, -1);
9074463Smckusic 		/*
9084463Smckusic 		 * deallocate the fragment
9094463Smckusic 		 */
9105960Smckusic 		frags = numfrags(fs, size);
9114463Smckusic 		for (i = 0; i < frags; i++) {
9126716Smckusick 			if (isset(cgp->cg_free, bno + i)) {
9136716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
9146716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
9154426Smckusic 				panic("free: freeing free frag");
9166716Smckusick 			}
9174426Smckusic 			setbit(cgp->cg_free, bno + i);
9184426Smckusic 		}
9196294Smckusick 		cgp->cg_cs.cs_nffree += i;
9206294Smckusick 		fs->fs_cstotal.cs_nffree += i;
9216294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
9224463Smckusic 		/*
9234463Smckusic 		 * add back in counts associated with the new frags
9244463Smckusic 		 */
9256294Smckusick 		blk = blkmap(fs, cgp->cg_free, bbase);
9265322Smckusic 		fragacct(fs, blk, cgp->cg_frsum, 1);
9274463Smckusic 		/*
9284463Smckusic 		 * if a complete block has been reassembled, account for it
9294463Smckusic 		 */
93011638Ssam 		if (isblock(fs, cgp->cg_free, fragstoblks(fs, bbase))) {
9315322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
9325322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
9335322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
9344792Smckusic 			cgp->cg_cs.cs_nbfree++;
9354792Smckusic 			fs->fs_cstotal.cs_nbfree++;
9365322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
9375375Smckusic 			i = cbtocylno(fs, bbase);
9385375Smckusic 			cgp->cg_b[i][cbtorpos(fs, bbase)]++;
9395375Smckusic 			cgp->cg_btot[i]++;
9404426Smckusic 		}
9414426Smckusic 	}
9424359Smckusick 	fs->fs_fmod++;
9434359Smckusick 	bdwrite(bp);
9444359Smckusick }
9454359Smckusick 
9465375Smckusic /*
9475375Smckusic  * Free an inode.
9485375Smckusic  *
9495375Smckusic  * The specified inode is placed back in the free map.
9505375Smckusic  */
9515965Smckusic ifree(ip, ino, mode)
9525965Smckusic 	struct inode *ip;
9534359Smckusick 	ino_t ino;
9544359Smckusick 	int mode;
9554359Smckusick {
9564359Smckusick 	register struct fs *fs;
9574359Smckusick 	register struct cg *cgp;
9584359Smckusick 	register struct buf *bp;
9594359Smckusick 	int cg;
9604359Smckusick 
9615965Smckusic 	fs = ip->i_fs;
9626716Smckusick 	if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
9636716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9646716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9654359Smckusick 		panic("ifree: range");
9666716Smckusick 	}
9675377Smckusic 	cg = itog(fs, ino);
968*30749Skarels #ifdef SECSIZE
969*30749Skarels 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
970*30749Skarels 	    fs->fs_dbsize);
971*30749Skarels #else SECSIZE
97210278Smckusick 	bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
973*30749Skarels #endif SECSIZE
9746531Smckusick 	cgp = bp->b_un.b_cg;
9756531Smckusick 	if (bp->b_flags & B_ERROR || cgp->cg_magic != CG_MAGIC) {
9765960Smckusic 		brelse(bp);
9774359Smckusick 		return;
9785960Smckusic 	}
9798105Sroot 	cgp->cg_time = time.tv_sec;
9804359Smckusick 	ino %= fs->fs_ipg;
9816716Smckusick 	if (isclr(cgp->cg_iused, ino)) {
9826716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
9836716Smckusick 		    ip->i_dev, ino, fs->fs_fsmnt);
9844359Smckusick 		panic("ifree: freeing free inode");
9856716Smckusick 	}
9864359Smckusick 	clrbit(cgp->cg_iused, ino);
98716784Smckusick 	if (ino < cgp->cg_irotor)
98816784Smckusick 		cgp->cg_irotor = ino;
9894792Smckusic 	cgp->cg_cs.cs_nifree++;
9904792Smckusic 	fs->fs_cstotal.cs_nifree++;
9915322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
9924359Smckusick 	if ((mode & IFMT) == IFDIR) {
9934792Smckusic 		cgp->cg_cs.cs_ndir--;
9944792Smckusic 		fs->fs_cstotal.cs_ndir--;
9955322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
9964359Smckusick 	}
9974359Smckusick 	fs->fs_fmod++;
9984359Smckusick 	bdwrite(bp);
9994359Smckusick }
10004359Smckusick 
10014463Smckusic /*
10025375Smckusic  * Find a block of the specified size in the specified cylinder group.
10035375Smckusic  *
10044651Smckusic  * It is a panic if a request is made to find a block if none are
10054651Smckusic  * available.
10064651Smckusic  */
10074651Smckusic daddr_t
10084651Smckusic mapsearch(fs, cgp, bpref, allocsiz)
10094651Smckusic 	register struct fs *fs;
10104651Smckusic 	register struct cg *cgp;
10114651Smckusic 	daddr_t bpref;
10124651Smckusic 	int allocsiz;
10134651Smckusic {
10144651Smckusic 	daddr_t bno;
10154651Smckusic 	int start, len, loc, i;
10164651Smckusic 	int blk, field, subfield, pos;
10174651Smckusic 
10184651Smckusic 	/*
10194651Smckusic 	 * find the fragment by searching through the free block
10204651Smckusic 	 * map for an appropriate bit pattern
10214651Smckusic 	 */
10224651Smckusic 	if (bpref)
10235377Smckusic 		start = dtogd(fs, bpref) / NBBY;
10244651Smckusic 	else
10254651Smckusic 		start = cgp->cg_frotor / NBBY;
10265398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
102712755Ssam 	loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[start],
102812755Ssam 		(caddr_t)fragtbl[fs->fs_frag],
102912755Ssam 		(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
10304651Smckusic 	if (loc == 0) {
10316531Smckusick 		len = start + 1;
10326531Smckusick 		start = 0;
103317697Smckusick 		loc = scanc((unsigned)len, (caddr_t)&cgp->cg_free[0],
103412755Ssam 			(caddr_t)fragtbl[fs->fs_frag],
103512755Ssam 			(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
103616784Smckusick 		if (loc == 0) {
103716784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
103816784Smckusick 			    start, len, fs->fs_fsmnt);
103916784Smckusick 			panic("alloccg: map corrupted");
104017697Smckusick 			/* NOTREACHED */
104116784Smckusick 		}
10424651Smckusic 	}
10434651Smckusic 	bno = (start + len - loc) * NBBY;
10444651Smckusic 	cgp->cg_frotor = bno;
10454651Smckusic 	/*
10464651Smckusic 	 * found the byte in the map
10474651Smckusic 	 * sift through the bits to find the selected frag
10484651Smckusic 	 */
10496294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
10506294Smckusick 		blk = blkmap(fs, cgp->cg_free, bno);
10514651Smckusic 		blk <<= 1;
10524651Smckusic 		field = around[allocsiz];
10534651Smckusic 		subfield = inside[allocsiz];
10545322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10556294Smckusick 			if ((blk & field) == subfield)
10566294Smckusick 				return (bno + pos);
10574651Smckusic 			field <<= 1;
10584651Smckusic 			subfield <<= 1;
10594651Smckusic 		}
10604651Smckusic 	}
10616716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
10624651Smckusic 	panic("alloccg: block not in map");
10636531Smckusick 	return (-1);
10644651Smckusic }
10654651Smckusic 
10664651Smckusic /*
10675375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10685375Smckusic  *
10695375Smckusic  * The form of the error message is:
10704359Smckusick  *	fs: error message
10714359Smckusick  */
10724359Smckusick fserr(fs, cp)
10734359Smckusick 	struct fs *fs;
10744359Smckusick 	char *cp;
10754359Smckusick {
10764359Smckusick 
107724839Seric 	log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp);
10784359Smckusick }
1079