Ari Sundholm | dbf5a6d | 2016-01-02 01:18:32 +0100 | [diff] [blame] | 1 | /* |
| 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 Sundholm | dbf5a6d | 2016-01-02 01:18:32 +0100 | [diff] [blame] | 8 | //config:config BLKDISCARD |
Denys Vlasenko | ae178ce | 2017-07-19 14:32:54 +0200 | [diff] [blame] | 9 | //config: bool "blkdiscard (5.3 kb)" |
Ari Sundholm | dbf5a6d | 2016-01-02 01:18:32 +0100 | [diff] [blame] | 10 | //config: default y |
James Clarke | 160d027 | 2017-10-07 18:53:19 +0100 | [diff] [blame] | 11 | //config: select PLATFORM_LINUX |
Ari Sundholm | dbf5a6d | 2016-01-02 01:18:32 +0100 | [diff] [blame] | 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: blkdiscard discards sectors on a given device. |
Ari Sundholm | dbf5a6d | 2016-01-02 01:18:32 +0100 | [diff] [blame] | 14 | |
Denys Vlasenko | 277081e | 2017-08-06 20:20:47 +0200 | [diff] [blame] | 15 | //applet:IF_BLKDISCARD(APPLET_NOEXEC(blkdiscard, blkdiscard, BB_DIR_USR_BIN, BB_SUID_DROP, blkdiscard)) |
| 16 | |
Ari Sundholm | dbf5a6d | 2016-01-02 01:18:32 +0100 | [diff] [blame] | 17 | //kbuild:lib-$(CONFIG_BLKDISCARD) += blkdiscard.o |
Ari Sundholm | dbf5a6d | 2016-01-02 01:18:32 +0100 | [diff] [blame] | 18 | |
| 19 | //usage:#define blkdiscard_trivial_usage |
| 20 | //usage: "[-o OFS] [-l LEN] [-s] DEVICE" |
| 21 | //usage:#define blkdiscard_full_usage "\n\n" |
| 22 | //usage: "Discard sectors on DEVICE\n" |
| 23 | //usage: "\n -o OFS Byte offset into device" |
| 24 | //usage: "\n -l LEN Number of bytes to discard" |
| 25 | //usage: "\n -s Perform a secure discard" |
| 26 | //usage: |
| 27 | //usage:#define blkdiscard_example_usage |
| 28 | //usage: "$ blkdiscard -o 0 -l 1G /dev/sdb" |
| 29 | |
| 30 | #include "libbb.h" |
| 31 | #include <linux/fs.h> |
| 32 | |
Denys Vlasenko | 8ea061e | 2017-07-15 13:53:41 +0200 | [diff] [blame] | 33 | #ifndef BLKDISCARD |
| 34 | #define BLKDISCARD 0x1277 |
| 35 | #endif |
| 36 | #ifndef BLKSECDISCARD |
| 37 | #define BLKSECDISCARD 0x127d |
| 38 | #endif |
| 39 | |
Ari Sundholm | dbf5a6d | 2016-01-02 01:18:32 +0100 | [diff] [blame] | 40 | int blkdiscard_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 41 | int blkdiscard_main(int argc UNUSED_PARAM, char **argv) |
| 42 | { |
| 43 | unsigned opts; |
| 44 | const char *offset_str = "0"; |
| 45 | const char *length_str; |
| 46 | uint64_t offset; /* Leaving these two variables out does not */ |
| 47 | uint64_t length; /* shrink code size and hampers readability. */ |
| 48 | uint64_t range[2]; |
Ari Sundholm | dbf5a6d | 2016-01-02 01:18:32 +0100 | [diff] [blame] | 49 | int fd; |
| 50 | |
| 51 | enum { |
| 52 | OPT_OFFSET = (1 << 0), |
| 53 | OPT_LENGTH = (1 << 1), |
| 54 | OPT_SECURE = (1 << 2), |
| 55 | }; |
| 56 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 57 | opts = getopt32(argv, "^" "o:l:s" "\0" "=1", &offset_str, &length_str); |
Ari Sundholm | dbf5a6d | 2016-01-02 01:18:32 +0100 | [diff] [blame] | 58 | argv += optind; |
| 59 | |
| 60 | fd = xopen(argv[0], O_RDWR|O_EXCL); |
| 61 | //Why bother, BLK[SEC]DISCARD will fail on non-blockdevs anyway? |
| 62 | // xfstat(fd, &st); |
| 63 | // if (!S_ISBLK(st.st_mode)) |
| 64 | // bb_error_msg_and_die("%s: not a block device", argv[0]); |
| 65 | |
| 66 | offset = xatoull_sfx(offset_str, kMG_suffixes); |
| 67 | |
| 68 | if (opts & OPT_LENGTH) |
| 69 | length = xatoull_sfx(length_str, kMG_suffixes); |
| 70 | else { |
| 71 | xioctl(fd, BLKGETSIZE64, &length); |
| 72 | length -= offset; |
| 73 | } |
| 74 | |
| 75 | range[0] = offset; |
| 76 | range[1] = length; |
| 77 | ioctl_or_perror_and_die(fd, |
| 78 | (opts & OPT_SECURE) ? BLKSECDISCARD : BLKDISCARD, |
| 79 | &range, |
| 80 | "%s: %s failed", |
| 81 | argv[0], |
| 82 | (opts & OPT_SECURE) ? "BLKSECDISCARD" : "BLKDISCARD" |
| 83 | ); |
| 84 | |
| 85 | if (ENABLE_FEATURE_CLEAN_UP) |
| 86 | close(fd); |
| 87 | |
| 88 | return EXIT_SUCCESS; |
| 89 | } |