Skip to contentSkip to conversation

btrfs-progs: dump-tree: Support print trees being half dropped

PATCH New Qu Wenruo 1 participant 3 messages Aug 14, 2019 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs-progs: dump-tree: Support print trees being half dropped

For half dropped subvolumes, dump-tree can sometimes lead to csum error if the dropped tree blocks are also trimmed.

E.g
node 33153024 level 1 items 61 free 60 generation 7 owner 256 fs uuid 793daf87-b345-4896-a500-adf19baabd92
chunk uuid 6fcfe63c-3a64-4e78-b095-5980186a0cc0

          key (256 INODE_ITEM 0) block 33173504 gen 7 <<< Dropped
          key (256 DIR_ITEM 266948847) block 30556160 gen 7
          key (256 DIR_ITEM 540403373) block 30658560 gen 7

...
checksum verify failed on 33173504 found 295F0086 wanted 00000000 checksum verify failed on 33173504 found 295F0086 wanted 00000000 checksum verify failed on 33173504 found 295F0086 wanted 00000000 bad tree block 33173504, bytenr mismatch, want=33173504, have=0 failed to read 33173504 in tree 256

This patch will make btrfs ins dump-tree to detect dropped tree blocks and output something like:

node 33153024 level 1 items 61 free 60 generation 7 owner 256 fs uuid 793daf87-b345-4896-a500-adf19baabd92
chunk uuid 6fcfe63c-3a64-4e78-b095-5980186a0cc0

          key (256 INODE_ITEM 0) block 33173504 gen 7 =DROPPED=
          key (256 DIR_ITEM 266948847) block 30556160 gen 7 =DROPPED=
          key (256 DIR_ITEM 540403373) block 30658560 gen 7

...
leaf 30658560 items 56 free space 172 generation 7 owner 256 leaf 30658560 flags 0x1(WRITTEN) backref revision 1
fs uuid 793daf87-b345-4896-a500-adf19baabd92
chunk uuid 6fcfe63c-3a64-4e78-b095-5980186a0cc0
item 0 key (256 DIR_ITEM 540403373) itemoff 3953 itemsize 42
location key (1045 INODE_ITEM 0) type FILE transid 7 data_len 0 name_len 12
name: file_reg_394
...

And skip the dropped tree blocks completely.

Such problem also seems to affect original mode check, when dropped subvolumes triggers false alert for missing backref.
But that problem needs more confirmation, and will be addressed in another patchset.

Qu Wenruo (2):
btrfs-progs: Introduce drop borderline for drop progress
btrfs-progs: print-tree: Skip dropped tree blocks properly

 cmds/inspect-dump-tree.c | 33 ++++++++++++++------
 ctree.c                  | 31 +++++++++++++++++++
 ctree.h                  | 60 +++++++++++++++++++++++++++++++++++
 print-tree.c             | 67 ++++++++++++++++++++++++++++++++--------
 print-tree.h             |  4 ++-
 5 files changed, 171 insertions(+), 24 deletions(-)
Signature · 1 line
2.22.0
author
#1 lore raw

PATCH 1/2 btrfs-progs: Introduce drop borderline for drop progress

Btrfs kernel module drop its subvolume tree using post-order traversal, this sometimes makes things harder to determine if we should print one subtree.

For the full hassle it can lead to, please check the lengthy comment.

This patch will introduce a simple new facility, btrfs_drop_borderline, to address the problem.

Currently the only user of such tree traversal is dump-tree.

---
 ctree.c | 31 +++++++++++++++++++++++++++++
 ctree.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)
ctree.c +31 -0
index c97fbb40bb6e..59d0cb7ab89e 100644
@@ -3233,3 +3233,34 @@ out:
 	btrfs_free_path(path);
 	return ret;
 }
+
+int btrfs_init_drop_borderline(struct btrfs_root *root,
+			       struct btrfs_drop_borderline *borderline)
+{
+	struct btrfs_path path;
+	struct btrfs_key drop_key;
+	int i;
+	int ret;
+
+	memset(borderline, 0, sizeof(*borderline));
+	btrfs_disk_key_to_cpu(&drop_key, &root->root_item.drop_progress);
+	if (is_empty_key(&drop_key) || root->root_item.drop_level == 0)
+		return 0;
+
+	btrfs_init_path(&path);
+	path.lowest_level = root->root_item.drop_level;
+
+	ret = btrfs_search_slot(NULL, root, &drop_key, &path, 0, 0);
+	if (ret < 0)
+		goto out;
+	ret = 0;
+
+	for (i = root->root_item.drop_level; i < BTRFS_MAX_LEVEL; i++) {
+		if (path.nodes[i])
+			btrfs_node_key_to_cpu(path.nodes[i],
+					&borderline->keys[i], path.slots[i]);
+	}
+out:
+	btrfs_release_path(&path);
+	return ret;
+}
ctree.h +60 -0

66 lines not on this page · raw message

Signature · 1 line
2.22.0
author
#2 lore raw

PATCH 2/2 btrfs-progs: print-tree: Skip dropped tree blocks properly

In reply to Qu Wenruo

[BUG]
For certain btrfs image, btrfs ins dump-tree can hit checksum error for tree blocks:

  $ btrfs ins dump-tree -t 256 /dev/test/scratch1 > /dev/null
  checksum verify failed on 33173504 found 295F0086 wanted 00000000
  checksum verify failed on 33173504 found 295F0086 wanted 00000000
  checksum verify failed on 33173504 found 295F0086 wanted 00000000
  bad tree block 33173504, bytenr mismatch, want=33173504, have=0

