Skip to contentSkip to conversation

btrfs: add snapshot_id to btrfs_header and root_item

PATCH New Josef Bacik 1 participant 12 messages Mar 7, 2022 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: add snapshot_id to btrfs_header and root_item

Hello,

This is the next series of changes for extent tree v2. This adds support for the snapshot_id to the btrfs_header and btrfs_root_item. This will allow us to track shared blocks more simply without requiring a lot of reference updates.

There are also a few extra patches tacked onto the end to add some more testing. Since we're changing the size of the btrfs_header I wanted to get as much coverage as possible on the different ways we modify leaves and nodes so I added another selftest and added support for running the selftests with extent tree v2 set.

Thanks,

Josef

Josef Bacik (11):
btrfs: take into account BTRFS_HEADER_FLAG_V2 in the item/node helpers
btrfs: add global_tree_id to btrfs_root_item
btrfs: add snapshot_id to the btrfs_root_item
btrfs: handle the new snapshot_id field properly
btrfs: add a sanity checker for the header flags
btrfs: zero dummy extent buffers
btrfs: zero out temporary superblock buffer header
btrfs: add a debug range check for header_v2
btrfs: selftests: add a test for delete_one_dir_name
btrfs: don't do the global_id thing if we are a DUMMY_FS_INFO
btrfs: selftests: run with EXTENT_TREE_V2 set as well

 fs/btrfs/ctree.c                       |  13 +-
 fs/btrfs/ctree.h                       |  14 +-
 fs/btrfs/disk-io.c                     |   4 +
 fs/btrfs/extent-tree.c                 |   7 +
 fs/btrfs/extent_io.c                   |  22 +++
 fs/btrfs/tests/btrfs-tests.c           |  77 +++++++----
 fs/btrfs/tests/btrfs-tests.h           |  18 ++-
 fs/btrfs/tests/extent-buffer-tests.c   | 181 ++++++++++++++++++++++++-
 fs/btrfs/tests/extent-io-tests.c       |   9 +-
 fs/btrfs/tests/extent-map-tests.c      |   2 +-
 fs/btrfs/tests/free-space-tests.c      |   6 +-
 fs/btrfs/tests/free-space-tree-tests.c |  21 +--
 fs/btrfs/tests/inode-tests.c           |  25 ++--
 fs/btrfs/tests/qgroup-tests.c          |   5 +-
 fs/btrfs/tree-checker.c                |  27 ++++
 fs/btrfs/volumes.c                     |   2 +
 include/uapi/linux/btrfs_tree.h        |  12 +-
 17 files changed, 376 insertions(+), 69 deletions(-)
Signature · 1 line
2.26.3
#1 lore raw

PATCH 1/11 btrfs: take into account BTRFS_HEADER_FLAG_V2 in the item/node helpers

The size of the header is slightly larger for _V2, so return the appropriate offset if the header has _V2 set.

---
 fs/btrfs/ctree.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
fs/btrfs/ctree.h +9 -1
index ec77871ad839..b1cc1d34944a 100644
@@ -2073,6 +2073,9 @@ BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr,
 static inline unsigned long btrfs_node_key_ptr_offset(const struct extent_buffer *eb,
 						      int nr)
 {
+	if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_V2))
+		return offsetof(struct btrfs_node_v2, ptrs) +
+			sizeof(struct btrfs_key_ptr) * nr;
 	return offsetof(struct btrfs_node, ptrs) +
 		sizeof(struct btrfs_key_ptr) * nr;
 }
@@ -2128,6 +2131,9 @@ BTRFS_SETGET_STACK_FUNCS(stack_item_size, struct btrfs_item, size, 32);
 static inline unsigned long btrfs_item_nr_offset(const struct extent_buffer *eb,
 						 int nr)
 {
+	if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_V2))
+		return offsetof(struct btrfs_leaf_v2, items) +
+			sizeof(struct btrfs_item) * nr;
 	return offsetof(struct btrfs_leaf, items) +
 		sizeof(struct btrfs_item) * nr;
 }
@@ -2187,12 +2193,14 @@ static inline void btrfs_set_item_key(struct extent_buffer *eb,
 static inline u32 BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info *info)
 {
 
+	if (btrfs_fs_incompat(info, EXTENT_TREE_V2))
+		return info->nodesize - sizeof(struct btrfs_header_v2);
 	return info->nodesize - sizeof(struct btrfs_header);
 }
 
 static inline unsigned long BTRFS_LEAF_DATA_OFFSET(const struct extent_buffer *leaf)
 {
-	return offsetof(struct btrfs_leaf, items);
+	return btrfs_item_nr_offset(leaf, 0);
 }
 
 static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
Signature · 1 line
2.26.3
#2 lore raw

PATCH 2/11 btrfs: add global_tree_id to btrfs_root_item

In reply to Josef Bacik

