From 604b66ae8b4005d89eed1cbab45a64cb57e75390 Mon Sep 17 00:00:00 2001 From: sin Date: Mon, 2 Jul 2018 14:03:49 +0100 Subject: [PATCH] Add blkdiscard(8) --- Makefile | 1 + blkdiscard.8 | 12 ++++++++++++ blkdiscard.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 blkdiscard.8 create mode 100644 blkdiscard.c 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 +#include +#include +#include + +#include +#include +#include + +#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; +}