Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * 2002-10-18 written by Jim Houston jim.houston@ccur.com |
| 3 | * Copyright (C) 2002 by Concurrent Computer Corporation |
| 4 | * Distributed under the GNU GPL license version 2. |
| 5 | * |
| 6 | * Modified by George Anzinger to reuse immediately and to use |
| 7 | * find bit instructions. Also removed _irq on spinlocks. |
| 8 | * |
Nadia Derbey | 3219b3b | 2008-07-25 01:48:00 -0700 | [diff] [blame^] | 9 | * Modified by Nadia Derbey to make it RCU safe. |
| 10 | * |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 11 | * Small id to pointer translation service. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 13 | * It uses a radix tree like structure as a sparse array indexed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * by the id to obtain the pointer. The bitmap makes allocating |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 15 | * a new id quick. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * |
| 17 | * You call it to allocate an id (an int) an associate with that id a |
| 18 | * pointer or what ever, we treat it as a (void *). You can pass this |
| 19 | * id to a user for him to pass back at a later time. You then pass |
| 20 | * that id to this code and it returns your pointer. |
| 21 | |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 22 | * You can release ids at any time. When all ids are released, most of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * the memory is returned (we keep IDR_FREE_MAX) in a local pool so we |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 24 | * don't need to go to the memory "store" during an id allocate, just |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * so you don't need to be too concerned about locking and conflicts |
| 26 | * with the slab allocator. |
| 27 | */ |
| 28 | |
| 29 | #ifndef TEST // to test in user space... |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/module.h> |
| 33 | #endif |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 34 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/string.h> |
| 36 | #include <linux/idr.h> |
| 37 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 38 | static struct kmem_cache *idr_layer_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 40 | static struct idr_layer *get_from_free_list(struct idr *idp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | struct idr_layer *p; |
Roland Dreier | c259cc2 | 2006-07-14 00:24:23 -0700 | [diff] [blame] | 43 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Roland Dreier | c259cc2 | 2006-07-14 00:24:23 -0700 | [diff] [blame] | 45 | spin_lock_irqsave(&idp->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | if ((p = idp->id_free)) { |
| 47 | idp->id_free = p->ary[0]; |
| 48 | idp->id_free_cnt--; |
| 49 | p->ary[0] = NULL; |
| 50 | } |
Roland Dreier | c259cc2 | 2006-07-14 00:24:23 -0700 | [diff] [blame] | 51 | spin_unlock_irqrestore(&idp->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | return(p); |
| 53 | } |
| 54 | |
Sonny Rao | 1eec005 | 2006-06-25 05:49:34 -0700 | [diff] [blame] | 55 | /* only called when idp->lock is held */ |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 56 | static void __move_to_free_list(struct idr *idp, struct idr_layer *p) |
Sonny Rao | 1eec005 | 2006-06-25 05:49:34 -0700 | [diff] [blame] | 57 | { |
| 58 | p->ary[0] = idp->id_free; |
| 59 | idp->id_free = p; |
| 60 | idp->id_free_cnt++; |
| 61 | } |
| 62 | |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 63 | static void move_to_free_list(struct idr *idp, struct idr_layer *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Roland Dreier | c259cc2 | 2006-07-14 00:24:23 -0700 | [diff] [blame] | 65 | unsigned long flags; |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* |
| 68 | * Depends on the return element being zeroed. |
| 69 | */ |
Roland Dreier | c259cc2 | 2006-07-14 00:24:23 -0700 | [diff] [blame] | 70 | spin_lock_irqsave(&idp->lock, flags); |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 71 | __move_to_free_list(idp, p); |
Roland Dreier | c259cc2 | 2006-07-14 00:24:23 -0700 | [diff] [blame] | 72 | spin_unlock_irqrestore(&idp->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Tejun Heo | e33ac8b | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 75 | static void idr_mark_full(struct idr_layer **pa, int id) |
| 76 | { |
| 77 | struct idr_layer *p = pa[0]; |
| 78 | int l = 0; |
| 79 | |
| 80 | __set_bit(id & IDR_MASK, &p->bitmap); |
| 81 | /* |
| 82 | * If this layer is full mark the bit in the layer above to |
| 83 | * show that this part of the radix tree is full. This may |
| 84 | * complete the layer above and require walking up the radix |
| 85 | * tree. |
| 86 | */ |
| 87 | while (p->bitmap == IDR_FULL) { |
| 88 | if (!(p = pa[++l])) |
| 89 | break; |
| 90 | id = id >> IDR_BITS; |
| 91 | __set_bit((id & IDR_MASK), &p->bitmap); |
| 92 | } |
| 93 | } |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | /** |
| 96 | * idr_pre_get - reserver resources for idr allocation |
| 97 | * @idp: idr handle |
| 98 | * @gfp_mask: memory allocation flags |
| 99 | * |
| 100 | * This function should be called prior to locking and calling the |
Nadia Derbey | 3219b3b | 2008-07-25 01:48:00 -0700 | [diff] [blame^] | 101 | * idr_get_new* functions. It preallocates enough memory to satisfy |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | * the worst possible allocation. |
| 103 | * |
| 104 | * If the system is REALLY out of memory this function returns 0, |
| 105 | * otherwise 1. |
| 106 | */ |
Al Viro | fd4f2df | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 107 | int idr_pre_get(struct idr *idp, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
| 109 | while (idp->id_free_cnt < IDR_FREE_MAX) { |
| 110 | struct idr_layer *new; |
| 111 | new = kmem_cache_alloc(idr_layer_cache, gfp_mask); |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 112 | if (new == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return (0); |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 114 | move_to_free_list(idp, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | return 1; |
| 117 | } |
| 118 | EXPORT_SYMBOL(idr_pre_get); |
| 119 | |
Tejun Heo | e33ac8b | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 120 | static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | int n, m, sh; |
| 123 | struct idr_layer *p, *new; |
Tejun Heo | 7aae6dd | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 124 | int l, id, oid; |
Al Viro | 5ba2533 | 2007-10-14 19:35:50 +0100 | [diff] [blame] | 125 | unsigned long bm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | id = *starting_id; |
Tejun Heo | 7aae6dd | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 128 | restart: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | p = idp->top; |
| 130 | l = idp->layers; |
| 131 | pa[l--] = NULL; |
| 132 | while (1) { |
| 133 | /* |
| 134 | * We run around this while until we reach the leaf node... |
| 135 | */ |
| 136 | n = (id >> (IDR_BITS*l)) & IDR_MASK; |
| 137 | bm = ~p->bitmap; |
| 138 | m = find_next_bit(&bm, IDR_SIZE, n); |
| 139 | if (m == IDR_SIZE) { |
| 140 | /* no space available go back to previous layer. */ |
| 141 | l++; |
Tejun Heo | 7aae6dd | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 142 | oid = id; |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 143 | id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1; |
Tejun Heo | 7aae6dd | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 144 | |
| 145 | /* if already at the top layer, we need to grow */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | if (!(p = pa[l])) { |
| 147 | *starting_id = id; |
Nadia Derbey | 944ca05 | 2008-07-25 01:47:59 -0700 | [diff] [blame] | 148 | return IDR_NEED_TO_GROW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
Tejun Heo | 7aae6dd | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 150 | |
| 151 | /* If we need to go up one layer, continue the |
| 152 | * loop; otherwise, restart from the top. |
| 153 | */ |
| 154 | sh = IDR_BITS * (l + 1); |
| 155 | if (oid >> sh == id >> sh) |
| 156 | continue; |
| 157 | else |
| 158 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | if (m != n) { |
| 161 | sh = IDR_BITS*l; |
| 162 | id = ((id >> sh) ^ n ^ m) << sh; |
| 163 | } |
| 164 | if ((id >= MAX_ID_BIT) || (id < 0)) |
Nadia Derbey | 944ca05 | 2008-07-25 01:47:59 -0700 | [diff] [blame] | 165 | return IDR_NOMORE_SPACE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if (l == 0) |
| 167 | break; |
| 168 | /* |
| 169 | * Create the layer below if it is missing. |
| 170 | */ |
| 171 | if (!p->ary[m]) { |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 172 | new = get_from_free_list(idp); |
| 173 | if (!new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return -1; |
Nadia Derbey | 3219b3b | 2008-07-25 01:48:00 -0700 | [diff] [blame^] | 175 | rcu_assign_pointer(p->ary[m], new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | p->count++; |
| 177 | } |
| 178 | pa[l--] = p; |
| 179 | p = p->ary[m]; |
| 180 | } |
Tejun Heo | e33ac8b | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 181 | |
| 182 | pa[l] = p; |
| 183 | return id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Tejun Heo | e33ac8b | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 186 | static int idr_get_empty_slot(struct idr *idp, int starting_id, |
| 187 | struct idr_layer **pa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
| 189 | struct idr_layer *p, *new; |
| 190 | int layers, v, id; |
Roland Dreier | c259cc2 | 2006-07-14 00:24:23 -0700 | [diff] [blame] | 191 | unsigned long flags; |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | id = starting_id; |
| 194 | build_up: |
| 195 | p = idp->top; |
| 196 | layers = idp->layers; |
| 197 | if (unlikely(!p)) { |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 198 | if (!(p = get_from_free_list(idp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | return -1; |
| 200 | layers = 1; |
| 201 | } |
| 202 | /* |
| 203 | * Add a new layer to the top of the tree if the requested |
| 204 | * id is larger than the currently allocated space. |
| 205 | */ |
Zaur Kambarov | 589777e | 2005-06-21 17:14:31 -0700 | [diff] [blame] | 206 | while ((layers < (MAX_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | layers++; |
| 208 | if (!p->count) |
| 209 | continue; |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 210 | if (!(new = get_from_free_list(idp))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | /* |
| 212 | * The allocation failed. If we built part of |
| 213 | * the structure tear it down. |
| 214 | */ |
Roland Dreier | c259cc2 | 2006-07-14 00:24:23 -0700 | [diff] [blame] | 215 | spin_lock_irqsave(&idp->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | for (new = p; p && p != idp->top; new = p) { |
| 217 | p = p->ary[0]; |
| 218 | new->ary[0] = NULL; |
| 219 | new->bitmap = new->count = 0; |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 220 | __move_to_free_list(idp, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
Roland Dreier | c259cc2 | 2006-07-14 00:24:23 -0700 | [diff] [blame] | 222 | spin_unlock_irqrestore(&idp->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | return -1; |
| 224 | } |
| 225 | new->ary[0] = p; |
| 226 | new->count = 1; |
| 227 | if (p->bitmap == IDR_FULL) |
| 228 | __set_bit(0, &new->bitmap); |
| 229 | p = new; |
| 230 | } |
Nadia Derbey | 3219b3b | 2008-07-25 01:48:00 -0700 | [diff] [blame^] | 231 | rcu_assign_pointer(idp->top, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | idp->layers = layers; |
Tejun Heo | e33ac8b | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 233 | v = sub_alloc(idp, &id, pa); |
Nadia Derbey | 944ca05 | 2008-07-25 01:47:59 -0700 | [diff] [blame] | 234 | if (v == IDR_NEED_TO_GROW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | goto build_up; |
| 236 | return(v); |
| 237 | } |
| 238 | |
Tejun Heo | e33ac8b | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 239 | static int idr_get_new_above_int(struct idr *idp, void *ptr, int starting_id) |
| 240 | { |
| 241 | struct idr_layer *pa[MAX_LEVEL]; |
| 242 | int id; |
| 243 | |
| 244 | id = idr_get_empty_slot(idp, starting_id, pa); |
| 245 | if (id >= 0) { |
| 246 | /* |
| 247 | * Successfully found an empty slot. Install the user |
| 248 | * pointer and mark the slot full. |
| 249 | */ |
Nadia Derbey | 3219b3b | 2008-07-25 01:48:00 -0700 | [diff] [blame^] | 250 | rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], |
| 251 | (struct idr_layer *)ptr); |
Tejun Heo | e33ac8b | 2007-06-14 03:45:12 +0900 | [diff] [blame] | 252 | pa[0]->count++; |
| 253 | idr_mark_full(pa, id); |
| 254 | } |
| 255 | |
| 256 | return id; |
| 257 | } |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | /** |
John McCutchan | 7c657f2 | 2005-08-26 14:02:04 -0400 | [diff] [blame] | 260 | * idr_get_new_above - allocate new idr entry above or equal to a start id |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | * @idp: idr handle |
| 262 | * @ptr: pointer you want associated with the ide |
| 263 | * @start_id: id to start search at |
| 264 | * @id: pointer to the allocated handle |
| 265 | * |
| 266 | * This is the allocate id function. It should be called with any |
| 267 | * required locks. |
| 268 | * |
| 269 | * If memory is required, it will return -EAGAIN, you should unlock |
| 270 | * and go back to the idr_pre_get() call. If the idr is full, it will |
| 271 | * return -ENOSPC. |
| 272 | * |
| 273 | * @id returns a value in the range 0 ... 0x7fffffff |
| 274 | */ |
| 275 | int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id) |
| 276 | { |
| 277 | int rv; |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | rv = idr_get_new_above_int(idp, ptr, starting_id); |
| 280 | /* |
| 281 | * This is a cheap hack until the IDR code can be fixed to |
| 282 | * return proper error values. |
| 283 | */ |
Nadia Derbey | 944ca05 | 2008-07-25 01:47:59 -0700 | [diff] [blame] | 284 | if (rv < 0) |
| 285 | return _idr_rc_to_errno(rv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | *id = rv; |
| 287 | return 0; |
| 288 | } |
| 289 | EXPORT_SYMBOL(idr_get_new_above); |
| 290 | |
| 291 | /** |
| 292 | * idr_get_new - allocate new idr entry |
| 293 | * @idp: idr handle |
| 294 | * @ptr: pointer you want associated with the ide |
| 295 | * @id: pointer to the allocated handle |
| 296 | * |
| 297 | * This is the allocate id function. It should be called with any |
| 298 | * required locks. |
| 299 | * |
| 300 | * If memory is required, it will return -EAGAIN, you should unlock |
| 301 | * and go back to the idr_pre_get() call. If the idr is full, it will |
| 302 | * return -ENOSPC. |
| 303 | * |
| 304 | * @id returns a value in the range 0 ... 0x7fffffff |
| 305 | */ |
| 306 | int idr_get_new(struct idr *idp, void *ptr, int *id) |
| 307 | { |
| 308 | int rv; |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | rv = idr_get_new_above_int(idp, ptr, 0); |
| 311 | /* |
| 312 | * This is a cheap hack until the IDR code can be fixed to |
| 313 | * return proper error values. |
| 314 | */ |
Nadia Derbey | 944ca05 | 2008-07-25 01:47:59 -0700 | [diff] [blame] | 315 | if (rv < 0) |
| 316 | return _idr_rc_to_errno(rv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | *id = rv; |
| 318 | return 0; |
| 319 | } |
| 320 | EXPORT_SYMBOL(idr_get_new); |
| 321 | |
| 322 | static void idr_remove_warning(int id) |
| 323 | { |
Nadia Derbey | f098ad6 | 2008-07-25 01:47:59 -0700 | [diff] [blame] | 324 | printk(KERN_WARNING |
| 325 | "idr_remove called for id=%d which is not allocated.\n", id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | dump_stack(); |
| 327 | } |
| 328 | |
| 329 | static void sub_remove(struct idr *idp, int shift, int id) |
| 330 | { |
| 331 | struct idr_layer *p = idp->top; |
| 332 | struct idr_layer **pa[MAX_LEVEL]; |
| 333 | struct idr_layer ***paa = &pa[0]; |
| 334 | int n; |
| 335 | |
| 336 | *paa = NULL; |
| 337 | *++paa = &idp->top; |
| 338 | |
| 339 | while ((shift > 0) && p) { |
| 340 | n = (id >> shift) & IDR_MASK; |
| 341 | __clear_bit(n, &p->bitmap); |
| 342 | *++paa = &p->ary[n]; |
| 343 | p = p->ary[n]; |
| 344 | shift -= IDR_BITS; |
| 345 | } |
| 346 | n = id & IDR_MASK; |
| 347 | if (likely(p != NULL && test_bit(n, &p->bitmap))){ |
| 348 | __clear_bit(n, &p->bitmap); |
| 349 | p->ary[n] = NULL; |
| 350 | while(*paa && ! --((**paa)->count)){ |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 351 | move_to_free_list(idp, **paa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | **paa-- = NULL; |
| 353 | } |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 354 | if (!*paa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | idp->layers = 0; |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 356 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | idr_remove_warning(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /** |
| 361 | * idr_remove - remove the given id and free it's slot |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 362 | * @idp: idr handle |
| 363 | * @id: unique key |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | */ |
| 365 | void idr_remove(struct idr *idp, int id) |
| 366 | { |
| 367 | struct idr_layer *p; |
| 368 | |
| 369 | /* Mask off upper bits we don't use for the search. */ |
| 370 | id &= MAX_ID_MASK; |
| 371 | |
| 372 | sub_remove(idp, (idp->layers - 1) * IDR_BITS, id); |
Jesper Juhl | e15ae2d | 2005-10-30 15:02:14 -0800 | [diff] [blame] | 373 | if (idp->top && idp->top->count == 1 && (idp->layers > 1) && |
| 374 | idp->top->ary[0]) { // We can drop a layer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | p = idp->top->ary[0]; |
| 377 | idp->top->bitmap = idp->top->count = 0; |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 378 | move_to_free_list(idp, idp->top); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | idp->top = p; |
| 380 | --idp->layers; |
| 381 | } |
| 382 | while (idp->id_free_cnt >= IDR_FREE_MAX) { |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 383 | p = get_from_free_list(idp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | kmem_cache_free(idr_layer_cache, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
Nadia Derbey | af8e2a4 | 2008-05-01 04:34:57 -0700 | [diff] [blame] | 386 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | EXPORT_SYMBOL(idr_remove); |
| 389 | |
| 390 | /** |
Kristian Hoegsberg | 23936cc | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 391 | * idr_remove_all - remove all ids from the given idr tree |
| 392 | * @idp: idr handle |
| 393 | * |
| 394 | * idr_destroy() only frees up unused, cached idp_layers, but this |
| 395 | * function will remove all id mappings and leave all idp_layers |
| 396 | * unused. |
| 397 | * |
| 398 | * A typical clean-up sequence for objects stored in an idr tree, will |
| 399 | * use idr_for_each() to free all objects, if necessay, then |
| 400 | * idr_remove_all() to remove all ids, and idr_destroy() to free |
| 401 | * up the cached idr_layers. |
| 402 | */ |
| 403 | void idr_remove_all(struct idr *idp) |
| 404 | { |
Oleg Nesterov | 6ace06dc | 2007-07-31 00:39:19 -0700 | [diff] [blame] | 405 | int n, id, max; |
Kristian Hoegsberg | 23936cc | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 406 | struct idr_layer *p; |
| 407 | struct idr_layer *pa[MAX_LEVEL]; |
| 408 | struct idr_layer **paa = &pa[0]; |
| 409 | |
| 410 | n = idp->layers * IDR_BITS; |
| 411 | p = idp->top; |
| 412 | max = 1 << n; |
| 413 | |
| 414 | id = 0; |
Oleg Nesterov | 6ace06dc | 2007-07-31 00:39:19 -0700 | [diff] [blame] | 415 | while (id < max) { |
Kristian Hoegsberg | 23936cc | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 416 | while (n > IDR_BITS && p) { |
| 417 | n -= IDR_BITS; |
| 418 | *paa++ = p; |
| 419 | p = p->ary[(id >> n) & IDR_MASK]; |
| 420 | } |
| 421 | |
| 422 | id += 1 << n; |
| 423 | while (n < fls(id)) { |
| 424 | if (p) { |
| 425 | memset(p, 0, sizeof *p); |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 426 | move_to_free_list(idp, p); |
Kristian Hoegsberg | 23936cc | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 427 | } |
| 428 | n += IDR_BITS; |
| 429 | p = *--paa; |
| 430 | } |
| 431 | } |
| 432 | idp->top = NULL; |
| 433 | idp->layers = 0; |
| 434 | } |
| 435 | EXPORT_SYMBOL(idr_remove_all); |
| 436 | |
| 437 | /** |
Andrew Morton | 8d3b359 | 2005-10-23 12:57:18 -0700 | [diff] [blame] | 438 | * idr_destroy - release all cached layers within an idr tree |
| 439 | * idp: idr handle |
| 440 | */ |
| 441 | void idr_destroy(struct idr *idp) |
| 442 | { |
| 443 | while (idp->id_free_cnt) { |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 444 | struct idr_layer *p = get_from_free_list(idp); |
Andrew Morton | 8d3b359 | 2005-10-23 12:57:18 -0700 | [diff] [blame] | 445 | kmem_cache_free(idr_layer_cache, p); |
| 446 | } |
| 447 | } |
| 448 | EXPORT_SYMBOL(idr_destroy); |
| 449 | |
| 450 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | * idr_find - return pointer for given id |
| 452 | * @idp: idr handle |
| 453 | * @id: lookup key |
| 454 | * |
| 455 | * Return the pointer given the id it has been registered with. A %NULL |
| 456 | * return indicates that @id is not valid or you passed %NULL in |
| 457 | * idr_get_new(). |
| 458 | * |
| 459 | * The caller must serialize idr_find() vs idr_get_new() and idr_remove(). |
| 460 | */ |
| 461 | void *idr_find(struct idr *idp, int id) |
| 462 | { |
| 463 | int n; |
| 464 | struct idr_layer *p; |
| 465 | |
| 466 | n = idp->layers * IDR_BITS; |
| 467 | p = idp->top; |
| 468 | |
| 469 | /* Mask off upper bits we don't use for the search. */ |
| 470 | id &= MAX_ID_MASK; |
| 471 | |
| 472 | if (id >= (1 << n)) |
| 473 | return NULL; |
| 474 | |
| 475 | while (n > 0 && p) { |
| 476 | n -= IDR_BITS; |
| 477 | p = p->ary[(id >> n) & IDR_MASK]; |
| 478 | } |
| 479 | return((void *)p); |
| 480 | } |
| 481 | EXPORT_SYMBOL(idr_find); |
| 482 | |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 483 | /** |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 484 | * idr_for_each - iterate through all stored pointers |
| 485 | * @idp: idr handle |
| 486 | * @fn: function to be called for each pointer |
| 487 | * @data: data passed back to callback function |
| 488 | * |
| 489 | * Iterate over the pointers registered with the given idr. The |
| 490 | * callback function will be called for each pointer currently |
| 491 | * registered, passing the id, the pointer and the data pointer passed |
| 492 | * to this function. It is not safe to modify the idr tree while in |
| 493 | * the callback, so functions such as idr_get_new and idr_remove are |
| 494 | * not allowed. |
| 495 | * |
| 496 | * We check the return of @fn each time. If it returns anything other |
| 497 | * than 0, we break out and return that value. |
| 498 | * |
| 499 | * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove(). |
| 500 | */ |
| 501 | int idr_for_each(struct idr *idp, |
| 502 | int (*fn)(int id, void *p, void *data), void *data) |
| 503 | { |
| 504 | int n, id, max, error = 0; |
| 505 | struct idr_layer *p; |
| 506 | struct idr_layer *pa[MAX_LEVEL]; |
| 507 | struct idr_layer **paa = &pa[0]; |
| 508 | |
| 509 | n = idp->layers * IDR_BITS; |
| 510 | p = idp->top; |
| 511 | max = 1 << n; |
| 512 | |
| 513 | id = 0; |
| 514 | while (id < max) { |
| 515 | while (n > 0 && p) { |
| 516 | n -= IDR_BITS; |
| 517 | *paa++ = p; |
| 518 | p = p->ary[(id >> n) & IDR_MASK]; |
| 519 | } |
| 520 | |
| 521 | if (p) { |
| 522 | error = fn(id, (void *)p, data); |
| 523 | if (error) |
| 524 | break; |
| 525 | } |
| 526 | |
| 527 | id += 1 << n; |
| 528 | while (n < fls(id)) { |
| 529 | n += IDR_BITS; |
| 530 | p = *--paa; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return error; |
| 535 | } |
| 536 | EXPORT_SYMBOL(idr_for_each); |
| 537 | |
| 538 | /** |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 539 | * idr_replace - replace pointer for given id |
| 540 | * @idp: idr handle |
| 541 | * @ptr: pointer you want associated with the id |
| 542 | * @id: lookup key |
| 543 | * |
| 544 | * Replace the pointer registered with an id and return the old value. |
| 545 | * A -ENOENT return indicates that @id was not found. |
| 546 | * A -EINVAL return indicates that @id was not within valid constraints. |
| 547 | * |
| 548 | * The caller must serialize vs idr_find(), idr_get_new(), and idr_remove(). |
| 549 | */ |
| 550 | void *idr_replace(struct idr *idp, void *ptr, int id) |
| 551 | { |
| 552 | int n; |
| 553 | struct idr_layer *p, *old_p; |
| 554 | |
| 555 | n = idp->layers * IDR_BITS; |
| 556 | p = idp->top; |
| 557 | |
| 558 | id &= MAX_ID_MASK; |
| 559 | |
| 560 | if (id >= (1 << n)) |
| 561 | return ERR_PTR(-EINVAL); |
| 562 | |
| 563 | n -= IDR_BITS; |
| 564 | while ((n > 0) && p) { |
| 565 | p = p->ary[(id >> n) & IDR_MASK]; |
| 566 | n -= IDR_BITS; |
| 567 | } |
| 568 | |
| 569 | n = id & IDR_MASK; |
| 570 | if (unlikely(p == NULL || !test_bit(n, &p->bitmap))) |
| 571 | return ERR_PTR(-ENOENT); |
| 572 | |
| 573 | old_p = p->ary[n]; |
| 574 | p->ary[n] = ptr; |
| 575 | |
| 576 | return old_p; |
| 577 | } |
| 578 | EXPORT_SYMBOL(idr_replace); |
| 579 | |
Christoph Lameter | 4ba9b9d | 2007-10-16 23:25:51 -0700 | [diff] [blame] | 580 | static void idr_cache_ctor(struct kmem_cache *idr_layer_cache, void *idr_layer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
| 582 | memset(idr_layer, 0, sizeof(struct idr_layer)); |
| 583 | } |
| 584 | |
Akinobu Mita | 199f0ca | 2008-04-29 01:03:13 -0700 | [diff] [blame] | 585 | void __init idr_init_cache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | { |
Akinobu Mita | 199f0ca | 2008-04-29 01:03:13 -0700 | [diff] [blame] | 587 | idr_layer_cache = kmem_cache_create("idr_layer_cache", |
| 588 | sizeof(struct idr_layer), 0, SLAB_PANIC, |
| 589 | idr_cache_ctor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /** |
| 593 | * idr_init - initialize idr handle |
| 594 | * @idp: idr handle |
| 595 | * |
| 596 | * This function is use to set up the handle (@idp) that you will pass |
| 597 | * to the rest of the functions. |
| 598 | */ |
| 599 | void idr_init(struct idr *idp) |
| 600 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | memset(idp, 0, sizeof(struct idr)); |
| 602 | spin_lock_init(&idp->lock); |
| 603 | } |
| 604 | EXPORT_SYMBOL(idr_init); |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 605 | |
| 606 | |
| 607 | /* |
| 608 | * IDA - IDR based ID allocator |
| 609 | * |
| 610 | * this is id allocator without id -> pointer translation. Memory |
| 611 | * usage is much lower than full blown idr because each id only |
| 612 | * occupies a bit. ida uses a custom leaf node which contains |
| 613 | * IDA_BITMAP_BITS slots. |
| 614 | * |
| 615 | * 2007-04-25 written by Tejun Heo <htejun@gmail.com> |
| 616 | */ |
| 617 | |
| 618 | static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap) |
| 619 | { |
| 620 | unsigned long flags; |
| 621 | |
| 622 | if (!ida->free_bitmap) { |
| 623 | spin_lock_irqsave(&ida->idr.lock, flags); |
| 624 | if (!ida->free_bitmap) { |
| 625 | ida->free_bitmap = bitmap; |
| 626 | bitmap = NULL; |
| 627 | } |
| 628 | spin_unlock_irqrestore(&ida->idr.lock, flags); |
| 629 | } |
| 630 | |
| 631 | kfree(bitmap); |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * ida_pre_get - reserve resources for ida allocation |
| 636 | * @ida: ida handle |
| 637 | * @gfp_mask: memory allocation flag |
| 638 | * |
| 639 | * This function should be called prior to locking and calling the |
| 640 | * following function. It preallocates enough memory to satisfy the |
| 641 | * worst possible allocation. |
| 642 | * |
| 643 | * If the system is REALLY out of memory this function returns 0, |
| 644 | * otherwise 1. |
| 645 | */ |
| 646 | int ida_pre_get(struct ida *ida, gfp_t gfp_mask) |
| 647 | { |
| 648 | /* allocate idr_layers */ |
| 649 | if (!idr_pre_get(&ida->idr, gfp_mask)) |
| 650 | return 0; |
| 651 | |
| 652 | /* allocate free_bitmap */ |
| 653 | if (!ida->free_bitmap) { |
| 654 | struct ida_bitmap *bitmap; |
| 655 | |
| 656 | bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask); |
| 657 | if (!bitmap) |
| 658 | return 0; |
| 659 | |
| 660 | free_bitmap(ida, bitmap); |
| 661 | } |
| 662 | |
| 663 | return 1; |
| 664 | } |
| 665 | EXPORT_SYMBOL(ida_pre_get); |
| 666 | |
| 667 | /** |
| 668 | * ida_get_new_above - allocate new ID above or equal to a start id |
| 669 | * @ida: ida handle |
| 670 | * @staring_id: id to start search at |
| 671 | * @p_id: pointer to the allocated handle |
| 672 | * |
| 673 | * Allocate new ID above or equal to @ida. It should be called with |
| 674 | * any required locks. |
| 675 | * |
| 676 | * If memory is required, it will return -EAGAIN, you should unlock |
| 677 | * and go back to the ida_pre_get() call. If the ida is full, it will |
| 678 | * return -ENOSPC. |
| 679 | * |
| 680 | * @p_id returns a value in the range 0 ... 0x7fffffff. |
| 681 | */ |
| 682 | int ida_get_new_above(struct ida *ida, int starting_id, int *p_id) |
| 683 | { |
| 684 | struct idr_layer *pa[MAX_LEVEL]; |
| 685 | struct ida_bitmap *bitmap; |
| 686 | unsigned long flags; |
| 687 | int idr_id = starting_id / IDA_BITMAP_BITS; |
| 688 | int offset = starting_id % IDA_BITMAP_BITS; |
| 689 | int t, id; |
| 690 | |
| 691 | restart: |
| 692 | /* get vacant slot */ |
| 693 | t = idr_get_empty_slot(&ida->idr, idr_id, pa); |
Nadia Derbey | 944ca05 | 2008-07-25 01:47:59 -0700 | [diff] [blame] | 694 | if (t < 0) |
| 695 | return _idr_rc_to_errno(t); |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 696 | |
| 697 | if (t * IDA_BITMAP_BITS >= MAX_ID_BIT) |
| 698 | return -ENOSPC; |
| 699 | |
| 700 | if (t != idr_id) |
| 701 | offset = 0; |
| 702 | idr_id = t; |
| 703 | |
| 704 | /* if bitmap isn't there, create a new one */ |
| 705 | bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK]; |
| 706 | if (!bitmap) { |
| 707 | spin_lock_irqsave(&ida->idr.lock, flags); |
| 708 | bitmap = ida->free_bitmap; |
| 709 | ida->free_bitmap = NULL; |
| 710 | spin_unlock_irqrestore(&ida->idr.lock, flags); |
| 711 | |
| 712 | if (!bitmap) |
| 713 | return -EAGAIN; |
| 714 | |
| 715 | memset(bitmap, 0, sizeof(struct ida_bitmap)); |
Nadia Derbey | 3219b3b | 2008-07-25 01:48:00 -0700 | [diff] [blame^] | 716 | rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK], |
| 717 | (void *)bitmap); |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 718 | pa[0]->count++; |
| 719 | } |
| 720 | |
| 721 | /* lookup for empty slot */ |
| 722 | t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset); |
| 723 | if (t == IDA_BITMAP_BITS) { |
| 724 | /* no empty slot after offset, continue to the next chunk */ |
| 725 | idr_id++; |
| 726 | offset = 0; |
| 727 | goto restart; |
| 728 | } |
| 729 | |
| 730 | id = idr_id * IDA_BITMAP_BITS + t; |
| 731 | if (id >= MAX_ID_BIT) |
| 732 | return -ENOSPC; |
| 733 | |
| 734 | __set_bit(t, bitmap->bitmap); |
| 735 | if (++bitmap->nr_busy == IDA_BITMAP_BITS) |
| 736 | idr_mark_full(pa, idr_id); |
| 737 | |
| 738 | *p_id = id; |
| 739 | |
| 740 | /* Each leaf node can handle nearly a thousand slots and the |
| 741 | * whole idea of ida is to have small memory foot print. |
| 742 | * Throw away extra resources one by one after each successful |
| 743 | * allocation. |
| 744 | */ |
| 745 | if (ida->idr.id_free_cnt || ida->free_bitmap) { |
Nadia Derbey | 4ae5378 | 2008-07-25 01:47:58 -0700 | [diff] [blame] | 746 | struct idr_layer *p = get_from_free_list(&ida->idr); |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 747 | if (p) |
| 748 | kmem_cache_free(idr_layer_cache, p); |
| 749 | } |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | EXPORT_SYMBOL(ida_get_new_above); |
| 754 | |
| 755 | /** |
| 756 | * ida_get_new - allocate new ID |
| 757 | * @ida: idr handle |
| 758 | * @p_id: pointer to the allocated handle |
| 759 | * |
| 760 | * Allocate new ID. It should be called with any required locks. |
| 761 | * |
| 762 | * If memory is required, it will return -EAGAIN, you should unlock |
| 763 | * and go back to the idr_pre_get() call. If the idr is full, it will |
| 764 | * return -ENOSPC. |
| 765 | * |
| 766 | * @id returns a value in the range 0 ... 0x7fffffff. |
| 767 | */ |
| 768 | int ida_get_new(struct ida *ida, int *p_id) |
| 769 | { |
| 770 | return ida_get_new_above(ida, 0, p_id); |
| 771 | } |
| 772 | EXPORT_SYMBOL(ida_get_new); |
| 773 | |
| 774 | /** |
| 775 | * ida_remove - remove the given ID |
| 776 | * @ida: ida handle |
| 777 | * @id: ID to free |
| 778 | */ |
| 779 | void ida_remove(struct ida *ida, int id) |
| 780 | { |
| 781 | struct idr_layer *p = ida->idr.top; |
| 782 | int shift = (ida->idr.layers - 1) * IDR_BITS; |
| 783 | int idr_id = id / IDA_BITMAP_BITS; |
| 784 | int offset = id % IDA_BITMAP_BITS; |
| 785 | int n; |
| 786 | struct ida_bitmap *bitmap; |
| 787 | |
| 788 | /* clear full bits while looking up the leaf idr_layer */ |
| 789 | while ((shift > 0) && p) { |
| 790 | n = (idr_id >> shift) & IDR_MASK; |
| 791 | __clear_bit(n, &p->bitmap); |
| 792 | p = p->ary[n]; |
| 793 | shift -= IDR_BITS; |
| 794 | } |
| 795 | |
| 796 | if (p == NULL) |
| 797 | goto err; |
| 798 | |
| 799 | n = idr_id & IDR_MASK; |
| 800 | __clear_bit(n, &p->bitmap); |
| 801 | |
| 802 | bitmap = (void *)p->ary[n]; |
| 803 | if (!test_bit(offset, bitmap->bitmap)) |
| 804 | goto err; |
| 805 | |
| 806 | /* update bitmap and remove it if empty */ |
| 807 | __clear_bit(offset, bitmap->bitmap); |
| 808 | if (--bitmap->nr_busy == 0) { |
| 809 | __set_bit(n, &p->bitmap); /* to please idr_remove() */ |
| 810 | idr_remove(&ida->idr, idr_id); |
| 811 | free_bitmap(ida, bitmap); |
| 812 | } |
| 813 | |
| 814 | return; |
| 815 | |
| 816 | err: |
| 817 | printk(KERN_WARNING |
| 818 | "ida_remove called for id=%d which is not allocated.\n", id); |
| 819 | } |
| 820 | EXPORT_SYMBOL(ida_remove); |
| 821 | |
| 822 | /** |
| 823 | * ida_destroy - release all cached layers within an ida tree |
| 824 | * ida: ida handle |
| 825 | */ |
| 826 | void ida_destroy(struct ida *ida) |
| 827 | { |
| 828 | idr_destroy(&ida->idr); |
| 829 | kfree(ida->free_bitmap); |
| 830 | } |
| 831 | EXPORT_SYMBOL(ida_destroy); |
| 832 | |
| 833 | /** |
| 834 | * ida_init - initialize ida handle |
| 835 | * @ida: ida handle |
| 836 | * |
| 837 | * This function is use to set up the handle (@ida) that you will pass |
| 838 | * to the rest of the functions. |
| 839 | */ |
| 840 | void ida_init(struct ida *ida) |
| 841 | { |
| 842 | memset(ida, 0, sizeof(struct ida)); |
| 843 | idr_init(&ida->idr); |
| 844 | |
| 845 | } |
| 846 | EXPORT_SYMBOL(ida_init); |