btrfs: delete stripe extent on extent deletion
authorJohannes Thumshirn <johannes.thumshirn@wdc.com>
Thu, 14 Sep 2023 16:06:59 +0000 (09:06 -0700)
committerDavid Sterba <dsterba@suse.com>
Thu, 12 Oct 2023 14:44:09 +0000 (16:44 +0200)
As each stripe extent is tied to an extent item, delete the stripe extent
once the corresponding extent item is deleted.

Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/extent-tree.c
fs/btrfs/raid-stripe-tree.c
fs/btrfs/raid-stripe-tree.h

index 31aae12619b71f0e2362453172bc5f78c95494cd..09e2b5522922c1b23fa934b0a2445d39dd0e6940 100644 (file)
@@ -2857,6 +2857,12 @@ static int do_free_extent_accounting(struct btrfs_trans_handle *trans,
                        btrfs_abort_transaction(trans, ret);
                        return ret;
                }
+
+               ret = btrfs_delete_raid_extent(trans, bytenr, num_bytes);
+               if (ret) {
+                       btrfs_abort_transaction(trans, ret);
+                       return ret;
+               }
        }
 
        ret = add_to_free_space_tree(trans, bytenr, num_bytes);
index c093e0bbb7be35b11e83e32e4595bad8247a894c..f2e052dfb0a5d9846e6ec559e48300a386323d19 100644 (file)
 #include "misc.h"
 #include "print-tree.h"
 
+int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 length)
+{
+       struct btrfs_fs_info *fs_info = trans->fs_info;
+       struct btrfs_root *stripe_root = fs_info->stripe_root;
+       struct btrfs_path *path;
+       struct btrfs_key key;
+       struct extent_buffer *leaf;
+       u64 found_start;
+       u64 found_end;
+       u64 end = start + length;
+       int slot;
+       int ret;
+
+       if (!stripe_root)
+               return 0;
+
+       path = btrfs_alloc_path();
+       if (!path)
+               return -ENOMEM;
+
+       while (1) {
+               key.objectid = start;
+               key.type = BTRFS_RAID_STRIPE_KEY;
+               key.offset = length;
+
+               ret = btrfs_search_slot(trans, stripe_root, &key, path, -1, 1);
+               if (ret < 0)
+                       break;
+               if (ret > 0) {
+                       ret = 0;
+                       if (path->slots[0] == 0)
+                               break;
+                       path->slots[0]--;
+               }
+
+               leaf = path->nodes[0];
+               slot = path->slots[0];
+               btrfs_item_key_to_cpu(leaf, &key, slot);
+               found_start = key.objectid;
+               found_end = found_start + key.offset;
+
+               /* That stripe ends before we start, we're done. */
+               if (found_end <= start)
+                       break;
+
+               ASSERT(found_start >= start && found_end <= end);
+               ret = btrfs_del_item(trans, stripe_root, path);
+               if (ret)
+                       break;
+
+               btrfs_release_path(path);
+       }
+
+       btrfs_free_path(path);
+       return ret;
+}
+
 static int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
                                        struct btrfs_io_context *bioc)
 {
index 7a169e75ad6df9259d70319c9ea9e0240b698766..9476131a64972d79d40e4bd5cd9721bf0154866e 100644 (file)
@@ -11,6 +11,7 @@ struct btrfs_io_stripe;
 struct btrfs_ordered_extent;
 struct btrfs_trans_handle;
 
+int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 length);
 int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans,
                             struct btrfs_ordered_extent *ordered_extent);