aboutsummaryrefslogtreecommitdiffstats
path: root/lib/radix-tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/radix-tree.c')
-rw-r--r--lib/radix-tree.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/lib/radix-tree.c b/lib/radix-tree.c
index 05da38bcc298..5b7d4623f0b7 100644
--- a/lib/radix-tree.c
+++ b/lib/radix-tree.c
@@ -609,6 +609,103 @@ int radix_tree_tag_get(struct radix_tree_root *root,
609EXPORT_SYMBOL(radix_tree_tag_get); 609EXPORT_SYMBOL(radix_tree_tag_get);
610 610
611/** 611/**
612 * radix_tree_range_tag_if_tagged - for each item in given range set given
613 * tag if item has another tag set
614 * @root: radix tree root
615 * @first_indexp: pointer to a starting index of a range to scan
616 * @last_index: last index of a range to scan
617 * @nr_to_tag: maximum number items to tag
618 * @iftag: tag index to test
619 * @settag: tag index to set if tested tag is set
620 *
621 * This function scans range of radix tree from first_index to last_index
622 * (inclusive). For each item in the range if iftag is set, the function sets
623 * also settag. The function stops either after tagging nr_to_tag items or
624 * after reaching last_index.
625 *
626 * The function returns number of leaves where the tag was set and sets
627 * *first_indexp to the first unscanned index.
628 * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must
629 * be prepared to handle that.
630 */
631unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
632 unsigned long *first_indexp, unsigned long last_index,
633 unsigned long nr_to_tag,
634 unsigned int iftag, unsigned int settag)
635{
636 unsigned int height = root->height, shift;
637 unsigned long tagged = 0, index = *first_indexp;
638 struct radix_tree_node *open_slots[height], *slot;
639
640 last_index = min(last_index, radix_tree_maxindex(height));
641 if (index > last_index)
642 return 0;
643 if (!nr_to_tag)
644 return 0;
645 if (!root_tag_get(root, iftag)) {
646 *first_indexp = last_index + 1;
647 return 0;
648 }
649 if (height == 0) {
650 *first_indexp = last_index + 1;
651 root_tag_set(root, settag);
652 return 1;
653 }
654
655 shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
656 slot = radix_tree_indirect_to_ptr(root->rnode);
657
658 for (;;) {
659 int offset;
660
661 offset = (index >> shift) & RADIX_TREE_MAP_MASK;
662 if (!slot->slots[offset])
663 goto next;
664 if (!tag_get(slot, iftag, offset))
665 goto next;
666 tag_set(slot, settag, offset);
667 if (height == 1) {
668 tagged++;
669 goto next;
670 }
671 /* Go down one level */
672 height--;
673 shift -= RADIX_TREE_MAP_SHIFT;
674 open_slots[height] = slot;
675 slot = slot->slots[offset];
676 continue;
677next:
678 /* Go to next item at level determined by 'shift' */
679 index = ((index >> shift) + 1) << shift;
680 /* Overflow can happen when last_index is ~0UL... */
681 if (index > last_index || !index)
682 break;
683 if (tagged >= nr_to_tag)
684 break;
685 while (((index >> shift) & RADIX_TREE_MAP_MASK) == 0) {
686 /*
687 * We've fully scanned this node. Go up. Because
688 * last_index is guaranteed to be in the tree, what
689 * we do below cannot wander astray.
690 */
691 slot = open_slots[height];
692 height++;
693 shift += RADIX_TREE_MAP_SHIFT;
694 }
695 }
696 /*
697 * The iftag must have been set somewhere because otherwise
698 * we would return immediated at the beginning of the function
699 */
700 root_tag_set(root, settag);
701 *first_indexp = index;
702
703 return tagged;
704}
705EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
706
707
708/**
612 * radix_tree_next_hole - find the next hole (not-present entry) 709 * radix_tree_next_hole - find the next hole (not-present entry)
613 * @root: tree root 710 * @root: tree root
614 * @index: index key 711 * @index: index key