]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/error.c
Update bcachefs sources to bed61fae3b bcachefs: Delete a faulty assertion
[bcachefs-tools-debian] / libbcachefs / error.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "bcachefs.h"
3 #include "error.h"
4 #include "io.h"
5 #include "super.h"
6
7 #define FSCK_ERR_RATELIMIT_NR   10
8
9 bool bch2_inconsistent_error(struct bch_fs *c)
10 {
11         set_bit(BCH_FS_ERROR, &c->flags);
12
13         switch (c->opts.errors) {
14         case BCH_ON_ERROR_continue:
15                 return false;
16         case BCH_ON_ERROR_ro:
17                 if (bch2_fs_emergency_read_only(c))
18                         bch_err(c, "inconsistency detected - emergency read only");
19                 return true;
20         case BCH_ON_ERROR_panic:
21                 panic(bch2_fmt(c, "panic after error"));
22                 return true;
23         default:
24                 BUG();
25         }
26 }
27
28 void bch2_topology_error(struct bch_fs *c)
29 {
30         set_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags);
31         if (test_bit(BCH_FS_FSCK_DONE, &c->flags))
32                 bch2_inconsistent_error(c);
33 }
34
35 void bch2_fatal_error(struct bch_fs *c)
36 {
37         if (bch2_fs_emergency_read_only(c))
38                 bch_err(c, "fatal error - emergency read only");
39 }
40
41 void bch2_io_error_work(struct work_struct *work)
42 {
43         struct bch_dev *ca = container_of(work, struct bch_dev, io_error_work);
44         struct bch_fs *c = ca->fs;
45         bool dev;
46
47         down_write(&c->state_lock);
48         dev = bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_ro,
49                                     BCH_FORCE_IF_DEGRADED);
50         if (dev
51             ? __bch2_dev_set_state(c, ca, BCH_MEMBER_STATE_ro,
52                                   BCH_FORCE_IF_DEGRADED)
53             : bch2_fs_emergency_read_only(c))
54                 bch_err(ca,
55                         "too many IO errors, setting %s RO",
56                         dev ? "device" : "filesystem");
57         up_write(&c->state_lock);
58 }
59
60 void bch2_io_error(struct bch_dev *ca)
61 {
62         //queue_work(system_long_wq, &ca->io_error_work);
63 }
64
65 enum ask_yn {
66         YN_NO,
67         YN_YES,
68         YN_ALLNO,
69         YN_ALLYES,
70 };
71
72 #ifdef __KERNEL__
73 #define bch2_fsck_ask_yn()      YN_NO
74 #else
75
76 #include "tools-util.h"
77
78 enum ask_yn bch2_fsck_ask_yn(void)
79 {
80         char *buf = NULL;
81         size_t buflen = 0;
82         bool ret;
83
84         while (true) {
85                 fputs(" (y,n, or Y,N for all errors of this type) ", stdout);
86                 fflush(stdout);
87
88                 if (getline(&buf, &buflen, stdin) < 0)
89                         die("error reading from standard input");
90
91                 strim(buf);
92                 if (strlen(buf) != 1)
93                         continue;
94
95                 switch (buf[0]) {
96                 case 'n':
97                         return YN_NO;
98                 case 'y':
99                         return YN_YES;
100                 case 'N':
101                         return YN_ALLNO;
102                 case 'Y':
103                         return YN_ALLYES;
104                 }
105         }
106
107         free(buf);
108         return ret;
109 }
110
111 #endif
112
113 static struct fsck_err_state *fsck_err_get(struct bch_fs *c, const char *fmt)
114 {
115         struct fsck_err_state *s;
116
117         if (test_bit(BCH_FS_FSCK_DONE, &c->flags))
118                 return NULL;
119
120         list_for_each_entry(s, &c->fsck_errors, list)
121                 if (s->fmt == fmt) {
122                         /*
123                          * move it to the head of the list: repeated fsck errors
124                          * are common
125                          */
126                         list_move(&s->list, &c->fsck_errors);
127                         return s;
128                 }
129
130         s = kzalloc(sizeof(*s), GFP_NOFS);
131         if (!s) {
132                 if (!c->fsck_alloc_err)
133                         bch_err(c, "kmalloc err, cannot ratelimit fsck errs");
134                 c->fsck_alloc_err = true;
135                 return NULL;
136         }
137
138         INIT_LIST_HEAD(&s->list);
139         s->fmt = fmt;
140         list_add(&s->list, &c->fsck_errors);
141         return s;
142 }
143
144 int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
145 {
146         struct fsck_err_state *s = NULL;
147         va_list args;
148         bool print = true, suppressing = false, inconsistent = false;
149         struct printbuf buf = PRINTBUF, *out = &buf;
150         int ret = -BCH_ERR_fsck_ignore;
151
152         va_start(args, fmt);
153         prt_vprintf(out, fmt, args);
154         va_end(args);
155
156         mutex_lock(&c->fsck_error_lock);
157         s = fsck_err_get(c, fmt);
158         if (s) {
159                 /*
160                  * We may be called multiple times for the same error on
161                  * transaction restart - this memoizes instead of asking the user
162                  * multiple times for the same error:
163                  */
164                 if (s->last_msg && !strcmp(buf.buf, s->last_msg)) {
165                         ret = s->ret;
166                         mutex_unlock(&c->fsck_error_lock);
167                         printbuf_exit(&buf);
168                         return ret;
169                 }
170
171                 kfree(s->last_msg);
172                 s->last_msg = kstrdup(buf.buf, GFP_KERNEL);
173
174                 if (c->opts.ratelimit_errors &&
175                     !(flags & FSCK_NO_RATELIMIT) &&
176                     s->nr >= FSCK_ERR_RATELIMIT_NR) {
177                         if (s->nr == FSCK_ERR_RATELIMIT_NR)
178                                 suppressing = true;
179                         else
180                                 print = false;
181                 }
182
183                 s->nr++;
184         }
185
186 #ifdef BCACHEFS_LOG_PREFIX
187         if (!strncmp(fmt, "bcachefs:", 9))
188                 prt_printf(out, bch2_log_msg(c, ""));
189 #endif
190
191         if (test_bit(BCH_FS_FSCK_DONE, &c->flags)) {
192                 if (c->opts.errors != BCH_ON_ERROR_continue ||
193                     !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) {
194                         prt_str(out, ", shutting down");
195                         inconsistent = true;
196                         ret = -BCH_ERR_fsck_errors_not_fixed;
197                 } else if (flags & FSCK_CAN_FIX) {
198                         prt_str(out, ", fixing");
199                         ret = -BCH_ERR_fsck_fix;
200                 } else {
201                         prt_str(out, ", continuing");
202                         ret = -BCH_ERR_fsck_ignore;
203                 }
204         } else if (c->opts.fix_errors == FSCK_FIX_exit) {
205                 prt_str(out, ", exiting");
206                 ret = -BCH_ERR_fsck_errors_not_fixed;
207         } else if (flags & FSCK_CAN_FIX) {
208                 int fix = s && s->fix
209                         ? s->fix
210                         : c->opts.fix_errors;
211
212                 if (fix == FSCK_FIX_ask) {
213                         int ask;
214
215                         prt_str(out, ": fix?");
216                         bch2_print_string_as_lines(KERN_ERR, out->buf);
217                         print = false;
218
219                         ask = bch2_fsck_ask_yn();
220
221                         if (ask >= YN_ALLNO && s)
222                                 s->fix = ask == YN_ALLNO
223                                         ? FSCK_FIX_no
224                                         : FSCK_FIX_yes;
225
226                         ret = ask & 1
227                                 ? -BCH_ERR_fsck_fix
228                                 : -BCH_ERR_fsck_ignore;
229                 } else if (fix == FSCK_FIX_yes ||
230                            (c->opts.nochanges &&
231                             !(flags & FSCK_CAN_IGNORE))) {
232                         prt_str(out, ", fixing");
233                         ret = -BCH_ERR_fsck_fix;
234                 } else {
235                         prt_str(out, ", not fixing");
236                 }
237         } else if (flags & FSCK_NEED_FSCK) {
238                 prt_str(out, " (run fsck to correct)");
239         } else {
240                 prt_str(out, " (repair unimplemented)");
241         }
242
243         if (ret == -BCH_ERR_fsck_ignore &&
244             (c->opts.fix_errors == FSCK_FIX_exit ||
245              !(flags & FSCK_CAN_IGNORE)))
246                 ret = -BCH_ERR_fsck_errors_not_fixed;
247
248         if (print)
249                 bch2_print_string_as_lines(KERN_ERR, out->buf);
250
251         if (!test_bit(BCH_FS_FSCK_DONE, &c->flags) &&
252             (ret != -BCH_ERR_fsck_fix &&
253              ret != -BCH_ERR_fsck_ignore))
254                 bch_err(c, "Unable to continue, halting");
255         else if (suppressing)
256                 bch_err(c, "Ratelimiting new instances of previous error");
257
258         if (s)
259                 s->ret = ret;
260
261         mutex_unlock(&c->fsck_error_lock);
262
263         printbuf_exit(&buf);
264
265         if (inconsistent)
266                 bch2_inconsistent_error(c);
267
268         if (ret == -BCH_ERR_fsck_fix) {
269                 set_bit(BCH_FS_ERRORS_FIXED, &c->flags);
270         } else {
271                 set_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags);
272                 set_bit(BCH_FS_ERROR, &c->flags);
273         }
274
275         return ret;
276 }
277
278 void bch2_flush_fsck_errs(struct bch_fs *c)
279 {
280         struct fsck_err_state *s, *n;
281
282         mutex_lock(&c->fsck_error_lock);
283
284         list_for_each_entry_safe(s, n, &c->fsck_errors, list) {
285                 if (s->ratelimited && s->last_msg)
286                         bch_err(c, "Saw %llu errors like:\n    %s", s->nr, s->last_msg);
287
288                 list_del(&s->list);
289                 kfree(s->last_msg);
290                 kfree(s);
291         }
292
293         mutex_unlock(&c->fsck_error_lock);
294 }