2013-11-10 01:57:27 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* fstrim.c - discard the part (or whole) of mounted filesystem.
|
|
|
|
*
|
|
|
|
* 03 March 2012 - Malek Degachi <malek-degachi@laposte.net>
|
|
|
|
* Adapted for busybox from util-linux-2.12a.
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
|
|
|
*/
|
|
|
|
|
2013-11-12 16:39:14 +05:30
|
|
|
//config:config FSTRIM
|
|
|
|
//config: bool "fstrim"
|
|
|
|
//config: default y
|
|
|
|
//config: select PLATFORM_LINUX
|
|
|
|
//config: help
|
|
|
|
//config: Discard unused blocks on a mounted filesystem.
|
|
|
|
|
|
|
|
//applet:IF_FSTRIM(APPLET(fstrim, BB_DIR_SBIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_FSTRIM) += fstrim.o
|
|
|
|
|
2013-11-10 01:57:27 +05:30
|
|
|
//usage:#define fstrim_trivial_usage
|
2013-11-12 16:39:14 +05:30
|
|
|
//usage: "[OPTIONS] MOUNTPOINT"
|
2013-11-10 01:57:27 +05:30
|
|
|
//usage:#define fstrim_full_usage "\n\n"
|
|
|
|
//usage: IF_LONG_OPTS(
|
2017-01-21 07:19:58 +05:30
|
|
|
//usage: " -o,--offset OFFSET Offset in bytes to discard from"
|
|
|
|
//usage: "\n -l,--length LEN Bytes to discard"
|
|
|
|
//usage: "\n -m,--minimum MIN Minimum extent length"
|
2013-11-12 16:39:14 +05:30
|
|
|
//usage: "\n -v,--verbose Print number of discarded bytes"
|
2013-11-10 01:57:27 +05:30
|
|
|
//usage: )
|
|
|
|
//usage: IF_NOT_LONG_OPTS(
|
2013-11-12 16:39:14 +05:30
|
|
|
//usage: " -o OFFSET Offset in bytes to discard from"
|
|
|
|
//usage: "\n -l LEN Bytes to discard"
|
|
|
|
//usage: "\n -m MIN Minimum extent length"
|
2015-02-04 19:49:38 +05:30
|
|
|
//usage: "\n -v Print number of discarded bytes"
|
2013-11-10 01:57:27 +05:30
|
|
|
//usage: )
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
#include <linux/fs.h>
|
|
|
|
|
|
|
|
#ifndef FITRIM
|
|
|
|
struct fstrim_range {
|
|
|
|
uint64_t start;
|
|
|
|
uint64_t len;
|
|
|
|
uint64_t minlen;
|
|
|
|
};
|
|
|
|
#define FITRIM _IOWR('X', 121, struct fstrim_range)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int fstrim_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
struct fstrim_range range;
|
2013-11-11 02:17:02 +05:30
|
|
|
char *arg_o, *arg_l, *arg_m, *mp;
|
2013-11-10 01:57:27 +05:30
|
|
|
unsigned opts;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
OPT_o = (1 << 0),
|
|
|
|
OPT_l = (1 << 1),
|
|
|
|
OPT_m = (1 << 2),
|
|
|
|
OPT_v = (1 << 3),
|
|
|
|
};
|
|
|
|
|
|
|
|
#if ENABLE_LONG_OPTS
|
|
|
|
static const char getopt_longopts[] ALIGN1 =
|
|
|
|
"offset\0" Required_argument "o"
|
|
|
|
"length\0" Required_argument "l"
|
|
|
|
"minimum\0" Required_argument "m"
|
|
|
|
"verbose\0" No_argument "v"
|
|
|
|
;
|
|
|
|
applet_long_options = getopt_longopts;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
opt_complementary = "=1"; /* exactly one non-option arg: the mountpoint */
|
|
|
|
opts = getopt32(argv, "o:l:m:v", &arg_o, &arg_l, &arg_m);
|
|
|
|
|
|
|
|
memset(&range, 0, sizeof(range));
|
|
|
|
range.len = ULLONG_MAX;
|
|
|
|
|
|
|
|
if (opts & OPT_o)
|
2016-12-13 00:26:31 +05:30
|
|
|
range.start = xatoull_sfx(arg_o, kmg_i_suffixes);
|
2013-11-10 01:57:27 +05:30
|
|
|
if (opts & OPT_l)
|
2016-12-13 00:26:31 +05:30
|
|
|
range.len = xatoull_sfx(arg_l, kmg_i_suffixes);
|
2013-11-10 01:57:27 +05:30
|
|
|
if (opts & OPT_m)
|
2016-12-13 00:26:31 +05:30
|
|
|
range.minlen = xatoull_sfx(arg_m, kmg_i_suffixes);
|
2013-11-10 01:57:27 +05:30
|
|
|
|
2013-11-12 16:39:14 +05:30
|
|
|
mp = argv[optind];
|
2013-11-11 02:17:02 +05:30
|
|
|
if (find_block_device(mp)) {
|
|
|
|
fd = xopen_nonblocking(mp);
|
2013-11-10 01:57:27 +05:30
|
|
|
xioctl(fd, FITRIM, &range);
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (opts & OPT_v)
|
2013-11-12 16:39:14 +05:30
|
|
|
printf("%s: %llu bytes trimmed\n", mp, (unsigned long long)range.len);
|
2013-11-10 04:32:43 +05:30
|
|
|
return EXIT_SUCCESS;
|
2013-11-10 01:57:27 +05:30
|
|
|
}
|
2013-11-10 04:32:43 +05:30
|
|
|
return EXIT_FAILURE;
|
2013-11-10 01:57:27 +05:30
|
|
|
}
|