]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/error.h
94b53312fbbda9b61e2f192f34f413ff88bcbeff
[bcachefs-tools-debian] / libbcachefs / error.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_ERROR_H
3 #define _BCACHEFS_ERROR_H
4
5 #include <linux/list.h>
6 #include <linux/printk.h>
7
8 struct bch_dev;
9 struct bch_fs;
10 struct work_struct;
11
12 /*
13  * XXX: separate out errors that indicate on disk data is inconsistent, and flag
14  * superblock as such
15  */
16
17 /* Error messages: */
18
19 /*
20  * Inconsistency errors: The on disk data is inconsistent. If these occur during
21  * initial recovery, they don't indicate a bug in the running code - we walk all
22  * the metadata before modifying anything. If they occur at runtime, they
23  * indicate either a bug in the running code or (less likely) data is being
24  * silently corrupted under us.
25  *
26  * XXX: audit all inconsistent errors and make sure they're all recoverable, in
27  * BCH_ON_ERROR_CONTINUE mode
28  */
29
30 bool bch2_inconsistent_error(struct bch_fs *);
31
32 #define bch2_fs_inconsistent(c, ...)                                    \
33 ({                                                                      \
34         bch_err(c, __VA_ARGS__);                                        \
35         bch2_inconsistent_error(c);                                     \
36 })
37
38 #define bch2_fs_inconsistent_on(cond, c, ...)                           \
39 ({                                                                      \
40         int _ret = !!(cond);                                            \
41                                                                         \
42         if (_ret)                                                       \
43                 bch2_fs_inconsistent(c, __VA_ARGS__);                   \
44         _ret;                                                           \
45 })
46
47 /*
48  * Later we might want to mark only the particular device inconsistent, not the
49  * entire filesystem:
50  */
51
52 #define bch2_dev_inconsistent(ca, ...)                                  \
53 do {                                                                    \
54         bch_err(ca, __VA_ARGS__);                                       \
55         bch2_inconsistent_error((ca)->fs);                              \
56 } while (0)
57
58 #define bch2_dev_inconsistent_on(cond, ca, ...)                         \
59 ({                                                                      \
60         int _ret = !!(cond);                                            \
61                                                                         \
62         if (_ret)                                                       \
63                 bch2_dev_inconsistent(ca, __VA_ARGS__);                 \
64         _ret;                                                           \
65 })
66
67 /*
68  * Fsck errors: inconsistency errors we detect at mount time, and should ideally
69  * be able to repair:
70  */
71
72 enum {
73         BCH_FSCK_OK                     = 0,
74         BCH_FSCK_ERRORS_NOT_FIXED       = 1,
75         BCH_FSCK_REPAIR_UNIMPLEMENTED   = 2,
76         BCH_FSCK_REPAIR_IMPOSSIBLE      = 3,
77         BCH_FSCK_UNKNOWN_VERSION        = 4,
78 };
79
80 enum fsck_err_opts {
81         FSCK_OPT_EXIT,
82         FSCK_OPT_YES,
83         FSCK_OPT_NO,
84         FSCK_OPT_ASK,
85 };
86
87 enum fsck_err_ret {
88         FSCK_ERR_IGNORE = 0,
89         FSCK_ERR_FIX    = 1,
90         FSCK_ERR_EXIT   = 2,
91 };
92
93 struct fsck_err_state {
94         struct list_head        list;
95         const char              *fmt;
96         u64                     nr;
97         bool                    ratelimited;
98         char                    buf[512];
99 };
100
101 #define FSCK_CAN_FIX            (1 << 0)
102 #define FSCK_CAN_IGNORE         (1 << 1)
103 #define FSCK_NEED_FSCK          (1 << 2)
104
105 __printf(3, 4) __cold
106 enum fsck_err_ret bch2_fsck_err(struct bch_fs *,
107                                 unsigned, const char *, ...);
108 void bch2_flush_fsck_errs(struct bch_fs *);
109
110 #define __fsck_err(c, _flags, msg, ...)                                 \
111 ({                                                                      \
112         int _fix = bch2_fsck_err(c, _flags, msg, ##__VA_ARGS__);\
113                                                                         \
114         if (_fix == FSCK_ERR_EXIT) {                                    \
115                 bch_err(c, "Unable to continue, halting");              \
116                 ret = BCH_FSCK_ERRORS_NOT_FIXED;                        \
117                 goto fsck_err;                                          \
118         }                                                               \
119                                                                         \
120         _fix;                                                           \
121 })
122
123 /* These macros return true if error should be fixed: */
124
125 /* XXX: mark in superblock that filesystem contains errors, if we ignore: */
126
127 #define __fsck_err_on(cond, c, _flags, ...)                             \
128         ((cond) ? __fsck_err(c, _flags, ##__VA_ARGS__) : false)
129
130 #define need_fsck_err_on(cond, c, ...)                                  \
131         __fsck_err_on(cond, c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, ##__VA_ARGS__)
132
133 #define need_fsck_err(c, ...)                                           \
134         __fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK, ##__VA_ARGS__)
135
136 #define mustfix_fsck_err(c, ...)                                        \
137         __fsck_err(c, FSCK_CAN_FIX, ##__VA_ARGS__)
138
139 #define mustfix_fsck_err_on(cond, c, ...)                               \
140         __fsck_err_on(cond, c, FSCK_CAN_FIX, ##__VA_ARGS__)
141
142 #define fsck_err(c, ...)                                                \
143         __fsck_err(c, FSCK_CAN_FIX|FSCK_CAN_IGNORE, ##__VA_ARGS__)
144
145 #define fsck_err_on(cond, c, ...)                                       \
146         __fsck_err_on(cond, c, FSCK_CAN_FIX|FSCK_CAN_IGNORE, ##__VA_ARGS__)
147
148 /*
149  * Fatal errors: these don't indicate a bug, but we can't continue running in RW
150  * mode - pretty much just due to metadata IO errors:
151  */
152
153 void bch2_fatal_error(struct bch_fs *);
154
155 #define bch2_fs_fatal_error(c, ...)                                     \
156 do {                                                                    \
157         bch_err(c, __VA_ARGS__);                                        \
158         bch2_fatal_error(c);                                            \
159 } while (0)
160
161 #define bch2_fs_fatal_err_on(cond, c, ...)                              \
162 ({                                                                      \
163         int _ret = !!(cond);                                            \
164                                                                         \
165         if (_ret)                                                       \
166                 bch2_fs_fatal_error(c, __VA_ARGS__);                    \
167         _ret;                                                           \
168 })
169
170 /*
171  * IO errors: either recoverable metadata IO (because we have replicas), or data
172  * IO - we need to log it and print out a message, but we don't (necessarily)
173  * want to shut down the fs:
174  */
175
176 void bch2_io_error_work(struct work_struct *);
177
178 /* Does the error handling without logging a message */
179 void bch2_io_error(struct bch_dev *);
180
181 /* Logs message and handles the error: */
182 #define bch2_dev_io_error(ca, fmt, ...)                                 \
183 do {                                                                    \
184         printk_ratelimited(KERN_ERR bch2_fmt((ca)->fs,                  \
185                 "IO error on %s for " fmt),                             \
186                 (ca)->name, ##__VA_ARGS__);                             \
187         bch2_io_error(ca);                                              \
188 } while (0)
189
190 #define bch2_dev_io_err_on(cond, ca, ...)                               \
191 ({                                                                      \
192         bool _ret = (cond);                                             \
193                                                                         \
194         if (_ret)                                                       \
195                 bch2_dev_io_error(ca, __VA_ARGS__);                     \
196         _ret;                                                           \
197 })
198
199 /* kill? */
200
201 #define __bcache_io_error(c, fmt, ...)                                  \
202         printk_ratelimited(KERN_ERR bch2_fmt(c,                         \
203                         "IO error: " fmt), ##__VA_ARGS__)
204
205 #define bcache_io_error(c, bio, fmt, ...)                               \
206 do {                                                                    \
207         __bcache_io_error(c, fmt, ##__VA_ARGS__);                       \
208         (bio)->bi_status = BLK_STS_IOERR;                                       \
209 } while (0)
210
211 #endif /* _BCACHEFS_ERROR_H */