From: Mark Harmstone <mark@harmstone.com>
To: linux-btrfs@vger.kernel.org
Cc: Mark Harmstone <mark@harmstone.com>
Subject: [PATCH v4 16/16] btrfs: add stripe removal pending flag
Date: Fri, 24 Oct 2025 19:12:17 +0100 [thread overview]
Message-ID: <20251024181227.32228-17-mark@harmstone.com> (raw)
In-Reply-To: <20251024181227.32228-1-mark@harmstone.com>
If the filesystem is unmounted while the async discard of a fully remapped
block group is in progress, its unused device extents will never be freed.
To counter this, add a new flag BTRFS_BLOCK_GROUP_STRIPE_REMOVAL_PENDING
to say that this has been interrupted. Set it in the transaction in which
the last identity remap has been removed, clear it when we remove the
device extents, and if we encounter it on mount queue that block group
up for discard.
Signed-off-by: Mark Harmstone <mark@harmstone.com>
---
fs/btrfs/block-group.c | 43 ++++++++++++++++++++++++++++++++-
fs/btrfs/free-space-cache.c | 7 ++++++
fs/btrfs/relocation.c | 18 ++++++++++++++
include/uapi/linux/btrfs_tree.h | 1 +
4 files changed, 68 insertions(+), 1 deletion(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 0c91553b02cf..8eb452068e1f 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -2526,6 +2526,24 @@ static int read_one_block_group(struct btrfs_fs_info *info,
inc_block_group_ro(cache, 1);
}
+ if (cache->flags & BTRFS_BLOCK_GROUP_STRIPE_REMOVAL_PENDING) {
+ spin_lock(&info->unused_bgs_lock);
+
+ if (list_empty(&cache->bg_list)) {
+ btrfs_get_block_group(cache);
+ list_add_tail(&cache->bg_list,
+ &info->fully_remapped_bgs);
+ } else {
+ list_move_tail(&cache->bg_list,
+ &info->fully_remapped_bgs);
+ }
+
+ spin_unlock(&info->unused_bgs_lock);
+
+ if (btrfs_test_opt(info, DISCARD_ASYNC))
+ btrfs_discard_queue_work(&info->discard_ctl, cache);
+ }
+
return 0;
error:
btrfs_put_block_group(cache);
@@ -4833,6 +4851,29 @@ void btrfs_mark_bg_fully_remapped(struct btrfs_block_group *bg,
spin_unlock(&fs_info->unused_bgs_lock);
- if (btrfs_test_opt(fs_info, DISCARD_ASYNC))
+ if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) {
+ bool bg_already_dirty = true;
+
+ spin_lock(&bg->lock);
+ bg->flags |= BTRFS_BLOCK_GROUP_STRIPE_REMOVAL_PENDING;
+ spin_unlock(&bg->lock);
+
+ spin_lock(&trans->transaction->dirty_bgs_lock);
+ if (list_empty(&bg->dirty_list)) {
+ list_add_tail(&bg->dirty_list,
+ &trans->transaction->dirty_bgs);
+ bg_already_dirty = false;
+ btrfs_get_block_group(bg);
+ }
+ spin_unlock(&trans->transaction->dirty_bgs_lock);
+
+ /*
+ * Modified block groups are accounted for in
+ * the delayed_refs_rsv.
+ */
+ if (!bg_already_dirty)
+ btrfs_inc_delayed_refs_rsv_bg_updates(trans->fs_info);
+
btrfs_discard_queue_work(&fs_info->discard_ctl, bg);
+ }
}
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index 5d5e3401e723..60c0df6f002c 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -3068,6 +3068,7 @@ bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group)
bool ret = true;
if (block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED &&
+ !(block_group->flags & BTRFS_BLOCK_GROUP_STRIPE_REMOVAL_PENDING) &&
block_group->identity_remap_count == 0) {
return true;
}
@@ -3847,6 +3848,11 @@ void btrfs_trim_fully_remapped_block_group(struct btrfs_block_group *bg)
struct btrfs_trans_handle *trans;
struct btrfs_chunk_map *map;
+ if (!(bg->flags & BTRFS_BLOCK_GROUP_STRIPE_REMOVAL_PENDING)) {
+ bg->discard_cursor = end;
+ goto skip_discard;
+ }
+
bytes = end - bg->discard_cursor;
if (max_discard_size &&
@@ -3893,6 +3899,7 @@ void btrfs_trim_fully_remapped_block_group(struct btrfs_block_group *bg)
btrfs_free_chunk_map(map);
+skip_discard:
if (bg->used == 0) {
spin_lock(&fs_info->unused_bgs_lock);
list_move_tail(&bg->bg_list, &fs_info->unused_bgs);
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index ebbc619be682..e01ff0174fb1 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -4740,6 +4740,7 @@ int btrfs_last_identity_remap_gone(struct btrfs_trans_handle *trans,
struct btrfs_block_group *bg)
{
int ret;
+ bool bg_already_dirty = true;
BTRFS_PATH_AUTO_FREE(path);
ret = btrfs_remove_dev_extents(trans, chunk);
@@ -4764,6 +4765,23 @@ int btrfs_last_identity_remap_gone(struct btrfs_trans_handle *trans,
btrfs_remove_bg_from_sinfo(bg);
+ spin_lock(&bg->lock);
+ bg->flags &= ~BTRFS_BLOCK_GROUP_STRIPE_REMOVAL_PENDING;
+ spin_unlock(&bg->lock);
+
+ spin_lock(&trans->transaction->dirty_bgs_lock);
+ if (list_empty(&bg->dirty_list)) {
+ list_add_tail(&bg->dirty_list,
+ &trans->transaction->dirty_bgs);
+ bg_already_dirty = false;
+ btrfs_get_block_group(bg);
+ }
+ spin_unlock(&trans->transaction->dirty_bgs_lock);
+
+ /* Modified block groups are accounted for in the delayed_refs_rsv. */
+ if (!bg_already_dirty)
+ btrfs_inc_delayed_refs_rsv_bg_updates(trans->fs_info);
+
path = btrfs_alloc_path();
if (!path)
return -ENOMEM;
diff --git a/include/uapi/linux/btrfs_tree.h b/include/uapi/linux/btrfs_tree.h
index 89bcb80081a6..36a7d1a3cbe3 100644
--- a/include/uapi/linux/btrfs_tree.h
+++ b/include/uapi/linux/btrfs_tree.h
@@ -1173,6 +1173,7 @@ struct btrfs_dev_replace_item {
#define BTRFS_BLOCK_GROUP_RAID1C4 (1ULL << 10)
#define BTRFS_BLOCK_GROUP_REMAPPED (1ULL << 11)
#define BTRFS_BLOCK_GROUP_REMAP (1ULL << 12)
+#define BTRFS_BLOCK_GROUP_STRIPE_REMOVAL_PENDING (1ULL << 13)
#define BTRFS_BLOCK_GROUP_RESERVED (BTRFS_AVAIL_ALLOC_BIT_SINGLE | \
BTRFS_SPACE_INFO_GLOBAL_RSV)
--
2.49.1
prev parent reply other threads:[~2025-10-24 18:12 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-24 18:12 [PATCH v4 00/16] Remap tree Mark Harmstone
2025-10-24 18:12 ` [PATCH v4 01/16] btrfs: add definitions and constants for remap-tree Mark Harmstone
2025-10-31 22:50 ` Boris Burkov
2025-11-03 12:18 ` Mark Harmstone
2025-10-24 18:12 ` [PATCH v4 02/16] btrfs: add REMAP chunk type Mark Harmstone
2025-10-24 18:12 ` [PATCH v4 03/16] btrfs: allow remapped chunks to have zero stripes Mark Harmstone
2025-10-31 21:39 ` Boris Burkov
2025-10-24 18:12 ` [PATCH v4 04/16] btrfs: remove remapped block groups from the free-space tree Mark Harmstone
2025-10-31 21:44 ` Boris Burkov
2025-11-03 12:39 ` Mark Harmstone
2025-10-24 18:12 ` [PATCH v4 05/16] btrfs: don't add metadata items for the remap tree to the extent tree Mark Harmstone
2025-10-24 18:12 ` [PATCH v4 06/16] btrfs: add extended version of struct block_group_item Mark Harmstone
2025-10-31 21:47 ` Boris Burkov
2025-10-24 18:12 ` [PATCH v4 07/16] btrfs: allow mounting filesystems with remap-tree incompat flag Mark Harmstone
2025-10-24 18:12 ` [PATCH v4 08/16] btrfs: redirect I/O for remapped block groups Mark Harmstone
2025-10-31 22:03 ` Boris Burkov
2025-10-24 18:12 ` [PATCH v4 09/16] btrfs: handle deletions from remapped block group Mark Harmstone
2025-10-31 23:05 ` Boris Burkov
2025-11-03 15:51 ` Mark Harmstone
2025-10-31 23:30 ` Boris Burkov
2025-11-04 12:30 ` Mark Harmstone
2025-10-24 18:12 ` [PATCH v4 10/16] btrfs: handle setting up relocation of block group with remap-tree Mark Harmstone
2025-10-31 23:43 ` Boris Burkov
2025-11-03 18:45 ` Mark Harmstone
2025-10-24 18:12 ` [PATCH v4 11/16] btrfs: move existing remaps before relocating block group Mark Harmstone
2025-11-01 0:02 ` Boris Burkov
2025-11-04 13:00 ` Mark Harmstone
2025-11-01 0:10 ` Boris Burkov
2025-10-24 18:12 ` [PATCH v4 12/16] btrfs: replace identity remaps with actual remaps when doing relocations Mark Harmstone
2025-11-01 0:09 ` Boris Burkov
2025-11-04 14:31 ` Mark Harmstone
2025-10-24 18:12 ` [PATCH v4 13/16] btrfs: add do_remap param to btrfs_discard_extent() Mark Harmstone
2025-11-01 0:12 ` Boris Burkov
2025-10-24 18:12 ` [PATCH v4 14/16] btrfs: allow balancing remap tree Mark Harmstone
2025-10-24 18:12 ` [PATCH v4 15/16] btrfs: handle discarding fully-remapped block groups Mark Harmstone
2025-10-27 16:04 ` kernel test robot
2025-10-31 22:12 ` Boris Burkov
2025-11-03 16:49 ` Mark Harmstone
2025-11-09 8:42 ` Philip Li
2025-10-31 22:11 ` Boris Burkov
2025-11-03 17:01 ` Mark Harmstone
2025-10-24 18:12 ` Mark Harmstone [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251024181227.32228-17-mark@harmstone.com \
--to=mark@harmstone.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox