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