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
29
30
31
32
33
34
35
36
37#include "libbb.h"
38#include <linux/fs.h>
39
40#ifndef FITRIM
41struct fstrim_range {
42 uint64_t start;
43 uint64_t len;
44 uint64_t minlen;
45};
46#define FITRIM _IOWR('X', 121, struct fstrim_range)
47#endif
48
49int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
50int fstrim_main(int argc UNUSED_PARAM, char **argv)
51{
52 struct fstrim_range range;
53 char *arg_o, *arg_l, *arg_m, *mp;
54 unsigned opts;
55 int fd;
56
57 enum {
58 OPT_o = (1 << 0),
59 OPT_l = (1 << 1),
60 OPT_m = (1 << 2),
61 OPT_v = (1 << 3),
62 };
63
64#if ENABLE_LONG_OPTS
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"
70 ;
71#endif
72
73 opts = getopt32long(argv, "^"
74 "o:l:m:v"
75 "\0" "=1", fstrim_longopts,
76 &arg_o, &arg_l, &arg_m
77 );
78
79 memset(&range, 0, sizeof(range));
80 range.len = ULLONG_MAX;
81
82 if (opts & OPT_o)
83 range.start = xatoull_sfx(arg_o, kmg_i_suffixes);
84 if (opts & OPT_l)
85 range.len = xatoull_sfx(arg_l, kmg_i_suffixes);
86 if (opts & OPT_m)
87 range.minlen = xatoull_sfx(arg_m, kmg_i_suffixes);
88
89 mp = argv[optind];
90
91
92 fd = xopen_nonblocking(mp);
93
94
95
96
97 xioctl(fd, FITRIM, &range);
98
99 if (ENABLE_FEATURE_CLEAN_UP)
100 close(fd);
101
102 if (opts & OPT_v)
103 printf("%s: %llu bytes trimmed\n", mp, (unsigned long long)range.len);
104 return EXIT_SUCCESS;
105
106 return EXIT_FAILURE;
107}
108