xref: /openbsd-src/usr.sbin/makefs/ffs/ffs_alloc.c (revision 8e544e80ad051c7ebb0293f80569ecb775fbd3bf)
1*8e544e80Sjsg /*	$OpenBSD: ffs_alloc.c,v 1.15 2022/01/11 05:34:33 jsg Exp $	*/
26163fc9cSnatano /*	$NetBSD: ffs_alloc.c,v 1.29 2016/06/24 19:24:11 christos Exp $	*/
36163fc9cSnatano /* From: NetBSD: ffs_alloc.c,v 1.50 2001/09/06 02:16:01 lukem Exp */
46163fc9cSnatano 
56163fc9cSnatano /*
66163fc9cSnatano  * Copyright (c) 2002 Networks Associates Technology, Inc.
76163fc9cSnatano  * All rights reserved.
86163fc9cSnatano  *
96163fc9cSnatano  * This software was developed for the FreeBSD Project by Marshall
106163fc9cSnatano  * Kirk McKusick and Network Associates Laboratories, the Security
116163fc9cSnatano  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
126163fc9cSnatano  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
136163fc9cSnatano  * research program
146163fc9cSnatano  *
156163fc9cSnatano  * Copyright (c) 1982, 1986, 1989, 1993
166163fc9cSnatano  *	The Regents of the University of California.  All rights reserved.
176163fc9cSnatano  *
186163fc9cSnatano  * Redistribution and use in source and binary forms, with or without
196163fc9cSnatano  * modification, are permitted provided that the following conditions
206163fc9cSnatano  * are met:
216163fc9cSnatano  * 1. Redistributions of source code must retain the above copyright
226163fc9cSnatano  *    notice, this list of conditions and the following disclaimer.
236163fc9cSnatano  * 2. Redistributions in binary form must reproduce the above copyright
246163fc9cSnatano  *    notice, this list of conditions and the following disclaimer in the
256163fc9cSnatano  *    documentation and/or other materials provided with the distribution.
266163fc9cSnatano  * 3. Neither the name of the University nor the names of its contributors
276163fc9cSnatano  *    may be used to endorse or promote products derived from this software
286163fc9cSnatano  *    without specific prior written permission.
296163fc9cSnatano  *
306163fc9cSnatano  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
316163fc9cSnatano  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
326163fc9cSnatano  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
336163fc9cSnatano  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
346163fc9cSnatano  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
356163fc9cSnatano  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
366163fc9cSnatano  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
376163fc9cSnatano  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
386163fc9cSnatano  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
396163fc9cSnatano  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
406163fc9cSnatano  * SUCH DAMAGE.
416163fc9cSnatano  *
426163fc9cSnatano  *	@(#)ffs_alloc.c	8.19 (Berkeley) 7/13/95
436163fc9cSnatano  */
446163fc9cSnatano 
454874b543Sderaadt #include <sys/param.h>	/* DEV_BSIZE setbit clrbit NBBY howmany */
464874b543Sderaadt #include <sys/types.h>
476163fc9cSnatano 
486c4d9c1cSnatano #include <ufs/ufs/dinode.h>
496c4d9c1cSnatano #include <ufs/ffs/fs.h>
506163fc9cSnatano 
516163fc9cSnatano #include "ffs/buf.h"
526163fc9cSnatano #include "ffs/ufs_inode.h"
536163fc9cSnatano #include "ffs/ffs_extern.h"
546163fc9cSnatano 
554874b543Sderaadt #include <errno.h>
566163fc9cSnatano 
576163fc9cSnatano static int scanc(u_int, const u_char *, const u_char *, int);
586163fc9cSnatano 
596163fc9cSnatano static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int);
604af638d7Stedu static daddr_t ffs_alloccgblk(struct inode *, struct mkfsbuf *, daddr_t);
616163fc9cSnatano static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int,
626163fc9cSnatano 		     daddr_t (*)(struct inode *, int, daddr_t, int));
636163fc9cSnatano static int32_t ffs_mapsearch(struct fs *, struct cg *, daddr_t, int);
646163fc9cSnatano 
656163fc9cSnatano /*
666163fc9cSnatano  * Allocate a block in the file system.
676163fc9cSnatano  *
686163fc9cSnatano  * The size of the requested block is given, which must be some
696163fc9cSnatano  * multiple of fs_fsize and <= fs_bsize.
706163fc9cSnatano  * A preference may be optionally specified. If a preference is given
716163fc9cSnatano  * the following hierarchy is used to allocate a block:
726163fc9cSnatano  *   1) allocate the requested block.
736163fc9cSnatano  *   2) allocate a rotationally optimal block in the same cylinder.
746163fc9cSnatano  *   3) allocate a block in the same cylinder group.
75*8e544e80Sjsg  *   4) quadratically rehash into other cylinder groups, until an
766163fc9cSnatano  *      available block is located.
776163fc9cSnatano  * If no block preference is given the following hierarchy is used
786163fc9cSnatano  * to allocate a block:
796163fc9cSnatano  *   1) allocate a block in the cylinder group that contains the
806163fc9cSnatano  *      inode for the file.
81*8e544e80Sjsg  *   2) quadratically rehash into other cylinder groups, until an
826163fc9cSnatano  *      available block is located.
836163fc9cSnatano  */
846163fc9cSnatano int
ffs_alloc(struct inode * ip,daddr_t lbn __unused,daddr_t bpref,int size,daddr_t * bnp)856163fc9cSnatano ffs_alloc(struct inode *ip, daddr_t lbn __unused, daddr_t bpref, int size,
866163fc9cSnatano     daddr_t *bnp)
876163fc9cSnatano {
886163fc9cSnatano 	struct fs *fs = ip->i_fs;
896163fc9cSnatano 	daddr_t bno;
906163fc9cSnatano 	int cg;
916163fc9cSnatano 
926163fc9cSnatano 	*bnp = 0;
936c4d9c1cSnatano 	if (size > fs->fs_bsize || fragoff(fs, size) != 0) {
94944eabd5Snatano 		errx(1, "%s: bad size: bsize %d size %d", __func__,
956163fc9cSnatano 		    fs->fs_bsize, size);
966163fc9cSnatano 	}
976163fc9cSnatano 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
986163fc9cSnatano 		goto nospace;
996163fc9cSnatano 	if (bpref >= fs->fs_size)
1006163fc9cSnatano 		bpref = 0;
1016163fc9cSnatano 	if (bpref == 0)
1026163fc9cSnatano 		cg = ino_to_cg(fs, ip->i_number);
1036163fc9cSnatano 	else
1046163fc9cSnatano 		cg = dtog(fs, bpref);
1056163fc9cSnatano 	bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
1066163fc9cSnatano 	if (bno > 0) {
1076163fc9cSnatano 		DIP_ADD(ip, blocks, size / DEV_BSIZE);
1086163fc9cSnatano 		*bnp = bno;
1096163fc9cSnatano 		return (0);
1106163fc9cSnatano 	}
1116163fc9cSnatano nospace:
1126163fc9cSnatano 	return (ENOSPC);
1136163fc9cSnatano }
1146163fc9cSnatano 
1156163fc9cSnatano /*
1166163fc9cSnatano  * Select the desired position for the next block in a file.  The file is
1176163fc9cSnatano  * logically divided into sections. The first section is composed of the
1186163fc9cSnatano  * direct blocks. Each additional section contains fs_maxbpg blocks.
1196163fc9cSnatano  *
1206163fc9cSnatano  * If no blocks have been allocated in the first section, the policy is to
1216163fc9cSnatano  * request a block in the same cylinder group as the inode that describes
1226163fc9cSnatano  * the file. If no blocks have been allocated in any other section, the
1236163fc9cSnatano  * policy is to place the section in a cylinder group with a greater than
1246163fc9cSnatano  * average number of free blocks.  An appropriate cylinder group is found
1256163fc9cSnatano  * by using a rotor that sweeps the cylinder groups. When a new group of
1266163fc9cSnatano  * blocks is needed, the sweep begins in the cylinder group following the
1276163fc9cSnatano  * cylinder group from which the previous allocation was made. The sweep
1286163fc9cSnatano  * continues until a cylinder group with greater than the average number
1296163fc9cSnatano  * of free blocks is found. If the allocation is for the first block in an
1306163fc9cSnatano  * indirect block, the information on the previous allocation is unavailable;
1316163fc9cSnatano  * here a best guess is made based upon the logical block number being
1326163fc9cSnatano  * allocated.
1336163fc9cSnatano  *
1346163fc9cSnatano  * If a section is already partially allocated, the policy is to
1356163fc9cSnatano  * contiguously allocate fs_maxcontig blocks.  The end of one of these
1366163fc9cSnatano  * contiguous blocks and the beginning of the next is physically separated
1376163fc9cSnatano  * so that the disk head will be in transit between them for at least
1386163fc9cSnatano  * fs_rotdelay milliseconds.  This is to allow time for the processor to
1396163fc9cSnatano  * schedule another I/O transfer.
1406163fc9cSnatano  */
1416163fc9cSnatano /* XXX ondisk32 */
1426163fc9cSnatano daddr_t
ffs_blkpref_ufs1(struct inode * ip,daddr_t lbn,int indx,int32_t * bap)1436163fc9cSnatano ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int32_t *bap)
1446163fc9cSnatano {
1456163fc9cSnatano 	struct fs *fs;
1466163fc9cSnatano 	int cg;
1476163fc9cSnatano 	int avgbfree, startcg;
1486163fc9cSnatano 
1496163fc9cSnatano 	fs = ip->i_fs;
1506163fc9cSnatano 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1516c4d9c1cSnatano 		if (lbn < NDADDR + NINDIR(fs)) {
1526163fc9cSnatano 			cg = ino_to_cg(fs, ip->i_number);
1536163fc9cSnatano 			return (fs->fs_fpg * cg + fs->fs_frag);
1546163fc9cSnatano 		}
1556163fc9cSnatano 		/*
1566163fc9cSnatano 		 * Find a cylinder with greater than average number of
1576163fc9cSnatano 		 * unused data blocks.
1586163fc9cSnatano 		 */
1596163fc9cSnatano 		if (indx == 0 || bap[indx - 1] == 0)
1606163fc9cSnatano 			startcg =
1616163fc9cSnatano 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1626163fc9cSnatano 		else
1637247f83aSnatano 			startcg = dtog(fs, bap[indx - 1] + 1);
1646163fc9cSnatano 		startcg %= fs->fs_ncg;
1656163fc9cSnatano 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1666163fc9cSnatano 		for (cg = startcg; cg < fs->fs_ncg; cg++)
1676163fc9cSnatano 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
1686163fc9cSnatano 				return (fs->fs_fpg * cg + fs->fs_frag);
1696163fc9cSnatano 		for (cg = 0; cg <= startcg; cg++)
1706163fc9cSnatano 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
1716163fc9cSnatano 				return (fs->fs_fpg * cg + fs->fs_frag);
1726163fc9cSnatano 		return (0);
1736163fc9cSnatano 	}
1746163fc9cSnatano 	/*
1756163fc9cSnatano 	 * We just always try to lay things out contiguously.
1766163fc9cSnatano 	 */
1777247f83aSnatano 	return bap[indx - 1] + fs->fs_frag;
1786163fc9cSnatano }
1796163fc9cSnatano 
1806163fc9cSnatano daddr_t
ffs_blkpref_ufs2(struct inode * ip,daddr_t lbn,int indx,int64_t * bap)1816163fc9cSnatano ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int64_t *bap)
1826163fc9cSnatano {
1836163fc9cSnatano 	struct fs *fs;
1846163fc9cSnatano 	int cg;
1856163fc9cSnatano 	int avgbfree, startcg;
1866163fc9cSnatano 
1876163fc9cSnatano 	fs = ip->i_fs;
1886163fc9cSnatano 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1896c4d9c1cSnatano 		if (lbn < NDADDR + NINDIR(fs)) {
1906163fc9cSnatano 			cg = ino_to_cg(fs, ip->i_number);
1916163fc9cSnatano 			return (fs->fs_fpg * cg + fs->fs_frag);
1926163fc9cSnatano 		}
1936163fc9cSnatano 		/*
1946163fc9cSnatano 		 * Find a cylinder with greater than average number of
1956163fc9cSnatano 		 * unused data blocks.
1966163fc9cSnatano 		 */
1976163fc9cSnatano 		if (indx == 0 || bap[indx - 1] == 0)
1986163fc9cSnatano 			startcg =
1996163fc9cSnatano 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
2006163fc9cSnatano 		else
2017247f83aSnatano 			startcg = dtog(fs, bap[indx - 1] + 1);
2026163fc9cSnatano 		startcg %= fs->fs_ncg;
2036163fc9cSnatano 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
2046163fc9cSnatano 		for (cg = startcg; cg < fs->fs_ncg; cg++)
2056163fc9cSnatano 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
2066163fc9cSnatano 				return (fs->fs_fpg * cg + fs->fs_frag);
2076163fc9cSnatano 			}
2086163fc9cSnatano 		for (cg = 0; cg < startcg; cg++)
2096163fc9cSnatano 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
2106163fc9cSnatano 				return (fs->fs_fpg * cg + fs->fs_frag);
2116163fc9cSnatano 			}
2126163fc9cSnatano 		return (0);
2136163fc9cSnatano 	}
2146163fc9cSnatano 	/*
2156163fc9cSnatano 	 * We just always try to lay things out contiguously.
2166163fc9cSnatano 	 */
2177247f83aSnatano 	return bap[indx - 1] + fs->fs_frag;
2186163fc9cSnatano }
2196163fc9cSnatano 
2206163fc9cSnatano /*
2216163fc9cSnatano  * Implement the cylinder overflow algorithm.
2226163fc9cSnatano  *
2236163fc9cSnatano  * The policy implemented by this algorithm is:
2246163fc9cSnatano  *   1) allocate the block in its requested cylinder group.
225*8e544e80Sjsg  *   2) quadratically rehash on the cylinder group number.
2266163fc9cSnatano  *   3) brute force search for a free block.
2276163fc9cSnatano  *
2286163fc9cSnatano  * `size':	size for data blocks, mode for inodes
2296163fc9cSnatano  */
2306163fc9cSnatano /*VARARGS5*/
2316163fc9cSnatano static daddr_t
ffs_hashalloc(struct inode * ip,int cg,daddr_t pref,int size,daddr_t (* allocator)(struct inode *,int,daddr_t,int))2326163fc9cSnatano ffs_hashalloc(struct inode *ip, int cg, daddr_t pref, int size,
2336163fc9cSnatano     daddr_t (*allocator)(struct inode *, int, daddr_t, int))
2346163fc9cSnatano {
2356163fc9cSnatano 	struct fs *fs;
2366163fc9cSnatano 	daddr_t result;
2376163fc9cSnatano 	int i, icg = cg;
2386163fc9cSnatano 
2396163fc9cSnatano 	fs = ip->i_fs;
2406163fc9cSnatano 	/*
2416163fc9cSnatano 	 * 1: preferred cylinder group
2426163fc9cSnatano 	 */
2436163fc9cSnatano 	result = (*allocator)(ip, cg, pref, size);
2446163fc9cSnatano 	if (result)
2456163fc9cSnatano 		return (result);
2466163fc9cSnatano 	/*
2476163fc9cSnatano 	 * 2: quadratic rehash
2486163fc9cSnatano 	 */
2496163fc9cSnatano 	for (i = 1; i < fs->fs_ncg; i *= 2) {
2506163fc9cSnatano 		cg += i;
2516163fc9cSnatano 		if (cg >= fs->fs_ncg)
2526163fc9cSnatano 			cg -= fs->fs_ncg;
2536163fc9cSnatano 		result = (*allocator)(ip, cg, 0, size);
2546163fc9cSnatano 		if (result)
2556163fc9cSnatano 			return (result);
2566163fc9cSnatano 	}
2576163fc9cSnatano 	/*
2586163fc9cSnatano 	 * 3: brute force search
2596163fc9cSnatano 	 * Note that we start at i == 2, since 0 was checked initially,
2606163fc9cSnatano 	 * and 1 is always checked in the quadratic rehash.
2616163fc9cSnatano 	 */
2626163fc9cSnatano 	cg = (icg + 2) % fs->fs_ncg;
2636163fc9cSnatano 	for (i = 2; i < fs->fs_ncg; i++) {
2646163fc9cSnatano 		result = (*allocator)(ip, cg, 0, size);
2656163fc9cSnatano 		if (result)
2666163fc9cSnatano 			return (result);
2676163fc9cSnatano 		cg++;
2686163fc9cSnatano 		if (cg == fs->fs_ncg)
2696163fc9cSnatano 			cg = 0;
2706163fc9cSnatano 	}
2716163fc9cSnatano 	return (0);
2726163fc9cSnatano }
2736163fc9cSnatano 
2746163fc9cSnatano /*
2756163fc9cSnatano  * Determine whether a block can be allocated.
2766163fc9cSnatano  *
2776163fc9cSnatano  * Check to see if a block of the appropriate size is available,
2786163fc9cSnatano  * and if it is, allocate it.
2796163fc9cSnatano  */
2806163fc9cSnatano static daddr_t
ffs_alloccg(struct inode * ip,int cg,daddr_t bpref,int size)2816163fc9cSnatano ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
2826163fc9cSnatano {
2836163fc9cSnatano 	struct cg *cgp;
2844af638d7Stedu 	struct mkfsbuf *bp;
2856163fc9cSnatano 	daddr_t bno, blkno;
2866163fc9cSnatano 	int error, frags, allocsiz, i;
2876163fc9cSnatano 	struct fs *fs = ip->i_fs;
2886163fc9cSnatano 
2896163fc9cSnatano 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
2906163fc9cSnatano 		return (0);
2916c4d9c1cSnatano 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
2926163fc9cSnatano 	    (int)fs->fs_cgsize, 0, &bp);
2936163fc9cSnatano 	if (error) {
2946163fc9cSnatano 		return (0);
2956163fc9cSnatano 	}
2966163fc9cSnatano 	cgp = (struct cg *)bp->b_data;
2976c4d9c1cSnatano 	if (!cg_chkmagic(cgp) ||
2986163fc9cSnatano 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
2996163fc9cSnatano 		brelse(bp, 0);
3006163fc9cSnatano 		return (0);
3016163fc9cSnatano 	}
3026163fc9cSnatano 	if (size == fs->fs_bsize) {
3036163fc9cSnatano 		bno = ffs_alloccgblk(ip, bp, bpref);
3046163fc9cSnatano 		bwrite(bp);
3056163fc9cSnatano 		return (bno);
3066163fc9cSnatano 	}
3076163fc9cSnatano 	/*
3086163fc9cSnatano 	 * check to see if any fragments are already available
3096163fc9cSnatano 	 * allocsiz is the size which will be allocated, hacking
3106163fc9cSnatano 	 * it down to a smaller size if necessary
3116163fc9cSnatano 	 */
3126c4d9c1cSnatano 	frags = numfrags(fs, size);
3136163fc9cSnatano 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
3146163fc9cSnatano 		if (cgp->cg_frsum[allocsiz] != 0)
3156163fc9cSnatano 			break;
3166163fc9cSnatano 	if (allocsiz == fs->fs_frag) {
3176163fc9cSnatano 		/*
3186163fc9cSnatano 		 * no fragments were available, so a block will be
3196163fc9cSnatano 		 * allocated, and hacked up
3206163fc9cSnatano 		 */
3216163fc9cSnatano 		if (cgp->cg_cs.cs_nbfree == 0) {
3226163fc9cSnatano 			brelse(bp, 0);
3236163fc9cSnatano 			return (0);
3246163fc9cSnatano 		}
3256163fc9cSnatano 		bno = ffs_alloccgblk(ip, bp, bpref);
3266163fc9cSnatano 		bpref = dtogd(fs, bno);
3276163fc9cSnatano 		for (i = frags; i < fs->fs_frag; i++)
3286c4d9c1cSnatano 			setbit(cg_blksfree(cgp), bpref + i);
3296163fc9cSnatano 		i = fs->fs_frag - frags;
33099a1e5aaStedu 		cgp->cg_cs.cs_nffree += i;
3316163fc9cSnatano 		fs->fs_cstotal.cs_nffree += i;
3326163fc9cSnatano 		fs->fs_cs(fs, cg).cs_nffree += i;
3336163fc9cSnatano 		fs->fs_fmod = 1;
33499a1e5aaStedu 		cgp->cg_frsum[i] += 1;
3356163fc9cSnatano 		bdwrite(bp);
3366163fc9cSnatano 		return (bno);
3376163fc9cSnatano 	}
3386163fc9cSnatano 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
3396163fc9cSnatano 	for (i = 0; i < frags; i++)
3406c4d9c1cSnatano 		clrbit(cg_blksfree(cgp), bno + i);
34199a1e5aaStedu 	cgp->cg_cs.cs_nffree -= frags;
3426163fc9cSnatano 	fs->fs_cstotal.cs_nffree -= frags;
3436163fc9cSnatano 	fs->fs_cs(fs, cg).cs_nffree -= frags;
3446163fc9cSnatano 	fs->fs_fmod = 1;
34599a1e5aaStedu 	cgp->cg_frsum[allocsiz] -= 1;
3466163fc9cSnatano 	if (frags != allocsiz)
34799a1e5aaStedu 		cgp->cg_frsum[allocsiz - frags] += 1;
3486163fc9cSnatano 	blkno = cg * fs->fs_fpg + bno;
3496163fc9cSnatano 	bdwrite(bp);
3506163fc9cSnatano 	return blkno;
3516163fc9cSnatano }
3526163fc9cSnatano 
3536163fc9cSnatano /*
3546163fc9cSnatano  * Allocate a block in a cylinder group.
3556163fc9cSnatano  *
3566163fc9cSnatano  * This algorithm implements the following policy:
3576163fc9cSnatano  *   1) allocate the requested block.
3586163fc9cSnatano  *   2) allocate a rotationally optimal block in the same cylinder.
3596163fc9cSnatano  *   3) allocate the next available block on the block rotor for the
3606163fc9cSnatano  *      specified cylinder group.
3616163fc9cSnatano  * Note that this routine only allocates fs_bsize blocks; these
3626163fc9cSnatano  * blocks may be fragmented by the routine that allocates them.
3636163fc9cSnatano  */
3646163fc9cSnatano static daddr_t
ffs_alloccgblk(struct inode * ip,struct mkfsbuf * bp,daddr_t bpref)3654af638d7Stedu ffs_alloccgblk(struct inode *ip, struct mkfsbuf *bp, daddr_t bpref)
3666163fc9cSnatano {
3676163fc9cSnatano 	struct cg *cgp;
3686163fc9cSnatano 	daddr_t blkno;
3696163fc9cSnatano 	int32_t bno;
3706163fc9cSnatano 	struct fs *fs = ip->i_fs;
3716163fc9cSnatano 	u_int8_t *blksfree;
3726163fc9cSnatano 
3736163fc9cSnatano 	cgp = (struct cg *)bp->b_data;
3746c4d9c1cSnatano 	blksfree = cg_blksfree(cgp);
3757247f83aSnatano 	if (bpref == 0 || dtog(fs, bpref) != cgp->cg_cgx) {
3767247f83aSnatano 		bpref = cgp->cg_rotor;
3776163fc9cSnatano 	} else {
3786c4d9c1cSnatano 		bpref = blknum(fs, bpref);
3796163fc9cSnatano 		bno = dtogd(fs, bpref);
3806163fc9cSnatano 		/*
3816163fc9cSnatano 		 * if the requested block is available, use it
3826163fc9cSnatano 		 */
3836c4d9c1cSnatano 		if (ffs_isblock(fs, blksfree, fragstoblks(fs, bno)))
3846163fc9cSnatano 			goto gotit;
3856163fc9cSnatano 	}
3866163fc9cSnatano 	/*
3876163fc9cSnatano 	 * Take the next available one in this cylinder group.
3886163fc9cSnatano 	 */
3896163fc9cSnatano 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
3906163fc9cSnatano 	if (bno < 0)
3916163fc9cSnatano 		return (0);
3927247f83aSnatano 	cgp->cg_rotor = bno;
3936163fc9cSnatano gotit:
3946c4d9c1cSnatano 	blkno = fragstoblks(fs, bno);
3956163fc9cSnatano 	ffs_clrblock(fs, blksfree, (long)blkno);
3966163fc9cSnatano 	ffs_clusteracct(fs, cgp, blkno, -1);
39799a1e5aaStedu 	cgp->cg_cs.cs_nbfree -= 1;
3986163fc9cSnatano 	fs->fs_cstotal.cs_nbfree--;
3997247f83aSnatano 	fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
4006163fc9cSnatano 	fs->fs_fmod = 1;
4017247f83aSnatano 	blkno = cgp->cg_cgx * fs->fs_fpg + bno;
4026163fc9cSnatano 	return (blkno);
4036163fc9cSnatano }
4046163fc9cSnatano 
4056163fc9cSnatano static int
scanc(u_int size,const u_char * cp,const u_char table[],int mask)4066163fc9cSnatano scanc(u_int size, const u_char *cp, const u_char table[], int mask)
4076163fc9cSnatano {
4086163fc9cSnatano 	const u_char *end = &cp[size];
4096163fc9cSnatano 
4106163fc9cSnatano 	while (cp < end && (table[*cp] & mask) == 0)
4116163fc9cSnatano 		cp++;
4126163fc9cSnatano 	return (end - cp);
4136163fc9cSnatano }
4146163fc9cSnatano 
4156163fc9cSnatano /*
4166163fc9cSnatano  * Find a block of the specified size in the specified cylinder group.
4176163fc9cSnatano  *
4186163fc9cSnatano  * It is a panic if a request is made to find a block if none are
4196163fc9cSnatano  * available.
4206163fc9cSnatano  */
4216163fc9cSnatano static int32_t
ffs_mapsearch(struct fs * fs,struct cg * cgp,daddr_t bpref,int allocsiz)4226163fc9cSnatano ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
4236163fc9cSnatano {
4246163fc9cSnatano 	int32_t bno;
4256163fc9cSnatano 	int start, len, loc, i;
4266163fc9cSnatano 	int blk, field, subfield, pos;
4276163fc9cSnatano 	int ostart, olen;
4286163fc9cSnatano 
4296163fc9cSnatano 	/*
4306163fc9cSnatano 	 * find the fragment by searching through the free block
4316163fc9cSnatano 	 * map for an appropriate bit pattern
4326163fc9cSnatano 	 */
4336163fc9cSnatano 	if (bpref)
4346163fc9cSnatano 		start = dtogd(fs, bpref) / NBBY;
4356163fc9cSnatano 	else
4367247f83aSnatano 		start = cgp->cg_frotor / NBBY;
4376163fc9cSnatano 	len = howmany(fs->fs_fpg, NBBY) - start;
4386163fc9cSnatano 	ostart = start;
4396163fc9cSnatano 	olen = len;
4406163fc9cSnatano 	loc = scanc((u_int)len,
4416c4d9c1cSnatano 		(const u_char *)&cg_blksfree(cgp)[start],
4426163fc9cSnatano 		(const u_char *)fragtbl[fs->fs_frag],
4436163fc9cSnatano 		(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
4446163fc9cSnatano 	if (loc == 0) {
4456163fc9cSnatano 		len = start + 1;
4466163fc9cSnatano 		start = 0;
4476163fc9cSnatano 		loc = scanc((u_int)len,
4486c4d9c1cSnatano 			(const u_char *)&cg_blksfree(cgp)[0],
4496163fc9cSnatano 			(const u_char *)fragtbl[fs->fs_frag],
4506163fc9cSnatano 			(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
4516163fc9cSnatano 		if (loc == 0) {
452944eabd5Snatano 			errx(1, "%s: map corrupted: start %d "
4536163fc9cSnatano 			    "len %d offset %d %ld", __func__, ostart, olen,
4547247f83aSnatano 			    cgp->cg_freeoff,
4556c4d9c1cSnatano 			    (long)cg_blksfree(cgp) - (long)cgp);
4566163fc9cSnatano 		}
4576163fc9cSnatano 	}
4586163fc9cSnatano 	bno = (start + len - loc) * NBBY;
4597247f83aSnatano 	cgp->cg_frotor = bno;
4606163fc9cSnatano 	/*
4616163fc9cSnatano 	 * found the byte in the map
4626163fc9cSnatano 	 * sift through the bits to find the selected frag
4636163fc9cSnatano 	 */
4646163fc9cSnatano 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
4656c4d9c1cSnatano 		blk = blkmap(fs, cg_blksfree(cgp), bno);
4666163fc9cSnatano 		blk <<= 1;
4676163fc9cSnatano 		field = around[allocsiz];
4686163fc9cSnatano 		subfield = inside[allocsiz];
4696163fc9cSnatano 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
4706163fc9cSnatano 			if ((blk & field) == subfield)
4716163fc9cSnatano 				return (bno + pos);
4726163fc9cSnatano 			field <<= 1;
4736163fc9cSnatano 			subfield <<= 1;
4746163fc9cSnatano 		}
4756163fc9cSnatano 	}
476944eabd5Snatano 	errx(1, "%s: block not in map: bno %lld", __func__, (long long)bno);
4776163fc9cSnatano 	return (-1);
4786163fc9cSnatano }
479