2 * Mini blkdiscard implementation for busybox
4 * Copyright (C) 2015 by Ari Sundholm <ari@tuxera.com> and Tuxera Inc.
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 //config:config BLKDISCARD
9 //config: bool "blkdiscard (5.3 kb)"
12 //config: blkdiscard discards sectors on a given device.
14 //kbuild:lib-$(CONFIG_BLKDISCARD) += blkdiscard.o
15 //applet:IF_BLKDISCARD(APPLET(blkdiscard, BB_DIR_USR_BIN, BB_SUID_DROP))
17 //usage:#define blkdiscard_trivial_usage
18 //usage: "[-o OFS] [-l LEN] [-s] DEVICE"
19 //usage:#define blkdiscard_full_usage "\n\n"
20 //usage: "Discard sectors on DEVICE\n"
21 //usage: "\n -o OFS Byte offset into device"
22 //usage: "\n -l LEN Number of bytes to discard"
23 //usage: "\n -s Perform a secure discard"
25 //usage:#define blkdiscard_example_usage
26 //usage: "$ blkdiscard -o 0 -l 1G /dev/sdb"
32 #define BLKDISCARD 0x1277
35 #define BLKSECDISCARD 0x127d
38 int blkdiscard_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
39 int blkdiscard_main(int argc UNUSED_PARAM, char **argv)
42 const char *offset_str = "0";
43 const char *length_str;
44 uint64_t offset; /* Leaving these two variables out does not */
45 uint64_t length; /* shrink code size and hampers readability. */
51 OPT_OFFSET = (1 << 0),
52 OPT_LENGTH = (1 << 1),
53 OPT_SECURE = (1 << 2),
56 opt_complementary = "=1";
57 opts = getopt32(argv, "o:l:s", &offset_str, &length_str);
60 fd = xopen(argv[0], O_RDWR|O_EXCL);
61 //Why bother, BLK[SEC]DISCARD will fail on non-blockdevs anyway?
63 // if (!S_ISBLK(st.st_mode))
64 // bb_error_msg_and_die("%s: not a block device", argv[0]);
66 offset = xatoull_sfx(offset_str, kMG_suffixes);
68 if (opts & OPT_LENGTH)
69 length = xatoull_sfx(length_str, kMG_suffixes);
71 xioctl(fd, BLKGETSIZE64, &length);
77 ioctl_or_perror_and_die(fd,
78 (opts & OPT_SECURE) ? BLKSECDISCARD : BLKDISCARD,
82 (opts & OPT_SECURE) ? "BLKSECDISCARD" : "BLKDISCARD"
85 if (ENABLE_FEATURE_CLEAN_UP)