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.
11 //usage:#define fstrim_trivial_usage
12 //usage: "[Options] <mountpoint>"
13 //usage:#define fstrim_full_usage "\n\n"
15 //usage: IF_LONG_OPTS(
16 //usage: "\n -o,--offset=offset offset in bytes to discard from"
17 //usage: "\n -l,--length=length length of bytes to discard from the offset"
18 //usage: "\n -m,--minimum=minimum minimum extent length to discard"
19 //usage: "\n -v,--verbose print number of discarded bytes"
21 //usage: IF_NOT_LONG_OPTS(
22 //usage: "\n -o offset offset in bytes to discard from"
23 //usage: "\n -l length length of bytes to discard from the offset"
24 //usage: "\n -m minimum minimum extent length to discard"
25 //usage: "\n -v, print number of discarded bytes"
37 #define FITRIM _IOWR('X', 121, struct fstrim_range)
40 static const struct suffix_mult fstrim_sfx[] = {
49 { "GiB", 1073741824 },
50 { "giB", 1073741824 },
59 int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
60 int fstrim_main(int argc UNUSED_PARAM, char **argv)
62 struct fstrim_range range;
63 char *arg_o, *arg_l, *arg_m, *bd;
75 static const char getopt_longopts[] ALIGN1 =
76 "offset\0" Required_argument "o"
77 "length\0" Required_argument "l"
78 "minimum\0" Required_argument "m"
79 "verbose\0" No_argument "v"
81 applet_long_options = getopt_longopts;
84 opt_complementary = "=1"; /* exactly one non-option arg: the mountpoint */
85 opts = getopt32(argv, "o:l:m:v", &arg_o, &arg_l, &arg_m);
87 memset(&range, 0, sizeof(range));
88 range.len = ULLONG_MAX;
91 range.start = xatoull_sfx(arg_o, fstrim_sfx);
93 range.len = xatoull_sfx(arg_l, fstrim_sfx);
95 range.minlen = xatoull_sfx(arg_m, fstrim_sfx);
97 bd = find_block_device(*(argv += optind));
99 fd = xopen_nonblocking(bd);
100 xioctl(fd, FITRIM, &range);
101 if (ENABLE_FEATURE_CLEAN_UP)
105 printf("%s: %llu bytes were trimmed\n", bd, range.len);