commit 604b66ae8b4005d89eed1cbab45a64cb57e75390
Author:     sin <s...@2f30.org>
AuthorDate: Mon Jul 2 14:03:49 2018 +0100
Commit:     sin <s...@2f30.org>
CommitDate: Mon Jul 2 14:03:57 2018 +0100

    Add blkdiscard(8)

diff --git a/Makefile b/Makefile
index 453607c..b526421 100644
--- a/Makefile
+++ b/Makefile
@@ -37,6 +37,7 @@ LIBUTILSRC = \
 LIB = $(LIBUTIL)
 
 BIN = \
+       blkdiscard        \
        chvt              \
        clear             \
        ctrlaltdel        \
diff --git a/blkdiscard.8 b/blkdiscard.8
new file mode 100644
index 0000000..c6d97be
--- /dev/null
+++ b/blkdiscard.8
@@ -0,0 +1,12 @@
+.Dd July 2, 2018
+.Dt BLKDISCARD 8
+.Os ubase
+.Sh NAME
+.Nm blkdiscard
+.Nd discard sectors on a device
+.Sh SYNOPSIS
+.Nm
+.Ar device
+.Sh DESCRIPTION
+.Nm
+is used to discard all device sectors on solid-state devices.
diff --git a/blkdiscard.c b/blkdiscard.c
new file mode 100644
index 0000000..de08a49
--- /dev/null
+++ b/blkdiscard.c
@@ -0,0 +1,48 @@
+/* See LICENSE file for copyright and license details. */
+#include <sys/ioctl.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <fcntl.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include "util.h"
+
+#define OFFSET_IDX     0
+#define LENGTH_IDX     1
+
+#define BLKDISCARD     _IO(0x12, 119)
+
+static void
+usage(void)
+{
+       eprintf("usage: %s device\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+       uint64_t range[2];
+       int fd;
+
+       ARGBEGIN {
+       default:
+               usage();
+       } ARGEND
+
+       if (argc != 1)
+               usage();
+
+       fd = open(argv[0], O_RDWR);
+       if (fd < 0)
+               eprintf("open: %s:", argv[0]);
+       range[OFFSET_IDX] = 0;
+       if (ioctl(fd, BLKGETSIZE64, &range[LENGTH_IDX]) < 0)
+               eprintf("BLKGETSIZE64: %s:", argv[0]);
+       if (ioctl(fd, BLKDISCARD, range) < 0)
+               eprintf("BLKDISCARD: %s:", argv[0]);
+       close(fd);
+       return 0;
+}

Reply via email to