blob: fefa76e6ff96cc9328826f14a8fd72f42277814d [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>
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;
Hugh Dickinse2bdb932012-01-12 17:20:41 -080052 union {
53 struct radix_tree_node *parent; /* Used when ascending tree */
54 struct rcu_head rcu_head; /* Used when freeing node */
55 };
Arnd Bergmanna1115572010-02-25 23:43:52 +010056 void __rcu *slots[RADIX_TREE_MAP_SIZE];
Jonathan Corbetdaff89f2006-03-25 03:08:05 -080057 unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
Jeff Moyer26fb1582007-10-16 01:24:49 -070061#define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
62 RADIX_TREE_MAP_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jeff Moyer26fb1582007-10-16 01:24:49 -070064/*
65 * The height_to_maxindex array needs to be one deeper than the maximum
66 * path as height 0 holds only 1 entry.
67 */
68static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 * Radix tree node cache.
72 */
Christoph Lametere18b8902006-12-06 20:33:20 -080073static struct kmem_cache *radix_tree_node_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
76 * Per-cpu pool of preloaded nodes
77 */
78struct radix_tree_preload {
79 int nr;
80 struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
81};
Harvey Harrison8cef7d52009-01-06 14:40:50 -080082static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Nick Piggin27d20fd2010-11-11 14:05:19 -080084static inline void *ptr_to_indirect(void *ptr)
85{
86 return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR);
87}
88
89static inline void *indirect_to_ptr(void *ptr)
90{
91 return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
92}
93
Nick Piggin612d6c12006-06-23 02:03:22 -070094static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
95{
96 return root->gfp_mask & __GFP_BITS_MASK;
97}
98
Nick Piggin643b52b2008-06-12 15:21:52 -070099static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
100 int offset)
101{
102 __set_bit(offset, node->tags[tag]);
103}
104
105static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
106 int offset)
107{
108 __clear_bit(offset, node->tags[tag]);
109}
110
111static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
112 int offset)
113{
114 return test_bit(offset, node->tags[tag]);
115}
116
117static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
118{
119 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
120}
121
122static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
123{
124 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
125}
126
127static inline void root_tag_clear_all(struct radix_tree_root *root)
128{
129 root->gfp_mask &= __GFP_BITS_MASK;
130}
131
132static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
133{
134 return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
135}
136
137/*
138 * Returns 1 if any slot in the node has this tag set.
139 * Otherwise returns 0.
140 */
141static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
142{
143 int idx;
144 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
145 if (node->tags[tag][idx])
146 return 1;
147 }
148 return 0;
149}
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700150
151/**
152 * radix_tree_find_next_bit - find the next set bit in a memory region
153 *
154 * @addr: The address to base the search on
155 * @size: The bitmap size in bits
156 * @offset: The bitnumber to start searching at
157 *
158 * Unrollable variant of find_next_bit() for constant size arrays.
159 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
160 * Returns next bit offset, or size if nothing found.
161 */
162static __always_inline unsigned long
163radix_tree_find_next_bit(const unsigned long *addr,
164 unsigned long size, unsigned long offset)
165{
166 if (!__builtin_constant_p(size))
167 return find_next_bit(addr, size, offset);
168
169 if (offset < size) {
170 unsigned long tmp;
171
172 addr += offset / BITS_PER_LONG;
173 tmp = *addr >> (offset % BITS_PER_LONG);
174 if (tmp)
175 return __ffs(tmp) + offset;
176 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
177 while (offset < size) {
178 tmp = *++addr;
179 if (tmp)
180 return __ffs(tmp) + offset;
181 offset += BITS_PER_LONG;
182 }
183 }
184 return size;
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
188 * This assumes that the caller has performed appropriate preallocation, and
189 * that the caller has pinned this thread of control to the current CPU.
190 */
191static struct radix_tree_node *
192radix_tree_node_alloc(struct radix_tree_root *root)
193{
Nick Piggine2848a02008-02-04 22:29:10 -0800194 struct radix_tree_node *ret = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -0700195 gfp_t gfp_mask = root_gfp_mask(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Nick Piggine2848a02008-02-04 22:29:10 -0800197 if (!(gfp_mask & __GFP_WAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct radix_tree_preload *rtp;
199
Nick Piggine2848a02008-02-04 22:29:10 -0800200 /*
201 * Provided the caller has preloaded here, we will always
202 * succeed in getting a node here (and never reach
203 * kmem_cache_alloc)
204 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 rtp = &__get_cpu_var(radix_tree_preloads);
206 if (rtp->nr) {
207 ret = rtp->nodes[rtp->nr - 1];
208 rtp->nodes[rtp->nr - 1] = NULL;
209 rtp->nr--;
210 }
211 }
Nick Piggine2848a02008-02-04 22:29:10 -0800212 if (ret == NULL)
Christoph Lameter488514d2008-04-28 02:12:05 -0700213 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Nick Piggine2848a02008-02-04 22:29:10 -0800214
Nick Pigginc0bc9872007-10-16 01:24:42 -0700215 BUG_ON(radix_tree_is_indirect_ptr(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return ret;
217}
218
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800219static void radix_tree_node_rcu_free(struct rcu_head *head)
220{
221 struct radix_tree_node *node =
222 container_of(head, struct radix_tree_node, rcu_head);
Dave Chinnerb6dd0862010-08-23 10:33:19 +1000223 int i;
Nick Piggin643b52b2008-06-12 15:21:52 -0700224
225 /*
226 * must only free zeroed nodes into the slab. radix_tree_shrink
227 * can leave us with a non-NULL entry in the first slot, so clear
228 * that here to make sure.
229 */
Dave Chinnerb6dd0862010-08-23 10:33:19 +1000230 for (i = 0; i < RADIX_TREE_MAX_TAGS; i++)
231 tag_clear(node, i, 0);
232
Nick Piggin643b52b2008-06-12 15:21:52 -0700233 node->slots[0] = NULL;
234 node->count = 0;
235
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800236 kmem_cache_free(radix_tree_node_cachep, node);
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static inline void
240radix_tree_node_free(struct radix_tree_node *node)
241{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800242 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245/*
246 * Load up this CPU's radix_tree_node buffer with sufficient objects to
247 * ensure that the addition of a single element in the tree cannot fail. On
248 * success, return zero, with preemption disabled. On error, return -ENOMEM
249 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000250 *
251 * To make use of this facility, the radix tree must be initialised without
252 * __GFP_WAIT being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
Al Virodd0fc662005-10-07 07:46:04 +0100254int radix_tree_preload(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct radix_tree_preload *rtp;
257 struct radix_tree_node *node;
258 int ret = -ENOMEM;
259
260 preempt_disable();
261 rtp = &__get_cpu_var(radix_tree_preloads);
262 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
263 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700264 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (node == NULL)
266 goto out;
267 preempt_disable();
268 rtp = &__get_cpu_var(radix_tree_preloads);
269 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
270 rtp->nodes[rtp->nr++] = node;
271 else
272 kmem_cache_free(radix_tree_node_cachep, node);
273 }
274 ret = 0;
275out:
276 return ret;
277}
David Chinnerd7f09232007-07-14 16:05:04 +1000278EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Nick Piggin6e954b92006-01-08 01:01:40 -0800280/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * Return the maximum key which can be store into a
282 * radix tree with height HEIGHT.
283 */
284static inline unsigned long radix_tree_maxindex(unsigned int height)
285{
286 return height_to_maxindex[height];
287}
288
289/*
290 * Extend a radix tree so it can store key @index.
291 */
292static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
293{
294 struct radix_tree_node *node;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800295 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 unsigned int height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int tag;
298
299 /* Figure out what the height should be. */
300 height = root->height + 1;
301 while (index > radix_tree_maxindex(height))
302 height++;
303
304 if (root->rnode == NULL) {
305 root->height = height;
306 goto out;
307 }
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 do {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800310 unsigned int newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!(node = radix_tree_node_alloc(root)))
312 return -ENOMEM;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* Propagate the aggregated tag info into the new root */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800315 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700316 if (root_tag_get(root, tag))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 tag_set(node, tag, 0);
318 }
319
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800320 /* Increase the height. */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800321 newheight = root->height+1;
322 node->height = newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 node->count = 1;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800324 node->parent = NULL;
325 slot = root->rnode;
326 if (newheight > 1) {
327 slot = indirect_to_ptr(slot);
328 slot->parent = node;
329 }
330 node->slots[0] = slot;
Nick Piggin27d20fd2010-11-11 14:05:19 -0800331 node = ptr_to_indirect(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800332 rcu_assign_pointer(root->rnode, node);
333 root->height = newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 } while (height > root->height);
335out:
336 return 0;
337}
338
339/**
340 * radix_tree_insert - insert into a radix tree
341 * @root: radix tree root
342 * @index: index key
343 * @item: item to insert
344 *
345 * Insert an item into the radix tree at position @index.
346 */
347int radix_tree_insert(struct radix_tree_root *root,
348 unsigned long index, void *item)
349{
Christoph Lameter201b6262005-09-06 15:16:46 -0700350 struct radix_tree_node *node = NULL, *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 unsigned int height, shift;
352 int offset;
353 int error;
354
Nick Pigginc0bc9872007-10-16 01:24:42 -0700355 BUG_ON(radix_tree_is_indirect_ptr(item));
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /* Make sure the tree is high enough. */
Nick Piggin612d6c12006-06-23 02:03:22 -0700358 if (index > radix_tree_maxindex(root->height)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 error = radix_tree_extend(root, index);
360 if (error)
361 return error;
362 }
363
Nick Piggin27d20fd2010-11-11 14:05:19 -0800364 slot = indirect_to_ptr(root->rnode);
Nick Pigginc0bc9872007-10-16 01:24:42 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 height = root->height;
367 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
368
369 offset = 0; /* uninitialised var warning */
Nick Piggin612d6c12006-06-23 02:03:22 -0700370 while (height > 0) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700371 if (slot == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* Have to add a child node. */
Christoph Lameter201b6262005-09-06 15:16:46 -0700373 if (!(slot = radix_tree_node_alloc(root)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return -ENOMEM;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800375 slot->height = height;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800376 slot->parent = node;
Christoph Lameter201b6262005-09-06 15:16:46 -0700377 if (node) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800378 rcu_assign_pointer(node->slots[offset], slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 node->count++;
Christoph Lameter201b6262005-09-06 15:16:46 -0700380 } else
Nick Piggin27d20fd2010-11-11 14:05:19 -0800381 rcu_assign_pointer(root->rnode, ptr_to_indirect(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
384 /* Go a level down */
385 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Christoph Lameter201b6262005-09-06 15:16:46 -0700386 node = slot;
387 slot = node->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 shift -= RADIX_TREE_MAP_SHIFT;
389 height--;
Nick Piggin612d6c12006-06-23 02:03:22 -0700390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Christoph Lameter201b6262005-09-06 15:16:46 -0700392 if (slot != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return -EEXIST;
Christoph Lameter201b6262005-09-06 15:16:46 -0700394
Nick Piggin612d6c12006-06-23 02:03:22 -0700395 if (node) {
396 node->count++;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800397 rcu_assign_pointer(node->slots[offset], item);
Nick Piggin612d6c12006-06-23 02:03:22 -0700398 BUG_ON(tag_get(node, 0, offset));
399 BUG_ON(tag_get(node, 1, offset));
400 } else {
Nick Pigginc0bc9872007-10-16 01:24:42 -0700401 rcu_assign_pointer(root->rnode, item);
Nick Piggin612d6c12006-06-23 02:03:22 -0700402 BUG_ON(root_tag_get(root, 0));
403 BUG_ON(root_tag_get(root, 1));
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
407}
408EXPORT_SYMBOL(radix_tree_insert);
409
Huang Shijieb72b71c2009-06-16 15:33:42 -0700410/*
411 * is_slot == 1 : search for the slot.
412 * is_slot == 0 : search for the node.
Hans Reisera4331362005-11-07 00:59:29 -0800413 */
Huang Shijieb72b71c2009-06-16 15:33:42 -0700414static void *radix_tree_lookup_element(struct radix_tree_root *root,
415 unsigned long index, int is_slot)
Hans Reisera4331362005-11-07 00:59:29 -0800416{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800417 unsigned int height, shift;
418 struct radix_tree_node *node, **slot;
419
Paul E. McKenney2676a582010-02-22 17:04:54 -0800420 node = rcu_dereference_raw(root->rnode);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800421 if (node == NULL)
422 return NULL;
423
Nick Pigginc0bc9872007-10-16 01:24:42 -0700424 if (!radix_tree_is_indirect_ptr(node)) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800425 if (index > 0)
426 return NULL;
Huang Shijieb72b71c2009-06-16 15:33:42 -0700427 return is_slot ? (void *)&root->rnode : node;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800428 }
Nick Piggin27d20fd2010-11-11 14:05:19 -0800429 node = indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800430
431 height = node->height;
432 if (index > radix_tree_maxindex(height))
433 return NULL;
434
435 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
436
437 do {
438 slot = (struct radix_tree_node **)
439 (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
Paul E. McKenney2676a582010-02-22 17:04:54 -0800440 node = rcu_dereference_raw(*slot);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800441 if (node == NULL)
442 return NULL;
443
444 shift -= RADIX_TREE_MAP_SHIFT;
445 height--;
446 } while (height > 0);
447
Nick Piggin27d20fd2010-11-11 14:05:19 -0800448 return is_slot ? (void *)slot : indirect_to_ptr(node);
Huang Shijieb72b71c2009-06-16 15:33:42 -0700449}
450
451/**
452 * radix_tree_lookup_slot - lookup a slot in a radix tree
453 * @root: radix tree root
454 * @index: index key
455 *
456 * Returns: the slot corresponding to the position @index in the
457 * radix tree @root. This is useful for update-if-exists operations.
458 *
459 * This function can be called under rcu_read_lock iff the slot is not
460 * modified by radix_tree_replace_slot, otherwise it must be called
461 * exclusive from other writers. Any dereference of the slot must be done
462 * using radix_tree_deref_slot.
463 */
464void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
465{
466 return (void **)radix_tree_lookup_element(root, index, 1);
Hans Reisera4331362005-11-07 00:59:29 -0800467}
468EXPORT_SYMBOL(radix_tree_lookup_slot);
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/**
471 * radix_tree_lookup - perform lookup operation on a radix tree
472 * @root: radix tree root
473 * @index: index key
474 *
475 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800476 *
477 * This function can be called under rcu_read_lock, however the caller
478 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
479 * them safely). No RCU barriers are required to access or modify the
480 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
482void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
483{
Huang Shijieb72b71c2009-06-16 15:33:42 -0700484 return radix_tree_lookup_element(root, index, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486EXPORT_SYMBOL(radix_tree_lookup);
487
488/**
489 * radix_tree_tag_set - set a tag on a radix tree node
490 * @root: radix tree root
491 * @index: index key
492 * @tag: tag index
493 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800494 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
495 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * the root all the way down to the leaf node.
497 *
498 * Returns the address of the tagged item. Setting a tag on a not-present
499 * item is a bug.
500 */
501void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800502 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700505 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 height = root->height;
Peter Zijlstra4c91c362006-06-23 02:03:25 -0700508 BUG_ON(index > radix_tree_maxindex(height));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Nick Piggin27d20fd2010-11-11 14:05:19 -0800510 slot = indirect_to_ptr(root->rnode);
Nick Piggin612d6c12006-06-23 02:03:22 -0700511 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 while (height > 0) {
514 int offset;
515
516 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Nick Piggind5274262006-01-08 01:01:41 -0800517 if (!tag_get(slot, tag, offset))
518 tag_set(slot, tag, offset);
Christoph Lameter201b6262005-09-06 15:16:46 -0700519 slot = slot->slots[offset];
520 BUG_ON(slot == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 shift -= RADIX_TREE_MAP_SHIFT;
522 height--;
523 }
524
Nick Piggin612d6c12006-06-23 02:03:22 -0700525 /* set the root's tag bit */
526 if (slot && !root_tag_get(root, tag))
527 root_tag_set(root, tag);
528
Christoph Lameter201b6262005-09-06 15:16:46 -0700529 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531EXPORT_SYMBOL(radix_tree_tag_set);
532
533/**
534 * radix_tree_tag_clear - clear a tag on a radix tree node
535 * @root: radix tree root
536 * @index: index key
537 * @tag: tag index
538 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800539 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
540 * corresponding to @index in the radix tree. If
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 * this causes the leaf node to have no tags set then clear the tag in the
542 * next-to-leaf node, etc.
543 *
544 * Returns the address of the tagged item on success, else NULL. ie:
545 * has the same return value and semantics as radix_tree_lookup().
546 */
547void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800548 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800550 struct radix_tree_node *node = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -0700551 struct radix_tree_node *slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 unsigned int height, shift;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800553 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 height = root->height;
556 if (index > radix_tree_maxindex(height))
557 goto out;
558
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800559 shift = height * RADIX_TREE_MAP_SHIFT;
Nick Piggin27d20fd2010-11-11 14:05:19 -0800560 slot = indirect_to_ptr(root->rnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800562 while (shift) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700563 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto out;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 shift -= RADIX_TREE_MAP_SHIFT;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800567 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
568 node = slot;
569 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571
Nick Piggin612d6c12006-06-23 02:03:22 -0700572 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto out;
574
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800575 while (node) {
576 if (!tag_get(node, tag, offset))
Nick Piggind5274262006-01-08 01:01:41 -0800577 goto out;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800578 tag_clear(node, tag, offset);
579 if (any_tag_set(node, tag))
Nick Piggin6e954b92006-01-08 01:01:40 -0800580 goto out;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800581
582 index >>= RADIX_TREE_MAP_SHIFT;
583 offset = index & RADIX_TREE_MAP_MASK;
584 node = node->parent;
Nick Piggin612d6c12006-06-23 02:03:22 -0700585 }
586
587 /* clear the root's tag bit */
588 if (root_tag_get(root, tag))
589 root_tag_clear(root, tag);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591out:
Nick Piggin612d6c12006-06-23 02:03:22 -0700592 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594EXPORT_SYMBOL(radix_tree_tag_clear);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700597 * radix_tree_tag_get - get a tag on a radix tree node
598 * @root: radix tree root
599 * @index: index key
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800600 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700602 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 *
Nick Piggin612d6c12006-06-23 02:03:22 -0700604 * 0: tag not present or not set
605 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +0100606 *
607 * Note that the return value of this function may not be relied on, even if
608 * the RCU lock is held, unless tag modification and node deletion are excluded
609 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
611int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800612 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 unsigned int height, shift;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800615 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Nick Piggin612d6c12006-06-23 02:03:22 -0700617 /* check the root's tag bit */
618 if (!root_tag_get(root, tag))
619 return 0;
620
Paul E. McKenney2676a582010-02-22 17:04:54 -0800621 node = rcu_dereference_raw(root->rnode);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800622 if (node == NULL)
623 return 0;
624
Nick Pigginc0bc9872007-10-16 01:24:42 -0700625 if (!radix_tree_is_indirect_ptr(node))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800626 return (index == 0);
Nick Piggin27d20fd2010-11-11 14:05:19 -0800627 node = indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800628
629 height = node->height;
630 if (index > radix_tree_maxindex(height))
631 return 0;
Nick Piggin612d6c12006-06-23 02:03:22 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 for ( ; ; ) {
636 int offset;
637
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800638 if (node == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return 0;
640
641 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800642 if (!tag_get(node, tag, offset))
Hugh Dickins3fa36ac2011-10-31 17:07:02 -0700643 return 0;
David Howellsce826532010-04-06 22:36:20 +0100644 if (height == 1)
Hugh Dickins3fa36ac2011-10-31 17:07:02 -0700645 return 1;
Paul E. McKenney2676a582010-02-22 17:04:54 -0800646 node = rcu_dereference_raw(node->slots[offset]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 shift -= RADIX_TREE_MAP_SHIFT;
648 height--;
649 }
650}
651EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700653/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700654 * radix_tree_next_chunk - find next chunk of slots for iteration
655 *
656 * @root: radix tree root
657 * @iter: iterator state
658 * @flags: RADIX_TREE_ITER_* flags and tag index
659 * Returns: pointer to chunk first slot, or NULL if iteration is over
660 */
661void **radix_tree_next_chunk(struct radix_tree_root *root,
662 struct radix_tree_iter *iter, unsigned flags)
663{
664 unsigned shift, tag = flags & RADIX_TREE_ITER_TAG_MASK;
665 struct radix_tree_node *rnode, *node;
666 unsigned long index, offset;
667
668 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
669 return NULL;
670
671 /*
672 * Catch next_index overflow after ~0UL. iter->index never overflows
673 * during iterating; it can be zero only at the beginning.
674 * And we cannot overflow iter->next_index in a single step,
675 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
676 */
677 index = iter->next_index;
678 if (!index && iter->index)
679 return NULL;
680
681 rnode = rcu_dereference_raw(root->rnode);
682 if (radix_tree_is_indirect_ptr(rnode)) {
683 rnode = indirect_to_ptr(rnode);
684 } else if (rnode && !index) {
685 /* Single-slot tree */
686 iter->index = 0;
687 iter->next_index = 1;
688 iter->tags = 1;
689 return (void **)&root->rnode;
690 } else
691 return NULL;
692
693restart:
694 shift = (rnode->height - 1) * RADIX_TREE_MAP_SHIFT;
695 offset = index >> shift;
696
697 /* Index outside of the tree */
698 if (offset >= RADIX_TREE_MAP_SIZE)
699 return NULL;
700
701 node = rnode;
702 while (1) {
703 if ((flags & RADIX_TREE_ITER_TAGGED) ?
704 !test_bit(offset, node->tags[tag]) :
705 !node->slots[offset]) {
706 /* Hole detected */
707 if (flags & RADIX_TREE_ITER_CONTIG)
708 return NULL;
709
710 if (flags & RADIX_TREE_ITER_TAGGED)
711 offset = radix_tree_find_next_bit(
712 node->tags[tag],
713 RADIX_TREE_MAP_SIZE,
714 offset + 1);
715 else
716 while (++offset < RADIX_TREE_MAP_SIZE) {
717 if (node->slots[offset])
718 break;
719 }
720 index &= ~((RADIX_TREE_MAP_SIZE << shift) - 1);
721 index += offset << shift;
722 /* Overflow after ~0UL */
723 if (!index)
724 return NULL;
725 if (offset == RADIX_TREE_MAP_SIZE)
726 goto restart;
727 }
728
729 /* This is leaf-node */
730 if (!shift)
731 break;
732
733 node = rcu_dereference_raw(node->slots[offset]);
734 if (node == NULL)
735 goto restart;
736 shift -= RADIX_TREE_MAP_SHIFT;
737 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
738 }
739
740 /* Update the iterator state */
741 iter->index = index;
742 iter->next_index = (index | RADIX_TREE_MAP_MASK) + 1;
743
744 /* Construct iter->tags bit-mask from node->tags[tag] array */
745 if (flags & RADIX_TREE_ITER_TAGGED) {
746 unsigned tag_long, tag_bit;
747
748 tag_long = offset / BITS_PER_LONG;
749 tag_bit = offset % BITS_PER_LONG;
750 iter->tags = node->tags[tag][tag_long] >> tag_bit;
751 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
752 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
753 /* Pick tags from next element */
754 if (tag_bit)
755 iter->tags |= node->tags[tag][tag_long + 1] <<
756 (BITS_PER_LONG - tag_bit);
757 /* Clip chunk size, here only BITS_PER_LONG tags */
758 iter->next_index = index + BITS_PER_LONG;
759 }
760 }
761
762 return node->slots + offset;
763}
764EXPORT_SYMBOL(radix_tree_next_chunk);
765
766/**
Jan Karaebf8aa42010-08-09 17:19:11 -0700767 * radix_tree_range_tag_if_tagged - for each item in given range set given
768 * tag if item has another tag set
769 * @root: radix tree root
770 * @first_indexp: pointer to a starting index of a range to scan
771 * @last_index: last index of a range to scan
772 * @nr_to_tag: maximum number items to tag
773 * @iftag: tag index to test
774 * @settag: tag index to set if tested tag is set
775 *
776 * This function scans range of radix tree from first_index to last_index
777 * (inclusive). For each item in the range if iftag is set, the function sets
778 * also settag. The function stops either after tagging nr_to_tag items or
779 * after reaching last_index.
780 *
Dave Chinner144dcfc2010-08-23 10:33:53 +1000781 * The tags must be set from the leaf level only and propagated back up the
782 * path to the root. We must do this so that we resolve the full path before
783 * setting any tags on intermediate nodes. If we set tags as we descend, then
784 * we can get to the leaf node and find that the index that has the iftag
785 * set is outside the range we are scanning. This reults in dangling tags and
786 * can lead to problems with later tag operations (e.g. livelocks on lookups).
787 *
Jan Karaebf8aa42010-08-09 17:19:11 -0700788 * The function returns number of leaves where the tag was set and sets
789 * *first_indexp to the first unscanned index.
Jan Karad5ed3a42010-08-19 14:13:33 -0700790 * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must
791 * be prepared to handle that.
Jan Karaebf8aa42010-08-09 17:19:11 -0700792 */
793unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
794 unsigned long *first_indexp, unsigned long last_index,
795 unsigned long nr_to_tag,
796 unsigned int iftag, unsigned int settag)
797{
Dave Chinner144dcfc2010-08-23 10:33:53 +1000798 unsigned int height = root->height;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800799 struct radix_tree_node *node = NULL;
Dave Chinner144dcfc2010-08-23 10:33:53 +1000800 struct radix_tree_node *slot;
801 unsigned int shift;
802 unsigned long tagged = 0;
803 unsigned long index = *first_indexp;
Jan Karaebf8aa42010-08-09 17:19:11 -0700804
805 last_index = min(last_index, radix_tree_maxindex(height));
806 if (index > last_index)
807 return 0;
808 if (!nr_to_tag)
809 return 0;
810 if (!root_tag_get(root, iftag)) {
811 *first_indexp = last_index + 1;
812 return 0;
813 }
814 if (height == 0) {
815 *first_indexp = last_index + 1;
816 root_tag_set(root, settag);
817 return 1;
818 }
819
820 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Nick Piggin27d20fd2010-11-11 14:05:19 -0800821 slot = indirect_to_ptr(root->rnode);
Jan Karaebf8aa42010-08-09 17:19:11 -0700822
823 for (;;) {
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800824 unsigned long upindex;
Jan Karaebf8aa42010-08-09 17:19:11 -0700825 int offset;
826
827 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
828 if (!slot->slots[offset])
829 goto next;
830 if (!tag_get(slot, iftag, offset))
831 goto next;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800832 if (shift) {
Dave Chinner144dcfc2010-08-23 10:33:53 +1000833 /* Go down one level */
Dave Chinner144dcfc2010-08-23 10:33:53 +1000834 shift -= RADIX_TREE_MAP_SHIFT;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800835 node = slot;
Dave Chinner144dcfc2010-08-23 10:33:53 +1000836 slot = slot->slots[offset];
837 continue;
Jan Karaebf8aa42010-08-09 17:19:11 -0700838 }
Dave Chinner144dcfc2010-08-23 10:33:53 +1000839
840 /* tag the leaf */
841 tagged++;
842 tag_set(slot, settag, offset);
843
844 /* walk back up the path tagging interior nodes */
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800845 upindex = index;
846 while (node) {
847 upindex >>= RADIX_TREE_MAP_SHIFT;
848 offset = upindex & RADIX_TREE_MAP_MASK;
849
Dave Chinner144dcfc2010-08-23 10:33:53 +1000850 /* stop if we find a node with the tag already set */
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800851 if (tag_get(node, settag, offset))
Dave Chinner144dcfc2010-08-23 10:33:53 +1000852 break;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800853 tag_set(node, settag, offset);
854 node = node->parent;
Dave Chinner144dcfc2010-08-23 10:33:53 +1000855 }
856
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800857 /*
858 * Small optimization: now clear that node pointer.
859 * Since all of this slot's ancestors now have the tag set
860 * from setting it above, we have no further need to walk
861 * back up the tree setting tags, until we update slot to
862 * point to another radix_tree_node.
863 */
864 node = NULL;
865
Jan Karaebf8aa42010-08-09 17:19:11 -0700866next:
867 /* Go to next item at level determined by 'shift' */
868 index = ((index >> shift) + 1) << shift;
Jan Karad5ed3a42010-08-19 14:13:33 -0700869 /* Overflow can happen when last_index is ~0UL... */
870 if (index > last_index || !index)
Jan Karaebf8aa42010-08-09 17:19:11 -0700871 break;
872 if (tagged >= nr_to_tag)
873 break;
874 while (((index >> shift) & RADIX_TREE_MAP_MASK) == 0) {
875 /*
876 * We've fully scanned this node. Go up. Because
877 * last_index is guaranteed to be in the tree, what
878 * we do below cannot wander astray.
879 */
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800880 slot = slot->parent;
Jan Karaebf8aa42010-08-09 17:19:11 -0700881 shift += RADIX_TREE_MAP_SHIFT;
882 }
883 }
884 /*
Toshiyuki Okajimaac15ee62011-01-25 15:07:32 -0800885 * We need not to tag the root tag if there is no tag which is set with
886 * settag within the range from *first_indexp to last_index.
Jan Karaebf8aa42010-08-09 17:19:11 -0700887 */
Toshiyuki Okajimaac15ee62011-01-25 15:07:32 -0800888 if (tagged > 0)
889 root_tag_set(root, settag);
Jan Karaebf8aa42010-08-09 17:19:11 -0700890 *first_indexp = index;
891
892 return tagged;
893}
894EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
895
896
897/**
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700898 * radix_tree_next_hole - find the next hole (not-present entry)
899 * @root: tree root
900 * @index: index key
901 * @max_scan: maximum range to search
902 *
903 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest
904 * indexed hole.
905 *
906 * Returns: the index of the hole if found, otherwise returns an index
907 * outside of the set specified (in which case 'return - index >= max_scan'
Wu Fengguang8e6bdb72008-11-27 11:42:22 +0100908 * will be true). In rare cases of index wrap-around, 0 will be returned.
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700909 *
910 * radix_tree_next_hole may be called under rcu_read_lock. However, like
Wu Fengguang8e6bdb72008-11-27 11:42:22 +0100911 * radix_tree_gang_lookup, this will not atomically search a snapshot of
912 * the tree at a single point in time. For example, if a hole is created
913 * at index 5, then subsequently a hole is created at index 10,
914 * radix_tree_next_hole covering both indexes may return 10 if called
915 * under rcu_read_lock.
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700916 */
917unsigned long radix_tree_next_hole(struct radix_tree_root *root,
918 unsigned long index, unsigned long max_scan)
919{
920 unsigned long i;
921
922 for (i = 0; i < max_scan; i++) {
923 if (!radix_tree_lookup(root, index))
924 break;
925 index++;
926 if (index == 0)
927 break;
928 }
929
930 return index;
931}
932EXPORT_SYMBOL(radix_tree_next_hole);
933
Wu Fengguangdc566122009-06-16 15:31:32 -0700934/**
935 * radix_tree_prev_hole - find the prev hole (not-present entry)
936 * @root: tree root
937 * @index: index key
938 * @max_scan: maximum range to search
939 *
940 * Search backwards in the range [max(index-max_scan+1, 0), index]
941 * for the first hole.
942 *
943 * Returns: the index of the hole if found, otherwise returns an index
944 * outside of the set specified (in which case 'index - return >= max_scan'
Cesar Eduardo Barrosedcd1d82010-05-26 14:44:27 -0700945 * will be true). In rare cases of wrap-around, ULONG_MAX will be returned.
Wu Fengguangdc566122009-06-16 15:31:32 -0700946 *
947 * radix_tree_next_hole may be called under rcu_read_lock. However, like
948 * radix_tree_gang_lookup, this will not atomically search a snapshot of
949 * the tree at a single point in time. For example, if a hole is created
950 * at index 10, then subsequently a hole is created at index 5,
951 * radix_tree_prev_hole covering both indexes may return 5 if called under
952 * rcu_read_lock.
953 */
954unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
955 unsigned long index, unsigned long max_scan)
956{
957 unsigned long i;
958
959 for (i = 0; i < max_scan; i++) {
960 if (!radix_tree_lookup(root, index))
961 break;
962 index--;
Cesar Eduardo Barrosedcd1d82010-05-26 14:44:27 -0700963 if (index == ULONG_MAX)
Wu Fengguangdc566122009-06-16 15:31:32 -0700964 break;
965 }
966
967 return index;
968}
969EXPORT_SYMBOL(radix_tree_prev_hole);
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971static unsigned int
Hugh Dickins63286502011-08-03 16:21:18 -0700972__lookup(struct radix_tree_node *slot, void ***results, unsigned long *indices,
973 unsigned long index, unsigned int max_items, unsigned long *next_index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 unsigned int nr_found = 0;
Christoph Lameter201b6262005-09-06 15:16:46 -0700976 unsigned int shift, height;
Christoph Lameter201b6262005-09-06 15:16:46 -0700977 unsigned long i;
978
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800979 height = slot->height;
980 if (height == 0)
Christoph Lameter201b6262005-09-06 15:16:46 -0700981 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Christoph Lameter201b6262005-09-06 15:16:46 -0700984 for ( ; height > 1; height--) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800985 i = (index >> shift) & RADIX_TREE_MAP_MASK;
986 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (slot->slots[i] != NULL)
988 break;
989 index &= ~((1UL << shift) - 1);
990 index += 1UL << shift;
991 if (index == 0)
992 goto out; /* 32-bit wraparound */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800993 i++;
994 if (i == RADIX_TREE_MAP_SIZE)
995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 shift -= RADIX_TREE_MAP_SHIFT;
Paul E. McKenney2676a582010-02-22 17:04:54 -0800999 slot = rcu_dereference_raw(slot->slots[i]);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001000 if (slot == NULL)
1001 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Christoph Lameter201b6262005-09-06 15:16:46 -07001003
1004 /* Bottom level: grab some items */
1005 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
Nick Piggin47feff22008-07-25 19:45:29 -07001006 if (slot->slots[i]) {
Hugh Dickins63286502011-08-03 16:21:18 -07001007 results[nr_found] = &(slot->slots[i]);
1008 if (indices)
1009 indices[nr_found] = index;
1010 if (++nr_found == max_items) {
1011 index++;
Christoph Lameter201b6262005-09-06 15:16:46 -07001012 goto out;
Hugh Dickins63286502011-08-03 16:21:18 -07001013 }
Christoph Lameter201b6262005-09-06 15:16:46 -07001014 }
Hugh Dickins63286502011-08-03 16:21:18 -07001015 index++;
Christoph Lameter201b6262005-09-06 15:16:46 -07001016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017out:
1018 *next_index = index;
1019 return nr_found;
1020}
1021
1022/**
1023 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1024 * @root: radix tree root
1025 * @results: where the results of the lookup are placed
1026 * @first_index: start the lookup from this key
1027 * @max_items: place up to this many items at *results
1028 *
1029 * Performs an index-ascending scan of the tree for present items. Places
1030 * them at *@results and returns the number of items which were placed at
1031 * *@results.
1032 *
1033 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001034 *
1035 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1036 * rcu_read_lock. In this case, rather than the returned results being
1037 * an atomic snapshot of the tree at a single point in time, the semantics
1038 * of an RCU protected gang lookup are as though multiple radix_tree_lookups
1039 * have been issued in individual locks, and results stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 */
1041unsigned int
1042radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
1043 unsigned long first_index, unsigned int max_items)
1044{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001045 unsigned long max_index;
1046 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 unsigned long cur_index = first_index;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001048 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Paul E. McKenney2676a582010-02-22 17:04:54 -08001050 node = rcu_dereference_raw(root->rnode);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001051 if (!node)
1052 return 0;
1053
Nick Pigginc0bc9872007-10-16 01:24:42 -07001054 if (!radix_tree_is_indirect_ptr(node)) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001055 if (first_index > 0)
1056 return 0;
Nick Pigginc0bc9872007-10-16 01:24:42 -07001057 results[0] = node;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001058 return 1;
1059 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001060 node = indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001061
1062 max_index = radix_tree_maxindex(node->height);
1063
1064 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 while (ret < max_items) {
Nick Piggin47feff22008-07-25 19:45:29 -07001066 unsigned int nr_found, slots_found, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 unsigned long next_index; /* Index of next search */
1068
1069 if (cur_index > max_index)
1070 break;
Hugh Dickins63286502011-08-03 16:21:18 -07001071 slots_found = __lookup(node, (void ***)results + ret, NULL,
1072 cur_index, max_items - ret, &next_index);
Nick Piggin47feff22008-07-25 19:45:29 -07001073 nr_found = 0;
1074 for (i = 0; i < slots_found; i++) {
1075 struct radix_tree_node *slot;
1076 slot = *(((void ***)results)[ret + i]);
1077 if (!slot)
1078 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001079 results[ret + nr_found] =
1080 indirect_to_ptr(rcu_dereference_raw(slot));
Nick Piggin47feff22008-07-25 19:45:29 -07001081 nr_found++;
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ret += nr_found;
1084 if (next_index == 0)
1085 break;
1086 cur_index = next_index;
1087 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return ret;
1090}
1091EXPORT_SYMBOL(radix_tree_gang_lookup);
1092
Nick Piggin47feff22008-07-25 19:45:29 -07001093/**
1094 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1095 * @root: radix tree root
1096 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001097 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001098 * @first_index: start the lookup from this key
1099 * @max_items: place up to this many items at *results
1100 *
1101 * Performs an index-ascending scan of the tree for present items. Places
1102 * their slots at *@results and returns the number of items which were
1103 * placed at *@results.
1104 *
1105 * The implementation is naive.
1106 *
1107 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1108 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1109 * protection, radix_tree_deref_slot may fail requiring a retry.
1110 */
1111unsigned int
Hugh Dickins63286502011-08-03 16:21:18 -07001112radix_tree_gang_lookup_slot(struct radix_tree_root *root,
1113 void ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001114 unsigned long first_index, unsigned int max_items)
1115{
1116 unsigned long max_index;
1117 struct radix_tree_node *node;
1118 unsigned long cur_index = first_index;
1119 unsigned int ret;
1120
Paul E. McKenney2676a582010-02-22 17:04:54 -08001121 node = rcu_dereference_raw(root->rnode);
Nick Piggin47feff22008-07-25 19:45:29 -07001122 if (!node)
1123 return 0;
1124
1125 if (!radix_tree_is_indirect_ptr(node)) {
1126 if (first_index > 0)
1127 return 0;
1128 results[0] = (void **)&root->rnode;
Hugh Dickins63286502011-08-03 16:21:18 -07001129 if (indices)
1130 indices[0] = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001131 return 1;
1132 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001133 node = indirect_to_ptr(node);
Nick Piggin47feff22008-07-25 19:45:29 -07001134
1135 max_index = radix_tree_maxindex(node->height);
1136
1137 ret = 0;
1138 while (ret < max_items) {
1139 unsigned int slots_found;
1140 unsigned long next_index; /* Index of next search */
1141
1142 if (cur_index > max_index)
1143 break;
Hugh Dickins63286502011-08-03 16:21:18 -07001144 slots_found = __lookup(node, results + ret,
1145 indices ? indices + ret : NULL,
1146 cur_index, max_items - ret, &next_index);
Nick Piggin47feff22008-07-25 19:45:29 -07001147 ret += slots_found;
1148 if (next_index == 0)
1149 break;
1150 cur_index = next_index;
1151 }
1152
1153 return ret;
1154}
1155EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/*
1158 * FIXME: the two tag_get()s here should use find_next_bit() instead of
1159 * open-coding the search.
1160 */
1161static unsigned int
Nick Piggin47feff22008-07-25 19:45:29 -07001162__lookup_tag(struct radix_tree_node *slot, void ***results, unsigned long index,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001163 unsigned int max_items, unsigned long *next_index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 unsigned int nr_found = 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001166 unsigned int shift, height;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001168 height = slot->height;
1169 if (height == 0)
Nick Piggin612d6c12006-06-23 02:03:22 -07001170 goto out;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001171 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
Nick Piggin612d6c12006-06-23 02:03:22 -07001172
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001173 while (height > 0) {
1174 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001176 for (;;) {
1177 if (tag_get(slot, tag, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 index &= ~((1UL << shift) - 1);
1180 index += 1UL << shift;
1181 if (index == 0)
1182 goto out; /* 32-bit wraparound */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001183 i++;
1184 if (i == RADIX_TREE_MAP_SIZE)
1185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 height--;
1188 if (height == 0) { /* Bottom level: grab some items */
1189 unsigned long j = index & RADIX_TREE_MAP_MASK;
1190
1191 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
1192 index++;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001193 if (!tag_get(slot, tag, j))
1194 continue;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001195 /*
1196 * Even though the tag was found set, we need to
1197 * recheck that we have a non-NULL node, because
1198 * if this lookup is lockless, it may have been
1199 * subsequently deleted.
1200 *
1201 * Similar care must be taken in any place that
1202 * lookup ->slots[x] without a lock (ie. can't
1203 * rely on its value remaining the same).
1204 */
Nick Piggin47feff22008-07-25 19:45:29 -07001205 if (slot->slots[j]) {
1206 results[nr_found++] = &(slot->slots[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (nr_found == max_items)
1208 goto out;
1209 }
1210 }
1211 }
1212 shift -= RADIX_TREE_MAP_SHIFT;
Paul E. McKenney2676a582010-02-22 17:04:54 -08001213 slot = rcu_dereference_raw(slot->slots[i]);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001214 if (slot == NULL)
1215 break;
1216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217out:
1218 *next_index = index;
1219 return nr_found;
1220}
1221
1222/**
1223 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1224 * based on a tag
1225 * @root: radix tree root
1226 * @results: where the results of the lookup are placed
1227 * @first_index: start the lookup from this key
1228 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001229 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 *
1231 * Performs an index-ascending scan of the tree for present items which
1232 * have the tag indexed by @tag set. Places the items at *@results and
1233 * returns the number of items which were placed at *@results.
1234 */
1235unsigned int
1236radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001237 unsigned long first_index, unsigned int max_items,
1238 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001240 struct radix_tree_node *node;
1241 unsigned long max_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 unsigned long cur_index = first_index;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001243 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Nick Piggin612d6c12006-06-23 02:03:22 -07001245 /* check the root's tag bit */
1246 if (!root_tag_get(root, tag))
1247 return 0;
1248
Paul E. McKenney2676a582010-02-22 17:04:54 -08001249 node = rcu_dereference_raw(root->rnode);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001250 if (!node)
1251 return 0;
1252
Nick Pigginc0bc9872007-10-16 01:24:42 -07001253 if (!radix_tree_is_indirect_ptr(node)) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001254 if (first_index > 0)
1255 return 0;
Nick Pigginc0bc9872007-10-16 01:24:42 -07001256 results[0] = node;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001257 return 1;
1258 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001259 node = indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001260
1261 max_index = radix_tree_maxindex(node->height);
1262
1263 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 while (ret < max_items) {
Nick Piggin47feff22008-07-25 19:45:29 -07001265 unsigned int nr_found, slots_found, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 unsigned long next_index; /* Index of next search */
1267
1268 if (cur_index > max_index)
1269 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001270 slots_found = __lookup_tag(node, (void ***)results + ret,
1271 cur_index, max_items - ret, &next_index, tag);
1272 nr_found = 0;
1273 for (i = 0; i < slots_found; i++) {
1274 struct radix_tree_node *slot;
1275 slot = *(((void ***)results)[ret + i]);
1276 if (!slot)
1277 continue;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001278 results[ret + nr_found] =
1279 indirect_to_ptr(rcu_dereference_raw(slot));
Nick Piggin47feff22008-07-25 19:45:29 -07001280 nr_found++;
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 ret += nr_found;
1283 if (next_index == 0)
1284 break;
1285 cur_index = next_index;
1286 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return ret;
1289}
1290EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1291
1292/**
Nick Piggin47feff22008-07-25 19:45:29 -07001293 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1294 * radix tree based on a tag
1295 * @root: radix tree root
1296 * @results: where the results of the lookup are placed
1297 * @first_index: start the lookup from this key
1298 * @max_items: place up to this many items at *results
1299 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1300 *
1301 * Performs an index-ascending scan of the tree for present items which
1302 * have the tag indexed by @tag set. Places the slots at *@results and
1303 * returns the number of slots which were placed at *@results.
1304 */
1305unsigned int
1306radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
1307 unsigned long first_index, unsigned int max_items,
1308 unsigned int tag)
1309{
1310 struct radix_tree_node *node;
1311 unsigned long max_index;
1312 unsigned long cur_index = first_index;
1313 unsigned int ret;
1314
1315 /* check the root's tag bit */
1316 if (!root_tag_get(root, tag))
1317 return 0;
1318
Paul E. McKenney2676a582010-02-22 17:04:54 -08001319 node = rcu_dereference_raw(root->rnode);
Nick Piggin47feff22008-07-25 19:45:29 -07001320 if (!node)
1321 return 0;
1322
1323 if (!radix_tree_is_indirect_ptr(node)) {
1324 if (first_index > 0)
1325 return 0;
1326 results[0] = (void **)&root->rnode;
1327 return 1;
1328 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001329 node = indirect_to_ptr(node);
Nick Piggin47feff22008-07-25 19:45:29 -07001330
1331 max_index = radix_tree_maxindex(node->height);
1332
1333 ret = 0;
1334 while (ret < max_items) {
1335 unsigned int slots_found;
1336 unsigned long next_index; /* Index of next search */
1337
1338 if (cur_index > max_index)
1339 break;
1340 slots_found = __lookup_tag(node, results + ret,
1341 cur_index, max_items - ret, &next_index, tag);
1342 ret += slots_found;
1343 if (next_index == 0)
1344 break;
1345 cur_index = next_index;
1346 }
1347
1348 return ret;
1349}
1350EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1351
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001352#if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
1353#include <linux/sched.h> /* for cond_resched() */
1354
1355/*
1356 * This linear search is at present only useful to shmem_unuse_inode().
1357 */
1358static unsigned long __locate(struct radix_tree_node *slot, void *item,
1359 unsigned long index, unsigned long *found_index)
1360{
1361 unsigned int shift, height;
1362 unsigned long i;
1363
1364 height = slot->height;
1365 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
1366
1367 for ( ; height > 1; height--) {
1368 i = (index >> shift) & RADIX_TREE_MAP_MASK;
1369 for (;;) {
1370 if (slot->slots[i] != NULL)
1371 break;
1372 index &= ~((1UL << shift) - 1);
1373 index += 1UL << shift;
1374 if (index == 0)
1375 goto out; /* 32-bit wraparound */
1376 i++;
1377 if (i == RADIX_TREE_MAP_SIZE)
1378 goto out;
1379 }
1380
1381 shift -= RADIX_TREE_MAP_SHIFT;
1382 slot = rcu_dereference_raw(slot->slots[i]);
1383 if (slot == NULL)
1384 goto out;
1385 }
1386
1387 /* Bottom level: check items */
1388 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
1389 if (slot->slots[i] == item) {
1390 *found_index = index + i;
1391 index = 0;
1392 goto out;
1393 }
1394 }
1395 index += RADIX_TREE_MAP_SIZE;
1396out:
1397 return index;
1398}
1399
1400/**
1401 * radix_tree_locate_item - search through radix tree for item
1402 * @root: radix tree root
1403 * @item: item to be found
1404 *
1405 * Returns index where item was found, or -1 if not found.
1406 * Caller must hold no lock (since this time-consuming function needs
1407 * to be preemptible), and must check afterwards if item is still there.
1408 */
1409unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1410{
1411 struct radix_tree_node *node;
1412 unsigned long max_index;
1413 unsigned long cur_index = 0;
1414 unsigned long found_index = -1;
1415
1416 do {
1417 rcu_read_lock();
1418 node = rcu_dereference_raw(root->rnode);
1419 if (!radix_tree_is_indirect_ptr(node)) {
1420 rcu_read_unlock();
1421 if (node == item)
1422 found_index = 0;
1423 break;
1424 }
1425
1426 node = indirect_to_ptr(node);
1427 max_index = radix_tree_maxindex(node->height);
1428 if (cur_index > max_index)
1429 break;
1430
1431 cur_index = __locate(node, item, cur_index, &found_index);
1432 rcu_read_unlock();
1433 cond_resched();
1434 } while (cur_index != 0 && cur_index <= max_index);
1435
1436 return found_index;
1437}
1438#else
1439unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1440{
1441 return -1;
1442}
1443#endif /* CONFIG_SHMEM && CONFIG_SWAP */
Nick Piggin47feff22008-07-25 19:45:29 -07001444
1445/**
Nick Piggina5f51c92006-01-08 01:01:41 -08001446 * radix_tree_shrink - shrink height of a radix tree to minimal
1447 * @root radix tree root
1448 */
1449static inline void radix_tree_shrink(struct radix_tree_root *root)
1450{
1451 /* try to shrink tree height */
Nick Pigginc0bc9872007-10-16 01:24:42 -07001452 while (root->height > 0) {
Nick Piggina5f51c92006-01-08 01:01:41 -08001453 struct radix_tree_node *to_free = root->rnode;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001454 struct radix_tree_node *slot;
Nick Piggina5f51c92006-01-08 01:01:41 -08001455
Nick Pigginc0bc9872007-10-16 01:24:42 -07001456 BUG_ON(!radix_tree_is_indirect_ptr(to_free));
Nick Piggin27d20fd2010-11-11 14:05:19 -08001457 to_free = indirect_to_ptr(to_free);
Nick Pigginc0bc9872007-10-16 01:24:42 -07001458
1459 /*
1460 * The candidate node has more than one child, or its child
1461 * is not at the leftmost slot, we cannot shrink.
1462 */
1463 if (to_free->count != 1)
1464 break;
1465 if (!to_free->slots[0])
1466 break;
1467
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001468 /*
1469 * We don't need rcu_assign_pointer(), since we are simply
Nick Piggin27d20fd2010-11-11 14:05:19 -08001470 * moving the node from one part of the tree to another: if it
1471 * was safe to dereference the old pointer to it
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001472 * (to_free->slots[0]), it will be safe to dereference the new
Nick Piggin27d20fd2010-11-11 14:05:19 -08001473 * one (root->rnode) as far as dependent read barriers go.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001474 */
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001475 slot = to_free->slots[0];
1476 if (root->height > 1) {
1477 slot->parent = NULL;
1478 slot = ptr_to_indirect(slot);
1479 }
1480 root->rnode = slot;
Nick Piggina5f51c92006-01-08 01:01:41 -08001481 root->height--;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001482
1483 /*
1484 * We have a dilemma here. The node's slot[0] must not be
1485 * NULLed in case there are concurrent lookups expecting to
1486 * find the item. However if this was a bottom-level node,
1487 * then it may be subject to the slot pointer being visible
1488 * to callers dereferencing it. If item corresponding to
1489 * slot[0] is subsequently deleted, these callers would expect
1490 * their slot to become empty sooner or later.
1491 *
1492 * For example, lockless pagecache will look up a slot, deref
1493 * the page pointer, and if the page is 0 refcount it means it
1494 * was concurrently deleted from pagecache so try the deref
1495 * again. Fortunately there is already a requirement for logic
1496 * to retry the entire slot lookup -- the indirect pointer
1497 * problem (replacing direct root node with an indirect pointer
1498 * also results in a stale slot). So tag the slot as indirect
1499 * to force callers to retry.
1500 */
1501 if (root->height == 0)
1502 *((unsigned long *)&to_free->slots[0]) |=
1503 RADIX_TREE_INDIRECT_PTR;
1504
Nick Piggina5f51c92006-01-08 01:01:41 -08001505 radix_tree_node_free(to_free);
1506 }
1507}
1508
1509/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 * radix_tree_delete - delete an item from a radix tree
1511 * @root: radix tree root
1512 * @index: index key
1513 *
1514 * Remove the item at @index from the radix tree rooted at @root.
1515 *
1516 * Returns the address of the deleted item, or NULL if it was not present.
1517 */
1518void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1519{
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001520 struct radix_tree_node *node = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -07001521 struct radix_tree_node *slot = NULL;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001522 struct radix_tree_node *to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 unsigned int height, shift;
Nick Piggind5274262006-01-08 01:01:41 -08001524 int tag;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001525 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 height = root->height;
1528 if (index > radix_tree_maxindex(height))
1529 goto out;
1530
Nick Piggin612d6c12006-06-23 02:03:22 -07001531 slot = root->rnode;
Nick Pigginc0bc9872007-10-16 01:24:42 -07001532 if (height == 0) {
Nick Piggin612d6c12006-06-23 02:03:22 -07001533 root_tag_clear_all(root);
1534 root->rnode = NULL;
1535 goto out;
1536 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001537 slot = indirect_to_ptr(slot);
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001538 shift = height * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Nick Piggin612d6c12006-06-23 02:03:22 -07001540 do {
Christoph Lameter201b6262005-09-06 15:16:46 -07001541 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 goto out;
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 shift -= RADIX_TREE_MAP_SHIFT;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001545 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
1546 node = slot;
1547 slot = slot->slots[offset];
1548 } while (shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Nick Piggin612d6c12006-06-23 02:03:22 -07001550 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 goto out;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 /*
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001554 * Clear all tags associated with the item to be deleted.
1555 * This way of doing it would be inefficient, but seldom is any set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001557 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001558 if (tag_get(node, tag, offset))
Nick Piggin612d6c12006-06-23 02:03:22 -07001559 radix_tree_tag_clear(root, index, tag);
Nick Piggind5274262006-01-08 01:01:41 -08001560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001562 to_free = NULL;
Christoph Lameter201b6262005-09-06 15:16:46 -07001563 /* Now free the nodes we do not need anymore */
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001564 while (node) {
1565 node->slots[offset] = NULL;
1566 node->count--;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001567 /*
1568 * Queue the node for deferred freeing after the
1569 * last reference to it disappears (set NULL, above).
1570 */
1571 if (to_free)
1572 radix_tree_node_free(to_free);
Nick Piggina5f51c92006-01-08 01:01:41 -08001573
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001574 if (node->count) {
1575 if (node == indirect_to_ptr(root->rnode))
Nick Piggina5f51c92006-01-08 01:01:41 -08001576 radix_tree_shrink(root);
Christoph Lameter201b6262005-09-06 15:16:46 -07001577 goto out;
Nick Piggina5f51c92006-01-08 01:01:41 -08001578 }
Christoph Lameter201b6262005-09-06 15:16:46 -07001579
1580 /* Node with zero slots in use so free it */
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001581 to_free = node;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001582
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001583 index >>= RADIX_TREE_MAP_SHIFT;
1584 offset = index & RADIX_TREE_MAP_MASK;
1585 node = node->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001587
Nick Piggin612d6c12006-06-23 02:03:22 -07001588 root_tag_clear_all(root);
Christoph Lameter201b6262005-09-06 15:16:46 -07001589 root->height = 0;
Nick Piggin612d6c12006-06-23 02:03:22 -07001590 root->rnode = NULL;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001591 if (to_free)
1592 radix_tree_node_free(to_free);
Nick Piggin612d6c12006-06-23 02:03:22 -07001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594out:
Nick Piggin612d6c12006-06-23 02:03:22 -07001595 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597EXPORT_SYMBOL(radix_tree_delete);
1598
1599/**
1600 * radix_tree_tagged - test whether any items in the tree are tagged
1601 * @root: radix tree root
1602 * @tag: tag to test
1603 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001604int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Nick Piggin612d6c12006-06-23 02:03:22 -07001606 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608EXPORT_SYMBOL(radix_tree_tagged);
1609
1610static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001611radix_tree_node_ctor(void *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 memset(node, 0, sizeof(struct radix_tree_node));
1614}
1615
1616static __init unsigned long __maxindex(unsigned int height)
1617{
Peter Lund430d2752007-10-16 23:29:35 -07001618 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1619 int shift = RADIX_TREE_INDEX_BITS - width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Peter Lund430d2752007-10-16 23:29:35 -07001621 if (shift < 0)
1622 return ~0UL;
1623 if (shift >= BITS_PER_LONG)
1624 return 0UL;
1625 return ~0UL >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628static __init void radix_tree_init_maxindex(void)
1629{
1630 unsigned int i;
1631
1632 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1633 height_to_maxindex[i] = __maxindex(i);
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636static int radix_tree_callback(struct notifier_block *nfb,
1637 unsigned long action,
1638 void *hcpu)
1639{
1640 int cpu = (long)hcpu;
1641 struct radix_tree_preload *rtp;
1642
1643 /* Free per-cpu pool of perloaded nodes */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001644 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 rtp = &per_cpu(radix_tree_preloads, cpu);
1646 while (rtp->nr) {
1647 kmem_cache_free(radix_tree_node_cachep,
1648 rtp->nodes[rtp->nr-1]);
1649 rtp->nodes[rtp->nr-1] = NULL;
1650 rtp->nr--;
1651 }
1652 }
1653 return NOTIFY_OK;
1654}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656void __init radix_tree_init(void)
1657{
1658 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1659 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07001660 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1661 radix_tree_node_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 radix_tree_init_maxindex();
1663 hotcpu_notifier(radix_tree_callback, 0);
1664}