xref: /csrg-svn/sys/ufs/ffs/ffs_alloc.c (revision 68664)
123394Smckusick /*
263371Sbostic  * Copyright (c) 1982, 1986, 1989, 1993
363371Sbostic  *	The Regents of the University of California.  All rights reserved.
423394Smckusick  *
544536Sbostic  * %sccs.include.redist.c%
634432Sbostic  *
7*68664Smckusick  *	@(#)ffs_alloc.c	8.16 (Berkeley) 03/30/95
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>
1554653Smckusick #include <sys/mount.h>
1651469Sbostic #include <sys/kernel.h>
1751469Sbostic #include <sys/syslog.h>
184359Smckusick 
1953317Smckusick #include <vm/vm.h>
2053317Smckusick 
2151469Sbostic #include <ufs/ufs/quota.h>
2251469Sbostic #include <ufs/ufs/inode.h>
2347571Skarels 
2451469Sbostic #include <ufs/ffs/fs.h>
2551469Sbostic #include <ufs/ffs/ffs_extern.h>
264359Smckusick 
2751469Sbostic extern u_long nextgennumber;
2851469Sbostic 
2968554Smckusick static ufs_daddr_t ffs_alloccg __P((struct inode *, int, ufs_daddr_t, int));
3068554Smckusick static ufs_daddr_t ffs_alloccgblk __P((struct fs *, struct cg *, ufs_daddr_t));
3168554Smckusick static ufs_daddr_t ffs_clusteralloc __P((struct inode *, int, ufs_daddr_t,
3268554Smckusick 	    int));
3351469Sbostic static ino_t	ffs_dirpref __P((struct fs *));
3468554Smckusick static ufs_daddr_t ffs_fragextend __P((struct inode *, int, long, int, int));
3551469Sbostic static void	ffs_fserr __P((struct fs *, u_int, char *));
3651469Sbostic static u_long	ffs_hashalloc
3768122Smckusick 		    __P((struct inode *, int, long, int, u_int32_t (*)()));
3868554Smckusick static ino_t	ffs_nodealloccg __P((struct inode *, int, ufs_daddr_t, int));
3968554Smckusick static ufs_daddr_t ffs_mapsearch __P((struct fs *, struct cg *, ufs_daddr_t,
4068554Smckusick 	    int));
4151469Sbostic 
425375Smckusic /*
435375Smckusic  * Allocate a block in the file system.
445375Smckusic  *
455375Smckusic  * The size of the requested block is given, which must be some
465375Smckusic  * multiple of fs_fsize and <= fs_bsize.
475375Smckusic  * A preference may be optionally specified. If a preference is given
485375Smckusic  * the following hierarchy is used to allocate a block:
495375Smckusic  *   1) allocate the requested block.
505375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
515375Smckusic  *   3) allocate a block in the same cylinder group.
525375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
535375Smckusic  *      available block is located.
545375Smckusic  * If no block preference is given the following heirarchy is used
555375Smckusic  * to allocate a block:
565375Smckusic  *   1) allocate a block in the cylinder group that contains the
575375Smckusic  *      inode for the file.
585375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
595375Smckusic  *      available block is located.
605375Smckusic  */
6153244Smckusick ffs_alloc(ip, lbn, bpref, size, cred, bnp)
624463Smckusic 	register struct inode *ip;
6368554Smckusick 	ufs_daddr_t lbn, bpref;
644359Smckusick 	int size;
6553244Smckusick 	struct ucred *cred;
6668554Smckusick 	ufs_daddr_t *bnp;
674359Smckusick {
6865468Sbostic 	register struct fs *fs;
6968554Smckusick 	ufs_daddr_t bno;
7037735Smckusick 	int cg, error;
714359Smckusick 
7239678Smckusick 	*bnp = 0;
735965Smckusic 	fs = ip->i_fs;
7453244Smckusick #ifdef DIAGNOSTIC
7564508Sbostic 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
766716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
776716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7851469Sbostic 		panic("ffs_alloc: bad size");
796716Smckusick 	}
8053244Smckusick 	if (cred == NOCRED)
8153244Smckusick 		panic("ffs_alloc: missing credential\n");
8253244Smckusick #endif /* DIAGNOSTIC */
835322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
844359Smckusick 		goto nospace;
8541309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
864792Smckusic 		goto nospace;
877650Ssam #ifdef QUOTA
8841309Smckusick 	if (error = chkdq(ip, (long)btodb(size), cred, 0))
8937735Smckusick 		return (error);
907483Skre #endif
914948Smckusic 	if (bpref >= fs->fs_size)
924948Smckusic 		bpref = 0;
934359Smckusick 	if (bpref == 0)
9464603Sbostic 		cg = ino_to_cg(fs, ip->i_number);
954359Smckusick 	else
965377Smckusic 		cg = dtog(fs, bpref);
9768554Smckusick 	bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
9868122Smckusick 	    (u_int32_t (*)())ffs_alloccg);
9939678Smckusick 	if (bno > 0) {
10039678Smckusick 		ip->i_blocks += btodb(size);
10164603Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
10239678Smckusick 		*bnp = bno;
10339678Smckusick 		return (0);
10439678Smckusick 	}
10545173Smckusick #ifdef QUOTA
10645173Smckusick 	/*
10745173Smckusick 	 * Restore user's disk quota because allocation failed.
10845173Smckusick 	 */
10945173Smckusick 	(void) chkdq(ip, (long)-btodb(size), cred, FORCE);
11045173Smckusick #endif
1114359Smckusick nospace:
11251469Sbostic 	ffs_fserr(fs, cred->cr_uid, "file system full");
1134359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
11437735Smckusick 	return (ENOSPC);
1154359Smckusick }
1164359Smckusick 
1175375Smckusic /*
1185375Smckusic  * Reallocate a fragment to a bigger size
1195375Smckusic  *
1205375Smckusic  * The number and size of the old block is given, and a preference
1215375Smckusic  * and new size is also specified. The allocator attempts to extend
1225375Smckusic  * the original block. Failing that, the regular block allocator is
1235375Smckusic  * invoked to get an appropriate block.
1245375Smckusic  */
12553244Smckusick ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
1265965Smckusic 	register struct inode *ip;
12768554Smckusick 	ufs_daddr_t lbprev;
12868554Smckusick 	ufs_daddr_t bpref;
1294426Smckusic 	int osize, nsize;
13053244Smckusick 	struct ucred *cred;
13137735Smckusick 	struct buf **bpp;
1324426Smckusic {
1334426Smckusic 	register struct fs *fs;
13465468Sbostic 	struct buf *bp;
13545719Smckusick 	int cg, request, error;
13668554Smckusick 	ufs_daddr_t bprev, bno;
1374426Smckusic 
13837735Smckusick 	*bpp = 0;
1395965Smckusic 	fs = ip->i_fs;
14053244Smckusick #ifdef DIAGNOSTIC
14164508Sbostic 	if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
14264508Sbostic 	    (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
14351469Sbostic 		printf(
14451469Sbostic 		    "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1456716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
14651469Sbostic 		panic("ffs_realloccg: bad size");
1476716Smckusick 	}
14853244Smckusick 	if (cred == NOCRED)
14953244Smckusick 		panic("ffs_realloccg: missing credential\n");
15053244Smckusick #endif /* DIAGNOSTIC */
15141309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1524792Smckusic 		goto nospace;
15339678Smckusick 	if ((bprev = ip->i_db[lbprev]) == 0) {
1546716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1556716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
15651469Sbostic 		panic("ffs_realloccg: bad bprev");
1576716Smckusick 	}
15839678Smckusick 	/*
15939678Smckusick 	 * Allocate the extra space in the buffer.
16039678Smckusick 	 */
16139678Smckusick 	if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
16239678Smckusick 		brelse(bp);
16339678Smckusick 		return (error);
16439678Smckusick 	}
16545173Smckusick #ifdef QUOTA
16645173Smckusick 	if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) {
16745173Smckusick 		brelse(bp);
16845173Smckusick 		return (error);
16945173Smckusick 	}
17045173Smckusick #endif
17139678Smckusick 	/*
17239678Smckusick 	 * Check for extension in the existing location.
17339678Smckusick 	 */
1746294Smckusick 	cg = dtog(fs, bprev);
17551469Sbostic 	if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) {
17639887Smckusick 		if (bp->b_blkno != fsbtodb(fs, bno))
17739678Smckusick 			panic("bad blockno");
17839762Smckusick 		ip->i_blocks += btodb(nsize - osize);
17964603Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
18048948Smckusick 		allocbuf(bp, nsize);
18148948Smckusick 		bp->b_flags |= B_DONE;
18264508Sbostic 		bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
18337735Smckusick 		*bpp = bp;
18437735Smckusick 		return (0);
1854463Smckusic 	}
18639678Smckusick 	/*
18739678Smckusick 	 * Allocate a new disk location.
18839678Smckusick 	 */
1894948Smckusic 	if (bpref >= fs->fs_size)
1904948Smckusic 		bpref = 0;
19126253Skarels 	switch ((int)fs->fs_optim) {
19225256Smckusick 	case FS_OPTSPACE:
19325256Smckusick 		/*
19425256Smckusick 		 * Allocate an exact sized fragment. Although this makes
19525256Smckusick 		 * best use of space, we will waste time relocating it if
19625256Smckusick 		 * the file continues to grow. If the fragmentation is
19725256Smckusick 		 * less than half of the minimum free reserve, we choose
19825256Smckusick 		 * to begin optimizing for time.
19925256Smckusick 		 */
20024698Smckusick 		request = nsize;
20125256Smckusick 		if (fs->fs_minfree < 5 ||
20225256Smckusick 		    fs->fs_cstotal.cs_nffree >
20325256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
20425256Smckusick 			break;
20525256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
20625256Smckusick 			fs->fs_fsmnt);
20725256Smckusick 		fs->fs_optim = FS_OPTTIME;
20825256Smckusick 		break;
20925256Smckusick 	case FS_OPTTIME:
21025256Smckusick 		/*
21151469Sbostic 		 * At this point we have discovered a file that is trying to
21251469Sbostic 		 * grow a small fragment to a larger fragment. To save time,
21351469Sbostic 		 * we allocate a full sized block, then free the unused portion.
21451469Sbostic 		 * If the file continues to grow, the `ffs_fragextend' call
21551469Sbostic 		 * above will be able to grow it in place without further
21651469Sbostic 		 * copying. If aberrant programs cause disk fragmentation to
21751469Sbostic 		 * grow within 2% of the free reserve, we choose to begin
21851469Sbostic 		 * optimizing for space.
21925256Smckusick 		 */
22024698Smckusick 		request = fs->fs_bsize;
22125256Smckusick 		if (fs->fs_cstotal.cs_nffree <
22225256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
22325256Smckusick 			break;
22425256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
22525256Smckusick 			fs->fs_fsmnt);
22625256Smckusick 		fs->fs_optim = FS_OPTSPACE;
22725256Smckusick 		break;
22825256Smckusick 	default:
22925256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
23025256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
23151469Sbostic 		panic("ffs_realloccg: bad optim");
23225256Smckusick 		/* NOTREACHED */
23325256Smckusick 	}
23468554Smckusick 	bno = (ufs_daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
23568122Smckusick 	    (u_int32_t (*)())ffs_alloccg);
2366567Smckusic 	if (bno > 0) {
23745719Smckusick 		bp->b_blkno = fsbtodb(fs, bno);
23845719Smckusick 		(void) vnode_pager_uncache(ITOV(ip));
23953059Smckusick 		ffs_blkfree(ip, bprev, (long)osize);
24024698Smckusick 		if (nsize < request)
24151469Sbostic 			ffs_blkfree(ip, bno + numfrags(fs, nsize),
24253059Smckusick 			    (long)(request - nsize));
24312643Ssam 		ip->i_blocks += btodb(nsize - osize);
24464603Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
24548948Smckusick 		allocbuf(bp, nsize);
24648948Smckusick 		bp->b_flags |= B_DONE;
24764508Sbostic 		bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
24837735Smckusick 		*bpp = bp;
24937735Smckusick 		return (0);
2504463Smckusic 	}
25145173Smckusick #ifdef QUOTA
25245173Smckusick 	/*
25345173Smckusick 	 * Restore user's disk quota because allocation failed.
25445173Smckusick 	 */
25545173Smckusick 	(void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
25645173Smckusick #endif
25739678Smckusick 	brelse(bp);
2584792Smckusic nospace:
2594463Smckusic 	/*
2604463Smckusic 	 * no space available
2614463Smckusic 	 */
26251469Sbostic 	ffs_fserr(fs, cred->cr_uid, "file system full");
2634426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
26437735Smckusick 	return (ENOSPC);
2654426Smckusic }
2664426Smckusic 
2675375Smckusic /*
26865999Smckusick  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
26965999Smckusick  *
27065999Smckusick  * The vnode and an array of buffer pointers for a range of sequential
27165999Smckusick  * logical blocks to be made contiguous is given. The allocator attempts
27265999Smckusick  * to find a range of sequential blocks starting as close as possible to
27365999Smckusick  * an fs_rotdelay offset from the end of the allocation for the logical
27465999Smckusick  * block immediately preceeding the current range. If successful, the
27565999Smckusick  * physical block numbers in the buffer pointers and in the inode are
27665999Smckusick  * changed to reflect the new allocation. If unsuccessful, the allocation
27765999Smckusick  * is left unchanged. The success in doing the reallocation is returned.
27865999Smckusick  * Note that the error return is not reflected back to the user. Rather
27965999Smckusick  * the previous block allocation will be used.
28065999Smckusick  */
28166176Smckusick int doasyncfree = 1;
282*68664Smckusick int doreallocblks = 1;
28367871Smckusick int prtrealloc = 0;
28467392Smkm 
28565999Smckusick int
28665999Smckusick ffs_reallocblks(ap)
28765999Smckusick 	struct vop_reallocblks_args /* {
28865999Smckusick 		struct vnode *a_vp;
28965999Smckusick 		struct cluster_save *a_buflist;
29065999Smckusick 	} */ *ap;
29165999Smckusick {
29265999Smckusick 	struct fs *fs;
29365999Smckusick 	struct inode *ip;
29465999Smckusick 	struct vnode *vp;
29565999Smckusick 	struct buf *sbp, *ebp;
29668554Smckusick 	ufs_daddr_t *bap, *sbap, *ebap;
29765999Smckusick 	struct cluster_save *buflist;
29868554Smckusick 	ufs_daddr_t start_lbn, end_lbn, soff, eoff, newblk, blkno;
29965999Smckusick 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
30065999Smckusick 	int i, len, start_lvl, end_lvl, pref, ssize;
30165999Smckusick 
302*68664Smckusick 	if (doreallocblks == 0)
303*68664Smckusick 		return (ENOSPC);
30465999Smckusick 	vp = ap->a_vp;
30565999Smckusick 	ip = VTOI(vp);
30665999Smckusick 	fs = ip->i_fs;
30765999Smckusick 	if (fs->fs_contigsumsize <= 0)
30865999Smckusick 		return (ENOSPC);
30965999Smckusick 	buflist = ap->a_buflist;
31065999Smckusick 	len = buflist->bs_nchildren;
31165999Smckusick 	start_lbn = buflist->bs_children[0]->b_lblkno;
31265999Smckusick 	end_lbn = start_lbn + len - 1;
31365999Smckusick #ifdef DIAGNOSTIC
31465999Smckusick 	for (i = 1; i < len; i++)
31565999Smckusick 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
31665999Smckusick 			panic("ffs_reallocblks: non-cluster");
31765999Smckusick #endif
31865999Smckusick 	/*
31965999Smckusick 	 * If the latest allocation is in a new cylinder group, assume that
32065999Smckusick 	 * the filesystem has decided to move and do not force it back to
32165999Smckusick 	 * the previous cylinder group.
32265999Smckusick 	 */
32365999Smckusick 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
32465999Smckusick 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
32565999Smckusick 		return (ENOSPC);
32665999Smckusick 	if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
32765999Smckusick 	    ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
32865999Smckusick 		return (ENOSPC);
32965999Smckusick 	/*
33065999Smckusick 	 * Get the starting offset and block map for the first block.
33165999Smckusick 	 */
33265999Smckusick 	if (start_lvl == 0) {
33365999Smckusick 		sbap = &ip->i_db[0];
33465999Smckusick 		soff = start_lbn;
33565999Smckusick 	} else {
33665999Smckusick 		idp = &start_ap[start_lvl - 1];
33765999Smckusick 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
33865999Smckusick 			brelse(sbp);
33965999Smckusick 			return (ENOSPC);
34065999Smckusick 		}
34168554Smckusick 		sbap = (ufs_daddr_t *)sbp->b_data;
34265999Smckusick 		soff = idp->in_off;
34365999Smckusick 	}
34465999Smckusick 	/*
34565999Smckusick 	 * Find the preferred location for the cluster.
34665999Smckusick 	 */
34765999Smckusick 	pref = ffs_blkpref(ip, start_lbn, soff, sbap);
34865999Smckusick 	/*
34965999Smckusick 	 * If the block range spans two block maps, get the second map.
35065999Smckusick 	 */
35166086Smckusick 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
35265999Smckusick 		ssize = len;
35365999Smckusick 	} else {
35466086Smckusick #ifdef DIAGNOSTIC
35566086Smckusick 		if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
35666086Smckusick 			panic("ffs_reallocblk: start == end");
35766086Smckusick #endif
35865999Smckusick 		ssize = len - (idp->in_off + 1);
35965999Smckusick 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
36065999Smckusick 			goto fail;
36168554Smckusick 		ebap = (ufs_daddr_t *)ebp->b_data;
36265999Smckusick 	}
36365999Smckusick 	/*
36465999Smckusick 	 * Search the block map looking for an allocation of the desired size.
36565999Smckusick 	 */
36668554Smckusick 	if ((newblk = (ufs_daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
36768122Smckusick 	    len, (u_int32_t (*)())ffs_clusteralloc)) == 0)
36865999Smckusick 		goto fail;
36965999Smckusick 	/*
37065999Smckusick 	 * We have found a new contiguous block.
37165999Smckusick 	 *
37265999Smckusick 	 * First we have to replace the old block pointers with the new
37365999Smckusick 	 * block pointers in the inode and indirect blocks associated
37465999Smckusick 	 * with the file.
37565999Smckusick 	 */
37667871Smckusick #ifdef DEBUG
37767871Smckusick 	if (prtrealloc)
37867871Smckusick 		printf("realloc: ino %d, lbns %d-%d\n\told:", ip->i_number,
37968112Smckusick 		    start_lbn, end_lbn);
38068112Smckusick #endif
38165999Smckusick 	blkno = newblk;
38265999Smckusick 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
38365999Smckusick 		if (i == ssize)
38465999Smckusick 			bap = ebap;
38565999Smckusick #ifdef DIAGNOSTIC
38667869Smckusick 		if (dbtofsb(fs, buflist->bs_children[i]->b_blkno) != *bap)
38765999Smckusick 			panic("ffs_reallocblks: alloc mismatch");
38865999Smckusick #endif
38967871Smckusick #ifdef DEBUG
39067871Smckusick 		if (prtrealloc)
39167871Smckusick 			printf(" %d,", *bap);
39267871Smckusick #endif
39365999Smckusick 		*bap++ = blkno;
39465999Smckusick 	}
39565999Smckusick 	/*
39665999Smckusick 	 * Next we must write out the modified inode and indirect blocks.
39766176Smckusick 	 * For strict correctness, the writes should be synchronous since
39866176Smckusick 	 * the old block values may have been written to disk. In practise
39966176Smckusick 	 * they are almost never written, but if we are concerned about
40066176Smckusick 	 * strict correctness, the `doasyncfree' flag should be set to zero.
40165999Smckusick 	 *
40266176Smckusick 	 * The test on `doasyncfree' should be changed to test a flag
40366176Smckusick 	 * that shows whether the associated buffers and inodes have
40466176Smckusick 	 * been written. The flag should be set when the cluster is
40566176Smckusick 	 * started and cleared whenever the buffer or inode is flushed.
40666176Smckusick 	 * We can then check below to see if it is set, and do the
40766176Smckusick 	 * synchronous write only when it has been cleared.
40865999Smckusick 	 */
40965999Smckusick 	if (sbap != &ip->i_db[0]) {
41066176Smckusick 		if (doasyncfree)
41166176Smckusick 			bdwrite(sbp);
41266176Smckusick 		else
41366176Smckusick 			bwrite(sbp);
41465999Smckusick 	} else {
41565999Smckusick 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
41666176Smckusick 		if (!doasyncfree)
41766176Smckusick 			VOP_UPDATE(vp, &time, &time, MNT_WAIT);
41865999Smckusick 	}
41965999Smckusick 	if (ssize < len)
42066176Smckusick 		if (doasyncfree)
42166176Smckusick 			bdwrite(ebp);
42266176Smckusick 		else
42366176Smckusick 			bwrite(ebp);
42465999Smckusick 	/*
42565999Smckusick 	 * Last, free the old blocks and assign the new blocks to the buffers.
42665999Smckusick 	 */
42767871Smckusick #ifdef DEBUG
42867871Smckusick 	if (prtrealloc)
42967871Smckusick 		printf("\n\tnew:");
43067871Smckusick #endif
43165999Smckusick 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
43265999Smckusick 		ffs_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
43365999Smckusick 		    fs->fs_bsize);
43465999Smckusick 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
43567871Smckusick #ifdef DEBUG
43667871Smckusick 		if (prtrealloc)
43767871Smckusick 			printf(" %d,", blkno);
43867871Smckusick #endif
43965999Smckusick 	}
44067871Smckusick #ifdef DEBUG
44167871Smckusick 	if (prtrealloc) {
44267871Smckusick 		prtrealloc--;
44367871Smckusick 		printf("\n");
44467871Smckusick 	}
44567871Smckusick #endif
44665999Smckusick 	return (0);
44765999Smckusick 
44865999Smckusick fail:
44965999Smckusick 	if (ssize < len)
45065999Smckusick 		brelse(ebp);
45165999Smckusick 	if (sbap != &ip->i_db[0])
45265999Smckusick 		brelse(sbp);
45365999Smckusick 	return (ENOSPC);
45465999Smckusick }
45565999Smckusick 
45665999Smckusick /*
4575375Smckusic  * Allocate an inode in the file system.
4585375Smckusic  *
45951469Sbostic  * If allocating a directory, use ffs_dirpref to select the inode.
46051469Sbostic  * If allocating in a directory, the following hierarchy is followed:
46151469Sbostic  *   1) allocate the preferred inode.
4625375Smckusic  *   2) allocate an inode in the same cylinder group.
4635375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
4645375Smckusic  *      available inode is located.
4655375Smckusic  * If no inode preference is given the following heirarchy is used
4665375Smckusic  * to allocate an inode:
4675375Smckusic  *   1) allocate an inode in cylinder group 0.
4685375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
4695375Smckusic  *      available inode is located.
4705375Smckusic  */
47154653Smckusick ffs_valloc(ap)
47254653Smckusick 	struct vop_valloc_args /* {
47354653Smckusick 		struct vnode *a_pvp;
47454653Smckusick 		int a_mode;
47554653Smckusick 		struct ucred *a_cred;
47654653Smckusick 		struct vnode **a_vpp;
47754653Smckusick 	} */ *ap;
4784359Smckusick {
47953865Sheideman 	register struct vnode *pvp = ap->a_pvp;
48051541Smckusick 	register struct inode *pip;
4814359Smckusick 	register struct fs *fs;
4824359Smckusick 	register struct inode *ip;
48354653Smckusick 	mode_t mode = ap->a_mode;
48451469Sbostic 	ino_t ino, ipref;
48537735Smckusick 	int cg, error;
4864359Smckusick 
48753583Sheideman 	*ap->a_vpp = NULL;
48853865Sheideman 	pip = VTOI(pvp);
4895965Smckusic 	fs = pip->i_fs;
4904792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
4914359Smckusick 		goto noinodes;
49251469Sbostic 
49354653Smckusick 	if ((mode & IFMT) == IFDIR)
49451541Smckusick 		ipref = ffs_dirpref(fs);
49551469Sbostic 	else
49651469Sbostic 		ipref = pip->i_number;
4974948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
4984948Smckusic 		ipref = 0;
49964603Sbostic 	cg = ino_to_cg(fs, ipref);
50064603Sbostic 	ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
5014359Smckusick 	if (ino == 0)
5024359Smckusick 		goto noinodes;
50354653Smckusick 	error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
50437735Smckusick 	if (error) {
50554653Smckusick 		VOP_VFREE(pvp, ino, mode);
50637735Smckusick 		return (error);
5074359Smckusick 	}
50853583Sheideman 	ip = VTOI(*ap->a_vpp);
5096716Smckusick 	if (ip->i_mode) {
51054653Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
5116716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
51251541Smckusick 		panic("ffs_valloc: dup alloc");
5136716Smckusick 	}
51412643Ssam 	if (ip->i_blocks) {				/* XXX */
51512643Ssam 		printf("free inode %s/%d had %d blocks\n",
51612643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
51712643Ssam 		ip->i_blocks = 0;
51812643Ssam 	}
51939516Smckusick 	ip->i_flags = 0;
52038255Smckusick 	/*
52138255Smckusick 	 * Set up a new generation number for this inode.
52238255Smckusick 	 */
52338255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
52438255Smckusick 		nextgennumber = time.tv_sec;
52538255Smckusick 	ip->i_gen = nextgennumber;
52637735Smckusick 	return (0);
5274359Smckusick noinodes:
52853583Sheideman 	ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
5296294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
53037735Smckusick 	return (ENOSPC);
5314359Smckusick }
5324359Smckusick 
5334651Smckusic /*
5345375Smckusic  * Find a cylinder to place a directory.
5355375Smckusic  *
5365375Smckusic  * The policy implemented by this algorithm is to select from
5375375Smckusic  * among those cylinder groups with above the average number of
5385375Smckusic  * free inodes, the one with the smallest number of directories.
5394651Smckusic  */
54051469Sbostic static ino_t
54151469Sbostic ffs_dirpref(fs)
5425965Smckusic 	register struct fs *fs;
5434359Smckusick {
5444651Smckusic 	int cg, minndir, mincg, avgifree;
5454359Smckusick 
5464792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
5474651Smckusic 	minndir = fs->fs_ipg;
5484359Smckusick 	mincg = 0;
5494651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
5505322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
5515322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
5524359Smckusick 			mincg = cg;
5535322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
5544359Smckusick 		}
5559163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
5564359Smckusick }
5574359Smckusick 
5584651Smckusic /*
5599163Ssam  * Select the desired position for the next block in a file.  The file is
5609163Ssam  * logically divided into sections. The first section is composed of the
5619163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
5629163Ssam  *
5639163Ssam  * If no blocks have been allocated in the first section, the policy is to
5649163Ssam  * request a block in the same cylinder group as the inode that describes
5659163Ssam  * the file. If no blocks have been allocated in any other section, the
5669163Ssam  * policy is to place the section in a cylinder group with a greater than
5679163Ssam  * average number of free blocks.  An appropriate cylinder group is found
56817696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
56917696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
57017696Smckusick  * cylinder group from which the previous allocation was made. The sweep
57117696Smckusick  * continues until a cylinder group with greater than the average number
57217696Smckusick  * of free blocks is found. If the allocation is for the first block in an
57317696Smckusick  * indirect block, the information on the previous allocation is unavailable;
57417696Smckusick  * here a best guess is made based upon the logical block number being
57517696Smckusick  * allocated.
5769163Ssam  *
5779163Ssam  * If a section is already partially allocated, the policy is to
5789163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
5799163Ssam  * contiguous blocks and the beginning of the next is physically separated
5809163Ssam  * so that the disk head will be in transit between them for at least
5819163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
5829163Ssam  * schedule another I/O transfer.
5834651Smckusic  */
58468554Smckusick ufs_daddr_t
58551469Sbostic ffs_blkpref(ip, lbn, indx, bap)
5869163Ssam 	struct inode *ip;
58768554Smckusick 	ufs_daddr_t lbn;
5889163Ssam 	int indx;
58968554Smckusick 	ufs_daddr_t *bap;
5909163Ssam {
5915965Smckusic 	register struct fs *fs;
59217696Smckusick 	register int cg;
59317696Smckusick 	int avgbfree, startcg;
59468554Smckusick 	ufs_daddr_t nextblk;
5954651Smckusic 
5969163Ssam 	fs = ip->i_fs;
5979163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
5989163Ssam 		if (lbn < NDADDR) {
59964603Sbostic 			cg = ino_to_cg(fs, ip->i_number);
6005322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
6014651Smckusic 		}
6029163Ssam 		/*
6039163Ssam 		 * Find a cylinder with greater than average number of
6049163Ssam 		 * unused data blocks.
6059163Ssam 		 */
60617696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
60764603Sbostic 			startcg =
60864603Sbostic 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
60917696Smckusick 		else
61017696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
61117696Smckusick 		startcg %= fs->fs_ncg;
6129163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
61317696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
6149163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
6159163Ssam 				fs->fs_cgrotor = cg;
6169163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
6179163Ssam 			}
61817696Smckusick 		for (cg = 0; cg <= startcg; cg++)
6199163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
6209163Ssam 				fs->fs_cgrotor = cg;
6219163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
6229163Ssam 			}
6239163Ssam 		return (NULL);
6249163Ssam 	}
6259163Ssam 	/*
6269163Ssam 	 * One or more previous blocks have been laid out. If less
6279163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
6289163Ssam 	 * next block is requested contiguously, otherwise it is
6299163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
6309163Ssam 	 */
6319163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
63256665Smargo 	if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] +
63356665Smargo 	    blkstofrags(fs, fs->fs_maxcontig) != nextblk)
6349163Ssam 		return (nextblk);
6359163Ssam 	if (fs->fs_rotdelay != 0)
6369163Ssam 		/*
6379163Ssam 		 * Here we convert ms of delay to frags as:
6389163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
6399163Ssam 		 *	((sect/frag) * (ms/sec))
6409163Ssam 		 * then round up to the next block.
6419163Ssam 		 */
6429163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
6439163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
6449163Ssam 	return (nextblk);
6454651Smckusic }
6464651Smckusic 
6475375Smckusic /*
6485375Smckusic  * Implement the cylinder overflow algorithm.
6495375Smckusic  *
6505375Smckusic  * The policy implemented by this algorithm is:
6515375Smckusic  *   1) allocate the block in its requested cylinder group.
6525375Smckusic  *   2) quadradically rehash on the cylinder group number.
6535375Smckusic  *   3) brute force search for a free block.
6545375Smckusic  */
6555212Smckusic /*VARARGS5*/
65651469Sbostic static u_long
65751469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator)
6585965Smckusic 	struct inode *ip;
6594359Smckusick 	int cg;
6604359Smckusick 	long pref;
6614359Smckusick 	int size;	/* size for data blocks, mode for inodes */
66268122Smckusick 	u_int32_t (*allocator)();
6634359Smckusick {
6645965Smckusic 	register struct fs *fs;
6654359Smckusick 	long result;
6664359Smckusick 	int i, icg = cg;
6674359Smckusick 
6685965Smckusic 	fs = ip->i_fs;
6694359Smckusick 	/*
6704359Smckusick 	 * 1: preferred cylinder group
6714359Smckusick 	 */
6725965Smckusic 	result = (*allocator)(ip, cg, pref, size);
6734359Smckusick 	if (result)
6744359Smckusick 		return (result);
6754359Smckusick 	/*
6764359Smckusick 	 * 2: quadratic rehash
6774359Smckusick 	 */
6784359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
6794359Smckusick 		cg += i;
6804359Smckusick 		if (cg >= fs->fs_ncg)
6814359Smckusick 			cg -= fs->fs_ncg;
6825965Smckusic 		result = (*allocator)(ip, cg, 0, size);
6834359Smckusick 		if (result)
6844359Smckusick 			return (result);
6854359Smckusick 	}
6864359Smckusick 	/*
6874359Smckusick 	 * 3: brute force search
68810847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
68910847Ssam 	 * and 1 is always checked in the quadratic rehash.
6904359Smckusick 	 */
69110848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
69210847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
6935965Smckusic 		result = (*allocator)(ip, cg, 0, size);
6944359Smckusick 		if (result)
6954359Smckusick 			return (result);
6964359Smckusick 		cg++;
6974359Smckusick 		if (cg == fs->fs_ncg)
6984359Smckusick 			cg = 0;
6994359Smckusick 	}
7006294Smckusick 	return (NULL);
7014359Smckusick }
7024359Smckusick 
7035375Smckusic /*
7045375Smckusic  * Determine whether a fragment can be extended.
7055375Smckusic  *
7065375Smckusic  * Check to see if the necessary fragments are available, and
7075375Smckusic  * if they are, allocate them.
7085375Smckusic  */
70968554Smckusick static ufs_daddr_t
71051469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize)
7115965Smckusic 	struct inode *ip;
7124426Smckusic 	int cg;
7134463Smckusic 	long bprev;
7144426Smckusic 	int osize, nsize;
7154426Smckusic {
7165965Smckusic 	register struct fs *fs;
7174463Smckusic 	register struct cg *cgp;
71837735Smckusick 	struct buf *bp;
7194463Smckusic 	long bno;
7204463Smckusic 	int frags, bbase;
72137735Smckusick 	int i, error;
7224426Smckusic 
7235965Smckusic 	fs = ip->i_fs;
72417224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
7256531Smckusick 		return (NULL);
7265960Smckusic 	frags = numfrags(fs, nsize);
72717224Smckusick 	bbase = fragnum(fs, bprev);
72817224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
72930749Skarels 		/* cannot extend across a block boundary */
7306294Smckusick 		return (NULL);
7314463Smckusic 	}
73237735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
73338776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
73437735Smckusick 	if (error) {
73537735Smckusick 		brelse(bp);
73637735Smckusick 		return (NULL);
73737735Smckusick 	}
73864508Sbostic 	cgp = (struct cg *)bp->b_data;
73937735Smckusick 	if (!cg_chkmagic(cgp)) {
7405960Smckusic 		brelse(bp);
7416294Smckusick 		return (NULL);
7425960Smckusic 	}
7438105Sroot 	cgp->cg_time = time.tv_sec;
7445377Smckusic 	bno = dtogd(fs, bprev);
7455960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
74634143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
7475361Smckusic 			brelse(bp);
7486294Smckusick 			return (NULL);
7495361Smckusic 		}
7505361Smckusic 	/*
7515361Smckusic 	 * the current fragment can be extended
7525361Smckusic 	 * deduct the count on fragment being extended into
7535361Smckusic 	 * increase the count on the remaining fragment (if any)
7545361Smckusic 	 * allocate the extended piece
7555361Smckusic 	 */
7565361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
75734143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
7584463Smckusic 			break;
7595960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
7605361Smckusic 	if (i != frags)
7615361Smckusic 		cgp->cg_frsum[i - frags]++;
7625960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
76334143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
7645361Smckusic 		cgp->cg_cs.cs_nffree--;
7655361Smckusic 		fs->fs_cstotal.cs_nffree--;
7665361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
7674463Smckusic 	}
76850893Smckusick 	fs->fs_fmod = 1;
7695361Smckusic 	bdwrite(bp);
7705361Smckusic 	return (bprev);
7714426Smckusic }
7724426Smckusic 
7735375Smckusic /*
7745375Smckusic  * Determine whether a block can be allocated.
7755375Smckusic  *
77664603Sbostic  * Check to see if a block of the appropriate size is available,
7775375Smckusic  * and if it is, allocate it.
7785375Smckusic  */
77968554Smckusick static ufs_daddr_t
78051469Sbostic ffs_alloccg(ip, cg, bpref, size)
7815965Smckusic 	struct inode *ip;
7824359Smckusick 	int cg;
78368554Smckusick 	ufs_daddr_t bpref;
7844359Smckusick 	int size;
7854359Smckusick {
7865965Smckusic 	register struct fs *fs;
7874463Smckusic 	register struct cg *cgp;
78837735Smckusick 	struct buf *bp;
7894463Smckusic 	register int i;
79037735Smckusick 	int error, bno, frags, allocsiz;
7914359Smckusick 
7925965Smckusic 	fs = ip->i_fs;
7935322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
7946294Smckusick 		return (NULL);
79537735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
79638776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
79737735Smckusick 	if (error) {
79837735Smckusick 		brelse(bp);
79937735Smckusick 		return (NULL);
80037735Smckusick 	}
80164508Sbostic 	cgp = (struct cg *)bp->b_data;
80237735Smckusick 	if (!cg_chkmagic(cgp) ||
80315950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
8045960Smckusic 		brelse(bp);
8056294Smckusick 		return (NULL);
8065960Smckusic 	}
8078105Sroot 	cgp->cg_time = time.tv_sec;
8085322Smckusic 	if (size == fs->fs_bsize) {
80951469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
8104463Smckusic 		bdwrite(bp);
8114463Smckusic 		return (bno);
8124463Smckusic 	}
8134463Smckusic 	/*
8144463Smckusic 	 * check to see if any fragments are already available
8154463Smckusic 	 * allocsiz is the size which will be allocated, hacking
8164463Smckusic 	 * it down to a smaller size if necessary
8174463Smckusic 	 */
8185960Smckusic 	frags = numfrags(fs, size);
8195322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
8204463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
8214463Smckusic 			break;
8225322Smckusic 	if (allocsiz == fs->fs_frag) {
8234463Smckusic 		/*
8244463Smckusic 		 * no fragments were available, so a block will be
8254463Smckusic 		 * allocated, and hacked up
8264463Smckusic 		 */
8274792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
8284463Smckusic 			brelse(bp);
8296294Smckusick 			return (NULL);
8304463Smckusic 		}
83151469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
8325377Smckusic 		bpref = dtogd(fs, bno);
8335322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
83434143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
8355322Smckusic 		i = fs->fs_frag - frags;
8364792Smckusic 		cgp->cg_cs.cs_nffree += i;
8374792Smckusic 		fs->fs_cstotal.cs_nffree += i;
8385322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
83950893Smckusick 		fs->fs_fmod = 1;
8404463Smckusic 		cgp->cg_frsum[i]++;
8414463Smckusic 		bdwrite(bp);
8424463Smckusic 		return (bno);
8434463Smckusic 	}
84451469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
84515950Smckusick 	if (bno < 0) {
84615950Smckusick 		brelse(bp);
8476294Smckusick 		return (NULL);
84815950Smckusick 	}
8494463Smckusic 	for (i = 0; i < frags; i++)
85034143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
8514792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
8524792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
8535322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
85450893Smckusick 	fs->fs_fmod = 1;
8554463Smckusic 	cgp->cg_frsum[allocsiz]--;
8564463Smckusic 	if (frags != allocsiz)
8574463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
8584463Smckusic 	bdwrite(bp);
8594463Smckusic 	return (cg * fs->fs_fpg + bno);
8604463Smckusic }
8614463Smckusic 
8625375Smckusic /*
8635375Smckusic  * Allocate a block in a cylinder group.
8645375Smckusic  *
8655375Smckusic  * This algorithm implements the following policy:
8665375Smckusic  *   1) allocate the requested block.
8675375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
8685375Smckusic  *   3) allocate the next available block on the block rotor for the
8695375Smckusic  *      specified cylinder group.
8705375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
8715375Smckusic  * blocks may be fragmented by the routine that allocates them.
8725375Smckusic  */
87368554Smckusick static ufs_daddr_t
87451469Sbostic ffs_alloccgblk(fs, cgp, bpref)
8755965Smckusic 	register struct fs *fs;
8764463Smckusic 	register struct cg *cgp;
87768554Smckusick 	ufs_daddr_t bpref;
8784463Smckusic {
87968554Smckusick 	ufs_daddr_t bno, blkno;
8806294Smckusick 	int cylno, pos, delta;
8814651Smckusic 	short *cylbp;
8825361Smckusic 	register int i;
8834463Smckusic 
88465999Smckusick 	if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
8854651Smckusic 		bpref = cgp->cg_rotor;
8865361Smckusic 		goto norot;
8875361Smckusic 	}
88817224Smckusick 	bpref = blknum(fs, bpref);
8895377Smckusic 	bpref = dtogd(fs, bpref);
8905361Smckusic 	/*
8915361Smckusic 	 * if the requested block is available, use it
8925361Smckusic 	 */
89351469Sbostic 	if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
8945361Smckusic 		bno = bpref;
8955361Smckusic 		goto gotit;
8965361Smckusic 	}
8975361Smckusic 	/*
8985361Smckusic 	 * check for a block available on the same cylinder
8995361Smckusic 	 */
9005361Smckusic 	cylno = cbtocylno(fs, bpref);
90134143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
9025375Smckusic 		goto norot;
9035375Smckusic 	if (fs->fs_cpc == 0) {
9045375Smckusic 		/*
90557000Smckusick 		 * Block layout information is not available.
90657000Smckusick 		 * Leaving bpref unchanged means we take the
90757000Smckusick 		 * next available free block following the one
90857000Smckusick 		 * we just allocated. Hopefully this will at
90957000Smckusick 		 * least hit a track cache on drives of unknown
91057000Smckusick 		 * geometry (e.g. SCSI).
9115375Smckusic 		 */
9125375Smckusic 		goto norot;
9135375Smckusic 	}
9145375Smckusic 	/*
9155361Smckusic 	 * check the summary information to see if a block is
9165361Smckusic 	 * available in the requested cylinder starting at the
9179163Ssam 	 * requested rotational position and proceeding around.
9185361Smckusic 	 */
91934143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
9209163Ssam 	pos = cbtorpos(fs, bpref);
92134143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
9225361Smckusic 		if (cylbp[i] > 0)
9235361Smckusic 			break;
92434143Smckusick 	if (i == fs->fs_nrpos)
9255361Smckusic 		for (i = 0; i < pos; i++)
9265361Smckusic 			if (cylbp[i] > 0)
9275361Smckusic 				break;
9285361Smckusic 	if (cylbp[i] > 0) {
9294651Smckusic 		/*
9305361Smckusic 		 * found a rotational position, now find the actual
9315361Smckusic 		 * block. A panic if none is actually there.
9324651Smckusic 		 */
9335361Smckusic 		pos = cylno % fs->fs_cpc;
9345361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
93534143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
9366716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
9376716Smckusick 			    pos, i, fs->fs_fsmnt);
93851469Sbostic 			panic("ffs_alloccgblk: cyl groups corrupted");
9396716Smckusick 		}
94034143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
94151469Sbostic 			if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
94211638Ssam 				bno = blkstofrags(fs, (bno + i));
9435361Smckusic 				goto gotit;
9445361Smckusic 			}
94534143Smckusick 			delta = fs_rotbl(fs)[i];
94634143Smckusick 			if (delta <= 0 ||
94734143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
9484651Smckusic 				break;
9496294Smckusick 			i += delta;
9504651Smckusic 		}
9516716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
95251469Sbostic 		panic("ffs_alloccgblk: can't find blk in cyl");
9534359Smckusick 	}
9545361Smckusic norot:
9555361Smckusic 	/*
9565361Smckusic 	 * no blocks in the requested cylinder, so take next
9575361Smckusic 	 * available one in this cylinder group.
9585361Smckusic 	 */
95951469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
9606567Smckusic 	if (bno < 0)
9616294Smckusick 		return (NULL);
9624651Smckusic 	cgp->cg_rotor = bno;
9634359Smckusick gotit:
96465999Smckusick 	blkno = fragstoblks(fs, bno);
96565999Smckusick 	ffs_clrblock(fs, cg_blksfree(cgp), (long)blkno);
96665999Smckusick 	ffs_clusteracct(fs, cgp, blkno, -1);
9674792Smckusic 	cgp->cg_cs.cs_nbfree--;
9684792Smckusic 	fs->fs_cstotal.cs_nbfree--;
9695322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
9705375Smckusic 	cylno = cbtocylno(fs, bno);
97134143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
97234143Smckusick 	cg_blktot(cgp)[cylno]--;
97350893Smckusick 	fs->fs_fmod = 1;
9744651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
9754359Smckusick }
97634143Smckusick 
9775375Smckusic /*
97865999Smckusick  * Determine whether a cluster can be allocated.
97965999Smckusick  *
98065999Smckusick  * We do not currently check for optimal rotational layout if there
98165999Smckusick  * are multiple choices in the same cylinder group. Instead we just
98265999Smckusick  * take the first one that we find following bpref.
98365999Smckusick  */
98468554Smckusick static ufs_daddr_t
98565999Smckusick ffs_clusteralloc(ip, cg, bpref, len)
98665999Smckusick 	struct inode *ip;
98765999Smckusick 	int cg;
98868554Smckusick 	ufs_daddr_t bpref;
98965999Smckusick 	int len;
99065999Smckusick {
99165999Smckusick 	register struct fs *fs;
99265999Smckusick 	register struct cg *cgp;
99365999Smckusick 	struct buf *bp;
99468336Smckusick 	int i, got, run, bno, bit, map;
99565999Smckusick 	u_char *mapp;
99667869Smckusick 	int32_t *lp;
99765999Smckusick 
99865999Smckusick 	fs = ip->i_fs;
99967869Smckusick 	if (fs->fs_maxcluster[cg] < len)
100065999Smckusick 		return (NULL);
100165999Smckusick 	if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
100265999Smckusick 	    NOCRED, &bp))
100365999Smckusick 		goto fail;
100465999Smckusick 	cgp = (struct cg *)bp->b_data;
100565999Smckusick 	if (!cg_chkmagic(cgp))
100665999Smckusick 		goto fail;
100765999Smckusick 	/*
100865999Smckusick 	 * Check to see if a cluster of the needed size (or bigger) is
100965999Smckusick 	 * available in this cylinder group.
101065999Smckusick 	 */
101167869Smckusick 	lp = &cg_clustersum(cgp)[len];
101265999Smckusick 	for (i = len; i <= fs->fs_contigsumsize; i++)
101367869Smckusick 		if (*lp++ > 0)
101465999Smckusick 			break;
101567869Smckusick 	if (i > fs->fs_contigsumsize) {
101667869Smckusick 		/*
101767869Smckusick 		 * This is the first time looking for a cluster in this
101867869Smckusick 		 * cylinder group. Update the cluster summary information
101967869Smckusick 		 * to reflect the true maximum sized cluster so that
102067869Smckusick 		 * future cluster allocation requests can avoid reading
102167869Smckusick 		 * the cylinder group map only to find no clusters.
102267869Smckusick 		 */
102367869Smckusick 		lp = &cg_clustersum(cgp)[len - 1];
102467869Smckusick 		for (i = len - 1; i > 0; i--)
102567869Smckusick 			if (*lp-- > 0)
102667869Smckusick 				break;
102767869Smckusick 		fs->fs_maxcluster[cg] = i;
102865999Smckusick 		goto fail;
102967869Smckusick 	}
103065999Smckusick 	/*
103165999Smckusick 	 * Search the cluster map to find a big enough cluster.
103265999Smckusick 	 * We take the first one that we find, even if it is larger
103365999Smckusick 	 * than we need as we prefer to get one close to the previous
103465999Smckusick 	 * block allocation. We do not search before the current
103565999Smckusick 	 * preference point as we do not want to allocate a block
103665999Smckusick 	 * that is allocated before the previous one (as we will
103765999Smckusick 	 * then have to wait for another pass of the elevator
103865999Smckusick 	 * algorithm before it will be read). We prefer to fail and
103965999Smckusick 	 * be recalled to try an allocation in the next cylinder group.
104065999Smckusick 	 */
104165999Smckusick 	if (dtog(fs, bpref) != cg)
104265999Smckusick 		bpref = 0;
104365999Smckusick 	else
104465999Smckusick 		bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
104565999Smckusick 	mapp = &cg_clustersfree(cgp)[bpref / NBBY];
104665999Smckusick 	map = *mapp++;
104765999Smckusick 	bit = 1 << (bpref % NBBY);
104868336Smckusick 	for (run = 0, got = bpref; got < cgp->cg_nclusterblks; got++) {
104965999Smckusick 		if ((map & bit) == 0) {
105065999Smckusick 			run = 0;
105165999Smckusick 		} else {
105265999Smckusick 			run++;
105365999Smckusick 			if (run == len)
105465999Smckusick 				break;
105565999Smckusick 		}
105668336Smckusick 		if ((got & (NBBY - 1)) != (NBBY - 1)) {
105765999Smckusick 			bit <<= 1;
105865999Smckusick 		} else {
105965999Smckusick 			map = *mapp++;
106065999Smckusick 			bit = 1;
106165999Smckusick 		}
106265999Smckusick 	}
106368336Smckusick 	if (got == cgp->cg_nclusterblks)
106465999Smckusick 		goto fail;
106565999Smckusick 	/*
106665999Smckusick 	 * Allocate the cluster that we have found.
106765999Smckusick 	 */
106868336Smckusick 	for (i = 1; i <= len; i++)
106968336Smckusick 		if (!ffs_isblock(fs, cg_blksfree(cgp), got - run + i))
107068336Smckusick 			panic("ffs_clusteralloc: map mismatch");
107168336Smckusick 	bno = cg * fs->fs_fpg + blkstofrags(fs, got - run + 1);
107265999Smckusick 	len = blkstofrags(fs, len);
107365999Smckusick 	for (i = 0; i < len; i += fs->fs_frag)
107468336Smckusick 		if ((got = ffs_alloccgblk(fs, cgp, bno + i)) != bno + i)
107565999Smckusick 			panic("ffs_clusteralloc: lost block");
107665999Smckusick 	brelse(bp);
107765999Smckusick 	return (bno);
107865999Smckusick 
107965999Smckusick fail:
108065999Smckusick 	brelse(bp);
108165999Smckusick 	return (0);
108265999Smckusick }
108365999Smckusick 
108465999Smckusick /*
10855375Smckusic  * Determine whether an inode can be allocated.
10865375Smckusic  *
10875375Smckusic  * Check to see if an inode is available, and if it is,
10885375Smckusic  * allocate it using the following policy:
10895375Smckusic  *   1) allocate the requested inode.
10905375Smckusic  *   2) allocate the next available inode after the requested
10915375Smckusic  *      inode in the specified cylinder group.
10925375Smckusic  */
109368122Smckusick static ino_t
109464603Sbostic ffs_nodealloccg(ip, cg, ipref, mode)
10955965Smckusic 	struct inode *ip;
10964359Smckusick 	int cg;
109768554Smckusick 	ufs_daddr_t ipref;
10984359Smckusick 	int mode;
10994359Smckusick {
11005965Smckusic 	register struct fs *fs;
11014463Smckusic 	register struct cg *cgp;
110216784Smckusick 	struct buf *bp;
110337735Smckusick 	int error, start, len, loc, map, i;
11044359Smckusick 
11055965Smckusic 	fs = ip->i_fs;
11065322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
11076294Smckusick 		return (NULL);
110837735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
110938776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
111037735Smckusick 	if (error) {
111137735Smckusick 		brelse(bp);
111237735Smckusick 		return (NULL);
111337735Smckusick 	}
111464508Sbostic 	cgp = (struct cg *)bp->b_data;
111537735Smckusick 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
11165960Smckusic 		brelse(bp);
11176294Smckusick 		return (NULL);
11185960Smckusic 	}
11198105Sroot 	cgp->cg_time = time.tv_sec;
11204359Smckusick 	if (ipref) {
11214359Smckusick 		ipref %= fs->fs_ipg;
112234143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
11234359Smckusick 			goto gotit;
112416784Smckusick 	}
112516784Smckusick 	start = cgp->cg_irotor / NBBY;
112616784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
112734143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
112816784Smckusick 	if (loc == 0) {
112917697Smckusick 		len = start + 1;
113017697Smckusick 		start = 0;
113134143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
113217697Smckusick 		if (loc == 0) {
113365718Sbostic 			printf("cg = %d, irotor = %d, fs = %s\n",
113417697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
113564603Sbostic 			panic("ffs_nodealloccg: map corrupted");
113617697Smckusick 			/* NOTREACHED */
113717697Smckusick 		}
113816784Smckusick 	}
113916784Smckusick 	i = start + len - loc;
114034143Smckusick 	map = cg_inosused(cgp)[i];
114116784Smckusick 	ipref = i * NBBY;
114216784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
114316784Smckusick 		if ((map & i) == 0) {
11444359Smckusick 			cgp->cg_irotor = ipref;
11454359Smckusick 			goto gotit;
11464359Smckusick 		}
11474359Smckusick 	}
114816784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
114964603Sbostic 	panic("ffs_nodealloccg: block not in map");
115016784Smckusick 	/* NOTREACHED */
11514359Smckusick gotit:
115234143Smckusick 	setbit(cg_inosused(cgp), ipref);
11534792Smckusic 	cgp->cg_cs.cs_nifree--;
11544792Smckusic 	fs->fs_cstotal.cs_nifree--;
11555322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
115650893Smckusick 	fs->fs_fmod = 1;
11574359Smckusick 	if ((mode & IFMT) == IFDIR) {
11584792Smckusic 		cgp->cg_cs.cs_ndir++;
11594792Smckusic 		fs->fs_cstotal.cs_ndir++;
11605322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
11614359Smckusick 	}
11624359Smckusick 	bdwrite(bp);
11634359Smckusick 	return (cg * fs->fs_ipg + ipref);
11644359Smckusick }
11654359Smckusick 
11665375Smckusic /*
11675375Smckusic  * Free a block or fragment.
11685375Smckusic  *
11695375Smckusic  * The specified block or fragment is placed back in the
11705375Smckusic  * free map. If a fragment is deallocated, a possible
11715375Smckusic  * block reassembly is checked.
11725375Smckusic  */
117351469Sbostic ffs_blkfree(ip, bno, size)
11745965Smckusic 	register struct inode *ip;
117568554Smckusick 	ufs_daddr_t bno;
117653059Smckusick 	long size;
11774359Smckusick {
11784359Smckusick 	register struct fs *fs;
11794359Smckusick 	register struct cg *cgp;
118037735Smckusick 	struct buf *bp;
118168554Smckusick 	ufs_daddr_t blkno;
118265999Smckusick 	int i, error, cg, blk, frags, bbase;
11834359Smckusick 
11845965Smckusic 	fs = ip->i_fs;
118564508Sbostic 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
11866716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
11876716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
118831402Smckusick 		panic("blkfree: bad size");
11896716Smckusick 	}
11905377Smckusic 	cg = dtog(fs, bno);
119164508Sbostic 	if ((u_int)bno >= fs->fs_size) {
11926567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
119353244Smckusick 		ffs_fserr(fs, ip->i_uid, "bad block");
11944359Smckusick 		return;
11956567Smckusic 	}
119637735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
119738776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
119837735Smckusick 	if (error) {
119937735Smckusick 		brelse(bp);
120037735Smckusick 		return;
120137735Smckusick 	}
120264508Sbostic 	cgp = (struct cg *)bp->b_data;
120337735Smckusick 	if (!cg_chkmagic(cgp)) {
12045960Smckusic 		brelse(bp);
12054359Smckusick 		return;
12065960Smckusic 	}
12078105Sroot 	cgp->cg_time = time.tv_sec;
12085377Smckusic 	bno = dtogd(fs, bno);
12095322Smckusic 	if (size == fs->fs_bsize) {
121065999Smckusick 		blkno = fragstoblks(fs, bno);
121165999Smckusick 		if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
12126716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
12136716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
121431402Smckusick 			panic("blkfree: freeing free block");
12156567Smckusic 		}
121665999Smckusick 		ffs_setblock(fs, cg_blksfree(cgp), blkno);
121765999Smckusick 		ffs_clusteracct(fs, cgp, blkno, 1);
12184792Smckusic 		cgp->cg_cs.cs_nbfree++;
12194792Smckusic 		fs->fs_cstotal.cs_nbfree++;
12205322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
12215375Smckusic 		i = cbtocylno(fs, bno);
122234143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
122334143Smckusick 		cg_blktot(cgp)[i]++;
12244426Smckusic 	} else {
122517224Smckusick 		bbase = bno - fragnum(fs, bno);
12264463Smckusic 		/*
12274463Smckusic 		 * decrement the counts associated with the old frags
12284463Smckusic 		 */
122934143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
123051469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
12314463Smckusic 		/*
12324463Smckusic 		 * deallocate the fragment
12334463Smckusic 		 */
12345960Smckusic 		frags = numfrags(fs, size);
12354463Smckusic 		for (i = 0; i < frags; i++) {
123634143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
12376716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
12386716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
123931402Smckusick 				panic("blkfree: freeing free frag");
12406716Smckusick 			}
124134143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
12424426Smckusic 		}
12436294Smckusick 		cgp->cg_cs.cs_nffree += i;
12446294Smckusick 		fs->fs_cstotal.cs_nffree += i;
12456294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
12464463Smckusic 		/*
12474463Smckusic 		 * add back in counts associated with the new frags
12484463Smckusic 		 */
124934143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
125051469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
12514463Smckusic 		/*
12524463Smckusic 		 * if a complete block has been reassembled, account for it
12534463Smckusic 		 */
125465999Smckusick 		blkno = fragstoblks(fs, bbase);
125565999Smckusick 		if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
12565322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
12575322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
12585322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
125965999Smckusick 			ffs_clusteracct(fs, cgp, blkno, 1);
12604792Smckusic 			cgp->cg_cs.cs_nbfree++;
12614792Smckusic 			fs->fs_cstotal.cs_nbfree++;
12625322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
12635375Smckusic 			i = cbtocylno(fs, bbase);
126434143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
126534143Smckusick 			cg_blktot(cgp)[i]++;
12664426Smckusic 		}
12674426Smckusic 	}
126850893Smckusick 	fs->fs_fmod = 1;
12694359Smckusick 	bdwrite(bp);
12704359Smckusick }
12714359Smckusick 
12725375Smckusic /*
12735375Smckusic  * Free an inode.
12745375Smckusic  *
12755375Smckusic  * The specified inode is placed back in the free map.
12765375Smckusic  */
127753577Sheideman int
127854653Smckusick ffs_vfree(ap)
127954653Smckusick 	struct vop_vfree_args /* {
128054653Smckusick 		struct vnode *a_pvp;
128154653Smckusick 		ino_t a_ino;
128254653Smckusick 		int a_mode;
128354653Smckusick 	} */ *ap;
12844359Smckusick {
12854359Smckusick 	register struct fs *fs;
12864359Smckusick 	register struct cg *cgp;
128751541Smckusick 	register struct inode *pip;
128854653Smckusick 	ino_t ino = ap->a_ino;
128937735Smckusick 	struct buf *bp;
129037735Smckusick 	int error, cg;
12914359Smckusick 
129253583Sheideman 	pip = VTOI(ap->a_pvp);
129351469Sbostic 	fs = pip->i_fs;
129454653Smckusick 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
129554653Smckusick 		panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
129654653Smckusick 		    pip->i_dev, ino, fs->fs_fsmnt);
129764603Sbostic 	cg = ino_to_cg(fs, ino);
129851469Sbostic 	error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
129938776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
130037735Smckusick 	if (error) {
130137735Smckusick 		brelse(bp);
130253577Sheideman 		return (0);
130337735Smckusick 	}
130464508Sbostic 	cgp = (struct cg *)bp->b_data;
130537735Smckusick 	if (!cg_chkmagic(cgp)) {
13065960Smckusic 		brelse(bp);
130753577Sheideman 		return (0);
13085960Smckusic 	}
13098105Sroot 	cgp->cg_time = time.tv_sec;
131054653Smckusick 	ino %= fs->fs_ipg;
131154653Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
131254653Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
131354653Smckusick 		    pip->i_dev, ino, fs->fs_fsmnt);
131448057Smckusick 		if (fs->fs_ronly == 0)
131548057Smckusick 			panic("ifree: freeing free inode");
13166716Smckusick 	}
131754653Smckusick 	clrbit(cg_inosused(cgp), ino);
131854653Smckusick 	if (ino < cgp->cg_irotor)
131954653Smckusick 		cgp->cg_irotor = ino;
13204792Smckusic 	cgp->cg_cs.cs_nifree++;
13214792Smckusic 	fs->fs_cstotal.cs_nifree++;
13225322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
132353583Sheideman 	if ((ap->a_mode & IFMT) == IFDIR) {
13244792Smckusic 		cgp->cg_cs.cs_ndir--;
13254792Smckusic 		fs->fs_cstotal.cs_ndir--;
13265322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
13274359Smckusick 	}
132850893Smckusick 	fs->fs_fmod = 1;
13294359Smckusick 	bdwrite(bp);
133053577Sheideman 	return (0);
13314359Smckusick }
13324359Smckusick 
13334463Smckusic /*
13345375Smckusic  * Find a block of the specified size in the specified cylinder group.
13355375Smckusic  *
13364651Smckusic  * It is a panic if a request is made to find a block if none are
13374651Smckusic  * available.
13384651Smckusic  */
133968554Smckusick static ufs_daddr_t
134051469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz)
13414651Smckusic 	register struct fs *fs;
13424651Smckusic 	register struct cg *cgp;
134368554Smckusick 	ufs_daddr_t bpref;
13444651Smckusic 	int allocsiz;
13454651Smckusic {
134668554Smckusick 	ufs_daddr_t bno;
13474651Smckusic 	int start, len, loc, i;
13484651Smckusic 	int blk, field, subfield, pos;
13494651Smckusic 
13504651Smckusic 	/*
13514651Smckusic 	 * find the fragment by searching through the free block
13524651Smckusic 	 * map for an appropriate bit pattern
13534651Smckusic 	 */
13544651Smckusic 	if (bpref)
13555377Smckusic 		start = dtogd(fs, bpref) / NBBY;
13564651Smckusic 	else
13574651Smckusic 		start = cgp->cg_frotor / NBBY;
13585398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
135964508Sbostic 	loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[start],
136034476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
136134476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
13624651Smckusic 	if (loc == 0) {
13636531Smckusick 		len = start + 1;
13646531Smckusick 		start = 0;
136564508Sbostic 		loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[0],
136634476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
136734476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
136816784Smckusick 		if (loc == 0) {
136916784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
137016784Smckusick 			    start, len, fs->fs_fsmnt);
137151469Sbostic 			panic("ffs_alloccg: map corrupted");
137217697Smckusick 			/* NOTREACHED */
137316784Smckusick 		}
13744651Smckusic 	}
13754651Smckusic 	bno = (start + len - loc) * NBBY;
13764651Smckusic 	cgp->cg_frotor = bno;
13774651Smckusic 	/*
13784651Smckusic 	 * found the byte in the map
13794651Smckusic 	 * sift through the bits to find the selected frag
13804651Smckusic 	 */
13816294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
138234143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
13834651Smckusic 		blk <<= 1;
13844651Smckusic 		field = around[allocsiz];
13854651Smckusic 		subfield = inside[allocsiz];
13865322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
13876294Smckusick 			if ((blk & field) == subfield)
13886294Smckusick 				return (bno + pos);
13894651Smckusic 			field <<= 1;
13904651Smckusic 			subfield <<= 1;
13914651Smckusic 		}
13924651Smckusic 	}
13936716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
139451469Sbostic 	panic("ffs_alloccg: block not in map");
13956531Smckusick 	return (-1);
13964651Smckusic }
13974651Smckusic 
13984651Smckusic /*
139965999Smckusick  * Update the cluster map because of an allocation or free.
140065999Smckusick  *
140165999Smckusick  * Cnt == 1 means free; cnt == -1 means allocating.
140265999Smckusick  */
140365999Smckusick ffs_clusteracct(fs, cgp, blkno, cnt)
140465999Smckusick 	struct fs *fs;
140565999Smckusick 	struct cg *cgp;
140668554Smckusick 	ufs_daddr_t blkno;
140765999Smckusick 	int cnt;
140865999Smckusick {
140968122Smckusick 	int32_t *sump;
141067869Smckusick 	int32_t *lp;
141165999Smckusick 	u_char *freemapp, *mapp;
141265999Smckusick 	int i, start, end, forw, back, map, bit;
141365999Smckusick 
141465999Smckusick 	if (fs->fs_contigsumsize <= 0)
141565999Smckusick 		return;
141665999Smckusick 	freemapp = cg_clustersfree(cgp);
141765999Smckusick 	sump = cg_clustersum(cgp);
141865999Smckusick 	/*
141965999Smckusick 	 * Allocate or clear the actual block.
142065999Smckusick 	 */
142165999Smckusick 	if (cnt > 0)
142265999Smckusick 		setbit(freemapp, blkno);
142365999Smckusick 	else
142465999Smckusick 		clrbit(freemapp, blkno);
142565999Smckusick 	/*
142665999Smckusick 	 * Find the size of the cluster going forward.
142765999Smckusick 	 */
142865999Smckusick 	start = blkno + 1;
142965999Smckusick 	end = start + fs->fs_contigsumsize;
143065999Smckusick 	if (end >= cgp->cg_nclusterblks)
143165999Smckusick 		end = cgp->cg_nclusterblks;
143265999Smckusick 	mapp = &freemapp[start / NBBY];
143365999Smckusick 	map = *mapp++;
143465999Smckusick 	bit = 1 << (start % NBBY);
143565999Smckusick 	for (i = start; i < end; i++) {
143665999Smckusick 		if ((map & bit) == 0)
143765999Smckusick 			break;
143865999Smckusick 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
143965999Smckusick 			bit <<= 1;
144065999Smckusick 		} else {
144165999Smckusick 			map = *mapp++;
144265999Smckusick 			bit = 1;
144365999Smckusick 		}
144465999Smckusick 	}
144565999Smckusick 	forw = i - start;
144665999Smckusick 	/*
144765999Smckusick 	 * Find the size of the cluster going backward.
144865999Smckusick 	 */
144965999Smckusick 	start = blkno - 1;
145065999Smckusick 	end = start - fs->fs_contigsumsize;
145165999Smckusick 	if (end < 0)
145265999Smckusick 		end = -1;
145365999Smckusick 	mapp = &freemapp[start / NBBY];
145465999Smckusick 	map = *mapp--;
145565999Smckusick 	bit = 1 << (start % NBBY);
145665999Smckusick 	for (i = start; i > end; i--) {
145765999Smckusick 		if ((map & bit) == 0)
145865999Smckusick 			break;
145965999Smckusick 		if ((i & (NBBY - 1)) != 0) {
146065999Smckusick 			bit >>= 1;
146165999Smckusick 		} else {
146265999Smckusick 			map = *mapp--;
146365999Smckusick 			bit = 1 << (NBBY - 1);
146465999Smckusick 		}
146565999Smckusick 	}
146665999Smckusick 	back = start - i;
146765999Smckusick 	/*
146865999Smckusick 	 * Account for old cluster and the possibly new forward and
146965999Smckusick 	 * back clusters.
147065999Smckusick 	 */
147165999Smckusick 	i = back + forw + 1;
147265999Smckusick 	if (i > fs->fs_contigsumsize)
147365999Smckusick 		i = fs->fs_contigsumsize;
147465999Smckusick 	sump[i] += cnt;
147565999Smckusick 	if (back > 0)
147665999Smckusick 		sump[back] -= cnt;
147765999Smckusick 	if (forw > 0)
147865999Smckusick 		sump[forw] -= cnt;
147967869Smckusick 	/*
148067869Smckusick 	 * Update cluster summary information.
148167869Smckusick 	 */
148267869Smckusick 	lp = &sump[fs->fs_contigsumsize];
148367869Smckusick 	for (i = fs->fs_contigsumsize; i > 0; i--)
148467869Smckusick 		if (*lp-- > 0)
148567869Smckusick 			break;
148667869Smckusick 	fs->fs_maxcluster[cgp->cg_cgx] = i;
148765999Smckusick }
148865999Smckusick 
148965999Smckusick /*
14905375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
14915375Smckusic  *
14925375Smckusic  * The form of the error message is:
14934359Smckusick  *	fs: error message
14944359Smckusick  */
149551469Sbostic static void
149651469Sbostic ffs_fserr(fs, uid, cp)
14974359Smckusick 	struct fs *fs;
149851469Sbostic 	u_int uid;
14994359Smckusick 	char *cp;
15004359Smckusick {
15014359Smckusick 
150242318Smckusick 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
15034359Smckusick }
1504