This is something Dave asked for in case we want to limit a subvolume to a set of global roots.

---
 fs/btrfs/ctree.h                | 2 ++
 include/uapi/linux/btrfs_tree.h | 6 +++++-
 2 files changed, 7 insertions(+), 1 deletion(-)
fs/btrfs/ctree.h +2 -0
index b1cc1d34944a..222c5dda9079 100644
@@ -2412,6 +2412,8 @@ BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
 			 stransid, 64);
 BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
 			 rtransid, 64);
+BTRFS_SETGET_STACK_FUNCS(root_global_tree_id, struct btrfs_root_item,
+			 global_tree_id, 64);
 
 static inline bool btrfs_root_readonly(const struct btrfs_root *root)
 {
include/uapi/linux/btrfs_tree.h +5 -1
index 92760ea4b448..cd62ca1950cb 100644
@@ -682,7 +682,11 @@ struct btrfs_root_item {
 	struct btrfs_timespec otime;
 	struct btrfs_timespec stime;
 	struct btrfs_timespec rtime;
-	__le64 reserved[8]; /* for future */
+
+	/* If we want to use a specific set of global roots for this root. */
+	__le64 global_tree_id;
+
+	__le64 reserved[7]; /* for future */
 } __attribute__ ((__packed__));
 
 /*
Signature · 1 line
2.26.3
#3 lore raw

PATCH 3/11 btrfs: add snapshot_id to the btrfs_root_item

In reply to Josef Bacik

This will be incremented on every snapshot in order to keep track of which blocks belong to which snapshots.

---
 fs/btrfs/ctree.h                | 2 ++
 include/uapi/linux/btrfs_tree.h | 8 +++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
fs/btrfs/ctree.h +2 -0
index 222c5dda9079..6f446cdf05c2 100644
@@ -2414,6 +2414,8 @@ BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
 			 rtransid, 64);
 BTRFS_SETGET_STACK_FUNCS(root_global_tree_id, struct btrfs_root_item,
 			 global_tree_id, 64);
+BTRFS_SETGET_STACK_FUNCS(root_snapshot_id, struct btrfs_root_item,
+			 snapshot_id, 64);
 
 static inline bool btrfs_root_readonly(const struct btrfs_root *root)
 {
include/uapi/linux/btrfs_tree.h +7 -1
index cd62ca1950cb..91dd7feae9aa 100644
@@ -686,7 +686,13 @@ struct btrfs_root_item {
 	/* If we want to use a specific set of global roots for this root. */
 	__le64 global_tree_id;
 
-	__le64 reserved[7]; /* for future */
+	/*
+	 * The current snapshot id of this root, increases every time we are
+	 * snapshotted.
+	 */
+	__le64 snapshot_id;
+
+	__le64 reserved[6]; /* for future */
 } __attribute__ ((__packed__));
 
 /*
Signature · 1 line
2.26.3
#4 lore raw

PATCH 4/11 btrfs: handle the new snapshot_id field properly

In reply to Josef Bacik

With new blocks we'll be setting the snapshot_id to the root's current snapshot_id. The COW rules are extended to make sure that if the block's snapshot_id is < the root's snapshot_id we know we need to cow the block.

---
 fs/btrfs/ctree.c       | 13 ++++++++++++-
 fs/btrfs/extent-tree.c |  7 +++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
fs/btrfs/ctree.c +12 -1
index 6e8c02eec548..e634eeab89a7 100644
@@ -521,6 +521,16 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
 	return 0;
 }
 
+static inline bool should_cow_block_v2(struct btrfs_root *root,
+				       struct extent_buffer *buf)
+{
+	if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_V2) &&
+	    (btrfs_root_snapshot_id(&root->root_item) >
+	     btrfs_header_snapshot_id(buf)))
+		return true;
+	return false;
+}
+
 static inline int should_cow_block(struct btrfs_trans_handle *trans,
 				   struct btrfs_root *root,
 				   struct extent_buffer *buf)
@@ -546,7 +556,8 @@ static inline int should_cow_block(struct btrfs_trans_handle *trans,
 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
 	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
-	    !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
+	    !test_bit(BTRFS_ROOT_FORCE_COW, &root->state) &&
+	    !should_cow_block_v2(root, buf))
 		return 0;
 	return 1;
 }
fs/btrfs/extent-tree.c +7 -0
index 309d8753bf41..504c6f152780 100644
@@ -4874,6 +4874,13 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 	btrfs_set_header_owner(buf, owner);
 	write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
 	write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
+
+	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
+		btrfs_set_header_snapshot_id(buf,
+				btrfs_root_snapshot_id(&root->root_item));
+		btrfs_set_header_flag(buf, BTRFS_HEADER_FLAG_V2);
+	}
+
 	if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
 		buf->log_index = root->log_transid % 2;
 		/*
Signature · 1 line
2.26.3
#5 lore raw

PATCH 5/11 btrfs: add a sanity checker for the header flags

In reply to Josef Bacik

Make sure that we don't have HEADER_FLAG_V2 set on !V2 file systems, but we do have it set on V2 file systems.

---
 fs/btrfs/tree-checker.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
fs/btrfs/tree-checker.c +27 -0
index f0aabda9fd94..41b7fbf67e97 100644
@@ -1578,6 +1578,27 @@ static int check_inode_ref(struct extent_buffer *leaf,
 	return 0;
 }
 
+static int check_header_flags(struct extent_buffer *buf)
+{
+	struct btrfs_fs_info *fs_info = buf->fs_info;
+
+	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
+		if (unlikely(!btrfs_header_flag(buf, BTRFS_HEADER_FLAG_V2))) {
+			generic_err(buf, 0,
+				"no HEADER_FLAG_V2 set on a V2 file system");
+			return -EUCLEAN;
+		}
+	} else {
+		if (unlikely(btrfs_header_flag(buf, BTRFS_HEADER_FLAG_V2))) {
+			generic_err(buf, 0,
+				"have HEADER_FLAG_V2 set on a !V2 file system");
+			return -EUCLEAN;
+		}
+	}
+
+	return 0;
+}
+
 /*
  * Common point to switch the item-specific validation.
  */
