Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001 Momchil Velikov |
| 3 | * Portions Copyright (C) 2001 Christoph Hellwig |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 4 | * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2, or (at |
| 9 | * your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/radix-tree.h> |
| 26 | #include <linux/percpu.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/notifier.h> |
| 29 | #include <linux/cpu.h> |
| 30 | #include <linux/gfp.h> |
| 31 | #include <linux/string.h> |
| 32 | #include <linux/bitops.h> |
| 33 | |
| 34 | |
| 35 | #ifdef __KERNEL__ |
| 36 | #define RADIX_TREE_MAP_SHIFT 6 |
| 37 | #else |
| 38 | #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */ |
| 39 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT) |
| 42 | #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1) |
| 43 | |
| 44 | #define RADIX_TREE_TAG_LONGS \ |
| 45 | ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG) |
| 46 | |
| 47 | struct radix_tree_node { |
| 48 | unsigned int count; |
| 49 | void *slots[RADIX_TREE_MAP_SIZE]; |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 50 | unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | struct radix_tree_path { |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 54 | struct radix_tree_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | int offset; |
| 56 | }; |
| 57 | |
| 58 | #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long)) |
| 59 | #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2) |
| 60 | |
Christoph Lameter | 6c03652 | 2005-07-07 17:56:59 -0700 | [diff] [blame] | 61 | static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * Radix tree node cache. |
| 65 | */ |
| 66 | static kmem_cache_t *radix_tree_node_cachep; |
| 67 | |
| 68 | /* |
| 69 | * Per-cpu pool of preloaded nodes |
| 70 | */ |
| 71 | struct radix_tree_preload { |
| 72 | int nr; |
| 73 | struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH]; |
| 74 | }; |
| 75 | DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, }; |
| 76 | |
| 77 | /* |
| 78 | * This assumes that the caller has performed appropriate preallocation, and |
| 79 | * that the caller has pinned this thread of control to the current CPU. |
| 80 | */ |
| 81 | static struct radix_tree_node * |
| 82 | radix_tree_node_alloc(struct radix_tree_root *root) |
| 83 | { |
| 84 | struct radix_tree_node *ret; |
| 85 | |
| 86 | ret = kmem_cache_alloc(radix_tree_node_cachep, root->gfp_mask); |
| 87 | if (ret == NULL && !(root->gfp_mask & __GFP_WAIT)) { |
| 88 | struct radix_tree_preload *rtp; |
| 89 | |
| 90 | rtp = &__get_cpu_var(radix_tree_preloads); |
| 91 | if (rtp->nr) { |
| 92 | ret = rtp->nodes[rtp->nr - 1]; |
| 93 | rtp->nodes[rtp->nr - 1] = NULL; |
| 94 | rtp->nr--; |
| 95 | } |
| 96 | } |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static inline void |
| 101 | radix_tree_node_free(struct radix_tree_node *node) |
| 102 | { |
| 103 | kmem_cache_free(radix_tree_node_cachep, node); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Load up this CPU's radix_tree_node buffer with sufficient objects to |
| 108 | * ensure that the addition of a single element in the tree cannot fail. On |
| 109 | * success, return zero, with preemption disabled. On error, return -ENOMEM |
| 110 | * with preemption not disabled. |
| 111 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 112 | int radix_tree_preload(gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
| 114 | struct radix_tree_preload *rtp; |
| 115 | struct radix_tree_node *node; |
| 116 | int ret = -ENOMEM; |
| 117 | |
| 118 | preempt_disable(); |
| 119 | rtp = &__get_cpu_var(radix_tree_preloads); |
| 120 | while (rtp->nr < ARRAY_SIZE(rtp->nodes)) { |
| 121 | preempt_enable(); |
| 122 | node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask); |
| 123 | if (node == NULL) |
| 124 | goto out; |
| 125 | preempt_disable(); |
| 126 | rtp = &__get_cpu_var(radix_tree_preloads); |
| 127 | if (rtp->nr < ARRAY_SIZE(rtp->nodes)) |
| 128 | rtp->nodes[rtp->nr++] = node; |
| 129 | else |
| 130 | kmem_cache_free(radix_tree_node_cachep, node); |
| 131 | } |
| 132 | ret = 0; |
| 133 | out: |
| 134 | return ret; |
| 135 | } |
| 136 | |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 137 | static inline void tag_set(struct radix_tree_node *node, unsigned int tag, |
| 138 | int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 140 | __set_bit(offset, node->tags[tag]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 143 | static inline void tag_clear(struct radix_tree_node *node, unsigned int tag, |
| 144 | int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 146 | __clear_bit(offset, node->tags[tag]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 149 | static inline int tag_get(struct radix_tree_node *node, unsigned int tag, |
| 150 | int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 152 | return test_bit(offset, node->tags[tag]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
Nick Piggin | 6e954b9 | 2006-01-08 01:01:40 -0800 | [diff] [blame] | 156 | * Returns 1 if any slot in the node has this tag set. |
| 157 | * Otherwise returns 0. |
| 158 | */ |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 159 | static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag) |
Nick Piggin | 6e954b9 | 2006-01-08 01:01:40 -0800 | [diff] [blame] | 160 | { |
| 161 | int idx; |
| 162 | for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) { |
| 163 | if (node->tags[tag][idx]) |
| 164 | return 1; |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | * Return the maximum key which can be store into a |
| 171 | * radix tree with height HEIGHT. |
| 172 | */ |
| 173 | static inline unsigned long radix_tree_maxindex(unsigned int height) |
| 174 | { |
| 175 | return height_to_maxindex[height]; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Extend a radix tree so it can store key @index. |
| 180 | */ |
| 181 | static int radix_tree_extend(struct radix_tree_root *root, unsigned long index) |
| 182 | { |
| 183 | struct radix_tree_node *node; |
| 184 | unsigned int height; |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 185 | char tags[RADIX_TREE_MAX_TAGS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | int tag; |
| 187 | |
| 188 | /* Figure out what the height should be. */ |
| 189 | height = root->height + 1; |
| 190 | while (index > radix_tree_maxindex(height)) |
| 191 | height++; |
| 192 | |
| 193 | if (root->rnode == NULL) { |
| 194 | root->height = height; |
| 195 | goto out; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Prepare the tag status of the top-level node for propagation |
| 200 | * into the newly-pushed top-level node(s) |
| 201 | */ |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 202 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | tags[tag] = 0; |
Nick Piggin | 6e954b9 | 2006-01-08 01:01:40 -0800 | [diff] [blame] | 204 | if (any_tag_set(root->rnode, tag)) |
| 205 | tags[tag] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | do { |
| 209 | if (!(node = radix_tree_node_alloc(root))) |
| 210 | return -ENOMEM; |
| 211 | |
| 212 | /* Increase the height. */ |
| 213 | node->slots[0] = root->rnode; |
| 214 | |
| 215 | /* Propagate the aggregated tag info into the new root */ |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 216 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (tags[tag]) |
| 218 | tag_set(node, tag, 0); |
| 219 | } |
| 220 | |
| 221 | node->count = 1; |
| 222 | root->rnode = node; |
| 223 | root->height++; |
| 224 | } while (height > root->height); |
| 225 | out: |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * radix_tree_insert - insert into a radix tree |
| 231 | * @root: radix tree root |
| 232 | * @index: index key |
| 233 | * @item: item to insert |
| 234 | * |
| 235 | * Insert an item into the radix tree at position @index. |
| 236 | */ |
| 237 | int radix_tree_insert(struct radix_tree_root *root, |
| 238 | unsigned long index, void *item) |
| 239 | { |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 240 | struct radix_tree_node *node = NULL, *slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | unsigned int height, shift; |
| 242 | int offset; |
| 243 | int error; |
| 244 | |
| 245 | /* Make sure the tree is high enough. */ |
| 246 | if ((!index && !root->rnode) || |
| 247 | index > radix_tree_maxindex(root->height)) { |
| 248 | error = radix_tree_extend(root, index); |
| 249 | if (error) |
| 250 | return error; |
| 251 | } |
| 252 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 253 | slot = root->rnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | height = root->height; |
| 255 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; |
| 256 | |
| 257 | offset = 0; /* uninitialised var warning */ |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 258 | do { |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 259 | if (slot == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | /* Have to add a child node. */ |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 261 | if (!(slot = radix_tree_node_alloc(root))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return -ENOMEM; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 263 | if (node) { |
| 264 | node->slots[offset] = slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | node->count++; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 266 | } else |
| 267 | root->rnode = slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* Go a level down */ |
| 271 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 272 | node = slot; |
| 273 | slot = node->slots[offset]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | shift -= RADIX_TREE_MAP_SHIFT; |
| 275 | height--; |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 276 | } while (height > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 278 | if (slot != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | return -EEXIST; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 280 | |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 281 | BUG_ON(!node); |
| 282 | node->count++; |
| 283 | node->slots[offset] = item; |
| 284 | BUG_ON(tag_get(node, 0, offset)); |
| 285 | BUG_ON(tag_get(node, 1, offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | EXPORT_SYMBOL(radix_tree_insert); |
| 290 | |
Hans Reiser | a433136 | 2005-11-07 00:59:29 -0800 | [diff] [blame] | 291 | static inline void **__lookup_slot(struct radix_tree_root *root, |
| 292 | unsigned long index) |
| 293 | { |
| 294 | unsigned int height, shift; |
| 295 | struct radix_tree_node **slot; |
| 296 | |
| 297 | height = root->height; |
| 298 | if (index > radix_tree_maxindex(height)) |
| 299 | return NULL; |
| 300 | |
| 301 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; |
| 302 | slot = &root->rnode; |
| 303 | |
| 304 | while (height > 0) { |
| 305 | if (*slot == NULL) |
| 306 | return NULL; |
| 307 | |
| 308 | slot = (struct radix_tree_node **) |
| 309 | ((*slot)->slots + |
| 310 | ((index >> shift) & RADIX_TREE_MAP_MASK)); |
| 311 | shift -= RADIX_TREE_MAP_SHIFT; |
| 312 | height--; |
| 313 | } |
| 314 | |
| 315 | return (void **)slot; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * radix_tree_lookup_slot - lookup a slot in a radix tree |
| 320 | * @root: radix tree root |
| 321 | * @index: index key |
| 322 | * |
| 323 | * Lookup the slot corresponding to the position @index in the radix tree |
| 324 | * @root. This is useful for update-if-exists operations. |
| 325 | */ |
| 326 | void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index) |
| 327 | { |
| 328 | return __lookup_slot(root, index); |
| 329 | } |
| 330 | EXPORT_SYMBOL(radix_tree_lookup_slot); |
| 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | /** |
| 333 | * radix_tree_lookup - perform lookup operation on a radix tree |
| 334 | * @root: radix tree root |
| 335 | * @index: index key |
| 336 | * |
| 337 | * Lookup the item at the position @index in the radix tree @root. |
| 338 | */ |
| 339 | void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index) |
| 340 | { |
Hans Reiser | a433136 | 2005-11-07 00:59:29 -0800 | [diff] [blame] | 341 | void **slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Hans Reiser | a433136 | 2005-11-07 00:59:29 -0800 | [diff] [blame] | 343 | slot = __lookup_slot(root, index); |
| 344 | return slot != NULL ? *slot : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
| 346 | EXPORT_SYMBOL(radix_tree_lookup); |
| 347 | |
| 348 | /** |
| 349 | * radix_tree_tag_set - set a tag on a radix tree node |
| 350 | * @root: radix tree root |
| 351 | * @index: index key |
| 352 | * @tag: tag index |
| 353 | * |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 354 | * Set the search tag (which must be < RADIX_TREE_MAX_TAGS) |
| 355 | * corresponding to @index in the radix tree. From |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | * the root all the way down to the leaf node. |
| 357 | * |
| 358 | * Returns the address of the tagged item. Setting a tag on a not-present |
| 359 | * item is a bug. |
| 360 | */ |
| 361 | void *radix_tree_tag_set(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 362 | unsigned long index, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
| 364 | unsigned int height, shift; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 365 | struct radix_tree_node *slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
| 367 | height = root->height; |
| 368 | if (index > radix_tree_maxindex(height)) |
| 369 | return NULL; |
| 370 | |
| 371 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 372 | slot = root->rnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | while (height > 0) { |
| 375 | int offset; |
| 376 | |
| 377 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 378 | if (!tag_get(slot, tag, offset)) |
| 379 | tag_set(slot, tag, offset); |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 380 | slot = slot->slots[offset]; |
| 381 | BUG_ON(slot == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | shift -= RADIX_TREE_MAP_SHIFT; |
| 383 | height--; |
| 384 | } |
| 385 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 386 | return slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | EXPORT_SYMBOL(radix_tree_tag_set); |
| 389 | |
| 390 | /** |
| 391 | * radix_tree_tag_clear - clear a tag on a radix tree node |
| 392 | * @root: radix tree root |
| 393 | * @index: index key |
| 394 | * @tag: tag index |
| 395 | * |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 396 | * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS) |
| 397 | * corresponding to @index in the radix tree. If |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | * this causes the leaf node to have no tags set then clear the tag in the |
| 399 | * next-to-leaf node, etc. |
| 400 | * |
| 401 | * Returns the address of the tagged item on success, else NULL. ie: |
| 402 | * has the same return value and semantics as radix_tree_lookup(). |
| 403 | */ |
| 404 | void *radix_tree_tag_clear(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 405 | unsigned long index, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
| 407 | struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 408 | struct radix_tree_node *slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | unsigned int height, shift; |
| 410 | void *ret = NULL; |
| 411 | |
| 412 | height = root->height; |
| 413 | if (index > radix_tree_maxindex(height)) |
| 414 | goto out; |
| 415 | |
| 416 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; |
| 417 | pathp->node = NULL; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 418 | slot = root->rnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | while (height > 0) { |
| 421 | int offset; |
| 422 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 423 | if (slot == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | goto out; |
| 425 | |
| 426 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; |
| 427 | pathp[1].offset = offset; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 428 | pathp[1].node = slot; |
| 429 | slot = slot->slots[offset]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | pathp++; |
| 431 | shift -= RADIX_TREE_MAP_SHIFT; |
| 432 | height--; |
| 433 | } |
| 434 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 435 | ret = slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | if (ret == NULL) |
| 437 | goto out; |
| 438 | |
| 439 | do { |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 440 | if (!tag_get(pathp->node, tag, pathp->offset)) |
| 441 | goto out; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 442 | tag_clear(pathp->node, tag, pathp->offset); |
Nick Piggin | 6e954b9 | 2006-01-08 01:01:40 -0800 | [diff] [blame] | 443 | if (any_tag_set(pathp->node, tag)) |
| 444 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | pathp--; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 446 | } while (pathp->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | out: |
| 448 | return ret; |
| 449 | } |
| 450 | EXPORT_SYMBOL(radix_tree_tag_clear); |
| 451 | |
| 452 | #ifndef __KERNEL__ /* Only the test harness uses this at present */ |
| 453 | /** |
Marcelo Tosatti | 32605a1 | 2005-09-06 15:16:48 -0700 | [diff] [blame] | 454 | * radix_tree_tag_get - get a tag on a radix tree node |
| 455 | * @root: radix tree root |
| 456 | * @index: index key |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 457 | * @tag: tag index (< RADIX_TREE_MAX_TAGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | * |
Marcelo Tosatti | 32605a1 | 2005-09-06 15:16:48 -0700 | [diff] [blame] | 459 | * Return values: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | * |
Marcelo Tosatti | 32605a1 | 2005-09-06 15:16:48 -0700 | [diff] [blame] | 461 | * 0: tag not present |
| 462 | * 1: tag present, set |
| 463 | * -1: tag present, unset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | */ |
| 465 | int radix_tree_tag_get(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 466 | unsigned long index, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
| 468 | unsigned int height, shift; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 469 | struct radix_tree_node *slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | int saw_unset_tag = 0; |
| 471 | |
| 472 | height = root->height; |
| 473 | if (index > radix_tree_maxindex(height)) |
| 474 | return 0; |
| 475 | |
| 476 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 477 | slot = root->rnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | for ( ; ; ) { |
| 480 | int offset; |
| 481 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 482 | if (slot == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | return 0; |
| 484 | |
| 485 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; |
| 486 | |
| 487 | /* |
| 488 | * This is just a debug check. Later, we can bale as soon as |
| 489 | * we see an unset tag. |
| 490 | */ |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 491 | if (!tag_get(slot, tag, offset)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | saw_unset_tag = 1; |
| 493 | if (height == 1) { |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 494 | int ret = tag_get(slot, tag, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | BUG_ON(ret && saw_unset_tag); |
Marcelo Tosatti | 32605a1 | 2005-09-06 15:16:48 -0700 | [diff] [blame] | 497 | return ret ? 1 : -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 499 | slot = slot->slots[offset]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | shift -= RADIX_TREE_MAP_SHIFT; |
| 501 | height--; |
| 502 | } |
| 503 | } |
| 504 | EXPORT_SYMBOL(radix_tree_tag_get); |
| 505 | #endif |
| 506 | |
| 507 | static unsigned int |
| 508 | __lookup(struct radix_tree_root *root, void **results, unsigned long index, |
| 509 | unsigned int max_items, unsigned long *next_index) |
| 510 | { |
| 511 | unsigned int nr_found = 0; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 512 | unsigned int shift, height; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | struct radix_tree_node *slot; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 514 | unsigned long i; |
| 515 | |
| 516 | height = root->height; |
| 517 | if (height == 0) |
| 518 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
| 520 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; |
| 521 | slot = root->rnode; |
| 522 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 523 | for ( ; height > 1; height--) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 525 | for (i = (index >> shift) & RADIX_TREE_MAP_MASK ; |
| 526 | i < RADIX_TREE_MAP_SIZE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | if (slot->slots[i] != NULL) |
| 528 | break; |
| 529 | index &= ~((1UL << shift) - 1); |
| 530 | index += 1UL << shift; |
| 531 | if (index == 0) |
| 532 | goto out; /* 32-bit wraparound */ |
| 533 | } |
| 534 | if (i == RADIX_TREE_MAP_SIZE) |
| 535 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | shift -= RADIX_TREE_MAP_SHIFT; |
| 538 | slot = slot->slots[i]; |
| 539 | } |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 540 | |
| 541 | /* Bottom level: grab some items */ |
| 542 | for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) { |
| 543 | index++; |
| 544 | if (slot->slots[i]) { |
| 545 | results[nr_found++] = slot->slots[i]; |
| 546 | if (nr_found == max_items) |
| 547 | goto out; |
| 548 | } |
| 549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | out: |
| 551 | *next_index = index; |
| 552 | return nr_found; |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * radix_tree_gang_lookup - perform multiple lookup on a radix tree |
| 557 | * @root: radix tree root |
| 558 | * @results: where the results of the lookup are placed |
| 559 | * @first_index: start the lookup from this key |
| 560 | * @max_items: place up to this many items at *results |
| 561 | * |
| 562 | * Performs an index-ascending scan of the tree for present items. Places |
| 563 | * them at *@results and returns the number of items which were placed at |
| 564 | * *@results. |
| 565 | * |
| 566 | * The implementation is naive. |
| 567 | */ |
| 568 | unsigned int |
| 569 | radix_tree_gang_lookup(struct radix_tree_root *root, void **results, |
| 570 | unsigned long first_index, unsigned int max_items) |
| 571 | { |
| 572 | const unsigned long max_index = radix_tree_maxindex(root->height); |
| 573 | unsigned long cur_index = first_index; |
| 574 | unsigned int ret = 0; |
| 575 | |
| 576 | while (ret < max_items) { |
| 577 | unsigned int nr_found; |
| 578 | unsigned long next_index; /* Index of next search */ |
| 579 | |
| 580 | if (cur_index > max_index) |
| 581 | break; |
| 582 | nr_found = __lookup(root, results + ret, cur_index, |
| 583 | max_items - ret, &next_index); |
| 584 | ret += nr_found; |
| 585 | if (next_index == 0) |
| 586 | break; |
| 587 | cur_index = next_index; |
| 588 | } |
| 589 | return ret; |
| 590 | } |
| 591 | EXPORT_SYMBOL(radix_tree_gang_lookup); |
| 592 | |
| 593 | /* |
| 594 | * FIXME: the two tag_get()s here should use find_next_bit() instead of |
| 595 | * open-coding the search. |
| 596 | */ |
| 597 | static unsigned int |
| 598 | __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 599 | unsigned int max_items, unsigned long *next_index, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | { |
| 601 | unsigned int nr_found = 0; |
| 602 | unsigned int shift; |
| 603 | unsigned int height = root->height; |
| 604 | struct radix_tree_node *slot; |
| 605 | |
| 606 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; |
| 607 | slot = root->rnode; |
| 608 | |
| 609 | while (height > 0) { |
| 610 | unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK; |
| 611 | |
| 612 | for ( ; i < RADIX_TREE_MAP_SIZE; i++) { |
| 613 | if (tag_get(slot, tag, i)) { |
| 614 | BUG_ON(slot->slots[i] == NULL); |
| 615 | break; |
| 616 | } |
| 617 | index &= ~((1UL << shift) - 1); |
| 618 | index += 1UL << shift; |
| 619 | if (index == 0) |
| 620 | goto out; /* 32-bit wraparound */ |
| 621 | } |
| 622 | if (i == RADIX_TREE_MAP_SIZE) |
| 623 | goto out; |
| 624 | height--; |
| 625 | if (height == 0) { /* Bottom level: grab some items */ |
| 626 | unsigned long j = index & RADIX_TREE_MAP_MASK; |
| 627 | |
| 628 | for ( ; j < RADIX_TREE_MAP_SIZE; j++) { |
| 629 | index++; |
| 630 | if (tag_get(slot, tag, j)) { |
| 631 | BUG_ON(slot->slots[j] == NULL); |
| 632 | results[nr_found++] = slot->slots[j]; |
| 633 | if (nr_found == max_items) |
| 634 | goto out; |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | shift -= RADIX_TREE_MAP_SHIFT; |
| 639 | slot = slot->slots[i]; |
| 640 | } |
| 641 | out: |
| 642 | *next_index = index; |
| 643 | return nr_found; |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree |
| 648 | * based on a tag |
| 649 | * @root: radix tree root |
| 650 | * @results: where the results of the lookup are placed |
| 651 | * @first_index: start the lookup from this key |
| 652 | * @max_items: place up to this many items at *results |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 653 | * @tag: the tag index (< RADIX_TREE_MAX_TAGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | * |
| 655 | * Performs an index-ascending scan of the tree for present items which |
| 656 | * have the tag indexed by @tag set. Places the items at *@results and |
| 657 | * returns the number of items which were placed at *@results. |
| 658 | */ |
| 659 | unsigned int |
| 660 | radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 661 | unsigned long first_index, unsigned int max_items, |
| 662 | unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | { |
| 664 | const unsigned long max_index = radix_tree_maxindex(root->height); |
| 665 | unsigned long cur_index = first_index; |
| 666 | unsigned int ret = 0; |
| 667 | |
| 668 | while (ret < max_items) { |
| 669 | unsigned int nr_found; |
| 670 | unsigned long next_index; /* Index of next search */ |
| 671 | |
| 672 | if (cur_index > max_index) |
| 673 | break; |
| 674 | nr_found = __lookup_tag(root, results + ret, cur_index, |
| 675 | max_items - ret, &next_index, tag); |
| 676 | ret += nr_found; |
| 677 | if (next_index == 0) |
| 678 | break; |
| 679 | cur_index = next_index; |
| 680 | } |
| 681 | return ret; |
| 682 | } |
| 683 | EXPORT_SYMBOL(radix_tree_gang_lookup_tag); |
| 684 | |
| 685 | /** |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 686 | * radix_tree_shrink - shrink height of a radix tree to minimal |
| 687 | * @root radix tree root |
| 688 | */ |
| 689 | static inline void radix_tree_shrink(struct radix_tree_root *root) |
| 690 | { |
| 691 | /* try to shrink tree height */ |
| 692 | while (root->height > 1 && |
| 693 | root->rnode->count == 1 && |
| 694 | root->rnode->slots[0]) { |
| 695 | struct radix_tree_node *to_free = root->rnode; |
| 696 | |
| 697 | root->rnode = to_free->slots[0]; |
| 698 | root->height--; |
| 699 | /* must only free zeroed nodes into the slab */ |
| 700 | tag_clear(to_free, 0, 0); |
| 701 | tag_clear(to_free, 1, 0); |
| 702 | to_free->slots[0] = NULL; |
| 703 | to_free->count = 0; |
| 704 | radix_tree_node_free(to_free); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | * radix_tree_delete - delete an item from a radix tree |
| 710 | * @root: radix tree root |
| 711 | * @index: index key |
| 712 | * |
| 713 | * Remove the item at @index from the radix tree rooted at @root. |
| 714 | * |
| 715 | * Returns the address of the deleted item, or NULL if it was not present. |
| 716 | */ |
| 717 | void *radix_tree_delete(struct radix_tree_root *root, unsigned long index) |
| 718 | { |
| 719 | struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path; |
| 720 | struct radix_tree_path *orig_pathp; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 721 | struct radix_tree_node *slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | unsigned int height, shift; |
| 723 | void *ret = NULL; |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 724 | char tags[RADIX_TREE_MAX_TAGS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | int nr_cleared_tags; |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 726 | int tag; |
| 727 | int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | height = root->height; |
| 730 | if (index > radix_tree_maxindex(height)) |
| 731 | goto out; |
| 732 | |
| 733 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; |
| 734 | pathp->node = NULL; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 735 | slot = root->rnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 737 | for ( ; height > 0; height--) { |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 738 | if (slot == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | goto out; |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | pathp++; |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 742 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; |
| 743 | pathp->offset = offset; |
| 744 | pathp->node = slot; |
| 745 | slot = slot->slots[offset]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | shift -= RADIX_TREE_MAP_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 749 | ret = slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | if (ret == NULL) |
| 751 | goto out; |
| 752 | |
| 753 | orig_pathp = pathp; |
| 754 | |
| 755 | /* |
| 756 | * Clear all tags associated with the just-deleted item |
| 757 | */ |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 758 | nr_cleared_tags = 0; |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 759 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { |
NeilBrown | 90f9dd8 | 2006-02-16 14:43:01 +1100 | [diff] [blame] | 760 | tags[tag] = 1; |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 761 | if (tag_get(pathp->node, tag, pathp->offset)) { |
| 762 | tag_clear(pathp->node, tag, pathp->offset); |
NeilBrown | 90f9dd8 | 2006-02-16 14:43:01 +1100 | [diff] [blame] | 763 | if (!any_tag_set(pathp->node, tag)) { |
| 764 | tags[tag] = 0; |
| 765 | nr_cleared_tags++; |
| 766 | } |
| 767 | } |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 770 | for (pathp--; nr_cleared_tags && pathp->node; pathp--) { |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 771 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | if (tags[tag]) |
| 773 | continue; |
| 774 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 775 | tag_clear(pathp->node, tag, pathp->offset); |
Nick Piggin | 6e954b9 | 2006-01-08 01:01:40 -0800 | [diff] [blame] | 776 | if (any_tag_set(pathp->node, tag)) { |
| 777 | tags[tag] = 1; |
| 778 | nr_cleared_tags--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } |
| 780 | } |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 781 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 783 | /* Now free the nodes we do not need anymore */ |
| 784 | for (pathp = orig_pathp; pathp->node; pathp--) { |
| 785 | pathp->node->slots[pathp->offset] = NULL; |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 786 | pathp->node->count--; |
| 787 | |
| 788 | if (pathp->node->count) { |
| 789 | if (pathp->node == root->rnode) |
| 790 | radix_tree_shrink(root); |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 791 | goto out; |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 792 | } |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 793 | |
| 794 | /* Node with zero slots in use so free it */ |
| 795 | radix_tree_node_free(pathp->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | } |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 797 | root->rnode = NULL; |
| 798 | root->height = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | out: |
| 800 | return ret; |
| 801 | } |
| 802 | EXPORT_SYMBOL(radix_tree_delete); |
| 803 | |
| 804 | /** |
| 805 | * radix_tree_tagged - test whether any items in the tree are tagged |
| 806 | * @root: radix tree root |
| 807 | * @tag: tag to test |
| 808 | */ |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 809 | int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | { |
Nick Piggin | 6e954b9 | 2006-01-08 01:01:40 -0800 | [diff] [blame] | 811 | struct radix_tree_node *rnode; |
| 812 | rnode = root->rnode; |
| 813 | if (!rnode) |
| 814 | return 0; |
| 815 | return any_tag_set(rnode, tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | } |
| 817 | EXPORT_SYMBOL(radix_tree_tagged); |
| 818 | |
| 819 | static void |
| 820 | radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags) |
| 821 | { |
| 822 | memset(node, 0, sizeof(struct radix_tree_node)); |
| 823 | } |
| 824 | |
| 825 | static __init unsigned long __maxindex(unsigned int height) |
| 826 | { |
| 827 | unsigned int tmp = height * RADIX_TREE_MAP_SHIFT; |
| 828 | unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1; |
| 829 | |
| 830 | if (tmp >= RADIX_TREE_INDEX_BITS) |
| 831 | index = ~0UL; |
| 832 | return index; |
| 833 | } |
| 834 | |
| 835 | static __init void radix_tree_init_maxindex(void) |
| 836 | { |
| 837 | unsigned int i; |
| 838 | |
| 839 | for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++) |
| 840 | height_to_maxindex[i] = __maxindex(i); |
| 841 | } |
| 842 | |
| 843 | #ifdef CONFIG_HOTPLUG_CPU |
| 844 | static int radix_tree_callback(struct notifier_block *nfb, |
| 845 | unsigned long action, |
| 846 | void *hcpu) |
| 847 | { |
| 848 | int cpu = (long)hcpu; |
| 849 | struct radix_tree_preload *rtp; |
| 850 | |
| 851 | /* Free per-cpu pool of perloaded nodes */ |
| 852 | if (action == CPU_DEAD) { |
| 853 | rtp = &per_cpu(radix_tree_preloads, cpu); |
| 854 | while (rtp->nr) { |
| 855 | kmem_cache_free(radix_tree_node_cachep, |
| 856 | rtp->nodes[rtp->nr-1]); |
| 857 | rtp->nodes[rtp->nr-1] = NULL; |
| 858 | rtp->nr--; |
| 859 | } |
| 860 | } |
| 861 | return NOTIFY_OK; |
| 862 | } |
| 863 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 864 | |
| 865 | void __init radix_tree_init(void) |
| 866 | { |
| 867 | radix_tree_node_cachep = kmem_cache_create("radix_tree_node", |
| 868 | sizeof(struct radix_tree_node), 0, |
| 869 | SLAB_PANIC, radix_tree_node_ctor, NULL); |
| 870 | radix_tree_init_maxindex(); |
| 871 | hotcpu_notifier(radix_tree_callback, 0); |
| 872 | } |