2006-10-12 05:30:41 +04:00
|
|
|
/* $NetBSD: subr_extent.c,v 1.60 2006/10/12 01:32:18 christos Exp $ */
|
1996-06-23 04:10:08 +04:00
|
|
|
|
|
|
|
/*-
|
1998-01-22 01:33:49 +03:00
|
|
|
* Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
|
1996-06-23 04:10:08 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Jason R. Thorpe and Matthias Drochner.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1998-09-12 21:20:02 +04:00
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
1996-06-23 04:10:08 +04:00
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
1997-10-09 11:37:50 +04:00
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
1996-06-23 04:10:08 +04:00
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* General purpose extent manager.
|
|
|
|
*/
|
|
|
|
|
2001-11-12 18:25:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2006-10-12 05:30:41 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: subr_extent.c,v 1.60 2006/10/12 01:32:18 christos Exp $");
|
2001-11-12 18:25:01 +03:00
|
|
|
|
1996-11-21 21:46:34 +03:00
|
|
|
#ifdef _KERNEL
|
2003-06-23 15:00:59 +04:00
|
|
|
#include "opt_lockdebug.h"
|
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/extent.h>
|
|
|
|
#include <sys/malloc.h>
|
1998-07-24 00:57:17 +04:00
|
|
|
#include <sys/pool.h>
|
1996-06-23 04:10:08 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/proc.h>
|
1998-06-06 06:25:46 +04:00
|
|
|
#include <sys/lock.h>
|
1999-10-12 02:57:17 +04:00
|
|
|
|
2000-06-26 18:20:25 +04:00
|
|
|
#include <uvm/uvm_extern.h>
|
1999-10-12 02:57:17 +04:00
|
|
|
|
|
|
|
#define KMEM_IS_RUNNING (kmem_map != NULL)
|
1998-09-01 21:59:36 +04:00
|
|
|
#elif defined(_EXTENT_TESTING)
|
1996-11-21 21:46:34 +03:00
|
|
|
/*
|
|
|
|
* user-land definitions, so it can fit into a testing harness.
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
1998-09-01 21:59:36 +04:00
|
|
|
#include <sys/pool.h>
|
1996-11-21 21:46:34 +03:00
|
|
|
#include <sys/extent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
1999-06-27 10:18:33 +04:00
|
|
|
#include <string.h>
|
1996-11-21 21:46:34 +03:00
|
|
|
|
2000-05-24 06:22:36 +04:00
|
|
|
/*
|
|
|
|
* Use multi-line #defines to avoid screwing up the kernel tags file;
|
|
|
|
* without this, ctags produces a tags file where panic() shows up
|
|
|
|
* in subr_extent.c rather than subr_prf.c.
|
|
|
|
*/
|
|
|
|
#define \
|
|
|
|
malloc(s, t, flags) malloc(s)
|
|
|
|
#define \
|
|
|
|
free(p, t) free(p)
|
|
|
|
#define \
|
|
|
|
tsleep(chan, pri, str, timo) (EWOULDBLOCK)
|
|
|
|
#define \
|
2000-08-13 03:56:50 +04:00
|
|
|
ltsleep(chan,pri,str,timo,lck) (EWOULDBLOCK)
|
|
|
|
#define \
|
2000-05-24 06:22:36 +04:00
|
|
|
wakeup(chan) ((void)0)
|
|
|
|
#define \
|
2001-05-10 03:38:20 +04:00
|
|
|
pool_get(pool, flags) malloc((pool)->pr_size,0,0)
|
2000-05-24 06:22:36 +04:00
|
|
|
#define \
|
|
|
|
pool_put(pool, rp) free(rp,0)
|
|
|
|
#define \
|
|
|
|
panic(a) printf(a)
|
|
|
|
#define \
|
|
|
|
splhigh() (1)
|
|
|
|
#define \
|
|
|
|
splx(s) ((void)(s))
|
1999-10-12 02:57:17 +04:00
|
|
|
|
2000-06-06 21:32:50 +04:00
|
|
|
#define \
|
|
|
|
simple_lock_init(l) ((void)(l))
|
|
|
|
#define \
|
|
|
|
simple_lock(l) ((void)(l))
|
|
|
|
#define \
|
|
|
|
simple_unlock(l) ((void)(l))
|
1999-10-12 02:57:17 +04:00
|
|
|
#define KMEM_IS_RUNNING (1)
|
1996-11-21 21:46:34 +03:00
|
|
|
#endif
|
1996-06-23 04:10:08 +04:00
|
|
|
|
2001-05-10 03:38:20 +04:00
|
|
|
static struct pool expool;
|
|
|
|
static struct simplelock expool_init_slock = SIMPLELOCK_INITIALIZER;
|
|
|
|
static int expool_initialized;
|
1998-07-24 00:57:17 +04:00
|
|
|
|
1996-10-17 12:27:35 +04:00
|
|
|
/*
|
|
|
|
* Macro to align to an arbitrary power-of-two boundary.
|
|
|
|
*/
|
1998-09-11 00:52:13 +04:00
|
|
|
#define EXTENT_ALIGN(_start, _align, _skew) \
|
|
|
|
(((((_start) - (_skew)) + ((_align) - 1)) & (-(_align))) + (_skew))
|
1996-10-17 12:27:35 +04:00
|
|
|
|
1998-07-24 10:40:45 +04:00
|
|
|
/*
|
|
|
|
* Create the extent_region pool.
|
|
|
|
* (This is deferred until one of our callers thinks we can malloc()).
|
|
|
|
*/
|
|
|
|
|
2005-12-24 22:12:23 +03:00
|
|
|
static inline void
|
2001-05-10 03:38:20 +04:00
|
|
|
expool_init(void)
|
1998-07-24 10:40:45 +04:00
|
|
|
{
|
2001-05-10 03:38:20 +04:00
|
|
|
|
|
|
|
simple_lock(&expool_init_slock);
|
|
|
|
if (expool_initialized) {
|
|
|
|
simple_unlock(&expool_init_slock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1998-09-01 21:59:36 +04:00
|
|
|
#if defined(_KERNEL)
|
2001-05-10 03:38:20 +04:00
|
|
|
pool_init(&expool, sizeof(struct extent_region), 0, 0, 0,
|
2002-03-08 23:48:27 +03:00
|
|
|
"extent", NULL);
|
1998-09-01 21:59:36 +04:00
|
|
|
#else
|
2001-05-10 03:38:20 +04:00
|
|
|
expool.pr_size = sizeof(struct extent_region);
|
1998-09-01 21:59:36 +04:00
|
|
|
#endif
|
2001-05-10 03:38:20 +04:00
|
|
|
|
|
|
|
expool_initialized = 1;
|
|
|
|
simple_unlock(&expool_init_slock);
|
1998-07-24 10:40:45 +04:00
|
|
|
}
|
|
|
|
|
2005-06-23 22:46:17 +04:00
|
|
|
/*
|
|
|
|
* Allocate an extent region descriptor. EXTENT MUST NOT BE LOCKED,
|
|
|
|
* AS THIS FUNCTION MAY BLOCK! We will handle any locking we may need.
|
|
|
|
*/
|
|
|
|
static struct extent_region *
|
|
|
|
extent_alloc_region_descriptor(struct extent *ex, int flags)
|
|
|
|
{
|
|
|
|
struct extent_region *rp;
|
|
|
|
int exflags;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the kernel memory allocator is not yet running, we can't
|
|
|
|
* use it (obviously).
|
|
|
|
*/
|
|
|
|
if (KMEM_IS_RUNNING == 0)
|
|
|
|
flags &= ~EX_MALLOCOK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX Make a static, create-time flags word, so we don't
|
|
|
|
* XXX have to lock to read it!
|
|
|
|
*/
|
|
|
|
simple_lock(&ex->ex_slock);
|
|
|
|
exflags = ex->ex_flags;
|
|
|
|
simple_unlock(&ex->ex_slock);
|
|
|
|
|
|
|
|
if (exflags & EXF_FIXED) {
|
|
|
|
struct extent_fixed *fex = (struct extent_fixed *)ex;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
simple_lock(&ex->ex_slock);
|
|
|
|
if ((rp = LIST_FIRST(&fex->fex_freelist)) != NULL) {
|
|
|
|
/*
|
|
|
|
* Don't muck with flags after pulling it off
|
|
|
|
* the freelist; it may have been dynamically
|
|
|
|
* allocated, and kindly given to us. We
|
|
|
|
* need to remember that information.
|
|
|
|
*/
|
|
|
|
LIST_REMOVE(rp, er_link);
|
|
|
|
simple_unlock(&ex->ex_slock);
|
|
|
|
return (rp);
|
|
|
|
}
|
|
|
|
if (flags & EX_MALLOCOK) {
|
|
|
|
simple_unlock(&ex->ex_slock);
|
|
|
|
goto alloc;
|
|
|
|
}
|
|
|
|
if ((flags & EX_WAITOK) == 0) {
|
|
|
|
simple_unlock(&ex->ex_slock);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
ex->ex_flags |= EXF_FLWANTED;
|
|
|
|
if (ltsleep(&fex->fex_freelist,
|
|
|
|
PNORELOCK| PRIBIO | ((flags & EX_CATCH) ? PCATCH : 0),
|
|
|
|
"extnt", 0, &ex->ex_slock))
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
alloc:
|
|
|
|
s = splhigh();
|
|
|
|
if (expool_initialized == 0)
|
|
|
|
expool_init();
|
|
|
|
rp = pool_get(&expool, (flags & EX_WAITOK) ? PR_WAITOK : 0);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
if (rp != NULL)
|
|
|
|
rp->er_flags = ER_ALLOC;
|
|
|
|
|
|
|
|
return (rp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free an extent region descriptor. EXTENT _MUST_ BE LOCKED! This
|
|
|
|
* is safe as we do not block here.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
extent_free_region_descriptor(struct extent *ex, struct extent_region *rp)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if (ex->ex_flags & EXF_FIXED) {
|
|
|
|
struct extent_fixed *fex = (struct extent_fixed *)ex;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If someone's waiting for a region descriptor,
|
|
|
|
* be nice and give them this one, rather than
|
|
|
|
* just free'ing it back to the system.
|
|
|
|
*/
|
|
|
|
if (rp->er_flags & ER_ALLOC) {
|
|
|
|
if (ex->ex_flags & EXF_FLWANTED) {
|
|
|
|
/* Clear all but ER_ALLOC flag. */
|
|
|
|
rp->er_flags = ER_ALLOC;
|
|
|
|
LIST_INSERT_HEAD(&fex->fex_freelist, rp,
|
|
|
|
er_link);
|
|
|
|
goto wake_em_up;
|
|
|
|
} else {
|
|
|
|
s = splhigh();
|
|
|
|
pool_put(&expool, rp);
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Clear all flags. */
|
|
|
|
rp->er_flags = 0;
|
|
|
|
LIST_INSERT_HEAD(&fex->fex_freelist, rp, er_link);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ex->ex_flags & EXF_FLWANTED) {
|
|
|
|
wake_em_up:
|
|
|
|
ex->ex_flags &= ~EXF_FLWANTED;
|
|
|
|
wakeup(&fex->fex_freelist);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We know it's dynamically allocated if we get here.
|
|
|
|
*/
|
|
|
|
s = splhigh();
|
|
|
|
pool_put(&expool, rp);
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* Allocate and initialize an extent map.
|
|
|
|
*/
|
|
|
|
struct extent *
|
2005-06-23 22:46:17 +04:00
|
|
|
extent_create(const char *name, u_long start, u_long end,
|
|
|
|
struct malloc_type *mtype, caddr_t storage, size_t storagesize, int flags)
|
1996-06-23 04:10:08 +04:00
|
|
|
{
|
|
|
|
struct extent *ex;
|
|
|
|
caddr_t cp = storage;
|
|
|
|
size_t sz = storagesize;
|
|
|
|
struct extent_region *rp;
|
|
|
|
int fixed_extent = (storage != NULL);
|
1999-06-07 02:14:53 +04:00
|
|
|
int s;
|
1996-06-23 04:10:08 +04:00
|
|
|
|
1996-10-17 12:27:35 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
1996-06-23 04:10:08 +04:00
|
|
|
/* Check arguments. */
|
|
|
|
if (name == NULL)
|
|
|
|
panic("extent_create: name == NULL");
|
|
|
|
if (end < start) {
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("extent_create: extent `%s', start 0x%lx, end 0x%lx\n",
|
1996-06-23 04:10:08 +04:00
|
|
|
name, start, end);
|
|
|
|
panic("extent_create: end < start");
|
|
|
|
}
|
|
|
|
if (fixed_extent && (storagesize < sizeof(struct extent_fixed)))
|
1999-02-12 03:46:11 +03:00
|
|
|
panic("extent_create: fixed extent, bad storagesize 0x%lx",
|
|
|
|
(u_long)storagesize);
|
1996-10-17 12:27:35 +04:00
|
|
|
if (fixed_extent == 0 && (storagesize != 0 || storage != NULL))
|
|
|
|
panic("extent_create: storage provided for non-fixed");
|
|
|
|
#endif
|
1996-06-23 04:10:08 +04:00
|
|
|
|
|
|
|
/* Allocate extent descriptor. */
|
|
|
|
if (fixed_extent) {
|
|
|
|
struct extent_fixed *fex;
|
|
|
|
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
memset(storage, 0, storagesize);
|
1996-06-23 04:10:08 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Align all descriptors on "long" boundaries.
|
|
|
|
*/
|
|
|
|
fex = (struct extent_fixed *)cp;
|
|
|
|
ex = (struct extent *)fex;
|
1996-10-17 12:27:35 +04:00
|
|
|
cp += ALIGN(sizeof(struct extent_fixed));
|
|
|
|
sz -= ALIGN(sizeof(struct extent_fixed));
|
1996-06-23 04:10:08 +04:00
|
|
|
fex->fex_storage = storage;
|
|
|
|
fex->fex_storagesize = storagesize;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In a fixed extent, we have to pre-allocate region
|
|
|
|
* descriptors and place them in the extent's freelist.
|
|
|
|
*/
|
|
|
|
LIST_INIT(&fex->fex_freelist);
|
1996-10-17 12:27:35 +04:00
|
|
|
while (sz >= ALIGN(sizeof(struct extent_region))) {
|
1996-06-23 04:10:08 +04:00
|
|
|
rp = (struct extent_region *)cp;
|
1996-10-17 12:27:35 +04:00
|
|
|
cp += ALIGN(sizeof(struct extent_region));
|
|
|
|
sz -= ALIGN(sizeof(struct extent_region));
|
1996-06-23 04:10:08 +04:00
|
|
|
LIST_INSERT_HEAD(&fex->fex_freelist, rp, er_link);
|
|
|
|
}
|
|
|
|
} else {
|
1999-06-07 02:14:53 +04:00
|
|
|
s = splhigh();
|
2001-05-10 03:38:20 +04:00
|
|
|
if (expool_initialized == 0)
|
|
|
|
expool_init();
|
1999-06-07 02:14:53 +04:00
|
|
|
splx(s);
|
1998-07-24 10:40:45 +04:00
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
ex = (struct extent *)malloc(sizeof(struct extent),
|
|
|
|
mtype, (flags & EX_WAITOK) ? M_WAITOK : M_NOWAIT);
|
|
|
|
if (ex == NULL)
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in the extent descriptor and return it to the caller. */
|
1998-06-06 06:25:46 +04:00
|
|
|
simple_lock_init(&ex->ex_slock);
|
1996-06-23 04:10:08 +04:00
|
|
|
LIST_INIT(&ex->ex_regions);
|
|
|
|
ex->ex_name = name;
|
|
|
|
ex->ex_start = start;
|
|
|
|
ex->ex_end = end;
|
|
|
|
ex->ex_mtype = mtype;
|
|
|
|
ex->ex_flags = 0;
|
|
|
|
if (fixed_extent)
|
|
|
|
ex->ex_flags |= EXF_FIXED;
|
1996-10-17 12:27:35 +04:00
|
|
|
if (flags & EX_NOCOALESCE)
|
|
|
|
ex->ex_flags |= EXF_NOCOALESCE;
|
1996-06-23 04:10:08 +04:00
|
|
|
return (ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy an extent map.
|
1999-01-22 10:57:59 +03:00
|
|
|
* Since we're freeing the data, there can't be any references
|
|
|
|
* so we don't need any locking.
|
1996-06-23 04:10:08 +04:00
|
|
|
*/
|
|
|
|
void
|
2005-06-23 22:46:17 +04:00
|
|
|
extent_destroy(struct extent *ex)
|
1996-06-23 04:10:08 +04:00
|
|
|
{
|
|
|
|
struct extent_region *rp, *orp;
|
|
|
|
|
1996-10-17 12:27:35 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
1996-06-23 04:10:08 +04:00
|
|
|
/* Check arguments. */
|
|
|
|
if (ex == NULL)
|
|
|
|
panic("extent_destroy: NULL extent");
|
1996-10-17 12:27:35 +04:00
|
|
|
#endif
|
1996-06-23 04:10:08 +04:00
|
|
|
|
|
|
|
/* Free all region descriptors in extent. */
|
2002-09-04 05:32:31 +04:00
|
|
|
for (rp = LIST_FIRST(&ex->ex_regions); rp != NULL; ) {
|
1996-06-23 04:10:08 +04:00
|
|
|
orp = rp;
|
2002-09-04 05:32:31 +04:00
|
|
|
rp = LIST_NEXT(rp, er_link);
|
1996-06-23 04:10:08 +04:00
|
|
|
LIST_REMOVE(orp, er_link);
|
|
|
|
extent_free_region_descriptor(ex, orp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're not a fixed extent, free the extent descriptor itself. */
|
|
|
|
if ((ex->ex_flags & EXF_FIXED) == 0)
|
1996-10-17 12:27:35 +04:00
|
|
|
free(ex, ex->ex_mtype);
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert a region descriptor into the sorted region list after the
|
|
|
|
* entry "after" or at the head of the list (if "after" is NULL).
|
1996-10-17 12:27:35 +04:00
|
|
|
* The region descriptor we insert is passed in "rp". We must
|
|
|
|
* allocate the region descriptor before calling this function!
|
|
|
|
* If we don't need the region descriptor, it will be freed here.
|
1996-06-23 04:10:08 +04:00
|
|
|
*/
|
1996-10-17 12:27:35 +04:00
|
|
|
static void
|
2005-06-23 22:46:17 +04:00
|
|
|
extent_insert_and_optimize(struct extent *ex, u_long start, u_long size,
|
2006-10-12 05:30:41 +04:00
|
|
|
int flags __unused, struct extent_region *after, struct extent_region *rp)
|
1996-06-23 04:10:08 +04:00
|
|
|
{
|
1996-11-21 21:46:34 +03:00
|
|
|
struct extent_region *nextr;
|
1996-06-23 04:10:08 +04:00
|
|
|
int appended = 0;
|
|
|
|
|
|
|
|
if (after == NULL) {
|
|
|
|
/*
|
|
|
|
* We're the first in the region list. If there's
|
|
|
|
* a region after us, attempt to coalesce to save
|
|
|
|
* descriptor overhead.
|
|
|
|
*/
|
1996-10-17 12:27:35 +04:00
|
|
|
if (((ex->ex_flags & EXF_NOCOALESCE) == 0) &&
|
2002-09-04 05:32:31 +04:00
|
|
|
(LIST_FIRST(&ex->ex_regions) != NULL) &&
|
|
|
|
((start + size) == LIST_FIRST(&ex->ex_regions)->er_start)) {
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* We can coalesce. Prepend us to the first region.
|
|
|
|
*/
|
2002-09-04 05:32:31 +04:00
|
|
|
LIST_FIRST(&ex->ex_regions)->er_start = start;
|
1996-10-17 12:27:35 +04:00
|
|
|
extent_free_region_descriptor(ex, rp);
|
|
|
|
return;
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-10-17 12:27:35 +04:00
|
|
|
* Can't coalesce. Fill in the region descriptor
|
1996-06-23 04:10:08 +04:00
|
|
|
* in, and insert us at the head of the region list.
|
|
|
|
*/
|
|
|
|
rp->er_start = start;
|
|
|
|
rp->er_end = start + (size - 1);
|
|
|
|
LIST_INSERT_HEAD(&ex->ex_regions, rp, er_link);
|
1996-10-17 12:27:35 +04:00
|
|
|
return;
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-10-17 12:27:35 +04:00
|
|
|
* If EXF_NOCOALESCE is set, coalescing is disallowed.
|
1996-06-23 04:10:08 +04:00
|
|
|
*/
|
1996-10-17 12:27:35 +04:00
|
|
|
if (ex->ex_flags & EXF_NOCOALESCE)
|
|
|
|
goto cant_coalesce;
|
1996-06-23 04:10:08 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to coalesce with the region before us.
|
|
|
|
*/
|
|
|
|
if ((after->er_end + 1) == start) {
|
|
|
|
/*
|
|
|
|
* We can coalesce. Append ourselves and make
|
|
|
|
* note of it.
|
|
|
|
*/
|
|
|
|
after->er_end = start + (size - 1);
|
|
|
|
appended = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to coalesce with the region after us.
|
|
|
|
*/
|
2002-09-04 05:32:31 +04:00
|
|
|
if ((LIST_NEXT(after, er_link) != NULL) &&
|
|
|
|
((start + size) == LIST_NEXT(after, er_link)->er_start)) {
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* We can coalesce. Note that if we appended ourselves
|
|
|
|
* to the previous region, we exactly fit the gap, and
|
|
|
|
* can free the "next" region descriptor.
|
|
|
|
*/
|
|
|
|
if (appended) {
|
|
|
|
/*
|
|
|
|
* Yup, we can free it up.
|
|
|
|
*/
|
2002-09-04 05:32:31 +04:00
|
|
|
after->er_end = LIST_NEXT(after, er_link)->er_end;
|
|
|
|
nextr = LIST_NEXT(after, er_link);
|
1996-11-21 21:46:34 +03:00
|
|
|
LIST_REMOVE(nextr, er_link);
|
|
|
|
extent_free_region_descriptor(ex, nextr);
|
1996-06-23 04:10:08 +04:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Nope, just prepend us to the next region.
|
|
|
|
*/
|
2002-09-04 05:32:31 +04:00
|
|
|
LIST_NEXT(after, er_link)->er_start = start;
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
1996-10-17 12:27:35 +04:00
|
|
|
|
|
|
|
extent_free_region_descriptor(ex, rp);
|
|
|
|
return;
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We weren't able to coalesce with the next region, but
|
|
|
|
* we don't need to allocate a region descriptor if we
|
|
|
|
* appended ourselves to the previous region.
|
|
|
|
*/
|
1996-10-17 12:27:35 +04:00
|
|
|
if (appended) {
|
|
|
|
extent_free_region_descriptor(ex, rp);
|
|
|
|
return;
|
|
|
|
}
|
1996-06-23 04:10:08 +04:00
|
|
|
|
1996-10-17 12:27:35 +04:00
|
|
|
cant_coalesce:
|
1996-06-23 04:10:08 +04:00
|
|
|
|
|
|
|
/*
|
1996-10-17 12:27:35 +04:00
|
|
|
* Fill in the region descriptor and insert ourselves
|
1996-06-23 04:10:08 +04:00
|
|
|
* into the region list.
|
|
|
|
*/
|
|
|
|
rp->er_start = start;
|
|
|
|
rp->er_end = start + (size - 1);
|
|
|
|
LIST_INSERT_AFTER(after, rp, er_link);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a specific region in an extent map.
|
|
|
|
*/
|
|
|
|
int
|
2005-06-23 22:46:17 +04:00
|
|
|
extent_alloc_region(struct extent *ex, u_long start, u_long size, int flags)
|
1996-06-23 04:10:08 +04:00
|
|
|
{
|
1996-10-17 12:27:35 +04:00
|
|
|
struct extent_region *rp, *last, *myrp;
|
1996-06-23 04:10:08 +04:00
|
|
|
u_long end = start + (size - 1);
|
|
|
|
int error;
|
|
|
|
|
1996-10-17 12:27:35 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
1996-06-23 04:10:08 +04:00
|
|
|
/* Check arguments. */
|
|
|
|
if (ex == NULL)
|
|
|
|
panic("extent_alloc_region: NULL extent");
|
|
|
|
if (size < 1) {
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("extent_alloc_region: extent `%s', size 0x%lx\n",
|
1996-06-23 04:10:08 +04:00
|
|
|
ex->ex_name, size);
|
|
|
|
panic("extent_alloc_region: bad size");
|
|
|
|
}
|
|
|
|
if (end < start) {
|
1996-10-13 06:32:29 +04:00
|
|
|
printf(
|
1996-06-23 04:10:08 +04:00
|
|
|
"extent_alloc_region: extent `%s', start 0x%lx, size 0x%lx\n",
|
|
|
|
ex->ex_name, start, size);
|
|
|
|
panic("extent_alloc_region: overflow");
|
|
|
|
}
|
1996-10-17 12:27:35 +04:00
|
|
|
#endif
|
2001-06-05 08:39:56 +04:00
|
|
|
#ifdef LOCKDEBUG
|
|
|
|
if (flags & EX_WAITSPACE)
|
2006-07-21 14:08:41 +04:00
|
|
|
ASSERT_SLEEPABLE(NULL, "extent_alloc_region(EX_WAITSPACE)");
|
2001-06-05 08:39:56 +04:00
|
|
|
#endif
|
1996-10-17 12:27:35 +04:00
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* Make sure the requested region lies within the
|
|
|
|
* extent.
|
1998-06-06 06:25:46 +04:00
|
|
|
*
|
|
|
|
* We don't lock to check the range, because those values
|
|
|
|
* are never modified, and if another thread deletes the
|
|
|
|
* extent, we're screwed anyway.
|
1996-06-23 04:10:08 +04:00
|
|
|
*/
|
|
|
|
if ((start < ex->ex_start) || (end > ex->ex_end)) {
|
1996-10-17 12:27:35 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("extent_alloc_region: extent `%s' (0x%lx - 0x%lx)\n",
|
1996-06-23 04:10:08 +04:00
|
|
|
ex->ex_name, ex->ex_start, ex->ex_end);
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("extent_alloc_region: start 0x%lx, end 0x%lx\n",
|
1996-06-23 04:10:08 +04:00
|
|
|
start, end);
|
|
|
|
panic("extent_alloc_region: region lies outside extent");
|
1996-10-17 12:27:35 +04:00
|
|
|
#else
|
|
|
|
return (EINVAL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate the region descriptor. It will be freed later
|
1998-06-06 06:25:46 +04:00
|
|
|
* if we can coalesce with another region. Don't lock before
|
|
|
|
* here! This could block.
|
1996-10-17 12:27:35 +04:00
|
|
|
*/
|
|
|
|
myrp = extent_alloc_region_descriptor(ex, flags);
|
|
|
|
if (myrp == NULL) {
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
printf(
|
|
|
|
"extent_alloc_region: can't allocate region descriptor\n");
|
|
|
|
#endif
|
|
|
|
return (ENOMEM);
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
alloc_start:
|
1998-06-06 06:25:46 +04:00
|
|
|
simple_lock(&ex->ex_slock);
|
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* Attempt to place ourselves in the desired area of the
|
|
|
|
* extent. We save ourselves some work by keeping the list sorted.
|
|
|
|
* In other words, if the start of the current region is greater
|
|
|
|
* than the end of our region, we don't have to search any further.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep a pointer to the last region we looked at so
|
|
|
|
* that we don't have to traverse the list again when
|
|
|
|
* we insert ourselves. If "last" is NULL when we
|
|
|
|
* finally insert ourselves, we go at the head of the
|
|
|
|
* list. See extent_insert_and_optimize() for details.
|
|
|
|
*/
|
|
|
|
last = NULL;
|
|
|
|
|
2002-09-04 05:32:31 +04:00
|
|
|
LIST_FOREACH(rp, &ex->ex_regions, er_link) {
|
1996-06-23 04:10:08 +04:00
|
|
|
if (rp->er_start > end) {
|
|
|
|
/*
|
|
|
|
* We lie before this region and don't
|
|
|
|
* conflict.
|
|
|
|
*/
|
1997-08-29 04:47:18 +04:00
|
|
|
break;
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The current region begins before we end.
|
|
|
|
* Check for a conflict.
|
|
|
|
*/
|
|
|
|
if (rp->er_end >= start) {
|
|
|
|
/*
|
1996-10-17 12:27:35 +04:00
|
|
|
* We conflict. If we can (and want to) wait,
|
|
|
|
* do so.
|
1996-06-23 04:10:08 +04:00
|
|
|
*/
|
1996-10-17 12:27:35 +04:00
|
|
|
if (flags & EX_WAITSPACE) {
|
1996-06-23 04:10:08 +04:00
|
|
|
ex->ex_flags |= EXF_WANTED;
|
2000-08-12 20:29:36 +04:00
|
|
|
error = ltsleep(ex,
|
|
|
|
PNORELOCK | PRIBIO | ((flags & EX_CATCH) ? PCATCH : 0),
|
|
|
|
"extnt", 0, &ex->ex_slock);
|
2006-04-02 13:27:05 +04:00
|
|
|
if (error == 0)
|
|
|
|
goto alloc_start;
|
|
|
|
} else {
|
|
|
|
simple_unlock(&ex->ex_slock);
|
|
|
|
error = EAGAIN;
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
1996-10-17 12:27:35 +04:00
|
|
|
extent_free_region_descriptor(ex, myrp);
|
2006-04-02 13:27:05 +04:00
|
|
|
return error;
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We don't conflict, but this region lies before
|
|
|
|
* us. Keep a pointer to this region, and keep
|
|
|
|
* trying.
|
|
|
|
*/
|
|
|
|
last = rp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't conflict with any regions. "last" points
|
|
|
|
* to the region we fall after, or is NULL if we belong
|
|
|
|
* at the beginning of the region list. Insert ourselves.
|
|
|
|
*/
|
1996-10-17 12:27:35 +04:00
|
|
|
extent_insert_and_optimize(ex, start, size, flags, last, myrp);
|
1998-06-06 06:25:46 +04:00
|
|
|
simple_unlock(&ex->ex_slock);
|
1996-10-17 12:27:35 +04:00
|
|
|
return (0);
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Macro to check (x + y) <= z. This check is designed to fail
|
|
|
|
* if an overflow occurs.
|
|
|
|
*/
|
|
|
|
#define LE_OV(x, y, z) ((((x) + (y)) >= (x)) && (((x) + (y)) <= (z)))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a region in an extent map subregion.
|
|
|
|
*
|
|
|
|
* If EX_FAST is specified, we return the first fit in the map.
|
|
|
|
* Otherwise, we try to minimize fragmentation by finding the
|
|
|
|
* smallest gap that will hold the request.
|
|
|
|
*
|
|
|
|
* The allocated region is aligned to "alignment", which must be
|
|
|
|
* a power of 2.
|
|
|
|
*/
|
|
|
|
int
|
2005-06-23 22:46:17 +04:00
|
|
|
extent_alloc_subregion1(struct extent *ex, u_long substart, u_long subend,
|
|
|
|
u_long size, u_long alignment, u_long skew, u_long boundary,
|
|
|
|
int flags, u_long *result)
|
1996-06-23 04:10:08 +04:00
|
|
|
{
|
1996-10-17 12:27:35 +04:00
|
|
|
struct extent_region *rp, *myrp, *last, *bestlast;
|
2001-08-27 17:35:44 +04:00
|
|
|
u_long newstart, newend, exend, beststart, bestovh, ovh;
|
1999-02-18 21:13:50 +03:00
|
|
|
u_long dontcross;
|
1996-06-23 04:10:08 +04:00
|
|
|
int error;
|
|
|
|
|
1996-10-17 12:27:35 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
1998-06-06 06:25:46 +04:00
|
|
|
/*
|
|
|
|
* Check arguments.
|
|
|
|
*
|
|
|
|
* We don't lock to check these, because these values
|
|
|
|
* are never modified, and if another thread deletes the
|
|
|
|
* extent, we're screwed anyway.
|
|
|
|
*/
|
1996-06-23 04:10:08 +04:00
|
|
|
if (ex == NULL)
|
|
|
|
panic("extent_alloc_subregion: NULL extent");
|
|
|
|
if (result == NULL)
|
|
|
|
panic("extent_alloc_subregion: NULL result pointer");
|
1997-05-13 03:36:32 +04:00
|
|
|
if ((substart < ex->ex_start) || (substart > ex->ex_end) ||
|
|
|
|
(subend > ex->ex_end) || (subend < ex->ex_start)) {
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("extent_alloc_subregion: extent `%s', ex_start 0x%lx, ex_end 0x%lx\n",
|
1996-06-23 04:10:08 +04:00
|
|
|
ex->ex_name, ex->ex_start, ex->ex_end);
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("extent_alloc_subregion: substart 0x%lx, subend 0x%lx\n",
|
1996-06-23 04:10:08 +04:00
|
|
|
substart, subend);
|
|
|
|
panic("extent_alloc_subregion: bad subregion");
|
|
|
|
}
|
1997-05-13 03:36:32 +04:00
|
|
|
if ((size < 1) || ((size - 1) > (subend - substart))) {
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("extent_alloc_subregion: extent `%s', size 0x%lx\n",
|
1996-06-23 04:10:08 +04:00
|
|
|
ex->ex_name, size);
|
|
|
|
panic("extent_alloc_subregion: bad size");
|
|
|
|
}
|
|
|
|
if (alignment == 0)
|
|
|
|
panic("extent_alloc_subregion: bad alignment");
|
|
|
|
if (boundary && (boundary < size)) {
|
1996-10-13 06:32:29 +04:00
|
|
|
printf(
|
2001-04-27 04:06:11 +04:00
|
|
|
"extent_alloc_subregion: extent `%s', size 0x%lx, "
|
|
|
|
"boundary 0x%lx\n", ex->ex_name, size, boundary);
|
1996-06-23 04:10:08 +04:00
|
|
|
panic("extent_alloc_subregion: bad boundary");
|
|
|
|
}
|
1996-10-17 12:27:35 +04:00
|
|
|
#endif
|
2001-06-05 08:39:56 +04:00
|
|
|
#ifdef LOCKDEBUG
|
|
|
|
if (flags & EX_WAITSPACE)
|
2006-07-21 14:08:41 +04:00
|
|
|
ASSERT_SLEEPABLE(NULL, "extent_alloc_subregion1(EX_WAITSPACE)");
|
2001-06-05 08:39:56 +04:00
|
|
|
#endif
|
1996-10-17 12:27:35 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate the region descriptor. It will be freed later
|
1998-06-06 06:25:46 +04:00
|
|
|
* if we can coalesce with another region. Don't lock before
|
|
|
|
* here! This could block.
|
1996-10-17 12:27:35 +04:00
|
|
|
*/
|
|
|
|
myrp = extent_alloc_region_descriptor(ex, flags);
|
|
|
|
if (myrp == NULL) {
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
printf(
|
|
|
|
"extent_alloc_subregion: can't allocate region descriptor\n");
|
|
|
|
#endif
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
1996-06-23 04:10:08 +04:00
|
|
|
|
|
|
|
alloc_start:
|
1998-06-06 06:25:46 +04:00
|
|
|
simple_lock(&ex->ex_slock);
|
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* Keep a pointer to the last region we looked at so
|
|
|
|
* that we don't have to traverse the list again when
|
|
|
|
* we insert ourselves. If "last" is NULL when we
|
|
|
|
* finally insert ourselves, we go at the head of the
|
|
|
|
* list. See extent_insert_and_optimize() for deatails.
|
|
|
|
*/
|
|
|
|
last = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep track of size and location of the smallest
|
|
|
|
* chunk we fit in.
|
|
|
|
*
|
|
|
|
* Since the extent can be as large as the numeric range
|
|
|
|
* of the CPU (0 - 0xffffffff for 32-bit systems), the
|
|
|
|
* best overhead value can be the maximum unsigned integer.
|
|
|
|
* Thus, we initialize "bestovh" to 0, since we insert ourselves
|
|
|
|
* into the region list immediately on an exact match (which
|
|
|
|
* is the only case where "bestovh" would be set to 0).
|
|
|
|
*/
|
|
|
|
bestovh = 0;
|
|
|
|
beststart = 0;
|
|
|
|
bestlast = NULL;
|
|
|
|
|
2001-08-27 17:35:44 +04:00
|
|
|
/*
|
|
|
|
* Keep track of end of free region. This is either the end of extent
|
|
|
|
* or the start of a region past the subend.
|
|
|
|
*/
|
|
|
|
exend = ex->ex_end;
|
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* For N allocated regions, we must make (N + 1)
|
|
|
|
* checks for unallocated space. The first chunk we
|
|
|
|
* check is the area from the beginning of the subregion
|
1997-08-29 04:47:18 +04:00
|
|
|
* to the first allocated region after that point.
|
1996-06-23 04:10:08 +04:00
|
|
|
*/
|
1998-09-11 00:52:13 +04:00
|
|
|
newstart = EXTENT_ALIGN(substart, alignment, skew);
|
1996-06-23 04:10:08 +04:00
|
|
|
if (newstart < ex->ex_start) {
|
1996-10-17 12:27:35 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
1996-10-13 06:32:29 +04:00
|
|
|
printf(
|
1996-06-23 04:10:08 +04:00
|
|
|
"extent_alloc_subregion: extent `%s' (0x%lx - 0x%lx), alignment 0x%lx\n",
|
|
|
|
ex->ex_name, ex->ex_start, ex->ex_end, alignment);
|
1998-06-06 06:25:46 +04:00
|
|
|
simple_unlock(&ex->ex_slock);
|
1996-06-23 04:10:08 +04:00
|
|
|
panic("extent_alloc_subregion: overflow after alignment");
|
1996-10-17 12:27:35 +04:00
|
|
|
#else
|
|
|
|
extent_free_region_descriptor(ex, myrp);
|
1998-06-06 06:25:46 +04:00
|
|
|
simple_unlock(&ex->ex_slock);
|
1996-10-17 12:27:35 +04:00
|
|
|
return (EINVAL);
|
|
|
|
#endif
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
|
1997-08-29 04:47:18 +04:00
|
|
|
/*
|
|
|
|
* Find the first allocated region that begins on or after
|
|
|
|
* the subregion start, advancing the "last" pointer along
|
|
|
|
* the way.
|
|
|
|
*/
|
2002-09-04 05:32:31 +04:00
|
|
|
LIST_FOREACH(rp, &ex->ex_regions, er_link) {
|
1997-08-29 04:47:18 +04:00
|
|
|
if (rp->er_start >= newstart)
|
|
|
|
break;
|
|
|
|
last = rp;
|
|
|
|
}
|
|
|
|
|
1998-09-01 21:57:44 +04:00
|
|
|
/*
|
1999-05-11 15:02:54 +04:00
|
|
|
* Relocate the start of our candidate region to the end of
|
|
|
|
* the last allocated region (if there was one overlapping
|
|
|
|
* our subrange).
|
1998-09-01 21:57:44 +04:00
|
|
|
*/
|
1999-05-11 15:02:54 +04:00
|
|
|
if (last != NULL && last->er_end >= newstart)
|
1998-09-11 00:52:13 +04:00
|
|
|
newstart = EXTENT_ALIGN((last->er_end + 1), alignment, skew);
|
1998-09-01 21:57:44 +04:00
|
|
|
|
2002-09-04 05:32:31 +04:00
|
|
|
for (; rp != NULL; rp = LIST_NEXT(rp, er_link)) {
|
2001-08-27 17:35:44 +04:00
|
|
|
/*
|
|
|
|
* If the region pasts the subend, bail out and see
|
|
|
|
* if we fit against the subend.
|
|
|
|
*/
|
2005-03-15 21:22:24 +03:00
|
|
|
if (rp->er_start > subend) {
|
2001-08-27 17:35:44 +04:00
|
|
|
exend = rp->er_start;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* Check the chunk before "rp". Note that our
|
|
|
|
* comparison is safe from overflow conditions.
|
|
|
|
*/
|
|
|
|
if (LE_OV(newstart, size, rp->er_start)) {
|
|
|
|
/*
|
|
|
|
* Do a boundary check, if necessary. Note
|
|
|
|
* that a region may *begin* on the boundary,
|
|
|
|
* but it must end before the boundary.
|
|
|
|
*/
|
|
|
|
if (boundary) {
|
|
|
|
newend = newstart + (size - 1);
|
|
|
|
|
|
|
|
/*
|
1999-02-18 21:13:50 +03:00
|
|
|
* Calculate the next boundary after the start
|
|
|
|
* of this region.
|
1996-06-23 04:10:08 +04:00
|
|
|
*/
|
2004-03-23 16:22:32 +03:00
|
|
|
dontcross = EXTENT_ALIGN(newstart+1, boundary,
|
1999-02-18 21:13:50 +03:00
|
|
|
(flags & EX_BOUNDZERO) ? 0 : ex->ex_start)
|
|
|
|
- 1;
|
1996-06-23 04:10:08 +04:00
|
|
|
|
1999-02-18 21:52:29 +03:00
|
|
|
#if 0
|
2000-06-24 16:46:57 +04:00
|
|
|
printf("newstart=%lx newend=%lx ex_start=%lx ex_end=%lx boundary=%lx dontcross=%lx\n",
|
1999-02-18 21:52:29 +03:00
|
|
|
newstart, newend, ex->ex_start, ex->ex_end,
|
|
|
|
boundary, dontcross);
|
|
|
|
#endif
|
|
|
|
|
2000-06-16 20:48:56 +04:00
|
|
|
/* Check for overflow */
|
|
|
|
if (dontcross < ex->ex_start)
|
|
|
|
dontcross = ex->ex_end;
|
|
|
|
else if (newend > dontcross) {
|
1999-02-18 21:52:29 +03:00
|
|
|
/*
|
|
|
|
* Candidate region crosses boundary.
|
|
|
|
* Throw away the leading part and see
|
|
|
|
* if we still fit.
|
|
|
|
*/
|
|
|
|
newstart = dontcross + 1;
|
|
|
|
newend = newstart + (size - 1);
|
|
|
|
dontcross += boundary;
|
|
|
|
if (!LE_OV(newstart, size, rp->er_start))
|
2002-02-09 04:00:09 +03:00
|
|
|
goto skip;
|
1999-02-18 21:52:29 +03:00
|
|
|
}
|
|
|
|
|
1999-02-18 21:13:50 +03:00
|
|
|
/*
|
|
|
|
* If we run past the end of
|
|
|
|
* the extent or the boundary
|
|
|
|
* overflows, then the request
|
|
|
|
* can't fit.
|
|
|
|
*/
|
2000-07-07 18:10:48 +04:00
|
|
|
if (newstart + size - 1 > ex->ex_end ||
|
1999-02-18 21:13:50 +03:00
|
|
|
dontcross < newstart)
|
|
|
|
goto fail;
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We would fit into this space. Calculate
|
|
|
|
* the overhead (wasted space). If we exactly
|
|
|
|
* fit, or we're taking the first fit, insert
|
|
|
|
* ourselves into the region list.
|
|
|
|
*/
|
|
|
|
ovh = rp->er_start - newstart - size;
|
|
|
|
if ((flags & EX_FAST) || (ovh == 0))
|
|
|
|
goto found;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't exactly fit, but check to see
|
|
|
|
* if we're better than any current choice.
|
|
|
|
*/
|
|
|
|
if ((bestovh == 0) || (ovh < bestovh)) {
|
|
|
|
bestovh = ovh;
|
|
|
|
beststart = newstart;
|
|
|
|
bestlast = last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-09 04:00:09 +03:00
|
|
|
skip:
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* Skip past the current region and check again.
|
|
|
|
*/
|
1998-09-11 00:52:13 +04:00
|
|
|
newstart = EXTENT_ALIGN((rp->er_end + 1), alignment, skew);
|
1996-06-23 04:10:08 +04:00
|
|
|
if (newstart < rp->er_end) {
|
|
|
|
/*
|
|
|
|
* Overflow condition. Don't error out, since
|
|
|
|
* we might have a chunk of space that we can
|
|
|
|
* use.
|
|
|
|
*/
|
|
|
|
goto fail;
|
|
|
|
}
|
2004-03-23 16:22:32 +03:00
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
last = rp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The final check is from the current starting point to the
|
|
|
|
* end of the subregion. If there were no allocated regions,
|
|
|
|
* "newstart" is set to the beginning of the subregion, or
|
|
|
|
* just past the end of the last allocated region, adjusted
|
|
|
|
* for alignment in either case.
|
|
|
|
*/
|
|
|
|
if (LE_OV(newstart, (size - 1), subend)) {
|
1999-02-18 21:52:29 +03:00
|
|
|
/*
|
|
|
|
* Do a boundary check, if necessary. Note
|
|
|
|
* that a region may *begin* on the boundary,
|
|
|
|
* but it must end before the boundary.
|
|
|
|
*/
|
|
|
|
if (boundary) {
|
|
|
|
newend = newstart + (size - 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the next boundary after the start
|
|
|
|
* of this region.
|
|
|
|
*/
|
2004-03-23 16:22:32 +03:00
|
|
|
dontcross = EXTENT_ALIGN(newstart+1, boundary,
|
1999-02-18 21:52:29 +03:00
|
|
|
(flags & EX_BOUNDZERO) ? 0 : ex->ex_start)
|
|
|
|
- 1;
|
|
|
|
|
|
|
|
#if 0
|
2000-06-24 16:46:57 +04:00
|
|
|
printf("newstart=%lx newend=%lx ex_start=%lx ex_end=%lx boundary=%lx dontcross=%lx\n",
|
1999-02-18 21:52:29 +03:00
|
|
|
newstart, newend, ex->ex_start, ex->ex_end,
|
|
|
|
boundary, dontcross);
|
|
|
|
#endif
|
|
|
|
|
2000-06-16 20:48:56 +04:00
|
|
|
/* Check for overflow */
|
|
|
|
if (dontcross < ex->ex_start)
|
|
|
|
dontcross = ex->ex_end;
|
|
|
|
else if (newend > dontcross) {
|
1999-02-18 21:52:29 +03:00
|
|
|
/*
|
|
|
|
* Candidate region crosses boundary.
|
|
|
|
* Throw away the leading part and see
|
|
|
|
* if we still fit.
|
|
|
|
*/
|
|
|
|
newstart = dontcross + 1;
|
|
|
|
newend = newstart + (size - 1);
|
|
|
|
dontcross += boundary;
|
|
|
|
if (!LE_OV(newstart, (size - 1), subend))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we run past the end of
|
|
|
|
* the extent or the boundary
|
|
|
|
* overflows, then the request
|
|
|
|
* can't fit.
|
|
|
|
*/
|
2000-07-07 18:10:48 +04:00
|
|
|
if (newstart + size - 1 > ex->ex_end ||
|
1999-02-18 21:52:29 +03:00
|
|
|
dontcross < newstart)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* We would fit into this space. Calculate
|
|
|
|
* the overhead (wasted space). If we exactly
|
|
|
|
* fit, or we're taking the first fit, insert
|
|
|
|
* ourselves into the region list.
|
|
|
|
*/
|
2001-08-27 17:35:44 +04:00
|
|
|
ovh = exend - newstart - (size - 1);
|
1996-06-23 04:10:08 +04:00
|
|
|
if ((flags & EX_FAST) || (ovh == 0))
|
|
|
|
goto found;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't exactly fit, but check to see
|
|
|
|
* if we're better than any current choice.
|
|
|
|
*/
|
|
|
|
if ((bestovh == 0) || (ovh < bestovh)) {
|
|
|
|
bestovh = ovh;
|
|
|
|
beststart = newstart;
|
|
|
|
bestlast = last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
/*
|
|
|
|
* One of the following two conditions have
|
|
|
|
* occurred:
|
|
|
|
*
|
|
|
|
* There is no chunk large enough to hold the request.
|
|
|
|
*
|
|
|
|
* If EX_FAST was not specified, there is not an
|
|
|
|
* exact match for the request.
|
|
|
|
*
|
|
|
|
* Note that if we reach this point and EX_FAST is
|
|
|
|
* set, then we know there is no space in the extent for
|
|
|
|
* the request.
|
|
|
|
*/
|
|
|
|
if (((flags & EX_FAST) == 0) && (bestovh != 0)) {
|
|
|
|
/*
|
|
|
|
* We have a match that's "good enough".
|
|
|
|
*/
|
|
|
|
newstart = beststart;
|
|
|
|
last = bestlast;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No space currently available. Wait for it to free up,
|
|
|
|
* if possible.
|
|
|
|
*/
|
1996-10-17 12:27:35 +04:00
|
|
|
if (flags & EX_WAITSPACE) {
|
1996-06-23 04:10:08 +04:00
|
|
|
ex->ex_flags |= EXF_WANTED;
|
2000-08-12 20:29:36 +04:00
|
|
|
error = ltsleep(ex,
|
|
|
|
PNORELOCK | PRIBIO | ((flags & EX_CATCH) ? PCATCH : 0),
|
|
|
|
"extnt", 0, &ex->ex_slock);
|
2006-04-02 13:27:05 +04:00
|
|
|
if (error == 0)
|
|
|
|
goto alloc_start;
|
|
|
|
} else {
|
|
|
|
simple_unlock(&ex->ex_slock);
|
|
|
|
error = EAGAIN;
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
|
1996-10-17 12:27:35 +04:00
|
|
|
extent_free_region_descriptor(ex, myrp);
|
2006-04-02 13:27:05 +04:00
|
|
|
return error;
|
1996-06-23 04:10:08 +04:00
|
|
|
|
|
|
|
found:
|
|
|
|
/*
|
|
|
|
* Insert ourselves into the region list.
|
|
|
|
*/
|
1996-10-17 12:27:35 +04:00
|
|
|
extent_insert_and_optimize(ex, newstart, size, flags, last, myrp);
|
1998-06-06 06:25:46 +04:00
|
|
|
simple_unlock(&ex->ex_slock);
|
1996-10-17 12:27:35 +04:00
|
|
|
*result = newstart;
|
|
|
|
return (0);
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|
|
|
|
|
2006-03-08 07:43:00 +03:00
|
|
|
int
|
|
|
|
extent_alloc_subregion(struct extent *ex, u_long start, u_long end, u_long size,
|
|
|
|
u_long alignment, u_long boundary, int flags, u_long *result)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (extent_alloc_subregion1(ex, start, end, size, alignment,
|
|
|
|
0, boundary, flags, result));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
extent_alloc(struct extent *ex, u_long size, u_long alignment, u_long boundary,
|
|
|
|
int flags, u_long *result)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (extent_alloc_subregion1(ex, ex->ex_start, ex->ex_end,
|
|
|
|
size, alignment, 0, boundary,
|
|
|
|
flags, result));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
extent_alloc1(struct extent *ex, u_long size, u_long alignment, u_long skew,
|
|
|
|
u_long boundary, int flags, u_long *result)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (extent_alloc_subregion1(ex, ex->ex_start, ex->ex_end,
|
|
|
|
size, alignment, skew, boundary,
|
|
|
|
flags, result));
|
|
|
|
}
|
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
int
|
2005-06-23 22:46:17 +04:00
|
|
|
extent_free(struct extent *ex, u_long start, u_long size, int flags)
|
1996-06-23 04:10:08 +04:00
|
|
|
{
|
1998-06-06 06:25:46 +04:00
|
|
|
struct extent_region *rp, *nrp = NULL;
|
1996-06-23 04:10:08 +04:00
|
|
|
u_long end = start + (size - 1);
|
2006-04-15 09:05:44 +04:00
|
|
|
int coalesce;
|
1996-06-23 04:10:08 +04:00
|
|
|
|
1996-10-17 12:27:35 +04:00
|
|
|
#ifdef DIAGNOSTIC
|
1998-06-06 06:25:46 +04:00
|
|
|
/*
|
|
|
|
* Check arguments.
|
|
|
|
*
|
|
|
|
* We don't lock to check these, because these values
|
|
|
|
* are never modified, and if another thread deletes the
|
|
|
|
* extent, we're screwed anyway.
|
|
|
|
*/
|
1996-06-23 04:10:08 +04:00
|
|
|
if (ex == NULL)
|
|
|
|
panic("extent_free: NULL extent");
|
2005-03-15 21:22:24 +03:00
|
|
|
if ((start < ex->ex_start) || (end > ex->ex_end)) {
|
1996-06-23 04:10:08 +04:00
|
|
|
extent_print(ex);
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
|
1996-06-23 04:10:08 +04:00
|
|
|
ex->ex_name, start, size);
|
|
|
|
panic("extent_free: extent `%s', region not within extent",
|
|
|
|
ex->ex_name);
|
|
|
|
}
|
|
|
|
/* Check for an overflow. */
|
|
|
|
if (end < start) {
|
|
|
|
extent_print(ex);
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("extent_free: extent `%s', start 0x%lx, size 0x%lx\n",
|
1996-06-23 04:10:08 +04:00
|
|
|
ex->ex_name, start, size);
|
|
|
|
panic("extent_free: overflow");
|
|
|
|
}
|
1996-10-17 12:27:35 +04:00
|
|
|
#endif
|
1996-06-23 04:10:08 +04:00
|
|
|
|
1998-06-06 06:25:46 +04:00
|
|
|
/*
|
|
|
|
* If we're allowing coalescing, we must allocate a region
|
|
|
|
* descriptor now, since it might block.
|
|
|
|
*
|
|
|
|
* XXX Make a static, create-time flags word, so we don't
|
|
|
|
* XXX have to lock to read it!
|
|
|
|
*/
|
|
|
|
simple_lock(&ex->ex_slock);
|
2006-04-15 09:05:44 +04:00
|
|
|
coalesce = (ex->ex_flags & EXF_NOCOALESCE) == 0;
|
1998-06-06 06:25:46 +04:00
|
|
|
simple_unlock(&ex->ex_slock);
|
1998-07-24 00:57:17 +04:00
|
|
|
|
2006-04-15 09:07:15 +04:00
|
|
|
if (coalesce) {
|
1998-06-06 06:25:46 +04:00
|
|
|
/* Allocate a region descriptor. */
|
|
|
|
nrp = extent_alloc_region_descriptor(ex, flags);
|
|
|
|
if (nrp == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
simple_lock(&ex->ex_slock);
|
|
|
|
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* Find region and deallocate. Several possibilities:
|
|
|
|
*
|
|
|
|
* 1. (start == er_start) && (end == er_end):
|
|
|
|
* Free descriptor.
|
|
|
|
*
|
|
|
|
* 2. (start == er_start) && (end < er_end):
|
|
|
|
* Adjust er_start.
|
|
|
|
*
|
|
|
|
* 3. (start > er_start) && (end == er_end):
|
|
|
|
* Adjust er_end.
|
|
|
|
*
|
|
|
|
* 4. (start > er_start) && (end < er_end):
|
|
|
|
* Fragment region. Requires descriptor alloc.
|
|
|
|
*
|
1996-10-17 12:27:35 +04:00
|
|
|
* Cases 2, 3, and 4 require that the EXF_NOCOALESCE flag
|
1996-06-23 04:10:08 +04:00
|
|
|
* is not set.
|
|
|
|
*/
|
2002-09-04 05:32:31 +04:00
|
|
|
LIST_FOREACH(rp, &ex->ex_regions, er_link) {
|
1996-06-23 04:10:08 +04:00
|
|
|
/*
|
|
|
|
* Save ourselves some comparisons; does the current
|
|
|
|
* region end before chunk to be freed begins? If so,
|
|
|
|
* then we haven't found the appropriate region descriptor.
|
|
|
|
*/
|
|
|
|
if (rp->er_end < start)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save ourselves some traversal; does the current
|
|
|
|
* region begin after the chunk to be freed ends? If so,
|
|
|
|
* then we've already passed any possible region descriptors
|
|
|
|
* that might have contained the chunk to be freed.
|
|
|
|
*/
|
|
|
|
if (rp->er_start > end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Case 1. */
|
|
|
|
if ((start == rp->er_start) && (end == rp->er_end)) {
|
|
|
|
LIST_REMOVE(rp, er_link);
|
|
|
|
extent_free_region_descriptor(ex, rp);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-10-17 12:27:35 +04:00
|
|
|
* The following cases all require that EXF_NOCOALESCE
|
1996-06-23 04:10:08 +04:00
|
|
|
* is not set.
|
|
|
|
*/
|
2006-04-15 09:05:44 +04:00
|
|
|
if (!coalesce)
|
1996-06-23 04:10:08 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Case 2. */
|
|
|
|
if ((start == rp->er_start) && (end < rp->er_end)) {
|
|
|
|
rp->er_start = (end + 1);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Case 3. */
|
|
|
|
if ((start > rp->er_start) && (end == rp->er_end)) {
|
|
|
|
rp->er_end = (start - 1);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Case 4. */
|
|
|
|
if ((start > rp->er_start) && (end < rp->er_end)) {
|
|
|
|
/* Fill in new descriptor. */
|
|
|
|
nrp->er_start = end + 1;
|
|
|
|
nrp->er_end = rp->er_end;
|
|
|
|
|
|
|
|
/* Adjust current descriptor. */
|
|
|
|
rp->er_end = start - 1;
|
|
|
|
|
1998-07-15 16:38:29 +04:00
|
|
|
/* Insert new descriptor after current. */
|
1996-06-23 04:10:08 +04:00
|
|
|
LIST_INSERT_AFTER(rp, nrp, er_link);
|
1998-07-15 16:38:29 +04:00
|
|
|
|
|
|
|
/* We used the new descriptor, so don't free it below */
|
|
|
|
nrp = NULL;
|
1996-06-23 04:10:08 +04:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Region not found, or request otherwise invalid. */
|
1998-06-06 06:25:46 +04:00
|
|
|
simple_unlock(&ex->ex_slock);
|
1996-06-23 04:10:08 +04:00
|
|
|
extent_print(ex);
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("extent_free: start 0x%lx, end 0x%lx\n", start, end);
|
1996-06-23 04:10:08 +04:00
|
|
|
panic("extent_free: region not found");
|
|
|
|
|
|
|
|
done:
|
1998-07-15 16:38:29 +04:00
|
|
|
if (nrp != NULL)
|
|
|
|
extent_free_region_descriptor(ex, nrp);
|
1996-06-23 04:10:08 +04:00
|
|
|
if (ex->ex_flags & EXF_WANTED) {
|
|
|
|
ex->ex_flags &= ~EXF_WANTED;
|
|
|
|
wakeup(ex);
|
|
|
|
}
|
1998-06-06 06:25:46 +04:00
|
|
|
simple_unlock(&ex->ex_slock);
|
1996-06-23 04:10:08 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-06-23 22:46:17 +04:00
|
|
|
extent_print(struct extent *ex)
|
1996-06-23 04:10:08 +04:00
|
|
|
{
|
|
|
|
struct extent_region *rp;
|
|
|
|
|
|
|
|
if (ex == NULL)
|
|
|
|
panic("extent_print: NULL extent");
|
|
|
|
|
1998-06-06 06:25:46 +04:00
|
|
|
simple_lock(&ex->ex_slock);
|
|
|
|
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("extent `%s' (0x%lx - 0x%lx), flags = 0x%x\n", ex->ex_name,
|
1996-06-23 04:10:08 +04:00
|
|
|
ex->ex_start, ex->ex_end, ex->ex_flags);
|
|
|
|
|
2002-09-04 05:32:31 +04:00
|
|
|
LIST_FOREACH(rp, &ex->ex_regions, er_link)
|
1996-10-13 06:32:29 +04:00
|
|
|
printf(" 0x%lx - 0x%lx\n", rp->er_start, rp->er_end);
|
1998-06-06 06:25:46 +04:00
|
|
|
|
|
|
|
simple_unlock(&ex->ex_slock);
|
1996-06-23 04:10:08 +04:00
|
|
|
}
|