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