blob: 88511c3805ad77fc9515b7a75bdc52e09a6393df [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{
140 if (!test_bit(offset, &node->tags[tag][0]))
141 __set_bit(offset, &node->tags[tag][0]);
142}
143
144static inline void tag_clear(struct radix_tree_node *node, int tag, int offset)
145{
146 __clear_bit(offset, &node->tags[tag][0]);
147}
148
149static inline int tag_get(struct radix_tree_node *node, int tag, int offset)
150{
151 return test_bit(offset, &node->tags[tag][0]);
152}
153
154/*
155 * Return the maximum key which can be store into a
156 * radix tree with height HEIGHT.
157 */
158static inline unsigned long radix_tree_maxindex(unsigned int height)
159{
160 return height_to_maxindex[height];
161}
162
163/*
164 * Extend a radix tree so it can store key @index.
165 */
166static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
167{
168 struct radix_tree_node *node;
169 unsigned int height;
170 char tags[RADIX_TREE_TAGS];
171 int tag;
172
173 /* Figure out what the height should be. */
174 height = root->height + 1;
175 while (index > radix_tree_maxindex(height))
176 height++;
177
178 if (root->rnode == NULL) {
179 root->height = height;
180 goto out;
181 }
182
183 /*
184 * Prepare the tag status of the top-level node for propagation
185 * into the newly-pushed top-level node(s)
186 */
187 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
188 int idx;
189
190 tags[tag] = 0;
191 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
192 if (root->rnode->tags[tag][idx]) {
193 tags[tag] = 1;
194 break;
195 }
196 }
197 }
198
199 do {
200 if (!(node = radix_tree_node_alloc(root)))
201 return -ENOMEM;
202
203 /* Increase the height. */
204 node->slots[0] = root->rnode;
205
206 /* Propagate the aggregated tag info into the new root */
207 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
208 if (tags[tag])
209 tag_set(node, tag, 0);
210 }
211
212 node->count = 1;
213 root->rnode = node;
214 root->height++;
215 } while (height > root->height);
216out:
217 return 0;
218}
219
220/**
221 * radix_tree_insert - insert into a radix tree
222 * @root: radix tree root
223 * @index: index key
224 * @item: item to insert
225 *
226 * Insert an item into the radix tree at position @index.
227 */
228int radix_tree_insert(struct radix_tree_root *root,
229 unsigned long index, void *item)
230{
Christoph Lameter201b6262005-09-06 15:16:46 -0700231 struct radix_tree_node *node = NULL, *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 unsigned int height, shift;
233 int offset;
234 int error;
235
236 /* Make sure the tree is high enough. */
237 if ((!index && !root->rnode) ||
238 index > radix_tree_maxindex(root->height)) {
239 error = radix_tree_extend(root, index);
240 if (error)
241 return error;
242 }
243
Christoph Lameter201b6262005-09-06 15:16:46 -0700244 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 height = root->height;
246 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
247
248 offset = 0; /* uninitialised var warning */
249 while (height > 0) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700250 if (slot == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* Have to add a child node. */
Christoph Lameter201b6262005-09-06 15:16:46 -0700252 if (!(slot = radix_tree_node_alloc(root)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -ENOMEM;
Christoph Lameter201b6262005-09-06 15:16:46 -0700254 if (node) {
255 node->slots[offset] = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 node->count++;
Christoph Lameter201b6262005-09-06 15:16:46 -0700257 } else
258 root->rnode = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
261 /* Go a level down */
262 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Christoph Lameter201b6262005-09-06 15:16:46 -0700263 node = slot;
264 slot = node->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 shift -= RADIX_TREE_MAP_SHIFT;
266 height--;
267 }
268
Christoph Lameter201b6262005-09-06 15:16:46 -0700269 if (slot != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return -EEXIST;
Christoph Lameter201b6262005-09-06 15:16:46 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (node) {
273 node->count++;
Christoph Lameter201b6262005-09-06 15:16:46 -0700274 node->slots[offset] = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 BUG_ON(tag_get(node, 0, offset));
276 BUG_ON(tag_get(node, 1, offset));
Christoph Lameter201b6262005-09-06 15:16:46 -0700277 } else
278 root->rnode = item;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return 0;
281}
282EXPORT_SYMBOL(radix_tree_insert);
283
Hans Reisera4331362005-11-07 00:59:29 -0800284static inline void **__lookup_slot(struct radix_tree_root *root,
285 unsigned long index)
286{
287 unsigned int height, shift;
288 struct radix_tree_node **slot;
289
290 height = root->height;
291 if (index > radix_tree_maxindex(height))
292 return NULL;
293
294 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
295 slot = &root->rnode;
296
297 while (height > 0) {
298 if (*slot == NULL)
299 return NULL;
300
301 slot = (struct radix_tree_node **)
302 ((*slot)->slots +
303 ((index >> shift) & RADIX_TREE_MAP_MASK));
304 shift -= RADIX_TREE_MAP_SHIFT;
305 height--;
306 }
307
308 return (void **)slot;
309}
310
311/**
312 * radix_tree_lookup_slot - lookup a slot in a radix tree
313 * @root: radix tree root
314 * @index: index key
315 *
316 * Lookup the slot corresponding to the position @index in the radix tree
317 * @root. This is useful for update-if-exists operations.
318 */
319void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
320{
321 return __lookup_slot(root, index);
322}
323EXPORT_SYMBOL(radix_tree_lookup_slot);
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/**
326 * radix_tree_lookup - perform lookup operation on a radix tree
327 * @root: radix tree root
328 * @index: index key
329 *
330 * Lookup the item at the position @index in the radix tree @root.
331 */
332void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
333{
Hans Reisera4331362005-11-07 00:59:29 -0800334 void **slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Hans Reisera4331362005-11-07 00:59:29 -0800336 slot = __lookup_slot(root, index);
337 return slot != NULL ? *slot : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339EXPORT_SYMBOL(radix_tree_lookup);
340
341/**
342 * radix_tree_tag_set - set a tag on a radix tree node
343 * @root: radix tree root
344 * @index: index key
345 * @tag: tag index
346 *
347 * Set the search tag corresponging to @index in the radix tree. From
348 * the root all the way down to the leaf node.
349 *
350 * Returns the address of the tagged item. Setting a tag on a not-present
351 * item is a bug.
352 */
353void *radix_tree_tag_set(struct radix_tree_root *root,
354 unsigned long index, int tag)
355{
356 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700357 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 height = root->height;
360 if (index > radix_tree_maxindex(height))
361 return NULL;
362
363 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Christoph Lameter201b6262005-09-06 15:16:46 -0700364 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 while (height > 0) {
367 int offset;
368
369 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Christoph Lameter201b6262005-09-06 15:16:46 -0700370 tag_set(slot, tag, offset);
371 slot = slot->slots[offset];
372 BUG_ON(slot == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 shift -= RADIX_TREE_MAP_SHIFT;
374 height--;
375 }
376
Christoph Lameter201b6262005-09-06 15:16:46 -0700377 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379EXPORT_SYMBOL(radix_tree_tag_set);
380
381/**
382 * radix_tree_tag_clear - clear a tag on a radix tree node
383 * @root: radix tree root
384 * @index: index key
385 * @tag: tag index
386 *
387 * Clear the search tag corresponging to @index in the radix tree. If
388 * this causes the leaf node to have no tags set then clear the tag in the
389 * next-to-leaf node, etc.
390 *
391 * Returns the address of the tagged item on success, else NULL. ie:
392 * has the same return value and semantics as radix_tree_lookup().
393 */
394void *radix_tree_tag_clear(struct radix_tree_root *root,
395 unsigned long index, int tag)
396{
397 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
Christoph Lameter201b6262005-09-06 15:16:46 -0700398 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 unsigned int height, shift;
400 void *ret = NULL;
401
402 height = root->height;
403 if (index > radix_tree_maxindex(height))
404 goto out;
405
406 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
407 pathp->node = NULL;
Christoph Lameter201b6262005-09-06 15:16:46 -0700408 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 while (height > 0) {
411 int offset;
412
Christoph Lameter201b6262005-09-06 15:16:46 -0700413 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 goto out;
415
416 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
417 pathp[1].offset = offset;
Christoph Lameter201b6262005-09-06 15:16:46 -0700418 pathp[1].node = slot;
419 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 pathp++;
421 shift -= RADIX_TREE_MAP_SHIFT;
422 height--;
423 }
424
Christoph Lameter201b6262005-09-06 15:16:46 -0700425 ret = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (ret == NULL)
427 goto out;
428
429 do {
430 int idx;
431
Christoph Lameter201b6262005-09-06 15:16:46 -0700432 tag_clear(pathp->node, tag, pathp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700434 if (pathp->node->tags[tag][idx])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 goto out;
436 }
437 pathp--;
Christoph Lameter201b6262005-09-06 15:16:46 -0700438 } while (pathp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439out:
440 return ret;
441}
442EXPORT_SYMBOL(radix_tree_tag_clear);
443
444#ifndef __KERNEL__ /* Only the test harness uses this at present */
445/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700446 * radix_tree_tag_get - get a tag on a radix tree node
447 * @root: radix tree root
448 * @index: index key
449 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700451 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700453 * 0: tag not present
454 * 1: tag present, set
455 * -1: tag present, unset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
457int radix_tree_tag_get(struct radix_tree_root *root,
458 unsigned long index, int tag)
459{
460 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700461 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int saw_unset_tag = 0;
463
464 height = root->height;
465 if (index > radix_tree_maxindex(height))
466 return 0;
467
468 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Christoph Lameter201b6262005-09-06 15:16:46 -0700469 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 for ( ; ; ) {
472 int offset;
473
Christoph Lameter201b6262005-09-06 15:16:46 -0700474 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return 0;
476
477 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
478
479 /*
480 * This is just a debug check. Later, we can bale as soon as
481 * we see an unset tag.
482 */
Christoph Lameter201b6262005-09-06 15:16:46 -0700483 if (!tag_get(slot, tag, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 saw_unset_tag = 1;
485 if (height == 1) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700486 int ret = tag_get(slot, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 BUG_ON(ret && saw_unset_tag);
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700489 return ret ? 1 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700491 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 shift -= RADIX_TREE_MAP_SHIFT;
493 height--;
494 }
495}
496EXPORT_SYMBOL(radix_tree_tag_get);
497#endif
498
499static unsigned int
500__lookup(struct radix_tree_root *root, void **results, unsigned long index,
501 unsigned int max_items, unsigned long *next_index)
502{
503 unsigned int nr_found = 0;
Christoph Lameter201b6262005-09-06 15:16:46 -0700504 unsigned int shift, height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct radix_tree_node *slot;
Christoph Lameter201b6262005-09-06 15:16:46 -0700506 unsigned long i;
507
508 height = root->height;
509 if (height == 0)
510 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
513 slot = root->rnode;
514
Christoph Lameter201b6262005-09-06 15:16:46 -0700515 for ( ; height > 1; height--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Christoph Lameter201b6262005-09-06 15:16:46 -0700517 for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
518 i < RADIX_TREE_MAP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (slot->slots[i] != NULL)
520 break;
521 index &= ~((1UL << shift) - 1);
522 index += 1UL << shift;
523 if (index == 0)
524 goto out; /* 32-bit wraparound */
525 }
526 if (i == RADIX_TREE_MAP_SIZE)
527 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 shift -= RADIX_TREE_MAP_SHIFT;
530 slot = slot->slots[i];
531 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700532
533 /* Bottom level: grab some items */
534 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
535 index++;
536 if (slot->slots[i]) {
537 results[nr_found++] = slot->slots[i];
538 if (nr_found == max_items)
539 goto out;
540 }
541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542out:
543 *next_index = index;
544 return nr_found;
545}
546
547/**
548 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
549 * @root: radix tree root
550 * @results: where the results of the lookup are placed
551 * @first_index: start the lookup from this key
552 * @max_items: place up to this many items at *results
553 *
554 * Performs an index-ascending scan of the tree for present items. Places
555 * them at *@results and returns the number of items which were placed at
556 * *@results.
557 *
558 * The implementation is naive.
559 */
560unsigned int
561radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
562 unsigned long first_index, unsigned int max_items)
563{
564 const unsigned long max_index = radix_tree_maxindex(root->height);
565 unsigned long cur_index = first_index;
566 unsigned int ret = 0;
567
568 while (ret < max_items) {
569 unsigned int nr_found;
570 unsigned long next_index; /* Index of next search */
571
572 if (cur_index > max_index)
573 break;
574 nr_found = __lookup(root, results + ret, cur_index,
575 max_items - ret, &next_index);
576 ret += nr_found;
577 if (next_index == 0)
578 break;
579 cur_index = next_index;
580 }
581 return ret;
582}
583EXPORT_SYMBOL(radix_tree_gang_lookup);
584
585/*
586 * FIXME: the two tag_get()s here should use find_next_bit() instead of
587 * open-coding the search.
588 */
589static unsigned int
590__lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
591 unsigned int max_items, unsigned long *next_index, int tag)
592{
593 unsigned int nr_found = 0;
594 unsigned int shift;
595 unsigned int height = root->height;
596 struct radix_tree_node *slot;
597
598 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
599 slot = root->rnode;
600
601 while (height > 0) {
602 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
603
604 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
605 if (tag_get(slot, tag, i)) {
606 BUG_ON(slot->slots[i] == NULL);
607 break;
608 }
609 index &= ~((1UL << shift) - 1);
610 index += 1UL << shift;
611 if (index == 0)
612 goto out; /* 32-bit wraparound */
613 }
614 if (i == RADIX_TREE_MAP_SIZE)
615 goto out;
616 height--;
617 if (height == 0) { /* Bottom level: grab some items */
618 unsigned long j = index & RADIX_TREE_MAP_MASK;
619
620 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
621 index++;
622 if (tag_get(slot, tag, j)) {
623 BUG_ON(slot->slots[j] == NULL);
624 results[nr_found++] = slot->slots[j];
625 if (nr_found == max_items)
626 goto out;
627 }
628 }
629 }
630 shift -= RADIX_TREE_MAP_SHIFT;
631 slot = slot->slots[i];
632 }
633out:
634 *next_index = index;
635 return nr_found;
636}
637
638/**
639 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
640 * based on a tag
641 * @root: radix tree root
642 * @results: where the results of the lookup are placed
643 * @first_index: start the lookup from this key
644 * @max_items: place up to this many items at *results
645 * @tag: the tag index
646 *
647 * Performs an index-ascending scan of the tree for present items which
648 * have the tag indexed by @tag set. Places the items at *@results and
649 * returns the number of items which were placed at *@results.
650 */
651unsigned int
652radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
653 unsigned long first_index, unsigned int max_items, int tag)
654{
655 const unsigned long max_index = radix_tree_maxindex(root->height);
656 unsigned long cur_index = first_index;
657 unsigned int ret = 0;
658
659 while (ret < max_items) {
660 unsigned int nr_found;
661 unsigned long next_index; /* Index of next search */
662
663 if (cur_index > max_index)
664 break;
665 nr_found = __lookup_tag(root, results + ret, cur_index,
666 max_items - ret, &next_index, tag);
667 ret += nr_found;
668 if (next_index == 0)
669 break;
670 cur_index = next_index;
671 }
672 return ret;
673}
674EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
675
676/**
677 * radix_tree_delete - delete an item from a radix tree
678 * @root: radix tree root
679 * @index: index key
680 *
681 * Remove the item at @index from the radix tree rooted at @root.
682 *
683 * Returns the address of the deleted item, or NULL if it was not present.
684 */
685void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
686{
687 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
688 struct radix_tree_path *orig_pathp;
Christoph Lameter201b6262005-09-06 15:16:46 -0700689 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 unsigned int height, shift;
691 void *ret = NULL;
692 char tags[RADIX_TREE_TAGS];
693 int nr_cleared_tags;
694
695 height = root->height;
696 if (index > radix_tree_maxindex(height))
697 goto out;
698
699 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
700 pathp->node = NULL;
Christoph Lameter201b6262005-09-06 15:16:46 -0700701 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Christoph Lameter201b6262005-09-06 15:16:46 -0700703 for ( ; height > 0; height--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int offset;
705
Christoph Lameter201b6262005-09-06 15:16:46 -0700706 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 goto out;
708
709 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
710 pathp[1].offset = offset;
Christoph Lameter201b6262005-09-06 15:16:46 -0700711 pathp[1].node = slot;
712 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 pathp++;
714 shift -= RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
Christoph Lameter201b6262005-09-06 15:16:46 -0700717 ret = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (ret == NULL)
719 goto out;
720
721 orig_pathp = pathp;
722
723 /*
724 * Clear all tags associated with the just-deleted item
725 */
726 memset(tags, 0, sizeof(tags));
727 do {
728 int tag;
729
730 nr_cleared_tags = RADIX_TREE_TAGS;
731 for (tag = 0; tag < RADIX_TREE_TAGS; tag++) {
732 int idx;
733
734 if (tags[tag])
735 continue;
736
Christoph Lameter201b6262005-09-06 15:16:46 -0700737 tag_clear(pathp->node, tag, pathp->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700740 if (pathp->node->tags[tag][idx]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 tags[tag] = 1;
742 nr_cleared_tags--;
743 break;
744 }
745 }
746 }
747 pathp--;
Christoph Lameter201b6262005-09-06 15:16:46 -0700748 } while (pathp->node && nr_cleared_tags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Christoph Lameter201b6262005-09-06 15:16:46 -0700750 /* Now free the nodes we do not need anymore */
751 for (pathp = orig_pathp; pathp->node; pathp--) {
752 pathp->node->slots[pathp->offset] = NULL;
753 if (--pathp->node->count)
754 goto out;
755
756 /* Node with zero slots in use so free it */
757 radix_tree_node_free(pathp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700759 root->rnode = NULL;
760 root->height = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761out:
762 return ret;
763}
764EXPORT_SYMBOL(radix_tree_delete);
765
766/**
767 * radix_tree_tagged - test whether any items in the tree are tagged
768 * @root: radix tree root
769 * @tag: tag to test
770 */
771int radix_tree_tagged(struct radix_tree_root *root, int tag)
772{
773 int idx;
774
775 if (!root->rnode)
776 return 0;
777 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
778 if (root->rnode->tags[tag][idx])
779 return 1;
780 }
781 return 0;
782}
783EXPORT_SYMBOL(radix_tree_tagged);
784
785static void
786radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
787{
788 memset(node, 0, sizeof(struct radix_tree_node));
789}
790
791static __init unsigned long __maxindex(unsigned int height)
792{
793 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
794 unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
795
796 if (tmp >= RADIX_TREE_INDEX_BITS)
797 index = ~0UL;
798 return index;
799}
800
801static __init void radix_tree_init_maxindex(void)
802{
803 unsigned int i;
804
805 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
806 height_to_maxindex[i] = __maxindex(i);
807}
808
809#ifdef CONFIG_HOTPLUG_CPU
810static int radix_tree_callback(struct notifier_block *nfb,
811 unsigned long action,
812 void *hcpu)
813{
814 int cpu = (long)hcpu;
815 struct radix_tree_preload *rtp;
816
817 /* Free per-cpu pool of perloaded nodes */
818 if (action == CPU_DEAD) {
819 rtp = &per_cpu(radix_tree_preloads, cpu);
820 while (rtp->nr) {
821 kmem_cache_free(radix_tree_node_cachep,
822 rtp->nodes[rtp->nr-1]);
823 rtp->nodes[rtp->nr-1] = NULL;
824 rtp->nr--;
825 }
826 }
827 return NOTIFY_OK;
828}
829#endif /* CONFIG_HOTPLUG_CPU */
830
831void __init radix_tree_init(void)
832{
833 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
834 sizeof(struct radix_tree_node), 0,
835 SLAB_PANIC, radix_tree_node_ctor, NULL);
836 radix_tree_init_maxindex();
837 hotcpu_notifier(radix_tree_callback, 0);
838}