xref: /csrg-svn/sys/ufs/ffs/ffs_alloc.c (revision 53244)
123394Smckusick /*
237735Smckusick  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
334432Sbostic  * All rights reserved.
423394Smckusick  *
544536Sbostic  * %sccs.include.redist.c%
634432Sbostic  *
7*53244Smckusick  *	@(#)ffs_alloc.c	7.32 (Berkeley) 04/21/92
823394Smckusick  */
94359Smckusick 
1051469Sbostic #include <sys/param.h>
1151469Sbostic #include <sys/systm.h>
1251469Sbostic #include <sys/buf.h>
1351469Sbostic #include <sys/proc.h>
1451469Sbostic #include <sys/vnode.h>
1551469Sbostic #include <sys/kernel.h>
1651469Sbostic #include <sys/syslog.h>
174359Smckusick 
1851469Sbostic #include <ufs/ufs/quota.h>
1951469Sbostic #include <ufs/ufs/inode.h>
2047571Skarels 
2151469Sbostic #include <ufs/ffs/fs.h>
2251469Sbostic #include <ufs/ffs/ffs_extern.h>
234359Smckusick 
2451469Sbostic extern u_long nextgennumber;
2551469Sbostic 
2651469Sbostic static daddr_t	ffs_alloccg __P((struct inode *, int, daddr_t, int));
2751469Sbostic static daddr_t	ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t));
2851469Sbostic static ino_t	ffs_dirpref __P((struct fs *));
2951469Sbostic static daddr_t	ffs_fragextend __P((struct inode *, int, long, int, int));
3051469Sbostic static void	ffs_fserr __P((struct fs *, u_int, char *));
3151469Sbostic static u_long	ffs_hashalloc
3251469Sbostic 		    __P((struct inode *, int, long, int, u_long (*)()));
3351469Sbostic static ino_t	ffs_ialloccg __P((struct inode *, int, daddr_t, int));
3451469Sbostic static daddr_t	ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int));
3551469Sbostic 
365375Smckusic /*
375375Smckusic  * Allocate a block in the file system.
385375Smckusic  *
395375Smckusic  * The size of the requested block is given, which must be some
405375Smckusic  * multiple of fs_fsize and <= fs_bsize.
415375Smckusic  * A preference may be optionally specified. If a preference is given
425375Smckusic  * the following hierarchy is used to allocate a block:
435375Smckusic  *   1) allocate the requested block.
445375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
455375Smckusic  *   3) allocate a block in the same cylinder group.
465375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
475375Smckusic  *      available block is located.
485375Smckusic  * If no block preference is given the following heirarchy is used
495375Smckusic  * to allocate a block:
505375Smckusic  *   1) allocate a block in the cylinder group that contains the
515375Smckusic  *      inode for the file.
525375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
535375Smckusic  *      available block is located.
545375Smckusic  */
55*53244Smckusick ffs_alloc(ip, lbn, bpref, size, cred, bnp)
564463Smckusic 	register struct inode *ip;
5739678Smckusick 	daddr_t lbn, bpref;
584359Smckusick 	int size;
59*53244Smckusick 	struct ucred *cred;
6039678Smckusick 	daddr_t *bnp;
614359Smckusick {
624359Smckusick 	daddr_t bno;
634359Smckusick 	register struct fs *fs;
644463Smckusic 	register struct buf *bp;
6537735Smckusick 	int cg, error;
664359Smckusick 
6739678Smckusick 	*bnp = 0;
685965Smckusic 	fs = ip->i_fs;
69*53244Smckusick #ifdef DIAGNOSTIC
706716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
716716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
726716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7351469Sbostic 		panic("ffs_alloc: bad size");
746716Smckusick 	}
75*53244Smckusick 	if (cred == NOCRED)
76*53244Smckusick 		panic("ffs_alloc: missing credential\n");
77*53244Smckusick #endif /* DIAGNOSTIC */
785322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
794359Smckusick 		goto nospace;
8041309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
814792Smckusic 		goto nospace;
827650Ssam #ifdef QUOTA
8341309Smckusick 	if (error = chkdq(ip, (long)btodb(size), cred, 0))
8437735Smckusick 		return (error);
857483Skre #endif
864948Smckusic 	if (bpref >= fs->fs_size)
874948Smckusic 		bpref = 0;
884359Smckusick 	if (bpref == 0)
895377Smckusic 		cg = itog(fs, ip->i_number);
904359Smckusick 	else
915377Smckusic 		cg = dtog(fs, bpref);
9251469Sbostic 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
9351469Sbostic 	    (u_long (*)())ffs_alloccg);
9439678Smckusick 	if (bno > 0) {
9539678Smckusick 		ip->i_blocks += btodb(size);
9639678Smckusick 		ip->i_flag |= IUPD|ICHG;
9739678Smckusick 		*bnp = bno;
9839678Smckusick 		return (0);
9939678Smckusick 	}
10045173Smckusick #ifdef QUOTA
10145173Smckusick 	/*
10245173Smckusick 	 * Restore user's disk quota because allocation failed.
10345173Smckusick 	 */
10445173Smckusick 	(void) chkdq(ip, (long)-btodb(size), cred, FORCE);
10545173Smckusick #endif
1064359Smckusick nospace:
10751469Sbostic 	ffs_fserr(fs, cred->cr_uid, "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  */
120*53244Smckusick ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
1215965Smckusic 	register struct inode *ip;
12253059Smckusick 	daddr_t lbprev;
12339678Smckusick 	daddr_t bpref;
1244426Smckusic 	int osize, nsize;
125*53244Smckusick 	struct ucred *cred;
12637735Smckusick 	struct buf **bpp;
1274426Smckusic {
1284426Smckusic 	register struct fs *fs;
12937735Smckusick 	struct buf *bp, *obp;
13045719Smckusick 	int cg, request, error;
13145719Smckusick 	daddr_t bprev, bno;
1324426Smckusic 
13337735Smckusick 	*bpp = 0;
1345965Smckusic 	fs = ip->i_fs;
135*53244Smckusick #ifdef DIAGNOSTIC
1365960Smckusic 	if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
1376716Smckusick 	    (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
13851469Sbostic 		printf(
13951469Sbostic 		    "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1406716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
14151469Sbostic 		panic("ffs_realloccg: bad size");
1426716Smckusick 	}
143*53244Smckusick 	if (cred == NOCRED)
144*53244Smckusick 		panic("ffs_realloccg: missing credential\n");
145*53244Smckusick #endif /* DIAGNOSTIC */
14641309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1474792Smckusic 		goto nospace;
14839678Smckusick 	if ((bprev = ip->i_db[lbprev]) == 0) {
1496716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1506716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
15151469Sbostic 		panic("ffs_realloccg: bad bprev");
1526716Smckusick 	}
15339678Smckusick 	/*
15439678Smckusick 	 * Allocate the extra space in the buffer.
15539678Smckusick 	 */
15639678Smckusick 	if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
15739678Smckusick 		brelse(bp);
15839678Smckusick 		return (error);
15939678Smckusick 	}
16045173Smckusick #ifdef QUOTA
16145173Smckusick 	if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) {
16245173Smckusick 		brelse(bp);
16345173Smckusick 		return (error);
16445173Smckusick 	}
16545173Smckusick #endif
16639678Smckusick 	/*
16739678Smckusick 	 * Check for extension in the existing location.
16839678Smckusick 	 */
1696294Smckusick 	cg = dtog(fs, bprev);
17051469Sbostic 	if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) {
17139887Smckusick 		if (bp->b_blkno != fsbtodb(fs, bno))
17239678Smckusick 			panic("bad blockno");
17339762Smckusick 		ip->i_blocks += btodb(nsize - osize);
17439762Smckusick 		ip->i_flag |= IUPD|ICHG;
17548948Smckusick 		allocbuf(bp, nsize);
17648948Smckusick 		bp->b_flags |= B_DONE;
17748948Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
17837735Smckusick 		*bpp = bp;
17937735Smckusick 		return (0);
1804463Smckusic 	}
18139678Smckusick 	/*
18239678Smckusick 	 * Allocate a new disk location.
18339678Smckusick 	 */
1844948Smckusic 	if (bpref >= fs->fs_size)
1854948Smckusic 		bpref = 0;
18626253Skarels 	switch ((int)fs->fs_optim) {
18725256Smckusick 	case FS_OPTSPACE:
18825256Smckusick 		/*
18925256Smckusick 		 * Allocate an exact sized fragment. Although this makes
19025256Smckusick 		 * best use of space, we will waste time relocating it if
19125256Smckusick 		 * the file continues to grow. If the fragmentation is
19225256Smckusick 		 * less than half of the minimum free reserve, we choose
19325256Smckusick 		 * to begin optimizing for time.
19425256Smckusick 		 */
19524698Smckusick 		request = nsize;
19625256Smckusick 		if (fs->fs_minfree < 5 ||
19725256Smckusick 		    fs->fs_cstotal.cs_nffree >
19825256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
19925256Smckusick 			break;
20025256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
20125256Smckusick 			fs->fs_fsmnt);
20225256Smckusick 		fs->fs_optim = FS_OPTTIME;
20325256Smckusick 		break;
20425256Smckusick 	case FS_OPTTIME:
20525256Smckusick 		/*
20651469Sbostic 		 * At this point we have discovered a file that is trying to
20751469Sbostic 		 * grow a small fragment to a larger fragment. To save time,
20851469Sbostic 		 * we allocate a full sized block, then free the unused portion.
20951469Sbostic 		 * If the file continues to grow, the `ffs_fragextend' call
21051469Sbostic 		 * above will be able to grow it in place without further
21151469Sbostic 		 * copying. If aberrant programs cause disk fragmentation to
21251469Sbostic 		 * grow within 2% of the free reserve, we choose to begin
21351469Sbostic 		 * optimizing for space.
21425256Smckusick 		 */
21524698Smckusick 		request = fs->fs_bsize;
21625256Smckusick 		if (fs->fs_cstotal.cs_nffree <
21725256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
21825256Smckusick 			break;
21925256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
22025256Smckusick 			fs->fs_fsmnt);
22125256Smckusick 		fs->fs_optim = FS_OPTSPACE;
22225256Smckusick 		break;
22325256Smckusick 	default:
22425256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
22525256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
22651469Sbostic 		panic("ffs_realloccg: bad optim");
22725256Smckusick 		/* NOTREACHED */
22825256Smckusick 	}
22951469Sbostic 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
23051469Sbostic 	    (u_long (*)())ffs_alloccg);
2316567Smckusic 	if (bno > 0) {
23245719Smckusick 		bp->b_blkno = fsbtodb(fs, bno);
23345719Smckusick 		(void) vnode_pager_uncache(ITOV(ip));
23453059Smckusick 		ffs_blkfree(ip, bprev, (long)osize);
23524698Smckusick 		if (nsize < request)
23651469Sbostic 			ffs_blkfree(ip, bno + numfrags(fs, nsize),
23753059Smckusick 			    (long)(request - nsize));
23812643Ssam 		ip->i_blocks += btodb(nsize - osize);
23912643Ssam 		ip->i_flag |= IUPD|ICHG;
24048948Smckusick 		allocbuf(bp, nsize);
24148948Smckusick 		bp->b_flags |= B_DONE;
24248948Smckusick 		bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
24337735Smckusick 		*bpp = bp;
24437735Smckusick 		return (0);
2454463Smckusic 	}
24645173Smckusick #ifdef QUOTA
24745173Smckusick 	/*
24845173Smckusick 	 * Restore user's disk quota because allocation failed.
24945173Smckusick 	 */
25045173Smckusick 	(void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
25145173Smckusick #endif
25239678Smckusick 	brelse(bp);
2534792Smckusic nospace:
2544463Smckusic 	/*
2554463Smckusic 	 * no space available
2564463Smckusic 	 */
25751469Sbostic 	ffs_fserr(fs, cred->cr_uid, "file system full");
2584426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
25937735Smckusick 	return (ENOSPC);
2604426Smckusic }
2614426Smckusic 
2625375Smckusic /*
2635375Smckusic  * Allocate an inode in the file system.
2645375Smckusic  *
26551469Sbostic  * If allocating a directory, use ffs_dirpref to select the inode.
26651469Sbostic  * If allocating in a directory, the following hierarchy is followed:
26751469Sbostic  *   1) allocate the preferred inode.
2685375Smckusic  *   2) allocate an inode in the same cylinder group.
2695375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
2705375Smckusic  *      available inode is located.
2715375Smckusic  * If no inode preference is given the following heirarchy is used
2725375Smckusic  * to allocate an inode:
2735375Smckusic  *   1) allocate an inode in cylinder group 0.
2745375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
2755375Smckusic  *      available inode is located.
2765375Smckusic  */
27751541Smckusick ffs_valloc(pvp, mode, cred, vpp)
27851541Smckusick 	register struct vnode *pvp;
2794359Smckusick 	int mode;
28041309Smckusick 	struct ucred *cred;
28151541Smckusick 	struct vnode **vpp;
2824359Smckusick {
28351541Smckusick 	register struct inode *pip;
2844359Smckusick 	register struct fs *fs;
2854359Smckusick 	register struct inode *ip;
28651469Sbostic 	ino_t ino, ipref;
28737735Smckusick 	int cg, error;
2884359Smckusick 
28951541Smckusick 	*vpp = NULL;
29051541Smckusick 	pip = VTOI(pvp);
2915965Smckusic 	fs = pip->i_fs;
2924792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
2934359Smckusick 		goto noinodes;
29451469Sbostic 
29551469Sbostic 	if ((mode & IFMT) == IFDIR)
29651541Smckusick 		ipref = ffs_dirpref(fs);
29751469Sbostic 	else
29851469Sbostic 		ipref = pip->i_number;
2994948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
3004948Smckusic 		ipref = 0;
3015377Smckusic 	cg = itog(fs, ipref);
30251469Sbostic 	ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_ialloccg);
3034359Smckusick 	if (ino == 0)
3044359Smckusick 		goto noinodes;
30551541Smckusick 	error = ffs_vget(pvp->v_mount, ino, vpp);
30637735Smckusick 	if (error) {
30751541Smckusick 		ffs_vfree(pvp, ino, mode);
30837735Smckusick 		return (error);
3094359Smckusick 	}
31051541Smckusick 	ip = VTOI(*vpp);
3116716Smckusick 	if (ip->i_mode) {
3126716Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
3136716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
31451541Smckusick 		panic("ffs_valloc: dup alloc");
3156716Smckusick 	}
31612643Ssam 	if (ip->i_blocks) {				/* XXX */
31712643Ssam 		printf("free inode %s/%d had %d blocks\n",
31812643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
31912643Ssam 		ip->i_blocks = 0;
32012643Ssam 	}
32139516Smckusick 	ip->i_flags = 0;
32238255Smckusick 	/*
32338255Smckusick 	 * Set up a new generation number for this inode.
32438255Smckusick 	 */
32538255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
32638255Smckusick 		nextgennumber = time.tv_sec;
32738255Smckusick 	ip->i_gen = nextgennumber;
32837735Smckusick 	return (0);
3294359Smckusick noinodes:
33051469Sbostic 	ffs_fserr(fs, cred->cr_uid, "out of inodes");
3316294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
33237735Smckusick 	return (ENOSPC);
3334359Smckusick }
3344359Smckusick 
3354651Smckusic /*
3365375Smckusic  * Find a cylinder to place a directory.
3375375Smckusic  *
3385375Smckusic  * The policy implemented by this algorithm is to select from
3395375Smckusic  * among those cylinder groups with above the average number of
3405375Smckusic  * free inodes, the one with the smallest number of directories.
3414651Smckusic  */
34251469Sbostic static ino_t
34351469Sbostic ffs_dirpref(fs)
3445965Smckusic 	register struct fs *fs;
3454359Smckusick {
3464651Smckusic 	int cg, minndir, mincg, avgifree;
3474359Smckusick 
3484792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
3494651Smckusic 	minndir = fs->fs_ipg;
3504359Smckusick 	mincg = 0;
3514651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
3525322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
3535322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
3544359Smckusick 			mincg = cg;
3555322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
3564359Smckusick 		}
3579163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
3584359Smckusick }
3594359Smckusick 
3604651Smckusic /*
3619163Ssam  * Select the desired position for the next block in a file.  The file is
3629163Ssam  * logically divided into sections. The first section is composed of the
3639163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
3649163Ssam  *
3659163Ssam  * If no blocks have been allocated in the first section, the policy is to
3669163Ssam  * request a block in the same cylinder group as the inode that describes
3679163Ssam  * the file. If no blocks have been allocated in any other section, the
3689163Ssam  * policy is to place the section in a cylinder group with a greater than
3699163Ssam  * average number of free blocks.  An appropriate cylinder group is found
37017696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
37117696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
37217696Smckusick  * cylinder group from which the previous allocation was made. The sweep
37317696Smckusick  * continues until a cylinder group with greater than the average number
37417696Smckusick  * of free blocks is found. If the allocation is for the first block in an
37517696Smckusick  * indirect block, the information on the previous allocation is unavailable;
37617696Smckusick  * here a best guess is made based upon the logical block number being
37717696Smckusick  * allocated.
3789163Ssam  *
3799163Ssam  * If a section is already partially allocated, the policy is to
3809163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
3819163Ssam  * contiguous blocks and the beginning of the next is physically separated
3829163Ssam  * so that the disk head will be in transit between them for at least
3839163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
3849163Ssam  * schedule another I/O transfer.
3854651Smckusic  */
3865212Smckusic daddr_t
38751469Sbostic ffs_blkpref(ip, lbn, indx, bap)
3889163Ssam 	struct inode *ip;
3899163Ssam 	daddr_t lbn;
3909163Ssam 	int indx;
3919163Ssam 	daddr_t *bap;
3929163Ssam {
3935965Smckusic 	register struct fs *fs;
39417696Smckusick 	register int cg;
39517696Smckusick 	int avgbfree, startcg;
3969163Ssam 	daddr_t nextblk;
3974651Smckusic 
3989163Ssam 	fs = ip->i_fs;
3999163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
4009163Ssam 		if (lbn < NDADDR) {
4019163Ssam 			cg = itog(fs, ip->i_number);
4025322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
4034651Smckusic 		}
4049163Ssam 		/*
4059163Ssam 		 * Find a cylinder with greater than average number of
4069163Ssam 		 * unused data blocks.
4079163Ssam 		 */
40817696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
40917696Smckusick 			startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
41017696Smckusick 		else
41117696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
41217696Smckusick 		startcg %= fs->fs_ncg;
4139163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
41417696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
4159163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4169163Ssam 				fs->fs_cgrotor = cg;
4179163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4189163Ssam 			}
41917696Smckusick 		for (cg = 0; cg <= startcg; cg++)
4209163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
4219163Ssam 				fs->fs_cgrotor = cg;
4229163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
4239163Ssam 			}
4249163Ssam 		return (NULL);
4259163Ssam 	}
4269163Ssam 	/*
4279163Ssam 	 * One or more previous blocks have been laid out. If less
4289163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
4299163Ssam 	 * next block is requested contiguously, otherwise it is
4309163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
4319163Ssam 	 */
4329163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
4339163Ssam 	if (indx > fs->fs_maxcontig &&
43411638Ssam 	    bap[indx - fs->fs_maxcontig] + blkstofrags(fs, fs->fs_maxcontig)
4359163Ssam 	    != nextblk)
4369163Ssam 		return (nextblk);
4379163Ssam 	if (fs->fs_rotdelay != 0)
4389163Ssam 		/*
4399163Ssam 		 * Here we convert ms of delay to frags as:
4409163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
4419163Ssam 		 *	((sect/frag) * (ms/sec))
4429163Ssam 		 * then round up to the next block.
4439163Ssam 		 */
4449163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
4459163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
4469163Ssam 	return (nextblk);
4474651Smckusic }
4484651Smckusic 
4495375Smckusic /*
4505375Smckusic  * Implement the cylinder overflow algorithm.
4515375Smckusic  *
4525375Smckusic  * The policy implemented by this algorithm is:
4535375Smckusic  *   1) allocate the block in its requested cylinder group.
4545375Smckusic  *   2) quadradically rehash on the cylinder group number.
4555375Smckusic  *   3) brute force search for a free block.
4565375Smckusic  */
4575212Smckusic /*VARARGS5*/
45851469Sbostic static u_long
45951469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator)
4605965Smckusic 	struct inode *ip;
4614359Smckusick 	int cg;
4624359Smckusick 	long pref;
4634359Smckusick 	int size;	/* size for data blocks, mode for inodes */
4645212Smckusic 	u_long (*allocator)();
4654359Smckusick {
4665965Smckusic 	register struct fs *fs;
4674359Smckusick 	long result;
4684359Smckusick 	int i, icg = cg;
4694359Smckusick 
4705965Smckusic 	fs = ip->i_fs;
4714359Smckusick 	/*
4724359Smckusick 	 * 1: preferred cylinder group
4734359Smckusick 	 */
4745965Smckusic 	result = (*allocator)(ip, cg, pref, size);
4754359Smckusick 	if (result)
4764359Smckusick 		return (result);
4774359Smckusick 	/*
4784359Smckusick 	 * 2: quadratic rehash
4794359Smckusick 	 */
4804359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
4814359Smckusick 		cg += i;
4824359Smckusick 		if (cg >= fs->fs_ncg)
4834359Smckusick 			cg -= fs->fs_ncg;
4845965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4854359Smckusick 		if (result)
4864359Smckusick 			return (result);
4874359Smckusick 	}
4884359Smckusick 	/*
4894359Smckusick 	 * 3: brute force search
49010847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
49110847Ssam 	 * and 1 is always checked in the quadratic rehash.
4924359Smckusick 	 */
49310848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
49410847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
4955965Smckusic 		result = (*allocator)(ip, cg, 0, size);
4964359Smckusick 		if (result)
4974359Smckusick 			return (result);
4984359Smckusick 		cg++;
4994359Smckusick 		if (cg == fs->fs_ncg)
5004359Smckusick 			cg = 0;
5014359Smckusick 	}
5026294Smckusick 	return (NULL);
5034359Smckusick }
5044359Smckusick 
5055375Smckusic /*
5065375Smckusic  * Determine whether a fragment can be extended.
5075375Smckusic  *
5085375Smckusic  * Check to see if the necessary fragments are available, and
5095375Smckusic  * if they are, allocate them.
5105375Smckusic  */
51151469Sbostic static daddr_t
51251469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize)
5135965Smckusic 	struct inode *ip;
5144426Smckusic 	int cg;
5154463Smckusic 	long bprev;
5164426Smckusic 	int osize, nsize;
5174426Smckusic {
5185965Smckusic 	register struct fs *fs;
5194463Smckusic 	register struct cg *cgp;
52037735Smckusick 	struct buf *bp;
5214463Smckusic 	long bno;
5224463Smckusic 	int frags, bbase;
52337735Smckusick 	int i, error;
5244426Smckusic 
5255965Smckusic 	fs = ip->i_fs;
52617224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
5276531Smckusick 		return (NULL);
5285960Smckusic 	frags = numfrags(fs, nsize);
52917224Smckusick 	bbase = fragnum(fs, bprev);
53017224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
53130749Skarels 		/* cannot extend across a block boundary */
5326294Smckusick 		return (NULL);
5334463Smckusic 	}
53437735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
53538776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
53637735Smckusick 	if (error) {
53737735Smckusick 		brelse(bp);
53837735Smckusick 		return (NULL);
53937735Smckusick 	}
5406531Smckusick 	cgp = bp->b_un.b_cg;
54137735Smckusick 	if (!cg_chkmagic(cgp)) {
5425960Smckusic 		brelse(bp);
5436294Smckusick 		return (NULL);
5445960Smckusic 	}
5458105Sroot 	cgp->cg_time = time.tv_sec;
5465377Smckusic 	bno = dtogd(fs, bprev);
5475960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
54834143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
5495361Smckusic 			brelse(bp);
5506294Smckusick 			return (NULL);
5515361Smckusic 		}
5525361Smckusic 	/*
5535361Smckusic 	 * the current fragment can be extended
5545361Smckusic 	 * deduct the count on fragment being extended into
5555361Smckusic 	 * increase the count on the remaining fragment (if any)
5565361Smckusic 	 * allocate the extended piece
5575361Smckusic 	 */
5585361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
55934143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
5604463Smckusic 			break;
5615960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
5625361Smckusic 	if (i != frags)
5635361Smckusic 		cgp->cg_frsum[i - frags]++;
5645960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
56534143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
5665361Smckusic 		cgp->cg_cs.cs_nffree--;
5675361Smckusic 		fs->fs_cstotal.cs_nffree--;
5685361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
5694463Smckusic 	}
57050893Smckusick 	fs->fs_fmod = 1;
5715361Smckusic 	bdwrite(bp);
5725361Smckusic 	return (bprev);
5734426Smckusic }
5744426Smckusic 
5755375Smckusic /*
5765375Smckusic  * Determine whether a block can be allocated.
5775375Smckusic  *
5785375Smckusic  * Check to see if a block of the apprpriate size is available,
5795375Smckusic  * and if it is, allocate it.
5805375Smckusic  */
58151779Smarc static daddr_t
58251469Sbostic ffs_alloccg(ip, cg, bpref, size)
5835965Smckusic 	struct inode *ip;
5844359Smckusick 	int cg;
5854359Smckusick 	daddr_t bpref;
5864359Smckusick 	int size;
5874359Smckusick {
5885965Smckusic 	register struct fs *fs;
5894463Smckusic 	register struct cg *cgp;
59037735Smckusick 	struct buf *bp;
5914463Smckusic 	register int i;
59237735Smckusick 	int error, bno, frags, allocsiz;
5934359Smckusick 
5945965Smckusic 	fs = ip->i_fs;
5955322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
5966294Smckusick 		return (NULL);
59737735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
59838776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
59937735Smckusick 	if (error) {
60037735Smckusick 		brelse(bp);
60137735Smckusick 		return (NULL);
60237735Smckusick 	}
6036531Smckusick 	cgp = bp->b_un.b_cg;
60437735Smckusick 	if (!cg_chkmagic(cgp) ||
60515950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
6065960Smckusic 		brelse(bp);
6076294Smckusick 		return (NULL);
6085960Smckusic 	}
6098105Sroot 	cgp->cg_time = time.tv_sec;
6105322Smckusic 	if (size == fs->fs_bsize) {
61151469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
6124463Smckusic 		bdwrite(bp);
6134463Smckusic 		return (bno);
6144463Smckusic 	}
6154463Smckusic 	/*
6164463Smckusic 	 * check to see if any fragments are already available
6174463Smckusic 	 * allocsiz is the size which will be allocated, hacking
6184463Smckusic 	 * it down to a smaller size if necessary
6194463Smckusic 	 */
6205960Smckusic 	frags = numfrags(fs, size);
6215322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
6224463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
6234463Smckusic 			break;
6245322Smckusic 	if (allocsiz == fs->fs_frag) {
6254463Smckusic 		/*
6264463Smckusic 		 * no fragments were available, so a block will be
6274463Smckusic 		 * allocated, and hacked up
6284463Smckusic 		 */
6294792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
6304463Smckusic 			brelse(bp);
6316294Smckusick 			return (NULL);
6324463Smckusic 		}
63351469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
6345377Smckusic 		bpref = dtogd(fs, bno);
6355322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
63634143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
6375322Smckusic 		i = fs->fs_frag - frags;
6384792Smckusic 		cgp->cg_cs.cs_nffree += i;
6394792Smckusic 		fs->fs_cstotal.cs_nffree += i;
6405322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
64150893Smckusick 		fs->fs_fmod = 1;
6424463Smckusic 		cgp->cg_frsum[i]++;
6434463Smckusic 		bdwrite(bp);
6444463Smckusic 		return (bno);
6454463Smckusic 	}
64651469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
64715950Smckusick 	if (bno < 0) {
64815950Smckusick 		brelse(bp);
6496294Smckusick 		return (NULL);
65015950Smckusick 	}
6514463Smckusic 	for (i = 0; i < frags; i++)
65234143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
6534792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
6544792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
6555322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
65650893Smckusick 	fs->fs_fmod = 1;
6574463Smckusic 	cgp->cg_frsum[allocsiz]--;
6584463Smckusic 	if (frags != allocsiz)
6594463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
6604463Smckusic 	bdwrite(bp);
6614463Smckusic 	return (cg * fs->fs_fpg + bno);
6624463Smckusic }
6634463Smckusic 
6645375Smckusic /*
6655375Smckusic  * Allocate a block in a cylinder group.
6665375Smckusic  *
6675375Smckusic  * This algorithm implements the following policy:
6685375Smckusic  *   1) allocate the requested block.
6695375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
6705375Smckusic  *   3) allocate the next available block on the block rotor for the
6715375Smckusic  *      specified cylinder group.
6725375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
6735375Smckusic  * blocks may be fragmented by the routine that allocates them.
6745375Smckusic  */
67551469Sbostic static daddr_t
67651469Sbostic ffs_alloccgblk(fs, cgp, bpref)
6775965Smckusic 	register struct fs *fs;
6784463Smckusic 	register struct cg *cgp;
6794463Smckusic 	daddr_t bpref;
6804463Smckusic {
6814651Smckusic 	daddr_t bno;
6826294Smckusick 	int cylno, pos, delta;
6834651Smckusic 	short *cylbp;
6845361Smckusic 	register int i;
6854463Smckusic 
6864651Smckusic 	if (bpref == 0) {
6874651Smckusic 		bpref = cgp->cg_rotor;
6885361Smckusic 		goto norot;
6895361Smckusic 	}
69017224Smckusick 	bpref = blknum(fs, bpref);
6915377Smckusic 	bpref = dtogd(fs, bpref);
6925361Smckusic 	/*
6935361Smckusic 	 * if the requested block is available, use it
6945361Smckusic 	 */
69551469Sbostic 	if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
6965361Smckusic 		bno = bpref;
6975361Smckusic 		goto gotit;
6985361Smckusic 	}
6995361Smckusic 	/*
7005361Smckusic 	 * check for a block available on the same cylinder
7015361Smckusic 	 */
7025361Smckusic 	cylno = cbtocylno(fs, bpref);
70334143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
7045375Smckusic 		goto norot;
7055375Smckusic 	if (fs->fs_cpc == 0) {
7065375Smckusic 		/*
7075375Smckusic 		 * block layout info is not available, so just have
7085375Smckusic 		 * to take any block in this cylinder.
7095375Smckusic 		 */
7105375Smckusic 		bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
7115375Smckusic 		goto norot;
7125375Smckusic 	}
7135375Smckusic 	/*
7145361Smckusic 	 * check the summary information to see if a block is
7155361Smckusic 	 * available in the requested cylinder starting at the
7169163Ssam 	 * requested rotational position and proceeding around.
7175361Smckusic 	 */
71834143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
7199163Ssam 	pos = cbtorpos(fs, bpref);
72034143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
7215361Smckusic 		if (cylbp[i] > 0)
7225361Smckusic 			break;
72334143Smckusick 	if (i == fs->fs_nrpos)
7245361Smckusic 		for (i = 0; i < pos; i++)
7255361Smckusic 			if (cylbp[i] > 0)
7265361Smckusic 				break;
7275361Smckusic 	if (cylbp[i] > 0) {
7284651Smckusic 		/*
7295361Smckusic 		 * found a rotational position, now find the actual
7305361Smckusic 		 * block. A panic if none is actually there.
7314651Smckusic 		 */
7325361Smckusic 		pos = cylno % fs->fs_cpc;
7335361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
73434143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
7356716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
7366716Smckusick 			    pos, i, fs->fs_fsmnt);
73751469Sbostic 			panic("ffs_alloccgblk: cyl groups corrupted");
7386716Smckusick 		}
73934143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
74051469Sbostic 			if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
74111638Ssam 				bno = blkstofrags(fs, (bno + i));
7425361Smckusic 				goto gotit;
7435361Smckusic 			}
74434143Smckusick 			delta = fs_rotbl(fs)[i];
74534143Smckusick 			if (delta <= 0 ||
74634143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
7474651Smckusic 				break;
7486294Smckusick 			i += delta;
7494651Smckusic 		}
7506716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
75151469Sbostic 		panic("ffs_alloccgblk: can't find blk in cyl");
7524359Smckusick 	}
7535361Smckusic norot:
7545361Smckusic 	/*
7555361Smckusic 	 * no blocks in the requested cylinder, so take next
7565361Smckusic 	 * available one in this cylinder group.
7575361Smckusic 	 */
75851469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
7596567Smckusic 	if (bno < 0)
7606294Smckusick 		return (NULL);
7614651Smckusic 	cgp->cg_rotor = bno;
7624359Smckusick gotit:
76351469Sbostic 	ffs_clrblock(fs, cg_blksfree(cgp), (long)fragstoblks(fs, bno));
7644792Smckusic 	cgp->cg_cs.cs_nbfree--;
7654792Smckusic 	fs->fs_cstotal.cs_nbfree--;
7665322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
7675375Smckusic 	cylno = cbtocylno(fs, bno);
76834143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
76934143Smckusick 	cg_blktot(cgp)[cylno]--;
77050893Smckusick 	fs->fs_fmod = 1;
7714651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
7724359Smckusick }
77334143Smckusick 
7745375Smckusic /*
7755375Smckusic  * Determine whether an inode can be allocated.
7765375Smckusic  *
7775375Smckusic  * Check to see if an inode is available, and if it is,
7785375Smckusic  * allocate it using the following policy:
7795375Smckusic  *   1) allocate the requested inode.
7805375Smckusic  *   2) allocate the next available inode after the requested
7815375Smckusic  *      inode in the specified cylinder group.
7825375Smckusic  */
78351469Sbostic static ino_t
78451469Sbostic ffs_ialloccg(ip, cg, ipref, mode)
7855965Smckusic 	struct inode *ip;
7864359Smckusick 	int cg;
7874359Smckusick 	daddr_t ipref;
7884359Smckusick 	int mode;
7894359Smckusick {
7905965Smckusic 	register struct fs *fs;
7914463Smckusic 	register struct cg *cgp;
79216784Smckusick 	struct buf *bp;
79337735Smckusick 	int error, start, len, loc, map, i;
7944359Smckusick 
7955965Smckusic 	fs = ip->i_fs;
7965322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
7976294Smckusick 		return (NULL);
79837735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
79938776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
80037735Smckusick 	if (error) {
80137735Smckusick 		brelse(bp);
80237735Smckusick 		return (NULL);
80337735Smckusick 	}
8046531Smckusick 	cgp = bp->b_un.b_cg;
80537735Smckusick 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
8065960Smckusic 		brelse(bp);
8076294Smckusick 		return (NULL);
8085960Smckusic 	}
8098105Sroot 	cgp->cg_time = time.tv_sec;
8104359Smckusick 	if (ipref) {
8114359Smckusick 		ipref %= fs->fs_ipg;
81234143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
8134359Smckusick 			goto gotit;
81416784Smckusick 	}
81516784Smckusick 	start = cgp->cg_irotor / NBBY;
81616784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
81734143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
81816784Smckusick 	if (loc == 0) {
81917697Smckusick 		len = start + 1;
82017697Smckusick 		start = 0;
82134143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
82217697Smckusick 		if (loc == 0) {
82317697Smckusick 			printf("cg = %s, irotor = %d, fs = %s\n",
82417697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
82551469Sbostic 			panic("ffs_ialloccg: map corrupted");
82617697Smckusick 			/* NOTREACHED */
82717697Smckusick 		}
82816784Smckusick 	}
82916784Smckusick 	i = start + len - loc;
83034143Smckusick 	map = cg_inosused(cgp)[i];
83116784Smckusick 	ipref = i * NBBY;
83216784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
83316784Smckusick 		if ((map & i) == 0) {
8344359Smckusick 			cgp->cg_irotor = ipref;
8354359Smckusick 			goto gotit;
8364359Smckusick 		}
8374359Smckusick 	}
83816784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
83951469Sbostic 	panic("ffs_ialloccg: block not in map");
84016784Smckusick 	/* NOTREACHED */
8414359Smckusick gotit:
84234143Smckusick 	setbit(cg_inosused(cgp), ipref);
8434792Smckusic 	cgp->cg_cs.cs_nifree--;
8444792Smckusic 	fs->fs_cstotal.cs_nifree--;
8455322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
84650893Smckusick 	fs->fs_fmod = 1;
8474359Smckusick 	if ((mode & IFMT) == IFDIR) {
8484792Smckusic 		cgp->cg_cs.cs_ndir++;
8494792Smckusic 		fs->fs_cstotal.cs_ndir++;
8505322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
8514359Smckusick 	}
8524359Smckusick 	bdwrite(bp);
8534359Smckusick 	return (cg * fs->fs_ipg + ipref);
8544359Smckusick }
8554359Smckusick 
8565375Smckusic /*
8575375Smckusic  * Free a block or fragment.
8585375Smckusic  *
8595375Smckusic  * The specified block or fragment is placed back in the
8605375Smckusic  * free map. If a fragment is deallocated, a possible
8615375Smckusic  * block reassembly is checked.
8625375Smckusic  */
86351469Sbostic ffs_blkfree(ip, bno, size)
8645965Smckusic 	register struct inode *ip;
8654359Smckusick 	daddr_t bno;
86653059Smckusick 	long size;
8674359Smckusick {
8684359Smckusick 	register struct fs *fs;
8694359Smckusick 	register struct cg *cgp;
87037735Smckusick 	struct buf *bp;
87137735Smckusick 	int error, cg, blk, frags, bbase;
8724463Smckusic 	register int i;
8734359Smckusick 
8745965Smckusic 	fs = ip->i_fs;
8756716Smckusick 	if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
8766716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
8776716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
87831402Smckusick 		panic("blkfree: bad size");
8796716Smckusick 	}
8805377Smckusic 	cg = dtog(fs, bno);
88142318Smckusick 	if ((unsigned)bno >= fs->fs_size) {
8826567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
883*53244Smckusick 		ffs_fserr(fs, ip->i_uid, "bad block");
8844359Smckusick 		return;
8856567Smckusic 	}
88637735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
88738776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
88837735Smckusick 	if (error) {
88937735Smckusick 		brelse(bp);
89037735Smckusick 		return;
89137735Smckusick 	}
8926531Smckusick 	cgp = bp->b_un.b_cg;
89337735Smckusick 	if (!cg_chkmagic(cgp)) {
8945960Smckusic 		brelse(bp);
8954359Smckusick 		return;
8965960Smckusic 	}
8978105Sroot 	cgp->cg_time = time.tv_sec;
8985377Smckusic 	bno = dtogd(fs, bno);
8995322Smckusic 	if (size == fs->fs_bsize) {
90051469Sbostic 		if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno))) {
9016716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
9026716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
90331402Smckusick 			panic("blkfree: freeing free block");
9046567Smckusic 		}
90551469Sbostic 		ffs_setblock(fs, cg_blksfree(cgp), fragstoblks(fs, bno));
9064792Smckusic 		cgp->cg_cs.cs_nbfree++;
9074792Smckusic 		fs->fs_cstotal.cs_nbfree++;
9085322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
9095375Smckusic 		i = cbtocylno(fs, bno);
91034143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
91134143Smckusick 		cg_blktot(cgp)[i]++;
9124426Smckusic 	} else {
91317224Smckusick 		bbase = bno - fragnum(fs, bno);
9144463Smckusic 		/*
9154463Smckusic 		 * decrement the counts associated with the old frags
9164463Smckusic 		 */
91734143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
91851469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
9194463Smckusic 		/*
9204463Smckusic 		 * deallocate the fragment
9214463Smckusic 		 */
9225960Smckusic 		frags = numfrags(fs, size);
9234463Smckusic 		for (i = 0; i < frags; i++) {
92434143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
9256716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
9266716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
92731402Smckusick 				panic("blkfree: freeing free frag");
9286716Smckusick 			}
92934143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
9304426Smckusic 		}
9316294Smckusick 		cgp->cg_cs.cs_nffree += i;
9326294Smckusick 		fs->fs_cstotal.cs_nffree += i;
9336294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
9344463Smckusic 		/*
9354463Smckusic 		 * add back in counts associated with the new frags
9364463Smckusic 		 */
93734143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
93851469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
9394463Smckusic 		/*
9404463Smckusic 		 * if a complete block has been reassembled, account for it
9414463Smckusic 		 */
94251469Sbostic 		if (ffs_isblock(fs, cg_blksfree(cgp),
94334476Smckusick 		    (daddr_t)fragstoblks(fs, bbase))) {
9445322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
9455322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
9465322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
9474792Smckusic 			cgp->cg_cs.cs_nbfree++;
9484792Smckusic 			fs->fs_cstotal.cs_nbfree++;
9495322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
9505375Smckusic 			i = cbtocylno(fs, bbase);
95134143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
95234143Smckusick 			cg_blktot(cgp)[i]++;
9534426Smckusic 		}
9544426Smckusic 	}
95550893Smckusick 	fs->fs_fmod = 1;
9564359Smckusick 	bdwrite(bp);
9574359Smckusick }
9584359Smckusick 
9595375Smckusic /*
9605375Smckusic  * Free an inode.
9615375Smckusic  *
9625375Smckusic  * The specified inode is placed back in the free map.
9635375Smckusic  */
96451469Sbostic void
96551541Smckusick ffs_vfree(pvp, ino, mode)
96651541Smckusick 	struct vnode *pvp;
9674359Smckusick 	ino_t ino;
9684359Smckusick 	int mode;
9694359Smckusick {
9704359Smckusick 	register struct fs *fs;
9714359Smckusick 	register struct cg *cgp;
97251541Smckusick 	register struct inode *pip;
97337735Smckusick 	struct buf *bp;
97437735Smckusick 	int error, cg;
9754359Smckusick 
97651541Smckusick 	pip = VTOI(pvp);
97751469Sbostic 	fs = pip->i_fs;
97851469Sbostic 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
97951469Sbostic 		panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
98051469Sbostic 		    pip->i_dev, ino, fs->fs_fsmnt);
9815377Smckusic 	cg = itog(fs, ino);
98251469Sbostic 	error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
98338776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
98437735Smckusick 	if (error) {
98537735Smckusick 		brelse(bp);
98637735Smckusick 		return;
98737735Smckusick 	}
9886531Smckusick 	cgp = bp->b_un.b_cg;
98937735Smckusick 	if (!cg_chkmagic(cgp)) {
9905960Smckusic 		brelse(bp);
9914359Smckusick 		return;
9925960Smckusic 	}
9938105Sroot 	cgp->cg_time = time.tv_sec;
9944359Smckusick 	ino %= fs->fs_ipg;
99534143Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
9966716Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
99751469Sbostic 		    pip->i_dev, ino, fs->fs_fsmnt);
99848057Smckusick 		if (fs->fs_ronly == 0)
99948057Smckusick 			panic("ifree: freeing free inode");
10006716Smckusick 	}
100134143Smckusick 	clrbit(cg_inosused(cgp), ino);
100216784Smckusick 	if (ino < cgp->cg_irotor)
100316784Smckusick 		cgp->cg_irotor = ino;
10044792Smckusic 	cgp->cg_cs.cs_nifree++;
10054792Smckusic 	fs->fs_cstotal.cs_nifree++;
10065322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
10074359Smckusick 	if ((mode & IFMT) == IFDIR) {
10084792Smckusic 		cgp->cg_cs.cs_ndir--;
10094792Smckusic 		fs->fs_cstotal.cs_ndir--;
10105322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
10114359Smckusick 	}
101250893Smckusick 	fs->fs_fmod = 1;
10134359Smckusick 	bdwrite(bp);
10144359Smckusick }
10154359Smckusick 
10164463Smckusic /*
10175375Smckusic  * Find a block of the specified size in the specified cylinder group.
10185375Smckusic  *
10194651Smckusic  * It is a panic if a request is made to find a block if none are
10204651Smckusic  * available.
10214651Smckusic  */
102251469Sbostic static daddr_t
102351469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz)
10244651Smckusic 	register struct fs *fs;
10254651Smckusic 	register struct cg *cgp;
10264651Smckusic 	daddr_t bpref;
10274651Smckusic 	int allocsiz;
10284651Smckusic {
10294651Smckusic 	daddr_t bno;
10304651Smckusic 	int start, len, loc, i;
10314651Smckusic 	int blk, field, subfield, pos;
10324651Smckusic 
10334651Smckusic 	/*
10344651Smckusic 	 * find the fragment by searching through the free block
10354651Smckusic 	 * map for an appropriate bit pattern
10364651Smckusic 	 */
10374651Smckusic 	if (bpref)
10385377Smckusic 		start = dtogd(fs, bpref) / NBBY;
10394651Smckusic 	else
10404651Smckusic 		start = cgp->cg_frotor / NBBY;
10415398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
104234476Smckusick 	loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start],
104334476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
104434476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
10454651Smckusic 	if (loc == 0) {
10466531Smckusick 		len = start + 1;
10476531Smckusick 		start = 0;
104834476Smckusick 		loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0],
104934476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
105034476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
105116784Smckusick 		if (loc == 0) {
105216784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
105316784Smckusick 			    start, len, fs->fs_fsmnt);
105451469Sbostic 			panic("ffs_alloccg: map corrupted");
105517697Smckusick 			/* NOTREACHED */
105616784Smckusick 		}
10574651Smckusic 	}
10584651Smckusic 	bno = (start + len - loc) * NBBY;
10594651Smckusic 	cgp->cg_frotor = bno;
10604651Smckusic 	/*
10614651Smckusic 	 * found the byte in the map
10624651Smckusic 	 * sift through the bits to find the selected frag
10634651Smckusic 	 */
10646294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
106534143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
10664651Smckusic 		blk <<= 1;
10674651Smckusic 		field = around[allocsiz];
10684651Smckusic 		subfield = inside[allocsiz];
10695322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
10706294Smckusick 			if ((blk & field) == subfield)
10716294Smckusick 				return (bno + pos);
10724651Smckusic 			field <<= 1;
10734651Smckusic 			subfield <<= 1;
10744651Smckusic 		}
10754651Smckusic 	}
10766716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
107751469Sbostic 	panic("ffs_alloccg: block not in map");
10786531Smckusick 	return (-1);
10794651Smckusic }
10804651Smckusic 
10814651Smckusic /*
10825375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
10835375Smckusic  *
10845375Smckusic  * The form of the error message is:
10854359Smckusick  *	fs: error message
10864359Smckusick  */
108751469Sbostic static void
108851469Sbostic ffs_fserr(fs, uid, cp)
10894359Smckusick 	struct fs *fs;
109051469Sbostic 	u_int uid;
10914359Smckusick 	char *cp;
10924359Smckusick {
10934359Smckusick 
109442318Smckusick 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
10954359Smckusick }
1096