cdown / rpms / util-linux

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