]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/error.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / libbcachefs / error.c
index 655e3ba9bfd2c09f221011ae16c38ba9f101e6ff..8ae95b218e8b0c9c1f8ba3a0ab1c2a58cad2b66d 100644 (file)
@@ -1,7 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 #include "bcachefs.h"
 #include "error.h"
+#include "recovery.h"
 #include "super.h"
+#include <linux/thread_with_file.h>
 
 #define FSCK_ERR_RATELIMIT_NR  10
 
@@ -24,11 +26,16 @@ bool bch2_inconsistent_error(struct bch_fs *c)
        }
 }
 
-void bch2_topology_error(struct bch_fs *c)
+int bch2_topology_error(struct bch_fs *c)
 {
        set_bit(BCH_FS_topology_error, &c->flags);
-       if (test_bit(BCH_FS_fsck_done, &c->flags))
+       if (!test_bit(BCH_FS_fsck_running, &c->flags)) {
                bch2_inconsistent_error(c);
+               return -BCH_ERR_btree_need_topology_repair;
+       } else {
+               return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_check_topology) ?:
+                       -BCH_ERR_btree_node_read_validate_error;
+       }
 }
 
 void bch2_fatal_error(struct bch_fs *c)
@@ -69,40 +76,66 @@ enum ask_yn {
        YN_ALLYES,
 };
 
+static enum ask_yn parse_yn_response(char *buf)
+{
+       buf = strim(buf);
+
+       if (strlen(buf) == 1)
+               switch (buf[0]) {
+               case 'n':
+                       return YN_NO;
+               case 'y':
+                       return YN_YES;
+               case 'N':
+                       return YN_ALLNO;
+               case 'Y':
+                       return YN_ALLYES;
+               }
+       return -1;
+}
+
 #ifdef __KERNEL__
-#define bch2_fsck_ask_yn()     YN_NO
+static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c)
+{
+       struct stdio_redirect *stdio = c->stdio;
+
+       if (c->stdio_filter && c->stdio_filter != current)
+               stdio = NULL;
+
+       if (!stdio)
+               return YN_NO;
+
+       char buf[100];
+       int ret;
+
+       do {
+               bch2_print(c, " (y,n, or Y,N for all errors of this type) ");
+
+               int r = stdio_redirect_readline(stdio, buf, sizeof(buf) - 1);
+               if (r < 0)
+                       return YN_NO;
+               buf[r] = '\0';
+       } while ((ret = parse_yn_response(buf)) < 0);
+
+       return ret;
+}
 #else
 
 #include "tools-util.h"
 
-enum ask_yn bch2_fsck_ask_yn(void)
+static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c)
 {
        char *buf = NULL;
        size_t buflen = 0;
-       bool ret;
+       int ret;
 
-       while (true) {
+       do {
                fputs(" (y,n, or Y,N for all errors of this type) ", stdout);
                fflush(stdout);
 
                if (getline(&buf, &buflen, stdin) < 0)
                        die("error reading from standard input");
-
-               strim(buf);
-               if (strlen(buf) != 1)
-                       continue;
-
-               switch (buf[0]) {
-               case 'n':
-                       return YN_NO;
-               case 'y':
-                       return YN_YES;
-               case 'N':
-                       return YN_ALLNO;
-               case 'Y':
-                       return YN_ALLYES;
-               }
-       }
+       } while ((ret = parse_yn_response(buf)) < 0);
 
        free(buf);
        return ret;
@@ -114,7 +147,7 @@ static struct fsck_err_state *fsck_err_get(struct bch_fs *c, const char *fmt)
 {
        struct fsck_err_state *s;
 
-       if (test_bit(BCH_FS_fsck_done, &c->flags))
+       if (!test_bit(BCH_FS_fsck_running, &c->flags))
                return NULL;
 
        list_for_each_entry(s, &c->fsck_error_msgs, list)
@@ -152,6 +185,10 @@ int bch2_fsck_err(struct bch_fs *c,
        struct printbuf buf = PRINTBUF, *out = &buf;
        int ret = -BCH_ERR_fsck_ignore;
 
+       if ((flags & FSCK_CAN_FIX) &&
+           test_bit(err, c->sb.errors_silent))
+               return -BCH_ERR_fsck_fix;
+
        bch2_sb_error_count(c, err);
 
        va_start(args, fmt);
@@ -193,7 +230,7 @@ int bch2_fsck_err(struct bch_fs *c,
                prt_printf(out, bch2_log_msg(c, ""));
 #endif
 
-       if (test_bit(BCH_FS_fsck_done, &c->flags)) {
+       if (!test_bit(BCH_FS_fsck_running, &c->flags)) {
                if (c->opts.errors != BCH_ON_ERROR_continue ||
                    !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) {
                        prt_str(out, ", shutting down");
@@ -218,10 +255,13 @@ int bch2_fsck_err(struct bch_fs *c,
                        int ask;
 
                        prt_str(out, ": fix?");
-                       bch2_print_string_as_lines(KERN_ERR, out->buf);
+                       if (bch2_fs_stdio_redirect(c))
+                               bch2_print(c, "%s", out->buf);
+                       else
+                               bch2_print_string_as_lines(KERN_ERR, out->buf);
                        print = false;
 
-                       ask = bch2_fsck_ask_yn();
+                       ask = bch2_fsck_ask_yn(c);
 
                        if (ask >= YN_ALLNO && s)
                                s->fix = ask == YN_ALLNO
@@ -250,10 +290,14 @@ int bch2_fsck_err(struct bch_fs *c,
             !(flags & FSCK_CAN_IGNORE)))
                ret = -BCH_ERR_fsck_errors_not_fixed;
 
-       if (print)
-               bch2_print_string_as_lines(KERN_ERR, out->buf);
+       if (print) {
+               if (bch2_fs_stdio_redirect(c))
+                       bch2_print(c, "%s\n", out->buf);
+               else
+                       bch2_print_string_as_lines(KERN_ERR, out->buf);
+       }
 
-       if (!test_bit(BCH_FS_fsck_done, &c->flags) &&
+       if (test_bit(BCH_FS_fsck_running, &c->flags) &&
            (ret != -BCH_ERR_fsck_fix &&
             ret != -BCH_ERR_fsck_ignore))
                bch_err(c, "Unable to continue, halting");