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