Blame SOURCES/xfsprogs-5.4.0-mkfs-Break-block-discard-into-chunks-of-2-GB.patch

212ea0
From 7e8a6edb4d1ba0079152eb477abbbc1dfb1ebb7e Mon Sep 17 00:00:00 2001
212ea0
From: Pavel Reichl <preichl@redhat.com>
212ea0
Date: Fri, 13 Dec 2019 16:21:26 -0500
212ea0
Subject: [PATCH] mkfs: Break block discard into chunks of 2 GB
212ea0
212ea0
Some users are not happy about the BLKDISCARD taking too long and at the
212ea0
same time not being informed about that - so they think that the command
212ea0
actually hung.
212ea0
212ea0
This commit changes code so that progress reporting is possible and also
212ea0
typing the ^C will cancel the ongoing BLKDISCARD.
212ea0
212ea0
Signed-off-by: Pavel Reichl <preichl@redhat.com>
212ea0
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
212ea0
Reviewed-by: Dave Chinner <dchinner@redhat.com>
212ea0
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
212ea0
---
212ea0
 mkfs/xfs_mkfs.c | 50 ++++++++++++++++++++++++++++++++++++-------------
212ea0
 1 file changed, 37 insertions(+), 13 deletions(-)
212ea0
212ea0
diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c
212ea0
index 18338a61..4bfdebf6 100644
212ea0
--- a/mkfs/xfs_mkfs.c
212ea0
+++ b/mkfs/xfs_mkfs.c
212ea0
@@ -1240,17 +1240,40 @@ done:
212ea0
 }
212ea0
 
212ea0
 static void
212ea0
-discard_blocks(dev_t dev, uint64_t nsectors)
212ea0
+discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
212ea0
 {
212ea0
-	int fd;
212ea0
+	int		fd;
212ea0
+	uint64_t	offset = 0;
212ea0
+	/* Discard the device 2G at a time */
212ea0
+	const uint64_t	step = 2ULL << 30;
212ea0
+	const uint64_t	count = BBTOB(nsectors);
212ea0
 
212ea0
-	/*
212ea0
-	 * We intentionally ignore errors from the discard ioctl.  It is
212ea0
-	 * not necessary for the mkfs functionality but just an optimization.
212ea0
-	 */
212ea0
 	fd = libxfs_device_to_fd(dev);
212ea0
-	if (fd > 0)
212ea0
-		platform_discard_blocks(fd, 0, nsectors << 9);
212ea0
+	if (fd <= 0)
212ea0
+		return;
212ea0
+	if (!quiet) {
212ea0
+		printf("Discarding blocks...");
212ea0
+		fflush(stdout);
212ea0
+	}
212ea0
+
212ea0
+	/* The block discarding happens in smaller batches so it can be
212ea0
+	 * interrupted prematurely
212ea0
+	 */
212ea0
+	while (offset < count) {
212ea0
+		uint64_t	tmp_step = min(step, count - offset);
212ea0
+
212ea0
+		/*
212ea0
+		 * We intentionally ignore errors from the discard ioctl. It is
212ea0
+		 * not necessary for the mkfs functionality but just an
212ea0
+		 * optimization. However we should stop on error.
212ea0
+		 */
212ea0
+		if (platform_discard_blocks(fd, offset, tmp_step))
212ea0
+			return;
212ea0
+
212ea0
+		offset += tmp_step;
212ea0
+	}
212ea0
+	if (!quiet)
212ea0
+		printf("Done.\n");
212ea0
 }
212ea0
 
212ea0
 static __attribute__((noreturn)) void
212ea0
@@ -2507,18 +2530,19 @@ open_devices(
212ea0
 
212ea0
 static void
212ea0
 discard_devices(
212ea0
-	struct libxfs_xinit	*xi)
212ea0
+	struct libxfs_xinit	*xi,
212ea0
+	int			quiet)
212ea0
 {
212ea0
 	/*
212ea0
 	 * This function has to be called after libxfs has been initialized.
212ea0
 	 */
212ea0
 
212ea0
 	if (!xi->disfile)
212ea0
-		discard_blocks(xi->ddev, xi->dsize);
212ea0
+		discard_blocks(xi->ddev, xi->dsize, quiet);
212ea0
 	if (xi->rtdev && !xi->risfile)
212ea0
-		discard_blocks(xi->rtdev, xi->rtsize);
212ea0
+		discard_blocks(xi->rtdev, xi->rtsize, quiet);
212ea0
 	if (xi->logdev && xi->logdev != xi->ddev && !xi->lisfile)
212ea0
-		discard_blocks(xi->logdev, xi->logBBsize);
212ea0
+		discard_blocks(xi->logdev, xi->logBBsize, quiet);
212ea0
 }
212ea0
 
212ea0
 static void
212ea0
@@ -3749,7 +3773,7 @@ main(
212ea0
 	 * All values have been validated, discard the old device layout.
212ea0
 	 */
212ea0
 	if (discard && !dry_run)
212ea0
-		discard_devices(&xi);
212ea0
+		discard_devices(&xi, quiet);
212ea0
 
212ea0
 	/*
212ea0
 	 * we need the libxfs buffer cache from here on in.
212ea0
-- 
212ea0
2.17.0
212ea0