blob: 336852f23592c8446d36b883230f755a9a9b1c61 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
Christoph Lameter201b6262005-09-06 15:16:46 -07004 * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
40#define RADIX_TREE_TAGS 2
41
42#define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
43#define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
44
45#define RADIX_TREE_TAG_LONGS \
46 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
47
48struct radix_tree_node {
49 unsigned int count;
50 void *slots[RADIX_TREE_MAP_SIZE];
51 unsigned long tags[RADIX_TREE_TAGS][RADIX_TREE_TAG_LONGS];
52};
53
54struct radix_tree_path {
Christoph Lameter201b6262005-09-06 15:16:46 -070055 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 int offset;
57};
58
59#define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
60#define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
61
Christoph Lameter6c036522005-07-07 17:56:59 -070062static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * Radix tree node cache.
66 */
67static kmem_cache_t *radix_tree_node_cachep;
68
69/*
70 * Per-cpu pool of preloaded nodes
71 */
72struct radix_tree_preload {
73 int nr;
74 struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
75};
76DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
77
78/*
79 * This assumes that the caller has performed appropriate preallocation, and
80 * that the caller has pinned this thread of control to the current CPU.
81 */
82static struct radix_tree_node *
83radix_tree_node_alloc(struct radix_tree_root *root)
84{
85 struct radix_tree_node *ret;
86
87 ret = kmem_cache_alloc(radix_tree_node_cachep, root->gfp_mask);
88 if (ret == NULL && !(root->gfp_mask & __GFP_WAIT)) {
89 struct radix_tree_preload *rtp;
90
91 rtp = &__get_cpu_var(radix_tree_preloads);
92 if (rtp->nr) {
93 ret = rtp->nodes[rtp->nr - 1];
94 rtp->nodes[rtp->nr - 1] = NULL;
95 rtp->nr--;
96 }
97 }
98 return ret;
99}
100
101static inline void
102radix_tree_node_free(struct radix_tree_node *node)
103{
104 kmem_cache_free(radix_tree_node_cachep, node);
105}
106
107/*
108 * Load up this CPU's radix_tree_node buffer with sufficient objects to
109 * ensure that the addition of a single element in the tree cannot fail. On
110 * success, return zero, with preemption disabled. On error, return -ENOMEM
111 * with preemption not disabled.
112 */
Al Virodd0fc662005-10-07 07:46:04 +0100113int radix_tree_preload(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct radix_tree_preload *rtp;
116 struct radix_tree_node *node;
117 int ret = -ENOMEM;
118
119 preempt_disable();
120 rtp = &__get_cpu_var(radix_tree_preloads);
121 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
122 preempt_enable();
123 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
124 if (node == NULL)
125 goto out;
126 preempt_disable();
127 rtp = &__get_cpu_var(radix_tree_preloads);
128 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
129 rtp->nodes[rtp->nr++] = node;
130 else
131 kmem_cache_free(radix_tree_node_cachep, node);
132 }
133 ret = 0;
134out:
135 return ret;
136}
137
138static inline void tag_set(struct radix_tree_node *node, int tag, int offset)
139{
Nick Piggind5274262006-01-08 01:01:41 -0800140 __set_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static inline void tag_clear(struct radix_tree_node *node, int tag, int offset)
144{
Nick Piggind5274262006-01-08 01:01:41 -0800145 __clear_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148static inline int tag_get(struct radix_tree_node *node, int tag, int offset)
149{
Nick Piggind5274262006-01-08 01:01:41 -0800150 return test_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153/*
Nick Piggin6e954b92006-01-08 01:01:40 -0800154 * Returns 1 if any slot in the node has this tag set.
155 * Otherwise returns 0.
156 */
157static inline int any_tag_set(struct radix_tree_node *node, int tag)
158{
159 int idx;
160 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
161 if (node->tags[tag][idx])
162 return 1;
163 }
164 return 0;
165}
166
167/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * Return the maximum key which can be store into a
169 * radix tree with height HEIGHT.
170 */
171static inline unsigned long radix_tree_maxindex(unsigned int height)
172{
173 return height_to_maxindex[height];
174}
175
176/*
177 * Extend a radix tree so it can store key @index.
178 */
179static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
180{
181 struct radix_tree_node *node;
182 unsigned int height;
183 char tags[RADIX_TREE_TAGS];
184 int tag;
185
186 /* Figure out what the height should be. */
187 height = root->height + 1;
188 while (index > radix_tree_maxindex(height))
189 height++;
190
191 if (root->rnode == NULL) {
192 root->height = height;
193 goto out;
194 }
195
196 /*
197 * Prepare the tag status of the top-level node for propagation
198 * into the newly-pushed top-level node(s)
199 */
200 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 tags[tag] = 0;
Nick Piggin6e954b92006-01-08 01:01:40 -0800202 if (any_tag_set(root->rnode, tag))
203 tags[tag] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 do {
207 if (!(node = radix_tree_node_alloc(root)))
208 return -ENOMEM;
209
210 /* Increase the height. */
211 node->slots[0] = root->rnode;
212
213 /* Propagate the aggregated tag info into the new root */
214 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
215 if (tags[tag])
216 tag_set(node, tag, 0);
217 }
218
219 node->count = 1;
220 root->rnode = node;
221 root->height++;
222 } while (height > root->height);
223out:
224 return 0;
225}
226
227/**
228 * radix_tree_insert - insert into a radix tree
229 * @root: radix tree root
230 * @index: index key
231 * @item: item to insert
232 *
233 * Insert an item into the radix tree at position @index.
234 */
235int radix_tree_insert(struct radix_tree_root *root,
236 unsigned long index, void *item)
237{
Christoph Lameter201b6262005-09-06 15:16:46 -0700238 struct radix_tree_node *node = NULL, *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 unsigned int height, shift;
240 int offset;
241 int error;
242
243 /* Make sure the tree is high enough. */
244 if ((!index && !root->rnode) ||
245 index > radix_tree_maxindex(root->height)) {
246 error = radix_tree_extend(root, index);
247 if (error)
248 return error;
249 }
250
Christoph Lameter201b6262005-09-06 15:16:46 -0700251 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 height = root->height;
253 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
254
255 offset = 0; /* uninitialised var warning */
256 while (height > 0) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700257 if (slot == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* Have to add a child node. */
Christoph Lameter201b6262005-09-06 15:16:46 -0700259 if (!(slot = radix_tree_node_alloc(root)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -ENOMEM;
Christoph Lameter201b6262005-09-06 15:16:46 -0700261 if (node) {
262 node->slots[offset] = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 node->count++;
Christoph Lameter201b6262005-09-06 15:16:46 -0700264 } else
265 root->rnode = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
268 /* Go a level down */
269 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Christoph Lameter201b6262005-09-06 15:16:46 -0700270 node = slot;
271 slot = node->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 shift -= RADIX_TREE_MAP_SHIFT;
273 height--;
274 }
275
Christoph Lameter201b6262005-09-06 15:16:46 -0700276 if (slot != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return -EEXIST;
Christoph Lameter201b6262005-09-06 15:16:46 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (node) {
280 node->count++;
Christoph Lameter201b6262005-09-06 15:16:46 -0700281 node->slots[offset] = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 BUG_ON(tag_get(node, 0, offset));
283 BUG_ON(tag_get(node, 1, offset));
Christoph Lameter201b6262005-09-06 15:16:46 -0700284 } else
285 root->rnode = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 0;
288}
289EXPORT_SYMBOL(radix_tree_insert);
290
Hans Reisera4331362005-11-07 00:59:29 -0800291static 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 */
326void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
327{
328 return __lookup_slot(root, index);
329}
330EXPORT_SYMBOL(radix_tree_lookup_slot);
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/**
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 */
339void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
340{
Hans Reisera4331362005-11-07 00:59:29 -0800341 void **slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Hans Reisera4331362005-11-07 00:59:29 -0800343 slot = __lookup_slot(root, index);
344 return slot != NULL ? *slot : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346EXPORT_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 *
354 * Set the search tag corresponging to @index in the radix tree. From
355 * the root all the way down to the leaf node.
356 *
357 * Returns the address of the tagged item. Setting a tag on a not-present
358 * item is a bug.
359 */
360void *radix_tree_tag_set(struct radix_tree_root *root,
361 unsigned long index, int tag)
362{
363 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700364 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 height = root->height;
367 if (index > radix_tree_maxindex(height))
368 return NULL;
369
370 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Christoph Lameter201b6262005-09-06 15:16:46 -0700371 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 while (height > 0) {
374 int offset;
375
376 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Nick Piggind5274262006-01-08 01:01:41 -0800377 if (!tag_get(slot, tag, offset))
378 tag_set(slot, tag, offset);
Christoph Lameter201b6262005-09-06 15:16:46 -0700379 slot = slot->slots[offset];
380 BUG_ON(slot == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 shift -= RADIX_TREE_MAP_SHIFT;
382 height--;
383 }
384
Christoph Lameter201b6262005-09-06 15:16:46 -0700385 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387EXPORT_SYMBOL(radix_tree_tag_set);
388
389/**
390 * radix_tree_tag_clear - clear a tag on a radix tree node
391 * @root: radix tree root
392 * @index: index key
393 * @tag: tag index
394 *
395 * Clear the search tag corresponging to @index in the radix tree. If
396 * this causes the leaf node to have no tags set then clear the tag in the
397 * next-to-leaf node, etc.
398 *
399 * Returns the address of the tagged item on success, else NULL. ie:
400 * has the same return value and semantics as radix_tree_lookup().
401 */
402void *radix_tree_tag_clear(struct radix_tree_root *root,
403 unsigned long index, int tag)
404{
405 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
Christoph Lameter201b6262005-09-06 15:16:46 -0700406 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 unsigned int height, shift;
408 void *ret = NULL;
409
410 height = root->height;
411 if (index > radix_tree_maxindex(height))
412 goto out;
413
414 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
415 pathp->node = NULL;
Christoph Lameter201b6262005-09-06 15:16:46 -0700416 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 while (height > 0) {
419 int offset;
420
Christoph Lameter201b6262005-09-06 15:16:46 -0700421 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 goto out;
423
424 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
425 pathp[1].offset = offset;
Christoph Lameter201b6262005-09-06 15:16:46 -0700426 pathp[1].node = slot;
427 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 pathp++;
429 shift -= RADIX_TREE_MAP_SHIFT;
430 height--;
431 }
432
Christoph Lameter201b6262005-09-06 15:16:46 -0700433 ret = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (ret == NULL)
435 goto out;
436
437 do {
Nick Piggind5274262006-01-08 01:01:41 -0800438 if (!tag_get(pathp->node, tag, pathp->offset))
439 goto out;
Christoph Lameter201b6262005-09-06 15:16:46 -0700440 tag_clear(pathp->node, tag, pathp->offset);
Nick Piggin6e954b92006-01-08 01:01:40 -0800441 if (any_tag_set(pathp->node, tag))
442 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 pathp--;
Christoph Lameter201b6262005-09-06 15:16:46 -0700444 } while (pathp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445out:
446 return ret;
447}
448EXPORT_SYMBOL(radix_tree_tag_clear);
449
450#ifndef __KERNEL__ /* Only the test harness uses this at present */
451/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700452 * radix_tree_tag_get - get a tag on a radix tree node
453 * @root: radix tree root
454 * @index: index key
455 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700457 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700459 * 0: tag not present
460 * 1: tag present, set
461 * -1: tag present, unset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
463int radix_tree_tag_get(struct radix_tree_root *root,
464 unsigned long index, int tag)
465{
466 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700467 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int saw_unset_tag = 0;
469
470 height = root->height;
471 if (index > radix_tree_maxindex(height))
472 return 0;
473
474 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Christoph Lameter201b6262005-09-06 15:16:46 -0700475 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 for ( ; ; ) {
478 int offset;
479
Christoph Lameter201b6262005-09-06 15:16:46 -0700480 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return 0;
482
483 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
484
485 /*
486 * This is just a debug check. Later, we can bale as soon as
487 * we see an unset tag.
488 */
Christoph Lameter201b6262005-09-06 15:16:46 -0700489 if (!tag_get(slot, tag, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 saw_unset_tag = 1;
491 if (height == 1) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700492 int ret = tag_get(slot, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 BUG_ON(ret && saw_unset_tag);
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700495 return ret ? 1 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700497 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 shift -= RADIX_TREE_MAP_SHIFT;
499 height--;
500 }
501}
502EXPORT_SYMBOL(radix_tree_tag_get);
503#endif
504
505static unsigned int
506__lookup(struct radix_tree_root *root, void **results, unsigned long index,
507 unsigned int max_items, unsigned long *next_index)
508{
509 unsigned int nr_found = 0;
Christoph Lameter201b6262005-09-06 15:16:46 -0700510 unsigned int shift, height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct radix_tree_node *slot;
Christoph Lameter201b6262005-09-06 15:16:46 -0700512 unsigned long i;
513
514 height = root->height;
515 if (height == 0)
516 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
519 slot = root->rnode;
520
Christoph Lameter201b6262005-09-06 15:16:46 -0700521 for ( ; height > 1; height--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Christoph Lameter201b6262005-09-06 15:16:46 -0700523 for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
524 i < RADIX_TREE_MAP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (slot->slots[i] != NULL)
526 break;
527 index &= ~((1UL << shift) - 1);
528 index += 1UL << shift;
529 if (index == 0)
530 goto out; /* 32-bit wraparound */
531 }
532 if (i == RADIX_TREE_MAP_SIZE)
533 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 shift -= RADIX_TREE_MAP_SHIFT;
536 slot = slot->slots[i];
537 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700538
539 /* Bottom level: grab some items */
540 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
541 index++;
542 if (slot->slots[i]) {
543 results[nr_found++] = slot->slots[i];
544 if (nr_found == max_items)
545 goto out;
546 }
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548out:
549 *next_index = index;
550 return nr_found;
551}
552
553/**
554 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
555 * @root: radix tree root
556 * @results: where the results of the lookup are placed
557 * @first_index: start the lookup from this key
558 * @max_items: place up to this many items at *results
559 *
560 * Performs an index-ascending scan of the tree for present items. Places
561 * them at *@results and returns the number of items which were placed at
562 * *@results.
563 *
564 * The implementation is naive.
565 */
566unsigned int
567radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
568 unsigned long first_index, unsigned int max_items)
569{
570 const unsigned long max_index = radix_tree_maxindex(root->height);
571 unsigned long cur_index = first_index;
572 unsigned int ret = 0;
573
574 while (ret < max_items) {
575 unsigned int nr_found;
576 unsigned long next_index; /* Index of next search */
577
578 if (cur_index > max_index)
579 break;
580 nr_found = __lookup(root, results + ret, cur_index,
581 max_items - ret, &next_index);
582 ret += nr_found;
583 if (next_index == 0)
584 break;
585 cur_index = next_index;
586 }
587 return ret;
588}
589EXPORT_SYMBOL(radix_tree_gang_lookup);
590
591/*
592 * FIXME: the two tag_get()s here should use find_next_bit() instead of
593 * open-coding the search.
594 */
595static unsigned int
596__lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
597 unsigned int max_items, unsigned long *next_index, int tag)
598{
599 unsigned int nr_found = 0;
600 unsigned int shift;
601 unsigned int height = root->height;
602 struct radix_tree_node *slot;
603
604 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
605 slot = root->rnode;
606
607 while (height > 0) {
608 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
609
610 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
611 if (tag_get(slot, tag, i)) {
612 BUG_ON(slot->slots[i] == NULL);
613 break;
614 }
615 index &= ~((1UL << shift) - 1);
616 index += 1UL << shift;
617 if (index == 0)
618 goto out; /* 32-bit wraparound */
619 }
620 if (i == RADIX_TREE_MAP_SIZE)
621 goto out;
622 height--;
623 if (height == 0) { /* Bottom level: grab some items */
624 unsigned long j = index & RADIX_TREE_MAP_MASK;
625
626 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
627 index++;
628 if (tag_get(slot, tag, j)) {
629 BUG_ON(slot->slots[j] == NULL);
630 results[nr_found++] = slot->slots[j];
631 if (nr_found == max_items)
632 goto out;
633 }
634 }
635 }
636 shift -= RADIX_TREE_MAP_SHIFT;
637 slot = slot->slots[i];
638 }
639out:
640 *next_index = index;
641 return nr_found;
642}
643
644/**
645 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
646 * based on a tag
647 * @root: radix tree root
648 * @results: where the results of the lookup are placed
649 * @first_index: start the lookup from this key
650 * @max_items: place up to this many items at *results
651 * @tag: the tag index
652 *
653 * Performs an index-ascending scan of the tree for present items which
654 * have the tag indexed by @tag set. Places the items at *@results and
655 * returns the number of items which were placed at *@results.
656 */
657unsigned int
658radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
659 unsigned long first_index, unsigned int max_items, int tag)
660{
661 const unsigned long max_index = radix_tree_maxindex(root->height);
662 unsigned long cur_index = first_index;
663 unsigned int ret = 0;
664
665 while (ret < max_items) {
666 unsigned int nr_found;
667 unsigned long next_index; /* Index of next search */
668
669 if (cur_index > max_index)
670 break;
671 nr_found = __lookup_tag(root, results + ret, cur_index,
672 max_items - ret, &next_index, tag);
673 ret += nr_found;
674 if (next_index == 0)
675 break;
676 cur_index = next_index;
677 }
678 return ret;
679}
680EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
681
682/**
683 * radix_tree_delete - delete an item from a radix tree
684 * @root: radix tree root
685 * @index: index key
686 *
687 * Remove the item at @index from the radix tree rooted at @root.
688 *
689 * Returns the address of the deleted item, or NULL if it was not present.
690 */
691void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
692{
693 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
694 struct radix_tree_path *orig_pathp;
Christoph Lameter201b6262005-09-06 15:16:46 -0700695 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 unsigned int height, shift;
697 void *ret = NULL;
698 char tags[RADIX_TREE_TAGS];
699 int nr_cleared_tags;
Nick Piggind5274262006-01-08 01:01:41 -0800700 int tag;
701 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 height = root->height;
704 if (index > radix_tree_maxindex(height))
705 goto out;
706
707 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
708 pathp->node = NULL;
Christoph Lameter201b6262005-09-06 15:16:46 -0700709 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Christoph Lameter201b6262005-09-06 15:16:46 -0700711 for ( ; height > 0; height--) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700712 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto out;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 pathp++;
Nick Piggind5274262006-01-08 01:01:41 -0800716 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
717 pathp->offset = offset;
718 pathp->node = slot;
719 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 shift -= RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
Christoph Lameter201b6262005-09-06 15:16:46 -0700723 ret = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (ret == NULL)
725 goto out;
726
727 orig_pathp = pathp;
728
729 /*
730 * Clear all tags associated with the just-deleted item
731 */
Nick Piggind5274262006-01-08 01:01:41 -0800732 nr_cleared_tags = 0;
733 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
734 if (tag_get(pathp->node, tag, pathp->offset)) {
735 tag_clear(pathp->node, tag, pathp->offset);
736 tags[tag] = 0;
737 nr_cleared_tags++;
738 } else
739 tags[tag] = 1;
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Nick Piggind5274262006-01-08 01:01:41 -0800742 for (pathp--; nr_cleared_tags && pathp->node; pathp--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (tags[tag])
745 continue;
746
Christoph Lameter201b6262005-09-06 15:16:46 -0700747 tag_clear(pathp->node, tag, pathp->offset);
Nick Piggin6e954b92006-01-08 01:01:40 -0800748 if (any_tag_set(pathp->node, tag)) {
749 tags[tag] = 1;
750 nr_cleared_tags--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 }
Nick Piggind5274262006-01-08 01:01:41 -0800753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Christoph Lameter201b6262005-09-06 15:16:46 -0700755 /* Now free the nodes we do not need anymore */
756 for (pathp = orig_pathp; pathp->node; pathp--) {
757 pathp->node->slots[pathp->offset] = NULL;
758 if (--pathp->node->count)
759 goto out;
760
761 /* Node with zero slots in use so free it */
762 radix_tree_node_free(pathp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700764 root->rnode = NULL;
765 root->height = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766out:
767 return ret;
768}
769EXPORT_SYMBOL(radix_tree_delete);
770
771/**
772 * radix_tree_tagged - test whether any items in the tree are tagged
773 * @root: radix tree root
774 * @tag: tag to test
775 */
776int radix_tree_tagged(struct radix_tree_root *root, int tag)
777{
Nick Piggin6e954b92006-01-08 01:01:40 -0800778 struct radix_tree_node *rnode;
779 rnode = root->rnode;
780 if (!rnode)
781 return 0;
782 return any_tag_set(rnode, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784EXPORT_SYMBOL(radix_tree_tagged);
785
786static void
787radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
788{
789 memset(node, 0, sizeof(struct radix_tree_node));
790}
791
792static __init unsigned long __maxindex(unsigned int height)
793{
794 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
795 unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
796
797 if (tmp >= RADIX_TREE_INDEX_BITS)
798 index = ~0UL;
799 return index;
800}
801
802static __init void radix_tree_init_maxindex(void)
803{
804 unsigned int i;
805
806 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
807 height_to_maxindex[i] = __maxindex(i);
808}
809
810#ifdef CONFIG_HOTPLUG_CPU
811static int radix_tree_callback(struct notifier_block *nfb,
812 unsigned long action,
813 void *hcpu)
814{
815 int cpu = (long)hcpu;
816 struct radix_tree_preload *rtp;
817
818 /* Free per-cpu pool of perloaded nodes */
819 if (action == CPU_DEAD) {
820 rtp = &per_cpu(radix_tree_preloads, cpu);
821 while (rtp->nr) {
822 kmem_cache_free(radix_tree_node_cachep,
823 rtp->nodes[rtp->nr-1]);
824 rtp->nodes[rtp->nr-1] = NULL;
825 rtp->nr--;
826 }
827 }
828 return NOTIFY_OK;
829}
830#endif /* CONFIG_HOTPLUG_CPU */
831
832void __init radix_tree_init(void)
833{
834 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
835 sizeof(struct radix_tree_node), 0,
836 SLAB_PANIC, radix_tree_node_ctor, NULL);
837 radix_tree_init_maxindex();
838 hotcpu_notifier(radix_tree_callback, 0);
839}