diff options
Diffstat (limited to 'util-linux/fstrim.c')
-rw-r--r-- | util-linux/fstrim.c | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/util-linux/fstrim.c b/util-linux/fstrim.c new file mode 100644 index 000000000..2fa457b5e --- /dev/null +++ b/util-linux/fstrim.c | |||
@@ -0,0 +1,111 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * fstrim.c - discard the part (or whole) of mounted filesystem. | ||
4 | * | ||
5 | * 03 March 2012 - Malek Degachi <malek-degachi@laposte.net> | ||
6 | * Adapted for busybox from util-linux-2.12a. | ||
7 | * | ||
8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | ||
9 | */ | ||
10 | |||
11 | //usage:#define fstrim_trivial_usage | ||
12 | //usage: "[Options] <mountpoint>" | ||
13 | //usage:#define fstrim_full_usage "\n\n" | ||
14 | //usage: "Options:" | ||
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" | ||
20 | //usage: ) | ||
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" | ||
26 | //usage: ) | ||
27 | |||
28 | #include "libbb.h" | ||
29 | #include <linux/fs.h> | ||
30 | |||
31 | #ifndef FITRIM | ||
32 | struct 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 | |||
40 | static const struct suffix_mult fstrim_sfx[] = { | ||
41 | { "KiB", 1024 }, | ||
42 | { "kiB", 1024 }, | ||
43 | { "K", 1024 }, | ||
44 | { "k", 1024 }, | ||
45 | { "MiB", 1048576 }, | ||
46 | { "miB", 1048576 }, | ||
47 | { "M", 1048576 }, | ||
48 | { "m", 1048576 }, | ||
49 | { "GiB", 1073741824 }, | ||
50 | { "giB", 1073741824 }, | ||
51 | { "G", 1073741824 }, | ||
52 | { "g", 1073741824 }, | ||
53 | { "KB", 1000 }, | ||
54 | { "MB", 1000000 }, | ||
55 | { "GB", 1000000000 }, | ||
56 | { "", 0 } | ||
57 | }; | ||
58 | |||
59 | int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
60 | int fstrim_main(int argc UNUSED_PARAM, char **argv) | ||
61 | { | ||
62 | struct fstrim_range range; | ||
63 | char *arg_o; | ||
64 | char *arg_l; | ||
65 | char *arg_m; | ||
66 | unsigned opts; | ||
67 | int fd; | ||
68 | |||
69 | enum { | ||
70 | OPT_o = (1 << 0), | ||
71 | OPT_l = (1 << 1), | ||
72 | OPT_m = (1 << 2), | ||
73 | OPT_v = (1 << 3), | ||
74 | }; | ||
75 | |||
76 | #if ENABLE_LONG_OPTS | ||
77 | static const char getopt_longopts[] ALIGN1 = | ||
78 | "offset\0" Required_argument "o" | ||
79 | "length\0" Required_argument "l" | ||
80 | "minimum\0" Required_argument "m" | ||
81 | "verbose\0" No_argument "v" | ||
82 | ; | ||
83 | applet_long_options = getopt_longopts; | ||
84 | #endif | ||
85 | |||
86 | opt_complementary = "=1"; /* exactly one non-option arg: the mountpoint */ | ||
87 | opts = getopt32(argv, "o:l:m:v", &arg_o, &arg_l, &arg_m); | ||
88 | |||
89 | memset(&range, 0, sizeof(range)); | ||
90 | range.len = ULLONG_MAX; | ||
91 | |||
92 | if (opts & OPT_o) | ||
93 | range.start = xatoull_sfx(arg_o, fstrim_sfx); | ||
94 | if (opts & OPT_l) | ||
95 | range.len = xatoull_sfx(arg_l, fstrim_sfx); | ||
96 | if (opts & OPT_m) | ||
97 | range.minlen = xatoull_sfx(arg_m, fstrim_sfx); | ||
98 | |||
99 | if (find_block_device(argv[optind])) { | ||
100 | fd = xopen_nonblocking(argv[optind]); | ||
101 | xioctl(fd, FITRIM, &range); | ||
102 | if (ENABLE_FEATURE_CLEAN_UP) | ||
103 | close(fd); | ||
104 | |||
105 | if (opts & OPT_v) | ||
106 | printf("%s: %llu bytes were trimmed\n", argv[optind], range.len); | ||
107 | } | ||
108 | |||
109 | return EXIT_SUCCESS; | ||
110 | } | ||
111 | |||