1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * linux/fs/jbd2/journal.c 4 * 5 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 6 * 7 * Copyright 1998 Red Hat corp --- All Rights Reserved 8 * 9 * Generic filesystem journal-writing code; part of the ext2fs 10 * journaling system. 11 * 12 * This file manages journals: areas of disk reserved for logging 13 * transactional updates. This includes the kernel journaling thread 14 * which is responsible for scheduling updates to the log. 15 * 16 * We do not actually manage the physical storage of the journal in this 17 * file: that is left to a per-journal policy function, which allows us 18 * to store the journal within a filesystem-specified area for ext2 19 * journaling (ext2 can use a reserved inode for storing the log). 20 */ 21 22 #include <linux/module.h> 23 #include <linux/time.h> 24 #include <linux/fs.h> 25 #include <linux/jbd2.h> 26 #include <linux/errno.h> 27 #include <linux/slab.h> 28 #include <linux/init.h> 29 #include <linux/mm.h> 30 #include <linux/freezer.h> 31 #include <linux/pagemap.h> 32 #include <linux/kthread.h> 33 #include <linux/poison.h> 34 #include <linux/proc_fs.h> 35 #include <linux/seq_file.h> 36 #include <linux/math64.h> 37 #include <linux/hash.h> 38 #include <linux/log2.h> 39 #include <linux/vmalloc.h> 40 #include <linux/backing-dev.h> 41 #include <linux/bitops.h> 42 #include <linux/ratelimit.h> 43 #include <linux/sched/mm.h> 44 45 #define CREATE_TRACE_POINTS 46 #include <trace/events/jbd2.h> 47 48 #include <linux/uaccess.h> 49 #include <asm/page.h> 50 51 #ifdef CONFIG_JBD2_DEBUG 52 static ushort jbd2_journal_enable_debug __read_mostly; 53 54 module_param_named(jbd2_debug, jbd2_journal_enable_debug, ushort, 0644); 55 MODULE_PARM_DESC(jbd2_debug, "Debugging level for jbd2"); 56 #endif 57 58 EXPORT_SYMBOL(jbd2_journal_extend); 59 EXPORT_SYMBOL(jbd2_journal_stop); 60 EXPORT_SYMBOL(jbd2_journal_lock_updates); 61 EXPORT_SYMBOL(jbd2_journal_unlock_updates); 62 EXPORT_SYMBOL(jbd2_journal_get_write_access); 63 EXPORT_SYMBOL(jbd2_journal_get_create_access); 64 EXPORT_SYMBOL(jbd2_journal_get_undo_access); 65 EXPORT_SYMBOL(jbd2_journal_set_triggers); 66 EXPORT_SYMBOL(jbd2_journal_dirty_metadata); 67 EXPORT_SYMBOL(jbd2_journal_forget); 68 EXPORT_SYMBOL(jbd2_journal_flush); 69 EXPORT_SYMBOL(jbd2_journal_revoke); 70 71 EXPORT_SYMBOL(jbd2_journal_init_dev); 72 EXPORT_SYMBOL(jbd2_journal_init_inode); 73 EXPORT_SYMBOL(jbd2_journal_check_used_features); 74 EXPORT_SYMBOL(jbd2_journal_check_available_features); 75 EXPORT_SYMBOL(jbd2_journal_set_features); 76 EXPORT_SYMBOL(jbd2_journal_load); 77 EXPORT_SYMBOL(jbd2_journal_destroy); 78 EXPORT_SYMBOL(jbd2_journal_abort); 79 EXPORT_SYMBOL(jbd2_journal_errno); 80 EXPORT_SYMBOL(jbd2_journal_ack_err); 81 EXPORT_SYMBOL(jbd2_journal_clear_err); 82 EXPORT_SYMBOL(jbd2_log_wait_commit); 83 EXPORT_SYMBOL(jbd2_journal_start_commit); 84 EXPORT_SYMBOL(jbd2_journal_force_commit_nested); 85 EXPORT_SYMBOL(jbd2_journal_wipe); 86 EXPORT_SYMBOL(jbd2_journal_blocks_per_folio); 87 EXPORT_SYMBOL(jbd2_journal_invalidate_folio); 88 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers); 89 EXPORT_SYMBOL(jbd2_journal_force_commit); 90 EXPORT_SYMBOL(jbd2_journal_inode_ranged_write); 91 EXPORT_SYMBOL(jbd2_journal_inode_ranged_wait); 92 EXPORT_SYMBOL(jbd2_journal_finish_inode_data_buffers); 93 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode); 94 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode); 95 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate); 96 EXPORT_SYMBOL(jbd2_inode_cache); 97 EXPORT_SYMBOL(jbd2_handle_cache); 98 99 #ifdef CONFIG_JBD2_DEBUG 100 void __jbd2_debug(int level, const char *file, const char *func, 101 unsigned int line, const char *fmt, ...) 102 { 103 struct va_format vaf; 104 va_list args; 105 106 if (level > jbd2_journal_enable_debug) 107 return; 108 va_start(args, fmt); 109 vaf.fmt = fmt; 110 vaf.va = &args; 111 printk(KERN_DEBUG "%s: (%s, %u): %pV", file, func, line, &vaf); 112 va_end(args); 113 } 114 #endif 115 116 /* Checksumming functions */ 117 static __be32 jbd2_superblock_csum(journal_superblock_t *sb) 118 { 119 __u32 csum; 120 __be32 old_csum; 121 122 old_csum = sb->s_checksum; 123 sb->s_checksum = 0; 124 csum = jbd2_chksum(~0, (char *)sb, sizeof(journal_superblock_t)); 125 sb->s_checksum = old_csum; 126 127 return cpu_to_be32(csum); 128 } 129 130 /* 131 * Helper function used to manage commit timeouts 132 */ 133 134 static void commit_timeout(struct timer_list *t) 135 { 136 journal_t *journal = timer_container_of(journal, t, j_commit_timer); 137 138 wake_up_process(journal->j_task); 139 } 140 141 /* 142 * kjournald2: The main thread function used to manage a logging device 143 * journal. 144 * 145 * This kernel thread is responsible for two things: 146 * 147 * 1) COMMIT: Every so often we need to commit the current state of the 148 * filesystem to disk. The journal thread is responsible for writing 149 * all of the metadata buffers to disk. If a fast commit is ongoing 150 * journal thread waits until it's done and then continues from 151 * there on. 152 * 153 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all 154 * of the data in that part of the log has been rewritten elsewhere on 155 * the disk. Flushing these old buffers to reclaim space in the log is 156 * known as checkpointing, and this thread is responsible for that job. 157 */ 158 159 static int kjournald2(void *arg) 160 { 161 journal_t *journal = arg; 162 transaction_t *transaction; 163 164 /* 165 * Set up an interval timer which can be used to trigger a commit wakeup 166 * after the commit interval expires 167 */ 168 timer_setup(&journal->j_commit_timer, commit_timeout, 0); 169 170 set_freezable(); 171 172 /* Record that the journal thread is running */ 173 journal->j_task = current; 174 wake_up(&journal->j_wait_done_commit); 175 176 /* 177 * Make sure that no allocations from this kernel thread will ever 178 * recurse to the fs layer because we are responsible for the 179 * transaction commit and any fs involvement might get stuck waiting for 180 * the trasn. commit. 181 */ 182 memalloc_nofs_save(); 183 184 /* 185 * And now, wait forever for commit wakeup events. 186 */ 187 write_lock(&journal->j_state_lock); 188 189 loop: 190 if (journal->j_flags & JBD2_UNMOUNT) 191 goto end_loop; 192 193 jbd2_debug(1, "commit_sequence=%u, commit_request=%u\n", 194 journal->j_commit_sequence, journal->j_commit_request); 195 196 if (journal->j_commit_sequence != journal->j_commit_request) { 197 jbd2_debug(1, "OK, requests differ\n"); 198 write_unlock(&journal->j_state_lock); 199 timer_delete_sync(&journal->j_commit_timer); 200 jbd2_journal_commit_transaction(journal); 201 write_lock(&journal->j_state_lock); 202 goto loop; 203 } 204 205 wake_up(&journal->j_wait_done_commit); 206 if (freezing(current)) { 207 /* 208 * The simpler the better. Flushing journal isn't a 209 * good idea, because that depends on threads that may 210 * be already stopped. 211 */ 212 jbd2_debug(1, "Now suspending kjournald2\n"); 213 write_unlock(&journal->j_state_lock); 214 try_to_freeze(); 215 write_lock(&journal->j_state_lock); 216 } else { 217 /* 218 * We assume on resume that commits are already there, 219 * so we don't sleep 220 */ 221 DEFINE_WAIT(wait); 222 223 prepare_to_wait(&journal->j_wait_commit, &wait, 224 TASK_INTERRUPTIBLE); 225 transaction = journal->j_running_transaction; 226 if (transaction == NULL || 227 time_before(jiffies, transaction->t_expires)) { 228 write_unlock(&journal->j_state_lock); 229 schedule(); 230 write_lock(&journal->j_state_lock); 231 } 232 finish_wait(&journal->j_wait_commit, &wait); 233 } 234 235 jbd2_debug(1, "kjournald2 wakes\n"); 236 237 /* 238 * Were we woken up by a commit wakeup event? 239 */ 240 transaction = journal->j_running_transaction; 241 if (transaction && time_after_eq(jiffies, transaction->t_expires)) { 242 journal->j_commit_request = transaction->t_tid; 243 jbd2_debug(1, "woke because of timeout\n"); 244 } 245 goto loop; 246 247 end_loop: 248 timer_delete_sync(&journal->j_commit_timer); 249 journal->j_task = NULL; 250 wake_up(&journal->j_wait_done_commit); 251 jbd2_debug(1, "Journal thread exiting.\n"); 252 write_unlock(&journal->j_state_lock); 253 return 0; 254 } 255 256 static int jbd2_journal_start_thread(journal_t *journal) 257 { 258 struct task_struct *t; 259 260 t = kthread_run(kjournald2, journal, "jbd2/%s", 261 journal->j_devname); 262 if (IS_ERR(t)) 263 return PTR_ERR(t); 264 265 wait_event(journal->j_wait_done_commit, journal->j_task != NULL); 266 return 0; 267 } 268 269 static void journal_kill_thread(journal_t *journal) 270 { 271 write_lock(&journal->j_state_lock); 272 journal->j_flags |= JBD2_UNMOUNT; 273 274 while (journal->j_task) { 275 write_unlock(&journal->j_state_lock); 276 wake_up(&journal->j_wait_commit); 277 wait_event(journal->j_wait_done_commit, journal->j_task == NULL); 278 write_lock(&journal->j_state_lock); 279 } 280 write_unlock(&journal->j_state_lock); 281 } 282 283 static inline bool jbd2_data_needs_escaping(char *data) 284 { 285 return *((__be32 *)data) == cpu_to_be32(JBD2_MAGIC_NUMBER); 286 } 287 288 static inline void jbd2_data_do_escape(char *data) 289 { 290 *((unsigned int *)data) = 0; 291 } 292 293 /* 294 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal. 295 * 296 * Writes a metadata buffer to a given disk block. The actual IO is not 297 * performed but a new buffer_head is constructed which labels the data 298 * to be written with the correct destination disk block. 299 * 300 * Any magic-number escaping which needs to be done will cause a 301 * copy-out here. If the buffer happens to start with the 302 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the 303 * magic number is only written to the log for descripter blocks. In 304 * this case, we copy the data and replace the first word with 0, and we 305 * return a result code which indicates that this buffer needs to be 306 * marked as an escaped buffer in the corresponding log descriptor 307 * block. The missing word can then be restored when the block is read 308 * during recovery. 309 * 310 * If the source buffer has already been modified by a new transaction 311 * since we took the last commit snapshot, we use the frozen copy of 312 * that data for IO. If we end up using the existing buffer_head's data 313 * for the write, then we have to make sure nobody modifies it while the 314 * IO is in progress. do_get_write_access() handles this. 315 * 316 * The function returns a pointer to the buffer_head to be used for IO. 317 * 318 * 319 * Return value: 320 * =0: Finished OK without escape 321 * =1: Finished OK with escape 322 */ 323 324 int jbd2_journal_write_metadata_buffer(transaction_t *transaction, 325 struct journal_head *jh_in, 326 struct buffer_head **bh_out, 327 sector_t blocknr) 328 { 329 int do_escape = 0; 330 struct buffer_head *new_bh; 331 struct folio *new_folio; 332 unsigned int new_offset; 333 struct buffer_head *bh_in = jh2bh(jh_in); 334 journal_t *journal = transaction->t_journal; 335 336 /* 337 * The buffer really shouldn't be locked: only the current committing 338 * transaction is allowed to write it, so nobody else is allowed 339 * to do any IO. 340 * 341 * akpm: except if we're journalling data, and write() output is 342 * also part of a shared mapping, and another thread has 343 * decided to launch a writepage() against this buffer. 344 */ 345 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in)); 346 347 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL); 348 349 /* keep subsequent assertions sane */ 350 atomic_set(&new_bh->b_count, 1); 351 352 spin_lock(&jh_in->b_state_lock); 353 /* 354 * If a new transaction has already done a buffer copy-out, then 355 * we use that version of the data for the commit. 356 */ 357 if (jh_in->b_frozen_data) { 358 new_folio = virt_to_folio(jh_in->b_frozen_data); 359 new_offset = offset_in_folio(new_folio, jh_in->b_frozen_data); 360 do_escape = jbd2_data_needs_escaping(jh_in->b_frozen_data); 361 if (do_escape) 362 jbd2_data_do_escape(jh_in->b_frozen_data); 363 } else { 364 char *tmp; 365 char *mapped_data; 366 367 new_folio = bh_in->b_folio; 368 new_offset = offset_in_folio(new_folio, bh_in->b_data); 369 mapped_data = kmap_local_folio(new_folio, new_offset); 370 /* 371 * Fire data frozen trigger if data already wasn't frozen. Do 372 * this before checking for escaping, as the trigger may modify 373 * the magic offset. If a copy-out happens afterwards, it will 374 * have the correct data in the buffer. 375 */ 376 jbd2_buffer_frozen_trigger(jh_in, mapped_data, 377 jh_in->b_triggers); 378 do_escape = jbd2_data_needs_escaping(mapped_data); 379 kunmap_local(mapped_data); 380 /* 381 * Do we need to do a data copy? 382 */ 383 if (!do_escape) 384 goto escape_done; 385 386 spin_unlock(&jh_in->b_state_lock); 387 tmp = kmalloc(bh_in->b_size, GFP_NOFS | __GFP_NOFAIL); 388 spin_lock(&jh_in->b_state_lock); 389 if (jh_in->b_frozen_data) { 390 kfree(tmp); 391 goto copy_done; 392 } 393 394 jh_in->b_frozen_data = tmp; 395 memcpy_from_folio(tmp, new_folio, new_offset, bh_in->b_size); 396 /* 397 * This isn't strictly necessary, as we're using frozen 398 * data for the escaping, but it keeps consistency with 399 * b_frozen_data usage. 400 */ 401 jh_in->b_frozen_triggers = jh_in->b_triggers; 402 403 copy_done: 404 new_folio = virt_to_folio(jh_in->b_frozen_data); 405 new_offset = offset_in_folio(new_folio, jh_in->b_frozen_data); 406 jbd2_data_do_escape(jh_in->b_frozen_data); 407 } 408 409 escape_done: 410 folio_set_bh(new_bh, new_folio, new_offset); 411 new_bh->b_size = bh_in->b_size; 412 new_bh->b_bdev = journal->j_dev; 413 new_bh->b_blocknr = blocknr; 414 new_bh->b_private = bh_in; 415 set_buffer_mapped(new_bh); 416 set_buffer_dirty(new_bh); 417 418 *bh_out = new_bh; 419 420 /* 421 * The to-be-written buffer needs to get moved to the io queue, 422 * and the original buffer whose contents we are shadowing or 423 * copying is moved to the transaction's shadow queue. 424 */ 425 JBUFFER_TRACE(jh_in, "file as BJ_Shadow"); 426 spin_lock(&journal->j_list_lock); 427 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow); 428 spin_unlock(&journal->j_list_lock); 429 set_buffer_shadow(bh_in); 430 spin_unlock(&jh_in->b_state_lock); 431 432 return do_escape; 433 } 434 435 /* 436 * Allocation code for the journal file. Manage the space left in the 437 * journal, so that we can begin checkpointing when appropriate. 438 */ 439 440 /* 441 * Called with j_state_lock locked for writing. 442 * Returns true if a transaction commit was started. 443 */ 444 static int __jbd2_log_start_commit(journal_t *journal, tid_t target) 445 { 446 /* Return if the txn has already requested to be committed */ 447 if (journal->j_commit_request == target) 448 return 0; 449 450 /* 451 * The only transaction we can possibly wait upon is the 452 * currently running transaction (if it exists). Otherwise, 453 * the target tid must be an old one. 454 */ 455 if (journal->j_running_transaction && 456 journal->j_running_transaction->t_tid == target) { 457 /* 458 * We want a new commit: OK, mark the request and wakeup the 459 * commit thread. We do _not_ do the commit ourselves. 460 */ 461 462 journal->j_commit_request = target; 463 jbd2_debug(1, "JBD2: requesting commit %u/%u\n", 464 journal->j_commit_request, 465 journal->j_commit_sequence); 466 journal->j_running_transaction->t_requested = jiffies; 467 wake_up(&journal->j_wait_commit); 468 return 1; 469 } else if (!tid_geq(journal->j_commit_request, target)) 470 /* This should never happen, but if it does, preserve 471 the evidence before kjournald goes into a loop and 472 increments j_commit_sequence beyond all recognition. */ 473 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n", 474 journal->j_commit_request, 475 journal->j_commit_sequence, 476 target, journal->j_running_transaction ? 477 journal->j_running_transaction->t_tid : 0); 478 return 0; 479 } 480 481 int jbd2_log_start_commit(journal_t *journal, tid_t tid) 482 { 483 int ret; 484 485 write_lock(&journal->j_state_lock); 486 ret = __jbd2_log_start_commit(journal, tid); 487 write_unlock(&journal->j_state_lock); 488 return ret; 489 } 490 491 /* 492 * Force and wait any uncommitted transactions. We can only force the running 493 * transaction if we don't have an active handle, otherwise, we will deadlock. 494 * Returns: <0 in case of error, 495 * 0 if nothing to commit, 496 * 1 if transaction was successfully committed. 497 */ 498 static int __jbd2_journal_force_commit(journal_t *journal) 499 { 500 transaction_t *transaction = NULL; 501 tid_t tid; 502 int need_to_start = 0, ret = 0; 503 504 read_lock(&journal->j_state_lock); 505 if (journal->j_running_transaction && !current->journal_info) { 506 transaction = journal->j_running_transaction; 507 if (!tid_geq(journal->j_commit_request, transaction->t_tid)) 508 need_to_start = 1; 509 } else if (journal->j_committing_transaction) 510 transaction = journal->j_committing_transaction; 511 512 if (!transaction) { 513 /* Nothing to commit */ 514 read_unlock(&journal->j_state_lock); 515 return 0; 516 } 517 tid = transaction->t_tid; 518 read_unlock(&journal->j_state_lock); 519 if (need_to_start) 520 jbd2_log_start_commit(journal, tid); 521 ret = jbd2_log_wait_commit(journal, tid); 522 if (!ret) 523 ret = 1; 524 525 return ret; 526 } 527 528 /** 529 * jbd2_journal_force_commit_nested - Force and wait upon a commit if the 530 * calling process is not within transaction. 531 * 532 * @journal: journal to force 533 * Returns true if progress was made. 534 * 535 * This is used for forcing out undo-protected data which contains 536 * bitmaps, when the fs is running out of space. 537 */ 538 int jbd2_journal_force_commit_nested(journal_t *journal) 539 { 540 int ret; 541 542 ret = __jbd2_journal_force_commit(journal); 543 return ret > 0; 544 } 545 546 /** 547 * jbd2_journal_force_commit() - force any uncommitted transactions 548 * @journal: journal to force 549 * 550 * Caller want unconditional commit. We can only force the running transaction 551 * if we don't have an active handle, otherwise, we will deadlock. 552 */ 553 int jbd2_journal_force_commit(journal_t *journal) 554 { 555 int ret; 556 557 J_ASSERT(!current->journal_info); 558 ret = __jbd2_journal_force_commit(journal); 559 if (ret > 0) 560 ret = 0; 561 return ret; 562 } 563 564 /* 565 * Start a commit of the current running transaction (if any). Returns true 566 * if a transaction is going to be committed (or is currently already 567 * committing), and fills its tid in at *ptid 568 */ 569 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid) 570 { 571 int ret = 0; 572 573 write_lock(&journal->j_state_lock); 574 if (journal->j_running_transaction) { 575 tid_t tid = journal->j_running_transaction->t_tid; 576 577 __jbd2_log_start_commit(journal, tid); 578 /* There's a running transaction and we've just made sure 579 * it's commit has been scheduled. */ 580 if (ptid) 581 *ptid = tid; 582 ret = 1; 583 } else if (journal->j_committing_transaction) { 584 /* 585 * If commit has been started, then we have to wait for 586 * completion of that transaction. 587 */ 588 if (ptid) 589 *ptid = journal->j_committing_transaction->t_tid; 590 ret = 1; 591 } 592 write_unlock(&journal->j_state_lock); 593 return ret; 594 } 595 596 /* 597 * Return 1 if a given transaction has not yet sent barrier request 598 * connected with a transaction commit. If 0 is returned, transaction 599 * may or may not have sent the barrier. Used to avoid sending barrier 600 * twice in common cases. 601 */ 602 int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid) 603 { 604 int ret = 0; 605 transaction_t *commit_trans, *running_trans; 606 607 if (!(journal->j_flags & JBD2_BARRIER)) 608 return 0; 609 read_lock(&journal->j_state_lock); 610 /* Transaction already committed? */ 611 if (tid_geq(journal->j_commit_sequence, tid)) 612 goto out; 613 commit_trans = journal->j_committing_transaction; 614 if (!commit_trans || commit_trans->t_tid != tid) { 615 running_trans = journal->j_running_transaction; 616 /* 617 * The query transaction hasn't started committing, 618 * it must still be running. 619 */ 620 if (WARN_ON_ONCE(!running_trans || 621 running_trans->t_tid != tid)) 622 goto out; 623 624 running_trans->t_need_data_flush = 1; 625 ret = 1; 626 goto out; 627 } 628 /* 629 * Transaction is being committed and we already proceeded to 630 * submitting a flush to fs partition? 631 */ 632 if (journal->j_fs_dev != journal->j_dev) { 633 if (!commit_trans->t_need_data_flush || 634 commit_trans->t_state >= T_COMMIT_DFLUSH) 635 goto out; 636 } else { 637 if (commit_trans->t_state >= T_COMMIT_JFLUSH) 638 goto out; 639 } 640 ret = 1; 641 out: 642 read_unlock(&journal->j_state_lock); 643 return ret; 644 } 645 EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier); 646 647 /* 648 * Wait for a specified commit to complete. 649 * The caller may not hold the journal lock. 650 */ 651 int jbd2_log_wait_commit(journal_t *journal, tid_t tid) 652 { 653 int err = 0; 654 655 read_lock(&journal->j_state_lock); 656 #ifdef CONFIG_PROVE_LOCKING 657 /* 658 * Some callers make sure transaction is already committing and in that 659 * case we cannot block on open handles anymore. So don't warn in that 660 * case. 661 */ 662 if (tid_gt(tid, journal->j_commit_sequence) && 663 (!journal->j_committing_transaction || 664 journal->j_committing_transaction->t_tid != tid)) { 665 read_unlock(&journal->j_state_lock); 666 jbd2_might_wait_for_commit(journal); 667 read_lock(&journal->j_state_lock); 668 } 669 #endif 670 #ifdef CONFIG_JBD2_DEBUG 671 if (!tid_geq(journal->j_commit_request, tid)) { 672 printk(KERN_ERR 673 "%s: error: j_commit_request=%u, tid=%u\n", 674 __func__, journal->j_commit_request, tid); 675 } 676 #endif 677 while (tid_gt(tid, journal->j_commit_sequence)) { 678 jbd2_debug(1, "JBD2: want %u, j_commit_sequence=%u\n", 679 tid, journal->j_commit_sequence); 680 read_unlock(&journal->j_state_lock); 681 wake_up(&journal->j_wait_commit); 682 wait_event(journal->j_wait_done_commit, 683 !tid_gt(tid, journal->j_commit_sequence)); 684 read_lock(&journal->j_state_lock); 685 } 686 read_unlock(&journal->j_state_lock); 687 688 if (unlikely(is_journal_aborted(journal))) 689 err = -EIO; 690 return err; 691 } 692 693 /* 694 * Start a fast commit. If there's an ongoing fast or full commit wait for 695 * it to complete. Returns 0 if a new fast commit was started. Returns -EALREADY 696 * if a fast commit is not needed, either because there's an already a commit 697 * going on or this tid has already been committed. Returns -EINVAL if no jbd2 698 * commit has yet been performed. 699 */ 700 int jbd2_fc_begin_commit(journal_t *journal, tid_t tid) 701 { 702 if (unlikely(is_journal_aborted(journal))) 703 return -EIO; 704 /* 705 * Fast commits only allowed if at least one full commit has 706 * been processed. 707 */ 708 if (!journal->j_stats.ts_tid) 709 return -EINVAL; 710 711 write_lock(&journal->j_state_lock); 712 if (tid_geq(journal->j_commit_sequence, tid)) { 713 write_unlock(&journal->j_state_lock); 714 return -EALREADY; 715 } 716 717 if (journal->j_flags & JBD2_FULL_COMMIT_ONGOING || 718 (journal->j_flags & JBD2_FAST_COMMIT_ONGOING)) { 719 DEFINE_WAIT(wait); 720 721 prepare_to_wait(&journal->j_fc_wait, &wait, 722 TASK_UNINTERRUPTIBLE); 723 write_unlock(&journal->j_state_lock); 724 schedule(); 725 finish_wait(&journal->j_fc_wait, &wait); 726 return -EALREADY; 727 } 728 journal->j_flags |= JBD2_FAST_COMMIT_ONGOING; 729 write_unlock(&journal->j_state_lock); 730 731 return 0; 732 } 733 EXPORT_SYMBOL(jbd2_fc_begin_commit); 734 735 /* 736 * Stop a fast commit. If fallback is set, this function starts commit of 737 * TID tid before any other fast commit can start. 738 */ 739 static int __jbd2_fc_end_commit(journal_t *journal, tid_t tid, bool fallback) 740 { 741 if (journal->j_fc_cleanup_callback) 742 journal->j_fc_cleanup_callback(journal, 0, tid); 743 write_lock(&journal->j_state_lock); 744 journal->j_flags &= ~JBD2_FAST_COMMIT_ONGOING; 745 if (fallback) 746 journal->j_flags |= JBD2_FULL_COMMIT_ONGOING; 747 write_unlock(&journal->j_state_lock); 748 wake_up(&journal->j_fc_wait); 749 if (fallback) 750 return jbd2_complete_transaction(journal, tid); 751 return 0; 752 } 753 754 int jbd2_fc_end_commit(journal_t *journal) 755 { 756 return __jbd2_fc_end_commit(journal, 0, false); 757 } 758 EXPORT_SYMBOL(jbd2_fc_end_commit); 759 760 int jbd2_fc_end_commit_fallback(journal_t *journal) 761 { 762 tid_t tid; 763 764 read_lock(&journal->j_state_lock); 765 tid = journal->j_running_transaction ? 766 journal->j_running_transaction->t_tid : 0; 767 read_unlock(&journal->j_state_lock); 768 return __jbd2_fc_end_commit(journal, tid, true); 769 } 770 EXPORT_SYMBOL(jbd2_fc_end_commit_fallback); 771 772 /* Return 1 when transaction with given tid has already committed. */ 773 int jbd2_transaction_committed(journal_t *journal, tid_t tid) 774 { 775 return tid_geq(READ_ONCE(journal->j_commit_sequence), tid); 776 } 777 EXPORT_SYMBOL(jbd2_transaction_committed); 778 779 /* 780 * When this function returns the transaction corresponding to tid 781 * will be completed. If the transaction has currently running, start 782 * committing that transaction before waiting for it to complete. If 783 * the transaction id is stale, it is by definition already completed, 784 * so just return SUCCESS. 785 */ 786 int jbd2_complete_transaction(journal_t *journal, tid_t tid) 787 { 788 int need_to_wait = 1; 789 790 read_lock(&journal->j_state_lock); 791 if (journal->j_running_transaction && 792 journal->j_running_transaction->t_tid == tid) { 793 if (journal->j_commit_request != tid) { 794 /* transaction not yet started, so request it */ 795 read_unlock(&journal->j_state_lock); 796 jbd2_log_start_commit(journal, tid); 797 goto wait_commit; 798 } 799 } else if (!(journal->j_committing_transaction && 800 journal->j_committing_transaction->t_tid == tid)) 801 need_to_wait = 0; 802 read_unlock(&journal->j_state_lock); 803 if (!need_to_wait) 804 return 0; 805 wait_commit: 806 return jbd2_log_wait_commit(journal, tid); 807 } 808 EXPORT_SYMBOL(jbd2_complete_transaction); 809 810 /* 811 * Log buffer allocation routines: 812 */ 813 814 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp) 815 { 816 unsigned long blocknr; 817 818 write_lock(&journal->j_state_lock); 819 J_ASSERT(journal->j_free > 1); 820 821 blocknr = journal->j_head; 822 journal->j_head++; 823 journal->j_free--; 824 if (journal->j_head == journal->j_last) 825 journal->j_head = journal->j_first; 826 write_unlock(&journal->j_state_lock); 827 return jbd2_journal_bmap(journal, blocknr, retp); 828 } 829 830 /* Map one fast commit buffer for use by the file system */ 831 int jbd2_fc_get_buf(journal_t *journal, struct buffer_head **bh_out) 832 { 833 unsigned long long pblock; 834 unsigned long blocknr; 835 int ret = 0; 836 struct buffer_head *bh; 837 int fc_off; 838 839 *bh_out = NULL; 840 841 if (journal->j_fc_off + journal->j_fc_first >= journal->j_fc_last) 842 return -EINVAL; 843 844 fc_off = journal->j_fc_off; 845 blocknr = journal->j_fc_first + fc_off; 846 journal->j_fc_off++; 847 ret = jbd2_journal_bmap(journal, blocknr, &pblock); 848 if (ret) 849 return ret; 850 851 bh = __getblk(journal->j_dev, pblock, journal->j_blocksize); 852 if (!bh) 853 return -ENOMEM; 854 855 journal->j_fc_wbuf[fc_off] = bh; 856 857 *bh_out = bh; 858 859 return 0; 860 } 861 EXPORT_SYMBOL(jbd2_fc_get_buf); 862 863 /* 864 * Wait on fast commit buffers that were allocated by jbd2_fc_get_buf 865 * for completion. 866 */ 867 int jbd2_fc_wait_bufs(journal_t *journal, int num_blks) 868 { 869 struct buffer_head *bh; 870 int i, j_fc_off; 871 872 j_fc_off = journal->j_fc_off; 873 874 /* 875 * Wait in reverse order to minimize chances of us being woken up before 876 * all IOs have completed 877 */ 878 for (i = j_fc_off - 1; i >= j_fc_off - num_blks; i--) { 879 bh = journal->j_fc_wbuf[i]; 880 wait_on_buffer(bh); 881 /* 882 * Update j_fc_off so jbd2_fc_release_bufs can release remain 883 * buffer head. 884 */ 885 if (unlikely(!buffer_uptodate(bh))) { 886 journal->j_fc_off = i + 1; 887 return -EIO; 888 } 889 put_bh(bh); 890 journal->j_fc_wbuf[i] = NULL; 891 } 892 893 return 0; 894 } 895 EXPORT_SYMBOL(jbd2_fc_wait_bufs); 896 897 void jbd2_fc_release_bufs(journal_t *journal) 898 { 899 struct buffer_head *bh; 900 int i, j_fc_off; 901 902 j_fc_off = journal->j_fc_off; 903 904 for (i = j_fc_off - 1; i >= 0; i--) { 905 bh = journal->j_fc_wbuf[i]; 906 if (!bh) 907 break; 908 put_bh(bh); 909 journal->j_fc_wbuf[i] = NULL; 910 } 911 } 912 EXPORT_SYMBOL(jbd2_fc_release_bufs); 913 914 /* 915 * Conversion of logical to physical block numbers for the journal 916 * 917 * On external journals the journal blocks are identity-mapped, so 918 * this is a no-op. If needed, we can use j_blk_offset - everything is 919 * ready. 920 */ 921 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr, 922 unsigned long long *retp) 923 { 924 int err = 0; 925 unsigned long long ret; 926 sector_t block = blocknr; 927 928 if (journal->j_bmap) { 929 err = journal->j_bmap(journal, &block); 930 if (err == 0) 931 *retp = block; 932 } else if (journal->j_inode) { 933 ret = bmap(journal->j_inode, &block); 934 935 if (ret || !block) { 936 printk(KERN_ALERT "%s: journal block not found " 937 "at offset %lu on %s\n", 938 __func__, blocknr, journal->j_devname); 939 jbd2_journal_abort(journal, ret ? ret : -EFSCORRUPTED); 940 err = -EIO; 941 } else { 942 *retp = block; 943 } 944 945 } else { 946 *retp = blocknr; /* +journal->j_blk_offset */ 947 } 948 return err; 949 } 950 951 /* 952 * We play buffer_head aliasing tricks to write data/metadata blocks to 953 * the journal without copying their contents, but for journal 954 * descriptor blocks we do need to generate bona fide buffers. 955 * 956 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying 957 * the buffer's contents they really should run flush_dcache_folio(bh->b_folio). 958 * But we don't bother doing that, so there will be coherency problems with 959 * mmaps of blockdevs which hold live JBD-controlled filesystems. 960 */ 961 struct buffer_head * 962 jbd2_journal_get_descriptor_buffer(transaction_t *transaction, int type) 963 { 964 journal_t *journal = transaction->t_journal; 965 struct buffer_head *bh; 966 unsigned long long blocknr; 967 journal_header_t *header; 968 int err; 969 970 err = jbd2_journal_next_log_block(journal, &blocknr); 971 972 if (err) 973 return NULL; 974 975 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize); 976 if (!bh) 977 return NULL; 978 atomic_dec(&transaction->t_outstanding_credits); 979 lock_buffer(bh); 980 memset(bh->b_data, 0, journal->j_blocksize); 981 header = (journal_header_t *)bh->b_data; 982 header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER); 983 header->h_blocktype = cpu_to_be32(type); 984 header->h_sequence = cpu_to_be32(transaction->t_tid); 985 set_buffer_uptodate(bh); 986 unlock_buffer(bh); 987 BUFFER_TRACE(bh, "return this buffer"); 988 return bh; 989 } 990 991 void jbd2_descriptor_block_csum_set(journal_t *j, struct buffer_head *bh) 992 { 993 struct jbd2_journal_block_tail *tail; 994 __u32 csum; 995 996 if (!jbd2_journal_has_csum_v2or3(j)) 997 return; 998 999 tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize - 1000 sizeof(struct jbd2_journal_block_tail)); 1001 tail->t_checksum = 0; 1002 csum = jbd2_chksum(j->j_csum_seed, bh->b_data, j->j_blocksize); 1003 tail->t_checksum = cpu_to_be32(csum); 1004 } 1005 1006 /* 1007 * Return tid of the oldest transaction in the journal and block in the journal 1008 * where the transaction starts. 1009 * 1010 * If the journal is now empty, return which will be the next transaction ID 1011 * we will write and where will that transaction start. 1012 * 1013 * The return value is 0 if journal tail cannot be pushed any further, 1 if 1014 * it can. 1015 */ 1016 int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid, 1017 unsigned long *block) 1018 { 1019 transaction_t *transaction; 1020 int ret; 1021 1022 read_lock(&journal->j_state_lock); 1023 spin_lock(&journal->j_list_lock); 1024 transaction = journal->j_checkpoint_transactions; 1025 if (transaction) { 1026 *tid = transaction->t_tid; 1027 *block = transaction->t_log_start; 1028 } else if ((transaction = journal->j_committing_transaction) != NULL) { 1029 *tid = transaction->t_tid; 1030 *block = transaction->t_log_start; 1031 } else if ((transaction = journal->j_running_transaction) != NULL) { 1032 *tid = transaction->t_tid; 1033 *block = journal->j_head; 1034 } else { 1035 *tid = journal->j_transaction_sequence; 1036 *block = journal->j_head; 1037 } 1038 ret = tid_gt(*tid, journal->j_tail_sequence); 1039 spin_unlock(&journal->j_list_lock); 1040 read_unlock(&journal->j_state_lock); 1041 1042 return ret; 1043 } 1044 1045 /* 1046 * Update information in journal structure and in on disk journal superblock 1047 * about log tail. This function does not check whether information passed in 1048 * really pushes log tail further. It's responsibility of the caller to make 1049 * sure provided log tail information is valid (e.g. by holding 1050 * j_checkpoint_mutex all the time between computing log tail and calling this 1051 * function as is the case with jbd2_cleanup_journal_tail()). 1052 * 1053 * Requires j_checkpoint_mutex 1054 */ 1055 int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block) 1056 { 1057 unsigned long freed; 1058 int ret; 1059 1060 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 1061 1062 /* 1063 * We cannot afford for write to remain in drive's caches since as 1064 * soon as we update j_tail, next transaction can start reusing journal 1065 * space and if we lose sb update during power failure we'd replay 1066 * old transaction with possibly newly overwritten data. 1067 */ 1068 ret = jbd2_journal_update_sb_log_tail(journal, tid, block, REQ_FUA); 1069 if (ret) 1070 goto out; 1071 1072 write_lock(&journal->j_state_lock); 1073 freed = block - journal->j_tail; 1074 if (block < journal->j_tail) 1075 freed += journal->j_last - journal->j_first; 1076 1077 trace_jbd2_update_log_tail(journal, tid, block, freed); 1078 jbd2_debug(1, 1079 "Cleaning journal tail from %u to %u (offset %lu), " 1080 "freeing %lu\n", 1081 journal->j_tail_sequence, tid, block, freed); 1082 1083 journal->j_free += freed; 1084 journal->j_tail_sequence = tid; 1085 journal->j_tail = block; 1086 write_unlock(&journal->j_state_lock); 1087 1088 out: 1089 return ret; 1090 } 1091 1092 /* 1093 * This is a variation of __jbd2_update_log_tail which checks for validity of 1094 * provided log tail and locks j_checkpoint_mutex. So it is safe against races 1095 * with other threads updating log tail. 1096 */ 1097 void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block) 1098 { 1099 mutex_lock_io(&journal->j_checkpoint_mutex); 1100 if (tid_gt(tid, journal->j_tail_sequence)) 1101 __jbd2_update_log_tail(journal, tid, block); 1102 mutex_unlock(&journal->j_checkpoint_mutex); 1103 } 1104 1105 struct jbd2_stats_proc_session { 1106 journal_t *journal; 1107 struct transaction_stats_s *stats; 1108 int start; 1109 int max; 1110 }; 1111 1112 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos) 1113 { 1114 return *pos ? NULL : SEQ_START_TOKEN; 1115 } 1116 1117 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos) 1118 { 1119 (*pos)++; 1120 return NULL; 1121 } 1122 1123 static int jbd2_seq_info_show(struct seq_file *seq, void *v) 1124 { 1125 struct jbd2_stats_proc_session *s = seq->private; 1126 1127 if (v != SEQ_START_TOKEN) 1128 return 0; 1129 seq_printf(seq, "%lu transactions (%lu requested), " 1130 "each up to %u blocks\n", 1131 s->stats->ts_tid, s->stats->ts_requested, 1132 s->journal->j_max_transaction_buffers); 1133 if (s->stats->ts_tid == 0) 1134 return 0; 1135 seq_printf(seq, "average: \n %ums waiting for transaction\n", 1136 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid)); 1137 seq_printf(seq, " %ums request delay\n", 1138 (s->stats->ts_requested == 0) ? 0 : 1139 jiffies_to_msecs(s->stats->run.rs_request_delay / 1140 s->stats->ts_requested)); 1141 seq_printf(seq, " %ums running transaction\n", 1142 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid)); 1143 seq_printf(seq, " %ums transaction was being locked\n", 1144 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid)); 1145 seq_printf(seq, " %ums flushing data (in ordered mode)\n", 1146 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid)); 1147 seq_printf(seq, " %ums logging transaction\n", 1148 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid)); 1149 seq_printf(seq, " %lluus average transaction commit time\n", 1150 div_u64(s->journal->j_average_commit_time, 1000)); 1151 seq_printf(seq, " %lu handles per transaction\n", 1152 s->stats->run.rs_handle_count / s->stats->ts_tid); 1153 seq_printf(seq, " %lu blocks per transaction\n", 1154 s->stats->run.rs_blocks / s->stats->ts_tid); 1155 seq_printf(seq, " %lu logged blocks per transaction\n", 1156 s->stats->run.rs_blocks_logged / s->stats->ts_tid); 1157 return 0; 1158 } 1159 1160 static void jbd2_seq_info_stop(struct seq_file *seq, void *v) 1161 { 1162 } 1163 1164 static const struct seq_operations jbd2_seq_info_ops = { 1165 .start = jbd2_seq_info_start, 1166 .next = jbd2_seq_info_next, 1167 .stop = jbd2_seq_info_stop, 1168 .show = jbd2_seq_info_show, 1169 }; 1170 1171 static int jbd2_seq_info_open(struct inode *inode, struct file *file) 1172 { 1173 journal_t *journal = pde_data(inode); 1174 struct jbd2_stats_proc_session *s; 1175 int rc, size; 1176 1177 s = kmalloc_obj(*s); 1178 if (s == NULL) 1179 return -ENOMEM; 1180 size = sizeof(struct transaction_stats_s); 1181 s->stats = kmalloc(size, GFP_KERNEL); 1182 if (s->stats == NULL) { 1183 kfree(s); 1184 return -ENOMEM; 1185 } 1186 spin_lock(&journal->j_history_lock); 1187 memcpy(s->stats, &journal->j_stats, size); 1188 s->journal = journal; 1189 spin_unlock(&journal->j_history_lock); 1190 1191 rc = seq_open(file, &jbd2_seq_info_ops); 1192 if (rc == 0) { 1193 struct seq_file *m = file->private_data; 1194 m->private = s; 1195 } else { 1196 kfree(s->stats); 1197 kfree(s); 1198 } 1199 return rc; 1200 1201 } 1202 1203 static int jbd2_seq_info_release(struct inode *inode, struct file *file) 1204 { 1205 struct seq_file *seq = file->private_data; 1206 struct jbd2_stats_proc_session *s = seq->private; 1207 kfree(s->stats); 1208 kfree(s); 1209 return seq_release(inode, file); 1210 } 1211 1212 static const struct proc_ops jbd2_info_proc_ops = { 1213 .proc_open = jbd2_seq_info_open, 1214 .proc_read = seq_read, 1215 .proc_lseek = seq_lseek, 1216 .proc_release = jbd2_seq_info_release, 1217 }; 1218 1219 static struct proc_dir_entry *proc_jbd2_stats; 1220 1221 static void jbd2_stats_proc_init(journal_t *journal) 1222 { 1223 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats); 1224 if (journal->j_proc_entry) { 1225 proc_create_data("info", S_IRUGO, journal->j_proc_entry, 1226 &jbd2_info_proc_ops, journal); 1227 } 1228 } 1229 1230 static void jbd2_stats_proc_exit(journal_t *journal) 1231 { 1232 remove_proc_entry("info", journal->j_proc_entry); 1233 remove_proc_entry(journal->j_devname, proc_jbd2_stats); 1234 } 1235 1236 /* Minimum size of descriptor tag */ 1237 static int jbd2_min_tag_size(void) 1238 { 1239 /* 1240 * Tag with 32-bit block numbers does not use last four bytes of the 1241 * structure 1242 */ 1243 return sizeof(journal_block_tag_t) - 4; 1244 } 1245 1246 /** 1247 * jbd2_journal_shrink_scan() 1248 * @shrink: shrinker to work on 1249 * @sc: reclaim request to process 1250 * 1251 * Scan the checkpointed buffer on the checkpoint list and release the 1252 * journal_head. 1253 */ 1254 static unsigned long jbd2_journal_shrink_scan(struct shrinker *shrink, 1255 struct shrink_control *sc) 1256 { 1257 journal_t *journal = shrink->private_data; 1258 unsigned long nr_to_scan = sc->nr_to_scan; 1259 unsigned long nr_shrunk; 1260 unsigned long count; 1261 1262 count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count); 1263 trace_jbd2_shrink_scan_enter(journal, sc->nr_to_scan, count); 1264 1265 nr_shrunk = jbd2_journal_shrink_checkpoint_list(journal, &nr_to_scan); 1266 1267 count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count); 1268 trace_jbd2_shrink_scan_exit(journal, nr_to_scan, nr_shrunk, count); 1269 1270 return nr_shrunk; 1271 } 1272 1273 /** 1274 * jbd2_journal_shrink_count() 1275 * @shrink: shrinker to work on 1276 * @sc: reclaim request to process 1277 * 1278 * Count the number of checkpoint buffers on the checkpoint list. 1279 */ 1280 static unsigned long jbd2_journal_shrink_count(struct shrinker *shrink, 1281 struct shrink_control *sc) 1282 { 1283 journal_t *journal = shrink->private_data; 1284 unsigned long count; 1285 1286 count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count); 1287 trace_jbd2_shrink_count(journal, sc->nr_to_scan, count); 1288 1289 return count; 1290 } 1291 1292 /* 1293 * If the journal init or create aborts, we need to mark the journal 1294 * superblock as being NULL to prevent the journal destroy from writing 1295 * back a bogus superblock. 1296 */ 1297 static void journal_fail_superblock(journal_t *journal) 1298 { 1299 struct buffer_head *bh = journal->j_sb_buffer; 1300 brelse(bh); 1301 journal->j_sb_buffer = NULL; 1302 } 1303 1304 /* 1305 * Check the superblock for a given journal, performing initial 1306 * validation of the format. 1307 */ 1308 static int journal_check_superblock(journal_t *journal) 1309 { 1310 journal_superblock_t *sb = journal->j_superblock; 1311 int num_fc_blks; 1312 int err = -EINVAL; 1313 1314 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) || 1315 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) { 1316 printk(KERN_WARNING "JBD2: no valid journal superblock found\n"); 1317 return err; 1318 } 1319 1320 if (be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V1 && 1321 be32_to_cpu(sb->s_header.h_blocktype) != JBD2_SUPERBLOCK_V2) { 1322 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n"); 1323 return err; 1324 } 1325 1326 if (be32_to_cpu(sb->s_maxlen) > journal->j_total_len) { 1327 printk(KERN_WARNING "JBD2: journal file too short\n"); 1328 return err; 1329 } 1330 1331 if (be32_to_cpu(sb->s_first) == 0 || 1332 be32_to_cpu(sb->s_first) >= journal->j_total_len) { 1333 printk(KERN_WARNING 1334 "JBD2: Invalid start block of journal: %u\n", 1335 be32_to_cpu(sb->s_first)); 1336 return err; 1337 } 1338 1339 /* 1340 * If this is a V2 superblock, then we have to check the 1341 * features flags on it. 1342 */ 1343 if (!jbd2_format_support_feature(journal)) 1344 return 0; 1345 1346 if ((sb->s_feature_ro_compat & 1347 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) || 1348 (sb->s_feature_incompat & 1349 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) { 1350 printk(KERN_WARNING "JBD2: Unrecognised features on journal\n"); 1351 return err; 1352 } 1353 1354 num_fc_blks = jbd2_has_feature_fast_commit(journal) ? 1355 jbd2_journal_get_num_fc_blks(sb) : 0; 1356 if (be32_to_cpu(sb->s_maxlen) < JBD2_MIN_JOURNAL_BLOCKS || 1357 be32_to_cpu(sb->s_maxlen) - JBD2_MIN_JOURNAL_BLOCKS < num_fc_blks) { 1358 printk(KERN_ERR "JBD2: journal file too short %u,%d\n", 1359 be32_to_cpu(sb->s_maxlen), num_fc_blks); 1360 return err; 1361 } 1362 1363 if (jbd2_has_feature_csum2(journal) && 1364 jbd2_has_feature_csum3(journal)) { 1365 /* Can't have checksum v2 and v3 at the same time! */ 1366 printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 " 1367 "at the same time!\n"); 1368 return err; 1369 } 1370 1371 if (jbd2_journal_has_csum_v2or3(journal) && 1372 jbd2_has_feature_checksum(journal)) { 1373 /* Can't have checksum v1 and v2 on at the same time! */ 1374 printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 " 1375 "at the same time!\n"); 1376 return err; 1377 } 1378 1379 if (jbd2_journal_has_csum_v2or3(journal)) { 1380 if (sb->s_checksum_type != JBD2_CRC32C_CHKSUM) { 1381 printk(KERN_ERR "JBD2: Unknown checksum type\n"); 1382 return err; 1383 } 1384 1385 /* Check superblock checksum */ 1386 if (sb->s_checksum != jbd2_superblock_csum(sb)) { 1387 printk(KERN_ERR "JBD2: journal checksum error\n"); 1388 err = -EFSBADCRC; 1389 return err; 1390 } 1391 } 1392 1393 return 0; 1394 } 1395 1396 static int journal_revoke_records_per_block(journal_t *journal) 1397 { 1398 int record_size; 1399 int space = journal->j_blocksize - sizeof(jbd2_journal_revoke_header_t); 1400 1401 if (jbd2_has_feature_64bit(journal)) 1402 record_size = 8; 1403 else 1404 record_size = 4; 1405 1406 if (jbd2_journal_has_csum_v2or3(journal)) 1407 space -= sizeof(struct jbd2_journal_block_tail); 1408 return space / record_size; 1409 } 1410 1411 static int jbd2_journal_get_max_txn_bufs(journal_t *journal) 1412 { 1413 return (journal->j_total_len - journal->j_fc_wbufsize) / 3; 1414 } 1415 1416 /* 1417 * Base amount of descriptor blocks we reserve for each transaction. 1418 */ 1419 static int jbd2_descriptor_blocks_per_trans(journal_t *journal) 1420 { 1421 int tag_space = journal->j_blocksize - sizeof(journal_header_t); 1422 int tags_per_block; 1423 1424 /* Subtract UUID */ 1425 tag_space -= 16; 1426 if (jbd2_journal_has_csum_v2or3(journal)) 1427 tag_space -= sizeof(struct jbd2_journal_block_tail); 1428 /* Commit code leaves a slack space of 16 bytes at the end of block */ 1429 tags_per_block = (tag_space - 16) / journal_tag_bytes(journal); 1430 /* 1431 * Revoke descriptors are accounted separately so we need to reserve 1432 * space for commit block and normal transaction descriptor blocks. 1433 */ 1434 return 1 + DIV_ROUND_UP(jbd2_journal_get_max_txn_bufs(journal), 1435 tags_per_block); 1436 } 1437 1438 /* 1439 * Initialize number of blocks each transaction reserves for its bookkeeping 1440 * and maximum number of blocks a transaction can use. This needs to be called 1441 * after the journal size and the fastcommit area size are initialized. 1442 */ 1443 static void jbd2_journal_init_transaction_limits(journal_t *journal) 1444 { 1445 journal->j_revoke_records_per_block = 1446 journal_revoke_records_per_block(journal); 1447 journal->j_transaction_overhead_buffers = 1448 jbd2_descriptor_blocks_per_trans(journal); 1449 journal->j_max_transaction_buffers = 1450 jbd2_journal_get_max_txn_bufs(journal); 1451 } 1452 1453 /* 1454 * Load the on-disk journal superblock and read the key fields into the 1455 * journal_t. 1456 */ 1457 static int journal_load_superblock(journal_t *journal) 1458 { 1459 int err; 1460 struct buffer_head *bh; 1461 journal_superblock_t *sb; 1462 1463 bh = getblk_unmovable(journal->j_dev, journal->j_blk_offset, 1464 journal->j_blocksize); 1465 if (bh) 1466 err = bh_read(bh, 0); 1467 if (!bh || err < 0) { 1468 pr_err("%s: Cannot read journal superblock\n", __func__); 1469 brelse(bh); 1470 return -EIO; 1471 } 1472 1473 journal->j_sb_buffer = bh; 1474 sb = (journal_superblock_t *)bh->b_data; 1475 journal->j_superblock = sb; 1476 err = journal_check_superblock(journal); 1477 if (err) { 1478 journal_fail_superblock(journal); 1479 return err; 1480 } 1481 1482 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence); 1483 journal->j_tail = be32_to_cpu(sb->s_start); 1484 journal->j_first = be32_to_cpu(sb->s_first); 1485 journal->j_errno = be32_to_cpu(sb->s_errno); 1486 journal->j_last = be32_to_cpu(sb->s_maxlen); 1487 1488 if (be32_to_cpu(sb->s_maxlen) < journal->j_total_len) 1489 journal->j_total_len = be32_to_cpu(sb->s_maxlen); 1490 /* Precompute checksum seed for all metadata */ 1491 if (jbd2_journal_has_csum_v2or3(journal)) 1492 journal->j_csum_seed = jbd2_chksum(~0, sb->s_uuid, 1493 sizeof(sb->s_uuid)); 1494 /* After journal features are set, we can compute transaction limits */ 1495 jbd2_journal_init_transaction_limits(journal); 1496 1497 if (jbd2_has_feature_fast_commit(journal)) { 1498 journal->j_fc_last = be32_to_cpu(sb->s_maxlen); 1499 journal->j_last = journal->j_fc_last - 1500 jbd2_journal_get_num_fc_blks(sb); 1501 journal->j_fc_first = journal->j_last + 1; 1502 journal->j_fc_off = 0; 1503 } 1504 1505 return 0; 1506 } 1507 1508 1509 /* 1510 * Management for journal control blocks: functions to create and 1511 * destroy journal_t structures, and to initialise and read existing 1512 * journal blocks from disk. */ 1513 1514 /* The journal_init_common() function creates and fills a journal_t object 1515 * in memory. It calls journal_load_superblock() to load the on-disk journal 1516 * superblock and initialize the journal_t object. 1517 */ 1518 1519 static journal_t *journal_init_common(struct block_device *bdev, 1520 struct block_device *fs_dev, 1521 unsigned long long start, int len, int blocksize) 1522 { 1523 journal_t *journal; 1524 int err; 1525 int n; 1526 1527 journal = kzalloc_obj(*journal); 1528 if (!journal) 1529 return ERR_PTR(-ENOMEM); 1530 1531 lockdep_register_key(&journal->jbd2_trans_commit_key); 1532 journal->j_blocksize = blocksize; 1533 journal->j_dev = bdev; 1534 journal->j_fs_dev = fs_dev; 1535 journal->j_blk_offset = start; 1536 journal->j_total_len = len; 1537 jbd2_init_fs_dev_write_error(journal); 1538 1539 err = journal_load_superblock(journal); 1540 if (err) 1541 goto err_cleanup; 1542 1543 init_waitqueue_head(&journal->j_wait_transaction_locked); 1544 init_waitqueue_head(&journal->j_wait_done_commit); 1545 init_waitqueue_head(&journal->j_wait_commit); 1546 init_waitqueue_head(&journal->j_wait_updates); 1547 init_waitqueue_head(&journal->j_wait_reserved); 1548 init_waitqueue_head(&journal->j_fc_wait); 1549 mutex_init(&journal->j_abort_mutex); 1550 mutex_init(&journal->j_barrier); 1551 mutex_init(&journal->j_checkpoint_mutex); 1552 spin_lock_init(&journal->j_revoke_lock); 1553 spin_lock_init(&journal->j_list_lock); 1554 spin_lock_init(&journal->j_history_lock); 1555 rwlock_init(&journal->j_state_lock); 1556 1557 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE); 1558 journal->j_min_batch_time = 0; 1559 journal->j_max_batch_time = 15000; /* 15ms */ 1560 atomic_set(&journal->j_reserved_credits, 0); 1561 lockdep_init_map(&journal->j_trans_commit_map, "jbd2_handle", 1562 &journal->jbd2_trans_commit_key, 0); 1563 1564 /* The journal is marked for error until we succeed with recovery! */ 1565 journal->j_flags = JBD2_ABORT; 1566 1567 /* Set up a default-sized revoke table for the new mount. */ 1568 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH); 1569 if (err) 1570 goto err_cleanup; 1571 1572 /* 1573 * journal descriptor can store up to n blocks, we need enough 1574 * buffers to write out full descriptor block. 1575 */ 1576 err = -ENOMEM; 1577 n = journal->j_blocksize / jbd2_min_tag_size(); 1578 journal->j_wbufsize = n; 1579 journal->j_fc_wbuf = NULL; 1580 journal->j_wbuf = kmalloc_objs(struct buffer_head *, n); 1581 if (!journal->j_wbuf) 1582 goto err_cleanup; 1583 1584 err = percpu_counter_init(&journal->j_checkpoint_jh_count, 0, 1585 GFP_KERNEL); 1586 if (err) 1587 goto err_cleanup; 1588 1589 journal->j_shrink_transaction = NULL; 1590 1591 journal->j_shrinker = shrinker_alloc(0, "jbd2-journal:(%u:%u)", 1592 MAJOR(bdev->bd_dev), 1593 MINOR(bdev->bd_dev)); 1594 if (!journal->j_shrinker) { 1595 err = -ENOMEM; 1596 goto err_cleanup; 1597 } 1598 1599 journal->j_shrinker->scan_objects = jbd2_journal_shrink_scan; 1600 journal->j_shrinker->count_objects = jbd2_journal_shrink_count; 1601 journal->j_shrinker->private_data = journal; 1602 1603 shrinker_register(journal->j_shrinker); 1604 1605 return journal; 1606 1607 err_cleanup: 1608 percpu_counter_destroy(&journal->j_checkpoint_jh_count); 1609 kfree(journal->j_wbuf); 1610 jbd2_journal_destroy_revoke(journal); 1611 journal_fail_superblock(journal); 1612 lockdep_unregister_key(&journal->jbd2_trans_commit_key); 1613 kfree(journal); 1614 return ERR_PTR(err); 1615 } 1616 1617 /* jbd2_journal_init_dev and jbd2_journal_init_inode: 1618 * 1619 * Create a journal structure assigned some fixed set of disk blocks to 1620 * the journal. We don't actually touch those disk blocks yet, but we 1621 * need to set up all of the mapping information to tell the journaling 1622 * system where the journal blocks are. 1623 * 1624 */ 1625 1626 /** 1627 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure 1628 * @bdev: Block device on which to create the journal 1629 * @fs_dev: Device which hold journalled filesystem for this journal. 1630 * @start: Block nr Start of journal. 1631 * @len: Length of the journal in blocks. 1632 * @blocksize: blocksize of journalling device 1633 * 1634 * Returns: a newly created journal_t * 1635 * 1636 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous 1637 * range of blocks on an arbitrary block device. 1638 * 1639 */ 1640 journal_t *jbd2_journal_init_dev(struct block_device *bdev, 1641 struct block_device *fs_dev, 1642 unsigned long long start, int len, int blocksize) 1643 { 1644 journal_t *journal; 1645 1646 journal = journal_init_common(bdev, fs_dev, start, len, blocksize); 1647 if (IS_ERR(journal)) 1648 return ERR_CAST(journal); 1649 1650 snprintf(journal->j_devname, sizeof(journal->j_devname), 1651 "%pg", journal->j_dev); 1652 strreplace(journal->j_devname, '/', '!'); 1653 jbd2_stats_proc_init(journal); 1654 1655 return journal; 1656 } 1657 1658 /** 1659 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode. 1660 * @inode: An inode to create the journal in 1661 * 1662 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as 1663 * the journal. The inode must exist already, must support bmap() and 1664 * must have all data blocks preallocated. 1665 */ 1666 journal_t *jbd2_journal_init_inode(struct inode *inode) 1667 { 1668 journal_t *journal; 1669 sector_t blocknr; 1670 int err = 0; 1671 1672 blocknr = 0; 1673 err = bmap(inode, &blocknr); 1674 if (err || !blocknr) { 1675 pr_err("%s: Cannot locate journal superblock\n", __func__); 1676 return err ? ERR_PTR(err) : ERR_PTR(-EINVAL); 1677 } 1678 1679 jbd2_debug(1, "JBD2: inode %s/%llu, size %lld, bits %d, blksize %ld\n", 1680 inode->i_sb->s_id, inode->i_ino, (long long) inode->i_size, 1681 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize); 1682 1683 journal = journal_init_common(inode->i_sb->s_bdev, inode->i_sb->s_bdev, 1684 blocknr, inode->i_size >> inode->i_sb->s_blocksize_bits, 1685 inode->i_sb->s_blocksize); 1686 if (IS_ERR(journal)) 1687 return ERR_CAST(journal); 1688 1689 journal->j_inode = inode; 1690 snprintf(journal->j_devname, sizeof(journal->j_devname), 1691 "%pg-%llu", journal->j_dev, journal->j_inode->i_ino); 1692 strreplace(journal->j_devname, '/', '!'); 1693 jbd2_stats_proc_init(journal); 1694 1695 return journal; 1696 } 1697 1698 /* 1699 * Given a journal_t structure, initialise the various fields for 1700 * startup of a new journaling session. We use this both when creating 1701 * a journal, and after recovering an old journal to reset it for 1702 * subsequent use. 1703 */ 1704 1705 static int journal_reset(journal_t *journal) 1706 { 1707 journal_superblock_t *sb = journal->j_superblock; 1708 unsigned long long first, last; 1709 1710 first = be32_to_cpu(sb->s_first); 1711 last = be32_to_cpu(sb->s_maxlen); 1712 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) { 1713 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n", 1714 first, last); 1715 journal_fail_superblock(journal); 1716 return -EINVAL; 1717 } 1718 1719 journal->j_first = first; 1720 journal->j_last = last; 1721 1722 if (journal->j_head != 0 && journal->j_flags & JBD2_CYCLE_RECORD) { 1723 /* 1724 * Disable the cycled recording mode if the journal head block 1725 * number is not correct. 1726 */ 1727 if (journal->j_head < first || journal->j_head >= last) { 1728 printk(KERN_WARNING "JBD2: Incorrect Journal head block %lu, " 1729 "disable journal_cycle_record\n", 1730 journal->j_head); 1731 journal->j_head = journal->j_first; 1732 } 1733 } else { 1734 journal->j_head = journal->j_first; 1735 } 1736 journal->j_tail = journal->j_head; 1737 journal->j_free = journal->j_last - journal->j_first; 1738 1739 journal->j_tail_sequence = journal->j_transaction_sequence; 1740 journal->j_commit_sequence = journal->j_transaction_sequence - 1; 1741 journal->j_commit_request = journal->j_commit_sequence; 1742 1743 /* 1744 * Now that journal recovery is done, turn fast commits off here. This 1745 * way, if fast commit was enabled before the crash but if now FS has 1746 * disabled it, we don't enable fast commits. 1747 */ 1748 jbd2_clear_feature_fast_commit(journal); 1749 1750 /* 1751 * As a special case, if the on-disk copy is already marked as needing 1752 * no recovery (s_start == 0), then we can safely defer the superblock 1753 * update until the next commit by setting JBD2_FLUSHED. This avoids 1754 * attempting a write to a potential-readonly device. 1755 */ 1756 if (sb->s_start == 0) { 1757 jbd2_debug(1, "JBD2: Skipping superblock update on recovered sb " 1758 "(start %ld, seq %u, errno %d)\n", 1759 journal->j_tail, journal->j_tail_sequence, 1760 journal->j_errno); 1761 journal->j_flags |= JBD2_FLUSHED; 1762 } else { 1763 /* Lock here to make assertions happy... */ 1764 mutex_lock_io(&journal->j_checkpoint_mutex); 1765 /* 1766 * Update log tail information. We use REQ_FUA since new 1767 * transaction will start reusing journal space and so we 1768 * must make sure information about current log tail is on 1769 * disk before that. 1770 */ 1771 jbd2_journal_update_sb_log_tail(journal, 1772 journal->j_tail_sequence, 1773 journal->j_tail, REQ_FUA); 1774 mutex_unlock(&journal->j_checkpoint_mutex); 1775 } 1776 return jbd2_journal_start_thread(journal); 1777 } 1778 1779 /* 1780 * This function expects that the caller will have locked the journal 1781 * buffer head, and will return with it unlocked 1782 */ 1783 static int jbd2_write_superblock(journal_t *journal, blk_opf_t write_flags) 1784 { 1785 struct buffer_head *bh = journal->j_sb_buffer; 1786 journal_superblock_t *sb = journal->j_superblock; 1787 int ret = 0; 1788 1789 /* Buffer got discarded which means block device got invalidated */ 1790 if (!buffer_mapped(bh)) { 1791 unlock_buffer(bh); 1792 return -EIO; 1793 } 1794 1795 /* 1796 * Always set high priority flags to exempt from block layer's 1797 * QOS policies, e.g. writeback throttle. 1798 */ 1799 write_flags |= JBD2_JOURNAL_REQ_FLAGS; 1800 if (!(journal->j_flags & JBD2_BARRIER)) 1801 write_flags &= ~(REQ_FUA | REQ_PREFLUSH); 1802 1803 trace_jbd2_write_superblock(journal, write_flags); 1804 1805 if (buffer_write_io_error(bh)) { 1806 /* 1807 * Oh, dear. A previous attempt to write the journal 1808 * superblock failed. This could happen because the 1809 * USB device was yanked out. Or it could happen to 1810 * be a transient write error and maybe the block will 1811 * be remapped. Nothing we can do but to retry the 1812 * write and hope for the best. 1813 */ 1814 printk(KERN_ERR "JBD2: previous I/O error detected " 1815 "for journal superblock update for %s.\n", 1816 journal->j_devname); 1817 clear_buffer_write_io_error(bh); 1818 set_buffer_uptodate(bh); 1819 } 1820 if (jbd2_journal_has_csum_v2or3(journal)) 1821 sb->s_checksum = jbd2_superblock_csum(sb); 1822 bh_submit(bh, REQ_OP_WRITE | write_flags, bh_end_write); 1823 wait_on_buffer(bh); 1824 if (buffer_write_io_error(bh)) { 1825 clear_buffer_write_io_error(bh); 1826 set_buffer_uptodate(bh); 1827 ret = -EIO; 1828 } 1829 if (ret) { 1830 printk(KERN_ERR "JBD2: I/O error when updating journal superblock for %s.\n", 1831 journal->j_devname); 1832 if (!is_journal_aborted(journal)) 1833 jbd2_journal_abort(journal, ret); 1834 } 1835 1836 return ret; 1837 } 1838 1839 /** 1840 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk. 1841 * @journal: The journal to update. 1842 * @tail_tid: TID of the new transaction at the tail of the log 1843 * @tail_block: The first block of the transaction at the tail of the log 1844 * @write_flags: Flags for the journal sb write operation 1845 * 1846 * Update a journal's superblock information about log tail and write it to 1847 * disk, waiting for the IO to complete. 1848 */ 1849 int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid, 1850 unsigned long tail_block, 1851 blk_opf_t write_flags) 1852 { 1853 journal_superblock_t *sb = journal->j_superblock; 1854 int ret; 1855 1856 if (is_journal_aborted(journal)) 1857 return -EIO; 1858 ret = jbd2_check_fs_dev_write_error(journal); 1859 if (ret) { 1860 jbd2_journal_abort(journal, ret); 1861 return -EIO; 1862 } 1863 1864 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 1865 jbd2_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n", 1866 tail_block, tail_tid); 1867 1868 lock_buffer(journal->j_sb_buffer); 1869 sb->s_sequence = cpu_to_be32(tail_tid); 1870 sb->s_start = cpu_to_be32(tail_block); 1871 1872 ret = jbd2_write_superblock(journal, write_flags); 1873 if (ret) 1874 goto out; 1875 1876 /* Log is no longer empty */ 1877 write_lock(&journal->j_state_lock); 1878 journal->j_flags &= ~JBD2_FLUSHED; 1879 write_unlock(&journal->j_state_lock); 1880 1881 out: 1882 return ret; 1883 } 1884 1885 /** 1886 * jbd2_mark_journal_empty() - Mark on disk journal as empty. 1887 * @journal: The journal to update. 1888 * @write_flags: Flags for the journal sb write operation 1889 * 1890 * Update a journal's dynamic superblock fields to show that journal is empty. 1891 * Write updated superblock to disk waiting for IO to complete. 1892 */ 1893 static void jbd2_mark_journal_empty(journal_t *journal, blk_opf_t write_flags) 1894 { 1895 journal_superblock_t *sb = journal->j_superblock; 1896 bool had_fast_commit = false; 1897 1898 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex)); 1899 lock_buffer(journal->j_sb_buffer); 1900 if (sb->s_start == 0) { /* Is it already empty? */ 1901 unlock_buffer(journal->j_sb_buffer); 1902 return; 1903 } 1904 1905 jbd2_debug(1, "JBD2: Marking journal as empty (seq %u)\n", 1906 journal->j_tail_sequence); 1907 1908 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence); 1909 sb->s_start = cpu_to_be32(0); 1910 sb->s_head = cpu_to_be32(journal->j_head); 1911 if (jbd2_has_feature_fast_commit(journal)) { 1912 /* 1913 * When journal is clean, no need to commit fast commit flag and 1914 * make file system incompatible with older kernels. 1915 */ 1916 jbd2_clear_feature_fast_commit(journal); 1917 had_fast_commit = true; 1918 } 1919 1920 jbd2_write_superblock(journal, write_flags); 1921 1922 if (had_fast_commit) 1923 jbd2_set_feature_fast_commit(journal); 1924 1925 /* Log is empty */ 1926 write_lock(&journal->j_state_lock); 1927 journal->j_flags |= JBD2_FLUSHED; 1928 write_unlock(&journal->j_state_lock); 1929 } 1930 1931 /** 1932 * __jbd2_journal_erase() - Discard or zeroout journal blocks (excluding superblock) 1933 * @journal: The journal to erase. 1934 * @flags: A discard/zeroout request is sent for each physically contigous 1935 * region of the journal. Either JBD2_JOURNAL_FLUSH_DISCARD or 1936 * JBD2_JOURNAL_FLUSH_ZEROOUT must be set to determine which operation 1937 * to perform. 1938 * 1939 * Note: JBD2_JOURNAL_FLUSH_ZEROOUT attempts to use hardware offload. Zeroes 1940 * will be explicitly written if no hardware offload is available, see 1941 * blkdev_issue_zeroout for more details. 1942 */ 1943 static int __jbd2_journal_erase(journal_t *journal, unsigned int flags) 1944 { 1945 int err = 0; 1946 unsigned long block, log_offset; /* logical */ 1947 unsigned long long phys_block, block_start, block_stop; /* physical */ 1948 loff_t byte_start, byte_stop, byte_count; 1949 1950 /* flags must be set to either discard or zeroout */ 1951 if ((flags & ~JBD2_JOURNAL_FLUSH_VALID) || !flags || 1952 ((flags & JBD2_JOURNAL_FLUSH_DISCARD) && 1953 (flags & JBD2_JOURNAL_FLUSH_ZEROOUT))) 1954 return -EINVAL; 1955 1956 if ((flags & JBD2_JOURNAL_FLUSH_DISCARD) && 1957 !bdev_max_discard_sectors(journal->j_dev)) 1958 return -EOPNOTSUPP; 1959 1960 /* 1961 * lookup block mapping and issue discard/zeroout for each 1962 * contiguous region 1963 */ 1964 log_offset = be32_to_cpu(journal->j_superblock->s_first); 1965 block_start = ~0ULL; 1966 for (block = log_offset; block < journal->j_total_len; block++) { 1967 err = jbd2_journal_bmap(journal, block, &phys_block); 1968 if (err) { 1969 pr_err("JBD2: bad block at offset %lu", block); 1970 return err; 1971 } 1972 1973 if (block_start == ~0ULL) 1974 block_stop = block_start = phys_block; 1975 1976 /* 1977 * last block not contiguous with current block, 1978 * process last contiguous region and return to this block on 1979 * next loop 1980 */ 1981 if (phys_block != block_stop) { 1982 block--; 1983 } else { 1984 block_stop++; 1985 /* 1986 * if this isn't the last block of journal, 1987 * no need to process now because next block may also 1988 * be part of this contiguous region 1989 */ 1990 if (block != journal->j_total_len - 1) 1991 continue; 1992 } 1993 1994 /* 1995 * end of contiguous region or this is last block of journal, 1996 * take care of the region 1997 */ 1998 byte_start = block_start * journal->j_blocksize; 1999 byte_stop = block_stop * journal->j_blocksize; 2000 byte_count = (block_stop - block_start) * journal->j_blocksize; 2001 2002 truncate_inode_pages_range(journal->j_dev->bd_mapping, 2003 byte_start, byte_stop - 1); 2004 2005 if (flags & JBD2_JOURNAL_FLUSH_DISCARD) { 2006 err = blkdev_issue_discard(journal->j_dev, 2007 byte_start >> SECTOR_SHIFT, 2008 byte_count >> SECTOR_SHIFT, 2009 GFP_NOFS); 2010 } else if (flags & JBD2_JOURNAL_FLUSH_ZEROOUT) { 2011 err = blkdev_issue_zeroout(journal->j_dev, 2012 byte_start >> SECTOR_SHIFT, 2013 byte_count >> SECTOR_SHIFT, 2014 GFP_NOFS, 0); 2015 } 2016 2017 if (unlikely(err != 0)) { 2018 pr_err("JBD2: (error %d) unable to wipe journal at physical blocks [%llu, %llu)", 2019 err, block_start, block_stop); 2020 return err; 2021 } 2022 2023 /* reset start and stop after processing a region */ 2024 block_start = ~0ULL; 2025 } 2026 2027 return blkdev_issue_flush(journal->j_dev); 2028 } 2029 2030 /** 2031 * jbd2_journal_update_sb_errno() - Update error in the journal. 2032 * @journal: The journal to update. 2033 * 2034 * Update a journal's errno. Write updated superblock to disk waiting for IO 2035 * to complete. 2036 */ 2037 void jbd2_journal_update_sb_errno(journal_t *journal) 2038 { 2039 journal_superblock_t *sb = journal->j_superblock; 2040 int errcode; 2041 2042 lock_buffer(journal->j_sb_buffer); 2043 errcode = journal->j_errno; 2044 if (errcode == -ESHUTDOWN) 2045 errcode = 0; 2046 jbd2_debug(1, "JBD2: updating superblock error (errno %d)\n", errcode); 2047 sb->s_errno = cpu_to_be32(errcode); 2048 2049 jbd2_write_superblock(journal, REQ_FUA); 2050 } 2051 EXPORT_SYMBOL(jbd2_journal_update_sb_errno); 2052 2053 /** 2054 * jbd2_journal_load() - Read journal from disk. 2055 * @journal: Journal to act on. 2056 * 2057 * Given a journal_t structure which tells us which disk blocks contain 2058 * a journal, read the journal from disk to initialise the in-memory 2059 * structures. 2060 */ 2061 int jbd2_journal_load(journal_t *journal) 2062 { 2063 int err; 2064 2065 /* Let the recovery code check whether it needs to recover any 2066 * data from the journal. */ 2067 err = jbd2_journal_recover(journal); 2068 if (err) { 2069 pr_warn("JBD2: journal recovery failed\n"); 2070 return err; 2071 } 2072 2073 if (journal->j_failed_commit) { 2074 printk(KERN_ERR "JBD2: journal transaction %u on %s " 2075 "is corrupt.\n", journal->j_failed_commit, 2076 journal->j_devname); 2077 return -EFSCORRUPTED; 2078 } 2079 /* 2080 * clear JBD2_ABORT flag initialized in journal_init_common 2081 * here to update log tail information with the newest seq. 2082 */ 2083 journal->j_flags &= ~JBD2_ABORT; 2084 2085 /* OK, we've finished with the dynamic journal bits: 2086 * reinitialise the dynamic contents of the superblock in memory 2087 * and reset them on disk. */ 2088 err = journal_reset(journal); 2089 if (err) { 2090 pr_warn("JBD2: journal reset failed\n"); 2091 return err; 2092 } 2093 2094 journal->j_flags |= JBD2_LOADED; 2095 return 0; 2096 } 2097 2098 /** 2099 * jbd2_journal_destroy() - Release a journal_t structure. 2100 * @journal: Journal to act on. 2101 * 2102 * Release a journal_t structure once it is no longer in use by the 2103 * journaled object. 2104 * Return <0 if we couldn't clean up the journal. 2105 */ 2106 int jbd2_journal_destroy(journal_t *journal) 2107 { 2108 int err = 0; 2109 2110 /* Wait for the commit thread to wake up and die. */ 2111 journal_kill_thread(journal); 2112 2113 /* Force a final log commit */ 2114 if (journal->j_running_transaction) 2115 jbd2_journal_commit_transaction(journal); 2116 2117 /* Force any old transactions to disk */ 2118 2119 /* Totally anal locking here... */ 2120 spin_lock(&journal->j_list_lock); 2121 while (journal->j_checkpoint_transactions != NULL) { 2122 spin_unlock(&journal->j_list_lock); 2123 mutex_lock_io(&journal->j_checkpoint_mutex); 2124 err = jbd2_log_do_checkpoint(journal); 2125 mutex_unlock(&journal->j_checkpoint_mutex); 2126 /* 2127 * If checkpointing failed, just free the buffers to avoid 2128 * looping forever 2129 */ 2130 if (err) { 2131 jbd2_journal_destroy_checkpoint(journal); 2132 spin_lock(&journal->j_list_lock); 2133 break; 2134 } 2135 spin_lock(&journal->j_list_lock); 2136 } 2137 2138 J_ASSERT(journal->j_running_transaction == NULL); 2139 J_ASSERT(journal->j_committing_transaction == NULL); 2140 J_ASSERT(journal->j_checkpoint_transactions == NULL); 2141 spin_unlock(&journal->j_list_lock); 2142 2143 /* 2144 * OK, all checkpoint transactions have been checked, now check the 2145 * writeback errseq of fs dev and abort the journal if some buffer 2146 * failed to write back to the original location, otherwise the 2147 * filesystem may become inconsistent. 2148 */ 2149 if (!is_journal_aborted(journal)) { 2150 int ret = jbd2_check_fs_dev_write_error(journal); 2151 if (ret) 2152 jbd2_journal_abort(journal, ret); 2153 } 2154 2155 if (journal->j_sb_buffer) { 2156 if (!is_journal_aborted(journal)) { 2157 mutex_lock_io(&journal->j_checkpoint_mutex); 2158 2159 write_lock(&journal->j_state_lock); 2160 journal->j_tail_sequence = 2161 ++journal->j_transaction_sequence; 2162 write_unlock(&journal->j_state_lock); 2163 2164 jbd2_mark_journal_empty(journal, REQ_PREFLUSH | REQ_FUA); 2165 mutex_unlock(&journal->j_checkpoint_mutex); 2166 } else 2167 err = -EIO; 2168 brelse(journal->j_sb_buffer); 2169 } 2170 2171 if (journal->j_shrinker) { 2172 percpu_counter_destroy(&journal->j_checkpoint_jh_count); 2173 shrinker_free(journal->j_shrinker); 2174 } 2175 if (journal->j_proc_entry) 2176 jbd2_stats_proc_exit(journal); 2177 iput(journal->j_inode); 2178 if (journal->j_revoke) 2179 jbd2_journal_destroy_revoke(journal); 2180 kfree(journal->j_fc_wbuf); 2181 kfree(journal->j_wbuf); 2182 lockdep_unregister_key(&journal->jbd2_trans_commit_key); 2183 kfree(journal); 2184 2185 return err; 2186 } 2187 2188 2189 /** 2190 * jbd2_journal_check_used_features() - Check if features specified are used. 2191 * @journal: Journal to check. 2192 * @compat: bitmask of compatible features 2193 * @ro: bitmask of features that force read-only mount 2194 * @incompat: bitmask of incompatible features 2195 * 2196 * Check whether the journal uses all of a given set of 2197 * features. Return true (non-zero) if it does. 2198 **/ 2199 2200 int jbd2_journal_check_used_features(journal_t *journal, unsigned long compat, 2201 unsigned long ro, unsigned long incompat) 2202 { 2203 journal_superblock_t *sb; 2204 2205 if (!compat && !ro && !incompat) 2206 return 1; 2207 if (!jbd2_format_support_feature(journal)) 2208 return 0; 2209 2210 sb = journal->j_superblock; 2211 2212 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) && 2213 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) && 2214 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat)) 2215 return 1; 2216 2217 return 0; 2218 } 2219 2220 /** 2221 * jbd2_journal_check_available_features() - Check feature set in journalling layer 2222 * @journal: Journal to check. 2223 * @compat: bitmask of compatible features 2224 * @ro: bitmask of features that force read-only mount 2225 * @incompat: bitmask of incompatible features 2226 * 2227 * Check whether the journaling code supports the use of 2228 * all of a given set of features on this journal. Return true 2229 * (non-zero) if it can. */ 2230 2231 int jbd2_journal_check_available_features(journal_t *journal, unsigned long compat, 2232 unsigned long ro, unsigned long incompat) 2233 { 2234 if (!compat && !ro && !incompat) 2235 return 1; 2236 2237 if (!jbd2_format_support_feature(journal)) 2238 return 0; 2239 2240 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat && 2241 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro && 2242 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat) 2243 return 1; 2244 2245 return 0; 2246 } 2247 2248 static int 2249 jbd2_journal_initialize_fast_commit(journal_t *journal) 2250 { 2251 journal_superblock_t *sb = journal->j_superblock; 2252 unsigned long long num_fc_blks; 2253 2254 num_fc_blks = jbd2_journal_get_num_fc_blks(sb); 2255 if (num_fc_blks > journal->j_last) 2256 return -EFSCORRUPTED; 2257 if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS) 2258 return -ENOSPC; 2259 2260 /* Are we called twice? */ 2261 WARN_ON(journal->j_fc_wbuf != NULL); 2262 journal->j_fc_wbuf = kmalloc_objs(struct buffer_head *, num_fc_blks); 2263 if (!journal->j_fc_wbuf) 2264 return -ENOMEM; 2265 2266 journal->j_fc_wbufsize = num_fc_blks; 2267 journal->j_fc_last = journal->j_last; 2268 journal->j_last = journal->j_fc_last - num_fc_blks; 2269 journal->j_fc_first = journal->j_last + 1; 2270 journal->j_fc_off = 0; 2271 journal->j_free = journal->j_last - journal->j_first; 2272 2273 return 0; 2274 } 2275 2276 /** 2277 * jbd2_journal_set_features() - Mark a given journal feature in the superblock 2278 * @journal: Journal to act on. 2279 * @compat: bitmask of compatible features 2280 * @ro: bitmask of features that force read-only mount 2281 * @incompat: bitmask of incompatible features 2282 * 2283 * Mark a given journal feature as present on the 2284 * superblock. Returns true if the requested features could be set. 2285 * 2286 */ 2287 2288 int jbd2_journal_set_features(journal_t *journal, unsigned long compat, 2289 unsigned long ro, unsigned long incompat) 2290 { 2291 #define INCOMPAT_FEATURE_ON(f) \ 2292 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f))) 2293 #define COMPAT_FEATURE_ON(f) \ 2294 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f))) 2295 journal_superblock_t *sb; 2296 2297 if (jbd2_journal_check_used_features(journal, compat, ro, incompat)) 2298 return 1; 2299 2300 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat)) 2301 return 0; 2302 2303 /* If enabling v2 checksums, turn on v3 instead */ 2304 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2) { 2305 incompat &= ~JBD2_FEATURE_INCOMPAT_CSUM_V2; 2306 incompat |= JBD2_FEATURE_INCOMPAT_CSUM_V3; 2307 } 2308 2309 /* Asking for checksumming v3 and v1? Only give them v3. */ 2310 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V3 && 2311 compat & JBD2_FEATURE_COMPAT_CHECKSUM) 2312 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM; 2313 2314 jbd2_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n", 2315 compat, ro, incompat); 2316 2317 sb = journal->j_superblock; 2318 2319 if (incompat & JBD2_FEATURE_INCOMPAT_FAST_COMMIT) { 2320 if (jbd2_journal_initialize_fast_commit(journal)) { 2321 pr_err("JBD2: Cannot enable fast commits.\n"); 2322 return 0; 2323 } 2324 } 2325 2326 lock_buffer(journal->j_sb_buffer); 2327 2328 /* If enabling v3 checksums, update superblock and precompute seed */ 2329 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) { 2330 sb->s_checksum_type = JBD2_CRC32C_CHKSUM; 2331 sb->s_feature_compat &= 2332 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM); 2333 journal->j_csum_seed = jbd2_chksum(~0, sb->s_uuid, 2334 sizeof(sb->s_uuid)); 2335 } 2336 2337 /* If enabling v1 checksums, downgrade superblock */ 2338 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM)) 2339 sb->s_feature_incompat &= 2340 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2 | 2341 JBD2_FEATURE_INCOMPAT_CSUM_V3); 2342 2343 sb->s_feature_compat |= cpu_to_be32(compat); 2344 sb->s_feature_ro_compat |= cpu_to_be32(ro); 2345 sb->s_feature_incompat |= cpu_to_be32(incompat); 2346 /* 2347 * Update the checksum now so that it is valid even for read-only 2348 * filesystems where jbd2_write_superblock() doesn't get called. 2349 */ 2350 if (jbd2_journal_has_csum_v2or3(journal)) 2351 sb->s_checksum = jbd2_superblock_csum(sb); 2352 unlock_buffer(journal->j_sb_buffer); 2353 jbd2_journal_init_transaction_limits(journal); 2354 2355 return 1; 2356 #undef COMPAT_FEATURE_ON 2357 #undef INCOMPAT_FEATURE_ON 2358 } 2359 2360 /* 2361 * jbd2_journal_clear_features() - Clear a given journal feature in the 2362 * superblock 2363 * @journal: Journal to act on. 2364 * @compat: bitmask of compatible features 2365 * @ro: bitmask of features that force read-only mount 2366 * @incompat: bitmask of incompatible features 2367 * 2368 * Clear a given journal feature as present on the 2369 * superblock. 2370 */ 2371 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat, 2372 unsigned long ro, unsigned long incompat) 2373 { 2374 journal_superblock_t *sb; 2375 2376 jbd2_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n", 2377 compat, ro, incompat); 2378 2379 sb = journal->j_superblock; 2380 2381 lock_buffer(journal->j_sb_buffer); 2382 sb->s_feature_compat &= ~cpu_to_be32(compat); 2383 sb->s_feature_ro_compat &= ~cpu_to_be32(ro); 2384 sb->s_feature_incompat &= ~cpu_to_be32(incompat); 2385 /* 2386 * Update the checksum now so that it is valid even for read-only 2387 * filesystems where jbd2_write_superblock() doesn't get called. 2388 */ 2389 if (jbd2_journal_has_csum_v2or3(journal)) 2390 sb->s_checksum = jbd2_superblock_csum(sb); 2391 unlock_buffer(journal->j_sb_buffer); 2392 jbd2_journal_init_transaction_limits(journal); 2393 } 2394 EXPORT_SYMBOL(jbd2_journal_clear_features); 2395 2396 /** 2397 * jbd2_journal_flush() - Flush journal 2398 * @journal: Journal to act on. 2399 * @flags: optional operation on the journal blocks after the flush (see below) 2400 * 2401 * Flush all data for a given journal to disk and empty the journal. 2402 * Filesystems can use this when remounting readonly to ensure that 2403 * recovery does not need to happen on remount. Optionally, a discard or zeroout 2404 * can be issued on the journal blocks after flushing. 2405 * 2406 * flags: 2407 * JBD2_JOURNAL_FLUSH_DISCARD: issues discards for the journal blocks 2408 * JBD2_JOURNAL_FLUSH_ZEROOUT: issues zeroouts for the journal blocks 2409 */ 2410 int jbd2_journal_flush(journal_t *journal, unsigned int flags) 2411 { 2412 int err = 0; 2413 transaction_t *transaction = NULL; 2414 2415 write_lock(&journal->j_state_lock); 2416 2417 /* Force everything buffered to the log... */ 2418 if (journal->j_running_transaction) { 2419 transaction = journal->j_running_transaction; 2420 __jbd2_log_start_commit(journal, transaction->t_tid); 2421 } else if (journal->j_committing_transaction) 2422 transaction = journal->j_committing_transaction; 2423 2424 /* Wait for the log commit to complete... */ 2425 if (transaction) { 2426 tid_t tid = transaction->t_tid; 2427 2428 write_unlock(&journal->j_state_lock); 2429 jbd2_log_wait_commit(journal, tid); 2430 } else { 2431 write_unlock(&journal->j_state_lock); 2432 } 2433 2434 /* ...and flush everything in the log out to disk. */ 2435 spin_lock(&journal->j_list_lock); 2436 while (!err && journal->j_checkpoint_transactions != NULL) { 2437 spin_unlock(&journal->j_list_lock); 2438 mutex_lock_io(&journal->j_checkpoint_mutex); 2439 err = jbd2_log_do_checkpoint(journal); 2440 mutex_unlock(&journal->j_checkpoint_mutex); 2441 spin_lock(&journal->j_list_lock); 2442 } 2443 spin_unlock(&journal->j_list_lock); 2444 2445 if (is_journal_aborted(journal)) 2446 return -EIO; 2447 2448 mutex_lock_io(&journal->j_checkpoint_mutex); 2449 if (!err) { 2450 err = jbd2_cleanup_journal_tail(journal); 2451 if (err < 0) { 2452 mutex_unlock(&journal->j_checkpoint_mutex); 2453 goto out; 2454 } 2455 err = 0; 2456 } 2457 2458 /* Finally, mark the journal as really needing no recovery. 2459 * This sets s_start==0 in the underlying superblock, which is 2460 * the magic code for a fully-recovered superblock. Any future 2461 * commits of data to the journal will restore the current 2462 * s_start value. */ 2463 jbd2_mark_journal_empty(journal, REQ_FUA); 2464 2465 if (flags) 2466 err = __jbd2_journal_erase(journal, flags); 2467 2468 mutex_unlock(&journal->j_checkpoint_mutex); 2469 write_lock(&journal->j_state_lock); 2470 J_ASSERT(!journal->j_running_transaction); 2471 J_ASSERT(!journal->j_committing_transaction); 2472 J_ASSERT(!journal->j_checkpoint_transactions); 2473 J_ASSERT(journal->j_head == journal->j_tail); 2474 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence); 2475 write_unlock(&journal->j_state_lock); 2476 out: 2477 return err; 2478 } 2479 2480 /** 2481 * jbd2_journal_wipe() - Wipe journal contents 2482 * @journal: Journal to act on. 2483 * @write: flag (see below) 2484 * 2485 * Wipe out all of the contents of a journal, safely. This will produce 2486 * a warning if the journal contains any valid recovery information. 2487 * Must be called between journal_init_*() and jbd2_journal_load(). 2488 * 2489 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise 2490 * we merely suppress recovery. 2491 */ 2492 2493 int jbd2_journal_wipe(journal_t *journal, int write) 2494 { 2495 int err; 2496 2497 J_ASSERT (!(journal->j_flags & JBD2_LOADED)); 2498 2499 if (!journal->j_tail) 2500 return 0; 2501 2502 printk(KERN_WARNING "JBD2: %s recovery information on journal\n", 2503 write ? "Clearing" : "Ignoring"); 2504 2505 err = jbd2_journal_skip_recovery(journal); 2506 if (write) { 2507 /* Lock to make assertions happy... */ 2508 mutex_lock_io(&journal->j_checkpoint_mutex); 2509 jbd2_mark_journal_empty(journal, REQ_FUA); 2510 mutex_unlock(&journal->j_checkpoint_mutex); 2511 } 2512 2513 return err; 2514 } 2515 2516 /** 2517 * jbd2_journal_abort () - Shutdown the journal immediately. 2518 * @journal: the journal to shutdown. 2519 * @errno: an error number to record in the journal indicating 2520 * the reason for the shutdown. 2521 * 2522 * Perform a complete, immediate shutdown of the ENTIRE 2523 * journal (not of a single transaction). This operation cannot be 2524 * undone without closing and reopening the journal. 2525 * 2526 * The jbd2_journal_abort function is intended to support higher level error 2527 * recovery mechanisms such as the ext2/ext3 remount-readonly error 2528 * mode. 2529 * 2530 * Journal abort has very specific semantics. Any existing dirty, 2531 * unjournaled buffers in the main filesystem will still be written to 2532 * disk by bdflush, but the journaling mechanism will be suspended 2533 * immediately and no further transaction commits will be honoured. 2534 * 2535 * Any dirty, journaled buffers will be written back to disk without 2536 * hitting the journal. Atomicity cannot be guaranteed on an aborted 2537 * filesystem, but we _do_ attempt to leave as much data as possible 2538 * behind for fsck to use for cleanup. 2539 * 2540 * Any attempt to get a new transaction handle on a journal which is in 2541 * ABORT state will just result in an -EROFS error return. A 2542 * jbd2_journal_stop on an existing handle will return -EIO if we have 2543 * entered abort state during the update. 2544 * 2545 * Recursive transactions are not disturbed by journal abort until the 2546 * final jbd2_journal_stop, which will receive the -EIO error. 2547 * 2548 * Finally, the jbd2_journal_abort call allows the caller to supply an errno 2549 * which will be recorded (if possible) in the journal superblock. This 2550 * allows a client to record failure conditions in the middle of a 2551 * transaction without having to complete the transaction to record the 2552 * failure to disk. ext3_error, for example, now uses this 2553 * functionality. 2554 * 2555 */ 2556 2557 void jbd2_journal_abort(journal_t *journal, int errno) 2558 { 2559 transaction_t *transaction; 2560 2561 /* 2562 * Lock the aborting procedure until everything is done, this avoid 2563 * races between filesystem's error handling flow (e.g. ext4_abort()), 2564 * ensure panic after the error info is written into journal's 2565 * superblock. 2566 */ 2567 mutex_lock(&journal->j_abort_mutex); 2568 /* 2569 * ESHUTDOWN always takes precedence because a file system check 2570 * caused by any other journal abort error is not required after 2571 * a shutdown triggered. 2572 */ 2573 write_lock(&journal->j_state_lock); 2574 if (journal->j_flags & JBD2_ABORT) { 2575 int old_errno = journal->j_errno; 2576 2577 write_unlock(&journal->j_state_lock); 2578 if (old_errno != -ESHUTDOWN && errno == -ESHUTDOWN) { 2579 journal->j_errno = errno; 2580 jbd2_journal_update_sb_errno(journal); 2581 } 2582 mutex_unlock(&journal->j_abort_mutex); 2583 return; 2584 } 2585 2586 /* 2587 * Mark the abort as occurred and start current running transaction 2588 * to release all journaled buffer. 2589 */ 2590 pr_err("Aborting journal on device %s.\n", journal->j_devname); 2591 2592 journal->j_flags |= JBD2_ABORT; 2593 journal->j_errno = errno; 2594 transaction = journal->j_running_transaction; 2595 if (transaction) 2596 __jbd2_log_start_commit(journal, transaction->t_tid); 2597 write_unlock(&journal->j_state_lock); 2598 2599 /* 2600 * Record errno to the journal super block, so that fsck and jbd2 2601 * layer could realise that a filesystem check is needed. 2602 */ 2603 jbd2_journal_update_sb_errno(journal); 2604 mutex_unlock(&journal->j_abort_mutex); 2605 } 2606 2607 /** 2608 * jbd2_journal_errno() - returns the journal's error state. 2609 * @journal: journal to examine. 2610 * 2611 * This is the errno number set with jbd2_journal_abort(), the last 2612 * time the journal was mounted - if the journal was stopped 2613 * without calling abort this will be 0. 2614 * 2615 * If the journal has been aborted on this mount time -EROFS will 2616 * be returned. 2617 */ 2618 int jbd2_journal_errno(journal_t *journal) 2619 { 2620 int err; 2621 2622 read_lock(&journal->j_state_lock); 2623 if (journal->j_flags & JBD2_ABORT) 2624 err = -EROFS; 2625 else 2626 err = journal->j_errno; 2627 read_unlock(&journal->j_state_lock); 2628 return err; 2629 } 2630 2631 /** 2632 * jbd2_journal_clear_err() - clears the journal's error state 2633 * @journal: journal to act on. 2634 * 2635 * An error must be cleared or acked to take a FS out of readonly 2636 * mode. 2637 */ 2638 int jbd2_journal_clear_err(journal_t *journal) 2639 { 2640 int err = 0; 2641 2642 write_lock(&journal->j_state_lock); 2643 if (journal->j_flags & JBD2_ABORT) 2644 err = -EROFS; 2645 else 2646 journal->j_errno = 0; 2647 write_unlock(&journal->j_state_lock); 2648 return err; 2649 } 2650 2651 /** 2652 * jbd2_journal_ack_err() - Ack journal err. 2653 * @journal: journal to act on. 2654 * 2655 * An error must be cleared or acked to take a FS out of readonly 2656 * mode. 2657 */ 2658 void jbd2_journal_ack_err(journal_t *journal) 2659 { 2660 write_lock(&journal->j_state_lock); 2661 if (journal->j_errno) 2662 journal->j_flags |= JBD2_ACK_ERR; 2663 write_unlock(&journal->j_state_lock); 2664 } 2665 2666 int jbd2_journal_blocks_per_folio(struct inode *inode) 2667 { 2668 return 1 << (PAGE_SHIFT + mapping_max_folio_order(inode->i_mapping) - 2669 inode->i_sb->s_blocksize_bits); 2670 } 2671 2672 /* 2673 * helper functions to deal with 32 or 64bit block numbers. 2674 */ 2675 size_t journal_tag_bytes(journal_t *journal) 2676 { 2677 size_t sz; 2678 2679 if (jbd2_has_feature_csum3(journal)) 2680 return sizeof(journal_block_tag3_t); 2681 2682 sz = sizeof(journal_block_tag_t); 2683 2684 if (jbd2_has_feature_csum2(journal)) 2685 sz += sizeof(__u16); 2686 2687 if (jbd2_has_feature_64bit(journal)) 2688 return sz; 2689 else 2690 return sz - sizeof(__u32); 2691 } 2692 2693 /* 2694 * Journal_head storage management 2695 */ 2696 static struct kmem_cache *jbd2_journal_head_cache; 2697 #ifdef CONFIG_JBD2_DEBUG 2698 static atomic_t nr_journal_heads = ATOMIC_INIT(0); 2699 #endif 2700 2701 static int __init jbd2_journal_init_journal_head_cache(void) 2702 { 2703 J_ASSERT(!jbd2_journal_head_cache); 2704 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head", 2705 sizeof(struct journal_head), 2706 0, /* offset */ 2707 SLAB_TEMPORARY | SLAB_TYPESAFE_BY_RCU, 2708 NULL); /* ctor */ 2709 if (!jbd2_journal_head_cache) { 2710 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n"); 2711 return -ENOMEM; 2712 } 2713 return 0; 2714 } 2715 2716 static void jbd2_journal_destroy_journal_head_cache(void) 2717 { 2718 kmem_cache_destroy(jbd2_journal_head_cache); 2719 jbd2_journal_head_cache = NULL; 2720 } 2721 2722 /* 2723 * journal_head splicing and dicing 2724 */ 2725 static struct journal_head *journal_alloc_journal_head(void) 2726 { 2727 struct journal_head *ret; 2728 2729 #ifdef CONFIG_JBD2_DEBUG 2730 atomic_inc(&nr_journal_heads); 2731 #endif 2732 ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS); 2733 if (!ret) { 2734 jbd2_debug(1, "out of memory for journal_head\n"); 2735 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__); 2736 ret = kmem_cache_zalloc(jbd2_journal_head_cache, 2737 GFP_NOFS | __GFP_NOFAIL); 2738 } 2739 spin_lock_init(&ret->b_state_lock); 2740 return ret; 2741 } 2742 2743 static void journal_free_journal_head(struct journal_head *jh) 2744 { 2745 #ifdef CONFIG_JBD2_DEBUG 2746 atomic_dec(&nr_journal_heads); 2747 memset(jh, JBD2_POISON_FREE, sizeof(*jh)); 2748 #endif 2749 kmem_cache_free(jbd2_journal_head_cache, jh); 2750 } 2751 2752 /* 2753 * A journal_head is attached to a buffer_head whenever JBD has an 2754 * interest in the buffer. 2755 * 2756 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit 2757 * is set. This bit is tested in core kernel code where we need to take 2758 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable 2759 * there. 2760 * 2761 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one. 2762 * 2763 * When a buffer has its BH_JBD bit set it is immune from being released by 2764 * core kernel code, mainly via ->b_count. 2765 * 2766 * A journal_head is detached from its buffer_head when the journal_head's 2767 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint 2768 * transaction (b_cp_transaction) hold their references to b_jcount. 2769 * 2770 * Various places in the kernel want to attach a journal_head to a buffer_head 2771 * _before_ attaching the journal_head to a transaction. To protect the 2772 * journal_head in this situation, jbd2_journal_add_journal_head elevates the 2773 * journal_head's b_jcount refcount by one. The caller must call 2774 * jbd2_journal_put_journal_head() to undo this. 2775 * 2776 * So the typical usage would be: 2777 * 2778 * (Attach a journal_head if needed. Increments b_jcount) 2779 * struct journal_head *jh = jbd2_journal_add_journal_head(bh); 2780 * ... 2781 * (Get another reference for transaction) 2782 * jbd2_journal_grab_journal_head(bh); 2783 * jh->b_transaction = xxx; 2784 * (Put original reference) 2785 * jbd2_journal_put_journal_head(jh); 2786 */ 2787 2788 /* 2789 * Give a buffer_head a journal_head. 2790 * 2791 * May sleep. 2792 */ 2793 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh) 2794 { 2795 struct journal_head *jh; 2796 struct journal_head *new_jh = NULL; 2797 2798 repeat: 2799 if (!buffer_jbd(bh)) 2800 new_jh = journal_alloc_journal_head(); 2801 2802 jbd_lock_bh_journal_head(bh); 2803 if (buffer_jbd(bh)) { 2804 jh = bh2jh(bh); 2805 } else { 2806 J_ASSERT_BH(bh, 2807 (atomic_read(&bh->b_count) > 0) || 2808 (bh->b_folio && bh->b_folio->mapping)); 2809 2810 if (!new_jh) { 2811 jbd_unlock_bh_journal_head(bh); 2812 goto repeat; 2813 } 2814 2815 jh = new_jh; 2816 new_jh = NULL; /* We consumed it */ 2817 set_buffer_jbd(bh); 2818 bh->b_private = jh; 2819 jh->b_bh = bh; 2820 get_bh(bh); 2821 BUFFER_TRACE(bh, "added journal_head"); 2822 } 2823 jh->b_jcount++; 2824 jbd_unlock_bh_journal_head(bh); 2825 if (new_jh) 2826 journal_free_journal_head(new_jh); 2827 return bh->b_private; 2828 } 2829 2830 /* 2831 * Grab a ref against this buffer_head's journal_head. If it ended up not 2832 * having a journal_head, return NULL 2833 */ 2834 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh) 2835 { 2836 struct journal_head *jh = NULL; 2837 2838 jbd_lock_bh_journal_head(bh); 2839 if (buffer_jbd(bh)) { 2840 jh = bh2jh(bh); 2841 jh->b_jcount++; 2842 } 2843 jbd_unlock_bh_journal_head(bh); 2844 return jh; 2845 } 2846 EXPORT_SYMBOL(jbd2_journal_grab_journal_head); 2847 2848 static void __journal_remove_journal_head(struct buffer_head *bh) 2849 { 2850 struct journal_head *jh = bh2jh(bh); 2851 2852 J_ASSERT_JH(jh, jh->b_transaction == NULL); 2853 J_ASSERT_JH(jh, jh->b_next_transaction == NULL); 2854 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL); 2855 J_ASSERT_JH(jh, jh->b_jlist == BJ_None); 2856 J_ASSERT_BH(bh, buffer_jbd(bh)); 2857 J_ASSERT_BH(bh, jh2bh(jh) == bh); 2858 BUFFER_TRACE(bh, "remove journal_head"); 2859 2860 /* Unlink before dropping the lock */ 2861 bh->b_private = NULL; 2862 jh->b_bh = NULL; /* debug, really */ 2863 clear_buffer_jbd(bh); 2864 } 2865 2866 static void journal_release_journal_head(struct journal_head *jh) 2867 { 2868 if (jh->b_frozen_data) { 2869 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__); 2870 kfree(jh->b_frozen_data); 2871 } 2872 if (jh->b_committed_data) { 2873 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__); 2874 kfree(jh->b_committed_data); 2875 } 2876 journal_free_journal_head(jh); 2877 } 2878 2879 /* 2880 * Drop a reference on the passed journal_head. If it fell to zero then 2881 * release the journal_head from the buffer_head. 2882 */ 2883 void jbd2_journal_put_journal_head(struct journal_head *jh) 2884 { 2885 struct buffer_head *bh = jh2bh(jh); 2886 2887 jbd_lock_bh_journal_head(bh); 2888 J_ASSERT_JH(jh, jh->b_jcount > 0); 2889 --jh->b_jcount; 2890 if (!jh->b_jcount) { 2891 __journal_remove_journal_head(bh); 2892 jbd_unlock_bh_journal_head(bh); 2893 journal_release_journal_head(jh); 2894 __brelse(bh); 2895 } else { 2896 jbd_unlock_bh_journal_head(bh); 2897 } 2898 } 2899 EXPORT_SYMBOL(jbd2_journal_put_journal_head); 2900 2901 /* 2902 * Initialize jbd inode head 2903 */ 2904 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode) 2905 { 2906 jinode->i_transaction = NULL; 2907 jinode->i_next_transaction = NULL; 2908 jinode->i_vfs_inode = inode; 2909 jinode->i_flags = 0; 2910 jinode->i_dirty_start_page = 0; 2911 jinode->i_dirty_end_page = 0; 2912 INIT_LIST_HEAD(&jinode->i_list); 2913 } 2914 2915 /* 2916 * Function to be called before we start removing inode from memory (i.e., 2917 * clear_inode() is a fine place to be called from). It removes inode from 2918 * transaction's lists. 2919 */ 2920 void jbd2_journal_release_jbd_inode(journal_t *journal, 2921 struct jbd2_inode *jinode) 2922 { 2923 if (!journal) 2924 return; 2925 restart: 2926 spin_lock(&journal->j_list_lock); 2927 /* Is commit writing out inode - we have to wait */ 2928 if (jinode->i_flags & JI_COMMIT_RUNNING) { 2929 wait_queue_head_t *wq; 2930 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING); 2931 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING); 2932 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); 2933 spin_unlock(&journal->j_list_lock); 2934 schedule(); 2935 finish_wait(wq, &wait.wq_entry); 2936 goto restart; 2937 } 2938 2939 if (jinode->i_transaction) { 2940 list_del(&jinode->i_list); 2941 jinode->i_transaction = NULL; 2942 } 2943 spin_unlock(&journal->j_list_lock); 2944 } 2945 2946 2947 #ifdef CONFIG_PROC_FS 2948 2949 #define JBD2_STATS_PROC_NAME "fs/jbd2" 2950 2951 static void __init jbd2_create_jbd_stats_proc_entry(void) 2952 { 2953 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL); 2954 } 2955 2956 static void __exit jbd2_remove_jbd_stats_proc_entry(void) 2957 { 2958 if (proc_jbd2_stats) 2959 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL); 2960 } 2961 2962 #else 2963 2964 #define jbd2_create_jbd_stats_proc_entry() do {} while (0) 2965 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0) 2966 2967 #endif 2968 2969 struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache; 2970 2971 static int __init jbd2_journal_init_inode_cache(void) 2972 { 2973 J_ASSERT(!jbd2_inode_cache); 2974 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0); 2975 if (!jbd2_inode_cache) { 2976 pr_emerg("JBD2: failed to create inode cache\n"); 2977 return -ENOMEM; 2978 } 2979 return 0; 2980 } 2981 2982 static int __init jbd2_journal_init_handle_cache(void) 2983 { 2984 J_ASSERT(!jbd2_handle_cache); 2985 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY); 2986 if (!jbd2_handle_cache) { 2987 printk(KERN_EMERG "JBD2: failed to create handle cache\n"); 2988 return -ENOMEM; 2989 } 2990 return 0; 2991 } 2992 2993 static void jbd2_journal_destroy_inode_cache(void) 2994 { 2995 kmem_cache_destroy(jbd2_inode_cache); 2996 jbd2_inode_cache = NULL; 2997 } 2998 2999 static void jbd2_journal_destroy_handle_cache(void) 3000 { 3001 kmem_cache_destroy(jbd2_handle_cache); 3002 jbd2_handle_cache = NULL; 3003 } 3004 3005 /* 3006 * Module startup and shutdown 3007 */ 3008 3009 static int __init journal_init_caches(void) 3010 { 3011 int ret; 3012 3013 ret = jbd2_journal_init_revoke_record_cache(); 3014 if (ret == 0) 3015 ret = jbd2_journal_init_revoke_table_cache(); 3016 if (ret == 0) 3017 ret = jbd2_journal_init_journal_head_cache(); 3018 if (ret == 0) 3019 ret = jbd2_journal_init_handle_cache(); 3020 if (ret == 0) 3021 ret = jbd2_journal_init_inode_cache(); 3022 if (ret == 0) 3023 ret = jbd2_journal_init_transaction_cache(); 3024 return ret; 3025 } 3026 3027 static void jbd2_journal_destroy_caches(void) 3028 { 3029 jbd2_journal_destroy_revoke_record_cache(); 3030 jbd2_journal_destroy_revoke_table_cache(); 3031 jbd2_journal_destroy_journal_head_cache(); 3032 jbd2_journal_destroy_handle_cache(); 3033 jbd2_journal_destroy_inode_cache(); 3034 jbd2_journal_destroy_transaction_cache(); 3035 } 3036 3037 static int __init journal_init(void) 3038 { 3039 int ret; 3040 3041 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024); 3042 3043 ret = journal_init_caches(); 3044 if (ret == 0) { 3045 jbd2_create_jbd_stats_proc_entry(); 3046 } else { 3047 jbd2_journal_destroy_caches(); 3048 } 3049 return ret; 3050 } 3051 3052 static void __exit journal_exit(void) 3053 { 3054 #ifdef CONFIG_JBD2_DEBUG 3055 int n = atomic_read(&nr_journal_heads); 3056 if (n) 3057 printk(KERN_ERR "JBD2: leaked %d journal_heads!\n", n); 3058 #endif 3059 jbd2_remove_jbd_stats_proc_entry(); 3060 jbd2_journal_destroy_caches(); 3061 } 3062 3063 MODULE_DESCRIPTION("Generic filesystem journal-writing module"); 3064 MODULE_LICENSE("GPL"); 3065 module_init(journal_init); 3066 module_exit(journal_exit); 3067