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 | */ |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 10 | //config:config FSTRIM |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 11 | //config: bool "fstrim (4.4 kb)" |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 12 | //config: default y |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: Discard unused blocks on a mounted filesystem. |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 15 | |
Denys Vlasenko | 9f59849 | 2017-08-05 01:29:12 +0200 | [diff] [blame] | 16 | //applet:IF_FSTRIM(APPLET_NOEXEC(fstrim, fstrim, BB_DIR_SBIN, BB_SUID_DROP, fstrim)) |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 17 | |
| 18 | //kbuild:lib-$(CONFIG_FSTRIM) += fstrim.o |
| 19 | |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 20 | //usage:#define fstrim_trivial_usage |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 21 | //usage: "[OPTIONS] MOUNTPOINT" |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 22 | //usage:#define fstrim_full_usage "\n\n" |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 23 | //usage: " -o OFFSET Offset in bytes to discard from" |
| 24 | //usage: "\n -l LEN Bytes to discard" |
| 25 | //usage: "\n -m MIN Minimum extent length" |
Denys Vlasenko | 3c5d133 | 2015-02-04 15:19:38 +0100 | [diff] [blame] | 26 | //usage: "\n -v Print number of discarded bytes" |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 27 | |
| 28 | #include "libbb.h" |
| 29 | #include <linux/fs.h> |
| 30 | |
| 31 | #ifndef FITRIM |
| 32 | struct fstrim_range { |
| 33 | uint64_t start; |
| 34 | uint64_t len; |
| 35 | uint64_t minlen; |
| 36 | }; |
| 37 | #define FITRIM _IOWR('X', 121, struct fstrim_range) |
| 38 | #endif |
| 39 | |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 40 | int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 41 | int fstrim_main(int argc UNUSED_PARAM, char **argv) |
| 42 | { |
| 43 | struct fstrim_range range; |
Bernhard Reutner-Fischer | 5e63776 | 2013-11-10 21:47:02 +0100 | [diff] [blame] | 44 | char *arg_o, *arg_l, *arg_m, *mp; |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 45 | unsigned opts; |
| 46 | int fd; |
| 47 | |
| 48 | enum { |
| 49 | OPT_o = (1 << 0), |
| 50 | OPT_l = (1 << 1), |
| 51 | OPT_m = (1 << 2), |
| 52 | OPT_v = (1 << 3), |
| 53 | }; |
| 54 | |
| 55 | #if ENABLE_LONG_OPTS |
Denys Vlasenko | 00677b5 | 2017-08-08 14:59:35 +0200 | [diff] [blame] | 56 | static const char fstrim_longopts[] ALIGN1 = |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 57 | "offset\0" Required_argument "o" |
| 58 | "length\0" Required_argument "l" |
| 59 | "minimum\0" Required_argument "m" |
| 60 | "verbose\0" No_argument "v" |
| 61 | ; |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 62 | #endif |
| 63 | |
Denys Vlasenko | 3be4b9b | 2018-03-29 16:00:30 +0200 | [diff] [blame] | 64 | opts = getopt32long(argv, "^" |
| 65 | "o:l:m:v" |
| 66 | "\0" "=1", fstrim_longopts, |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 67 | &arg_o, &arg_l, &arg_m |
| 68 | ); |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 69 | |
| 70 | memset(&range, 0, sizeof(range)); |
| 71 | range.len = ULLONG_MAX; |
| 72 | |
| 73 | if (opts & OPT_o) |
Denys Vlasenko | e184a88 | 2016-12-12 19:56:31 +0100 | [diff] [blame] | 74 | range.start = xatoull_sfx(arg_o, kmg_i_suffixes); |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 75 | if (opts & OPT_l) |
Denys Vlasenko | e184a88 | 2016-12-12 19:56:31 +0100 | [diff] [blame] | 76 | range.len = xatoull_sfx(arg_l, kmg_i_suffixes); |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 77 | if (opts & OPT_m) |
Denys Vlasenko | e184a88 | 2016-12-12 19:56:31 +0100 | [diff] [blame] | 78 | range.minlen = xatoull_sfx(arg_m, kmg_i_suffixes); |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 79 | |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 80 | mp = argv[optind]; |
Denys Vlasenko | 3be4b9b | 2018-03-29 16:00:30 +0200 | [diff] [blame] | 81 | //Wwhy bother checking that it's a blockdev? |
| 82 | // if (find_block_device(mp)) { |
Bernhard Reutner-Fischer | 5e63776 | 2013-11-10 21:47:02 +0100 | [diff] [blame] | 83 | fd = xopen_nonblocking(mp); |
Denys Vlasenko | 3be4b9b | 2018-03-29 16:00:30 +0200 | [diff] [blame] | 84 | |
| 85 | /* On ENOTTY error, util-linux 2.31 says: |
| 86 | * "fstrim: FILE: the discard operation is not supported" |
| 87 | */ |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 88 | xioctl(fd, FITRIM, &range); |
Denys Vlasenko | 3be4b9b | 2018-03-29 16:00:30 +0200 | [diff] [blame] | 89 | |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 90 | if (ENABLE_FEATURE_CLEAN_UP) |
| 91 | close(fd); |
| 92 | |
| 93 | if (opts & OPT_v) |
Denys Vlasenko | cd0936b | 2013-11-12 12:09:14 +0100 | [diff] [blame] | 94 | 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] | 95 | return EXIT_SUCCESS; |
Denys Vlasenko | 3be4b9b | 2018-03-29 16:00:30 +0200 | [diff] [blame] | 96 | // } |
Bernhard Reutner-Fischer | b1489f9 | 2013-11-10 00:02:43 +0100 | [diff] [blame] | 97 | return EXIT_FAILURE; |
Malek Degachi | 1d39e33 | 2013-11-09 21:27:27 +0100 | [diff] [blame] | 98 | } |