dm persistent data: add btree_walk
authorJoe Thornber <ejt@redhat.com>
Fri, 1 Mar 2013 22:45:50 +0000 (22:45 +0000)
committerAlasdair G Kergon <agk@redhat.com>
Fri, 1 Mar 2013 22:45:50 +0000 (22:45 +0000)
Add dm_btree_walk to iterate through the contents of a btree.
This will be used by the dm cache target.

Signed-off-by: Joe Thornber <ejt@redhat.com>
Signed-off-by: Alasdair G Kergon <agk@redhat.com>
drivers/md/persistent-data/dm-btree-internal.h
drivers/md/persistent-data/dm-btree-spine.c
drivers/md/persistent-data/dm-btree.c
drivers/md/persistent-data/dm-btree.h

index accbb05f17b628456231d94e8a4a880862f980dd..37d367bb9aa8976653d5bbc80b29d0dfbf037372 100644 (file)
@@ -64,6 +64,7 @@ struct ro_spine {
 void init_ro_spine(struct ro_spine *s, struct dm_btree_info *info);
 int exit_ro_spine(struct ro_spine *s);
 int ro_step(struct ro_spine *s, dm_block_t new_child);
+void ro_pop(struct ro_spine *s);
 struct btree_node *ro_node(struct ro_spine *s);
 
 struct shadow_spine {
index f199a0c4ed04ad2cbb263c73c9f61be6e69b46b5..cf9fd676ae444ad29f8fb6f6ef58d85706438092 100644 (file)
@@ -164,6 +164,13 @@ int ro_step(struct ro_spine *s, dm_block_t new_child)
        return r;
 }
 
+void ro_pop(struct ro_spine *s)
+{
+       BUG_ON(!s->count);
+       --s->count;
+       unlock_block(s->info, s->nodes[s->count]);
+}
+
 struct btree_node *ro_node(struct ro_spine *s)
 {
        struct dm_block *block;
index 4caf66918cdb35249544fdf296dd07d2594ae63a..35865425e4b4443e925014fd918655ef51c07d57 100644 (file)
@@ -807,3 +807,55 @@ int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
        return r ? r : count;
 }
 EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);
+
+/*
+ * FIXME: We shouldn't use a recursive algorithm when we have limited stack
+ * space.  Also this only works for single level trees.
+ */
+static int walk_node(struct ro_spine *s, dm_block_t block,
+                    int (*fn)(void *context, uint64_t *keys, void *leaf),
+                    void *context)
+{
+       int r;
+       unsigned i, nr;
+       struct btree_node *n;
+       uint64_t keys;
+
+       r = ro_step(s, block);
+       n = ro_node(s);
+
+       nr = le32_to_cpu(n->header.nr_entries);
+       for (i = 0; i < nr; i++) {
+               if (le32_to_cpu(n->header.flags) & INTERNAL_NODE) {
+                       r = walk_node(s, value64(n, i), fn, context);
+                       if (r)
+                               goto out;
+               } else {
+                       keys = le64_to_cpu(*key_ptr(n, i));
+                       r = fn(context, &keys, value_ptr(n, i));
+                       if (r)
+                               goto out;
+               }
+       }
+
+out:
+       ro_pop(s);
+       return r;
+}
+
+int dm_btree_walk(struct dm_btree_info *info, dm_block_t root,
+                 int (*fn)(void *context, uint64_t *keys, void *leaf),
+                 void *context)
+{
+       int r;
+       struct ro_spine spine;
+
+       BUG_ON(info->levels > 1);
+
+       init_ro_spine(&spine, info);
+       r = walk_node(&spine, root, fn, context);
+       exit_ro_spine(&spine);
+
+       return r;
+}
+EXPORT_SYMBOL_GPL(dm_btree_walk);
index fced8316bca23ad1a3ae4902ae8d9062eef28a19..8672d159e0b5e3fc01641f7524ae3c61df04ed1c 100644 (file)
@@ -142,4 +142,13 @@ int dm_btree_remove(struct dm_btree_info *info, dm_block_t root,
 int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
                              uint64_t *result_keys);
 
+/*
+ * Iterate through the a btree, calling fn() on each entry.
+ * It only works for single level trees and is internally recursive, so
+ * monitor stack usage carefully.
+ */
+int dm_btree_walk(struct dm_btree_info *info, dm_block_t root,
+                 int (*fn)(void *context, uint64_t *keys, void *leaf),
+                 void *context);
+
 #endif /* _LINUX_DM_BTREE_H */