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

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