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