xref: /netbsd-src/sys/kern/subr_extent.c (revision 0b1a41caff4d2c565724b244a3ac45f65506af42)
1*0b1a41caSskrll /*	$NetBSD: subr_extent.c,v 1.89 2019/08/15 09:04:22 skrll Exp $	*/
26041e29fSthorpej 
36041e29fSthorpej /*-
4f0301095Syamt  * Copyright (c) 1996, 1998, 2007 The NetBSD Foundation, Inc.
56041e29fSthorpej  * All rights reserved.
66041e29fSthorpej  *
76041e29fSthorpej  * This code is derived from software contributed to The NetBSD Foundation
86041e29fSthorpej  * by Jason R. Thorpe and Matthias Drochner.
96041e29fSthorpej  *
106041e29fSthorpej  * Redistribution and use in source and binary forms, with or without
116041e29fSthorpej  * modification, are permitted provided that the following conditions
126041e29fSthorpej  * are met:
136041e29fSthorpej  * 1. Redistributions of source code must retain the above copyright
146041e29fSthorpej  *    notice, this list of conditions and the following disclaimer.
156041e29fSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
166041e29fSthorpej  *    notice, this list of conditions and the following disclaimer in the
176041e29fSthorpej  *    documentation and/or other materials provided with the distribution.
186041e29fSthorpej  *
196041e29fSthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206041e29fSthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216041e29fSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22324ce8d6Sjtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23324ce8d6Sjtc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246041e29fSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256041e29fSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266041e29fSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276041e29fSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286041e29fSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296041e29fSthorpej  * POSSIBILITY OF SUCH DAMAGE.
306041e29fSthorpej  */
316041e29fSthorpej 
326041e29fSthorpej /*
336041e29fSthorpej  * General purpose extent manager.
346041e29fSthorpej  */
356041e29fSthorpej 
36adc783d5Slukem #include <sys/cdefs.h>
37*0b1a41caSskrll __KERNEL_RCSID(0, "$NetBSD: subr_extent.c,v 1.89 2019/08/15 09:04:22 skrll Exp $");
38adc783d5Slukem 
39e77b6879Scgd #ifdef _KERNEL
40d8e04c90Spooka #ifdef _KERNEL_OPT
41d505b189Smartin #include "opt_lockdebug.h"
42d8e04c90Spooka #endif
43d505b189Smartin 
446041e29fSthorpej #include <sys/param.h>
456041e29fSthorpej #include <sys/extent.h>
4689c9828dSpara #include <sys/kmem.h>
474e465abfSpk #include <sys/pool.h>
486041e29fSthorpej #include <sys/time.h>
496041e29fSthorpej #include <sys/systm.h>
506041e29fSthorpej #include <sys/proc.h>
51e3203cc4Sthorpej 
522f159a1bSmrg #include <uvm/uvm_extern.h>
53e3203cc4Sthorpej 
546f107350Spk #elif defined(_EXTENT_TESTING)
55b308d13cSchristos 
56e77b6879Scgd /*
57e77b6879Scgd  * user-land definitions, so it can fit into a testing harness.
58e77b6879Scgd  */
59e77b6879Scgd #include <sys/param.h>
606f107350Spk #include <sys/pool.h>
61e77b6879Scgd #include <sys/extent.h>
6288ab7da9Sad 
63e77b6879Scgd #include <errno.h>
64e77b6879Scgd #include <stdlib.h>
65e77b6879Scgd #include <stdio.h>
66d13c150eSross #include <string.h>
67e77b6879Scgd 
no_op(void)68968e76ebSkre static inline void no_op(void) { return; }
69968e76ebSkre 
70a403c3e1Sjhawk /*
71a403c3e1Sjhawk  * Use multi-line #defines to avoid screwing up the kernel tags file;
72a403c3e1Sjhawk  * without this, ctags produces a tags file where panic() shows up
73a403c3e1Sjhawk  * in subr_extent.c rather than subr_prf.c.
74a403c3e1Sjhawk  */
75a403c3e1Sjhawk #define	\
7689c9828dSpara kmem_alloc(s, flags)		malloc(s)
77a403c3e1Sjhawk #define	\
7889c9828dSpara kmem_free(p, s)			free(p)
79a403c3e1Sjhawk #define	\
8088ab7da9Sad cv_wait_sig(cv, lock)		(EWOULDBLOCK)
81a403c3e1Sjhawk #define	\
8289c9828dSpara pool_get(pool, flags)		kmem_alloc((pool)->pr_size,0)
83a403c3e1Sjhawk #define	\
8489c9828dSpara pool_put(pool, rp)		kmem_free(rp,0)
85a403c3e1Sjhawk #define	\
8628fcb4a4Scherry panic(a ...)			printf(a)
87968e76ebSkre #define	mutex_init(a, b, c)	no_op()
88968e76ebSkre #define	mutex_destroy(a)	no_op()
89968e76ebSkre #define	mutex_enter(l)		no_op()
90968e76ebSkre #define	mutex_exit(l)		no_op()
91968e76ebSkre #define	cv_wait(cv, lock)	no_op()
92968e76ebSkre #define	cv_broadcast(cv)	no_op()
93968e76ebSkre #define	cv_init(a, b)		no_op()
94968e76ebSkre #define	cv_destroy(a)		no_op()
95e3203cc4Sthorpej #define	KMEM_IS_RUNNING			(1)
96010df7a9Sad #define	IPL_VM				(0)
97598ab03aSad #define	MUTEX_DEFAULT			(0)
98968e76ebSkre #define	KASSERT(exp)
99e77b6879Scgd #endif
1006041e29fSthorpej 
101626cb1c3Sthorpej static struct pool expool;
1024e465abfSpk 
1036041e29fSthorpej /*
10452c0d38aSthorpej  * Macro to align to an arbitrary power-of-two boundary.
10552c0d38aSthorpej  */
1063718fc4aSpk #define EXTENT_ALIGN(_start, _align, _skew)		\
1073718fc4aSpk 	(((((_start) - (_skew)) + ((_align) - 1)) & (-(_align))) + (_skew))
10852c0d38aSthorpej 
10952c0d38aSthorpej /*
1101c2f0a15Ssommerfe  * Create the extent_region pool.
1111c2f0a15Ssommerfe  */
11288ab7da9Sad void
extent_init(void)11388ab7da9Sad extent_init(void)
1141c2f0a15Ssommerfe {
115626cb1c3Sthorpej 
1166f107350Spk #if defined(_KERNEL)
117626cb1c3Sthorpej 	pool_init(&expool, sizeof(struct extent_region), 0, 0, 0,
11859d979c5Sad 	    "extent", NULL, IPL_VM);
1196f107350Spk #else
120626cb1c3Sthorpej 	expool.pr_size = sizeof(struct extent_region);
1216f107350Spk #endif
1221c2f0a15Ssommerfe }
1231c2f0a15Ssommerfe 
1241c2f0a15Ssommerfe /*
12588ab7da9Sad  * Allocate an extent region descriptor.  EXTENT MUST NOT BE LOCKED.
12688ab7da9Sad  * We will handle any locking we may need.
1272f6855b6Sthorpej  */
1282f6855b6Sthorpej static struct extent_region *
extent_alloc_region_descriptor(struct extent * ex,int flags)1292f6855b6Sthorpej extent_alloc_region_descriptor(struct extent *ex, int flags)
1302f6855b6Sthorpej {
1312f6855b6Sthorpej 	struct extent_region *rp;
132111cbb59Sskrll 	int error;
1332f6855b6Sthorpej 
134111cbb59Sskrll 	if (ex->ex_flags & EXF_FIXED) {
1352f6855b6Sthorpej 		struct extent_fixed *fex = (struct extent_fixed *)ex;
1362f6855b6Sthorpej 
1377de85ed2Sjmcneill 		if (!(ex->ex_flags & EXF_EARLY))
13888ab7da9Sad 			mutex_enter(&ex->ex_lock);
1392f6855b6Sthorpej 		for (;;) {
1402f6855b6Sthorpej 			if ((rp = LIST_FIRST(&fex->fex_freelist)) != NULL) {
1412f6855b6Sthorpej 				/*
1422f6855b6Sthorpej 				 * Don't muck with flags after pulling it off
1432f6855b6Sthorpej 				 * the freelist; it may have been dynamically
1442f6855b6Sthorpej 				 * allocated, and kindly given to us.  We
1452f6855b6Sthorpej 				 * need to remember that information.
1462f6855b6Sthorpej 				 */
1472f6855b6Sthorpej 				LIST_REMOVE(rp, er_link);
1487de85ed2Sjmcneill 				if (!(ex->ex_flags & EXF_EARLY))
14988ab7da9Sad 					mutex_exit(&ex->ex_lock);
1502f6855b6Sthorpej 				return (rp);
1512f6855b6Sthorpej 			}
1522f6855b6Sthorpej 			if (flags & EX_MALLOCOK) {
1537de85ed2Sjmcneill 				if (!(ex->ex_flags & EXF_EARLY))
15488ab7da9Sad 					mutex_exit(&ex->ex_lock);
1552f6855b6Sthorpej 				goto alloc;
1562f6855b6Sthorpej 			}
1572f6855b6Sthorpej 			if ((flags & EX_WAITOK) == 0) {
1587de85ed2Sjmcneill 				if (!(ex->ex_flags & EXF_EARLY))
15988ab7da9Sad 					mutex_exit(&ex->ex_lock);
1602f6855b6Sthorpej 				return (NULL);
1612f6855b6Sthorpej 			}
1627de85ed2Sjmcneill 			KASSERT(mutex_owned(&ex->ex_lock));
163111cbb59Sskrll 			ex->ex_flwanted = true;
16488ab7da9Sad 			if ((flags & EX_CATCH) != 0)
16588ab7da9Sad 				error = cv_wait_sig(&ex->ex_cv, &ex->ex_lock);
16688ab7da9Sad 			else {
16788ab7da9Sad 				cv_wait(&ex->ex_cv, &ex->ex_lock);
16888ab7da9Sad 				error = 0;
16988ab7da9Sad 			}
17088ab7da9Sad 			if (error != 0) {
17188ab7da9Sad 				mutex_exit(&ex->ex_lock);
1722f6855b6Sthorpej 				return (NULL);
1732f6855b6Sthorpej 			}
1742f6855b6Sthorpej 		}
17588ab7da9Sad 	}
1762f6855b6Sthorpej 
1772f6855b6Sthorpej  alloc:
178b368d720Schristos 	rp = pool_get(&expool, (flags & EX_WAITOK) ? PR_WAITOK : PR_NOWAIT);
1792f6855b6Sthorpej 
1802f6855b6Sthorpej 	if (rp != NULL)
1812f6855b6Sthorpej 		rp->er_flags = ER_ALLOC;
1822f6855b6Sthorpej 
1832f6855b6Sthorpej 	return (rp);
1842f6855b6Sthorpej }
1852f6855b6Sthorpej 
1862f6855b6Sthorpej /*
18788ab7da9Sad  * Free an extent region descriptor.  EXTENT _MUST_ BE LOCKED!
1882f6855b6Sthorpej  */
1892f6855b6Sthorpej static void
extent_free_region_descriptor(struct extent * ex,struct extent_region * rp)1902f6855b6Sthorpej extent_free_region_descriptor(struct extent *ex, struct extent_region *rp)
1912f6855b6Sthorpej {
1922f6855b6Sthorpej 
1932f6855b6Sthorpej 	if (ex->ex_flags & EXF_FIXED) {
1942f6855b6Sthorpej 		struct extent_fixed *fex = (struct extent_fixed *)ex;
1952f6855b6Sthorpej 
1962f6855b6Sthorpej 		/*
1972f6855b6Sthorpej 		 * If someone's waiting for a region descriptor,
1982f6855b6Sthorpej 		 * be nice and give them this one, rather than
1992f6855b6Sthorpej 		 * just free'ing it back to the system.
2002f6855b6Sthorpej 		 */
2012f6855b6Sthorpej 		if (rp->er_flags & ER_ALLOC) {
202111cbb59Sskrll 			if (ex->ex_flwanted) {
2032f6855b6Sthorpej 				/* Clear all but ER_ALLOC flag. */
2042f6855b6Sthorpej 				rp->er_flags = ER_ALLOC;
2052f6855b6Sthorpej 				LIST_INSERT_HEAD(&fex->fex_freelist, rp,
2062f6855b6Sthorpej 				    er_link);
2072f6855b6Sthorpej 				goto wake_em_up;
20888ab7da9Sad 			} else
2092f6855b6Sthorpej 				pool_put(&expool, rp);
2102f6855b6Sthorpej 		} else {
2112f6855b6Sthorpej 			/* Clear all flags. */
2122f6855b6Sthorpej 			rp->er_flags = 0;
2132f6855b6Sthorpej 			LIST_INSERT_HEAD(&fex->fex_freelist, rp, er_link);
2142f6855b6Sthorpej 		}
2152f6855b6Sthorpej 
2162f6855b6Sthorpej  wake_em_up:
2177de85ed2Sjmcneill 		if (!(ex->ex_flags & EXF_EARLY)) {
218111cbb59Sskrll 			ex->ex_flwanted = false;
21988ab7da9Sad 			cv_broadcast(&ex->ex_cv);
2207de85ed2Sjmcneill 		}
2212f6855b6Sthorpej 		return;
2222f6855b6Sthorpej 	}
2232f6855b6Sthorpej 
2242f6855b6Sthorpej 	/*
2252f6855b6Sthorpej 	 * We know it's dynamically allocated if we get here.
2262f6855b6Sthorpej 	 */
2272f6855b6Sthorpej 	pool_put(&expool, rp);
2282f6855b6Sthorpej }
2292f6855b6Sthorpej 
2302f6855b6Sthorpej /*
2316041e29fSthorpej  * Allocate and initialize an extent map.
2326041e29fSthorpej  */
2336041e29fSthorpej struct extent *
extent_create(const char * name,u_long start,u_long end,void * storage,size_t storagesize,int flags)2342f6855b6Sthorpej extent_create(const char *name, u_long start, u_long end,
23589c9828dSpara     void *storage, size_t storagesize, int flags)
2366041e29fSthorpej {
2376041e29fSthorpej 	struct extent *ex;
23853524e44Schristos 	char *cp = storage;
2396041e29fSthorpej 	size_t sz = storagesize;
2406041e29fSthorpej 	struct extent_region *rp;
2416041e29fSthorpej 	int fixed_extent = (storage != NULL);
24288ab7da9Sad 
24388ab7da9Sad #ifndef _KERNEL
24488ab7da9Sad 	extent_init();
24588ab7da9Sad #endif
2466041e29fSthorpej 
24752c0d38aSthorpej #ifdef DIAGNOSTIC
2486041e29fSthorpej 	/* Check arguments. */
2496041e29fSthorpej 	if (name == NULL)
2506041e29fSthorpej 		panic("extent_create: name == NULL");
2516041e29fSthorpej 	if (end < start) {
252f443b89cSchristos 		printf("extent_create: extent `%s', start 0x%lx, end 0x%lx\n",
2536041e29fSthorpej 		    name, start, end);
2546041e29fSthorpej 		panic("extent_create: end < start");
2556041e29fSthorpej 	}
2566041e29fSthorpej 	if (fixed_extent && (storagesize < sizeof(struct extent_fixed)))
25794962b67Sthorpej 		panic("extent_create: fixed extent, bad storagesize 0x%lx",
25894962b67Sthorpej 		    (u_long)storagesize);
25952c0d38aSthorpej 	if (fixed_extent == 0 && (storagesize != 0 || storage != NULL))
26052c0d38aSthorpej 		panic("extent_create: storage provided for non-fixed");
26152c0d38aSthorpej #endif
2626041e29fSthorpej 
2636041e29fSthorpej 	/* Allocate extent descriptor. */
2646041e29fSthorpej 	if (fixed_extent) {
2656041e29fSthorpej 		struct extent_fixed *fex;
2666041e29fSthorpej 
267275d1554Sperry 		memset(storage, 0, storagesize);
2686041e29fSthorpej 
2696041e29fSthorpej 		/*
2706041e29fSthorpej 		 * Align all descriptors on "long" boundaries.
2716041e29fSthorpej 		 */
2726041e29fSthorpej 		fex = (struct extent_fixed *)cp;
2736041e29fSthorpej 		ex = (struct extent *)fex;
27452c0d38aSthorpej 		cp += ALIGN(sizeof(struct extent_fixed));
27552c0d38aSthorpej 		sz -= ALIGN(sizeof(struct extent_fixed));
2766041e29fSthorpej 		fex->fex_storage = storage;
2776041e29fSthorpej 		fex->fex_storagesize = storagesize;
2786041e29fSthorpej 
2796041e29fSthorpej 		/*
2806041e29fSthorpej 		 * In a fixed extent, we have to pre-allocate region
2816041e29fSthorpej 		 * descriptors and place them in the extent's freelist.
2826041e29fSthorpej 		 */
2836041e29fSthorpej 		LIST_INIT(&fex->fex_freelist);
28452c0d38aSthorpej 		while (sz >= ALIGN(sizeof(struct extent_region))) {
2856041e29fSthorpej 			rp = (struct extent_region *)cp;
28652c0d38aSthorpej 			cp += ALIGN(sizeof(struct extent_region));
28752c0d38aSthorpej 			sz -= ALIGN(sizeof(struct extent_region));
2886041e29fSthorpej 			LIST_INSERT_HEAD(&fex->fex_freelist, rp, er_link);
2896041e29fSthorpej 		}
2906041e29fSthorpej 	} else {
291e65c85beSchristos 		ex = kmem_alloc(sizeof(*ex),
29289c9828dSpara 		    (flags & EX_WAITOK) ? KM_SLEEP : KM_NOSLEEP);
2936041e29fSthorpej 		if (ex == NULL)
2946041e29fSthorpej 			return (NULL);
2956041e29fSthorpej 	}
2966041e29fSthorpej 
2976041e29fSthorpej 	/* Fill in the extent descriptor and return it to the caller. */
2986b34528aSjakllsch 	if ((flags & EX_EARLY) == 0) {
299598ab03aSad 		mutex_init(&ex->ex_lock, MUTEX_DEFAULT, IPL_VM);
30088ab7da9Sad 		cv_init(&ex->ex_cv, "extent");
3016b34528aSjakllsch 	}
3026041e29fSthorpej 	LIST_INIT(&ex->ex_regions);
3036041e29fSthorpej 	ex->ex_name = name;
3046041e29fSthorpej 	ex->ex_start = start;
3056041e29fSthorpej 	ex->ex_end = end;
3066041e29fSthorpej 	ex->ex_flags = 0;
307111cbb59Sskrll 	ex->ex_flwanted = false;
3086041e29fSthorpej 	if (fixed_extent)
3096041e29fSthorpej 		ex->ex_flags |= EXF_FIXED;
31052c0d38aSthorpej 	if (flags & EX_NOCOALESCE)
31152c0d38aSthorpej 		ex->ex_flags |= EXF_NOCOALESCE;
3127de85ed2Sjmcneill 	if (flags & EX_EARLY)
3137de85ed2Sjmcneill 		ex->ex_flags |= EXF_EARLY;
3146041e29fSthorpej 	return (ex);
3156041e29fSthorpej }
3166041e29fSthorpej 
3176041e29fSthorpej /*
3186041e29fSthorpej  * Destroy an extent map.
3193bd133daSchs  * Since we're freeing the data, there can't be any references
3203bd133daSchs  * so we don't need any locking.
3216041e29fSthorpej  */
3226041e29fSthorpej void
extent_destroy(struct extent * ex)3232f6855b6Sthorpej extent_destroy(struct extent *ex)
3246041e29fSthorpej {
3256041e29fSthorpej 	struct extent_region *rp, *orp;
3266041e29fSthorpej 
32752c0d38aSthorpej #ifdef DIAGNOSTIC
3286041e29fSthorpej 	/* Check arguments. */
3296041e29fSthorpej 	if (ex == NULL)
3306041e29fSthorpej 		panic("extent_destroy: NULL extent");
33152c0d38aSthorpej #endif
3326041e29fSthorpej 
3336041e29fSthorpej 	/* Free all region descriptors in extent. */
33448bbf5f2Smatt 	for (rp = LIST_FIRST(&ex->ex_regions); rp != NULL; ) {
3356041e29fSthorpej 		orp = rp;
33648bbf5f2Smatt 		rp = LIST_NEXT(rp, er_link);
3376041e29fSthorpej 		LIST_REMOVE(orp, er_link);
3386041e29fSthorpej 		extent_free_region_descriptor(ex, orp);
3396041e29fSthorpej 	}
3406041e29fSthorpej 
34188ab7da9Sad 	cv_destroy(&ex->ex_cv);
34288ab7da9Sad 	mutex_destroy(&ex->ex_lock);
34388ab7da9Sad 
3446041e29fSthorpej 	/* If we're not a fixed extent, free the extent descriptor itself. */
3456041e29fSthorpej 	if ((ex->ex_flags & EXF_FIXED) == 0)
34689c9828dSpara 		kmem_free(ex, sizeof(*ex));
3476041e29fSthorpej }
3486041e29fSthorpej 
3496041e29fSthorpej /*
3506041e29fSthorpej  * Insert a region descriptor into the sorted region list after the
3516041e29fSthorpej  * entry "after" or at the head of the list (if "after" is NULL).
35252c0d38aSthorpej  * The region descriptor we insert is passed in "rp".  We must
35352c0d38aSthorpej  * allocate the region descriptor before calling this function!
35452c0d38aSthorpej  * If we don't need the region descriptor, it will be freed here.
3556041e29fSthorpej  */
35652c0d38aSthorpej static void
extent_insert_and_optimize(struct extent * ex,u_long start,u_long size,int flags,struct extent_region * after,struct extent_region * rp)3572f6855b6Sthorpej extent_insert_and_optimize(struct extent *ex, u_long start, u_long size,
3581a7bc55dSyamt     int flags, struct extent_region *after, struct extent_region *rp)
3596041e29fSthorpej {
360e77b6879Scgd 	struct extent_region *nextr;
3616041e29fSthorpej 	int appended = 0;
3626041e29fSthorpej 
3636041e29fSthorpej 	if (after == NULL) {
3646041e29fSthorpej 		/*
3656041e29fSthorpej 		 * We're the first in the region list.  If there's
3666041e29fSthorpej 		 * a region after us, attempt to coalesce to save
3676041e29fSthorpej 		 * descriptor overhead.
3686041e29fSthorpej 		 */
36952c0d38aSthorpej 		if (((ex->ex_flags & EXF_NOCOALESCE) == 0) &&
37048bbf5f2Smatt 		    (LIST_FIRST(&ex->ex_regions) != NULL) &&
37148bbf5f2Smatt 		    ((start + size) == LIST_FIRST(&ex->ex_regions)->er_start)) {
3726041e29fSthorpej 			/*
3736041e29fSthorpej 			 * We can coalesce.  Prepend us to the first region.
3746041e29fSthorpej 			 */
37548bbf5f2Smatt 			LIST_FIRST(&ex->ex_regions)->er_start = start;
37652c0d38aSthorpej 			extent_free_region_descriptor(ex, rp);
37752c0d38aSthorpej 			return;
3786041e29fSthorpej 		}
3796041e29fSthorpej 
3806041e29fSthorpej 		/*
38152c0d38aSthorpej 		 * Can't coalesce.  Fill in the region descriptor
3826041e29fSthorpej 		 * in, and insert us at the head of the region list.
3836041e29fSthorpej 		 */
3846041e29fSthorpej 		rp->er_start = start;
3856041e29fSthorpej 		rp->er_end = start + (size - 1);
3866041e29fSthorpej 		LIST_INSERT_HEAD(&ex->ex_regions, rp, er_link);
38752c0d38aSthorpej 		return;
3886041e29fSthorpej 	}
3896041e29fSthorpej 
3906041e29fSthorpej 	/*
39152c0d38aSthorpej 	 * If EXF_NOCOALESCE is set, coalescing is disallowed.
3926041e29fSthorpej 	 */
39352c0d38aSthorpej 	if (ex->ex_flags & EXF_NOCOALESCE)
39452c0d38aSthorpej 		goto cant_coalesce;
3956041e29fSthorpej 
3966041e29fSthorpej 	/*
3976041e29fSthorpej 	 * Attempt to coalesce with the region before us.
3986041e29fSthorpej 	 */
3996041e29fSthorpej 	if ((after->er_end + 1) == start) {
4006041e29fSthorpej 		/*
4016041e29fSthorpej 		 * We can coalesce.  Append ourselves and make
4026041e29fSthorpej 		 * note of it.
4036041e29fSthorpej 		 */
4046041e29fSthorpej 		after->er_end = start + (size - 1);
4056041e29fSthorpej 		appended = 1;
4066041e29fSthorpej 	}
4076041e29fSthorpej 
4086041e29fSthorpej 	/*
4096041e29fSthorpej 	 * Attempt to coalesce with the region after us.
4106041e29fSthorpej 	 */
41148bbf5f2Smatt 	if ((LIST_NEXT(after, er_link) != NULL) &&
41248bbf5f2Smatt 	    ((start + size) == LIST_NEXT(after, er_link)->er_start)) {
4136041e29fSthorpej 		/*
4146041e29fSthorpej 		 * We can coalesce.  Note that if we appended ourselves
4156041e29fSthorpej 		 * to the previous region, we exactly fit the gap, and
4166041e29fSthorpej 		 * can free the "next" region descriptor.
4176041e29fSthorpej 		 */
4186041e29fSthorpej 		if (appended) {
4196041e29fSthorpej 			/*
4206041e29fSthorpej 			 * Yup, we can free it up.
4216041e29fSthorpej 			 */
42248bbf5f2Smatt 			after->er_end = LIST_NEXT(after, er_link)->er_end;
42348bbf5f2Smatt 			nextr = LIST_NEXT(after, er_link);
424e77b6879Scgd 			LIST_REMOVE(nextr, er_link);
425e77b6879Scgd 			extent_free_region_descriptor(ex, nextr);
4266041e29fSthorpej 		} else {
4276041e29fSthorpej 			/*
4286041e29fSthorpej 			 * Nope, just prepend us to the next region.
4296041e29fSthorpej 			 */
43048bbf5f2Smatt 			LIST_NEXT(after, er_link)->er_start = start;
4316041e29fSthorpej 		}
43252c0d38aSthorpej 
43352c0d38aSthorpej 		extent_free_region_descriptor(ex, rp);
43452c0d38aSthorpej 		return;
4356041e29fSthorpej 	}
4366041e29fSthorpej 
4376041e29fSthorpej 	/*
4386041e29fSthorpej 	 * We weren't able to coalesce with the next region, but
4396041e29fSthorpej 	 * we don't need to allocate a region descriptor if we
4406041e29fSthorpej 	 * appended ourselves to the previous region.
4416041e29fSthorpej 	 */
44252c0d38aSthorpej 	if (appended) {
44352c0d38aSthorpej 		extent_free_region_descriptor(ex, rp);
44452c0d38aSthorpej 		return;
44552c0d38aSthorpej 	}
4466041e29fSthorpej 
44752c0d38aSthorpej  cant_coalesce:
4486041e29fSthorpej 
4496041e29fSthorpej 	/*
45052c0d38aSthorpej 	 * Fill in the region descriptor and insert ourselves
4516041e29fSthorpej 	 * into the region list.
4526041e29fSthorpej 	 */
4536041e29fSthorpej 	rp->er_start = start;
4546041e29fSthorpej 	rp->er_end = start + (size - 1);
4556041e29fSthorpej 	LIST_INSERT_AFTER(after, rp, er_link);
4566041e29fSthorpej }
4576041e29fSthorpej 
4586041e29fSthorpej /*
4596041e29fSthorpej  * Allocate a specific region in an extent map.
4606041e29fSthorpej  */
4616041e29fSthorpej int
extent_alloc_region(struct extent * ex,u_long start,u_long size,int flags)4622f6855b6Sthorpej extent_alloc_region(struct extent *ex, u_long start, u_long size, int flags)
4636041e29fSthorpej {
46452c0d38aSthorpej 	struct extent_region *rp, *last, *myrp;
4656041e29fSthorpej 	u_long end = start + (size - 1);
4666041e29fSthorpej 	int error;
4676041e29fSthorpej 
46852c0d38aSthorpej #ifdef DIAGNOSTIC
4696041e29fSthorpej 	/* Check arguments. */
4706041e29fSthorpej 	if (ex == NULL)
4716041e29fSthorpej 		panic("extent_alloc_region: NULL extent");
4726041e29fSthorpej 	if (size < 1) {
473f443b89cSchristos 		printf("extent_alloc_region: extent `%s', size 0x%lx\n",
4746041e29fSthorpej 		    ex->ex_name, size);
4756041e29fSthorpej 		panic("extent_alloc_region: bad size");
4766041e29fSthorpej 	}
4776041e29fSthorpej 	if (end < start) {
478f443b89cSchristos 		printf(
4796041e29fSthorpej 		 "extent_alloc_region: extent `%s', start 0x%lx, size 0x%lx\n",
4806041e29fSthorpej 		 ex->ex_name, start, size);
4816041e29fSthorpej 		panic("extent_alloc_region: overflow");
4826041e29fSthorpej 	}
48352c0d38aSthorpej #endif
4842fbef8e2Sthorpej #ifdef LOCKDEBUG
485ad4f28d1Sad 	if (flags & EX_WAITSPACE) {
486a67bae0bSyamt 		ASSERT_SLEEPABLE();
487ad4f28d1Sad 	}
4882fbef8e2Sthorpej #endif
48952c0d38aSthorpej 
4906041e29fSthorpej 	/*
4916041e29fSthorpej 	 * Make sure the requested region lies within the
4926041e29fSthorpej 	 * extent.
493618c279dSthorpej 	 *
494618c279dSthorpej 	 * We don't lock to check the range, because those values
495618c279dSthorpej 	 * are never modified, and if another thread deletes the
496618c279dSthorpej 	 * extent, we're screwed anyway.
4976041e29fSthorpej 	 */
4986041e29fSthorpej 	if ((start < ex->ex_start) || (end > ex->ex_end)) {
49952c0d38aSthorpej #ifdef DIAGNOSTIC
500f443b89cSchristos 		printf("extent_alloc_region: extent `%s' (0x%lx - 0x%lx)\n",
5016041e29fSthorpej 		    ex->ex_name, ex->ex_start, ex->ex_end);
502f443b89cSchristos 		printf("extent_alloc_region: start 0x%lx, end 0x%lx\n",
5036041e29fSthorpej 		    start, end);
5046041e29fSthorpej 		panic("extent_alloc_region: region lies outside extent");
50552c0d38aSthorpej #else
50652c0d38aSthorpej 		return (EINVAL);
50752c0d38aSthorpej #endif
50852c0d38aSthorpej 	}
50952c0d38aSthorpej 
51052c0d38aSthorpej 	/*
51152c0d38aSthorpej 	 * Allocate the region descriptor.  It will be freed later
512618c279dSthorpej 	 * if we can coalesce with another region.  Don't lock before
513618c279dSthorpej 	 * here!  This could block.
51452c0d38aSthorpej 	 */
51552c0d38aSthorpej 	myrp = extent_alloc_region_descriptor(ex, flags);
51652c0d38aSthorpej 	if (myrp == NULL) {
51752c0d38aSthorpej #ifdef DIAGNOSTIC
51852c0d38aSthorpej 		printf(
51952c0d38aSthorpej 		    "extent_alloc_region: can't allocate region descriptor\n");
52052c0d38aSthorpej #endif
52152c0d38aSthorpej 		return (ENOMEM);
5226041e29fSthorpej 	}
5236041e29fSthorpej 
5247de85ed2Sjmcneill 	if (!(ex->ex_flags & EXF_EARLY))
52588ab7da9Sad 		mutex_enter(&ex->ex_lock);
5266041e29fSthorpej  alloc_start:
527618c279dSthorpej 
5286041e29fSthorpej 	/*
5296041e29fSthorpej 	 * Attempt to place ourselves in the desired area of the
5306041e29fSthorpej 	 * extent.  We save ourselves some work by keeping the list sorted.
5316041e29fSthorpej 	 * In other words, if the start of the current region is greater
5326041e29fSthorpej 	 * than the end of our region, we don't have to search any further.
5336041e29fSthorpej 	 */
5346041e29fSthorpej 
5356041e29fSthorpej 	/*
5366041e29fSthorpej 	 * Keep a pointer to the last region we looked at so
5376041e29fSthorpej 	 * that we don't have to traverse the list again when
5386041e29fSthorpej 	 * we insert ourselves.  If "last" is NULL when we
5396041e29fSthorpej 	 * finally insert ourselves, we go at the head of the
5406041e29fSthorpej 	 * list.  See extent_insert_and_optimize() for details.
5416041e29fSthorpej 	 */
5426041e29fSthorpej 	last = NULL;
5436041e29fSthorpej 
54448bbf5f2Smatt 	LIST_FOREACH(rp, &ex->ex_regions, er_link) {
5456041e29fSthorpej 		if (rp->er_start > end) {
5466041e29fSthorpej 			/*
5476041e29fSthorpej 			 * We lie before this region and don't
5486041e29fSthorpej 			 * conflict.
5496041e29fSthorpej 			 */
5506041e29fSthorpej 			break;
5516041e29fSthorpej 		}
5526041e29fSthorpej 
5536041e29fSthorpej 		/*
5546041e29fSthorpej 		 * The current region begins before we end.
5556041e29fSthorpej 		 * Check for a conflict.
5566041e29fSthorpej 		 */
5576041e29fSthorpej 		if (rp->er_end >= start) {
5586041e29fSthorpej 			/*
55952c0d38aSthorpej 			 * We conflict.  If we can (and want to) wait,
56052c0d38aSthorpej 			 * do so.
5616041e29fSthorpej 			 */
56252c0d38aSthorpej 			if (flags & EX_WAITSPACE) {
5637de85ed2Sjmcneill 				KASSERT(!(ex->ex_flags & EXF_EARLY));
56488ab7da9Sad 				if ((flags & EX_CATCH) != 0)
56588ab7da9Sad 					error = cv_wait_sig(&ex->ex_cv,
56688ab7da9Sad 					    &ex->ex_lock);
56788ab7da9Sad 				else {
56888ab7da9Sad 					cv_wait(&ex->ex_cv, &ex->ex_lock);
56988ab7da9Sad 					error = 0;
57088ab7da9Sad 				}
571114adc27Sdsl 				if (error == 0)
5726041e29fSthorpej 					goto alloc_start;
57388ab7da9Sad 				mutex_exit(&ex->ex_lock);
574114adc27Sdsl 			} else {
5757de85ed2Sjmcneill 				if (!(ex->ex_flags & EXF_EARLY))
57688ab7da9Sad 					mutex_exit(&ex->ex_lock);
577114adc27Sdsl 				error = EAGAIN;
5786041e29fSthorpej 			}
57952c0d38aSthorpej 			extent_free_region_descriptor(ex, myrp);
580114adc27Sdsl 			return error;
5816041e29fSthorpej 		}
5826041e29fSthorpej 		/*
5836041e29fSthorpej 		 * We don't conflict, but this region lies before
5846041e29fSthorpej 		 * us.  Keep a pointer to this region, and keep
5856041e29fSthorpej 		 * trying.
5866041e29fSthorpej 		 */
5876041e29fSthorpej 		last = rp;
5886041e29fSthorpej 	}
5896041e29fSthorpej 
5906041e29fSthorpej 	/*
5916041e29fSthorpej 	 * We don't conflict with any regions.  "last" points
5926041e29fSthorpej 	 * to the region we fall after, or is NULL if we belong
5936041e29fSthorpej 	 * at the beginning of the region list.  Insert ourselves.
5946041e29fSthorpej 	 */
59552c0d38aSthorpej 	extent_insert_and_optimize(ex, start, size, flags, last, myrp);
5967de85ed2Sjmcneill 	if (!(ex->ex_flags & EXF_EARLY))
59788ab7da9Sad 		mutex_exit(&ex->ex_lock);
59852c0d38aSthorpej 	return (0);
5996041e29fSthorpej }
6006041e29fSthorpej 
6016041e29fSthorpej /*
6026041e29fSthorpej  * Macro to check (x + y) <= z.  This check is designed to fail
6036041e29fSthorpej  * if an overflow occurs.
6046041e29fSthorpej  */
6056041e29fSthorpej #define LE_OV(x, y, z)	((((x) + (y)) >= (x)) && (((x) + (y)) <= (z)))
6066041e29fSthorpej 
6076041e29fSthorpej /*
6086041e29fSthorpej  * Allocate a region in an extent map subregion.
6096041e29fSthorpej  *
6106041e29fSthorpej  * If EX_FAST is specified, we return the first fit in the map.
6116041e29fSthorpej  * Otherwise, we try to minimize fragmentation by finding the
6126041e29fSthorpej  * smallest gap that will hold the request.
6136041e29fSthorpej  *
6146041e29fSthorpej  * The allocated region is aligned to "alignment", which must be
6156041e29fSthorpej  * a power of 2.
6166041e29fSthorpej  */
6176041e29fSthorpej int
extent_alloc_subregion1(struct extent * ex,u_long substart,u_long subend,u_long size,u_long alignment,u_long skew,u_long boundary,int flags,u_long * result)6182f6855b6Sthorpej extent_alloc_subregion1(struct extent *ex, u_long substart, u_long subend,
6192f6855b6Sthorpej     u_long size, u_long alignment, u_long skew, u_long boundary,
6202f6855b6Sthorpej     int flags, u_long *result)
6216041e29fSthorpej {
62252c0d38aSthorpej 	struct extent_region *rp, *myrp, *last, *bestlast;
623405f8740Senami 	u_long newstart, newend, exend, beststart, bestovh, ovh;
624ca00239fSmycroft 	u_long dontcross;
6256041e29fSthorpej 	int error;
6266041e29fSthorpej 
62752c0d38aSthorpej #ifdef DIAGNOSTIC
628618c279dSthorpej 	/*
629618c279dSthorpej 	 * Check arguments.
630618c279dSthorpej 	 *
631618c279dSthorpej 	 * We don't lock to check these, because these values
632618c279dSthorpej 	 * are never modified, and if another thread deletes the
633618c279dSthorpej 	 * extent, we're screwed anyway.
634618c279dSthorpej 	 */
6356041e29fSthorpej 	if (ex == NULL)
6366041e29fSthorpej 		panic("extent_alloc_subregion: NULL extent");
6376041e29fSthorpej 	if (result == NULL)
6386041e29fSthorpej 		panic("extent_alloc_subregion: NULL result pointer");
639e98c5a9bSthorpej 	if ((substart < ex->ex_start) || (substart > ex->ex_end) ||
640e98c5a9bSthorpej 	    (subend > ex->ex_end) || (subend < ex->ex_start)) {
641d065a433Sskrll   		printf("extent_alloc_subregion: extent `%s', ex_start 0x%lx, "
642d065a433Sskrll 		    "ex_end 0x%lx\n", ex->ex_name, ex->ex_start, ex->ex_end);
643f443b89cSchristos 		printf("extent_alloc_subregion: substart 0x%lx, subend 0x%lx\n",
6446041e29fSthorpej 		    substart, subend);
6456041e29fSthorpej 		panic("extent_alloc_subregion: bad subregion");
6466041e29fSthorpej 	}
647e98c5a9bSthorpej 	if ((size < 1) || ((size - 1) > (subend - substart))) {
648f443b89cSchristos 		printf("extent_alloc_subregion: extent `%s', size 0x%lx\n",
6496041e29fSthorpej 		    ex->ex_name, size);
650*0b1a41caSskrll 		printf("extent_alloc_subregion: substart 0x%lx, subend 0x%lx\n",
651*0b1a41caSskrll 		    substart, subend);
6526041e29fSthorpej 		panic("extent_alloc_subregion: bad size");
6536041e29fSthorpej 	}
6546041e29fSthorpej 	if (alignment == 0)
6556041e29fSthorpej 		panic("extent_alloc_subregion: bad alignment");
6566041e29fSthorpej 	if (boundary && (boundary < size)) {
657f443b89cSchristos 		printf(
65811ac2c31Smarcus 		    "extent_alloc_subregion: extent `%s', size 0x%lx, "
65911ac2c31Smarcus 		    "boundary 0x%lx\n", ex->ex_name, size, boundary);
6606041e29fSthorpej 		panic("extent_alloc_subregion: bad boundary");
6616041e29fSthorpej 	}
66252c0d38aSthorpej #endif
6632fbef8e2Sthorpej #ifdef LOCKDEBUG
664ad4f28d1Sad 	if (flags & EX_WAITSPACE) {
665a67bae0bSyamt 		ASSERT_SLEEPABLE();
666ad4f28d1Sad 	}
6672fbef8e2Sthorpej #endif
66852c0d38aSthorpej 
66952c0d38aSthorpej 	/*
67052c0d38aSthorpej 	 * Allocate the region descriptor.  It will be freed later
671618c279dSthorpej 	 * if we can coalesce with another region.  Don't lock before
672618c279dSthorpej 	 * here!  This could block.
67352c0d38aSthorpej 	 */
67452c0d38aSthorpej 	myrp = extent_alloc_region_descriptor(ex, flags);
67552c0d38aSthorpej 	if (myrp == NULL) {
67652c0d38aSthorpej #ifdef DIAGNOSTIC
67752c0d38aSthorpej 		printf(
67852c0d38aSthorpej 		 "extent_alloc_subregion: can't allocate region descriptor\n");
67952c0d38aSthorpej #endif
68052c0d38aSthorpej 		return (ENOMEM);
68152c0d38aSthorpej 	}
6826041e29fSthorpej 
6836041e29fSthorpej  alloc_start:
68488ab7da9Sad 	mutex_enter(&ex->ex_lock);
685618c279dSthorpej 
6866041e29fSthorpej 	/*
6876041e29fSthorpej 	 * Keep a pointer to the last region we looked at so
6886041e29fSthorpej 	 * that we don't have to traverse the list again when
6896041e29fSthorpej 	 * we insert ourselves.  If "last" is NULL when we
6906041e29fSthorpej 	 * finally insert ourselves, we go at the head of the
6916041e29fSthorpej 	 * list.  See extent_insert_and_optimize() for deatails.
6926041e29fSthorpej 	 */
6936041e29fSthorpej 	last = NULL;
6946041e29fSthorpej 
6956041e29fSthorpej 	/*
6966041e29fSthorpej 	 * Keep track of size and location of the smallest
6976041e29fSthorpej 	 * chunk we fit in.
6986041e29fSthorpej 	 *
6996041e29fSthorpej 	 * Since the extent can be as large as the numeric range
7006041e29fSthorpej 	 * of the CPU (0 - 0xffffffff for 32-bit systems), the
7016041e29fSthorpej 	 * best overhead value can be the maximum unsigned integer.
7026041e29fSthorpej 	 * Thus, we initialize "bestovh" to 0, since we insert ourselves
7036041e29fSthorpej 	 * into the region list immediately on an exact match (which
7046041e29fSthorpej 	 * is the only case where "bestovh" would be set to 0).
7056041e29fSthorpej 	 */
7066041e29fSthorpej 	bestovh = 0;
7076041e29fSthorpej 	beststart = 0;
7086041e29fSthorpej 	bestlast = NULL;
7096041e29fSthorpej 
7106041e29fSthorpej 	/*
711405f8740Senami 	 * Keep track of end of free region.  This is either the end of extent
712405f8740Senami 	 * or the start of a region past the subend.
713405f8740Senami 	 */
714405f8740Senami 	exend = ex->ex_end;
715405f8740Senami 
716405f8740Senami 	/*
7176041e29fSthorpej 	 * For N allocated regions, we must make (N + 1)
7186041e29fSthorpej 	 * checks for unallocated space.  The first chunk we
7196041e29fSthorpej 	 * check is the area from the beginning of the subregion
720fc8c3e00Sthorpej 	 * to the first allocated region after that point.
7216041e29fSthorpej 	 */
7223718fc4aSpk 	newstart = EXTENT_ALIGN(substart, alignment, skew);
7236041e29fSthorpej 	if (newstart < ex->ex_start) {
72452c0d38aSthorpej #ifdef DIAGNOSTIC
725f443b89cSchristos 		printf(
7266041e29fSthorpej       "extent_alloc_subregion: extent `%s' (0x%lx - 0x%lx), alignment 0x%lx\n",
7276041e29fSthorpej 		 ex->ex_name, ex->ex_start, ex->ex_end, alignment);
72888ab7da9Sad 		mutex_exit(&ex->ex_lock);
7296041e29fSthorpej 		panic("extent_alloc_subregion: overflow after alignment");
73052c0d38aSthorpej #else
73152c0d38aSthorpej 		extent_free_region_descriptor(ex, myrp);
73288ab7da9Sad 		mutex_exit(&ex->ex_lock);
73352c0d38aSthorpej 		return (EINVAL);
73452c0d38aSthorpej #endif
7356041e29fSthorpej 	}
7366041e29fSthorpej 
737fc8c3e00Sthorpej 	/*
738fc8c3e00Sthorpej 	 * Find the first allocated region that begins on or after
739fc8c3e00Sthorpej 	 * the subregion start, advancing the "last" pointer along
740fc8c3e00Sthorpej 	 * the way.
741fc8c3e00Sthorpej 	 */
74248bbf5f2Smatt 	LIST_FOREACH(rp, &ex->ex_regions, er_link) {
743fc8c3e00Sthorpej 		if (rp->er_start >= newstart)
744fc8c3e00Sthorpej 			break;
745fc8c3e00Sthorpej 		last = rp;
746fc8c3e00Sthorpej 	}
747fc8c3e00Sthorpej 
7485e3bbeb1Spk 	/*
7494a60ccd1Sdrochner 	 * Relocate the start of our candidate region to the end of
7504a60ccd1Sdrochner 	 * the last allocated region (if there was one overlapping
7514a60ccd1Sdrochner 	 * our subrange).
7525e3bbeb1Spk 	 */
7534a60ccd1Sdrochner 	if (last != NULL && last->er_end >= newstart)
7543718fc4aSpk 		newstart = EXTENT_ALIGN((last->er_end + 1), alignment, skew);
7555e3bbeb1Spk 
75648bbf5f2Smatt 	for (; rp != NULL; rp = LIST_NEXT(rp, er_link)) {
7576041e29fSthorpej 		/*
758405f8740Senami 		 * If the region pasts the subend, bail out and see
759405f8740Senami 		 * if we fit against the subend.
760405f8740Senami 		 */
761e97ceab9Sbouyer 		if (rp->er_start > subend) {
762405f8740Senami 			exend = rp->er_start;
763405f8740Senami 			break;
764405f8740Senami 		}
765405f8740Senami 
766405f8740Senami 		/*
7676041e29fSthorpej 		 * Check the chunk before "rp".  Note that our
7686041e29fSthorpej 		 * comparison is safe from overflow conditions.
7696041e29fSthorpej 		 */
7706041e29fSthorpej 		if (LE_OV(newstart, size, rp->er_start)) {
7716041e29fSthorpej 			/*
7726041e29fSthorpej 			 * Do a boundary check, if necessary.  Note
7736041e29fSthorpej 			 * that a region may *begin* on the boundary,
7746041e29fSthorpej 			 * but it must end before the boundary.
7756041e29fSthorpej 			 */
7766041e29fSthorpej 			if (boundary) {
7776041e29fSthorpej 				newend = newstart + (size - 1);
7786041e29fSthorpej 
7796041e29fSthorpej 				/*
780ca00239fSmycroft 				 * Calculate the next boundary after the start
781ca00239fSmycroft 				 * of this region.
7826041e29fSthorpej 				 */
783bb815bdaSmycroft 				dontcross = EXTENT_ALIGN(newstart+1, boundary,
784ca00239fSmycroft 				    (flags & EX_BOUNDZERO) ? 0 : ex->ex_start)
785ca00239fSmycroft 				    - 1;
7866041e29fSthorpej 
787bb815bdaSmycroft #if 0
788ac2055bcSpk 				printf("newstart=%lx newend=%lx ex_start=%lx ex_end=%lx boundary=%lx dontcross=%lx\n",
789bb815bdaSmycroft 				    newstart, newend, ex->ex_start, ex->ex_end,
790bb815bdaSmycroft 				    boundary, dontcross);
791bb815bdaSmycroft #endif
792bb815bdaSmycroft 
7938c758332Smrg 				/* Check for overflow */
7948c758332Smrg 				if (dontcross < ex->ex_start)
7958c758332Smrg 					dontcross = ex->ex_end;
7968c758332Smrg 				else if (newend > dontcross) {
797bb815bdaSmycroft 					/*
798bb815bdaSmycroft 					 * Candidate region crosses boundary.
799bb815bdaSmycroft 					 * Throw away the leading part and see
800bb815bdaSmycroft 					 * if we still fit.
801bb815bdaSmycroft 					 */
802bb815bdaSmycroft 					newstart = dontcross + 1;
803bb815bdaSmycroft 					newend = newstart + (size - 1);
804bb815bdaSmycroft 					dontcross += boundary;
805bb815bdaSmycroft 					if (!LE_OV(newstart, size, rp->er_start))
806149c671bSbouyer 						goto skip;
807bb815bdaSmycroft 				}
808bb815bdaSmycroft 
8096041e29fSthorpej 				/*
8106041e29fSthorpej 				 * If we run past the end of
8116041e29fSthorpej 				 * the extent or the boundary
8126041e29fSthorpej 				 * overflows, then the request
8136041e29fSthorpej 				 * can't fit.
8146041e29fSthorpej 				 */
81543840103Smrg 				if (newstart + size - 1 > ex->ex_end ||
816ca00239fSmycroft 				    dontcross < newstart)
8176041e29fSthorpej 					goto fail;
8186041e29fSthorpej 			}
8196041e29fSthorpej 
8206041e29fSthorpej 			/*
8216041e29fSthorpej 			 * We would fit into this space.  Calculate
8226041e29fSthorpej 			 * the overhead (wasted space).  If we exactly
8236041e29fSthorpej 			 * fit, or we're taking the first fit, insert
8246041e29fSthorpej 			 * ourselves into the region list.
8256041e29fSthorpej 			 */
8266041e29fSthorpej 			ovh = rp->er_start - newstart - size;
8276041e29fSthorpej 			if ((flags & EX_FAST) || (ovh == 0))
8286041e29fSthorpej 				goto found;
8296041e29fSthorpej 
8306041e29fSthorpej 			/*
8316041e29fSthorpej 			 * Don't exactly fit, but check to see
8326041e29fSthorpej 			 * if we're better than any current choice.
8336041e29fSthorpej 			 */
8346041e29fSthorpej 			if ((bestovh == 0) || (ovh < bestovh)) {
8356041e29fSthorpej 				bestovh = ovh;
8366041e29fSthorpej 				beststart = newstart;
8376041e29fSthorpej 				bestlast = last;
8386041e29fSthorpej 			}
8396041e29fSthorpej 		}
8406041e29fSthorpej 
841149c671bSbouyer skip:
8426041e29fSthorpej 		/*
8436041e29fSthorpej 		 * Skip past the current region and check again.
8446041e29fSthorpej 		 */
8453718fc4aSpk 		newstart = EXTENT_ALIGN((rp->er_end + 1), alignment, skew);
8466041e29fSthorpej 		if (newstart < rp->er_end) {
8476041e29fSthorpej 			/*
8486041e29fSthorpej 			 * Overflow condition.  Don't error out, since
8496041e29fSthorpej 			 * we might have a chunk of space that we can
8506041e29fSthorpej 			 * use.
8516041e29fSthorpej 			 */
8526041e29fSthorpej 			goto fail;
8536041e29fSthorpej 		}
8546041e29fSthorpej 
8556041e29fSthorpej 		last = rp;
8566041e29fSthorpej 	}
8576041e29fSthorpej 
8586041e29fSthorpej 	/*
8596041e29fSthorpej 	 * The final check is from the current starting point to the
8606041e29fSthorpej 	 * end of the subregion.  If there were no allocated regions,
8616041e29fSthorpej 	 * "newstart" is set to the beginning of the subregion, or
8626041e29fSthorpej 	 * just past the end of the last allocated region, adjusted
8636041e29fSthorpej 	 * for alignment in either case.
8646041e29fSthorpej 	 */
8656041e29fSthorpej 	if (LE_OV(newstart, (size - 1), subend)) {
8666041e29fSthorpej 		/*
867bb815bdaSmycroft 		 * Do a boundary check, if necessary.  Note
868bb815bdaSmycroft 		 * that a region may *begin* on the boundary,
869bb815bdaSmycroft 		 * but it must end before the boundary.
870bb815bdaSmycroft 		 */
871bb815bdaSmycroft 		if (boundary) {
872bb815bdaSmycroft 			newend = newstart + (size - 1);
873bb815bdaSmycroft 
874bb815bdaSmycroft 			/*
875bb815bdaSmycroft 			 * Calculate the next boundary after the start
876bb815bdaSmycroft 			 * of this region.
877bb815bdaSmycroft 			 */
878bb815bdaSmycroft 			dontcross = EXTENT_ALIGN(newstart+1, boundary,
879bb815bdaSmycroft 			    (flags & EX_BOUNDZERO) ? 0 : ex->ex_start)
880bb815bdaSmycroft 			    - 1;
881bb815bdaSmycroft 
882bb815bdaSmycroft #if 0
883ac2055bcSpk 			printf("newstart=%lx newend=%lx ex_start=%lx ex_end=%lx boundary=%lx dontcross=%lx\n",
884bb815bdaSmycroft 			    newstart, newend, ex->ex_start, ex->ex_end,
885bb815bdaSmycroft 			    boundary, dontcross);
886bb815bdaSmycroft #endif
887bb815bdaSmycroft 
8888c758332Smrg 			/* Check for overflow */
8898c758332Smrg 			if (dontcross < ex->ex_start)
8908c758332Smrg 				dontcross = ex->ex_end;
8918c758332Smrg 			else if (newend > dontcross) {
892bb815bdaSmycroft 				/*
893bb815bdaSmycroft 				 * Candidate region crosses boundary.
894bb815bdaSmycroft 				 * Throw away the leading part and see
895bb815bdaSmycroft 				 * if we still fit.
896bb815bdaSmycroft 				 */
897bb815bdaSmycroft 				newstart = dontcross + 1;
898bb815bdaSmycroft 				newend = newstart + (size - 1);
899bb815bdaSmycroft 				dontcross += boundary;
900bb815bdaSmycroft 				if (!LE_OV(newstart, (size - 1), subend))
901bb815bdaSmycroft 					goto fail;
902bb815bdaSmycroft 			}
903bb815bdaSmycroft 
904bb815bdaSmycroft 			/*
905bb815bdaSmycroft 			 * If we run past the end of
906bb815bdaSmycroft 			 * the extent or the boundary
907bb815bdaSmycroft 			 * overflows, then the request
908bb815bdaSmycroft 			 * can't fit.
909bb815bdaSmycroft 			 */
91043840103Smrg 			if (newstart + size - 1 > ex->ex_end ||
911bb815bdaSmycroft 			    dontcross < newstart)
912bb815bdaSmycroft 				goto fail;
913bb815bdaSmycroft 		}
914bb815bdaSmycroft 
915bb815bdaSmycroft 		/*
9166041e29fSthorpej 		 * We would fit into this space.  Calculate
9176041e29fSthorpej 		 * the overhead (wasted space).  If we exactly
9186041e29fSthorpej 		 * fit, or we're taking the first fit, insert
9196041e29fSthorpej 		 * ourselves into the region list.
9206041e29fSthorpej 		 */
921405f8740Senami 		ovh = exend - newstart - (size - 1);
9226041e29fSthorpej 		if ((flags & EX_FAST) || (ovh == 0))
9236041e29fSthorpej 			goto found;
9246041e29fSthorpej 
9256041e29fSthorpej 		/*
9266041e29fSthorpej 		 * Don't exactly fit, but check to see
9276041e29fSthorpej 		 * if we're better than any current choice.
9286041e29fSthorpej 		 */
9296041e29fSthorpej 		if ((bestovh == 0) || (ovh < bestovh)) {
9306041e29fSthorpej 			bestovh = ovh;
9316041e29fSthorpej 			beststart = newstart;
9326041e29fSthorpej 			bestlast = last;
9336041e29fSthorpej 		}
9346041e29fSthorpej 	}
9356041e29fSthorpej 
9366041e29fSthorpej  fail:
9376041e29fSthorpej 	/*
9386041e29fSthorpej 	 * One of the following two conditions have
9396041e29fSthorpej 	 * occurred:
9406041e29fSthorpej 	 *
9416041e29fSthorpej 	 *	There is no chunk large enough to hold the request.
9426041e29fSthorpej 	 *
9436041e29fSthorpej 	 *	If EX_FAST was not specified, there is not an
9446041e29fSthorpej 	 *	exact match for the request.
9456041e29fSthorpej 	 *
9466041e29fSthorpej 	 * Note that if we reach this point and EX_FAST is
9476041e29fSthorpej 	 * set, then we know there is no space in the extent for
9486041e29fSthorpej 	 * the request.
9496041e29fSthorpej 	 */
9506041e29fSthorpej 	if (((flags & EX_FAST) == 0) && (bestovh != 0)) {
9516041e29fSthorpej 		/*
9526041e29fSthorpej 		 * We have a match that's "good enough".
9536041e29fSthorpej 		 */
9546041e29fSthorpej 		newstart = beststart;
9556041e29fSthorpej 		last = bestlast;
9566041e29fSthorpej 		goto found;
9576041e29fSthorpej 	}
9586041e29fSthorpej 
9596041e29fSthorpej 	/*
9606041e29fSthorpej 	 * No space currently available.  Wait for it to free up,
9616041e29fSthorpej 	 * if possible.
9626041e29fSthorpej 	 */
96352c0d38aSthorpej 	if (flags & EX_WAITSPACE) {
96488ab7da9Sad 		if ((flags & EX_CATCH) != 0) {
96588ab7da9Sad 			error = cv_wait_sig(&ex->ex_cv, &ex->ex_lock);
96688ab7da9Sad 		} else {
96788ab7da9Sad 			cv_wait(&ex->ex_cv, &ex->ex_lock);
96888ab7da9Sad 			error = 0;
96988ab7da9Sad 		}
970114adc27Sdsl 		if (error == 0)
9716041e29fSthorpej 			goto alloc_start;
97288ab7da9Sad 		mutex_exit(&ex->ex_lock);
973114adc27Sdsl 	} else {
97488ab7da9Sad 		mutex_exit(&ex->ex_lock);
975114adc27Sdsl 		error = EAGAIN;
9766041e29fSthorpej 	}
9776041e29fSthorpej 
97852c0d38aSthorpej 	extent_free_region_descriptor(ex, myrp);
979114adc27Sdsl 	return error;
9806041e29fSthorpej 
9816041e29fSthorpej  found:
9826041e29fSthorpej 	/*
9836041e29fSthorpej 	 * Insert ourselves into the region list.
9846041e29fSthorpej 	 */
98552c0d38aSthorpej 	extent_insert_and_optimize(ex, newstart, size, flags, last, myrp);
98688ab7da9Sad 	mutex_exit(&ex->ex_lock);
9876041e29fSthorpej 	*result = newstart;
98852c0d38aSthorpej 	return (0);
9896041e29fSthorpej }
9906041e29fSthorpej 
9916041e29fSthorpej int
extent_alloc_subregion(struct extent * ex,u_long start,u_long end,u_long size,u_long alignment,u_long boundary,int flags,u_long * result)992b0275539Sthorpej extent_alloc_subregion(struct extent *ex, u_long start, u_long end, u_long size,
993b0275539Sthorpej     u_long alignment, u_long boundary, int flags, u_long *result)
994b0275539Sthorpej {
995b0275539Sthorpej 
996b0275539Sthorpej 	return (extent_alloc_subregion1(ex, start, end, size, alignment,
997b0275539Sthorpej 					0, boundary, flags, result));
998b0275539Sthorpej }
999b0275539Sthorpej 
1000b0275539Sthorpej int
extent_alloc(struct extent * ex,u_long size,u_long alignment,u_long boundary,int flags,u_long * result)1001b0275539Sthorpej extent_alloc(struct extent *ex, u_long size, u_long alignment, u_long boundary,
1002b0275539Sthorpej     int flags, u_long *result)
1003b0275539Sthorpej {
1004b0275539Sthorpej 
1005b0275539Sthorpej 	return (extent_alloc_subregion1(ex, ex->ex_start, ex->ex_end,
1006b0275539Sthorpej 					size, alignment, 0, boundary,
1007b0275539Sthorpej 					flags, result));
1008b0275539Sthorpej }
1009b0275539Sthorpej 
1010b0275539Sthorpej int
extent_alloc1(struct extent * ex,u_long size,u_long alignment,u_long skew,u_long boundary,int flags,u_long * result)1011b0275539Sthorpej extent_alloc1(struct extent *ex, u_long size, u_long alignment, u_long skew,
1012b0275539Sthorpej     u_long boundary, int flags, u_long *result)
1013b0275539Sthorpej {
1014b0275539Sthorpej 
1015b0275539Sthorpej 	return (extent_alloc_subregion1(ex, ex->ex_start, ex->ex_end,
1016b0275539Sthorpej 					size, alignment, skew, boundary,
1017b0275539Sthorpej 					flags, result));
1018b0275539Sthorpej }
1019b0275539Sthorpej 
1020b0275539Sthorpej int
extent_free(struct extent * ex,u_long start,u_long size,int flags)10212f6855b6Sthorpej extent_free(struct extent *ex, u_long start, u_long size, int flags)
10226041e29fSthorpej {
1023618c279dSthorpej 	struct extent_region *rp, *nrp = NULL;
10246041e29fSthorpej 	u_long end = start + (size - 1);
10256041e29fSthorpej 
102652c0d38aSthorpej #ifdef DIAGNOSTIC
1027618c279dSthorpej 	/*
1028618c279dSthorpej 	 * Check arguments.
1029618c279dSthorpej 	 *
1030618c279dSthorpej 	 * We don't lock to check these, because these values
1031618c279dSthorpej 	 * are never modified, and if another thread deletes the
1032618c279dSthorpej 	 * extent, we're screwed anyway.
1033618c279dSthorpej 	 */
10346041e29fSthorpej 	if (ex == NULL)
10356041e29fSthorpej 		panic("extent_free: NULL extent");
1036e97ceab9Sbouyer 	if ((start < ex->ex_start) || (end > ex->ex_end)) {
10376041e29fSthorpej 		extent_print(ex);
1038f443b89cSchristos 		printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
10396041e29fSthorpej 		    ex->ex_name, start, size);
10406041e29fSthorpej 		panic("extent_free: extent `%s', region not within extent",
10416041e29fSthorpej 		    ex->ex_name);
10426041e29fSthorpej 	}
10436041e29fSthorpej 	/* Check for an overflow. */
10446041e29fSthorpej 	if (end < start) {
10456041e29fSthorpej 		extent_print(ex);
1046f443b89cSchristos 		printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
10476041e29fSthorpej 		    ex->ex_name, start, size);
10486041e29fSthorpej 		panic("extent_free: overflow");
10496041e29fSthorpej 	}
105052c0d38aSthorpej #endif
10516041e29fSthorpej 
10526041e29fSthorpej 	/*
1053618c279dSthorpej 	 * If we're allowing coalescing, we must allocate a region
1054618c279dSthorpej 	 * descriptor now, since it might block.
1055618c279dSthorpej 	 */
1056111cbb59Sskrll 	const bool coalesce = (ex->ex_flags & EXF_NOCOALESCE) == 0;
10574e465abfSpk 
10586f0c68c5Schristos 	if (coalesce) {
1059618c279dSthorpej 		/* Allocate a region descriptor. */
1060618c279dSthorpej 		nrp = extent_alloc_region_descriptor(ex, flags);
1061618c279dSthorpej 		if (nrp == NULL)
1062618c279dSthorpej 			return (ENOMEM);
1063618c279dSthorpej 	}
1064618c279dSthorpej 
10657de85ed2Sjmcneill 	if (!(ex->ex_flags & EXF_EARLY))
106688ab7da9Sad 		mutex_enter(&ex->ex_lock);
1067618c279dSthorpej 
1068618c279dSthorpej 	/*
10696041e29fSthorpej 	 * Find region and deallocate.  Several possibilities:
10706041e29fSthorpej 	 *
10716041e29fSthorpej 	 *	1. (start == er_start) && (end == er_end):
10726041e29fSthorpej 	 *	   Free descriptor.
10736041e29fSthorpej 	 *
10746041e29fSthorpej 	 *	2. (start == er_start) && (end < er_end):
10756041e29fSthorpej 	 *	   Adjust er_start.
10766041e29fSthorpej 	 *
10776041e29fSthorpej 	 *	3. (start > er_start) && (end == er_end):
10786041e29fSthorpej 	 *	   Adjust er_end.
10796041e29fSthorpej 	 *
10806041e29fSthorpej 	 *	4. (start > er_start) && (end < er_end):
10816041e29fSthorpej 	 *	   Fragment region.  Requires descriptor alloc.
10826041e29fSthorpej 	 *
108352c0d38aSthorpej 	 * Cases 2, 3, and 4 require that the EXF_NOCOALESCE flag
10846041e29fSthorpej 	 * is not set.
10856041e29fSthorpej 	 */
108648bbf5f2Smatt 	LIST_FOREACH(rp, &ex->ex_regions, er_link) {
10876041e29fSthorpej 		/*
10886041e29fSthorpej 		 * Save ourselves some comparisons; does the current
10896041e29fSthorpej 		 * region end before chunk to be freed begins?  If so,
10906041e29fSthorpej 		 * then we haven't found the appropriate region descriptor.
10916041e29fSthorpej 		 */
10926041e29fSthorpej 		if (rp->er_end < start)
10936041e29fSthorpej 			continue;
10946041e29fSthorpej 
10956041e29fSthorpej 		/*
10966041e29fSthorpej 		 * Save ourselves some traversal; does the current
10976041e29fSthorpej 		 * region begin after the chunk to be freed ends?  If so,
10986041e29fSthorpej 		 * then we've already passed any possible region descriptors
10996041e29fSthorpej 		 * that might have contained the chunk to be freed.
11006041e29fSthorpej 		 */
11016041e29fSthorpej 		if (rp->er_start > end)
11026041e29fSthorpej 			break;
11036041e29fSthorpej 
11046041e29fSthorpej 		/* Case 1. */
11056041e29fSthorpej 		if ((start == rp->er_start) && (end == rp->er_end)) {
11066041e29fSthorpej 			LIST_REMOVE(rp, er_link);
11076041e29fSthorpej 			extent_free_region_descriptor(ex, rp);
11086041e29fSthorpej 			goto done;
11096041e29fSthorpej 		}
11106041e29fSthorpej 
11116041e29fSthorpej 		/*
111252c0d38aSthorpej 		 * The following cases all require that EXF_NOCOALESCE
11136041e29fSthorpej 		 * is not set.
11146041e29fSthorpej 		 */
11151e59f0daSchristos 		if (!coalesce)
11166041e29fSthorpej 			continue;
11176041e29fSthorpej 
11186041e29fSthorpej 		/* Case 2. */
11196041e29fSthorpej 		if ((start == rp->er_start) && (end < rp->er_end)) {
11206041e29fSthorpej 			rp->er_start = (end + 1);
11216041e29fSthorpej 			goto done;
11226041e29fSthorpej 		}
11236041e29fSthorpej 
11246041e29fSthorpej 		/* Case 3. */
11256041e29fSthorpej 		if ((start > rp->er_start) && (end == rp->er_end)) {
11266041e29fSthorpej 			rp->er_end = (start - 1);
11276041e29fSthorpej 			goto done;
11286041e29fSthorpej 		}
11296041e29fSthorpej 
11306041e29fSthorpej 		/* Case 4. */
11316041e29fSthorpej 		if ((start > rp->er_start) && (end < rp->er_end)) {
11326041e29fSthorpej 			/* Fill in new descriptor. */
11336041e29fSthorpej 			nrp->er_start = end + 1;
11346041e29fSthorpej 			nrp->er_end = rp->er_end;
11356041e29fSthorpej 
11366041e29fSthorpej 			/* Adjust current descriptor. */
11376041e29fSthorpej 			rp->er_end = start - 1;
11386041e29fSthorpej 
11393036bcf8Spk 			/* Insert new descriptor after current. */
11406041e29fSthorpej 			LIST_INSERT_AFTER(rp, nrp, er_link);
11413036bcf8Spk 
11423036bcf8Spk 			/* We used the new descriptor, so don't free it below */
11433036bcf8Spk 			nrp = NULL;
11446041e29fSthorpej 			goto done;
11456041e29fSthorpej 		}
11466041e29fSthorpej 	}
11476041e29fSthorpej 
11486041e29fSthorpej 	/* Region not found, or request otherwise invalid. */
11497de85ed2Sjmcneill 	if (!(ex->ex_flags & EXF_EARLY))
115088ab7da9Sad 		mutex_exit(&ex->ex_lock);
11516041e29fSthorpej 	extent_print(ex);
1152f443b89cSchristos 	printf("extent_free: start 0x%lx, end 0x%lx\n", start, end);
11536041e29fSthorpej 	panic("extent_free: region not found");
11546041e29fSthorpej 
11556041e29fSthorpej  done:
11563036bcf8Spk 	if (nrp != NULL)
11573036bcf8Spk 		extent_free_region_descriptor(ex, nrp);
11587de85ed2Sjmcneill 	if (!(ex->ex_flags & EXF_EARLY)) {
115988ab7da9Sad 		cv_broadcast(&ex->ex_cv);
116088ab7da9Sad 		mutex_exit(&ex->ex_lock);
11617de85ed2Sjmcneill 	}
11626041e29fSthorpej 	return (0);
11636041e29fSthorpej }
11646041e29fSthorpej 
11656041e29fSthorpej void
extent_print(struct extent * ex)11662f6855b6Sthorpej extent_print(struct extent *ex)
11676041e29fSthorpej {
11686041e29fSthorpej 	struct extent_region *rp;
11696041e29fSthorpej 
11706041e29fSthorpej 	if (ex == NULL)
11716041e29fSthorpej 		panic("extent_print: NULL extent");
11726041e29fSthorpej 
11736745c449Sskrll 	if (!(ex->ex_flags & EXF_EARLY))
117488ab7da9Sad 		mutex_enter(&ex->ex_lock);
1175618c279dSthorpej 
1176f443b89cSchristos 	printf("extent `%s' (0x%lx - 0x%lx), flags = 0x%x\n", ex->ex_name,
11776041e29fSthorpej 	    ex->ex_start, ex->ex_end, ex->ex_flags);
11786041e29fSthorpej 
117948bbf5f2Smatt 	LIST_FOREACH(rp, &ex->ex_regions, er_link)
1180f443b89cSchristos 		printf("     0x%lx - 0x%lx\n", rp->er_start, rp->er_end);
1181618c279dSthorpej 
11826745c449Sskrll 	if (!(ex->ex_flags & EXF_EARLY))
118388ab7da9Sad 		mutex_exit(&ex->ex_lock);
11846041e29fSthorpej }
1185