summaryrefslogtreecommitdiffstats
path: root/sys-utils/fstrim.c
diff options
context:
space:
mode:
authorLukas Czerner2010-11-25 00:05:37 +0100
committerKarel Zak2010-11-26 14:59:01 +0100
commitd9e2d0dd144c2c06bff590c35400965016e55b7c (patch)
tree2950718e7e64a4c720477426adb53ff7d9c37b88 /sys-utils/fstrim.c
parentlibblkid: 256MiB is min size of btrfs (diff)
downloadkernel-qcow2-util-linux-d9e2d0dd144c2c06bff590c35400965016e55b7c.tar.gz
kernel-qcow2-util-linux-d9e2d0dd144c2c06bff590c35400965016e55b7c.tar.xz
kernel-qcow2-util-linux-d9e2d0dd144c2c06bff590c35400965016e55b7c.zip
fstrim: add new command
fstrim is used on a mounted filesystem to discard (or "trim") blocks which are not in use by the filesystem. This is useful for solid-state drives (SSDs) and thinly-provisioned storage Signed-off-by: Lukas Czerner <lczerner@redhat.com> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'sys-utils/fstrim.c')
-rw-r--r--sys-utils/fstrim.c154
1 files changed, 154 insertions, 0 deletions
diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c
new file mode 100644
index 000000000..fd83d3111
--- /dev/null
+++ b/sys-utils/fstrim.c
@@ -0,0 +1,154 @@
+/*
+ * fstrim.c -- discard the part (or whole) of mounted filesystem.
+ *
+ * Copyright (C) 2010 Red Hat, Inc. All rights reserved.
+ * Written by Lukas Czerner <lczerner@redhat.com>
+ * Karel Zak <kzak@redhat.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * This program uses FITRIM ioctl to discard parts or the whole filesystem
+ * online (mounted). You can specify range (start and length) to be
+ * discarded, or simply discard while filesystem.
+ */
+
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <getopt.h>
+#include <err.h>
+#include <error.h>
+#include <errno.h>
+
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <linux/fs.h>
+
+#include "nls.h"
+#include "strutils.h"
+
+#ifndef FITRIM
+struct fstrim_range {
+ uint64_t start;
+ uint64_t len;
+ uint64_t minlen;
+};
+#define FITRIM _IOWR('X', 121, struct fstrim_range)
+#endif
+
+static void __attribute__((__noreturn__)) usage(FILE *out)
+{
+ fprintf(out, _("Usage: %s [options] <mount point>\n\nOptions:\n"),
+ program_invocation_short_name);
+
+ fprintf(out, _(
+ " -h, --help this help\n"
+ " -o, --offset <num> offset in bytes to discard from\n"
+ " -l, --length <num> length of bytes to discard from the offset\n"
+ " -m, --minimum <num> minimum extent length to discard\n"
+ " -v, --verbose print number of discarded bytes\n"));
+
+ fprintf(out, _("\nFor more information see fstrim(1).\n"));
+
+ exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+int main(int argc, char **argv)
+{
+ char *path;
+ int c, fd, verbose = 0;
+ struct fstrim_range range;
+ struct stat sb;
+
+ struct option longopts[] = {
+ { "help", 0, 0, 'h' },
+ { "offset", 1, 0, 'o' },
+ { "length", 1, 0, 'l' },
+ { "minimum", 1, 0, 'm' },
+ { "verbose", 0, 0, 'v' },
+ { NULL, 0, 0, 0 }
+ };
+
+ setlocale(LC_ALL, "");
+ bindtextdomain(PACKAGE, LOCALEDIR);
+ textdomain(PACKAGE);
+
+ memset(&range, 0, sizeof(range));
+ range.len = ULLONG_MAX;
+
+ while ((c = getopt_long(argc, argv, "ho:l:m:v", longopts, NULL)) != -1) {
+ switch(c) {
+ case 'h':
+ usage(stdout);
+ break;
+ case 'l':
+ if (strtosize(optarg, &range.len))
+ errx(EXIT_FAILURE,
+ _("failed to parse length: %s"), optarg);
+ break;
+ case 'o':
+ if (strtosize(optarg, &range.start))
+ errx(EXIT_FAILURE,
+ _("failed to parse offset: %s"), optarg);
+ break;
+ case 'm':
+ if (strtosize(optarg, &range.minlen))
+ errx(EXIT_FAILURE,
+ _("failed to parse minimal extend length: %s"),
+ optarg);
+ break;
+ case 'v':
+ verbose = 1;
+ break;
+ default:
+ usage(stderr);
+ break;
+ }
+ }
+
+ if (optind == argc)
+ errx(EXIT_FAILURE, _("no mountpoint specified."));
+
+ path = argv[optind++];
+
+ if (optind != argc) {
+ warnx(_("unexpected number of arguments"));
+ usage(stderr);
+ }
+
+ if (stat(path, &sb) == -1)
+ err(EXIT_FAILURE, _("%s: stat failed"), path);
+ if (!S_ISDIR(sb.st_mode))
+ errx(EXIT_FAILURE, _("%s: not a directory"), path);
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ err(EXIT_FAILURE, _("%s: open failed"), path);
+
+ if (ioctl(fd, FITRIM, &range)) {
+ int errsv = errno;
+ close(fd);
+ error(EXIT_FAILURE, errsv, _("%s: FITRIM ioctl failed"), path);
+ }
+ if (verbose)
+ printf(_("%s: %" PRIu64 " bytes was trimmed\n"),
+ path, range.len);
+ close(fd);
+ return EXIT_SUCCESS;
+}