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