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