diff mbox

[v2,06/10] btrfs-progs: Add better search generation judgment for btrfs-find-root.

Message ID 1421649912-14539-7-git-send-email-quwenruo@cn.fujitsu.com (mailing list archive)
State Not Applicable
Headers show

Commit Message

Qu Wenruo Jan. 19, 2015, 6:45 a.m. UTC
Before the patch, btrfs-find-root will only consider it find a good root
if its generation matches generation in superblock and its level is
currently found highest level.

But that's not correct in 2 ways.
1) Root with decreased level
Since tree level can decrease, like subvolume/file deletion.
Which will make the new root have higher generation but lower level.

2) Root not updated in latest transaction.
If there is some root not updated in latest transaction, its generation
will be smaller than the one in superblock, and btrfs-find-root will not
find it.

This patch will use different generation for different tree to search,
solving the above problems.

Currently, it only supports generation/level in superblock. Using tree
root level/generation if possible will be introduced later.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 btrfs-find-root.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 57 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/btrfs-find-root.c b/btrfs-find-root.c
index bc2b344..45c48c2 100644
--- a/btrfs-find-root.c
+++ b/btrfs-find-root.c
@@ -278,6 +278,61 @@  static int find_root(struct btrfs_root *root)
 	return ret;
 }
 
+/*
+ * Get reliable generation and level for given root.
+ *
+ * We have two sources of gen/level: superblock and tree root.
+ * superblock include the following level:
+ *   Root, chunk, log
+ * and the following generations:
+ *   Root, chunk, uuid
+ * Other gen/leven can only be read from its btrfs_tree_root if possible.
+ *
+ * Currently we only believe things from superblock.
+ */
+static void get_root_gen_and_level(u64 objectid, struct btrfs_fs_info *fs_info,
+				   u64 *ret_gen, u8 *ret_level)
+{
+	struct btrfs_super_block *super = fs_info->super_copy;
+	u64 gen = (u64)-1;
+	u8 level = (u8)-1;
+
+	switch (objectid) {
+	case BTRFS_ROOT_TREE_OBJECTID:
+		level = btrfs_super_root_level(super);
+		gen = btrfs_super_generation(super);
+		break;
+	case BTRFS_CHUNK_TREE_OBJECTID:
+		level = btrfs_super_chunk_root_level(super);
+		gen = btrfs_super_chunk_root_generation(super);
+		printf("Search for chunk root is not supported yet\n");
+		break;
+	case BTRFS_TREE_LOG_OBJECTID:
+		level = btrfs_super_log_root_level(super);
+		gen = btrfs_super_log_root_transid(super);
+		break;
+	case BTRFS_UUID_TREE_OBJECTID:
+		gen = btrfs_super_uuid_tree_generation(super);
+		break;
+	}
+	if (gen != (u64)-1) {
+		printf("Superblock thinks the generation is %llu\n", gen);
+		if (ret_gen)
+			*ret_gen = gen;
+	} else {
+		printf("Superblock doesn't contain generation info for root %llu\n",
+		       objectid);
+	}
+	if (level != (u8)-1) {
+		printf("Superblock thinks the level is %u\n", level);
+		if (ret_level)
+			*ret_level = level;
+	} else {
+		printf("Superblock doesn't contain the level info for root %llu\n",
+		       objectid);
+	}
+}
+
 int main(int argc, char **argv)
 {
 	struct btrfs_root *root;
@@ -316,7 +371,8 @@  int main(int argc, char **argv)
 	}
 
 	if (search_generation == 0)
-		search_generation = btrfs_super_generation(root->fs_info->super_copy);
+		get_root_gen_and_level(search_objectid, root->fs_info,
+				       &search_generation, NULL);
 
 	csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
 	ret = find_root(root);