1 // SPDX-License-Identifier: GPL-2.0 2 3 #include "fs.h" 4 #include "messages.h" 5 #include "discard.h" 6 #include "super.h" 7 8 #ifdef CONFIG_PRINTK 9 10 #define STATE_STRING_PREFACE " state " 11 #define STATE_STRING_BUF_LEN (sizeof(STATE_STRING_PREFACE) + BTRFS_FS_STATE_COUNT + 1) 12 13 /* 14 * Characters to print to indicate error conditions or uncommon filesystem state. 15 * RO is not an error. 16 * Don't use 'E' because that's used to signal that there's an error stored in 17 * fs_info->fs_error (see btrfs_state_to_string() below). 18 */ 19 static const char fs_state_chars[] = { 20 [BTRFS_FS_STATE_REMOUNTING] = 'M', 21 [BTRFS_FS_STATE_RO] = 0, 22 [BTRFS_FS_STATE_TRANS_ABORTED] = 'A', 23 [BTRFS_FS_STATE_LOG_REPLAY_ABORTED] = 'O', 24 [BTRFS_FS_STATE_DEV_REPLACING] = 'R', 25 [BTRFS_FS_STATE_DUMMY_FS_INFO] = 0, 26 [BTRFS_FS_STATE_NO_DATA_CSUMS] = 'C', 27 [BTRFS_FS_STATE_SKIP_META_CSUMS] = 'S', 28 [BTRFS_FS_STATE_LOG_CLEANUP_ERROR] = 'L', 29 [BTRFS_FS_STATE_NO_DELAYED_IPUT] = 'I', 30 [BTRFS_FS_STATE_EMERGENCY_SHUTDOWN] = 'H', 31 }; 32 33 static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf) 34 { 35 unsigned int bit; 36 unsigned long fs_state = READ_ONCE(info->fs_state); 37 char *curr = buf; 38 39 if (likely(BTRFS_FS_ERROR(info) == 0 && fs_state == 0)) { 40 *curr = 0; 41 return; 42 } 43 44 memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE)); 45 curr += sizeof(STATE_STRING_PREFACE) - 1; 46 47 if (BTRFS_FS_ERROR(info)) 48 *curr++ = 'E'; 49 50 for_each_set_bit(bit, &fs_state, BTRFS_FS_STATE_COUNT) { 51 if (fs_state_chars[bit]) 52 *curr++ = fs_state_chars[bit]; 53 } 54 55 /* If nothing printed, ensure we return an empty string. */ 56 if (curr == (buf + sizeof(STATE_STRING_PREFACE) - 1)) 57 curr = buf; 58 59 *curr = 0; 60 } 61 #endif 62 63 /* 64 * Generally the error codes correspond to their respective errors, but there 65 * are a few special cases. 66 * 67 * EUCLEAN: Any sort of corruption that we encounter. The tree-checker for 68 * instance will return EUCLEAN if any of the blocks are corrupted in 69 * a way that is problematic. We want to reserve EUCLEAN for these 70 * sort of corruptions. 71 * 72 * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we 73 * need to use EROFS for this case. We will have no idea of the 74 * original failure, that will have been reported at the time we tripped 75 * over the error. Each subsequent error that doesn't have any context 76 * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR. 77 */ 78 const char * __attribute_const__ btrfs_decode_error(int error) 79 { 80 char *errstr = "unknown"; 81 82 switch (error) { 83 case -ENOENT: /* -2 */ 84 errstr = "No such entry"; 85 break; 86 case -EIO: /* -5 */ 87 errstr = "IO failure"; 88 break; 89 case -ENOMEM: /* -12*/ 90 errstr = "Out of memory"; 91 break; 92 case -EEXIST: /* -17 */ 93 errstr = "Object already exists"; 94 break; 95 case -ENOSPC: /* -28 */ 96 errstr = "No space left"; 97 break; 98 case -EROFS: /* -30 */ 99 errstr = "Readonly filesystem"; 100 break; 101 case -EOPNOTSUPP: /* -95 */ 102 errstr = "Operation not supported"; 103 break; 104 case -EUCLEAN: /* -117 */ 105 errstr = "Filesystem corrupted"; 106 break; 107 case -EDQUOT: /* -122 */ 108 errstr = "Quota exceeded"; 109 break; 110 } 111 112 return errstr; 113 } 114 115 /* 116 * Decodes expected errors from the caller and invokes the appropriate error 117 * response. 118 */ 119 __cold 120 void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 121 unsigned int line, int error, const char *fmt, ...) 122 { 123 struct super_block *sb = fs_info->sb; 124 #ifdef CONFIG_PRINTK 125 char statestr[STATE_STRING_BUF_LEN]; 126 const char *errstr; 127 #endif 128 129 #ifdef CONFIG_PRINTK_INDEX 130 printk_index_subsys_emit( 131 "BTRFS: error (device %s%s) in %s:%d: errno=%d %s", KERN_CRIT, fmt); 132 #endif 133 134 /* 135 * Special case: if the error is EROFS, and we're already under 136 * SB_RDONLY, then it is safe here. 137 */ 138 if (error == -EROFS && sb_rdonly(sb)) 139 return; 140 141 #ifdef CONFIG_PRINTK 142 errstr = btrfs_decode_error(error); 143 btrfs_state_to_string(fs_info, statestr); 144 if (fmt) { 145 struct va_format vaf; 146 va_list args; 147 148 va_start(args, fmt); 149 vaf.fmt = fmt; 150 vaf.va = &args; 151 152 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n", 153 sb->s_id, statestr, function, line, error, errstr, &vaf); 154 va_end(args); 155 } else { 156 pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n", 157 sb->s_id, statestr, function, line, error, errstr); 158 } 159 #endif 160 161 /* 162 * Today we only save the error info to memory. Long term we'll also 163 * send it down to the disk. 164 */ 165 WRITE_ONCE(fs_info->fs_error, error); 166 167 /* Don't go through full error handling during mount. */ 168 if (!(sb->s_flags & SB_BORN)) 169 return; 170 171 if (sb_rdonly(sb)) 172 return; 173 174 btrfs_discard_stop(fs_info); 175 176 /* Handle error by forcing the filesystem readonly. */ 177 btrfs_set_sb_rdonly(sb); 178 btrfs_info(fs_info, "forced readonly"); 179 /* 180 * Note that a running device replace operation is not canceled here 181 * although there is no way to update the progress. It would add the 182 * risk of a deadlock, therefore the canceling is omitted. The only 183 * penalty is that some I/O remains active until the procedure 184 * completes. The next time when the filesystem is mounted writable 185 * again, the device replace operation continues. 186 */ 187 } 188 189 #ifdef CONFIG_PRINTK 190 static const char * const logtypes[] = { 191 "emergency", 192 "alert", 193 "critical", 194 "error", 195 "warning", 196 "notice", 197 "info", 198 "debug", 199 }; 200 201 /* 202 * Use one ratelimit state per log level so that a flood of less important 203 * messages doesn't cause more important ones to be dropped. 204 */ 205 static struct ratelimit_state printk_limits[] = { 206 RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100), 207 RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100), 208 RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100), 209 RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100), 210 RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100), 211 RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100), 212 RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100), 213 RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100), 214 }; 215 216 __printf(3, 4) __cold 217 void _btrfs_printk(const struct btrfs_fs_info *fs_info, unsigned int level, const char *fmt, ...) 218 { 219 struct va_format vaf; 220 va_list args; 221 const char *type = logtypes[level]; 222 struct ratelimit_state *ratelimit = &printk_limits[level]; 223 224 #ifdef CONFIG_PRINTK_INDEX 225 printk_index_subsys_emit("%sBTRFS %s (device %s): ", NULL, fmt); 226 #endif 227 228 va_start(args, fmt); 229 vaf.fmt = fmt; 230 vaf.va = &args; 231 232 /* Do not ratelimit if CONFIG_BTRFS_DEBUG is enabled. */ 233 if (IS_ENABLED(CONFIG_BTRFS_DEBUG) || __ratelimit(ratelimit)) { 234 if (fs_info) { 235 char statestr[STATE_STRING_BUF_LEN]; 236 237 btrfs_state_to_string(fs_info, statestr); 238 _printk(KERN_SOH "%dBTRFS %s (device %s%s): %pV\n", level, type, 239 fs_info->sb->s_id, statestr, &vaf); 240 } else { 241 _printk(KERN_SOH "%dBTRFS %s: %pV\n", level, type, &vaf); 242 } 243 } 244 245 va_end(args); 246 } 247 #endif 248 249 #if BITS_PER_LONG == 32 250 void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info) 251 { 252 if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) { 253 btrfs_warn(fs_info, "reaching 32bit limit for logical addresses"); 254 btrfs_warn(fs_info, 255 "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT", 256 BTRFS_32BIT_MAX_FILE_SIZE >> 40); 257 btrfs_warn(fs_info, 258 "please consider upgrading to 64bit kernel/hardware"); 259 } 260 } 261 262 void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info) 263 { 264 if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) { 265 btrfs_err(fs_info, "reached 32bit limit for logical addresses"); 266 btrfs_err(fs_info, 267 "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed", 268 BTRFS_32BIT_MAX_FILE_SIZE >> 40); 269 btrfs_err(fs_info, 270 "please consider upgrading to 64bit kernel/hardware"); 271 } 272 } 273 #endif 274 275 /* 276 * Decode unexpected, fatal errors from the caller, issue an alert, and either 277 * panic or BUGs, depending on mount options. 278 */ 279 __cold 280 void __btrfs_panic(const struct btrfs_fs_info *fs_info, const char *function, 281 unsigned int line, int error, const char *fmt, ...) 282 { 283 char *s_id = "<unknown>"; 284 struct va_format vaf = { .fmt = fmt }; 285 va_list args; 286 287 if (fs_info) 288 s_id = fs_info->sb->s_id; 289 290 va_start(args, fmt); 291 vaf.va = &args; 292 293 if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR))) 294 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %pe)\n", 295 s_id, function, line, &vaf, error, ERR_PTR(error)); 296 297 btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %pe)", 298 function, line, &vaf, error, ERR_PTR(error)); 299 va_end(args); 300 /* Caller calls BUG() */ 301 } 302