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