radix-tree: omplement function radix_tree_range_tag_if_tagged
[linux-2.6.git] / lib / radix-tree.c
index 92cdd99..e907858 100644 (file)
@@ -28,7 +28,6 @@
 #include <linux/slab.h>
 #include <linux/notifier.h>
 #include <linux/cpu.h>
-#include <linux/gfp.h>
 #include <linux/string.h>
 #include <linux/bitops.h>
 #include <linux/rcupdate.h>
@@ -364,7 +363,7 @@ static void *radix_tree_lookup_element(struct radix_tree_root *root,
        unsigned int height, shift;
        struct radix_tree_node *node, **slot;
 
-       node = rcu_dereference(root->rnode);
+       node = rcu_dereference_raw(root->rnode);
        if (node == NULL)
                return NULL;
 
@@ -384,7 +383,7 @@ static void *radix_tree_lookup_element(struct radix_tree_root *root,
        do {
                slot = (struct radix_tree_node **)
                        (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
-               node = rcu_dereference(*slot);
+               node = rcu_dereference_raw(*slot);
                if (node == NULL)
                        return NULL;
 
@@ -556,6 +555,10 @@ EXPORT_SYMBOL(radix_tree_tag_clear);
  *
  *  0: tag not present or not set
  *  1: tag set
+ *
+ * Note that the return value of this function may not be relied on, even if
+ * the RCU lock is held, unless tag modification and node deletion are excluded
+ * from concurrency.
  */
 int radix_tree_tag_get(struct radix_tree_root *root,
                        unsigned long index, unsigned int tag)
@@ -568,7 +571,7 @@ int radix_tree_tag_get(struct radix_tree_root *root,
        if (!root_tag_get(root, tag))
                return 0;
 
-       node = rcu_dereference(root->rnode);
+       node = rcu_dereference_raw(root->rnode);
        if (node == NULL)
                return 0;
 
@@ -596,13 +599,9 @@ int radix_tree_tag_get(struct radix_tree_root *root,
                 */
                if (!tag_get(node, tag, offset))
                        saw_unset_tag = 1;
-               if (height == 1) {
-                       int ret = tag_get(node, tag, offset);
-
-                       BUG_ON(ret && saw_unset_tag);
-                       return !!ret;
-               }
-               node = rcu_dereference(node->slots[offset]);
+               if (height == 1)
+                       return !!tag_get(node, tag, offset);
+               node = rcu_dereference_raw(node->slots[offset]);
                shift -= RADIX_TREE_MAP_SHIFT;
                height--;
        }
@@ -610,6 +609,100 @@ int radix_tree_tag_get(struct radix_tree_root *root,
 EXPORT_SYMBOL(radix_tree_tag_get);
 
 /**
+ * radix_tree_range_tag_if_tagged - for each item in given range set given
+ *                                tag if item has another tag set
+ * @root:              radix tree root
+ * @first_indexp:      pointer to a starting index of a range to scan
+ * @last_index:                last index of a range to scan
+ * @nr_to_tag:         maximum number items to tag
+ * @iftag:             tag index to test
+ * @settag:            tag index to set if tested tag is set
+ *
+ * This function scans range of radix tree from first_index to last_index
+ * (inclusive).  For each item in the range if iftag is set, the function sets
+ * also settag. The function stops either after tagging nr_to_tag items or
+ * after reaching last_index.
+ *
+ * The function returns number of leaves where the tag was set and sets
+ * *first_indexp to the first unscanned index.
+ */
+unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
+               unsigned long *first_indexp, unsigned long last_index,
+               unsigned long nr_to_tag,
+               unsigned int iftag, unsigned int settag)
+{
+       unsigned int height = root->height, shift;
+       unsigned long tagged = 0, index = *first_indexp;
+       struct radix_tree_node *open_slots[height], *slot;
+
+       last_index = min(last_index, radix_tree_maxindex(height));
+       if (index > last_index)
+               return 0;
+       if (!nr_to_tag)
+               return 0;
+       if (!root_tag_get(root, iftag)) {
+               *first_indexp = last_index + 1;
+               return 0;
+       }
+       if (height == 0) {
+               *first_indexp = last_index + 1;
+               root_tag_set(root, settag);
+               return 1;
+       }
+
+       shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
+       slot = radix_tree_indirect_to_ptr(root->rnode);
+
+       for (;;) {
+               int offset;
+
+               offset = (index >> shift) & RADIX_TREE_MAP_MASK;
+               if (!slot->slots[offset])
+                       goto next;
+               if (!tag_get(slot, iftag, offset))
+                       goto next;
+               tag_set(slot, settag, offset);
+               if (height == 1) {
+                       tagged++;
+                       goto next;
+               }
+               /* Go down one level */
+               height--;
+               shift -= RADIX_TREE_MAP_SHIFT;
+               open_slots[height] = slot;
+               slot = slot->slots[offset];
+               continue;
+next:
+               /* Go to next item at level determined by 'shift' */
+               index = ((index >> shift) + 1) << shift;
+               if (index > last_index)
+                       break;
+               if (tagged >= nr_to_tag)
+                       break;
+               while (((index >> shift) & RADIX_TREE_MAP_MASK) == 0) {
+                       /*
+                        * We've fully scanned this node. Go up. Because
+                        * last_index is guaranteed to be in the tree, what
+                        * we do below cannot wander astray.
+                        */
+                       slot = open_slots[height];
+                       height++;
+                       shift += RADIX_TREE_MAP_SHIFT;
+               }
+       }
+       /*
+        * The iftag must have been set somewhere because otherwise
+        * we would return immediated at the beginning of the function
+        */
+       root_tag_set(root, settag);
+       *first_indexp = index;
+
+       return tagged;
+}
+EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
+
+
+/**
  *     radix_tree_next_hole    -    find the next hole (not-present entry)
  *     @root:          tree root
  *     @index:         index key
@@ -657,7 +750,7 @@ EXPORT_SYMBOL(radix_tree_next_hole);
  *
  *     Returns: the index of the hole if found, otherwise returns an index
  *     outside of the set specified (in which case 'index - return >= max_scan'
- *     will be true). In rare cases of wrap-around, LONG_MAX will be returned.
+ *     will be true). In rare cases of wrap-around, ULONG_MAX will be returned.
  *
  *     radix_tree_next_hole may be called under rcu_read_lock. However, like
  *     radix_tree_gang_lookup, this will not atomically search a snapshot of
@@ -675,7 +768,7 @@ unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
                if (!radix_tree_lookup(root, index))
                        break;
                index--;
-               if (index == LONG_MAX)
+               if (index == ULONG_MAX)
                        break;
        }
 
@@ -711,7 +804,7 @@ __lookup(struct radix_tree_node *slot, void ***results, unsigned long index,
                }
 
                shift -= RADIX_TREE_MAP_SHIFT;
-               slot = rcu_dereference(slot->slots[i]);
+               slot = rcu_dereference_raw(slot->slots[i]);
                if (slot == NULL)
                        goto out;
        }
@@ -758,7 +851,7 @@ radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
        unsigned long cur_index = first_index;
        unsigned int ret;
 
-       node = rcu_dereference(root->rnode);
+       node = rcu_dereference_raw(root->rnode);
        if (!node)
                return 0;
 
@@ -787,7 +880,7 @@ radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
                        slot = *(((void ***)results)[ret + i]);
                        if (!slot)
                                continue;
-                       results[ret + nr_found] = rcu_dereference(slot);
+                       results[ret + nr_found] = rcu_dereference_raw(slot);
                        nr_found++;
                }
                ret += nr_found;
@@ -826,7 +919,7 @@ radix_tree_gang_lookup_slot(struct radix_tree_root *root, void ***results,
        unsigned long cur_index = first_index;
        unsigned int ret;
 
-       node = rcu_dereference(root->rnode);
+       node = rcu_dereference_raw(root->rnode);
        if (!node)
                return 0;
 
@@ -915,7 +1008,7 @@ __lookup_tag(struct radix_tree_node *slot, void ***results, unsigned long index,
                        }
                }
                shift -= RADIX_TREE_MAP_SHIFT;
-               slot = rcu_dereference(slot->slots[i]);
+               slot = rcu_dereference_raw(slot->slots[i]);
                if (slot == NULL)
                        break;
        }
@@ -951,7 +1044,7 @@ radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
        if (!root_tag_get(root, tag))
                return 0;
 
-       node = rcu_dereference(root->rnode);
+       node = rcu_dereference_raw(root->rnode);
        if (!node)
                return 0;
 
@@ -980,7 +1073,7 @@ radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
                        slot = *(((void ***)results)[ret + i]);
                        if (!slot)
                                continue;
-                       results[ret + nr_found] = rcu_dereference(slot);
+                       results[ret + nr_found] = rcu_dereference_raw(slot);
                        nr_found++;
                }
                ret += nr_found;
@@ -1020,7 +1113,7 @@ radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
        if (!root_tag_get(root, tag))
                return 0;
 
-       node = rcu_dereference(root->rnode);
+       node = rcu_dereference_raw(root->rnode);
        if (!node)
                return 0;