@@ -1644,6 +1665,9 @@ static int check_leaf(struct extent_buffer *leaf, bool check_item_data)
 	u32 nritems = btrfs_header_nritems(leaf);
 	int slot;
 
+	if (unlikely(check_header_flags(leaf)))
+		return -EUCLEAN;
+
 	if (unlikely(btrfs_header_level(leaf) != 0)) {
 		generic_err(leaf, 0,
 			"invalid level for leaf, have %d expect 0",
@@ -1807,6 +1831,9 @@ int btrfs_check_node(struct extent_buffer *node)
 	u64 bytenr;
 	int ret = 0;
 
+	if (unlikely(check_header_flags(node)))
+		return -EUCLEAN;
+
 	if (unlikely(level <= 0 || level >= BTRFS_MAX_LEVEL)) {
 		generic_err(node, 0,
 			"invalid level for node, have %d expect [1, %d]",
Signature · 1 line
2.26.3
#6 lore raw

PATCH 6/11 btrfs: zero dummy extent buffers

In reply to Josef Bacik

Our self tests allocate dummy extent buffers to test a variety of things, however these eb's are very lightly initialized. This causes problems with the extent tree v2 stuff as we will read the header flags and could have the V2 flag set and thus do the wrong thing. Fix this by zero'ing out the header of any dummy extent buffers we allocate.

---
 fs/btrfs/extent_io.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/extent_io.c +1 -0
index 951b2e0e0df1..31309aba3344 100644
@@ -5960,6 +5960,7 @@ struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
 	set_extent_buffer_uptodate(eb);
 	btrfs_set_header_nritems(eb, 0);
 	set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
+	memzero_extent_buffer(eb, 0, sizeof(struct btrfs_header));
 
 	return eb;
 err:
Signature · 1 line
2.26.3
#7 lore raw

PATCH 7/11 btrfs: zero out temporary superblock buffer header

In reply to Josef Bacik

We use HEADER_FLAG_V2 to determine different offsets into the buffer, so we need to make sure that we clear the header of the temporary extent_buffer we allocate to read the super block in, otherwise we could have this flag set and do the wrong math for the different item helpers.

---
 fs/btrfs/volumes.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/volumes.c +2 -0
index 1be7cb2f955f..fc2d3db8e539 100644
@@ -7403,6 +7403,8 @@ int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
 	if (IS_ERR(sb))
 		return PTR_ERR(sb);
 	set_extent_buffer_uptodate(sb);
+	memzero_extent_buffer(sb, 0, sizeof(struct btrfs_header));
+
 	/*
 	 * The sb extent buffer is artificial and just used to read the system array.
 	 * set_extent_buffer_uptodate() call does not properly mark all it's
Signature · 1 line
2.26.3
#8 lore raw

PATCH 8/11 btrfs: add a debug range check for header_v2

In reply to Josef Bacik

There's a few places where we modify the extent buffer data that I missed in my initial conversion. This check helped me catch the places I missed. Simply check to see if the eb has FLAG_V2 set and then make sure we're not overlapping access with the header_v2. This will go away in the future, but is handy while I'm working on the code.

---
 fs/btrfs/extent_io.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
fs/btrfs/extent_io.c +19 -0
index 31309aba3344..88dc53595192 100644
@@ -6736,6 +6736,25 @@ static inline int check_eb_range(const struct extent_buffer *eb,
 	if (unlikely(check_add_overflow(start, len, &offset) || offset > eb->len))
 		return report_eb_range(eb, start, len);
 
+#ifdef CONFIG_BTRFS_DEBUG
+	/*
+	 * Catch places where we may have the wrong header math, this can go
+	 * away later.
+	 */
+	if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_V2)) {
+		if (start >= sizeof(struct btrfs_header_v2))
+			return false;
+		if (start == 0 && len == eb->len)
+			return false;
+		if (start + len > sizeof(struct btrfs_header_v2)) {
+			btrfs_warn(eb->fs_info,
+			   "access overlaps the header_v2 on eb %llu start %lu len %lu",
+			   eb->start, start, len);
+			return true;
+		}
+	}
+#endif
+
 	return false;
 }
 
Signature · 1 line
2.26.3
#9 lore raw

PATCH 9/11 btrfs: selftests: add a test for delete_one_dir_name

In reply to Josef Bacik

Because I can't do simple math I was convinced btrfs_delete_one_dir_name was doing the wrong thing, so I wrote a self test to validate my theory. I turned out to be wrong, but it's a valuable test to have for the future.

---
 fs/btrfs/tests/extent-buffer-tests.c | 170 ++++++++++++++++++++++++++-
 1 file changed, 169 insertions(+), 1 deletion(-)
fs/btrfs/tests/extent-buffer-tests.c +169 -1
index 51a8b075c259..131495ffad12 100644
@@ -8,6 +8,7 @@
 #include "../ctree.h"
 #include "../extent_io.h"
 #include "../disk-io.h"
+#include "../transaction.h"
 
 static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
 {
@@ -210,8 +211,175 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
 	return ret;
 }
 
+static int test_delete_one_dir_name(u32 sectorsize, u32 nodesize)
+{
+	struct btrfs_fs_info *fs_info;
+	struct btrfs_path *path = NULL;
+	struct btrfs_root *root = NULL;
+	struct extent_buffer *eb;
+	struct btrfs_dir_item *dir_item;
+	char *ptr;
+	struct btrfs_trans_handle trans;
+	char name_buf[6];
+	struct btrfs_key key;
+	u32 len = 0;
+	int ret = 0;
+	int i;
+
+	test_msg("running btrfs_delete_one_dir_name tests");
+
+	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
+	if (!fs_info) {
+		test_std_err(TEST_ALLOC_FS_INFO);
+		return -ENOMEM;
+	}
+
+	root = btrfs_alloc_dummy_root(fs_info);
+	if (IS_ERR(root)) {
+		test_std_err(TEST_ALLOC_ROOT);
+		ret = PTR_ERR(root);
+		goto out;
+	}
+
+	path = btrfs_alloc_path();
+	if (!path) {
+		test_std_err(TEST_ALLOC_PATH);
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	path->nodes[0] = eb = alloc_dummy_extent_buffer(fs_info, nodesize);
+	if (!eb) {
+		test_std_err(TEST_ALLOC_EXTENT_BUFFER);
+		ret = -ENOMEM;
+		goto out;
+	}
+	path->slots[0] = 0;
+
+	key.objectid = 0;
+	key.type = BTRFS_DIR_ITEM_KEY;
+	key.offset = 0;
+
+	btrfs_init_dummy_trans(&trans, fs_info);
+
+	/*
+	 * We are going to have 5 names, a, bb, ccc, dddd, eeeee, so the sizes
+	 * are the dir_item + i + 1.
+	 */
+	for (i = 0; i < 5; i++)
+		len += sizeof(struct btrfs_dir_item) + i + 1;
+
+	btrfs_setup_item_for_insert(root, path, &key, len);
+
+	ptr = btrfs_item_ptr(eb, path->slots[0], char);
+	for (i = 0; i < 5; i++) {
+		unsigned long name_ptr;
+
+		memset(name_buf, 'a' + i, i + 1);
+		dir_item = (struct btrfs_dir_item *)ptr;
+		btrfs_set_dir_type(eb, dir_item, i);
+		btrfs_set_dir_data_len(eb, dir_item, 0);
+		btrfs_set_dir_name_len(eb, dir_item, i + 1);
+		name_ptr = (unsigned long)(dir_item + 1);
+		write_extent_buffer(eb, name_buf, name_ptr, i + 1);
+		ptr += sizeof(struct btrfs_dir_item) + i + 1;
+	}
+
+	if (btrfs_item_size(eb, 0) != len) {
+		test_err("invalid len after initial start up");
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Delete the ccc dir_name */
+	ptr = btrfs_item_ptr(eb, path->slots[0], char);
+	for (i = 0; i < 2; i++)
+		ptr += sizeof(struct btrfs_dir_item) + i + 1;
+	dir_item = (struct btrfs_dir_item *)ptr;
+	if (btrfs_dir_type(eb, dir_item) != 2) {
+		test_err("got the wrong dir type???\n");
+		ret = -EINVAL;
+		goto out;
+	}
+
+	ret = btrfs_delete_one_dir_name(&trans, root, path, dir_item);
+	if (ret) {
+		test_err("got %d from btrfs_delete_one_dir_name", ret);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	len -= sizeof(struct btrfs_dir_item) + 3;
+	if (btrfs_item_size(eb, 0) != len) {
+		test_err("invalid len after delete");
+		ret = -EINVAL;
+		goto out;
+	}
+
+	ptr = btrfs_item_ptr(eb, path->slots[0], char);
+	for (i = 0; i < 4; i++) {
+		int real_index = i;
+		int c;
+
+		/*
+		 * We deleted ccc, which was index 2, so increase real_index by
+		 * 1 so we get the right value.
+		 */
+		if (i >= 2)
+			real_index++;
+
+		dir_item = (struct btrfs_dir_item *)ptr;
+		if (btrfs_dir_type(eb, dir_item) != real_index) {
+			test_err("dir item %d is mangled, dir_type is %d expected %d",
+				 i, btrfs_dir_type(eb, dir_item), real_index);
+			ret = -EINVAL;
+			goto out;
+		}
+
+		if (btrfs_dir_data_len(eb, dir_item) != 0) {
+			test_err("dir item %d is mangled, data_len is %d expected 0",
+				 i, btrfs_dir_data_len(eb, dir_item));
+			ret = -EINVAL;
+			goto out;
+		}
+
+		if (btrfs_dir_name_len(eb, dir_item) != real_index + 1) {
+			test_err("dir item %d is mangled, name_len is %d expected %d",
+				 i, btrfs_dir_name_len(eb, dir_item),
+				 real_index + 1);
+			ret = -EINVAL;
+			goto out;
+		}
+
+		read_extent_buffer(eb, name_buf, (unsigned long)(dir_item + 1),
+				   btrfs_dir_name_len(eb, dir_item));
+		for (c = 0; c < real_index + 1; c++) {
+			if (name_buf[c] != 'a' + real_index) {
+				test_err(
+		"dir item name %d is mangled, index is %d val is %c wanted %c",
+					 i, c, name_buf[c], 'a' + real_index);
+				ret = -EINVAL;
+				goto out;
+			}
+		}
+
+		ptr += sizeof(struct btrfs_dir_item) + real_index + 1;
+	}
+out:
+	btrfs_free_path(path);
+	btrfs_free_dummy_root(root);
+	btrfs_free_dummy_fs_info(fs_info);
+	return ret;
+}
+
 int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize)
 {
+	int ret;
+
 	test_msg("running extent buffer operation tests");
-	return test_btrfs_split_item(sectorsize, nodesize);
+	ret = test_btrfs_split_item(sectorsize, nodesize);
+	if (ret)
+		return ret;
+	test_msg("running delete dir name etests");
+	return test_delete_one_dir_name(sectorsize, nodesize);
 }
Signature · 1 line
2.26.3
#10 lore raw

PATCH 10/11 btrfs: don't do the global_id thing if we are a DUMMY_FS_INFO

In reply to Josef Bacik

I want to use extent tree v2 in our self tests to make sure the extent buffer related tests all do the correct thing, which requires setting BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2 on the fs_info. However we won't be having per-block group global roots for the dummy fs_info, so add another check to btrfs_global_root_id to bail if we have DUMMY set.

---
 fs/btrfs/disk-io.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/btrfs/disk-io.c +4 -0
index c4e9d69ed672..00e97156101b 100644
@@ -1308,6 +1308,10 @@ static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr)
 	struct btrfs_block_group *block_group;
 	u64 ret;
 
+#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
+	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
+		return 0;
+#endif
 	if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
 		return 0;
 
Signature · 1 line
2.26.3
#11 lore raw

PATCH 11/11 btrfs: selftests: run with EXTENT_TREE_V2 set as well

In reply to Josef Bacik

With the btrfs header change I wanted to validate that the math worked out properly. To do this I modified the tests to also test with EXTENT_TREE_V2 set and without. This requires passing the incompat flags throughout the stack which is why this looks so churny. Finally with the flag set on the fs_info we can allocate dummy extent buffers with the HEADER_FLAG_V2 set on them to get the correct math.

with '#' will be ignored, and an empty message aborts the commit.

---
 fs/btrfs/extent_io.c                   |  2 +
 fs/btrfs/tests/btrfs-tests.c           | 77 ++++++++++++++++----------
 fs/btrfs/tests/btrfs-tests.h           | 18 +++---
 fs/btrfs/tests/extent-buffer-tests.c   | 19 ++++---
 fs/btrfs/tests/extent-io-tests.c       |  9 +--
 fs/btrfs/tests/extent-map-tests.c      |  2 +-
 fs/btrfs/tests/free-space-tests.c      |  6 +-
 fs/btrfs/tests/free-space-tree-tests.c | 21 ++++---
 fs/btrfs/tests/inode-tests.c           | 25 +++++----
 fs/btrfs/tests/qgroup-tests.c          |  5 +-
 10 files changed, 115 insertions(+), 69 deletions(-)
fs/btrfs/extent_io.c +2 -0
index 88dc53595192..c7088c4ad305 100644
@@ -5961,6 +5961,8 @@ struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
 	btrfs_set_header_nritems(eb, 0);
 	set_bit(EXTENT_BUFFER_UNMAPPED, &eb->bflags);
 	memzero_extent_buffer(eb, 0, sizeof(struct btrfs_header));
+	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
+		btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_V2);
 
 	return eb;
 err:
fs/btrfs/tests/btrfs-tests.c +49 -28

104 lines not on this page · raw message

fs/btrfs/tests/btrfs-tests.h +11 -7
index 7a2d7ffbe30e..5d02bf83f7f3 100644
@@ -30,15 +30,19 @@ extern const char *test_error[];
 struct btrfs_root;
 struct btrfs_trans_handle;
 
-int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize);
-int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize);
-int btrfs_test_extent_io(u32 sectorsize, u32 nodesize);
-int btrfs_test_inodes(u32 sectorsize, u32 nodesize);
-int btrfs_test_qgroups(u32 sectorsize, u32 nodesize);
-int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize);
+int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize,
+					u64 incompat_flags);
+int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize,
+				u64 incompat_flags);
+int btrfs_test_extent_io(u32 sectorsize, u32 nodesize, u64 incompat_flags);
+int btrfs_test_inodes(u32 sectorsize, u32 nodesize, u64 incompat_flags);
+int btrfs_test_qgroups(u32 sectorsize, u32 nodesize, u64 incompat_flags);
+int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize,
+			       u64 incompat_flags);
 int btrfs_test_extent_map(void);
 struct inode *btrfs_new_test_inode(void);
-struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize);
+struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(u32 nodesize, u32 sectorsize,
+						u64 incompat_flags);
 void btrfs_free_dummy_fs_info(struct btrfs_fs_info *fs_info);
 void btrfs_free_dummy_root(struct btrfs_root *root);
 struct btrfs_block_group *
fs/btrfs/tests/extent-buffer-tests.c +12 -7
index 131495ffad12..9765638120a7 100644
@@ -10,7 +10,8 @@
 #include "../disk-io.h"
 #include "../transaction.h"
 
-static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
+static int test_btrfs_split_item(u32 sectorsize, u32 nodesize,
+				 u64 incompat_flags)
 {
 	struct btrfs_fs_info *fs_info;
 	struct btrfs_path *path = NULL;
@@ -28,7 +29,8 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
 
 	test_msg("running btrfs_split_item tests");
 
-	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
+	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize,
+					    incompat_flags);
 	if (!fs_info) {
 		test_std_err(TEST_ALLOC_FS_INFO);
 		return -ENOMEM;
@@ -211,7 +213,8 @@ static int test_btrfs_split_item(u32 sectorsize, u32 nodesize)
 	return ret;
 }
 
-static int test_delete_one_dir_name(u32 sectorsize, u32 nodesize)
+static int test_delete_one_dir_name(u32 sectorsize, u32 nodesize,
+				    u64 incompat_flags)
 {
 	struct btrfs_fs_info *fs_info;
 	struct btrfs_path *path = NULL;
@@ -228,7 +231,8 @@ static int test_delete_one_dir_name(u32 sectorsize, u32 nodesize)
 
 	test_msg("running btrfs_delete_one_dir_name tests");
 
-	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
+	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize,
+					    incompat_flags);
 	if (!fs_info) {
 		test_std_err(TEST_ALLOC_FS_INFO);
 		return -ENOMEM;
@@ -372,14 +376,15 @@ static int test_delete_one_dir_name(u32 sectorsize, u32 nodesize)
 	return ret;
 }
 
