]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/error.c
Move c_src dirs back to toplevel
[bcachefs-tools-debian] / libbcachefs / error.c
index 2a5af88726132bc936e4ddc404086b862f0666ef..d32c8bebe46c32f7abc1a11ad49ee80752f2a623 100644 (file)
@@ -2,12 +2,13 @@
 #include "bcachefs.h"
 #include "error.h"
 #include "super.h"
+#include "thread_with_file.h"
 
 #define FSCK_ERR_RATELIMIT_NR  10
 
 bool bch2_inconsistent_error(struct bch_fs *c)
 {
-       set_bit(BCH_FS_ERROR, &c->flags);
+       set_bit(BCH_FS_error, &c->flags);
 
        switch (c->opts.errors) {
        case BCH_ON_ERROR_continue:
@@ -26,8 +27,8 @@ 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_FSCK_DONE, &c->flags))
+       set_bit(BCH_FS_topology_error, &c->flags);
+       if (!test_bit(BCH_FS_fsck_running, &c->flags))
                bch2_inconsistent_error(c);
 }
 
@@ -56,8 +57,9 @@ void bch2_io_error_work(struct work_struct *work)
        up_write(&c->state_lock);
 }
 
-void bch2_io_error(struct bch_dev *ca)
+void bch2_io_error(struct bch_dev *ca, enum bch_member_error_type type)
 {
+       atomic64_inc(&ca->errors[type]);
        //queue_work(system_long_wq, &ca->io_error_work);
 }
 
@@ -68,40 +70,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 = bch2_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;
@@ -113,34 +141,37 @@ 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_errors, list)
+       list_for_each_entry(s, &c->fsck_error_msgs, list)
                if (s->fmt == fmt) {
                        /*
                         * move it to the head of the list: repeated fsck errors
                         * are common
                         */
-                       list_move(&s->list, &c->fsck_errors);
+                       list_move(&s->list, &c->fsck_error_msgs);
                        return s;
                }
 
        s = kzalloc(sizeof(*s), GFP_NOFS);
        if (!s) {
-               if (!c->fsck_alloc_err)
+               if (!c->fsck_alloc_msgs_err)
                        bch_err(c, "kmalloc err, cannot ratelimit fsck errs");
-               c->fsck_alloc_err = true;
+               c->fsck_alloc_msgs_err = true;
                return NULL;
        }
 
        INIT_LIST_HEAD(&s->list);
        s->fmt = fmt;
-       list_add(&s->list, &c->fsck_errors);
+       list_add(&s->list, &c->fsck_error_msgs);
        return s;
 }
 
-int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
+int bch2_fsck_err(struct bch_fs *c,
+                 enum bch_fsck_flags flags,
+                 enum bch_sb_error_id err,
+                 const char *fmt, ...)
 {
        struct fsck_err_state *s = NULL;
        va_list args;
@@ -148,11 +179,17 @@ 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;
 
+       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);
        prt_vprintf(out, fmt, args);
        va_end(args);
 
-       mutex_lock(&c->fsck_error_lock);
+       mutex_lock(&c->fsck_error_msgs_lock);
        s = fsck_err_get(c, fmt);
        if (s) {
                /*
@@ -162,7 +199,7 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
                 */
                if (s->last_msg && !strcmp(buf.buf, s->last_msg)) {
                        ret = s->ret;
-                       mutex_unlock(&c->fsck_error_lock);
+                       mutex_unlock(&c->fsck_error_msgs_lock);
                        printbuf_exit(&buf);
                        return ret;
                }
@@ -187,7 +224,7 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
                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");
@@ -212,10 +249,13 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
                        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
@@ -244,10 +284,14 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
             !(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");
@@ -257,7 +301,7 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
        if (s)
                s->ret = ret;
 
-       mutex_unlock(&c->fsck_error_lock);
+       mutex_unlock(&c->fsck_error_msgs_lock);
 
        printbuf_exit(&buf);
 
@@ -265,10 +309,10 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
                bch2_inconsistent_error(c);
 
        if (ret == -BCH_ERR_fsck_fix) {
-               set_bit(BCH_FS_ERRORS_FIXED, &c->flags);
+               set_bit(BCH_FS_errors_fixed, &c->flags);
        } else {
-               set_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags);
-               set_bit(BCH_FS_ERROR, &c->flags);
+               set_bit(BCH_FS_errors_not_fixed, &c->flags);
+               set_bit(BCH_FS_error, &c->flags);
        }
 
        return ret;
@@ -278,9 +322,9 @@ void bch2_flush_fsck_errs(struct bch_fs *c)
 {
        struct fsck_err_state *s, *n;
 
-       mutex_lock(&c->fsck_error_lock);
+       mutex_lock(&c->fsck_error_msgs_lock);
 
-       list_for_each_entry_safe(s, n, &c->fsck_errors, list) {
+       list_for_each_entry_safe(s, n, &c->fsck_error_msgs, list) {
                if (s->ratelimited && s->last_msg)
                        bch_err(c, "Saw %llu errors like:\n    %s", s->nr, s->last_msg);
 
@@ -289,5 +333,5 @@ void bch2_flush_fsck_errs(struct bch_fs *c)
                kfree(s);
        }
 
-       mutex_unlock(&c->fsck_error_lock);
+       mutex_unlock(&c->fsck_error_msgs_lock);
 }