Skip to contentSkip to conversation

mkfs: print newline if discard fails

PATCH In review Darrick J. Wong 2 participants 6 messages Dec 14, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mkfs: print newline if discard fails

From: Darrick J. Wong <[email protected]>

Make sure the "Discarding..." gets a newline after it no matter how we exit the function. Don't bother with any printing it even a small discard request fails.

---
 mkfs/xfs_mkfs.c |   12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
mkfs/xfs_mkfs.c +11 -1
index 4bfdebf6..948a5a77 100644
@@ -1251,6 +1251,14 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 	fd = libxfs_device_to_fd(dev);
 	if (fd <= 0)
 		return;
+
+	/*
+	 * Try discarding the first 64k; if that fails, don't bother printing
+	 * any messages at all.
+	 */
+	if (platform_discard_blocks(fd, offset, 65536))
+		return;
+
 	if (!quiet) {
 		printf("Discarding blocks...");
 		fflush(stdout);
@@ -1267,8 +1275,10 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 		 * not necessary for the mkfs functionality but just an
 		 * optimization. However we should stop on error.
 		 */
-		if (platform_discard_blocks(fd, offset, tmp_step))
+		if (platform_discard_blocks(fd, offset, tmp_step)) {
+			printf("\n");
 			return;
+		}
 
 		offset += tmp_step;
 	}
#1 lore raw
Quoted 16 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Make sure the "Discarding..." gets a newline after it no matter how we exit the function. Don't bother with any printing it even a small discard request fails.

---
 mkfs/xfs_mkfs.c |   12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
mkfs/xfs_mkfs.c +0 -0
index 4bfdebf6..948a5a77 100644

mkfs/xfs_mkfs.c discard_blocks(dev_t dev, uint64_t nsectors, int quiet) · line 1260

Quoted 10 lines
mkfs/xfs_mkfs.c +7 -0
@@ -1251,6 +1251,14 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 	fd = libxfs_device_to_fd(dev);
 	if (fd <= 0)
 		return;
+
+	/*
+	 * Try discarding the first 64k; if that fails, don't bother printing
+	 * any messages at all.
+	 */
+	if (platform_discard_blocks(fd, offset, 65536))
+		return;

or trying to discard at all for that matter ;) (i.e. this does more than suppress messages)

I think this patch does 2 things... and that comment is a little confusing.

Also you discard the first 64k twice which is ok but...?

mkfs/xfs_mkfs.c discard_blocks(dev_t dev, uint64_t nsectors, int quiet) · line 1278

Quoted 9 lines
mkfs/xfs_mkfs.c +1 -1
 	if (!quiet) {
 		printf("(ks...");
 		fflush(stdout);
@@ -1267,8 +1275,10 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 		 * not necessary for the mkfs functionality but just an
 		 * optimization. However we should stop on error.
 		 */
-		if (platform_discard_blocks(fd, offset, tmp_step))
+		if (platform_discard_blocks(fd, offset, tmp_step)) {

also we don't want to do this if (quiet) ;)

mkfs/xfs_mkfs.c discard_blocks(dev_t dev, uint64_t nsectors, int quiet) · line 1284

Quoted 7 lines
mkfs/xfs_mkfs.c +2 -0
+			printf("\n");
 			return;
+		}
 
 		offset += tmp_step;
 	}

How about this, though I'd split it into 2 patches, 1 to catch the newline if discard fails (and !quiet), and another to only print status if/after the first discard succeeds

mkfs/xfs_mkfs.c +11 -6
index 4bfdebf6..9f07f042 100644
@@ -1244,6 +1244,7 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 {
 	int		fd;
 	uint64_t	offset = 0;
+	bool		messaged = false;
 	/* Discard the device 2G at a time */
 	const uint64_t	step = 2ULL << 30;
 	const uint64_t	count = BBTOB(nsectors);
@@ -1251,10 +1252,6 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 	fd = libxfs_device_to_fd(dev);
 	if (fd <= 0)
 		return;
-	if (!quiet) {
-		printf("Discarding blocks...");
-		fflush(stdout);
-	}
 
 	/* The block discarding happens in smaller batches so it can be
 	 * interrupted prematurely
@@ -1267,12 +1264,20 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 		 * not necessary for the mkfs functionality but just an
 		 * optimization. However we should stop on error.
 		 */
-		if (platform_discard_blocks(fd, offset, tmp_step))
+		if (platform_discard_blocks(fd, offset, tmp_step) == 0) {
+			if (!messaged && !quiet) {
+				printf("Discarding blocks...");
+				fflush(stdout);
+				messaged = true;
+			}
+		} else if (messaged && !quiet) {
+			printf("\n");
 			return;
+		}
 
 		offset += tmp_step;
 	}
-	if (!quiet)
+	if (messaged && !quiet)
 		printf("Done.\n");
 }
 
#2 lore raw

PATCH 1/1 mkfs: tidy up discard notifications

Only notify user of discard operations if the first one succeeds, and be sure to print a trailing newline if we stop early.

---

mkfs/xfs_mkfs.c +9 -5
index 4bfdebf6..afa7feb4 100644
@@ -1251,10 +1251,6 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 	fd = libxfs_device_to_fd(dev);
 	if (fd <= 0)
 		return;
-	if (!quiet) {
-		printf("Discarding blocks...");
-		fflush(stdout);
-	}
 
 	/* The block discarding happens in smaller batches so it can be
 	 * interrupted prematurely
@@ -1267,8 +1263,16 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 		 * not necessary for the mkfs functionality but just an
 		 * optimization. However we should stop on error.
 		 */