-int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize)
+int btrfs_test_extent_buffer_operations(u32 sectorsize, u32 nodesize,
+					u64 incompat_flags)
 {
 	int ret;
 
 	test_msg("running extent buffer operation tests");
-	ret = test_btrfs_split_item(sectorsize, nodesize);
+	ret = test_btrfs_split_item(sectorsize, nodesize, incompat_flags);
 	if (ret)
 		return ret;
 	test_msg("running delete dir name etests");
-	return test_delete_one_dir_name(sectorsize, nodesize);
+	return test_delete_one_dir_name(sectorsize, nodesize, incompat_flags);
 }
fs/btrfs/tests/extent-io-tests.c +5 -4
index a232b15b8021..3c181d26b431 100644
@@ -426,7 +426,7 @@ static int __test_eb_bitmaps(unsigned long *bitmap, struct extent_buffer *eb,
 	return 0;
 }
 
-static int test_eb_bitmaps(u32 sectorsize, u32 nodesize)
+static int test_eb_bitmaps(u32 sectorsize, u32 nodesize, u64 incompat_flags)
 {
 	struct btrfs_fs_info *fs_info;
 	unsigned long *bitmap = NULL;
@@ -435,7 +435,8 @@ static int test_eb_bitmaps(u32 sectorsize, u32 nodesize)
 
 	test_msg("running extent buffer bitmap tests");
 
-	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
+	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize,
+					    incompat_flags);
 	if (!fs_info) {
 		test_std_err(TEST_ALLOC_FS_INFO);
 		return -ENOMEM;
@@ -591,7 +592,7 @@ static int test_find_first_clear_extent_bit(void)
 	return ret;
 }
 
