1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Interface between ext4 and JBD 4 */ 5 6 #include "ext4_jbd2.h" 7 8 #include <trace/events/ext4.h> 9 10 int ext4_inode_journal_mode(struct inode *inode) 11 { 12 if (EXT4_JOURNAL(inode) == NULL) 13 return EXT4_INODE_WRITEBACK_DATA_MODE; /* writeback */ 14 /* We do not support data journalling with delayed allocation */ 15 if (!S_ISREG(inode->i_mode) || 16 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE) || 17 test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || 18 (ext4_test_inode_flag(inode, EXT4_INODE_JOURNAL_DATA) && 19 !test_opt(inode->i_sb, DELALLOC))) { 20 /* We do not support data journalling for encrypted data */ 21 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) 22 return EXT4_INODE_ORDERED_DATA_MODE; /* ordered */ 23 return EXT4_INODE_JOURNAL_DATA_MODE; /* journal data */ 24 } 25 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) 26 return EXT4_INODE_ORDERED_DATA_MODE; /* ordered */ 27 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA) 28 return EXT4_INODE_WRITEBACK_DATA_MODE; /* writeback */ 29 BUG(); 30 } 31 32 /* Just increment the non-pointer handle value */ 33 static handle_t *ext4_get_nojournal(void) 34 { 35 handle_t *handle = current->journal_info; 36 unsigned long ref_cnt = (unsigned long)handle; 37 38 BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT); 39 40 ref_cnt++; 41 handle = (handle_t *)ref_cnt; 42 43 current->journal_info = handle; 44 return handle; 45 } 46 47 48 /* Decrement the non-pointer handle value */ 49 static void ext4_put_nojournal(handle_t *handle) 50 { 51 unsigned long ref_cnt = (unsigned long)handle; 52 53 BUG_ON(ref_cnt == 0); 54 55 ref_cnt--; 56 handle = (handle_t *)ref_cnt; 57 58 current->journal_info = handle; 59 } 60 61 /* 62 * Wrappers for jbd2_journal_start/end. 63 */ 64 static int ext4_journal_check_start(struct super_block *sb) 65 { 66 int ret; 67 journal_t *journal; 68 69 might_sleep(); 70 71 ret = ext4_emergency_state(sb); 72 if (unlikely(ret)) 73 return ret; 74 75 if (WARN_ON_ONCE(sb_rdonly(sb))) 76 return -EROFS; 77 78 WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE); 79 journal = EXT4_SB(sb)->s_journal; 80 /* 81 * Special case here: if the journal has aborted behind our 82 * backs (eg. EIO in the commit thread), then we still need to 83 * take the FS itself readonly cleanly. 84 */ 85 if (journal && is_journal_aborted(journal)) { 86 ext4_abort(sb, -journal->j_errno, "Detected aborted journal"); 87 return -EROFS; 88 } 89 return 0; 90 } 91 92 handle_t *__ext4_journal_start_sb(struct inode *inode, 93 struct super_block *sb, unsigned int line, 94 int type, int blocks, int rsv_blocks, 95 int revoke_creds) 96 { 97 journal_t *journal; 98 int err; 99 if (inode) 100 trace_ext4_journal_start_inode(inode, blocks, rsv_blocks, 101 revoke_creds, type, 102 _RET_IP_); 103 else 104 trace_ext4_journal_start_sb(sb, blocks, rsv_blocks, 105 revoke_creds, type, 106 _RET_IP_); 107 err = ext4_journal_check_start(sb); 108 if (err < 0) 109 return ERR_PTR(err); 110 111 journal = EXT4_SB(sb)->s_journal; 112 if (!journal || (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY)) 113 return ext4_get_nojournal(); 114 return jbd2__journal_start(journal, blocks, rsv_blocks, revoke_creds, 115 GFP_NOFS, type, line); 116 } 117 118 int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle) 119 { 120 struct super_block *sb; 121 int err; 122 int rc; 123 124 if (!ext4_handle_valid(handle)) { 125 ext4_put_nojournal(handle); 126 return 0; 127 } 128 129 err = handle->h_err; 130 if (!handle->h_transaction) { 131 rc = jbd2_journal_stop(handle); 132 return err ? err : rc; 133 } 134 135 sb = handle->h_transaction->t_journal->j_private; 136 rc = jbd2_journal_stop(handle); 137 138 if (!err) 139 err = rc; 140 if (err) 141 __ext4_std_error(sb, where, line, err); 142 return err; 143 } 144 145 handle_t *__ext4_journal_start_reserved(handle_t *handle, unsigned int line, 146 int type) 147 { 148 struct super_block *sb; 149 int err; 150 151 if (!ext4_handle_valid(handle)) 152 return ext4_get_nojournal(); 153 154 sb = handle->h_journal->j_private; 155 trace_ext4_journal_start_reserved(sb, 156 jbd2_handle_buffer_credits(handle), _RET_IP_); 157 err = ext4_journal_check_start(sb); 158 if (err < 0) { 159 jbd2_journal_free_reserved(handle); 160 return ERR_PTR(err); 161 } 162 163 err = jbd2_journal_start_reserved(handle, type, line); 164 if (err < 0) 165 return ERR_PTR(err); 166 return handle; 167 } 168 169 int __ext4_journal_ensure_credits(handle_t *handle, int check_cred, 170 int extend_cred, int revoke_cred) 171 { 172 if (!ext4_handle_valid(handle)) 173 return 0; 174 if (is_handle_aborted(handle)) 175 return -EROFS; 176 if (jbd2_handle_buffer_credits(handle) >= check_cred && 177 handle->h_revoke_credits >= revoke_cred) 178 return 0; 179 extend_cred = max(0, extend_cred - jbd2_handle_buffer_credits(handle)); 180 revoke_cred = max(0, revoke_cred - handle->h_revoke_credits); 181 return ext4_journal_extend(handle, extend_cred, revoke_cred); 182 } 183 184 static void ext4_journal_abort_handle(const char *caller, unsigned int line, 185 const char *err_fn, 186 struct buffer_head *bh, 187 handle_t *handle, int err) 188 { 189 char nbuf[16]; 190 const char *errstr = ext4_decode_error(NULL, err, nbuf); 191 192 BUG_ON(!ext4_handle_valid(handle)); 193 194 if (bh) 195 BUFFER_TRACE(bh, "abort"); 196 197 if (!handle->h_err) 198 handle->h_err = err; 199 200 if (is_handle_aborted(handle)) 201 return; 202 203 printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n", 204 caller, line, errstr, err_fn); 205 206 jbd2_journal_abort_handle(handle); 207 } 208 209 static void ext4_check_bdev_write_error(struct super_block *sb) 210 { 211 struct address_space *mapping = sb->s_bdev->bd_mapping; 212 struct ext4_sb_info *sbi = EXT4_SB(sb); 213 int err; 214 215 /* 216 * If the block device has write error flag, it may have failed to 217 * async write out metadata buffers in the background. In this case, 218 * we could read old data from disk and write it out again, which 219 * may lead to on-disk filesystem inconsistency. 220 */ 221 if (errseq_check(&mapping->wb_err, READ_ONCE(sbi->s_bdev_wb_err))) { 222 spin_lock(&sbi->s_bdev_wb_lock); 223 err = errseq_check_and_advance(&mapping->wb_err, &sbi->s_bdev_wb_err); 224 spin_unlock(&sbi->s_bdev_wb_lock); 225 if (err) 226 ext4_error_err(sb, -err, 227 "Error while async write back metadata"); 228 } 229 } 230 231 int __ext4_journal_get_write_access(const char *where, unsigned int line, 232 handle_t *handle, struct super_block *sb, 233 struct buffer_head *bh, 234 enum ext4_journal_trigger_type trigger_type) 235 { 236 int err; 237 238 might_sleep(); 239 240 if (ext4_handle_valid(handle)) { 241 err = jbd2_journal_get_write_access(handle, bh); 242 if (err) { 243 ext4_journal_abort_handle(where, line, __func__, bh, 244 handle, err); 245 return err; 246 } 247 } else 248 ext4_check_bdev_write_error(sb); 249 if (trigger_type == EXT4_JTR_NONE || 250 !ext4_has_feature_metadata_csum(sb)) 251 return 0; 252 BUG_ON(trigger_type >= EXT4_JOURNAL_TRIGGER_COUNT); 253 jbd2_journal_set_triggers(bh, 254 &EXT4_SB(sb)->s_journal_triggers[trigger_type].tr_triggers); 255 return 0; 256 } 257 258 /* 259 * The ext4 forget function must perform a revoke if we are freeing data 260 * which has been journaled. Metadata (eg. indirect blocks) must be 261 * revoked in all cases. 262 * 263 * "bh" may be NULL: a metadata block may have been freed from memory 264 * but there may still be a record of it in the journal, and that record 265 * still needs to be revoked. 266 */ 267 int __ext4_forget(const char *where, unsigned int line, handle_t *handle, 268 int is_metadata, struct inode *inode, 269 struct buffer_head *bh, ext4_fsblk_t blocknr) 270 { 271 int err; 272 273 might_sleep(); 274 275 trace_ext4_forget(inode, is_metadata, blocknr); 276 BUFFER_TRACE(bh, "enter"); 277 278 ext4_debug("forgetting bh %p: is_metadata=%d, mode %o, data mode %x\n", 279 bh, is_metadata, inode->i_mode, 280 test_opt(inode->i_sb, DATA_FLAGS)); 281 282 /* In the no journal case, we can just do a bforget and return */ 283 if (!ext4_handle_valid(handle)) { 284 bforget(bh); 285 return 0; 286 } 287 288 /* Never use the revoke function if we are doing full data 289 * journaling: there is no need to, and a V1 superblock won't 290 * support it. Otherwise, only skip the revoke on un-journaled 291 * data blocks. */ 292 293 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || 294 (!is_metadata && !ext4_should_journal_data(inode))) { 295 if (bh) { 296 BUFFER_TRACE(bh, "call jbd2_journal_forget"); 297 err = jbd2_journal_forget(handle, bh); 298 if (err) 299 ext4_journal_abort_handle(where, line, __func__, 300 bh, handle, err); 301 return err; 302 } 303 return 0; 304 } 305 306 /* 307 * data!=journal && (is_metadata || should_journal_data(inode)) 308 */ 309 BUFFER_TRACE(bh, "call jbd2_journal_revoke"); 310 err = jbd2_journal_revoke(handle, blocknr, bh); 311 if (err) { 312 ext4_journal_abort_handle(where, line, __func__, 313 bh, handle, err); 314 __ext4_error(inode->i_sb, where, line, true, -err, 0, 315 "error %d when attempting revoke", err); 316 } 317 BUFFER_TRACE(bh, "exit"); 318 return err; 319 } 320 321 int __ext4_journal_get_create_access(const char *where, unsigned int line, 322 handle_t *handle, struct super_block *sb, 323 struct buffer_head *bh, 324 enum ext4_journal_trigger_type trigger_type) 325 { 326 int err; 327 328 if (!ext4_handle_valid(handle)) 329 return 0; 330 331 err = jbd2_journal_get_create_access(handle, bh); 332 if (err) { 333 ext4_journal_abort_handle(where, line, __func__, bh, handle, 334 err); 335 return err; 336 } 337 if (trigger_type == EXT4_JTR_NONE || 338 !ext4_has_feature_metadata_csum(sb)) 339 return 0; 340 BUG_ON(trigger_type >= EXT4_JOURNAL_TRIGGER_COUNT); 341 jbd2_journal_set_triggers(bh, 342 &EXT4_SB(sb)->s_journal_triggers[trigger_type].tr_triggers); 343 return 0; 344 } 345 346 int __ext4_handle_dirty_metadata(const char *where, unsigned int line, 347 handle_t *handle, struct inode *inode, 348 struct buffer_head *bh) 349 { 350 int err = 0; 351 352 might_sleep(); 353 354 set_buffer_meta(bh); 355 set_buffer_prio(bh); 356 set_buffer_uptodate(bh); 357 if (ext4_handle_valid(handle)) { 358 err = jbd2_journal_dirty_metadata(handle, bh); 359 /* Errors can only happen due to aborted journal or a nasty bug */ 360 if (!is_handle_aborted(handle) && WARN_ON_ONCE(err)) { 361 ext4_journal_abort_handle(where, line, __func__, bh, 362 handle, err); 363 if (inode == NULL) { 364 pr_err("EXT4: jbd2_journal_dirty_metadata " 365 "failed: handle type %u started at " 366 "line %u, credits %u/%u, errcode %d", 367 handle->h_type, 368 handle->h_line_no, 369 handle->h_requested_credits, 370 jbd2_handle_buffer_credits(handle), err); 371 return err; 372 } 373 ext4_error_inode(inode, where, line, 374 bh->b_blocknr, 375 "journal_dirty_metadata failed: " 376 "handle type %u started at line %u, " 377 "credits %u/%u, errcode %d", 378 handle->h_type, 379 handle->h_line_no, 380 handle->h_requested_credits, 381 jbd2_handle_buffer_credits(handle), 382 err); 383 } 384 } else { 385 if (inode) 386 mark_buffer_dirty_inode(bh, inode); 387 else 388 mark_buffer_dirty(bh); 389 if (inode && inode_needs_sync(inode)) { 390 sync_dirty_buffer(bh); 391 if (buffer_req(bh) && !buffer_uptodate(bh)) { 392 ext4_error_inode_err(inode, where, line, 393 bh->b_blocknr, EIO, 394 "IO error syncing itable block"); 395 err = -EIO; 396 } 397 } 398 } 399 return err; 400 } 401