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