-int btrfs_test_extent_io(u32 sectorsize, u32 nodesize)
+int btrfs_test_extent_io(u32 sectorsize, u32 nodesize, u64 incompat_flags)
 {
 	int ret;
 
@@ -605,7 +606,7 @@ int btrfs_test_extent_io(u32 sectorsize, u32 nodesize)
 	if (ret)
 		goto out;
 
-	ret = test_eb_bitmaps(sectorsize, nodesize);
+	ret = test_eb_bitmaps(sectorsize, nodesize, incompat_flags);
 out:
 	return ret;
 }
fs/btrfs/tests/extent-map-tests.c +1 -1
index c5b3a631bf4f..74aef02664fc 100644
@@ -596,7 +596,7 @@ int btrfs_test_extent_map(void)
 	 * Note: the fs_info is not set up completely, we only need
 	 * fs_info::fsid for the tracepoint.
 	 */
-	fs_info = btrfs_alloc_dummy_fs_info(PAGE_SIZE, PAGE_SIZE);
+	fs_info = btrfs_alloc_dummy_fs_info(PAGE_SIZE, PAGE_SIZE, 0);
 	if (!fs_info) {
 		test_std_err(TEST_ALLOC_FS_INFO);
 		return -ENOMEM;
fs/btrfs/tests/free-space-tests.c +4 -2
index 5930cdcae5cb..f703c9085543 100644
@@ -1002,7 +1002,8 @@ static int test_bytes_index(struct btrfs_block_group *cache, u32 sectorsize)
 	return 0;
 }
 
