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