]> www.infradead.org Git - mtd-utils.git/commitdiff
fsck.ubifs: Add inconsistent problem handling asking function
authorZhihao Cheng <chengzhihao1@huawei.com>
Mon, 11 Nov 2024 09:01:03 +0000 (17:01 +0800)
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>
Mon, 11 Nov 2024 09:32:45 +0000 (10:32 +0100)
There are four dimensions to define the type of inconsistent problems:
 1. fixable: Some inconsistent problems can't be fixed, for example
    corrupted superblock. Un-fixable problem will abort program.
 2. must fix: Some inconsistent problems can be ignored(eg. incorrect
    isize), but some are not(eg. corrupted TNC), which will affect the
    subsequent fsck steps.
 3. drop data: Some fixing methods will drop user data, which is
    unacceptable for safe mode. If it happens, fsck will be aborted.
 4. need rebuild: Some inconsistent problems depends on rebuilding
    filesystem to be fixed(eg. corrupted master node, corrupted TNC).
Define an asking function to handle above kinds of inconsistent problems.

Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
ubifs-utils/Makemodule.am
ubifs-utils/fsck.ubifs/fsck.ubifs.h
ubifs-utils/fsck.ubifs/problem.c [new file with mode: 0644]

index 9f4bd429e0da7dccc941eea65759ca9b77bf355d..590f17b69d6b7863c67b67a0854e4eafe497864a 100644 (file)
@@ -80,7 +80,8 @@ fsck_ubifs_SOURCES = \
        $(common_SOURCES) \
        $(libubifs_SOURCES) \
        ubifs-utils/fsck.ubifs/fsck.ubifs.h \
-       ubifs-utils/fsck.ubifs/fsck.ubifs.c
+       ubifs-utils/fsck.ubifs/fsck.ubifs.c \
+       ubifs-utils/fsck.ubifs/problem.c
 
 fsck_ubifs_LDADD = libmtd.a libubi.a $(ZLIB_LIBS) $(LZO_LIBS) $(ZSTD_LIBS) $(UUID_LIBS) $(LIBSELINUX_LIBS) $(OPENSSL_LIBS) \
                   $(DUMP_STACK_LD) -lm -lpthread
index 531a3ab99651fd827a69ae283607d672bfc00f4a..033a601e9a67449918f210d4a99ef413d054384a 100644 (file)
@@ -87,4 +87,7 @@ static inline const char *mode_name(const struct ubifs_info *c)
 /* Exit code for fsck program. */
 extern int exit_code;
 
+/* problem.c */
+bool fix_problem(const struct ubifs_info *c, int problem_type);
+
 #endif
diff --git a/ubifs-utils/fsck.ubifs/problem.c b/ubifs-utils/fsck.ubifs/problem.c
new file mode 100644 (file)
index 0000000..9a8c2e0
--- /dev/null
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2024, Huawei Technologies Co, Ltd.
+ *
+ * Authors: Zhihao Cheng <chengzhihao1@huawei.com>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+
+#include "ubifs.h"
+#include "defs.h"
+#include "debug.h"
+#include "fsck.ubifs.h"
+
+/*
+ * problem flags.
+ *
+ * PROBLEM_FIXABLE: problem is fixable, unsolvable problem such as corrupted
+ *                 super block will abort the fsck program
+ * PROBLEM_MUST_FIX: problem must be fixed because it will affect the subsequent
+ *                  fsck process, otherwise aborting the fsck program
+ * PROBLEM_DROP_DATA: user data could be dropped after fixing the problem
+ * PROBLEM_NEED_REBUILD: rebuilding filesystem is needed to fix the problem
+ */
+#define PROBLEM_FIXABLE                (1<<0)
+#define PROBLEM_MUST_FIX       (1<<1)
+#define PROBLEM_DROP_DATA      (1<<2)
+#define PROBLEM_NEED_REBUILD   (1<<3)
+
+struct fsck_problem {
+       unsigned int flags;
+       const char *desc;
+};
+
+static const struct fsck_problem problem_table[] = {};
+
+static void print_problem(const struct ubifs_info *c,
+                         const struct fsck_problem *problem)
+{
+       log_out(c, "problem: %s", problem->desc);
+}
+
+static void fatal_error(const struct ubifs_info *c,
+                       const struct fsck_problem *problem)
+{
+       if (!(problem->flags & PROBLEM_FIXABLE))
+               log_out(c, "inconsistent problem cannot be fixed");
+       else
+               log_out(c, "inconsistent problem must be fixed");
+       exit(exit_code);
+}
+
+/**
+ * fix_problem - whether fixing the inconsistent problem
+ * @c: UBIFS file-system description object
+ * @problem_type: the type of inconsistent problem
+ *
+ * This function decides to fix/skip the inconsistent problem or abort the
+ * program according to @problem_type, returns %true if the problem should
+ * be fixed, returns %false if the problem will be skipped.
+ */
+bool fix_problem(const struct ubifs_info *c, int problem_type)
+{
+       bool ans, ask = true, def_y = true;
+       const struct fsck_problem *problem = &problem_table[problem_type];
+       const char *question = (problem->flags & PROBLEM_NEED_REBUILD) ?
+                               "Rebuild filesystem?" : "Fix it?";
+
+       ubifs_assert(c, FSCK(c)->mode != REBUILD_MODE);
+
+       if (!(problem->flags & PROBLEM_FIXABLE)) {
+               exit_code |= FSCK_UNCORRECTED;
+               fatal_error(c, problem);
+       }
+
+       if (FSCK(c)->mode == CHECK_MODE ||
+           ((problem->flags & PROBLEM_DROP_DATA) && FSCK(c)->mode == SAFE_MODE) ||
+           ((problem->flags & PROBLEM_NEED_REBUILD) &&
+            (FSCK(c)->mode == SAFE_MODE || FSCK(c)->mode == DANGER_MODE0)))
+               def_y = false;
+
+       if ((problem->flags & PROBLEM_NEED_REBUILD) &&
+           (FSCK(c)->mode == DANGER_MODE0 || FSCK(c)->mode == DANGER_MODE1))
+               ask = false;
+
+       print_problem(c, problem);
+       ans = def_y;
+       if (FSCK(c)->mode == NORMAL_MODE) {
+               printf("%s[%d] (%s%s)", c->program_name, getpid(),
+                      c->dev_name ? : "-", mode_name(c));
+               if (prompt(question, def_y))
+                       ans = true;
+               else
+                       ans = false;
+       } else {
+               if (ask)
+                       log_out(c, "%s %c\n", question, def_y ? 'y' : 'n');
+       }
+
+       if (!ans) {
+               exit_code |= FSCK_UNCORRECTED;
+               if (problem->flags & PROBLEM_MUST_FIX)
+                       fatal_error(c, problem);
+       } else {
+               exit_code |= FSCK_NONDESTRUCT;
+       }
+
+       return ans;
+}