btrfs-progs: doc: correct the destination of btrfs-receive
[btrfs-progs-unstable/devel.git] / radix-tree.c
blobf259ab563b0e9362acbc5d7a438b49094e8341bc
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 * Copyright (C) 2001 Momchil Velikov
21 * Portions Copyright (C) 2001 Christoph Hellwig
22 * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com>
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License as
26 * published by the Free Software Foundation; either version 2, or (at
27 * your option) any later version.
29 * This program is distributed in the hope that it will be useful, but
30 * WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include "kerncompat.h"
40 #include "radix-tree.h"
41 #ifdef __KERNEL__
42 #define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
43 #else
44 #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
45 #endif
47 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
48 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
50 #define RADIX_TREE_TAG_LONGS \
51 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
53 struct radix_tree_node {
54 unsigned int count;
55 void *slots[RADIX_TREE_MAP_SIZE];
56 unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
59 struct radix_tree_path {
60 struct radix_tree_node *node;
61 int offset;
64 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
65 #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
67 static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly;
70 * Per-cpu pool of preloaded nodes
72 struct radix_tree_preload {
73 int nr;
74 struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
76 static struct radix_tree_preload radix_tree_preloads = { 0, };
78 static int internal_nodes = 0;
80 * This assumes that the caller has performed appropriate preallocation, and
81 * that the caller has pinned this thread of control to the current CPU.
83 static struct radix_tree_node *
84 radix_tree_node_alloc(struct radix_tree_root *root)
86 struct radix_tree_node *ret;
87 ret = malloc(sizeof(struct radix_tree_node));
88 if (ret) {
89 memset(ret, 0, sizeof(struct radix_tree_node));
90 internal_nodes++;
92 return ret;
95 static inline void
96 radix_tree_node_free(struct radix_tree_node *node)
98 internal_nodes--;
99 free(node);
103 * Load up this CPU's radix_tree_node buffer with sufficient objects to
104 * ensure that the addition of a single element in the tree cannot fail. On
105 * success, return zero, with preemption disabled. On error, return -ENOMEM
106 * with preemption not disabled.
108 int radix_tree_preload(gfp_t gfp_mask)
110 struct radix_tree_preload *rtp;
111 struct radix_tree_node *node;
112 int ret = -ENOMEM;
114 preempt_disable();
115 rtp = &__get_cpu_var(radix_tree_preloads);
116 while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
117 preempt_enable();
118 node = radix_tree_node_alloc(NULL);
119 if (node == NULL)
120 goto out;
121 preempt_disable();
122 rtp = &__get_cpu_var(radix_tree_preloads);
123 if (rtp->nr < ARRAY_SIZE(rtp->nodes))
124 rtp->nodes[rtp->nr++] = node;
125 else
126 radix_tree_node_free(node);
128 ret = 0;
129 out:
130 return ret;
133 static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
134 int offset)
136 __set_bit(offset, node->tags[tag]);
139 static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
140 int offset)
142 __clear_bit(offset, node->tags[tag]);
145 static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
146 int offset)
148 return test_bit(offset, node->tags[tag]);
151 static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
153 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
157 static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
159 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
162 static inline void root_tag_clear_all(struct radix_tree_root *root)
164 root->gfp_mask &= __GFP_BITS_MASK;
167 static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
169 return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
173 * Returns 1 if any slot in the node has this tag set.
174 * Otherwise returns 0.
176 static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
178 int idx;
179 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
180 if (node->tags[tag][idx])
181 return 1;
183 return 0;
187 * Return the maximum key which can be store into a
188 * radix tree with height HEIGHT.
190 static inline unsigned long radix_tree_maxindex(unsigned int height)
192 return height_to_maxindex[height];
196 * Extend a radix tree so it can store key @index.
198 static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
200 struct radix_tree_node *node;
201 unsigned int height;
202 int tag;
204 /* Figure out what the height should be. */
205 height = root->height + 1;
206 while (index > radix_tree_maxindex(height))
207 height++;
209 if (root->rnode == NULL) {
210 root->height = height;
211 goto out;
214 do {
215 if (!(node = radix_tree_node_alloc(root)))
216 return -ENOMEM;
218 /* Increase the height. */
219 node->slots[0] = root->rnode;
221 /* Propagate the aggregated tag info into the new root */
222 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
223 if (root_tag_get(root, tag))
224 tag_set(node, tag, 0);
227 node->count = 1;
228 root->rnode = node;
229 root->height++;
230 } while (height > root->height);
231 out:
232 return 0;
236 * radix_tree_insert - insert into a radix tree
237 * @root: radix tree root
238 * @index: index key
239 * @item: item to insert
241 * Insert an item into the radix tree at position @index.
243 int radix_tree_insert(struct radix_tree_root *root,
244 unsigned long index, void *item)
246 struct radix_tree_node *node = NULL, *slot;
247 unsigned int height, shift;
248 int offset;
249 int error;
251 /* Make sure the tree is high enough. */
252 if (index > radix_tree_maxindex(root->height)) {
253 error = radix_tree_extend(root, index);
254 if (error)
255 return error;
258 slot = root->rnode;
259 height = root->height;
260 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
262 offset = 0; /* uninitialised var warning */
263 while (height > 0) {
264 if (slot == NULL) {
265 /* Have to add a child node. */
266 if (!(slot = radix_tree_node_alloc(root)))
267 return -ENOMEM;
268 if (node) {
269 node->slots[offset] = slot;
270 node->count++;
271 } else
272 root->rnode = slot;
275 /* Go a level down */
276 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
277 node = slot;
278 slot = node->slots[offset];
279 shift -= RADIX_TREE_MAP_SHIFT;
280 height--;
283 if (slot != NULL)
284 return -EEXIST;
286 if (node) {
287 node->count++;
288 node->slots[offset] = item;
289 BUG_ON(tag_get(node, 0, offset));
290 BUG_ON(tag_get(node, 1, offset));
291 } else {
292 root->rnode = item;
293 BUG_ON(root_tag_get(root, 0));
294 BUG_ON(root_tag_get(root, 1));
297 return 0;
300 static inline void **__lookup_slot(struct radix_tree_root *root,
301 unsigned long index)
303 unsigned int height, shift;
304 struct radix_tree_node **slot;
306 height = root->height;
308 if (index > radix_tree_maxindex(height))
309 return NULL;
311 if (height == 0 && root->rnode)
312 return (void *)&root->rnode;
314 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
315 slot = &root->rnode;
317 while (height > 0) {
318 if (*slot == NULL)
319 return NULL;
321 slot = (struct radix_tree_node **)
322 ((*slot)->slots +
323 ((index >> shift) & RADIX_TREE_MAP_MASK));
324 shift -= RADIX_TREE_MAP_SHIFT;
325 height--;
328 return (void **)slot;
332 * radix_tree_lookup_slot - lookup a slot in a radix tree
333 * @root: radix tree root
334 * @index: index key
336 * Lookup the slot corresponding to the position @index in the radix tree
337 * @root. This is useful for update-if-exists operations.
339 void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
341 return __lookup_slot(root, index);
345 * radix_tree_lookup - perform lookup operation on a radix tree
346 * @root: radix tree root
347 * @index: index key
349 * Lookup the item at the position @index in the radix tree @root.
351 void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
353 void **slot;
355 slot = __lookup_slot(root, index);
356 return slot != NULL ? *slot : NULL;
360 * radix_tree_tag_set - set a tag on a radix tree node
361 * @root: radix tree root
362 * @index: index key
363 * @tag: tag index
365 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
366 * corresponding to @index in the radix tree. From
367 * the root all the way down to the leaf node.
369 * Returns the address of the tagged item. Setting a tag on a not-present
370 * item is a bug.
372 void *radix_tree_tag_set(struct radix_tree_root *root,
373 unsigned long index, unsigned int tag)
375 unsigned int height, shift;
376 struct radix_tree_node *slot;
378 height = root->height;
379 BUG_ON(index > radix_tree_maxindex(height));
381 slot = root->rnode;
382 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
384 while (height > 0) {
385 int offset;
387 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
388 if (!tag_get(slot, tag, offset))
389 tag_set(slot, tag, offset);
390 slot = slot->slots[offset];
391 BUG_ON(slot == NULL);
392 shift -= RADIX_TREE_MAP_SHIFT;
393 height--;
396 /* set the root's tag bit */
397 if (slot && !root_tag_get(root, tag))
398 root_tag_set(root, tag);
400 return slot;
404 * radix_tree_tag_clear - clear a tag on a radix tree node
405 * @root: radix tree root
406 * @index: index key
407 * @tag: tag index
409 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
410 * corresponding to @index in the radix tree. If
411 * this causes the leaf node to have no tags set then clear the tag in the
412 * next-to-leaf node, etc.
414 * Returns the address of the tagged item on success, else NULL. ie:
415 * has the same return value and semantics as radix_tree_lookup().
417 void *radix_tree_tag_clear(struct radix_tree_root *root,
418 unsigned long index, unsigned int tag)
420 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
421 struct radix_tree_node *slot = NULL;
422 unsigned int height, shift;
424 height = root->height;
425 if (index > radix_tree_maxindex(height))
426 goto out;
428 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
429 pathp->node = NULL;
430 slot = root->rnode;
432 while (height > 0) {
433 int offset;
435 if (slot == NULL)
436 goto out;
438 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
439 pathp[1].offset = offset;
440 pathp[1].node = slot;
441 slot = slot->slots[offset];
442 pathp++;
443 shift -= RADIX_TREE_MAP_SHIFT;
444 height--;
447 if (slot == NULL)
448 goto out;
450 while (pathp->node) {
451 if (!tag_get(pathp->node, tag, pathp->offset))
452 goto out;
453 tag_clear(pathp->node, tag, pathp->offset);
454 if (any_tag_set(pathp->node, tag))
455 goto out;
456 pathp--;
459 /* clear the root's tag bit */
460 if (root_tag_get(root, tag))
461 root_tag_clear(root, tag);
463 out:
464 return slot;
467 #ifndef __KERNEL__ /* Only the test harness uses this at present */
469 * radix_tree_tag_get - get a tag on a radix tree node
470 * @root: radix tree root
471 * @index: index key
472 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
474 * Return values:
476 * 0: tag not present or not set
477 * 1: tag set
479 int radix_tree_tag_get(struct radix_tree_root *root,
480 unsigned long index, unsigned int tag)
482 unsigned int height, shift;
483 struct radix_tree_node *slot;
484 int saw_unset_tag = 0;
486 height = root->height;
487 if (index > radix_tree_maxindex(height))
488 return 0;
490 /* check the root's tag bit */
491 if (!root_tag_get(root, tag))
492 return 0;
494 if (height == 0)
495 return 1;
497 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
498 slot = root->rnode;
500 for ( ; ; ) {
501 int offset;
503 if (slot == NULL)
504 return 0;
506 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
509 * This is just a debug check. Later, we can bale as soon as
510 * we see an unset tag.
512 if (!tag_get(slot, tag, offset))
513 saw_unset_tag = 1;
514 if (height == 1) {
515 int ret = tag_get(slot, tag, offset);
517 BUG_ON(ret && saw_unset_tag);
518 return !!ret;
520 slot = slot->slots[offset];
521 shift -= RADIX_TREE_MAP_SHIFT;
522 height--;
525 #endif
527 static unsigned int
528 __lookup(struct radix_tree_root *root, void **results, unsigned long index,
529 unsigned int max_items, unsigned long *next_index)
531 unsigned int nr_found = 0;
532 unsigned int shift, height;
533 struct radix_tree_node *slot;
534 unsigned long i;
536 height = root->height;
537 if (height == 0) {
538 if (root->rnode && index == 0)
539 results[nr_found++] = root->rnode;
540 goto out;
543 shift = (height-1) * RADIX_TREE_MAP_SHIFT;
544 slot = root->rnode;
546 for ( ; height > 1; height--) {
548 for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
549 i < RADIX_TREE_MAP_SIZE; i++) {
550 if (slot->slots[i] != NULL)
551 break;
552 index &= ~((1UL << shift) - 1);
553 index += 1UL << shift;
554 if (index == 0)
555 goto out; /* 32-bit wraparound */
557 if (i == RADIX_TREE_MAP_SIZE)
558 goto out;
560 shift -= RADIX_TREE_MAP_SHIFT;
561 slot = slot->slots[i];
564 /* Bottom level: grab some items */
565 for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
566 index++;
567 if (slot->slots[i]) {
568 results[nr_found++] = slot->slots[i];
569 if (nr_found == max_items)
570 goto out;
573 out:
574 *next_index = index;
575 return nr_found;
579 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
580 * @root: radix tree root
581 * @results: where the results of the lookup are placed
582 * @first_index: start the lookup from this key
583 * @max_items: place up to this many items at *results
585 * Performs an index-ascending scan of the tree for present items. Places
586 * them at *@results and returns the number of items which were placed at
587 * *@results.
589 * The implementation is naive.
591 unsigned int
592 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
593 unsigned long first_index, unsigned int max_items)
595 const unsigned long max_index = radix_tree_maxindex(root->height);
596 unsigned long cur_index = first_index;
597 unsigned int ret = 0;
599 while (ret < max_items) {
600 unsigned int nr_found;
601 unsigned long next_index; /* Index of next search */
603 if (cur_index > max_index)
604 break;
605 nr_found = __lookup(root, results + ret, cur_index,
606 max_items - ret, &next_index);
607 ret += nr_found;
608 if (next_index == 0)
609 break;
610 cur_index = next_index;
612 return ret;
616 * FIXME: the two tag_get()s here should use find_next_bit() instead of
617 * open-coding the search.
619 static unsigned int
620 __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
621 unsigned int max_items, unsigned long *next_index, unsigned int tag)
623 unsigned int nr_found = 0;
624 unsigned int shift;
625 unsigned int height = root->height;
626 struct radix_tree_node *slot;
628 if (height == 0) {
629 if (root->rnode && index == 0)
630 results[nr_found++] = root->rnode;
631 goto out;
634 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
635 slot = root->rnode;
637 do {
638 unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
640 for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
641 if (tag_get(slot, tag, i)) {
642 BUG_ON(slot->slots[i] == NULL);
643 break;
645 index &= ~((1UL << shift) - 1);
646 index += 1UL << shift;
647 if (index == 0)
648 goto out; /* 32-bit wraparound */
650 if (i == RADIX_TREE_MAP_SIZE)
651 goto out;
652 height--;
653 if (height == 0) { /* Bottom level: grab some items */
654 unsigned long j = index & RADIX_TREE_MAP_MASK;
656 for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
657 index++;
658 if (tag_get(slot, tag, j)) {
659 BUG_ON(slot->slots[j] == NULL);
660 results[nr_found++] = slot->slots[j];
661 if (nr_found == max_items)
662 goto out;
666 shift -= RADIX_TREE_MAP_SHIFT;
667 slot = slot->slots[i];
668 } while (height > 0);
669 out:
670 *next_index = index;
671 return nr_found;
675 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
676 * based on a tag
677 * @root: radix tree root
678 * @results: where the results of the lookup are placed
679 * @first_index: start the lookup from this key
680 * @max_items: place up to this many items at *results
681 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
683 * Performs an index-ascending scan of the tree for present items which
684 * have the tag indexed by @tag set. Places the items at *@results and
685 * returns the number of items which were placed at *@results.
687 unsigned int
688 radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
689 unsigned long first_index, unsigned int max_items,
690 unsigned int tag)
692 const unsigned long max_index = radix_tree_maxindex(root->height);
693 unsigned long cur_index = first_index;
694 unsigned int ret = 0;
696 /* check the root's tag bit */
697 if (!root_tag_get(root, tag))
698 return 0;
700 while (ret < max_items) {
701 unsigned int nr_found;
702 unsigned long next_index; /* Index of next search */
704 if (cur_index > max_index)
705 break;
706 nr_found = __lookup_tag(root, results + ret, cur_index,
707 max_items - ret, &next_index, tag);
708 ret += nr_found;
709 if (next_index == 0)
710 break;
711 cur_index = next_index;
713 return ret;
717 * radix_tree_shrink - shrink height of a radix tree to minimal
718 * @root radix tree root
720 static inline void radix_tree_shrink(struct radix_tree_root *root)
722 /* try to shrink tree height */
723 while (root->height > 0 &&
724 root->rnode->count == 1 &&
725 root->rnode->slots[0]) {
726 struct radix_tree_node *to_free = root->rnode;
728 root->rnode = to_free->slots[0];
729 root->height--;
730 /* must only free zeroed nodes into the slab */
731 tag_clear(to_free, 0, 0);
732 tag_clear(to_free, 1, 0);
733 to_free->slots[0] = NULL;
734 to_free->count = 0;
735 radix_tree_node_free(to_free);
740 * radix_tree_delete - delete an item from a radix tree
741 * @root: radix tree root
742 * @index: index key
744 * Remove the item at @index from the radix tree rooted at @root.
746 * Returns the address of the deleted item, or NULL if it was not present.
748 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
750 struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
751 struct radix_tree_node *slot = NULL;
752 unsigned int height, shift;
753 int tag;
754 int offset;
756 height = root->height;
757 if (index > radix_tree_maxindex(height))
758 goto out;
760 slot = root->rnode;
761 if (height == 0 && root->rnode) {
762 root_tag_clear_all(root);
763 root->rnode = NULL;
764 goto out;
767 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
768 pathp->node = NULL;
770 do {
771 if (slot == NULL)
772 goto out;
774 pathp++;
775 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
776 pathp->offset = offset;
777 pathp->node = slot;
778 slot = slot->slots[offset];
779 shift -= RADIX_TREE_MAP_SHIFT;
780 height--;
781 } while (height > 0);
783 if (slot == NULL)
784 goto out;
787 * Clear all tags associated with the just-deleted item
789 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
790 if (tag_get(pathp->node, tag, pathp->offset))
791 radix_tree_tag_clear(root, index, tag);
794 /* Now free the nodes we do not need anymore */
795 while (pathp->node) {
796 pathp->node->slots[pathp->offset] = NULL;
797 pathp->node->count--;
799 if (pathp->node->count) {
800 if (pathp->node == root->rnode)
801 radix_tree_shrink(root);
802 goto out;
805 /* Node with zero slots in use so free it */
806 radix_tree_node_free(pathp->node);
808 pathp--;
810 root_tag_clear_all(root);
811 root->height = 0;
812 root->rnode = NULL;
814 out:
815 return slot;
819 * radix_tree_tagged - test whether any items in the tree are tagged
820 * @root: radix tree root
821 * @tag: tag to test
823 int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
825 return root_tag_get(root, tag);
828 static unsigned long __maxindex(unsigned int height)
830 unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
831 unsigned long index = ~0UL;
833 if (tmp < RADIX_TREE_INDEX_BITS)
834 index = (index >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
835 return index;
838 static void radix_tree_init_maxindex(void)
840 unsigned int i;
842 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
843 height_to_maxindex[i] = __maxindex(i);
846 void radix_tree_init(void)
848 radix_tree_init_maxindex();