blob: 7097bb239e406b2ccca5772cb9516e852498f9ef [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
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
47struct radix_tree_node {
48 unsigned int count;
49 void *slots[RADIX_TREE_MAP_SIZE];
Jonathan Corbetdaff89f2006-03-25 03:08:05 -080050 unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53struct radix_tree_path {
Christoph Lameter201b6262005-09-06 15:16:46 -070054 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 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 Lameter6c036522005-07-07 17:56:59 -070061static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
64 * Radix tree node cache.
65 */
66static kmem_cache_t *radix_tree_node_cachep;
67
68/*
69 * Per-cpu pool of preloaded nodes
70 */
71struct radix_tree_preload {
72 int nr;
73 struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
74};
75DEFINE_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 */
81static struct radix_tree_node *
82radix_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
100static inline void
101radix_tree_node_free(struct radix_tree_node *node)
102{
103 kmem_cache_free(radix_tree_node_cachep, node);
104}
105
106/*
107 * Load up this CPU's radix_tree_node buffer with sufficient objects to
108 * ensure that the addition of a single element in the tree cannot fail. On
109 * success, return zero, with preemption disabled. On error, return -ENOMEM
110 * with preemption not disabled.
111 */
Al Virodd0fc662005-10-07 07:46:04 +0100112int radix_tree_preload(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
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;
133out:
134 return ret;
135}
136
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800137static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
138 int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Nick Piggind5274262006-01-08 01:01:41 -0800140 __set_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800143static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
144 int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Nick Piggind5274262006-01-08 01:01:41 -0800146 __clear_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800149static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
150 int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Nick Piggind5274262006-01-08 01:01:41 -0800152 return test_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/*
Nick Piggin6e954b92006-01-08 01:01:40 -0800156 * Returns 1 if any slot in the node has this tag set.
157 * Otherwise returns 0.
158 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800159static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
Nick Piggin6e954b92006-01-08 01:01:40 -0800160{
161 int idx;
162 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
163 if (node->tags[tag][idx])
164 return 1;
165 }
166 return 0;
167}
168
169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Return the maximum key which can be store into a
171 * radix tree with height HEIGHT.
172 */
173static 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 */
181static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
182{
183 struct radix_tree_node *node;
184 unsigned int height;
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800185 char tags[RADIX_TREE_MAX_TAGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int tag;
187
188 /* Figure out what the height should be. */
189 height = root->height + 1;
190 while (index > radix_tree_maxindex(height))
191 height++;
192
193 if (root->rnode == NULL) {
194 root->height = height;
195 goto out;
196 }
197
198 /*
199 * Prepare the tag status of the top-level node for propagation
200 * into the newly-pushed top-level node(s)
201 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800202 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 tags[tag] = 0;
Nick Piggin6e954b92006-01-08 01:01:40 -0800204 if (any_tag_set(root->rnode, tag))
205 tags[tag] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 do {
209 if (!(node = radix_tree_node_alloc(root)))
210 return -ENOMEM;
211
212 /* Increase the height. */
213 node->slots[0] = root->rnode;
214
215 /* Propagate the aggregated tag info into the new root */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800216 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 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);
225out:
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 */
237int radix_tree_insert(struct radix_tree_root *root,
238 unsigned long index, void *item)
239{
Christoph Lameter201b6262005-09-06 15:16:46 -0700240 struct radix_tree_node *node = NULL, *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned int height, shift;
242 int offset;
243 int error;
244
245 /* Make sure the tree is high enough. */
246 if ((!index && !root->rnode) ||
247 index > radix_tree_maxindex(root->height)) {
248 error = radix_tree_extend(root, index);
249 if (error)
250 return error;
251 }
252
Christoph Lameter201b6262005-09-06 15:16:46 -0700253 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 height = root->height;
255 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
256
257 offset = 0; /* uninitialised var warning */
Nick Piggina5f51c92006-01-08 01:01:41 -0800258 do {
Christoph Lameter201b6262005-09-06 15:16:46 -0700259 if (slot == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* Have to add a child node. */
Christoph Lameter201b6262005-09-06 15:16:46 -0700261 if (!(slot = radix_tree_node_alloc(root)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -ENOMEM;
Christoph Lameter201b6262005-09-06 15:16:46 -0700263 if (node) {
264 node->slots[offset] = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 node->count++;
Christoph Lameter201b6262005-09-06 15:16:46 -0700266 } else
267 root->rnode = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
270 /* Go a level down */
271 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Christoph Lameter201b6262005-09-06 15:16:46 -0700272 node = slot;
273 slot = node->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 shift -= RADIX_TREE_MAP_SHIFT;
275 height--;
Nick Piggina5f51c92006-01-08 01:01:41 -0800276 } while (height > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Christoph Lameter201b6262005-09-06 15:16:46 -0700278 if (slot != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -EEXIST;
Christoph Lameter201b6262005-09-06 15:16:46 -0700280
Nick Piggina5f51c92006-01-08 01:01:41 -0800281 BUG_ON(!node);
282 node->count++;
283 node->slots[offset] = item;
284 BUG_ON(tag_get(node, 0, offset));
285 BUG_ON(tag_get(node, 1, offset));
Linus 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 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800354 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
355 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * 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 */
361void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800362 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700365 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 height = root->height;
368 if (index > radix_tree_maxindex(height))
369 return NULL;
370
371 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Christoph Lameter201b6262005-09-06 15:16:46 -0700372 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 while (height > 0) {
375 int offset;
376
377 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Nick Piggind5274262006-01-08 01:01:41 -0800378 if (!tag_get(slot, tag, offset))
379 tag_set(slot, tag, offset);
Christoph Lameter201b6262005-09-06 15:16:46 -0700380 slot = slot->slots[offset];
381 BUG_ON(slot == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 shift -= RADIX_TREE_MAP_SHIFT;
383 height--;
384 }
385
Christoph Lameter201b6262005-09-06 15:16:46 -0700386 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388EXPORT_SYMBOL(radix_tree_tag_set);
389
390/**
391 * radix_tree_tag_clear - clear a tag on a radix tree node
392 * @root: radix tree root
393 * @index: index key
394 * @tag: tag index
395 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800396 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
397 * corresponding to @index in the radix tree. If
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * 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 */
404void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800405 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
Christoph Lameter201b6262005-09-06 15:16:46 -0700408 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 unsigned int height, shift;
410 void *ret = NULL;
411
412 height = root->height;
413 if (index > radix_tree_maxindex(height))
414 goto out;
415
416 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
417 pathp->node = NULL;
Christoph Lameter201b6262005-09-06 15:16:46 -0700418 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 while (height > 0) {
421 int offset;
422
Christoph Lameter201b6262005-09-06 15:16:46 -0700423 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 goto out;
425
426 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
427 pathp[1].offset = offset;
Christoph Lameter201b6262005-09-06 15:16:46 -0700428 pathp[1].node = slot;
429 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 pathp++;
431 shift -= RADIX_TREE_MAP_SHIFT;
432 height--;
433 }
434
Christoph Lameter201b6262005-09-06 15:16:46 -0700435 ret = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (ret == NULL)
437 goto out;
438
439 do {
Nick Piggind5274262006-01-08 01:01:41 -0800440 if (!tag_get(pathp->node, tag, pathp->offset))
441 goto out;
Christoph Lameter201b6262005-09-06 15:16:46 -0700442 tag_clear(pathp->node, tag, pathp->offset);
Nick Piggin6e954b92006-01-08 01:01:40 -0800443 if (any_tag_set(pathp->node, tag))
444 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 pathp--;
Christoph Lameter201b6262005-09-06 15:16:46 -0700446 } while (pathp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447out:
448 return ret;
449}
450EXPORT_SYMBOL(radix_tree_tag_clear);
451
452#ifndef __KERNEL__ /* Only the test harness uses this at present */
453/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700454 * radix_tree_tag_get - get a tag on a radix tree node
455 * @root: radix tree root
456 * @index: index key
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800457 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700459 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700461 * 0: tag not present
462 * 1: tag present, set
463 * -1: tag present, unset
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
465int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800466 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700469 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int saw_unset_tag = 0;
471
472 height = root->height;
473 if (index > radix_tree_maxindex(height))
474 return 0;
475
476 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Christoph Lameter201b6262005-09-06 15:16:46 -0700477 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 for ( ; ; ) {
480 int offset;
481
Christoph Lameter201b6262005-09-06 15:16:46 -0700482 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 0;
484
485 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
486
487 /*
488 * This is just a debug check. Later, we can bale as soon as
489 * we see an unset tag.
490 */
Christoph Lameter201b6262005-09-06 15:16:46 -0700491 if (!tag_get(slot, tag, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 saw_unset_tag = 1;
493 if (height == 1) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700494 int ret = tag_get(slot, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 BUG_ON(ret && saw_unset_tag);
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700497 return ret ? 1 : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700499 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 shift -= RADIX_TREE_MAP_SHIFT;
501 height--;
502 }
503}
504EXPORT_SYMBOL(radix_tree_tag_get);
505#endif
506
507static unsigned int
508__lookup(struct radix_tree_root *root, void **results, unsigned long index,
509 unsigned int max_items, unsigned long *next_index)
510{
511 unsigned int nr_found = 0;
Christoph Lameter201b6262005-09-06 15:16:46 -0700512 unsigned int shift, height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct radix_tree_node *slot;
Christoph Lameter201b6262005-09-06 15:16:46 -0700514 unsigned long i;
515
516 height = root->height;
517 if (height == 0)
518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
521 slot = root->rnode;
522
Christoph Lameter201b6262005-09-06 15:16:46 -0700523 for ( ; height > 1; height--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Christoph Lameter201b6262005-09-06 15:16:46 -0700525 for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
526 i < RADIX_TREE_MAP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (slot->slots[i] != NULL)
528 break;
529 index &= ~((1UL << shift) - 1);
530 index += 1UL << shift;
531 if (index == 0)
532 goto out; /* 32-bit wraparound */
533 }
534 if (i == RADIX_TREE_MAP_SIZE)
535 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 shift -= RADIX_TREE_MAP_SHIFT;
538 slot = slot->slots[i];
539 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700540
541 /* Bottom level: grab some items */
542 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
543 index++;
544 if (slot->slots[i]) {
545 results[nr_found++] = slot->slots[i];
546 if (nr_found == max_items)
547 goto out;
548 }
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550out:
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 */
568unsigned int
569radix_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}
591EXPORT_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 */
597static unsigned int
598__lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800599 unsigned int max_items, unsigned long *next_index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
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 }
641out:
642 *next_index = index;
643 return nr_found;
644}
645
646/**
647 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
648 * based on a tag
649 * @root: radix tree root
650 * @results: where the results of the lookup are placed
651 * @first_index: start the lookup from this key
652 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800653 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 *
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 */
659unsigned int
660radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800661 unsigned long first_index, unsigned int max_items,
662 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
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}
683EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
684
685/**
Nick Piggina5f51c92006-01-08 01:01:41 -0800686 * radix_tree_shrink - shrink height of a radix tree to minimal
687 * @root radix tree root
688 */
689static inline void radix_tree_shrink(struct radix_tree_root *root)
690{
691 /* try to shrink tree height */
692 while (root->height > 1 &&
693 root->rnode->count == 1 &&
694 root->rnode->slots[0]) {
695 struct radix_tree_node *to_free = root->rnode;
696
697 root->rnode = to_free->slots[0];
698 root->height--;
699 /* must only free zeroed nodes into the slab */
700 tag_clear(to_free, 0, 0);
701 tag_clear(to_free, 1, 0);
702 to_free->slots[0] = NULL;
703 to_free->count = 0;
704 radix_tree_node_free(to_free);
705 }
706}
707
708/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * 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 */
717void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
718{
719 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
720 struct radix_tree_path *orig_pathp;
Christoph Lameter201b6262005-09-06 15:16:46 -0700721 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unsigned int height, shift;
723 void *ret = NULL;
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800724 char tags[RADIX_TREE_MAX_TAGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int nr_cleared_tags;
Nick Piggind5274262006-01-08 01:01:41 -0800726 int tag;
727 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 height = root->height;
730 if (index > radix_tree_maxindex(height))
731 goto out;
732
733 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
734 pathp->node = NULL;
Christoph Lameter201b6262005-09-06 15:16:46 -0700735 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Christoph Lameter201b6262005-09-06 15:16:46 -0700737 for ( ; height > 0; height--) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700738 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto out;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 pathp++;
Nick Piggind5274262006-01-08 01:01:41 -0800742 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
743 pathp->offset = offset;
744 pathp->node = slot;
745 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 shift -= RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Christoph Lameter201b6262005-09-06 15:16:46 -0700749 ret = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (ret == NULL)
751 goto out;
752
753 orig_pathp = pathp;
754
755 /*
756 * Clear all tags associated with the just-deleted item
757 */
Nick Piggind5274262006-01-08 01:01:41 -0800758 nr_cleared_tags = 0;
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800759 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
NeilBrown90f9dd82006-02-16 14:43:01 +1100760 tags[tag] = 1;
Nick Piggind5274262006-01-08 01:01:41 -0800761 if (tag_get(pathp->node, tag, pathp->offset)) {
762 tag_clear(pathp->node, tag, pathp->offset);
NeilBrown90f9dd82006-02-16 14:43:01 +1100763 if (!any_tag_set(pathp->node, tag)) {
764 tags[tag] = 0;
765 nr_cleared_tags++;
766 }
767 }
Nick Piggind5274262006-01-08 01:01:41 -0800768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Nick Piggind5274262006-01-08 01:01:41 -0800770 for (pathp--; nr_cleared_tags && pathp->node; pathp--) {
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800771 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (tags[tag])
773 continue;
774
Christoph Lameter201b6262005-09-06 15:16:46 -0700775 tag_clear(pathp->node, tag, pathp->offset);
Nick Piggin6e954b92006-01-08 01:01:40 -0800776 if (any_tag_set(pathp->node, tag)) {
777 tags[tag] = 1;
778 nr_cleared_tags--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 }
Nick Piggind5274262006-01-08 01:01:41 -0800781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Christoph Lameter201b6262005-09-06 15:16:46 -0700783 /* Now free the nodes we do not need anymore */
784 for (pathp = orig_pathp; pathp->node; pathp--) {
785 pathp->node->slots[pathp->offset] = NULL;
Nick Piggina5f51c92006-01-08 01:01:41 -0800786 pathp->node->count--;
787
788 if (pathp->node->count) {
789 if (pathp->node == root->rnode)
790 radix_tree_shrink(root);
Christoph Lameter201b6262005-09-06 15:16:46 -0700791 goto out;
Nick Piggina5f51c92006-01-08 01:01:41 -0800792 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700793
794 /* Node with zero slots in use so free it */
795 radix_tree_node_free(pathp->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700797 root->rnode = NULL;
798 root->height = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799out:
800 return ret;
801}
802EXPORT_SYMBOL(radix_tree_delete);
803
804/**
805 * radix_tree_tagged - test whether any items in the tree are tagged
806 * @root: radix tree root
807 * @tag: tag to test
808 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800809int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Nick Piggin6e954b92006-01-08 01:01:40 -0800811 struct radix_tree_node *rnode;
812 rnode = root->rnode;
813 if (!rnode)
814 return 0;
815 return any_tag_set(rnode, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817EXPORT_SYMBOL(radix_tree_tagged);
818
819static void
820radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
821{
822 memset(node, 0, sizeof(struct radix_tree_node));
823}
824
825static __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
835static __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
844static 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
865void __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}