]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/error.c
Update bcachefs sources to 0d63ed13ea3d closures: Fix race in closure_sync()
[bcachefs-tools-debian] / libbcachefs / error.c
index 3e49d72d65b5e8db791cfa6254529e9217fce4e6..2a5af88726132bc936e4ddc404086b862f0666ef 100644 (file)
@@ -1,7 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include "bcachefs.h"
 #include "error.h"
-#include "io.h"
 #include "super.h"
 
 #define FSCK_ERR_RATELIMIT_NR  10
@@ -28,7 +27,7 @@ bool bch2_inconsistent_error(struct bch_fs *c)
 void bch2_topology_error(struct bch_fs *c)
 {
        set_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags);
-       if (test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
+       if (test_bit(BCH_FS_FSCK_DONE, &c->flags))
                bch2_inconsistent_error(c);
 }
 
@@ -62,10 +61,52 @@ void bch2_io_error(struct bch_dev *ca)
        //queue_work(system_long_wq, &ca->io_error_work);
 }
 
+enum ask_yn {
+       YN_NO,
+       YN_YES,
+       YN_ALLNO,
+       YN_ALLYES,
+};
+
 #ifdef __KERNEL__
-#define ask_yn()       false
+#define bch2_fsck_ask_yn()     YN_NO
 #else
+
 #include "tools-util.h"
+
+enum ask_yn bch2_fsck_ask_yn(void)
+{
+       char *buf = NULL;
+       size_t buflen = 0;
+       bool ret;
+
+       while (true) {
+               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;
+               }
+       }
+
+       free(buf);
+       return ret;
+}
+
 #endif
 
 static struct fsck_err_state *fsck_err_get(struct bch_fs *c, const char *fmt)
@@ -95,7 +136,6 @@ static struct fsck_err_state *fsck_err_get(struct bch_fs *c, const char *fmt)
 
        INIT_LIST_HEAD(&s->list);
        s->fmt = fmt;
-       s->buf = PRINTBUF;
        list_add(&s->list, &c->fsck_errors);
        return s;
 }
@@ -108,9 +148,28 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
        struct printbuf buf = PRINTBUF, *out = &buf;
        int ret = -BCH_ERR_fsck_ignore;
 
+       va_start(args, fmt);
+       prt_vprintf(out, fmt, args);
+       va_end(args);
+
        mutex_lock(&c->fsck_error_lock);
        s = fsck_err_get(c, fmt);
        if (s) {
+               /*
+                * We may be called multiple times for the same error on
+                * transaction restart - this memoizes instead of asking the user
+                * multiple times for the same error:
+                */
+               if (s->last_msg && !strcmp(buf.buf, s->last_msg)) {
+                       ret = s->ret;
+                       mutex_unlock(&c->fsck_error_lock);
+                       printbuf_exit(&buf);
+                       return ret;
+               }
+
+               kfree(s->last_msg);
+               s->last_msg = kstrdup(buf.buf, GFP_KERNEL);
+
                if (c->opts.ratelimit_errors &&
                    !(flags & FSCK_NO_RATELIMIT) &&
                    s->nr >= FSCK_ERR_RATELIMIT_NR) {
@@ -120,8 +179,6 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
                                print = false;
                }
 
-               printbuf_reset(&s->buf);
-               out = &s->buf;
                s->nr++;
        }
 
@@ -130,10 +187,6 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
                prt_printf(out, bch2_log_msg(c, ""));
 #endif
 
-       va_start(args, fmt);
-       prt_vprintf(out, fmt, args);
-       va_end(args);
-
        if (test_bit(BCH_FS_FSCK_DONE, &c->flags)) {
                if (c->opts.errors != BCH_ON_ERROR_continue ||
                    !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) {
@@ -147,18 +200,32 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
                        prt_str(out, ", continuing");
                        ret = -BCH_ERR_fsck_ignore;
                }
-       } else if (c->opts.fix_errors == FSCK_OPT_EXIT) {
+       } else if (c->opts.fix_errors == FSCK_FIX_exit) {
                prt_str(out, ", exiting");
                ret = -BCH_ERR_fsck_errors_not_fixed;
        } else if (flags & FSCK_CAN_FIX) {
-               if (c->opts.fix_errors == FSCK_OPT_ASK) {
+               int fix = s && s->fix
+                       ? s->fix
+                       : c->opts.fix_errors;
+
+               if (fix == FSCK_FIX_ask) {
+                       int ask;
+
                        prt_str(out, ": fix?");
                        bch2_print_string_as_lines(KERN_ERR, out->buf);
                        print = false;
-                       ret = ask_yn()
+
+                       ask = bch2_fsck_ask_yn();
+
+                       if (ask >= YN_ALLNO && s)
+                               s->fix = ask == YN_ALLNO
+                                       ? FSCK_FIX_no
+                                       : FSCK_FIX_yes;
+
+                       ret = ask & 1
                                ? -BCH_ERR_fsck_fix
                                : -BCH_ERR_fsck_ignore;
-               } else if (c->opts.fix_errors == FSCK_OPT_YES ||
+               } else if (fix == FSCK_FIX_yes ||
                           (c->opts.nochanges &&
                            !(flags & FSCK_CAN_IGNORE))) {
                        prt_str(out, ", fixing");
@@ -173,7 +240,7 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
        }
 
        if (ret == -BCH_ERR_fsck_ignore &&
-           (c->opts.fix_errors == FSCK_OPT_EXIT ||
+           (c->opts.fix_errors == FSCK_FIX_exit ||
             !(flags & FSCK_CAN_IGNORE)))
                ret = -BCH_ERR_fsck_errors_not_fixed;
 
@@ -187,6 +254,9 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
        else if (suppressing)
                bch_err(c, "Ratelimiting new instances of previous error");
 
+       if (s)
+               s->ret = ret;
+
        mutex_unlock(&c->fsck_error_lock);
 
        printbuf_exit(&buf);
@@ -211,11 +281,11 @@ void bch2_flush_fsck_errs(struct bch_fs *c)
        mutex_lock(&c->fsck_error_lock);
 
        list_for_each_entry_safe(s, n, &c->fsck_errors, list) {
-               if (s->ratelimited)
-                       bch_err(c, "Saw %llu errors like:\n    %s", s->nr, s->buf.buf);
+               if (s->ratelimited && s->last_msg)
+                       bch_err(c, "Saw %llu errors like:\n    %s", s->nr, s->last_msg);
 
                list_del(&s->list);
-               printbuf_exit(&s->buf);
+               kfree(s->last_msg);
                kfree(s);
        }