1083bd7e5SJosef Bacik // SPDX-License-Identifier: GPL-2.0 2083bd7e5SJosef Bacik 3083bd7e5SJosef Bacik #include "fs.h" 4083bd7e5SJosef Bacik #include "messages.h" 5083bd7e5SJosef Bacik #include "discard.h" 6083bd7e5SJosef Bacik #include "transaction.h" 7083bd7e5SJosef Bacik #include "space-info.h" 87f0add25SJosef Bacik #include "super.h" 9083bd7e5SJosef Bacik 10083bd7e5SJosef Bacik #ifdef CONFIG_PRINTK 11083bd7e5SJosef Bacik 12083bd7e5SJosef Bacik #define STATE_STRING_PREFACE ": state " 13*ae3364e5SFilipe Manana #define STATE_STRING_BUF_LEN (sizeof(STATE_STRING_PREFACE) + BTRFS_FS_STATE_COUNT + 1) 14083bd7e5SJosef Bacik 15083bd7e5SJosef Bacik /* 16083bd7e5SJosef Bacik * Characters to print to indicate error conditions or uncommon filesystem state. 17083bd7e5SJosef Bacik * RO is not an error. 18083bd7e5SJosef Bacik */ 19083bd7e5SJosef Bacik static const char fs_state_chars[] = { 20083bd7e5SJosef Bacik [BTRFS_FS_STATE_REMOUNTING] = 'M', 21083bd7e5SJosef Bacik [BTRFS_FS_STATE_RO] = 0, 22083bd7e5SJosef Bacik [BTRFS_FS_STATE_TRANS_ABORTED] = 'A', 23083bd7e5SJosef Bacik [BTRFS_FS_STATE_DEV_REPLACING] = 'R', 24083bd7e5SJosef Bacik [BTRFS_FS_STATE_DUMMY_FS_INFO] = 0, 25083bd7e5SJosef Bacik [BTRFS_FS_STATE_NO_CSUMS] = 'C', 26083bd7e5SJosef Bacik [BTRFS_FS_STATE_LOG_CLEANUP_ERROR] = 'L', 27083bd7e5SJosef Bacik }; 28083bd7e5SJosef Bacik 29083bd7e5SJosef Bacik static void btrfs_state_to_string(const struct btrfs_fs_info *info, char *buf) 30083bd7e5SJosef Bacik { 31083bd7e5SJosef Bacik unsigned int bit; 32083bd7e5SJosef Bacik bool states_printed = false; 33083bd7e5SJosef Bacik unsigned long fs_state = READ_ONCE(info->fs_state); 34083bd7e5SJosef Bacik char *curr = buf; 35083bd7e5SJosef Bacik 36083bd7e5SJosef Bacik memcpy(curr, STATE_STRING_PREFACE, sizeof(STATE_STRING_PREFACE)); 37083bd7e5SJosef Bacik curr += sizeof(STATE_STRING_PREFACE) - 1; 38083bd7e5SJosef Bacik 39*ae3364e5SFilipe Manana if (BTRFS_FS_ERROR(info)) { 40*ae3364e5SFilipe Manana *curr++ = 'E'; 41*ae3364e5SFilipe Manana states_printed = true; 42*ae3364e5SFilipe Manana } 43*ae3364e5SFilipe Manana 44083bd7e5SJosef Bacik for_each_set_bit(bit, &fs_state, sizeof(fs_state)) { 45083bd7e5SJosef Bacik WARN_ON_ONCE(bit >= BTRFS_FS_STATE_COUNT); 46083bd7e5SJosef Bacik if ((bit < BTRFS_FS_STATE_COUNT) && fs_state_chars[bit]) { 47083bd7e5SJosef Bacik *curr++ = fs_state_chars[bit]; 48083bd7e5SJosef Bacik states_printed = true; 49083bd7e5SJosef Bacik } 50083bd7e5SJosef Bacik } 51083bd7e5SJosef Bacik 52083bd7e5SJosef Bacik /* If no states were printed, reset the buffer */ 53083bd7e5SJosef Bacik if (!states_printed) 54083bd7e5SJosef Bacik curr = buf; 55083bd7e5SJosef Bacik 56083bd7e5SJosef Bacik *curr++ = 0; 57083bd7e5SJosef Bacik } 58083bd7e5SJosef Bacik #endif 59083bd7e5SJosef Bacik 60083bd7e5SJosef Bacik /* 61083bd7e5SJosef Bacik * Generally the error codes correspond to their respective errors, but there 62083bd7e5SJosef Bacik * are a few special cases. 63083bd7e5SJosef Bacik * 64083bd7e5SJosef Bacik * EUCLEAN: Any sort of corruption that we encounter. The tree-checker for 65083bd7e5SJosef Bacik * instance will return EUCLEAN if any of the blocks are corrupted in 66083bd7e5SJosef Bacik * a way that is problematic. We want to reserve EUCLEAN for these 67083bd7e5SJosef Bacik * sort of corruptions. 68083bd7e5SJosef Bacik * 69083bd7e5SJosef Bacik * EROFS: If we check BTRFS_FS_STATE_ERROR and fail out with a return error, we 70083bd7e5SJosef Bacik * need to use EROFS for this case. We will have no idea of the 71083bd7e5SJosef Bacik * original failure, that will have been reported at the time we tripped 72083bd7e5SJosef Bacik * over the error. Each subsequent error that doesn't have any context 73083bd7e5SJosef Bacik * of the original error should use EROFS when handling BTRFS_FS_STATE_ERROR. 74083bd7e5SJosef Bacik */ 75083bd7e5SJosef Bacik const char * __attribute_const__ btrfs_decode_error(int errno) 76083bd7e5SJosef Bacik { 77083bd7e5SJosef Bacik char *errstr = "unknown"; 78083bd7e5SJosef Bacik 79083bd7e5SJosef Bacik switch (errno) { 80083bd7e5SJosef Bacik case -ENOENT: /* -2 */ 81083bd7e5SJosef Bacik errstr = "No such entry"; 82083bd7e5SJosef Bacik break; 83083bd7e5SJosef Bacik case -EIO: /* -5 */ 84083bd7e5SJosef Bacik errstr = "IO failure"; 85083bd7e5SJosef Bacik break; 86083bd7e5SJosef Bacik case -ENOMEM: /* -12*/ 87083bd7e5SJosef Bacik errstr = "Out of memory"; 88083bd7e5SJosef Bacik break; 89083bd7e5SJosef Bacik case -EEXIST: /* -17 */ 90083bd7e5SJosef Bacik errstr = "Object already exists"; 91083bd7e5SJosef Bacik break; 92083bd7e5SJosef Bacik case -ENOSPC: /* -28 */ 93083bd7e5SJosef Bacik errstr = "No space left"; 94083bd7e5SJosef Bacik break; 95083bd7e5SJosef Bacik case -EROFS: /* -30 */ 96083bd7e5SJosef Bacik errstr = "Readonly filesystem"; 97083bd7e5SJosef Bacik break; 98083bd7e5SJosef Bacik case -EOPNOTSUPP: /* -95 */ 99083bd7e5SJosef Bacik errstr = "Operation not supported"; 100083bd7e5SJosef Bacik break; 101083bd7e5SJosef Bacik case -EUCLEAN: /* -117 */ 102083bd7e5SJosef Bacik errstr = "Filesystem corrupted"; 103083bd7e5SJosef Bacik break; 104083bd7e5SJosef Bacik case -EDQUOT: /* -122 */ 105083bd7e5SJosef Bacik errstr = "Quota exceeded"; 106083bd7e5SJosef Bacik break; 107083bd7e5SJosef Bacik } 108083bd7e5SJosef Bacik 109083bd7e5SJosef Bacik return errstr; 110083bd7e5SJosef Bacik } 111083bd7e5SJosef Bacik 112083bd7e5SJosef Bacik /* 113083bd7e5SJosef Bacik * __btrfs_handle_fs_error decodes expected errors from the caller and 114083bd7e5SJosef Bacik * invokes the appropriate error response. 115083bd7e5SJosef Bacik */ 116083bd7e5SJosef Bacik __cold 117083bd7e5SJosef Bacik void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 118083bd7e5SJosef Bacik unsigned int line, int errno, const char *fmt, ...) 119083bd7e5SJosef Bacik { 120083bd7e5SJosef Bacik struct super_block *sb = fs_info->sb; 121083bd7e5SJosef Bacik #ifdef CONFIG_PRINTK 122083bd7e5SJosef Bacik char statestr[STATE_STRING_BUF_LEN]; 123083bd7e5SJosef Bacik const char *errstr; 124083bd7e5SJosef Bacik #endif 125083bd7e5SJosef Bacik 126083bd7e5SJosef Bacik #ifdef CONFIG_PRINTK_INDEX 127083bd7e5SJosef Bacik printk_index_subsys_emit( 128083bd7e5SJosef Bacik "BTRFS: error (device %s%s) in %s:%d: errno=%d %s", KERN_CRIT, fmt); 129083bd7e5SJosef Bacik #endif 130083bd7e5SJosef Bacik 131083bd7e5SJosef Bacik /* 132083bd7e5SJosef Bacik * Special case: if the error is EROFS, and we're already under 133083bd7e5SJosef Bacik * SB_RDONLY, then it is safe here. 134083bd7e5SJosef Bacik */ 135083bd7e5SJosef Bacik if (errno == -EROFS && sb_rdonly(sb)) 136083bd7e5SJosef Bacik return; 137083bd7e5SJosef Bacik 138083bd7e5SJosef Bacik #ifdef CONFIG_PRINTK 139083bd7e5SJosef Bacik errstr = btrfs_decode_error(errno); 140083bd7e5SJosef Bacik btrfs_state_to_string(fs_info, statestr); 141083bd7e5SJosef Bacik if (fmt) { 142083bd7e5SJosef Bacik struct va_format vaf; 143083bd7e5SJosef Bacik va_list args; 144083bd7e5SJosef Bacik 145083bd7e5SJosef Bacik va_start(args, fmt); 146083bd7e5SJosef Bacik vaf.fmt = fmt; 147083bd7e5SJosef Bacik vaf.va = &args; 148083bd7e5SJosef Bacik 149083bd7e5SJosef Bacik pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s (%pV)\n", 150083bd7e5SJosef Bacik sb->s_id, statestr, function, line, errno, errstr, &vaf); 151083bd7e5SJosef Bacik va_end(args); 152083bd7e5SJosef Bacik } else { 153083bd7e5SJosef Bacik pr_crit("BTRFS: error (device %s%s) in %s:%d: errno=%d %s\n", 154083bd7e5SJosef Bacik sb->s_id, statestr, function, line, errno, errstr); 155083bd7e5SJosef Bacik } 156083bd7e5SJosef Bacik #endif 157083bd7e5SJosef Bacik 158083bd7e5SJosef Bacik /* 159083bd7e5SJosef Bacik * Today we only save the error info to memory. Long term we'll also 160083bd7e5SJosef Bacik * send it down to the disk. 161083bd7e5SJosef Bacik */ 162*ae3364e5SFilipe Manana WRITE_ONCE(fs_info->fs_error, errno); 163083bd7e5SJosef Bacik 164083bd7e5SJosef Bacik /* Don't go through full error handling during mount. */ 165083bd7e5SJosef Bacik if (!(sb->s_flags & SB_BORN)) 166083bd7e5SJosef Bacik return; 167083bd7e5SJosef Bacik 168083bd7e5SJosef Bacik if (sb_rdonly(sb)) 169083bd7e5SJosef Bacik return; 170083bd7e5SJosef Bacik 171083bd7e5SJosef Bacik btrfs_discard_stop(fs_info); 172083bd7e5SJosef Bacik 173083bd7e5SJosef Bacik /* Handle error by forcing the filesystem readonly. */ 174083bd7e5SJosef Bacik btrfs_set_sb_rdonly(sb); 175083bd7e5SJosef Bacik btrfs_info(fs_info, "forced readonly"); 176083bd7e5SJosef Bacik /* 177083bd7e5SJosef Bacik * Note that a running device replace operation is not canceled here 178083bd7e5SJosef Bacik * although there is no way to update the progress. It would add the 179083bd7e5SJosef Bacik * risk of a deadlock, therefore the canceling is omitted. The only 180083bd7e5SJosef Bacik * penalty is that some I/O remains active until the procedure 181083bd7e5SJosef Bacik * completes. The next time when the filesystem is mounted writable 182083bd7e5SJosef Bacik * again, the device replace operation continues. 183083bd7e5SJosef Bacik */ 184083bd7e5SJosef Bacik } 185083bd7e5SJosef Bacik 186083bd7e5SJosef Bacik #ifdef CONFIG_PRINTK 187083bd7e5SJosef Bacik static const char * const logtypes[] = { 188083bd7e5SJosef Bacik "emergency", 189083bd7e5SJosef Bacik "alert", 190083bd7e5SJosef Bacik "critical", 191083bd7e5SJosef Bacik "error", 192083bd7e5SJosef Bacik "warning", 193083bd7e5SJosef Bacik "notice", 194083bd7e5SJosef Bacik "info", 195083bd7e5SJosef Bacik "debug", 196083bd7e5SJosef Bacik }; 197083bd7e5SJosef Bacik 198083bd7e5SJosef Bacik /* 199083bd7e5SJosef Bacik * Use one ratelimit state per log level so that a flood of less important 200083bd7e5SJosef Bacik * messages doesn't cause more important ones to be dropped. 201083bd7e5SJosef Bacik */ 202083bd7e5SJosef Bacik static struct ratelimit_state printk_limits[] = { 203083bd7e5SJosef Bacik RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100), 204083bd7e5SJosef Bacik RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100), 205083bd7e5SJosef Bacik RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100), 206083bd7e5SJosef Bacik RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100), 207083bd7e5SJosef Bacik RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100), 208083bd7e5SJosef Bacik RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100), 209083bd7e5SJosef Bacik RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100), 210083bd7e5SJosef Bacik RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100), 211083bd7e5SJosef Bacik }; 212083bd7e5SJosef Bacik 213083bd7e5SJosef Bacik void __cold _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 214083bd7e5SJosef Bacik { 215083bd7e5SJosef Bacik char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0"; 216083bd7e5SJosef Bacik struct va_format vaf; 217083bd7e5SJosef Bacik va_list args; 218083bd7e5SJosef Bacik int kern_level; 219083bd7e5SJosef Bacik const char *type = logtypes[4]; 220083bd7e5SJosef Bacik struct ratelimit_state *ratelimit = &printk_limits[4]; 221083bd7e5SJosef Bacik 222083bd7e5SJosef Bacik #ifdef CONFIG_PRINTK_INDEX 223083bd7e5SJosef Bacik printk_index_subsys_emit("%sBTRFS %s (device %s): ", NULL, fmt); 224083bd7e5SJosef Bacik #endif 225083bd7e5SJosef Bacik 226083bd7e5SJosef Bacik va_start(args, fmt); 227083bd7e5SJosef Bacik 228083bd7e5SJosef Bacik while ((kern_level = printk_get_level(fmt)) != 0) { 229083bd7e5SJosef Bacik size_t size = printk_skip_level(fmt) - fmt; 230083bd7e5SJosef Bacik 231083bd7e5SJosef Bacik if (kern_level >= '0' && kern_level <= '7') { 232083bd7e5SJosef Bacik memcpy(lvl, fmt, size); 233083bd7e5SJosef Bacik lvl[size] = '\0'; 234083bd7e5SJosef Bacik type = logtypes[kern_level - '0']; 235083bd7e5SJosef Bacik ratelimit = &printk_limits[kern_level - '0']; 236083bd7e5SJosef Bacik } 237083bd7e5SJosef Bacik fmt += size; 238083bd7e5SJosef Bacik } 239083bd7e5SJosef Bacik 240083bd7e5SJosef Bacik vaf.fmt = fmt; 241083bd7e5SJosef Bacik vaf.va = &args; 242083bd7e5SJosef Bacik 243083bd7e5SJosef Bacik if (__ratelimit(ratelimit)) { 244083bd7e5SJosef Bacik if (fs_info) { 245083bd7e5SJosef Bacik char statestr[STATE_STRING_BUF_LEN]; 246083bd7e5SJosef Bacik 247083bd7e5SJosef Bacik btrfs_state_to_string(fs_info, statestr); 248083bd7e5SJosef Bacik _printk("%sBTRFS %s (device %s%s): %pV\n", lvl, type, 249083bd7e5SJosef Bacik fs_info->sb->s_id, statestr, &vaf); 250083bd7e5SJosef Bacik } else { 251083bd7e5SJosef Bacik _printk("%sBTRFS %s: %pV\n", lvl, type, &vaf); 252083bd7e5SJosef Bacik } 253083bd7e5SJosef Bacik } 254083bd7e5SJosef Bacik 255083bd7e5SJosef Bacik va_end(args); 256083bd7e5SJosef Bacik } 257083bd7e5SJosef Bacik #endif 258083bd7e5SJosef Bacik 259083bd7e5SJosef Bacik void __cold btrfs_print_v0_err(struct btrfs_fs_info *fs_info) 260083bd7e5SJosef Bacik { 261083bd7e5SJosef Bacik btrfs_err(fs_info, 262083bd7e5SJosef Bacik "Unsupported V0 extent filesystem detected. Aborting. Please re-create your filesystem with a newer kernel"); 263083bd7e5SJosef Bacik } 264083bd7e5SJosef Bacik 265083bd7e5SJosef Bacik #if BITS_PER_LONG == 32 266083bd7e5SJosef Bacik void __cold btrfs_warn_32bit_limit(struct btrfs_fs_info *fs_info) 267083bd7e5SJosef Bacik { 268083bd7e5SJosef Bacik if (!test_and_set_bit(BTRFS_FS_32BIT_WARN, &fs_info->flags)) { 269083bd7e5SJosef Bacik btrfs_warn(fs_info, "reaching 32bit limit for logical addresses"); 270083bd7e5SJosef Bacik btrfs_warn(fs_info, 271083bd7e5SJosef Bacik "due to page cache limit on 32bit systems, btrfs can't access metadata at or beyond %lluT", 272083bd7e5SJosef Bacik BTRFS_32BIT_MAX_FILE_SIZE >> 40); 273083bd7e5SJosef Bacik btrfs_warn(fs_info, 274083bd7e5SJosef Bacik "please consider upgrading to 64bit kernel/hardware"); 275083bd7e5SJosef Bacik } 276083bd7e5SJosef Bacik } 277083bd7e5SJosef Bacik 278083bd7e5SJosef Bacik void __cold btrfs_err_32bit_limit(struct btrfs_fs_info *fs_info) 279083bd7e5SJosef Bacik { 280083bd7e5SJosef Bacik if (!test_and_set_bit(BTRFS_FS_32BIT_ERROR, &fs_info->flags)) { 281083bd7e5SJosef Bacik btrfs_err(fs_info, "reached 32bit limit for logical addresses"); 282083bd7e5SJosef Bacik btrfs_err(fs_info, 283083bd7e5SJosef Bacik "due to page cache limit on 32bit systems, metadata beyond %lluT can't be accessed", 284083bd7e5SJosef Bacik BTRFS_32BIT_MAX_FILE_SIZE >> 40); 285083bd7e5SJosef Bacik btrfs_err(fs_info, 286083bd7e5SJosef Bacik "please consider upgrading to 64bit kernel/hardware"); 287083bd7e5SJosef Bacik } 288083bd7e5SJosef Bacik } 289083bd7e5SJosef Bacik #endif 290083bd7e5SJosef Bacik 291083bd7e5SJosef Bacik /* 292083bd7e5SJosef Bacik * __btrfs_panic decodes unexpected, fatal errors from the caller, issues an 293083bd7e5SJosef Bacik * alert, and either panics or BUGs, depending on mount options. 294083bd7e5SJosef Bacik */ 295083bd7e5SJosef Bacik __cold 296083bd7e5SJosef Bacik void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 297083bd7e5SJosef Bacik unsigned int line, int errno, const char *fmt, ...) 298083bd7e5SJosef Bacik { 299083bd7e5SJosef Bacik char *s_id = "<unknown>"; 300083bd7e5SJosef Bacik const char *errstr; 301083bd7e5SJosef Bacik struct va_format vaf = { .fmt = fmt }; 302083bd7e5SJosef Bacik va_list args; 303083bd7e5SJosef Bacik 304083bd7e5SJosef Bacik if (fs_info) 305083bd7e5SJosef Bacik s_id = fs_info->sb->s_id; 306083bd7e5SJosef Bacik 307083bd7e5SJosef Bacik va_start(args, fmt); 308083bd7e5SJosef Bacik vaf.va = &args; 309083bd7e5SJosef Bacik 310083bd7e5SJosef Bacik errstr = btrfs_decode_error(errno); 311083bd7e5SJosef Bacik if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR))) 312083bd7e5SJosef Bacik panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 313083bd7e5SJosef Bacik s_id, function, line, &vaf, errno, errstr); 314083bd7e5SJosef Bacik 315083bd7e5SJosef Bacik btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 316083bd7e5SJosef Bacik function, line, &vaf, errno, errstr); 317083bd7e5SJosef Bacik va_end(args); 318083bd7e5SJosef Bacik /* Caller calls BUG() */ 319083bd7e5SJosef Bacik } 320