diff --git a/fs/bcachefs/error.c b/fs/bcachefs/error.c
index c2882c599896cc694c9e0180bf4abf4e90f05e0a..1dae649ff0e223386eefe0e1992ddd564f020197 100644
--- a/fs/bcachefs/error.c
+++ b/fs/bcachefs/error.c
@@ -98,7 +98,6 @@ static struct fsck_err_state *fsck_err_get(struct bch_fs *c, const char *fmt)
 
 	INIT_LIST_HEAD(&s->list);
 	s->fmt = fmt;
-	s->buf = PRINTBUF;
 	list_add(&s->list, &c->fsck_errors);
 	return s;
 }
@@ -111,9 +110,23 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
 	struct printbuf buf = PRINTBUF, *out = &buf;
 	int ret = -BCH_ERR_fsck_ignore;
 
+	va_start(args, fmt);
+	prt_vprintf(out, fmt, args);
+	va_end(args);
+
 	mutex_lock(&c->fsck_error_lock);
 	s = fsck_err_get(c, fmt);
 	if (s) {
+		if (s->last_msg && !strcmp(buf.buf, s->last_msg)) {
+			ret = s->ret;
+			mutex_unlock(&c->fsck_error_lock);
+			printbuf_exit(&buf);
+			return ret;
+		}
+
+		kfree(s->last_msg);
+		s->last_msg = kstrdup(buf.buf, GFP_KERNEL);
+
 		if (c->opts.ratelimit_errors &&
 		    !(flags & FSCK_NO_RATELIMIT) &&
 		    s->nr >= FSCK_ERR_RATELIMIT_NR) {
@@ -123,8 +136,6 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
 				print = false;
 		}
 
-		printbuf_reset(&s->buf);
-		out = &s->buf;
 		s->nr++;
 	}
 
@@ -133,10 +144,6 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
 		prt_printf(out, bch2_log_msg(c, ""));
 #endif
 
-	va_start(args, fmt);
-	prt_vprintf(out, fmt, args);
-	va_end(args);
-
 	if (test_bit(BCH_FS_FSCK_DONE, &c->flags)) {
 		if (c->opts.errors != BCH_ON_ERROR_continue ||
 		    !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) {
@@ -190,6 +197,9 @@ int bch2_fsck_err(struct bch_fs *c, unsigned flags, const char *fmt, ...)
 	else if (suppressing)
 		bch_err(c, "Ratelimiting new instances of previous error");
 
+	if (s)
+		s->ret = ret;
+
 	mutex_unlock(&c->fsck_error_lock);
 
 	printbuf_exit(&buf);
@@ -214,11 +224,11 @@ void bch2_flush_fsck_errs(struct bch_fs *c)
 	mutex_lock(&c->fsck_error_lock);
 
 	list_for_each_entry_safe(s, n, &c->fsck_errors, list) {
-		if (s->ratelimited)
-			bch_err(c, "Saw %llu errors like:\n    %s", s->nr, s->buf.buf);
+		if (s->ratelimited && s->last_msg)
+			bch_err(c, "Saw %llu errors like:\n    %s", s->nr, s->last_msg);
 
 		list_del(&s->list);
-		printbuf_exit(&s->buf);
+		kfree(s->last_msg);
 		kfree(s);
 	}
 
diff --git a/fs/bcachefs/error.h b/fs/bcachefs/error.h
index 9991879dfbff12bb36119646d4222423f09bfd2b..91c7e4ee8f7266ea193414461a145236e7dc5277 100644
--- a/fs/bcachefs/error.h
+++ b/fs/bcachefs/error.h
@@ -103,7 +103,8 @@ struct fsck_err_state {
 	const char		*fmt;
 	u64			nr;
 	bool			ratelimited;
-	struct printbuf		buf;
+	int			ret;
+	char			*last_msg;
 };
 
 #define FSCK_CAN_FIX		(1 << 0)