blob: 205d1e42b8eaaa747fa8ad5479163c790d32a637 [file] [log] [blame]
Malek Degachi1d39e332013-11-09 21:27:27 +01001/* 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 */
Denys Vlasenkocd0936b2013-11-12 12:09:14 +010010//config:config FSTRIM
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "fstrim (5.5 kb)"
Denys Vlasenkocd0936b2013-11-12 12:09:14 +010012//config: default y
13//config: select PLATFORM_LINUX
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: Discard unused blocks on a mounted filesystem.
Denys Vlasenkocd0936b2013-11-12 12:09:14 +010016
Denys Vlasenko9f598492017-08-05 01:29:12 +020017//applet:IF_FSTRIM(APPLET_NOEXEC(fstrim, fstrim, BB_DIR_SBIN, BB_SUID_DROP, fstrim))
Denys Vlasenkocd0936b2013-11-12 12:09:14 +010018
19//kbuild:lib-$(CONFIG_FSTRIM) += fstrim.o
20
Malek Degachi1d39e332013-11-09 21:27:27 +010021//usage:#define fstrim_trivial_usage
Denys Vlasenkocd0936b2013-11-12 12:09:14 +010022//usage: "[OPTIONS] MOUNTPOINT"
Malek Degachi1d39e332013-11-09 21:27:27 +010023//usage:#define fstrim_full_usage "\n\n"
Malek Degachi1d39e332013-11-09 21:27:27 +010024//usage: IF_LONG_OPTS(
Denys Vlasenkobbc7bee2017-01-21 02:49:58 +010025//usage: " -o,--offset OFFSET Offset in bytes to discard from"
26//usage: "\n -l,--length LEN Bytes to discard"
27//usage: "\n -m,--minimum MIN Minimum extent length"
Denys Vlasenkocd0936b2013-11-12 12:09:14 +010028//usage: "\n -v,--verbose Print number of discarded bytes"
Malek Degachi1d39e332013-11-09 21:27:27 +010029//usage: )
30//usage: IF_NOT_LONG_OPTS(
Denys Vlasenkocd0936b2013-11-12 12:09:14 +010031//usage: " -o OFFSET Offset in bytes to discard from"
32//usage: "\n -l LEN Bytes to discard"
33//usage: "\n -m MIN Minimum extent length"
Denys Vlasenko3c5d1332015-02-04 15:19:38 +010034//usage: "\n -v Print number of discarded bytes"
Malek Degachi1d39e332013-11-09 21:27:27 +010035//usage: )
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
Malek Degachi1d39e332013-11-09 21:27:27 +010049int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
50int fstrim_main(int argc UNUSED_PARAM, char **argv)
51{
52 struct fstrim_range range;
Bernhard Reutner-Fischer5e637762013-11-10 21:47:02 +010053 char *arg_o, *arg_l, *arg_m, *mp;
Malek Degachi1d39e332013-11-09 21:27:27 +010054 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
Denys Vlasenko00677b52017-08-08 14:59:35 +020065 static const char fstrim_longopts[] ALIGN1 =
Malek Degachi1d39e332013-11-09 21:27:27 +010066 "offset\0" Required_argument "o"
67 "length\0" Required_argument "l"
68 "minimum\0" Required_argument "m"
69 "verbose\0" No_argument "v"
70 ;
Malek Degachi1d39e332013-11-09 21:27:27 +010071#endif
72
Denys Vlasenko22542ec2017-08-08 21:55:02 +020073 opts = getopt32long(argv, "^" "o:l:m:v" "\0" "=1", fstrim_longopts,
74 &arg_o, &arg_l, &arg_m
75 );
Malek Degachi1d39e332013-11-09 21:27:27 +010076
77 memset(&range, 0, sizeof(range));
78 range.len = ULLONG_MAX;
79
80 if (opts & OPT_o)
Denys Vlasenkoe184a882016-12-12 19:56:31 +010081 range.start = xatoull_sfx(arg_o, kmg_i_suffixes);
Malek Degachi1d39e332013-11-09 21:27:27 +010082 if (opts & OPT_l)
Denys Vlasenkoe184a882016-12-12 19:56:31 +010083 range.len = xatoull_sfx(arg_l, kmg_i_suffixes);
Malek Degachi1d39e332013-11-09 21:27:27 +010084 if (opts & OPT_m)
Denys Vlasenkoe184a882016-12-12 19:56:31 +010085 range.minlen = xatoull_sfx(arg_m, kmg_i_suffixes);
Malek Degachi1d39e332013-11-09 21:27:27 +010086
Denys Vlasenkocd0936b2013-11-12 12:09:14 +010087 mp = argv[optind];
Bernhard Reutner-Fischer5e637762013-11-10 21:47:02 +010088 if (find_block_device(mp)) {
89 fd = xopen_nonblocking(mp);
Malek Degachi1d39e332013-11-09 21:27:27 +010090 xioctl(fd, FITRIM, &range);
91 if (ENABLE_FEATURE_CLEAN_UP)
92 close(fd);
93
94 if (opts & OPT_v)
Denys Vlasenkocd0936b2013-11-12 12:09:14 +010095 printf("%s: %llu bytes trimmed\n", mp, (unsigned long long)range.len);
Bernhard Reutner-Fischerb1489f92013-11-10 00:02:43 +010096 return EXIT_SUCCESS;
Malek Degachi1d39e332013-11-09 21:27:27 +010097 }
Bernhard Reutner-Fischerb1489f92013-11-10 00:02:43 +010098 return EXIT_FAILURE;
Malek Degachi1d39e332013-11-09 21:27:27 +010099}