xref: /minix3/usr.sbin/makefs/ffs/ffs_alloc.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ffs_alloc.c,v 1.28 2015/03/29 05:52:59 agc Exp $	*/
29f988b79SJean-Baptiste Boric /* From: NetBSD: ffs_alloc.c,v 1.50 2001/09/06 02:16:01 lukem Exp */
39f988b79SJean-Baptiste Boric 
49f988b79SJean-Baptiste Boric /*
59f988b79SJean-Baptiste Boric  * Copyright (c) 2002 Networks Associates Technology, Inc.
69f988b79SJean-Baptiste Boric  * All rights reserved.
79f988b79SJean-Baptiste Boric  *
89f988b79SJean-Baptiste Boric  * This software was developed for the FreeBSD Project by Marshall
99f988b79SJean-Baptiste Boric  * Kirk McKusick and Network Associates Laboratories, the Security
109f988b79SJean-Baptiste Boric  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
119f988b79SJean-Baptiste Boric  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
129f988b79SJean-Baptiste Boric  * research program
139f988b79SJean-Baptiste Boric  *
149f988b79SJean-Baptiste Boric  * Copyright (c) 1982, 1986, 1989, 1993
159f988b79SJean-Baptiste Boric  *	The Regents of the University of California.  All rights reserved.
169f988b79SJean-Baptiste Boric  *
179f988b79SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
189f988b79SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
199f988b79SJean-Baptiste Boric  * are met:
209f988b79SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
219f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
229f988b79SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
239f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
249f988b79SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
259f988b79SJean-Baptiste Boric  * 3. Neither the name of the University nor the names of its contributors
269f988b79SJean-Baptiste Boric  *    may be used to endorse or promote products derived from this software
279f988b79SJean-Baptiste Boric  *    without specific prior written permission.
289f988b79SJean-Baptiste Boric  *
299f988b79SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
309f988b79SJean-Baptiste Boric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
319f988b79SJean-Baptiste Boric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
329f988b79SJean-Baptiste Boric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
339f988b79SJean-Baptiste Boric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
349f988b79SJean-Baptiste Boric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
359f988b79SJean-Baptiste Boric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
369f988b79SJean-Baptiste Boric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
379f988b79SJean-Baptiste Boric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
389f988b79SJean-Baptiste Boric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
399f988b79SJean-Baptiste Boric  * SUCH DAMAGE.
409f988b79SJean-Baptiste Boric  *
419f988b79SJean-Baptiste Boric  *	@(#)ffs_alloc.c	8.19 (Berkeley) 7/13/95
429f988b79SJean-Baptiste Boric  */
439f988b79SJean-Baptiste Boric 
449f988b79SJean-Baptiste Boric #if HAVE_NBTOOL_CONFIG_H
459f988b79SJean-Baptiste Boric #include "nbtool_config.h"
469f988b79SJean-Baptiste Boric #endif
479f988b79SJean-Baptiste Boric 
489f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
499f988b79SJean-Baptiste Boric #if defined(__RCSID) && !defined(__lint)
50*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ffs_alloc.c,v 1.28 2015/03/29 05:52:59 agc Exp $");
519f988b79SJean-Baptiste Boric #endif	/* !__lint */
529f988b79SJean-Baptiste Boric 
539f988b79SJean-Baptiste Boric #include <sys/param.h>
549f988b79SJean-Baptiste Boric #include <sys/time.h>
559f988b79SJean-Baptiste Boric 
569f988b79SJean-Baptiste Boric #include <errno.h>
579f988b79SJean-Baptiste Boric 
589f988b79SJean-Baptiste Boric #include "makefs.h"
599f988b79SJean-Baptiste Boric 
609f988b79SJean-Baptiste Boric #include <ufs/ufs/dinode.h>
619f988b79SJean-Baptiste Boric #include <ufs/ufs/ufs_bswap.h>
629f988b79SJean-Baptiste Boric #include <ufs/ffs/fs.h>
639f988b79SJean-Baptiste Boric 
649f988b79SJean-Baptiste Boric #include "ffs/buf.h"
659f988b79SJean-Baptiste Boric #include "ffs/ufs_inode.h"
669f988b79SJean-Baptiste Boric #include "ffs/ffs_extern.h"
679f988b79SJean-Baptiste Boric 
689f988b79SJean-Baptiste Boric 
699f988b79SJean-Baptiste Boric static int scanc(u_int, const u_char *, const u_char *, int);
709f988b79SJean-Baptiste Boric 
719f988b79SJean-Baptiste Boric static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int);
729f988b79SJean-Baptiste Boric static daddr_t ffs_alloccgblk(struct inode *, struct buf *, daddr_t);
739f988b79SJean-Baptiste Boric static daddr_t ffs_hashalloc(struct inode *, int, daddr_t, int,
749f988b79SJean-Baptiste Boric 		     daddr_t (*)(struct inode *, int, daddr_t, int));
759f988b79SJean-Baptiste Boric static int32_t ffs_mapsearch(struct fs *, struct cg *, daddr_t, int);
769f988b79SJean-Baptiste Boric 
779f988b79SJean-Baptiste Boric /* in ffs_tables.c */
789f988b79SJean-Baptiste Boric extern const int inside[], around[];
799f988b79SJean-Baptiste Boric extern const u_char * const fragtbl[];
809f988b79SJean-Baptiste Boric 
819f988b79SJean-Baptiste Boric /*
829f988b79SJean-Baptiste Boric  * Allocate a block in the file system.
839f988b79SJean-Baptiste Boric  *
849f988b79SJean-Baptiste Boric  * The size of the requested block is given, which must be some
859f988b79SJean-Baptiste Boric  * multiple of fs_fsize and <= fs_bsize.
869f988b79SJean-Baptiste Boric  * A preference may be optionally specified. If a preference is given
879f988b79SJean-Baptiste Boric  * the following hierarchy is used to allocate a block:
889f988b79SJean-Baptiste Boric  *   1) allocate the requested block.
899f988b79SJean-Baptiste Boric  *   2) allocate a rotationally optimal block in the same cylinder.
909f988b79SJean-Baptiste Boric  *   3) allocate a block in the same cylinder group.
919f988b79SJean-Baptiste Boric  *   4) quadradically rehash into other cylinder groups, until an
929f988b79SJean-Baptiste Boric  *      available block is located.
939f988b79SJean-Baptiste Boric  * If no block preference is given the following hierarchy is used
949f988b79SJean-Baptiste Boric  * to allocate a block:
959f988b79SJean-Baptiste Boric  *   1) allocate a block in the cylinder group that contains the
969f988b79SJean-Baptiste Boric  *      inode for the file.
979f988b79SJean-Baptiste Boric  *   2) quadradically rehash into other cylinder groups, until an
989f988b79SJean-Baptiste Boric  *      available block is located.
999f988b79SJean-Baptiste Boric  */
1009f988b79SJean-Baptiste Boric int
ffs_alloc(struct inode * ip,daddr_t lbn __unused,daddr_t bpref,int size,daddr_t * bnp)1019f988b79SJean-Baptiste Boric ffs_alloc(struct inode *ip, daddr_t lbn __unused, daddr_t bpref, int size,
1029f988b79SJean-Baptiste Boric     daddr_t *bnp)
1039f988b79SJean-Baptiste Boric {
1049f988b79SJean-Baptiste Boric 	struct fs *fs = ip->i_fs;
1059f988b79SJean-Baptiste Boric 	daddr_t bno;
1069f988b79SJean-Baptiste Boric 	int cg;
1079f988b79SJean-Baptiste Boric 
1089f988b79SJean-Baptiste Boric 	*bnp = 0;
1099f988b79SJean-Baptiste Boric 	if (size > fs->fs_bsize || ffs_fragoff(fs, size) != 0) {
1109f988b79SJean-Baptiste Boric 		errx(1, "ffs_alloc: bad size: bsize %d size %d",
1119f988b79SJean-Baptiste Boric 		    fs->fs_bsize, size);
1129f988b79SJean-Baptiste Boric 	}
1139f988b79SJean-Baptiste Boric 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
1149f988b79SJean-Baptiste Boric 		goto nospace;
1159f988b79SJean-Baptiste Boric 	if (bpref >= fs->fs_size)
1169f988b79SJean-Baptiste Boric 		bpref = 0;
1179f988b79SJean-Baptiste Boric 	if (bpref == 0)
1189f988b79SJean-Baptiste Boric 		cg = ino_to_cg(fs, ip->i_number);
1199f988b79SJean-Baptiste Boric 	else
1209f988b79SJean-Baptiste Boric 		cg = dtog(fs, bpref);
1219f988b79SJean-Baptiste Boric 	bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
1229f988b79SJean-Baptiste Boric 	if (bno > 0) {
1239f988b79SJean-Baptiste Boric 		DIP_ADD(ip, blocks, size / DEV_BSIZE);
1249f988b79SJean-Baptiste Boric 		*bnp = bno;
1259f988b79SJean-Baptiste Boric 		return (0);
1269f988b79SJean-Baptiste Boric 	}
1279f988b79SJean-Baptiste Boric nospace:
1289f988b79SJean-Baptiste Boric 	return (ENOSPC);
1299f988b79SJean-Baptiste Boric }
1309f988b79SJean-Baptiste Boric 
1319f988b79SJean-Baptiste Boric /*
1329f988b79SJean-Baptiste Boric  * Select the desired position for the next block in a file.  The file is
1339f988b79SJean-Baptiste Boric  * logically divided into sections. The first section is composed of the
1349f988b79SJean-Baptiste Boric  * direct blocks. Each additional section contains fs_maxbpg blocks.
1359f988b79SJean-Baptiste Boric  *
1369f988b79SJean-Baptiste Boric  * If no blocks have been allocated in the first section, the policy is to
1379f988b79SJean-Baptiste Boric  * request a block in the same cylinder group as the inode that describes
1389f988b79SJean-Baptiste Boric  * the file. If no blocks have been allocated in any other section, the
1399f988b79SJean-Baptiste Boric  * policy is to place the section in a cylinder group with a greater than
1409f988b79SJean-Baptiste Boric  * average number of free blocks.  An appropriate cylinder group is found
1419f988b79SJean-Baptiste Boric  * by using a rotor that sweeps the cylinder groups. When a new group of
1429f988b79SJean-Baptiste Boric  * blocks is needed, the sweep begins in the cylinder group following the
1439f988b79SJean-Baptiste Boric  * cylinder group from which the previous allocation was made. The sweep
1449f988b79SJean-Baptiste Boric  * continues until a cylinder group with greater than the average number
1459f988b79SJean-Baptiste Boric  * of free blocks is found. If the allocation is for the first block in an
1469f988b79SJean-Baptiste Boric  * indirect block, the information on the previous allocation is unavailable;
1479f988b79SJean-Baptiste Boric  * here a best guess is made based upon the logical block number being
1489f988b79SJean-Baptiste Boric  * allocated.
1499f988b79SJean-Baptiste Boric  *
1509f988b79SJean-Baptiste Boric  * If a section is already partially allocated, the policy is to
1519f988b79SJean-Baptiste Boric  * contiguously allocate fs_maxcontig blocks.  The end of one of these
1529f988b79SJean-Baptiste Boric  * contiguous blocks and the beginning of the next is physically separated
1539f988b79SJean-Baptiste Boric  * so that the disk head will be in transit between them for at least
1549f988b79SJean-Baptiste Boric  * fs_rotdelay milliseconds.  This is to allow time for the processor to
1559f988b79SJean-Baptiste Boric  * schedule another I/O transfer.
1569f988b79SJean-Baptiste Boric  */
1579f988b79SJean-Baptiste Boric /* XXX ondisk32 */
1589f988b79SJean-Baptiste Boric daddr_t
ffs_blkpref_ufs1(struct inode * ip,daddr_t lbn,int indx,int32_t * bap)1599f988b79SJean-Baptiste Boric ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int32_t *bap)
1609f988b79SJean-Baptiste Boric {
1619f988b79SJean-Baptiste Boric 	struct fs *fs;
1629f988b79SJean-Baptiste Boric 	int cg;
1639f988b79SJean-Baptiste Boric 	int avgbfree, startcg;
1649f988b79SJean-Baptiste Boric 
1659f988b79SJean-Baptiste Boric 	fs = ip->i_fs;
1669f988b79SJean-Baptiste Boric 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
1679f988b79SJean-Baptiste Boric 		if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
1689f988b79SJean-Baptiste Boric 			cg = ino_to_cg(fs, ip->i_number);
1699f988b79SJean-Baptiste Boric 			return (fs->fs_fpg * cg + fs->fs_frag);
1709f988b79SJean-Baptiste Boric 		}
1719f988b79SJean-Baptiste Boric 		/*
1729f988b79SJean-Baptiste Boric 		 * Find a cylinder with greater than average number of
1739f988b79SJean-Baptiste Boric 		 * unused data blocks.
1749f988b79SJean-Baptiste Boric 		 */
1759f988b79SJean-Baptiste Boric 		if (indx == 0 || bap[indx - 1] == 0)
1769f988b79SJean-Baptiste Boric 			startcg =
1779f988b79SJean-Baptiste Boric 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1789f988b79SJean-Baptiste Boric 		else
1799f988b79SJean-Baptiste Boric 			startcg = dtog(fs,
1809f988b79SJean-Baptiste Boric 				ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
1819f988b79SJean-Baptiste Boric 		startcg %= fs->fs_ncg;
1829f988b79SJean-Baptiste Boric 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1839f988b79SJean-Baptiste Boric 		for (cg = startcg; cg < fs->fs_ncg; cg++)
1849f988b79SJean-Baptiste Boric 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
1859f988b79SJean-Baptiste Boric 				return (fs->fs_fpg * cg + fs->fs_frag);
1869f988b79SJean-Baptiste Boric 		for (cg = 0; cg <= startcg; cg++)
1879f988b79SJean-Baptiste Boric 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
1889f988b79SJean-Baptiste Boric 				return (fs->fs_fpg * cg + fs->fs_frag);
1899f988b79SJean-Baptiste Boric 		return (0);
1909f988b79SJean-Baptiste Boric 	}
1919f988b79SJean-Baptiste Boric 	/*
1929f988b79SJean-Baptiste Boric 	 * We just always try to lay things out contiguously.
1939f988b79SJean-Baptiste Boric 	 */
1949f988b79SJean-Baptiste Boric 	return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
1959f988b79SJean-Baptiste Boric }
1969f988b79SJean-Baptiste Boric 
1979f988b79SJean-Baptiste Boric daddr_t
ffs_blkpref_ufs2(struct inode * ip,daddr_t lbn,int indx,int64_t * bap)1989f988b79SJean-Baptiste Boric ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int64_t *bap)
1999f988b79SJean-Baptiste Boric {
2009f988b79SJean-Baptiste Boric 	struct fs *fs;
2019f988b79SJean-Baptiste Boric 	int cg;
2029f988b79SJean-Baptiste Boric 	int avgbfree, startcg;
2039f988b79SJean-Baptiste Boric 
2049f988b79SJean-Baptiste Boric 	fs = ip->i_fs;
2059f988b79SJean-Baptiste Boric 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
2069f988b79SJean-Baptiste Boric 		if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
2079f988b79SJean-Baptiste Boric 			cg = ino_to_cg(fs, ip->i_number);
2089f988b79SJean-Baptiste Boric 			return (fs->fs_fpg * cg + fs->fs_frag);
2099f988b79SJean-Baptiste Boric 		}
2109f988b79SJean-Baptiste Boric 		/*
2119f988b79SJean-Baptiste Boric 		 * Find a cylinder with greater than average number of
2129f988b79SJean-Baptiste Boric 		 * unused data blocks.
2139f988b79SJean-Baptiste Boric 		 */
2149f988b79SJean-Baptiste Boric 		if (indx == 0 || bap[indx - 1] == 0)
2159f988b79SJean-Baptiste Boric 			startcg =
2169f988b79SJean-Baptiste Boric 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
2179f988b79SJean-Baptiste Boric 		else
2189f988b79SJean-Baptiste Boric 			startcg = dtog(fs,
2199f988b79SJean-Baptiste Boric 				ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
2209f988b79SJean-Baptiste Boric 		startcg %= fs->fs_ncg;
2219f988b79SJean-Baptiste Boric 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
2229f988b79SJean-Baptiste Boric 		for (cg = startcg; cg < fs->fs_ncg; cg++)
2239f988b79SJean-Baptiste Boric 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
2249f988b79SJean-Baptiste Boric 				return (fs->fs_fpg * cg + fs->fs_frag);
2259f988b79SJean-Baptiste Boric 			}
2269f988b79SJean-Baptiste Boric 		for (cg = 0; cg < startcg; cg++)
2279f988b79SJean-Baptiste Boric 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
2289f988b79SJean-Baptiste Boric 				return (fs->fs_fpg * cg + fs->fs_frag);
2299f988b79SJean-Baptiste Boric 			}
2309f988b79SJean-Baptiste Boric 		return (0);
2319f988b79SJean-Baptiste Boric 	}
2329f988b79SJean-Baptiste Boric 	/*
2339f988b79SJean-Baptiste Boric 	 * We just always try to lay things out contiguously.
2349f988b79SJean-Baptiste Boric 	 */
2359f988b79SJean-Baptiste Boric 	return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
2369f988b79SJean-Baptiste Boric }
2379f988b79SJean-Baptiste Boric 
2389f988b79SJean-Baptiste Boric /*
2399f988b79SJean-Baptiste Boric  * Implement the cylinder overflow algorithm.
2409f988b79SJean-Baptiste Boric  *
2419f988b79SJean-Baptiste Boric  * The policy implemented by this algorithm is:
2429f988b79SJean-Baptiste Boric  *   1) allocate the block in its requested cylinder group.
2439f988b79SJean-Baptiste Boric  *   2) quadradically rehash on the cylinder group number.
2449f988b79SJean-Baptiste Boric  *   3) brute force search for a free block.
2459f988b79SJean-Baptiste Boric  *
2469f988b79SJean-Baptiste Boric  * `size':	size for data blocks, mode for inodes
2479f988b79SJean-Baptiste Boric  */
2489f988b79SJean-Baptiste Boric /*VARARGS5*/
2499f988b79SJean-Baptiste Boric static daddr_t
ffs_hashalloc(struct inode * ip,int cg,daddr_t pref,int size,daddr_t (* allocator)(struct inode *,int,daddr_t,int))2509f988b79SJean-Baptiste Boric ffs_hashalloc(struct inode *ip, int cg, daddr_t pref, int size,
2519f988b79SJean-Baptiste Boric     daddr_t (*allocator)(struct inode *, int, daddr_t, int))
2529f988b79SJean-Baptiste Boric {
2539f988b79SJean-Baptiste Boric 	struct fs *fs;
2549f988b79SJean-Baptiste Boric 	daddr_t result;
2559f988b79SJean-Baptiste Boric 	int i, icg = cg;
2569f988b79SJean-Baptiste Boric 
2579f988b79SJean-Baptiste Boric 	fs = ip->i_fs;
2589f988b79SJean-Baptiste Boric 	/*
2599f988b79SJean-Baptiste Boric 	 * 1: preferred cylinder group
2609f988b79SJean-Baptiste Boric 	 */
2619f988b79SJean-Baptiste Boric 	result = (*allocator)(ip, cg, pref, size);
2629f988b79SJean-Baptiste Boric 	if (result)
2639f988b79SJean-Baptiste Boric 		return (result);
2649f988b79SJean-Baptiste Boric 	/*
2659f988b79SJean-Baptiste Boric 	 * 2: quadratic rehash
2669f988b79SJean-Baptiste Boric 	 */
2679f988b79SJean-Baptiste Boric 	for (i = 1; i < fs->fs_ncg; i *= 2) {
2689f988b79SJean-Baptiste Boric 		cg += i;
2699f988b79SJean-Baptiste Boric 		if (cg >= fs->fs_ncg)
2709f988b79SJean-Baptiste Boric 			cg -= fs->fs_ncg;
2719f988b79SJean-Baptiste Boric 		result = (*allocator)(ip, cg, 0, size);
2729f988b79SJean-Baptiste Boric 		if (result)
2739f988b79SJean-Baptiste Boric 			return (result);
2749f988b79SJean-Baptiste Boric 	}
2759f988b79SJean-Baptiste Boric 	/*
2769f988b79SJean-Baptiste Boric 	 * 3: brute force search
2779f988b79SJean-Baptiste Boric 	 * Note that we start at i == 2, since 0 was checked initially,
2789f988b79SJean-Baptiste Boric 	 * and 1 is always checked in the quadratic rehash.
2799f988b79SJean-Baptiste Boric 	 */
2809f988b79SJean-Baptiste Boric 	cg = (icg + 2) % fs->fs_ncg;
2819f988b79SJean-Baptiste Boric 	for (i = 2; i < fs->fs_ncg; i++) {
2829f988b79SJean-Baptiste Boric 		result = (*allocator)(ip, cg, 0, size);
2839f988b79SJean-Baptiste Boric 		if (result)
2849f988b79SJean-Baptiste Boric 			return (result);
2859f988b79SJean-Baptiste Boric 		cg++;
2869f988b79SJean-Baptiste Boric 		if (cg == fs->fs_ncg)
2879f988b79SJean-Baptiste Boric 			cg = 0;
2889f988b79SJean-Baptiste Boric 	}
2899f988b79SJean-Baptiste Boric 	return (0);
2909f988b79SJean-Baptiste Boric }
2919f988b79SJean-Baptiste Boric 
2929f988b79SJean-Baptiste Boric /*
2939f988b79SJean-Baptiste Boric  * Determine whether a block can be allocated.
2949f988b79SJean-Baptiste Boric  *
2959f988b79SJean-Baptiste Boric  * Check to see if a block of the appropriate size is available,
2969f988b79SJean-Baptiste Boric  * and if it is, allocate it.
2979f988b79SJean-Baptiste Boric  */
2989f988b79SJean-Baptiste Boric static daddr_t
ffs_alloccg(struct inode * ip,int cg,daddr_t bpref,int size)2999f988b79SJean-Baptiste Boric ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
3009f988b79SJean-Baptiste Boric {
3019f988b79SJean-Baptiste Boric 	struct cg *cgp;
3029f988b79SJean-Baptiste Boric 	struct buf *bp;
3039f988b79SJean-Baptiste Boric 	daddr_t bno, blkno;
3049f988b79SJean-Baptiste Boric 	int error, frags, allocsiz, i;
3059f988b79SJean-Baptiste Boric 	struct fs *fs = ip->i_fs;
3069f988b79SJean-Baptiste Boric 	const int needswap = UFS_FSNEEDSWAP(fs);
3079f988b79SJean-Baptiste Boric 
3089f988b79SJean-Baptiste Boric 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
3099f988b79SJean-Baptiste Boric 		return (0);
3109f988b79SJean-Baptiste Boric 	error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
311*0a6a1f1dSLionel Sambuc 	    (int)fs->fs_cgsize, 0, &bp);
3129f988b79SJean-Baptiste Boric 	if (error) {
3139f988b79SJean-Baptiste Boric 		return (0);
3149f988b79SJean-Baptiste Boric 	}
3159f988b79SJean-Baptiste Boric 	cgp = (struct cg *)bp->b_data;
3169f988b79SJean-Baptiste Boric 	if (!cg_chkmagic(cgp, needswap) ||
3179f988b79SJean-Baptiste Boric 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
3189f988b79SJean-Baptiste Boric 		brelse(bp, 0);
3199f988b79SJean-Baptiste Boric 		return (0);
3209f988b79SJean-Baptiste Boric 	}
3219f988b79SJean-Baptiste Boric 	if (size == fs->fs_bsize) {
3229f988b79SJean-Baptiste Boric 		bno = ffs_alloccgblk(ip, bp, bpref);
3239f988b79SJean-Baptiste Boric 		bwrite(bp);
3249f988b79SJean-Baptiste Boric 		return (bno);
3259f988b79SJean-Baptiste Boric 	}
3269f988b79SJean-Baptiste Boric 	/*
3279f988b79SJean-Baptiste Boric 	 * check to see if any fragments are already available
3289f988b79SJean-Baptiste Boric 	 * allocsiz is the size which will be allocated, hacking
3299f988b79SJean-Baptiste Boric 	 * it down to a smaller size if necessary
3309f988b79SJean-Baptiste Boric 	 */
3319f988b79SJean-Baptiste Boric 	frags = ffs_numfrags(fs, size);
3329f988b79SJean-Baptiste Boric 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
3339f988b79SJean-Baptiste Boric 		if (cgp->cg_frsum[allocsiz] != 0)
3349f988b79SJean-Baptiste Boric 			break;
3359f988b79SJean-Baptiste Boric 	if (allocsiz == fs->fs_frag) {
3369f988b79SJean-Baptiste Boric 		/*
3379f988b79SJean-Baptiste Boric 		 * no fragments were available, so a block will be
3389f988b79SJean-Baptiste Boric 		 * allocated, and hacked up
3399f988b79SJean-Baptiste Boric 		 */
3409f988b79SJean-Baptiste Boric 		if (cgp->cg_cs.cs_nbfree == 0) {
3419f988b79SJean-Baptiste Boric 			brelse(bp, 0);
3429f988b79SJean-Baptiste Boric 			return (0);
3439f988b79SJean-Baptiste Boric 		}
3449f988b79SJean-Baptiste Boric 		bno = ffs_alloccgblk(ip, bp, bpref);
3459f988b79SJean-Baptiste Boric 		bpref = dtogd(fs, bno);
3469f988b79SJean-Baptiste Boric 		for (i = frags; i < fs->fs_frag; i++)
3479f988b79SJean-Baptiste Boric 			setbit(cg_blksfree(cgp, needswap), bpref + i);
3489f988b79SJean-Baptiste Boric 		i = fs->fs_frag - frags;
3499f988b79SJean-Baptiste Boric 		ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
3509f988b79SJean-Baptiste Boric 		fs->fs_cstotal.cs_nffree += i;
3519f988b79SJean-Baptiste Boric 		fs->fs_cs(fs, cg).cs_nffree += i;
3529f988b79SJean-Baptiste Boric 		fs->fs_fmod = 1;
3539f988b79SJean-Baptiste Boric 		ufs_add32(cgp->cg_frsum[i], 1, needswap);
3549f988b79SJean-Baptiste Boric 		bdwrite(bp);
3559f988b79SJean-Baptiste Boric 		return (bno);
3569f988b79SJean-Baptiste Boric 	}
3579f988b79SJean-Baptiste Boric 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
3589f988b79SJean-Baptiste Boric 	for (i = 0; i < frags; i++)
3599f988b79SJean-Baptiste Boric 		clrbit(cg_blksfree(cgp, needswap), bno + i);
3609f988b79SJean-Baptiste Boric 	ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
3619f988b79SJean-Baptiste Boric 	fs->fs_cstotal.cs_nffree -= frags;
3629f988b79SJean-Baptiste Boric 	fs->fs_cs(fs, cg).cs_nffree -= frags;
3639f988b79SJean-Baptiste Boric 	fs->fs_fmod = 1;
3649f988b79SJean-Baptiste Boric 	ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
3659f988b79SJean-Baptiste Boric 	if (frags != allocsiz)
3669f988b79SJean-Baptiste Boric 		ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
3679f988b79SJean-Baptiste Boric 	blkno = cg * fs->fs_fpg + bno;
3689f988b79SJean-Baptiste Boric 	bdwrite(bp);
3699f988b79SJean-Baptiste Boric 	return blkno;
3709f988b79SJean-Baptiste Boric }
3719f988b79SJean-Baptiste Boric 
3729f988b79SJean-Baptiste Boric /*
3739f988b79SJean-Baptiste Boric  * Allocate a block in a cylinder group.
3749f988b79SJean-Baptiste Boric  *
3759f988b79SJean-Baptiste Boric  * This algorithm implements the following policy:
3769f988b79SJean-Baptiste Boric  *   1) allocate the requested block.
3779f988b79SJean-Baptiste Boric  *   2) allocate a rotationally optimal block in the same cylinder.
3789f988b79SJean-Baptiste Boric  *   3) allocate the next available block on the block rotor for the
3799f988b79SJean-Baptiste Boric  *      specified cylinder group.
3809f988b79SJean-Baptiste Boric  * Note that this routine only allocates fs_bsize blocks; these
3819f988b79SJean-Baptiste Boric  * blocks may be fragmented by the routine that allocates them.
3829f988b79SJean-Baptiste Boric  */
3839f988b79SJean-Baptiste Boric static daddr_t
ffs_alloccgblk(struct inode * ip,struct buf * bp,daddr_t bpref)3849f988b79SJean-Baptiste Boric ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref)
3859f988b79SJean-Baptiste Boric {
3869f988b79SJean-Baptiste Boric 	struct cg *cgp;
3879f988b79SJean-Baptiste Boric 	daddr_t blkno;
3889f988b79SJean-Baptiste Boric 	int32_t bno;
3899f988b79SJean-Baptiste Boric 	struct fs *fs = ip->i_fs;
3909f988b79SJean-Baptiste Boric 	const int needswap = UFS_FSNEEDSWAP(fs);
3919f988b79SJean-Baptiste Boric 	u_int8_t *blksfree;
3929f988b79SJean-Baptiste Boric 
3939f988b79SJean-Baptiste Boric 	cgp = (struct cg *)bp->b_data;
3949f988b79SJean-Baptiste Boric 	blksfree = cg_blksfree(cgp, needswap);
3959f988b79SJean-Baptiste Boric 	if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
3969f988b79SJean-Baptiste Boric 		bpref = ufs_rw32(cgp->cg_rotor, needswap);
3979f988b79SJean-Baptiste Boric 	} else {
3989f988b79SJean-Baptiste Boric 		bpref = ffs_blknum(fs, bpref);
3999f988b79SJean-Baptiste Boric 		bno = dtogd(fs, bpref);
4009f988b79SJean-Baptiste Boric 		/*
4019f988b79SJean-Baptiste Boric 		 * if the requested block is available, use it
4029f988b79SJean-Baptiste Boric 		 */
4039f988b79SJean-Baptiste Boric 		if (ffs_isblock(fs, blksfree, ffs_fragstoblks(fs, bno)))
4049f988b79SJean-Baptiste Boric 			goto gotit;
4059f988b79SJean-Baptiste Boric 	}
4069f988b79SJean-Baptiste Boric 	/*
4079f988b79SJean-Baptiste Boric 	 * Take the next available one in this cylinder group.
4089f988b79SJean-Baptiste Boric 	 */
4099f988b79SJean-Baptiste Boric 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
4109f988b79SJean-Baptiste Boric 	if (bno < 0)
4119f988b79SJean-Baptiste Boric 		return (0);
4129f988b79SJean-Baptiste Boric 	cgp->cg_rotor = ufs_rw32(bno, needswap);
4139f988b79SJean-Baptiste Boric gotit:
4149f988b79SJean-Baptiste Boric 	blkno = ffs_fragstoblks(fs, bno);
4159f988b79SJean-Baptiste Boric 	ffs_clrblock(fs, blksfree, (long)blkno);
4169f988b79SJean-Baptiste Boric 	ffs_clusteracct(fs, cgp, blkno, -1);
4179f988b79SJean-Baptiste Boric 	ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
4189f988b79SJean-Baptiste Boric 	fs->fs_cstotal.cs_nbfree--;
4199f988b79SJean-Baptiste Boric 	fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
4209f988b79SJean-Baptiste Boric 	fs->fs_fmod = 1;
4219f988b79SJean-Baptiste Boric 	blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
4229f988b79SJean-Baptiste Boric 	return (blkno);
4239f988b79SJean-Baptiste Boric }
4249f988b79SJean-Baptiste Boric 
4259f988b79SJean-Baptiste Boric /*
4269f988b79SJean-Baptiste Boric  * Free a block or fragment.
4279f988b79SJean-Baptiste Boric  *
4289f988b79SJean-Baptiste Boric  * The specified block or fragment is placed back in the
4299f988b79SJean-Baptiste Boric  * free map. If a fragment is deallocated, a possible
4309f988b79SJean-Baptiste Boric  * block reassembly is checked.
4319f988b79SJean-Baptiste Boric  */
4329f988b79SJean-Baptiste Boric void
ffs_blkfree(struct inode * ip,daddr_t bno,long size)4339f988b79SJean-Baptiste Boric ffs_blkfree(struct inode *ip, daddr_t bno, long size)
4349f988b79SJean-Baptiste Boric {
4359f988b79SJean-Baptiste Boric 	struct cg *cgp;
4369f988b79SJean-Baptiste Boric 	struct buf *bp;
4379f988b79SJean-Baptiste Boric 	int32_t fragno, cgbno;
4389f988b79SJean-Baptiste Boric 	int i, error, cg, blk, frags, bbase;
4399f988b79SJean-Baptiste Boric 	struct fs *fs = ip->i_fs;
4409f988b79SJean-Baptiste Boric 	const int needswap = UFS_FSNEEDSWAP(fs);
4419f988b79SJean-Baptiste Boric 
4429f988b79SJean-Baptiste Boric 	if (size > fs->fs_bsize || ffs_fragoff(fs, size) != 0 ||
4439f988b79SJean-Baptiste Boric 	    ffs_fragnum(fs, bno) + ffs_numfrags(fs, size) > fs->fs_frag) {
4449f988b79SJean-Baptiste Boric 		errx(1, "blkfree: bad size: bno %lld bsize %d size %ld",
4459f988b79SJean-Baptiste Boric 		    (long long)bno, fs->fs_bsize, size);
4469f988b79SJean-Baptiste Boric 	}
4479f988b79SJean-Baptiste Boric 	cg = dtog(fs, bno);
4489f988b79SJean-Baptiste Boric 	if (bno >= fs->fs_size) {
4499f988b79SJean-Baptiste Boric 		warnx("bad block %lld, ino %llu", (long long)bno,
4509f988b79SJean-Baptiste Boric 		    (unsigned long long)ip->i_number);
4519f988b79SJean-Baptiste Boric 		return;
4529f988b79SJean-Baptiste Boric 	}
4539f988b79SJean-Baptiste Boric 	error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
454*0a6a1f1dSLionel Sambuc 	    (int)fs->fs_cgsize, 0, &bp);
4559f988b79SJean-Baptiste Boric 	if (error) {
4569f988b79SJean-Baptiste Boric 		brelse(bp, 0);
4579f988b79SJean-Baptiste Boric 		return;
4589f988b79SJean-Baptiste Boric 	}
4599f988b79SJean-Baptiste Boric 	cgp = (struct cg *)bp->b_data;
4609f988b79SJean-Baptiste Boric 	if (!cg_chkmagic(cgp, needswap)) {
4619f988b79SJean-Baptiste Boric 		brelse(bp, 0);
4629f988b79SJean-Baptiste Boric 		return;
4639f988b79SJean-Baptiste Boric 	}
4649f988b79SJean-Baptiste Boric 	cgbno = dtogd(fs, bno);
4659f988b79SJean-Baptiste Boric 	if (size == fs->fs_bsize) {
4669f988b79SJean-Baptiste Boric 		fragno = ffs_fragstoblks(fs, cgbno);
4679f988b79SJean-Baptiste Boric 		if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), fragno)) {
4689f988b79SJean-Baptiste Boric 			errx(1, "blkfree: freeing free block %lld",
4699f988b79SJean-Baptiste Boric 			    (long long)bno);
4709f988b79SJean-Baptiste Boric 		}
4719f988b79SJean-Baptiste Boric 		ffs_setblock(fs, cg_blksfree(cgp, needswap), fragno);
4729f988b79SJean-Baptiste Boric 		ffs_clusteracct(fs, cgp, fragno, 1);
4739f988b79SJean-Baptiste Boric 		ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
4749f988b79SJean-Baptiste Boric 		fs->fs_cstotal.cs_nbfree++;
4759f988b79SJean-Baptiste Boric 		fs->fs_cs(fs, cg).cs_nbfree++;
4769f988b79SJean-Baptiste Boric 	} else {
4779f988b79SJean-Baptiste Boric 		bbase = cgbno - ffs_fragnum(fs, cgbno);
4789f988b79SJean-Baptiste Boric 		/*
4799f988b79SJean-Baptiste Boric 		 * decrement the counts associated with the old frags
4809f988b79SJean-Baptiste Boric 		 */
4819f988b79SJean-Baptiste Boric 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
4829f988b79SJean-Baptiste Boric 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
4839f988b79SJean-Baptiste Boric 		/*
4849f988b79SJean-Baptiste Boric 		 * deallocate the fragment
4859f988b79SJean-Baptiste Boric 		 */
4869f988b79SJean-Baptiste Boric 		frags = ffs_numfrags(fs, size);
4879f988b79SJean-Baptiste Boric 		for (i = 0; i < frags; i++) {
4889f988b79SJean-Baptiste Boric 			if (isset(cg_blksfree(cgp, needswap), cgbno + i)) {
4899f988b79SJean-Baptiste Boric 				errx(1, "blkfree: freeing free frag: block %lld",
4909f988b79SJean-Baptiste Boric 				    (long long)(cgbno + i));
4919f988b79SJean-Baptiste Boric 			}
4929f988b79SJean-Baptiste Boric 			setbit(cg_blksfree(cgp, needswap), cgbno + i);
4939f988b79SJean-Baptiste Boric 		}
4949f988b79SJean-Baptiste Boric 		ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
4959f988b79SJean-Baptiste Boric 		fs->fs_cstotal.cs_nffree += i;
4969f988b79SJean-Baptiste Boric 		fs->fs_cs(fs, cg).cs_nffree += i;
4979f988b79SJean-Baptiste Boric 		/*
4989f988b79SJean-Baptiste Boric 		 * add back in counts associated with the new frags
4999f988b79SJean-Baptiste Boric 		 */
5009f988b79SJean-Baptiste Boric 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
5019f988b79SJean-Baptiste Boric 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
5029f988b79SJean-Baptiste Boric 		/*
5039f988b79SJean-Baptiste Boric 		 * if a complete block has been reassembled, account for it
5049f988b79SJean-Baptiste Boric 		 */
5059f988b79SJean-Baptiste Boric 		fragno = ffs_fragstoblks(fs, bbase);
5069f988b79SJean-Baptiste Boric 		if (ffs_isblock(fs, cg_blksfree(cgp, needswap), fragno)) {
5079f988b79SJean-Baptiste Boric 			ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
5089f988b79SJean-Baptiste Boric 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
5099f988b79SJean-Baptiste Boric 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
5109f988b79SJean-Baptiste Boric 			ffs_clusteracct(fs, cgp, fragno, 1);
5119f988b79SJean-Baptiste Boric 			ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
5129f988b79SJean-Baptiste Boric 			fs->fs_cstotal.cs_nbfree++;
5139f988b79SJean-Baptiste Boric 			fs->fs_cs(fs, cg).cs_nbfree++;
5149f988b79SJean-Baptiste Boric 		}
5159f988b79SJean-Baptiste Boric 	}
5169f988b79SJean-Baptiste Boric 	fs->fs_fmod = 1;
5179f988b79SJean-Baptiste Boric 	bdwrite(bp);
5189f988b79SJean-Baptiste Boric }
5199f988b79SJean-Baptiste Boric 
5209f988b79SJean-Baptiste Boric 
5219f988b79SJean-Baptiste Boric static int
scanc(u_int size,const u_char * cp,const u_char table[],int mask)5229f988b79SJean-Baptiste Boric scanc(u_int size, const u_char *cp, const u_char table[], int mask)
5239f988b79SJean-Baptiste Boric {
5249f988b79SJean-Baptiste Boric 	const u_char *end = &cp[size];
5259f988b79SJean-Baptiste Boric 
5269f988b79SJean-Baptiste Boric 	while (cp < end && (table[*cp] & mask) == 0)
5279f988b79SJean-Baptiste Boric 		cp++;
5289f988b79SJean-Baptiste Boric 	return (end - cp);
5299f988b79SJean-Baptiste Boric }
5309f988b79SJean-Baptiste Boric 
5319f988b79SJean-Baptiste Boric /*
5329f988b79SJean-Baptiste Boric  * Find a block of the specified size in the specified cylinder group.
5339f988b79SJean-Baptiste Boric  *
5349f988b79SJean-Baptiste Boric  * It is a panic if a request is made to find a block if none are
5359f988b79SJean-Baptiste Boric  * available.
5369f988b79SJean-Baptiste Boric  */
5379f988b79SJean-Baptiste Boric static int32_t
ffs_mapsearch(struct fs * fs,struct cg * cgp,daddr_t bpref,int allocsiz)5389f988b79SJean-Baptiste Boric ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
5399f988b79SJean-Baptiste Boric {
5409f988b79SJean-Baptiste Boric 	int32_t bno;
5419f988b79SJean-Baptiste Boric 	int start, len, loc, i;
5429f988b79SJean-Baptiste Boric 	int blk, field, subfield, pos;
5439f988b79SJean-Baptiste Boric 	int ostart, olen;
5449f988b79SJean-Baptiste Boric 	const int needswap = UFS_FSNEEDSWAP(fs);
5459f988b79SJean-Baptiste Boric 
5469f988b79SJean-Baptiste Boric 	/*
5479f988b79SJean-Baptiste Boric 	 * find the fragment by searching through the free block
5489f988b79SJean-Baptiste Boric 	 * map for an appropriate bit pattern
5499f988b79SJean-Baptiste Boric 	 */
5509f988b79SJean-Baptiste Boric 	if (bpref)
5519f988b79SJean-Baptiste Boric 		start = dtogd(fs, bpref) / NBBY;
5529f988b79SJean-Baptiste Boric 	else
5539f988b79SJean-Baptiste Boric 		start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
5549f988b79SJean-Baptiste Boric 	len = howmany(fs->fs_fpg, NBBY) - start;
5559f988b79SJean-Baptiste Boric 	ostart = start;
5569f988b79SJean-Baptiste Boric 	olen = len;
5579f988b79SJean-Baptiste Boric 	loc = scanc((u_int)len,
5589f988b79SJean-Baptiste Boric 		(const u_char *)&cg_blksfree(cgp, needswap)[start],
5599f988b79SJean-Baptiste Boric 		(const u_char *)fragtbl[fs->fs_frag],
5609f988b79SJean-Baptiste Boric 		(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
5619f988b79SJean-Baptiste Boric 	if (loc == 0) {
5629f988b79SJean-Baptiste Boric 		len = start + 1;
5639f988b79SJean-Baptiste Boric 		start = 0;
5649f988b79SJean-Baptiste Boric 		loc = scanc((u_int)len,
5659f988b79SJean-Baptiste Boric 			(const u_char *)&cg_blksfree(cgp, needswap)[0],
5669f988b79SJean-Baptiste Boric 			(const u_char *)fragtbl[fs->fs_frag],
5679f988b79SJean-Baptiste Boric 			(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
5689f988b79SJean-Baptiste Boric 		if (loc == 0) {
5699f988b79SJean-Baptiste Boric 			errx(1,
5709f988b79SJean-Baptiste Boric     "ffs_alloccg: map corrupted: start %d len %d offset %d %ld",
5719f988b79SJean-Baptiste Boric 				ostart, olen,
5729f988b79SJean-Baptiste Boric 				ufs_rw32(cgp->cg_freeoff, needswap),
5739f988b79SJean-Baptiste Boric 				(long)cg_blksfree(cgp, needswap) - (long)cgp);
5749f988b79SJean-Baptiste Boric 			/* NOTREACHED */
5759f988b79SJean-Baptiste Boric 		}
5769f988b79SJean-Baptiste Boric 	}
5779f988b79SJean-Baptiste Boric 	bno = (start + len - loc) * NBBY;
5789f988b79SJean-Baptiste Boric 	cgp->cg_frotor = ufs_rw32(bno, needswap);
5799f988b79SJean-Baptiste Boric 	/*
5809f988b79SJean-Baptiste Boric 	 * found the byte in the map
5819f988b79SJean-Baptiste Boric 	 * sift through the bits to find the selected frag
5829f988b79SJean-Baptiste Boric 	 */
5839f988b79SJean-Baptiste Boric 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
5849f988b79SJean-Baptiste Boric 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bno);
5859f988b79SJean-Baptiste Boric 		blk <<= 1;
5869f988b79SJean-Baptiste Boric 		field = around[allocsiz];
5879f988b79SJean-Baptiste Boric 		subfield = inside[allocsiz];
5889f988b79SJean-Baptiste Boric 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
5899f988b79SJean-Baptiste Boric 			if ((blk & field) == subfield)
5909f988b79SJean-Baptiste Boric 				return (bno + pos);
5919f988b79SJean-Baptiste Boric 			field <<= 1;
5929f988b79SJean-Baptiste Boric 			subfield <<= 1;
5939f988b79SJean-Baptiste Boric 		}
5949f988b79SJean-Baptiste Boric 	}
5959f988b79SJean-Baptiste Boric 	errx(1, "ffs_alloccg: block not in map: bno %lld", (long long)bno);
5969f988b79SJean-Baptiste Boric 	return (-1);
5979f988b79SJean-Baptiste Boric }
598