1 // SPDX-License-Identifier: GPL-2.0
2 #include "bcachefs.h"
3 #include "btree_cache.h"
4 #include "btree_iter.h"
5 #include "error.h"
6 #include "journal.h"
7 #include "namei.h"
8 #include "recovery_passes.h"
9 #include "super.h"
10 #include "thread_with_file.h"
11 
12 #define FSCK_ERR_RATELIMIT_NR	10
13 
bch2_log_msg_start(struct bch_fs * c,struct printbuf * out)14 void bch2_log_msg_start(struct bch_fs *c, struct printbuf *out)
15 {
16 	printbuf_indent_add_nextline(out, 2);
17 
18 #ifdef BCACHEFS_LOG_PREFIX
19 	prt_printf(out, bch2_log_msg(c, ""));
20 #endif
21 }
22 
__bch2_inconsistent_error(struct bch_fs * c,struct printbuf * out)23 bool __bch2_inconsistent_error(struct bch_fs *c, struct printbuf *out)
24 {
25 	set_bit(BCH_FS_error, &c->flags);
26 
27 	switch (c->opts.errors) {
28 	case BCH_ON_ERROR_continue:
29 		return false;
30 	case BCH_ON_ERROR_fix_safe:
31 	case BCH_ON_ERROR_ro:
32 		if (bch2_fs_emergency_read_only(c))
33 			prt_printf(out, "inconsistency detected - emergency read only at journal seq %llu\n",
34 				   journal_cur_seq(&c->journal));
35 		return true;
36 	case BCH_ON_ERROR_panic:
37 		bch2_print_string_as_lines_nonblocking(KERN_ERR, out->buf);
38 		panic(bch2_fmt(c, "panic after error"));
39 		return true;
40 	default:
41 		BUG();
42 	}
43 }
44 
bch2_inconsistent_error(struct bch_fs * c)45 bool bch2_inconsistent_error(struct bch_fs *c)
46 {
47 	struct printbuf buf = PRINTBUF;
48 	buf.atomic++;
49 
50 	printbuf_indent_add_nextline(&buf, 2);
51 
52 	bool ret = __bch2_inconsistent_error(c, &buf);
53 	if (ret)
54 		bch_err(c, "%s", buf.buf);
55 	printbuf_exit(&buf);
56 	return ret;
57 }
58 
59 __printf(3, 0)
bch2_fs_trans_inconsistent(struct bch_fs * c,struct btree_trans * trans,const char * fmt,va_list args)60 static bool bch2_fs_trans_inconsistent(struct bch_fs *c, struct btree_trans *trans,
61 				       const char *fmt, va_list args)
62 {
63 	struct printbuf buf = PRINTBUF;
64 	buf.atomic++;
65 
66 	bch2_log_msg_start(c, &buf);
67 
68 	prt_vprintf(&buf, fmt, args);
69 	prt_newline(&buf);
70 
71 	if (trans)
72 		bch2_trans_updates_to_text(&buf, trans);
73 	bool ret = __bch2_inconsistent_error(c, &buf);
74 	bch2_print_string_as_lines_nonblocking(KERN_ERR, buf.buf);
75 
76 	printbuf_exit(&buf);
77 	return ret;
78 }
79 
bch2_fs_inconsistent(struct bch_fs * c,const char * fmt,...)80 bool bch2_fs_inconsistent(struct bch_fs *c, const char *fmt, ...)
81 {
82 	va_list args;
83 	va_start(args, fmt);
84 	bool ret = bch2_fs_trans_inconsistent(c, NULL, fmt, args);
85 	va_end(args);
86 	return ret;
87 }
88 
bch2_trans_inconsistent(struct btree_trans * trans,const char * fmt,...)89 bool bch2_trans_inconsistent(struct btree_trans *trans, const char *fmt, ...)
90 {
91 	va_list args;
92 	va_start(args, fmt);
93 	bool ret = bch2_fs_trans_inconsistent(trans->c, trans, fmt, args);
94 	va_end(args);
95 	return ret;
96 }
97 
__bch2_topology_error(struct bch_fs * c,struct printbuf * out)98 int __bch2_topology_error(struct bch_fs *c, struct printbuf *out)
99 {
100 	prt_printf(out, "btree topology error: ");
101 
102 	set_bit(BCH_FS_topology_error, &c->flags);
103 	if (!test_bit(BCH_FS_recovery_running, &c->flags)) {
104 		__bch2_inconsistent_error(c, out);
105 		return -BCH_ERR_btree_need_topology_repair;
106 	} else {
107 		return bch2_run_explicit_recovery_pass(c, BCH_RECOVERY_PASS_check_topology) ?:
108 			-BCH_ERR_btree_node_read_validate_error;
109 	}
110 }
111 
bch2_fs_topology_error(struct bch_fs * c,const char * fmt,...)112 int bch2_fs_topology_error(struct bch_fs *c, const char *fmt, ...)
113 {
114 	struct printbuf buf = PRINTBUF;
115 
116 	bch2_log_msg_start(c, &buf);
117 
118 	va_list args;
119 	va_start(args, fmt);
120 	prt_vprintf(&buf, fmt, args);
121 	va_end(args);
122 
123 	int ret = __bch2_topology_error(c, &buf);
124 	bch2_print_string_as_lines(KERN_ERR, buf.buf);
125 
126 	printbuf_exit(&buf);
127 	return ret;
128 }
129 
bch2_fatal_error(struct bch_fs * c)130 void bch2_fatal_error(struct bch_fs *c)
131 {
132 	if (bch2_fs_emergency_read_only(c))
133 		bch_err(c, "fatal error - emergency read only");
134 }
135 
bch2_io_error_work(struct work_struct * work)136 void bch2_io_error_work(struct work_struct *work)
137 {
138 	struct bch_dev *ca = container_of(work, struct bch_dev, io_error_work);
139 	struct bch_fs *c = ca->fs;
140 
141 	/* XXX: if it's reads or checksums that are failing, set it to failed */
142 
143 	down_write(&c->state_lock);
144 	unsigned long write_errors_start = READ_ONCE(ca->write_errors_start);
145 
146 	if (write_errors_start &&
147 	    time_after(jiffies,
148 		       write_errors_start + c->opts.write_error_timeout * HZ)) {
149 		if (ca->mi.state >= BCH_MEMBER_STATE_ro)
150 			goto out;
151 
152 		bool dev = !__bch2_dev_set_state(c, ca, BCH_MEMBER_STATE_ro,
153 						 BCH_FORCE_IF_DEGRADED);
154 
155 		bch_err(ca,
156 			"writes erroring for %u seconds, setting %s ro",
157 			c->opts.write_error_timeout,
158 			dev ? "device" : "filesystem");
159 		if (!dev)
160 			bch2_fs_emergency_read_only(c);
161 
162 	}
163 out:
164 	up_write(&c->state_lock);
165 }
166 
bch2_io_error(struct bch_dev * ca,enum bch_member_error_type type)167 void bch2_io_error(struct bch_dev *ca, enum bch_member_error_type type)
168 {
169 	atomic64_inc(&ca->errors[type]);
170 
171 	if (type == BCH_MEMBER_ERROR_write && !ca->write_errors_start)
172 		ca->write_errors_start = jiffies;
173 
174 	queue_work(system_long_wq, &ca->io_error_work);
175 }
176 
177 enum ask_yn {
178 	YN_NO,
179 	YN_YES,
180 	YN_ALLNO,
181 	YN_ALLYES,
182 };
183 
parse_yn_response(char * buf)184 static enum ask_yn parse_yn_response(char *buf)
185 {
186 	buf = strim(buf);
187 
188 	if (strlen(buf) == 1)
189 		switch (buf[0]) {
190 		case 'n':
191 			return YN_NO;
192 		case 'y':
193 			return YN_YES;
194 		case 'N':
195 			return YN_ALLNO;
196 		case 'Y':
197 			return YN_ALLYES;
198 		}
199 	return -1;
200 }
201 
202 #ifdef __KERNEL__
bch2_fsck_ask_yn(struct bch_fs * c,struct btree_trans * trans)203 static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c, struct btree_trans *trans)
204 {
205 	struct stdio_redirect *stdio = c->stdio;
206 
207 	if (c->stdio_filter && c->stdio_filter != current)
208 		stdio = NULL;
209 
210 	if (!stdio)
211 		return YN_NO;
212 
213 	if (trans)
214 		bch2_trans_unlock(trans);
215 
216 	unsigned long unlock_long_at = trans ? jiffies + HZ * 2 : 0;
217 	darray_char line = {};
218 	int ret;
219 
220 	do {
221 		unsigned long t;
222 		bch2_print(c, " (y,n, or Y,N for all errors of this type) ");
223 rewait:
224 		t = unlock_long_at
225 			? max_t(long, unlock_long_at - jiffies, 0)
226 			: MAX_SCHEDULE_TIMEOUT;
227 
228 		int r = bch2_stdio_redirect_readline_timeout(stdio, &line, t);
229 		if (r == -ETIME) {
230 			bch2_trans_unlock_long(trans);
231 			unlock_long_at = 0;
232 			goto rewait;
233 		}
234 
235 		if (r < 0) {
236 			ret = YN_NO;
237 			break;
238 		}
239 
240 		darray_last(line) = '\0';
241 	} while ((ret = parse_yn_response(line.data)) < 0);
242 
243 	darray_exit(&line);
244 	return ret;
245 }
246 #else
247 
248 #include "tools-util.h"
249 
bch2_fsck_ask_yn(struct bch_fs * c,struct btree_trans * trans)250 static enum ask_yn bch2_fsck_ask_yn(struct bch_fs *c, struct btree_trans *trans)
251 {
252 	char *buf = NULL;
253 	size_t buflen = 0;
254 	int ret;
255 
256 	do {
257 		fputs(" (y,n, or Y,N for all errors of this type) ", stdout);
258 		fflush(stdout);
259 
260 		if (getline(&buf, &buflen, stdin) < 0)
261 			die("error reading from standard input");
262 	} while ((ret = parse_yn_response(buf)) < 0);
263 
264 	free(buf);
265 	return ret;
266 }
267 
268 #endif
269 
fsck_err_get(struct bch_fs * c,enum bch_sb_error_id id)270 static struct fsck_err_state *fsck_err_get(struct bch_fs *c,
271 					   enum bch_sb_error_id id)
272 {
273 	struct fsck_err_state *s;
274 
275 	list_for_each_entry(s, &c->fsck_error_msgs, list)
276 		if (s->id == id) {
277 			/*
278 			 * move it to the head of the list: repeated fsck errors
279 			 * are common
280 			 */
281 			list_move(&s->list, &c->fsck_error_msgs);
282 			return s;
283 		}
284 
285 	s = kzalloc(sizeof(*s), GFP_NOFS);
286 	if (!s) {
287 		if (!c->fsck_alloc_msgs_err)
288 			bch_err(c, "kmalloc err, cannot ratelimit fsck errs");
289 		c->fsck_alloc_msgs_err = true;
290 		return NULL;
291 	}
292 
293 	INIT_LIST_HEAD(&s->list);
294 	s->id = id;
295 	list_add(&s->list, &c->fsck_error_msgs);
296 	return s;
297 }
298 
299 /* s/fix?/fixing/ s/recreate?/recreating/ */
prt_actioning(struct printbuf * out,const char * action)300 static void prt_actioning(struct printbuf *out, const char *action)
301 {
302 	unsigned len = strlen(action);
303 
304 	BUG_ON(action[len - 1] != '?');
305 	--len;
306 
307 	if (action[len - 1] == 'e')
308 		--len;
309 
310 	prt_bytes(out, action, len);
311 	prt_str(out, "ing");
312 }
313 
314 static const u8 fsck_flags_extra[] = {
315 #define x(t, n, flags)		[BCH_FSCK_ERR_##t] = flags,
316 	BCH_SB_ERRS()
317 #undef x
318 };
319 
do_fsck_ask_yn(struct bch_fs * c,struct btree_trans * trans,struct printbuf * question,const char * action)320 static int do_fsck_ask_yn(struct bch_fs *c,
321 			  struct btree_trans *trans,
322 			  struct printbuf *question,
323 			  const char *action)
324 {
325 	prt_str(question, ", ");
326 	prt_str(question, action);
327 
328 	if (bch2_fs_stdio_redirect(c))
329 		bch2_print(c, "%s", question->buf);
330 	else
331 		bch2_print_string_as_lines(KERN_ERR, question->buf);
332 
333 	int ask = bch2_fsck_ask_yn(c, trans);
334 
335 	if (trans) {
336 		int ret = bch2_trans_relock(trans);
337 		if (ret)
338 			return ret;
339 	}
340 
341 	return ask;
342 }
343 
count_fsck_err_locked(struct bch_fs * c,enum bch_sb_error_id id,const char * msg,bool * repeat,bool * print,bool * suppress)344 static struct fsck_err_state *count_fsck_err_locked(struct bch_fs *c,
345 			  enum bch_sb_error_id id, const char *msg,
346 			  bool *repeat, bool *print, bool *suppress)
347 {
348 	bch2_sb_error_count(c, id);
349 
350 	struct fsck_err_state *s = fsck_err_get(c, id);
351 	if (s) {
352 		/*
353 		 * We may be called multiple times for the same error on
354 		 * transaction restart - this memoizes instead of asking the user
355 		 * multiple times for the same error:
356 		 */
357 		if (s->last_msg && !strcmp(msg, s->last_msg)) {
358 			*repeat = true;
359 			*print = false;
360 			return s;
361 		}
362 
363 		kfree(s->last_msg);
364 		s->last_msg = kstrdup(msg, GFP_KERNEL);
365 
366 		if (c->opts.ratelimit_errors &&
367 		    s->nr >= FSCK_ERR_RATELIMIT_NR) {
368 			if (s->nr == FSCK_ERR_RATELIMIT_NR)
369 				*suppress = true;
370 			else
371 				*print = false;
372 		}
373 
374 		s->nr++;
375 	}
376 	return s;
377 }
378 
__bch2_count_fsck_err(struct bch_fs * c,enum bch_sb_error_id id,const char * msg,bool * repeat,bool * print,bool * suppress)379 void __bch2_count_fsck_err(struct bch_fs *c,
380 			   enum bch_sb_error_id id, const char *msg,
381 			   bool *repeat, bool *print, bool *suppress)
382 {
383 	bch2_sb_error_count(c, id);
384 
385 	mutex_lock(&c->fsck_error_msgs_lock);
386 	count_fsck_err_locked(c, id, msg, repeat, print, suppress);
387 	mutex_unlock(&c->fsck_error_msgs_lock);
388 }
389 
__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,...)390 int __bch2_fsck_err(struct bch_fs *c,
391 		  struct btree_trans *trans,
392 		  enum bch_fsck_flags flags,
393 		  enum bch_sb_error_id err,
394 		  const char *fmt, ...)
395 {
396 	va_list args;
397 	struct printbuf buf = PRINTBUF, *out = &buf;
398 	int ret = -BCH_ERR_fsck_ignore;
399 	const char *action_orig = "fix?", *action = action_orig;
400 
401 	might_sleep();
402 
403 	if (!WARN_ON(err >= ARRAY_SIZE(fsck_flags_extra)))
404 		flags |= fsck_flags_extra[err];
405 
406 	if (!c)
407 		c = trans->c;
408 
409 	/*
410 	 * Ugly: if there's a transaction in the current task it has to be
411 	 * passed in to unlock if we prompt for user input.
412 	 *
413 	 * But, plumbing a transaction and transaction restarts into
414 	 * bkey_validate() is problematic.
415 	 *
416 	 * So:
417 	 * - make all bkey errors AUTOFIX, they're simple anyways (we just
418 	 *   delete the key)
419 	 * - and we don't need to warn if we're not prompting
420 	 */
421 	WARN_ON((flags & FSCK_CAN_FIX) &&
422 		!(flags & FSCK_AUTOFIX) &&
423 		!trans &&
424 		bch2_current_has_btree_trans(c));
425 
426 	if (test_bit(err, c->sb.errors_silent))
427 		return flags & FSCK_CAN_FIX
428 			? -BCH_ERR_fsck_fix
429 			: -BCH_ERR_fsck_ignore;
430 
431 	printbuf_indent_add_nextline(out, 2);
432 
433 #ifdef BCACHEFS_LOG_PREFIX
434 	if (strncmp(fmt, "bcachefs", 8))
435 		prt_printf(out, bch2_log_msg(c, ""));
436 #endif
437 
438 	va_start(args, fmt);
439 	prt_vprintf(out, fmt, args);
440 	va_end(args);
441 
442 	/* Custom fix/continue/recreate/etc.? */
443 	if (out->buf[out->pos - 1] == '?') {
444 		const char *p = strrchr(out->buf, ',');
445 		if (p) {
446 			out->pos = p - out->buf;
447 			action = kstrdup(p + 2, GFP_KERNEL);
448 			if (!action) {
449 				ret = -ENOMEM;
450 				goto err;
451 			}
452 		}
453 	}
454 
455 	mutex_lock(&c->fsck_error_msgs_lock);
456 	bool repeat = false, print = true, suppress = false;
457 	bool inconsistent = false, exiting = false;
458 	struct fsck_err_state *s =
459 		count_fsck_err_locked(c, err, buf.buf, &repeat, &print, &suppress);
460 	if (repeat) {
461 		ret = s->ret;
462 		goto err_unlock;
463 	}
464 
465 	if ((flags & FSCK_AUTOFIX) &&
466 	    (c->opts.errors == BCH_ON_ERROR_continue ||
467 	     c->opts.errors == BCH_ON_ERROR_fix_safe)) {
468 		prt_str(out, ", ");
469 		if (flags & FSCK_CAN_FIX) {
470 			prt_actioning(out, action);
471 			ret = -BCH_ERR_fsck_fix;
472 		} else {
473 			prt_str(out, ", continuing");
474 			ret = -BCH_ERR_fsck_ignore;
475 		}
476 
477 		goto print;
478 	} else if (!test_bit(BCH_FS_fsck_running, &c->flags)) {
479 		if (c->opts.errors != BCH_ON_ERROR_continue ||
480 		    !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) {
481 			prt_str_indented(out, ", shutting down\n"
482 					 "error not marked as autofix and not in fsck\n"
483 					 "run fsck, and forward to devs so error can be marked for self-healing");
484 			inconsistent = true;
485 			print = true;
486 			ret = -BCH_ERR_fsck_errors_not_fixed;
487 		} else if (flags & FSCK_CAN_FIX) {
488 			prt_str(out, ", ");
489 			prt_actioning(out, action);
490 			ret = -BCH_ERR_fsck_fix;
491 		} else {
492 			prt_str(out, ", continuing");
493 			ret = -BCH_ERR_fsck_ignore;
494 		}
495 	} else if (c->opts.fix_errors == FSCK_FIX_exit) {
496 		prt_str(out, ", exiting");
497 		ret = -BCH_ERR_fsck_errors_not_fixed;
498 	} else if (flags & FSCK_CAN_FIX) {
499 		int fix = s && s->fix
500 			? s->fix
501 			: c->opts.fix_errors;
502 
503 		if (fix == FSCK_FIX_ask) {
504 			print = false;
505 
506 			ret = do_fsck_ask_yn(c, trans, out, action);
507 			if (ret < 0)
508 				goto err_unlock;
509 
510 			if (ret >= YN_ALLNO && s)
511 				s->fix = ret == YN_ALLNO
512 					? FSCK_FIX_no
513 					: FSCK_FIX_yes;
514 
515 			ret = ret & 1
516 				? -BCH_ERR_fsck_fix
517 				: -BCH_ERR_fsck_ignore;
518 		} else if (fix == FSCK_FIX_yes ||
519 			   (c->opts.nochanges &&
520 			    !(flags & FSCK_CAN_IGNORE))) {
521 			prt_str(out, ", ");
522 			prt_actioning(out, action);
523 			ret = -BCH_ERR_fsck_fix;
524 		} else {
525 			prt_str(out, ", not ");
526 			prt_actioning(out, action);
527 		}
528 	} else if (!(flags & FSCK_CAN_IGNORE)) {
529 		prt_str(out, " (repair unimplemented)");
530 	}
531 
532 	if (ret == -BCH_ERR_fsck_ignore &&
533 	    (c->opts.fix_errors == FSCK_FIX_exit ||
534 	     !(flags & FSCK_CAN_IGNORE)))
535 		ret = -BCH_ERR_fsck_errors_not_fixed;
536 
537 	if (test_bit(BCH_FS_fsck_running, &c->flags) &&
538 	    (ret != -BCH_ERR_fsck_fix &&
539 	     ret != -BCH_ERR_fsck_ignore)) {
540 		exiting = true;
541 		print = true;
542 	}
543 print:
544 	prt_newline(out);
545 
546 	if (inconsistent)
547 		__bch2_inconsistent_error(c, out);
548 	else if (exiting)
549 		prt_printf(out, "Unable to continue, halting\n");
550 	else if (suppress)
551 		prt_printf(out, "Ratelimiting new instances of previous error\n");
552 
553 	if (print) {
554 		/* possibly strip an empty line, from printbuf_indent_add */
555 		while (out->pos && out->buf[out->pos - 1] == ' ')
556 			--out->pos;
557 		printbuf_nul_terminate(out);
558 
559 		if (bch2_fs_stdio_redirect(c))
560 			bch2_print(c, "%s", out->buf);
561 		else
562 			bch2_print_string_as_lines(KERN_ERR, out->buf);
563 	}
564 
565 	if (s)
566 		s->ret = ret;
567 
568 	/*
569 	 * We don't yet track whether the filesystem currently has errors, for
570 	 * log_fsck_err()s: that would require us to track for every error type
571 	 * which recovery pass corrects it, to get the fsck exit status correct:
572 	 */
573 	if (flags & FSCK_CAN_FIX) {
574 		if (ret == -BCH_ERR_fsck_fix) {
575 			set_bit(BCH_FS_errors_fixed, &c->flags);
576 		} else {
577 			set_bit(BCH_FS_errors_not_fixed, &c->flags);
578 			set_bit(BCH_FS_error, &c->flags);
579 		}
580 	}
581 err_unlock:
582 	mutex_unlock(&c->fsck_error_msgs_lock);
583 err:
584 	if (action != action_orig)
585 		kfree(action);
586 	printbuf_exit(&buf);
587 	return ret;
588 }
589 
590 static const char * const bch2_bkey_validate_contexts[] = {
591 #define x(n) #n,
592 	BKEY_VALIDATE_CONTEXTS()
593 #undef x
594 	NULL
595 };
596 
__bch2_bkey_fsck_err(struct bch_fs * c,struct bkey_s_c k,struct bkey_validate_context from,enum bch_sb_error_id err,const char * fmt,...)597 int __bch2_bkey_fsck_err(struct bch_fs *c,
598 			 struct bkey_s_c k,
599 			 struct bkey_validate_context from,
600 			 enum bch_sb_error_id err,
601 			 const char *fmt, ...)
602 {
603 	if (from.flags & BCH_VALIDATE_silent)
604 		return -BCH_ERR_fsck_delete_bkey;
605 
606 	unsigned fsck_flags = 0;
607 	if (!(from.flags & (BCH_VALIDATE_write|BCH_VALIDATE_commit))) {
608 		if (test_bit(err, c->sb.errors_silent))
609 			return -BCH_ERR_fsck_delete_bkey;
610 
611 		fsck_flags |= FSCK_AUTOFIX|FSCK_CAN_FIX;
612 	}
613 	if (!WARN_ON(err >= ARRAY_SIZE(fsck_flags_extra)))
614 		fsck_flags |= fsck_flags_extra[err];
615 
616 	struct printbuf buf = PRINTBUF;
617 	prt_printf(&buf, "invalid bkey in %s",
618 		   bch2_bkey_validate_contexts[from.from]);
619 
620 	if (from.from == BKEY_VALIDATE_journal)
621 		prt_printf(&buf, " journal seq=%llu offset=%u",
622 			   from.journal_seq, from.journal_offset);
623 
624 	prt_str(&buf, " btree=");
625 	bch2_btree_id_to_text(&buf, from.btree);
626 	prt_printf(&buf, " level=%u: ", from.level);
627 
628 	bch2_bkey_val_to_text(&buf, c, k);
629 	prt_newline(&buf);
630 
631 	va_list args;
632 	va_start(args, fmt);
633 	prt_vprintf(&buf, fmt, args);
634 	va_end(args);
635 
636 	int ret = __bch2_fsck_err(c, NULL, fsck_flags, err, "%s, delete?", buf.buf);
637 	printbuf_exit(&buf);
638 	return ret;
639 }
640 
__bch2_flush_fsck_errs(struct bch_fs * c,bool print)641 static void __bch2_flush_fsck_errs(struct bch_fs *c, bool print)
642 {
643 	struct fsck_err_state *s, *n;
644 
645 	mutex_lock(&c->fsck_error_msgs_lock);
646 
647 	list_for_each_entry_safe(s, n, &c->fsck_error_msgs, list) {
648 		if (print && s->ratelimited && s->last_msg)
649 			bch_err(c, "Saw %llu errors like:\n  %s", s->nr, s->last_msg);
650 
651 		list_del(&s->list);
652 		kfree(s->last_msg);
653 		kfree(s);
654 	}
655 
656 	mutex_unlock(&c->fsck_error_msgs_lock);
657 }
658 
bch2_flush_fsck_errs(struct bch_fs * c)659 void bch2_flush_fsck_errs(struct bch_fs *c)
660 {
661 	__bch2_flush_fsck_errs(c, true);
662 }
663 
bch2_free_fsck_errs(struct bch_fs * c)664 void bch2_free_fsck_errs(struct bch_fs *c)
665 {
666 	__bch2_flush_fsck_errs(c, false);
667 }
668 
bch2_inum_offset_err_msg_trans(struct btree_trans * trans,struct printbuf * out,subvol_inum inum,u64 offset)669 int bch2_inum_offset_err_msg_trans(struct btree_trans *trans, struct printbuf *out,
670 				    subvol_inum inum, u64 offset)
671 {
672 	u32 restart_count = trans->restart_count;
673 	int ret = 0;
674 
675 	if (inum.subvol) {
676 		ret = bch2_inum_to_path(trans, inum, out);
677 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
678 			return ret;
679 	}
680 	if (!inum.subvol || ret)
681 		prt_printf(out, "inum %llu:%llu", inum.subvol, inum.inum);
682 	prt_printf(out, " offset %llu: ", offset);
683 
684 	return trans_was_restarted(trans, restart_count);
685 }
686 
bch2_inum_offset_err_msg(struct bch_fs * c,struct printbuf * out,subvol_inum inum,u64 offset)687 void bch2_inum_offset_err_msg(struct bch_fs *c, struct printbuf *out,
688 			      subvol_inum inum, u64 offset)
689 {
690 	bch2_trans_do(c, bch2_inum_offset_err_msg_trans(trans, out, inum, offset));
691 }
692 
bch2_inum_snap_offset_err_msg_trans(struct btree_trans * trans,struct printbuf * out,struct bpos pos)693 int bch2_inum_snap_offset_err_msg_trans(struct btree_trans *trans, struct printbuf *out,
694 					struct bpos pos)
695 {
696 	struct bch_fs *c = trans->c;
697 	int ret = 0;
698 
699 	if (!bch2_snapshot_is_leaf(c, pos.snapshot))
700 		prt_str(out, "(multiple snapshots) ");
701 
702 	subvol_inum inum = {
703 		.subvol	= bch2_snapshot_tree_oldest_subvol(c, pos.snapshot),
704 		.inum	= pos.inode,
705 	};
706 
707 	if (inum.subvol) {
708 		ret = bch2_inum_to_path(trans, inum, out);
709 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
710 			return ret;
711 	}
712 
713 	if (!inum.subvol || ret)
714 		prt_printf(out, "inum %llu:%u", pos.inode, pos.snapshot);
715 
716 	prt_printf(out, " offset %llu: ", pos.offset << 8);
717 	return 0;
718 }
719 
bch2_inum_snap_offset_err_msg(struct bch_fs * c,struct printbuf * out,struct bpos pos)720 void bch2_inum_snap_offset_err_msg(struct bch_fs *c, struct printbuf *out,
721 				  struct bpos pos)
722 {
723 	bch2_trans_do(c, bch2_inum_snap_offset_err_msg_trans(trans, out, pos));
724 }
725