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