1 // SPDX-License-Identifier: GPL-2.0
7 #define FSCK_ERR_RATELIMIT_NR 10
9 bool bch2_inconsistent_error(struct bch_fs *c)
11 set_bit(BCH_FS_ERROR, &c->flags);
13 switch (c->opts.errors) {
14 case BCH_ON_ERROR_continue:
17 if (bch2_fs_emergency_read_only(c))
18 bch_err(c, "inconsistency detected - emergency read only");
20 case BCH_ON_ERROR_panic:
21 panic(bch2_fmt(c, "panic after error"));
28 void bch2_topology_error(struct bch_fs *c)
30 set_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags);
31 if (test_bit(BCH_FS_FSCK_DONE, &c->flags))
32 bch2_inconsistent_error(c);
35 void bch2_fatal_error(struct bch_fs *c)
37 if (bch2_fs_emergency_read_only(c))
38 bch_err(c, "fatal error - emergency read only");
41 void bch2_io_error_work(struct work_struct *work)
43 struct bch_dev *ca = container_of(work, struct bch_dev, io_error_work);
44 struct bch_fs *c = ca->fs;
47 down_write(&c->state_lock);
48 dev = bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_ro,
49 BCH_FORCE_IF_DEGRADED);
51 ? __bch2_dev_set_state(c, ca, BCH_MEMBER_STATE_ro,
52 BCH_FORCE_IF_DEGRADED)
53 : bch2_fs_emergency_read_only(c))
55 "too many IO errors, setting %s RO",
56 dev ? "device" : "filesystem");
57 up_write(&c->state_lock);
60 void bch2_io_error(struct bch_dev *ca)
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_errors, list)
123 * move it to the head of the list: repeated fsck errors
126 list_move(&s->list, &c->fsck_errors);
130 s = kzalloc(sizeof(*s), GFP_NOFS);
132 if (!c->fsck_alloc_err)
133 bch_err(c, "kmalloc err, cannot ratelimit fsck errs");
134 c->fsck_alloc_err = true;
138 INIT_LIST_HEAD(&s->list);
140 list_add(&s->list, &c->fsck_errors);
144 int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
146 struct fsck_err_state *s = NULL;
148 bool print = true, suppressing = false, inconsistent = false;
149 struct printbuf buf = PRINTBUF, *out = &buf;
150 int ret = -BCH_ERR_fsck_ignore;
153 prt_vprintf(out, fmt, args);
156 mutex_lock(&c->fsck_error_lock);
157 s = fsck_err_get(c, fmt);
160 * We may be called multiple times for the same error on
161 * transaction restart - this memoizes instead of asking the user
162 * multiple times for the same error:
164 if (s->last_msg && !strcmp(buf.buf, s->last_msg)) {
166 mutex_unlock(&c->fsck_error_lock);
172 s->last_msg = kstrdup(buf.buf, GFP_KERNEL);
174 if (c->opts.ratelimit_errors &&
175 !(flags & FSCK_NO_RATELIMIT) &&
176 s->nr >= FSCK_ERR_RATELIMIT_NR) {
177 if (s->nr == FSCK_ERR_RATELIMIT_NR)
186 #ifdef BCACHEFS_LOG_PREFIX
187 if (!strncmp(fmt, "bcachefs:", 9))
188 prt_printf(out, bch2_log_msg(c, ""));
191 if (test_bit(BCH_FS_FSCK_DONE, &c->flags)) {
192 if (c->opts.errors != BCH_ON_ERROR_continue ||
193 !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) {
194 prt_str(out, ", shutting down");
196 ret = -BCH_ERR_fsck_errors_not_fixed;
197 } else if (flags & FSCK_CAN_FIX) {
198 prt_str(out, ", fixing");
199 ret = -BCH_ERR_fsck_fix;
201 prt_str(out, ", continuing");
202 ret = -BCH_ERR_fsck_ignore;
204 } else if (c->opts.fix_errors == FSCK_FIX_exit) {
205 prt_str(out, ", exiting");
206 ret = -BCH_ERR_fsck_errors_not_fixed;
207 } else if (flags & FSCK_CAN_FIX) {
208 int fix = s && s->fix
210 : c->opts.fix_errors;
212 if (fix == FSCK_FIX_ask) {
215 prt_str(out, ": fix?");
216 bch2_print_string_as_lines(KERN_ERR, out->buf);
219 ask = bch2_fsck_ask_yn();
221 if (ask >= YN_ALLNO && s)
222 s->fix = ask == YN_ALLNO
228 : -BCH_ERR_fsck_ignore;
229 } else if (fix == FSCK_FIX_yes ||
230 (c->opts.nochanges &&
231 !(flags & FSCK_CAN_IGNORE))) {
232 prt_str(out, ", fixing");
233 ret = -BCH_ERR_fsck_fix;
235 prt_str(out, ", not fixing");
237 } else if (flags & FSCK_NEED_FSCK) {
238 prt_str(out, " (run fsck to correct)");
240 prt_str(out, " (repair unimplemented)");
243 if (ret == -BCH_ERR_fsck_ignore &&
244 (c->opts.fix_errors == FSCK_FIX_exit ||
245 !(flags & FSCK_CAN_IGNORE)))
246 ret = -BCH_ERR_fsck_errors_not_fixed;
249 bch2_print_string_as_lines(KERN_ERR, out->buf);
251 if (!test_bit(BCH_FS_FSCK_DONE, &c->flags) &&
252 (ret != -BCH_ERR_fsck_fix &&
253 ret != -BCH_ERR_fsck_ignore))
254 bch_err(c, "Unable to continue, halting");
255 else if (suppressing)
256 bch_err(c, "Ratelimiting new instances of previous error");
261 mutex_unlock(&c->fsck_error_lock);
266 bch2_inconsistent_error(c);
268 if (ret == -BCH_ERR_fsck_fix) {
269 set_bit(BCH_FS_ERRORS_FIXED, &c->flags);
271 set_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags);
272 set_bit(BCH_FS_ERROR, &c->flags);
278 void bch2_flush_fsck_errs(struct bch_fs *c)
280 struct fsck_err_state *s, *n;
282 mutex_lock(&c->fsck_error_lock);
284 list_for_each_entry_safe(s, n, &c->fsck_errors, list) {
285 if (s->ratelimited && s->last_msg)
286 bch_err(c, "Saw %llu errors like:\n %s", s->nr, s->last_msg);
293 mutex_unlock(&c->fsck_error_lock);