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