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