]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/error.h
Update bcachefs sources to 5074caad6a fixup! bcachefs: BTREE_ID_snapshot_tree
[bcachefs-tools-debian] / libbcachefs / error.h
index 83d3a6274eb72e899ad759d9c6f39b1abc8031c5..edf12443822c2599fe99651b3fd1855d40f18c00 100644 (file)
@@ -1,10 +1,13 @@
-#ifndef _BCACHE_ERROR_H
-#define _BCACHE_ERROR_H
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _BCACHEFS_ERROR_H
+#define _BCACHEFS_ERROR_H
 
+#include <linux/list.h>
 #include <linux/printk.h>
 
 struct bch_dev;
 struct bch_fs;
+struct work_struct;
 
 /*
  * XXX: separate out errors that indicate on disk data is inconsistent, and flag
@@ -13,26 +16,6 @@ struct bch_fs;
 
 /* Error messages: */
 
-/*
- * Very fatal logic/inconsistency errors: these indicate that we've majorly
- * screwed up at runtime, i.e. it's not likely that it was just caused by the
- * data on disk being inconsistent. These BUG():
- *
- * XXX: audit and convert to inconsistent() checks
- */
-
-#define bch2_fs_bug(c, ...)                                            \
-do {                                                                   \
-       bch_err(c, __VA_ARGS__);                                        \
-       BUG();                                                          \
-} while (0)
-
-#define bch2_fs_bug_on(cond, c, ...)                                   \
-do {                                                                   \
-       if (cond)                                                       \
-               bch2_fs_bug(c, __VA_ARGS__);                            \
-} while (0)
-
 /*
  * Inconsistency errors: The on disk data is inconsistent. If these occur during
  * initial recovery, they don't indicate a bug in the running code - we walk all
@@ -44,17 +27,19 @@ do {                                                                        \
  * BCH_ON_ERROR_CONTINUE mode
  */
 
-void bch2_inconsistent_error(struct bch_fs *);
+bool bch2_inconsistent_error(struct bch_fs *);
+
+void bch2_topology_error(struct bch_fs *);
 
 #define bch2_fs_inconsistent(c, ...)                                   \
