]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs_scrub: fstrim the free areas if there are no errors on the filesystem
authorDarrick J. Wong <darrick.wong@oracle.com>
Fri, 2 Feb 2018 15:32:46 +0000 (09:32 -0600)
committerEric Sandeen <sandeen@redhat.com>
Fri, 2 Feb 2018 15:32:46 +0000 (09:32 -0600)
If the filesystem scan comes out clean or fixes all the problems, call
fstrim to clean out the free areas (if it's an ssd/thinp/whatever).

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
man/man8/xfs_scrub.8
scrub/Makefile
scrub/phase4.c [new file with mode: 0644]
scrub/vfs.c
scrub/vfs.h
scrub/xfs_scrub.c
scrub/xfs_scrub.h

index c9df7d6bb36009991de9e1b577722e291d0a3b07..b6e156057c2ff55da84278e988e618977e41284a 100644 (file)
@@ -63,6 +63,9 @@ If
 is given, no action is taken if errors are found; this is the default
 behavior.
 .TP
+.B \-k
+Do not call FITRIM on the free space.
+.TP
 .BI \-m " file"
 Search this file for mounted filesystems instead of /etc/mtab.
 .TP
index fd266244b09f9b0f370f55d2fa7ea69e1a330880..91f99fff1104a809f9a6b1d75b06b6c48276c046 100644 (file)
@@ -41,6 +41,7 @@ inodes.c \
 phase1.c \
 phase2.c \
 phase3.c \
+phase4.c \
 phase5.c \
 phase6.c \
 phase7.c \
diff --git a/scrub/phase4.c b/scrub/phase4.c
new file mode 100644 (file)
index 0000000..31211f6
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2018 Oracle.  All Rights Reserved.
+ *
+ * Author: Darrick J. Wong <darrick.wong@oracle.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 would 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, write the Free Software Foundation,
+ * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/statvfs.h>
+#include "xfs.h"
+#include "xfs_fs.h"
+#include "list.h"
+#include "path.h"
+#include "workqueue.h"
+#include "xfs_scrub.h"
+#include "common.h"
+#include "scrub.h"
+#include "vfs.h"
+
+/* Phase 4: Repair filesystem. */
+
+/* Process all the action items. */
+static bool
+xfs_process_action_items(
+       struct scrub_ctx                *ctx)
+{
+       bool                            moveon = true;
+
+       pthread_mutex_lock(&ctx->lock);
+       if (moveon && ctx->errors_found == 0 && want_fstrim)
+               fstrim(ctx);
+       pthread_mutex_unlock(&ctx->lock);
+
+       return moveon;
+}
+
+/* Fix everything that needs fixing. */
+bool
+xfs_repair_fs(
+       struct scrub_ctx                *ctx)
+{
+       return xfs_process_action_items(ctx);
+}
+
+/* Run the optimize-only phase if there are no errors. */
+bool
+xfs_optimize_fs(
+       struct scrub_ctx        *ctx)
+{
+       /*
+        * In preen mode, corruptions are immediately recorded as errors,
+        * so if there are any corruptions on the filesystem errors_found
+        * will be non-zero and we won't do anything.
+        */
+       if (ctx->errors_found) {
+               str_info(ctx, ctx->mntpoint,
+_("Errors found, please re-run with -y."));
+               return true;
+       }
+
+       return xfs_process_action_items(ctx);
+}
index 3c0c2f3e251438d73bead9d574cfdb04218d70c8..e3c8e62c64666812719c834498123dfd862bf9db 100644 (file)
@@ -223,3 +223,26 @@ out_free:
        free(sftd);
        return false;
 }
