~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/bcachefs/error.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include "bcachefs.h"
  3 #include "btree_iter.h"
  4 #include "error.h"
  5 #include "journal.h"
  6 #include "recovery_passes.h"
  7 #include "super.h"
  8 #include "thread_with_file.h"
  9 
 10 #define FSCK_ERR_RATELIMIT_NR   10
 11 
 12 bool bch2_inconsistent_error(struct bch_fs *c)
 13 {
 14         set_bit(BCH_FS_error, &c->flags);
 15 
 16         switch (c->opts.errors) {
 17         case BCH_ON_ERROR_continue:
 18                 return false;
 19         case BCH_ON_ERROR_fix_safe:
 20         case BCH_ON_ERROR_ro:
 21                 if (bch2_fs_emergency_read_only(c))
 22                         bch_err(c, "inconsistency detected - emergency read only at journal seq %llu",
 23                                 journal_cur_seq(&c->journal));
 24                 return true;
 25         case BCH_ON_ERROR_panic:
 26                 panic(bch2_fmt(c, "panic after error"));
 27                 return true;
 28         default:
 29                 BUG();
 30         }
 31 }
 32 
 33 int bch2_topology_error(struct bch_fs *c)
 34 {
 35         set_bit(BCH_FS_topology_error, &c->flags);
 36         if (!test_bit(BCH_FS_fsck_running, &c->flags)) {
 37                 bch2_inconsistent_error(c);
 38                 return -BCH_ERR_btree_need_topology_repair;
 39         } else {
 40                 return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_check_topology) ?:
 41                         -BCH_ERR_btree_node_read_validate_error;
 42         }
 43 }
 44 
 45 void bch2_fatal_error(struct bch_fs *c)
 46 {
 47         if (bch2_fs_emergency_read_only(c))
 48                 bch_err(c, "fatal error - emergency read only");
 49 }
 50 
 51 void bch2_io_error_work(struct work_struct *work)
 52 {
 53         struct bch_dev *ca = container_of(work, struct bch_dev, io_error_work);
 54         struct bch_fs *c = ca->fs;
 55         bool dev;
 56 
 57         down_write(&c->state_lock);
 58         dev = bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_ro,
 59                                     BCH_FORCE_IF_DEGRADED);
 60         if (dev
 61             ? __bch2_dev_set_state(c, ca, BCH_MEMBER_STATE_ro,
 62                                   BCH_FORCE_IF_DEGRADED)
 63             : bch2_fs_emergency_read_only(c))
 64                 bch_err(ca,
 65                         "too many IO errors, setting %s RO",
 66                         dev ? "device" : "filesystem");
 67         up_write(&c->state_lock);
 68 }
 69 
 70 void bch2_io_error(struct bch_dev *ca, enum bch_member_error_type type)
 71 {
 72         atomic64_inc(&ca->errors[type]);
 73         //queue_work(system_long_wq, &ca->io_error_work);
 74 }
 75 
 76 enum ask_yn {
 77         YN_NO,
 78         YN_YES,
 79         YN_ALLNO,
 80         YN_ALLYES,
 81 };
 82 
 83 static enum ask_yn parse_yn_response(char *buf)
 84 {
 85         buf = strim(buf);
 86 
 87         if (strlen(buf) == 1)
 88                 switch (buf[0]) {
 89                 case 'n':
 90                         return YN_NO;
 91                 case 'y':
 92                         return YN_YES;
 93                 case 'N':
 94                         return YN_ALLNO;
 95                 case 'Y':
 96                         return YN_ALLYES;
 97                 }
 98         return -1;
 99 }
