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