Skip to contentSkip to conversation

btrfs: stripe-tree: correctly truncate stripe extents on delete

PATCH In review Johannes Thumshirn 3 participants 3 messages Sep 11, 2024 – Sep 26, 2024 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 btrfs: stripe-tree: correctly truncate stripe extents on delete

From: Johannes Thumshirn <[email protected]>

In our CI system, we're seeing the following ASSERT()ion to trigger when running RAID stripe-tree tests on non-zoned devices:

assertion failed: found_start >= start && found_end <= end, in fs/btrfs/raid-stripe-tree.c:64

This ASSERT()ion triggers, because for the initial design of RAID stripe-tree, I had the "one ordered-extent equals one bio" rule of zoned btrfs in mind.

But for a RAID stripe-tree based system, that is not hosted on a zoned storage device, but on a regular device this rule doesn't apply.

So in case the range we want to delete starts in the middle of the previous item, grab the item and "truncate" it's length. That is, subtract the deleted portion from the key's offset.

In case the range to delete ends in the middle of an item, we have to adjust both the item's key as well as the stripe extents.

---

Changes to v1:
- ASSERT() that slot > 0 before calling btrfs_previous_item()

 fs/btrfs/raid-stripe-tree.c | 52 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)
fs/btrfs/raid-stripe-tree.c +51 -1
index 4c859b550f6c..075fecd08d87 100644
@@ -61,7 +61,57 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le
 		trace_btrfs_raid_extent_delete(fs_info, start, end,
 					       found_start, found_end);
 
-		ASSERT(found_start >= start && found_end <= end);
+		if (found_start < start) {
+			struct btrfs_key prev;
+			u64 diff = start - found_start;
+
+			ASSERT(slot > 0);
+
+			ret = btrfs_previous_item(stripe_root, path, start,
+						  BTRFS_RAID_STRIPE_KEY);
+			leaf = path->nodes[0];
+			slot = path->slots[0];
+			btrfs_item_key_to_cpu(leaf, &prev, slot);
+			prev.offset -= diff;
+
+			btrfs_mark_buffer_dirty(trans, leaf);
+
+			start += diff;
+			length -= diff;
+
+			btrfs_release_path(path);
+			continue;
+		}
+
+		if (end < found_end && found_end - end < key.offset) {
+			struct btrfs_stripe_extent *stripe_extent;
+			u64 diff = key.offset - length;
+			int num_stripes;
+
+			num_stripes = btrfs_num_raid_stripes(
+				btrfs_item_size(leaf, slot));
+			stripe_extent = btrfs_item_ptr(
+				leaf, slot, struct btrfs_stripe_extent);
+
+			for (int i = 0; i < num_stripes; i++) {
+				struct btrfs_raid_stride *stride =
+					&stripe_extent->strides[i];
+				u64 physical = btrfs_raid_stride_physical(
+					leaf, stride);
+
+				physical += diff;
+				btrfs_set_raid_stride_physical(leaf, stride,
+							       physical);
+			}
+
+			key.objectid += diff;
+			key.offset -= diff;
+
+			btrfs_mark_buffer_dirty(trans, leaf);
+			btrfs_release_path(path);
+			break;
+		}
+
 		ret = btrfs_del_item(trans, stripe_root, path);
 		if (ret)
 			break;
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 73 lines · Johannes Thumshirn

From: Johannes Thumshirn <[email protected]>

In our CI system, we're seeing the following ASSERT()ion to trigger when running RAID stripe-tree tests on non-zoned devices:

assertion failed: found_start >= start && found_end <= end, in fs/btrfs/raid-stripe-tree.c:64

This ASSERT()ion triggers, because for the initial design of RAID stripe-tree, I had the "one ordered-extent equals one bio" rule of zoned btrfs in mind.

But for a RAID stripe-tree based system, that is not hosted on a zoned storage device, but on a regular device this rule doesn't apply.

So in case the range we want to delete starts in the middle of the previous item, grab the item and "truncate" it's length. That is, subtract the deleted portion from the key's offset.

In case the range to delete ends in the middle of an item, we have to adjust both the item's key as well as the stripe extents.

---

Changes to v1:
- ASSERT() that slot > 0 before calling btrfs_previous_item()

 fs/btrfs/raid-stripe-tree.c | 52 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)
fs/btrfs/raid-stripe-tree.c +35 -1
index 4c859b550f6c..075fecd08d87 100644
@@ -61,7 +61,57 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le
 		trace_btrfs_raid_extent_delete(fs_info, start, end,
 					       found_start, found_end);
 