-int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize)
+int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize,
+				u64 incompat_flags)
 {
 	struct btrfs_fs_info *fs_info;
 	struct btrfs_block_group *cache;
@@ -1010,7 +1011,8 @@ int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize)
 	int ret = -ENOMEM;
 
 	test_msg("running btrfs free space cache tests");
-	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
+	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize,
+					    incompat_flags);
 	if (!fs_info) {
 		test_std_err(TEST_ALLOC_FS_INFO);
 		return -ENOMEM;
fs/btrfs/tests/free-space-tree-tests.c +13 -8
index 13734ed43bfc..a57261136653 100644
@@ -421,7 +421,7 @@ typedef int (*test_func_t)(struct btrfs_trans_handle *,
 			   u32 alignment);
 
 static int run_test(test_func_t test_func, int bitmaps, u32 sectorsize,
-		    u32 nodesize, u32 alignment)
+		    u32 nodesize, u32 alignment, u64 incompat_flags)
 {
 	struct btrfs_fs_info *fs_info;
 	struct btrfs_root *root = NULL;
@@ -430,7 +430,8 @@ static int run_test(test_func_t test_func, int bitmaps, u32 sectorsize,
 	struct btrfs_path *path = NULL;
 	int ret;
 
-	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
+	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize,
+					    incompat_flags);
 	if (!fs_info) {
 		test_std_err(TEST_ALLOC_FS_INFO);
 		ret = -ENOMEM;
@@ -522,12 +523,14 @@ static int run_test(test_func_t test_func, int bitmaps, u32 sectorsize,
 }
 
 static int run_test_both_formats(test_func_t test_func, u32 sectorsize,
-				 u32 nodesize, u32 alignment)
+				 u32 nodesize, u32 alignment,
+				 u64 incompat_flags)
 {
 	int test_ret = 0;
 	int ret;
 
-	ret = run_test(test_func, 0, sectorsize, nodesize, alignment);
+	ret = run_test(test_func, 0, sectorsize, nodesize, alignment,
+		       incompat_flags);
 	if (ret) {
 		test_err(
 	"%ps failed with extents, sectorsize=%u, nodesize=%u, alignment=%u",
@@ -535,7 +538,8 @@ static int run_test_both_formats(test_func_t test_func, u32 sectorsize,
 		test_ret = ret;
 	}
 
-	ret = run_test(test_func, 1, sectorsize, nodesize, alignment);
+	ret = run_test(test_func, 1, sectorsize, nodesize, alignment,
+		       incompat_flags);
 	if (ret) {
 		test_err(
 	"%ps failed with bitmaps, sectorsize=%u, nodesize=%u, alignment=%u",
@@ -546,7 +550,8 @@ static int run_test_both_formats(test_func_t test_func, u32 sectorsize,
 	return test_ret;
 }
 
-int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize)
+int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize,
+			       u64 incompat_flags)
 {
 	test_func_t tests[] = {
 		test_empty_block_group,
@@ -574,12 +579,12 @@ int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize)
 		int ret;
 
 		ret = run_test_both_formats(tests[i], sectorsize, nodesize,
-					    sectorsize);
+					    sectorsize, incompat_flags);
 		if (ret)
 			test_ret = ret;
 
 		ret = run_test_both_formats(tests[i], sectorsize, nodesize,
-					    bitmap_alignment);
+					    bitmap_alignment, incompat_flags);
 		if (ret)
 			test_ret = ret;
 	}
fs/btrfs/tests/inode-tests.c +15 -10
index cac89c388131..df960a0f96ae 100644
@@ -213,7 +213,8 @@ static unsigned long prealloc_only = 0;
 static unsigned long compressed_only = 0;
 static unsigned long vacancy_only = 0;
 
-static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
+static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize,
+					  u64 incompat_flags)
 {
 	struct btrfs_fs_info *fs_info = NULL;
 	struct inode *inode = NULL;
@@ -232,7 +233,8 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
 		return ret;
 	}
 
-	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
+	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize,
+					    incompat_flags);
 	if (!fs_info) {
 		test_std_err(TEST_ALLOC_FS_INFO);
 		goto out;
@@ -814,7 +816,7 @@ static noinline int test_btrfs_get_extent(u32 sectorsize, u32 nodesize)
 	return ret;
 }
 