-do {                                                                   \
+({                                                                     \
        bch_err(c, __VA_ARGS__);                                        \
        bch2_inconsistent_error(c);                                     \
-} while (0)
+})
 
 #define bch2_fs_inconsistent_on(cond, c, ...)                          \
 ({                                                                     \
-       int _ret = !!(cond);                                            \
+       bool _ret = unlikely(!!(cond));                                 \
                                                                        \
        if (_ret)                                                       \
                bch2_fs_inconsistent(c, __VA_ARGS__);                   \
@@ -74,71 +59,101 @@ do {                                                                       \
 
 #define bch2_dev_inconsistent_on(cond, ca, ...)                                \
 ({                                                                     \
-       int _ret = !!(cond);                                            \
+       bool _ret = unlikely(!!(cond));                                 \
                                                                        \
        if (_ret)                                                       \
                bch2_dev_inconsistent(ca, __VA_ARGS__);                 \
        _ret;                                                           \
 })
 
+/*
+ * When a transaction update discovers or is causing a fs inconsistency, it's
+ * helpful to also dump the pending updates:
+ */
+#define bch2_trans_inconsistent(trans, ...)                            \
+({                                                                     \
+       bch_err(trans->c, __VA_ARGS__);                                 \
+       bch2_dump_trans_updates(trans);                                 \
+       bch2_inconsistent_error(trans->c);                              \
+})
+
+#define bch2_trans_inconsistent_on(cond, trans, ...)                   \
+({                                                                     \
+       bool _ret = unlikely(!!(cond));                                 \
+                                                                       \
+       if (_ret)                                                       \
+               bch2_trans_inconsistent(trans, __VA_ARGS__);            \
+       _ret;                                                           \
+})
+
 /*
  * Fsck errors: inconsistency errors we detect at mount time, and should ideally
  * be able to repair:
  */
 
-enum {
-       BCH_FSCK_OK                     = 0,
-       BCH_FSCK_ERRORS_NOT_FIXED       = 1,
-       BCH_FSCK_REPAIR_UNIMPLEMENTED   = 2,
-       BCH_FSCK_REPAIR_IMPOSSIBLE      = 3,
-       BCH_FSCK_UNKNOWN_VERSION        = 4,
+enum fsck_err_opts {
+       FSCK_OPT_EXIT,
+       FSCK_OPT_YES,
+       FSCK_OPT_NO,
+       FSCK_OPT_ASK,
 };
 
-/* These macros return true if error should be fixed: */
+struct fsck_err_state {
+       struct list_head        list;
+       const char              *fmt;
+       u64                     nr;
+       bool                    ratelimited;
+       int                     ret;
+       int                     fix;
+       char                    *last_msg;
+};
 
-/* XXX: mark in superblock that filesystem contains errors, if we ignore: */
+#define FSCK_CAN_FIX           (1 << 0)
+#define FSCK_CAN_IGNORE                (1 << 1)
+#define FSCK_NEED_FSCK         (1 << 2)
+#define FSCK_NO_RATELIMIT      (1 << 3)
+
+__printf(3, 4) __cold
+int bch2_fsck_err(struct bch_fs *, unsigned, const char *, ...);
+void bch2_flush_fsck_errs(struct bch_fs *);
 
-#ifndef __fsck_err
-#define __fsck_err(c, _can_fix, _can_ignore, _nofix_msg, msg, ...)     \
+#define __fsck_err(c, _flags, msg, ...)                                        \
 ({                                                                     \
-       bool _fix = false;                                              \
+       int _ret = bch2_fsck_err(c, _flags, msg, ##__VA_ARGS__);        \
                                                                        \
-       if (_can_fix && (c)->opts.fix_errors) {                         \
-               bch_err(c, msg ", fixing", ##__VA_ARGS__);              \
-               set_bit(BCH_FS_FSCK_FIXED_ERRORS, &(c)->flags); \
-               _fix = true;                                            \
-       } else if (_can_ignore &&                                       \
-                  (c)->opts.errors == BCH_ON_ERROR_CONTINUE) {         \
-               bch_err(c, msg " (ignoring)", ##__VA_ARGS__);           \
-       } else {                                                        \
-               bch_err(c, msg " ("_nofix_msg")", ##__VA_ARGS__);       \
-               ret = BCH_FSCK_ERRORS_NOT_FIXED;                        \
+       if (_ret != -BCH_ERR_fsck_fix &&                                \
+           _ret != -BCH_ERR_fsck_ignore) {                             \
+               ret = _ret;                                             \
                goto fsck_err;                                          \
        }                                                               \
                                                                        \
-       BUG_ON(!_fix && !_can_ignore);                                  \
-       _fix;                                                           \
+       _ret == -BCH_ERR_fsck_fix;                                      \
 })
-#endif
 
-#define __fsck_err_on(cond, c, _can_fix, _can_ignore, _nofix_msg, ...) \
-       ((cond) ? __fsck_err(c, _can_fix, _can_ignore,                  \
-                            _nofix_msg, ##__VA_ARGS__) : false)
+/* These macros return true if error should be fixed: */
+
+/* XXX: mark in superblock that filesystem contains errors, if we ignore: */
 
-#define unfixable_fsck_err_on(cond, c, ...)                            \
-       __fsck_err_on(cond, c, false, true, "repair unimplemented", ##__VA_ARGS__)
+#define __fsck_err_on(cond, c, _flags, ...)                            \
+       (unlikely(cond) ? __fsck_err(c, _flags, ##__VA_ARGS__) : false)
 
 #define need_fsck_err_on(cond, c, ...)                                 \
-       __fsck_err_on(cond, c, false, true, "run fsck to correct", ##__VA_ARGS__)
+       __fsck_err_on(cond, c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, ##__VA_ARGS__)
+
+#define need_fsck_err(c, ...)                                          \
+       __fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, ##__VA_ARGS__)
 
 #define mustfix_fsck_err(c, ...)                                       \
-       __fsck_err(c, true, false, "not fixing", ##__VA_ARGS__)
+       __fsck_err(c, FSCK_CAN_FIX, ##__VA_ARGS__)
 
 #define mustfix_fsck_err_on(cond, c, ...)                              \
-       __fsck_err_on(cond, c, true, false, "not fixing", ##__VA_ARGS__)
+       __fsck_err_on(cond, c, FSCK_CAN_FIX, ##__VA_ARGS__)
+
+#define fsck_err(c, ...)                                               \
+       __fsck_err(c, FSCK_CAN_FIX|FSCK_CAN_IGNORE, ##__VA_ARGS__)
 
 #define fsck_err_on(cond, c, ...)                                      \
-       __fsck_err_on(cond, c, true, true, "not fixing", ##__VA_ARGS__)
+       __fsck_err_on(cond, c, FSCK_CAN_FIX|FSCK_CAN_IGNORE, ##__VA_ARGS__)
 
 /*
  * Fatal errors: these don't indicate a bug, but we can't continue running in RW
@@ -155,83 +170,44 @@ do {                                                                      \
 
 #define bch2_fs_fatal_err_on(cond, c, ...)                             \
 ({                                                                     \
-       int _ret = !!(cond);                                            \
+       bool _ret = unlikely(!!(cond));                                 \
                                                                        \
        if (_ret)                                                       \
                bch2_fs_fatal_error(c, __VA_ARGS__);                    \
        _ret;                                                           \
 })
 
-#define bch2_dev_fatal_error(ca, ...)                                  \
-do {                                                                   \
-       bch_err(ca, __VA_ARGS__);                                       \
-       bch2_fatal_error(c);                                            \
-} while (0)
-
-#define bch2_dev_fatal_io_error(ca, fmt, ...)                          \
-do {                                                                   \
-       printk_ratelimited(KERN_ERR bch2_fmt((ca)->fs,                  \
-               "fatal IO error on %s for " fmt),                       \
-               (ca)->name, ##__VA_ARGS__);                             \
-       bch2_fatal_error((ca)->fs);                                     \
-} while (0)
-
-#define bch2_dev_fatal_io_err_on(cond, ca, ...)                                \
-({                                                                     \
-       int _ret = !!(cond);                                            \
-                                                                       \
-       if (_ret)                                                       \
-               bch2_dev_fatal_io_error(ca, __VA_ARGS__);               \
-       _ret;                                                           \
-})
-
 /*
- * Nonfatal IO errors: either recoverable metadata IO (because we have
- * replicas), or data IO - we need to log it and print out a message, but we
- * don't (necessarily) want to shut down the fs:
+ * IO errors: either recoverable metadata IO (because we have replicas), or data
+ * IO - we need to log it and print out a message, but we don't (necessarily)
+ * want to shut down the fs:
  */
 
-void bch2_nonfatal_io_error_work(struct work_struct *);
+void bch2_io_error_work(struct work_struct *);
 
 /* Does the error handling without logging a message */
-void bch2_nonfatal_io_error(struct bch_dev *);
-
-#if 0
-#define bch2_fs_nonfatal_io_error(c, ...)                              \
-do {                                                                   \
-       bch_err(c, __VA_ARGS__);                                        \
-       bch2_nonfatal_io_error(c);                                      \
-} while (0)
-#endif
-
-/* Logs message and handles the error: */
-#define bch2_dev_nonfatal_io_error(ca, fmt, ...)                               \
-do {                                                                   \
-       printk_ratelimited(KERN_ERR bch2_fmt((ca)->fs,                  \
-               "IO error on %s for " fmt),                             \
-               (ca)->name, ##__VA_ARGS__);                             \
-       bch2_nonfatal_io_error(ca);                                     \
-} while (0)
+void bch2_io_error(struct bch_dev *);
 
-#define bch2_dev_nonfatal_io_err_on(cond, ca, ...)                     \
+#define bch2_dev_io_err_on(cond, ca, ...)                              \
 ({                                                                     \
        bool _ret = (cond);                                             \
                                                                        \
-       if (_ret)                                                       \
-               bch2_dev_nonfatal_io_error(ca, __VA_ARGS__);            \
+       if (_ret) {                                                     \
+               bch_err_dev_ratelimited(ca, __VA_ARGS__);               \
+               bch2_io_error(ca);                                      \
+       }                                                               \
        _ret;                                                           \
 })
 
-/* kill? */
-
-#define __bcache_io_error(c, fmt, ...)                                 \
-       printk_ratelimited(KERN_ERR bch2_fmt(c,                         \
-                       "IO error: " fmt), ##__VA_ARGS__)
-
-#define bcache_io_error(c, bio, fmt, ...)                              \
-do {                                                                   \
-       __bcache_io_error(c, fmt, ##__VA_ARGS__);                       \
-       (bio)->bi_error = -EIO;                                         \
-} while (0)
+#define bch2_dev_inum_io_err_on(cond, ca, ...)                         \
+({                                                                     \
+       bool _ret = (cond);                                             \
+                                                                       \
+       if (_ret) {                                                     \
+               bch_err_inum_offset_ratelimited(ca, __VA_ARGS__);       \
+               bch2_io_error(ca);                                      \
+       }                                                               \
+       _ret;                                                           \
+})
 
-#endif /* _BCACHE_ERROR_H */
+#endif /* _BCACHEFS_ERROR_H */