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