+
+#ifndef FITRIM
+struct fstrim_range {
+       __u64 start;
+       __u64 len;
+       __u64 minlen;
+};
+#define FITRIM         _IOWR('X', 121, struct fstrim_range)    /* Trim */
+#endif
+
+/* Call FITRIM to trim all the unused space in a filesystem. */
+void
+fstrim(
+       struct scrub_ctx        *ctx)
+{
+       struct fstrim_range     range = {0};
+       int                     error;
+
+       range.len = ULLONG_MAX;
+       error = ioctl(ctx->mnt_fd, FITRIM, &range);
+       if (error && errno != EOPNOTSUPP && errno != ENOTTY)
+               perror(_("fstrim"));
+}
index 100eb184866a0596808977a97402133430a805d2..3305159e4df46b48bf06ddabffc9e2f8bda04c45 100644 (file)
@@ -28,4 +28,6 @@ typedef bool (*scan_fs_tree_dirent_fn)(struct scrub_ctx *, const char *,
 bool scan_fs_tree(struct scrub_ctx *ctx, scan_fs_tree_dir_fn dir_fn,
                scan_fs_tree_dirent_fn dirent_fn, void *arg);
 
+void fstrim(struct scrub_ctx *ctx);
+
 #endif /* XFS_SCRUB_VFS_H_ */
index f6f5f0d5f657b8d8f605c6fca08f1e9b960c5c57..85fd89338264be5ab6a0cdfecb2b1cc2ae274577 100644 (file)
@@ -146,6 +146,9 @@ static bool                 scrub_data;
 /* Size of a memory page. */
 long                           page_size;
 
+/* Should we FSTRIM after a successful run? */
+bool                           want_fstrim = true;
+
 #define SCRUB_RET_SUCCESS      (0)     /* no problems left behind */
 #define SCRUB_RET_CORRUPT      (1)     /* corruption remains on fs */
 #define SCRUB_RET_UNOPTIMIZED  (2)     /* fs could be optimized */
@@ -161,6 +164,7 @@ usage(void)
        fprintf(stderr, _("  -a count     Stop after this many errors are found.\n"));
        fprintf(stderr, _("  -b           Background mode.\n"));
        fprintf(stderr, _("  -e behavior  What to do if errors are found.\n"));
+       fprintf(stderr, _("  -k           Do not FITRIM the free space.\n"));
        fprintf(stderr, _("  -m path      Path to /etc/mtab.\n"));
        fprintf(stderr, _("  -n           Dry run.  Do not modify anything.\n"));
        fprintf(stderr, _("  -T           Display timing/usage information.\n"));
@@ -408,8 +412,19 @@ run_scrub_phases(
        /* Run all phases of the scrub tool. */
        for (phase = 1, sp = phases; sp->fn; sp++, phase++) {
                /* Turn on certain phases if user said to. */
-               if (sp->fn == DATASCAN_DUMMY_FN && scrub_data)
+               if (sp->fn == DATASCAN_DUMMY_FN && scrub_data) {
                        sp->fn = xfs_scan_blocks;
+               } else if (sp->fn == REPAIR_DUMMY_FN) {
+                       if (ctx->mode == SCRUB_MODE_PREEN) {
+                               sp->descr = _("Optimize filesystem.");
+                               sp->fn = xfs_optimize_fs;
+                               sp->must_run = true;
+                       } else if (ctx->mode == SCRUB_MODE_REPAIR) {
+                               sp->descr = _("Repair filesystem.");
+                               sp->fn = xfs_repair_fs;
+                               sp->must_run = true;
+                       }
+               }
 
                /* Skip certain phases unless they're turned on. */
                if (sp->fn == REPAIR_DUMMY_FN ||
@@ -469,7 +484,7 @@ main(
        pthread_mutex_init(&ctx.lock, NULL);
        ctx.mode = SCRUB_MODE_DEFAULT;
        ctx.error_action = ERRORS_CONTINUE;
-       while ((c = getopt(argc, argv, "a:bde:m:nTvxVy")) != EOF) {
+       while ((c = getopt(argc, argv, "a:bde:km:nTvxVy")) != EOF) {
                switch (c) {
                case 'a':
                        ctx.max_errors = cvt_u64(optarg, 10);
@@ -497,6 +512,9 @@ main(
                                usage();
                        }
                        break;
+               case 'k':
+                       want_fstrim = false;
+                       break;
                case 'm':
                        mtab = optarg;
                        break;
index 91f457740ae99e279ca843035bd3706ce31eabcd..47d63de57d688c6e11eee31bcd3c0a25893a1f1e 100644 (file)
@@ -28,6 +28,7 @@ extern unsigned int           debug;
 extern int                     nproc;
 extern bool                    verbose;
 extern long                    page_size;
+extern bool                    want_fstrim;
 
 enum scrub_mode {
        SCRUB_MODE_DRY_RUN,
@@ -105,5 +106,7 @@ bool xfs_scan_inodes(struct scrub_ctx *ctx);
 bool xfs_scan_connections(struct scrub_ctx *ctx);
 bool xfs_scan_blocks(struct scrub_ctx *ctx);
 bool xfs_scan_summary(struct scrub_ctx *ctx);
+bool xfs_repair_fs(struct scrub_ctx *ctx);
+bool xfs_optimize_fs(struct scrub_ctx *ctx);
 
 #endif /* XFS_SCRUB_XFS_SCRUB_H_ */