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