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