Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001 Momchil Velikov |
| 3 | * Portions Copyright (C) 2001 Christoph Hellwig |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 4 | * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com> |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 5 | * Copyright (C) 2006 Nick Piggin |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/radix-tree.h> |
| 27 | #include <linux/percpu.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/notifier.h> |
| 30 | #include <linux/cpu.h> |
| 31 | #include <linux/gfp.h> |
| 32 | #include <linux/string.h> |
| 33 | #include <linux/bitops.h> |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 34 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | |
| 37 | #ifdef __KERNEL__ |
Nick Piggin | cfd9b7d | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 38 | #define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #else |
| 40 | #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */ |
| 41 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 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 | |
| 49 | struct radix_tree_node { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 50 | unsigned int height; /* Height from the bottom */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | unsigned int count; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 52 | struct rcu_head rcu_head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | void *slots[RADIX_TREE_MAP_SIZE]; |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 54 | unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct radix_tree_path { |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 58 | struct radix_tree_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | int offset; |
| 60 | }; |
| 61 | |
| 62 | #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long)) |
| 63 | #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2) |
| 64 | |
Christoph Lameter | 6c03652 | 2005-07-07 17:56:59 -0700 | [diff] [blame] | 65 | static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * Radix tree node cache. |
| 69 | */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 70 | static struct kmem_cache *radix_tree_node_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * Per-cpu pool of preloaded nodes |
| 74 | */ |
| 75 | struct radix_tree_preload { |
| 76 | int nr; |
| 77 | struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH]; |
| 78 | }; |
| 79 | DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, }; |
| 80 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 81 | static inline gfp_t root_gfp_mask(struct radix_tree_root *root) |
| 82 | { |
| 83 | return root->gfp_mask & __GFP_BITS_MASK; |
| 84 | } |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | /* |
| 87 | * This assumes that the caller has performed appropriate preallocation, and |
| 88 | * that the caller has pinned this thread of control to the current CPU. |
| 89 | */ |
| 90 | static struct radix_tree_node * |
| 91 | radix_tree_node_alloc(struct radix_tree_root *root) |
| 92 | { |
| 93 | struct radix_tree_node *ret; |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 94 | gfp_t gfp_mask = root_gfp_mask(root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 96 | ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask); |
| 97 | if (ret == NULL && !(gfp_mask & __GFP_WAIT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | struct radix_tree_preload *rtp; |
| 99 | |
| 100 | rtp = &__get_cpu_var(radix_tree_preloads); |
| 101 | if (rtp->nr) { |
| 102 | ret = rtp->nodes[rtp->nr - 1]; |
| 103 | rtp->nodes[rtp->nr - 1] = NULL; |
| 104 | rtp->nr--; |
| 105 | } |
| 106 | } |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 107 | BUG_ON(radix_tree_is_direct_ptr(ret)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return ret; |
| 109 | } |
| 110 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 111 | static void radix_tree_node_rcu_free(struct rcu_head *head) |
| 112 | { |
| 113 | struct radix_tree_node *node = |
| 114 | container_of(head, struct radix_tree_node, rcu_head); |
| 115 | kmem_cache_free(radix_tree_node_cachep, node); |
| 116 | } |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | static inline void |
| 119 | radix_tree_node_free(struct radix_tree_node *node) |
| 120 | { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 121 | call_rcu(&node->rcu_head, radix_tree_node_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Load up this CPU's radix_tree_node buffer with sufficient objects to |
| 126 | * ensure that the addition of a single element in the tree cannot fail. On |
| 127 | * success, return zero, with preemption disabled. On error, return -ENOMEM |
| 128 | * with preemption not disabled. |
| 129 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 130 | int radix_tree_preload(gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | struct radix_tree_preload *rtp; |
| 133 | struct radix_tree_node *node; |
| 134 | int ret = -ENOMEM; |
| 135 | |
| 136 | preempt_disable(); |
| 137 | rtp = &__get_cpu_var(radix_tree_preloads); |
| 138 | while (rtp->nr < ARRAY_SIZE(rtp->nodes)) { |
| 139 | preempt_enable(); |
| 140 | node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask); |
| 141 | if (node == NULL) |
| 142 | goto out; |
| 143 | preempt_disable(); |
| 144 | rtp = &__get_cpu_var(radix_tree_preloads); |
| 145 | if (rtp->nr < ARRAY_SIZE(rtp->nodes)) |
| 146 | rtp->nodes[rtp->nr++] = node; |
| 147 | else |
| 148 | kmem_cache_free(radix_tree_node_cachep, node); |
| 149 | } |
| 150 | ret = 0; |
| 151 | out: |
| 152 | return ret; |
| 153 | } |
David Chinner | d7f0923 | 2007-07-14 16:05:04 +1000 | [diff] [blame] | 154 | EXPORT_SYMBOL(radix_tree_preload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 156 | static inline void tag_set(struct radix_tree_node *node, unsigned int tag, |
| 157 | int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 159 | __set_bit(offset, node->tags[tag]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 162 | static inline void tag_clear(struct radix_tree_node *node, unsigned int tag, |
| 163 | int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 165 | __clear_bit(offset, node->tags[tag]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 168 | static inline int tag_get(struct radix_tree_node *node, unsigned int tag, |
| 169 | int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 171 | return test_bit(offset, node->tags[tag]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 174 | static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag) |
| 175 | { |
Al Viro | 20241ad | 2006-10-10 22:47:57 +0100 | [diff] [blame] | 176 | root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT)); |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | |
| 180 | static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag) |
| 181 | { |
Al Viro | 20241ad | 2006-10-10 22:47:57 +0100 | [diff] [blame] | 182 | root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT)); |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static inline void root_tag_clear_all(struct radix_tree_root *root) |
| 186 | { |
| 187 | root->gfp_mask &= __GFP_BITS_MASK; |
| 188 | } |
| 189 | |
| 190 | static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag) |
| 191 | { |
Al Viro | 20241ad | 2006-10-10 22:47:57 +0100 | [diff] [blame] | 192 | return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT)); |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /* |
Nick Piggin | 6e954b9 | 2006-01-08 01:01:40 -0800 | [diff] [blame] | 196 | * Returns 1 if any slot in the node has this tag set. |
| 197 | * Otherwise returns 0. |
| 198 | */ |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 199 | static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag) |
Nick Piggin | 6e954b9 | 2006-01-08 01:01:40 -0800 | [diff] [blame] | 200 | { |
| 201 | int idx; |
| 202 | for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) { |
| 203 | if (node->tags[tag][idx]) |
| 204 | return 1; |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | * Return the maximum key which can be store into a |
| 211 | * radix tree with height HEIGHT. |
| 212 | */ |
| 213 | static inline unsigned long radix_tree_maxindex(unsigned int height) |
| 214 | { |
| 215 | return height_to_maxindex[height]; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Extend a radix tree so it can store key @index. |
| 220 | */ |
| 221 | static int radix_tree_extend(struct radix_tree_root *root, unsigned long index) |
| 222 | { |
| 223 | struct radix_tree_node *node; |
| 224 | unsigned int height; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | int tag; |
| 226 | |
| 227 | /* Figure out what the height should be. */ |
| 228 | height = root->height + 1; |
| 229 | while (index > radix_tree_maxindex(height)) |
| 230 | height++; |
| 231 | |
| 232 | if (root->rnode == NULL) { |
| 233 | root->height = height; |
| 234 | goto out; |
| 235 | } |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | do { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 238 | unsigned int newheight; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | if (!(node = radix_tree_node_alloc(root))) |
| 240 | return -ENOMEM; |
| 241 | |
| 242 | /* Increase the height. */ |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 243 | node->slots[0] = radix_tree_direct_to_ptr(root->rnode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | /* Propagate the aggregated tag info into the new root */ |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 246 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 247 | if (root_tag_get(root, tag)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | tag_set(node, tag, 0); |
| 249 | } |
| 250 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 251 | newheight = root->height+1; |
| 252 | node->height = newheight; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | node->count = 1; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 254 | rcu_assign_pointer(root->rnode, node); |
| 255 | root->height = newheight; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } while (height > root->height); |
| 257 | out: |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * radix_tree_insert - insert into a radix tree |
| 263 | * @root: radix tree root |
| 264 | * @index: index key |
| 265 | * @item: item to insert |
| 266 | * |
| 267 | * Insert an item into the radix tree at position @index. |
| 268 | */ |
| 269 | int radix_tree_insert(struct radix_tree_root *root, |
| 270 | unsigned long index, void *item) |
| 271 | { |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 272 | struct radix_tree_node *node = NULL, *slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | unsigned int height, shift; |
| 274 | int offset; |
| 275 | int error; |
| 276 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 277 | BUG_ON(radix_tree_is_direct_ptr(item)); |
| 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | /* Make sure the tree is high enough. */ |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 280 | if (index > radix_tree_maxindex(root->height)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | error = radix_tree_extend(root, index); |
| 282 | if (error) |
| 283 | return error; |
| 284 | } |
| 285 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 286 | slot = root->rnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | height = root->height; |
| 288 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; |
| 289 | |
| 290 | offset = 0; /* uninitialised var warning */ |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 291 | while (height > 0) { |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 292 | if (slot == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | /* Have to add a child node. */ |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 294 | if (!(slot = radix_tree_node_alloc(root))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | return -ENOMEM; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 296 | slot->height = height; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 297 | if (node) { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 298 | rcu_assign_pointer(node->slots[offset], slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | node->count++; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 300 | } else |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 301 | rcu_assign_pointer(root->rnode, slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* Go a level down */ |
| 305 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 306 | node = slot; |
| 307 | slot = node->slots[offset]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | shift -= RADIX_TREE_MAP_SHIFT; |
| 309 | height--; |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 312 | if (slot != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return -EEXIST; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 314 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 315 | if (node) { |
| 316 | node->count++; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 317 | rcu_assign_pointer(node->slots[offset], item); |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 318 | BUG_ON(tag_get(node, 0, offset)); |
| 319 | BUG_ON(tag_get(node, 1, offset)); |
| 320 | } else { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 321 | rcu_assign_pointer(root->rnode, radix_tree_ptr_to_direct(item)); |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 322 | BUG_ON(root_tag_get(root, 0)); |
| 323 | BUG_ON(root_tag_get(root, 1)); |
| 324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | return 0; |
| 327 | } |
| 328 | EXPORT_SYMBOL(radix_tree_insert); |
| 329 | |
Hans Reiser | a433136 | 2005-11-07 00:59:29 -0800 | [diff] [blame] | 330 | /** |
| 331 | * radix_tree_lookup_slot - lookup a slot in a radix tree |
| 332 | * @root: radix tree root |
| 333 | * @index: index key |
| 334 | * |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 335 | * Returns: the slot corresponding to the position @index in the |
| 336 | * radix tree @root. This is useful for update-if-exists operations. |
| 337 | * |
| 338 | * This function cannot be called under rcu_read_lock, it must be |
| 339 | * excluded from writers, as must the returned slot for subsequent |
| 340 | * use by radix_tree_deref_slot() and radix_tree_replace slot. |
| 341 | * Caller must hold tree write locked across slot lookup and |
| 342 | * replace. |
Hans Reiser | a433136 | 2005-11-07 00:59:29 -0800 | [diff] [blame] | 343 | */ |
| 344 | void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index) |
| 345 | { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 346 | unsigned int height, shift; |
| 347 | struct radix_tree_node *node, **slot; |
| 348 | |
| 349 | node = root->rnode; |
| 350 | if (node == NULL) |
| 351 | return NULL; |
| 352 | |
| 353 | if (radix_tree_is_direct_ptr(node)) { |
| 354 | if (index > 0) |
| 355 | return NULL; |
| 356 | return (void **)&root->rnode; |
| 357 | } |
| 358 | |
| 359 | height = node->height; |
| 360 | if (index > radix_tree_maxindex(height)) |
| 361 | return NULL; |
| 362 | |
| 363 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; |
| 364 | |
| 365 | do { |
| 366 | slot = (struct radix_tree_node **) |
| 367 | (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK)); |
| 368 | node = *slot; |
| 369 | if (node == NULL) |
| 370 | return NULL; |
| 371 | |
| 372 | shift -= RADIX_TREE_MAP_SHIFT; |
| 373 | height--; |
| 374 | } while (height > 0); |
| 375 | |
| 376 | return (void **)slot; |
Hans Reiser | a433136 | 2005-11-07 00:59:29 -0800 | [diff] [blame] | 377 | } |
| 378 | EXPORT_SYMBOL(radix_tree_lookup_slot); |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /** |
| 381 | * radix_tree_lookup - perform lookup operation on a radix tree |
| 382 | * @root: radix tree root |
| 383 | * @index: index key |
| 384 | * |
| 385 | * Lookup the item at the position @index in the radix tree @root. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 386 | * |
| 387 | * This function can be called under rcu_read_lock, however the caller |
| 388 | * must manage lifetimes of leaf nodes (eg. RCU may also be used to free |
| 389 | * them safely). No RCU barriers are required to access or modify the |
| 390 | * returned item, however. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | */ |
| 392 | void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index) |
| 393 | { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 394 | unsigned int height, shift; |
| 395 | struct radix_tree_node *node, **slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 397 | node = rcu_dereference(root->rnode); |
| 398 | if (node == NULL) |
| 399 | return NULL; |
| 400 | |
| 401 | if (radix_tree_is_direct_ptr(node)) { |
| 402 | if (index > 0) |
| 403 | return NULL; |
| 404 | return radix_tree_direct_to_ptr(node); |
| 405 | } |
| 406 | |
| 407 | height = node->height; |
| 408 | if (index > radix_tree_maxindex(height)) |
| 409 | return NULL; |
| 410 | |
| 411 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; |
| 412 | |
| 413 | do { |
| 414 | slot = (struct radix_tree_node **) |
| 415 | (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK)); |
| 416 | node = rcu_dereference(*slot); |
| 417 | if (node == NULL) |
| 418 | return NULL; |
| 419 | |
| 420 | shift -= RADIX_TREE_MAP_SHIFT; |
| 421 | height--; |
| 422 | } while (height > 0); |
| 423 | |
| 424 | return node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | EXPORT_SYMBOL(radix_tree_lookup); |
| 427 | |
| 428 | /** |
| 429 | * radix_tree_tag_set - set a tag on a radix tree node |
| 430 | * @root: radix tree root |
| 431 | * @index: index key |
| 432 | * @tag: tag index |
| 433 | * |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 434 | * Set the search tag (which must be < RADIX_TREE_MAX_TAGS) |
| 435 | * corresponding to @index in the radix tree. From |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | * the root all the way down to the leaf node. |
| 437 | * |
| 438 | * Returns the address of the tagged item. Setting a tag on a not-present |
| 439 | * item is a bug. |
| 440 | */ |
| 441 | void *radix_tree_tag_set(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 442 | unsigned long index, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
| 444 | unsigned int height, shift; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 445 | struct radix_tree_node *slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
| 447 | height = root->height; |
Peter Zijlstra | 4c91c36 | 2006-06-23 02:03:25 -0700 | [diff] [blame] | 448 | BUG_ON(index > radix_tree_maxindex(height)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 450 | slot = root->rnode; |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 451 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | while (height > 0) { |
| 454 | int offset; |
| 455 | |
| 456 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 457 | if (!tag_get(slot, tag, offset)) |
| 458 | tag_set(slot, tag, offset); |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 459 | slot = slot->slots[offset]; |
| 460 | BUG_ON(slot == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | shift -= RADIX_TREE_MAP_SHIFT; |
| 462 | height--; |
| 463 | } |
| 464 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 465 | /* set the root's tag bit */ |
| 466 | if (slot && !root_tag_get(root, tag)) |
| 467 | root_tag_set(root, tag); |
| 468 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 469 | return slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
| 471 | EXPORT_SYMBOL(radix_tree_tag_set); |
| 472 | |
| 473 | /** |
| 474 | * radix_tree_tag_clear - clear a tag on a radix tree node |
| 475 | * @root: radix tree root |
| 476 | * @index: index key |
| 477 | * @tag: tag index |
| 478 | * |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 479 | * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS) |
| 480 | * corresponding to @index in the radix tree. If |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | * this causes the leaf node to have no tags set then clear the tag in the |
| 482 | * next-to-leaf node, etc. |
| 483 | * |
| 484 | * Returns the address of the tagged item on success, else NULL. ie: |
| 485 | * has the same return value and semantics as radix_tree_lookup(). |
| 486 | */ |
| 487 | void *radix_tree_tag_clear(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 488 | unsigned long index, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
| 490 | struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path; |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 491 | struct radix_tree_node *slot = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | unsigned int height, shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | height = root->height; |
| 495 | if (index > radix_tree_maxindex(height)) |
| 496 | goto out; |
| 497 | |
| 498 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; |
| 499 | pathp->node = NULL; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 500 | slot = root->rnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
| 502 | while (height > 0) { |
| 503 | int offset; |
| 504 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 505 | if (slot == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | goto out; |
| 507 | |
| 508 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; |
| 509 | pathp[1].offset = offset; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 510 | pathp[1].node = slot; |
| 511 | slot = slot->slots[offset]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | pathp++; |
| 513 | shift -= RADIX_TREE_MAP_SHIFT; |
| 514 | height--; |
| 515 | } |
| 516 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 517 | if (slot == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | goto out; |
| 519 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 520 | while (pathp->node) { |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 521 | if (!tag_get(pathp->node, tag, pathp->offset)) |
| 522 | goto out; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 523 | tag_clear(pathp->node, tag, pathp->offset); |
Nick Piggin | 6e954b9 | 2006-01-08 01:01:40 -0800 | [diff] [blame] | 524 | if (any_tag_set(pathp->node, tag)) |
| 525 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | pathp--; |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* clear the root's tag bit */ |
| 530 | if (root_tag_get(root, tag)) |
| 531 | root_tag_clear(root, tag); |
| 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | out: |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 534 | return slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | } |
| 536 | EXPORT_SYMBOL(radix_tree_tag_clear); |
| 537 | |
| 538 | #ifndef __KERNEL__ /* Only the test harness uses this at present */ |
| 539 | /** |
Marcelo Tosatti | 32605a1 | 2005-09-06 15:16:48 -0700 | [diff] [blame] | 540 | * radix_tree_tag_get - get a tag on a radix tree node |
| 541 | * @root: radix tree root |
| 542 | * @index: index key |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 543 | * @tag: tag index (< RADIX_TREE_MAX_TAGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | * |
Marcelo Tosatti | 32605a1 | 2005-09-06 15:16:48 -0700 | [diff] [blame] | 545 | * Return values: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | * |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 547 | * 0: tag not present or not set |
| 548 | * 1: tag set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | */ |
| 550 | int radix_tree_tag_get(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 551 | unsigned long index, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
| 553 | unsigned int height, shift; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 554 | struct radix_tree_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | int saw_unset_tag = 0; |
| 556 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 557 | /* check the root's tag bit */ |
| 558 | if (!root_tag_get(root, tag)) |
| 559 | return 0; |
| 560 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 561 | node = rcu_dereference(root->rnode); |
| 562 | if (node == NULL) |
| 563 | return 0; |
| 564 | |
| 565 | if (radix_tree_is_direct_ptr(node)) |
| 566 | return (index == 0); |
| 567 | |
| 568 | height = node->height; |
| 569 | if (index > radix_tree_maxindex(height)) |
| 570 | return 0; |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
| 574 | for ( ; ; ) { |
| 575 | int offset; |
| 576 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 577 | if (node == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | return 0; |
| 579 | |
| 580 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; |
| 581 | |
| 582 | /* |
| 583 | * This is just a debug check. Later, we can bale as soon as |
| 584 | * we see an unset tag. |
| 585 | */ |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 586 | if (!tag_get(node, tag, offset)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | saw_unset_tag = 1; |
| 588 | if (height == 1) { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 589 | int ret = tag_get(node, tag, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
| 591 | BUG_ON(ret && saw_unset_tag); |
Wu Fengguang | e5dcd90 | 2006-06-25 05:48:14 -0700 | [diff] [blame] | 592 | return !!ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 594 | node = rcu_dereference(node->slots[offset]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | shift -= RADIX_TREE_MAP_SHIFT; |
| 596 | height--; |
| 597 | } |
| 598 | } |
| 599 | EXPORT_SYMBOL(radix_tree_tag_get); |
| 600 | #endif |
| 601 | |
Fengguang Wu | 6df8ba4 | 2007-10-16 01:24:33 -0700 | [diff] [blame^] | 602 | /** |
| 603 | * radix_tree_next_hole - find the next hole (not-present entry) |
| 604 | * @root: tree root |
| 605 | * @index: index key |
| 606 | * @max_scan: maximum range to search |
| 607 | * |
| 608 | * Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest |
| 609 | * indexed hole. |
| 610 | * |
| 611 | * Returns: the index of the hole if found, otherwise returns an index |
| 612 | * outside of the set specified (in which case 'return - index >= max_scan' |
| 613 | * will be true). |
| 614 | * |
| 615 | * radix_tree_next_hole may be called under rcu_read_lock. However, like |
| 616 | * radix_tree_gang_lookup, this will not atomically search a snapshot of the |
| 617 | * tree at a single point in time. For example, if a hole is created at index |
| 618 | * 5, then subsequently a hole is created at index 10, radix_tree_next_hole |
| 619 | * covering both indexes may return 10 if called under rcu_read_lock. |
| 620 | */ |
| 621 | unsigned long radix_tree_next_hole(struct radix_tree_root *root, |
| 622 | unsigned long index, unsigned long max_scan) |
| 623 | { |
| 624 | unsigned long i; |
| 625 | |
| 626 | for (i = 0; i < max_scan; i++) { |
| 627 | if (!radix_tree_lookup(root, index)) |
| 628 | break; |
| 629 | index++; |
| 630 | if (index == 0) |
| 631 | break; |
| 632 | } |
| 633 | |
| 634 | return index; |
| 635 | } |
| 636 | EXPORT_SYMBOL(radix_tree_next_hole); |
| 637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | static unsigned int |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 639 | __lookup(struct radix_tree_node *slot, void **results, unsigned long index, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | unsigned int max_items, unsigned long *next_index) |
| 641 | { |
| 642 | unsigned int nr_found = 0; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 643 | unsigned int shift, height; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 644 | unsigned long i; |
| 645 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 646 | height = slot->height; |
| 647 | if (height == 0) |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 648 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 651 | for ( ; height > 1; height--) { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 652 | i = (index >> shift) & RADIX_TREE_MAP_MASK; |
| 653 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | if (slot->slots[i] != NULL) |
| 655 | break; |
| 656 | index &= ~((1UL << shift) - 1); |
| 657 | index += 1UL << shift; |
| 658 | if (index == 0) |
| 659 | goto out; /* 32-bit wraparound */ |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 660 | i++; |
| 661 | if (i == RADIX_TREE_MAP_SIZE) |
| 662 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | shift -= RADIX_TREE_MAP_SHIFT; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 666 | slot = rcu_dereference(slot->slots[i]); |
| 667 | if (slot == NULL) |
| 668 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 670 | |
| 671 | /* Bottom level: grab some items */ |
| 672 | for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 673 | struct radix_tree_node *node; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 674 | index++; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 675 | node = slot->slots[i]; |
| 676 | if (node) { |
| 677 | results[nr_found++] = rcu_dereference(node); |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 678 | if (nr_found == max_items) |
| 679 | goto out; |
| 680 | } |
| 681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | out: |
| 683 | *next_index = index; |
| 684 | return nr_found; |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * radix_tree_gang_lookup - perform multiple lookup on a radix tree |
| 689 | * @root: radix tree root |
| 690 | * @results: where the results of the lookup are placed |
| 691 | * @first_index: start the lookup from this key |
| 692 | * @max_items: place up to this many items at *results |
| 693 | * |
| 694 | * Performs an index-ascending scan of the tree for present items. Places |
| 695 | * them at *@results and returns the number of items which were placed at |
| 696 | * *@results. |
| 697 | * |
| 698 | * The implementation is naive. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 699 | * |
| 700 | * Like radix_tree_lookup, radix_tree_gang_lookup may be called under |
| 701 | * rcu_read_lock. In this case, rather than the returned results being |
| 702 | * an atomic snapshot of the tree at a single point in time, the semantics |
| 703 | * of an RCU protected gang lookup are as though multiple radix_tree_lookups |
| 704 | * have been issued in individual locks, and results stored in 'results'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | */ |
| 706 | unsigned int |
| 707 | radix_tree_gang_lookup(struct radix_tree_root *root, void **results, |
| 708 | unsigned long first_index, unsigned int max_items) |
| 709 | { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 710 | unsigned long max_index; |
| 711 | struct radix_tree_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | unsigned long cur_index = first_index; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 713 | unsigned int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 715 | node = rcu_dereference(root->rnode); |
| 716 | if (!node) |
| 717 | return 0; |
| 718 | |
| 719 | if (radix_tree_is_direct_ptr(node)) { |
| 720 | if (first_index > 0) |
| 721 | return 0; |
| 722 | node = radix_tree_direct_to_ptr(node); |
| 723 | results[0] = rcu_dereference(node); |
| 724 | return 1; |
| 725 | } |
| 726 | |
| 727 | max_index = radix_tree_maxindex(node->height); |
| 728 | |
| 729 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | while (ret < max_items) { |
| 731 | unsigned int nr_found; |
| 732 | unsigned long next_index; /* Index of next search */ |
| 733 | |
| 734 | if (cur_index > max_index) |
| 735 | break; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 736 | nr_found = __lookup(node, results + ret, cur_index, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | max_items - ret, &next_index); |
| 738 | ret += nr_found; |
| 739 | if (next_index == 0) |
| 740 | break; |
| 741 | cur_index = next_index; |
| 742 | } |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | return ret; |
| 745 | } |
| 746 | EXPORT_SYMBOL(radix_tree_gang_lookup); |
| 747 | |
| 748 | /* |
| 749 | * FIXME: the two tag_get()s here should use find_next_bit() instead of |
| 750 | * open-coding the search. |
| 751 | */ |
| 752 | static unsigned int |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 753 | __lookup_tag(struct radix_tree_node *slot, void **results, unsigned long index, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 754 | unsigned int max_items, unsigned long *next_index, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | { |
| 756 | unsigned int nr_found = 0; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 757 | unsigned int shift, height; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 759 | height = slot->height; |
| 760 | if (height == 0) |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 761 | goto out; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 762 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 763 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 764 | while (height > 0) { |
| 765 | unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 767 | for (;;) { |
| 768 | if (tag_get(slot, tag, i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | index &= ~((1UL << shift) - 1); |
| 771 | index += 1UL << shift; |
| 772 | if (index == 0) |
| 773 | goto out; /* 32-bit wraparound */ |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 774 | i++; |
| 775 | if (i == RADIX_TREE_MAP_SIZE) |
| 776 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | height--; |
| 779 | if (height == 0) { /* Bottom level: grab some items */ |
| 780 | unsigned long j = index & RADIX_TREE_MAP_MASK; |
| 781 | |
| 782 | for ( ; j < RADIX_TREE_MAP_SIZE; j++) { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 783 | struct radix_tree_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | index++; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 785 | if (!tag_get(slot, tag, j)) |
| 786 | continue; |
| 787 | node = slot->slots[j]; |
| 788 | /* |
| 789 | * Even though the tag was found set, we need to |
| 790 | * recheck that we have a non-NULL node, because |
| 791 | * if this lookup is lockless, it may have been |
| 792 | * subsequently deleted. |
| 793 | * |
| 794 | * Similar care must be taken in any place that |
| 795 | * lookup ->slots[x] without a lock (ie. can't |
| 796 | * rely on its value remaining the same). |
| 797 | */ |
| 798 | if (node) { |
| 799 | node = rcu_dereference(node); |
| 800 | results[nr_found++] = node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | if (nr_found == max_items) |
| 802 | goto out; |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | shift -= RADIX_TREE_MAP_SHIFT; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 807 | slot = rcu_dereference(slot->slots[i]); |
| 808 | if (slot == NULL) |
| 809 | break; |
| 810 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | out: |
| 812 | *next_index = index; |
| 813 | return nr_found; |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree |
| 818 | * based on a tag |
| 819 | * @root: radix tree root |
| 820 | * @results: where the results of the lookup are placed |
| 821 | * @first_index: start the lookup from this key |
| 822 | * @max_items: place up to this many items at *results |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 823 | * @tag: the tag index (< RADIX_TREE_MAX_TAGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | * |
| 825 | * Performs an index-ascending scan of the tree for present items which |
| 826 | * have the tag indexed by @tag set. Places the items at *@results and |
| 827 | * returns the number of items which were placed at *@results. |
| 828 | */ |
| 829 | unsigned int |
| 830 | radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 831 | unsigned long first_index, unsigned int max_items, |
| 832 | unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 834 | struct radix_tree_node *node; |
| 835 | unsigned long max_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | unsigned long cur_index = first_index; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 837 | unsigned int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 839 | /* check the root's tag bit */ |
| 840 | if (!root_tag_get(root, tag)) |
| 841 | return 0; |
| 842 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 843 | node = rcu_dereference(root->rnode); |
| 844 | if (!node) |
| 845 | return 0; |
| 846 | |
| 847 | if (radix_tree_is_direct_ptr(node)) { |
| 848 | if (first_index > 0) |
| 849 | return 0; |
| 850 | node = radix_tree_direct_to_ptr(node); |
| 851 | results[0] = rcu_dereference(node); |
| 852 | return 1; |
| 853 | } |
| 854 | |
| 855 | max_index = radix_tree_maxindex(node->height); |
| 856 | |
| 857 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | while (ret < max_items) { |
| 859 | unsigned int nr_found; |
| 860 | unsigned long next_index; /* Index of next search */ |
| 861 | |
| 862 | if (cur_index > max_index) |
| 863 | break; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 864 | nr_found = __lookup_tag(node, results + ret, cur_index, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | max_items - ret, &next_index, tag); |
| 866 | ret += nr_found; |
| 867 | if (next_index == 0) |
| 868 | break; |
| 869 | cur_index = next_index; |
| 870 | } |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | return ret; |
| 873 | } |
| 874 | EXPORT_SYMBOL(radix_tree_gang_lookup_tag); |
| 875 | |
| 876 | /** |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 877 | * radix_tree_shrink - shrink height of a radix tree to minimal |
| 878 | * @root radix tree root |
| 879 | */ |
| 880 | static inline void radix_tree_shrink(struct radix_tree_root *root) |
| 881 | { |
| 882 | /* try to shrink tree height */ |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 883 | while (root->height > 0 && |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 884 | root->rnode->count == 1 && |
| 885 | root->rnode->slots[0]) { |
| 886 | struct radix_tree_node *to_free = root->rnode; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 887 | void *newptr; |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 888 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 889 | /* |
| 890 | * We don't need rcu_assign_pointer(), since we are simply |
| 891 | * moving the node from one part of the tree to another. If |
| 892 | * it was safe to dereference the old pointer to it |
| 893 | * (to_free->slots[0]), it will be safe to dereference the new |
| 894 | * one (root->rnode). |
| 895 | */ |
| 896 | newptr = to_free->slots[0]; |
| 897 | if (root->height == 1) |
| 898 | newptr = radix_tree_ptr_to_direct(newptr); |
| 899 | root->rnode = newptr; |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 900 | root->height--; |
| 901 | /* must only free zeroed nodes into the slab */ |
| 902 | tag_clear(to_free, 0, 0); |
| 903 | tag_clear(to_free, 1, 0); |
| 904 | to_free->slots[0] = NULL; |
| 905 | to_free->count = 0; |
| 906 | radix_tree_node_free(to_free); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | * radix_tree_delete - delete an item from a radix tree |
| 912 | * @root: radix tree root |
| 913 | * @index: index key |
| 914 | * |
| 915 | * Remove the item at @index from the radix tree rooted at @root. |
| 916 | * |
| 917 | * Returns the address of the deleted item, or NULL if it was not present. |
| 918 | */ |
| 919 | void *radix_tree_delete(struct radix_tree_root *root, unsigned long index) |
| 920 | { |
| 921 | struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path; |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 922 | struct radix_tree_node *slot = NULL; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 923 | struct radix_tree_node *to_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | unsigned int height, shift; |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 925 | int tag; |
| 926 | int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | |
| 928 | height = root->height; |
| 929 | if (index > radix_tree_maxindex(height)) |
| 930 | goto out; |
| 931 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 932 | slot = root->rnode; |
| 933 | if (height == 0 && root->rnode) { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 934 | slot = radix_tree_direct_to_ptr(slot); |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 935 | root_tag_clear_all(root); |
| 936 | root->rnode = NULL; |
| 937 | goto out; |
| 938 | } |
| 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; |
| 941 | pathp->node = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 943 | do { |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 944 | if (slot == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | goto out; |
| 946 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | pathp++; |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 948 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; |
| 949 | pathp->offset = offset; |
| 950 | pathp->node = slot; |
| 951 | slot = slot->slots[offset]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | shift -= RADIX_TREE_MAP_SHIFT; |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 953 | height--; |
| 954 | } while (height > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 956 | if (slot == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | goto out; |
| 958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | /* |
| 960 | * Clear all tags associated with the just-deleted item |
| 961 | */ |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 962 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 963 | if (tag_get(pathp->node, tag, pathp->offset)) |
| 964 | radix_tree_tag_clear(root, index, tag); |
Nick Piggin | d527426 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 965 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 967 | to_free = NULL; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 968 | /* Now free the nodes we do not need anymore */ |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 969 | while (pathp->node) { |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 970 | pathp->node->slots[pathp->offset] = NULL; |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 971 | pathp->node->count--; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 972 | /* |
| 973 | * Queue the node for deferred freeing after the |
| 974 | * last reference to it disappears (set NULL, above). |
| 975 | */ |
| 976 | if (to_free) |
| 977 | radix_tree_node_free(to_free); |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 978 | |
| 979 | if (pathp->node->count) { |
| 980 | if (pathp->node == root->rnode) |
| 981 | radix_tree_shrink(root); |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 982 | goto out; |
Nick Piggin | a5f51c9 | 2006-01-08 01:01:41 -0800 | [diff] [blame] | 983 | } |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 984 | |
| 985 | /* Node with zero slots in use so free it */ |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 986 | to_free = pathp->node; |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 987 | pathp--; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | } |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 990 | root_tag_clear_all(root); |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 991 | root->height = 0; |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 992 | root->rnode = NULL; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 993 | if (to_free) |
| 994 | radix_tree_node_free(to_free); |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | out: |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 997 | return slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | } |
| 999 | EXPORT_SYMBOL(radix_tree_delete); |
| 1000 | |
| 1001 | /** |
| 1002 | * radix_tree_tagged - test whether any items in the tree are tagged |
| 1003 | * @root: radix tree root |
| 1004 | * @tag: tag to test |
| 1005 | */ |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 1006 | int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | { |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 1008 | return root_tag_get(root, tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | } |
| 1010 | EXPORT_SYMBOL(radix_tree_tagged); |
| 1011 | |
| 1012 | static void |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1013 | radix_tree_node_ctor(void *node, struct kmem_cache *cachep, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | { |
| 1015 | memset(node, 0, sizeof(struct radix_tree_node)); |
| 1016 | } |
| 1017 | |
| 1018 | static __init unsigned long __maxindex(unsigned int height) |
| 1019 | { |
| 1020 | unsigned int tmp = height * RADIX_TREE_MAP_SHIFT; |
| 1021 | unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1; |
| 1022 | |
| 1023 | if (tmp >= RADIX_TREE_INDEX_BITS) |
| 1024 | index = ~0UL; |
| 1025 | return index; |
| 1026 | } |
| 1027 | |
| 1028 | static __init void radix_tree_init_maxindex(void) |
| 1029 | { |
| 1030 | unsigned int i; |
| 1031 | |
| 1032 | for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++) |
| 1033 | height_to_maxindex[i] = __maxindex(i); |
| 1034 | } |
| 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | static int radix_tree_callback(struct notifier_block *nfb, |
| 1037 | unsigned long action, |
| 1038 | void *hcpu) |
| 1039 | { |
| 1040 | int cpu = (long)hcpu; |
| 1041 | struct radix_tree_preload *rtp; |
| 1042 | |
| 1043 | /* Free per-cpu pool of perloaded nodes */ |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1044 | if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | rtp = &per_cpu(radix_tree_preloads, cpu); |
| 1046 | while (rtp->nr) { |
| 1047 | kmem_cache_free(radix_tree_node_cachep, |
| 1048 | rtp->nodes[rtp->nr-1]); |
| 1049 | rtp->nodes[rtp->nr-1] = NULL; |
| 1050 | rtp->nr--; |
| 1051 | } |
| 1052 | } |
| 1053 | return NOTIFY_OK; |
| 1054 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
| 1056 | void __init radix_tree_init(void) |
| 1057 | { |
| 1058 | radix_tree_node_cachep = kmem_cache_create("radix_tree_node", |
| 1059 | sizeof(struct radix_tree_node), 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1060 | SLAB_PANIC, radix_tree_node_ctor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | radix_tree_init_maxindex(); |
| 1062 | hotcpu_notifier(radix_tree_callback, 0); |
| 1063 | } |