11c6fdbd8SKent Overstreet // SPDX-License-Identifier: GPL-2.0
21c6fdbd8SKent Overstreet #include "bcachefs.h"
3a850bde6SKent Overstreet #include "btree_iter.h"
41c6fdbd8SKent Overstreet #include "error.h"
5b3c7fd35SKent Overstreet #include "journal.h"
6d2554263SKent Overstreet #include "recovery_passes.h"
71c6fdbd8SKent Overstreet #include "super.h"
896f37eabSKent Overstreet #include "thread_with_file.h"
91c6fdbd8SKent Overstreet
1089b05118SKent Overstreet #define FSCK_ERR_RATELIMIT_NR 10
1189b05118SKent Overstreet
bch2_inconsistent_error(struct bch_fs * c)121c6fdbd8SKent Overstreet bool bch2_inconsistent_error(struct bch_fs *c)
131c6fdbd8SKent Overstreet {
143c471b65SKent Overstreet set_bit(BCH_FS_error, &c->flags);
151c6fdbd8SKent Overstreet
161c6fdbd8SKent Overstreet switch (c->opts.errors) {
172436cb9fSKent Overstreet case BCH_ON_ERROR_continue:
181c6fdbd8SKent Overstreet return false;
1933dfafa9SKent Overstreet case BCH_ON_ERROR_fix_safe:
202436cb9fSKent Overstreet case BCH_ON_ERROR_ro:
211c6fdbd8SKent Overstreet if (bch2_fs_emergency_read_only(c))
22b3c7fd35SKent Overstreet bch_err(c, "inconsistency detected - emergency read only at journal seq %llu",
23b3c7fd35SKent Overstreet journal_cur_seq(&c->journal));
241c6fdbd8SKent Overstreet return true;
252436cb9fSKent Overstreet case BCH_ON_ERROR_panic:
261c6fdbd8SKent Overstreet panic(bch2_fmt(c, "panic after error"));
271c6fdbd8SKent Overstreet return true;
281c6fdbd8SKent Overstreet default:
291c6fdbd8SKent Overstreet BUG();
301c6fdbd8SKent Overstreet }
311c6fdbd8SKent Overstreet }
321c6fdbd8SKent Overstreet
bch2_topology_error(struct bch_fs * c)3352946d82SKent Overstreet int bch2_topology_error(struct bch_fs *c)
34aae15aafSKent Overstreet {
353c471b65SKent Overstreet set_bit(BCH_FS_topology_error, &c->flags);
3652946d82SKent Overstreet if (!test_bit(BCH_FS_fsck_running, &c->flags)) {
37aae15aafSKent Overstreet bch2_inconsistent_error(c);
3852946d82SKent Overstreet return -BCH_ERR_btree_need_topology_repair;
3952946d82SKent Overstreet } else {
4052946d82SKent Overstreet return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_check_topology) ?:
4152946d82SKent Overstreet -BCH_ERR_btree_node_read_validate_error;
4252946d82SKent Overstreet }
43aae15aafSKent Overstreet }
44aae15aafSKent Overstreet
bch2_fatal_error(struct bch_fs * c)451c6fdbd8SKent Overstreet void bch2_fatal_error(struct bch_fs *c)
461c6fdbd8SKent Overstreet {
471c6fdbd8SKent Overstreet if (bch2_fs_emergency_read_only(c))
48b74b147dSKent Overstreet bch_err(c, "fatal error - emergency read only");
491c6fdbd8SKent Overstreet }
501c6fdbd8SKent Overstreet
bch2_io_error_work(struct work_struct * work)511c6fdbd8SKent Overstreet void bch2_io_error_work(struct work_struct *work)
521c6fdbd8SKent Overstreet {
531c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(work, struct bch_dev, io_error_work);
541c6fdbd8SKent Overstreet struct bch_fs *c = ca->fs;
551c6fdbd8SKent Overstreet bool dev;
561c6fdbd8SKent Overstreet
571ada1606SKent Overstreet down_write(&c->state_lock);
582436cb9fSKent Overstreet dev = bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_ro,
591c6fdbd8SKent Overstreet BCH_FORCE_IF_DEGRADED);
601c6fdbd8SKent Overstreet if (dev
612436cb9fSKent Overstreet ? __bch2_dev_set_state(c, ca, BCH_MEMBER_STATE_ro,
621c6fdbd8SKent Overstreet BCH_FORCE_IF_DEGRADED)
631c6fdbd8SKent Overstreet : bch2_fs_emergency_read_only(c))
641c6fdbd8SKent Overstreet bch_err(ca,
651c6fdbd8SKent Overstreet "too many IO errors, setting %s RO",
661c6fdbd8SKent Overstreet dev ? "device" : "filesystem");
671ada1606SKent Overstreet up_write(&c->state_lock);
681c6fdbd8SKent Overstreet }
691c6fdbd8SKent Overstreet
bch2_io_error(struct bch_dev * ca,enum bch_member_error_type type)7094119eebSKent Overstreet void bch2_io_error(struct bch_dev *ca, enum bch_member_error_type type)
711c6fdbd8SKent Overstreet {
7294119eebSKent Overstreet atomic64_inc(&ca->errors[type]);
731c6fdbd8SKent Overstreet //queue_work(system_long_wq, &ca->io_error_work);
741c6fdbd8SKent Overstreet }
751c6fdbd8SKent Overstreet
76853b7393SKent Overstreet enum ask_yn {
77853b7393SKent Overstreet YN_NO,
78853b7393SKent Overstreet YN_YES,
79853b7393SKent Overstreet YN_ALLNO,
80853b7393SKent Overstreet YN_ALLYES,
81853b7393SKent Overstreet };
82853b7393SKent Overstreet
parse_yn_response(char * buf)8396f37eabSKent Overstreet static enum ask_yn parse_yn_response(char *buf)
84853b7393SKent Overstreet {
8596f37eabSKent Overstreet buf = strim(buf);
86853b7393SKent Overstreet
8796f37eabSKent Overstreet if (strlen(buf) == 1)
88853b7393SKent Overstreet switch (buf[0]) {
89853b7393SKent Overstreet case 'n':
90853b7393SKent Overstreet return YN_NO;
91853b7393SKent Overstreet case 'y':
92853b7393SKent Overstreet return YN_YES;
93853b7393SKent Overstreet case 'N':
94853b7393SKent Overstreet return YN_ALLNO;
95853b7393SKent Overstreet case 'Y':
96853b7393SKent Overstreet return YN_ALLYES;
97853b7393SKent Overstreet }
9896f37eabSKent Overstreet return -1;
99853b7393SKent Overstreet }
100853b7393SKent Overstreet
10196f37eabSKent Overstreet #ifdef __KERNEL__
bch2_fsck_ask_yn(struct bch_fs * c,struct btree_trans * trans)102889fb3dcSKent Overstreet static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c, struct btree_trans *trans)
10396f37eabSKent Overstreet {
10496f37eabSKent Overstreet struct stdio_redirect *stdio = c->stdio;
10596f37eabSKent Overstreet
10696f37eabSKent Overstreet if (c->stdio_filter && c->stdio_filter != current)
10796f37eabSKent Overstreet stdio = NULL;
10896f37eabSKent Overstreet
10996f37eabSKent Overstreet if (!stdio)
11096f37eabSKent Overstreet return YN_NO;
11196f37eabSKent Overstreet
112889fb3dcSKent Overstreet if (trans)
113889fb3dcSKent Overstreet bch2_trans_unlock(trans);
114889fb3dcSKent Overstreet
115889fb3dcSKent Overstreet unsigned long unlock_long_at = trans ? jiffies + HZ * 2 : 0;
1160c97c437SKent Overstreet darray_char line = {};
11796f37eabSKent Overstreet int ret;
11896f37eabSKent Overstreet
11996f37eabSKent Overstreet do {
120889fb3dcSKent Overstreet unsigned long t;
12196f37eabSKent Overstreet bch2_print(c, " (y,n, or Y,N for all errors of this type) ");
122889fb3dcSKent Overstreet rewait:
123889fb3dcSKent Overstreet t = unlock_long_at
124889fb3dcSKent Overstreet ? max_t(long, unlock_long_at - jiffies, 0)
125889fb3dcSKent Overstreet : MAX_SCHEDULE_TIMEOUT;
12696f37eabSKent Overstreet
127889fb3dcSKent Overstreet int r = bch2_stdio_redirect_readline_timeout(stdio, &line, t);
128889fb3dcSKent Overstreet if (r == -ETIME) {
129889fb3dcSKent Overstreet bch2_trans_unlock_long(trans);
130889fb3dcSKent Overstreet unlock_long_at = 0;
131889fb3dcSKent Overstreet goto rewait;
132889fb3dcSKent Overstreet }
133889fb3dcSKent Overstreet
1340c97c437SKent Overstreet if (r < 0) {
1350c97c437SKent Overstreet ret = YN_NO;
1360c97c437SKent Overstreet break;
1370c97c437SKent Overstreet }
138889fb3dcSKent Overstreet
1390c97c437SKent Overstreet darray_last(line) = '\0';
1400c97c437SKent Overstreet } while ((ret = parse_yn_response(line.data)) < 0);
14196f37eabSKent Overstreet
1420c97c437SKent Overstreet darray_exit(&line);
14396f37eabSKent Overstreet return ret;
14496f37eabSKent Overstreet }
14596f37eabSKent Overstreet #else
14696f37eabSKent Overstreet
14796f37eabSKent Overstreet #include "tools-util.h"
14896f37eabSKent Overstreet
bch2_fsck_ask_yn(struct bch_fs * c,struct btree_trans * trans)149889fb3dcSKent Overstreet static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c, struct btree_trans *trans)
15096f37eabSKent Overstreet {
15196f37eabSKent Overstreet char *buf = NULL;
15296f37eabSKent Overstreet size_t buflen = 0;
15396f37eabSKent Overstreet int ret;
15496f37eabSKent Overstreet
15596f37eabSKent Overstreet do {
15696f37eabSKent Overstreet fputs(" (y,n, or Y,N for all errors of this type) ", stdout);
15796f37eabSKent Overstreet fflush(stdout);
15896f37eabSKent Overstreet
15996f37eabSKent Overstreet if (getline(&buf, &buflen, stdin) < 0)
16096f37eabSKent Overstreet die("error reading from standard input");
16196f37eabSKent Overstreet } while ((ret = parse_yn_response(buf)) < 0);
16296f37eabSKent Overstreet
163853b7393SKent Overstreet free(buf);
164853b7393SKent Overstreet return ret;
165853b7393SKent Overstreet }
166853b7393SKent Overstreet
1671c6fdbd8SKent Overstreet #endif
1681c6fdbd8SKent Overstreet
fsck_err_get(struct bch_fs * c,const char * fmt)169dbb9936bSKent Overstreet static struct fsck_err_state *fsck_err_get(struct bch_fs *c, const char *fmt)
1701c6fdbd8SKent Overstreet {
171dbb9936bSKent Overstreet struct fsck_err_state *s;
1721c6fdbd8SKent Overstreet
173d55ddf6eSKent Overstreet if (!test_bit(BCH_FS_fsck_running, &c->flags))
174dbb9936bSKent Overstreet return NULL;
1751c6fdbd8SKent Overstreet
176f5d26fa3SKent Overstreet list_for_each_entry(s, &c->fsck_error_msgs, list)
177dbb9936bSKent Overstreet if (s->fmt == fmt) {
178dbb9936bSKent Overstreet /*
179dbb9936bSKent Overstreet * move it to the head of the list: repeated fsck errors
180dbb9936bSKent Overstreet * are common
181dbb9936bSKent Overstreet */
182f5d26fa3SKent Overstreet list_move(&s->list, &c->fsck_error_msgs);
183dbb9936bSKent Overstreet return s;
184dbb9936bSKent Overstreet }
1851c6fdbd8SKent Overstreet
186beb6db68SKent Overstreet s = kzalloc(sizeof(*s), GFP_NOFS);
1871c6fdbd8SKent Overstreet if (!s) {
188f5d26fa3SKent Overstreet if (!c->fsck_alloc_msgs_err)
1891c6fdbd8SKent Overstreet bch_err(c, "kmalloc err, cannot ratelimit fsck errs");
190f5d26fa3SKent Overstreet c->fsck_alloc_msgs_err = true;
191dbb9936bSKent Overstreet return NULL;
1921c6fdbd8SKent Overstreet }
1931c6fdbd8SKent Overstreet
1941c6fdbd8SKent Overstreet INIT_LIST_HEAD(&s->list);
1951c6fdbd8SKent Overstreet s->fmt = fmt;
196f5d26fa3SKent Overstreet list_add(&s->list, &c->fsck_error_msgs);
197dbb9936bSKent Overstreet return s;
198dbb9936bSKent Overstreet }
199dbb9936bSKent Overstreet
20019391b92SKent Overstreet /* s/fix?/fixing/ s/recreate?/recreating/ */
prt_actioning(struct printbuf * out,const char * action)20119391b92SKent Overstreet static void prt_actioning(struct printbuf *out, const char *action)
20219391b92SKent Overstreet {
20319391b92SKent Overstreet unsigned len = strlen(action);
20419391b92SKent Overstreet
20519391b92SKent Overstreet BUG_ON(action[len - 1] != '?');
20619391b92SKent Overstreet --len;
20719391b92SKent Overstreet
20819391b92SKent Overstreet if (action[len - 1] == 'e')
20919391b92SKent Overstreet --len;
21019391b92SKent Overstreet
21119391b92SKent Overstreet prt_bytes(out, action, len);
21219391b92SKent Overstreet prt_str(out, "ing");
21319391b92SKent Overstreet }
21419391b92SKent Overstreet
21533dfafa9SKent Overstreet static const u8 fsck_flags_extra[] = {
21633dfafa9SKent Overstreet #define x(t, n, flags) [BCH_FSCK_ERR_##t] = flags,
21733dfafa9SKent Overstreet BCH_SB_ERRS()
21833dfafa9SKent Overstreet #undef x
21933dfafa9SKent Overstreet };
22033dfafa9SKent Overstreet
__bch2_fsck_err(struct bch_fs * c,struct btree_trans * trans,enum bch_fsck_flags flags,enum bch_sb_error_id err,const char * fmt,...)221a850bde6SKent Overstreet int __bch2_fsck_err(struct bch_fs *c,
222a850bde6SKent Overstreet struct btree_trans *trans,
223b65db750SKent Overstreet enum bch_fsck_flags flags,
224b65db750SKent Overstreet enum bch_sb_error_id err,
225b65db750SKent Overstreet const char *fmt, ...)
226dbb9936bSKent Overstreet {
227dbb9936bSKent Overstreet struct fsck_err_state *s = NULL;
228dbb9936bSKent Overstreet va_list args;
22922f51621SKent Overstreet bool print = true, suppressing = false, inconsistent = false;
230dbb9936bSKent Overstreet struct printbuf buf = PRINTBUF, *out = &buf;
231dbb9936bSKent Overstreet int ret = -BCH_ERR_fsck_ignore;
23219391b92SKent Overstreet const char *action_orig = "fix?", *action = action_orig;
233dbb9936bSKent Overstreet
234e76a2b65SKent Overstreet might_sleep();
235e76a2b65SKent Overstreet
23633dfafa9SKent Overstreet if (!WARN_ON(err >= ARRAY_SIZE(fsck_flags_extra)))
23733dfafa9SKent Overstreet flags |= fsck_flags_extra[err];
23833dfafa9SKent Overstreet
239a850bde6SKent Overstreet if (!c)
240a850bde6SKent Overstreet c = trans->c;
241a850bde6SKent Overstreet
242a850bde6SKent Overstreet WARN_ON(!trans && bch2_current_has_btree_trans(c));
243a850bde6SKent Overstreet
244a64a3733SKent Overstreet if ((flags & FSCK_CAN_FIX) &&
245a64a3733SKent Overstreet test_bit(err, c->sb.errors_silent))
2468b16413cSKent Overstreet return -BCH_ERR_fsck_fix;
2478b16413cSKent Overstreet
248b65db750SKent Overstreet bch2_sb_error_count(c, err);
249b65db750SKent Overstreet
2509c5d38bbSKent Overstreet va_start(args, fmt);
2519c5d38bbSKent Overstreet prt_vprintf(out, fmt, args);
2529c5d38bbSKent Overstreet va_end(args);
2539c5d38bbSKent Overstreet
25419391b92SKent Overstreet /* Custom fix/continue/recreate/etc.? */
25519391b92SKent Overstreet if (out->buf[out->pos - 1] == '?') {
25619391b92SKent Overstreet const char *p = strrchr(out->buf, ',');
25719391b92SKent Overstreet if (p) {
25819391b92SKent Overstreet out->pos = p - out->buf;
25919391b92SKent Overstreet action = kstrdup(p + 2, GFP_KERNEL);
26019391b92SKent Overstreet if (!action) {
26119391b92SKent Overstreet ret = -ENOMEM;
26219391b92SKent Overstreet goto err;
26319391b92SKent Overstreet }
26419391b92SKent Overstreet }
26519391b92SKent Overstreet }
26619391b92SKent Overstreet
267f5d26fa3SKent Overstreet mutex_lock(&c->fsck_error_msgs_lock);
268dbb9936bSKent Overstreet s = fsck_err_get(c, fmt);
269dbb9936bSKent Overstreet if (s) {
270c8d5b714SKent Overstreet /*
271c8d5b714SKent Overstreet * We may be called multiple times for the same error on
272c8d5b714SKent Overstreet * transaction restart - this memoizes instead of asking the user
273c8d5b714SKent Overstreet * multiple times for the same error:
274c8d5b714SKent Overstreet */
2759c5d38bbSKent Overstreet if (s->last_msg && !strcmp(buf.buf, s->last_msg)) {
2769c5d38bbSKent Overstreet ret = s->ret;
277f5d26fa3SKent Overstreet mutex_unlock(&c->fsck_error_msgs_lock);
27819391b92SKent Overstreet goto err;
2799c5d38bbSKent Overstreet }
2809c5d38bbSKent Overstreet
2819c5d38bbSKent Overstreet kfree(s->last_msg);
2829c5d38bbSKent Overstreet s->last_msg = kstrdup(buf.buf, GFP_KERNEL);
28319391b92SKent Overstreet if (!s->last_msg) {
28419391b92SKent Overstreet mutex_unlock(&c->fsck_error_msgs_lock);
28519391b92SKent Overstreet ret = -ENOMEM;
28619391b92SKent Overstreet goto err;
28719391b92SKent Overstreet }
2889c5d38bbSKent Overstreet
289e2ee3eaaSKent Overstreet if (c->opts.ratelimit_errors &&
29008061519SKent Overstreet !(flags & FSCK_NO_RATELIMIT) &&
291e2ee3eaaSKent Overstreet s->nr >= FSCK_ERR_RATELIMIT_NR) {
292e2ee3eaaSKent Overstreet if (s->nr == FSCK_ERR_RATELIMIT_NR)
293e2ee3eaaSKent Overstreet suppressing = true;
294e2ee3eaaSKent Overstreet else
295e2ee3eaaSKent Overstreet print = false;
296e2ee3eaaSKent Overstreet }
297dbb9936bSKent Overstreet
298dbb9936bSKent Overstreet s->nr++;
299dbb9936bSKent Overstreet }
300dbb9936bSKent Overstreet
301b2d1d56bSKent Overstreet #ifdef BCACHEFS_LOG_PREFIX
302dbb9936bSKent Overstreet if (!strncmp(fmt, "bcachefs:", 9))
303dbb9936bSKent Overstreet prt_printf(out, bch2_log_msg(c, ""));
304b2d1d56bSKent Overstreet #endif
305dbb9936bSKent Overstreet
30633dfafa9SKent Overstreet if ((flags & FSCK_CAN_FIX) &&
30733dfafa9SKent Overstreet (flags & FSCK_AUTOFIX) &&
30833dfafa9SKent Overstreet (c->opts.errors == BCH_ON_ERROR_continue ||
30933dfafa9SKent Overstreet c->opts.errors == BCH_ON_ERROR_fix_safe)) {
31033dfafa9SKent Overstreet prt_str(out, ", ");
31133dfafa9SKent Overstreet prt_actioning(out, action);
31233dfafa9SKent Overstreet ret = -BCH_ERR_fsck_fix;
31333dfafa9SKent Overstreet } else if (!test_bit(BCH_FS_fsck_running, &c->flags)) {
314dbb9936bSKent Overstreet if (c->opts.errors != BCH_ON_ERROR_continue ||
315dbb9936bSKent Overstreet !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) {
316dbb9936bSKent Overstreet prt_str(out, ", shutting down");
31722f51621SKent Overstreet inconsistent = true;
318dbb9936bSKent Overstreet ret = -BCH_ERR_fsck_errors_not_fixed;
319dbb9936bSKent Overstreet } else if (flags & FSCK_CAN_FIX) {
32019391b92SKent Overstreet prt_str(out, ", ");
32119391b92SKent Overstreet prt_actioning(out, action);
322dbb9936bSKent Overstreet ret = -BCH_ERR_fsck_fix;
323dbb9936bSKent Overstreet } else {
324dbb9936bSKent Overstreet prt_str(out, ", continuing");
325dbb9936bSKent Overstreet ret = -BCH_ERR_fsck_ignore;
326dbb9936bSKent Overstreet }
327a0f8faeaSKent Overstreet } else if (c->opts.fix_errors == FSCK_FIX_exit) {
328dbb9936bSKent Overstreet prt_str(out, ", exiting");
329dbb9936bSKent Overstreet ret = -BCH_ERR_fsck_errors_not_fixed;
3300bc166ffSKent Overstreet } else if (flags & FSCK_CAN_FIX) {
331853b7393SKent Overstreet int fix = s && s->fix
332853b7393SKent Overstreet ? s->fix
333853b7393SKent Overstreet : c->opts.fix_errors;
334853b7393SKent Overstreet
335a0f8faeaSKent Overstreet if (fix == FSCK_FIX_ask) {
33619391b92SKent Overstreet prt_str(out, ", ");
33719391b92SKent Overstreet prt_str(out, action);
338853b7393SKent Overstreet
33996f37eabSKent Overstreet if (bch2_fs_stdio_redirect(c))
34096f37eabSKent Overstreet bch2_print(c, "%s", out->buf);
34196f37eabSKent Overstreet else
342dbb9936bSKent Overstreet bch2_print_string_as_lines(KERN_ERR, out->buf);
343dbb9936bSKent Overstreet print = false;
344853b7393SKent Overstreet
345889fb3dcSKent Overstreet int ask = bch2_fsck_ask_yn(c, trans);
346889fb3dcSKent Overstreet
347889fb3dcSKent Overstreet if (trans) {
348889fb3dcSKent Overstreet ret = bch2_trans_relock(trans);
349889fb3dcSKent Overstreet if (ret) {
350889fb3dcSKent Overstreet mutex_unlock(&c->fsck_error_msgs_lock);
351889fb3dcSKent Overstreet goto err;
352889fb3dcSKent Overstreet }
353889fb3dcSKent Overstreet }
354853b7393SKent Overstreet
355853b7393SKent Overstreet if (ask >= YN_ALLNO && s)
356853b7393SKent Overstreet s->fix = ask == YN_ALLNO
357a0f8faeaSKent Overstreet ? FSCK_FIX_no
358a0f8faeaSKent Overstreet : FSCK_FIX_yes;
359853b7393SKent Overstreet
360853b7393SKent Overstreet ret = ask & 1
361dbb9936bSKent Overstreet ? -BCH_ERR_fsck_fix
362dbb9936bSKent Overstreet : -BCH_ERR_fsck_ignore;
363a0f8faeaSKent Overstreet } else if (fix == FSCK_FIX_yes ||
3641c6fdbd8SKent Overstreet (c->opts.nochanges &&
3651c6fdbd8SKent Overstreet !(flags & FSCK_CAN_IGNORE))) {
36619391b92SKent Overstreet prt_str(out, ", ");
36719391b92SKent Overstreet prt_actioning(out, action);
368dbb9936bSKent Overstreet ret = -BCH_ERR_fsck_fix;
3691c6fdbd8SKent Overstreet } else {
37019391b92SKent Overstreet prt_str(out, ", not ");
37119391b92SKent Overstreet prt_actioning(out, action);
3721c6fdbd8SKent Overstreet }
3731c6fdbd8SKent Overstreet } else if (flags & FSCK_NEED_FSCK) {
374dbb9936bSKent Overstreet prt_str(out, " (run fsck to correct)");
3751c6fdbd8SKent Overstreet } else {
376dbb9936bSKent Overstreet prt_str(out, " (repair unimplemented)");
3771c6fdbd8SKent Overstreet }
3781c6fdbd8SKent Overstreet
379dbb9936bSKent Overstreet if (ret == -BCH_ERR_fsck_ignore &&
380a0f8faeaSKent Overstreet (c->opts.fix_errors == FSCK_FIX_exit ||
381dbb9936bSKent Overstreet !(flags & FSCK_CAN_IGNORE)))
382dbb9936bSKent Overstreet ret = -BCH_ERR_fsck_errors_not_fixed;
383dbb9936bSKent Overstreet
38496f37eabSKent Overstreet if (print) {
38596f37eabSKent Overstreet if (bch2_fs_stdio_redirect(c))
38696f37eabSKent Overstreet bch2_print(c, "%s\n", out->buf);
38796f37eabSKent Overstreet else
388dbb9936bSKent Overstreet bch2_print_string_as_lines(KERN_ERR, out->buf);
38996f37eabSKent Overstreet }
390dbb9936bSKent Overstreet
391d55ddf6eSKent Overstreet if (test_bit(BCH_FS_fsck_running, &c->flags) &&
392dbb9936bSKent Overstreet (ret != -BCH_ERR_fsck_fix &&
393dbb9936bSKent Overstreet ret != -BCH_ERR_fsck_ignore))
394dbb9936bSKent Overstreet bch_err(c, "Unable to continue, halting");
395dbb9936bSKent Overstreet else if (suppressing)
3961c6fdbd8SKent Overstreet bch_err(c, "Ratelimiting new instances of previous error");
3971c6fdbd8SKent Overstreet
3989c5d38bbSKent Overstreet if (s)
3999c5d38bbSKent Overstreet s->ret = ret;
4009c5d38bbSKent Overstreet
401f5d26fa3SKent Overstreet mutex_unlock(&c->fsck_error_msgs_lock);
4021c6fdbd8SKent Overstreet
40322f51621SKent Overstreet if (inconsistent)
40422f51621SKent Overstreet bch2_inconsistent_error(c);
40522f51621SKent Overstreet
406dbb9936bSKent Overstreet if (ret == -BCH_ERR_fsck_fix) {
4073c471b65SKent Overstreet set_bit(BCH_FS_errors_fixed, &c->flags);
4080bc166ffSKent Overstreet } else {
4093c471b65SKent Overstreet set_bit(BCH_FS_errors_not_fixed, &c->flags);
4103c471b65SKent Overstreet set_bit(BCH_FS_error, &c->flags);
4110bc166ffSKent Overstreet }
41219391b92SKent Overstreet err:
41319391b92SKent Overstreet if (action != action_orig)
41419391b92SKent Overstreet kfree(action);
41519391b92SKent Overstreet printbuf_exit(&buf);
416dbb9936bSKent Overstreet return ret;
4171c6fdbd8SKent Overstreet }
4181c6fdbd8SKent Overstreet
__bch2_bkey_fsck_err(struct bch_fs * c,struct bkey_s_c k,enum bch_fsck_flags flags,enum bch_sb_error_id err,const char * fmt,...)419*d97de0d0SKent Overstreet int __bch2_bkey_fsck_err(struct bch_fs *c,
420*d97de0d0SKent Overstreet struct bkey_s_c k,
421*d97de0d0SKent Overstreet enum bch_fsck_flags flags,
422*d97de0d0SKent Overstreet enum bch_sb_error_id err,
423*d97de0d0SKent Overstreet const char *fmt, ...)
424*d97de0d0SKent Overstreet {
425*d97de0d0SKent Overstreet struct printbuf buf = PRINTBUF;
426*d97de0d0SKent Overstreet va_list args;
427*d97de0d0SKent Overstreet
428*d97de0d0SKent Overstreet prt_str(&buf, "invalid bkey ");
429*d97de0d0SKent Overstreet bch2_bkey_val_to_text(&buf, c, k);
430*d97de0d0SKent Overstreet prt_str(&buf, "\n ");
431*d97de0d0SKent Overstreet va_start(args, fmt);
432*d97de0d0SKent Overstreet prt_vprintf(&buf, fmt, args);
433*d97de0d0SKent Overstreet va_end(args);
434*d97de0d0SKent Overstreet prt_str(&buf, ": delete?");
435*d97de0d0SKent Overstreet
436*d97de0d0SKent Overstreet int ret = __bch2_fsck_err(c, NULL, flags, err, "%s", buf.buf);
437*d97de0d0SKent Overstreet printbuf_exit(&buf);
438*d97de0d0SKent Overstreet return ret;
439*d97de0d0SKent Overstreet }
440*d97de0d0SKent Overstreet
bch2_flush_fsck_errs(struct bch_fs * c)4411c6fdbd8SKent Overstreet void bch2_flush_fsck_errs(struct bch_fs *c)
4421c6fdbd8SKent Overstreet {
4431c6fdbd8SKent Overstreet struct fsck_err_state *s, *n;
4441c6fdbd8SKent Overstreet
445f5d26fa3SKent Overstreet mutex_lock(&c->fsck_error_msgs_lock);
4461c6fdbd8SKent Overstreet
447f5d26fa3SKent Overstreet list_for_each_entry_safe(s, n, &c->fsck_error_msgs, list) {
4489c5d38bbSKent Overstreet if (s->ratelimited && s->last_msg)
4499c5d38bbSKent Overstreet bch_err(c, "Saw %llu errors like:\n %s", s->nr, s->last_msg);
4501c6fdbd8SKent Overstreet
4511c6fdbd8SKent Overstreet list_del(&s->list);
4529c5d38bbSKent Overstreet kfree(s->last_msg);
4531c6fdbd8SKent Overstreet kfree(s);
4541c6fdbd8SKent Overstreet }
4551c6fdbd8SKent Overstreet
456f5d26fa3SKent Overstreet mutex_unlock(&c->fsck_error_msgs_lock);
4571c6fdbd8SKent Overstreet }
458