diff mbox series

[RFC,1/2] btrfs: introduce dummy RAID stripes for preallocated data

Message ID 20240918140850.27261-2-jth@kernel.org (mailing list archive)
State New, archived
Headers show
Series Add dummy RAID stripe tree entries for PREALLOC data | expand

Commit Message

Johannes Thumshirn Sept. 18, 2024, 2:08 p.m. UTC
From: Johannes Thumshirn <johannes.thumshirn@wdc.com>

Introduce BTRFS_RAID_STRIPE_DUMMY_KEY which describes a range in the
RAID stripe-tree that is backed by meta-data but no data. For instance
for preallocated extents.

On read, we can simply ignore the contents of the stripe_extent as
we're not interested in the physical address and device id of these
stripe_extents.

Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
 fs/btrfs/raid-stripe-tree.c     | 43 +++++++++++++++++++++++++++++++++
 fs/btrfs/raid-stripe-tree.h     |  2 ++
 include/uapi/linux/btrfs_tree.h |  1 +
 3 files changed, 46 insertions(+)
diff mbox series

Patch

diff --git a/fs/btrfs/raid-stripe-tree.c b/fs/btrfs/raid-stripe-tree.c
index 075fecd08d87..bbe0689b1d17 100644
--- a/fs/btrfs/raid-stripe-tree.c
+++ b/fs/btrfs/raid-stripe-tree.c
@@ -158,6 +158,40 @@  static int update_raid_extent_item(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
+int btrfs_insert_dummy_raid_extent(struct btrfs_trans_handle *trans,
+				   u64 logical, u64 length)
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_key stripe_key;
+	struct btrfs_root *stripe_root = fs_info->stripe_root;
+	struct btrfs_path *path;
+	int ret;
+
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	stripe_key.objectid = logical;
+	stripe_key.type = BTRFS_RAID_STRIPE_DUMMY_KEY;
+	stripe_key.offset = length;
+
+	ret = btrfs_insert_empty_item(trans, stripe_root, path, &stripe_key, 0);
+	if (ret == -EEXIST) {
+		struct extent_buffer *leaf = path->nodes[0];
+		int slot = path->slots[0];
+		struct btrfs_key found_key;
+
+		btrfs_item_key_to_cpu(leaf, &found_key, slot);
+		found_key.objectid = logical;
+		found_key.offset = length;
+		btrfs_mark_buffer_dirty(trans, leaf);
+		ret = 0;
+	}
+	btrfs_free_path(path);
+
+	return ret;
+}
+
 static int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
 					struct btrfs_io_context *bioc)
 {
@@ -305,6 +339,15 @@  int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 	if (end > found_end)
 		*length -= end - found_end;
 
+	/*
+	 * If we have a BTRFS_RAID_STRIPE_DUMMY_KEY it means we've hit
+	 * an entry for a preallocated extent. Noone will ever check
+	 * the physical, only logical and length, so we're good to
+	 * bail out from here.
+	 */
+	if (found_key.type == BTRFS_RAID_STRIPE_DUMMY_KEY)
+		goto out;
+
 	num_stripes = btrfs_num_raid_stripes(btrfs_item_size(leaf, slot));
 	stripe_extent = btrfs_item_ptr(leaf, slot, struct btrfs_stripe_extent);
 
diff --git a/fs/btrfs/raid-stripe-tree.h b/fs/btrfs/raid-stripe-tree.h
index 1ac1c21aac2f..dbe52789f201 100644
--- a/fs/btrfs/raid-stripe-tree.h
+++ b/fs/btrfs/raid-stripe-tree.h
@@ -27,6 +27,8 @@  int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 				 u32 stripe_index, struct btrfs_io_stripe *stripe);
 int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans,
 			     struct btrfs_ordered_extent *ordered_extent);
+int btrfs_insert_dummy_raid_extent(struct btrfs_trans_handle *trans,
+				   u64 logical, u64 length);
 
 static inline bool btrfs_need_stripe_tree_update(struct btrfs_fs_info *fs_info,
 						 u64 map_type)
diff --git a/include/uapi/linux/btrfs_tree.h b/include/uapi/linux/btrfs_tree.h
index fc29d273845d..76b18013b394 100644
--- a/include/uapi/linux/btrfs_tree.h
+++ b/include/uapi/linux/btrfs_tree.h
@@ -281,6 +281,7 @@ 
 #define BTRFS_CHUNK_ITEM_KEY	228
 
 #define BTRFS_RAID_STRIPE_KEY	230
+#define BTRFS_RAID_STRIPE_DUMMY_KEY 231
 
 /*
  * Records the overall state of the qgroups.