blob: 7fdc4121e44c71945ca28c12544355b5b212177b [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 Lametercde53532008-07-04 09:59:22 -07004 * Copyright (C) 2005 SGI, Christoph Lameter
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08005 * Copyright (C) 2006 Nick Piggin
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07006 * Copyright (C) 2012 Konstantin Khlebnikov
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/errno.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050026#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/radix-tree.h>
28#include <linux/percpu.h>
29#include <linux/slab.h>
30#include <linux/notifier.h>
31#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/string.h>
33#include <linux/bitops.h>
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080034#include <linux/rcupdate.h>
Jan Kara5e4c0d972013-09-11 14:26:05 -070035#include <linux/hardirq.h> /* in_interrupt() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37
38#ifdef __KERNEL__
Nick Piggincfd9b7d2006-06-23 02:03:22 -070039#define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#else
41#define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
45#define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
46
47#define RADIX_TREE_TAG_LONGS \
48 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
49
50struct radix_tree_node {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080051 unsigned int height; /* Height from the bottom */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 unsigned int count;
Hugh Dickinse2bdb932012-01-12 17:20:41 -080053 union {
54 struct radix_tree_node *parent; /* Used when ascending tree */
55 struct rcu_head rcu_head; /* Used when freeing node */
56 };
Arnd Bergmanna1115572010-02-25 23:43:52 +010057 void __rcu *slots[RADIX_TREE_MAP_SIZE];
Jonathan Corbetdaff89f2006-03-25 03:08:05 -080058 unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
Jeff Moyer26fb1582007-10-16 01:24:49 -070062#define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
63 RADIX_TREE_MAP_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jeff Moyer26fb1582007-10-16 01:24:49 -070065/*
66 * The height_to_maxindex array needs to be one deeper than the maximum
67 * path as height 0 holds only 1 entry.
68 */
69static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 * Radix tree node cache.
73 */
Christoph Lametere18b8902006-12-06 20:33:20 -080074static struct kmem_cache *radix_tree_node_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/*
Nick Piggin55368052012-05-29 15:07:34 -070077 * The radix tree is variable-height, so an insert operation not only has
78 * to build the branch to its corresponding item, it also has to build the
79 * branch to existing items if the size has to be increased (by
80 * radix_tree_extend).
81 *
82 * The worst case is a zero height tree with just a single item at index 0,
83 * and then inserting an item at index ULONG_MAX. This requires 2 new branches
84 * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
85 * Hence:
86 */
87#define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
88
89/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Per-cpu pool of preloaded nodes
91 */
92struct radix_tree_preload {
93 int nr;
Nick Piggin55368052012-05-29 15:07:34 -070094 struct radix_tree_node *nodes[RADIX_TREE_PRELOAD_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
Harvey Harrison8cef7d52009-01-06 14:40:50 -080096static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Nick Piggin27d20fd2010-11-11 14:05:19 -080098static inline void *ptr_to_indirect(void *ptr)
99{
100 return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR);
101}
102
103static inline void *indirect_to_ptr(void *ptr)
104{
105 return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
106}
107
Nick Piggin612d6c12006-06-23 02:03:22 -0700108static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
109{
110 return root->gfp_mask & __GFP_BITS_MASK;
111}
112
Nick Piggin643b52b2008-06-12 15:21:52 -0700113static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
114 int offset)
115{
116 __set_bit(offset, node->tags[tag]);
117}
118
119static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
120 int offset)
121{
122 __clear_bit(offset, node->tags[tag]);
123}
124
125static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
126 int offset)
127{
128 return test_bit(offset, node->tags[tag]);
129}
130
131static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
132{
133 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
134}
135
136static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
137{
138 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
139}
140
141static inline void root_tag_clear_all(struct radix_tree_root *root)
142{
143 root->gfp_mask &= __GFP_BITS_MASK;
144}
145
146static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
147{
148 return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
149}
150
151/*
152 * Returns 1 if any slot in the node has this tag set.
153 * Otherwise returns 0.
154 */
155static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
156{
157 int idx;
158 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
159 if (node->tags[tag][idx])
160 return 1;
161 }
162 return 0;
163}
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700164
165/**
166 * radix_tree_find_next_bit - find the next set bit in a memory region
167 *
168 * @addr: The address to base the search on
169 * @size: The bitmap size in bits
170 * @offset: The bitnumber to start searching at
171 *
172 * Unrollable variant of find_next_bit() for constant size arrays.
173 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
174 * Returns next bit offset, or size if nothing found.
175 */
176static __always_inline unsigned long
177radix_tree_find_next_bit(const unsigned long *addr,
178 unsigned long size, unsigned long offset)
179{
180 if (!__builtin_constant_p(size))
181 return find_next_bit(addr, size, offset);
182
183 if (offset < size) {
184 unsigned long tmp;
185
186 addr += offset / BITS_PER_LONG;
187 tmp = *addr >> (offset % BITS_PER_LONG);
188 if (tmp)
189 return __ffs(tmp) + offset;
190 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
191 while (offset < size) {
192 tmp = *++addr;
193 if (tmp)
194 return __ffs(tmp) + offset;
195 offset += BITS_PER_LONG;
196 }
197 }
198 return size;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
202 * This assumes that the caller has performed appropriate preallocation, and
203 * that the caller has pinned this thread of control to the current CPU.
204 */
205static struct radix_tree_node *
206radix_tree_node_alloc(struct radix_tree_root *root)
207{
Nick Piggine2848a02008-02-04 22:29:10 -0800208 struct radix_tree_node *ret = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -0700209 gfp_t gfp_mask = root_gfp_mask(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Jan Kara5e4c0d972013-09-11 14:26:05 -0700211 /*
212 * Preload code isn't irq safe and it doesn't make sence to use
213 * preloading in the interrupt anyway as all the allocations have to
214 * be atomic. So just do normal allocation when in interrupt.
215 */
216 if (!(gfp_mask & __GFP_WAIT) && !in_interrupt()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct radix_tree_preload *rtp;
218
Nick Piggine2848a02008-02-04 22:29:10 -0800219 /*
220 * Provided the caller has preloaded here, we will always
221 * succeed in getting a node here (and never reach
222 * kmem_cache_alloc)
223 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 rtp = &__get_cpu_var(radix_tree_preloads);
225 if (rtp->nr) {
226 ret = rtp->nodes[rtp->nr - 1];
227 rtp->nodes[rtp->nr - 1] = NULL;
228 rtp->nr--;
229 }
230 }
Nick Piggine2848a02008-02-04 22:29:10 -0800231 if (ret == NULL)
Christoph Lameter488514d2008-04-28 02:12:05 -0700232 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Nick Piggine2848a02008-02-04 22:29:10 -0800233
Nick Pigginc0bc9872007-10-16 01:24:42 -0700234 BUG_ON(radix_tree_is_indirect_ptr(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return ret;
236}
237
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800238static void radix_tree_node_rcu_free(struct rcu_head *head)
239{
240 struct radix_tree_node *node =
241 container_of(head, struct radix_tree_node, rcu_head);
Dave Chinnerb6dd0862010-08-23 10:33:19 +1000242 int i;
Nick Piggin643b52b2008-06-12 15:21:52 -0700243
244 /*
245 * must only free zeroed nodes into the slab. radix_tree_shrink
246 * can leave us with a non-NULL entry in the first slot, so clear
247 * that here to make sure.
248 */
Dave Chinnerb6dd0862010-08-23 10:33:19 +1000249 for (i = 0; i < RADIX_TREE_MAX_TAGS; i++)
250 tag_clear(node, i, 0);
251
Nick Piggin643b52b2008-06-12 15:21:52 -0700252 node->slots[0] = NULL;
253 node->count = 0;
254
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800255 kmem_cache_free(radix_tree_node_cachep, node);
256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static inline void
259radix_tree_node_free(struct radix_tree_node *node)
260{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800261 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264/*
265 * Load up this CPU's radix_tree_node buffer with sufficient objects to
266 * ensure that the addition of a single element in the tree cannot fail. On
267 * success, return zero, with preemption disabled. On error, return -ENOMEM
268 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000269 *
270 * To make use of this facility, the radix tree must be initialised without
271 * __GFP_WAIT being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
Jan Kara5e4c0d972013-09-11 14:26:05 -0700273static int __radix_tree_preload(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct radix_tree_preload *rtp;
276 struct radix_tree_node *node;
277 int ret = -ENOMEM;
278
279 preempt_disable();
280 rtp = &__get_cpu_var(radix_tree_preloads);
281 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
282 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700283 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (node == NULL)
285 goto out;
286 preempt_disable();
287 rtp = &__get_cpu_var(radix_tree_preloads);
288 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
289 rtp->nodes[rtp->nr++] = node;
290 else
291 kmem_cache_free(radix_tree_node_cachep, node);
292 }
293 ret = 0;
294out:
295 return ret;
296}
Jan Kara5e4c0d972013-09-11 14:26:05 -0700297
298/*
299 * Load up this CPU's radix_tree_node buffer with sufficient objects to
300 * ensure that the addition of a single element in the tree cannot fail. On
301 * success, return zero, with preemption disabled. On error, return -ENOMEM
302 * with preemption not disabled.
303 *
304 * To make use of this facility, the radix tree must be initialised without
305 * __GFP_WAIT being passed to INIT_RADIX_TREE().
306 */
307int radix_tree_preload(gfp_t gfp_mask)
308{
309 /* Warn on non-sensical use... */
310 WARN_ON_ONCE(!(gfp_mask & __GFP_WAIT));
311 return __radix_tree_preload(gfp_mask);
312}
David Chinnerd7f09232007-07-14 16:05:04 +1000313EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Nick Piggin6e954b92006-01-08 01:01:40 -0800315/*
Jan Kara5e4c0d972013-09-11 14:26:05 -0700316 * The same as above function, except we don't guarantee preloading happens.
317 * We do it, if we decide it helps. On success, return zero with preemption
318 * disabled. On error, return -ENOMEM with preemption not disabled.
319 */
320int radix_tree_maybe_preload(gfp_t gfp_mask)
321{
322 if (gfp_mask & __GFP_WAIT)
323 return __radix_tree_preload(gfp_mask);
324 /* Preloading doesn't help anything with this gfp mask, skip it */
325 preempt_disable();
326 return 0;
327}
328EXPORT_SYMBOL(radix_tree_maybe_preload);
329
330/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * Return the maximum key which can be store into a
332 * radix tree with height HEIGHT.
333 */
334static inline unsigned long radix_tree_maxindex(unsigned int height)
335{
336 return height_to_maxindex[height];
337}
338
339/*
340 * Extend a radix tree so it can store key @index.
341 */
342static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
343{
344 struct radix_tree_node *node;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800345 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 unsigned int height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int tag;
348
349 /* Figure out what the height should be. */
350 height = root->height + 1;
351 while (index > radix_tree_maxindex(height))
352 height++;
353
354 if (root->rnode == NULL) {
355 root->height = height;
356 goto out;
357 }
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 do {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800360 unsigned int newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!(node = radix_tree_node_alloc(root)))
362 return -ENOMEM;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /* Propagate the aggregated tag info into the new root */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800365 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700366 if (root_tag_get(root, tag))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 tag_set(node, tag, 0);
368 }
369
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800370 /* Increase the height. */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800371 newheight = root->height+1;
372 node->height = newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 node->count = 1;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800374 node->parent = NULL;
375 slot = root->rnode;
376 if (newheight > 1) {
377 slot = indirect_to_ptr(slot);
378 slot->parent = node;
379 }
380 node->slots[0] = slot;
Nick Piggin27d20fd2010-11-11 14:05:19 -0800381 node = ptr_to_indirect(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800382 rcu_assign_pointer(root->rnode, node);
383 root->height = newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } while (height > root->height);
385out:
386 return 0;
387}
388
389/**
390 * radix_tree_insert - insert into a radix tree
391 * @root: radix tree root
392 * @index: index key
393 * @item: item to insert
394 *
395 * Insert an item into the radix tree at position @index.
396 */
397int radix_tree_insert(struct radix_tree_root *root,
398 unsigned long index, void *item)
399{
Christoph Lameter201b6262005-09-06 15:16:46 -0700400 struct radix_tree_node *node = NULL, *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 unsigned int height, shift;
402 int offset;
403 int error;
404
Nick Pigginc0bc9872007-10-16 01:24:42 -0700405 BUG_ON(radix_tree_is_indirect_ptr(item));
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* Make sure the tree is high enough. */
Nick Piggin612d6c12006-06-23 02:03:22 -0700408 if (index > radix_tree_maxindex(root->height)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 error = radix_tree_extend(root, index);
410 if (error)
411 return error;
412 }
413
Nick Piggin27d20fd2010-11-11 14:05:19 -0800414 slot = indirect_to_ptr(root->rnode);
Nick Pigginc0bc9872007-10-16 01:24:42 -0700415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 height = root->height;
417 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
418
419 offset = 0; /* uninitialised var warning */
Nick Piggin612d6c12006-06-23 02:03:22 -0700420 while (height > 0) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700421 if (slot == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* Have to add a child node. */
Christoph Lameter201b6262005-09-06 15:16:46 -0700423 if (!(slot = radix_tree_node_alloc(root)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -ENOMEM;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800425 slot->height = height;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800426 slot->parent = node;
Christoph Lameter201b6262005-09-06 15:16:46 -0700427 if (node) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800428 rcu_assign_pointer(node->slots[offset], slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 node->count++;
Christoph Lameter201b6262005-09-06 15:16:46 -0700430 } else
Nick Piggin27d20fd2010-11-11 14:05:19 -0800431 rcu_assign_pointer(root->rnode, ptr_to_indirect(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
434 /* Go a level down */
435 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Christoph Lameter201b6262005-09-06 15:16:46 -0700436 node = slot;
437 slot = node->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 shift -= RADIX_TREE_MAP_SHIFT;
439 height--;
Nick Piggin612d6c12006-06-23 02:03:22 -0700440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Christoph Lameter201b6262005-09-06 15:16:46 -0700442 if (slot != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return -EEXIST;
Christoph Lameter201b6262005-09-06 15:16:46 -0700444
Nick Piggin612d6c12006-06-23 02:03:22 -0700445 if (node) {
446 node->count++;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800447 rcu_assign_pointer(node->slots[offset], item);
Nick Piggin612d6c12006-06-23 02:03:22 -0700448 BUG_ON(tag_get(node, 0, offset));
449 BUG_ON(tag_get(node, 1, offset));
450 } else {
Nick Pigginc0bc9872007-10-16 01:24:42 -0700451 rcu_assign_pointer(root->rnode, item);
Nick Piggin612d6c12006-06-23 02:03:22 -0700452 BUG_ON(root_tag_get(root, 0));
453 BUG_ON(root_tag_get(root, 1));
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
457}
458EXPORT_SYMBOL(radix_tree_insert);
459
Huang Shijieb72b71c2009-06-16 15:33:42 -0700460/*
461 * is_slot == 1 : search for the slot.
462 * is_slot == 0 : search for the node.
Hans Reisera4331362005-11-07 00:59:29 -0800463 */
Huang Shijieb72b71c2009-06-16 15:33:42 -0700464static void *radix_tree_lookup_element(struct radix_tree_root *root,
465 unsigned long index, int is_slot)
Hans Reisera4331362005-11-07 00:59:29 -0800466{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800467 unsigned int height, shift;
468 struct radix_tree_node *node, **slot;
469
Paul E. McKenney2676a582010-02-22 17:04:54 -0800470 node = rcu_dereference_raw(root->rnode);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800471 if (node == NULL)
472 return NULL;
473
Nick Pigginc0bc9872007-10-16 01:24:42 -0700474 if (!radix_tree_is_indirect_ptr(node)) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800475 if (index > 0)
476 return NULL;
Huang Shijieb72b71c2009-06-16 15:33:42 -0700477 return is_slot ? (void *)&root->rnode : node;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800478 }
Nick Piggin27d20fd2010-11-11 14:05:19 -0800479 node = indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800480
481 height = node->height;
482 if (index > radix_tree_maxindex(height))
483 return NULL;
484
485 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
486
487 do {
488 slot = (struct radix_tree_node **)
489 (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
Paul E. McKenney2676a582010-02-22 17:04:54 -0800490 node = rcu_dereference_raw(*slot);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800491 if (node == NULL)
492 return NULL;
493
494 shift -= RADIX_TREE_MAP_SHIFT;
495 height--;
496 } while (height > 0);
497
Nick Piggin27d20fd2010-11-11 14:05:19 -0800498 return is_slot ? (void *)slot : indirect_to_ptr(node);
Huang Shijieb72b71c2009-06-16 15:33:42 -0700499}
500
501/**
502 * radix_tree_lookup_slot - lookup a slot in a radix tree
503 * @root: radix tree root
504 * @index: index key
505 *
506 * Returns: the slot corresponding to the position @index in the
507 * radix tree @root. This is useful for update-if-exists operations.
508 *
509 * This function can be called under rcu_read_lock iff the slot is not
510 * modified by radix_tree_replace_slot, otherwise it must be called
511 * exclusive from other writers. Any dereference of the slot must be done
512 * using radix_tree_deref_slot.
513 */
514void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
515{
516 return (void **)radix_tree_lookup_element(root, index, 1);
Hans Reisera4331362005-11-07 00:59:29 -0800517}
518EXPORT_SYMBOL(radix_tree_lookup_slot);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/**
521 * radix_tree_lookup - perform lookup operation on a radix tree
522 * @root: radix tree root
523 * @index: index key
524 *
525 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800526 *
527 * This function can be called under rcu_read_lock, however the caller
528 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
529 * them safely). No RCU barriers are required to access or modify the
530 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
532void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
533{
Huang Shijieb72b71c2009-06-16 15:33:42 -0700534 return radix_tree_lookup_element(root, index, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536EXPORT_SYMBOL(radix_tree_lookup);
537
538/**
539 * radix_tree_tag_set - set a tag on a radix tree node
540 * @root: radix tree root
541 * @index: index key
542 * @tag: tag index
543 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800544 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
545 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * the root all the way down to the leaf node.
547 *
548 * Returns the address of the tagged item. Setting a tag on a not-present
549 * item is a bug.
550 */
551void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800552 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700555 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 height = root->height;
Peter Zijlstra4c91c362006-06-23 02:03:25 -0700558 BUG_ON(index > radix_tree_maxindex(height));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Nick Piggin27d20fd2010-11-11 14:05:19 -0800560 slot = indirect_to_ptr(root->rnode);
Nick Piggin612d6c12006-06-23 02:03:22 -0700561 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 while (height > 0) {
564 int offset;
565
566 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Nick Piggind5274262006-01-08 01:01:41 -0800567 if (!tag_get(slot, tag, offset))
568 tag_set(slot, tag, offset);
Christoph Lameter201b6262005-09-06 15:16:46 -0700569 slot = slot->slots[offset];
570 BUG_ON(slot == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 shift -= RADIX_TREE_MAP_SHIFT;
572 height--;
573 }
574
Nick Piggin612d6c12006-06-23 02:03:22 -0700575 /* set the root's tag bit */
576 if (slot && !root_tag_get(root, tag))
577 root_tag_set(root, tag);
578
Christoph Lameter201b6262005-09-06 15:16:46 -0700579 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581EXPORT_SYMBOL(radix_tree_tag_set);
582
583/**
584 * radix_tree_tag_clear - clear a tag on a radix tree node
585 * @root: radix tree root
586 * @index: index key
587 * @tag: tag index
588 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800589 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
590 * corresponding to @index in the radix tree. If
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * this causes the leaf node to have no tags set then clear the tag in the
592 * next-to-leaf node, etc.
593 *
594 * Returns the address of the tagged item on success, else NULL. ie:
595 * has the same return value and semantics as radix_tree_lookup().
596 */
597void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800598 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800600 struct radix_tree_node *node = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -0700601 struct radix_tree_node *slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 unsigned int height, shift;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800603 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 height = root->height;
606 if (index > radix_tree_maxindex(height))
607 goto out;
608
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800609 shift = height * RADIX_TREE_MAP_SHIFT;
Nick Piggin27d20fd2010-11-11 14:05:19 -0800610 slot = indirect_to_ptr(root->rnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800612 while (shift) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700613 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto out;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 shift -= RADIX_TREE_MAP_SHIFT;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800617 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
618 node = slot;
619 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Nick Piggin612d6c12006-06-23 02:03:22 -0700622 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto out;
624
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800625 while (node) {
626 if (!tag_get(node, tag, offset))
Nick Piggind5274262006-01-08 01:01:41 -0800627 goto out;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800628 tag_clear(node, tag, offset);
629 if (any_tag_set(node, tag))
Nick Piggin6e954b92006-01-08 01:01:40 -0800630 goto out;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800631
632 index >>= RADIX_TREE_MAP_SHIFT;
633 offset = index & RADIX_TREE_MAP_MASK;
634 node = node->parent;
Nick Piggin612d6c12006-06-23 02:03:22 -0700635 }
636
637 /* clear the root's tag bit */
638 if (root_tag_get(root, tag))
639 root_tag_clear(root, tag);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641out:
Nick Piggin612d6c12006-06-23 02:03:22 -0700642 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644EXPORT_SYMBOL(radix_tree_tag_clear);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700647 * radix_tree_tag_get - get a tag on a radix tree node
648 * @root: radix tree root
649 * @index: index key
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800650 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700652 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 *
Nick Piggin612d6c12006-06-23 02:03:22 -0700654 * 0: tag not present or not set
655 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +0100656 *
657 * Note that the return value of this function may not be relied on, even if
658 * the RCU lock is held, unless tag modification and node deletion are excluded
659 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
661int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800662 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 unsigned int height, shift;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800665 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Nick Piggin612d6c12006-06-23 02:03:22 -0700667 /* check the root's tag bit */
668 if (!root_tag_get(root, tag))
669 return 0;
670
Paul E. McKenney2676a582010-02-22 17:04:54 -0800671 node = rcu_dereference_raw(root->rnode);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800672 if (node == NULL)
673 return 0;
674
Nick Pigginc0bc9872007-10-16 01:24:42 -0700675 if (!radix_tree_is_indirect_ptr(node))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800676 return (index == 0);
Nick Piggin27d20fd2010-11-11 14:05:19 -0800677 node = indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800678
679 height = node->height;
680 if (index > radix_tree_maxindex(height))
681 return 0;
Nick Piggin612d6c12006-06-23 02:03:22 -0700682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 for ( ; ; ) {
686 int offset;
687
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800688 if (node == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690
691 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800692 if (!tag_get(node, tag, offset))
Hugh Dickins3fa36ac2011-10-31 17:07:02 -0700693 return 0;
David Howellsce826532010-04-06 22:36:20 +0100694 if (height == 1)
Hugh Dickins3fa36ac2011-10-31 17:07:02 -0700695 return 1;
Paul E. McKenney2676a582010-02-22 17:04:54 -0800696 node = rcu_dereference_raw(node->slots[offset]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 shift -= RADIX_TREE_MAP_SHIFT;
698 height--;
699 }
700}
701EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700703/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700704 * radix_tree_next_chunk - find next chunk of slots for iteration
705 *
706 * @root: radix tree root
707 * @iter: iterator state
708 * @flags: RADIX_TREE_ITER_* flags and tag index
709 * Returns: pointer to chunk first slot, or NULL if iteration is over
710 */
711void **radix_tree_next_chunk(struct radix_tree_root *root,
712 struct radix_tree_iter *iter, unsigned flags)
713{
714 unsigned shift, tag = flags & RADIX_TREE_ITER_TAG_MASK;
715 struct radix_tree_node *rnode, *node;
716 unsigned long index, offset;
717
718 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
719 return NULL;
720
721 /*
722 * Catch next_index overflow after ~0UL. iter->index never overflows
723 * during iterating; it can be zero only at the beginning.
724 * And we cannot overflow iter->next_index in a single step,
725 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +0400726 *
727 * This condition also used by radix_tree_next_slot() to stop
728 * contiguous iterating, and forbid swithing to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700729 */
730 index = iter->next_index;
731 if (!index && iter->index)
732 return NULL;
733
734 rnode = rcu_dereference_raw(root->rnode);
735 if (radix_tree_is_indirect_ptr(rnode)) {
736 rnode = indirect_to_ptr(rnode);
737 } else if (rnode && !index) {
738 /* Single-slot tree */
739 iter->index = 0;
740 iter->next_index = 1;
741 iter->tags = 1;
742 return (void **)&root->rnode;
743 } else
744 return NULL;
745
746restart:
747 shift = (rnode->height - 1) * RADIX_TREE_MAP_SHIFT;
748 offset = index >> shift;
749
750 /* Index outside of the tree */
751 if (offset >= RADIX_TREE_MAP_SIZE)
752 return NULL;
753
754 node = rnode;
755 while (1) {
756 if ((flags & RADIX_TREE_ITER_TAGGED) ?
757 !test_bit(offset, node->tags[tag]) :
758 !node->slots[offset]) {
759 /* Hole detected */
760 if (flags & RADIX_TREE_ITER_CONTIG)
761 return NULL;
762
763 if (flags & RADIX_TREE_ITER_TAGGED)
764 offset = radix_tree_find_next_bit(
765 node->tags[tag],
766 RADIX_TREE_MAP_SIZE,
767 offset + 1);
768 else
769 while (++offset < RADIX_TREE_MAP_SIZE) {
770 if (node->slots[offset])
771 break;
772 }
773 index &= ~((RADIX_TREE_MAP_SIZE << shift) - 1);
774 index += offset << shift;
775 /* Overflow after ~0UL */
776 if (!index)
777 return NULL;
778 if (offset == RADIX_TREE_MAP_SIZE)
779 goto restart;
780 }
781
782 /* This is leaf-node */
783 if (!shift)
784 break;
785
786 node = rcu_dereference_raw(node->slots[offset]);
787 if (node == NULL)
788 goto restart;
789 shift -= RADIX_TREE_MAP_SHIFT;
790 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
791 }
792
793 /* Update the iterator state */
794 iter->index = index;
795 iter->next_index = (index | RADIX_TREE_MAP_MASK) + 1;
796
797 /* Construct iter->tags bit-mask from node->tags[tag] array */
798 if (flags & RADIX_TREE_ITER_TAGGED) {
799 unsigned tag_long, tag_bit;
800
801 tag_long = offset / BITS_PER_LONG;
802 tag_bit = offset % BITS_PER_LONG;
803 iter->tags = node->tags[tag][tag_long] >> tag_bit;
804 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
805 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
806 /* Pick tags from next element */
807 if (tag_bit)
808 iter->tags |= node->tags[tag][tag_long + 1] <<
809 (BITS_PER_LONG - tag_bit);
810 /* Clip chunk size, here only BITS_PER_LONG tags */
811 iter->next_index = index + BITS_PER_LONG;
812 }
813 }
814
815 return node->slots + offset;
816}
817EXPORT_SYMBOL(radix_tree_next_chunk);
818
819/**
Jan Karaebf8aa42010-08-09 17:19:11 -0700820 * radix_tree_range_tag_if_tagged - for each item in given range set given
821 * tag if item has another tag set
822 * @root: radix tree root
823 * @first_indexp: pointer to a starting index of a range to scan
824 * @last_index: last index of a range to scan
825 * @nr_to_tag: maximum number items to tag
826 * @iftag: tag index to test
827 * @settag: tag index to set if tested tag is set
828 *
829 * This function scans range of radix tree from first_index to last_index
830 * (inclusive). For each item in the range if iftag is set, the function sets
831 * also settag. The function stops either after tagging nr_to_tag items or
832 * after reaching last_index.
833 *
Dave Chinner144dcfc2010-08-23 10:33:53 +1000834 * The tags must be set from the leaf level only and propagated back up the
835 * path to the root. We must do this so that we resolve the full path before
836 * setting any tags on intermediate nodes. If we set tags as we descend, then
837 * we can get to the leaf node and find that the index that has the iftag
838 * set is outside the range we are scanning. This reults in dangling tags and
839 * can lead to problems with later tag operations (e.g. livelocks on lookups).
840 *
Jan Karaebf8aa42010-08-09 17:19:11 -0700841 * The function returns number of leaves where the tag was set and sets
842 * *first_indexp to the first unscanned index.
Jan Karad5ed3a42010-08-19 14:13:33 -0700843 * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must
844 * be prepared to handle that.
Jan Karaebf8aa42010-08-09 17:19:11 -0700845 */
846unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
847 unsigned long *first_indexp, unsigned long last_index,
848 unsigned long nr_to_tag,
849 unsigned int iftag, unsigned int settag)
850{
Dave Chinner144dcfc2010-08-23 10:33:53 +1000851 unsigned int height = root->height;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800852 struct radix_tree_node *node = NULL;
Dave Chinner144dcfc2010-08-23 10:33:53 +1000853 struct radix_tree_node *slot;
854 unsigned int shift;
855 unsigned long tagged = 0;
856 unsigned long index = *first_indexp;
Jan Karaebf8aa42010-08-09 17:19:11 -0700857
858 last_index = min(last_index, radix_tree_maxindex(height));
859 if (index > last_index)
860 return 0;
861 if (!nr_to_tag)
862 return 0;
863 if (!root_tag_get(root, iftag)) {
864 *first_indexp = last_index + 1;
865 return 0;
866 }
867 if (height == 0) {
868 *first_indexp = last_index + 1;
869 root_tag_set(root, settag);
870 return 1;
871 }
872
873 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Nick Piggin27d20fd2010-11-11 14:05:19 -0800874 slot = indirect_to_ptr(root->rnode);
Jan Karaebf8aa42010-08-09 17:19:11 -0700875
876 for (;;) {
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800877 unsigned long upindex;
Jan Karaebf8aa42010-08-09 17:19:11 -0700878 int offset;
879
880 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
881 if (!slot->slots[offset])
882 goto next;
883 if (!tag_get(slot, iftag, offset))
884 goto next;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800885 if (shift) {
Dave Chinner144dcfc2010-08-23 10:33:53 +1000886 /* Go down one level */
Dave Chinner144dcfc2010-08-23 10:33:53 +1000887 shift -= RADIX_TREE_MAP_SHIFT;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800888 node = slot;
Dave Chinner144dcfc2010-08-23 10:33:53 +1000889 slot = slot->slots[offset];
890 continue;
Jan Karaebf8aa42010-08-09 17:19:11 -0700891 }
Dave Chinner144dcfc2010-08-23 10:33:53 +1000892
893 /* tag the leaf */
894 tagged++;
895 tag_set(slot, settag, offset);
896
897 /* walk back up the path tagging interior nodes */
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800898 upindex = index;
899 while (node) {
900 upindex >>= RADIX_TREE_MAP_SHIFT;
901 offset = upindex & RADIX_TREE_MAP_MASK;
902
Dave Chinner144dcfc2010-08-23 10:33:53 +1000903 /* stop if we find a node with the tag already set */
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800904 if (tag_get(node, settag, offset))
Dave Chinner144dcfc2010-08-23 10:33:53 +1000905 break;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800906 tag_set(node, settag, offset);
907 node = node->parent;
Dave Chinner144dcfc2010-08-23 10:33:53 +1000908 }
909
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800910 /*
911 * Small optimization: now clear that node pointer.
912 * Since all of this slot's ancestors now have the tag set
913 * from setting it above, we have no further need to walk
914 * back up the tree setting tags, until we update slot to
915 * point to another radix_tree_node.
916 */
917 node = NULL;
918
Jan Karaebf8aa42010-08-09 17:19:11 -0700919next:
920 /* Go to next item at level determined by 'shift' */
921 index = ((index >> shift) + 1) << shift;
Jan Karad5ed3a42010-08-19 14:13:33 -0700922 /* Overflow can happen when last_index is ~0UL... */
923 if (index > last_index || !index)
Jan Karaebf8aa42010-08-09 17:19:11 -0700924 break;
925 if (tagged >= nr_to_tag)
926 break;
927 while (((index >> shift) & RADIX_TREE_MAP_MASK) == 0) {
928 /*
929 * We've fully scanned this node. Go up. Because
930 * last_index is guaranteed to be in the tree, what
931 * we do below cannot wander astray.
932 */
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800933 slot = slot->parent;
Jan Karaebf8aa42010-08-09 17:19:11 -0700934 shift += RADIX_TREE_MAP_SHIFT;
935 }
936 }
937 /*
Toshiyuki Okajimaac15ee62011-01-25 15:07:32 -0800938 * We need not to tag the root tag if there is no tag which is set with
939 * settag within the range from *first_indexp to last_index.
Jan Karaebf8aa42010-08-09 17:19:11 -0700940 */
Toshiyuki Okajimaac15ee62011-01-25 15:07:32 -0800941 if (tagged > 0)
942 root_tag_set(root, settag);
Jan Karaebf8aa42010-08-09 17:19:11 -0700943 *first_indexp = index;
944
945 return tagged;
946}
947EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/**
950 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
951 * @root: radix tree root
952 * @results: where the results of the lookup are placed
953 * @first_index: start the lookup from this key
954 * @max_items: place up to this many items at *results
955 *
956 * Performs an index-ascending scan of the tree for present items. Places
957 * them at *@results and returns the number of items which were placed at
958 * *@results.
959 *
960 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800961 *
962 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
963 * rcu_read_lock. In this case, rather than the returned results being
964 * an atomic snapshot of the tree at a single point in time, the semantics
965 * of an RCU protected gang lookup are as though multiple radix_tree_lookups
966 * have been issued in individual locks, and results stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
968unsigned int
969radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
970 unsigned long first_index, unsigned int max_items)
971{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -0700972 struct radix_tree_iter iter;
973 void **slot;
974 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -0700976 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800977 return 0;
978
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -0700979 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox6251e8b2016-02-02 16:57:52 -0800980 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -0700981 if (!results[ret])
982 continue;
Matthew Wilcox6251e8b2016-02-02 16:57:52 -0800983 if (radix_tree_is_indirect_ptr(results[ret])) {
984 slot = radix_tree_iter_retry(&iter);
985 continue;
986 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -0700987 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return ret;
992}
993EXPORT_SYMBOL(radix_tree_gang_lookup);
994
Nick Piggin47feff22008-07-25 19:45:29 -0700995/**
996 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
997 * @root: radix tree root
998 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -0700999 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001000 * @first_index: start the lookup from this key
1001 * @max_items: place up to this many items at *results
1002 *
1003 * Performs an index-ascending scan of the tree for present items. Places
1004 * their slots at *@results and returns the number of items which were
1005 * placed at *@results.
1006 *
1007 * The implementation is naive.
1008 *
1009 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1010 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1011 * protection, radix_tree_deref_slot may fail requiring a retry.
1012 */
1013unsigned int
Hugh Dickins63286502011-08-03 16:21:18 -07001014radix_tree_gang_lookup_slot(struct radix_tree_root *root,
1015 void ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001016 unsigned long first_index, unsigned int max_items)
1017{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001018 struct radix_tree_iter iter;
1019 void **slot;
1020 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001021
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001022 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001023 return 0;
1024
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001025 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1026 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001027 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001028 indices[ret] = iter.index;
1029 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001030 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001031 }
1032
1033 return ret;
1034}
1035EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/**
1038 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1039 * based on a tag
1040 * @root: radix tree root
1041 * @results: where the results of the lookup are placed
1042 * @first_index: start the lookup from this key
1043 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001044 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 *
1046 * Performs an index-ascending scan of the tree for present items which
1047 * have the tag indexed by @tag set. Places the items at *@results and
1048 * returns the number of items which were placed at *@results.
1049 */
1050unsigned int
1051radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001052 unsigned long first_index, unsigned int max_items,
1053 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001055 struct radix_tree_iter iter;
1056 void **slot;
1057 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001059 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001060 return 0;
1061
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001062 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox6251e8b2016-02-02 16:57:52 -08001063 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001064 if (!results[ret])
1065 continue;
Matthew Wilcox6251e8b2016-02-02 16:57:52 -08001066 if (radix_tree_is_indirect_ptr(results[ret])) {
1067 slot = radix_tree_iter_retry(&iter);
1068 continue;
1069 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001070 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return ret;
1075}
1076EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1077
1078/**
Nick Piggin47feff22008-07-25 19:45:29 -07001079 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1080 * radix tree based on a tag
1081 * @root: radix tree root
1082 * @results: where the results of the lookup are placed
1083 * @first_index: start the lookup from this key
1084 * @max_items: place up to this many items at *results
1085 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1086 *
1087 * Performs an index-ascending scan of the tree for present items which
1088 * have the tag indexed by @tag set. Places the slots at *@results and
1089 * returns the number of slots which were placed at *@results.
1090 */
1091unsigned int
1092radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
1093 unsigned long first_index, unsigned int max_items,
1094 unsigned int tag)
1095{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001096 struct radix_tree_iter iter;
1097 void **slot;
1098 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001099
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001100 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001101 return 0;
1102
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001103 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1104 results[ret] = slot;
1105 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001106 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001107 }
1108
1109 return ret;
1110}
1111EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1112
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001113#if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
1114#include <linux/sched.h> /* for cond_resched() */
1115
1116/*
1117 * This linear search is at present only useful to shmem_unuse_inode().
1118 */
1119static unsigned long __locate(struct radix_tree_node *slot, void *item,
1120 unsigned long index, unsigned long *found_index)
1121{
1122 unsigned int shift, height;
1123 unsigned long i;
1124
1125 height = slot->height;
1126 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
1127
1128 for ( ; height > 1; height--) {
1129 i = (index >> shift) & RADIX_TREE_MAP_MASK;
1130 for (;;) {
1131 if (slot->slots[i] != NULL)
1132 break;
1133 index &= ~((1UL << shift) - 1);
1134 index += 1UL << shift;
1135 if (index == 0)
1136 goto out; /* 32-bit wraparound */
1137 i++;
1138 if (i == RADIX_TREE_MAP_SIZE)
1139 goto out;
1140 }
1141
1142 shift -= RADIX_TREE_MAP_SHIFT;
1143 slot = rcu_dereference_raw(slot->slots[i]);
1144 if (slot == NULL)
1145 goto out;
1146 }
1147
1148 /* Bottom level: check items */
1149 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
1150 if (slot->slots[i] == item) {
1151 *found_index = index + i;
1152 index = 0;
1153 goto out;
1154 }
1155 }
1156 index += RADIX_TREE_MAP_SIZE;
1157out:
1158 return index;
1159}
1160
1161/**
1162 * radix_tree_locate_item - search through radix tree for item
1163 * @root: radix tree root
1164 * @item: item to be found
1165 *
1166 * Returns index where item was found, or -1 if not found.
1167 * Caller must hold no lock (since this time-consuming function needs
1168 * to be preemptible), and must check afterwards if item is still there.
1169 */
1170unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1171{
1172 struct radix_tree_node *node;
1173 unsigned long max_index;
1174 unsigned long cur_index = 0;
1175 unsigned long found_index = -1;
1176
1177 do {
1178 rcu_read_lock();
1179 node = rcu_dereference_raw(root->rnode);
1180 if (!radix_tree_is_indirect_ptr(node)) {
1181 rcu_read_unlock();
1182 if (node == item)
1183 found_index = 0;
1184 break;
1185 }
1186
1187 node = indirect_to_ptr(node);
1188 max_index = radix_tree_maxindex(node->height);
Hugh Dickins5f30fc92014-03-03 15:38:23 -08001189 if (cur_index > max_index) {
1190 rcu_read_unlock();
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001191 break;
Hugh Dickins5f30fc92014-03-03 15:38:23 -08001192 }
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001193
1194 cur_index = __locate(node, item, cur_index, &found_index);
1195 rcu_read_unlock();
1196 cond_resched();
1197 } while (cur_index != 0 && cur_index <= max_index);
1198
1199 return found_index;
1200}
1201#else
1202unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1203{
1204 return -1;
1205}
1206#endif /* CONFIG_SHMEM && CONFIG_SWAP */
Nick Piggin47feff22008-07-25 19:45:29 -07001207
1208/**
Nick Piggina5f51c92006-01-08 01:01:41 -08001209 * radix_tree_shrink - shrink height of a radix tree to minimal
1210 * @root radix tree root
1211 */
1212static inline void radix_tree_shrink(struct radix_tree_root *root)
1213{
1214 /* try to shrink tree height */
Nick Pigginc0bc9872007-10-16 01:24:42 -07001215 while (root->height > 0) {
Nick Piggina5f51c92006-01-08 01:01:41 -08001216 struct radix_tree_node *to_free = root->rnode;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001217 struct radix_tree_node *slot;
Nick Piggina5f51c92006-01-08 01:01:41 -08001218
Nick Pigginc0bc9872007-10-16 01:24:42 -07001219 BUG_ON(!radix_tree_is_indirect_ptr(to_free));
Nick Piggin27d20fd2010-11-11 14:05:19 -08001220 to_free = indirect_to_ptr(to_free);
Nick Pigginc0bc9872007-10-16 01:24:42 -07001221
1222 /*
1223 * The candidate node has more than one child, or its child
1224 * is not at the leftmost slot, we cannot shrink.
1225 */
1226 if (to_free->count != 1)
1227 break;
1228 if (!to_free->slots[0])
1229 break;
1230
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001231 /*
1232 * We don't need rcu_assign_pointer(), since we are simply
Nick Piggin27d20fd2010-11-11 14:05:19 -08001233 * moving the node from one part of the tree to another: if it
1234 * was safe to dereference the old pointer to it
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001235 * (to_free->slots[0]), it will be safe to dereference the new
Nick Piggin27d20fd2010-11-11 14:05:19 -08001236 * one (root->rnode) as far as dependent read barriers go.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001237 */
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001238 slot = to_free->slots[0];
1239 if (root->height > 1) {
1240 slot->parent = NULL;
1241 slot = ptr_to_indirect(slot);
1242 }
1243 root->rnode = slot;
Nick Piggina5f51c92006-01-08 01:01:41 -08001244 root->height--;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001245
1246 /*
1247 * We have a dilemma here. The node's slot[0] must not be
1248 * NULLed in case there are concurrent lookups expecting to
1249 * find the item. However if this was a bottom-level node,
1250 * then it may be subject to the slot pointer being visible
1251 * to callers dereferencing it. If item corresponding to
1252 * slot[0] is subsequently deleted, these callers would expect
1253 * their slot to become empty sooner or later.
1254 *
1255 * For example, lockless pagecache will look up a slot, deref
1256 * the page pointer, and if the page is 0 refcount it means it
1257 * was concurrently deleted from pagecache so try the deref
1258 * again. Fortunately there is already a requirement for logic
1259 * to retry the entire slot lookup -- the indirect pointer
1260 * problem (replacing direct root node with an indirect pointer
1261 * also results in a stale slot). So tag the slot as indirect
1262 * to force callers to retry.
1263 */
1264 if (root->height == 0)
1265 *((unsigned long *)&to_free->slots[0]) |=
1266 RADIX_TREE_INDIRECT_PTR;
1267
Nick Piggina5f51c92006-01-08 01:01:41 -08001268 radix_tree_node_free(to_free);
1269 }
1270}
1271
1272/**
Johannes Weinerd35a6232014-04-03 14:47:39 -07001273 * radix_tree_delete_item - delete an item from a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 * @root: radix tree root
1275 * @index: index key
Johannes Weinerd35a6232014-04-03 14:47:39 -07001276 * @item: expected item
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 *
Johannes Weinerd35a6232014-04-03 14:47:39 -07001278 * Remove @item at @index from the radix tree rooted at @root.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 *
Johannes Weinerd35a6232014-04-03 14:47:39 -07001280 * Returns the address of the deleted item, or NULL if it was not present
1281 * or the entry at the given @index was not @item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
Johannes Weinerd35a6232014-04-03 14:47:39 -07001283void *radix_tree_delete_item(struct radix_tree_root *root,
1284 unsigned long index, void *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001286 struct radix_tree_node *node = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -07001287 struct radix_tree_node *slot = NULL;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001288 struct radix_tree_node *to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 unsigned int height, shift;
Nick Piggind5274262006-01-08 01:01:41 -08001290 int tag;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001291 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 height = root->height;
1294 if (index > radix_tree_maxindex(height))
1295 goto out;
1296
Nick Piggin612d6c12006-06-23 02:03:22 -07001297 slot = root->rnode;
Nick Pigginc0bc9872007-10-16 01:24:42 -07001298 if (height == 0) {
Nick Piggin612d6c12006-06-23 02:03:22 -07001299 root_tag_clear_all(root);
1300 root->rnode = NULL;
1301 goto out;
1302 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001303 slot = indirect_to_ptr(slot);
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001304 shift = height * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Nick Piggin612d6c12006-06-23 02:03:22 -07001306 do {
Christoph Lameter201b6262005-09-06 15:16:46 -07001307 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 goto out;
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 shift -= RADIX_TREE_MAP_SHIFT;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001311 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
1312 node = slot;
1313 slot = slot->slots[offset];
1314 } while (shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Nick Piggin612d6c12006-06-23 02:03:22 -07001316 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 goto out;
1318
Johannes Weinerd35a6232014-04-03 14:47:39 -07001319 if (item && slot != item) {
1320 slot = NULL;
1321 goto out;
1322 }
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /*
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001325 * Clear all tags associated with the item to be deleted.
1326 * This way of doing it would be inefficient, but seldom is any set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001328 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001329 if (tag_get(node, tag, offset))
Nick Piggin612d6c12006-06-23 02:03:22 -07001330 radix_tree_tag_clear(root, index, tag);
Nick Piggind5274262006-01-08 01:01:41 -08001331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001333 to_free = NULL;
Christoph Lameter201b6262005-09-06 15:16:46 -07001334 /* Now free the nodes we do not need anymore */
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001335 while (node) {
1336 node->slots[offset] = NULL;
1337 node->count--;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001338 /*
1339 * Queue the node for deferred freeing after the
1340 * last reference to it disappears (set NULL, above).
1341 */
1342 if (to_free)
1343 radix_tree_node_free(to_free);
Nick Piggina5f51c92006-01-08 01:01:41 -08001344
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001345 if (node->count) {
1346 if (node == indirect_to_ptr(root->rnode))
Nick Piggina5f51c92006-01-08 01:01:41 -08001347 radix_tree_shrink(root);
Christoph Lameter201b6262005-09-06 15:16:46 -07001348 goto out;
Nick Piggina5f51c92006-01-08 01:01:41 -08001349 }
Christoph Lameter201b6262005-09-06 15:16:46 -07001350
1351 /* Node with zero slots in use so free it */
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001352 to_free = node;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001353
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001354 index >>= RADIX_TREE_MAP_SHIFT;
1355 offset = index & RADIX_TREE_MAP_MASK;
1356 node = node->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001358
Nick Piggin612d6c12006-06-23 02:03:22 -07001359 root_tag_clear_all(root);
Christoph Lameter201b6262005-09-06 15:16:46 -07001360 root->height = 0;
Nick Piggin612d6c12006-06-23 02:03:22 -07001361 root->rnode = NULL;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001362 if (to_free)
1363 radix_tree_node_free(to_free);
Nick Piggin612d6c12006-06-23 02:03:22 -07001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365out:
Nick Piggin612d6c12006-06-23 02:03:22 -07001366 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
Johannes Weinerd35a6232014-04-03 14:47:39 -07001368EXPORT_SYMBOL(radix_tree_delete_item);
1369
1370/**
1371 * radix_tree_delete - delete an item from a radix tree
1372 * @root: radix tree root
1373 * @index: index key
1374 *
1375 * Remove the item at @index from the radix tree rooted at @root.
1376 *
1377 * Returns the address of the deleted item, or NULL if it was not present.
1378 */
1379void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1380{
1381 return radix_tree_delete_item(root, index, NULL);
1382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383EXPORT_SYMBOL(radix_tree_delete);
1384
1385/**
1386 * radix_tree_tagged - test whether any items in the tree are tagged
1387 * @root: radix tree root
1388 * @tag: tag to test
1389 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001390int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Nick Piggin612d6c12006-06-23 02:03:22 -07001392 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394EXPORT_SYMBOL(radix_tree_tagged);
1395
1396static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001397radix_tree_node_ctor(void *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 memset(node, 0, sizeof(struct radix_tree_node));
1400}
1401
1402static __init unsigned long __maxindex(unsigned int height)
1403{
Peter Lund430d2752007-10-16 23:29:35 -07001404 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1405 int shift = RADIX_TREE_INDEX_BITS - width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Peter Lund430d2752007-10-16 23:29:35 -07001407 if (shift < 0)
1408 return ~0UL;
1409 if (shift >= BITS_PER_LONG)
1410 return 0UL;
1411 return ~0UL >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
1414static __init void radix_tree_init_maxindex(void)
1415{
1416 unsigned int i;
1417
1418 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1419 height_to_maxindex[i] = __maxindex(i);
1420}
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422static int radix_tree_callback(struct notifier_block *nfb,
1423 unsigned long action,
1424 void *hcpu)
1425{
1426 int cpu = (long)hcpu;
1427 struct radix_tree_preload *rtp;
1428
1429 /* Free per-cpu pool of perloaded nodes */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001430 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 rtp = &per_cpu(radix_tree_preloads, cpu);
1432 while (rtp->nr) {
1433 kmem_cache_free(radix_tree_node_cachep,
1434 rtp->nodes[rtp->nr-1]);
1435 rtp->nodes[rtp->nr-1] = NULL;
1436 rtp->nr--;
1437 }
1438 }
1439 return NOTIFY_OK;
1440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442void __init radix_tree_init(void)
1443{
1444 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1445 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07001446 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1447 radix_tree_node_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 radix_tree_init_maxindex();
1449 hotcpu_notifier(radix_tree_callback, 0);
1450}