1 // SPDX-License-Identifier: GPL-2.0 2 #include "bcachefs.h" 3 #include "error.h" 4 #include "super.h" 5 6 #define FSCK_ERR_RATELIMIT_NR 10 7 8 bool bch2_inconsistent_error(struct bch_fs *c) 9 { 10 set_bit(BCH_FS_ERROR, &c->flags); 11 12 switch (c->opts.errors) { 13 case BCH_ON_ERROR_continue: 14 return false; 15 case BCH_ON_ERROR_ro: 16 if (bch2_fs_emergency_read_only(c)) 17 bch_err(c, "inconsistency detected - emergency read only"); 18 return true; 19 case BCH_ON_ERROR_panic: 20 panic(bch2_fmt(c, "panic after error")); 21 return true; 22 default: 23 BUG(); 24 } 25 } 26 27 void bch2_topology_error(struct bch_fs *c) 28 { 29 set_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags); 30 if (test_bit(BCH_FS_FSCK_DONE, &c->flags)) 31 bch2_inconsistent_error(c); 32 } 33 34 void bch2_fatal_error(struct bch_fs *c) 35 { 36 if (bch2_fs_emergency_read_only(c)) 37 bch_err(c, "fatal error - emergency read only"); 38 } 39 40 void bch2_io_error_work(struct work_struct *work) 41 { 42 struct bch_dev *ca = container_of(work, struct bch_dev, io_error_work); 43 struct bch_fs *c = ca->fs; 44 bool dev; 45 46 down_write(&c->state_lock); 47 dev = bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_ro, 48 BCH_FORCE_IF_DEGRADED); 49 if (dev 50 ? __bch2_dev_set_state(c, ca, BCH_MEMBER_STATE_ro, 51 BCH_FORCE_IF_DEGRADED) 52 : bch2_fs_emergency_read_only(c)) 53 bch_err(ca, 54 "too many IO errors, setting %s RO", 55 dev ? "device" : "filesystem"); 56 up_write(&c->state_lock); 57 } 58 59 void bch2_io_error(struct bch_dev *ca, enum bch_member_error_type type) 60 { 61 atomic64_inc(&ca->errors[type]); 62 //queue_work(system_long_wq, &ca->io_error_work); 63 } 64 65 enum ask_yn { 66 YN_NO, 67 YN_YES, 68 YN_ALLNO, 69 YN_ALLYES, 70 }; 71 72 #ifdef __KERNEL__ 73 #define bch2_fsck_ask_yn() YN_NO 74 #else 75 76 #include "tools-util.h" 77 78 enum ask_yn bch2_fsck_ask_yn(void) 79 { 80 char *buf = NULL; 81 size_t buflen = 0; 82 bool ret; 83 84 while (true) { 85 fputs(" (y,n, or Y,N for all errors of this type) ", stdout); 86 fflush(stdout); 87 88 if (getline(&buf, &buflen, stdin) < 0) 89 die("error reading from standard input"); 90 91 strim(buf); 92 if (strlen(buf) != 1) 93 continue; 94 95 switch (buf[0]) { 96 case 'n': 97 return YN_NO; 98 case 'y': 99 return YN_YES; 100 case 'N': 101 return YN_ALLNO; 102 case 'Y': 103 return YN_ALLYES; 104 } 105 } 106 107 free(buf); 108 return ret; 109 } 110 111 #endif 112 113 static struct fsck_err_state *fsck_err_get(struct bch_fs *c, const char *fmt) 114 { 115 struct fsck_err_state *s; 116 117 if (test_bit(BCH_FS_FSCK_DONE, &c->flags)) 118 return NULL; 119 120 list_for_each_entry(s, &c->fsck_error_msgs, list) 121 if (s->fmt == fmt) { 122 /* 123 * move it to the head of the list: repeated fsck errors 124 * are common 125 */ 126 list_move(&s->list, &c->fsck_error_msgs); 127 return s; 128 } 129 130 s = kzalloc(sizeof(*s), GFP_NOFS); 131 if (!s) { 132 if (!c->fsck_alloc_msgs_err) 133 bch_err(c, "kmalloc err, cannot ratelimit fsck errs"); 134 c->fsck_alloc_msgs_err = true; 135 return NULL; 136 } 137 138 INIT_LIST_HEAD(&s->list); 139 s->fmt = fmt; 140 list_add(&s->list, &c->fsck_error_msgs); 141 return s; 142 } 143 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, ...) 148 { 149 struct fsck_err_state *s = NULL; 150 va_list args; 151 bool print = true, suppressing = false, inconsistent = false; 152 struct printbuf buf = PRINTBUF, *out = &buf; 153 int ret = -BCH_ERR_fsck_ignore; 154 155 bch2_sb_error_count(c, err); 156 157 va_start(args, fmt); 158 prt_vprintf(out, fmt, args); 159 va_end(args); 160 161 mutex_lock(&c->fsck_error_msgs_lock); 162 s = fsck_err_get(c, fmt); 163 if (s) { 164 /* 165 * We may be called multiple times for the same error on 166 * transaction restart - this memoizes instead of asking the user 167 * multiple times for the same error: 168 */ 169 if (s->last_msg && !strcmp(buf.buf, s->last_msg)) { 170 ret = s->ret; 171 mutex_unlock(&c->fsck_error_msgs_lock); 172 printbuf_exit(&buf); 173 return ret; 174 } 175 176 kfree(s->last_msg); 177 s->last_msg = kstrdup(buf.buf, GFP_KERNEL); 178 179 if (c->opts.ratelimit_errors && 180 !(flags & FSCK_NO_RATELIMIT) && 181 s->nr >= FSCK_ERR_RATELIMIT_NR) { 182 if (s->nr == FSCK_ERR_RATELIMIT_NR) 183 suppressing = true; 184 else 185 print = false; 186 } 187 188 s->nr++; 189 } 190 191 #ifdef BCACHEFS_LOG_PREFIX 192 if (!strncmp(fmt, "bcachefs:", 9)) 193 prt_printf(out, bch2_log_msg(c, "")); 194 #endif 195 196 if (test_bit(BCH_FS_FSCK_DONE, &c->flags)) { 197 if (c->opts.errors != BCH_ON_ERROR_continue || 198 !(flags & (FSCK_CAN_FIX|FSCK_CAN_IGNORE))) { 199 prt_str(out, ", shutting down"); 200 inconsistent = true; 201 ret = -BCH_ERR_fsck_errors_not_fixed; 202 } else if (flags & FSCK_CAN_FIX) { 203 prt_str(out, ", fixing"); 204 ret = -BCH_ERR_fsck_fix; 205 } else { 206 prt_str(out, ", continuing"); 207 ret = -BCH_ERR_fsck_ignore; 208 } 209 } else if (c->opts.fix_errors == FSCK_FIX_exit) { 210 prt_str(out, ", exiting"); 211 ret = -BCH_ERR_fsck_errors_not_fixed; 212 } else if (flags & FSCK_CAN_FIX) { 213 int fix = s && s->fix 214 ? s->fix 215 : c->opts.fix_errors; 216 217 if (fix == FSCK_FIX_ask) { 218 int ask; 219 220 prt_str(out, ": fix?"); 221 bch2_print_string_as_lines(KERN_ERR, out->buf); 222 print = false; 223 224 ask = bch2_fsck_ask_yn(); 225 226 if (ask >= YN_ALLNO && s) 227 s->fix = ask == YN_ALLNO 228 ? FSCK_FIX_no 229 : FSCK_FIX_yes; 230 231 ret = ask & 1 232 ? -BCH_ERR_fsck_fix 233 : -BCH_ERR_fsck_ignore; 234 } else if (fix == FSCK_FIX_yes || 235 (c->opts.nochanges && 236 !(flags & FSCK_CAN_IGNORE))) { 237 prt_str(out, ", fixing"); 238 ret = -BCH_ERR_fsck_fix; 239 } else { 240 prt_str(out, ", not fixing"); 241 } 242 } else if (flags & FSCK_NEED_FSCK) { 243 prt_str(out, " (run fsck to correct)"); 244 } else { 245 prt_str(out, " (repair unimplemented)"); 246 } 247 248 if (ret == -BCH_ERR_fsck_ignore && 249 (c->opts.fix_errors == FSCK_FIX_exit || 250 !(flags & FSCK_CAN_IGNORE))) 251 ret = -BCH_ERR_fsck_errors_not_fixed; 252 253 if (print) 254 bch2_print_string_as_lines(KERN_ERR, out->buf); 255 256 if (!test_bit(BCH_FS_FSCK_DONE, &c->flags) && 257 (ret != -BCH_ERR_fsck_fix && 258 ret != -BCH_ERR_fsck_ignore)) 259 bch_err(c, "Unable to continue, halting"); 260 else if (suppressing) 261 bch_err(c, "Ratelimiting new instances of previous error"); 262 263 if (s) 264 s->ret = ret; 265 266 mutex_unlock(&c->fsck_error_msgs_lock); 267 268 printbuf_exit(&buf); 269 270 if (inconsistent) 271 bch2_inconsistent_error(c); 272 273 if (ret == -BCH_ERR_fsck_fix) { 274 set_bit(BCH_FS_ERRORS_FIXED, &c->flags); 275 } else { 276 set_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags); 277 set_bit(BCH_FS_ERROR, &c->flags); 278 } 279 280 return ret; 281 } 282 283 void bch2_flush_fsck_errs(struct bch_fs *c) 284 { 285 struct fsck_err_state *s, *n; 286 287 mutex_lock(&c->fsck_error_msgs_lock); 288 289 list_for_each_entry_safe(s, n, &c->fsck_error_msgs, list) { 290 if (s->ratelimited && s->last_msg) 291 bch_err(c, "Saw %llu errors like:\n %s", s->nr, s->last_msg); 292 293 list_del(&s->list); 294 kfree(s->last_msg); 295 kfree(s); 296 } 297 298 mutex_unlock(&c->fsck_error_msgs_lock); 299 } 300