531551
diff -up util-linux-2.23.2/sys-utils/blkdiscard.8.kzak util-linux-2.23.2/sys-utils/blkdiscard.8
531551
--- util-linux-2.23.2/sys-utils/blkdiscard.8.kzak	2013-06-13 09:46:10.532651579 +0200
531551
+++ util-linux-2.23.2/sys-utils/blkdiscard.8	2014-10-27 10:03:13.650011708 +0100
531551
@@ -1,5 +1,5 @@
531551
 .\" -*- nroff -*-
531551
-.TH BLKDISCARD 8 "October 2012" "util-linux" "System Administration"
531551
+.TH BLKDISCARD 8 "July 2014" "util-linux" "System Administration"
531551
 .SH NAME
531551
 blkdiscard \- discard sectors on a device
531551
 .SH SYNOPSIS
531551
@@ -15,7 +15,7 @@ blkdiscard \- discard sectors on a devic
531551
 .B blkdiscard
531551
 is used to discard device sectors.  This is useful for solid-state
531551
 drivers (SSDs) and thinly-provisioned storage.  Unlike
531551
-.BR fstrim (8)
531551
+.BR fstrim (8) ,
531551
 this command is used directly on the block device.
531551
 .PP
531551
 By default,
531551
@@ -33,32 +33,44 @@ The
531551
 .I offset
531551
 and
531551
 .I length
