Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * fstrim.c - discard the part (or whole) of mounted filesystem. |
| 4 | * |
| 5 | * 03 March 2012 - Malek Degachi <malek-degachi@laposte.net> |
| 6 | * Adapted for busybox from util-linux-2.12a. |
| 7 | * |
| 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 9 | */ |
| 10 | |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 11 | //config:config FSTRIM |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 12 | //config: bool "fstrim (5.5 kb)" |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 13 | //config: default y |
| 14 | //config: select PLATFORM_LINUX |
| 15 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | //config: Discard unused blocks on a mounted filesystem. |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 17 | |
| 18 | //applet:IF_FSTRIM(APPLET(fstrim, BB_DIR_SBIN, BB_SUID_DROP)) |
| 19 | |
| 20 | //kbuild:lib-$(CONFIG_FSTRIM) += fstrim.o |
| 21 | |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 22 | //usage:#define fstrim_trivial_usage |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 23 | //usage: "[OPTIONS] MOUNTPOINT" |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 24 | //usage:#define fstrim_full_usage "\n\n" |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 25 | //usage: IF_LONG_OPTS( |
Denys Vlasenko | bbc7bee | 2017-01-21 02:49:58 +0100 | [diff] [blame] | 26 | //usage: " -o,--offset OFFSET Offset in bytes to discard from" |
| 27 | //usage: "\n -l,--length LEN Bytes to discard" |
| 28 | //usage: "\n -m,--minimum MIN Minimum extent length" |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 29 | //usage: "\n -v,--verbose Print number of discarded bytes" |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 30 | //usage: ) |
| 31 | //usage: IF_NOT_LONG_OPTS( |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 32 | //usage: " -o OFFSET Offset in bytes to discard from" |
| 33 | //usage: "\n -l LEN Bytes to discard" |
| 34 | //usage: "\n -m MIN Minimum extent length" |
Denys Vlasenko | 3c5d133 | 2015-02-04 15:19:38 +0100 | [diff] [blame] | 35 | //usage: "\n -v Print number of discarded bytes" |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 36 | //usage: ) |
| 37 | |
| 38 | #include "libbb.h" |
| 39 | #include <linux/fs.h> |
| 40 | |
| 41 | #ifndef FITRIM |
| 42 | struct fstrim_range { |
| 43 | uint64_t start; |
| 44 | uint64_t len; |
| 45 | uint64_t minlen; |
| 46 | }; |
| 47 | #define FITRIM _IOWR('X', 121, struct fstrim_range) |
| 48 | #endif |
| 49 | |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 50 | int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 51 | int fstrim_main(int argc UNUSED_PARAM, char **argv) |
| 52 | { |
| 53 | struct fstrim_range range; |
Bernhard Reutner-Fischer | 5e63776 | 2013-11-10 21:47:02 +0100 | [diff] [blame] | 54 | char *arg_o, *arg_l, *arg_m, *mp; |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 55 | unsigned opts; |
| 56 | int fd; |
| 57 | |
| 58 | enum { |
| 59 | OPT_o = (1 << 0), |
| 60 | OPT_l = (1 << 1), |
| 61 | OPT_m = (1 << 2), |
| 62 | OPT_v = (1 << 3), |
| 63 | }; |
| 64 | |
| 65 | #if ENABLE_LONG_OPTS |
| 66 | static const char getopt_longopts[] ALIGN1 = |
| 67 | "offset\0" Required_argument "o" |
| 68 | "length\0" Required_argument "l" |
| 69 | "minimum\0" Required_argument "m" |
| 70 | "verbose\0" No_argument "v" |
| 71 | ; |
| 72 | applet_long_options = getopt_longopts; |
| 73 | #endif |
| 74 | |
| 75 | opt_complementary = "=1"; /* exactly one non-option arg: the mountpoint */ |
| 76 | opts = getopt32(argv, "o:l:m:v", &arg_o, &arg_l, &arg_m); |
| 77 | |
| 78 | memset(&range, 0, sizeof(range)); |
| 79 | range.len = ULLONG_MAX; |
| 80 | |
| 81 | if (opts & OPT_o) |
Denys Vlasenko | e184a88 | 2016-12-12 19:56:31 +0100 | [diff] [blame] | 82 | range.start = xatoull_sfx(arg_o, kmg_i_suffixes); |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 83 | if (opts & OPT_l) |
Denys Vlasenko | e184a88 | 2016-12-12 19:56:31 +0100 | [diff] [blame] | 84 | range.len = xatoull_sfx(arg_l, kmg_i_suffixes); |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 85 | if (opts & OPT_m) |
Denys Vlasenko | e184a88 | 2016-12-12 19:56:31 +0100 | [diff] [blame] | 86 | range.minlen = xatoull_sfx(arg_m, kmg_i_suffixes); |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 87 | |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 88 | mp = argv[optind]; |
Bernhard Reutner-Fischer | 5e63776 | 2013-11-10 21:47:02 +0100 | [diff] [blame] | 89 | if (find_block_device(mp)) { |
| 90 | fd = xopen_nonblocking(mp); |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 91 | xioctl(fd, FITRIM, &range); |
| 92 | if (ENABLE_FEATURE_CLEAN_UP) |
| 93 | close(fd); |
| 94 | |
| 95 | if (opts & OPT_v) |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 96 | printf("%s: %llu bytes trimmed\n", mp, (unsigned long long)range.len); |
Bernhard Reutner-Fischer | b1489f9 | 2013-11-10 00:02:43 +0100 | [diff] [blame] | 97 | return EXIT_SUCCESS; |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 98 | } |
Bernhard Reutner-Fischer | b1489f9 | 2013-11-10 00:02:43 +0100 | [diff] [blame] | 99 | return EXIT_FAILURE; |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 100 | } |