1 /* vi: set sw=4 ts=4: */
3 * fstrim.c - discard the part (or whole) of mounted filesystem.
5 * 03 March 2012 - Malek Degachi <malek-degachi@laposte.net>
6 * Adapted for busybox from util-linux-2.12a.
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config:config FSTRIM
11 //config: bool "fstrim (4.4 kb)"
13 //config: select PLATFORM_LINUX
15 //config: Discard unused blocks on a mounted filesystem.
17 //applet:IF_FSTRIM(APPLET_NOEXEC(fstrim, fstrim, BB_DIR_SBIN, BB_SUID_DROP, fstrim))
19 //kbuild:lib-$(CONFIG_FSTRIM) += fstrim.o
21 //usage:#define fstrim_trivial_usage
22 //usage: "[OPTIONS] MOUNTPOINT"
23 //usage:#define fstrim_full_usage "\n\n"
24 //usage: IF_LONG_OPTS(
25 //usage: " -o,--offset OFFSET Offset in bytes to discard from"
26 //usage: "\n -l,--length LEN Bytes to discard"
27 //usage: "\n -m,--minimum MIN Minimum extent length"
28 //usage: "\n -v,--verbose Print number of discarded bytes"
30 //usage: IF_NOT_LONG_OPTS(
31 //usage: " -o OFFSET Offset in bytes to discard from"
32 //usage: "\n -l LEN Bytes to discard"
33 //usage: "\n -m MIN Minimum extent length"
34 //usage: "\n -v Print number of discarded bytes"
46 #define FITRIM _IOWR('X', 121, struct fstrim_range)
49 int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
50 int fstrim_main(int argc UNUSED_PARAM, char **argv)
52 struct fstrim_range range;
53 char *arg_o, *arg_l, *arg_m, *mp;
65 static const char fstrim_longopts[] ALIGN1 =
66 "offset\0" Required_argument "o"
67 "length\0" Required_argument "l"
68 "minimum\0" Required_argument "m"
69 "verbose\0" No_argument "v"
73 opts = getopt32long(argv, "^"
75 "\0" "=1", fstrim_longopts,
76 &arg_o, &arg_l, &arg_m
79 memset(&range, 0, sizeof(range));
80 range.len = ULLONG_MAX;
83 range.start = xatoull_sfx(arg_o, kmg_i_suffixes);
85 range.len = xatoull_sfx(arg_l, kmg_i_suffixes);
87 range.minlen = xatoull_sfx(arg_m, kmg_i_suffixes);
90 //Wwhy bother checking that it's a blockdev?
91 // if (find_block_device(mp)) {
92 fd = xopen_nonblocking(mp);
94 /* On ENOTTY error, util-linux 2.31 says:
95 * "fstrim: FILE: the discard operation is not supported"
97 xioctl(fd, FITRIM, &range);
99 if (ENABLE_FEATURE_CLEAN_UP)
103 printf("%s: %llu bytes trimmed\n", mp, (unsigned long long)range.len);