531551
-arguments may be followed by the multiplicative suffixes KiB=1024,
531551
-MiB=1024*1024, and so on for GiB, TiB, PiB, EiB, ZiB and YiB (the "iB" is
531551
+arguments may be followed by the multiplicative suffixes KiB (=1024),
531551
+MiB (=1024*1024), and so on for GiB, TiB, PiB, EiB, ZiB and YiB (the "iB" is
531551
 optional, e.g., "K" has the same meaning as "KiB") or the suffixes
531551
-KB=1000, MB=1000*1000, and so on for GB, TB, PB, EB, ZB and YB.
531551
-.IP "\fB\-h, \-\-help\fP"
531551
-Print help and exit.
531551
-.IP "\fB\-o, \-\-offset\fP \fIoffset\fP"
531551
-Byte offset in the device from which to discard.  Provided value will be
531551
-aligned to the device sector size.  Default value is zero.
531551
-.IP "\fB\-l, \-\-length\fP \fIlength\fP"
531551
-Number of bytes after starting point to discard.  Provided value will be
531551
-aligned to the device sector size.  If the specified value extends past
531551
+KB (=1000), MB (=1000*1000), and so on for GB, TB, PB, EB, ZB and YB.
531551
+.TP
531551
+.BR \-o , " \-\-offset \fIoffset"
531551
+Byte offset into the device from which to start discarding.  The provided value
531551
+will be aligned to the device sector size.  The default value is zero.
531551
+.TP
531551
+.BR \-l , " \-\-length \fIlength"
531551
+The number of bytes to discard (counting from the starting point).  The provided value
531551
+will be aligned to the device sector size.  If the specified value extends past
531551
 the end of the device,
531551
 .B blkdiscard
531551
-will stop at the device size boundary.  Default value extends to the end
531551
+will stop at the device size boundary.  The default value extends to the end
531551
 of the device.
531551
-.IP "\fB\-s, \-\-secure\fP"
531551
-Perform secure discard.  Secure discard is the same as regular discard
531551
-except all copies of the discarded blocks possibly created by garbage
531551
-collection must also be erased.  It has to be supported by the device.
531551
-.IP "\fB\-v, \-\-verbose\fP"
531551
-Print aligned
531551
+.TP
531551
+.BR \-p , " \-\-step \fIlength"
531551
+The number of bytes to discard within one iteration. The default is to discard
531551
+all by one ioctl call.
531551
+.TP
531551
+.BR \-s , " \-\-secure"
531551
+Perform a secure discard.  A secure discard is the same as a regular discard
531551
+except that all copies of the discarded blocks that were possibly created by
531551
+garbage collection must also be erased.  This requires support from the device.
531551
+.TP
531551
+.BR \-v , " \-\-verbose"
531551
+Display the aligned values of
531551
 .I offset
531551
 and
531551
-.I length
531551
-arguments.
531551
+.IR length .
531551
+If the option \fB\-\-step\fR specified than it prints discard progress every second.
531551
+.TP
531551
+.BR \-V , " \-\-version"
531551
+Display version information and exit.
531551
+.TP
531551
+.BR \-h , " \-\-help"
531551
+Display help text and exit.
531551
 .SH AUTHOR
531551
 .MT lczerner@redhat.com
531551
 Lukas Czerner
531551
diff -up util-linux-2.23.2/sys-utils/blkdiscard.c.kzak util-linux-2.23.2/sys-utils/blkdiscard.c
531551
--- util-linux-2.23.2/sys-utils/blkdiscard.c.kzak	2013-07-30 10:39:26.337739534 +0200
531551
+++ util-linux-2.23.2/sys-utils/blkdiscard.c	2014-10-27 10:03:20.981088614 +0100
531551
@@ -31,9 +31,11 @@
531551
 #include <fcntl.h>
531551
 #include <limits.h>
531551
 #include <getopt.h>
531551
+#include <time.h>
531551
 
531551
 #include <sys/ioctl.h>
531551
 #include <sys/stat.h>
531551
+#include <sys/time.h>
531551
 #include <linux/fs.h>
531551
 
531551
 #include "nls.h"
531551
@@ -49,6 +51,10 @@
531551
 #define BLKSECDISCARD	_IO(0x12,125)
531551
 #endif
531551
 
531551
+#define print_stats(path, stats) \
531551
+	printf(_("%s: Discarded %" PRIu64 " bytes from the " \
531551
+		 "offset %" PRIu64"\n"), path, stats[1], stats[0]);
531551
+
531551
 static void __attribute__((__noreturn__)) usage(FILE *out)
531551
 {
531551
 	fputs(USAGE_HEADER, out);
531551
@@ -57,6 +63,7 @@ static void __attribute__((__noreturn__)
531551
 	fputs(USAGE_OPTIONS, out);
531551
 	fputs(_(" -o, --offset <num>  offset in bytes to discard from\n"
531551
 		" -l, --length <num>  length of bytes to discard from the offset\n"
531551
+		" -p, --step <num>    size of the discard iterations within the offset\n"
531551
 		" -s, --secure        perform secure discard\n"
531551
 		" -v, --verbose       print aligned length and offset\n"),
531551
 		out);
531551
@@ -70,15 +77,17 @@ static void __attribute__((__noreturn__)
531551
 int main(int argc, char **argv)
531551
 {
531551
 	char *path;
531551
-	int c, fd, verbose = 0, secure = 0;
531551
-	uint64_t end, blksize, secsize, range[2];
531551
+	int c, fd, verbose = 0, secure = 0, secsize;
531551
+	uint64_t end, blksize, step, range[2], stats[2];
531551
 	struct stat sb;
531551
+	struct timespec now, last;
531551
 
531551
 	static const struct option longopts[] = {
531551
 	    { "help",      0, 0, 'h' },
531551
 	    { "version",   0, 0, 'V' },
531551
 	    { "offset",    1, 0, 'o' },
531551
 	    { "length",    1, 0, 'l' },
531551
+	    { "step",      1, 0, 'p' },
531551
 	    { "secure",    0, 0, 's' },
531551
 	    { "verbose",   0, 0, 'v' },
531551
 	    { NULL,        0, 0, 0 }
531551
@@ -91,8 +100,9 @@ int main(int argc, char **argv)
531551
 
531551
 	range[0] = 0;
531551
 	range[1] = ULLONG_MAX;
531551
+	step = 0;
531551
 
531551
-	while ((c = getopt_long(argc, argv, "hVsvo:l:", longopts, NULL)) != -1) {
531551
+	while ((c = getopt_long(argc, argv, "hVsvo:l:p:", longopts, NULL)) != -1) {
531551
 		switch(c) {
531551
 		case 'h':
531551
 			usage(stdout);
531551
@@ -108,6 +118,10 @@ int main(int argc, char **argv)
531551
 			range[0] = strtosize_or_err(optarg,
531551
 					_("failed to parse offset"));
531551
 			break;
531551
+		case 'p':
531551
+			step = strtosize_or_err(optarg,
531551
+					_("failed to parse step"));
531551
+			break;
531551
 		case 's':
531551
 			secure = 1;
531551
 			break;
531551
@@ -121,7 +135,7 @@ int main(int argc, char **argv)
531551
 	}
531551
 
531551
 	if (optind == argc)
531551
-		errx(EXIT_FAILURE, _("no device specified."));
531551
+		errx(EXIT_FAILURE, _("no device specified"));
531551
 
531551
 	path = argv[optind++];
531551
 
531551
@@ -130,43 +144,69 @@ int main(int argc, char **argv)
531551
 		usage(stderr);
531551
 	}
531551
 
531551
-	if (stat(path, &sb) == -1)
531551
-		err(EXIT_FAILURE, _("stat failed %s"), path);
531551
-	if (!S_ISBLK(sb.st_mode))
531551
-		errx(EXIT_FAILURE, _("%s: not a block device"), path);
531551
-
531551
 	fd = open(path, O_WRONLY);
531551
 	if (fd < 0)
531551
 		err(EXIT_FAILURE, _("cannot open %s"), path);
531551
 
531551
+	if (fstat(fd, &sb) == -1)
531551
+		err(EXIT_FAILURE, _("stat failed %s"), path);
531551
+	if (!S_ISBLK(sb.st_mode))
531551
+		errx(EXIT_FAILURE, _("%s: not a block device"), path);
531551
+
531551
 	if (ioctl(fd, BLKGETSIZE64, &blksize))
531551
 		err(EXIT_FAILURE, _("%s: BLKGETSIZE64 ioctl failed"), path);
531551
-
531551
 	if (ioctl(fd, BLKSSZGET, &secsize))
531551
 		err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), path);
531551
 
531551
-	/* align range to the sector size */
531551
-	range[0] = (range[0] + secsize - 1) & ~(secsize - 1);
531551
-	range[1] &= ~(secsize - 1);
531551
+	/* check offset alignment to the sector size */
531551
+	if (range[0] % secsize)
531551
+		errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned "
531551
+			 "to sector size %i"), path, range[0], secsize);
531551
 
531551
 	/* is the range end behind the end of the device ?*/
531551
+	if (range[0] > blksize)
531551
+		errx(EXIT_FAILURE, _("%s: offset is greater than device size"), path);
531551
 	end = range[0] + range[1];
531551
 	if (end < range[0] || end > blksize)
531551
-		range[1] = blksize - range[0];
531551
+		end = blksize;
531551
+
531551
+	range[1] = (step > 0) ? step : end - range[0];
531551
+
531551
+	/* check length alignment to the sector size */
531551
+	if (range[1] % secsize)
531551
+		errx(EXIT_FAILURE, _("%s: length %" PRIu64 " is not aligned "
531551
+			 "to sector size %i"), path, range[1], secsize);
531551
+
531551
+	stats[0] = range[0], stats[1] = 0;
531551
+	clock_gettime(CLOCK_MONOTONIC, &last);
531551
+
531551
+	for (range[0] = range[0]; range[0] < end; range[0] += range[1]) {
531551
+		if (range[0] + range[1] > end)
531551
+			range[1] = end - range[0];
531551
+
531551
+		if (secure) {
531551
+			if (ioctl(fd, BLKSECDISCARD, &range))
531551
+				err(EXIT_FAILURE, _("%s: BLKSECDISCARD ioctl failed"), path);
531551
+		} else {
531551
+			if (ioctl(fd, BLKDISCARD, &range))
531551
+				err(EXIT_FAILURE, _("%s: BLKDISCARD ioctl failed"), path);
531551
+		}
531551
+
531551
+		/* reporting progress */
531551
+		if (verbose && step) {
531551
+			clock_gettime(CLOCK_MONOTONIC, &now;;
531551
+			if (last.tv_sec < now.tv_sec) {
531551
+				print_stats(path, stats);
531551
+				stats[0] = range[0], stats[1] = 0;
531551
+				last = now;
531551
+			}
531551
+		}
531551
 
531551
-	if (secure) {
531551
-		if (ioctl(fd, BLKSECDISCARD, &range))
531551
-			err(EXIT_FAILURE, _("%s: BLKSECDISCARD ioctl failed"), path);
531551
-	} else {
531551
-		if (ioctl(fd, BLKDISCARD, &range))
531551
-			err(EXIT_FAILURE, _("%s: BLKDISCARD ioctl failed"), path);
531551
+		stats[1] += range[1];
531551
 	}
531551
 
531551
 	if (verbose)
531551
-		/* TRANSLATORS: The standard value here is a very large number. */
531551
-		printf(_("%s: Discarded %" PRIu64 " bytes from the "
531551
-			 "offset %" PRIu64"\n"), path,
531551
-			 (uint64_t) range[1], (uint64_t) range[0]);
531551
+		print_stats(path, stats);
531551
 
531551
 	close(fd);
531551
 	return EXIT_SUCCESS;