1 // SPDX-License-Identifier: GPL-2.0
3 #include "btree_cache.h"
4 #include "btree_iter.h"
8 #include "recovery_passes.h"
10 #include "thread_with_file.h"
12 #define FSCK_ERR_RATELIMIT_NR 10
14 void __bch2_log_msg_start(const char *fs_or_dev_name, struct printbuf *out)
16 printbuf_indent_add_nextline(out, 2);
18 #ifdef BCACHEFS_LOG_PREFIX
19 prt_printf(out, "bcachefs (%s): ", fs_or_dev_name);
23 bool __bch2_inconsistent_error(struct bch_fs *c, struct printbuf *out)
25 set_bit(BCH_FS_error, &c->flags);
27 switch (c->opts.errors) {
28 case BCH_ON_ERROR_continue:
30 case BCH_ON_ERROR_fix_safe:
32 bch2_fs_emergency_read_only2(c, out);
34 case BCH_ON_ERROR_panic:
35 bch2_print_str(c, KERN_ERR, out->buf);
36 panic(bch2_fmt(c, "panic after error"));
43 bool bch2_inconsistent_error(struct bch_fs *c)
45 struct printbuf buf = PRINTBUF;
48 printbuf_indent_add_nextline(&buf, 2);
50 bool ret = __bch2_inconsistent_error(c, &buf);
52 bch_err(c, "%s", buf.buf);
58 static bool bch2_fs_trans_inconsistent(struct bch_fs *c, struct btree_trans *trans,
59 const char *fmt, va_list args)
61 struct printbuf buf = PRINTBUF;
64 bch2_log_msg_start(c, &buf);
66 prt_vprintf(&buf, fmt, args);
70 bch2_trans_updates_to_text(&buf, trans);
71 bool ret = __bch2_inconsistent_error(c, &buf);
72 bch2_print_str(c, KERN_ERR, buf.buf);
78 bool bch2_fs_inconsistent(struct bch_fs *c, const char *fmt, ...)
82 bool ret = bch2_fs_trans_inconsistent(c, NULL, fmt, args);
87 bool bch2_trans_inconsistent(struct btree_trans *trans, const char *fmt, ...)
91 bool ret = bch2_fs_trans_inconsistent(trans->c, trans, fmt, args);
96 int __bch2_topology_error(struct bch_fs *c, struct printbuf *out)
98 prt_printf(out, "btree topology error: ");
100 set_bit(BCH_FS_topology_error, &c->flags);
101 if (!test_bit(BCH_FS_in_recovery, &c->flags)) {
102 __bch2_inconsistent_error(c, out);
103 return bch_err_throw(c, btree_need_topology_repair);
105 return bch2_run_explicit_recovery_pass(c, out, BCH_RECOVERY_PASS_check_topology, 0) ?:
106 bch_err_throw(c, btree_need_topology_repair);
110 int bch2_fs_topology_error(struct bch_fs *c, const char *fmt, ...)
112 struct printbuf buf = PRINTBUF;
114 bch2_log_msg_start(c, &buf);
118 prt_vprintf(&buf, fmt, args);
121 int ret = __bch2_topology_error(c, &buf);
122 bch2_print_str(c, KERN_ERR, buf.buf);
128 void bch2_fatal_error(struct bch_fs *c)
130 if (bch2_fs_emergency_read_only(c))
131 bch_err(c, "fatal error - emergency read only");
134 void bch2_io_error_work(struct work_struct *work)
136 struct bch_dev *ca = container_of(work, struct bch_dev, io_error_work);
137 struct bch_fs *c = ca->fs;
139 /* XXX: if it's reads or checksums that are failing, set it to failed */
141 down_write(&c->state_lock);
142 unsigned long write_errors_start = READ_ONCE(ca->write_errors_start);
144 if (write_errors_start &&
146 write_errors_start + c->opts.write_error_timeout * HZ)) {
147 if (ca->mi.state >= BCH_MEMBER_STATE_ro)
150 bool dev = !__bch2_dev_set_state(c, ca, BCH_MEMBER_STATE_ro,
151 BCH_FORCE_IF_DEGRADED);
152 struct printbuf buf = PRINTBUF;
153 __bch2_log_msg_start(ca->name, &buf);
155 prt_printf(&buf, "writes erroring for %u seconds, setting %s ro",
156 c->opts.write_error_timeout,
157 dev ? "device" : "filesystem");
159 bch2_fs_emergency_read_only2(c, &buf);
161 bch2_print_str(c, KERN_ERR, buf.buf);
165 up_write(&c->state_lock);
168 void bch2_io_error(struct bch_dev *ca, enum bch_member_error_type type)
170 atomic64_inc(&ca->errors[type]);
172 if (type == BCH_MEMBER_ERROR_write && !ca->write_errors_start)
173 ca->write_errors_start = jiffies;
175 queue_work(system_long_wq, &ca->io_error_work);
185 static enum ask_yn parse_yn_response(char *buf)
189 if (strlen(buf) == 1)
204 static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c, struct btree_trans *trans)
206 struct stdio_redirect *stdio = c->stdio;
208 if (c->stdio_filter && c->stdio_filter != current)
215 bch2_trans_unlock(trans);
217 unsigned long unlock_long_at = trans ? jiffies + HZ * 2 : 0;
218 darray_char line = {};
223 bch2_print(c, " (y,n, or Y,N for all errors of this type) ");
226 ? max_t(long, unlock_long_at - jiffies, 0)
227 : MAX_SCHEDULE_TIMEOUT;
229 int r = bch2_stdio_redirect_readline_timeout(stdio, &line, t);
231 bch2_trans_unlock_long(trans);
241 darray_last(line) = '\0';
242 } while ((ret = parse_yn_response(line.data)) < 0);
249 #include "tools-util.h"
251 static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c, struct btree_trans *trans)
258 fputs(" (y,n, or Y,N for all errors of this type) ", stdout);
261 if (getline(&buf, &buflen, stdin) < 0)
262 die("error reading from standard input");
263 } while ((ret = parse_yn_response(buf)) < 0);
271 static struct fsck_err_state *fsck_err_get(struct bch_fs *c,
272 enum bch_sb_error_id id)
274 struct fsck_err_state *s;
276 list_for_each_entry(s, &c->fsck_error_msgs, list)
279 * move it to the head of the list: repeated fsck errors
282 list_move(&s->list, &c->fsck_error_msgs);
286 s = kzalloc(sizeof(*s), GFP_NOFS);
288 if (!c->fsck_alloc_msgs_err)
289 bch_err(c, "kmalloc err, cannot ratelimit fsck errs");
290 c->fsck_alloc_msgs_err = true;
294 INIT_LIST_HEAD(&s->list);
296 list_add(&s->list, &c->fsck_error_msgs);
300 /* s/fix?/fixing/ s/recreate?/recreating/ */
301 static void prt_actioning(struct printbuf *out, const char *action)
303 unsigned len = strlen(action);
305 BUG_ON(action[len - 1] != '?');
308 if (action[len - 1] == 'e')
311 prt_bytes(out, action, len);
315 static const u8 fsck_flags_extra[] = {
316 #define x(t, n, flags) [BCH_FSCK_ERR_##t] = flags,
321 static int do_fsck_ask_yn(struct bch_fs *c,
322 struct btree_trans *trans,
323 struct printbuf *question,
326 prt_str(question, ", ");
327 prt_str(question, action);
329 if (bch2_fs_stdio_redirect(c))
330 bch2_print(c, "%s", question->buf);
332 bch2_print_str(c, KERN_ERR, question->buf);
334 int ask = bch2_fsck_ask_yn(c, trans);
337 int ret = bch2_trans_relock(trans);
345 static struct fsck_err_state *count_fsck_err_locked(struct bch_fs *c,
346 enum bch_sb_error_id id, const char *msg,
347 bool *repeat, bool *print, bool *suppress)
349 bch2_sb_error_count(c, id);
351 struct fsck_err_state *s = fsck_err_get(c, id);
354 * We may be called multiple times for the same error on
355 * transaction restart - this memoizes instead of asking the user
356 * multiple times for the same error:
358 if (s->last_msg && !strcmp(msg, s->last_msg)) {
365 s->last_msg = kstrdup(msg, GFP_KERNEL);
367 if (c->opts.ratelimit_errors &&
368 s->nr >= FSCK_ERR_RATELIMIT_NR) {
369 if (s->nr == FSCK_ERR_RATELIMIT_NR)
380 bool __bch2_count_fsck_err(struct bch_fs *c,
381 enum bch_sb_error_id id, struct printbuf *msg)
383 bch2_sb_error_count(c, id);
385 mutex_lock(&c->fsck_error_msgs_lock);
386 bool print = true, repeat = false, suppress = false;
388 count_fsck_err_locked(c, id, msg->buf, &repeat, &print, &suppress);
389 mutex_unlock(&c->fsck_error_msgs_lock);
392 prt_printf(msg, "Ratelimiting new instances of previous error\n");
394 return print && !repeat;
397 int bch2_fsck_err_opt(struct bch_fs *c,
398 enum bch_fsck_flags flags,
399 enum bch_sb_error_id err)
401 if (!WARN_ON(err >= ARRAY_SIZE(fsck_flags_extra)))
402 flags |= fsck_flags_extra[err];
404 if (test_bit(BCH_FS_in_fsck, &c->flags)) {
405 if (!(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE)))
406 return bch_err_throw(c, fsck_repair_unimplemented);
408 switch (c->opts.fix_errors) {
410 return bch_err_throw(c, fsck_errors_not_fixed);
412 if (flags & FSCK_CAN_FIX)
413 return bch_err_throw(c, fsck_fix);
416 if (flags & FSCK_CAN_IGNORE)
417 return bch_err_throw(c, fsck_ignore);
418 return bch_err_throw(c, fsck_errors_not_fixed);
420 if (flags & FSCK_AUTOFIX)
421 return bch_err_throw(c, fsck_fix);
422 return bch_err_throw(c, fsck_ask);
427 if ((flags & FSCK_AUTOFIX) &&
428 (c->opts.errors == BCH_ON_ERROR_continue ||
429 c->opts.errors == BCH_ON_ERROR_fix_safe))
430 return bch_err_throw(c, fsck_fix);
432 if (c->opts.errors == BCH_ON_ERROR_continue &&
433 (flags & FSCK_CAN_IGNORE))
434 return bch_err_throw(c, fsck_ignore);
435 return bch_err_throw(c, fsck_errors_not_fixed);
439 int __bch2_fsck_err(struct bch_fs *c,
440 struct btree_trans *trans,
441 enum bch_fsck_flags flags,
442 enum bch_sb_error_id err,
443 const char *fmt, ...)
446 struct printbuf buf = PRINTBUF, *out = &buf;
448 const char *action_orig = "fix?", *action = action_orig;
452 if (!WARN_ON(err >= ARRAY_SIZE(fsck_flags_extra)))
453 flags |= fsck_flags_extra[err];
459 * Ugly: if there's a transaction in the current task it has to be
460 * passed in to unlock if we prompt for user input.
462 * But, plumbing a transaction and transaction restarts into
463 * bkey_validate() is problematic.
466 * - make all bkey errors AUTOFIX, they're simple anyways (we just
468 * - and we don't need to warn if we're not prompting
470 WARN_ON((flags & FSCK_CAN_FIX) &&
471 !(flags & FSCK_AUTOFIX) &&
473 bch2_current_has_btree_trans(c));
475 if (test_bit(err, c->sb.errors_silent))
476 return flags & FSCK_CAN_FIX
477 ? bch_err_throw(c, fsck_fix)
478 : bch_err_throw(c, fsck_ignore);
480 printbuf_indent_add_nextline(out, 2);
482 #ifdef BCACHEFS_LOG_PREFIX
483 if (strncmp(fmt, "bcachefs", 8))
484 prt_printf(out, bch2_log_msg(c, ""));
488 prt_vprintf(out, fmt, args);
491 /* Custom fix/continue/recreate/etc.? */
492 if (out->buf[out->pos - 1] == '?') {
493 const char *p = strrchr(out->buf, ',');
495 out->pos = p - out->buf;
496 action = kstrdup(p + 2, GFP_KERNEL);
504 mutex_lock(&c->fsck_error_msgs_lock);
505 bool repeat = false, print = true, suppress = false;
506 bool inconsistent = false, exiting = false;
507 struct fsck_err_state *s =
508 count_fsck_err_locked(c, err, buf.buf, &repeat, &print, &suppress);
514 if ((flags & FSCK_AUTOFIX) &&
515 (c->opts.errors == BCH_ON_ERROR_continue ||
516 c->opts.errors == BCH_ON_ERROR_fix_safe)) {
518 if (flags & FSCK_CAN_FIX) {
519 prt_actioning(out, action);
520 ret = bch_err_throw(c, fsck_fix);
522 prt_str(out, ", continuing");
523 ret = bch_err_throw(c, fsck_ignore);
527 } else if (!test_bit(BCH_FS_in_fsck, &c->flags)) {
528 if (c->opts.errors != BCH_ON_ERROR_continue ||
529 !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) {
530 prt_str_indented(out, ", shutting down\n"
531 "error not marked as autofix and not in fsck\n"
532 "run fsck, and forward to devs so error can be marked for self-healing");
535 ret = bch_err_throw(c, fsck_errors_not_fixed);
536 } else if (flags & FSCK_CAN_FIX) {
538 prt_actioning(out, action);
539 ret = bch_err_throw(c, fsck_fix);
541 prt_str(out, ", continuing");
542 ret = bch_err_throw(c, fsck_ignore);
544 } else if (c->opts.fix_errors == FSCK_FIX_exit) {
545 prt_str(out, ", exiting");
546 ret = bch_err_throw(c, fsck_errors_not_fixed);
547 } else if (flags & FSCK_CAN_FIX) {
548 int fix = s && s->fix
550 : c->opts.fix_errors;
552 if (fix == FSCK_FIX_ask) {
555 ret = do_fsck_ask_yn(c, trans, out, action);
559 if (ret >= YN_ALLNO && s)
560 s->fix = ret == YN_ALLNO
565 ? bch_err_throw(c, fsck_fix)
566 : bch_err_throw(c, fsck_ignore);
567 } else if (fix == FSCK_FIX_yes ||
568 (c->opts.nochanges &&
569 !(flags & FSCK_CAN_IGNORE))) {
571 prt_actioning(out, action);
572 ret = bch_err_throw(c, fsck_fix);
574 prt_str(out, ", not ");
575 prt_actioning(out, action);
576 ret = bch_err_throw(c, fsck_ignore);
579 if (flags & FSCK_CAN_IGNORE) {
580 prt_str(out, ", continuing");
581 ret = bch_err_throw(c, fsck_ignore);
583 prt_str(out, " (repair unimplemented)");
584 ret = bch_err_throw(c, fsck_repair_unimplemented);
588 if (bch2_err_matches(ret, BCH_ERR_fsck_ignore) &&
589 (c->opts.fix_errors == FSCK_FIX_exit ||
590 !(flags & FSCK_CAN_IGNORE)))
591 ret = bch_err_throw(c, fsck_errors_not_fixed);
593 if (test_bit(BCH_FS_in_fsck, &c->flags) &&
594 (!bch2_err_matches(ret, BCH_ERR_fsck_fix) &&
595 !bch2_err_matches(ret, BCH_ERR_fsck_ignore))) {
603 __bch2_inconsistent_error(c, out);
605 prt_printf(out, "Unable to continue, halting\n");
607 prt_printf(out, "Ratelimiting new instances of previous error\n");
610 /* possibly strip an empty line, from printbuf_indent_add */
611 while (out->pos && out->buf[out->pos - 1] == ' ')
613 printbuf_nul_terminate(out);
615 if (bch2_fs_stdio_redirect(c))
616 bch2_print(c, "%s", out->buf);
618 bch2_print_str(c, KERN_ERR, out->buf);
625 !(flags & FSCK_ERR_NO_LOG) &&
626 ret == -BCH_ERR_fsck_fix)
627 ret = bch2_trans_log_str(trans, bch2_sb_error_strs[err]) ?: ret;
629 mutex_unlock(&c->fsck_error_msgs_lock);
632 * We don't yet track whether the filesystem currently has errors, for
633 * log_fsck_err()s: that would require us to track for every error type
634 * which recovery pass corrects it, to get the fsck exit status correct:
636 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
638 } else if (bch2_err_matches(ret, BCH_ERR_fsck_fix)) {
639 set_bit(BCH_FS_errors_fixed, &c->flags);
641 set_bit(BCH_FS_errors_not_fixed, &c->flags);
642 set_bit(BCH_FS_error, &c->flags);
645 if (action != action_orig)
653 static const char * const bch2_bkey_validate_contexts[] = {
655 BKEY_VALIDATE_CONTEXTS()
660 int __bch2_bkey_fsck_err(struct bch_fs *c,
662 struct bkey_validate_context from,
663 enum bch_sb_error_id err,
664 const char *fmt, ...)
666 if (from.flags & BCH_VALIDATE_silent)
667 return bch_err_throw(c, fsck_delete_bkey);
669 unsigned fsck_flags = 0;
670 if (!(from.flags & (BCH_VALIDATE_write|BCH_VALIDATE_commit))) {
671 if (test_bit(err, c->sb.errors_silent))
672 return bch_err_throw(c, fsck_delete_bkey);
674 fsck_flags |= FSCK_AUTOFIX|FSCK_CAN_FIX;
676 if (!WARN_ON(err >= ARRAY_SIZE(fsck_flags_extra)))
677 fsck_flags |= fsck_flags_extra[err];
679 struct printbuf buf = PRINTBUF;
680 prt_printf(&buf, "invalid bkey in %s",
681 bch2_bkey_validate_contexts[from.from]);
683 if (from.from == BKEY_VALIDATE_journal)
684 prt_printf(&buf, " journal seq=%llu offset=%u",
685 from.journal_seq, from.journal_offset);
687 prt_str(&buf, " btree=");
688 bch2_btree_id_to_text(&buf, from.btree);
689 prt_printf(&buf, " level=%u: ", from.level);
691 bch2_bkey_val_to_text(&buf, c, k);
696 prt_vprintf(&buf, fmt, args);
699 int ret = __bch2_fsck_err(c, NULL, fsck_flags, err, "%s, delete?", buf.buf);
704 static void __bch2_flush_fsck_errs(struct bch_fs *c, bool print)
706 struct fsck_err_state *s, *n;
708 mutex_lock(&c->fsck_error_msgs_lock);
710 list_for_each_entry_safe(s, n, &c->fsck_error_msgs, list) {
711 if (print && s->ratelimited && s->last_msg)
712 bch_err(c, "Saw %llu errors like:\n %s", s->nr, s->last_msg);
719 mutex_unlock(&c->fsck_error_msgs_lock);
722 void bch2_flush_fsck_errs(struct bch_fs *c)
724 __bch2_flush_fsck_errs(c, true);
727 void bch2_free_fsck_errs(struct bch_fs *c)
729 __bch2_flush_fsck_errs(c, false);
732 int bch2_inum_offset_err_msg_trans(struct btree_trans *trans, struct printbuf *out,
733 subvol_inum inum, u64 offset)
735 u32 restart_count = trans->restart_count;
739 ret = bch2_inum_to_path(trans, inum, out);
740 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
743 if (!inum.subvol || ret)
744 prt_printf(out, "inum %llu:%llu", inum.subvol, inum.inum);
745 prt_printf(out, " offset %llu: ", offset);
747 return trans_was_restarted(trans, restart_count);
750 void bch2_inum_offset_err_msg(struct bch_fs *c, struct printbuf *out,
751 subvol_inum inum, u64 offset)
753 bch2_trans_do(c, bch2_inum_offset_err_msg_trans(trans, out, inum, offset));
756 int bch2_inum_snap_offset_err_msg_trans(struct btree_trans *trans, struct printbuf *out,
759 int ret = bch2_inum_snapshot_to_path(trans, pos.inode, pos.snapshot, NULL, out);
763 prt_printf(out, " offset %llu: ", pos.offset << 8);
767 void bch2_inum_snap_offset_err_msg(struct bch_fs *c, struct printbuf *out,
770 bch2_trans_do(c, bch2_inum_snap_offset_err_msg_trans(trans, out, pos));