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

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