-		ASSERT(found_start >= start && found_end <= end);
+		if (found_start < start) {
+			struct btrfs_key prev;
+			u64 diff = start - found_start;
+
+			ASSERT(slot > 0);
+
+			ret = btrfs_previous_item(stripe_root, path, start,
+						  BTRFS_RAID_STRIPE_KEY);
+			leaf = path->nodes[0];
+			slot = path->slots[0];
+			btrfs_item_key_to_cpu(leaf, &prev, slot);
+			prev.offset -= diff;
+
+			btrfs_mark_buffer_dirty(trans, leaf);
+
+			start += diff;
+			length -= diff;
+
+			btrfs_release_path(path);
+			continue;
+		}
+
+		if (end < found_end && found_end - end < key.offset) {
+			struct btrfs_stripe_extent *stripe_extent;
+			u64 diff = key.offset - length;
+			int num_stripes;
+
+			num_stripes = btrfs_num_raid_stripes(
+				btrfs_item_size(leaf, slot));
+			stripe_extent = btrfs_item_ptr(
+				leaf, slot, struct btrfs_stripe_extent);
+
+			for (int i = 0; i < num_stripes; i++) {
+				struct btrfs_raid_stride *stride =
+					&stripe_extent->strides[i];

fs/btrfs/raid-stripe-tree.c int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le · line 108

Quoted 10 lines
fs/btrfs/raid-stripe-tree.c +10 -0
+				u64 physical = btrfs_raid_stride_physical(
+					leaf, stride);
+
+				physical += diff;
+				btrfs_set_raid_stride_physical(leaf, stride,
+							       physical);
+			}
+
+			key.objectid += diff;
+			key.offset -= diff;

This part was confusing and isn't necessary, you can drop this bit and then add

Thanks,

Josef

#2 lore raw
In reply to Johannes Thumshirn
Quoted 35 lines · Johannes Thumshirn

From: Johannes Thumshirn <[email protected]>

In our CI system, we're seeing the following ASSERT()ion to trigger when running RAID stripe-tree tests on non-zoned devices:

assertion failed: found_start >= start && found_end <= end, in fs/btrfs/raid-stripe-tree.c:64

This ASSERT()ion triggers, because for the initial design of RAID stripe-tree, I had the "one ordered-extent equals one bio" rule of zoned btrfs in mind.

But for a RAID stripe-tree based system, that is not hosted on a zoned storage device, but on a regular device this rule doesn't apply.

So in case the range we want to delete starts in the middle of the previous item, grab the item and "truncate" it's length. That is, subtract the deleted portion from the key's offset.

In case the range to delete ends in the middle of an item, we have to adjust both the item's key as well as the stripe extents.

---

Changes to v1:
- ASSERT() that slot > 0 before calling btrfs_previous_item()

 fs/btrfs/raid-stripe-tree.c | 52 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)
fs/btrfs/raid-stripe-tree.c +0 -0
index 4c859b550f6c..075fecd08d87 100644

fs/btrfs/raid-stripe-tree.c int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le · line 64

Quoted 4 lines
fs/btrfs/raid-stripe-tree.c +0 -1
@@ -61,7 +61,57 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le
                trace_btrfs_raid_extent_delete(fs_info, start, end,
                                               found_start, found_end);
 
-               ASSERT(found_start >= start && found_end <= end);

I was looking at this in for-next, and several questions popped in my mind, see below.

fs/btrfs/raid-stripe-tree.c int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le · line 71

Quoted 8 lines
fs/btrfs/raid-stripe-tree.c +8 -0
+               if (found_start < start) {
+                       struct btrfs_key prev;
+                       u64 diff = start - found_start;
+
+                       ASSERT(slot > 0);
+
+                       ret = btrfs_previous_item(stripe_root, path, start,
+                                                 BTRFS_RAID_STRIPE_KEY);

This doesn't do anything, we ignore the return value and then the "continue" below makes us go to the start of the loop and do a btrfs_search_slot(), without using the leaf (see below as well).

fs/btrfs/raid-stripe-tree.c int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le · line 75

Quoted 4 lines
fs/btrfs/raid-stripe-tree.c +4 -0
+                       leaf = path->nodes[0];
+                       slot = path->slots[0];
+                       btrfs_item_key_to_cpu(leaf, &prev, slot);
+                       prev.offset -= diff;

Why do we decrement prom prev.offset?
It's a local variable and then we don't use it anymore.

I.e. these 4 lines can be removed.

fs/btrfs/raid-stripe-tree.c int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le · line 77

Quoted 2 lines
fs/btrfs/raid-stripe-tree.c +2 -0
+
+                       btrfs_mark_buffer_dirty(trans, leaf);

Why do we mark the leaf as dirty?
We haven't changed it

fs/btrfs/raid-stripe-tree.c int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le · line 82

Quoted 5 lines
fs/btrfs/raid-stripe-tree.c +5 -0
+
+                       start += diff;
+                       length -= diff;
+
+                       btrfs_release_path(path);

We don't use the path for anything, we extract the key, don't do anything with it and then release the path.

Or did I miss something?

Thanks.

Quoted 39 lines
fs/btrfs/raid-stripe-tree.c +32 -1
+                       continue;
+               }
+
+               if (end < found_end && found_end - end < key.offset) {
+                       struct btrfs_stripe_extent *stripe_extent;
+                       u64 diff = key.offset - length;
+                       int num_stripes;
+
+                       num_stripes = btrfs_num_raid_stripes(
+                               btrfs_item_size(leaf, slot));
+                       stripe_extent = btrfs_item_ptr(
+                               leaf, slot, struct btrfs_stripe_extent);
+
+                       for (int i = 0; i < num_stripes; i++) {
+                               struct btrfs_raid_stride *stride =
+                                       &stripe_extent->strides[i];
+                               u64 physical = btrfs_raid_stride_physical(
+                                       leaf, stride);
+
+                               physical += diff;
+                               btrfs_set_raid_stride_physical(leaf, stride,
+                                                              physical);
+                       }
+
+                       key.objectid += diff;
+                       key.offset -= diff;
+
+                       btrfs_mark_buffer_dirty(trans, leaf);
+                       btrfs_release_path(path);
+                       break;
+               }
+
                ret = btrfs_del_item(trans, stripe_root, path);
                if (ret)
                        break;
--

2.43.0