Home | History | Annotate | Line # | Download | only in kern
subr_extent.c revision 1.2
      1 /*	$NetBSD: subr_extent.c,v 1.2 1996/07/23 23:09:10 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 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 REGENTS OR CONTRIBUTORS BE
     30  * 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 #include <sys/param.h>
     44 #include <sys/extent.h>
     45 #include <sys/malloc.h>
     46 #include <sys/time.h>
     47 #include <sys/systm.h>
     48 #include <sys/proc.h>
     49 
     50 static	int extent_insert_and_optimize __P((struct extent *, u_long, u_long,
     51 	    int, struct extent_region *));
     52 static	struct extent_region *extent_alloc_region_descriptor
     53 	    __P((struct extent *, int));
     54 static	void extent_free_region_descriptor __P((struct extent *,
     55 	    struct extent_region *));
     56 
     57 /*
     58  * Allocate and initialize an extent map.
     59  */
     60 struct extent *
     61 extent_create(name, start, end, mtype, storage, storagesize, flags)
     62 	char *name;
     63 	u_long start, end;
     64 	int mtype;
     65 	caddr_t storage;
     66 	size_t storagesize;
     67 	int flags;
     68 {
     69 	struct extent *ex;
     70 	caddr_t cp = storage;
     71 	size_t sz = storagesize;
     72 	struct extent_region *rp;
     73 	int fixed_extent = (storage != NULL);
     74 
     75 	/* Check arguments. */
     76 	if (name == NULL)
     77 		panic("extent_create: name == NULL");
     78 	if (end < start) {
     79 		printf("extent_create: extent `%s', start 0x%lx, end 0x%lx\n",
     80 		    name, start, end);
     81 		panic("extent_create: end < start");
     82 	}
     83 	if (fixed_extent && (storagesize < sizeof(struct extent_fixed)))
     84 		panic("extent_create: fixed extent, bad storagesize 0x%x",
     85 		    storagesize);
     86 
     87 	/* Allocate extent descriptor. */
     88 	if (fixed_extent) {
     89 		struct extent_fixed *fex;
     90 
     91 		bzero(storage, storagesize);
     92 
     93 		/*
     94 		 * Align all descriptors on "long" boundaries.
     95 		 */
     96 		fex = (struct extent_fixed *)cp;
     97 		ex = (struct extent *)fex;
     98 		cp += EXTENT_ALIGN(sizeof(struct extent_fixed), sizeof(long));
     99 		sz -= EXTENT_ALIGN(sizeof(struct extent_fixed), sizeof(long));
    100 		fex->fex_storage = storage;
    101 		fex->fex_storagesize = storagesize;
    102 
    103 		/*
    104 		 * In a fixed extent, we have to pre-allocate region
    105 		 * descriptors and place them in the extent's freelist.
    106 		 */
    107 		LIST_INIT(&fex->fex_freelist);
    108 		while (sz >= sizeof(struct extent_region)) {
    109 			rp = (struct extent_region *)cp;
    110 			cp += EXTENT_ALIGN(sizeof(struct extent_region),
    111 			    sizeof(long));
    112 			sz -= EXTENT_ALIGN(sizeof(struct extent_region),
    113 			    sizeof(long));
    114 			LIST_INSERT_HEAD(&fex->fex_freelist, rp, er_link);
    115 		}
    116 	} else {
    117 		ex = (struct extent *)malloc(sizeof(struct extent),
    118 		    mtype, (flags & EX_WAITOK) ? M_WAITOK : M_NOWAIT);
    119 		if (ex == NULL)
    120 			return (NULL);
    121 	}
    122 
    123 	/* Fill in the extent descriptor and return it to the caller. */
    124 	LIST_INIT(&ex->ex_regions);
    125 	ex->ex_name = name;
    126 	ex->ex_start = start;
    127 	ex->ex_end = end;
    128 	ex->ex_mtype = mtype;
    129 	ex->ex_flags = 0;
    130 	if (fixed_extent)
    131 		ex->ex_flags |= EXF_FIXED;
    132 	if (flags & EX_NOBLOB)
    133 		ex->ex_flags |= EXF_NOBLOB;
    134 	return (ex);
    135 }
    136 
    137 /*
    138  * Destroy an extent map.
    139  */
    140 void
    141 extent_destroy(ex)
    142 	struct extent *ex;
    143 {
    144 	struct extent_region *rp, *orp;
    145 	int mtype;
    146 
    147 	/* Check arguments. */
    148 	if (ex == NULL)
    149 		panic("extent_destroy: NULL extent");
    150 
    151 	mtype = ex->ex_mtype;
    152 
    153 	/* Free all region descriptors in extent. */
    154 	for (rp = ex->ex_regions.lh_first; rp != NULL; ) {
    155 		orp = rp;
    156 		rp = rp->er_link.le_next;
    157 		LIST_REMOVE(orp, er_link);
    158 		extent_free_region_descriptor(ex, orp);
    159 	}
    160 
    161 	/* If we're not a fixed extent, free the extent descriptor itself. */
    162 	if ((ex->ex_flags & EXF_FIXED) == 0)
    163 		free(ex, mtype);
    164 }
    165 
    166 /*
    167  * Insert a region descriptor into the sorted region list after the
    168  * entry "after" or at the head of the list (if "after" is NULL).
    169  */
    170 static int
    171 extent_insert_and_optimize(ex, start, size, flags, after)
    172 	struct extent *ex;
    173 	u_long start, size;
    174 	int flags;
    175 	struct extent_region *after;
    176 {
    177 	struct extent_region *rp;
    178 	int appended = 0;
    179 
    180 	if (after == NULL) {
    181 		/*
    182 		 * We're the first in the region list.  If there's
    183 		 * a region after us, attempt to coalesce to save
    184 		 * descriptor overhead.
    185 		 */
    186 		if (((ex->ex_flags & EXF_NOBLOB) == 0) &&
    187 		    (ex->ex_regions.lh_first != NULL) &&
    188 		    ((start + size) == ex->ex_regions.lh_first->er_start)) {
    189 			/*
    190 			 * We can coalesce.  Prepend us to the first region.
    191 			 */
    192 			ex->ex_regions.lh_first->er_start = start;
    193 			return (0);
    194 		}
    195 
    196 		/*
    197 		 * Can't coalesce.  Allocate a region descriptor, fill it
    198 		 * in, and insert us at the head of the region list.
    199 		 */
    200 		rp = extent_alloc_region_descriptor(ex, flags);
    201 		if (rp == NULL) {
    202 #if defined(DEBUG) || defined(DIAGNOSTIC)
    203 			printf("extent `%s': can't alloc region descriptor.\n",
    204 			    ex->ex_name);
    205 #endif
    206 			return (ENOMEM);
    207 		}
    208 		rp->er_start = start;
    209 		rp->er_end = start + (size - 1);
    210 		LIST_INSERT_HEAD(&ex->ex_regions, rp, er_link);
    211 		return (0);
    212 	}
    213 
    214 	/*
    215 	 * If EXF_NOBLOB is set, coalescing is disallowed.
    216 	 */
    217 	if (ex->ex_flags & EXF_NOBLOB)
    218 		goto allocate_region_descriptor;
    219 
    220 	/*
    221 	 * Attempt to coalesce with the region before us.
    222 	 */
    223 	if ((after->er_end + 1) == start) {
    224 		/*
    225 		 * We can coalesce.  Append ourselves and make
    226 		 * note of it.
    227 		 */
    228 		after->er_end = start + (size - 1);
    229 		appended = 1;
    230 	}
    231 
    232 	/*
    233 	 * Attempt to coalesce with the region after us.
    234 	 */
    235 	if ((after->er_link.le_next != NULL) &&
    236 	    ((start + size) == after->er_link.le_next->er_start)) {
    237 		/*
    238 		 * We can coalesce.  Note that if we appended ourselves
    239 		 * to the previous region, we exactly fit the gap, and
    240 		 * can free the "next" region descriptor.
    241 		 */
    242 		if (appended) {
    243 			/*
    244 			 * Yup, we can free it up.
    245 			 */
    246 			after->er_end = after->er_link.le_next->er_end;
    247 			LIST_REMOVE(after->er_link.le_next, er_link);
    248 			extent_free_region_descriptor(ex,
    249 			    after->er_link.le_next);
    250 		} else {
    251 			/*
    252 			 * Nope, just prepend us to the next region.
    253 			 */
    254 			after->er_link.le_next->er_start = start;
    255 		}
    256 		return (0);
    257 	}
    258 
    259 	/*
    260 	 * We weren't able to coalesce with the next region, but
    261 	 * we don't need to allocate a region descriptor if we
    262 	 * appended ourselves to the previous region.
    263 	 */
    264 	if (appended)
    265 		return (0);
    266 
    267  allocate_region_descriptor:
    268 
    269 	/*
    270 	 * Allocate a region descriptor and insert ourselves
    271 	 * into the region list.
    272 	 */
    273 	rp = extent_alloc_region_descriptor(ex, flags);
    274 	if (rp == NULL) {
    275 #if defined(DEBUG) || defined(DIAGNOSTIC)
    276 		printf("extent `%s': can't allocate region descriptor.\n",
    277 		    ex->ex_name);
    278 #endif
    279 		return (ENOMEM);
    280 	}
    281 	rp->er_start = start;
    282 	rp->er_end = start + (size - 1);
    283 	LIST_INSERT_AFTER(after, rp, er_link);
    284 	return (0);
    285 }
    286 
    287 /*
    288  * Allocate a specific region in an extent map.
    289  */
    290 int
    291 extent_alloc_region(ex, start, size, flags)
    292 	struct extent *ex;
    293 	u_long start, size;
    294 	int flags;
    295 {
    296 	struct extent_region *rp, *last;
    297 	u_long end = start + (size - 1);
    298 	int error;
    299 
    300 	/* Check arguments. */
    301 	if (ex == NULL)
    302 		panic("extent_alloc_region: NULL extent");
    303 	if (size < 1) {
    304 		printf("extent_alloc_region: extent `%s', size 0x%lx\n",
    305 		    ex->ex_name, size);
    306 		panic("extent_alloc_region: bad size");
    307 	}
    308 	if (end < start) {
    309 		printf(
    310 		 "extent_alloc_region: extent `%s', start 0x%lx, size 0x%lx\n",
    311 		 ex->ex_name, start, size);
    312 		panic("extent_alloc_region: overflow");
    313 	}
    314 	/*
    315 	 * Make sure the requested region lies within the
    316 	 * extent.
    317 	 */
    318 	if ((start < ex->ex_start) || (end > ex->ex_end)) {
    319 		printf("extent_alloc_region: extent `%s' (0x%lx - 0x%lx)\n",
    320 		    ex->ex_name, ex->ex_start, ex->ex_end);
    321 		printf("extent_alloc_region: start 0x%lx, end 0x%lx\n",
    322 		    start, end);
    323 		panic("extent_alloc_region: region lies outside extent");
    324 	}
    325 
    326  alloc_start:
    327 	/*
    328 	 * Attempt to place ourselves in the desired area of the
    329 	 * extent.  We save ourselves some work by keeping the list sorted.
    330 	 * In other words, if the start of the current region is greater
    331 	 * than the end of our region, we don't have to search any further.
    332 	 */
    333 
    334 	/*
    335 	 * Keep a pointer to the last region we looked at so
    336 	 * that we don't have to traverse the list again when
    337 	 * we insert ourselves.  If "last" is NULL when we
    338 	 * finally insert ourselves, we go at the head of the
    339 	 * list.  See extent_insert_and_optimize() for details.
    340 	 */
    341 	last = NULL;
    342 
    343 	for (rp = ex->ex_regions.lh_first; rp != NULL;
    344 	    rp = rp->er_link.le_next) {
    345 		if (rp->er_start > end) {
    346 			/*
    347 			 * We lie before this region and don't
    348 			 * conflict.
    349 			 */
    350 			 break;
    351 		}
    352 
    353 		/*
    354 		 * The current region begins before we end.
    355 		 * Check for a conflict.
    356 		 */
    357 		if (rp->er_end >= start) {
    358 			/*
    359 			 * We conflict.  If we can wait, do so.
    360 			 */
    361 			if (flags & EX_WAITOK) {
    362 				ex->ex_flags |= EXF_WANTED;
    363 				error = tsleep(ex,
    364 				    PRIBIO | ((flags & EX_CATCH) ? PCATCH : 0),
    365 				    "extnt", 0);
    366 				if (error)
    367 					return (error);
    368 				goto alloc_start;
    369 			}
    370 			return (EAGAIN);
    371 		}
    372 
    373 		/*
    374 		 * We don't conflict, but this region lies before
    375 		 * us.  Keep a pointer to this region, and keep
    376 		 * trying.
    377 		 */
    378 		last = rp;
    379 	}
    380 
    381 	/*
    382 	 * We don't conflict with any regions.  "last" points
    383 	 * to the region we fall after, or is NULL if we belong
    384 	 * at the beginning of the region list.  Insert ourselves.
    385 	 */
    386 	return (extent_insert_and_optimize(ex, start, size, flags, last));
    387 }
    388 
    389 /*
    390  * Macro to check (x + y) <= z.  This check is designed to fail
    391  * if an overflow occurs.
    392  */
    393 #define LE_OV(x, y, z)	((((x) + (y)) >= (x)) && (((x) + (y)) <= (z)))
    394 
    395 /*
    396  * Allocate a region in an extent map subregion.
    397  *
    398  * If EX_FAST is specified, we return the first fit in the map.
    399  * Otherwise, we try to minimize fragmentation by finding the
    400  * smallest gap that will hold the request.
    401  *
    402  * The allocated region is aligned to "alignment", which must be
    403  * a power of 2.
    404  */
    405 int
    406 extent_alloc_subregion(ex, substart, subend, size, alignment, boundary,
    407     flags, result)
    408 	struct extent *ex;
    409 	u_long substart, subend, size, alignment, boundary;
    410 	int flags;
    411 	u_long *result;
    412 {
    413 	struct extent_region *rp, *last, *bestlast;
    414 	u_long newstart, newend, beststart, bestovh, ovh;
    415 	u_long dontcross, odontcross;
    416 	int error;
    417 
    418 	/* Check arguments. */
    419 	if (ex == NULL)
    420 		panic("extent_alloc_subregion: NULL extent");
    421 	if (result == NULL)
    422 		panic("extent_alloc_subregion: NULL result pointer");
    423 	if ((substart < ex->ex_start) || (substart >= ex->ex_end) ||
    424 	    (subend > ex->ex_end) || (subend <= ex->ex_start)) {
    425   printf("extent_alloc_subregion: extent `%s', ex_start 0x%lx, ex_end 0x%lx\n",
    426 		    ex->ex_name, ex->ex_start, ex->ex_end);
    427 		printf("extent_alloc_subregion: substart 0x%lx, subend 0x%lx\n",
    428 		    substart, subend);
    429 		panic("extent_alloc_subregion: bad subregion");
    430 	}
    431 	if ((size < 1) || (size > ((subend - substart) + 1))) {
    432 		printf("extent_alloc_subregion: extent `%s', size 0x%lx\n",
    433 		    ex->ex_name, size);
    434 		panic("extent_alloc_subregion: bad size");
    435 	}
    436 	if (alignment == 0)
    437 		panic("extent_alloc_subregion: bad alignment");
    438 	if (boundary && (boundary < size)) {
    439 		printf(
    440 		    "extent_alloc_subregion: extent `%s', size 0x%lx,
    441 		    boundary 0x%lx\n", ex->ex_name, size, boundary);
    442 		panic("extent_alloc_subregion: bad boundary");
    443 	}
    444 
    445  alloc_start:
    446 	/*
    447 	 * Keep a pointer to the last region we looked at so
    448 	 * that we don't have to traverse the list again when
    449 	 * we insert ourselves.  If "last" is NULL when we
    450 	 * finally insert ourselves, we go at the head of the
    451 	 * list.  See extent_insert_and_optimize() for deatails.
    452 	 */
    453 	last = NULL;
    454 
    455 	/*
    456 	 * Initialize the "don't cross" boundary, a.k.a a line
    457 	 * that a region should not cross.  If the boundary lies
    458 	 * before the region starts, we add the "boundary" argument
    459 	 * until we get a meaningful comparison.
    460 	 */
    461 	if (boundary) {
    462 		dontcross = ex->ex_start + boundary;
    463 		while (dontcross < substart)
    464 			dontcross += boundary;
    465 	}
    466 
    467 	/*
    468 	 * Keep track of size and location of the smallest
    469 	 * chunk we fit in.
    470 	 *
    471 	 * Since the extent can be as large as the numeric range
    472 	 * of the CPU (0 - 0xffffffff for 32-bit systems), the
    473 	 * best overhead value can be the maximum unsigned integer.
    474 	 * Thus, we initialize "bestovh" to 0, since we insert ourselves
    475 	 * into the region list immediately on an exact match (which
    476 	 * is the only case where "bestovh" would be set to 0).
    477 	 */
    478 	bestovh = 0;
    479 	beststart = 0;
    480 	bestlast = NULL;
    481 
    482 	/*
    483 	 * For N allocated regions, we must make (N + 1)
    484 	 * checks for unallocated space.  The first chunk we
    485 	 * check is the area from the beginning of the subregion
    486 	 * to the first allocated region.
    487 	 */
    488 	newstart = EXTENT_ALIGN(substart, alignment);
    489 	if (newstart < ex->ex_start) {
    490 		printf(
    491       "extent_alloc_subregion: extent `%s' (0x%lx - 0x%lx), alignment 0x%lx\n",
    492 		 ex->ex_name, ex->ex_start, ex->ex_end, alignment);
    493 		panic("extent_alloc_subregion: overflow after alignment");
    494 	}
    495 
    496 	for (rp = ex->ex_regions.lh_first; rp != NULL;
    497 	    rp = rp->er_link.le_next) {
    498 		/*
    499 		 * Check the chunk before "rp".  Note that our
    500 		 * comparison is safe from overflow conditions.
    501 		 */
    502 		if (LE_OV(newstart, size, rp->er_start)) {
    503 			/*
    504 			 * Do a boundary check, if necessary.  Note
    505 			 * that a region may *begin* on the boundary,
    506 			 * but it must end before the boundary.
    507 			 */
    508 			if (boundary) {
    509 				newend = newstart + (size - 1);
    510 
    511 				/*
    512 				 * Adjust boundary for a meaningful
    513 				 * comparison.
    514 				 */
    515 				while (dontcross <= newstart) {
    516 					odontcross = dontcross;
    517 					dontcross += boundary;
    518 
    519 					/*
    520 					 * If we run past the end of
    521 					 * the extent or the boundary
    522 					 * overflows, then the request
    523 					 * can't fit.
    524 					 */
    525 					if ((dontcross > ex->ex_end) ||
    526 					    (dontcross < odontcross))
    527 						goto fail;
    528 				}
    529 
    530 				/* Do the boundary check. */
    531 				if (newend >= dontcross) {
    532 					/*
    533 					 * Candidate region crosses
    534 					 * boundary.  Try again.
    535 					 */
    536 					continue;
    537 				}
    538 			}
    539 
    540 			/*
    541 			 * We would fit into this space.  Calculate
    542 			 * the overhead (wasted space).  If we exactly
    543 			 * fit, or we're taking the first fit, insert
    544 			 * ourselves into the region list.
    545 			 */
    546 			ovh = rp->er_start - newstart - size;
    547 			if ((flags & EX_FAST) || (ovh == 0))
    548 				goto found;
    549 
    550 			/*
    551 			 * Don't exactly fit, but check to see
    552 			 * if we're better than any current choice.
    553 			 */
    554 			if ((bestovh == 0) || (ovh < bestovh)) {
    555 				bestovh = ovh;
    556 				beststart = newstart;
    557 				bestlast = last;
    558 			}
    559 		}
    560 
    561 		/*
    562 		 * Skip past the current region and check again.
    563 		 */
    564 		newstart = EXTENT_ALIGN((rp->er_end + 1), alignment);
    565 		if (newstart < rp->er_end) {
    566 			/*
    567 			 * Overflow condition.  Don't error out, since
    568 			 * we might have a chunk of space that we can
    569 			 * use.
    570 			 */
    571 			goto fail;
    572 		}
    573 
    574 		last = rp;
    575 	}
    576 
    577 	/*
    578 	 * The final check is from the current starting point to the
    579 	 * end of the subregion.  If there were no allocated regions,
    580 	 * "newstart" is set to the beginning of the subregion, or
    581 	 * just past the end of the last allocated region, adjusted
    582 	 * for alignment in either case.
    583 	 */
    584 	if (LE_OV(newstart, (size - 1), subend)) {
    585 		/*
    586 		 * We would fit into this space.  Calculate
    587 		 * the overhead (wasted space).  If we exactly
    588 		 * fit, or we're taking the first fit, insert
    589 		 * ourselves into the region list.
    590 		 */
    591 		ovh = ex->ex_end - newstart - (size - 1);
    592 		if ((flags & EX_FAST) || (ovh == 0))
    593 			goto found;
    594 
    595 		/*
    596 		 * Don't exactly fit, but check to see
    597 		 * if we're better than any current choice.
    598 		 */
    599 		if ((bestovh == 0) || (ovh < bestovh)) {
    600 			bestovh = ovh;
    601 			beststart = newstart;
    602 			bestlast = last;
    603 		}
    604 	}
    605 
    606  fail:
    607 	/*
    608 	 * One of the following two conditions have
    609 	 * occurred:
    610 	 *
    611 	 *	There is no chunk large enough to hold the request.
    612 	 *
    613 	 *	If EX_FAST was not specified, there is not an
    614 	 *	exact match for the request.
    615 	 *
    616 	 * Note that if we reach this point and EX_FAST is
    617 	 * set, then we know there is no space in the extent for
    618 	 * the request.
    619 	 */
    620 	if (((flags & EX_FAST) == 0) && (bestovh != 0)) {
    621 		/*
    622 		 * We have a match that's "good enough".
    623 		 */
    624 		newstart = beststart;
    625 		last = bestlast;
    626 		goto found;
    627 	}
    628 
    629 	/*
    630 	 * No space currently available.  Wait for it to free up,
    631 	 * if possible.
    632 	 */
    633 	if (flags & EX_WAITOK) {
    634 		ex->ex_flags |= EXF_WANTED;
    635 		error = tsleep(ex,
    636 		    PRIBIO | ((flags & EX_CATCH) ? PCATCH : 0), "extnt", 0);
    637 		if (error)
    638 			return (error);
    639 		goto alloc_start;
    640 	}
    641 
    642 	return (EAGAIN);
    643 
    644  found:
    645 	/*
    646 	 * Insert ourselves into the region list.
    647 	 */
    648 	error = extent_insert_and_optimize(ex, newstart, size, flags, last);
    649 	if (error == 0)
    650 		*result = newstart;
    651 	return (error);
    652 }
    653 
    654 int
    655 extent_free(ex, start, size, flags)
    656 	struct extent *ex;
    657 	u_long start, size;
    658 	int flags;
    659 {
    660 	struct extent_region *rp;
    661 	u_long end = start + (size - 1);
    662 
    663 	/* Check arguments. */
    664 	if (ex == NULL)
    665 		panic("extent_free: NULL extent");
    666 	if ((start < ex->ex_start) || (start > ex->ex_end)) {
    667 		extent_print(ex);
    668 		printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
    669 		    ex->ex_name, start, size);
    670 		panic("extent_free: extent `%s', region not within extent",
    671 		    ex->ex_name);
    672 	}
    673 	/* Check for an overflow. */
    674 	if (end < start) {
    675 		extent_print(ex);
    676 		printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
    677 		    ex->ex_name, start, size);
    678 		panic("extent_free: overflow");
    679 	}
    680 
    681 	/*
    682 	 * Find region and deallocate.  Several possibilities:
    683 	 *
    684 	 *	1. (start == er_start) && (end == er_end):
    685 	 *	   Free descriptor.
    686 	 *
    687 	 *	2. (start == er_start) && (end < er_end):
    688 	 *	   Adjust er_start.
    689 	 *
    690 	 *	3. (start > er_start) && (end == er_end):
    691 	 *	   Adjust er_end.
    692 	 *
    693 	 *	4. (start > er_start) && (end < er_end):
    694 	 *	   Fragment region.  Requires descriptor alloc.
    695 	 *
    696 	 * Cases 2, 3, and 4 require that the EXF_NOBLOB flag
    697 	 * is not set.
    698 	 */
    699 	for (rp = ex->ex_regions.lh_first; rp != NULL;
    700 	    rp = rp->er_link.le_next) {
    701 		/*
    702 		 * Save ourselves some comparisons; does the current
    703 		 * region end before chunk to be freed begins?  If so,
    704 		 * then we haven't found the appropriate region descriptor.
    705 		 */
    706 		if (rp->er_end < start)
    707 			continue;
    708 
    709 		/*
    710 		 * Save ourselves some traversal; does the current
    711 		 * region begin after the chunk to be freed ends?  If so,
    712 		 * then we've already passed any possible region descriptors
    713 		 * that might have contained the chunk to be freed.
    714 		 */
    715 		if (rp->er_start > end)
    716 			break;
    717 
    718 		/* Case 1. */
    719 		if ((start == rp->er_start) && (end == rp->er_end)) {
    720 			LIST_REMOVE(rp, er_link);
    721 			extent_free_region_descriptor(ex, rp);
    722 			goto done;
    723 		}
    724 
    725 		/*
    726 		 * The following cases all require that EXF_NOBLOB
    727 		 * is not set.
    728 		 */
    729 		if (ex->ex_flags & EXF_NOBLOB)
    730 			continue;
    731 
    732 		/* Case 2. */
    733 		if ((start == rp->er_start) && (end < rp->er_end)) {
    734 			rp->er_start = (end + 1);
    735 			goto done;
    736 		}
    737 
    738 		/* Case 3. */
    739 		if ((start > rp->er_start) && (end == rp->er_end)) {
    740 			rp->er_end = (start - 1);
    741 			goto done;
    742 		}
    743 
    744 		/* Case 4. */
    745 		if ((start > rp->er_start) && (end < rp->er_end)) {
    746 			struct extent_region *nrp;
    747 
    748 			/* Allocate a region descriptor. */
    749 			nrp = extent_alloc_region_descriptor(ex, flags);
    750 			if (nrp == NULL)
    751 				return (ENOMEM);
    752 
    753 			/* Fill in new descriptor. */
    754 			nrp->er_start = end + 1;
    755 			nrp->er_end = rp->er_end;
    756 
    757 			/* Adjust current descriptor. */
    758 			rp->er_end = start - 1;
    759 
    760 			/* Instert new descriptor after current. */
    761 			LIST_INSERT_AFTER(rp, nrp, er_link);
    762 			goto done;
    763 		}
    764 	}
    765 
    766 	/* Region not found, or request otherwise invalid. */
    767 	extent_print(ex);
    768 	printf("extent_free: start 0x%lx, end 0x%lx\n", start, end);
    769 	panic("extent_free: region not found");
    770 
    771  done:
    772 	if (ex->ex_flags & EXF_WANTED) {
    773 		ex->ex_flags &= ~EXF_WANTED;
    774 		wakeup(ex);
    775 	}
    776 	return (0);
    777 }
    778 
    779 static struct extent_region *
    780 extent_alloc_region_descriptor(ex, flags)
    781 	struct extent *ex;
    782 	int flags;
    783 {
    784 	struct extent_region *rp;
    785 	int s;
    786 
    787 	if (ex->ex_flags & EXF_FIXED) {
    788 		struct extent_fixed *fex = (struct extent_fixed *)ex;
    789 
    790 		while (fex->fex_freelist.lh_first == NULL) {
    791 			if (flags & EX_MALLOCOK)
    792 				goto alloc;
    793 
    794 			if ((flags & EX_WAITOK) == 0)
    795 				return (NULL);
    796 			ex->ex_flags |= EXF_FLWANTED;
    797 			if (tsleep(&fex->fex_freelist,
    798 			    PRIBIO | ((flags & EX_CATCH) ? PCATCH : 0),
    799 			    "extnt", 0))
    800 				return (NULL);
    801 		}
    802 		/* Atomic. */
    803 		s = splhigh();
    804 		rp = fex->fex_freelist.lh_first;
    805 		LIST_REMOVE(rp, er_link);
    806 		splx(s);
    807 
    808 		rp->er_flags = 0;
    809 
    810 		return (rp);
    811 	}
    812 
    813  alloc:
    814 	rp = (struct extent_region *)
    815 	    malloc(sizeof(struct extent_region), ex->ex_mtype,
    816 	    (flags & EX_WAITOK) ? M_WAITOK : M_NOWAIT);
    817 
    818 	rp->er_flags = ER_ALLOC;
    819 
    820 	return (rp);
    821 }
    822 
    823 static void
    824 extent_free_region_descriptor(ex, rp)
    825 	struct extent *ex;
    826 	struct extent_region *rp;
    827 {
    828 
    829 	if ((rp->er_flags & ER_ALLOC) == 0) {
    830 		struct extent_fixed *fex = (struct extent_fixed *)ex;
    831 
    832 		LIST_INSERT_HEAD(&fex->fex_freelist, rp, er_link);
    833 		if (ex->ex_flags & EXF_FLWANTED) {
    834 			ex->ex_flags &= ~EXF_FLWANTED;
    835 			wakeup(&fex->fex_freelist);
    836 		}
    837 	} else
    838 		free(rp, ex->ex_mtype);
    839 }
    840 
    841 void
    842 extent_print(ex)
    843 	struct extent *ex;
    844 {
    845 	struct extent_region *rp;
    846 
    847 	if (ex == NULL)
    848 		panic("extent_print: NULL extent");
    849 
    850 	printf("extent `%s' (0x%lx - 0x%lx), flags = 0x%x\n", ex->ex_name,
    851 	    ex->ex_start, ex->ex_end, ex->ex_flags);
    852 
    853 	for (rp = ex->ex_regions.lh_first; rp != NULL;
    854 	    rp = rp->er_link.le_next)
    855 		printf("     0x%lx - 0x%lx\n", rp->er_start, rp->er_end);
    856 }
    857