blob: 0eee4020a7be750a6d601c039ab2bca2a6c71111 [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>
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08005 * Copyright (C) 2006 Nick Piggin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2, or (at
10 * your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/errno.h>
23#include <linux/init.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/radix-tree.h>
27#include <linux/percpu.h>
28#include <linux/slab.h>
29#include <linux/notifier.h>
30#include <linux/cpu.h>
31#include <linux/gfp.h>
32#include <linux/string.h>
33#include <linux/bitops.h>
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080034#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36
37#ifdef __KERNEL__
Nick Piggincfd9b7d2006-06-23 02:03:22 -070038#define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#else
40#define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
41#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
44#define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
45
46#define RADIX_TREE_TAG_LONGS \
47 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
48
49struct radix_tree_node {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080050 unsigned int height; /* Height from the bottom */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 unsigned int count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080052 struct rcu_head rcu_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 void *slots[RADIX_TREE_MAP_SIZE];
Jonathan Corbetdaff89f2006-03-25 03:08:05 -080054 unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57struct radix_tree_path {
Christoph Lameter201b6262005-09-06 15:16:46 -070058 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 int offset;
60};
61
62#define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
63#define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
64
Christoph Lameter6c036522005-07-07 17:56:59 -070065static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*
68 * Radix tree node cache.
69 */
Christoph Lametere18b8902006-12-06 20:33:20 -080070static struct kmem_cache *radix_tree_node_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/*
73 * Per-cpu pool of preloaded nodes
74 */
75struct radix_tree_preload {
76 int nr;
77 struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
78};
79DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
80
Nick Piggin612d6c12006-06-23 02:03:22 -070081static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
82{
83 return root->gfp_mask & __GFP_BITS_MASK;
84}
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
87 * This assumes that the caller has performed appropriate preallocation, and
88 * that the caller has pinned this thread of control to the current CPU.
89 */
90static struct radix_tree_node *
91radix_tree_node_alloc(struct radix_tree_root *root)
92{
93 struct radix_tree_node *ret;
Nick Piggin612d6c12006-06-23 02:03:22 -070094 gfp_t gfp_mask = root_gfp_mask(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Nick Piggin612d6c12006-06-23 02:03:22 -070096 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
97 if (ret == NULL && !(gfp_mask & __GFP_WAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct radix_tree_preload *rtp;
99
100 rtp = &__get_cpu_var(radix_tree_preloads);
101 if (rtp->nr) {
102 ret = rtp->nodes[rtp->nr - 1];
103 rtp->nodes[rtp->nr - 1] = NULL;
104 rtp->nr--;
105 }
106 }
Nick Pigginc0bc9872007-10-16 01:24:42 -0700107 BUG_ON(radix_tree_is_indirect_ptr(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return ret;
109}
110
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800111static void radix_tree_node_rcu_free(struct rcu_head *head)
112{
113 struct radix_tree_node *node =
114 container_of(head, struct radix_tree_node, rcu_head);
115 kmem_cache_free(radix_tree_node_cachep, node);
116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static inline void
119radix_tree_node_free(struct radix_tree_node *node)
120{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800121 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124/*
125 * Load up this CPU's radix_tree_node buffer with sufficient objects to
126 * ensure that the addition of a single element in the tree cannot fail. On
127 * success, return zero, with preemption disabled. On error, return -ENOMEM
128 * with preemption not disabled.
129 */
Al Virodd0fc662005-10-07 07:46:04 +0100130int radix_tree_preload(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct radix_tree_preload *rtp;
133 struct radix_tree_node *node;
134 int ret = -ENOMEM;
135
136 preempt_disable();
137 rtp = &__get_cpu_var(radix_tree_preloads);
138 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
139 preempt_enable();
140 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
141 if (node == NULL)
142 goto out;
143 preempt_disable();
144 rtp = &__get_cpu_var(radix_tree_preloads);
145 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
146 rtp->nodes[rtp->nr++] = node;
147 else
148 kmem_cache_free(radix_tree_node_cachep, node);
149 }
150 ret = 0;
151out:
152 return ret;
153}
David Chinnerd7f09232007-07-14 16:05:04 +1000154EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800156static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
157 int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Nick Piggind5274262006-01-08 01:01:41 -0800159 __set_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800162static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
163 int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Nick Piggind5274262006-01-08 01:01:41 -0800165 __clear_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800168static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
169 int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Nick Piggind5274262006-01-08 01:01:41 -0800171 return test_bit(offset, node->tags[tag]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Nick Piggin612d6c12006-06-23 02:03:22 -0700174static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
175{
Al Viro20241ad2006-10-10 22:47:57 +0100176 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
Nick Piggin612d6c12006-06-23 02:03:22 -0700177}
178
179
180static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
181{
Al Viro20241ad2006-10-10 22:47:57 +0100182 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
Nick Piggin612d6c12006-06-23 02:03:22 -0700183}
184
185static inline void root_tag_clear_all(struct radix_tree_root *root)
186{
187 root->gfp_mask &= __GFP_BITS_MASK;
188}
189
190static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
191{
Al Viro20241ad2006-10-10 22:47:57 +0100192 return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
Nick Piggin612d6c12006-06-23 02:03:22 -0700193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/*
Nick Piggin6e954b92006-01-08 01:01:40 -0800196 * Returns 1 if any slot in the node has this tag set.
197 * Otherwise returns 0.
198 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800199static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
Nick Piggin6e954b92006-01-08 01:01:40 -0800200{
201 int idx;
202 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
203 if (node->tags[tag][idx])
204 return 1;
205 }
206 return 0;
207}
208
209/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * Return the maximum key which can be store into a
211 * radix tree with height HEIGHT.
212 */
213static inline unsigned long radix_tree_maxindex(unsigned int height)
214{
215 return height_to_maxindex[height];
216}
217
218/*
219 * Extend a radix tree so it can store key @index.
220 */
221static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
222{
223 struct radix_tree_node *node;
224 unsigned int height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int tag;
226
227 /* Figure out what the height should be. */
228 height = root->height + 1;
229 while (index > radix_tree_maxindex(height))
230 height++;
231
232 if (root->rnode == NULL) {
233 root->height = height;
234 goto out;
235 }
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 do {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800238 unsigned int newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!(node = radix_tree_node_alloc(root)))
240 return -ENOMEM;
241
242 /* Increase the height. */
Nick Pigginc0bc9872007-10-16 01:24:42 -0700243 node->slots[0] = radix_tree_indirect_to_ptr(root->rnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* Propagate the aggregated tag info into the new root */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800246 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700247 if (root_tag_get(root, tag))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 tag_set(node, tag, 0);
249 }
250
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800251 newheight = root->height+1;
252 node->height = newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 node->count = 1;
Nick Pigginc0bc9872007-10-16 01:24:42 -0700254 node = radix_tree_ptr_to_indirect(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800255 rcu_assign_pointer(root->rnode, node);
256 root->height = newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 } while (height > root->height);
258out:
259 return 0;
260}
261
262/**
263 * radix_tree_insert - insert into a radix tree
264 * @root: radix tree root
265 * @index: index key
266 * @item: item to insert
267 *
268 * Insert an item into the radix tree at position @index.
269 */
270int radix_tree_insert(struct radix_tree_root *root,
271 unsigned long index, void *item)
272{
Christoph Lameter201b6262005-09-06 15:16:46 -0700273 struct radix_tree_node *node = NULL, *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 unsigned int height, shift;
275 int offset;
276 int error;
277
Nick Pigginc0bc9872007-10-16 01:24:42 -0700278 BUG_ON(radix_tree_is_indirect_ptr(item));
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* Make sure the tree is high enough. */
Nick Piggin612d6c12006-06-23 02:03:22 -0700281 if (index > radix_tree_maxindex(root->height)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 error = radix_tree_extend(root, index);
283 if (error)
284 return error;
285 }
286
Nick Pigginc0bc9872007-10-16 01:24:42 -0700287 slot = radix_tree_indirect_to_ptr(root->rnode);
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 height = root->height;
290 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
291
292 offset = 0; /* uninitialised var warning */
Nick Piggin612d6c12006-06-23 02:03:22 -0700293 while (height > 0) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700294 if (slot == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* Have to add a child node. */
Christoph Lameter201b6262005-09-06 15:16:46 -0700296 if (!(slot = radix_tree_node_alloc(root)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return -ENOMEM;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800298 slot->height = height;
Christoph Lameter201b6262005-09-06 15:16:46 -0700299 if (node) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800300 rcu_assign_pointer(node->slots[offset], slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 node->count++;
Christoph Lameter201b6262005-09-06 15:16:46 -0700302 } else
Nick Pigginc0bc9872007-10-16 01:24:42 -0700303 rcu_assign_pointer(root->rnode,
304 radix_tree_ptr_to_indirect(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
307 /* Go a level down */
308 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Christoph Lameter201b6262005-09-06 15:16:46 -0700309 node = slot;
310 slot = node->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 shift -= RADIX_TREE_MAP_SHIFT;
312 height--;
Nick Piggin612d6c12006-06-23 02:03:22 -0700313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Christoph Lameter201b6262005-09-06 15:16:46 -0700315 if (slot != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -EEXIST;
Christoph Lameter201b6262005-09-06 15:16:46 -0700317
Nick Piggin612d6c12006-06-23 02:03:22 -0700318 if (node) {
319 node->count++;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800320 rcu_assign_pointer(node->slots[offset], item);
Nick Piggin612d6c12006-06-23 02:03:22 -0700321 BUG_ON(tag_get(node, 0, offset));
322 BUG_ON(tag_get(node, 1, offset));
323 } else {
Nick Pigginc0bc9872007-10-16 01:24:42 -0700324 rcu_assign_pointer(root->rnode, item);
Nick Piggin612d6c12006-06-23 02:03:22 -0700325 BUG_ON(root_tag_get(root, 0));
326 BUG_ON(root_tag_get(root, 1));
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330}
331EXPORT_SYMBOL(radix_tree_insert);
332
Hans Reisera4331362005-11-07 00:59:29 -0800333/**
334 * radix_tree_lookup_slot - lookup a slot in a radix tree
335 * @root: radix tree root
336 * @index: index key
337 *
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800338 * Returns: the slot corresponding to the position @index in the
339 * radix tree @root. This is useful for update-if-exists operations.
340 *
341 * This function cannot be called under rcu_read_lock, it must be
342 * excluded from writers, as must the returned slot for subsequent
343 * use by radix_tree_deref_slot() and radix_tree_replace slot.
344 * Caller must hold tree write locked across slot lookup and
345 * replace.
Hans Reisera4331362005-11-07 00:59:29 -0800346 */
347void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
348{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800349 unsigned int height, shift;
350 struct radix_tree_node *node, **slot;
351
352 node = root->rnode;
353 if (node == NULL)
354 return NULL;
355
Nick Pigginc0bc9872007-10-16 01:24:42 -0700356 if (!radix_tree_is_indirect_ptr(node)) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800357 if (index > 0)
358 return NULL;
359 return (void **)&root->rnode;
360 }
Nick Pigginc0bc9872007-10-16 01:24:42 -0700361 node = radix_tree_indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800362
363 height = node->height;
364 if (index > radix_tree_maxindex(height))
365 return NULL;
366
367 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
368
369 do {
370 slot = (struct radix_tree_node **)
371 (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
372 node = *slot;
373 if (node == NULL)
374 return NULL;
375
376 shift -= RADIX_TREE_MAP_SHIFT;
377 height--;
378 } while (height > 0);
379
380 return (void **)slot;
Hans Reisera4331362005-11-07 00:59:29 -0800381}
382EXPORT_SYMBOL(radix_tree_lookup_slot);
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/**
385 * radix_tree_lookup - perform lookup operation on a radix tree
386 * @root: radix tree root
387 * @index: index key
388 *
389 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800390 *
391 * This function can be called under rcu_read_lock, however the caller
392 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
393 * them safely). No RCU barriers are required to access or modify the
394 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
396void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
397{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800398 unsigned int height, shift;
399 struct radix_tree_node *node, **slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800401 node = rcu_dereference(root->rnode);
402 if (node == NULL)
403 return NULL;
404
Nick Pigginc0bc9872007-10-16 01:24:42 -0700405 if (!radix_tree_is_indirect_ptr(node)) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800406 if (index > 0)
407 return NULL;
Nick Pigginc0bc9872007-10-16 01:24:42 -0700408 return node;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800409 }
Nick Pigginc0bc9872007-10-16 01:24:42 -0700410 node = radix_tree_indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800411
412 height = node->height;
413 if (index > radix_tree_maxindex(height))
414 return NULL;
415
416 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
417
418 do {
419 slot = (struct radix_tree_node **)
420 (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
421 node = rcu_dereference(*slot);
422 if (node == NULL)
423 return NULL;
424
425 shift -= RADIX_TREE_MAP_SHIFT;
426 height--;
427 } while (height > 0);
428
429 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431EXPORT_SYMBOL(radix_tree_lookup);
432
433/**
434 * radix_tree_tag_set - set a tag on a radix tree node
435 * @root: radix tree root
436 * @index: index key
437 * @tag: tag index
438 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800439 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
440 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * the root all the way down to the leaf node.
442 *
443 * Returns the address of the tagged item. Setting a tag on a not-present
444 * item is a bug.
445 */
446void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800447 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700450 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 height = root->height;
Peter Zijlstra4c91c362006-06-23 02:03:25 -0700453 BUG_ON(index > radix_tree_maxindex(height));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Nick Pigginc0bc9872007-10-16 01:24:42 -0700455 slot = radix_tree_indirect_to_ptr(root->rnode);
Nick Piggin612d6c12006-06-23 02:03:22 -0700456 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 while (height > 0) {
459 int offset;
460
461 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Nick Piggind5274262006-01-08 01:01:41 -0800462 if (!tag_get(slot, tag, offset))
463 tag_set(slot, tag, offset);
Christoph Lameter201b6262005-09-06 15:16:46 -0700464 slot = slot->slots[offset];
465 BUG_ON(slot == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 shift -= RADIX_TREE_MAP_SHIFT;
467 height--;
468 }
469
Nick Piggin612d6c12006-06-23 02:03:22 -0700470 /* set the root's tag bit */
471 if (slot && !root_tag_get(root, tag))
472 root_tag_set(root, tag);
473
Christoph Lameter201b6262005-09-06 15:16:46 -0700474 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476EXPORT_SYMBOL(radix_tree_tag_set);
477
478/**
479 * radix_tree_tag_clear - clear a tag on a radix tree node
480 * @root: radix tree root
481 * @index: index key
482 * @tag: tag index
483 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800484 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
485 * corresponding to @index in the radix tree. If
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * this causes the leaf node to have no tags set then clear the tag in the
487 * next-to-leaf node, etc.
488 *
489 * Returns the address of the tagged item on success, else NULL. ie:
490 * has the same return value and semantics as radix_tree_lookup().
491 */
492void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800493 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
Nick Piggin612d6c12006-06-23 02:03:22 -0700496 struct radix_tree_node *slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 unsigned int height, shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 height = root->height;
500 if (index > radix_tree_maxindex(height))
501 goto out;
502
503 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
504 pathp->node = NULL;
Nick Pigginc0bc9872007-10-16 01:24:42 -0700505 slot = radix_tree_indirect_to_ptr(root->rnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 while (height > 0) {
508 int offset;
509
Christoph Lameter201b6262005-09-06 15:16:46 -0700510 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto out;
512
513 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
514 pathp[1].offset = offset;
Christoph Lameter201b6262005-09-06 15:16:46 -0700515 pathp[1].node = slot;
516 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 pathp++;
518 shift -= RADIX_TREE_MAP_SHIFT;
519 height--;
520 }
521
Nick Piggin612d6c12006-06-23 02:03:22 -0700522 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto out;
524
Nick Piggin612d6c12006-06-23 02:03:22 -0700525 while (pathp->node) {
Nick Piggind5274262006-01-08 01:01:41 -0800526 if (!tag_get(pathp->node, tag, pathp->offset))
527 goto out;
Christoph Lameter201b6262005-09-06 15:16:46 -0700528 tag_clear(pathp->node, tag, pathp->offset);
Nick Piggin6e954b92006-01-08 01:01:40 -0800529 if (any_tag_set(pathp->node, tag))
530 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 pathp--;
Nick Piggin612d6c12006-06-23 02:03:22 -0700532 }
533
534 /* clear the root's tag bit */
535 if (root_tag_get(root, tag))
536 root_tag_clear(root, tag);
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538out:
Nick Piggin612d6c12006-06-23 02:03:22 -0700539 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541EXPORT_SYMBOL(radix_tree_tag_clear);
542
543#ifndef __KERNEL__ /* Only the test harness uses this at present */
544/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700545 * radix_tree_tag_get - get a tag on a radix tree node
546 * @root: radix tree root
547 * @index: index key
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800548 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700550 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
Nick Piggin612d6c12006-06-23 02:03:22 -0700552 * 0: tag not present or not set
553 * 1: tag set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
555int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800556 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 unsigned int height, shift;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800559 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int saw_unset_tag = 0;
561
Nick Piggin612d6c12006-06-23 02:03:22 -0700562 /* check the root's tag bit */
563 if (!root_tag_get(root, tag))
564 return 0;
565
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800566 node = rcu_dereference(root->rnode);
567 if (node == NULL)
568 return 0;
569
Nick Pigginc0bc9872007-10-16 01:24:42 -0700570 if (!radix_tree_is_indirect_ptr(node))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800571 return (index == 0);
Nick Pigginc0bc9872007-10-16 01:24:42 -0700572 node = radix_tree_indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800573
574 height = node->height;
575 if (index > radix_tree_maxindex(height))
576 return 0;
Nick Piggin612d6c12006-06-23 02:03:22 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 for ( ; ; ) {
581 int offset;
582
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800583 if (node == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return 0;
585
586 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
587
588 /*
589 * This is just a debug check. Later, we can bale as soon as
590 * we see an unset tag.
591 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800592 if (!tag_get(node, tag, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 saw_unset_tag = 1;
594 if (height == 1) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800595 int ret = tag_get(node, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 BUG_ON(ret && saw_unset_tag);
Wu Fengguange5dcd902006-06-25 05:48:14 -0700598 return !!ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800600 node = rcu_dereference(node->slots[offset]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 shift -= RADIX_TREE_MAP_SHIFT;
602 height--;
603 }
604}
605EXPORT_SYMBOL(radix_tree_tag_get);
606#endif
607
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700608/**
609 * radix_tree_next_hole - find the next hole (not-present entry)
610 * @root: tree root
611 * @index: index key
612 * @max_scan: maximum range to search
613 *
614 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest
615 * indexed hole.
616 *
617 * Returns: the index of the hole if found, otherwise returns an index
618 * outside of the set specified (in which case 'return - index >= max_scan'
619 * will be true).
620 *
621 * radix_tree_next_hole may be called under rcu_read_lock. However, like
622 * radix_tree_gang_lookup, this will not atomically search a snapshot of the
623 * tree at a single point in time. For example, if a hole is created at index
624 * 5, then subsequently a hole is created at index 10, radix_tree_next_hole
625 * covering both indexes may return 10 if called under rcu_read_lock.
626 */
627unsigned long radix_tree_next_hole(struct radix_tree_root *root,
628 unsigned long index, unsigned long max_scan)
629{
630 unsigned long i;
631
632 for (i = 0; i < max_scan; i++) {
633 if (!radix_tree_lookup(root, index))
634 break;
635 index++;
636 if (index == 0)
637 break;
638 }
639
640 return index;
641}
642EXPORT_SYMBOL(radix_tree_next_hole);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644static unsigned int
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800645__lookup(struct radix_tree_node *slot, void **results, unsigned long index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 unsigned int max_items, unsigned long *next_index)
647{
648 unsigned int nr_found = 0;
Christoph Lameter201b6262005-09-06 15:16:46 -0700649 unsigned int shift, height;
Christoph Lameter201b6262005-09-06 15:16:46 -0700650 unsigned long i;
651
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800652 height = slot->height;
653 if (height == 0)
Christoph Lameter201b6262005-09-06 15:16:46 -0700654 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Christoph Lameter201b6262005-09-06 15:16:46 -0700657 for ( ; height > 1; height--) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800658 i = (index >> shift) & RADIX_TREE_MAP_MASK;
659 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (slot->slots[i] != NULL)
661 break;
662 index &= ~((1UL << shift) - 1);
663 index += 1UL << shift;
664 if (index == 0)
665 goto out; /* 32-bit wraparound */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800666 i++;
667 if (i == RADIX_TREE_MAP_SIZE)
668 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 shift -= RADIX_TREE_MAP_SHIFT;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800672 slot = rcu_dereference(slot->slots[i]);
673 if (slot == NULL)
674 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Christoph Lameter201b6262005-09-06 15:16:46 -0700676
677 /* Bottom level: grab some items */
678 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800679 struct radix_tree_node *node;
Christoph Lameter201b6262005-09-06 15:16:46 -0700680 index++;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800681 node = slot->slots[i];
682 if (node) {
683 results[nr_found++] = rcu_dereference(node);
Christoph Lameter201b6262005-09-06 15:16:46 -0700684 if (nr_found == max_items)
685 goto out;
686 }
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688out:
689 *next_index = index;
690 return nr_found;
691}
692
693/**
694 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
695 * @root: radix tree root
696 * @results: where the results of the lookup are placed
697 * @first_index: start the lookup from this key
698 * @max_items: place up to this many items at *results
699 *
700 * Performs an index-ascending scan of the tree for present items. Places
701 * them at *@results and returns the number of items which were placed at
702 * *@results.
703 *
704 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800705 *
706 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
707 * rcu_read_lock. In this case, rather than the returned results being
708 * an atomic snapshot of the tree at a single point in time, the semantics
709 * of an RCU protected gang lookup are as though multiple radix_tree_lookups
710 * have been issued in individual locks, and results stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
712unsigned int
713radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
714 unsigned long first_index, unsigned int max_items)
715{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800716 unsigned long max_index;
717 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 unsigned long cur_index = first_index;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800719 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800721 node = rcu_dereference(root->rnode);
722 if (!node)
723 return 0;
724
Nick Pigginc0bc9872007-10-16 01:24:42 -0700725 if (!radix_tree_is_indirect_ptr(node)) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800726 if (first_index > 0)
727 return 0;
Nick Pigginc0bc9872007-10-16 01:24:42 -0700728 results[0] = node;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800729 return 1;
730 }
Nick Pigginc0bc9872007-10-16 01:24:42 -0700731 node = radix_tree_indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800732
733 max_index = radix_tree_maxindex(node->height);
734
735 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 while (ret < max_items) {
737 unsigned int nr_found;
738 unsigned long next_index; /* Index of next search */
739
740 if (cur_index > max_index)
741 break;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800742 nr_found = __lookup(node, results + ret, cur_index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 max_items - ret, &next_index);
744 ret += nr_found;
745 if (next_index == 0)
746 break;
747 cur_index = next_index;
748 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return ret;
751}
752EXPORT_SYMBOL(radix_tree_gang_lookup);
753
754/*
755 * FIXME: the two tag_get()s here should use find_next_bit() instead of
756 * open-coding the search.
757 */
758static unsigned int
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800759__lookup_tag(struct radix_tree_node *slot, void **results, unsigned long index,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800760 unsigned int max_items, unsigned long *next_index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 unsigned int nr_found = 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800763 unsigned int shift, height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800765 height = slot->height;
766 if (height == 0)
Nick Piggin612d6c12006-06-23 02:03:22 -0700767 goto out;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800768 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
Nick Piggin612d6c12006-06-23 02:03:22 -0700769
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800770 while (height > 0) {
771 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800773 for (;;) {
774 if (tag_get(slot, tag, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 index &= ~((1UL << shift) - 1);
777 index += 1UL << shift;
778 if (index == 0)
779 goto out; /* 32-bit wraparound */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800780 i++;
781 if (i == RADIX_TREE_MAP_SIZE)
782 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 height--;
785 if (height == 0) { /* Bottom level: grab some items */
786 unsigned long j = index & RADIX_TREE_MAP_MASK;
787
788 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800789 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 index++;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800791 if (!tag_get(slot, tag, j))
792 continue;
793 node = slot->slots[j];
794 /*
795 * Even though the tag was found set, we need to
796 * recheck that we have a non-NULL node, because
797 * if this lookup is lockless, it may have been
798 * subsequently deleted.
799 *
800 * Similar care must be taken in any place that
801 * lookup ->slots[x] without a lock (ie. can't
802 * rely on its value remaining the same).
803 */
804 if (node) {
805 node = rcu_dereference(node);
806 results[nr_found++] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (nr_found == max_items)
808 goto out;
809 }
810 }
811 }
812 shift -= RADIX_TREE_MAP_SHIFT;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800813 slot = rcu_dereference(slot->slots[i]);
814 if (slot == NULL)
815 break;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817out:
818 *next_index = index;
819 return nr_found;
820}
821
822/**
823 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
824 * based on a tag
825 * @root: radix tree root
826 * @results: where the results of the lookup are placed
827 * @first_index: start the lookup from this key
828 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800829 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 *
831 * Performs an index-ascending scan of the tree for present items which
832 * have the tag indexed by @tag set. Places the items at *@results and
833 * returns the number of items which were placed at *@results.
834 */
835unsigned int
836radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800837 unsigned long first_index, unsigned int max_items,
838 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800840 struct radix_tree_node *node;
841 unsigned long max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 unsigned long cur_index = first_index;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800843 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Nick Piggin612d6c12006-06-23 02:03:22 -0700845 /* check the root's tag bit */
846 if (!root_tag_get(root, tag))
847 return 0;
848
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800849 node = rcu_dereference(root->rnode);
850 if (!node)
851 return 0;
852
Nick Pigginc0bc9872007-10-16 01:24:42 -0700853 if (!radix_tree_is_indirect_ptr(node)) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800854 if (first_index > 0)
855 return 0;
Nick Pigginc0bc9872007-10-16 01:24:42 -0700856 results[0] = node;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800857 return 1;
858 }
Nick Pigginc0bc9872007-10-16 01:24:42 -0700859 node = radix_tree_indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800860
861 max_index = radix_tree_maxindex(node->height);
862
863 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 while (ret < max_items) {
865 unsigned int nr_found;
866 unsigned long next_index; /* Index of next search */
867
868 if (cur_index > max_index)
869 break;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800870 nr_found = __lookup_tag(node, results + ret, cur_index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 max_items - ret, &next_index, tag);
872 ret += nr_found;
873 if (next_index == 0)
874 break;
875 cur_index = next_index;
876 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return ret;
879}
880EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
881
882/**
Nick Piggina5f51c92006-01-08 01:01:41 -0800883 * radix_tree_shrink - shrink height of a radix tree to minimal
884 * @root radix tree root
885 */
886static inline void radix_tree_shrink(struct radix_tree_root *root)
887{
888 /* try to shrink tree height */
Nick Pigginc0bc9872007-10-16 01:24:42 -0700889 while (root->height > 0) {
Nick Piggina5f51c92006-01-08 01:01:41 -0800890 struct radix_tree_node *to_free = root->rnode;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800891 void *newptr;
Nick Piggina5f51c92006-01-08 01:01:41 -0800892
Nick Pigginc0bc9872007-10-16 01:24:42 -0700893 BUG_ON(!radix_tree_is_indirect_ptr(to_free));
894 to_free = radix_tree_indirect_to_ptr(to_free);
895
896 /*
897 * The candidate node has more than one child, or its child
898 * is not at the leftmost slot, we cannot shrink.
899 */
900 if (to_free->count != 1)
901 break;
902 if (!to_free->slots[0])
903 break;
904
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800905 /*
906 * We don't need rcu_assign_pointer(), since we are simply
907 * moving the node from one part of the tree to another. If
908 * it was safe to dereference the old pointer to it
909 * (to_free->slots[0]), it will be safe to dereference the new
910 * one (root->rnode).
911 */
912 newptr = to_free->slots[0];
Nick Pigginc0bc9872007-10-16 01:24:42 -0700913 if (root->height > 1)
914 newptr = radix_tree_ptr_to_indirect(newptr);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800915 root->rnode = newptr;
Nick Piggina5f51c92006-01-08 01:01:41 -0800916 root->height--;
917 /* must only free zeroed nodes into the slab */
918 tag_clear(to_free, 0, 0);
919 tag_clear(to_free, 1, 0);
920 to_free->slots[0] = NULL;
921 to_free->count = 0;
922 radix_tree_node_free(to_free);
923 }
924}
925
926/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 * radix_tree_delete - delete an item from a radix tree
928 * @root: radix tree root
929 * @index: index key
930 *
931 * Remove the item at @index from the radix tree rooted at @root.
932 *
933 * Returns the address of the deleted item, or NULL if it was not present.
934 */
935void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
936{
937 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
Nick Piggin612d6c12006-06-23 02:03:22 -0700938 struct radix_tree_node *slot = NULL;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800939 struct radix_tree_node *to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 unsigned int height, shift;
Nick Piggind5274262006-01-08 01:01:41 -0800941 int tag;
942 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 height = root->height;
945 if (index > radix_tree_maxindex(height))
946 goto out;
947
Nick Piggin612d6c12006-06-23 02:03:22 -0700948 slot = root->rnode;
Nick Pigginc0bc9872007-10-16 01:24:42 -0700949 if (height == 0) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700950 root_tag_clear_all(root);
951 root->rnode = NULL;
952 goto out;
953 }
Nick Pigginc0bc9872007-10-16 01:24:42 -0700954 slot = radix_tree_indirect_to_ptr(slot);
Nick Piggin612d6c12006-06-23 02:03:22 -0700955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
957 pathp->node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Nick Piggin612d6c12006-06-23 02:03:22 -0700959 do {
Christoph Lameter201b6262005-09-06 15:16:46 -0700960 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 goto out;
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 pathp++;
Nick Piggind5274262006-01-08 01:01:41 -0800964 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
965 pathp->offset = offset;
966 pathp->node = slot;
967 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 shift -= RADIX_TREE_MAP_SHIFT;
Nick Piggin612d6c12006-06-23 02:03:22 -0700969 height--;
970 } while (height > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Nick Piggin612d6c12006-06-23 02:03:22 -0700972 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 goto out;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /*
976 * Clear all tags associated with the just-deleted item
977 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800978 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700979 if (tag_get(pathp->node, tag, pathp->offset))
980 radix_tree_tag_clear(root, index, tag);
Nick Piggind5274262006-01-08 01:01:41 -0800981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800983 to_free = NULL;
Christoph Lameter201b6262005-09-06 15:16:46 -0700984 /* Now free the nodes we do not need anymore */
Nick Piggin612d6c12006-06-23 02:03:22 -0700985 while (pathp->node) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700986 pathp->node->slots[pathp->offset] = NULL;
Nick Piggina5f51c92006-01-08 01:01:41 -0800987 pathp->node->count--;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800988 /*
989 * Queue the node for deferred freeing after the
990 * last reference to it disappears (set NULL, above).
991 */
992 if (to_free)
993 radix_tree_node_free(to_free);
Nick Piggina5f51c92006-01-08 01:01:41 -0800994
995 if (pathp->node->count) {
Nick Pigginc0bc9872007-10-16 01:24:42 -0700996 if (pathp->node ==
997 radix_tree_indirect_to_ptr(root->rnode))
Nick Piggina5f51c92006-01-08 01:01:41 -0800998 radix_tree_shrink(root);
Christoph Lameter201b6262005-09-06 15:16:46 -0700999 goto out;
Nick Piggina5f51c92006-01-08 01:01:41 -08001000 }
Christoph Lameter201b6262005-09-06 15:16:46 -07001001
1002 /* Node with zero slots in use so free it */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001003 to_free = pathp->node;
Nick Piggin612d6c12006-06-23 02:03:22 -07001004 pathp--;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Nick Piggin612d6c12006-06-23 02:03:22 -07001007 root_tag_clear_all(root);
Christoph Lameter201b6262005-09-06 15:16:46 -07001008 root->height = 0;
Nick Piggin612d6c12006-06-23 02:03:22 -07001009 root->rnode = NULL;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001010 if (to_free)
1011 radix_tree_node_free(to_free);
Nick Piggin612d6c12006-06-23 02:03:22 -07001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013out:
Nick Piggin612d6c12006-06-23 02:03:22 -07001014 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016EXPORT_SYMBOL(radix_tree_delete);
1017
1018/**
1019 * radix_tree_tagged - test whether any items in the tree are tagged
1020 * @root: radix tree root
1021 * @tag: tag to test
1022 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001023int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Nick Piggin612d6c12006-06-23 02:03:22 -07001025 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027EXPORT_SYMBOL(radix_tree_tagged);
1028
1029static void
Christoph Lametere18b8902006-12-06 20:33:20 -08001030radix_tree_node_ctor(void *node, struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 memset(node, 0, sizeof(struct radix_tree_node));
1033}
1034
1035static __init unsigned long __maxindex(unsigned int height)
1036{
1037 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
1038 unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
1039
1040 if (tmp >= RADIX_TREE_INDEX_BITS)
1041 index = ~0UL;
1042 return index;
1043}
1044
1045static __init void radix_tree_init_maxindex(void)
1046{
1047 unsigned int i;
1048
1049 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1050 height_to_maxindex[i] = __maxindex(i);
1051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053static int radix_tree_callback(struct notifier_block *nfb,
1054 unsigned long action,
1055 void *hcpu)
1056{
1057 int cpu = (long)hcpu;
1058 struct radix_tree_preload *rtp;
1059
1060 /* Free per-cpu pool of perloaded nodes */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001061 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 rtp = &per_cpu(radix_tree_preloads, cpu);
1063 while (rtp->nr) {
1064 kmem_cache_free(radix_tree_node_cachep,
1065 rtp->nodes[rtp->nr-1]);
1066 rtp->nodes[rtp->nr-1] = NULL;
1067 rtp->nr--;
1068 }
1069 }
1070 return NOTIFY_OK;
1071}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073void __init radix_tree_init(void)
1074{
1075 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1076 sizeof(struct radix_tree_node), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001077 SLAB_PANIC, radix_tree_node_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 radix_tree_init_maxindex();
1079 hotcpu_notifier(radix_tree_callback, 0);
1080}