blob: 63bac7d19498a8991d844b162aa135680372bb3e [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/*
Nick Piggin55368052012-05-29 15:07:34 -070076 * The radix tree is variable-height, so an insert operation not only has
77 * to build the branch to its corresponding item, it also has to build the
78 * branch to existing items if the size has to be increased (by
79 * radix_tree_extend).
80 *
81 * The worst case is a zero height tree with just a single item at index 0,
82 * and then inserting an item at index ULONG_MAX. This requires 2 new branches
83 * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
84 * Hence:
85 */
86#define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
87
88/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * Per-cpu pool of preloaded nodes
90 */
91struct radix_tree_preload {
92 int nr;
Nick Piggin55368052012-05-29 15:07:34 -070093 struct radix_tree_node *nodes[RADIX_TREE_PRELOAD_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
Harvey Harrison8cef7d52009-01-06 14:40:50 -080095static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Nick Piggin27d20fd2010-11-11 14:05:19 -080097static inline void *ptr_to_indirect(void *ptr)
98{
99 return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR);
100}
101
102static inline void *indirect_to_ptr(void *ptr)
103{
104 return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
105}
106
Nick Piggin612d6c12006-06-23 02:03:22 -0700107static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
108{
109 return root->gfp_mask & __GFP_BITS_MASK;
110}
111
Nick Piggin643b52b2008-06-12 15:21:52 -0700112static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
113 int offset)
114{
115 __set_bit(offset, node->tags[tag]);
116}
117
118static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
119 int offset)
120{
121 __clear_bit(offset, node->tags[tag]);
122}
123
124static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
125 int offset)
126{
127 return test_bit(offset, node->tags[tag]);
128}
129
130static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
131{
132 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
133}
134
135static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
136{
137 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
138}
139
140static inline void root_tag_clear_all(struct radix_tree_root *root)
141{
142 root->gfp_mask &= __GFP_BITS_MASK;
143}
144
145static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
146{
147 return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
148}
149
150/*
151 * Returns 1 if any slot in the node has this tag set.
152 * Otherwise returns 0.
153 */
154static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
155{
156 int idx;
157 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
158 if (node->tags[tag][idx])
159 return 1;
160 }
161 return 0;
162}
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700163
164/**
165 * radix_tree_find_next_bit - find the next set bit in a memory region
166 *
167 * @addr: The address to base the search on
168 * @size: The bitmap size in bits
169 * @offset: The bitnumber to start searching at
170 *
171 * Unrollable variant of find_next_bit() for constant size arrays.
172 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
173 * Returns next bit offset, or size if nothing found.
174 */
175static __always_inline unsigned long
176radix_tree_find_next_bit(const unsigned long *addr,
177 unsigned long size, unsigned long offset)
178{
179 if (!__builtin_constant_p(size))
180 return find_next_bit(addr, size, offset);
181
182 if (offset < size) {
183 unsigned long tmp;
184
185 addr += offset / BITS_PER_LONG;
186 tmp = *addr >> (offset % BITS_PER_LONG);
187 if (tmp)
188 return __ffs(tmp) + offset;
189 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
190 while (offset < size) {
191 tmp = *++addr;
192 if (tmp)
193 return __ffs(tmp) + offset;
194 offset += BITS_PER_LONG;
195 }
196 }
197 return size;
198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
201 * This assumes that the caller has performed appropriate preallocation, and
202 * that the caller has pinned this thread of control to the current CPU.
203 */
204static struct radix_tree_node *
205radix_tree_node_alloc(struct radix_tree_root *root)
206{
Nick Piggine2848a02008-02-04 22:29:10 -0800207 struct radix_tree_node *ret = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -0700208 gfp_t gfp_mask = root_gfp_mask(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Nick Piggine2848a02008-02-04 22:29:10 -0800210 if (!(gfp_mask & __GFP_WAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct radix_tree_preload *rtp;
212
Nick Piggine2848a02008-02-04 22:29:10 -0800213 /*
214 * Provided the caller has preloaded here, we will always
215 * succeed in getting a node here (and never reach
216 * kmem_cache_alloc)
217 */
Thomas Gleixnerf1c17312011-07-17 21:33:18 +0200218 rtp = &get_cpu_var(radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (rtp->nr) {
220 ret = rtp->nodes[rtp->nr - 1];
221 rtp->nodes[rtp->nr - 1] = NULL;
222 rtp->nr--;
223 }
Thomas Gleixnerf1c17312011-07-17 21:33:18 +0200224 put_cpu_var(radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Nick Piggine2848a02008-02-04 22:29:10 -0800226 if (ret == NULL)
Christoph Lameter488514d2008-04-28 02:12:05 -0700227 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Nick Piggine2848a02008-02-04 22:29:10 -0800228
Nick Pigginc0bc9872007-10-16 01:24:42 -0700229 BUG_ON(radix_tree_is_indirect_ptr(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return ret;
231}
232
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800233static void radix_tree_node_rcu_free(struct rcu_head *head)
234{
235 struct radix_tree_node *node =
236 container_of(head, struct radix_tree_node, rcu_head);
Dave Chinnerb6dd0862010-08-23 10:33:19 +1000237 int i;
Nick Piggin643b52b2008-06-12 15:21:52 -0700238
239 /*
240 * must only free zeroed nodes into the slab. radix_tree_shrink
241 * can leave us with a non-NULL entry in the first slot, so clear
242 * that here to make sure.
243 */
Dave Chinnerb6dd0862010-08-23 10:33:19 +1000244 for (i = 0; i < RADIX_TREE_MAX_TAGS; i++)
245 tag_clear(node, i, 0);
246
Nick Piggin643b52b2008-06-12 15:21:52 -0700247 node->slots[0] = NULL;
248 node->count = 0;
249
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800250 kmem_cache_free(radix_tree_node_cachep, node);
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static inline void
254radix_tree_node_free(struct radix_tree_node *node)
255{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800256 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Thomas Gleixnerf1c17312011-07-17 21:33:18 +0200259#ifndef CONFIG_PREEMPT_RT_FULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/*
261 * Load up this CPU's radix_tree_node buffer with sufficient objects to
262 * ensure that the addition of a single element in the tree cannot fail. On
263 * success, return zero, with preemption disabled. On error, return -ENOMEM
264 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000265 *
266 * To make use of this facility, the radix tree must be initialised without
267 * __GFP_WAIT being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
Al Virodd0fc662005-10-07 07:46:04 +0100269int radix_tree_preload(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct radix_tree_preload *rtp;
272 struct radix_tree_node *node;
273 int ret = -ENOMEM;
274
275 preempt_disable();
276 rtp = &__get_cpu_var(radix_tree_preloads);
277 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
278 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700279 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (node == NULL)
281 goto out;
282 preempt_disable();
283 rtp = &__get_cpu_var(radix_tree_preloads);
284 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
285 rtp->nodes[rtp->nr++] = node;
286 else
287 kmem_cache_free(radix_tree_node_cachep, node);
288 }
289 ret = 0;
290out:
291 return ret;
292}
David Chinnerd7f09232007-07-14 16:05:04 +1000293EXPORT_SYMBOL(radix_tree_preload);
Thomas Gleixnerf1c17312011-07-17 21:33:18 +0200294#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Nick Piggin6e954b92006-01-08 01:01:40 -0800296/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Return the maximum key which can be store into a
298 * radix tree with height HEIGHT.
299 */
300static inline unsigned long radix_tree_maxindex(unsigned int height)
301{
302 return height_to_maxindex[height];
303}
304
305/*
306 * Extend a radix tree so it can store key @index.
307 */
308static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
309{
310 struct radix_tree_node *node;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800311 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 unsigned int height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int tag;
314
315 /* Figure out what the height should be. */
316 height = root->height + 1;
317 while (index > radix_tree_maxindex(height))
318 height++;
319
320 if (root->rnode == NULL) {
321 root->height = height;
322 goto out;
323 }
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 do {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800326 unsigned int newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!(node = radix_tree_node_alloc(root)))
328 return -ENOMEM;
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /* Propagate the aggregated tag info into the new root */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800331 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700332 if (root_tag_get(root, tag))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 tag_set(node, tag, 0);
334 }
335
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800336 /* Increase the height. */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800337 newheight = root->height+1;
338 node->height = newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 node->count = 1;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800340 node->parent = NULL;
341 slot = root->rnode;
342 if (newheight > 1) {
343 slot = indirect_to_ptr(slot);
344 slot->parent = node;
345 }
346 node->slots[0] = slot;
Nick Piggin27d20fd2010-11-11 14:05:19 -0800347 node = ptr_to_indirect(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800348 rcu_assign_pointer(root->rnode, node);
349 root->height = newheight;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 } while (height > root->height);
351out:
352 return 0;
353}
354
355/**
356 * radix_tree_insert - insert into a radix tree
357 * @root: radix tree root
358 * @index: index key
359 * @item: item to insert
360 *
361 * Insert an item into the radix tree at position @index.
362 */
363int radix_tree_insert(struct radix_tree_root *root,
364 unsigned long index, void *item)
365{
Christoph Lameter201b6262005-09-06 15:16:46 -0700366 struct radix_tree_node *node = NULL, *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 unsigned int height, shift;
368 int offset;
369 int error;
370
Nick Pigginc0bc9872007-10-16 01:24:42 -0700371 BUG_ON(radix_tree_is_indirect_ptr(item));
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* Make sure the tree is high enough. */
Nick Piggin612d6c12006-06-23 02:03:22 -0700374 if (index > radix_tree_maxindex(root->height)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 error = radix_tree_extend(root, index);
376 if (error)
377 return error;
378 }
379
Nick Piggin27d20fd2010-11-11 14:05:19 -0800380 slot = indirect_to_ptr(root->rnode);
Nick Pigginc0bc9872007-10-16 01:24:42 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 height = root->height;
383 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
384
385 offset = 0; /* uninitialised var warning */
Nick Piggin612d6c12006-06-23 02:03:22 -0700386 while (height > 0) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700387 if (slot == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* Have to add a child node. */
Christoph Lameter201b6262005-09-06 15:16:46 -0700389 if (!(slot = radix_tree_node_alloc(root)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return -ENOMEM;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800391 slot->height = height;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800392 slot->parent = node;
Christoph Lameter201b6262005-09-06 15:16:46 -0700393 if (node) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800394 rcu_assign_pointer(node->slots[offset], slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 node->count++;
Christoph Lameter201b6262005-09-06 15:16:46 -0700396 } else
Nick Piggin27d20fd2010-11-11 14:05:19 -0800397 rcu_assign_pointer(root->rnode, ptr_to_indirect(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
400 /* Go a level down */
401 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Christoph Lameter201b6262005-09-06 15:16:46 -0700402 node = slot;
403 slot = node->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 shift -= RADIX_TREE_MAP_SHIFT;
405 height--;
Nick Piggin612d6c12006-06-23 02:03:22 -0700406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Christoph Lameter201b6262005-09-06 15:16:46 -0700408 if (slot != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return -EEXIST;
Christoph Lameter201b6262005-09-06 15:16:46 -0700410
Nick Piggin612d6c12006-06-23 02:03:22 -0700411 if (node) {
412 node->count++;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800413 rcu_assign_pointer(node->slots[offset], item);
Nick Piggin612d6c12006-06-23 02:03:22 -0700414 BUG_ON(tag_get(node, 0, offset));
415 BUG_ON(tag_get(node, 1, offset));
416 } else {
Nick Pigginc0bc9872007-10-16 01:24:42 -0700417 rcu_assign_pointer(root->rnode, item);
Nick Piggin612d6c12006-06-23 02:03:22 -0700418 BUG_ON(root_tag_get(root, 0));
419 BUG_ON(root_tag_get(root, 1));
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0;
423}
424EXPORT_SYMBOL(radix_tree_insert);
425
Huang Shijieb72b71c2009-06-16 15:33:42 -0700426/*
427 * is_slot == 1 : search for the slot.
428 * is_slot == 0 : search for the node.
Hans Reisera4331362005-11-07 00:59:29 -0800429 */
Huang Shijieb72b71c2009-06-16 15:33:42 -0700430static void *radix_tree_lookup_element(struct radix_tree_root *root,
431 unsigned long index, int is_slot)
Hans Reisera4331362005-11-07 00:59:29 -0800432{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800433 unsigned int height, shift;
434 struct radix_tree_node *node, **slot;
435
Paul E. McKenney2676a582010-02-22 17:04:54 -0800436 node = rcu_dereference_raw(root->rnode);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800437 if (node == NULL)
438 return NULL;
439
Nick Pigginc0bc9872007-10-16 01:24:42 -0700440 if (!radix_tree_is_indirect_ptr(node)) {
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800441 if (index > 0)
442 return NULL;
Huang Shijieb72b71c2009-06-16 15:33:42 -0700443 return is_slot ? (void *)&root->rnode : node;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800444 }
Nick Piggin27d20fd2010-11-11 14:05:19 -0800445 node = indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800446
447 height = node->height;
448 if (index > radix_tree_maxindex(height))
449 return NULL;
450
451 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
452
453 do {
454 slot = (struct radix_tree_node **)
455 (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
Paul E. McKenney2676a582010-02-22 17:04:54 -0800456 node = rcu_dereference_raw(*slot);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800457 if (node == NULL)
458 return NULL;
459
460 shift -= RADIX_TREE_MAP_SHIFT;
461 height--;
462 } while (height > 0);
463
Nick Piggin27d20fd2010-11-11 14:05:19 -0800464 return is_slot ? (void *)slot : indirect_to_ptr(node);
Huang Shijieb72b71c2009-06-16 15:33:42 -0700465}
466
467/**
468 * radix_tree_lookup_slot - lookup a slot in a radix tree
469 * @root: radix tree root
470 * @index: index key
471 *
472 * Returns: the slot corresponding to the position @index in the
473 * radix tree @root. This is useful for update-if-exists operations.
474 *
475 * This function can be called under rcu_read_lock iff the slot is not
476 * modified by radix_tree_replace_slot, otherwise it must be called
477 * exclusive from other writers. Any dereference of the slot must be done
478 * using radix_tree_deref_slot.
479 */
480void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
481{
482 return (void **)radix_tree_lookup_element(root, index, 1);
Hans Reisera4331362005-11-07 00:59:29 -0800483}
484EXPORT_SYMBOL(radix_tree_lookup_slot);
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/**
487 * radix_tree_lookup - perform lookup operation on a radix tree
488 * @root: radix tree root
489 * @index: index key
490 *
491 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800492 *
493 * This function can be called under rcu_read_lock, however the caller
494 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
495 * them safely). No RCU barriers are required to access or modify the
496 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
498void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
499{
Huang Shijieb72b71c2009-06-16 15:33:42 -0700500 return radix_tree_lookup_element(root, index, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502EXPORT_SYMBOL(radix_tree_lookup);
503
504/**
505 * radix_tree_tag_set - set a tag on a radix tree node
506 * @root: radix tree root
507 * @index: index key
508 * @tag: tag index
509 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800510 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
511 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * the root all the way down to the leaf node.
513 *
514 * Returns the address of the tagged item. Setting a tag on a not-present
515 * item is a bug.
516 */
517void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800518 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 unsigned int height, shift;
Christoph Lameter201b6262005-09-06 15:16:46 -0700521 struct radix_tree_node *slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 height = root->height;
Peter Zijlstra4c91c362006-06-23 02:03:25 -0700524 BUG_ON(index > radix_tree_maxindex(height));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Nick Piggin27d20fd2010-11-11 14:05:19 -0800526 slot = indirect_to_ptr(root->rnode);
Nick Piggin612d6c12006-06-23 02:03:22 -0700527 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 while (height > 0) {
530 int offset;
531
532 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Nick Piggind5274262006-01-08 01:01:41 -0800533 if (!tag_get(slot, tag, offset))
534 tag_set(slot, tag, offset);
Christoph Lameter201b6262005-09-06 15:16:46 -0700535 slot = slot->slots[offset];
536 BUG_ON(slot == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 shift -= RADIX_TREE_MAP_SHIFT;
538 height--;
539 }
540
Nick Piggin612d6c12006-06-23 02:03:22 -0700541 /* set the root's tag bit */
542 if (slot && !root_tag_get(root, tag))
543 root_tag_set(root, tag);
544
Christoph Lameter201b6262005-09-06 15:16:46 -0700545 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547EXPORT_SYMBOL(radix_tree_tag_set);
548
549/**
550 * radix_tree_tag_clear - clear a tag on a radix tree node
551 * @root: radix tree root
552 * @index: index key
553 * @tag: tag index
554 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800555 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
556 * corresponding to @index in the radix tree. If
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * this causes the leaf node to have no tags set then clear the tag in the
558 * next-to-leaf node, etc.
559 *
560 * Returns the address of the tagged item on success, else NULL. ie:
561 * has the same return value and semantics as radix_tree_lookup().
562 */
563void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800564 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800566 struct radix_tree_node *node = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -0700567 struct radix_tree_node *slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 unsigned int height, shift;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800569 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 height = root->height;
572 if (index > radix_tree_maxindex(height))
573 goto out;
574
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800575 shift = height * RADIX_TREE_MAP_SHIFT;
Nick Piggin27d20fd2010-11-11 14:05:19 -0800576 slot = indirect_to_ptr(root->rnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800578 while (shift) {
Christoph Lameter201b6262005-09-06 15:16:46 -0700579 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 goto out;
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 shift -= RADIX_TREE_MAP_SHIFT;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800583 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
584 node = slot;
585 slot = slot->slots[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
Nick Piggin612d6c12006-06-23 02:03:22 -0700588 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto out;
590
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800591 while (node) {
592 if (!tag_get(node, tag, offset))
Nick Piggind5274262006-01-08 01:01:41 -0800593 goto out;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800594 tag_clear(node, tag, offset);
595 if (any_tag_set(node, tag))
Nick Piggin6e954b92006-01-08 01:01:40 -0800596 goto out;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800597
598 index >>= RADIX_TREE_MAP_SHIFT;
599 offset = index & RADIX_TREE_MAP_MASK;
600 node = node->parent;
Nick Piggin612d6c12006-06-23 02:03:22 -0700601 }
602
603 /* clear the root's tag bit */
604 if (root_tag_get(root, tag))
605 root_tag_clear(root, tag);
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607out:
Nick Piggin612d6c12006-06-23 02:03:22 -0700608 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610EXPORT_SYMBOL(radix_tree_tag_clear);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700613 * radix_tree_tag_get - get a tag on a radix tree node
614 * @root: radix tree root
615 * @index: index key
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800616 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700618 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 *
Nick Piggin612d6c12006-06-23 02:03:22 -0700620 * 0: tag not present or not set
621 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +0100622 *
623 * Note that the return value of this function may not be relied on, even if
624 * the RCU lock is held, unless tag modification and node deletion are excluded
625 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
627int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800628 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 unsigned int height, shift;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800631 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Nick Piggin612d6c12006-06-23 02:03:22 -0700633 /* check the root's tag bit */
634 if (!root_tag_get(root, tag))
635 return 0;
636
Paul E. McKenney2676a582010-02-22 17:04:54 -0800637 node = rcu_dereference_raw(root->rnode);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800638 if (node == NULL)
639 return 0;
640
Nick Pigginc0bc9872007-10-16 01:24:42 -0700641 if (!radix_tree_is_indirect_ptr(node))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800642 return (index == 0);
Nick Piggin27d20fd2010-11-11 14:05:19 -0800643 node = indirect_to_ptr(node);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800644
645 height = node->height;
646 if (index > radix_tree_maxindex(height))
647 return 0;
Nick Piggin612d6c12006-06-23 02:03:22 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 for ( ; ; ) {
652 int offset;
653
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800654 if (node == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return 0;
656
657 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800658 if (!tag_get(node, tag, offset))
Hugh Dickins3fa36ac2011-10-31 17:07:02 -0700659 return 0;
David Howellsce826532010-04-06 22:36:20 +0100660 if (height == 1)
Hugh Dickins3fa36ac2011-10-31 17:07:02 -0700661 return 1;
Paul E. McKenney2676a582010-02-22 17:04:54 -0800662 node = rcu_dereference_raw(node->slots[offset]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 shift -= RADIX_TREE_MAP_SHIFT;
664 height--;
665 }
666}
667EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700669/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700670 * radix_tree_next_chunk - find next chunk of slots for iteration
671 *
672 * @root: radix tree root
673 * @iter: iterator state
674 * @flags: RADIX_TREE_ITER_* flags and tag index
675 * Returns: pointer to chunk first slot, or NULL if iteration is over
676 */
677void **radix_tree_next_chunk(struct radix_tree_root *root,
678 struct radix_tree_iter *iter, unsigned flags)
679{
680 unsigned shift, tag = flags & RADIX_TREE_ITER_TAG_MASK;
681 struct radix_tree_node *rnode, *node;
682 unsigned long index, offset;
683
684 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
685 return NULL;
686
687 /*
688 * Catch next_index overflow after ~0UL. iter->index never overflows
689 * during iterating; it can be zero only at the beginning.
690 * And we cannot overflow iter->next_index in a single step,
691 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +0400692 *
693 * This condition also used by radix_tree_next_slot() to stop
694 * contiguous iterating, and forbid swithing to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700695 */
696 index = iter->next_index;
697 if (!index && iter->index)
698 return NULL;
699
700 rnode = rcu_dereference_raw(root->rnode);
701 if (radix_tree_is_indirect_ptr(rnode)) {
702 rnode = indirect_to_ptr(rnode);
703 } else if (rnode && !index) {
704 /* Single-slot tree */
705 iter->index = 0;
706 iter->next_index = 1;
707 iter->tags = 1;
708 return (void **)&root->rnode;
709 } else
710 return NULL;
711
712restart:
713 shift = (rnode->height - 1) * RADIX_TREE_MAP_SHIFT;
714 offset = index >> shift;
715
716 /* Index outside of the tree */
717 if (offset >= RADIX_TREE_MAP_SIZE)
718 return NULL;
719
720 node = rnode;
721 while (1) {
722 if ((flags & RADIX_TREE_ITER_TAGGED) ?
723 !test_bit(offset, node->tags[tag]) :
724 !node->slots[offset]) {
725 /* Hole detected */
726 if (flags & RADIX_TREE_ITER_CONTIG)
727 return NULL;
728
729 if (flags & RADIX_TREE_ITER_TAGGED)
730 offset = radix_tree_find_next_bit(
731 node->tags[tag],
732 RADIX_TREE_MAP_SIZE,
733 offset + 1);
734 else
735 while (++offset < RADIX_TREE_MAP_SIZE) {
736 if (node->slots[offset])
737 break;
738 }
739 index &= ~((RADIX_TREE_MAP_SIZE << shift) - 1);
740 index += offset << shift;
741 /* Overflow after ~0UL */
742 if (!index)
743 return NULL;
744 if (offset == RADIX_TREE_MAP_SIZE)
745 goto restart;
746 }
747
748 /* This is leaf-node */
749 if (!shift)
750 break;
751
752 node = rcu_dereference_raw(node->slots[offset]);
753 if (node == NULL)
754 goto restart;
755 shift -= RADIX_TREE_MAP_SHIFT;
756 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
757 }
758
759 /* Update the iterator state */
760 iter->index = index;
761 iter->next_index = (index | RADIX_TREE_MAP_MASK) + 1;
762
763 /* Construct iter->tags bit-mask from node->tags[tag] array */
764 if (flags & RADIX_TREE_ITER_TAGGED) {
765 unsigned tag_long, tag_bit;
766
767 tag_long = offset / BITS_PER_LONG;
768 tag_bit = offset % BITS_PER_LONG;
769 iter->tags = node->tags[tag][tag_long] >> tag_bit;
770 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
771 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
772 /* Pick tags from next element */
773 if (tag_bit)
774 iter->tags |= node->tags[tag][tag_long + 1] <<
775 (BITS_PER_LONG - tag_bit);
776 /* Clip chunk size, here only BITS_PER_LONG tags */
777 iter->next_index = index + BITS_PER_LONG;
778 }
779 }
780
781 return node->slots + offset;
782}
783EXPORT_SYMBOL(radix_tree_next_chunk);
784
785/**
Jan Karaebf8aa42010-08-09 17:19:11 -0700786 * radix_tree_range_tag_if_tagged - for each item in given range set given
787 * tag if item has another tag set
788 * @root: radix tree root
789 * @first_indexp: pointer to a starting index of a range to scan
790 * @last_index: last index of a range to scan
791 * @nr_to_tag: maximum number items to tag
792 * @iftag: tag index to test
793 * @settag: tag index to set if tested tag is set
794 *
795 * This function scans range of radix tree from first_index to last_index
796 * (inclusive). For each item in the range if iftag is set, the function sets
797 * also settag. The function stops either after tagging nr_to_tag items or
798 * after reaching last_index.
799 *
Dave Chinner144dcfc2010-08-23 10:33:53 +1000800 * The tags must be set from the leaf level only and propagated back up the
801 * path to the root. We must do this so that we resolve the full path before
802 * setting any tags on intermediate nodes. If we set tags as we descend, then
803 * we can get to the leaf node and find that the index that has the iftag
804 * set is outside the range we are scanning. This reults in dangling tags and
805 * can lead to problems with later tag operations (e.g. livelocks on lookups).
806 *
Jan Karaebf8aa42010-08-09 17:19:11 -0700807 * The function returns number of leaves where the tag was set and sets
808 * *first_indexp to the first unscanned index.
Jan Karad5ed3a42010-08-19 14:13:33 -0700809 * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must
810 * be prepared to handle that.
Jan Karaebf8aa42010-08-09 17:19:11 -0700811 */
812unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
813 unsigned long *first_indexp, unsigned long last_index,
814 unsigned long nr_to_tag,
815 unsigned int iftag, unsigned int settag)
816{
Dave Chinner144dcfc2010-08-23 10:33:53 +1000817 unsigned int height = root->height;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800818 struct radix_tree_node *node = NULL;
Dave Chinner144dcfc2010-08-23 10:33:53 +1000819 struct radix_tree_node *slot;
820 unsigned int shift;
821 unsigned long tagged = 0;
822 unsigned long index = *first_indexp;
Jan Karaebf8aa42010-08-09 17:19:11 -0700823
824 last_index = min(last_index, radix_tree_maxindex(height));
825 if (index > last_index)
826 return 0;
827 if (!nr_to_tag)
828 return 0;
829 if (!root_tag_get(root, iftag)) {
830 *first_indexp = last_index + 1;
831 return 0;
832 }
833 if (height == 0) {
834 *first_indexp = last_index + 1;
835 root_tag_set(root, settag);
836 return 1;
837 }
838
839 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
Nick Piggin27d20fd2010-11-11 14:05:19 -0800840 slot = indirect_to_ptr(root->rnode);
Jan Karaebf8aa42010-08-09 17:19:11 -0700841
842 for (;;) {
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800843 unsigned long upindex;
Jan Karaebf8aa42010-08-09 17:19:11 -0700844 int offset;
845
846 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
847 if (!slot->slots[offset])
848 goto next;
849 if (!tag_get(slot, iftag, offset))
850 goto next;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800851 if (shift) {
Dave Chinner144dcfc2010-08-23 10:33:53 +1000852 /* Go down one level */
Dave Chinner144dcfc2010-08-23 10:33:53 +1000853 shift -= RADIX_TREE_MAP_SHIFT;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800854 node = slot;
Dave Chinner144dcfc2010-08-23 10:33:53 +1000855 slot = slot->slots[offset];
856 continue;
Jan Karaebf8aa42010-08-09 17:19:11 -0700857 }
Dave Chinner144dcfc2010-08-23 10:33:53 +1000858
859 /* tag the leaf */
860 tagged++;
861 tag_set(slot, settag, offset);
862
863 /* walk back up the path tagging interior nodes */
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800864 upindex = index;
865 while (node) {
866 upindex >>= RADIX_TREE_MAP_SHIFT;
867 offset = upindex & RADIX_TREE_MAP_MASK;
868
Dave Chinner144dcfc2010-08-23 10:33:53 +1000869 /* stop if we find a node with the tag already set */
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800870 if (tag_get(node, settag, offset))
Dave Chinner144dcfc2010-08-23 10:33:53 +1000871 break;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800872 tag_set(node, settag, offset);
873 node = node->parent;
Dave Chinner144dcfc2010-08-23 10:33:53 +1000874 }
875
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800876 /*
877 * Small optimization: now clear that node pointer.
878 * Since all of this slot's ancestors now have the tag set
879 * from setting it above, we have no further need to walk
880 * back up the tree setting tags, until we update slot to
881 * point to another radix_tree_node.
882 */
883 node = NULL;
884
Jan Karaebf8aa42010-08-09 17:19:11 -0700885next:
886 /* Go to next item at level determined by 'shift' */
887 index = ((index >> shift) + 1) << shift;
Jan Karad5ed3a42010-08-19 14:13:33 -0700888 /* Overflow can happen when last_index is ~0UL... */
889 if (index > last_index || !index)
Jan Karaebf8aa42010-08-09 17:19:11 -0700890 break;
891 if (tagged >= nr_to_tag)
892 break;
893 while (((index >> shift) & RADIX_TREE_MAP_MASK) == 0) {
894 /*
895 * We've fully scanned this node. Go up. Because
896 * last_index is guaranteed to be in the tree, what
897 * we do below cannot wander astray.
898 */
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800899 slot = slot->parent;
Jan Karaebf8aa42010-08-09 17:19:11 -0700900 shift += RADIX_TREE_MAP_SHIFT;
901 }
902 }
903 /*
Toshiyuki Okajimaac15ee62011-01-25 15:07:32 -0800904 * We need not to tag the root tag if there is no tag which is set with
905 * settag within the range from *first_indexp to last_index.
Jan Karaebf8aa42010-08-09 17:19:11 -0700906 */
Toshiyuki Okajimaac15ee62011-01-25 15:07:32 -0800907 if (tagged > 0)
908 root_tag_set(root, settag);
Jan Karaebf8aa42010-08-09 17:19:11 -0700909 *first_indexp = index;
910
911 return tagged;
912}
913EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
914
915
916/**
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700917 * radix_tree_next_hole - find the next hole (not-present entry)
918 * @root: tree root
919 * @index: index key
920 * @max_scan: maximum range to search
921 *
922 * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest
923 * indexed hole.
924 *
925 * Returns: the index of the hole if found, otherwise returns an index
926 * outside of the set specified (in which case 'return - index >= max_scan'
Wu Fengguang8e6bdb72008-11-27 11:42:22 +0100927 * will be true). In rare cases of index wrap-around, 0 will be returned.
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700928 *
929 * radix_tree_next_hole may be called under rcu_read_lock. However, like
Wu Fengguang8e6bdb72008-11-27 11:42:22 +0100930 * radix_tree_gang_lookup, this will not atomically search a snapshot of
931 * the tree at a single point in time. For example, if a hole is created
932 * at index 5, then subsequently a hole is created at index 10,
933 * radix_tree_next_hole covering both indexes may return 10 if called
934 * under rcu_read_lock.
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700935 */
936unsigned long radix_tree_next_hole(struct radix_tree_root *root,
937 unsigned long index, unsigned long max_scan)
938{
939 unsigned long i;
940
941 for (i = 0; i < max_scan; i++) {
942 if (!radix_tree_lookup(root, index))
943 break;
944 index++;
945 if (index == 0)
946 break;
947 }
948
949 return index;
950}
951EXPORT_SYMBOL(radix_tree_next_hole);
952
Wu Fengguangdc566122009-06-16 15:31:32 -0700953/**
954 * radix_tree_prev_hole - find the prev hole (not-present entry)
955 * @root: tree root
956 * @index: index key
957 * @max_scan: maximum range to search
958 *
959 * Search backwards in the range [max(index-max_scan+1, 0), index]
960 * for the first hole.
961 *
962 * Returns: the index of the hole if found, otherwise returns an index
963 * outside of the set specified (in which case 'index - return >= max_scan'
Cesar Eduardo Barrosedcd1d82010-05-26 14:44:27 -0700964 * will be true). In rare cases of wrap-around, ULONG_MAX will be returned.
Wu Fengguangdc566122009-06-16 15:31:32 -0700965 *
966 * radix_tree_next_hole may be called under rcu_read_lock. However, like
967 * radix_tree_gang_lookup, this will not atomically search a snapshot of
968 * the tree at a single point in time. For example, if a hole is created
969 * at index 10, then subsequently a hole is created at index 5,
970 * radix_tree_prev_hole covering both indexes may return 5 if called under
971 * rcu_read_lock.
972 */
973unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
974 unsigned long index, unsigned long max_scan)
975{
976 unsigned long i;
977
978 for (i = 0; i < max_scan; i++) {
979 if (!radix_tree_lookup(root, index))
980 break;
981 index--;
Cesar Eduardo Barrosedcd1d82010-05-26 14:44:27 -0700982 if (index == ULONG_MAX)
Wu Fengguangdc566122009-06-16 15:31:32 -0700983 break;
984 }
985
986 return index;
987}
988EXPORT_SYMBOL(radix_tree_prev_hole);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990/**
991 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
992 * @root: radix tree root
993 * @results: where the results of the lookup are placed
994 * @first_index: start the lookup from this key
995 * @max_items: place up to this many items at *results
996 *
997 * Performs an index-ascending scan of the tree for present items. Places
998 * them at *@results and returns the number of items which were placed at
999 * *@results.
1000 *
1001 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001002 *
1003 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1004 * rcu_read_lock. In this case, rather than the returned results being
1005 * an atomic snapshot of the tree at a single point in time, the semantics
1006 * of an RCU protected gang lookup are as though multiple radix_tree_lookups
1007 * have been issued in individual locks, and results stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
1009unsigned int
1010radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
1011 unsigned long first_index, unsigned int max_items)
1012{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001013 struct radix_tree_iter iter;
1014 void **slot;
1015 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001017 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001018 return 0;
1019
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001020 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1021 results[ret] = indirect_to_ptr(rcu_dereference_raw(*slot));
1022 if (!results[ret])
1023 continue;
1024 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return ret;
1029}
1030EXPORT_SYMBOL(radix_tree_gang_lookup);
1031
Nick Piggin47feff22008-07-25 19:45:29 -07001032/**
1033 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1034 * @root: radix tree root
1035 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001036 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001037 * @first_index: start the lookup from this key
1038 * @max_items: place up to this many items at *results
1039 *
1040 * Performs an index-ascending scan of the tree for present items. Places
1041 * their slots at *@results and returns the number of items which were
1042 * placed at *@results.
1043 *
1044 * The implementation is naive.
1045 *
1046 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1047 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1048 * protection, radix_tree_deref_slot may fail requiring a retry.
1049 */
1050unsigned int
Hugh Dickins63286502011-08-03 16:21:18 -07001051radix_tree_gang_lookup_slot(struct radix_tree_root *root,
1052 void ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001053 unsigned long first_index, unsigned int max_items)
1054{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001055 struct radix_tree_iter iter;
1056 void **slot;
1057 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001058
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001059 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001060 return 0;
1061
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001062 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1063 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001064 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001065 indices[ret] = iter.index;
1066 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001067 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001068 }
1069
1070 return ret;
1071}
1072EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074/**
1075 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1076 * based on a tag
1077 * @root: radix tree root
1078 * @results: where the results of the lookup are placed
1079 * @first_index: start the lookup from this key
1080 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001081 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 *
1083 * Performs an index-ascending scan of the tree for present items which
1084 * have the tag indexed by @tag set. Places the items at *@results and
1085 * returns the number of items which were placed at *@results.
1086 */
1087unsigned int
1088radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001089 unsigned long first_index, unsigned int max_items,
1090 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001092 struct radix_tree_iter iter;
1093 void **slot;
1094 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001096 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001097 return 0;
1098
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001099 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1100 results[ret] = indirect_to_ptr(rcu_dereference_raw(*slot));
1101 if (!results[ret])
1102 continue;
1103 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return ret;
1108}
1109EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1110
1111/**
Nick Piggin47feff22008-07-25 19:45:29 -07001112 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1113 * radix tree based on a tag
1114 * @root: radix tree root
1115 * @results: where the results of the lookup are placed
1116 * @first_index: start the lookup from this key
1117 * @max_items: place up to this many items at *results
1118 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1119 *
1120 * Performs an index-ascending scan of the tree for present items which
1121 * have the tag indexed by @tag set. Places the slots at *@results and
1122 * returns the number of slots which were placed at *@results.
1123 */
1124unsigned int
1125radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
1126 unsigned long first_index, unsigned int max_items,
1127 unsigned int tag)
1128{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001129 struct radix_tree_iter iter;
1130 void **slot;
1131 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001132
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001133 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001134 return 0;
1135
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001136 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1137 results[ret] = slot;
1138 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001139 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001140 }
1141
1142 return ret;
1143}
1144EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1145
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001146#if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
1147#include <linux/sched.h> /* for cond_resched() */
1148
1149/*
1150 * This linear search is at present only useful to shmem_unuse_inode().
1151 */
1152static unsigned long __locate(struct radix_tree_node *slot, void *item,
1153 unsigned long index, unsigned long *found_index)
1154{
1155 unsigned int shift, height;
1156 unsigned long i;
1157
1158 height = slot->height;
1159 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
1160
1161 for ( ; height > 1; height--) {
1162 i = (index >> shift) & RADIX_TREE_MAP_MASK;
1163 for (;;) {
1164 if (slot->slots[i] != NULL)
1165 break;
1166 index &= ~((1UL << shift) - 1);
1167 index += 1UL << shift;
1168 if (index == 0)
1169 goto out; /* 32-bit wraparound */
1170 i++;
1171 if (i == RADIX_TREE_MAP_SIZE)
1172 goto out;
1173 }
1174
1175 shift -= RADIX_TREE_MAP_SHIFT;
1176 slot = rcu_dereference_raw(slot->slots[i]);
1177 if (slot == NULL)
1178 goto out;
1179 }
1180
1181 /* Bottom level: check items */
1182 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
1183 if (slot->slots[i] == item) {
1184 *found_index = index + i;
1185 index = 0;
1186 goto out;
1187 }
1188 }
1189 index += RADIX_TREE_MAP_SIZE;
1190out:
1191 return index;
1192}
1193
1194/**
1195 * radix_tree_locate_item - search through radix tree for item
1196 * @root: radix tree root
1197 * @item: item to be found
1198 *
1199 * Returns index where item was found, or -1 if not found.
1200 * Caller must hold no lock (since this time-consuming function needs
1201 * to be preemptible), and must check afterwards if item is still there.
1202 */
1203unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1204{
1205 struct radix_tree_node *node;
1206 unsigned long max_index;
1207 unsigned long cur_index = 0;
1208 unsigned long found_index = -1;
1209
1210 do {
1211 rcu_read_lock();
1212 node = rcu_dereference_raw(root->rnode);
1213 if (!radix_tree_is_indirect_ptr(node)) {
1214 rcu_read_unlock();
1215 if (node == item)
1216 found_index = 0;
1217 break;
1218 }
1219
1220 node = indirect_to_ptr(node);
1221 max_index = radix_tree_maxindex(node->height);
1222 if (cur_index > max_index)
1223 break;
1224
1225 cur_index = __locate(node, item, cur_index, &found_index);
1226 rcu_read_unlock();
1227 cond_resched();
1228 } while (cur_index != 0 && cur_index <= max_index);
1229
1230 return found_index;
1231}
1232#else
1233unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1234{
1235 return -1;
1236}
1237#endif /* CONFIG_SHMEM && CONFIG_SWAP */
Nick Piggin47feff22008-07-25 19:45:29 -07001238
1239/**
Nick Piggina5f51c92006-01-08 01:01:41 -08001240 * radix_tree_shrink - shrink height of a radix tree to minimal
1241 * @root radix tree root
1242 */
1243static inline void radix_tree_shrink(struct radix_tree_root *root)
1244{
1245 /* try to shrink tree height */
Nick Pigginc0bc9872007-10-16 01:24:42 -07001246 while (root->height > 0) {
Nick Piggina5f51c92006-01-08 01:01:41 -08001247 struct radix_tree_node *to_free = root->rnode;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001248 struct radix_tree_node *slot;
Nick Piggina5f51c92006-01-08 01:01:41 -08001249
Nick Pigginc0bc9872007-10-16 01:24:42 -07001250 BUG_ON(!radix_tree_is_indirect_ptr(to_free));
Nick Piggin27d20fd2010-11-11 14:05:19 -08001251 to_free = indirect_to_ptr(to_free);
Nick Pigginc0bc9872007-10-16 01:24:42 -07001252
1253 /*
1254 * The candidate node has more than one child, or its child
1255 * is not at the leftmost slot, we cannot shrink.
1256 */
1257 if (to_free->count != 1)
1258 break;
1259 if (!to_free->slots[0])
1260 break;
1261
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001262 /*
1263 * We don't need rcu_assign_pointer(), since we are simply
Nick Piggin27d20fd2010-11-11 14:05:19 -08001264 * moving the node from one part of the tree to another: if it
1265 * was safe to dereference the old pointer to it
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001266 * (to_free->slots[0]), it will be safe to dereference the new
Nick Piggin27d20fd2010-11-11 14:05:19 -08001267 * one (root->rnode) as far as dependent read barriers go.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001268 */
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001269 slot = to_free->slots[0];
1270 if (root->height > 1) {
1271 slot->parent = NULL;
1272 slot = ptr_to_indirect(slot);
1273 }
1274 root->rnode = slot;
Nick Piggina5f51c92006-01-08 01:01:41 -08001275 root->height--;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001276
1277 /*
1278 * We have a dilemma here. The node's slot[0] must not be
1279 * NULLed in case there are concurrent lookups expecting to
1280 * find the item. However if this was a bottom-level node,
1281 * then it may be subject to the slot pointer being visible
1282 * to callers dereferencing it. If item corresponding to
1283 * slot[0] is subsequently deleted, these callers would expect
1284 * their slot to become empty sooner or later.
1285 *
1286 * For example, lockless pagecache will look up a slot, deref
1287 * the page pointer, and if the page is 0 refcount it means it
1288 * was concurrently deleted from pagecache so try the deref
1289 * again. Fortunately there is already a requirement for logic
1290 * to retry the entire slot lookup -- the indirect pointer
1291 * problem (replacing direct root node with an indirect pointer
1292 * also results in a stale slot). So tag the slot as indirect
1293 * to force callers to retry.
1294 */
1295 if (root->height == 0)
1296 *((unsigned long *)&to_free->slots[0]) |=
1297 RADIX_TREE_INDIRECT_PTR;
1298
Nick Piggina5f51c92006-01-08 01:01:41 -08001299 radix_tree_node_free(to_free);
1300 }
1301}
1302
1303/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 * radix_tree_delete - delete an item from a radix tree
1305 * @root: radix tree root
1306 * @index: index key
1307 *
1308 * Remove the item at @index from the radix tree rooted at @root.
1309 *
1310 * Returns the address of the deleted item, or NULL if it was not present.
1311 */
1312void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1313{
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001314 struct radix_tree_node *node = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -07001315 struct radix_tree_node *slot = NULL;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001316 struct radix_tree_node *to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 unsigned int height, shift;
Nick Piggind5274262006-01-08 01:01:41 -08001318 int tag;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001319 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 height = root->height;
1322 if (index > radix_tree_maxindex(height))
1323 goto out;
1324
Nick Piggin612d6c12006-06-23 02:03:22 -07001325 slot = root->rnode;
Nick Pigginc0bc9872007-10-16 01:24:42 -07001326 if (height == 0) {
Nick Piggin612d6c12006-06-23 02:03:22 -07001327 root_tag_clear_all(root);
1328 root->rnode = NULL;
1329 goto out;
1330 }
Nick Piggin27d20fd2010-11-11 14:05:19 -08001331 slot = indirect_to_ptr(slot);
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001332 shift = height * RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Nick Piggin612d6c12006-06-23 02:03:22 -07001334 do {
Christoph Lameter201b6262005-09-06 15:16:46 -07001335 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 goto out;
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 shift -= RADIX_TREE_MAP_SHIFT;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001339 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
1340 node = slot;
1341 slot = slot->slots[offset];
1342 } while (shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Nick Piggin612d6c12006-06-23 02:03:22 -07001344 if (slot == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 goto out;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /*
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001348 * Clear all tags associated with the item to be deleted.
1349 * This way of doing it would be inefficient, but seldom is any set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001351 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001352 if (tag_get(node, tag, offset))
Nick Piggin612d6c12006-06-23 02:03:22 -07001353 radix_tree_tag_clear(root, index, tag);
Nick Piggind5274262006-01-08 01:01:41 -08001354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001356 to_free = NULL;
Christoph Lameter201b6262005-09-06 15:16:46 -07001357 /* Now free the nodes we do not need anymore */
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001358 while (node) {
1359 node->slots[offset] = NULL;
1360 node->count--;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001361 /*
1362 * Queue the node for deferred freeing after the
1363 * last reference to it disappears (set NULL, above).
1364 */
1365 if (to_free)
1366 radix_tree_node_free(to_free);
Nick Piggina5f51c92006-01-08 01:01:41 -08001367
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001368 if (node->count) {
1369 if (node == indirect_to_ptr(root->rnode))
Nick Piggina5f51c92006-01-08 01:01:41 -08001370 radix_tree_shrink(root);
Christoph Lameter201b6262005-09-06 15:16:46 -07001371 goto out;
Nick Piggina5f51c92006-01-08 01:01:41 -08001372 }
Christoph Lameter201b6262005-09-06 15:16:46 -07001373
1374 /* Node with zero slots in use so free it */
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001375 to_free = node;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001376
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001377 index >>= RADIX_TREE_MAP_SHIFT;
1378 offset = index & RADIX_TREE_MAP_MASK;
1379 node = node->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001381
Nick Piggin612d6c12006-06-23 02:03:22 -07001382 root_tag_clear_all(root);
Christoph Lameter201b6262005-09-06 15:16:46 -07001383 root->height = 0;
Nick Piggin612d6c12006-06-23 02:03:22 -07001384 root->rnode = NULL;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001385 if (to_free)
1386 radix_tree_node_free(to_free);
Nick Piggin612d6c12006-06-23 02:03:22 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388out:
Nick Piggin612d6c12006-06-23 02:03:22 -07001389 return slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391EXPORT_SYMBOL(radix_tree_delete);
1392
1393/**
1394 * radix_tree_tagged - test whether any items in the tree are tagged
1395 * @root: radix tree root
1396 * @tag: tag to test
1397 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001398int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Nick Piggin612d6c12006-06-23 02:03:22 -07001400 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402EXPORT_SYMBOL(radix_tree_tagged);
1403
1404static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001405radix_tree_node_ctor(void *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 memset(node, 0, sizeof(struct radix_tree_node));
1408}
1409
1410static __init unsigned long __maxindex(unsigned int height)
1411{
Peter Lund430d2752007-10-16 23:29:35 -07001412 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1413 int shift = RADIX_TREE_INDEX_BITS - width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Peter Lund430d2752007-10-16 23:29:35 -07001415 if (shift < 0)
1416 return ~0UL;
1417 if (shift >= BITS_PER_LONG)
1418 return 0UL;
1419 return ~0UL >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
1422static __init void radix_tree_init_maxindex(void)
1423{
1424 unsigned int i;
1425
1426 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1427 height_to_maxindex[i] = __maxindex(i);
1428}
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430static int radix_tree_callback(struct notifier_block *nfb,
1431 unsigned long action,
1432 void *hcpu)
1433{
1434 int cpu = (long)hcpu;
1435 struct radix_tree_preload *rtp;
1436
1437 /* Free per-cpu pool of perloaded nodes */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001438 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 rtp = &per_cpu(radix_tree_preloads, cpu);
1440 while (rtp->nr) {
1441 kmem_cache_free(radix_tree_node_cachep,
1442 rtp->nodes[rtp->nr-1]);
1443 rtp->nodes[rtp->nr-1] = NULL;
1444 rtp->nr--;
1445 }
1446 }
1447 return NOTIFY_OK;
1448}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450void __init radix_tree_init(void)
1451{
1452 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1453 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07001454 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1455 radix_tree_node_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 radix_tree_init_maxindex();
1457 hotcpu_notifier(radix_tree_callback, 0);
1458}