xref: /netbsd-src/usr.sbin/makefs/ffs/ffs_alloc.c (revision 6839df46b47b8abb26becca6066e8797de3b5b55)
1*6839df46Schristos /*	$NetBSD: ffs_alloc.c,v 1.32 2023/03/13 22:17:24 christos Exp $	*/
26325773eSlukem /* From: NetBSD: ffs_alloc.c,v 1.50 2001/09/06 02:16:01 lukem Exp */
36325773eSlukem 
46325773eSlukem /*
542614ed3Sfvdl  * Copyright (c) 2002 Networks Associates Technology, Inc.
642614ed3Sfvdl  * All rights reserved.
742614ed3Sfvdl  *
842614ed3Sfvdl  * This software was developed for the FreeBSD Project by Marshall
942614ed3Sfvdl  * Kirk McKusick and Network Associates Laboratories, the Security
1042614ed3Sfvdl  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
1142614ed3Sfvdl  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
1242614ed3Sfvdl  * research program
1342614ed3Sfvdl  *
146325773eSlukem  * Copyright (c) 1982, 1986, 1989, 1993
156325773eSlukem  *	The Regents of the University of California.  All rights reserved.
166325773eSlukem  *
176325773eSlukem  * Redistribution and use in source and binary forms, with or without
186325773eSlukem  * modification, are permitted provided that the following conditions
196325773eSlukem  * are met:
206325773eSlukem  * 1. Redistributions of source code must retain the above copyright
216325773eSlukem  *    notice, this list of conditions and the following disclaimer.
226325773eSlukem  * 2. Redistributions in binary form must reproduce the above copyright
236325773eSlukem  *    notice, this list of conditions and the following disclaimer in the
246325773eSlukem  *    documentation and/or other materials provided with the distribution.
25326b2259Sagc  * 3. Neither the name of the University nor the names of its contributors
266325773eSlukem  *    may be used to endorse or promote products derived from this software
276325773eSlukem  *    without specific prior written permission.
286325773eSlukem  *
296325773eSlukem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
306325773eSlukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
316325773eSlukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
326325773eSlukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
336325773eSlukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
346325773eSlukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
356325773eSlukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
366325773eSlukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
376325773eSlukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
386325773eSlukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
396325773eSlukem  * SUCH DAMAGE.
406325773eSlukem  *
416325773eSlukem  *	@(#)ffs_alloc.c	8.19 (Berkeley) 7/13/95
426325773eSlukem  */
436325773eSlukem 
44b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H
45b2f78261Sjmc #include "nbtool_config.h"
46b2f78261Sjmc #endif
47b2f78261Sjmc 
48cafb53fcSlukem #include <sys/cdefs.h>
499fbd8888Stv #if defined(__RCSID) && !defined(__lint)
50*6839df46Schristos __RCSID("$NetBSD: ffs_alloc.c,v 1.32 2023/03/13 22:17:24 christos Exp $");
51cafb53fcSlukem #endif	/* !__lint */
52cafb53fcSlukem 
536325773eSlukem #include <sys/param.h>
546325773eSlukem #include <sys/time.h>
556325773eSlukem 
566325773eSlukem #include <errno.h>
576325773eSlukem 
58d74b2fc0Slukem #include "makefs.h"
59d74b2fc0Slukem 
603d765797Slukem #include <ufs/ufs/dinode.h>
61557afc60Slukem #include <ufs/ufs/ufs_bswap.h>
62557afc60Slukem #include <ufs/ffs/fs.h>
636325773eSlukem 
646325773eSlukem #include "ffs/buf.h"
65944794a5Slukem #include "ffs/ufs_inode.h"
666325773eSlukem #include "ffs/ffs_extern.h"
676325773eSlukem 
686325773eSlukem 
696325773eSlukem static int scanc(u_int, const u_char *, const u_char *, int);
706325773eSlukem 
71a3ff3a30Sfvdl static daddr_t ffs_alloccg(struct inode *, int, daddr_t, int);
72a3ff3a30Sfvdl static daddr_t ffs_alloccgblk(struct inode *, struct buf *, daddr_t);
73f298a94bSchs static daddr_t ffs_hashalloc(struct inode *, uint32_t, daddr_t, int,
74a3ff3a30Sfvdl 		     daddr_t (*)(struct inode *, int, daddr_t, int));
7542614ed3Sfvdl static int32_t ffs_mapsearch(struct fs *, struct cg *, daddr_t, int);
766325773eSlukem 
776325773eSlukem /* in ffs_tables.c */
786325773eSlukem extern const int inside[], around[];
796325773eSlukem extern const u_char * const fragtbl[];
806325773eSlukem 
816325773eSlukem /*
826325773eSlukem  * Allocate a block in the file system.
836325773eSlukem  *
846325773eSlukem  * The size of the requested block is given, which must be some
856325773eSlukem  * multiple of fs_fsize and <= fs_bsize.
866325773eSlukem  * A preference may be optionally specified. If a preference is given
876325773eSlukem  * the following hierarchy is used to allocate a block:
886325773eSlukem  *   1) allocate the requested block.
896325773eSlukem  *   2) allocate a rotationally optimal block in the same cylinder.
906325773eSlukem  *   3) allocate a block in the same cylinder group.
916325773eSlukem  *   4) quadradically rehash into other cylinder groups, until an
926325773eSlukem  *      available block is located.
936325773eSlukem  * If no block preference is given the following hierarchy is used
946325773eSlukem  * to allocate a block:
956325773eSlukem  *   1) allocate a block in the cylinder group that contains the
966325773eSlukem  *      inode for the file.
976325773eSlukem  *   2) quadradically rehash into other cylinder groups, until an
986325773eSlukem  *      available block is located.
996325773eSlukem  */
1006325773eSlukem int
ffs_alloc(struct inode * ip,daddr_t lbn __unused,daddr_t bpref,int size,daddr_t * bnp)1019450025aSchristos ffs_alloc(struct inode *ip, daddr_t lbn __unused, daddr_t bpref, int size,
102a3ff3a30Sfvdl     daddr_t *bnp)
1036325773eSlukem {
1046325773eSlukem 	struct fs *fs = ip->i_fs;
105a3ff3a30Sfvdl 	daddr_t bno;
1066325773eSlukem 	int cg;
1076325773eSlukem 
1086325773eSlukem 	*bnp = 0;
109f1333577Sdholland 	if (size > fs->fs_bsize || ffs_fragoff(fs, size) != 0) {
1106125bf59Schristos 		errx(EXIT_FAILURE, "%s: bad size: bsize %d size %d", __func__,
1116325773eSlukem 		    fs->fs_bsize, size);
1126325773eSlukem 	}
1136325773eSlukem 	if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
1146325773eSlukem 		goto nospace;
1156325773eSlukem 	if (bpref >= fs->fs_size)
1166325773eSlukem 		bpref = 0;
1176325773eSlukem 	if (bpref == 0)
1186325773eSlukem 		cg = ino_to_cg(fs, ip->i_number);
1196325773eSlukem 	else
1206325773eSlukem 		cg = dtog(fs, bpref);
121a3ff3a30Sfvdl 	bno = ffs_hashalloc(ip, cg, bpref, size, ffs_alloccg);
1226325773eSlukem 	if (bno > 0) {
123b14624e5Sfvdl 		DIP_ADD(ip, blocks, size / DEV_BSIZE);
1246325773eSlukem 		*bnp = bno;
1256325773eSlukem 		return (0);
1266325773eSlukem 	}
1276325773eSlukem nospace:
1286325773eSlukem 	return (ENOSPC);
1296325773eSlukem }
1306325773eSlukem 
1316325773eSlukem /*
1326325773eSlukem  * Select the desired position for the next block in a file.  The file is
1336325773eSlukem  * logically divided into sections. The first section is composed of the
1346325773eSlukem  * direct blocks. Each additional section contains fs_maxbpg blocks.
1356325773eSlukem  *
1366325773eSlukem  * If no blocks have been allocated in the first section, the policy is to
1376325773eSlukem  * request a block in the same cylinder group as the inode that describes
1386325773eSlukem  * the file. If no blocks have been allocated in any other section, the
1396325773eSlukem  * policy is to place the section in a cylinder group with a greater than
1406325773eSlukem  * average number of free blocks.  An appropriate cylinder group is found
1416325773eSlukem  * by using a rotor that sweeps the cylinder groups. When a new group of
1426325773eSlukem  * blocks is needed, the sweep begins in the cylinder group following the
1436325773eSlukem  * cylinder group from which the previous allocation was made. The sweep
1446325773eSlukem  * continues until a cylinder group with greater than the average number
1456325773eSlukem  * of free blocks is found. If the allocation is for the first block in an
1466325773eSlukem  * indirect block, the information on the previous allocation is unavailable;
1476325773eSlukem  * here a best guess is made based upon the logical block number being
1486325773eSlukem  * allocated.
1496325773eSlukem  *
1506325773eSlukem  * If a section is already partially allocated, the policy is to
1516325773eSlukem  * contiguously allocate fs_maxcontig blocks.  The end of one of these
1526325773eSlukem  * contiguous blocks and the beginning of the next is physically separated
1536325773eSlukem  * so that the disk head will be in transit between them for at least
1546325773eSlukem  * fs_rotdelay milliseconds.  This is to allow time for the processor to
1556325773eSlukem  * schedule another I/O transfer.
1566325773eSlukem  */
157a3ff3a30Sfvdl /* XXX ondisk32 */
158a3ff3a30Sfvdl daddr_t
ffs_blkpref_ufs1(struct inode * ip,daddr_t lbn,int indx,int32_t * bap)15942614ed3Sfvdl ffs_blkpref_ufs1(struct inode *ip, daddr_t lbn, int indx, int32_t *bap)
1606325773eSlukem {
1616325773eSlukem 	struct fs *fs;
162f298a94bSchs 	uint32_t cg, startcg;
163f298a94bSchs 	int avgbfree;
1646325773eSlukem 
1656325773eSlukem 	fs = ip->i_fs;
1666325773eSlukem 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
167f1333577Sdholland 		if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
1686325773eSlukem 			cg = ino_to_cg(fs, ip->i_number);
1696325773eSlukem 			return (fs->fs_fpg * cg + fs->fs_frag);
1706325773eSlukem 		}
1716325773eSlukem 		/*
1726325773eSlukem 		 * Find a cylinder with greater than average number of
1736325773eSlukem 		 * unused data blocks.
1746325773eSlukem 		 */
1756325773eSlukem 		if (indx == 0 || bap[indx - 1] == 0)
1766325773eSlukem 			startcg =
1776325773eSlukem 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
1786325773eSlukem 		else
1796325773eSlukem 			startcg = dtog(fs,
1806325773eSlukem 				ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
1816325773eSlukem 		startcg %= fs->fs_ncg;
1826325773eSlukem 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
1836325773eSlukem 		for (cg = startcg; cg < fs->fs_ncg; cg++)
18442614ed3Sfvdl 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
18542614ed3Sfvdl 				return (fs->fs_fpg * cg + fs->fs_frag);
18642614ed3Sfvdl 		for (cg = 0; cg <= startcg; cg++)
18742614ed3Sfvdl 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree)
18842614ed3Sfvdl 				return (fs->fs_fpg * cg + fs->fs_frag);
18942614ed3Sfvdl 		return (0);
19042614ed3Sfvdl 	}
19142614ed3Sfvdl 	/*
19242614ed3Sfvdl 	 * We just always try to lay things out contiguously.
19342614ed3Sfvdl 	 */
19442614ed3Sfvdl 	return ufs_rw32(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
19542614ed3Sfvdl }
19642614ed3Sfvdl 
19742614ed3Sfvdl daddr_t
ffs_blkpref_ufs2(struct inode * ip,daddr_t lbn,int indx,int64_t * bap)198b825b96bSchristos ffs_blkpref_ufs2(struct inode *ip, daddr_t lbn, int indx, int64_t *bap)
19942614ed3Sfvdl {
20042614ed3Sfvdl 	struct fs *fs;
201f298a94bSchs 	uint32_t cg, startcg;
202f298a94bSchs 	int avgbfree;
20342614ed3Sfvdl 
20442614ed3Sfvdl 	fs = ip->i_fs;
20542614ed3Sfvdl 	if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
206f1333577Sdholland 		if (lbn < UFS_NDADDR + FFS_NINDIR(fs)) {
20742614ed3Sfvdl 			cg = ino_to_cg(fs, ip->i_number);
2086325773eSlukem 			return (fs->fs_fpg * cg + fs->fs_frag);
2096325773eSlukem 		}
21042614ed3Sfvdl 		/*
21142614ed3Sfvdl 		 * Find a cylinder with greater than average number of
21242614ed3Sfvdl 		 * unused data blocks.
21342614ed3Sfvdl 		 */
21442614ed3Sfvdl 		if (indx == 0 || bap[indx - 1] == 0)
21542614ed3Sfvdl 			startcg =
21642614ed3Sfvdl 			    ino_to_cg(fs, ip->i_number) + lbn / fs->fs_maxbpg;
21742614ed3Sfvdl 		else
21842614ed3Sfvdl 			startcg = dtog(fs,
21942614ed3Sfvdl 				ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + 1);
22042614ed3Sfvdl 		startcg %= fs->fs_ncg;
22142614ed3Sfvdl 		avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
22242614ed3Sfvdl 		for (cg = startcg; cg < fs->fs_ncg; cg++)
2236325773eSlukem 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
22442614ed3Sfvdl 				return (fs->fs_fpg * cg + fs->fs_frag);
22542614ed3Sfvdl 			}
22642614ed3Sfvdl 		for (cg = 0; cg < startcg; cg++)
22742614ed3Sfvdl 			if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
2286325773eSlukem 				return (fs->fs_fpg * cg + fs->fs_frag);
2296325773eSlukem 			}
2306325773eSlukem 		return (0);
2316325773eSlukem 	}
2326325773eSlukem 	/*
23342614ed3Sfvdl 	 * We just always try to lay things out contiguously.
2346325773eSlukem 	 */
23542614ed3Sfvdl 	return ufs_rw64(bap[indx - 1], UFS_FSNEEDSWAP(fs)) + fs->fs_frag;
2366325773eSlukem }
2376325773eSlukem 
2386325773eSlukem /*
2396325773eSlukem  * Implement the cylinder overflow algorithm.
2406325773eSlukem  *
2416325773eSlukem  * The policy implemented by this algorithm is:
2426325773eSlukem  *   1) allocate the block in its requested cylinder group.
2436325773eSlukem  *   2) quadradically rehash on the cylinder group number.
2446325773eSlukem  *   3) brute force search for a free block.
2456325773eSlukem  *
2466325773eSlukem  * `size':	size for data blocks, mode for inodes
2476325773eSlukem  */
2486325773eSlukem /*VARARGS5*/
249a3ff3a30Sfvdl static daddr_t
ffs_hashalloc(struct inode * ip,uint32_t cg,daddr_t pref,int size,daddr_t (* allocator)(struct inode *,int,daddr_t,int))250f298a94bSchs ffs_hashalloc(struct inode *ip, uint32_t cg, daddr_t pref, int size,
251a3ff3a30Sfvdl     daddr_t (*allocator)(struct inode *, int, daddr_t, int))
2526325773eSlukem {
2536325773eSlukem 	struct fs *fs;
254a3ff3a30Sfvdl 	daddr_t result;
255f298a94bSchs 	uint32_t i, icg = cg;
2566325773eSlukem 
2576325773eSlukem 	fs = ip->i_fs;
2586325773eSlukem 	/*
2596325773eSlukem 	 * 1: preferred cylinder group
2606325773eSlukem 	 */
2616325773eSlukem 	result = (*allocator)(ip, cg, pref, size);
2626325773eSlukem 	if (result)
2636325773eSlukem 		return (result);
2646325773eSlukem 	/*
2656325773eSlukem 	 * 2: quadratic rehash
2666325773eSlukem 	 */
2676325773eSlukem 	for (i = 1; i < fs->fs_ncg; i *= 2) {
2686325773eSlukem 		cg += i;
2696325773eSlukem 		if (cg >= fs->fs_ncg)
2706325773eSlukem 			cg -= fs->fs_ncg;
2716325773eSlukem 		result = (*allocator)(ip, cg, 0, size);
2726325773eSlukem 		if (result)
2736325773eSlukem 			return (result);
2746325773eSlukem 	}
2756325773eSlukem 	/*
2766325773eSlukem 	 * 3: brute force search
2776325773eSlukem 	 * Note that we start at i == 2, since 0 was checked initially,
2786325773eSlukem 	 * and 1 is always checked in the quadratic rehash.
2796325773eSlukem 	 */
2806325773eSlukem 	cg = (icg + 2) % fs->fs_ncg;
2816325773eSlukem 	for (i = 2; i < fs->fs_ncg; i++) {
2826325773eSlukem 		result = (*allocator)(ip, cg, 0, size);
2836325773eSlukem 		if (result)
2846325773eSlukem 			return (result);
2856325773eSlukem 		cg++;
2866325773eSlukem 		if (cg == fs->fs_ncg)
2876325773eSlukem 			cg = 0;
2886325773eSlukem 	}
2896325773eSlukem 	return (0);
2906325773eSlukem }
2916325773eSlukem 
2926325773eSlukem /*
2936325773eSlukem  * Determine whether a block can be allocated.
2946325773eSlukem  *
2956325773eSlukem  * Check to see if a block of the appropriate size is available,
2966325773eSlukem  * and if it is, allocate it.
2976325773eSlukem  */
298a3ff3a30Sfvdl static daddr_t
ffs_alloccg(struct inode * ip,int cg,daddr_t bpref,int size)299a3ff3a30Sfvdl ffs_alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
3006325773eSlukem {
3016325773eSlukem 	struct cg *cgp;
3026325773eSlukem 	struct buf *bp;
303a3ff3a30Sfvdl 	daddr_t bno, blkno;
3046325773eSlukem 	int error, frags, allocsiz, i;
3056325773eSlukem 	struct fs *fs = ip->i_fs;
3066325773eSlukem 	const int needswap = UFS_FSNEEDSWAP(fs);
3076325773eSlukem 
3086325773eSlukem 	if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
3096325773eSlukem 		return (0);
3102737439dSdholland 	error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
311aa7e0e4bSagc 	    (int)fs->fs_cgsize, 0, &bp);
3126325773eSlukem 	if (error) {
3136325773eSlukem 		return (0);
3146325773eSlukem 	}
3156325773eSlukem 	cgp = (struct cg *)bp->b_data;
3166325773eSlukem 	if (!cg_chkmagic(cgp, needswap) ||
3176325773eSlukem 	    (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
3189ac350beSchristos 		brelse(bp, 0);
3196325773eSlukem 		return (0);
3206325773eSlukem 	}
3216325773eSlukem 	if (size == fs->fs_bsize) {
3226325773eSlukem 		bno = ffs_alloccgblk(ip, bp, bpref);
323e502c2ffSchristos 		bwrite(bp);
3246325773eSlukem 		return (bno);
3256325773eSlukem 	}
3266325773eSlukem 	/*
3276325773eSlukem 	 * check to see if any fragments are already available
3286325773eSlukem 	 * allocsiz is the size which will be allocated, hacking
3296325773eSlukem 	 * it down to a smaller size if necessary
3306325773eSlukem 	 */
331e1610ba4Sdholland 	frags = ffs_numfrags(fs, size);
3326325773eSlukem 	for (allocsiz = frags; allocsiz < fs->fs_frag; allocsiz++)
3336325773eSlukem 		if (cgp->cg_frsum[allocsiz] != 0)
3346325773eSlukem 			break;
3356325773eSlukem 	if (allocsiz == fs->fs_frag) {
3366325773eSlukem 		/*
3376325773eSlukem 		 * no fragments were available, so a block will be
3386325773eSlukem 		 * allocated, and hacked up
3396325773eSlukem 		 */
3406325773eSlukem 		if (cgp->cg_cs.cs_nbfree == 0) {
3419ac350beSchristos 			brelse(bp, 0);
3426325773eSlukem 			return (0);
3436325773eSlukem 		}
3446325773eSlukem 		bno = ffs_alloccgblk(ip, bp, bpref);
3456325773eSlukem 		bpref = dtogd(fs, bno);
3466325773eSlukem 		for (i = frags; i < fs->fs_frag; i++)
3476325773eSlukem 			setbit(cg_blksfree(cgp, needswap), bpref + i);
3486325773eSlukem 		i = fs->fs_frag - frags;
3496325773eSlukem 		ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
3506325773eSlukem 		fs->fs_cstotal.cs_nffree += i;
3516325773eSlukem 		fs->fs_cs(fs, cg).cs_nffree += i;
3526325773eSlukem 		fs->fs_fmod = 1;
3536325773eSlukem 		ufs_add32(cgp->cg_frsum[i], 1, needswap);
3546325773eSlukem 		bdwrite(bp);
3556325773eSlukem 		return (bno);
3566325773eSlukem 	}
3576325773eSlukem 	bno = ffs_mapsearch(fs, cgp, bpref, allocsiz);
3586325773eSlukem 	for (i = 0; i < frags; i++)
3596325773eSlukem 		clrbit(cg_blksfree(cgp, needswap), bno + i);
3606325773eSlukem 	ufs_add32(cgp->cg_cs.cs_nffree, -frags, needswap);
3616325773eSlukem 	fs->fs_cstotal.cs_nffree -= frags;
3626325773eSlukem 	fs->fs_cs(fs, cg).cs_nffree -= frags;
3636325773eSlukem 	fs->fs_fmod = 1;
3646325773eSlukem 	ufs_add32(cgp->cg_frsum[allocsiz], -1, needswap);
3656325773eSlukem 	if (frags != allocsiz)
3666325773eSlukem 		ufs_add32(cgp->cg_frsum[allocsiz - frags], 1, needswap);
3676325773eSlukem 	blkno = cg * fs->fs_fpg + bno;
3686325773eSlukem 	bdwrite(bp);
3696325773eSlukem 	return blkno;
3706325773eSlukem }
3716325773eSlukem 
3726325773eSlukem /*
3736325773eSlukem  * Allocate a block in a cylinder group.
3746325773eSlukem  *
3756325773eSlukem  * This algorithm implements the following policy:
3766325773eSlukem  *   1) allocate the requested block.
3776325773eSlukem  *   2) allocate a rotationally optimal block in the same cylinder.
3786325773eSlukem  *   3) allocate the next available block on the block rotor for the
3796325773eSlukem  *      specified cylinder group.
3806325773eSlukem  * Note that this routine only allocates fs_bsize blocks; these
3816325773eSlukem  * blocks may be fragmented by the routine that allocates them.
3826325773eSlukem  */
383a3ff3a30Sfvdl static daddr_t
ffs_alloccgblk(struct inode * ip,struct buf * bp,daddr_t bpref)384a3ff3a30Sfvdl ffs_alloccgblk(struct inode *ip, struct buf *bp, daddr_t bpref)
3856325773eSlukem {
3866325773eSlukem 	struct cg *cgp;
38742614ed3Sfvdl 	daddr_t blkno;
38842614ed3Sfvdl 	int32_t bno;
3896325773eSlukem 	struct fs *fs = ip->i_fs;
3906325773eSlukem 	const int needswap = UFS_FSNEEDSWAP(fs);
39142614ed3Sfvdl 	u_int8_t *blksfree;
3926325773eSlukem 
3936325773eSlukem 	cgp = (struct cg *)bp->b_data;
39442614ed3Sfvdl 	blksfree = cg_blksfree(cgp, needswap);
3956325773eSlukem 	if (bpref == 0 || dtog(fs, bpref) != ufs_rw32(cgp->cg_cgx, needswap)) {
3966325773eSlukem 		bpref = ufs_rw32(cgp->cg_rotor, needswap);
39742614ed3Sfvdl 	} else {
39875571afdSdholland 		bpref = ffs_blknum(fs, bpref);
39942614ed3Sfvdl 		bno = dtogd(fs, bpref);
4006325773eSlukem 		/*
4016325773eSlukem 		 * if the requested block is available, use it
4026325773eSlukem 		 */
40375571afdSdholland 		if (ffs_isblock(fs, blksfree, ffs_fragstoblks(fs, bno)))
4046325773eSlukem 			goto gotit;
4056325773eSlukem 	}
4066325773eSlukem 	/*
40742614ed3Sfvdl 	 * Take the next available one in this cylinder group.
4086325773eSlukem 	 */
4096325773eSlukem 	bno = ffs_mapsearch(fs, cgp, bpref, (int)fs->fs_frag);
4106325773eSlukem 	if (bno < 0)
4116325773eSlukem 		return (0);
41242614ed3Sfvdl 	cgp->cg_rotor = ufs_rw32(bno, needswap);
4136325773eSlukem gotit:
41475571afdSdholland 	blkno = ffs_fragstoblks(fs, bno);
41542614ed3Sfvdl 	ffs_clrblock(fs, blksfree, (long)blkno);
4166325773eSlukem 	ffs_clusteracct(fs, cgp, blkno, -1);
4176325773eSlukem 	ufs_add32(cgp->cg_cs.cs_nbfree, -1, needswap);
4186325773eSlukem 	fs->fs_cstotal.cs_nbfree--;
4196325773eSlukem 	fs->fs_cs(fs, ufs_rw32(cgp->cg_cgx, needswap)).cs_nbfree--;
4206325773eSlukem 	fs->fs_fmod = 1;
4216325773eSlukem 	blkno = ufs_rw32(cgp->cg_cgx, needswap) * fs->fs_fpg + bno;
4226325773eSlukem 	return (blkno);
4236325773eSlukem }
4246325773eSlukem 
4256325773eSlukem /*
4266325773eSlukem  * Free a block or fragment.
4276325773eSlukem  *
4286325773eSlukem  * The specified block or fragment is placed back in the
4296325773eSlukem  * free map. If a fragment is deallocated, a possible
4306325773eSlukem  * block reassembly is checked.
4316325773eSlukem  */
4326325773eSlukem void
ffs_blkfree(struct inode * ip,daddr_t bno,long size)433a3ff3a30Sfvdl ffs_blkfree(struct inode *ip, daddr_t bno, long size)
4346325773eSlukem {
4356325773eSlukem 	struct cg *cgp;
4366325773eSlukem 	struct buf *bp;
43742614ed3Sfvdl 	int32_t fragno, cgbno;
4386325773eSlukem 	int i, error, cg, blk, frags, bbase;
4396325773eSlukem 	struct fs *fs = ip->i_fs;
4406325773eSlukem 	const int needswap = UFS_FSNEEDSWAP(fs);
4416325773eSlukem 
442f1333577Sdholland 	if (size > fs->fs_bsize || ffs_fragoff(fs, size) != 0 ||
44375571afdSdholland 	    ffs_fragnum(fs, bno) + ffs_numfrags(fs, size) > fs->fs_frag) {
4446125bf59Schristos 		errx(EXIT_FAILURE, "%s: bad size: bno %lld bsize %d "
4456125bf59Schristos 		    "size %ld", __func__, (long long)bno, fs->fs_bsize, size);
4466325773eSlukem 	}
4476325773eSlukem 	cg = dtog(fs, bno);
44842614ed3Sfvdl 	if (bno >= fs->fs_size) {
449c4ee9f6dSchristos 		warnx("bad block %lld, ino %llu", (long long)bno,
450c4ee9f6dSchristos 		    (unsigned long long)ip->i_number);
4516325773eSlukem 		return;
4526325773eSlukem 	}
4532737439dSdholland 	error = bread(ip->i_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)),
454aa7e0e4bSagc 	    (int)fs->fs_cgsize, 0, &bp);
4556325773eSlukem 	if (error) {
4566325773eSlukem 		return;
4576325773eSlukem 	}
4586325773eSlukem 	cgp = (struct cg *)bp->b_data;
4596325773eSlukem 	if (!cg_chkmagic(cgp, needswap)) {
4609ac350beSchristos 		brelse(bp, 0);
4616325773eSlukem 		return;
4626325773eSlukem 	}
46342614ed3Sfvdl 	cgbno = dtogd(fs, bno);
4646325773eSlukem 	if (size == fs->fs_bsize) {
46575571afdSdholland 		fragno = ffs_fragstoblks(fs, cgbno);
46642614ed3Sfvdl 		if (!ffs_isfreeblock(fs, cg_blksfree(cgp, needswap), fragno)) {
4676125bf59Schristos 			errx(EXIT_FAILURE, "%s: freeing free block %lld",
4686125bf59Schristos 			    __func__, (long long)bno);
4696325773eSlukem 		}
47042614ed3Sfvdl 		ffs_setblock(fs, cg_blksfree(cgp, needswap), fragno);
47142614ed3Sfvdl 		ffs_clusteracct(fs, cgp, fragno, 1);
4726325773eSlukem 		ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
4736325773eSlukem 		fs->fs_cstotal.cs_nbfree++;
4746325773eSlukem 		fs->fs_cs(fs, cg).cs_nbfree++;
4756325773eSlukem 	} else {
47675571afdSdholland 		bbase = cgbno - ffs_fragnum(fs, cgbno);
4776325773eSlukem 		/*
4786325773eSlukem 		 * decrement the counts associated with the old frags
4796325773eSlukem 		 */
4806325773eSlukem 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
4816325773eSlukem 		ffs_fragacct(fs, blk, cgp->cg_frsum, -1, needswap);
4826325773eSlukem 		/*
4836325773eSlukem 		 * deallocate the fragment
4846325773eSlukem 		 */
485e1610ba4Sdholland 		frags = ffs_numfrags(fs, size);
4866325773eSlukem 		for (i = 0; i < frags; i++) {
48742614ed3Sfvdl 			if (isset(cg_blksfree(cgp, needswap), cgbno + i)) {
4886125bf59Schristos 				errx(EXIT_FAILURE, "%s: freeing free frag: "
4896125bf59Schristos 				    "block %lld", __func__,
49042614ed3Sfvdl 				    (long long)(cgbno + i));
4916325773eSlukem 			}
49242614ed3Sfvdl 			setbit(cg_blksfree(cgp, needswap), cgbno + i);
4936325773eSlukem 		}
4946325773eSlukem 		ufs_add32(cgp->cg_cs.cs_nffree, i, needswap);
4956325773eSlukem 		fs->fs_cstotal.cs_nffree += i;
4966325773eSlukem 		fs->fs_cs(fs, cg).cs_nffree += i;
4976325773eSlukem 		/*
4986325773eSlukem 		 * add back in counts associated with the new frags
4996325773eSlukem 		 */
5006325773eSlukem 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bbase);
5016325773eSlukem 		ffs_fragacct(fs, blk, cgp->cg_frsum, 1, needswap);
5026325773eSlukem 		/*
5036325773eSlukem 		 * if a complete block has been reassembled, account for it
5046325773eSlukem 		 */
50575571afdSdholland 		fragno = ffs_fragstoblks(fs, bbase);
50642614ed3Sfvdl 		if (ffs_isblock(fs, cg_blksfree(cgp, needswap), fragno)) {
5076325773eSlukem 			ufs_add32(cgp->cg_cs.cs_nffree, -fs->fs_frag, needswap);
5086325773eSlukem 			fs->fs_cstotal.cs_nffree -= fs->fs_frag;
5096325773eSlukem 			fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
51042614ed3Sfvdl 			ffs_clusteracct(fs, cgp, fragno, 1);
5116325773eSlukem 			ufs_add32(cgp->cg_cs.cs_nbfree, 1, needswap);
5126325773eSlukem 			fs->fs_cstotal.cs_nbfree++;
5136325773eSlukem 			fs->fs_cs(fs, cg).cs_nbfree++;
5146325773eSlukem 		}
5156325773eSlukem 	}
5166325773eSlukem 	fs->fs_fmod = 1;
5176325773eSlukem 	bdwrite(bp);
5186325773eSlukem }
5196325773eSlukem 
5206325773eSlukem 
5216325773eSlukem static int
scanc(u_int size,const u_char * cp,const u_char table[],int mask)5226325773eSlukem scanc(u_int size, const u_char *cp, const u_char table[], int mask)
5236325773eSlukem {
5246325773eSlukem 	const u_char *end = &cp[size];
5256325773eSlukem 
5266325773eSlukem 	while (cp < end && (table[*cp] & mask) == 0)
5276325773eSlukem 		cp++;
5286325773eSlukem 	return (end - cp);
5296325773eSlukem }
5306325773eSlukem 
5316325773eSlukem /*
5326325773eSlukem  * Find a block of the specified size in the specified cylinder group.
5336325773eSlukem  *
5346325773eSlukem  * It is a panic if a request is made to find a block if none are
5356325773eSlukem  * available.
5366325773eSlukem  */
53742614ed3Sfvdl static int32_t
ffs_mapsearch(struct fs * fs,struct cg * cgp,daddr_t bpref,int allocsiz)538a3ff3a30Sfvdl ffs_mapsearch(struct fs *fs, struct cg *cgp, daddr_t bpref, int allocsiz)
5396325773eSlukem {
54042614ed3Sfvdl 	int32_t bno;
5416325773eSlukem 	int start, len, loc, i;
5426325773eSlukem 	int blk, field, subfield, pos;
5436325773eSlukem 	int ostart, olen;
5446325773eSlukem 	const int needswap = UFS_FSNEEDSWAP(fs);
5456325773eSlukem 
5466325773eSlukem 	/*
5476325773eSlukem 	 * find the fragment by searching through the free block
5486325773eSlukem 	 * map for an appropriate bit pattern
5496325773eSlukem 	 */
5506325773eSlukem 	if (bpref)
5516325773eSlukem 		start = dtogd(fs, bpref) / NBBY;
5526325773eSlukem 	else
5536325773eSlukem 		start = ufs_rw32(cgp->cg_frotor, needswap) / NBBY;
5546325773eSlukem 	len = howmany(fs->fs_fpg, NBBY) - start;
5556325773eSlukem 	ostart = start;
5566325773eSlukem 	olen = len;
5576325773eSlukem 	loc = scanc((u_int)len,
5586325773eSlukem 		(const u_char *)&cg_blksfree(cgp, needswap)[start],
5596325773eSlukem 		(const u_char *)fragtbl[fs->fs_frag],
5606325773eSlukem 		(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
5616325773eSlukem 	if (loc == 0) {
5626325773eSlukem 		len = start + 1;
5636325773eSlukem 		start = 0;
5646325773eSlukem 		loc = scanc((u_int)len,
5656325773eSlukem 			(const u_char *)&cg_blksfree(cgp, needswap)[0],
5666325773eSlukem 			(const u_char *)fragtbl[fs->fs_frag],
5676325773eSlukem 			(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
5686325773eSlukem 		if (loc == 0) {
5696125bf59Schristos 			errx(EXIT_FAILURE, "%s: map corrupted: start %d "
5706125bf59Schristos 			    "len %d offset %d %ld", __func__, ostart, olen,
5716325773eSlukem 			    ufs_rw32(cgp->cg_freeoff, needswap),
5726325773eSlukem 			    (long)cg_blksfree(cgp, needswap) - (long)cgp);
5736325773eSlukem 			/* NOTREACHED */
5746325773eSlukem 		}
5756325773eSlukem 	}
5766325773eSlukem 	bno = (start + len - loc) * NBBY;
5776325773eSlukem 	cgp->cg_frotor = ufs_rw32(bno, needswap);
5786325773eSlukem 	/*
5796325773eSlukem 	 * found the byte in the map
5806325773eSlukem 	 * sift through the bits to find the selected frag
5816325773eSlukem 	 */
5826325773eSlukem 	for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
5836325773eSlukem 		blk = blkmap(fs, cg_blksfree(cgp, needswap), bno);
5846325773eSlukem 		blk <<= 1;
5856325773eSlukem 		field = around[allocsiz];
5866325773eSlukem 		subfield = inside[allocsiz];
5876325773eSlukem 		for (pos = 0; pos <= fs->fs_frag - allocsiz; pos++) {
5886325773eSlukem 			if ((blk & field) == subfield)
5896325773eSlukem 				return (bno + pos);
5906325773eSlukem 			field <<= 1;
5916325773eSlukem 			subfield <<= 1;
5926325773eSlukem 		}
5936325773eSlukem 	}
5946125bf59Schristos 	errx(EXIT_FAILURE, "%s: block not in map: bno %lld", __func__,
5956125bf59Schristos 	    (long long)bno);
5966325773eSlukem 	return (-1);
5976325773eSlukem }
598