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

3d5736
From 7e8a6edb4d1ba0079152eb477abbbc1dfb1ebb7e Mon Sep 17 00:00:00 2001
3d5736
From: Pavel Reichl <preichl@redhat.com>
3d5736
Date: Fri, 13 Dec 2019 16:21:26 -0500
3d5736
Subject: [PATCH] mkfs: Break block discard into chunks of 2 GB
3d5736
3d5736
Some users are not happy about the BLKDISCARD taking too long and at the
3d5736
same time not being informed about that - so they think that the command
3d5736
actually hung.
3d5736
3d5736
This commit changes code so that progress reporting is possible and also
3d5736
typing the ^C will cancel the ongoing BLKDISCARD.
3d5736
3d5736
Signed-off-by: Pavel Reichl <preichl@redhat.com>
3d5736
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
3d5736
Reviewed-by: Dave Chinner <dchinner@redhat.com>
3d5736
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
3d5736
---
3d5736
 mkfs/xfs_mkfs.c | 50 ++++++++++++++++++++++++++++++++++++-------------
3d5736
 1 file changed, 37 insertions(+), 13 deletions(-)
3d5736
3d5736
Index: xfsprogs-4.5.0/mkfs/xfs_mkfs.c
3d5736
===================================================================
3d5736
--- xfsprogs-4.5.0.orig/mkfs/xfs_mkfs.c
3d5736
+++ xfsprogs-4.5.0/mkfs/xfs_mkfs.c
3d5736
@@ -875,17 +875,41 @@ done:
3d5736
 }
3d5736
 
3d5736
 static void
3d5736
-discard_blocks(dev_t dev, __uint64_t nsectors)
3d5736
+discard_blocks(dev_t dev, __uint64_t nsectors, int quiet)
3d5736
 {
3d5736
-	int fd;
3d5736
+	int		fd;
3d5736
+	uint64_t	offset = 0;
3d5736
+	/* Discard the device 2G at a time */
3d5736
+	const __uint64_t step = 2ULL << 30;
3d5736
+	const __uint64_t count = BBTOB(nsectors);
3d5736
 
3d5736
-	/*
3d5736
-	 * We intentionally ignore errors from the discard ioctl.  It is
3d5736
-	 * not necessary for the mkfs functionality but just an optimization.
3d5736
-	 */
3d5736
 	fd = libxfs_device_to_fd(dev);
3d5736
-	if (fd > 0)
3d5736
-		platform_discard_blocks(fd, 0, nsectors << 9);
3d5736
+	if (fd <= 0)
3d5736
+		return;
3d5736
+	if (!quiet) {
3d5736
+		printf("Discarding blocks...");
3d5736
+		fflush(stdout);
3d5736
+	}
3d5736
+
3d5736
+	/* The block discarding happens in smaller batches so it can be
3d5736
+	 * interrupted prematurely
3d5736
+	 */
3d5736
+	while (offset < count) {
3d5736
+		__uint64_t	tmp_step = min(step, count - offset);
3d5736
+
3d5736
+		/*
3d5736
+		 * We intentionally ignore errors from the discard ioctl. It is
3d5736
+		 * not necessary for the mkfs functionality but just an
3d5736
+		 * optimization. However we should stop on error.
3d5736
+		 */
3d5736
+		if (platform_discard_blocks(fd, offset, tmp_step))
3d5736
+			return;
3d5736
+
3d5736
+		offset += tmp_step;
3d5736
+	}
3d5736
+	if (!quiet)
3d5736
+		printf("Done.\n");
3d5736
+
3d5736
 }
3d5736
 
3d5736
 int
3d5736
@@ -2140,11 +2164,11 @@ _("warning: sparse inodes not supported
3d5736
 	}
3d5736
 
3d5736
 	if (discard && !Nflag) {
3d5736
-		discard_blocks(xi.ddev, xi.dsize);
3d5736
+		discard_blocks(xi.ddev, xi.dsize, qflag);
3d5736
 		if (xi.rtdev)
3d5736
-			discard_blocks(xi.rtdev, xi.rtsize);
3d5736
+			discard_blocks(xi.rtdev, xi.rtsize, qflag);
3d5736
 		if (xi.logdev && xi.logdev != xi.ddev)
3d5736
-			discard_blocks(xi.logdev, xi.logBBsize);
3d5736
+			discard_blocks(xi.logdev, xi.logBBsize, qflag);
3d5736
 	}
3d5736
 
3d5736
 	if (!liflag && !ldflag)