xref: /csrg-svn/sys/ufs/ffs/ffs_alloc.c (revision 65999)
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*65999Smckusick  *	@(#)ffs_alloc.c	8.6 (Berkeley) 02/05/94
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 
2951469Sbostic static daddr_t	ffs_alloccg __P((struct inode *, int, daddr_t, int));
3051469Sbostic static daddr_t	ffs_alloccgblk __P((struct fs *, struct cg *, daddr_t));
31*65999Smckusick static daddr_t	ffs_clusteralloc __P((struct inode *, int, daddr_t, int));
3251469Sbostic static ino_t	ffs_dirpref __P((struct fs *));
3351469Sbostic static daddr_t	ffs_fragextend __P((struct inode *, int, long, int, int));
3451469Sbostic static void	ffs_fserr __P((struct fs *, u_int, char *));
3551469Sbostic static u_long	ffs_hashalloc
3651469Sbostic 		    __P((struct inode *, int, long, int, u_long (*)()));
3764603Sbostic static ino_t	ffs_nodealloccg __P((struct inode *, int, daddr_t, int));
3851469Sbostic static daddr_t	ffs_mapsearch __P((struct fs *, struct cg *, daddr_t, int));
3951469Sbostic 
405375Smckusic /*
415375Smckusic  * Allocate a block in the file system.
425375Smckusic  *
435375Smckusic  * The size of the requested block is given, which must be some
445375Smckusic  * multiple of fs_fsize and <= fs_bsize.
455375Smckusic  * A preference may be optionally specified. If a preference is given
465375Smckusic  * the following hierarchy is used to allocate a block:
475375Smckusic  *   1) allocate the requested block.
485375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
495375Smckusic  *   3) allocate a block in the same cylinder group.
505375Smckusic  *   4) quadradically rehash into other cylinder groups, until an
515375Smckusic  *      available block is located.
525375Smckusic  * If no block preference is given the following heirarchy is used
535375Smckusic  * to allocate a block:
545375Smckusic  *   1) allocate a block in the cylinder group that contains the
555375Smckusic  *      inode for the file.
565375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
575375Smckusic  *      available block is located.
585375Smckusic  */
5953244Smckusick ffs_alloc(ip, lbn, bpref, size, cred, bnp)
604463Smckusic 	register struct inode *ip;
6139678Smckusick 	daddr_t lbn, bpref;
624359Smckusick 	int size;
6353244Smckusick 	struct ucred *cred;
6439678Smckusick 	daddr_t *bnp;
654359Smckusick {
6665468Sbostic 	register struct fs *fs;
674359Smckusick 	daddr_t bno;
6837735Smckusick 	int cg, error;
694359Smckusick 
7039678Smckusick 	*bnp = 0;
715965Smckusic 	fs = ip->i_fs;
7253244Smckusick #ifdef DIAGNOSTIC
7364508Sbostic 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
746716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
756716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
7651469Sbostic 		panic("ffs_alloc: bad size");
776716Smckusick 	}
7853244Smckusick 	if (cred == NOCRED)
7953244Smckusick 		panic("ffs_alloc: missing credential\n");
8053244Smckusick #endif /* DIAGNOSTIC */
815322Smckusic 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
824359Smckusick 		goto nospace;
8341309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
844792Smckusic 		goto nospace;
857650Ssam #ifdef QUOTA
8641309Smckusick 	if (error = chkdq(ip, (long)btodb(size), cred, 0))
8737735Smckusick 		return (error);
887483Skre #endif
894948Smckusic 	if (bpref >= fs->fs_size)
904948Smckusic 		bpref = 0;
914359Smckusick 	if (bpref == 0)
9264603Sbostic 		cg = ino_to_cg(fs, ip->i_number);
934359Smckusick 	else
945377Smckusic 		cg = dtog(fs, bpref);
9551469Sbostic 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, size,
9651469Sbostic 	    (u_long (*)())ffs_alloccg);
9739678Smckusick 	if (bno > 0) {
9839678Smckusick 		ip->i_blocks += btodb(size);
9964603Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
10039678Smckusick 		*bnp = bno;
10139678Smckusick 		return (0);
10239678Smckusick 	}
10345173Smckusick #ifdef QUOTA
10445173Smckusick 	/*
10545173Smckusick 	 * Restore user's disk quota because allocation failed.
10645173Smckusick 	 */
10745173Smckusick 	(void) chkdq(ip, (long)-btodb(size), cred, FORCE);
10845173Smckusick #endif
1094359Smckusick nospace:
11051469Sbostic 	ffs_fserr(fs, cred->cr_uid, "file system full");
1114359Smckusick 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
11237735Smckusick 	return (ENOSPC);
1134359Smckusick }
1144359Smckusick 
1155375Smckusic /*
1165375Smckusic  * Reallocate a fragment to a bigger size
1175375Smckusic  *
1185375Smckusic  * The number and size of the old block is given, and a preference
1195375Smckusic  * and new size is also specified. The allocator attempts to extend
1205375Smckusic  * the original block. Failing that, the regular block allocator is
1215375Smckusic  * invoked to get an appropriate block.
1225375Smckusic  */
12353244Smckusick ffs_realloccg(ip, lbprev, bpref, osize, nsize, cred, bpp)
1245965Smckusic 	register struct inode *ip;
12553059Smckusick 	daddr_t lbprev;
12639678Smckusick 	daddr_t bpref;
1274426Smckusic 	int osize, nsize;
12853244Smckusick 	struct ucred *cred;
12937735Smckusick 	struct buf **bpp;
1304426Smckusic {
1314426Smckusic 	register struct fs *fs;
13265468Sbostic 	struct buf *bp;
13345719Smckusick 	int cg, request, error;
13445719Smckusick 	daddr_t bprev, bno;
1354426Smckusic 
13637735Smckusick 	*bpp = 0;
1375965Smckusic 	fs = ip->i_fs;
13853244Smckusick #ifdef DIAGNOSTIC
13964508Sbostic 	if ((u_int)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
14064508Sbostic 	    (u_int)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
14151469Sbostic 		printf(
14251469Sbostic 		    "dev = 0x%x, bsize = %d, osize = %d, nsize = %d, fs = %s\n",
1436716Smckusick 		    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
14451469Sbostic 		panic("ffs_realloccg: bad size");
1456716Smckusick 	}
14653244Smckusick 	if (cred == NOCRED)
14753244Smckusick 		panic("ffs_realloccg: missing credential\n");
14853244Smckusick #endif /* DIAGNOSTIC */
14941309Smckusick 	if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
1504792Smckusic 		goto nospace;
15139678Smckusick 	if ((bprev = ip->i_db[lbprev]) == 0) {
1526716Smckusick 		printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
1536716Smckusick 		    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
15451469Sbostic 		panic("ffs_realloccg: bad bprev");
1556716Smckusick 	}
15639678Smckusick 	/*
15739678Smckusick 	 * Allocate the extra space in the buffer.
15839678Smckusick 	 */
15939678Smckusick 	if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
16039678Smckusick 		brelse(bp);
16139678Smckusick 		return (error);
16239678Smckusick 	}
16345173Smckusick #ifdef QUOTA
16445173Smckusick 	if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0)) {
16545173Smckusick 		brelse(bp);
16645173Smckusick 		return (error);
16745173Smckusick 	}
16845173Smckusick #endif
16939678Smckusick 	/*
17039678Smckusick 	 * Check for extension in the existing location.
17139678Smckusick 	 */
1726294Smckusick 	cg = dtog(fs, bprev);
17351469Sbostic 	if (bno = ffs_fragextend(ip, cg, (long)bprev, osize, nsize)) {
17439887Smckusick 		if (bp->b_blkno != fsbtodb(fs, bno))
17539678Smckusick 			panic("bad blockno");
17639762Smckusick 		ip->i_blocks += btodb(nsize - osize);
17764603Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
17848948Smckusick 		allocbuf(bp, nsize);
17948948Smckusick 		bp->b_flags |= B_DONE;
18064508Sbostic 		bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
18137735Smckusick 		*bpp = bp;
18237735Smckusick 		return (0);
1834463Smckusic 	}
18439678Smckusick 	/*
18539678Smckusick 	 * Allocate a new disk location.
18639678Smckusick 	 */
1874948Smckusic 	if (bpref >= fs->fs_size)
1884948Smckusic 		bpref = 0;
18926253Skarels 	switch ((int)fs->fs_optim) {
19025256Smckusick 	case FS_OPTSPACE:
19125256Smckusick 		/*
19225256Smckusick 		 * Allocate an exact sized fragment. Although this makes
19325256Smckusick 		 * best use of space, we will waste time relocating it if
19425256Smckusick 		 * the file continues to grow. If the fragmentation is
19525256Smckusick 		 * less than half of the minimum free reserve, we choose
19625256Smckusick 		 * to begin optimizing for time.
19725256Smckusick 		 */
19824698Smckusick 		request = nsize;
19925256Smckusick 		if (fs->fs_minfree < 5 ||
20025256Smckusick 		    fs->fs_cstotal.cs_nffree >
20125256Smckusick 		    fs->fs_dsize * fs->fs_minfree / (2 * 100))
20225256Smckusick 			break;
20325256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from SPACE to TIME\n",
20425256Smckusick 			fs->fs_fsmnt);
20525256Smckusick 		fs->fs_optim = FS_OPTTIME;
20625256Smckusick 		break;
20725256Smckusick 	case FS_OPTTIME:
20825256Smckusick 		/*
20951469Sbostic 		 * At this point we have discovered a file that is trying to
21051469Sbostic 		 * grow a small fragment to a larger fragment. To save time,
21151469Sbostic 		 * we allocate a full sized block, then free the unused portion.
21251469Sbostic 		 * If the file continues to grow, the `ffs_fragextend' call
21351469Sbostic 		 * above will be able to grow it in place without further
21451469Sbostic 		 * copying. If aberrant programs cause disk fragmentation to
21551469Sbostic 		 * grow within 2% of the free reserve, we choose to begin
21651469Sbostic 		 * optimizing for space.
21725256Smckusick 		 */
21824698Smckusick 		request = fs->fs_bsize;
21925256Smckusick 		if (fs->fs_cstotal.cs_nffree <
22025256Smckusick 		    fs->fs_dsize * (fs->fs_minfree - 2) / 100)
22125256Smckusick 			break;
22225256Smckusick 		log(LOG_NOTICE, "%s: optimization changed from TIME to SPACE\n",
22325256Smckusick 			fs->fs_fsmnt);
22425256Smckusick 		fs->fs_optim = FS_OPTSPACE;
22525256Smckusick 		break;
22625256Smckusick 	default:
22725256Smckusick 		printf("dev = 0x%x, optim = %d, fs = %s\n",
22825256Smckusick 		    ip->i_dev, fs->fs_optim, fs->fs_fsmnt);
22951469Sbostic 		panic("ffs_realloccg: bad optim");
23025256Smckusick 		/* NOTREACHED */
23125256Smckusick 	}
23251469Sbostic 	bno = (daddr_t)ffs_hashalloc(ip, cg, (long)bpref, request,
23351469Sbostic 	    (u_long (*)())ffs_alloccg);
2346567Smckusic 	if (bno > 0) {
23545719Smckusick 		bp->b_blkno = fsbtodb(fs, bno);
23645719Smckusick 		(void) vnode_pager_uncache(ITOV(ip));
23753059Smckusick 		ffs_blkfree(ip, bprev, (long)osize);
23824698Smckusick 		if (nsize < request)
23951469Sbostic 			ffs_blkfree(ip, bno + numfrags(fs, nsize),
24053059Smckusick 			    (long)(request - nsize));
24112643Ssam 		ip->i_blocks += btodb(nsize - osize);
24264603Sbostic 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
24348948Smckusick 		allocbuf(bp, nsize);
24448948Smckusick 		bp->b_flags |= B_DONE;
24564508Sbostic 		bzero((char *)bp->b_data + osize, (u_int)nsize - osize);
24637735Smckusick 		*bpp = bp;
24737735Smckusick 		return (0);
2484463Smckusic 	}
24945173Smckusick #ifdef QUOTA
25045173Smckusick 	/*
25145173Smckusick 	 * Restore user's disk quota because allocation failed.
25245173Smckusick 	 */
25345173Smckusick 	(void) chkdq(ip, (long)-btodb(nsize - osize), cred, FORCE);
25445173Smckusick #endif
25539678Smckusick 	brelse(bp);
2564792Smckusic nospace:
2574463Smckusic 	/*
2584463Smckusic 	 * no space available
2594463Smckusic 	 */
26051469Sbostic 	ffs_fserr(fs, cred->cr_uid, "file system full");
2614426Smckusic 	uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
26237735Smckusick 	return (ENOSPC);
2634426Smckusic }
2644426Smckusic 
2655375Smckusic /*
266*65999Smckusick  * Reallocate a sequence of blocks into a contiguous sequence of blocks.
267*65999Smckusick  *
268*65999Smckusick  * The vnode and an array of buffer pointers for a range of sequential
269*65999Smckusick  * logical blocks to be made contiguous is given. The allocator attempts
270*65999Smckusick  * to find a range of sequential blocks starting as close as possible to
271*65999Smckusick  * an fs_rotdelay offset from the end of the allocation for the logical
272*65999Smckusick  * block immediately preceeding the current range. If successful, the
273*65999Smckusick  * physical block numbers in the buffer pointers and in the inode are
274*65999Smckusick  * changed to reflect the new allocation. If unsuccessful, the allocation
275*65999Smckusick  * is left unchanged. The success in doing the reallocation is returned.
276*65999Smckusick  * Note that the error return is not reflected back to the user. Rather
277*65999Smckusick  * the previous block allocation will be used.
278*65999Smckusick  */
279*65999Smckusick int
280*65999Smckusick ffs_reallocblks(ap)
281*65999Smckusick 	struct vop_reallocblks_args /* {
282*65999Smckusick 		struct vnode *a_vp;
283*65999Smckusick 		struct cluster_save *a_buflist;
284*65999Smckusick 	} */ *ap;
285*65999Smckusick {
286*65999Smckusick 	struct fs *fs;
287*65999Smckusick 	struct inode *ip;
288*65999Smckusick 	struct vnode *vp;
289*65999Smckusick 	struct buf *sbp, *ebp;
290*65999Smckusick 	daddr_t *bap, *sbap, *ebap;
291*65999Smckusick 	struct cluster_save *buflist;
292*65999Smckusick 	daddr_t start_lbn, end_lbn, soff, eoff, newblk, blkno;
293*65999Smckusick 	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
294*65999Smckusick 	int i, len, start_lvl, end_lvl, pref, ssize;
295*65999Smckusick 
296*65999Smckusick 	vp = ap->a_vp;
297*65999Smckusick 	ip = VTOI(vp);
298*65999Smckusick 	fs = ip->i_fs;
299*65999Smckusick 	if (fs->fs_contigsumsize <= 0)
300*65999Smckusick 		return (ENOSPC);
301*65999Smckusick 	buflist = ap->a_buflist;
302*65999Smckusick 	len = buflist->bs_nchildren;
303*65999Smckusick 	start_lbn = buflist->bs_children[0]->b_lblkno;
304*65999Smckusick 	end_lbn = start_lbn + len - 1;
305*65999Smckusick #ifdef DIAGNOSTIC
306*65999Smckusick 	for (i = 1; i < len; i++)
307*65999Smckusick 		if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
308*65999Smckusick 			panic("ffs_reallocblks: non-cluster");
309*65999Smckusick #endif
310*65999Smckusick 	/*
311*65999Smckusick 	 * If the latest allocation is in a new cylinder group, assume that
312*65999Smckusick 	 * the filesystem has decided to move and do not force it back to
313*65999Smckusick 	 * the previous cylinder group.
314*65999Smckusick 	 */
315*65999Smckusick 	if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
316*65999Smckusick 	    dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
317*65999Smckusick 		return (ENOSPC);
318*65999Smckusick 	if (ufs_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
319*65999Smckusick 	    ufs_getlbns(vp, end_lbn, end_ap, &end_lvl))
320*65999Smckusick 		return (ENOSPC);
321*65999Smckusick 	/*
322*65999Smckusick 	 * Get the starting offset and block map for the first block.
323*65999Smckusick 	 */
324*65999Smckusick 	if (start_lvl == 0) {
325*65999Smckusick 		sbap = &ip->i_db[0];
326*65999Smckusick 		soff = start_lbn;
327*65999Smckusick 	} else {
328*65999Smckusick 		idp = &start_ap[start_lvl - 1];
329*65999Smckusick 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &sbp)) {
330*65999Smckusick 			brelse(sbp);
331*65999Smckusick 			return (ENOSPC);
332*65999Smckusick 		}
333*65999Smckusick 		sbap = (daddr_t *)sbp->b_data;
334*65999Smckusick 		soff = idp->in_off;
335*65999Smckusick 	}
336*65999Smckusick 	/*
337*65999Smckusick 	 * Find the preferred location for the cluster.
338*65999Smckusick 	 */
339*65999Smckusick 	pref = ffs_blkpref(ip, start_lbn, soff, sbap);
340*65999Smckusick 	/*
341*65999Smckusick 	 * If the block range spans two block maps, get the second map.
342*65999Smckusick 	 */
343*65999Smckusick 	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off >= len) {
344*65999Smckusick 		ssize = len;
345*65999Smckusick 	} else {
346*65999Smckusick 		ssize = len - (idp->in_off + 1);
347*65999Smckusick 		if (bread(vp, idp->in_lbn, (int)fs->fs_bsize, NOCRED, &ebp))
348*65999Smckusick 			goto fail;
349*65999Smckusick 		ebap = (daddr_t *)ebp->b_data;
350*65999Smckusick 	}
351*65999Smckusick 	/*
352*65999Smckusick 	 * Search the block map looking for an allocation of the desired size.
353*65999Smckusick 	 */
354*65999Smckusick 	if ((newblk = (daddr_t)ffs_hashalloc(ip, dtog(fs, pref), (long)pref,
355*65999Smckusick 	    len, (u_long (*)())ffs_clusteralloc)) == 0)
356*65999Smckusick 		goto fail;
357*65999Smckusick 	/*
358*65999Smckusick 	 * We have found a new contiguous block.
359*65999Smckusick 	 *
360*65999Smckusick 	 * First we have to replace the old block pointers with the new
361*65999Smckusick 	 * block pointers in the inode and indirect blocks associated
362*65999Smckusick 	 * with the file.
363*65999Smckusick 	 */
364*65999Smckusick 	blkno = newblk;
365*65999Smckusick 	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->fs_frag) {
366*65999Smckusick 		if (i == ssize)
367*65999Smckusick 			bap = ebap;
368*65999Smckusick #ifdef DIAGNOSTIC
369*65999Smckusick 		if (buflist->bs_children[i]->b_blkno != fsbtodb(fs, *bap))
370*65999Smckusick 			panic("ffs_reallocblks: alloc mismatch");
371*65999Smckusick #endif
372*65999Smckusick 		*bap++ = blkno;
373*65999Smckusick 	}
374*65999Smckusick 	/*
375*65999Smckusick 	 * Next we must write out the modified inode and indirect blocks.
376*65999Smckusick 	 * The writes are synchronous since the old block values may have
377*65999Smckusick 	 * been written to disk.
378*65999Smckusick 	 *
379*65999Smckusick 	 * These writes should be changed to be bdwrites (and the VOP_UPDATE
380*65999Smckusick 	 * dropped) when a flag has been added to the buffers and inodes
381*65999Smckusick 	 * to detect when they have been written. It should be set when the
382*65999Smckusick 	 * cluster is started and cleared whenever the buffer or inode is
383*65999Smckusick 	 * flushed. We can then check below to see if it is set, and do
384*65999Smckusick 	 * the synchronous write only when it has been cleared.
385*65999Smckusick 	 */
386*65999Smckusick 	if (sbap != &ip->i_db[0]) {
387*65999Smckusick 		bwrite(sbp);
388*65999Smckusick 	} else {
389*65999Smckusick 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
390*65999Smckusick 		VOP_UPDATE(vp, &time, &time, MNT_WAIT);
391*65999Smckusick 	}
392*65999Smckusick 	if (ssize < len)
393*65999Smckusick 		bwrite(ebp);
394*65999Smckusick 	/*
395*65999Smckusick 	 * Last, free the old blocks and assign the new blocks to the buffers.
396*65999Smckusick 	 */
397*65999Smckusick 	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->fs_frag) {
398*65999Smckusick 		ffs_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
399*65999Smckusick 		    fs->fs_bsize);
400*65999Smckusick 		buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
401*65999Smckusick 	}
402*65999Smckusick 	return (0);
403*65999Smckusick 
404*65999Smckusick fail:
405*65999Smckusick 	if (ssize < len)
406*65999Smckusick 		brelse(ebp);
407*65999Smckusick 	if (sbap != &ip->i_db[0])
408*65999Smckusick 		brelse(sbp);
409*65999Smckusick 	return (ENOSPC);
410*65999Smckusick }
411*65999Smckusick 
412*65999Smckusick /*
4135375Smckusic  * Allocate an inode in the file system.
4145375Smckusic  *
41551469Sbostic  * If allocating a directory, use ffs_dirpref to select the inode.
41651469Sbostic  * If allocating in a directory, the following hierarchy is followed:
41751469Sbostic  *   1) allocate the preferred inode.
4185375Smckusic  *   2) allocate an inode in the same cylinder group.
4195375Smckusic  *   3) quadradically rehash into other cylinder groups, until an
4205375Smckusic  *      available inode is located.
4215375Smckusic  * If no inode preference is given the following heirarchy is used
4225375Smckusic  * to allocate an inode:
4235375Smckusic  *   1) allocate an inode in cylinder group 0.
4245375Smckusic  *   2) quadradically rehash into other cylinder groups, until an
4255375Smckusic  *      available inode is located.
4265375Smckusic  */
42754653Smckusick ffs_valloc(ap)
42854653Smckusick 	struct vop_valloc_args /* {
42954653Smckusick 		struct vnode *a_pvp;
43054653Smckusick 		int a_mode;
43154653Smckusick 		struct ucred *a_cred;
43254653Smckusick 		struct vnode **a_vpp;
43354653Smckusick 	} */ *ap;
4344359Smckusick {
43553865Sheideman 	register struct vnode *pvp = ap->a_pvp;
43651541Smckusick 	register struct inode *pip;
4374359Smckusick 	register struct fs *fs;
4384359Smckusick 	register struct inode *ip;
43954653Smckusick 	mode_t mode = ap->a_mode;
44051469Sbostic 	ino_t ino, ipref;
44137735Smckusick 	int cg, error;
4424359Smckusick 
44353583Sheideman 	*ap->a_vpp = NULL;
44453865Sheideman 	pip = VTOI(pvp);
4455965Smckusic 	fs = pip->i_fs;
4464792Smckusic 	if (fs->fs_cstotal.cs_nifree == 0)
4474359Smckusick 		goto noinodes;
44851469Sbostic 
44954653Smckusick 	if ((mode & IFMT) == IFDIR)
45051541Smckusick 		ipref = ffs_dirpref(fs);
45151469Sbostic 	else
45251469Sbostic 		ipref = pip->i_number;
4534948Smckusic 	if (ipref >= fs->fs_ncg * fs->fs_ipg)
4544948Smckusic 		ipref = 0;
45564603Sbostic 	cg = ino_to_cg(fs, ipref);
45664603Sbostic 	ino = (ino_t)ffs_hashalloc(pip, cg, (long)ipref, mode, ffs_nodealloccg);
4574359Smckusick 	if (ino == 0)
4584359Smckusick 		goto noinodes;
45954653Smckusick 	error = VFS_VGET(pvp->v_mount, ino, ap->a_vpp);
46037735Smckusick 	if (error) {
46154653Smckusick 		VOP_VFREE(pvp, ino, mode);
46237735Smckusick 		return (error);
4634359Smckusick 	}
46453583Sheideman 	ip = VTOI(*ap->a_vpp);
4656716Smckusick 	if (ip->i_mode) {
46654653Smckusick 		printf("mode = 0%o, inum = %d, fs = %s\n",
4676716Smckusick 		    ip->i_mode, ip->i_number, fs->fs_fsmnt);
46851541Smckusick 		panic("ffs_valloc: dup alloc");
4696716Smckusick 	}
47012643Ssam 	if (ip->i_blocks) {				/* XXX */
47112643Ssam 		printf("free inode %s/%d had %d blocks\n",
47212643Ssam 		    fs->fs_fsmnt, ino, ip->i_blocks);
47312643Ssam 		ip->i_blocks = 0;
47412643Ssam 	}
47539516Smckusick 	ip->i_flags = 0;
47638255Smckusick 	/*
47738255Smckusick 	 * Set up a new generation number for this inode.
47838255Smckusick 	 */
47938255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
48038255Smckusick 		nextgennumber = time.tv_sec;
48138255Smckusick 	ip->i_gen = nextgennumber;
48237735Smckusick 	return (0);
4834359Smckusick noinodes:
48453583Sheideman 	ffs_fserr(fs, ap->a_cred->cr_uid, "out of inodes");
4856294Smckusick 	uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
48637735Smckusick 	return (ENOSPC);
4874359Smckusick }
4884359Smckusick 
4894651Smckusic /*
4905375Smckusic  * Find a cylinder to place a directory.
4915375Smckusic  *
4925375Smckusic  * The policy implemented by this algorithm is to select from
4935375Smckusic  * among those cylinder groups with above the average number of
4945375Smckusic  * free inodes, the one with the smallest number of directories.
4954651Smckusic  */
49651469Sbostic static ino_t
49751469Sbostic ffs_dirpref(fs)
4985965Smckusic 	register struct fs *fs;
4994359Smckusick {
5004651Smckusic 	int cg, minndir, mincg, avgifree;
5014359Smckusick 
5024792Smckusic 	avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
5034651Smckusic 	minndir = fs->fs_ipg;
5044359Smckusick 	mincg = 0;
5054651Smckusic 	for (cg = 0; cg < fs->fs_ncg; cg++)
5065322Smckusic 		if (fs->fs_cs(fs, cg).cs_ndir < minndir &&
5075322Smckusic 		    fs->fs_cs(fs, cg).cs_nifree >= avgifree) {
5084359Smckusick 			mincg = cg;
5095322Smckusic 			minndir = fs->fs_cs(fs, cg).cs_ndir;
5104359Smckusick 		}
5119163Ssam 	return ((ino_t)(fs->fs_ipg * mincg));
5124359Smckusick }
5134359Smckusick 
5144651Smckusic /*
5159163Ssam  * Select the desired position for the next block in a file.  The file is
5169163Ssam  * logically divided into sections. The first section is composed of the
5179163Ssam  * direct blocks. Each additional section contains fs_maxbpg blocks.
5189163Ssam  *
5199163Ssam  * If no blocks have been allocated in the first section, the policy is to
5209163Ssam  * request a block in the same cylinder group as the inode that describes
5219163Ssam  * the file. If no blocks have been allocated in any other section, the
5229163Ssam  * policy is to place the section in a cylinder group with a greater than
5239163Ssam  * average number of free blocks.  An appropriate cylinder group is found
52417696Smckusick  * by using a rotor that sweeps the cylinder groups. When a new group of
52517696Smckusick  * blocks is needed, the sweep begins in the cylinder group following the
52617696Smckusick  * cylinder group from which the previous allocation was made. The sweep
52717696Smckusick  * continues until a cylinder group with greater than the average number
52817696Smckusick  * of free blocks is found. If the allocation is for the first block in an
52917696Smckusick  * indirect block, the information on the previous allocation is unavailable;
53017696Smckusick  * here a best guess is made based upon the logical block number being
53117696Smckusick  * allocated.
5329163Ssam  *
5339163Ssam  * If a section is already partially allocated, the policy is to
5349163Ssam  * contiguously allocate fs_maxcontig blocks.  The end of one of these
5359163Ssam  * contiguous blocks and the beginning of the next is physically separated
5369163Ssam  * so that the disk head will be in transit between them for at least
5379163Ssam  * fs_rotdelay milliseconds.  This is to allow time for the processor to
5389163Ssam  * schedule another I/O transfer.
5394651Smckusic  */
5405212Smckusic daddr_t
54151469Sbostic ffs_blkpref(ip, lbn, indx, bap)
5429163Ssam 	struct inode *ip;
5439163Ssam 	daddr_t lbn;
5449163Ssam 	int indx;
5459163Ssam 	daddr_t *bap;
5469163Ssam {
5475965Smckusic 	register struct fs *fs;
54817696Smckusick 	register int cg;
54917696Smckusick 	int avgbfree, startcg;
5509163Ssam 	daddr_t nextblk;
5514651Smckusic 
5529163Ssam 	fs = ip->i_fs;
5539163Ssam 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
5549163Ssam 		if (lbn < NDADDR) {
55564603Sbostic 			cg = ino_to_cg(fs, ip->i_number);
5565322Smckusic 			return (fs->fs_fpg * cg + fs->fs_frag);
5574651Smckusic 		}
5589163Ssam 		/*
5599163Ssam 		 * Find a cylinder with greater than average number of
5609163Ssam 		 * unused data blocks.
5619163Ssam 		 */
56217696Smckusick 		if (indx == 0 || bap[indx - 1] == 0)
56364603Sbostic 			startcg =
56464603Sbostic 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
56517696Smckusick 		else
56617696Smckusick 			startcg = dtog(fs, bap[indx - 1]) + 1;
56717696Smckusick 		startcg %= fs->fs_ncg;
5689163Ssam 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
56917696Smckusick 		for (cg = startcg; cg < fs->fs_ncg; cg++)
5709163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
5719163Ssam 				fs->fs_cgrotor = cg;
5729163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
5739163Ssam 			}
57417696Smckusick 		for (cg = 0; cg <= startcg; cg++)
5759163Ssam 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
5769163Ssam 				fs->fs_cgrotor = cg;
5779163Ssam 				return (fs->fs_fpg * cg + fs->fs_frag);
5789163Ssam 			}
5799163Ssam 		return (NULL);
5809163Ssam 	}
5819163Ssam 	/*
5829163Ssam 	 * One or more previous blocks have been laid out. If less
5839163Ssam 	 * than fs_maxcontig previous blocks are contiguous, the
5849163Ssam 	 * next block is requested contiguously, otherwise it is
5859163Ssam 	 * requested rotationally delayed by fs_rotdelay milliseconds.
5869163Ssam 	 */
5879163Ssam 	nextblk = bap[indx - 1] + fs->fs_frag;
58856665Smargo 	if (indx < fs->fs_maxcontig || bap[indx - fs->fs_maxcontig] +
58956665Smargo 	    blkstofrags(fs, fs->fs_maxcontig) != nextblk)
5909163Ssam 		return (nextblk);
5919163Ssam 	if (fs->fs_rotdelay != 0)
5929163Ssam 		/*
5939163Ssam 		 * Here we convert ms of delay to frags as:
5949163Ssam 		 * (frags) = (ms) * (rev/sec) * (sect/rev) /
5959163Ssam 		 *	((sect/frag) * (ms/sec))
5969163Ssam 		 * then round up to the next block.
5979163Ssam 		 */
5989163Ssam 		nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
5999163Ssam 		    (NSPF(fs) * 1000), fs->fs_frag);
6009163Ssam 	return (nextblk);
6014651Smckusic }
6024651Smckusic 
6035375Smckusic /*
6045375Smckusic  * Implement the cylinder overflow algorithm.
6055375Smckusic  *
6065375Smckusic  * The policy implemented by this algorithm is:
6075375Smckusic  *   1) allocate the block in its requested cylinder group.
6085375Smckusic  *   2) quadradically rehash on the cylinder group number.
6095375Smckusic  *   3) brute force search for a free block.
6105375Smckusic  */
6115212Smckusic /*VARARGS5*/
61251469Sbostic static u_long
61351469Sbostic ffs_hashalloc(ip, cg, pref, size, allocator)
6145965Smckusic 	struct inode *ip;
6154359Smckusick 	int cg;
6164359Smckusick 	long pref;
6174359Smckusick 	int size;	/* size for data blocks, mode for inodes */
6185212Smckusic 	u_long (*allocator)();
6194359Smckusick {
6205965Smckusic 	register struct fs *fs;
6214359Smckusick 	long result;
6224359Smckusick 	int i, icg = cg;
6234359Smckusick 
6245965Smckusic 	fs = ip->i_fs;
6254359Smckusick 	/*
6264359Smckusick 	 * 1: preferred cylinder group
6274359Smckusick 	 */
6285965Smckusic 	result = (*allocator)(ip, cg, pref, size);
6294359Smckusick 	if (result)
6304359Smckusick 		return (result);
6314359Smckusick 	/*
6324359Smckusick 	 * 2: quadratic rehash
6334359Smckusick 	 */
6344359Smckusick 	for (i = 1; i < fs->fs_ncg; i *= 2) {
6354359Smckusick 		cg += i;
6364359Smckusick 		if (cg >= fs->fs_ncg)
6374359Smckusick 			cg -= fs->fs_ncg;
6385965Smckusic 		result = (*allocator)(ip, cg, 0, size);
6394359Smckusick 		if (result)
6404359Smckusick 			return (result);
6414359Smckusick 	}
6424359Smckusick 	/*
6434359Smckusick 	 * 3: brute force search
64410847Ssam 	 * Note that we start at i == 2, since 0 was checked initially,
64510847Ssam 	 * and 1 is always checked in the quadratic rehash.
6464359Smckusick 	 */
64710848Smckusick 	cg = (icg + 2) % fs->fs_ncg;
64810847Ssam 	for (i = 2; i < fs->fs_ncg; i++) {
6495965Smckusic 		result = (*allocator)(ip, cg, 0, size);
6504359Smckusick 		if (result)
6514359Smckusick 			return (result);
6524359Smckusick 		cg++;
6534359Smckusick 		if (cg == fs->fs_ncg)
6544359Smckusick 			cg = 0;
6554359Smckusick 	}
6566294Smckusick 	return (NULL);
6574359Smckusick }
6584359Smckusick 
6595375Smckusic /*
6605375Smckusic  * Determine whether a fragment can be extended.
6615375Smckusic  *
6625375Smckusic  * Check to see if the necessary fragments are available, and
6635375Smckusic  * if they are, allocate them.
6645375Smckusic  */
66551469Sbostic static daddr_t
66651469Sbostic ffs_fragextend(ip, cg, bprev, osize, nsize)
6675965Smckusic 	struct inode *ip;
6684426Smckusic 	int cg;
6694463Smckusic 	long bprev;
6704426Smckusic 	int osize, nsize;
6714426Smckusic {
6725965Smckusic 	register struct fs *fs;
6734463Smckusic 	register struct cg *cgp;
67437735Smckusick 	struct buf *bp;
6754463Smckusic 	long bno;
6764463Smckusic 	int frags, bbase;
67737735Smckusick 	int i, error;
6784426Smckusic 
6795965Smckusic 	fs = ip->i_fs;
68017224Smckusick 	if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
6816531Smckusick 		return (NULL);
6825960Smckusic 	frags = numfrags(fs, nsize);
68317224Smckusick 	bbase = fragnum(fs, bprev);
68417224Smckusick 	if (bbase > fragnum(fs, (bprev + frags - 1))) {
68530749Skarels 		/* cannot extend across a block boundary */
6866294Smckusick 		return (NULL);
6874463Smckusic 	}
68837735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
68938776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
69037735Smckusick 	if (error) {
69137735Smckusick 		brelse(bp);
69237735Smckusick 		return (NULL);
69337735Smckusick 	}
69464508Sbostic 	cgp = (struct cg *)bp->b_data;
69537735Smckusick 	if (!cg_chkmagic(cgp)) {
6965960Smckusic 		brelse(bp);
6976294Smckusick 		return (NULL);
6985960Smckusic 	}
6998105Sroot 	cgp->cg_time = time.tv_sec;
7005377Smckusic 	bno = dtogd(fs, bprev);
7015960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++)
70234143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i)) {
7035361Smckusic 			brelse(bp);
7046294Smckusick 			return (NULL);
7055361Smckusic 		}
7065361Smckusic 	/*
7075361Smckusic 	 * the current fragment can be extended
7085361Smckusic 	 * deduct the count on fragment being extended into
7095361Smckusic 	 * increase the count on the remaining fragment (if any)
7105361Smckusic 	 * allocate the extended piece
7115361Smckusic 	 */
7125361Smckusic 	for (i = frags; i < fs->fs_frag - bbase; i++)
71334143Smckusick 		if (isclr(cg_blksfree(cgp), bno + i))
7144463Smckusic 			break;
7155960Smckusic 	cgp->cg_frsum[i - numfrags(fs, osize)]--;
7165361Smckusic 	if (i != frags)
7175361Smckusic 		cgp->cg_frsum[i - frags]++;
7185960Smckusic 	for (i = numfrags(fs, osize); i < frags; i++) {
71934143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
7205361Smckusic 		cgp->cg_cs.cs_nffree--;
7215361Smckusic 		fs->fs_cstotal.cs_nffree--;
7225361Smckusic 		fs->fs_cs(fs, cg).cs_nffree--;
7234463Smckusic 	}
72450893Smckusick 	fs->fs_fmod = 1;
7255361Smckusic 	bdwrite(bp);
7265361Smckusic 	return (bprev);
7274426Smckusic }
7284426Smckusic 
7295375Smckusic /*
7305375Smckusic  * Determine whether a block can be allocated.
7315375Smckusic  *
73264603Sbostic  * Check to see if a block of the appropriate size is available,
7335375Smckusic  * and if it is, allocate it.
7345375Smckusic  */
73551779Smarc static daddr_t
73651469Sbostic ffs_alloccg(ip, cg, bpref, size)
7375965Smckusic 	struct inode *ip;
7384359Smckusick 	int cg;
7394359Smckusick 	daddr_t bpref;
7404359Smckusick 	int size;
7414359Smckusick {
7425965Smckusic 	register struct fs *fs;
7434463Smckusic 	register struct cg *cgp;
74437735Smckusick 	struct buf *bp;
7454463Smckusic 	register int i;
74637735Smckusick 	int error, bno, frags, allocsiz;
7474359Smckusick 
7485965Smckusic 	fs = ip->i_fs;
7495322Smckusic 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
7506294Smckusick 		return (NULL);
75137735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
75238776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
75337735Smckusick 	if (error) {
75437735Smckusick 		brelse(bp);
75537735Smckusick 		return (NULL);
75637735Smckusick 	}
75764508Sbostic 	cgp = (struct cg *)bp->b_data;
75837735Smckusick 	if (!cg_chkmagic(cgp) ||
75915950Smckusick 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
7605960Smckusic 		brelse(bp);
7616294Smckusick 		return (NULL);
7625960Smckusic 	}
7638105Sroot 	cgp->cg_time = time.tv_sec;
7645322Smckusic 	if (size == fs->fs_bsize) {
76551469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
7664463Smckusic 		bdwrite(bp);
7674463Smckusic 		return (bno);
7684463Smckusic 	}
7694463Smckusic 	/*
7704463Smckusic 	 * check to see if any fragments are already available
7714463Smckusic 	 * allocsiz is the size which will be allocated, hacking
7724463Smckusic 	 * it down to a smaller size if necessary
7734463Smckusic 	 */
7745960Smckusic 	frags = numfrags(fs, size);
7755322Smckusic 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
7764463Smckusic 		if (cgp->cg_frsum[allocsiz] != 0)
7774463Smckusic 			break;
7785322Smckusic 	if (allocsiz == fs->fs_frag) {
7794463Smckusic 		/*
7804463Smckusic 		 * no fragments were available, so a block will be
7814463Smckusic 		 * allocated, and hacked up
7824463Smckusic 		 */
7834792Smckusic 		if (cgp->cg_cs.cs_nbfree == 0) {
7844463Smckusic 			brelse(bp);
7856294Smckusick 			return (NULL);
7864463Smckusic 		}
78751469Sbostic 		bno = ffs_alloccgblk(fs, cgp, bpref);
7885377Smckusic 		bpref = dtogd(fs, bno);
7895322Smckusic 		for (i = frags; i < fs->fs_frag; i++)
79034143Smckusick 			setbit(cg_blksfree(cgp), bpref + i);
7915322Smckusic 		i = fs->fs_frag - frags;
7924792Smckusic 		cgp->cg_cs.cs_nffree += i;
7934792Smckusic 		fs->fs_cstotal.cs_nffree += i;
7945322Smckusic 		fs->fs_cs(fs, cg).cs_nffree += i;
79550893Smckusick 		fs->fs_fmod = 1;
7964463Smckusic 		cgp->cg_frsum[i]++;
7974463Smckusic 		bdwrite(bp);
7984463Smckusic 		return (bno);
7994463Smckusic 	}
80051469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
80115950Smckusick 	if (bno < 0) {
80215950Smckusick 		brelse(bp);
8036294Smckusick 		return (NULL);
80415950Smckusick 	}
8054463Smckusic 	for (i = 0; i < frags; i++)
80634143Smckusick 		clrbit(cg_blksfree(cgp), bno + i);
8074792Smckusic 	cgp->cg_cs.cs_nffree -= frags;
8084792Smckusic 	fs->fs_cstotal.cs_nffree -= frags;
8095322Smckusic 	fs->fs_cs(fs, cg).cs_nffree -= frags;
81050893Smckusick 	fs->fs_fmod = 1;
8114463Smckusic 	cgp->cg_frsum[allocsiz]--;
8124463Smckusic 	if (frags != allocsiz)
8134463Smckusic 		cgp->cg_frsum[allocsiz - frags]++;
8144463Smckusic 	bdwrite(bp);
8154463Smckusic 	return (cg * fs->fs_fpg + bno);
8164463Smckusic }
8174463Smckusic 
8185375Smckusic /*
8195375Smckusic  * Allocate a block in a cylinder group.
8205375Smckusic  *
8215375Smckusic  * This algorithm implements the following policy:
8225375Smckusic  *   1) allocate the requested block.
8235375Smckusic  *   2) allocate a rotationally optimal block in the same cylinder.
8245375Smckusic  *   3) allocate the next available block on the block rotor for the
8255375Smckusic  *      specified cylinder group.
8265375Smckusic  * Note that this routine only allocates fs_bsize blocks; these
8275375Smckusic  * blocks may be fragmented by the routine that allocates them.
8285375Smckusic  */
82951469Sbostic static daddr_t
83051469Sbostic ffs_alloccgblk(fs, cgp, bpref)
8315965Smckusic 	register struct fs *fs;
8324463Smckusic 	register struct cg *cgp;
8334463Smckusic 	daddr_t bpref;
8344463Smckusic {
835*65999Smckusick 	daddr_t bno, blkno;
8366294Smckusick 	int cylno, pos, delta;
8374651Smckusic 	short *cylbp;
8385361Smckusic 	register int i;
8394463Smckusic 
840*65999Smckusick 	if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
8414651Smckusic 		bpref = cgp->cg_rotor;
8425361Smckusic 		goto norot;
8435361Smckusic 	}
84417224Smckusick 	bpref = blknum(fs, bpref);
8455377Smckusic 	bpref = dtogd(fs, bpref);
8465361Smckusic 	/*
8475361Smckusic 	 * if the requested block is available, use it
8485361Smckusic 	 */
84951469Sbostic 	if (ffs_isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bpref))) {
8505361Smckusic 		bno = bpref;
8515361Smckusic 		goto gotit;
8525361Smckusic 	}
8535361Smckusic 	/*
8545361Smckusic 	 * check for a block available on the same cylinder
8555361Smckusic 	 */
8565361Smckusic 	cylno = cbtocylno(fs, bpref);
85734143Smckusick 	if (cg_blktot(cgp)[cylno] == 0)
8585375Smckusic 		goto norot;
8595375Smckusic 	if (fs->fs_cpc == 0) {
8605375Smckusic 		/*
86157000Smckusick 		 * Block layout information is not available.
86257000Smckusick 		 * Leaving bpref unchanged means we take the
86357000Smckusick 		 * next available free block following the one
86457000Smckusick 		 * we just allocated. Hopefully this will at
86557000Smckusick 		 * least hit a track cache on drives of unknown
86657000Smckusick 		 * geometry (e.g. SCSI).
8675375Smckusic 		 */
8685375Smckusic 		goto norot;
8695375Smckusic 	}
8705375Smckusic 	/*
8715361Smckusic 	 * check the summary information to see if a block is
8725361Smckusic 	 * available in the requested cylinder starting at the
8739163Ssam 	 * requested rotational position and proceeding around.
8745361Smckusic 	 */
87534143Smckusick 	cylbp = cg_blks(fs, cgp, cylno);
8769163Ssam 	pos = cbtorpos(fs, bpref);
87734143Smckusick 	for (i = pos; i < fs->fs_nrpos; i++)
8785361Smckusic 		if (cylbp[i] > 0)
8795361Smckusic 			break;
88034143Smckusick 	if (i == fs->fs_nrpos)
8815361Smckusic 		for (i = 0; i < pos; i++)
8825361Smckusic 			if (cylbp[i] > 0)
8835361Smckusic 				break;
8845361Smckusic 	if (cylbp[i] > 0) {
8854651Smckusic 		/*
8865361Smckusic 		 * found a rotational position, now find the actual
8875361Smckusic 		 * block. A panic if none is actually there.
8884651Smckusic 		 */
8895361Smckusic 		pos = cylno % fs->fs_cpc;
8905361Smckusic 		bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
89134143Smckusick 		if (fs_postbl(fs, pos)[i] == -1) {
8926716Smckusick 			printf("pos = %d, i = %d, fs = %s\n",
8936716Smckusick 			    pos, i, fs->fs_fsmnt);
89451469Sbostic 			panic("ffs_alloccgblk: cyl groups corrupted");
8956716Smckusick 		}
89634143Smckusick 		for (i = fs_postbl(fs, pos)[i];; ) {
89751469Sbostic 			if (ffs_isblock(fs, cg_blksfree(cgp), bno + i)) {
89811638Ssam 				bno = blkstofrags(fs, (bno + i));
8995361Smckusic 				goto gotit;
9005361Smckusic 			}
90134143Smckusick 			delta = fs_rotbl(fs)[i];
90234143Smckusick 			if (delta <= 0 ||
90334143Smckusick 			    delta + i > fragstoblks(fs, fs->fs_fpg))
9044651Smckusic 				break;
9056294Smckusick 			i += delta;
9064651Smckusic 		}
9076716Smckusick 		printf("pos = %d, i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
90851469Sbostic 		panic("ffs_alloccgblk: can't find blk in cyl");
9094359Smckusick 	}
9105361Smckusic norot:
9115361Smckusic 	/*
9125361Smckusic 	 * no blocks in the requested cylinder, so take next
9135361Smckusic 	 * available one in this cylinder group.
9145361Smckusic 	 */
91551469Sbostic 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
9166567Smckusic 	if (bno < 0)
9176294Smckusick 		return (NULL);
9184651Smckusic 	cgp->cg_rotor = bno;
9194359Smckusick gotit:
920*65999Smckusick 	blkno = fragstoblks(fs, bno);
921*65999Smckusick 	ffs_clrblock(fs, cg_blksfree(cgp), (long)blkno);
922*65999Smckusick 	ffs_clusteracct(fs, cgp, blkno, -1);
9234792Smckusic 	cgp->cg_cs.cs_nbfree--;
9244792Smckusic 	fs->fs_cstotal.cs_nbfree--;
9255322Smckusic 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
9265375Smckusic 	cylno = cbtocylno(fs, bno);
92734143Smckusick 	cg_blks(fs, cgp, cylno)[cbtorpos(fs, bno)]--;
92834143Smckusick 	cg_blktot(cgp)[cylno]--;
92950893Smckusick 	fs->fs_fmod = 1;
9304651Smckusic 	return (cgp->cg_cgx * fs->fs_fpg + bno);
9314359Smckusick }
93234143Smckusick 
9335375Smckusic /*
934*65999Smckusick  * Determine whether a cluster can be allocated.
935*65999Smckusick  *
936*65999Smckusick  * We do not currently check for optimal rotational layout if there
937*65999Smckusick  * are multiple choices in the same cylinder group. Instead we just
938*65999Smckusick  * take the first one that we find following bpref.
939*65999Smckusick  */
940*65999Smckusick static daddr_t
941*65999Smckusick ffs_clusteralloc(ip, cg, bpref, len)
942*65999Smckusick 	struct inode *ip;
943*65999Smckusick 	int cg;
944*65999Smckusick 	daddr_t bpref;
945*65999Smckusick 	int len;
946*65999Smckusick {
947*65999Smckusick 	register struct fs *fs;
948*65999Smckusick 	register struct cg *cgp;
949*65999Smckusick 	struct buf *bp;
950*65999Smckusick 	int i, run, bno, bit, map;
951*65999Smckusick 	u_char *mapp;
952*65999Smckusick 
953*65999Smckusick 	fs = ip->i_fs;
954*65999Smckusick 	if (fs->fs_cs(fs, cg).cs_nbfree < len)
955*65999Smckusick 		return (NULL);
956*65999Smckusick 	if (bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
957*65999Smckusick 	    NOCRED, &bp))
958*65999Smckusick 		goto fail;
959*65999Smckusick 	cgp = (struct cg *)bp->b_data;
960*65999Smckusick 	if (!cg_chkmagic(cgp))
961*65999Smckusick 		goto fail;
962*65999Smckusick 	/*
963*65999Smckusick 	 * Check to see if a cluster of the needed size (or bigger) is
964*65999Smckusick 	 * available in this cylinder group.
965*65999Smckusick 	 */
966*65999Smckusick 	for (i = len; i <= fs->fs_contigsumsize; i++)
967*65999Smckusick 		if (cg_clustersum(cgp)[i] > 0)
968*65999Smckusick 			break;
969*65999Smckusick 	if (i > fs->fs_contigsumsize)
970*65999Smckusick 		goto fail;
971*65999Smckusick 	/*
972*65999Smckusick 	 * Search the cluster map to find a big enough cluster.
973*65999Smckusick 	 * We take the first one that we find, even if it is larger
974*65999Smckusick 	 * than we need as we prefer to get one close to the previous
975*65999Smckusick 	 * block allocation. We do not search before the current
976*65999Smckusick 	 * preference point as we do not want to allocate a block
977*65999Smckusick 	 * that is allocated before the previous one (as we will
978*65999Smckusick 	 * then have to wait for another pass of the elevator
979*65999Smckusick 	 * algorithm before it will be read). We prefer to fail and
980*65999Smckusick 	 * be recalled to try an allocation in the next cylinder group.
981*65999Smckusick 	 */
982*65999Smckusick 	if (dtog(fs, bpref) != cg)
983*65999Smckusick 		bpref = 0;
984*65999Smckusick 	else
985*65999Smckusick 		bpref = fragstoblks(fs, dtogd(fs, blknum(fs, bpref)));
986*65999Smckusick 	mapp = &cg_clustersfree(cgp)[bpref / NBBY];
987*65999Smckusick 	map = *mapp++;
988*65999Smckusick 	bit = 1 << (bpref % NBBY);
989*65999Smckusick 	for (run = 0, i = bpref; i < cgp->cg_nclusterblks; i++) {
990*65999Smckusick 		if ((map & bit) == 0) {
991*65999Smckusick 			run = 0;
992*65999Smckusick 		} else {
993*65999Smckusick 			run++;
994*65999Smckusick 			if (run == len)
995*65999Smckusick 				break;
996*65999Smckusick 		}
997*65999Smckusick 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
998*65999Smckusick 			bit <<= 1;
999*65999Smckusick 		} else {
1000*65999Smckusick 			map = *mapp++;
1001*65999Smckusick 			bit = 1;
1002*65999Smckusick 		}
1003*65999Smckusick 	}
1004*65999Smckusick 	if (i == cgp->cg_nclusterblks)
1005*65999Smckusick 		goto fail;
1006*65999Smckusick 	/*
1007*65999Smckusick 	 * Allocate the cluster that we have found.
1008*65999Smckusick 	 */
1009*65999Smckusick 	bno = cg * fs->fs_fpg + blkstofrags(fs, i - run + 1);
1010*65999Smckusick 	len = blkstofrags(fs, len);
1011*65999Smckusick 	for (i = 0; i < len; i += fs->fs_frag)
1012*65999Smckusick 		if (ffs_alloccgblk(fs, cgp, bno + i) != bno + i)
1013*65999Smckusick 			panic("ffs_clusteralloc: lost block");
1014*65999Smckusick 	brelse(bp);
1015*65999Smckusick 	return (bno);
1016*65999Smckusick 
1017*65999Smckusick fail:
1018*65999Smckusick 	brelse(bp);
1019*65999Smckusick 	return (0);
1020*65999Smckusick }
1021*65999Smckusick 
1022*65999Smckusick /*
10235375Smckusic  * Determine whether an inode can be allocated.
10245375Smckusic  *
10255375Smckusic  * Check to see if an inode is available, and if it is,
10265375Smckusic  * allocate it using the following policy:
10275375Smckusic  *   1) allocate the requested inode.
10285375Smckusic  *   2) allocate the next available inode after the requested
10295375Smckusic  *      inode in the specified cylinder group.
10305375Smckusic  */
103151469Sbostic static ino_t
103264603Sbostic ffs_nodealloccg(ip, cg, ipref, mode)
10335965Smckusic 	struct inode *ip;
10344359Smckusick 	int cg;
10354359Smckusick 	daddr_t ipref;
10364359Smckusick 	int mode;
10374359Smckusick {
10385965Smckusic 	register struct fs *fs;
10394463Smckusic 	register struct cg *cgp;
104016784Smckusick 	struct buf *bp;
104137735Smckusick 	int error, start, len, loc, map, i;
10424359Smckusick 
10435965Smckusic 	fs = ip->i_fs;
10445322Smckusic 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
10456294Smckusick 		return (NULL);
104637735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
104738776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
104837735Smckusick 	if (error) {
104937735Smckusick 		brelse(bp);
105037735Smckusick 		return (NULL);
105137735Smckusick 	}
105264508Sbostic 	cgp = (struct cg *)bp->b_data;
105337735Smckusick 	if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
10545960Smckusic 		brelse(bp);
10556294Smckusick 		return (NULL);
10565960Smckusic 	}
10578105Sroot 	cgp->cg_time = time.tv_sec;
10584359Smckusick 	if (ipref) {
10594359Smckusick 		ipref %= fs->fs_ipg;
106034143Smckusick 		if (isclr(cg_inosused(cgp), ipref))
10614359Smckusick 			goto gotit;
106216784Smckusick 	}
106316784Smckusick 	start = cgp->cg_irotor / NBBY;
106416784Smckusick 	len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
106534143Smckusick 	loc = skpc(0xff, len, &cg_inosused(cgp)[start]);
106616784Smckusick 	if (loc == 0) {
106717697Smckusick 		len = start + 1;
106817697Smckusick 		start = 0;
106934143Smckusick 		loc = skpc(0xff, len, &cg_inosused(cgp)[0]);
107017697Smckusick 		if (loc == 0) {
107165718Sbostic 			printf("cg = %d, irotor = %d, fs = %s\n",
107217697Smckusick 			    cg, cgp->cg_irotor, fs->fs_fsmnt);
107364603Sbostic 			panic("ffs_nodealloccg: map corrupted");
107417697Smckusick 			/* NOTREACHED */
107517697Smckusick 		}
107616784Smckusick 	}
107716784Smckusick 	i = start + len - loc;
107834143Smckusick 	map = cg_inosused(cgp)[i];
107916784Smckusick 	ipref = i * NBBY;
108016784Smckusick 	for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
108116784Smckusick 		if ((map & i) == 0) {
10824359Smckusick 			cgp->cg_irotor = ipref;
10834359Smckusick 			goto gotit;
10844359Smckusick 		}
10854359Smckusick 	}
108616784Smckusick 	printf("fs = %s\n", fs->fs_fsmnt);
108764603Sbostic 	panic("ffs_nodealloccg: block not in map");
108816784Smckusick 	/* NOTREACHED */
10894359Smckusick gotit:
109034143Smckusick 	setbit(cg_inosused(cgp), ipref);
10914792Smckusic 	cgp->cg_cs.cs_nifree--;
10924792Smckusic 	fs->fs_cstotal.cs_nifree--;
10935322Smckusic 	fs->fs_cs(fs, cg).cs_nifree--;
109450893Smckusick 	fs->fs_fmod = 1;
10954359Smckusick 	if ((mode & IFMT) == IFDIR) {
10964792Smckusic 		cgp->cg_cs.cs_ndir++;
10974792Smckusic 		fs->fs_cstotal.cs_ndir++;
10985322Smckusic 		fs->fs_cs(fs, cg).cs_ndir++;
10994359Smckusick 	}
11004359Smckusick 	bdwrite(bp);
11014359Smckusick 	return (cg * fs->fs_ipg + ipref);
11024359Smckusick }
11034359Smckusick 
11045375Smckusic /*
11055375Smckusic  * Free a block or fragment.
11065375Smckusic  *
11075375Smckusic  * The specified block or fragment is placed back in the
11085375Smckusic  * free map. If a fragment is deallocated, a possible
11095375Smckusic  * block reassembly is checked.
11105375Smckusic  */
111151469Sbostic ffs_blkfree(ip, bno, size)
11125965Smckusic 	register struct inode *ip;
11134359Smckusick 	daddr_t bno;
111453059Smckusick 	long size;
11154359Smckusick {
11164359Smckusick 	register struct fs *fs;
11174359Smckusick 	register struct cg *cgp;
111837735Smckusick 	struct buf *bp;
1119*65999Smckusick 	daddr_t blkno;
1120*65999Smckusick 	int i, error, cg, blk, frags, bbase;
11214359Smckusick 
11225965Smckusic 	fs = ip->i_fs;
112364508Sbostic 	if ((u_int)size > fs->fs_bsize || fragoff(fs, size) != 0) {
11246716Smckusick 		printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
11256716Smckusick 		    ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
112631402Smckusick 		panic("blkfree: bad size");
11276716Smckusick 	}
11285377Smckusic 	cg = dtog(fs, bno);
112964508Sbostic 	if ((u_int)bno >= fs->fs_size) {
11306567Smckusic 		printf("bad block %d, ino %d\n", bno, ip->i_number);
113153244Smckusick 		ffs_fserr(fs, ip->i_uid, "bad block");
11324359Smckusick 		return;
11336567Smckusic 	}
113437735Smckusick 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
113538776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
113637735Smckusick 	if (error) {
113737735Smckusick 		brelse(bp);
113837735Smckusick 		return;
113937735Smckusick 	}
114064508Sbostic 	cgp = (struct cg *)bp->b_data;
114137735Smckusick 	if (!cg_chkmagic(cgp)) {
11425960Smckusic 		brelse(bp);
11434359Smckusick 		return;
11445960Smckusic 	}
11458105Sroot 	cgp->cg_time = time.tv_sec;
11465377Smckusic 	bno = dtogd(fs, bno);
11475322Smckusic 	if (size == fs->fs_bsize) {
1148*65999Smckusick 		blkno = fragstoblks(fs, bno);
1149*65999Smckusick 		if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
11506716Smckusick 			printf("dev = 0x%x, block = %d, fs = %s\n",
11516716Smckusick 			    ip->i_dev, bno, fs->fs_fsmnt);
115231402Smckusick 			panic("blkfree: freeing free block");
11536567Smckusic 		}
1154*65999Smckusick 		ffs_setblock(fs, cg_blksfree(cgp), blkno);
1155*65999Smckusick 		ffs_clusteracct(fs, cgp, blkno, 1);
11564792Smckusic 		cgp->cg_cs.cs_nbfree++;
11574792Smckusic 		fs->fs_cstotal.cs_nbfree++;
11585322Smckusic 		fs->fs_cs(fs, cg).cs_nbfree++;
11595375Smckusic 		i = cbtocylno(fs, bno);
116034143Smckusick 		cg_blks(fs, cgp, i)[cbtorpos(fs, bno)]++;
116134143Smckusick 		cg_blktot(cgp)[i]++;
11624426Smckusic 	} else {
116317224Smckusick 		bbase = bno - fragnum(fs, bno);
11644463Smckusic 		/*
11654463Smckusic 		 * decrement the counts associated with the old frags
11664463Smckusic 		 */
116734143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
116851469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
11694463Smckusic 		/*
11704463Smckusic 		 * deallocate the fragment
11714463Smckusic 		 */
11725960Smckusic 		frags = numfrags(fs, size);
11734463Smckusic 		for (i = 0; i < frags; i++) {
117434143Smckusick 			if (isset(cg_blksfree(cgp), bno + i)) {
11756716Smckusick 				printf("dev = 0x%x, block = %d, fs = %s\n",
11766716Smckusick 				    ip->i_dev, bno + i, fs->fs_fsmnt);
117731402Smckusick 				panic("blkfree: freeing free frag");
11786716Smckusick 			}
117934143Smckusick 			setbit(cg_blksfree(cgp), bno + i);
11804426Smckusic 		}
11816294Smckusick 		cgp->cg_cs.cs_nffree += i;
11826294Smckusick 		fs->fs_cstotal.cs_nffree += i;
11836294Smckusick 		fs->fs_cs(fs, cg).cs_nffree += i;
11844463Smckusic 		/*
11854463Smckusic 		 * add back in counts associated with the new frags
11864463Smckusic 		 */
118734143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bbase);
118851469Sbostic 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
11894463Smckusic 		/*
11904463Smckusic 		 * if a complete block has been reassembled, account for it
11914463Smckusic 		 */
1192*65999Smckusick 		blkno = fragstoblks(fs, bbase);
1193*65999Smckusick 		if (ffs_isblock(fs, cg_blksfree(cgp), blkno)) {
11945322Smckusic 			cgp->cg_cs.cs_nffree -= fs->fs_frag;
11955322Smckusic 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
11965322Smckusic 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
1197*65999Smckusick 			ffs_clusteracct(fs, cgp, blkno, 1);
11984792Smckusic 			cgp->cg_cs.cs_nbfree++;
11994792Smckusic 			fs->fs_cstotal.cs_nbfree++;
12005322Smckusic 			fs->fs_cs(fs, cg).cs_nbfree++;
12015375Smckusic 			i = cbtocylno(fs, bbase);
120234143Smckusick 			cg_blks(fs, cgp, i)[cbtorpos(fs, bbase)]++;
120334143Smckusick 			cg_blktot(cgp)[i]++;
12044426Smckusic 		}
12054426Smckusic 	}
120650893Smckusick 	fs->fs_fmod = 1;
12074359Smckusick 	bdwrite(bp);
12084359Smckusick }
12094359Smckusick 
12105375Smckusic /*
12115375Smckusic  * Free an inode.
12125375Smckusic  *
12135375Smckusic  * The specified inode is placed back in the free map.
12145375Smckusic  */
121553577Sheideman int
121654653Smckusick ffs_vfree(ap)
121754653Smckusick 	struct vop_vfree_args /* {
121854653Smckusick 		struct vnode *a_pvp;
121954653Smckusick 		ino_t a_ino;
122054653Smckusick 		int a_mode;
122154653Smckusick 	} */ *ap;
12224359Smckusick {
12234359Smckusick 	register struct fs *fs;
12244359Smckusick 	register struct cg *cgp;
122551541Smckusick 	register struct inode *pip;
122654653Smckusick 	ino_t ino = ap->a_ino;
122737735Smckusick 	struct buf *bp;
122837735Smckusick 	int error, cg;
12294359Smckusick 
123053583Sheideman 	pip = VTOI(ap->a_pvp);
123151469Sbostic 	fs = pip->i_fs;
123254653Smckusick 	if ((u_int)ino >= fs->fs_ipg * fs->fs_ncg)
123354653Smckusick 		panic("ifree: range: dev = 0x%x, ino = %d, fs = %s\n",
123454653Smckusick 		    pip->i_dev, ino, fs->fs_fsmnt);
123564603Sbostic 	cg = ino_to_cg(fs, ino);
123651469Sbostic 	error = bread(pip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
123738776Smckusick 		(int)fs->fs_cgsize, NOCRED, &bp);
123837735Smckusick 	if (error) {
123937735Smckusick 		brelse(bp);
124053577Sheideman 		return (0);
124137735Smckusick 	}
124264508Sbostic 	cgp = (struct cg *)bp->b_data;
124337735Smckusick 	if (!cg_chkmagic(cgp)) {
12445960Smckusic 		brelse(bp);
124553577Sheideman 		return (0);
12465960Smckusic 	}
12478105Sroot 	cgp->cg_time = time.tv_sec;
124854653Smckusick 	ino %= fs->fs_ipg;
124954653Smckusick 	if (isclr(cg_inosused(cgp), ino)) {
125054653Smckusick 		printf("dev = 0x%x, ino = %d, fs = %s\n",
125154653Smckusick 		    pip->i_dev, ino, fs->fs_fsmnt);
125248057Smckusick 		if (fs->fs_ronly == 0)
125348057Smckusick 			panic("ifree: freeing free inode");
12546716Smckusick 	}
125554653Smckusick 	clrbit(cg_inosused(cgp), ino);
125654653Smckusick 	if (ino < cgp->cg_irotor)
125754653Smckusick 		cgp->cg_irotor = ino;
12584792Smckusic 	cgp->cg_cs.cs_nifree++;
12594792Smckusic 	fs->fs_cstotal.cs_nifree++;
12605322Smckusic 	fs->fs_cs(fs, cg).cs_nifree++;
126153583Sheideman 	if ((ap->a_mode & IFMT) == IFDIR) {
12624792Smckusic 		cgp->cg_cs.cs_ndir--;
12634792Smckusic 		fs->fs_cstotal.cs_ndir--;
12645322Smckusic 		fs->fs_cs(fs, cg).cs_ndir--;
12654359Smckusick 	}
126650893Smckusick 	fs->fs_fmod = 1;
12674359Smckusick 	bdwrite(bp);
126853577Sheideman 	return (0);
12694359Smckusick }
12704359Smckusick 
12714463Smckusic /*
12725375Smckusic  * Find a block of the specified size in the specified cylinder group.
12735375Smckusic  *
12744651Smckusic  * It is a panic if a request is made to find a block if none are
12754651Smckusic  * available.
12764651Smckusic  */
127751469Sbostic static daddr_t
127851469Sbostic ffs_mapsearch(fs, cgp, bpref, allocsiz)
12794651Smckusic 	register struct fs *fs;
12804651Smckusic 	register struct cg *cgp;
12814651Smckusic 	daddr_t bpref;
12824651Smckusic 	int allocsiz;
12834651Smckusic {
12844651Smckusic 	daddr_t bno;
12854651Smckusic 	int start, len, loc, i;
12864651Smckusic 	int blk, field, subfield, pos;
12874651Smckusic 
12884651Smckusic 	/*
12894651Smckusic 	 * find the fragment by searching through the free block
12904651Smckusic 	 * map for an appropriate bit pattern
12914651Smckusic 	 */
12924651Smckusic 	if (bpref)
12935377Smckusic 		start = dtogd(fs, bpref) / NBBY;
12944651Smckusic 	else
12954651Smckusic 		start = cgp->cg_frotor / NBBY;
12965398Smckusic 	len = howmany(fs->fs_fpg, NBBY) - start;
129764508Sbostic 	loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[start],
129834476Smckusick 		(u_char *)fragtbl[fs->fs_frag],
129934476Smckusick 		(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
13004651Smckusic 	if (loc == 0) {
13016531Smckusick 		len = start + 1;
13026531Smckusick 		start = 0;
130364508Sbostic 		loc = scanc((u_int)len, (u_char *)&cg_blksfree(cgp)[0],
130434476Smckusick 			(u_char *)fragtbl[fs->fs_frag],
130534476Smckusick 			(u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
130616784Smckusick 		if (loc == 0) {
130716784Smckusick 			printf("start = %d, len = %d, fs = %s\n",
130816784Smckusick 			    start, len, fs->fs_fsmnt);
130951469Sbostic 			panic("ffs_alloccg: map corrupted");
131017697Smckusick 			/* NOTREACHED */
131116784Smckusick 		}
13124651Smckusic 	}
13134651Smckusic 	bno = (start + len - loc) * NBBY;
13144651Smckusic 	cgp->cg_frotor = bno;
13154651Smckusic 	/*
13164651Smckusic 	 * found the byte in the map
13174651Smckusic 	 * sift through the bits to find the selected frag
13184651Smckusic 	 */
13196294Smckusick 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
132034143Smckusick 		blk = blkmap(fs, cg_blksfree(cgp), bno);
13214651Smckusic 		blk <<= 1;
13224651Smckusic 		field = around[allocsiz];
13234651Smckusic 		subfield = inside[allocsiz];
13245322Smckusic 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
13256294Smckusick 			if ((blk & field) == subfield)
13266294Smckusick 				return (bno + pos);
13274651Smckusic 			field <<= 1;
13284651Smckusic 			subfield <<= 1;
13294651Smckusic 		}
13304651Smckusic 	}
13316716Smckusick 	printf("bno = %d, fs = %s\n", bno, fs->fs_fsmnt);
133251469Sbostic 	panic("ffs_alloccg: block not in map");
13336531Smckusick 	return (-1);
13344651Smckusic }
13354651Smckusic 
13364651Smckusic /*
1337*65999Smckusick  * Update the cluster map because of an allocation or free.
1338*65999Smckusick  *
1339*65999Smckusick  * Cnt == 1 means free; cnt == -1 means allocating.
1340*65999Smckusick  */
1341*65999Smckusick ffs_clusteracct(fs, cgp, blkno, cnt)
1342*65999Smckusick 	struct fs *fs;
1343*65999Smckusick 	struct cg *cgp;
1344*65999Smckusick 	daddr_t blkno;
1345*65999Smckusick 	int cnt;
1346*65999Smckusick {
1347*65999Smckusick 	long *sump;
1348*65999Smckusick 	u_char *freemapp, *mapp;
1349*65999Smckusick 	int i, start, end, forw, back, map, bit;
1350*65999Smckusick 
1351*65999Smckusick 	if (fs->fs_contigsumsize <= 0)
1352*65999Smckusick 		return;
1353*65999Smckusick 	freemapp = cg_clustersfree(cgp);
1354*65999Smckusick 	sump = cg_clustersum(cgp);
1355*65999Smckusick 	/*
1356*65999Smckusick 	 * Allocate or clear the actual block.
1357*65999Smckusick 	 */
1358*65999Smckusick 	if (cnt > 0)
1359*65999Smckusick 		setbit(freemapp, blkno);
1360*65999Smckusick 	else
1361*65999Smckusick 		clrbit(freemapp, blkno);
1362*65999Smckusick 	/*
1363*65999Smckusick 	 * Find the size of the cluster going forward.
1364*65999Smckusick 	 */
1365*65999Smckusick 	start = blkno + 1;
1366*65999Smckusick 	end = start + fs->fs_contigsumsize;
1367*65999Smckusick 	if (end >= cgp->cg_nclusterblks)
1368*65999Smckusick 		end = cgp->cg_nclusterblks;
1369*65999Smckusick 	mapp = &freemapp[start / NBBY];
1370*65999Smckusick 	map = *mapp++;
1371*65999Smckusick 	bit = 1 << (start % NBBY);
1372*65999Smckusick 	for (i = start; i < end; i++) {
1373*65999Smckusick 		if ((map & bit) == 0)
1374*65999Smckusick 			break;
1375*65999Smckusick 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
1376*65999Smckusick 			bit <<= 1;
1377*65999Smckusick 		} else {
1378*65999Smckusick 			map = *mapp++;
1379*65999Smckusick 			bit = 1;
1380*65999Smckusick 		}
1381*65999Smckusick 	}
1382*65999Smckusick 	forw = i - start;
1383*65999Smckusick 	/*
1384*65999Smckusick 	 * Find the size of the cluster going backward.
1385*65999Smckusick 	 */
1386*65999Smckusick 	start = blkno - 1;
1387*65999Smckusick 	end = start - fs->fs_contigsumsize;
1388*65999Smckusick 	if (end < 0)
1389*65999Smckusick 		end = -1;
1390*65999Smckusick 	mapp = &freemapp[start / NBBY];
1391*65999Smckusick 	map = *mapp--;
1392*65999Smckusick 	bit = 1 << (start % NBBY);
1393*65999Smckusick 	for (i = start; i > end; i--) {
1394*65999Smckusick 		if ((map & bit) == 0)
1395*65999Smckusick 			break;
1396*65999Smckusick 		if ((i & (NBBY - 1)) != 0) {
1397*65999Smckusick 			bit >>= 1;
1398*65999Smckusick 		} else {
1399*65999Smckusick 			map = *mapp--;
1400*65999Smckusick 			bit = 1 << (NBBY - 1);
1401*65999Smckusick 		}
1402*65999Smckusick 	}
1403*65999Smckusick 	back = start - i;
1404*65999Smckusick 	/*
1405*65999Smckusick 	 * Account for old cluster and the possibly new forward and
1406*65999Smckusick 	 * back clusters.
1407*65999Smckusick 	 */
1408*65999Smckusick 	i = back + forw + 1;
1409*65999Smckusick 	if (i > fs->fs_contigsumsize)
1410*65999Smckusick 		i = fs->fs_contigsumsize;
1411*65999Smckusick 	sump[i] += cnt;
1412*65999Smckusick 	if (back > 0)
1413*65999Smckusick 		sump[back] -= cnt;
1414*65999Smckusick 	if (forw > 0)
1415*65999Smckusick 		sump[forw] -= cnt;
1416*65999Smckusick }
1417*65999Smckusick 
1418*65999Smckusick /*
14195375Smckusic  * Fserr prints the name of a file system with an error diagnostic.
14205375Smckusic  *
14215375Smckusic  * The form of the error message is:
14224359Smckusick  *	fs: error message
14234359Smckusick  */
142451469Sbostic static void
142551469Sbostic ffs_fserr(fs, uid, cp)
14264359Smckusick 	struct fs *fs;
142751469Sbostic 	u_int uid;
14284359Smckusick 	char *cp;
14294359Smckusick {
14304359Smckusick 
143142318Smckusick 	log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
14324359Smckusick }
1433