mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Luca Stefani via B4 Relay <devnull+luca.stefani.ge1.gmail.com@kernel.org>
To: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
	 Luca Stefani <luca.stefani.ge1@gmail.com>
Cc: Robbie Ko <robbieko@synology.com>,
	Filipe Manana <fdmanana@suse.com>,
	 linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] btrfs: account partially completed discards
Date: Wed, 28 Oct 2026 18:55:49 +0100	[thread overview]
Message-ID: <20261028-b4-btrfs-fitrim-interruption-v1-1-bf5c53738b3a@gmail.com> (raw)
In-Reply-To: <20261028-b4-btrfs-fitrim-interruption-v1-0-bf5c53738b3a@gmail.com>

From: Luca Stefani <luca.stefani.ge1@gmail.com>

A discard can complete part of a range before a later chunk is interrupted
or fails. btrfs_issue_discard() reports the bytes already discarded, but
its callers only accumulate those bytes when the entire operation succeeds.

Link: https://lore.kernel.org/20261028-btrfs-fitrim-freezer-v1-1-0ad1d58aa94d@zliang.me/
Signed-off-by: Luca Stefani <luca.stefani.ge1@gmail.com>
---
 fs/btrfs/extent-tree.c      | 5 ++---
 fs/btrfs/free-space-cache.c | 5 ++---
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 9b0a5c6b96f3..242db255cf1c 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -1463,6 +1463,7 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
 				continue;
 
 			ret = do_discard_extent(stripe, &bytes);
+			discarded_bytes += bytes;
 			if (ret) {
 				/*
 				 * Keep going if discard is not supported by the
@@ -1471,8 +1472,6 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
 				if (ret != -EOPNOTSUPP)
 					break;
 				ret = 0;
-			} else {
-				discarded_bytes += bytes;
 			}
 		}
 		kfree(stripes);
@@ -6703,6 +6702,7 @@ static int btrfs_trim_free_extents_throttle(struct btrfs_device *device,
 
 		ret = btrfs_issue_discard(device->bdev, start, len,
 					  &bytes);
+		*trimmed += bytes;
 		if (!ret)
 			btrfs_set_extent_bit(&device->alloc_state, start,
 					     start + bytes - 1, CHUNK_TRIMMED, NULL);
@@ -6712,7 +6712,6 @@ static int btrfs_trim_free_extents_throttle(struct btrfs_device *device,
 			break;
 
 		start += len;
-		*trimmed += bytes;
 		trim_len += len;
 		if (trim_len >= BTRFS_MAX_TRIM_LENGTH) {
 			*ret_next_pos = start;
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index bff6e5b372d6..a85806bcf87a 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -2415,10 +2415,9 @@ static int do_trimming(struct btrfs_block_group *block_group,
 	spin_unlock(&space_info->lock);
 
 	ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed, false);
-	if (!ret) {
-		*total_trimmed += trimmed;
+	*total_trimmed += trimmed;
+	if (!ret)
 		trim_state = BTRFS_TRIM_STATE_TRIMMED;
-	}
 
 	if (reserved_start < start)
 		__btrfs_add_free_space(block_group, reserved_start,

-- 
2.56.0



  reply	other threads:[~2026-10-28 17:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-28 17:55 [PATCH 0/2] btrfs: don't expose ERESTARTSYS to userspace Luca Stefani via B4 Relay
2026-10-28 17:55 ` Luca Stefani via B4 Relay [this message]
2026-10-28 21:02   ` [PATCH 1/2] btrfs: account partially completed discards Qu Wenruo
2026-10-28 17:55 ` [PATCH 2/2] btrfs: don't report failure when trim is interrupted Luca Stefani via B4 Relay
2026-10-28 21:06   ` Qu Wenruo
2026-10-29  8:56     ` Luca Stefani
2026-10-28 20:13 ` [PATCH 0/2] btrfs: don't expose ERESTARTSYS to userspace zhiwei.liang

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=20261028-b4-btrfs-fitrim-interruption-v1-1-bf5c53738b3a@gmail.com \
    --to=devnull+luca.stefani.ge1.gmail.com@kernel.org \
    --cc=dsterba@suse.com \
    --cc=fdmanana@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luca.stefani.ge1@gmail.com \
    --cc=mason@kernel.org \
    --cc=robbieko@synology.com \
    /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

all inboxes | Powered by JetHome®