aboutsummaryrefslogtreecommitdiff
path: root/util-linux
diff options
context:
space:
mode:
authorAri Sundholm <ari@tuxera.com>2016-01-02 01:18:32 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2016-01-02 01:18:32 +0100
commitdbf5a6da6a4295ce26edd1ce34fde567d19afa02 (patch)
treefd8602a4c46e4bcd75f8e5f1664b15bde2499598 /util-linux
parentc2a2625bcbd0d883ca74afb5275e6fd9806936d2 (diff)
downloadbusybox-dbf5a6da6a4295ce26edd1ce34fde567d19afa02.tar.gz
blkdiscard: new applet
function old new delta blkdiscard_main - 264 +264 Signed-off-by: Ari Sundholm <ari@tuxera.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'util-linux')
-rw-r--r--util-linux/blkdiscard.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/util-linux/blkdiscard.c b/util-linux/blkdiscard.c
new file mode 100644
index 000000000..ace88a1f0
--- /dev/null
+++ b/util-linux/blkdiscard.c
@@ -0,0 +1,83 @@
+/*
+ * Mini blkdiscard implementation for busybox
+ *
+ * Copyright (C) 2015 by Ari Sundholm <ari@tuxera.com> and Tuxera Inc.
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+
+//config:config BLKDISCARD
+//config: bool "blkdiscard"
+//config: default y
+//config: help
+//config: blkdiscard discards sectors on a given device.
+
+//kbuild:lib-$(CONFIG_BLKDISCARD) += blkdiscard.o
+//applet:IF_BLKDISCARD(APPLET(blkdiscard, BB_DIR_USR_BIN, BB_SUID_DROP))
+
+//usage:#define blkdiscard_trivial_usage
+//usage: "[-o OFS] [-l LEN] [-s] DEVICE"
+//usage:#define blkdiscard_full_usage "\n\n"
+//usage: "Discard sectors on DEVICE\n"
+//usage: "\n -o OFS Byte offset into device"
+//usage: "\n -l LEN Number of bytes to discard"
+//usage: "\n -s Perform a secure discard"
+//usage:
+//usage:#define blkdiscard_example_usage
+//usage: "$ blkdiscard -o 0 -l 1G /dev/sdb"
+
+#include "libbb.h"
+#include <linux/fs.h>
+
+int blkdiscard_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int blkdiscard_main(int argc UNUSED_PARAM, char **argv)
+{
+ unsigned opts;
+ const char *offset_str = "0";
+ const char *length_str;
+ uint64_t offset; /* Leaving these two variables out does not */
+ uint64_t length; /* shrink code size and hampers readability. */
+ uint64_t range[2];
+// struct stat st;
+ int fd;
+
+ enum {
+ OPT_OFFSET = (1 << 0),
+ OPT_LENGTH = (1 << 1),
+ OPT_SECURE = (1 << 2),
+ };
+
+ opt_complementary = "=1";
+ opts = getopt32(argv, "o:l:s", &offset_str, &length_str);
+ argv += optind;
+
+ fd = xopen(argv[0], O_RDWR|O_EXCL);
+//Why bother, BLK[SEC]DISCARD will fail on non-blockdevs anyway?
+// xfstat(fd, &st);
+// if (!S_ISBLK(st.st_mode))
+// bb_error_msg_and_die("%s: not a block device", argv[0]);
+
+ offset = xatoull_sfx(offset_str, kMG_suffixes);
+
+ if (opts & OPT_LENGTH)
+ length = xatoull_sfx(length_str, kMG_suffixes);
+ else {
+ xioctl(fd, BLKGETSIZE64, &length);
+ length -= offset;
+ }
+
+ range[0] = offset;
+ range[1] = length;
+ ioctl_or_perror_and_die(fd,
+ (opts & OPT_SECURE) ? BLKSECDISCARD : BLKDISCARD,
+ &range,
+ "%s: %s failed",
+ argv[0],
+ (opts & OPT_SECURE) ? "BLKSECDISCARD" : "BLKDISCARD"
+ );
+
+ if (ENABLE_FEATURE_CLEAN_UP)
+ close(fd);
+
+ return EXIT_SUCCESS;
+}