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