xref: /freebsd/sys/kern/subr_blist.c (revision 69a2875821ed9404a95f41bbeeb561908e3c38dc)
106b4bf3eSWarner Losh /*-
206b4bf3eSWarner Losh  * Copyright (c) 1998 Matthew Dillon.  All Rights Reserved.
306b4bf3eSWarner Losh  * Redistribution and use in source and binary forms, with or without
406b4bf3eSWarner Losh  * modification, are permitted provided that the following conditions
506b4bf3eSWarner Losh  * are met:
606b4bf3eSWarner Losh  * 1. Redistributions of source code must retain the above copyright
706b4bf3eSWarner Losh  *    notice, this list of conditions and the following disclaimer.
806b4bf3eSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
906b4bf3eSWarner Losh  *    notice, this list of conditions and the following disclaimer in the
1006b4bf3eSWarner Losh  *    documentation and/or other materials provided with the distribution.
11*69a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
1206b4bf3eSWarner Losh  *    may be used to endorse or promote products derived from this software
1306b4bf3eSWarner Losh  *    without specific prior written permission.
1406b4bf3eSWarner Losh  *
1506b4bf3eSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1606b4bf3eSWarner Losh  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1706b4bf3eSWarner Losh  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1806b4bf3eSWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
1906b4bf3eSWarner Losh  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2006b4bf3eSWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2106b4bf3eSWarner Losh  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2206b4bf3eSWarner Losh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2306b4bf3eSWarner Losh  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2406b4bf3eSWarner Losh  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2506b4bf3eSWarner Losh  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2606b4bf3eSWarner Losh  */
277090df5aSMatthew Dillon /*
287090df5aSMatthew Dillon  * BLIST.C -	Bitmap allocator/deallocator, using a radix tree with hinting
297090df5aSMatthew Dillon  *
307090df5aSMatthew Dillon  *	This module implements a general bitmap allocator/deallocator.  The
317090df5aSMatthew Dillon  *	allocator eats around 2 bits per 'block'.  The module does not
32f565f395SEitan Adler  *	try to interpret the meaning of a 'block' other than to return
337090df5aSMatthew Dillon  *	SWAPBLK_NONE on an allocation failure.
347090df5aSMatthew Dillon  *
357090df5aSMatthew Dillon  *	A radix tree is used to maintain the bitmap.  Two radix constants are
367090df5aSMatthew Dillon  *	involved:  One for the bitmaps contained in the leaf nodes (typically
377090df5aSMatthew Dillon  *	32), and one for the meta nodes (typically 16).  Both meta and leaf
387090df5aSMatthew Dillon  *	nodes have a hint field.  This field gives us a hint as to the largest
397090df5aSMatthew Dillon  *	free contiguous range of blocks under the node.  It may contain a
407090df5aSMatthew Dillon  *	value that is too high, but will never contain a value that is too
417090df5aSMatthew Dillon  *	low.  When the radix tree is searched, allocation failures in subtrees
427090df5aSMatthew Dillon  *	update the hint.
437090df5aSMatthew Dillon  *
447090df5aSMatthew Dillon  *	The radix tree also implements two collapsed states for meta nodes:
457090df5aSMatthew Dillon  *	the ALL-ALLOCATED state and the ALL-FREE state.  If a meta node is
467090df5aSMatthew Dillon  *	in either of these two states, all information contained underneath
477090df5aSMatthew Dillon  *	the node is considered stale.  These states are used to optimize
487090df5aSMatthew Dillon  *	allocation and freeing operations.
497090df5aSMatthew Dillon  *
507090df5aSMatthew Dillon  * 	The hinting greatly increases code efficiency for allocations while
517090df5aSMatthew Dillon  *	the general radix structure optimizes both allocations and frees.  The
527090df5aSMatthew Dillon  *	radix tree should be able to operate well no matter how much
537090df5aSMatthew Dillon  *	fragmentation there is and no matter how large a bitmap is used.
547090df5aSMatthew Dillon  *
5551dc4feaSSergey Kandaurov  *	The blist code wires all necessary memory at creation time.  Neither
5651dc4feaSSergey Kandaurov  *	allocations nor frees require interaction with the memory subsystem.
5751dc4feaSSergey Kandaurov  *	The non-blocking features of the blist code are used in the swap code
5851dc4feaSSergey Kandaurov  *	(vm/swap_pager.c).
597090df5aSMatthew Dillon  *
60e3043798SPedro F. Giffuni  *	LAYOUT: The radix tree is laid out recursively using a
61e3043798SPedro F. Giffuni  *	linear array.  Each meta node is immediately followed (laid out
627090df5aSMatthew Dillon  *	sequentially in memory) by BLIST_META_RADIX lower level nodes.  This
637090df5aSMatthew Dillon  *	is a recursive structure but one that can be easily scanned through
647090df5aSMatthew Dillon  *	a very simple 'skip' calculation.  In order to support large radixes,
657090df5aSMatthew Dillon  *	portions of the tree may reside outside our memory allocation.  We
667090df5aSMatthew Dillon  *	handle this with an early-termination optimization (when bighint is
677090df5aSMatthew Dillon  *	set to -1) on the scan.  The memory allocation is only large enough
687090df5aSMatthew Dillon  *	to cover the number of blocks requested at creation time even if it
697090df5aSMatthew Dillon  *	must be encompassed in larger root-node radix.
707090df5aSMatthew Dillon  *
71f565f395SEitan Adler  *	NOTE: the allocator cannot currently allocate more than
727090df5aSMatthew Dillon  *	BLIST_BMAP_RADIX blocks per call.  It will panic with 'allocation too
737090df5aSMatthew Dillon  *	large' if you try.  This is an area that could use improvement.  The
747090df5aSMatthew Dillon  *	radix is large enough that this restriction does not effect the swap
757090df5aSMatthew Dillon  *	system, though.  Currently only the allocation code is effected by
767090df5aSMatthew Dillon  *	this algorithmic unfeature.  The freeing code can handle arbitrary
777090df5aSMatthew Dillon  *	ranges.
787090df5aSMatthew Dillon  *
797090df5aSMatthew Dillon  *	This code can be compiled stand-alone for debugging.
807090df5aSMatthew Dillon  */
817090df5aSMatthew Dillon 
82677b542eSDavid E. O'Brien #include <sys/cdefs.h>
83677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
84677b542eSDavid E. O'Brien 
85c4473420SPeter Wemm #ifdef _KERNEL
867090df5aSMatthew Dillon 
877090df5aSMatthew Dillon #include <sys/param.h>
887090df5aSMatthew Dillon #include <sys/systm.h>
897090df5aSMatthew Dillon #include <sys/lock.h>
907090df5aSMatthew Dillon #include <sys/kernel.h>
917090df5aSMatthew Dillon #include <sys/blist.h>
927090df5aSMatthew Dillon #include <sys/malloc.h>
930cddd8f0SMatthew Dillon #include <sys/proc.h>
9423955314SAlfred Perlstein #include <sys/mutex.h>
957090df5aSMatthew Dillon 
967090df5aSMatthew Dillon #else
977090df5aSMatthew Dillon 
987090df5aSMatthew Dillon #ifndef BLIST_NO_DEBUG
997090df5aSMatthew Dillon #define BLIST_DEBUG
1007090df5aSMatthew Dillon #endif
1017090df5aSMatthew Dillon 
1027090df5aSMatthew Dillon #define SWAPBLK_NONE ((daddr_t)-1)
1037090df5aSMatthew Dillon 
1047090df5aSMatthew Dillon #include <sys/types.h>
1057090df5aSMatthew Dillon #include <stdio.h>
1067090df5aSMatthew Dillon #include <string.h>
1077090df5aSMatthew Dillon #include <stdlib.h>
1087090df5aSMatthew Dillon #include <stdarg.h>
1097090df5aSMatthew Dillon 
11092da00bbSMatthew Dillon #define malloc(a,b,c)	calloc(a, 1)
1117090df5aSMatthew Dillon #define free(a,b)	free(a)
1127090df5aSMatthew Dillon 
1137090df5aSMatthew Dillon typedef unsigned int u_daddr_t;
1147090df5aSMatthew Dillon 
1157090df5aSMatthew Dillon #include <sys/blist.h>
1167090df5aSMatthew Dillon 
1177090df5aSMatthew Dillon void panic(const char *ctl, ...);
1187090df5aSMatthew Dillon 
1197090df5aSMatthew Dillon #endif
1207090df5aSMatthew Dillon 
1217090df5aSMatthew Dillon /*
1227090df5aSMatthew Dillon  * static support functions
1237090df5aSMatthew Dillon  */
1247090df5aSMatthew Dillon 
1257090df5aSMatthew Dillon static daddr_t blst_leaf_alloc(blmeta_t *scan, daddr_t blk, int count);
1267090df5aSMatthew Dillon static daddr_t blst_meta_alloc(blmeta_t *scan, daddr_t blk,
1277090df5aSMatthew Dillon 				daddr_t count, daddr_t radix, int skip);
1287090df5aSMatthew Dillon static void blst_leaf_free(blmeta_t *scan, daddr_t relblk, int count);
1297090df5aSMatthew Dillon static void blst_meta_free(blmeta_t *scan, daddr_t freeBlk, daddr_t count,
1307090df5aSMatthew Dillon 					daddr_t radix, int skip, daddr_t blk);
1317090df5aSMatthew Dillon static void blst_copy(blmeta_t *scan, daddr_t blk, daddr_t radix,
1327090df5aSMatthew Dillon 				daddr_t skip, blist_t dest, daddr_t count);
13392da00bbSMatthew Dillon static int blst_leaf_fill(blmeta_t *scan, daddr_t blk, int count);
13492da00bbSMatthew Dillon static int blst_meta_fill(blmeta_t *scan, daddr_t allocBlk, daddr_t count,
13592da00bbSMatthew Dillon 				daddr_t radix, int skip, daddr_t blk);
1367090df5aSMatthew Dillon static daddr_t	blst_radix_init(blmeta_t *scan, daddr_t radix,
1377090df5aSMatthew Dillon 						int skip, daddr_t count);
138c4473420SPeter Wemm #ifndef _KERNEL
1397090df5aSMatthew Dillon static void	blst_radix_print(blmeta_t *scan, daddr_t blk,
1407090df5aSMatthew Dillon 					daddr_t radix, int skip, int tab);
1417090df5aSMatthew Dillon #endif
1427090df5aSMatthew Dillon 
143c4473420SPeter Wemm #ifdef _KERNEL
1447090df5aSMatthew Dillon static MALLOC_DEFINE(M_SWAP, "SWAP", "Swap space");
1457090df5aSMatthew Dillon #endif
1467090df5aSMatthew Dillon 
1477090df5aSMatthew Dillon /*
1487090df5aSMatthew Dillon  * blist_create() - create a blist capable of handling up to the specified
1497090df5aSMatthew Dillon  *		    number of blocks
1507090df5aSMatthew Dillon  *
151f565f395SEitan Adler  *	blocks - must be greater than 0
152c8c7ad92SKip Macy  * 	flags  - malloc flags
1537090df5aSMatthew Dillon  *
1547090df5aSMatthew Dillon  *	The smallest blist consists of a single leaf node capable of
1557090df5aSMatthew Dillon  *	managing BLIST_BMAP_RADIX blocks.
1567090df5aSMatthew Dillon  */
1577090df5aSMatthew Dillon 
1587090df5aSMatthew Dillon blist_t
159c8c7ad92SKip Macy blist_create(daddr_t blocks, int flags)
1607090df5aSMatthew Dillon {
1617090df5aSMatthew Dillon 	blist_t bl;
1627090df5aSMatthew Dillon 	int radix;
1637090df5aSMatthew Dillon 	int skip = 0;
1647090df5aSMatthew Dillon 
1657090df5aSMatthew Dillon 	/*
1667090df5aSMatthew Dillon 	 * Calculate radix and skip field used for scanning.
1677090df5aSMatthew Dillon 	 */
1687090df5aSMatthew Dillon 	radix = BLIST_BMAP_RADIX;
1697090df5aSMatthew Dillon 
1707090df5aSMatthew Dillon 	while (radix < blocks) {
17157e6d29bSMatthew Dillon 		radix *= BLIST_META_RADIX;
17257e6d29bSMatthew Dillon 		skip = (skip + 1) * BLIST_META_RADIX;
1737090df5aSMatthew Dillon 	}
1747090df5aSMatthew Dillon 
175c8c7ad92SKip Macy 	bl = malloc(sizeof(struct blist), M_SWAP, flags | M_ZERO);
1767090df5aSMatthew Dillon 
1777090df5aSMatthew Dillon 	bl->bl_blocks = blocks;
1787090df5aSMatthew Dillon 	bl->bl_radix = radix;
1797090df5aSMatthew Dillon 	bl->bl_skip = skip;
1807090df5aSMatthew Dillon 	bl->bl_rootblks = 1 +
1817090df5aSMatthew Dillon 	    blst_radix_init(NULL, bl->bl_radix, bl->bl_skip, blocks);
182c8c7ad92SKip Macy 	bl->bl_root = malloc(sizeof(blmeta_t) * bl->bl_rootblks, M_SWAP, flags);
1837090df5aSMatthew Dillon 
1847090df5aSMatthew Dillon #if defined(BLIST_DEBUG)
1857090df5aSMatthew Dillon 	printf(
18692da00bbSMatthew Dillon 		"BLIST representing %lld blocks (%lld MB of swap)"
18792da00bbSMatthew Dillon 		", requiring %lldK of ram\n",
18892da00bbSMatthew Dillon 		(long long)bl->bl_blocks,
18992da00bbSMatthew Dillon 		(long long)bl->bl_blocks * 4 / 1024,
19092da00bbSMatthew Dillon 		(long long)(bl->bl_rootblks * sizeof(blmeta_t) + 1023) / 1024
1917090df5aSMatthew Dillon 	);
19292da00bbSMatthew Dillon 	printf("BLIST raw radix tree contains %lld records\n",
19392da00bbSMatthew Dillon 	    (long long)bl->bl_rootblks);
1947090df5aSMatthew Dillon #endif
1957090df5aSMatthew Dillon 	blst_radix_init(bl->bl_root, bl->bl_radix, bl->bl_skip, blocks);
1967090df5aSMatthew Dillon 
1977090df5aSMatthew Dillon 	return(bl);
1987090df5aSMatthew Dillon }
1997090df5aSMatthew Dillon 
2007090df5aSMatthew Dillon void
2017090df5aSMatthew Dillon blist_destroy(blist_t bl)
2027090df5aSMatthew Dillon {
2037090df5aSMatthew Dillon 	free(bl->bl_root, M_SWAP);
2047090df5aSMatthew Dillon 	free(bl, M_SWAP);
2057090df5aSMatthew Dillon }
2067090df5aSMatthew Dillon 
2077090df5aSMatthew Dillon /*
2087090df5aSMatthew Dillon  * blist_alloc() - reserve space in the block bitmap.  Return the base
2097090df5aSMatthew Dillon  *		     of a contiguous region or SWAPBLK_NONE if space could
2107090df5aSMatthew Dillon  *		     not be allocated.
2117090df5aSMatthew Dillon  */
2127090df5aSMatthew Dillon 
2137090df5aSMatthew Dillon daddr_t
2147090df5aSMatthew Dillon blist_alloc(blist_t bl, daddr_t count)
2157090df5aSMatthew Dillon {
2167090df5aSMatthew Dillon 	daddr_t blk = SWAPBLK_NONE;
2177090df5aSMatthew Dillon 
2187090df5aSMatthew Dillon 	if (bl) {
2197090df5aSMatthew Dillon 		if (bl->bl_radix == BLIST_BMAP_RADIX)
2207090df5aSMatthew Dillon 			blk = blst_leaf_alloc(bl->bl_root, 0, count);
2217090df5aSMatthew Dillon 		else
2227090df5aSMatthew Dillon 			blk = blst_meta_alloc(bl->bl_root, 0, count, bl->bl_radix, bl->bl_skip);
2237090df5aSMatthew Dillon 		if (blk != SWAPBLK_NONE)
2247090df5aSMatthew Dillon 			bl->bl_free -= count;
2257090df5aSMatthew Dillon 	}
2267090df5aSMatthew Dillon 	return(blk);
2277090df5aSMatthew Dillon }
2287090df5aSMatthew Dillon 
2297090df5aSMatthew Dillon /*
2307090df5aSMatthew Dillon  * blist_free() -	free up space in the block bitmap.  Return the base
2317090df5aSMatthew Dillon  *		     	of a contiguous region.  Panic if an inconsistancy is
2327090df5aSMatthew Dillon  *			found.
2337090df5aSMatthew Dillon  */
2347090df5aSMatthew Dillon 
2357090df5aSMatthew Dillon void
2367090df5aSMatthew Dillon blist_free(blist_t bl, daddr_t blkno, daddr_t count)
2377090df5aSMatthew Dillon {
2387090df5aSMatthew Dillon 	if (bl) {
2397090df5aSMatthew Dillon 		if (bl->bl_radix == BLIST_BMAP_RADIX)
2407090df5aSMatthew Dillon 			blst_leaf_free(bl->bl_root, blkno, count);
2417090df5aSMatthew Dillon 		else
2427090df5aSMatthew Dillon 			blst_meta_free(bl->bl_root, blkno, count, bl->bl_radix, bl->bl_skip, 0);
2437090df5aSMatthew Dillon 		bl->bl_free += count;
2447090df5aSMatthew Dillon 	}
2457090df5aSMatthew Dillon }
2467090df5aSMatthew Dillon 
2477090df5aSMatthew Dillon /*
24892da00bbSMatthew Dillon  * blist_fill() -	mark a region in the block bitmap as off-limits
24992da00bbSMatthew Dillon  *			to the allocator (i.e. allocate it), ignoring any
25092da00bbSMatthew Dillon  *			existing allocations.  Return the number of blocks
25192da00bbSMatthew Dillon  *			actually filled that were free before the call.
25292da00bbSMatthew Dillon  */
25392da00bbSMatthew Dillon 
25492da00bbSMatthew Dillon int
25592da00bbSMatthew Dillon blist_fill(blist_t bl, daddr_t blkno, daddr_t count)
25692da00bbSMatthew Dillon {
25792da00bbSMatthew Dillon 	int filled;
25892da00bbSMatthew Dillon 
25992da00bbSMatthew Dillon 	if (bl) {
26092da00bbSMatthew Dillon 		if (bl->bl_radix == BLIST_BMAP_RADIX)
26192da00bbSMatthew Dillon 			filled = blst_leaf_fill(bl->bl_root, blkno, count);
26292da00bbSMatthew Dillon 		else
26392da00bbSMatthew Dillon 			filled = blst_meta_fill(bl->bl_root, blkno, count,
26492da00bbSMatthew Dillon 			    bl->bl_radix, bl->bl_skip, 0);
26592da00bbSMatthew Dillon 		bl->bl_free -= filled;
26692da00bbSMatthew Dillon 		return filled;
26792da00bbSMatthew Dillon 	} else
26892da00bbSMatthew Dillon 		return 0;
26992da00bbSMatthew Dillon }
27092da00bbSMatthew Dillon 
27192da00bbSMatthew Dillon /*
2727090df5aSMatthew Dillon  * blist_resize() -	resize an existing radix tree to handle the
2737090df5aSMatthew Dillon  *			specified number of blocks.  This will reallocate
2747090df5aSMatthew Dillon  *			the tree and transfer the previous bitmap to the new
2757090df5aSMatthew Dillon  *			one.  When extending the tree you can specify whether
2767090df5aSMatthew Dillon  *			the new blocks are to left allocated or freed.
2777090df5aSMatthew Dillon  */
2787090df5aSMatthew Dillon 
2797090df5aSMatthew Dillon void
280c8c7ad92SKip Macy blist_resize(blist_t *pbl, daddr_t count, int freenew, int flags)
2817090df5aSMatthew Dillon {
282c8c7ad92SKip Macy     blist_t newbl = blist_create(count, flags);
2837090df5aSMatthew Dillon     blist_t save = *pbl;
2847090df5aSMatthew Dillon 
2857090df5aSMatthew Dillon     *pbl = newbl;
2867090df5aSMatthew Dillon     if (count > save->bl_blocks)
2877090df5aSMatthew Dillon 	    count = save->bl_blocks;
2887090df5aSMatthew Dillon     blst_copy(save->bl_root, 0, save->bl_radix, save->bl_skip, newbl, count);
2897090df5aSMatthew Dillon 
2907090df5aSMatthew Dillon     /*
2917090df5aSMatthew Dillon      * If resizing upwards, should we free the new space or not?
2927090df5aSMatthew Dillon      */
2937090df5aSMatthew Dillon     if (freenew && count < newbl->bl_blocks) {
2947090df5aSMatthew Dillon 	    blist_free(newbl, count, newbl->bl_blocks - count);
2957090df5aSMatthew Dillon     }
2967090df5aSMatthew Dillon     blist_destroy(save);
2977090df5aSMatthew Dillon }
2987090df5aSMatthew Dillon 
2997090df5aSMatthew Dillon #ifdef BLIST_DEBUG
3007090df5aSMatthew Dillon 
3017090df5aSMatthew Dillon /*
3027090df5aSMatthew Dillon  * blist_print()    - dump radix tree
3037090df5aSMatthew Dillon  */
3047090df5aSMatthew Dillon 
3057090df5aSMatthew Dillon void
3067090df5aSMatthew Dillon blist_print(blist_t bl)
3077090df5aSMatthew Dillon {
3087090df5aSMatthew Dillon 	printf("BLIST {\n");
3097090df5aSMatthew Dillon 	blst_radix_print(bl->bl_root, 0, bl->bl_radix, bl->bl_skip, 4);
3107090df5aSMatthew Dillon 	printf("}\n");
3117090df5aSMatthew Dillon }
3127090df5aSMatthew Dillon 
3137090df5aSMatthew Dillon #endif
3147090df5aSMatthew Dillon 
3157090df5aSMatthew Dillon /************************************************************************
3167090df5aSMatthew Dillon  *			  ALLOCATION SUPPORT FUNCTIONS			*
3177090df5aSMatthew Dillon  ************************************************************************
3187090df5aSMatthew Dillon  *
3197090df5aSMatthew Dillon  *	These support functions do all the actual work.  They may seem
3207090df5aSMatthew Dillon  *	rather longish, but that's because I've commented them up.  The
3217090df5aSMatthew Dillon  *	actual code is straight forward.
3227090df5aSMatthew Dillon  *
3237090df5aSMatthew Dillon  */
3247090df5aSMatthew Dillon 
3257090df5aSMatthew Dillon /*
3267090df5aSMatthew Dillon  * blist_leaf_alloc() -	allocate at a leaf in the radix tree (a bitmap).
3277090df5aSMatthew Dillon  *
3287090df5aSMatthew Dillon  *	This is the core of the allocator and is optimized for the 1 block
3297090df5aSMatthew Dillon  *	and the BLIST_BMAP_RADIX block allocation cases.  Other cases are
3307090df5aSMatthew Dillon  *	somewhat slower.  The 1 block allocation case is log2 and extremely
3317090df5aSMatthew Dillon  *	quick.
3327090df5aSMatthew Dillon  */
3337090df5aSMatthew Dillon 
3347090df5aSMatthew Dillon static daddr_t
3357090df5aSMatthew Dillon blst_leaf_alloc(
3367090df5aSMatthew Dillon 	blmeta_t *scan,
3377090df5aSMatthew Dillon 	daddr_t blk,
3387090df5aSMatthew Dillon 	int count
3397090df5aSMatthew Dillon ) {
3407090df5aSMatthew Dillon 	u_daddr_t orig = scan->u.bmu_bitmap;
3417090df5aSMatthew Dillon 
3427090df5aSMatthew Dillon 	if (orig == 0) {
3437090df5aSMatthew Dillon 		/*
3447090df5aSMatthew Dillon 		 * Optimize bitmap all-allocated case.  Also, count = 1
3457090df5aSMatthew Dillon 		 * case assumes at least 1 bit is free in the bitmap, so
3467090df5aSMatthew Dillon 		 * we have to take care of this case here.
3477090df5aSMatthew Dillon 		 */
3487090df5aSMatthew Dillon 		scan->bm_bighint = 0;
3497090df5aSMatthew Dillon 		return(SWAPBLK_NONE);
3507090df5aSMatthew Dillon 	}
3517090df5aSMatthew Dillon 	if (count == 1) {
3527090df5aSMatthew Dillon 		/*
3537090df5aSMatthew Dillon 		 * Optimized code to allocate one bit out of the bitmap
3547090df5aSMatthew Dillon 		 */
3557090df5aSMatthew Dillon 		u_daddr_t mask;
3567090df5aSMatthew Dillon 		int j = BLIST_BMAP_RADIX/2;
3577090df5aSMatthew Dillon 		int r = 0;
3587090df5aSMatthew Dillon 
3597090df5aSMatthew Dillon 		mask = (u_daddr_t)-1 >> (BLIST_BMAP_RADIX/2);
3607090df5aSMatthew Dillon 
3617090df5aSMatthew Dillon 		while (j) {
3627090df5aSMatthew Dillon 			if ((orig & mask) == 0) {
3637090df5aSMatthew Dillon 			    r += j;
3647090df5aSMatthew Dillon 			    orig >>= j;
3657090df5aSMatthew Dillon 			}
3667090df5aSMatthew Dillon 			j >>= 1;
3677090df5aSMatthew Dillon 			mask >>= j;
3687090df5aSMatthew Dillon 		}
3697090df5aSMatthew Dillon 		scan->u.bmu_bitmap &= ~(1 << r);
3707090df5aSMatthew Dillon 		return(blk + r);
3717090df5aSMatthew Dillon 	}
3727090df5aSMatthew Dillon 	if (count <= BLIST_BMAP_RADIX) {
3737090df5aSMatthew Dillon 		/*
3747090df5aSMatthew Dillon 		 * non-optimized code to allocate N bits out of the bitmap.
3757090df5aSMatthew Dillon 		 * The more bits, the faster the code runs.  It will run
3767090df5aSMatthew Dillon 		 * the slowest allocating 2 bits, but since there aren't any
3777090df5aSMatthew Dillon 		 * memory ops in the core loop (or shouldn't be, anyway),
3787090df5aSMatthew Dillon 		 * you probably won't notice the difference.
3797090df5aSMatthew Dillon 		 */
3807090df5aSMatthew Dillon 		int j;
3817090df5aSMatthew Dillon 		int n = BLIST_BMAP_RADIX - count;
3827090df5aSMatthew Dillon 		u_daddr_t mask;
3837090df5aSMatthew Dillon 
3847090df5aSMatthew Dillon 		mask = (u_daddr_t)-1 >> n;
3857090df5aSMatthew Dillon 
3867090df5aSMatthew Dillon 		for (j = 0; j <= n; ++j) {
3877090df5aSMatthew Dillon 			if ((orig & mask) == mask) {
3887090df5aSMatthew Dillon 				scan->u.bmu_bitmap &= ~mask;
3897090df5aSMatthew Dillon 				return(blk + j);
3907090df5aSMatthew Dillon 			}
3917090df5aSMatthew Dillon 			mask = (mask << 1);
3927090df5aSMatthew Dillon 		}
3937090df5aSMatthew Dillon 	}
3947090df5aSMatthew Dillon 	/*
3957090df5aSMatthew Dillon 	 * We couldn't allocate count in this subtree, update bighint.
3967090df5aSMatthew Dillon 	 */
3977090df5aSMatthew Dillon 	scan->bm_bighint = count - 1;
3987090df5aSMatthew Dillon 	return(SWAPBLK_NONE);
3997090df5aSMatthew Dillon }
4007090df5aSMatthew Dillon 
4017090df5aSMatthew Dillon /*
4027090df5aSMatthew Dillon  * blist_meta_alloc() -	allocate at a meta in the radix tree.
4037090df5aSMatthew Dillon  *
4047090df5aSMatthew Dillon  *	Attempt to allocate at a meta node.  If we can't, we update
4057090df5aSMatthew Dillon  *	bighint and return a failure.  Updating bighint optimize future
4067090df5aSMatthew Dillon  *	calls that hit this node.  We have to check for our collapse cases
4077090df5aSMatthew Dillon  *	and we have a few optimizations strewn in as well.
4087090df5aSMatthew Dillon  */
4097090df5aSMatthew Dillon 
4107090df5aSMatthew Dillon static daddr_t
4117090df5aSMatthew Dillon blst_meta_alloc(
4127090df5aSMatthew Dillon 	blmeta_t *scan,
4137090df5aSMatthew Dillon 	daddr_t blk,
4147090df5aSMatthew Dillon 	daddr_t count,
4157090df5aSMatthew Dillon 	daddr_t radix,
4167090df5aSMatthew Dillon 	int skip
4177090df5aSMatthew Dillon ) {
4187090df5aSMatthew Dillon 	int i;
41957e6d29bSMatthew Dillon 	int next_skip = ((u_int)skip / BLIST_META_RADIX);
4207090df5aSMatthew Dillon 
4217090df5aSMatthew Dillon 	if (scan->u.bmu_avail == 0)  {
4227090df5aSMatthew Dillon 		/*
4237090df5aSMatthew Dillon 		 * ALL-ALLOCATED special case
4247090df5aSMatthew Dillon 		 */
4257090df5aSMatthew Dillon 		scan->bm_bighint = count;
4267090df5aSMatthew Dillon 		return(SWAPBLK_NONE);
4277090df5aSMatthew Dillon 	}
4287090df5aSMatthew Dillon 
4297090df5aSMatthew Dillon 	if (scan->u.bmu_avail == radix) {
43057e6d29bSMatthew Dillon 		radix /= BLIST_META_RADIX;
4317090df5aSMatthew Dillon 
4327090df5aSMatthew Dillon 		/*
4337090df5aSMatthew Dillon 		 * ALL-FREE special case, initialize uninitialize
4347090df5aSMatthew Dillon 		 * sublevel.
4357090df5aSMatthew Dillon 		 */
4367090df5aSMatthew Dillon 		for (i = 1; i <= skip; i += next_skip) {
4377090df5aSMatthew Dillon 			if (scan[i].bm_bighint == (daddr_t)-1)
4387090df5aSMatthew Dillon 				break;
4397090df5aSMatthew Dillon 			if (next_skip == 1) {
4407090df5aSMatthew Dillon 				scan[i].u.bmu_bitmap = (u_daddr_t)-1;
4417090df5aSMatthew Dillon 				scan[i].bm_bighint = BLIST_BMAP_RADIX;
4427090df5aSMatthew Dillon 			} else {
4437090df5aSMatthew Dillon 				scan[i].bm_bighint = radix;
4447090df5aSMatthew Dillon 				scan[i].u.bmu_avail = radix;
4457090df5aSMatthew Dillon 			}
4467090df5aSMatthew Dillon 		}
4477090df5aSMatthew Dillon 	} else {
44857e6d29bSMatthew Dillon 		radix /= BLIST_META_RADIX;
4497090df5aSMatthew Dillon 	}
4507090df5aSMatthew Dillon 
4517090df5aSMatthew Dillon 	for (i = 1; i <= skip; i += next_skip) {
4527090df5aSMatthew Dillon 		if (count <= scan[i].bm_bighint) {
4537090df5aSMatthew Dillon 			/*
4547090df5aSMatthew Dillon 			 * count fits in object
4557090df5aSMatthew Dillon 			 */
4567090df5aSMatthew Dillon 			daddr_t r;
4577090df5aSMatthew Dillon 			if (next_skip == 1) {
4587090df5aSMatthew Dillon 				r = blst_leaf_alloc(&scan[i], blk, count);
4597090df5aSMatthew Dillon 			} else {
4607090df5aSMatthew Dillon 				r = blst_meta_alloc(&scan[i], blk, count, radix, next_skip - 1);
4617090df5aSMatthew Dillon 			}
4627090df5aSMatthew Dillon 			if (r != SWAPBLK_NONE) {
4637090df5aSMatthew Dillon 				scan->u.bmu_avail -= count;
4647090df5aSMatthew Dillon 				if (scan->bm_bighint > scan->u.bmu_avail)
4657090df5aSMatthew Dillon 					scan->bm_bighint = scan->u.bmu_avail;
4667090df5aSMatthew Dillon 				return(r);
4677090df5aSMatthew Dillon 			}
4687090df5aSMatthew Dillon 		} else if (scan[i].bm_bighint == (daddr_t)-1) {
4697090df5aSMatthew Dillon 			/*
4707090df5aSMatthew Dillon 			 * Terminator
4717090df5aSMatthew Dillon 			 */
4727090df5aSMatthew Dillon 			break;
4737090df5aSMatthew Dillon 		} else if (count > radix) {
4747090df5aSMatthew Dillon 			/*
4757090df5aSMatthew Dillon 			 * count does not fit in object even if it were
4767090df5aSMatthew Dillon 			 * complete free.
4777090df5aSMatthew Dillon 			 */
4787090df5aSMatthew Dillon 			panic("blist_meta_alloc: allocation too large");
4797090df5aSMatthew Dillon 		}
4807090df5aSMatthew Dillon 		blk += radix;
4817090df5aSMatthew Dillon 	}
4827090df5aSMatthew Dillon 
4837090df5aSMatthew Dillon 	/*
4847090df5aSMatthew Dillon 	 * We couldn't allocate count in this subtree, update bighint.
4857090df5aSMatthew Dillon 	 */
4867090df5aSMatthew Dillon 	if (scan->bm_bighint >= count)
4877090df5aSMatthew Dillon 		scan->bm_bighint = count - 1;
4887090df5aSMatthew Dillon 	return(SWAPBLK_NONE);
4897090df5aSMatthew Dillon }
4907090df5aSMatthew Dillon 
4917090df5aSMatthew Dillon /*
4927090df5aSMatthew Dillon  * BLST_LEAF_FREE() -	free allocated block from leaf bitmap
4937090df5aSMatthew Dillon  *
4947090df5aSMatthew Dillon  */
4957090df5aSMatthew Dillon 
4967090df5aSMatthew Dillon static void
4977090df5aSMatthew Dillon blst_leaf_free(
4987090df5aSMatthew Dillon 	blmeta_t *scan,
4997090df5aSMatthew Dillon 	daddr_t blk,
5007090df5aSMatthew Dillon 	int count
5017090df5aSMatthew Dillon ) {
5027090df5aSMatthew Dillon 	/*
5037090df5aSMatthew Dillon 	 * free some data in this bitmap
5047090df5aSMatthew Dillon 	 *
5057090df5aSMatthew Dillon 	 * e.g.
5067090df5aSMatthew Dillon 	 *	0000111111111110000
5077090df5aSMatthew Dillon 	 *          \_________/\__/
5087090df5aSMatthew Dillon 	 *		v        n
5097090df5aSMatthew Dillon 	 */
5107090df5aSMatthew Dillon 	int n = blk & (BLIST_BMAP_RADIX - 1);
5117090df5aSMatthew Dillon 	u_daddr_t mask;
5127090df5aSMatthew Dillon 
5137090df5aSMatthew Dillon 	mask = ((u_daddr_t)-1 << n) &
5147090df5aSMatthew Dillon 	    ((u_daddr_t)-1 >> (BLIST_BMAP_RADIX - count - n));
5157090df5aSMatthew Dillon 
5167090df5aSMatthew Dillon 	if (scan->u.bmu_bitmap & mask)
5177090df5aSMatthew Dillon 		panic("blst_radix_free: freeing free block");
5187090df5aSMatthew Dillon 	scan->u.bmu_bitmap |= mask;
5197090df5aSMatthew Dillon 
5207090df5aSMatthew Dillon 	/*
5217090df5aSMatthew Dillon 	 * We could probably do a better job here.  We are required to make
5227090df5aSMatthew Dillon 	 * bighint at least as large as the biggest contiguous block of
5237090df5aSMatthew Dillon 	 * data.  If we just shoehorn it, a little extra overhead will
5247090df5aSMatthew Dillon 	 * be incured on the next allocation (but only that one typically).
5257090df5aSMatthew Dillon 	 */
5267090df5aSMatthew Dillon 	scan->bm_bighint = BLIST_BMAP_RADIX;
5277090df5aSMatthew Dillon }
5287090df5aSMatthew Dillon 
5297090df5aSMatthew Dillon /*
5307090df5aSMatthew Dillon  * BLST_META_FREE() - free allocated blocks from radix tree meta info
5317090df5aSMatthew Dillon  *
5327090df5aSMatthew Dillon  *	This support routine frees a range of blocks from the bitmap.
5337090df5aSMatthew Dillon  *	The range must be entirely enclosed by this radix node.  If a
5347090df5aSMatthew Dillon  *	meta node, we break the range down recursively to free blocks
5357090df5aSMatthew Dillon  *	in subnodes (which means that this code can free an arbitrary
5367090df5aSMatthew Dillon  *	range whereas the allocation code cannot allocate an arbitrary
5377090df5aSMatthew Dillon  *	range).
5387090df5aSMatthew Dillon  */
5397090df5aSMatthew Dillon 
5407090df5aSMatthew Dillon static void
5417090df5aSMatthew Dillon blst_meta_free(
5427090df5aSMatthew Dillon 	blmeta_t *scan,
5437090df5aSMatthew Dillon 	daddr_t freeBlk,
5447090df5aSMatthew Dillon 	daddr_t count,
5457090df5aSMatthew Dillon 	daddr_t radix,
5467090df5aSMatthew Dillon 	int skip,
5477090df5aSMatthew Dillon 	daddr_t blk
5487090df5aSMatthew Dillon ) {
5497090df5aSMatthew Dillon 	int i;
55057e6d29bSMatthew Dillon 	int next_skip = ((u_int)skip / BLIST_META_RADIX);
5517090df5aSMatthew Dillon 
5527090df5aSMatthew Dillon #if 0
5531ede983cSDag-Erling Smørgrav 	printf("free (%llx,%lld) FROM (%llx,%lld)\n",
55492da00bbSMatthew Dillon 	    (long long)freeBlk, (long long)count,
55592da00bbSMatthew Dillon 	    (long long)blk, (long long)radix
5567090df5aSMatthew Dillon 	);
5577090df5aSMatthew Dillon #endif
5587090df5aSMatthew Dillon 
5597090df5aSMatthew Dillon 	if (scan->u.bmu_avail == 0) {
5607090df5aSMatthew Dillon 		/*
5617090df5aSMatthew Dillon 		 * ALL-ALLOCATED special case, with possible
5627090df5aSMatthew Dillon 		 * shortcut to ALL-FREE special case.
5637090df5aSMatthew Dillon 		 */
5647090df5aSMatthew Dillon 		scan->u.bmu_avail = count;
5657090df5aSMatthew Dillon 		scan->bm_bighint = count;
5667090df5aSMatthew Dillon 
5677090df5aSMatthew Dillon 		if (count != radix)  {
5687090df5aSMatthew Dillon 			for (i = 1; i <= skip; i += next_skip) {
5697090df5aSMatthew Dillon 				if (scan[i].bm_bighint == (daddr_t)-1)
5707090df5aSMatthew Dillon 					break;
5717090df5aSMatthew Dillon 				scan[i].bm_bighint = 0;
5727090df5aSMatthew Dillon 				if (next_skip == 1) {
5737090df5aSMatthew Dillon 					scan[i].u.bmu_bitmap = 0;
5747090df5aSMatthew Dillon 				} else {
5757090df5aSMatthew Dillon 					scan[i].u.bmu_avail = 0;
5767090df5aSMatthew Dillon 				}
5777090df5aSMatthew Dillon 			}
5787090df5aSMatthew Dillon 			/* fall through */
5797090df5aSMatthew Dillon 		}
5807090df5aSMatthew Dillon 	} else {
5817090df5aSMatthew Dillon 		scan->u.bmu_avail += count;
5827090df5aSMatthew Dillon 		/* scan->bm_bighint = radix; */
5837090df5aSMatthew Dillon 	}
5847090df5aSMatthew Dillon 
5857090df5aSMatthew Dillon 	/*
5867090df5aSMatthew Dillon 	 * ALL-FREE special case.
5877090df5aSMatthew Dillon 	 */
5887090df5aSMatthew Dillon 
5897090df5aSMatthew Dillon 	if (scan->u.bmu_avail == radix)
5907090df5aSMatthew Dillon 		return;
5917090df5aSMatthew Dillon 	if (scan->u.bmu_avail > radix)
592bdc9a8d0SJohn Baldwin 		panic("blst_meta_free: freeing already free blocks (%lld) %lld/%lld",
593bdc9a8d0SJohn Baldwin 		    (long long)count, (long long)scan->u.bmu_avail,
594bdc9a8d0SJohn Baldwin 		    (long long)radix);
5957090df5aSMatthew Dillon 
5967090df5aSMatthew Dillon 	/*
5977090df5aSMatthew Dillon 	 * Break the free down into its components
5987090df5aSMatthew Dillon 	 */
5997090df5aSMatthew Dillon 
60057e6d29bSMatthew Dillon 	radix /= BLIST_META_RADIX;
6017090df5aSMatthew Dillon 
6027090df5aSMatthew Dillon 	i = (freeBlk - blk) / radix;
6037090df5aSMatthew Dillon 	blk += i * radix;
6047090df5aSMatthew Dillon 	i = i * next_skip + 1;
6057090df5aSMatthew Dillon 
6067090df5aSMatthew Dillon 	while (i <= skip && blk < freeBlk + count) {
6077090df5aSMatthew Dillon 		daddr_t v;
6087090df5aSMatthew Dillon 
6097090df5aSMatthew Dillon 		v = blk + radix - freeBlk;
6107090df5aSMatthew Dillon 		if (v > count)
6117090df5aSMatthew Dillon 			v = count;
6127090df5aSMatthew Dillon 
6137090df5aSMatthew Dillon 		if (scan->bm_bighint == (daddr_t)-1)
6147090df5aSMatthew Dillon 			panic("blst_meta_free: freeing unexpected range");
6157090df5aSMatthew Dillon 
6167090df5aSMatthew Dillon 		if (next_skip == 1) {
6177090df5aSMatthew Dillon 			blst_leaf_free(&scan[i], freeBlk, v);
6187090df5aSMatthew Dillon 		} else {
6197090df5aSMatthew Dillon 			blst_meta_free(&scan[i], freeBlk, v, radix, next_skip - 1, blk);
6207090df5aSMatthew Dillon 		}
6217090df5aSMatthew Dillon 		if (scan->bm_bighint < scan[i].bm_bighint)
6227090df5aSMatthew Dillon 		    scan->bm_bighint = scan[i].bm_bighint;
6237090df5aSMatthew Dillon 		count -= v;
6247090df5aSMatthew Dillon 		freeBlk += v;
6257090df5aSMatthew Dillon 		blk += radix;
6267090df5aSMatthew Dillon 		i += next_skip;
6277090df5aSMatthew Dillon 	}
6287090df5aSMatthew Dillon }
6297090df5aSMatthew Dillon 
6307090df5aSMatthew Dillon /*
6317090df5aSMatthew Dillon  * BLIST_RADIX_COPY() - copy one radix tree to another
6327090df5aSMatthew Dillon  *
6337090df5aSMatthew Dillon  *	Locates free space in the source tree and frees it in the destination
6347090df5aSMatthew Dillon  *	tree.  The space may not already be free in the destination.
6357090df5aSMatthew Dillon  */
6367090df5aSMatthew Dillon 
6377090df5aSMatthew Dillon static void blst_copy(
6387090df5aSMatthew Dillon 	blmeta_t *scan,
6397090df5aSMatthew Dillon 	daddr_t blk,
6407090df5aSMatthew Dillon 	daddr_t radix,
6417090df5aSMatthew Dillon 	daddr_t skip,
6427090df5aSMatthew Dillon 	blist_t dest,
6437090df5aSMatthew Dillon 	daddr_t count
6447090df5aSMatthew Dillon ) {
6457090df5aSMatthew Dillon 	int next_skip;
6467090df5aSMatthew Dillon 	int i;
6477090df5aSMatthew Dillon 
6487090df5aSMatthew Dillon 	/*
6497090df5aSMatthew Dillon 	 * Leaf node
6507090df5aSMatthew Dillon 	 */
6517090df5aSMatthew Dillon 
6527090df5aSMatthew Dillon 	if (radix == BLIST_BMAP_RADIX) {
6537090df5aSMatthew Dillon 		u_daddr_t v = scan->u.bmu_bitmap;
6547090df5aSMatthew Dillon 
6557090df5aSMatthew Dillon 		if (v == (u_daddr_t)-1) {
6567090df5aSMatthew Dillon 			blist_free(dest, blk, count);
6577090df5aSMatthew Dillon 		} else if (v != 0) {
6587090df5aSMatthew Dillon 			int i;
6597090df5aSMatthew Dillon 
6607090df5aSMatthew Dillon 			for (i = 0; i < BLIST_BMAP_RADIX && i < count; ++i) {
6617090df5aSMatthew Dillon 				if (v & (1 << i))
6627090df5aSMatthew Dillon 					blist_free(dest, blk + i, 1);
6637090df5aSMatthew Dillon 			}
6647090df5aSMatthew Dillon 		}
6657090df5aSMatthew Dillon 		return;
6667090df5aSMatthew Dillon 	}
6677090df5aSMatthew Dillon 
6687090df5aSMatthew Dillon 	/*
6697090df5aSMatthew Dillon 	 * Meta node
6707090df5aSMatthew Dillon 	 */
6717090df5aSMatthew Dillon 
6727090df5aSMatthew Dillon 	if (scan->u.bmu_avail == 0) {
6737090df5aSMatthew Dillon 		/*
6747090df5aSMatthew Dillon 		 * Source all allocated, leave dest allocated
6757090df5aSMatthew Dillon 		 */
6767090df5aSMatthew Dillon 		return;
6777090df5aSMatthew Dillon 	}
6787090df5aSMatthew Dillon 	if (scan->u.bmu_avail == radix) {
6797090df5aSMatthew Dillon 		/*
6807090df5aSMatthew Dillon 		 * Source all free, free entire dest
6817090df5aSMatthew Dillon 		 */
6827090df5aSMatthew Dillon 		if (count < radix)
6837090df5aSMatthew Dillon 			blist_free(dest, blk, count);
6847090df5aSMatthew Dillon 		else
6857090df5aSMatthew Dillon 			blist_free(dest, blk, radix);
6867090df5aSMatthew Dillon 		return;
6877090df5aSMatthew Dillon 	}
6887090df5aSMatthew Dillon 
6897090df5aSMatthew Dillon 
69057e6d29bSMatthew Dillon 	radix /= BLIST_META_RADIX;
69157e6d29bSMatthew Dillon 	next_skip = ((u_int)skip / BLIST_META_RADIX);
6927090df5aSMatthew Dillon 
6937090df5aSMatthew Dillon 	for (i = 1; count && i <= skip; i += next_skip) {
6947090df5aSMatthew Dillon 		if (scan[i].bm_bighint == (daddr_t)-1)
6957090df5aSMatthew Dillon 			break;
6967090df5aSMatthew Dillon 
6977090df5aSMatthew Dillon 		if (count >= radix) {
6987090df5aSMatthew Dillon 			blst_copy(
6997090df5aSMatthew Dillon 			    &scan[i],
7007090df5aSMatthew Dillon 			    blk,
7017090df5aSMatthew Dillon 			    radix,
7027090df5aSMatthew Dillon 			    next_skip - 1,
7037090df5aSMatthew Dillon 			    dest,
7047090df5aSMatthew Dillon 			    radix
7057090df5aSMatthew Dillon 			);
7067090df5aSMatthew Dillon 			count -= radix;
7077090df5aSMatthew Dillon 		} else {
7087090df5aSMatthew Dillon 			if (count) {
7097090df5aSMatthew Dillon 				blst_copy(
7107090df5aSMatthew Dillon 				    &scan[i],
7117090df5aSMatthew Dillon 				    blk,
7127090df5aSMatthew Dillon 				    radix,
7137090df5aSMatthew Dillon 				    next_skip - 1,
7147090df5aSMatthew Dillon 				    dest,
7157090df5aSMatthew Dillon 				    count
7167090df5aSMatthew Dillon 				);
7177090df5aSMatthew Dillon 			}
7187090df5aSMatthew Dillon 			count = 0;
7197090df5aSMatthew Dillon 		}
7207090df5aSMatthew Dillon 		blk += radix;
7217090df5aSMatthew Dillon 	}
7227090df5aSMatthew Dillon }
7237090df5aSMatthew Dillon 
7247090df5aSMatthew Dillon /*
72592da00bbSMatthew Dillon  * BLST_LEAF_FILL() -	allocate specific blocks in leaf bitmap
72692da00bbSMatthew Dillon  *
72792da00bbSMatthew Dillon  *	This routine allocates all blocks in the specified range
72892da00bbSMatthew Dillon  *	regardless of any existing allocations in that range.  Returns
72992da00bbSMatthew Dillon  *	the number of blocks allocated by the call.
73092da00bbSMatthew Dillon  */
73192da00bbSMatthew Dillon 
73292da00bbSMatthew Dillon static int
73392da00bbSMatthew Dillon blst_leaf_fill(blmeta_t *scan, daddr_t blk, int count)
73492da00bbSMatthew Dillon {
73592da00bbSMatthew Dillon 	int n = blk & (BLIST_BMAP_RADIX - 1);
73692da00bbSMatthew Dillon 	int nblks;
73792da00bbSMatthew Dillon 	u_daddr_t mask, bitmap;
73892da00bbSMatthew Dillon 
73992da00bbSMatthew Dillon 	mask = ((u_daddr_t)-1 << n) &
74092da00bbSMatthew Dillon 	    ((u_daddr_t)-1 >> (BLIST_BMAP_RADIX - count - n));
74192da00bbSMatthew Dillon 
74292da00bbSMatthew Dillon 	/* Count the number of blocks we're about to allocate */
74392da00bbSMatthew Dillon 	bitmap = scan->u.bmu_bitmap & mask;
74492da00bbSMatthew Dillon 	for (nblks = 0; bitmap != 0; nblks++)
74592da00bbSMatthew Dillon 		bitmap &= bitmap - 1;
74692da00bbSMatthew Dillon 
74792da00bbSMatthew Dillon 	scan->u.bmu_bitmap &= ~mask;
74892da00bbSMatthew Dillon 	return nblks;
74992da00bbSMatthew Dillon }
75092da00bbSMatthew Dillon 
75192da00bbSMatthew Dillon /*
75292da00bbSMatthew Dillon  * BLIST_META_FILL() -	allocate specific blocks at a meta node
75392da00bbSMatthew Dillon  *
75492da00bbSMatthew Dillon  *	This routine allocates the specified range of blocks,
75592da00bbSMatthew Dillon  *	regardless of any existing allocations in the range.  The
75692da00bbSMatthew Dillon  *	range must be within the extent of this node.  Returns the
75792da00bbSMatthew Dillon  *	number of blocks allocated by the call.
75892da00bbSMatthew Dillon  */
75992da00bbSMatthew Dillon static int
76092da00bbSMatthew Dillon blst_meta_fill(
76192da00bbSMatthew Dillon 	blmeta_t *scan,
76292da00bbSMatthew Dillon 	daddr_t allocBlk,
76392da00bbSMatthew Dillon 	daddr_t count,
76492da00bbSMatthew Dillon 	daddr_t radix,
76592da00bbSMatthew Dillon 	int skip,
76692da00bbSMatthew Dillon 	daddr_t blk
76792da00bbSMatthew Dillon ) {
76892da00bbSMatthew Dillon 	int i;
76957e6d29bSMatthew Dillon 	int next_skip = ((u_int)skip / BLIST_META_RADIX);
77092da00bbSMatthew Dillon 	int nblks = 0;
77192da00bbSMatthew Dillon 
77292da00bbSMatthew Dillon 	if (count == radix || scan->u.bmu_avail == 0)  {
77392da00bbSMatthew Dillon 		/*
77492da00bbSMatthew Dillon 		 * ALL-ALLOCATED special case
77592da00bbSMatthew Dillon 		 */
77692da00bbSMatthew Dillon 		nblks = scan->u.bmu_avail;
77792da00bbSMatthew Dillon 		scan->u.bmu_avail = 0;
77892da00bbSMatthew Dillon 		scan->bm_bighint = count;
77992da00bbSMatthew Dillon 		return nblks;
78092da00bbSMatthew Dillon 	}
78192da00bbSMatthew Dillon 
78292da00bbSMatthew Dillon 	if (scan->u.bmu_avail == radix) {
78357e6d29bSMatthew Dillon 		radix /= BLIST_META_RADIX;
78492da00bbSMatthew Dillon 
78592da00bbSMatthew Dillon 		/*
78692da00bbSMatthew Dillon 		 * ALL-FREE special case, initialize sublevel
78792da00bbSMatthew Dillon 		 */
78892da00bbSMatthew Dillon 		for (i = 1; i <= skip; i += next_skip) {
78992da00bbSMatthew Dillon 			if (scan[i].bm_bighint == (daddr_t)-1)
79092da00bbSMatthew Dillon 				break;
79192da00bbSMatthew Dillon 			if (next_skip == 1) {
79292da00bbSMatthew Dillon 				scan[i].u.bmu_bitmap = (u_daddr_t)-1;
79392da00bbSMatthew Dillon 				scan[i].bm_bighint = BLIST_BMAP_RADIX;
79492da00bbSMatthew Dillon 			} else {
79592da00bbSMatthew Dillon 				scan[i].bm_bighint = radix;
79692da00bbSMatthew Dillon 				scan[i].u.bmu_avail = radix;
79792da00bbSMatthew Dillon 			}
79892da00bbSMatthew Dillon 		}
79992da00bbSMatthew Dillon 	} else {
80057e6d29bSMatthew Dillon 		radix /= BLIST_META_RADIX;
80192da00bbSMatthew Dillon 	}
80292da00bbSMatthew Dillon 
80392da00bbSMatthew Dillon 	if (count > radix)
80492da00bbSMatthew Dillon 		panic("blist_meta_fill: allocation too large");
80592da00bbSMatthew Dillon 
80692da00bbSMatthew Dillon 	i = (allocBlk - blk) / radix;
80792da00bbSMatthew Dillon 	blk += i * radix;
80892da00bbSMatthew Dillon 	i = i * next_skip + 1;
80992da00bbSMatthew Dillon 
81092da00bbSMatthew Dillon 	while (i <= skip && blk < allocBlk + count) {
81192da00bbSMatthew Dillon 		daddr_t v;
81292da00bbSMatthew Dillon 
81392da00bbSMatthew Dillon 		v = blk + radix - allocBlk;
81492da00bbSMatthew Dillon 		if (v > count)
81592da00bbSMatthew Dillon 			v = count;
81692da00bbSMatthew Dillon 
81792da00bbSMatthew Dillon 		if (scan->bm_bighint == (daddr_t)-1)
81892da00bbSMatthew Dillon 			panic("blst_meta_fill: filling unexpected range");
81992da00bbSMatthew Dillon 
82092da00bbSMatthew Dillon 		if (next_skip == 1) {
82192da00bbSMatthew Dillon 			nblks += blst_leaf_fill(&scan[i], allocBlk, v);
82292da00bbSMatthew Dillon 		} else {
82392da00bbSMatthew Dillon 			nblks += blst_meta_fill(&scan[i], allocBlk, v,
82492da00bbSMatthew Dillon 			    radix, next_skip - 1, blk);
82592da00bbSMatthew Dillon 		}
82692da00bbSMatthew Dillon 		count -= v;
82792da00bbSMatthew Dillon 		allocBlk += v;
82892da00bbSMatthew Dillon 		blk += radix;
82992da00bbSMatthew Dillon 		i += next_skip;
83092da00bbSMatthew Dillon 	}
83192da00bbSMatthew Dillon 	scan->u.bmu_avail -= nblks;
83292da00bbSMatthew Dillon 	return nblks;
83392da00bbSMatthew Dillon }
83492da00bbSMatthew Dillon 
83592da00bbSMatthew Dillon /*
8367090df5aSMatthew Dillon  * BLST_RADIX_INIT() - initialize radix tree
8377090df5aSMatthew Dillon  *
8387090df5aSMatthew Dillon  *	Initialize our meta structures and bitmaps and calculate the exact
8397090df5aSMatthew Dillon  *	amount of space required to manage 'count' blocks - this space may
840f565f395SEitan Adler  *	be considerably less than the calculated radix due to the large
8417090df5aSMatthew Dillon  *	RADIX values we use.
8427090df5aSMatthew Dillon  */
8437090df5aSMatthew Dillon 
8447090df5aSMatthew Dillon static daddr_t
8457090df5aSMatthew Dillon blst_radix_init(blmeta_t *scan, daddr_t radix, int skip, daddr_t count)
8467090df5aSMatthew Dillon {
8477090df5aSMatthew Dillon 	int i;
8487090df5aSMatthew Dillon 	int next_skip;
8497090df5aSMatthew Dillon 	daddr_t memindex = 0;
8507090df5aSMatthew Dillon 
8517090df5aSMatthew Dillon 	/*
8527090df5aSMatthew Dillon 	 * Leaf node
8537090df5aSMatthew Dillon 	 */
8547090df5aSMatthew Dillon 
8557090df5aSMatthew Dillon 	if (radix == BLIST_BMAP_RADIX) {
8567090df5aSMatthew Dillon 		if (scan) {
8577090df5aSMatthew Dillon 			scan->bm_bighint = 0;
8587090df5aSMatthew Dillon 			scan->u.bmu_bitmap = 0;
8597090df5aSMatthew Dillon 		}
8607090df5aSMatthew Dillon 		return(memindex);
8617090df5aSMatthew Dillon 	}
8627090df5aSMatthew Dillon 
8637090df5aSMatthew Dillon 	/*
8647090df5aSMatthew Dillon 	 * Meta node.  If allocating the entire object we can special
8657090df5aSMatthew Dillon 	 * case it.  However, we need to figure out how much memory
8667090df5aSMatthew Dillon 	 * is required to manage 'count' blocks, so we continue on anyway.
8677090df5aSMatthew Dillon 	 */
8687090df5aSMatthew Dillon 
8697090df5aSMatthew Dillon 	if (scan) {
8707090df5aSMatthew Dillon 		scan->bm_bighint = 0;
8717090df5aSMatthew Dillon 		scan->u.bmu_avail = 0;
8727090df5aSMatthew Dillon 	}
8737090df5aSMatthew Dillon 
87457e6d29bSMatthew Dillon 	radix /= BLIST_META_RADIX;
87557e6d29bSMatthew Dillon 	next_skip = ((u_int)skip / BLIST_META_RADIX);
8767090df5aSMatthew Dillon 
8777090df5aSMatthew Dillon 	for (i = 1; i <= skip; i += next_skip) {
8787090df5aSMatthew Dillon 		if (count >= radix) {
8797090df5aSMatthew Dillon 			/*
8807090df5aSMatthew Dillon 			 * Allocate the entire object
8817090df5aSMatthew Dillon 			 */
8827090df5aSMatthew Dillon 			memindex = i + blst_radix_init(
8837090df5aSMatthew Dillon 			    ((scan) ? &scan[i] : NULL),
8847090df5aSMatthew Dillon 			    radix,
8857090df5aSMatthew Dillon 			    next_skip - 1,
8867090df5aSMatthew Dillon 			    radix
8877090df5aSMatthew Dillon 			);
8887090df5aSMatthew Dillon 			count -= radix;
8897090df5aSMatthew Dillon 		} else if (count > 0) {
8907090df5aSMatthew Dillon 			/*
8917090df5aSMatthew Dillon 			 * Allocate a partial object
8927090df5aSMatthew Dillon 			 */
8937090df5aSMatthew Dillon 			memindex = i + blst_radix_init(
8947090df5aSMatthew Dillon 			    ((scan) ? &scan[i] : NULL),
8957090df5aSMatthew Dillon 			    radix,
8967090df5aSMatthew Dillon 			    next_skip - 1,
8977090df5aSMatthew Dillon 			    count
8987090df5aSMatthew Dillon 			);
8997090df5aSMatthew Dillon 			count = 0;
9007090df5aSMatthew Dillon 		} else {
9017090df5aSMatthew Dillon 			/*
9027090df5aSMatthew Dillon 			 * Add terminator and break out
9037090df5aSMatthew Dillon 			 */
9047090df5aSMatthew Dillon 			if (scan)
9057090df5aSMatthew Dillon 				scan[i].bm_bighint = (daddr_t)-1;
9067090df5aSMatthew Dillon 			break;
9077090df5aSMatthew Dillon 		}
9087090df5aSMatthew Dillon 	}
9097090df5aSMatthew Dillon 	if (memindex < i)
9107090df5aSMatthew Dillon 		memindex = i;
9117090df5aSMatthew Dillon 	return(memindex);
9127090df5aSMatthew Dillon }
9137090df5aSMatthew Dillon 
9147090df5aSMatthew Dillon #ifdef BLIST_DEBUG
9157090df5aSMatthew Dillon 
9167090df5aSMatthew Dillon static void
9177090df5aSMatthew Dillon blst_radix_print(blmeta_t *scan, daddr_t blk, daddr_t radix, int skip, int tab)
9187090df5aSMatthew Dillon {
9197090df5aSMatthew Dillon 	int i;
9207090df5aSMatthew Dillon 	int next_skip;
9217090df5aSMatthew Dillon 	int lastState = 0;
9227090df5aSMatthew Dillon 
9237090df5aSMatthew Dillon 	if (radix == BLIST_BMAP_RADIX) {
9247090df5aSMatthew Dillon 		printf(
92592da00bbSMatthew Dillon 		    "%*.*s(%08llx,%lld): bitmap %08llx big=%lld\n",
9267090df5aSMatthew Dillon 		    tab, tab, "",
92792da00bbSMatthew Dillon 		    (long long)blk, (long long)radix,
92892da00bbSMatthew Dillon 		    (long long)scan->u.bmu_bitmap,
92992da00bbSMatthew Dillon 		    (long long)scan->bm_bighint
9307090df5aSMatthew Dillon 		);
9317090df5aSMatthew Dillon 		return;
9327090df5aSMatthew Dillon 	}
9337090df5aSMatthew Dillon 
9347090df5aSMatthew Dillon 	if (scan->u.bmu_avail == 0) {
9357090df5aSMatthew Dillon 		printf(
93692da00bbSMatthew Dillon 		    "%*.*s(%08llx,%lld) ALL ALLOCATED\n",
9377090df5aSMatthew Dillon 		    tab, tab, "",
93892da00bbSMatthew Dillon 		    (long long)blk,
93992da00bbSMatthew Dillon 		    (long long)radix
9407090df5aSMatthew Dillon 		);
9417090df5aSMatthew Dillon 		return;
9427090df5aSMatthew Dillon 	}
9437090df5aSMatthew Dillon 	if (scan->u.bmu_avail == radix) {
9447090df5aSMatthew Dillon 		printf(
94592da00bbSMatthew Dillon 		    "%*.*s(%08llx,%lld) ALL FREE\n",
9467090df5aSMatthew Dillon 		    tab, tab, "",
94792da00bbSMatthew Dillon 		    (long long)blk,
94892da00bbSMatthew Dillon 		    (long long)radix
9497090df5aSMatthew Dillon 		);
9507090df5aSMatthew Dillon 		return;
9517090df5aSMatthew Dillon 	}
9527090df5aSMatthew Dillon 
9537090df5aSMatthew Dillon 	printf(
95492da00bbSMatthew Dillon 	    "%*.*s(%08llx,%lld): subtree (%lld/%lld) big=%lld {\n",
9557090df5aSMatthew Dillon 	    tab, tab, "",
95692da00bbSMatthew Dillon 	    (long long)blk, (long long)radix,
95792da00bbSMatthew Dillon 	    (long long)scan->u.bmu_avail,
95892da00bbSMatthew Dillon 	    (long long)radix,
95992da00bbSMatthew Dillon 	    (long long)scan->bm_bighint
9607090df5aSMatthew Dillon 	);
9617090df5aSMatthew Dillon 
96257e6d29bSMatthew Dillon 	radix /= BLIST_META_RADIX;
96357e6d29bSMatthew Dillon 	next_skip = ((u_int)skip / BLIST_META_RADIX);
9647090df5aSMatthew Dillon 	tab += 4;
9657090df5aSMatthew Dillon 
9667090df5aSMatthew Dillon 	for (i = 1; i <= skip; i += next_skip) {
9677090df5aSMatthew Dillon 		if (scan[i].bm_bighint == (daddr_t)-1) {
9687090df5aSMatthew Dillon 			printf(
96992da00bbSMatthew Dillon 			    "%*.*s(%08llx,%lld): Terminator\n",
9707090df5aSMatthew Dillon 			    tab, tab, "",
97192da00bbSMatthew Dillon 			    (long long)blk, (long long)radix
9727090df5aSMatthew Dillon 			);
9737090df5aSMatthew Dillon 			lastState = 0;
9747090df5aSMatthew Dillon 			break;
9757090df5aSMatthew Dillon 		}
9767090df5aSMatthew Dillon 		blst_radix_print(
9777090df5aSMatthew Dillon 		    &scan[i],
9787090df5aSMatthew Dillon 		    blk,
9797090df5aSMatthew Dillon 		    radix,
9807090df5aSMatthew Dillon 		    next_skip - 1,
9817090df5aSMatthew Dillon 		    tab
9827090df5aSMatthew Dillon 		);
9837090df5aSMatthew Dillon 		blk += radix;
9847090df5aSMatthew Dillon 	}
9857090df5aSMatthew Dillon 	tab -= 4;
9867090df5aSMatthew Dillon 
9877090df5aSMatthew Dillon 	printf(
9887090df5aSMatthew Dillon 	    "%*.*s}\n",
9897090df5aSMatthew Dillon 	    tab, tab, ""
9907090df5aSMatthew Dillon 	);
9917090df5aSMatthew Dillon }
9927090df5aSMatthew Dillon 
9937090df5aSMatthew Dillon #endif
9947090df5aSMatthew Dillon 
9957090df5aSMatthew Dillon #ifdef BLIST_DEBUG
9967090df5aSMatthew Dillon 
9977090df5aSMatthew Dillon int
9987090df5aSMatthew Dillon main(int ac, char **av)
9997090df5aSMatthew Dillon {
10007090df5aSMatthew Dillon 	int size = 1024;
10017090df5aSMatthew Dillon 	int i;
10027090df5aSMatthew Dillon 	blist_t bl;
10037090df5aSMatthew Dillon 
10047090df5aSMatthew Dillon 	for (i = 1; i < ac; ++i) {
10057090df5aSMatthew Dillon 		const char *ptr = av[i];
10067090df5aSMatthew Dillon 		if (*ptr != '-') {
10077090df5aSMatthew Dillon 			size = strtol(ptr, NULL, 0);
10087090df5aSMatthew Dillon 			continue;
10097090df5aSMatthew Dillon 		}
10107090df5aSMatthew Dillon 		ptr += 2;
10117090df5aSMatthew Dillon 		fprintf(stderr, "Bad option: %s\n", ptr - 2);
10127090df5aSMatthew Dillon 		exit(1);
10137090df5aSMatthew Dillon 	}
1014c8c7ad92SKip Macy 	bl = blist_create(size, M_WAITOK);
10157090df5aSMatthew Dillon 	blist_free(bl, 0, size);
10167090df5aSMatthew Dillon 
10177090df5aSMatthew Dillon 	for (;;) {
10187090df5aSMatthew Dillon 		char buf[1024];
10197090df5aSMatthew Dillon 		daddr_t da = 0;
10207090df5aSMatthew Dillon 		daddr_t count = 0;
10217090df5aSMatthew Dillon 
10227090df5aSMatthew Dillon 
102392da00bbSMatthew Dillon 		printf("%lld/%lld/%lld> ", (long long)bl->bl_free,
102492da00bbSMatthew Dillon 		    (long long)size, (long long)bl->bl_radix);
10257090df5aSMatthew Dillon 		fflush(stdout);
10267090df5aSMatthew Dillon 		if (fgets(buf, sizeof(buf), stdin) == NULL)
10277090df5aSMatthew Dillon 			break;
10287090df5aSMatthew Dillon 		switch(buf[0]) {
10297090df5aSMatthew Dillon 		case 'r':
103092da00bbSMatthew Dillon 			if (sscanf(buf + 1, "%lld", &count) == 1) {
10317090df5aSMatthew Dillon 				blist_resize(&bl, count, 1);
10327090df5aSMatthew Dillon 			} else {
10337090df5aSMatthew Dillon 				printf("?\n");
10347090df5aSMatthew Dillon 			}
10357090df5aSMatthew Dillon 		case 'p':
10367090df5aSMatthew Dillon 			blist_print(bl);
10377090df5aSMatthew Dillon 			break;
10387090df5aSMatthew Dillon 		case 'a':
103992da00bbSMatthew Dillon 			if (sscanf(buf + 1, "%lld", &count) == 1) {
10407090df5aSMatthew Dillon 				daddr_t blk = blist_alloc(bl, count);
104192da00bbSMatthew Dillon 				printf("    R=%08llx\n", (long long)blk);
10427090df5aSMatthew Dillon 			} else {
10437090df5aSMatthew Dillon 				printf("?\n");
10447090df5aSMatthew Dillon 			}
10457090df5aSMatthew Dillon 			break;
10467090df5aSMatthew Dillon 		case 'f':
104792da00bbSMatthew Dillon 			if (sscanf(buf + 1, "%llx %lld",
104892da00bbSMatthew Dillon 			    (long long *)&da, (long long *)&count) == 2) {
10497090df5aSMatthew Dillon 				blist_free(bl, da, count);
10507090df5aSMatthew Dillon 			} else {
10517090df5aSMatthew Dillon 				printf("?\n");
10527090df5aSMatthew Dillon 			}
10537090df5aSMatthew Dillon 			break;
105492da00bbSMatthew Dillon 		case 'l':
105592da00bbSMatthew Dillon 			if (sscanf(buf + 1, "%llx %lld",
105692da00bbSMatthew Dillon 			    (long long *)&da, (long long *)&count) == 2) {
105792da00bbSMatthew Dillon 				printf("    n=%d\n",
105892da00bbSMatthew Dillon 				    blist_fill(bl, da, count));
105992da00bbSMatthew Dillon 			} else {
106092da00bbSMatthew Dillon 				printf("?\n");
106192da00bbSMatthew Dillon 			}
106292da00bbSMatthew Dillon 			break;
10637090df5aSMatthew Dillon 		case '?':
10647090df5aSMatthew Dillon 		case 'h':
10657090df5aSMatthew Dillon 			puts(
10667090df5aSMatthew Dillon 			    "p          -print\n"
10677090df5aSMatthew Dillon 			    "a %d       -allocate\n"
10687090df5aSMatthew Dillon 			    "f %x %d    -free\n"
106992da00bbSMatthew Dillon 			    "l %x %d    -fill\n"
10707090df5aSMatthew Dillon 			    "r %d       -resize\n"
10717090df5aSMatthew Dillon 			    "h/?        -help"
10727090df5aSMatthew Dillon 			);
10737090df5aSMatthew Dillon 			break;
10747090df5aSMatthew Dillon 		default:
10757090df5aSMatthew Dillon 			printf("?\n");
10767090df5aSMatthew Dillon 			break;
10777090df5aSMatthew Dillon 		}
10787090df5aSMatthew Dillon 	}
10797090df5aSMatthew Dillon 	return(0);
10807090df5aSMatthew Dillon }
10817090df5aSMatthew Dillon 
10827090df5aSMatthew Dillon void
10837090df5aSMatthew Dillon panic(const char *ctl, ...)
10847090df5aSMatthew Dillon {
10857090df5aSMatthew Dillon 	va_list va;
10867090df5aSMatthew Dillon 
10877090df5aSMatthew Dillon 	va_start(va, ctl);
10887090df5aSMatthew Dillon 	vfprintf(stderr, ctl, va);
10897090df5aSMatthew Dillon 	fprintf(stderr, "\n");
10907090df5aSMatthew Dillon 	va_end(va);
10917090df5aSMatthew Dillon 	exit(1);
10927090df5aSMatthew Dillon }
10937090df5aSMatthew Dillon 
10947090df5aSMatthew Dillon #endif
10957090df5aSMatthew Dillon 
1096