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