-static int test_hole_first(u32 sectorsize, u32 nodesize)
+static int test_hole_first(u32 sectorsize, u32 nodesize, u64 incompat_flags)
 {
 	struct btrfs_fs_info *fs_info = NULL;
 	struct inode *inode = NULL;
@@ -830,7 +832,8 @@ static int test_hole_first(u32 sectorsize, u32 nodesize)
 		return ret;
 	}
 
-	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
+	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize,
+					    incompat_flags);
 	if (!fs_info) {
 		test_std_err(TEST_ALLOC_FS_INFO);
 		goto out;
@@ -912,7 +915,8 @@ static int test_hole_first(u32 sectorsize, u32 nodesize)
 	return ret;
 }
 
-static int test_extent_accounting(u32 sectorsize, u32 nodesize)
+static int test_extent_accounting(u32 sectorsize, u32 nodesize,
+				  u64 incompat_flags)
 {
 	struct btrfs_fs_info *fs_info = NULL;
 	struct inode *inode = NULL;
@@ -927,7 +931,8 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
 		return ret;
 	}
 
-	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
+	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize,
+					    incompat_flags);
 	if (!fs_info) {
 		test_std_err(TEST_ALLOC_FS_INFO);
 		goto out;
@@ -1099,7 +1104,7 @@ static int test_extent_accounting(u32 sectorsize, u32 nodesize)
 	return ret;
 }
 
-int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
+int btrfs_test_inodes(u32 sectorsize, u32 nodesize, u64 incompat_flags)
 {
 	int ret;
 
@@ -1108,11 +1113,11 @@ int btrfs_test_inodes(u32 sectorsize, u32 nodesize)
 	set_bit(EXTENT_FLAG_COMPRESSED, &compressed_only);
 	set_bit(EXTENT_FLAG_PREALLOC, &prealloc_only);
 
-	ret = test_btrfs_get_extent(sectorsize, nodesize);
+	ret = test_btrfs_get_extent(sectorsize, nodesize, incompat_flags);
 	if (ret)
 		return ret;
-	ret = test_hole_first(sectorsize, nodesize);
+	ret = test_hole_first(sectorsize, nodesize, incompat_flags);
 	if (ret)
 		return ret;
-	return test_extent_accounting(sectorsize, nodesize);
+	return test_extent_accounting(sectorsize, nodesize, incompat_flags);
 }
fs/btrfs/tests/qgroup-tests.c +3 -2
index eee1e4459541..ef42662b2769 100644
@@ -434,14 +434,15 @@ static int test_multiple_refs(struct btrfs_root *root,
 	return 0;
 }
 
-int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
+int btrfs_test_qgroups(u32 sectorsize, u32 nodesize, u64 incompat_flags)
 {
 	struct btrfs_fs_info *fs_info = NULL;
 	struct btrfs_root *root;
 	struct btrfs_root *tmp_root;
 	int ret = 0;
 
-	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
+	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize,
+					    incompat_flags);
 	if (!fs_info) {
 		test_std_err(TEST_ALLOC_FS_INFO);
 		return -ENOMEM;
Signature · 1 line
2.26.3