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 //config:config FSTRIM
12 //config: bool "fstrim"
14 //config: select PLATFORM_LINUX
16 //config: Discard unused blocks on a mounted filesystem.
18 //applet:IF_FSTRIM(APPLET(fstrim, BB_DIR_SBIN, BB_SUID_DROP))
20 //kbuild:lib-$(CONFIG_FSTRIM) += fstrim.o
22 //usage:#define fstrim_trivial_usage
23 //usage: "[OPTIONS] MOUNTPOINT"
24 //usage:#define fstrim_full_usage "\n\n"
25 //usage: IF_LONG_OPTS(
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"
29 //usage: "\n -v,--verbose Print number of discarded bytes"
31 //usage: IF_NOT_LONG_OPTS(
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"
35 //usage: "\n -v Print number of discarded bytes"
47 #define FITRIM _IOWR('X', 121, struct fstrim_range)
50 static const struct suffix_mult fstrim_sfx
[] = {
59 { "GiB", 1073741824 },
60 { "giB", 1073741824 },
69 int fstrim_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
70 int fstrim_main(int argc UNUSED_PARAM
, char **argv
)
72 struct fstrim_range range
;
73 char *arg_o
, *arg_l
, *arg_m
, *mp
;
85 static const char getopt_longopts
[] ALIGN1
=
86 "offset\0" Required_argument
"o"
87 "length\0" Required_argument
"l"
88 "minimum\0" Required_argument
"m"
89 "verbose\0" No_argument
"v"
91 applet_long_options
= getopt_longopts
;
94 opt_complementary
= "=1"; /* exactly one non-option arg: the mountpoint */
95 opts
= getopt32(argv
, "o:l:m:v", &arg_o
, &arg_l
, &arg_m
);
97 memset(&range
, 0, sizeof(range
));
98 range
.len
= ULLONG_MAX
;
101 range
.start
= xatoull_sfx(arg_o
, fstrim_sfx
);
103 range
.len
= xatoull_sfx(arg_l
, fstrim_sfx
);
105 range
.minlen
= xatoull_sfx(arg_m
, fstrim_sfx
);
108 if (find_block_device(mp
)) {
109 fd
= xopen_nonblocking(mp
);
110 xioctl(fd
, FITRIM
, &range
);
111 if (ENABLE_FEATURE_CLEAN_UP
)
115 printf("%s: %llu bytes trimmed\n", mp
, (unsigned long long)range
.len
);