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 /* 283 * In the no journal case, we should wait for the ongoing buffer 284 * to complete and do a forget. 285 */ 286 if (!ext4_handle_valid(handle)) { 287 if (bh) { 288 clear_buffer_dirty(bh); 289 wait_on_buffer(bh); 290 __bforget(bh); 291 } 292 return 0; 293 } 294 295 /* Never use the revoke function if we are doing full data 296 * journaling: there is no need to, and a V1 superblock won't 297 * support it. Otherwise, only skip the revoke on un-journaled 298 * data blocks. */ 299 300 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || 301 (!is_metadata && !ext4_should_journal_data(inode))) { 302 if (bh) { 303 BUFFER_TRACE(bh, "call jbd2_journal_forget"); 304 err = jbd2_journal_forget(handle, bh); 305 if (err) 306 ext4_journal_abort_handle(where, line, __func__, 307 bh, handle, err); 308 return err; 309 } 310 return 0; 311 } 312 313 /* 314 * data!=journal && (is_metadata || should_journal_data(inode)) 315 */ 316 BUFFER_TRACE(bh, "call jbd2_journal_revoke"); 317 err = jbd2_journal_revoke(handle, blocknr, bh); 318 if (err) { 319 ext4_journal_abort_handle(where, line, __func__, 320 bh, handle, err); 321 __ext4_error(inode->i_sb, where, line, true, -err, 0, 322 "error %d when attempting revoke", err); 323 } 324 BUFFER_TRACE(bh, "exit"); 325 return err; 326 } 327 328 int __ext4_journal_get_create_access(const char *where, unsigned int line, 329 handle_t *handle, struct super_block *sb, 330 struct buffer_head *bh, 331 enum ext4_journal_trigger_type trigger_type) 332 { 333 int err; 334 335 if (!ext4_handle_valid(handle)) 336 return 0; 337 338 err = jbd2_journal_get_create_access(handle, bh); 339 if (err) { 340 ext4_journal_abort_handle(where, line, __func__, bh, handle, 341 err); 342 return err; 343 } 344 if (trigger_type == EXT4_JTR_NONE || 345 !ext4_has_feature_metadata_csum(sb)) 346 return 0; 347 BUG_ON(trigger_type >= EXT4_JOURNAL_TRIGGER_COUNT); 348 jbd2_journal_set_triggers(bh, 349 &EXT4_SB(sb)->s_journal_triggers[trigger_type].tr_triggers); 350 return 0; 351 } 352 353 int __ext4_handle_dirty_metadata(const char *where, unsigned int line, 354 handle_t *handle, struct inode *inode, 355 struct buffer_head *bh) 356 { 357 int err = 0; 358 359 might_sleep(); 360 361 set_buffer_meta(bh); 362 set_buffer_prio(bh); 363 set_buffer_uptodate(bh); 364 if (ext4_handle_valid(handle)) { 365 err = jbd2_journal_dirty_metadata(handle, bh); 366 /* Errors can only happen due to aborted journal or a nasty bug */ 367 if (!is_handle_aborted(handle) && WARN_ON_ONCE(err)) { 368 ext4_journal_abort_handle(where, line, __func__, bh, 369 handle, err); 370 if (inode == NULL) { 371 pr_err("EXT4: jbd2_journal_dirty_metadata " 372 "failed: handle type %u started at " 373 "line %u, credits %u/%u, errcode %d", 374 handle->h_type, 375 handle->h_line_no, 376 handle->h_requested_credits, 377 jbd2_handle_buffer_credits(handle), err); 378 return err; 379 } 380 ext4_error_inode(inode, where, line, 381 bh->b_blocknr, 382 "journal_dirty_metadata failed: " 383 "handle type %u started at line %u, " 384 "credits %u/%u, errcode %d", 385 handle->h_type, 386 handle->h_line_no, 387 handle->h_requested_credits, 388 jbd2_handle_buffer_credits(handle), 389 err); 390 } 391 } else { 392 if (inode) 393 mark_buffer_dirty_inode(bh, inode); 394 else 395 mark_buffer_dirty(bh); 396 if (inode && inode_needs_sync(inode)) { 397 sync_dirty_buffer(bh); 398 if (buffer_req(bh) && !buffer_uptodate(bh)) { 399 ext4_error_inode_err(inode, where, line, 400 bh->b_blocknr, EIO, 401 "IO error syncing itable block"); 402 err = -EIO; 403 } 404 } 405 } 406 return err; 407 } 408