public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: mkfs: use device_discard_blocks() to replace prepare_discard_device()
@ 2026-01-08  4:42 Qu Wenruo
  2026-01-08  4:51 ` Roman Mamedov
  0 siblings, 1 reply; 5+ messages in thread
From: Qu Wenruo @ 2026-01-08  4:42 UTC (permalink / raw)
  To: linux-btrfs

The helper device_discard_blocks() is more generic, meanwhile
prepare_discard_device() is only utilized once to output a message.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 common/device-utils.c | 35 ++++++++++-------------------------
 1 file changed, 10 insertions(+), 25 deletions(-)

diff --git a/common/device-utils.c b/common/device-utils.c
index 9dfc50211955..e60196ec8e8b 100644
--- a/common/device-utils.c
+++ b/common/device-utils.c
@@ -80,29 +80,6 @@ int device_discard_blocks(int fd, u64 start, u64 len)
 	return 0;
 }
 
-static void prepare_discard_device(const char *filename, int fd, u64 byte_count, unsigned opflags)
-{
-	u64 cur = 0;
-
-	while (cur < byte_count) {
-		/* 1G granularity */
-		u64 chunk_size = (cur == 0) ? SZ_1M : min_t(u64, byte_count - cur, SZ_1G);
-		int ret;
-
-		ret = discard_range(fd, cur, chunk_size);
-		if (ret)
-			return;
-		/*
-		 * The first range discarded successfully, meaning the device supports
-		 * discard.
-		 */
-		if (opflags & PREP_DEVICE_VERBOSE && cur == 0)
-			printf("Performing full device TRIM %s (%s) ...\n",
-			       filename, pretty_size(byte_count));
-		cur += chunk_size;
-	}
-}
-
 /*
  * Write zeros to the given range [start, start + len)
  */
@@ -293,8 +270,16 @@ int btrfs_prepare_device(int fd, const char *file, u64 *byte_count_ret,
 		goto err;
 	}
 
-	if (!(opflags & PREP_DEVICE_ZONED) && (opflags & PREP_DEVICE_DISCARD))
-		prepare_discard_device(file, fd, byte_count, opflags);
+	if (!(opflags & PREP_DEVICE_ZONED) && (opflags & PREP_DEVICE_DISCARD)) {
+		ret = device_discard_blocks(fd, 0, byte_count);
+		if (ret < 0) {
+			errno = -ret;
+			warning("failed to discard device '%s': %m", file);
+		} else {
+			printf("Performing full device TRIM %s (%s) ...\n",
+			       file, pretty_size(byte_count));
+		}
+	}
 
 	ret = btrfs_wipe_existing_sb(fd, zinfo);
 	if (ret < 0) {
-- 
2.52.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-01-08  5:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-08  4:42 [PATCH] btrfs-progs: mkfs: use device_discard_blocks() to replace prepare_discard_device() Qu Wenruo
2026-01-08  4:51 ` Roman Mamedov
2026-01-08  4:54   ` Qu Wenruo
2026-01-08  5:10     ` Qu Wenruo
2026-01-08  5:38       ` Roman Mamedov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox