2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
39 #include <sys/mutex.h>
41 #include <machine/stdarg.h>
43 #include <linux/bitops.h>
44 #include <linux/kobject.h>
45 #include <linux/slab.h>
46 #include <linux/idr.h>
47 #include <linux/err.h>
52 * This is quick and dirty and not as re-entrant as the linux version
53 * however it should be fairly fast. It is basically a radix tree with
54 * a builtin bitmap for allocation.
56 static MALLOC_DEFINE(M_IDR, "idr", "Linux IDR compat");
59 idr_max(struct idr *idr)
61 return (1 << (idr->layers * IDR_BITS)) - 1;
65 idr_pos(int id, int layer)
67 return (id >> (IDR_BITS * layer)) & IDR_MASK;
71 idr_init(struct idr *idr)
73 bzero(idr, sizeof(*idr));
74 mtx_init(&idr->lock, "idr", NULL, MTX_DEF);
77 /* Only frees cached pages. */
79 idr_destroy(struct idr *idr)
81 struct idr_layer *il, *iln;
85 for (il = idr->free; il != NULL; il = iln) {
89 mtx_unlock(&idr->lock);
90 mtx_destroy(&idr->lock);
94 idr_remove_layer(struct idr_layer *il, int layer)
104 for (i = 0; i < IDR_SIZE; i++)
106 idr_remove_layer(il->ary[i], layer - 1);
110 idr_remove_all(struct idr *idr)
113 mtx_lock(&idr->lock);
114 idr_remove_layer(idr->top, idr->layers - 1);
117 mtx_unlock(&idr->lock);
121 idr_remove_locked(struct idr *idr, int id)
123 struct idr_layer *il;
129 layer = idr->layers - 1;
130 if (il == NULL || id > idr_max(idr))
133 * Walk down the tree to this item setting bitmaps along the way
134 * as we know at least one item will be free along this path.
136 while (layer && il) {
137 idx = idr_pos(id, layer);
138 il->bitmap |= 1 << idx;
144 * At this point we've set free space bitmaps up the whole tree.
145 * We could make this non-fatal and unwind but linux dumps a stack
146 * and a warning so I don't think it's necessary.
148 if (il == NULL || (il->bitmap & (1 << idx)) != 0)
149 panic("idr_remove: Item %d not allocated (%p, %p)\n",
152 il->bitmap |= 1 << idx;
156 idr_remove(struct idr *idr, int id)
158 mtx_lock(&idr->lock);
159 idr_remove_locked(idr, id);
160 mtx_unlock(&idr->lock);
164 static inline struct idr_layer *
165 idr_find_layer_locked(struct idr *idr, int id)
167 struct idr_layer *il;
172 layer = idr->layers - 1;
173 if (il == NULL || id > idr_max(idr))
175 while (layer && il) {
176 il = il->ary[idr_pos(id, layer)];
183 idr_replace(struct idr *idr, void *ptr, int id)
185 struct idr_layer *il;
189 mtx_lock(&idr->lock);
190 il = idr_find_layer_locked(idr, id);
193 /* Replace still returns an error if the item was not allocated. */
194 if (il == NULL || (il->bitmap & (1 << idx))) {
195 res = ERR_PTR(-ENOENT);
200 mtx_unlock(&idr->lock);
205 idr_find_locked(struct idr *idr, int id)
207 struct idr_layer *il;
210 mtx_assert(&idr->lock, MA_OWNED);
211 il = idr_find_layer_locked(idr, id);
213 res = il->ary[id & IDR_MASK];
220 idr_find(struct idr *idr, int id)
224 mtx_lock(&idr->lock);
225 res = idr_find_locked(idr, id);
226 mtx_unlock(&idr->lock);
231 idr_pre_get(struct idr *idr, gfp_t gfp_mask)
233 struct idr_layer *il, *iln;
234 struct idr_layer *head;
237 mtx_lock(&idr->lock);
239 need = idr->layers + 1;
240 for (il = idr->free; il != NULL; il = il->ary[0])
242 mtx_unlock(&idr->lock);
245 for (head = NULL; need; need--) {
246 iln = malloc(sizeof(*il), M_IDR, M_ZERO | gfp_mask);
249 bitmap_fill(&iln->bitmap, IDR_SIZE);
258 mtx_lock(&idr->lock);
259 il->ary[0] = idr->free;
265 static inline struct idr_layer *
266 idr_get(struct idr *idr)
268 struct idr_layer *il;
272 idr->free = il->ary[0];
276 il = malloc(sizeof(*il), M_IDR, M_ZERO | M_NOWAIT);
277 bitmap_fill(&il->bitmap, IDR_SIZE);
282 * Could be implemented as get_new_above(idr, ptr, 0, idp) but written
283 * first for simplicity sake.
286 idr_get_new_locked(struct idr *idr, void *ptr, int *idp)
288 struct idr_layer *stack[MAX_LEVEL];
289 struct idr_layer *il;
295 mtx_assert(&idr->lock, MA_OWNED);
299 * Expand the tree until there is free space.
301 if (idr->top == NULL || idr->top->bitmap == 0) {
302 if (idr->layers == MAX_LEVEL + 1) {
309 il->ary[0] = idr->top;
318 * Walk the tree following free bitmaps, record our path.
320 for (layer = idr->layers - 1;; layer--) {
322 idx = ffsl(il->bitmap);
324 panic("idr_get_new: Invalid leaf state (%p, %p)\n",
327 id |= idx << (layer * IDR_BITS);
330 if (il->ary[idx] == NULL) {
331 il->ary[idx] = idr_get(idr);
332 if (il->ary[idx] == NULL)
338 * Allocate the leaf to the consumer.
340 il->bitmap &= ~(1 << idx);
344 * Clear bitmaps potentially up to the root.
346 while (il->bitmap == 0 && ++layer < idr->layers) {
348 il->bitmap &= ~(1 << idr_pos(id, layer));
353 if (error == 0 && idr_find_locked(idr, id) != ptr) {
354 panic("idr_get_new: Failed for idr %p, id %d, ptr %p\n",
362 idr_get_new(struct idr *idr, void *ptr, int *idp)
366 mtx_lock(&idr->lock);
367 retval = idr_get_new_locked(idr, ptr, idp);
368 mtx_unlock(&idr->lock);
373 idr_get_new_above_locked(struct idr *idr, void *ptr, int starting_id, int *idp)
375 struct idr_layer *stack[MAX_LEVEL];
376 struct idr_layer *il;
382 mtx_assert(&idr->lock, MA_OWNED);
386 * Compute the layers required to support starting_id and the mask
392 while (idx & ~IDR_MASK) {
396 if (layer == MAX_LEVEL + 1) {
401 * Expand the tree until there is free space at or beyond starting_id.
403 while (idr->layers <= layer ||
404 idr->top->bitmap < (1 << idr_pos(starting_id, idr->layers - 1))) {
405 if (idr->layers == MAX_LEVEL + 1) {
412 il->ary[0] = idr->top;
413 if (idr->top && idr->top->bitmap == 0)
421 * Walk the tree following free bitmaps, record our path.
423 for (layer = idr->layers - 1;; layer--) {
425 sidx = idr_pos(starting_id, layer);
426 /* Returns index numbered from 0 or size if none exists. */
427 idx = find_next_bit(&il->bitmap, IDR_SIZE, sidx);
428 if (idx == IDR_SIZE && sidx == 0)
429 panic("idr_get_new: Invalid leaf state (%p, %p)\n",
432 * We may have walked a path where there was a free bit but
433 * it was lower than what we wanted. Restart the search with
434 * a larger starting id. id contains the progress we made so
435 * far. Search the leaf one above this level. This may
436 * restart as many as MAX_LEVEL times but that is expected
439 if (idx == IDR_SIZE) {
440 starting_id = id + (1 << ((layer + 1) * IDR_BITS));
444 starting_id = 0; /* Search the whole subtree. */
445 id |= idx << (layer * IDR_BITS);
448 if (il->ary[idx] == NULL) {
449 il->ary[idx] = idr_get(idr);
450 if (il->ary[idx] == NULL)
456 * Allocate the leaf to the consumer.
458 il->bitmap &= ~(1 << idx);
462 * Clear bitmaps potentially up to the root.
464 while (il->bitmap == 0 && ++layer < idr->layers) {
466 il->bitmap &= ~(1 << idr_pos(id, layer));
471 if (error == 0 && idr_find_locked(idr, id) != ptr) {
472 panic("idr_get_new_above: Failed for idr %p, id %d, ptr %p\n",
480 idr_get_new_above(struct idr *idr, void *ptr, int starting_id, int *idp)
484 mtx_lock(&idr->lock);
485 retval = idr_get_new_above_locked(idr, ptr, starting_id, idp);
486 mtx_unlock(&idr->lock);
491 idr_alloc_locked(struct idr *idr, void *ptr, int start, int end)
493 int max = end > 0 ? end - 1 : INT_MAX;
497 mtx_assert(&idr->lock, MA_OWNED);
499 if (unlikely(start < 0))
501 if (unlikely(max < start))
505 error = idr_get_new_locked(idr, ptr, &id);
507 error = idr_get_new_above_locked(idr, ptr, start, &id);
509 if (unlikely(error < 0))
511 if (unlikely(id > max)) {
512 idr_remove_locked(idr, id);
519 idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
523 mtx_lock(&idr->lock);
524 retval = idr_alloc_locked(idr, ptr, start, end);
525 mtx_unlock(&idr->lock);
530 idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
534 mtx_lock(&idr->lock);
535 retval = idr_alloc_locked(idr, ptr, max(start, idr->next_cyclic_id), end);
536 if (unlikely(retval == -ENOSPC))
537 retval = idr_alloc_locked(idr, ptr, start, end);
538 if (likely(retval >= 0))
539 idr->next_cyclic_id = retval + 1;
540 mtx_unlock(&idr->lock);