blob: 675a02184e177848b389ac2d1d15166695ee36d7 [file] [log] [blame]
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +02001/* 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
Denys Vlasenkoeddb8342013-11-12 12:09:14 +010011//config:config FSTRIM
12//config: bool "fstrim"
13//config: default y
14//config: select PLATFORM_LINUX
15//config: help
16//config: Discard unused blocks on a mounted filesystem.
17
18//applet:IF_FSTRIM(APPLET(fstrim, BB_DIR_SBIN, BB_SUID_DROP))
19
20//kbuild:lib-$(CONFIG_FSTRIM) += fstrim.o
21
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +020022//usage:#define fstrim_trivial_usage
Denys Vlasenkoeddb8342013-11-12 12:09:14 +010023//usage: "[OPTIONS] MOUNTPOINT"
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +020024//usage:#define fstrim_full_usage "\n\n"
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +020025//usage: IF_LONG_OPTS(
Denys Vlasenkoeddb8342013-11-12 12:09:14 +010026//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"
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +020030//usage: )
31//usage: IF_NOT_LONG_OPTS(
Denys Vlasenkoeddb8342013-11-12 12:09:14 +010032//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"
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +020036//usage: )
37
38#include "libbb.h"
39#include <linux/fs.h>
40
41#ifndef FITRIM
42struct fstrim_range {
43 uint64_t start;
44 uint64_t len;
45 uint64_t minlen;
46};
47#define FITRIM _IOWR('X', 121, struct fstrim_range)
48#endif
49
50static const struct suffix_mult fstrim_sfx[] = {
51 { "KiB", 1024 },
52 { "kiB", 1024 },
53 { "K", 1024 },
54 { "k", 1024 },
55 { "MiB", 1048576 },
56 { "miB", 1048576 },
57 { "M", 1048576 },
58 { "m", 1048576 },
59 { "GiB", 1073741824 },
60 { "giB", 1073741824 },
61 { "G", 1073741824 },
62 { "g", 1073741824 },
63 { "KB", 1000 },
64 { "MB", 1000000 },
65 { "GB", 1000000000 },
66 { "", 0 }
67};
68
69int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
70int fstrim_main(int argc UNUSED_PARAM, char **argv)
71{
72 struct fstrim_range range;
Bernhard Reutner-Fischerbb19d252013-11-10 21:47:02 +010073 char *arg_o, *arg_l, *arg_m, *mp;
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +020074 unsigned opts;
75 int fd;
76
77 enum {
78 OPT_o = (1 << 0),
79 OPT_l = (1 << 1),
80 OPT_m = (1 << 2),
81 OPT_v = (1 << 3),
82 };
83
84#if ENABLE_LONG_OPTS
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"
90 ;
91 applet_long_options = getopt_longopts;
92#endif
93
Bernhard Reutner-Fischerbb19d252013-11-10 21:47:02 +010094 opt_complementary = "=1"; /* exactly one non-option arg: the mountpoint */
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +020095 opts = getopt32(argv, "o:l:m:v", &arg_o, &arg_l, &arg_m);
96
97 memset(&range, 0, sizeof(range));
98 range.len = ULLONG_MAX;
99
Bernhard Reutner-Fischerbb19d252013-11-10 21:47:02 +0100100 if (opts & OPT_o)
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +0200101 range.start = xatoull_sfx(arg_o, fstrim_sfx);
Bernhard Reutner-Fischerbb19d252013-11-10 21:47:02 +0100102 if (opts & OPT_l)
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +0200103 range.len = xatoull_sfx(arg_l, fstrim_sfx);
Bernhard Reutner-Fischerbb19d252013-11-10 21:47:02 +0100104 if (opts & OPT_m)
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +0200105 range.minlen = xatoull_sfx(arg_m, fstrim_sfx);
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +0200106
Denys Vlasenkoeddb8342013-11-12 12:09:14 +0100107 mp = argv[optind];
Bernhard Reutner-Fischerbb19d252013-11-10 21:47:02 +0100108 if (find_block_device(mp)) {
109 fd = xopen_nonblocking(mp);
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +0200110 xioctl(fd, FITRIM, &range);
Bernhard Reutner-Fischerbb19d252013-11-10 21:47:02 +0100111 if (ENABLE_FEATURE_CLEAN_UP)
112 close(fd);
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +0200113
114 if (opts & OPT_v)
Denys Vlasenkoeddb8342013-11-12 12:09:14 +0100115 printf("%s: %llu bytes trimmed\n", mp, (unsigned long long)range.len);
Bernhard Reutner-Fischerbb19d252013-11-10 21:47:02 +0100116 return EXIT_SUCCESS;
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +0200117 }
Bernhard Reutner-Fischerbb19d252013-11-10 21:47:02 +0100118 return EXIT_FAILURE;
Eugene San (eugenesan)886ed402013-01-27 10:45:06 +0200119}