-		if (platform_discard_blocks(fd, offset, tmp_step))
+		if (platform_discard_blocks(fd, offset, tmp_step) == 0) {
+			if (offset == 0 && !quiet) {
+				printf("Discarding blocks...");
+				fflush(stdout);
+			}
+		} else if (!quiet) {
+			if (offset != 0)
+				printf("\n");
 			return;
+		}
 
 		offset += tmp_step;
 	}
#3 lore raw

PATCH v2 1/1 mkfs: tidy up discard notifications

Only notify user of discard operations if the first one succeeds, and be sure to print a trailing newline if we stop early.

---

V2: Logic is hard. ;) If I messed this one up, take it back Darrick. :)

mkfs/xfs_mkfs.c +10 -6
index 4bfdebf6..606f79da 100644
@@ -1251,10 +1251,6 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 	fd = libxfs_device_to_fd(dev);
 	if (fd <= 0)
 		return;
-	if (!quiet) {
-		printf("Discarding blocks...");
-		fflush(stdout);
-	}
 
 	/* The block discarding happens in smaller batches so it can be
 	 * interrupted prematurely
@@ -1267,12 +1263,20 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 		 * not necessary for the mkfs functionality but just an
 		 * optimization. However we should stop on error.
 		 */
-		if (platform_discard_blocks(fd, offset, tmp_step))
+		if (platform_discard_blocks(fd, offset, tmp_step) == 0) {
+			if (offset == 0 && !quiet) {
+				printf("Discarding blocks...");
+				fflush(stdout);
+			}
+		} else {
+			if (offset > 0 && !quiet)
+				printf("\n");
 			return;
+		}
 
 		offset += tmp_step;
 	}
-	if (!quiet)
+	if (offset > 0 && !quiet)
 		printf("Done.\n");
 }
 
author Reviewed-by on v2 1/1
#4 lore raw

Re: [PATCH V2] mkfs: tidy up discard notifications

Quoted 26 lines · Eric Sandeen

Only notify user of discard operations if the first one succeeds, and be sure to print a trailing newline if we stop early.

---

V2: Logic is hard. ;) If I messed this one up, take it back Darrick. :)

mkfs/xfs_mkfs.c +0 -4
index 4bfdebf6..606f79da 100644
@@ -1251,10 +1251,6 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 	fd = libxfs_device_to_fd(dev);
 	if (fd <= 0)
 		return;
-	if (!quiet) {
-		printf("Discarding blocks...");
-		fflush(stdout);
-	}
 
 	/* The block discarding happens in smaller batches so it can be
 	 * interrupted prematurely
@@ -1267,12 +1263,20 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 		 * not necessary for the mkfs functionality but just an
 		 * optimization. However we should stop on error.

mkfs/xfs_mkfs.c discard_blocks(dev_t dev, uint64_t nsectors, int quiet) · line 1273

Quoted 10 lines
mkfs/xfs_mkfs.c +8 -1
 		 */
-		if (platform_discard_blocks(fd, offset, tmp_step))
+		if (platform_discard_blocks(fd, offset, tmp_step) == 0) {
+			if (offset == 0 && !quiet) {
+				printf("Discarding blocks...");
+				fflush(stdout);
+			}
+		} else {
+			if (offset > 0 && !quiet)
+				printf("\n");

Maybe we should say failed? Or ... eh... whatever, the format happens regardless.

Looks ok,

--D

Quoted 12 lines
mkfs/xfs_mkfs.c +2 -1
 			return;
+		}
 
 		offset += tmp_step;
 	}
-	if (!quiet)
+	if (offset > 0 && !quiet)
 		printf("Done.\n");
 }
 
#5 lore raw

Re: [PATCH V2] mkfs: tidy up discard notifications

Quoted 32 lines · Darrick J. Wong
Quoted 31 lines · Eric Sandeen

Only notify user of discard operations if the first one succeeds, and be sure to print a trailing newline if we stop early.

---

V2: Logic is hard. ;) If I messed this one up, take it back Darrick. :)

mkfs/xfs_mkfs.c +3 -5
index 4bfdebf6..606f79da 100644
@@ -1251,10 +1251,6 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 	fd = libxfs_device_to_fd(dev);
 	if (fd <= 0)
 		return;
-	if (!quiet) {
-		printf("Discarding blocks...");
-		fflush(stdout);
-	}
 
 	/* The block discarding happens in smaller batches so it can be
 	 * interrupted prematurely
@@ -1267,12 +1263,20 @@ discard_blocks(dev_t dev, uint64_t nsectors, int quiet)
 		 * not necessary for the mkfs functionality but just an
 		 * optimization. However we should stop on error.
 		 */
-		if (platform_discard_blocks(fd, offset, tmp_step))
+		if (platform_discard_blocks(fd, offset, tmp_step) == 0) {
+			if (offset == 0 && !quiet) {
+				printf("Discarding blocks...");
Quoted 8 lines

mkfs/xfs_mkfs.c discard_blocks(dev_t dev, uint64_t nsectors, int quiet) · line 1273

Quoted 5 lines
mkfs/xfs_mkfs.c +5 -0
+				fflush(stdout);
+			}
+		} else {
+			if (offset > 0 && !quiet)
+				printf("\n");

Maybe we should say failed? Or ... eh... whatever, the format happens regardless.

Yeah... a "failed" printf will launch support calls & emails. :)

Quoted 2 lines

Looks ok,

Thanks Darrick.

-Eric

Quoted 15 lines

--D

Quoted 12 lines
mkfs/xfs_mkfs.c +2 -1
 			return;
+		}
 
 		offset += tmp_step;
 	}
-	if (!quiet)
+	if (offset > 0 && !quiet)
 		printf("Done.\n");
 }