1 // SPDX-License-Identifier: GPL-2.0
5 #include "recovery_passes.h"
7 #include "thread_with_file.h"
9 #define FSCK_ERR_RATELIMIT_NR 10
11 bool bch2_inconsistent_error(struct bch_fs *c)
13 set_bit(BCH_FS_error, &c->flags);
15 switch (c->opts.errors) {
16 case BCH_ON_ERROR_continue:
19 if (bch2_fs_emergency_read_only(c))
20 bch_err(c, "inconsistency detected - emergency read only at journal seq %llu",
21 journal_cur_seq(&c->journal));
23 case BCH_ON_ERROR_panic:
24 panic(bch2_fmt(c, "panic after error"));
31 int bch2_topology_error(struct bch_fs *c)
33 set_bit(BCH_FS_topology_error, &c->flags);
34 if (!test_bit(BCH_FS_fsck_running, &c->flags)) {
35 bch2_inconsistent_error(c);
36 return -BCH_ERR_btree_need_topology_repair;
38 return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_check_topology) ?:
39 -BCH_ERR_btree_node_read_validate_error;
43 void bch2_fatal_error(struct bch_fs *c)
45 if (bch2_fs_emergency_read_only(c))
46 bch_err(c, "fatal error - emergency read only");
49 void bch2_io_error_work(struct work_struct *work)
51 struct bch_dev *ca = container_of(work, struct bch_dev, io_error_work);
52 struct bch_fs *c = ca->fs;
55 down_write(&c->state_lock);
56 dev = bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_ro,
57 BCH_FORCE_IF_DEGRADED);
59 ? __bch2_dev_set_state(c, ca, BCH_MEMBER_STATE_ro,
60 BCH_FORCE_IF_DEGRADED)
61 : bch2_fs_emergency_read_only(c))
63 "too many IO errors, setting %s RO",
64 dev ? "device" : "filesystem");
65 up_write(&c->state_lock);
68 void bch2_io_error(struct bch_dev *ca, enum bch_member_error_type type)
70 atomic64_inc(&ca->errors[type]);
71 //queue_work(system_long_wq, &ca->io_error_work);
81 static enum ask_yn parse_yn_response(char *buf)
100 static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c)
102 struct stdio_redirect *stdio = c->stdio;
104 if (c->stdio_filter && c->stdio_filter != current)
114 bch2_print(c, " (y,n, or Y,N for all errors of this type) ");
116 int r = bch2_stdio_redirect_readline(stdio, buf, sizeof(buf) - 1);
120 } while ((ret = parse_yn_response(buf)) < 0);
126 #include "tools-util.h"
128 static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c)
135 fputs(" (y,n, or Y,N for all errors of this type) ", stdout);
138 if (getline(&buf, &buflen, stdin) < 0)
139 die("error reading from standard input");
140 } while ((ret = parse_yn_response(buf)) < 0);
148 static struct fsck_err_state *fsck_err_get(struct bch_fs *c, const char *fmt)
150 struct fsck_err_state *s;
152 if (!test_bit(BCH_FS_fsck_running, &c->flags))
155 list_for_each_entry(s, &c->fsck_error_msgs, list)
158 * move it to the head of the list: repeated fsck errors
161 list_move(&s->list, &c->fsck_error_msgs);
165 s = kzalloc(sizeof(*s), GFP_NOFS);
167 if (!c->fsck_alloc_msgs_err)
168 bch_err(c, "kmalloc err, cannot ratelimit fsck errs");
169 c->fsck_alloc_msgs_err = true;
173 INIT_LIST_HEAD(&s->list);
175 list_add(&s->list, &c->fsck_error_msgs);
179 /* s/fix?/fixing/ s/recreate?/recreating/ */
180 static void prt_actioning(struct printbuf *out, const char *action)
182 unsigned len = strlen(action);
184 BUG_ON(action[len - 1] != '?');
187 if (action[len - 1] == 'e')
190 prt_bytes(out, action, len);
194 int bch2_fsck_err(struct bch_fs *c,
195 enum bch_fsck_flags flags,
196 enum bch_sb_error_id err,
197 const char *fmt, ...)
199 struct fsck_err_state *s = NULL;
201 bool print = true, suppressing = false, inconsistent = false;
202 struct printbuf buf = PRINTBUF, *out = &buf;
203 int ret = -BCH_ERR_fsck_ignore;
204 const char *action_orig = "fix?", *action = action_orig;
206 if ((flags & FSCK_CAN_FIX) &&
207 test_bit(err, c->sb.errors_silent))
208 return -BCH_ERR_fsck_fix;
210 bch2_sb_error_count(c, err);
213 prt_vprintf(out, fmt, args);
216 /* Custom fix/continue/recreate/etc.? */
217 if (out->buf[out->pos - 1] == '?') {
218 const char *p = strrchr(out->buf, ',');
220 out->pos = p - out->buf;
221 action = kstrdup(p + 2, GFP_KERNEL);
229 mutex_lock(&c->fsck_error_msgs_lock);
230 s = fsck_err_get(c, fmt);
233 * We may be called multiple times for the same error on
234 * transaction restart - this memoizes instead of asking the user
235 * multiple times for the same error:
237 if (s->last_msg && !strcmp(buf.buf, s->last_msg)) {
239 mutex_unlock(&c->fsck_error_msgs_lock);
244 s->last_msg = kstrdup(buf.buf, GFP_KERNEL);
246 mutex_unlock(&c->fsck_error_msgs_lock);
251 if (c->opts.ratelimit_errors &&
252 !(flags & FSCK_NO_RATELIMIT) &&
253 s->nr >= FSCK_ERR_RATELIMIT_NR) {
254 if (s->nr == FSCK_ERR_RATELIMIT_NR)
263 #ifdef BCACHEFS_LOG_PREFIX
264 if (!strncmp(fmt, "bcachefs:", 9))
265 prt_printf(out, bch2_log_msg(c, ""));
268 if (!test_bit(BCH_FS_fsck_running, &c->flags)) {
269 if (c->opts.errors != BCH_ON_ERROR_continue ||
270 !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) {
271 prt_str(out, ", shutting down");
273 ret = -BCH_ERR_fsck_errors_not_fixed;
274 } else if (flags & FSCK_CAN_FIX) {
276 prt_actioning(out, action);
277 ret = -BCH_ERR_fsck_fix;
279 prt_str(out, ", continuing");
280 ret = -BCH_ERR_fsck_ignore;
282 } else if (c->opts.fix_errors == FSCK_FIX_exit) {
283 prt_str(out, ", exiting");
284 ret = -BCH_ERR_fsck_errors_not_fixed;
285 } else if (flags & FSCK_CAN_FIX) {
286 int fix = s && s->fix
288 : c->opts.fix_errors;
290 if (fix == FSCK_FIX_ask) {
292 prt_str(out, action);
294 if (bch2_fs_stdio_redirect(c))
295 bch2_print(c, "%s", out->buf);
297 bch2_print_string_as_lines(KERN_ERR, out->buf);
300 int ask = bch2_fsck_ask_yn(c);
302 if (ask >= YN_ALLNO && s)
303 s->fix = ask == YN_ALLNO
309 : -BCH_ERR_fsck_ignore;
310 } else if (fix == FSCK_FIX_yes ||
311 (c->opts.nochanges &&
312 !(flags & FSCK_CAN_IGNORE))) {
314 prt_actioning(out, action);
315 ret = -BCH_ERR_fsck_fix;
317 prt_str(out, ", not ");
318 prt_actioning(out, action);
320 } else if (flags & FSCK_NEED_FSCK) {
321 prt_str(out, " (run fsck to correct)");
323 prt_str(out, " (repair unimplemented)");
326 if (ret == -BCH_ERR_fsck_ignore &&
327 (c->opts.fix_errors == FSCK_FIX_exit ||
328 !(flags & FSCK_CAN_IGNORE)))
329 ret = -BCH_ERR_fsck_errors_not_fixed;
332 if (bch2_fs_stdio_redirect(c))
333 bch2_print(c, "%s\n", out->buf);
335 bch2_print_string_as_lines(KERN_ERR, out->buf);
338 if (test_bit(BCH_FS_fsck_running, &c->flags) &&
339 (ret != -BCH_ERR_fsck_fix &&
340 ret != -BCH_ERR_fsck_ignore))
341 bch_err(c, "Unable to continue, halting");
342 else if (suppressing)
343 bch_err(c, "Ratelimiting new instances of previous error");
348 mutex_unlock(&c->fsck_error_msgs_lock);
351 bch2_inconsistent_error(c);
353 if (ret == -BCH_ERR_fsck_fix) {
354 set_bit(BCH_FS_errors_fixed, &c->flags);
356 set_bit(BCH_FS_errors_not_fixed, &c->flags);
357 set_bit(BCH_FS_error, &c->flags);
360 if (action != action_orig)
366 void bch2_flush_fsck_errs(struct bch_fs *c)
368 struct fsck_err_state *s, *n;
370 mutex_lock(&c->fsck_error_msgs_lock);
372 list_for_each_entry_safe(s, n, &c->fsck_error_msgs, list) {
373 if (s->ratelimited && s->last_msg)
374 bch_err(c, "Saw %llu errors like:\n %s", s->nr, s->last_msg);
381 mutex_unlock(&c->fsck_error_msgs_lock);