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