dcavalca / rpms / util-linux

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