Home | History | Annotate | Line # | Download | only in kern
subr_extent.c revision 1.26
      1 /*	$NetBSD: subr_extent.c,v 1.26 1999/06/06 22:14:53 pk Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe and Matthias Drochner.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * General purpose extent manager.
     41  */
     42 
     43 #ifdef _KERNEL
     44 #include <sys/param.h>
     45 #include <sys/extent.h>
     46 #include <sys/malloc.h>
     47 #include <sys/pool.h>
     48 #include <sys/time.h>
     49 #include <sys/systm.h>
     50 #include <sys/proc.h>
     51 #include <sys/lock.h>
     52 #elif defined(_EXTENT_TESTING)
     53 /*
     54  * user-land definitions, so it can fit into a testing harness.
     55  */
     56 #include <sys/param.h>
     57 #include <sys/pool.h>
     58 #include <sys/extent.h>
     59 #include <errno.h>
     60 #include <stdlib.h>
     61 #include <stdio.h>
     62 
     63 #define	malloc(s, t, flags)		malloc(s)
     64 #define	free(p, t)			free(p)
     65 #define	tsleep(chan, pri, str, timo)	(EWOULDBLOCK)
     66 #define	wakeup(chan)			((void)0)
     67 #define	pool_get(pool, flags)		malloc(pool->pr_size,0,0)
     68 #define	pool_put(pool, rp)		free(rp,0)
     69 #define	panic(a)			printf(a)
     70 #endif
     71 
     72 static	pool_handle_t expool_create __P((void));
     73 static	void extent_insert_and_optimize __P((struct extent *, u_long, u_long,
     74 	    int, struct extent_region *, struct extent_region *));
     75 static	struct extent_region *extent_alloc_region_descriptor
     76 	    __P((struct extent *, int));
     77 static	void extent_free_region_descriptor __P((struct extent *,
     78 	    struct extent_region *));
     79 
     80 static pool_handle_t expool;
     81 
     82 /*
     83  * Macro to align to an arbitrary power-of-two boundary.
     84  */
     85 #define EXTENT_ALIGN(_start, _align, _skew)		\
     86 	(((((_start) - (_skew)) + ((_align) - 1)) & (-(_align))) + (_skew))
     87 
     88 /*
     89  * Create the extent_region pool.
     90  * (This is deferred until one of our callers thinks we can malloc()).
     91  */
     92 
     93 static pool_handle_t expool_create()
     94 {
     95 #if defined(_KERNEL)
     96 	expool = pool_create(sizeof(struct extent_region), 0, 0,
     97 			     0, "extent", 0, 0, 0, 0);
     98 #else
     99 	expool = (pool_handle_t)malloc(sizeof(*expool),0,0);
    100 	expool->pr_size = sizeof(struct extent_region);
    101 #endif
    102 	return (expool);
    103 }
    104 
    105 /*
    106  * Allocate and initialize an extent map.
    107  */
    108 struct extent *
    109 extent_create(name, start, end, mtype, storage, storagesize, flags)
    110 	const char *name;
    111 	u_long start, end;
    112 	int mtype;
    113 	caddr_t storage;
    114 	size_t storagesize;
    115 	int flags;
    116 {
    117 	struct extent *ex;
    118 	caddr_t cp = storage;
    119 	size_t sz = storagesize;
    120 	struct extent_region *rp;
    121 	int fixed_extent = (storage != NULL);
    122 	int s;
    123 
    124 #ifdef DIAGNOSTIC
    125 	/* Check arguments. */
    126 	if (name == NULL)
    127 		panic("extent_create: name == NULL");
    128 	if (end < start) {
    129 		printf("extent_create: extent `%s', start 0x%lx, end 0x%lx\n",
    130 		    name, start, end);
    131 		panic("extent_create: end < start");
    132 	}
    133 	if (fixed_extent && (storagesize < sizeof(struct extent_fixed)))
    134 		panic("extent_create: fixed extent, bad storagesize 0x%lx",
    135 		    (u_long)storagesize);
    136 	if (fixed_extent == 0 && (storagesize != 0 || storage != NULL))
    137 		panic("extent_create: storage provided for non-fixed");
    138 #endif
    139 
    140 	/* Allocate extent descriptor. */
    141 	if (fixed_extent) {
    142 		struct extent_fixed *fex;
    143 
    144 		memset(storage, 0, storagesize);
    145 
    146 		/*
    147 		 * Align all descriptors on "long" boundaries.
    148 		 */
    149 		fex = (struct extent_fixed *)cp;
    150 		ex = (struct extent *)fex;
    151 		cp += ALIGN(sizeof(struct extent_fixed));
    152 		sz -= ALIGN(sizeof(struct extent_fixed));
    153 		fex->fex_storage = storage;
    154 		fex->fex_storagesize = storagesize;
    155 
    156 		/*
    157 		 * In a fixed extent, we have to pre-allocate region
    158 		 * descriptors and place them in the extent's freelist.
    159 		 */
    160 		LIST_INIT(&fex->fex_freelist);
    161 		while (sz >= ALIGN(sizeof(struct extent_region))) {
    162 			rp = (struct extent_region *)cp;
    163 			cp += ALIGN(sizeof(struct extent_region));
    164 			sz -= ALIGN(sizeof(struct extent_region));
    165 			LIST_INSERT_HEAD(&fex->fex_freelist, rp, er_link);
    166 		}
    167 	} else {
    168 		s = splhigh();
    169 		if (expool == NULL)
    170 			expool_create();
    171 		splx(s);
    172 		if (expool == NULL)
    173 			return (NULL);
    174 
    175 		ex = (struct extent *)malloc(sizeof(struct extent),
    176 		    mtype, (flags & EX_WAITOK) ? M_WAITOK : M_NOWAIT);
    177 		if (ex == NULL)
    178 			return (NULL);
    179 	}
    180 
    181 	/* Fill in the extent descriptor and return it to the caller. */
    182 	simple_lock_init(&ex->ex_slock);
    183 	LIST_INIT(&ex->ex_regions);
    184 	ex->ex_name = name;
    185 	ex->ex_start = start;
    186 	ex->ex_end = end;
    187 	ex->ex_mtype = mtype;
    188 	ex->ex_flags = 0;
    189 	if (fixed_extent)
    190 		ex->ex_flags |= EXF_FIXED;
    191 	if (flags & EX_NOCOALESCE)
    192 		ex->ex_flags |= EXF_NOCOALESCE;
    193 	return (ex);
    194 }
    195 
    196 /*
    197  * Destroy an extent map.
    198  * Since we're freeing the data, there can't be any references
    199  * so we don't need any locking.
    200  */
    201 void
    202 extent_destroy(ex)
    203 	struct extent *ex;
    204 {
    205 	struct extent_region *rp, *orp;
    206 
    207 #ifdef DIAGNOSTIC
    208 	/* Check arguments. */
    209 	if (ex == NULL)
    210 		panic("extent_destroy: NULL extent");
    211 #endif
    212 
    213 	/* Free all region descriptors in extent. */
    214 	for (rp = ex->ex_regions.lh_first; rp != NULL; ) {
    215 		orp = rp;
    216 		rp = rp->er_link.le_next;
    217 		LIST_REMOVE(orp, er_link);
    218 		extent_free_region_descriptor(ex, orp);
    219 	}
    220 
    221 	/* If we're not a fixed extent, free the extent descriptor itself. */
    222 	if ((ex->ex_flags & EXF_FIXED) == 0)
    223 		free(ex, ex->ex_mtype);
    224 }
    225 
    226 /*
    227  * Insert a region descriptor into the sorted region list after the
    228  * entry "after" or at the head of the list (if "after" is NULL).
    229  * The region descriptor we insert is passed in "rp".  We must
    230  * allocate the region descriptor before calling this function!
    231  * If we don't need the region descriptor, it will be freed here.
    232  */
    233 static void
    234 extent_insert_and_optimize(ex, start, size, flags, after, rp)
    235 	struct extent *ex;
    236 	u_long start, size;
    237 	int flags;
    238 	struct extent_region *after, *rp;
    239 {
    240 	struct extent_region *nextr;
    241 	int appended = 0;
    242 
    243 	if (after == NULL) {
    244 		/*
    245 		 * We're the first in the region list.  If there's
    246 		 * a region after us, attempt to coalesce to save
    247 		 * descriptor overhead.
    248 		 */
    249 		if (((ex->ex_flags & EXF_NOCOALESCE) == 0) &&
    250 		    (ex->ex_regions.lh_first != NULL) &&
    251 		    ((start + size) == ex->ex_regions.lh_first->er_start)) {
    252 			/*
    253 			 * We can coalesce.  Prepend us to the first region.
    254 			 */
    255 			ex->ex_regions.lh_first->er_start = start;
    256 			extent_free_region_descriptor(ex, rp);
    257 			return;
    258 		}
    259 
    260 		/*
    261 		 * Can't coalesce.  Fill in the region descriptor
    262 		 * in, and insert us at the head of the region list.
    263 		 */
    264 		rp->er_start = start;
    265 		rp->er_end = start + (size - 1);
    266 		LIST_INSERT_HEAD(&ex->ex_regions, rp, er_link);
    267 		return;
    268 	}
    269 
    270 	/*
    271 	 * If EXF_NOCOALESCE is set, coalescing is disallowed.
    272 	 */
    273 	if (ex->ex_flags & EXF_NOCOALESCE)
    274 		goto cant_coalesce;
    275 
    276 	/*
    277 	 * Attempt to coalesce with the region before us.
    278 	 */
    279 	if ((after->er_end + 1) == start) {
    280 		/*
    281 		 * We can coalesce.  Append ourselves and make
    282 		 * note of it.
    283 		 */
    284 		after->er_end = start + (size - 1);
    285 		appended = 1;
    286 	}
    287 
    288 	/*
    289 	 * Attempt to coalesce with the region after us.
    290 	 */
    291 	if ((after->er_link.le_next != NULL) &&
    292 	    ((start + size) == after->er_link.le_next->er_start)) {
    293 		/*
    294 		 * We can coalesce.  Note that if we appended ourselves
    295 		 * to the previous region, we exactly fit the gap, and
    296 		 * can free the "next" region descriptor.
    297 		 */
    298 		if (appended) {
    299 			/*
    300 			 * Yup, we can free it up.
    301 			 */
    302 			after->er_end = after->er_link.le_next->er_end;
    303 			nextr = after->er_link.le_next;
    304 			LIST_REMOVE(nextr, er_link);
    305 			extent_free_region_descriptor(ex, nextr);
    306 		} else {
    307 			/*
    308 			 * Nope, just prepend us to the next region.
    309 			 */
    310 			after->er_link.le_next->er_start = start;
    311 		}
    312 
    313 		extent_free_region_descriptor(ex, rp);
    314 		return;
    315 	}
    316 
    317 	/*
    318 	 * We weren't able to coalesce with the next region, but
    319 	 * we don't need to allocate a region descriptor if we
    320 	 * appended ourselves to the previous region.
    321 	 */
    322 	if (appended) {
    323 		extent_free_region_descriptor(ex, rp);
    324 		return;
    325 	}
    326 
    327  cant_coalesce:
    328 
    329 	/*
    330 	 * Fill in the region descriptor and insert ourselves
    331 	 * into the region list.
    332 	 */
    333 	rp->er_start = start;
    334 	rp->er_end = start + (size - 1);
    335 	LIST_INSERT_AFTER(after, rp, er_link);
    336 }
    337 
    338 /*
    339  * Allocate a specific region in an extent map.
    340  */
    341 int
    342 extent_alloc_region(ex, start, size, flags)
    343 	struct extent *ex;
    344 	u_long start, size;
    345 	int flags;
    346 {
    347 	struct extent_region *rp, *last, *myrp;
    348 	u_long end = start + (size - 1);
    349 	int error;
    350 
    351 #ifdef DIAGNOSTIC
    352 	/* Check arguments. */
    353 	if (ex == NULL)
    354 		panic("extent_alloc_region: NULL extent");
    355 	if (size < 1) {
    356 		printf("extent_alloc_region: extent `%s', size 0x%lx\n",
    357 		    ex->ex_name, size);
    358 		panic("extent_alloc_region: bad size");
    359 	}
    360 	if (end < start) {
    361 		printf(
    362 		 "extent_alloc_region: extent `%s', start 0x%lx, size 0x%lx\n",
    363 		 ex->ex_name, start, size);
    364 		panic("extent_alloc_region: overflow");
    365 	}
    366 #endif
    367 
    368 	/*
    369 	 * Make sure the requested region lies within the
    370 	 * extent.
    371 	 *
    372 	 * We don't lock to check the range, because those values
    373 	 * are never modified, and if another thread deletes the
    374 	 * extent, we're screwed anyway.
    375 	 */
    376 	if ((start < ex->ex_start) || (end > ex->ex_end)) {
    377 #ifdef DIAGNOSTIC
    378 		printf("extent_alloc_region: extent `%s' (0x%lx - 0x%lx)\n",
    379 		    ex->ex_name, ex->ex_start, ex->ex_end);
    380 		printf("extent_alloc_region: start 0x%lx, end 0x%lx\n",
    381 		    start, end);
    382 		panic("extent_alloc_region: region lies outside extent");
    383 #else
    384 		return (EINVAL);
    385 #endif
    386 	}
    387 
    388 	/*
    389 	 * Allocate the region descriptor.  It will be freed later
    390 	 * if we can coalesce with another region.  Don't lock before
    391 	 * here!  This could block.
    392 	 */
    393 	myrp = extent_alloc_region_descriptor(ex, flags);
    394 	if (myrp == NULL) {
    395 #ifdef DIAGNOSTIC
    396 		printf(
    397 		    "extent_alloc_region: can't allocate region descriptor\n");
    398 #endif
    399 		return (ENOMEM);
    400 	}
    401 
    402  alloc_start:
    403 	simple_lock(&ex->ex_slock);
    404 
    405 	/*
    406 	 * Attempt to place ourselves in the desired area of the
    407 	 * extent.  We save ourselves some work by keeping the list sorted.
    408 	 * In other words, if the start of the current region is greater
    409 	 * than the end of our region, we don't have to search any further.
    410 	 */
    411 
    412 	/*
    413 	 * Keep a pointer to the last region we looked at so
    414 	 * that we don't have to traverse the list again when
    415 	 * we insert ourselves.  If "last" is NULL when we
    416 	 * finally insert ourselves, we go at the head of the
    417 	 * list.  See extent_insert_and_optimize() for details.
    418 	 */
    419 	last = NULL;
    420 
    421 	for (rp = ex->ex_regions.lh_first; rp != NULL;
    422 	    rp = rp->er_link.le_next) {
    423 		if (rp->er_start > end) {
    424 			/*
    425 			 * We lie before this region and don't
    426 			 * conflict.
    427 			 */
    428 			break;
    429 		}
    430 
    431 		/*
    432 		 * The current region begins before we end.
    433 		 * Check for a conflict.
    434 		 */
    435 		if (rp->er_end >= start) {
    436 			/*
    437 			 * We conflict.  If we can (and want to) wait,
    438 			 * do so.
    439 			 */
    440 			if (flags & EX_WAITSPACE) {
    441 				ex->ex_flags |= EXF_WANTED;
    442 				simple_unlock(&ex->ex_slock);
    443 				error = tsleep(ex,
    444 				    PRIBIO | ((flags & EX_CATCH) ? PCATCH : 0),
    445 				    "extnt", 0);
    446 				if (error)
    447 					return (error);
    448 				goto alloc_start;
    449 			}
    450 			extent_free_region_descriptor(ex, myrp);
    451 			simple_unlock(&ex->ex_slock);
    452 			return (EAGAIN);
    453 		}
    454 		/*
    455 		 * We don't conflict, but this region lies before
    456 		 * us.  Keep a pointer to this region, and keep
    457 		 * trying.
    458 		 */
    459 		last = rp;
    460 	}
    461 
    462 	/*
    463 	 * We don't conflict with any regions.  "last" points
    464 	 * to the region we fall after, or is NULL if we belong
    465 	 * at the beginning of the region list.  Insert ourselves.
    466 	 */
    467 	extent_insert_and_optimize(ex, start, size, flags, last, myrp);
    468 	simple_unlock(&ex->ex_slock);
    469 	return (0);
    470 }
    471 
    472 /*
    473  * Macro to check (x + y) <= z.  This check is designed to fail
    474  * if an overflow occurs.
    475  */
    476 #define LE_OV(x, y, z)	((((x) + (y)) >= (x)) && (((x) + (y)) <= (z)))
    477 
    478 /*
    479  * Allocate a region in an extent map subregion.
    480  *
    481  * If EX_FAST is specified, we return the first fit in the map.
    482  * Otherwise, we try to minimize fragmentation by finding the
    483  * smallest gap that will hold the request.
    484  *
    485  * The allocated region is aligned to "alignment", which must be
    486  * a power of 2.
    487  */
    488 int
    489 extent_alloc_subregion1(ex, substart, subend, size, alignment, skew, boundary,
    490     flags, result)
    491 	struct extent *ex;
    492 	u_long substart, subend, size, alignment, skew, boundary;
    493 	int flags;
    494 	u_long *result;
    495 {
    496 	struct extent_region *rp, *myrp, *last, *bestlast;
    497 	u_long newstart, newend, beststart, bestovh, ovh;
    498 	u_long dontcross;
    499 	int error;
    500 
    501 #ifdef DIAGNOSTIC
    502 	/*
    503 	 * Check arguments.
    504 	 *
    505 	 * We don't lock to check these, because these values
    506 	 * are never modified, and if another thread deletes the
    507 	 * extent, we're screwed anyway.
    508 	 */
    509 	if (ex == NULL)
    510 		panic("extent_alloc_subregion: NULL extent");
    511 	if (result == NULL)
    512 		panic("extent_alloc_subregion: NULL result pointer");
    513 	if ((substart < ex->ex_start) || (substart > ex->ex_end) ||
    514 	    (subend > ex->ex_end) || (subend < ex->ex_start)) {
    515   printf("extent_alloc_subregion: extent `%s', ex_start 0x%lx, ex_end 0x%lx\n",
    516 		    ex->ex_name, ex->ex_start, ex->ex_end);
    517 		printf("extent_alloc_subregion: substart 0x%lx, subend 0x%lx\n",
    518 		    substart, subend);
    519 		panic("extent_alloc_subregion: bad subregion");
    520 	}
    521 	if ((size < 1) || ((size - 1) > (subend - substart))) {
    522 		printf("extent_alloc_subregion: extent `%s', size 0x%lx\n",
    523 		    ex->ex_name, size);
    524 		panic("extent_alloc_subregion: bad size");
    525 	}
    526 	if (alignment == 0)
    527 		panic("extent_alloc_subregion: bad alignment");
    528 	if (boundary && (boundary < size)) {
    529 		printf(
    530 		    "extent_alloc_subregion: extent `%s', size 0x%lx,
    531 		    boundary 0x%lx\n", ex->ex_name, size, boundary);
    532 		panic("extent_alloc_subregion: bad boundary");
    533 	}
    534 #endif
    535 
    536 	/*
    537 	 * Allocate the region descriptor.  It will be freed later
    538 	 * if we can coalesce with another region.  Don't lock before
    539 	 * here!  This could block.
    540 	 */
    541 	myrp = extent_alloc_region_descriptor(ex, flags);
    542 	if (myrp == NULL) {
    543 #ifdef DIAGNOSTIC
    544 		printf(
    545 		 "extent_alloc_subregion: can't allocate region descriptor\n");
    546 #endif
    547 		return (ENOMEM);
    548 	}
    549 
    550  alloc_start:
    551 	simple_lock(&ex->ex_slock);
    552 
    553 	/*
    554 	 * Keep a pointer to the last region we looked at so
    555 	 * that we don't have to traverse the list again when
    556 	 * we insert ourselves.  If "last" is NULL when we
    557 	 * finally insert ourselves, we go at the head of the
    558 	 * list.  See extent_insert_and_optimize() for deatails.
    559 	 */
    560 	last = NULL;
    561 
    562 	/*
    563 	 * Keep track of size and location of the smallest
    564 	 * chunk we fit in.
    565 	 *
    566 	 * Since the extent can be as large as the numeric range
    567 	 * of the CPU (0 - 0xffffffff for 32-bit systems), the
    568 	 * best overhead value can be the maximum unsigned integer.
    569 	 * Thus, we initialize "bestovh" to 0, since we insert ourselves
    570 	 * into the region list immediately on an exact match (which
    571 	 * is the only case where "bestovh" would be set to 0).
    572 	 */
    573 	bestovh = 0;
    574 	beststart = 0;
    575 	bestlast = NULL;
    576 
    577 	/*
    578 	 * For N allocated regions, we must make (N + 1)
    579 	 * checks for unallocated space.  The first chunk we
    580 	 * check is the area from the beginning of the subregion
    581 	 * to the first allocated region after that point.
    582 	 */
    583 	newstart = EXTENT_ALIGN(substart, alignment, skew);
    584 	if (newstart < ex->ex_start) {
    585 #ifdef DIAGNOSTIC
    586 		printf(
    587       "extent_alloc_subregion: extent `%s' (0x%lx - 0x%lx), alignment 0x%lx\n",
    588 		 ex->ex_name, ex->ex_start, ex->ex_end, alignment);
    589 		simple_unlock(&ex->ex_slock);
    590 		panic("extent_alloc_subregion: overflow after alignment");
    591 #else
    592 		extent_free_region_descriptor(ex, myrp);
    593 		simple_unlock(&ex->ex_slock);
    594 		return (EINVAL);
    595 #endif
    596 	}
    597 
    598 	/*
    599 	 * Find the first allocated region that begins on or after
    600 	 * the subregion start, advancing the "last" pointer along
    601 	 * the way.
    602 	 */
    603 	for (rp = ex->ex_regions.lh_first; rp != NULL;
    604 	     rp = rp->er_link.le_next) {
    605 		if (rp->er_start >= newstart)
    606 			break;
    607 		last = rp;
    608 	}
    609 
    610 	/*
    611 	 * Relocate the start of our candidate region to the end of
    612 	 * the last allocated region (if there was one overlapping
    613 	 * our subrange).
    614 	 */
    615 	if (last != NULL && last->er_end >= newstart)
    616 		newstart = EXTENT_ALIGN((last->er_end + 1), alignment, skew);
    617 
    618 	for (; rp != NULL; rp = rp->er_link.le_next) {
    619 		/*
    620 		 * Check the chunk before "rp".  Note that our
    621 		 * comparison is safe from overflow conditions.
    622 		 */
    623 		if (LE_OV(newstart, size, rp->er_start)) {
    624 			/*
    625 			 * Do a boundary check, if necessary.  Note
    626 			 * that a region may *begin* on the boundary,
    627 			 * but it must end before the boundary.
    628 			 */
    629 			if (boundary) {
    630 				newend = newstart + (size - 1);
    631 
    632 				/*
    633 				 * Calculate the next boundary after the start
    634 				 * of this region.
    635 				 */
    636 				dontcross = EXTENT_ALIGN(newstart+1, boundary,
    637 				    (flags & EX_BOUNDZERO) ? 0 : ex->ex_start)
    638 				    - 1;
    639 
    640 #if 0
    641 				printf("newstart=%x newend=%x ex_start=%x ex_end=%x boundary=%x dontcross=%x\n",
    642 				    newstart, newend, ex->ex_start, ex->ex_end,
    643 				    boundary, dontcross);
    644 #endif
    645 
    646 				if (newend > dontcross) {
    647 					/*
    648 					 * Candidate region crosses boundary.
    649 					 * Throw away the leading part and see
    650 					 * if we still fit.
    651 					 */
    652 					newstart = dontcross + 1;
    653 					newend = newstart + (size - 1);
    654 					dontcross += boundary;
    655 					if (!LE_OV(newstart, size, rp->er_start))
    656 						continue;
    657 				}
    658 
    659 				/*
    660 				 * If we run past the end of
    661 				 * the extent or the boundary
    662 				 * overflows, then the request
    663 				 * can't fit.
    664 				 */
    665 				if (dontcross > ex->ex_end ||
    666 				    dontcross < newstart)
    667 					goto fail;
    668 			}
    669 
    670 			/*
    671 			 * We would fit into this space.  Calculate
    672 			 * the overhead (wasted space).  If we exactly
    673 			 * fit, or we're taking the first fit, insert
    674 			 * ourselves into the region list.
    675 			 */
    676 			ovh = rp->er_start - newstart - size;
    677 			if ((flags & EX_FAST) || (ovh == 0))
    678 				goto found;
    679 
    680 			/*
    681 			 * Don't exactly fit, but check to see
    682 			 * if we're better than any current choice.
    683 			 */
    684 			if ((bestovh == 0) || (ovh < bestovh)) {
    685 				bestovh = ovh;
    686 				beststart = newstart;
    687 				bestlast = last;
    688 			}
    689 		}
    690 
    691 		/*
    692 		 * Skip past the current region and check again.
    693 		 */
    694 		newstart = EXTENT_ALIGN((rp->er_end + 1), alignment, skew);
    695 		if (newstart < rp->er_end) {
    696 			/*
    697 			 * Overflow condition.  Don't error out, since
    698 			 * we might have a chunk of space that we can
    699 			 * use.
    700 			 */
    701 			goto fail;
    702 		}
    703 
    704 		last = rp;
    705 	}
    706 
    707 	/*
    708 	 * The final check is from the current starting point to the
    709 	 * end of the subregion.  If there were no allocated regions,
    710 	 * "newstart" is set to the beginning of the subregion, or
    711 	 * just past the end of the last allocated region, adjusted
    712 	 * for alignment in either case.
    713 	 */
    714 	if (LE_OV(newstart, (size - 1), subend)) {
    715 		/*
    716 		 * Do a boundary check, if necessary.  Note
    717 		 * that a region may *begin* on the boundary,
    718 		 * but it must end before the boundary.
    719 		 */
    720 		if (boundary) {
    721 			newend = newstart + (size - 1);
    722 
    723 			/*
    724 			 * Calculate the next boundary after the start
    725 			 * of this region.
    726 			 */
    727 			dontcross = EXTENT_ALIGN(newstart+1, boundary,
    728 			    (flags & EX_BOUNDZERO) ? 0 : ex->ex_start)
    729 			    - 1;
    730 
    731 #if 0
    732 			printf("newstart=%x newend=%x ex_start=%x ex_end=%x boundary=%x dontcross=%x\n",
    733 			    newstart, newend, ex->ex_start, ex->ex_end,
    734 			    boundary, dontcross);
    735 #endif
    736 
    737 			if (newend > dontcross) {
    738 				/*
    739 				 * Candidate region crosses boundary.
    740 				 * Throw away the leading part and see
    741 				 * if we still fit.
    742 				 */
    743 				newstart = dontcross + 1;
    744 				newend = newstart + (size - 1);
    745 				dontcross += boundary;
    746 				if (!LE_OV(newstart, (size - 1), subend))
    747 					goto fail;
    748 			}
    749 
    750 			/*
    751 			 * If we run past the end of
    752 			 * the extent or the boundary
    753 			 * overflows, then the request
    754 			 * can't fit.
    755 			 */
    756 			if (dontcross > ex->ex_end ||
    757 			    dontcross < newstart)
    758 				goto fail;
    759 		}
    760 
    761 		/*
    762 		 * We would fit into this space.  Calculate
    763 		 * the overhead (wasted space).  If we exactly
    764 		 * fit, or we're taking the first fit, insert
    765 		 * ourselves into the region list.
    766 		 */
    767 		ovh = ex->ex_end - newstart - (size - 1);
    768 		if ((flags & EX_FAST) || (ovh == 0))
    769 			goto found;
    770 
    771 		/*
    772 		 * Don't exactly fit, but check to see
    773 		 * if we're better than any current choice.
    774 		 */
    775 		if ((bestovh == 0) || (ovh < bestovh)) {
    776 			bestovh = ovh;
    777 			beststart = newstart;
    778 			bestlast = last;
    779 		}
    780 	}
    781 
    782  fail:
    783 	/*
    784 	 * One of the following two conditions have
    785 	 * occurred:
    786 	 *
    787 	 *	There is no chunk large enough to hold the request.
    788 	 *
    789 	 *	If EX_FAST was not specified, there is not an
    790 	 *	exact match for the request.
    791 	 *
    792 	 * Note that if we reach this point and EX_FAST is
    793 	 * set, then we know there is no space in the extent for
    794 	 * the request.
    795 	 */
    796 	if (((flags & EX_FAST) == 0) && (bestovh != 0)) {
    797 		/*
    798 		 * We have a match that's "good enough".
    799 		 */
    800 		newstart = beststart;
    801 		last = bestlast;
    802 		goto found;
    803 	}
    804 
    805 	/*
    806 	 * No space currently available.  Wait for it to free up,
    807 	 * if possible.
    808 	 */
    809 	if (flags & EX_WAITSPACE) {
    810 		ex->ex_flags |= EXF_WANTED;
    811 		simple_unlock(&ex->ex_slock);
    812 		error = tsleep(ex,
    813 		    PRIBIO | ((flags & EX_CATCH) ? PCATCH : 0), "extnt", 0);
    814 		if (error)
    815 			return (error);
    816 		goto alloc_start;
    817 	}
    818 
    819 	extent_free_region_descriptor(ex, myrp);
    820 	simple_unlock(&ex->ex_slock);
    821 	return (EAGAIN);
    822 
    823  found:
    824 	/*
    825 	 * Insert ourselves into the region list.
    826 	 */
    827 	extent_insert_and_optimize(ex, newstart, size, flags, last, myrp);
    828 	simple_unlock(&ex->ex_slock);
    829 	*result = newstart;
    830 	return (0);
    831 }
    832 
    833 int
    834 extent_free(ex, start, size, flags)
    835 	struct extent *ex;
    836 	u_long start, size;
    837 	int flags;
    838 {
    839 	struct extent_region *rp, *nrp = NULL;
    840 	u_long end = start + (size - 1);
    841 	int exflags;
    842 
    843 #ifdef DIAGNOSTIC
    844 	/*
    845 	 * Check arguments.
    846 	 *
    847 	 * We don't lock to check these, because these values
    848 	 * are never modified, and if another thread deletes the
    849 	 * extent, we're screwed anyway.
    850 	 */
    851 	if (ex == NULL)
    852 		panic("extent_free: NULL extent");
    853 	if ((start < ex->ex_start) || (start > ex->ex_end)) {
    854 		extent_print(ex);
    855 		printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
    856 		    ex->ex_name, start, size);
    857 		panic("extent_free: extent `%s', region not within extent",
    858 		    ex->ex_name);
    859 	}
    860 	/* Check for an overflow. */
    861 	if (end < start) {
    862 		extent_print(ex);
    863 		printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
    864 		    ex->ex_name, start, size);
    865 		panic("extent_free: overflow");
    866 	}
    867 #endif
    868 
    869 	/*
    870 	 * If we're allowing coalescing, we must allocate a region
    871 	 * descriptor now, since it might block.
    872 	 *
    873 	 * XXX Make a static, create-time flags word, so we don't
    874 	 * XXX have to lock to read it!
    875 	 */
    876 	simple_lock(&ex->ex_slock);
    877 	exflags = ex->ex_flags;
    878 	simple_unlock(&ex->ex_slock);
    879 
    880 	if ((exflags & EXF_NOCOALESCE) == 0) {
    881 		/* Allocate a region descriptor. */
    882 		nrp = extent_alloc_region_descriptor(ex, flags);
    883 		if (nrp == NULL)
    884 			return (ENOMEM);
    885 	}
    886 
    887 	simple_lock(&ex->ex_slock);
    888 
    889 	/*
    890 	 * Find region and deallocate.  Several possibilities:
    891 	 *
    892 	 *	1. (start == er_start) && (end == er_end):
    893 	 *	   Free descriptor.
    894 	 *
    895 	 *	2. (start == er_start) && (end < er_end):
    896 	 *	   Adjust er_start.
    897 	 *
    898 	 *	3. (start > er_start) && (end == er_end):
    899 	 *	   Adjust er_end.
    900 	 *
    901 	 *	4. (start > er_start) && (end < er_end):
    902 	 *	   Fragment region.  Requires descriptor alloc.
    903 	 *
    904 	 * Cases 2, 3, and 4 require that the EXF_NOCOALESCE flag
    905 	 * is not set.
    906 	 */
    907 	for (rp = ex->ex_regions.lh_first; rp != NULL;
    908 	    rp = rp->er_link.le_next) {
    909 		/*
    910 		 * Save ourselves some comparisons; does the current
    911 		 * region end before chunk to be freed begins?  If so,
    912 		 * then we haven't found the appropriate region descriptor.
    913 		 */
    914 		if (rp->er_end < start)
    915 			continue;
    916 
    917 		/*
    918 		 * Save ourselves some traversal; does the current
    919 		 * region begin after the chunk to be freed ends?  If so,
    920 		 * then we've already passed any possible region descriptors
    921 		 * that might have contained the chunk to be freed.
    922 		 */
    923 		if (rp->er_start > end)
    924 			break;
    925 
    926 		/* Case 1. */
    927 		if ((start == rp->er_start) && (end == rp->er_end)) {
    928 			LIST_REMOVE(rp, er_link);
    929 			extent_free_region_descriptor(ex, rp);
    930 			goto done;
    931 		}
    932 
    933 		/*
    934 		 * The following cases all require that EXF_NOCOALESCE
    935 		 * is not set.
    936 		 */
    937 		if (ex->ex_flags & EXF_NOCOALESCE)
    938 			continue;
    939 
    940 		/* Case 2. */
    941 		if ((start == rp->er_start) && (end < rp->er_end)) {
    942 			rp->er_start = (end + 1);
    943 			goto done;
    944 		}
    945 
    946 		/* Case 3. */
    947 		if ((start > rp->er_start) && (end == rp->er_end)) {
    948 			rp->er_end = (start - 1);
    949 			goto done;
    950 		}
    951 
    952 		/* Case 4. */
    953 		if ((start > rp->er_start) && (end < rp->er_end)) {
    954 			/* Fill in new descriptor. */
    955 			nrp->er_start = end + 1;
    956 			nrp->er_end = rp->er_end;
    957 
    958 			/* Adjust current descriptor. */
    959 			rp->er_end = start - 1;
    960 
    961 			/* Insert new descriptor after current. */
    962 			LIST_INSERT_AFTER(rp, nrp, er_link);
    963 
    964 			/* We used the new descriptor, so don't free it below */
    965 			nrp = NULL;
    966 			goto done;
    967 		}
    968 	}
    969 
    970 	/* Region not found, or request otherwise invalid. */
    971 	simple_unlock(&ex->ex_slock);
    972 	extent_print(ex);
    973 	printf("extent_free: start 0x%lx, end 0x%lx\n", start, end);
    974 	panic("extent_free: region not found");
    975 
    976  done:
    977 	if (nrp != NULL)
    978 		extent_free_region_descriptor(ex, nrp);
    979 	if (ex->ex_flags & EXF_WANTED) {
    980 		ex->ex_flags &= ~EXF_WANTED;
    981 		wakeup(ex);
    982 	}
    983 	simple_unlock(&ex->ex_slock);
    984 	return (0);
    985 }
    986 
    987 /*
    988  * Allocate an extent region descriptor.  EXTENT MUST NOT BE LOCKED,
    989  * AS THIS FUNCTION MAY BLOCK!  We will handle any locking we may need.
    990  */
    991 static struct extent_region *
    992 extent_alloc_region_descriptor(ex, flags)
    993 	struct extent *ex;
    994 	int flags;
    995 {
    996 	struct extent_region *rp;
    997 	int exflags;
    998 	int s;
    999 
   1000 	/*
   1001 	 * XXX Make a static, create-time flags word, so we don't
   1002 	 * XXX have to lock to read it!
   1003 	 */
   1004 	simple_lock(&ex->ex_slock);
   1005 	exflags = ex->ex_flags;
   1006 	simple_unlock(&ex->ex_slock);
   1007 
   1008 	if (exflags & EXF_FIXED) {
   1009 		struct extent_fixed *fex = (struct extent_fixed *)ex;
   1010 
   1011 		for (;;) {
   1012 			simple_lock(&ex->ex_slock);
   1013 			if ((rp = fex->fex_freelist.lh_first) != NULL) {
   1014 				/*
   1015 				 * Don't muck with flags after pulling it off
   1016 				 * the freelist; it may have been dynamically
   1017 				 * allocated, and kindly given to us.  We
   1018 				 * need to remember that information.
   1019 				 */
   1020 				LIST_REMOVE(rp, er_link);
   1021 				simple_unlock(&ex->ex_slock);
   1022 				return (rp);
   1023 			}
   1024 			if (flags & EX_MALLOCOK) {
   1025 				simple_unlock(&ex->ex_slock);
   1026 				goto alloc;
   1027 			}
   1028 			if ((flags & EX_WAITOK) == 0) {
   1029 				simple_unlock(&ex->ex_slock);
   1030 				return (NULL);
   1031 			}
   1032 			ex->ex_flags |= EXF_FLWANTED;
   1033 			simple_unlock(&ex->ex_slock);
   1034 			if (tsleep(&fex->fex_freelist,
   1035 			    PRIBIO | ((flags & EX_CATCH) ? PCATCH : 0),
   1036 			    "extnt", 0))
   1037 				return (NULL);
   1038 		}
   1039 	}
   1040 
   1041  alloc:
   1042 	s = splhigh();
   1043 	if (expool == NULL && !expool_create()) {
   1044 		splx(s);
   1045 		return (NULL);
   1046 	}
   1047 
   1048 	rp = pool_get(expool, (flags & EX_WAITOK) ? PR_WAITOK : 0);
   1049 	splx(s);
   1050 
   1051 	if (rp != NULL)
   1052 		rp->er_flags = ER_ALLOC;
   1053 
   1054 	return (rp);
   1055 }
   1056 
   1057 /*
   1058  * Free an extent region descriptor.  EXTENT _MUST_ BE LOCKED!  This
   1059  * is safe as we do not block here.
   1060  */
   1061 static void
   1062 extent_free_region_descriptor(ex, rp)
   1063 	struct extent *ex;
   1064 	struct extent_region *rp;
   1065 {
   1066 	int s;
   1067 
   1068 	if (ex->ex_flags & EXF_FIXED) {
   1069 		struct extent_fixed *fex = (struct extent_fixed *)ex;
   1070 
   1071 		/*
   1072 		 * If someone's waiting for a region descriptor,
   1073 		 * be nice and give them this one, rather than
   1074 		 * just free'ing it back to the system.
   1075 		 */
   1076 		if (rp->er_flags & ER_ALLOC) {
   1077 			if (ex->ex_flags & EXF_FLWANTED) {
   1078 				/* Clear all but ER_ALLOC flag. */
   1079 				rp->er_flags = ER_ALLOC;
   1080 				LIST_INSERT_HEAD(&fex->fex_freelist, rp,
   1081 				    er_link);
   1082 				goto wake_em_up;
   1083 			} else {
   1084 				s = splhigh();
   1085 				pool_put(expool, rp);
   1086 				splx(s);
   1087 			}
   1088 		} else {
   1089 			/* Clear all flags. */
   1090 			rp->er_flags = 0;
   1091 			LIST_INSERT_HEAD(&fex->fex_freelist, rp, er_link);
   1092 		}
   1093 
   1094 		if (ex->ex_flags & EXF_FLWANTED) {
   1095  wake_em_up:
   1096 			ex->ex_flags &= ~EXF_FLWANTED;
   1097 			wakeup(&fex->fex_freelist);
   1098 		}
   1099 		return;
   1100 	}
   1101 
   1102 	/*
   1103 	 * We know it's dynamically allocated if we get here.
   1104 	 */
   1105 	s = splhigh();
   1106 	pool_put(expool, rp);
   1107 	splx(s);
   1108 }
   1109 
   1110 void
   1111 extent_print(ex)
   1112 	struct extent *ex;
   1113 {
   1114 	struct extent_region *rp;
   1115 
   1116 	if (ex == NULL)
   1117 		panic("extent_print: NULL extent");
   1118 
   1119 	simple_lock(&ex->ex_slock);
   1120 
   1121 	printf("extent `%s' (0x%lx - 0x%lx), flags = 0x%x\n", ex->ex_name,
   1122 	    ex->ex_start, ex->ex_end, ex->ex_flags);
   1123 
   1124 	for (rp = ex->ex_regions.lh_first; rp != NULL;
   1125 	    rp = rp->er_link.le_next)
   1126 		printf("     0x%lx - 0x%lx\n", rp->er_start, rp->er_end);
   1127 
   1128 	simple_unlock(&ex->ex_slock);
   1129 }
   1130