[CAUSE]
The fs is a completely valid fs, with its file tree 256 being half dropped.

The fs is crafted by using dm-log-writes, with extra kernel hack to commit transaction for each tree block dropped, and mounted with discard option to discard dropped tree blocks.

The cause is pretty simple, btrfs ins dump-tree doesn't really check the drop progress of a tree.
So when above condition is met, dump-tree will still try to print a tree block which is already dropped.

[FIX]
Fix this problem by introducing @borderline parameter for btrfs_print_tree() and its children functions.

---
 cmds/inspect-dump-tree.c | 33 ++++++++++++++------
 print-tree.c             | 67 ++++++++++++++++++++++++++++++++--------
 print-tree.h             |  4 ++-
 3 files changed, 80 insertions(+), 24 deletions(-)
cmds/inspect-dump-tree.c +23 -10
index e50130a4a161..c2145a2559bc 100644
@@ -277,7 +277,7 @@ static int dump_print_tree_blocks(struct btrfs_fs_info *fs_info,
 			ret = -EIO;
 			goto next;
 		}
-		btrfs_print_tree(eb, follow, BTRFS_PRINT_TREE_DEFAULT);
+		btrfs_print_tree(eb, NULL, follow, BTRFS_PRINT_TREE_DEFAULT);
 		free_extent_buffer(eb);
 next:
 		remove_cache_extent(tree, ce);
@@ -486,20 +486,20 @@ static int cmd_inspect_dump_tree(const struct cmd_struct *cmd,
 		} else {
 			if (info->tree_root->node) {
 				printf("root tree\n");
-				btrfs_print_tree(info->tree_root->node, true,
-						 traverse);
+				btrfs_print_tree(info->tree_root->node, NULL,
+						 true, traverse);
 			}
 
 			if (info->chunk_root->node) {
 				printf("chunk tree\n");
-				btrfs_print_tree(info->chunk_root->node, true,
-						 traverse);
+				btrfs_print_tree(info->chunk_root->node, NULL,
+						 true, traverse);
 			}
 
 			if (info->log_root_tree) {
 				printf("log root tree\n");
 				btrfs_print_tree(info->log_root_tree->node,
-						 true, traverse);
+						 NULL, true, traverse);
 			}
 		}
 	}
@@ -519,7 +519,7 @@ again:
 			goto close_root;
 		}
 		printf("root tree\n");
-		btrfs_print_tree(info->tree_root->node, true, traverse);
+		btrfs_print_tree(info->tree_root->node, NULL, true, traverse);
 		goto close_root;
 	}
 
@@ -529,7 +529,7 @@ again:
 			goto close_root;
 		}
 		printf("chunk tree\n");
-		btrfs_print_tree(info->chunk_root->node, true, traverse);
+		btrfs_print_tree(info->chunk_root->node, NULL, true, traverse);
 		goto close_root;
 	}
 
@@ -539,7 +539,7 @@ again:
 			goto close_root;
 		}
 		printf("log root tree\n");
-		btrfs_print_tree(info->log_root_tree->node, true, traverse);
+		btrfs_print_tree(info->log_root_tree->node, NULL, true, traverse);
 		goto close_root;
 	}
 
@@ -566,12 +566,24 @@ again:
 		btrfs_item_key(leaf, &disk_key, path.slots[0]);
 		btrfs_disk_key_to_cpu(&found_key, &disk_key);
 		if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
+			struct btrfs_root *root;
+			struct btrfs_key search_key;
+			struct btrfs_drop_borderline borderline;
 			unsigned long offset;
 			struct extent_buffer *buf;
 			int skip = extent_only | device_only | uuid_tree_only;
 
+			search_key.objectid = found_key.objectid;
+			search_key.type = found_key.type;
+			search_key.offset = -1;
 			offset = btrfs_item_ptr_offset(leaf, slot);
 			read_extent_buffer(leaf, &ri, offset, sizeof(ri));
+			root = btrfs_read_fs_root(info, &search_key);
+			if (!IS_ERR_OR_NULL(root)) {
+				btrfs_init_drop_borderline(root, &borderline);
+			} else {
+				memset(&borderline, 0, sizeof(borderline));
+			}
 			buf = read_tree_block(info, btrfs_root_bytenr(&ri), 0);
 			if (!extent_buffer_uptodate(buf))
 				goto next;
@@ -685,7 +697,8 @@ again:
 					       btrfs_header_level(buf));
 				} else {
 					printf(" \n");
-					btrfs_print_tree(buf, true, traverse);
+					btrfs_print_tree(buf, &borderline,
+							 true, traverse);
 				}
 			}
 			free_extent_buffer(buf);
print-tree.c +54 -13

150 lines not on this page · raw message

print-tree.h +3 -1
index d4721b60647f..5574d9102614 100644
@@ -32,7 +32,9 @@ void btrfs_print_leaf(struct extent_buffer *l);
 #define BTRFS_PRINT_TREE_DFS		0
 #define BTRFS_PRINT_TREE_BFS		1
 #define BTRFS_PRINT_TREE_DEFAULT	BTRFS_PRINT_TREE_DFS
-void btrfs_print_tree(struct extent_buffer *eb, bool follow, int traverse);
+void btrfs_print_tree(struct extent_buffer *eb,
+		      struct btrfs_drop_borderline *borderline,
+		      bool follow, int traverse);
 
 void btrfs_print_key(struct btrfs_disk_key *disk_key);
 void print_chunk_item(struct extent_buffer *eb, struct btrfs_chunk *chunk);
Signature · 1 line
2.22.0