1 // SPDX-License-Identifier: GPL-2.0
6 #define FSCK_ERR_RATELIMIT_NR 10
8 bool bch2_inconsistent_error(struct bch_fs *c)
10 set_bit(BCH_FS_error, &c->flags);
12 switch (c->opts.errors) {
13 case BCH_ON_ERROR_continue:
16 if (bch2_fs_emergency_read_only(c))
17 bch_err(c, "inconsistency detected - emergency read only");
19 case BCH_ON_ERROR_panic:
20 panic(bch2_fmt(c, "panic after error"));
27 void bch2_topology_error(struct bch_fs *c)
29 set_bit(BCH_FS_topology_error, &c->flags);
30 if (test_bit(BCH_FS_fsck_done, &c->flags))
31 bch2_inconsistent_error(c);
34 void bch2_fatal_error(struct bch_fs *c)
36 if (bch2_fs_emergency_read_only(c))
37 bch_err(c, "fatal error - emergency read only");
40 void bch2_io_error_work(struct work_struct *work)
42 struct bch_dev *ca = container_of(work, struct bch_dev, io_error_work);
43 struct bch_fs *c = ca->fs;
46 down_write(&c->state_lock);
47 dev = bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_ro,
48 BCH_FORCE_IF_DEGRADED);
50 ? __bch2_dev_set_state(c, ca, BCH_MEMBER_STATE_ro,
51 BCH_FORCE_IF_DEGRADED)
52 : bch2_fs_emergency_read_only(c))
54 "too many IO errors, setting %s RO",
55 dev ? "device" : "filesystem");
56 up_write(&c->state_lock);
59 void bch2_io_error(struct bch_dev *ca, enum bch_member_error_type type)
61 atomic64_inc(&ca->errors[type]);
62 //queue_work(system_long_wq, &ca->io_error_work);
73 #define bch2_fsck_ask_yn() YN_NO
76 #include "tools-util.h"
78 enum ask_yn bch2_fsck_ask_yn(void)
85 fputs(" (y,n, or Y,N for all errors of this type) ", stdout);
88 if (getline(&buf, &buflen, stdin) < 0)
89 die("error reading from standard input");
113 static struct fsck_err_state *fsck_err_get(struct bch_fs *c, const char *fmt)
115 struct fsck_err_state *s;
117 if (test_bit(BCH_FS_fsck_done, &c->flags))
120 list_for_each_entry(s, &c->fsck_error_msgs, list)
123 * move it to the head of the list: repeated fsck errors
126 list_move(&s->list, &c->fsck_error_msgs);
130 s = kzalloc(sizeof(*s), GFP_NOFS);
132 if (!c->fsck_alloc_msgs_err)
133 bch_err(c, "kmalloc err, cannot ratelimit fsck errs");
134 c->fsck_alloc_msgs_err = true;
138 INIT_LIST_HEAD(&s->list);
140 list_add(&s->list, &c->fsck_error_msgs);
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, ...)
149 struct fsck_err_state *s = NULL;
151 bool print = true, suppressing = false, inconsistent = false;
152 struct printbuf buf = PRINTBUF, *out = &buf;
153 int ret = -BCH_ERR_fsck_ignore;
155 if (test_bit(err, c->sb.errors_silent))
156 return -BCH_ERR_fsck_fix;
158 bch2_sb_error_count(c, err);
161 prt_vprintf(out, fmt, args);
164 mutex_lock(&c->fsck_error_msgs_lock);
165 s = fsck_err_get(c, fmt);
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:
172 if (s->last_msg && !strcmp(buf.buf, s->last_msg)) {
174 mutex_unlock(&c->fsck_error_msgs_lock);
180 s->last_msg = kstrdup(buf.buf, GFP_KERNEL);
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)
194 #ifdef BCACHEFS_LOG_PREFIX
195 if (!strncmp(fmt, "bcachefs:", 9))
196 prt_printf(out, bch2_log_msg(c, ""));
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");
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;
209 prt_str(out, ", continuing");
210 ret = -BCH_ERR_fsck_ignore;
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
218 : c->opts.fix_errors;
220 if (fix == FSCK_FIX_ask) {
223 prt_str(out, ": fix?");
224 bch2_print_string_as_lines(KERN_ERR, out->buf);
227 ask = bch2_fsck_ask_yn();
229 if (ask >= YN_ALLNO && s)
230 s->fix = ask == YN_ALLNO
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;
243 prt_str(out, ", not fixing");
245 } else if (flags & FSCK_NEED_FSCK) {
246 prt_str(out, " (run fsck to correct)");
248 prt_str(out, " (repair unimplemented)");
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;
257 bch2_print_string_as_lines(KERN_ERR, out->buf);
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");
269 mutex_unlock(&c->fsck_error_msgs_lock);
274 bch2_inconsistent_error(c);
276 if (ret == -BCH_ERR_fsck_fix) {
277 set_bit(BCH_FS_errors_fixed, &c->flags);
279 set_bit(BCH_FS_errors_not_fixed, &c->flags);
280 set_bit(BCH_FS_error, &c->flags);
286 void bch2_flush_fsck_errs(struct bch_fs *c)
288 struct fsck_err_state *s, *n;
290 mutex_lock(&c->fsck_error_msgs_lock);
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);
301 mutex_unlock(&c->fsck_error_msgs_lock);