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