blob: b32efae7688e316e8825df6fd76a316a2c6f18bf [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__
Nick Piggincfd9b7d2006-06-23 02:03:22 -070036#define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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
Nick Piggin612d6c12006-06-23 02:03:22 -070077static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
78{
79 return root->gfp_mask & __GFP_BITS_MASK;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * This assumes that the caller has performed appropriate preallocation, and
84 * that the caller has pinned this thread of control to the current CPU.
85 */
86static struct radix_tree_node *
87radix_tree_node_alloc(struct radix_tree_root *root)
88{
89 struct radix_tree_node *ret;
Nick Piggin612d6c12006-06-23 02:03:22 -070090 gfp_t gfp_mask = root_gfp_mask(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Nick Piggin612d6c12006-06-23 02:03:22 -070092 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
93 if (ret == NULL && !(gfp_mask & __GFP_WAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct radix_tree_preload *rtp;
95
96 rtp = &__get_cpu_var(radix_tree_preloads);
97 if (rtp->nr) {
98 ret = rtp->nodes[rtp->nr - 1];
99 rtp->nodes[rtp->nr - 1] = NULL;
100 rtp->nr--;
101 }
102 }
103 return ret;
104}
105
106static inline void
107radix_tree_node_free(struct radix_tree_node *node)
108{
109 kmem_cache_free(radix_tree_node_cachep, node);
110}
111
112/*
113 * Load up this CPU's radix_tree_node buffer with sufficient objects to
114 * ensure that the addition of a single element in the tree cannot fail. On
115 * success, return zero, with preemption disabled. On error, return -ENOMEM
116 * with preemption not disabled.
117 */
Al Virodd0fc662005-10-07 07:46:04 +0100118int radix_tree_preload(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 struct radix_tree_preload *rtp;
121 struct radix_tree_node *node;
122 int ret = -ENOMEM;
123
124 preempt_disable();
125 rtp = &__get_cpu_var(radix_tree_preloads);
126 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
127 preempt_enable();
128 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
129 if (node == NULL)
130 goto out;
131 preempt_disable();
132 rtp = &__get_cpu_var(radix_tree_preloads);
133 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
134 rtp->nodes[rtp->nr++] = node;
135 else
136 kmem_cache_free(radix_tree_node_cachep, node);
137 }
138 ret = 0;
139out:
140 return ret;
141}
142
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800143static inline void tag_set(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 __set_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800149static inline void tag_clear(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 __clear_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800155static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
156 int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Nick Piggind5274262006-01-08 01:01:41 -0800158 return test_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Nick Piggin612d6c12006-06-23 02:03:22 -0700161static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
162{
163 root->gfp_mask |= (1 << (tag + __GFP_BITS_SHIFT));
164}
165
166
167static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
168{
169 root->gfp_mask &= ~(1 << (tag + __GFP_BITS_SHIFT));
170}
171
172static inline void root_tag_clear_all(struct radix_tree_root *root)
173{
174 root->gfp_mask &= __GFP_BITS_MASK;
175}
176
177static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
178{
179 return root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
Nick Piggin6e954b92006-01-08 01:01:40 -0800183 * Returns 1 if any slot in the node has this tag set.
184 * Otherwise returns 0.
185 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800186static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
Nick Piggin6e954b92006-01-08 01:01:40 -0800187{
188 int idx;
189 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
190 if (node->tags[tag][idx])
191 return 1;
192 }
193 return 0;
194}
195
196/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * Return the maximum key which can be store into a
198 * radix tree with height HEIGHT.
199 */
200static inline unsigned long radix_tree_maxindex(unsigned int height)
201{
202 return height_to_maxindex[height];
203}
204
205/*
206 * Extend a radix tree so it can store key @index.
207 */
208static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
209{
210 struct radix_tree_node *node;
211 unsigned int height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int tag;
213
214 /* Figure out what the height should be. */
215 height = root->height + 1;
216 while (index > radix_tree_maxindex(height))
217 height++;
218
219 if (root->rnode == NULL) {
220 root->height = height;
221 goto out;
222 }
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 do {
225 if (!(node = radix_tree_node_alloc(root)))
226 return -ENOMEM;
227
228 /* Increase the height. */
229 node->slots[0] = root->rnode;
230
231 /* Propagate the aggregated tag info into the new root */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800232 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700233 if (root_tag_get(root, tag))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 tag_set(node, tag, 0);
235 }
236
237 node->count = 1;
238 root->rnode = node;
239 root->height++;
240 } while (height > root->height);
241out:
242 return 0;
243}
244
245/**
246 * radix_tree_insert - insert into a radix tree
247 * @root: radix tree root
248 * @index: index key
249 * @item: item to insert
250 *
251 * Insert an item into the radix tree at position @index.
252 */
253int radix_tree_insert(struct radix_tree_root *root,
254 unsigned long index, void *item)
255{
Christoph Lameter201b6262005-09-06 15:16:46 -0700256 struct radix_tree_node *node = NULL, *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 unsigned int height, shift;
258 int offset;
259 int error;
260
261 /* Make sure the tree is high enough. */
Nick Piggin612d6c12006-06-23 02:03:22 -0700262 if (index > radix_tree_maxindex(root->height)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 error = radix_tree_extend(root, index);
264 if (error)
265 return error;
266 }
267
Christoph Lameter201b6262005-09-06 15:16:46 -0700268 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 height = root->height;
270 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
271
272 offset = 0; /* uninitialised var warning */
Nick Piggin612d6c12006-06-23 02:03:22 -0700273 while (height > 0) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700274 if (slot == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* Have to add a child node. */
Christoph Lameter201b6262005-09-06 15:16:46 -0700276 if (!(slot = radix_tree_node_alloc(root)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return -ENOMEM;
Christoph Lameter201b6262005-09-06 15:16:46 -0700278 if (node) {
279 node->slots[offset] = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 node->count++;
Christoph Lameter201b6262005-09-06 15:16:46 -0700281 } else
282 root->rnode = slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284
285 /* Go a level down */
286 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Christoph Lameter201b6262005-09-06 15:16:46 -0700287 node = slot;
288 slot = node->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 shift -= RADIX_TREE_MAP_SHIFT;
290 height--;
Nick Piggin612d6c12006-06-23 02:03:22 -0700291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Christoph Lameter201b6262005-09-06 15:16:46 -0700293 if (slot != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -EEXIST;
Christoph Lameter201b6262005-09-06 15:16:46 -0700295
Nick Piggin612d6c12006-06-23 02:03:22 -0700296 if (node) {
297 node->count++;
298 node->slots[offset] = item;
299 BUG_ON(tag_get(node, 0, offset));
300 BUG_ON(tag_get(node, 1, offset));
301 } else {
302 root->rnode = item;
303 BUG_ON(root_tag_get(root, 0));
304 BUG_ON(root_tag_get(root, 1));
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
308}
309EXPORT_SYMBOL(radix_tree_insert);
310
Hans Reisera4331362005-11-07 00:59:29 -0800311static inline void **__lookup_slot(struct radix_tree_root *root,
312 unsigned long index)
313{
314 unsigned int height, shift;
315 struct radix_tree_node **slot;
316
317 height = root->height;
Nick Piggin612d6c12006-06-23 02:03:22 -0700318
Hans Reisera4331362005-11-07 00:59:29 -0800319 if (index > radix_tree_maxindex(height))
320 return NULL;
321
Nick Piggin612d6c12006-06-23 02:03:22 -0700322 if (height == 0 && root->rnode)
323 return (void **)&root->rnode;
324
Hans Reisera4331362005-11-07 00:59:29 -0800325 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
326 slot = &root->rnode;
327
328 while (height > 0) {
329 if (*slot == NULL)
330 return NULL;
331
332 slot = (struct radix_tree_node **)
333 ((*slot)->slots +
334 ((index >> shift) & RADIX_TREE_MAP_MASK));
335 shift -= RADIX_TREE_MAP_SHIFT;
336 height--;
337 }
338
339 return (void **)slot;
340}
341
342/**
343 * radix_tree_lookup_slot - lookup a slot in a radix tree
344 * @root: radix tree root
345 * @index: index key
346 *
347 * Lookup the slot corresponding to the position @index in the radix tree
348 * @root. This is useful for update-if-exists operations.
349 */
350void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
351{
352 return __lookup_slot(root, index);
353}
354EXPORT_SYMBOL(radix_tree_lookup_slot);
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/**
357 * radix_tree_lookup - perform lookup operation on a radix tree
358 * @root: radix tree root
359 * @index: index key
360 *
361 * Lookup the item at the position @index in the radix tree @root.
362 */
363void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
364{
Hans Reisera4331362005-11-07 00:59:29 -0800365 void **slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Hans Reisera4331362005-11-07 00:59:29 -0800367 slot = __lookup_slot(root, index);
368 return slot != NULL ? *slot : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370EXPORT_SYMBOL(radix_tree_lookup);
371
372/**
373 * radix_tree_tag_set - set a tag on a radix tree node
374 * @root: radix tree root
375 * @index: index key
376 * @tag: tag index
377 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800378 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
379 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * the root all the way down to the leaf node.
381 *
382 * Returns the address of the tagged item. Setting a tag on a not-present
383 * item is a bug.
384 */
385void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800386 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700389 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 height = root->height;
Peter Zijlstra4c91c362006-06-23 02:03:25 -0700392 BUG_ON(index > radix_tree_maxindex(height));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Christoph Lameter201b6262005-09-06 15:16:46 -0700394 slot = root->rnode;
Nick Piggin612d6c12006-06-23 02:03:22 -0700395 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 while (height > 0) {
398 int offset;
399
400 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Nick Piggind5274262006-01-08 01:01:41 -0800401 if (!tag_get(slot, tag, offset))
402 tag_set(slot, tag, offset);
Christoph Lameter201b6262005-09-06 15:16:46 -0700403 slot = slot->slots[offset];
404 BUG_ON(slot == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 shift -= RADIX_TREE_MAP_SHIFT;
406 height--;
407 }
408
Nick Piggin612d6c12006-06-23 02:03:22 -0700409 /* set the root's tag bit */
410 if (slot && !root_tag_get(root, tag))
411 root_tag_set(root, tag);
412
Christoph Lameter201b6262005-09-06 15:16:46 -0700413 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415EXPORT_SYMBOL(radix_tree_tag_set);
416
417/**
418 * radix_tree_tag_clear - clear a tag on a radix tree node
419 * @root: radix tree root
420 * @index: index key
421 * @tag: tag index
422 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800423 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
424 * corresponding to @index in the radix tree. If
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * this causes the leaf node to have no tags set then clear the tag in the
426 * next-to-leaf node, etc.
427 *
428 * Returns the address of the tagged item on success, else NULL. ie:
429 * has the same return value and semantics as radix_tree_lookup().
430 */
431void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800432 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
Nick Piggin612d6c12006-06-23 02:03:22 -0700435 struct radix_tree_node *slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 unsigned int height, shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 height = root->height;
439 if (index > radix_tree_maxindex(height))
440 goto out;
441
442 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
443 pathp->node = NULL;
Christoph Lameter201b6262005-09-06 15:16:46 -0700444 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 while (height > 0) {
447 int offset;
448
Christoph Lameter201b6262005-09-06 15:16:46 -0700449 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto out;
451
452 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
453 pathp[1].offset = offset;
Christoph Lameter201b6262005-09-06 15:16:46 -0700454 pathp[1].node = slot;
455 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 pathp++;
457 shift -= RADIX_TREE_MAP_SHIFT;
458 height--;
459 }
460
Nick Piggin612d6c12006-06-23 02:03:22 -0700461 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto out;
463
Nick Piggin612d6c12006-06-23 02:03:22 -0700464 while (pathp->node) {
Nick Piggind5274262006-01-08 01:01:41 -0800465 if (!tag_get(pathp->node, tag, pathp->offset))
466 goto out;
Christoph Lameter201b6262005-09-06 15:16:46 -0700467 tag_clear(pathp->node, tag, pathp->offset);
Nick Piggin6e954b92006-01-08 01:01:40 -0800468 if (any_tag_set(pathp->node, tag))
469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 pathp--;
Nick Piggin612d6c12006-06-23 02:03:22 -0700471 }
472
473 /* clear the root's tag bit */
474 if (root_tag_get(root, tag))
475 root_tag_clear(root, tag);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477out:
Nick Piggin612d6c12006-06-23 02:03:22 -0700478 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480EXPORT_SYMBOL(radix_tree_tag_clear);
481
482#ifndef __KERNEL__ /* Only the test harness uses this at present */
483/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700484 * radix_tree_tag_get - get a tag on a radix tree node
485 * @root: radix tree root
486 * @index: index key
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800487 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700489 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 *
Nick Piggin612d6c12006-06-23 02:03:22 -0700491 * 0: tag not present or not set
492 * 1: tag set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 */
494int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800495 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700498 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int saw_unset_tag = 0;
500
501 height = root->height;
502 if (index > radix_tree_maxindex(height))
503 return 0;
504
Nick Piggin612d6c12006-06-23 02:03:22 -0700505 /* check the root's tag bit */
506 if (!root_tag_get(root, tag))
507 return 0;
508
509 if (height == 0)
510 return 1;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Christoph Lameter201b6262005-09-06 15:16:46 -0700513 slot = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 for ( ; ; ) {
516 int offset;
517
Christoph Lameter201b6262005-09-06 15:16:46 -0700518 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return 0;
520
521 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
522
523 /*
524 * This is just a debug check. Later, we can bale as soon as
525 * we see an unset tag.
526 */
Christoph Lameter201b6262005-09-06 15:16:46 -0700527 if (!tag_get(slot, tag, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 saw_unset_tag = 1;
529 if (height == 1) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700530 int ret = tag_get(slot, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 BUG_ON(ret && saw_unset_tag);
Nick Piggin612d6c12006-06-23 02:03:22 -0700533 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700535 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 shift -= RADIX_TREE_MAP_SHIFT;
537 height--;
538 }
539}
540EXPORT_SYMBOL(radix_tree_tag_get);
541#endif
542
543static unsigned int
544__lookup(struct radix_tree_root *root, void **results, unsigned long index,
545 unsigned int max_items, unsigned long *next_index)
546{
547 unsigned int nr_found = 0;
Christoph Lameter201b6262005-09-06 15:16:46 -0700548 unsigned int shift, height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct radix_tree_node *slot;
Christoph Lameter201b6262005-09-06 15:16:46 -0700550 unsigned long i;
551
552 height = root->height;
Nick Piggin612d6c12006-06-23 02:03:22 -0700553 if (height == 0) {
554 if (root->rnode && index == 0)
555 results[nr_found++] = root->rnode;
Christoph Lameter201b6262005-09-06 15:16:46 -0700556 goto out;
Nick Piggin612d6c12006-06-23 02:03:22 -0700557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
560 slot = root->rnode;
561
Christoph Lameter201b6262005-09-06 15:16:46 -0700562 for ( ; height > 1; height--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Christoph Lameter201b6262005-09-06 15:16:46 -0700564 for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
565 i < RADIX_TREE_MAP_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (slot->slots[i] != NULL)
567 break;
568 index &= ~((1UL << shift) - 1);
569 index += 1UL << shift;
570 if (index == 0)
571 goto out; /* 32-bit wraparound */
572 }
573 if (i == RADIX_TREE_MAP_SIZE)
574 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 shift -= RADIX_TREE_MAP_SHIFT;
577 slot = slot->slots[i];
578 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700579
580 /* Bottom level: grab some items */
581 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
582 index++;
583 if (slot->slots[i]) {
584 results[nr_found++] = slot->slots[i];
585 if (nr_found == max_items)
586 goto out;
587 }
588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589out:
590 *next_index = index;
591 return nr_found;
592}
593
594/**
595 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
596 * @root: radix tree root
597 * @results: where the results of the lookup are placed
598 * @first_index: start the lookup from this key
599 * @max_items: place up to this many items at *results
600 *
601 * Performs an index-ascending scan of the tree for present items. Places
602 * them at *@results and returns the number of items which were placed at
603 * *@results.
604 *
605 * The implementation is naive.
606 */
607unsigned int
608radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
609 unsigned long first_index, unsigned int max_items)
610{
611 const unsigned long max_index = radix_tree_maxindex(root->height);
612 unsigned long cur_index = first_index;
613 unsigned int ret = 0;
614
615 while (ret < max_items) {
616 unsigned int nr_found;
617 unsigned long next_index; /* Index of next search */
618
619 if (cur_index > max_index)
620 break;
621 nr_found = __lookup(root, results + ret, cur_index,
622 max_items - ret, &next_index);
623 ret += nr_found;
624 if (next_index == 0)
625 break;
626 cur_index = next_index;
627 }
628 return ret;
629}
630EXPORT_SYMBOL(radix_tree_gang_lookup);
631
632/*
633 * FIXME: the two tag_get()s here should use find_next_bit() instead of
634 * open-coding the search.
635 */
636static unsigned int
637__lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800638 unsigned int max_items, unsigned long *next_index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 unsigned int nr_found = 0;
641 unsigned int shift;
642 unsigned int height = root->height;
643 struct radix_tree_node *slot;
644
Nick Piggin612d6c12006-06-23 02:03:22 -0700645 if (height == 0) {
646 if (root->rnode && index == 0)
647 results[nr_found++] = root->rnode;
648 goto out;
649 }
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
652 slot = root->rnode;
653
Nick Piggin612d6c12006-06-23 02:03:22 -0700654 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
656
657 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
658 if (tag_get(slot, tag, i)) {
659 BUG_ON(slot->slots[i] == NULL);
660 break;
661 }
662 index &= ~((1UL << shift) - 1);
663 index += 1UL << shift;
664 if (index == 0)
665 goto out; /* 32-bit wraparound */
666 }
667 if (i == RADIX_TREE_MAP_SIZE)
668 goto out;
669 height--;
670 if (height == 0) { /* Bottom level: grab some items */
671 unsigned long j = index & RADIX_TREE_MAP_MASK;
672
673 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
674 index++;
675 if (tag_get(slot, tag, j)) {
676 BUG_ON(slot->slots[j] == NULL);
677 results[nr_found++] = slot->slots[j];
678 if (nr_found == max_items)
679 goto out;
680 }
681 }
682 }
683 shift -= RADIX_TREE_MAP_SHIFT;
684 slot = slot->slots[i];
Nick Piggin612d6c12006-06-23 02:03:22 -0700685 } while (height > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686out:
687 *next_index = index;
688 return nr_found;
689}
690
691/**
692 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
693 * based on a tag
694 * @root: radix tree root
695 * @results: where the results of the lookup are placed
696 * @first_index: start the lookup from this key
697 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800698 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *
700 * Performs an index-ascending scan of the tree for present items which
701 * have the tag indexed by @tag set. Places the items at *@results and
702 * returns the number of items which were placed at *@results.
703 */
704unsigned int
705radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800706 unsigned long first_index, unsigned int max_items,
707 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 const unsigned long max_index = radix_tree_maxindex(root->height);
710 unsigned long cur_index = first_index;
711 unsigned int ret = 0;
712
Nick Piggin612d6c12006-06-23 02:03:22 -0700713 /* check the root's tag bit */
714 if (!root_tag_get(root, tag))
715 return 0;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 while (ret < max_items) {
718 unsigned int nr_found;
719 unsigned long next_index; /* Index of next search */
720
721 if (cur_index > max_index)
722 break;
723 nr_found = __lookup_tag(root, results + ret, cur_index,
724 max_items - ret, &next_index, tag);
725 ret += nr_found;
726 if (next_index == 0)
727 break;
728 cur_index = next_index;
729 }
730 return ret;
731}
732EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
733
734/**
Nick Piggina5f51c92006-01-08 01:01:41 -0800735 * radix_tree_shrink - shrink height of a radix tree to minimal
736 * @root radix tree root
737 */
738static inline void radix_tree_shrink(struct radix_tree_root *root)
739{
740 /* try to shrink tree height */
Nick Piggin612d6c12006-06-23 02:03:22 -0700741 while (root->height > 0 &&
Nick Piggina5f51c92006-01-08 01:01:41 -0800742 root->rnode->count == 1 &&
743 root->rnode->slots[0]) {
744 struct radix_tree_node *to_free = root->rnode;
745
746 root->rnode = to_free->slots[0];
747 root->height--;
748 /* must only free zeroed nodes into the slab */
749 tag_clear(to_free, 0, 0);
750 tag_clear(to_free, 1, 0);
751 to_free->slots[0] = NULL;
752 to_free->count = 0;
753 radix_tree_node_free(to_free);
754 }
755}
756
757/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * radix_tree_delete - delete an item from a radix tree
759 * @root: radix tree root
760 * @index: index key
761 *
762 * Remove the item at @index from the radix tree rooted at @root.
763 *
764 * Returns the address of the deleted item, or NULL if it was not present.
765 */
766void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
767{
768 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
Nick Piggin612d6c12006-06-23 02:03:22 -0700769 struct radix_tree_node *slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 unsigned int height, shift;
Nick Piggind5274262006-01-08 01:01:41 -0800771 int tag;
772 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 height = root->height;
775 if (index > radix_tree_maxindex(height))
776 goto out;
777
Nick Piggin612d6c12006-06-23 02:03:22 -0700778 slot = root->rnode;
779 if (height == 0 && root->rnode) {
780 root_tag_clear_all(root);
781 root->rnode = NULL;
782 goto out;
783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
786 pathp->node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Nick Piggin612d6c12006-06-23 02:03:22 -0700788 do {
Christoph Lameter201b6262005-09-06 15:16:46 -0700789 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto out;
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 pathp++;
Nick Piggind5274262006-01-08 01:01:41 -0800793 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
794 pathp->offset = offset;
795 pathp->node = slot;
796 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 shift -= RADIX_TREE_MAP_SHIFT;
Nick Piggin612d6c12006-06-23 02:03:22 -0700798 height--;
799 } while (height > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Nick Piggin612d6c12006-06-23 02:03:22 -0700801 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 goto out;
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /*
805 * Clear all tags associated with the just-deleted item
806 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800807 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700808 if (tag_get(pathp->node, tag, pathp->offset))
809 radix_tree_tag_clear(root, index, tag);
Nick Piggind5274262006-01-08 01:01:41 -0800810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Christoph Lameter201b6262005-09-06 15:16:46 -0700812 /* Now free the nodes we do not need anymore */
Nick Piggin612d6c12006-06-23 02:03:22 -0700813 while (pathp->node) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700814 pathp->node->slots[pathp->offset] = NULL;
Nick Piggina5f51c92006-01-08 01:01:41 -0800815 pathp->node->count--;
816
817 if (pathp->node->count) {
818 if (pathp->node == root->rnode)
819 radix_tree_shrink(root);
Christoph Lameter201b6262005-09-06 15:16:46 -0700820 goto out;
Nick Piggina5f51c92006-01-08 01:01:41 -0800821 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700822
823 /* Node with zero slots in use so free it */
824 radix_tree_node_free(pathp->node);
Nick Piggin612d6c12006-06-23 02:03:22 -0700825
826 pathp--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Nick Piggin612d6c12006-06-23 02:03:22 -0700828 root_tag_clear_all(root);
Christoph Lameter201b6262005-09-06 15:16:46 -0700829 root->height = 0;
Nick Piggin612d6c12006-06-23 02:03:22 -0700830 root->rnode = NULL;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832out:
Nick Piggin612d6c12006-06-23 02:03:22 -0700833 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835EXPORT_SYMBOL(radix_tree_delete);
836
837/**
838 * radix_tree_tagged - test whether any items in the tree are tagged
839 * @root: radix tree root
840 * @tag: tag to test
841 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800842int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Nick Piggin612d6c12006-06-23 02:03:22 -0700844 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846EXPORT_SYMBOL(radix_tree_tagged);
847
848static void
849radix_tree_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags)
850{
851 memset(node, 0, sizeof(struct radix_tree_node));
852}
853
854static __init unsigned long __maxindex(unsigned int height)
855{
856 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
857 unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
858
859 if (tmp >= RADIX_TREE_INDEX_BITS)
860 index = ~0UL;
861 return index;
862}
863
864static __init void radix_tree_init_maxindex(void)
865{
866 unsigned int i;
867
868 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
869 height_to_maxindex[i] = __maxindex(i);
870}
871
872#ifdef CONFIG_HOTPLUG_CPU
873static int radix_tree_callback(struct notifier_block *nfb,
874 unsigned long action,
875 void *hcpu)
876{
877 int cpu = (long)hcpu;
878 struct radix_tree_preload *rtp;
879
880 /* Free per-cpu pool of perloaded nodes */
881 if (action == CPU_DEAD) {
882 rtp = &per_cpu(radix_tree_preloads, cpu);
883 while (rtp->nr) {
884 kmem_cache_free(radix_tree_node_cachep,
885 rtp->nodes[rtp->nr-1]);
886 rtp->nodes[rtp->nr-1] = NULL;
887 rtp->nr--;
888 }
889 }
890 return NOTIFY_OK;
891}
892#endif /* CONFIG_HOTPLUG_CPU */
893
894void __init radix_tree_init(void)
895{
896 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
897 sizeof(struct radix_tree_node), 0,
898 SLAB_PANIC, radix_tree_node_ctor, NULL);
899 radix_tree_init_maxindex();
900 hotcpu_notifier(radix_tree_callback, 0);
901}