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