blob: ff2101ed049b515baa5d89d74c849639b3ad318e [file] [log] [blame]
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +01001/*
2 * Mini blkdiscard implementation for busybox
3 *
4 * Copyright (C) 2015 by Ari Sundholm <ari@tuxera.com> and Tuxera Inc.
5 *
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7 */
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +01008//config:config BLKDISCARD
Denys Vlasenkob097a842018-12-28 03:20:17 +01009//config: bool "blkdiscard (4.3 kb)"
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010010//config: default y
11//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020012//config: blkdiscard discards sectors on a given device.
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010013
Denys Vlasenko277081e2017-08-06 20:20:47 +020014//applet:IF_BLKDISCARD(APPLET_NOEXEC(blkdiscard, blkdiscard, BB_DIR_USR_BIN, BB_SUID_DROP, blkdiscard))
15
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010016//kbuild:lib-$(CONFIG_BLKDISCARD) += blkdiscard.o
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010017
18//usage:#define blkdiscard_trivial_usage
19//usage: "[-o OFS] [-l LEN] [-s] DEVICE"
20//usage:#define blkdiscard_full_usage "\n\n"
21//usage: "Discard sectors on DEVICE\n"
22//usage: "\n -o OFS Byte offset into device"
23//usage: "\n -l LEN Number of bytes to discard"
24//usage: "\n -s Perform a secure discard"
25//usage:
26//usage:#define blkdiscard_example_usage
27//usage: "$ blkdiscard -o 0 -l 1G /dev/sdb"
28
29#include "libbb.h"
30#include <linux/fs.h>
31
Denys Vlasenko8ea061e2017-07-15 13:53:41 +020032#ifndef BLKDISCARD
33#define BLKDISCARD 0x1277
34#endif
35#ifndef BLKSECDISCARD
36#define BLKSECDISCARD 0x127d
37#endif
38
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010039int blkdiscard_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
40int blkdiscard_main(int argc UNUSED_PARAM, char **argv)
41{
42 unsigned opts;
43 const char *offset_str = "0";
44 const char *length_str;
45 uint64_t offset; /* Leaving these two variables out does not */
46 uint64_t length; /* shrink code size and hampers readability. */
47 uint64_t range[2];
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010048 int fd;
49
50 enum {
51 OPT_OFFSET = (1 << 0),
52 OPT_LENGTH = (1 << 1),
53 OPT_SECURE = (1 << 2),
54 };
55
Denys Vlasenko22542ec2017-08-08 21:55:02 +020056 opts = getopt32(argv, "^" "o:l:s" "\0" "=1", &offset_str, &length_str);
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010057 argv += optind;
58
59 fd = xopen(argv[0], O_RDWR|O_EXCL);
60//Why bother, BLK[SEC]DISCARD will fail on non-blockdevs anyway?
61// xfstat(fd, &st);
62// if (!S_ISBLK(st.st_mode))
63// bb_error_msg_and_die("%s: not a block device", argv[0]);
64
65 offset = xatoull_sfx(offset_str, kMG_suffixes);
66
67 if (opts & OPT_LENGTH)
68 length = xatoull_sfx(length_str, kMG_suffixes);
69 else {
70 xioctl(fd, BLKGETSIZE64, &length);
71 length -= offset;
72 }
73
74 range[0] = offset;
75 range[1] = length;
76 ioctl_or_perror_and_die(fd,
77 (opts & OPT_SECURE) ? BLKSECDISCARD : BLKDISCARD,
78 &range,
79 "%s: %s failed",
80 argv[0],
81 (opts & OPT_SECURE) ? "BLKSECDISCARD" : "BLKDISCARD"
82 );
83
84 if (ENABLE_FEATURE_CLEAN_UP)
85 close(fd);
86
87 return EXIT_SUCCESS;
88}