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