100 
101 #ifdef __KERNEL__
102 static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c, struct btree_trans *trans)
103 {
104         struct stdio_redirect *stdio = c->stdio;
105 
106         if (c->stdio_filter && c->stdio_filter != current)
107                 stdio = NULL;
108 
109         if (!stdio)
110                 return YN_NO;
111 
112         if (trans)
113                 bch2_trans_unlock(trans);
114 
115         unsigned long unlock_long_at = trans ? jiffies + HZ * 2 : 0;
116         darray_char line = {};
117         int ret;
118 
119         do {
120                 unsigned long t;
121                 bch2_print(c, " (y,n, or Y,N for all errors of this type) ");
122 rewait:
123                 t = unlock_long_at
124                         ? max_t(long, unlock_long_at - jiffies, 0)
125                         : MAX_SCHEDULE_TIMEOUT;
126 
127                 int r = bch2_stdio_redirect_readline_timeout(stdio, &line, t);
128                 if (r == -ETIME) {
129                         bch2_trans_unlock_long(trans);
130                         unlock_long_at = 0;
131                         goto rewait;
132                 }
133 
134                 if (r < 0) {
135                         ret = YN_NO;
136                         break;
137                 }
138 
139                 darray_last(line) = '\0';
140         } while ((ret = parse_yn_response(line.data)) < 0);
141 
142         darray_exit(&line);
143         return ret;
144 }
145 #else
146 
147 #include "tools-util.h"
148 
149 static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c, struct btree_trans *trans)
150 {
151         char *buf = NULL;
152         size_t buflen = 0;
153         int ret;
154 
155         do {
156                 fputs(" (y,n, or Y,N for all errors of this type) ", stdout);
157                 fflush(stdout);
158 
159                 if (getline(&buf, &buflen, stdin) < 0)
160                         die("error reading from standard input");
161         } while ((ret = parse_yn_response(buf)) < 0);
162 
163         free(buf);
164         return ret;
165 }
166 
167 #endif
168 
169 static struct fsck_err_state *fsck_err_get(struct bch_fs *c, const char *fmt)
170 {
171         struct fsck_err_state *s;
172 
173         if (!test_bit(BCH_FS_fsck_running, &c->flags))
174                 return NULL;
175 
176         list_for_each_entry(s, &c->fsck_error_msgs, list)
177                 if (s->fmt == fmt) {
178                         /*
179                          * move it to the head of the list: repeated fsck errors
180                          * are common
181                          */
182                         list_move(&s->list, &c->fsck_error_msgs);
183                         return s;
184                 }
185 
186         s = kzalloc(sizeof(*s), GFP_NOFS);
187         if (!s) {
188                 if (!c->fsck_alloc_msgs_err)
189                         bch_err(c, "kmalloc err, cannot ratelimit fsck errs");
190                 c->fsck_alloc_msgs_err = true;
191                 return NULL;
192         }
193 
194         INIT_LIST_HEAD(&s->list);
195         s->fmt = fmt;
196         list_add(&s->list, &c->fsck_error_msgs);
197         return s;
198 }
199 
200 /* s/fix?/fixing/ s/recreate?/recreating/ */
201 static void prt_actioning(struct printbuf *out, const char *action)
202 {
203         unsigned len = strlen(action);
204 
205         BUG_ON(action[len - 1] != '?');
206         --len;
207 
208         if (action[len - 1] == 'e')
209                 --len;
210 
211         prt_bytes(out, action, len);
212         prt_str(out, "ing");
213 }
214 
215 static const u8 fsck_flags_extra[] = {
216 #define x(t, n, flags)          [BCH_FSCK_ERR_##t] = flags,
217         BCH_SB_ERRS()
218 #undef x
219 };
220 
221 int __bch2_fsck_err(struct bch_fs *c,
222                   struct btree_trans *trans,
223                   enum bch_fsck_flags flags,
224                   enum bch_sb_error_id err,
225                   const char *fmt, ...)
226 {
227         struct fsck_err_state *s = NULL;
228         va_list args;
229         bool print = true, suppressing = false, inconsistent = false;
230         struct printbuf buf = PRINTBUF, *out = &buf;
231         int ret = -BCH_ERR_fsck_ignore;
232         const char *action_orig = "fix?", *action = action_orig;
233 
234         might_sleep();
235 
236         if (!WARN_ON(err >= ARRAY_SIZE(fsck_flags_extra)))
237                 flags |= fsck_flags_extra[err];
238 
239         if (!c)
240                 c = trans->c;
241 
242         WARN_ON(!trans && bch2_current_has_btree_trans(c));
243 
244         if ((flags & FSCK_CAN_FIX) &&
245             test_bit(err, c->sb.errors_silent))
246                 return -BCH_ERR_fsck_fix;
247 
248         bch2_sb_error_count(c, err);
249 
250         va_start(args, fmt);
251         prt_vprintf(out, fmt, args);
252         va_end(args);
253 
254         /* Custom fix/continue/recreate/etc.? */
255         if (out->buf[out->pos - 1] == '?') {
256                 const char *p = strrchr(out->buf, ',');
257                 if (p) {
258                         out->pos = p - out->buf;
259                         action = kstrdup(p + 2, GFP_KERNEL);
260                         if (!action) {
261                                 ret = -ENOMEM;
262                                 goto err;
263                         }
264                 }
265         }
266 
267         mutex_lock(&c->fsck_error_msgs_lock);
268         s = fsck_err_get(c, fmt);
269         if (s) {
270                 /*
271                  * We may be called multiple times for the same error on
272                  * transaction restart - this memoizes instead of asking the user
273                  * multiple times for the same error:
274                  */
275                 if (s->last_msg && !strcmp(buf.buf, s->last_msg)) {
276                         ret = s->ret;
277                         mutex_unlock(&c->fsck_error_msgs_lock);
278                         goto err;
279                 }
280 
281                 kfree(s->last_msg);
282                 s->last_msg = kstrdup(buf.buf, GFP_KERNEL);
283                 if (!s->last_msg) {
284                         mutex_unlock(&c->fsck_error_msgs_lock);
285                         ret = -ENOMEM;
286                         goto err;
287                 }
288 
289                 if (c->opts.ratelimit_errors &&
290                     !(flags & FSCK_NO_RATELIMIT) &&
291                     s->nr >= FSCK_ERR_RATELIMIT_NR) {
292                         if (s->nr == FSCK_ERR_RATELIMIT_NR)
293                                 suppressing = true;
294                         else
295                                 print = false;
296                 }
297 
298                 s->nr++;
299         }
300 
301 #ifdef BCACHEFS_LOG_PREFIX
302         if (!strncmp(fmt, "bcachefs:", 9))
303                 prt_printf(out, bch2_log_msg(c, ""));
304 #endif
305 
306         if ((flags & FSCK_CAN_FIX) &&
307             (flags & FSCK_AUTOFIX) &&
308             (c->opts.errors == BCH_ON_ERROR_continue ||
309              c->opts.errors == BCH_ON_ERROR_fix_safe)) {
310                 prt_str(out, ", ");
311                 prt_actioning(out, action);
312                 ret = -BCH_ERR_fsck_fix;
313         } else if (!test_bit(BCH_FS_fsck_running, &c->flags)) {
314                 if (c->opts.errors != BCH_ON_ERROR_continue ||
315                     !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) {
316                         prt_str(out, ", shutting down");
317                         inconsistent = true;
318                         ret = -BCH_ERR_fsck_errors_not_fixed;
319                 } else if (flags & FSCK_CAN_FIX) {
320                         prt_str(out, ", ");
321                         prt_actioning(out, action);
322                         ret = -BCH_ERR_fsck_fix;
323                 } else {
324                         prt_str(out, ", continuing");
325                         ret = -BCH_ERR_fsck_ignore;
326                 }
327         } else if (c->opts.fix_errors == FSCK_FIX_exit) {
328                 prt_str(out, ", exiting");
329                 ret = -BCH_ERR_fsck_errors_not_fixed;
330         } else if (flags & FSCK_CAN_FIX) {
331                 int fix = s && s->fix
332                         ? s->fix
333                         : c->opts.fix_errors;
334 
335                 if (fix == FSCK_FIX_ask) {
336                         prt_str(out, ", ");
337                         prt_str(out, action);
338 
339                         if (bch2_fs_stdio_redirect(c))
340                                 bch2_print(c, "%s", out->buf);
341                         else
342                                 bch2_print_string_as_lines(KERN_ERR, out->buf);
343                         print = false;
344 
345                         int ask = bch2_fsck_ask_yn(c, trans);
346 
347                         if (trans) {
348                                 ret = bch2_trans_relock(trans);
349                                 if (ret) {
350                                         mutex_unlock(&c->fsck_error_msgs_lock);
351                                         goto err;
352                                 }
353                         }
354 
355                         if (ask >= YN_ALLNO && s)
356                                 s->fix = ask == YN_ALLNO
357                                         ? FSCK_FIX_no
358                                         : FSCK_FIX_yes;
359 
360                         ret = ask & 1
361                                 ? -BCH_ERR_fsck_fix
362                                 : -BCH_ERR_fsck_ignore;
363                 } else if (fix == FSCK_FIX_yes ||
364                            (c->opts.nochanges &&
365                             !(flags & FSCK_CAN_IGNORE))) {
366                         prt_str(out, ", ");
367                         prt_actioning(out, action);
368                         ret = -BCH_ERR_fsck_fix;
369                 } else {
370                         prt_str(out, ", not ");
371                         prt_actioning(out, action);
372                 }
373         } else if (flags & FSCK_NEED_FSCK) {
374                 prt_str(out, " (run fsck to correct)");
375         } else {
376                 prt_str(out, " (repair unimplemented)");
377         }
378 
379         if (ret == -BCH_ERR_fsck_ignore &&
380             (c->opts.fix_errors == FSCK_FIX_exit ||
381              !(flags & FSCK_CAN_IGNORE)))
382                 ret = -BCH_ERR_fsck_errors_not_fixed;
383 
384         if (print) {
385                 if (bch2_fs_stdio_redirect(c))
386                         bch2_print(c, "%s\n", out->buf);
387                 else
388                         bch2_print_string_as_lines(KERN_ERR, out->buf);
389         }
390 
391         if (test_bit(BCH_FS_fsck_running, &c->flags) &&
392             (ret != -BCH_ERR_fsck_fix &&
393              ret != -BCH_ERR_fsck_ignore))
394                 bch_err(c, "Unable to continue, halting");
395         else if (suppressing)
396                 bch_err(c, "Ratelimiting new instances of previous error");
397 
398         if (s)
399                 s->ret = ret;
400 
401         mutex_unlock(&c->fsck_error_msgs_lock);
402 
403         if (inconsistent)
404                 bch2_inconsistent_error(c);
405 
406         if (ret == -BCH_ERR_fsck_fix) {
407                 set_bit(BCH_FS_errors_fixed, &c->flags);
408         } else {
409                 set_bit(BCH_FS_errors_not_fixed, &c->flags);
410                 set_bit(BCH_FS_error, &c->flags);
411         }
412 err:
413         if (action != action_orig)
414                 kfree(action);
415         printbuf_exit(&buf);
416         return ret;
417 }
418 
419 int __bch2_bkey_fsck_err(struct bch_fs *c,
420                          struct bkey_s_c k,
421                          enum bch_fsck_flags flags,
422                          enum bch_sb_error_id err,
423                          const char *fmt, ...)
424 {
425         struct printbuf buf = PRINTBUF;
426         va_list args;
427 
428         prt_str(&buf, "invalid bkey ");
429         bch2_bkey_val_to_text(&buf, c, k);
430         prt_str(&buf, "\n  ");
431         va_start(args, fmt);
432         prt_vprintf(&buf, fmt, args);
433         va_end(args);
434         prt_str(&buf, ": delete?");
435 
436         int ret = __bch2_fsck_err(c, NULL, flags, err, "%s", buf.buf);
437         printbuf_exit(&buf);
438         return ret;
439 }
440 
441 void bch2_flush_fsck_errs(struct bch_fs *c)
442 {
443         struct fsck_err_state *s, *n;
444 
445         mutex_lock(&c->fsck_error_msgs_lock);
446 
447         list_for_each_entry_safe(s, n, &c->fsck_error_msgs, list) {
448                 if (s->ratelimited && s->last_msg)
449                         bch_err(c, "Saw %llu errors like:\n    %s", s->nr, s->last_msg);
450 
451                 list_del(&s->list);
452                 kfree(s->last_msg);
453                 kfree(s);
454         }
455 
456         mutex_unlock(&c->fsck_error_msgs_lock);
457 }
458 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php