1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include "libbb.h"
29#include <linux/fs.h>
30
31#ifndef FITRIM
32struct 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
40int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
41int fstrim_main(int argc UNUSED_PARAM, char **argv)
42{
43 struct fstrim_range range;
44 char *arg_o, *arg_l, *arg_m, *mp;
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
56 static const char fstrim_longopts[] ALIGN1 =
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 ;
62#endif
63
64 opts = getopt32long(argv, "^"
65 "o:l:m:v"
66 "\0" "=1", fstrim_longopts,
67 &arg_o, &arg_l, &arg_m
68 );
69
70 memset(&range, 0, sizeof(range));
71 range.len = ULLONG_MAX;
72
73 if (opts & OPT_o)
74 range.start = xatoull_sfx(arg_o, kmg_i_suffixes);
75 if (opts & OPT_l)
76 range.len = xatoull_sfx(arg_l, kmg_i_suffixes);
77 if (opts & OPT_m)
78 range.minlen = xatoull_sfx(arg_m, kmg_i_suffixes);
79
80 mp = argv[optind];
81
82
83 fd = xopen_nonblocking(mp);
84
85
86
87
88 xioctl(fd, FITRIM, &range);
89
90 if (ENABLE_FEATURE_CLEAN_UP)
91 close(fd);
92
93 if (opts & OPT_v)
94 printf("%s: %llu bytes trimmed\n", mp, (unsigned long long)range.len);
95 return EXIT_SUCCESS;
96
97 return EXIT_FAILURE;
98}
99