diff mbox series

[15/15] btrfs-progs: add new COW rules for extent tree v2

Message ID 94b5b2608259c333dc4d98a8df74d5f9487c5a5a.1646691255.git.josef@toxicpanda.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: initial snapshot_id support | expand

Commit Message

Josef Bacik March 7, 2022, 10:17 p.m. UTC
All of the previous COW rules will apply, but with extent tree v2 we add
a new COW rule where we must COW if the snapshot id of the buffer is
less than the current snapshot id of the root.  If the root's snapshot
id has been increased we know that the block is now shared by a new root
and we must cow this block.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 kernel-shared/ctree.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/kernel-shared/ctree.c b/kernel-shared/ctree.c
index c6ce82b0..215c33ce 100644
--- a/kernel-shared/ctree.c
+++ b/kernel-shared/ctree.c
@@ -390,6 +390,16 @@  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)
@@ -397,7 +407,8 @@  static inline int should_cow_block(struct btrfs_trans_handle *trans,
 	if (btrfs_header_generation(buf) == trans->transid &&
 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
-	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
+	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
+	    !should_cow_block_v2(root, buf))
 		return 0;
 	return 1;
 }