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