]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/error.c
Update bcachefs sources to 2a6125decb43 bcachefs: bch_sb_field_downgrade
[bcachefs-tools-debian] / libbcachefs / error.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "bcachefs.h"
3 #include "error.h"
4 #include "super.h"
5
6 #define FSCK_ERR_RATELIMIT_NR   10
7
8 bool bch2_inconsistent_error(struct bch_fs *c)
9 {
10         set_bit(BCH_FS_error, &c->flags);
11
12         switch (c->opts.errors) {
13         case BCH_ON_ERROR_continue:
14                 return false;
15         case BCH_ON_ERROR_ro:
16                 if (bch2_fs_emergency_read_only(c))
17                         bch_err(c, "inconsistency detected - emergency read only");
18                 return true;
19         case BCH_ON_ERROR_panic:
20                 panic(bch2_fmt(c, "panic after error"));
21                 return true;
22         default:
23                 BUG();
24         }
25 }
26
27 void bch2_topology_error(struct bch_fs *c)
28 {
29         set_bit(BCH_FS_topology_error, &c->flags);
30         if (test_bit(BCH_FS_fsck_done, &c->flags))
31                 bch2_inconsistent_error(c);
32 }
33
34 void bch2_fatal_error(struct bch_fs *c)
35 {
36         if (bch2_fs_emergency_read_only(c))
37                 bch_err(c, "fatal error - emergency read only");
38 }
39
40 void bch2_io_error_work(struct work_struct *work)
41 {
42         struct bch_dev *ca = container_of(work, struct bch_dev, io_error_work);
43         struct bch_fs *c = ca->fs;
44         bool dev;
45
46         down_write(&c->state_lock);
47         dev = bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_ro,
48                                     BCH_FORCE_IF_DEGRADED);
49         if (dev
50             ? __bch2_dev_set_state(c, ca, BCH_MEMBER_STATE_ro,
51                                   BCH_FORCE_IF_DEGRADED)
52             : bch2_fs_emergency_read_only(c))
53                 bch_err(ca,
54                         "too many IO errors, setting %s RO",
55                         dev ? "device" : "filesystem");
56         up_write(&c->state_lock);
57 }
58
59 void bch2_io_error(struct bch_dev *ca, enum bch_member_error_type type)
60 {
61         atomic64_inc(&ca->errors[type]);
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_error_msgs, 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_error_msgs);
127                         return s;
128                 }
129
130         s = kzalloc(sizeof(*s), GFP_NOFS);
131         if (!s) {
132                 if (!c->fsck_alloc_msgs_err)
133                         bch_err(c, "kmalloc err, cannot ratelimit fsck errs");
134                 c->fsck_alloc_msgs_err = true;
135                 return NULL;
136         }
137
138         INIT_LIST_HEAD(&s->list);
139         s->fmt = fmt;
140         list_add(&s->list, &c->fsck_error_msgs);
141         return s;
142 }
143
144 int bch2_fsck_err(struct bch_fs *c,
145                   enum bch_fsck_flags flags,
146                   enum bch_sb_error_id err,
147                   const char *fmt, ...)
148 {
149         struct fsck_err_state *s = NULL;
150         va_list args;
151         bool print = true, suppressing = false, inconsistent = false;
152         struct printbuf buf = PRINTBUF, *out = &buf;
153         int ret = -BCH_ERR_fsck_ignore;
154
155         if (test_bit(err, c->sb.errors_silent))
156                 return -BCH_ERR_fsck_fix;
157
158         bch2_sb_error_count(c, err);
159
160         va_start(args, fmt);
161         prt_vprintf(out, fmt, args);
162         va_end(args);
163
164         mutex_lock(&c->fsck_error_msgs_lock);
165         s = fsck_err_get(c, fmt);
166         if (s) {
167                 /*
168                  * We may be called multiple times for the same error on
169                  * transaction restart - this memoizes instead of asking the user
170                  * multiple times for the same error:
171                  */
172                 if (s->last_msg && !strcmp(buf.buf, s->last_msg)) {
173                         ret = s->ret;
174                         mutex_unlock(&c->fsck_error_msgs_lock);
175                         printbuf_exit(&buf);
176                         return ret;
177                 }
178
179                 kfree(s->last_msg);
180                 s->last_msg = kstrdup(buf.buf, GFP_KERNEL);
181
182                 if (c->opts.ratelimit_errors &&
183                     !(flags & FSCK_NO_RATELIMIT) &&
184                     s->nr >= FSCK_ERR_RATELIMIT_NR) {
185                         if (s->nr == FSCK_ERR_RATELIMIT_NR)
186                                 suppressing = true;
187                         else
188                                 print = false;
189                 }
190
191                 s->nr++;
192         }
193
194 #ifdef BCACHEFS_LOG_PREFIX
195         if (!strncmp(fmt, "bcachefs:", 9))
196                 prt_printf(out, bch2_log_msg(c, ""));
197 #endif
198
199         if (test_bit(BCH_FS_fsck_done, &c->flags)) {
200                 if (c->opts.errors != BCH_ON_ERROR_continue ||
201                     !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) {
202                         prt_str(out, ", shutting down");
203                         inconsistent = true;
204                         ret = -BCH_ERR_fsck_errors_not_fixed;
205                 } else if (flags & FSCK_CAN_FIX) {
206                         prt_str(out, ", fixing");
207                         ret = -BCH_ERR_fsck_fix;
208                 } else {
209                         prt_str(out, ", continuing");
210                         ret = -BCH_ERR_fsck_ignore;
211                 }
212         } else if (c->opts.fix_errors == FSCK_FIX_exit) {
213                 prt_str(out, ", exiting");
214                 ret = -BCH_ERR_fsck_errors_not_fixed;
215         } else if (flags & FSCK_CAN_FIX) {
216                 int fix = s && s->fix
217                         ? s->fix
218                         : c->opts.fix_errors;
219
220                 if (fix == FSCK_FIX_ask) {
221                         int ask;
222
223                         prt_str(out, ": fix?");
224                         bch2_print_string_as_lines(KERN_ERR, out->buf);
225                         print = false;
226
227                         ask = bch2_fsck_ask_yn();
228
229                         if (ask >= YN_ALLNO && s)
230                                 s->fix = ask == YN_ALLNO
231                                         ? FSCK_FIX_no
232                                         : FSCK_FIX_yes;
233
234                         ret = ask & 1
235                                 ? -BCH_ERR_fsck_fix
236                                 : -BCH_ERR_fsck_ignore;
237                 } else if (fix == FSCK_FIX_yes ||
238                            (c->opts.nochanges &&
239                             !(flags & FSCK_CAN_IGNORE))) {
240                         prt_str(out, ", fixing");
241                         ret = -BCH_ERR_fsck_fix;
242                 } else {
243                         prt_str(out, ", not fixing");
244                 }
245         } else if (flags & FSCK_NEED_FSCK) {
246                 prt_str(out, " (run fsck to correct)");
247         } else {
248                 prt_str(out, " (repair unimplemented)");
249         }
250
251         if (ret == -BCH_ERR_fsck_ignore &&
252             (c->opts.fix_errors == FSCK_FIX_exit ||
253              !(flags & FSCK_CAN_IGNORE)))
254                 ret = -BCH_ERR_fsck_errors_not_fixed;
255
256         if (print)
257                 bch2_print_string_as_lines(KERN_ERR, out->buf);
258
259         if (!test_bit(BCH_FS_fsck_done, &c->flags) &&
260             (ret != -BCH_ERR_fsck_fix &&
261              ret != -BCH_ERR_fsck_ignore))
262                 bch_err(c, "Unable to continue, halting");
263         else if (suppressing)
264                 bch_err(c, "Ratelimiting new instances of previous error");
265
266         if (s)
267                 s->ret = ret;
268
269         mutex_unlock(&c->fsck_error_msgs_lock);
270
271         printbuf_exit(&buf);
272
273         if (inconsistent)
274                 bch2_inconsistent_error(c);
275
276         if (ret == -BCH_ERR_fsck_fix) {
277                 set_bit(BCH_FS_errors_fixed, &c->flags);
278         } else {
279                 set_bit(BCH_FS_errors_not_fixed, &c->flags);
280                 set_bit(BCH_FS_error, &c->flags);
281         }
282
283         return ret;
284 }
285
286 void bch2_flush_fsck_errs(struct bch_fs *c)
287 {
288         struct fsck_err_state *s, *n;
289
290         mutex_lock(&c->fsck_error_msgs_lock);
291
292         list_for_each_entry_safe(s, n, &c->fsck_error_msgs, list) {
293                 if (s->ratelimited && s->last_msg)
294                         bch_err(c, "Saw %llu errors like:\n    %s", s->nr, s->last_msg);
295
296                 list_del(&s->list);
297                 kfree(s->last_msg);
298                 kfree(s);
299         }
300
301         mutex_unlock(&c->fsck_error_msgs_lock);
302 }