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