2 * linux/fs/jbd2/journal.c
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
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.
12 * Generic filesystem journal-writing code; part of the ext2fs
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.
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).
25 #include <linux/module.h>
26 #include <linux/time.h>
28 #include <linux/jbd2.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/init.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>
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/jbd2.h>
50 #include <asm/uaccess.h>
53 #ifdef CONFIG_JBD2_DEBUG
54 ushort jbd2_journal_enable_debug __read_mostly;
55 EXPORT_SYMBOL(jbd2_journal_enable_debug);
57 module_param_named(jbd2_debug, jbd2_journal_enable_debug, ushort, 0644);
58 MODULE_PARM_DESC(jbd2_debug, "Debugging level for jbd2");
61 EXPORT_SYMBOL(jbd2_journal_extend);
62 EXPORT_SYMBOL(jbd2_journal_stop);
63 EXPORT_SYMBOL(jbd2_journal_lock_updates);
64 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
65 EXPORT_SYMBOL(jbd2_journal_get_write_access);
66 EXPORT_SYMBOL(jbd2_journal_get_create_access);
67 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
68 EXPORT_SYMBOL(jbd2_journal_set_triggers);
69 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
70 EXPORT_SYMBOL(jbd2_journal_forget);
72 EXPORT_SYMBOL(journal_sync_buffer);
74 EXPORT_SYMBOL(jbd2_journal_flush);
75 EXPORT_SYMBOL(jbd2_journal_revoke);
77 EXPORT_SYMBOL(jbd2_journal_init_dev);
78 EXPORT_SYMBOL(jbd2_journal_init_inode);
79 EXPORT_SYMBOL(jbd2_journal_check_used_features);
80 EXPORT_SYMBOL(jbd2_journal_check_available_features);
81 EXPORT_SYMBOL(jbd2_journal_set_features);
82 EXPORT_SYMBOL(jbd2_journal_load);
83 EXPORT_SYMBOL(jbd2_journal_destroy);
84 EXPORT_SYMBOL(jbd2_journal_abort);
85 EXPORT_SYMBOL(jbd2_journal_errno);
86 EXPORT_SYMBOL(jbd2_journal_ack_err);
87 EXPORT_SYMBOL(jbd2_journal_clear_err);
88 EXPORT_SYMBOL(jbd2_log_wait_commit);
89 EXPORT_SYMBOL(jbd2_log_start_commit);
90 EXPORT_SYMBOL(jbd2_journal_start_commit);
91 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
92 EXPORT_SYMBOL(jbd2_journal_wipe);
93 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
94 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
95 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
96 EXPORT_SYMBOL(jbd2_journal_force_commit);
97 EXPORT_SYMBOL(jbd2_journal_file_inode);
98 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
99 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
100 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
101 EXPORT_SYMBOL(jbd2_inode_cache);
103 static void __journal_abort_soft (journal_t *journal, int errno);
104 static int jbd2_journal_create_slab(size_t slab_size);
106 /* Checksumming functions */
107 int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
109 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
112 return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
115 static __u32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
117 __u32 csum, old_csum;
119 old_csum = sb->s_checksum;
121 csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t));
122 sb->s_checksum = old_csum;
124 return cpu_to_be32(csum);
127 int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb)
129 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
132 return sb->s_checksum == jbd2_superblock_csum(j, sb);
135 void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb)
137 if (!JBD2_HAS_INCOMPAT_FEATURE(j, JBD2_FEATURE_INCOMPAT_CSUM_V2))
140 sb->s_checksum = jbd2_superblock_csum(j, sb);
144 * Helper function used to manage commit timeouts
147 static void commit_timeout(unsigned long __data)
149 struct task_struct * p = (struct task_struct *) __data;
155 * kjournald2: The main thread function used to manage a logging device
158 * This kernel thread is responsible for two things:
160 * 1) COMMIT: Every so often we need to commit the current state of the
161 * filesystem to disk. The journal thread is responsible for writing
162 * all of the metadata buffers to disk.
164 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
165 * of the data in that part of the log has been rewritten elsewhere on
166 * the disk. Flushing these old buffers to reclaim space in the log is
167 * known as checkpointing, and this thread is responsible for that job.
170 static int kjournald2(void *arg)
172 journal_t *journal = arg;
173 transaction_t *transaction;
176 * Set up an interval timer which can be used to trigger a commit wakeup
177 * after the commit interval expires
179 setup_timer(&journal->j_commit_timer, commit_timeout,
180 (unsigned long)current);
184 /* Record that the journal thread is running */
185 journal->j_task = current;
186 wake_up(&journal->j_wait_done_commit);
189 * And now, wait forever for commit wakeup events.
191 write_lock(&journal->j_state_lock);
194 if (journal->j_flags & JBD2_UNMOUNT)
197 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
198 journal->j_commit_sequence, journal->j_commit_request);
200 if (journal->j_commit_sequence != journal->j_commit_request) {
201 jbd_debug(1, "OK, requests differ\n");
202 write_unlock(&journal->j_state_lock);
203 del_timer_sync(&journal->j_commit_timer);
204 jbd2_journal_commit_transaction(journal);
205 write_lock(&journal->j_state_lock);
209 wake_up(&journal->j_wait_done_commit);
210 if (freezing(current)) {
212 * The simpler the better. Flushing journal isn't a
213 * good idea, because that depends on threads that may
214 * be already stopped.
216 jbd_debug(1, "Now suspending kjournald2\n");
217 write_unlock(&journal->j_state_lock);
219 write_lock(&journal->j_state_lock);
222 * We assume on resume that commits are already there,
226 int should_sleep = 1;
228 prepare_to_wait(&journal->j_wait_commit, &wait,
230 if (journal->j_commit_sequence != journal->j_commit_request)
232 transaction = journal->j_running_transaction;
233 if (transaction && time_after_eq(jiffies,
234 transaction->t_expires))
236 if (journal->j_flags & JBD2_UNMOUNT)
239 write_unlock(&journal->j_state_lock);
241 write_lock(&journal->j_state_lock);
243 finish_wait(&journal->j_wait_commit, &wait);
246 jbd_debug(1, "kjournald2 wakes\n");
249 * Were we woken up by a commit wakeup event?
251 transaction = journal->j_running_transaction;
252 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
253 journal->j_commit_request = transaction->t_tid;
254 jbd_debug(1, "woke because of timeout\n");
259 write_unlock(&journal->j_state_lock);
260 del_timer_sync(&journal->j_commit_timer);
261 journal->j_task = NULL;
262 wake_up(&journal->j_wait_done_commit);
263 jbd_debug(1, "Journal thread exiting.\n");
267 static int jbd2_journal_start_thread(journal_t *journal)
269 struct task_struct *t;
271 t = kthread_run(kjournald2, journal, "jbd2/%s",
276 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
280 static void journal_kill_thread(journal_t *journal)
282 write_lock(&journal->j_state_lock);
283 journal->j_flags |= JBD2_UNMOUNT;
285 while (journal->j_task) {
286 wake_up(&journal->j_wait_commit);
287 write_unlock(&journal->j_state_lock);
288 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
289 write_lock(&journal->j_state_lock);
291 write_unlock(&journal->j_state_lock);
295 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
297 * Writes a metadata buffer to a given disk block. The actual IO is not
298 * performed but a new buffer_head is constructed which labels the data
299 * to be written with the correct destination disk block.
301 * Any magic-number escaping which needs to be done will cause a
302 * copy-out here. If the buffer happens to start with the
303 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
304 * magic number is only written to the log for descripter blocks. In
305 * this case, we copy the data and replace the first word with 0, and we
306 * return a result code which indicates that this buffer needs to be
307 * marked as an escaped buffer in the corresponding log descriptor
308 * block. The missing word can then be restored when the block is read
311 * If the source buffer has already been modified by a new transaction
312 * since we took the last commit snapshot, we use the frozen copy of
313 * that data for IO. If we end up using the existing buffer_head's data
314 * for the write, then we have to make sure nobody modifies it while the
315 * IO is in progress. do_get_write_access() handles this.
317 * The function returns a pointer to the buffer_head to be used for IO.
325 * Bit 0 set == escape performed on the data
326 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
329 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
330 struct journal_head *jh_in,
331 struct buffer_head **bh_out,
334 int need_copy_out = 0;
335 int done_copy_out = 0;
338 struct buffer_head *new_bh;
339 struct page *new_page;
340 unsigned int new_offset;
341 struct buffer_head *bh_in = jh2bh(jh_in);
342 journal_t *journal = transaction->t_journal;
345 * The buffer really shouldn't be locked: only the current committing
346 * transaction is allowed to write it, so nobody else is allowed
349 * akpm: except if we're journalling data, and write() output is
350 * also part of a shared mapping, and another thread has
351 * decided to launch a writepage() against this buffer.
353 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
356 new_bh = alloc_buffer_head(GFP_NOFS);
359 * Failure is not an option, but __GFP_NOFAIL is going
360 * away; so we retry ourselves here.
362 congestion_wait(BLK_RW_ASYNC, HZ/50);
366 /* keep subsequent assertions sane */
367 atomic_set(&new_bh->b_count, 1);
369 jbd_lock_bh_state(bh_in);
372 * If a new transaction has already done a buffer copy-out, then
373 * we use that version of the data for the commit.
375 if (jh_in->b_frozen_data) {
377 new_page = virt_to_page(jh_in->b_frozen_data);
378 new_offset = offset_in_page(jh_in->b_frozen_data);
380 new_page = jh2bh(jh_in)->b_page;
381 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
384 mapped_data = kmap_atomic(new_page);
386 * Fire data frozen trigger if data already wasn't frozen. Do this
387 * before checking for escaping, as the trigger may modify the magic
388 * offset. If a copy-out happens afterwards, it will have the correct
389 * data in the buffer.
392 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
398 if (*((__be32 *)(mapped_data + new_offset)) ==
399 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
403 kunmap_atomic(mapped_data);
406 * Do we need to do a data copy?
408 if (need_copy_out && !done_copy_out) {
411 jbd_unlock_bh_state(bh_in);
412 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
417 jbd_lock_bh_state(bh_in);
418 if (jh_in->b_frozen_data) {
419 jbd2_free(tmp, bh_in->b_size);
423 jh_in->b_frozen_data = tmp;
424 mapped_data = kmap_atomic(new_page);
425 memcpy(tmp, mapped_data + new_offset, bh_in->b_size);
426 kunmap_atomic(mapped_data);
428 new_page = virt_to_page(tmp);
429 new_offset = offset_in_page(tmp);
433 * This isn't strictly necessary, as we're using frozen
434 * data for the escaping, but it keeps consistency with
435 * b_frozen_data usage.
437 jh_in->b_frozen_triggers = jh_in->b_triggers;
441 * Did we need to do an escaping? Now we've done all the
442 * copying, we can finally do so.
445 mapped_data = kmap_atomic(new_page);
446 *((unsigned int *)(mapped_data + new_offset)) = 0;
447 kunmap_atomic(mapped_data);
450 set_bh_page(new_bh, new_page, new_offset);
451 new_bh->b_size = bh_in->b_size;
452 new_bh->b_bdev = journal->j_dev;
453 new_bh->b_blocknr = blocknr;
454 new_bh->b_private = bh_in;
455 set_buffer_mapped(new_bh);
456 set_buffer_dirty(new_bh);
461 * The to-be-written buffer needs to get moved to the io queue,
462 * and the original buffer whose contents we are shadowing or
463 * copying is moved to the transaction's shadow queue.
465 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
466 spin_lock(&journal->j_list_lock);
467 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
468 spin_unlock(&journal->j_list_lock);
469 set_buffer_shadow(bh_in);
470 jbd_unlock_bh_state(bh_in);
472 return do_escape | (done_copy_out << 1);
476 * Allocation code for the journal file. Manage the space left in the
477 * journal, so that we can begin checkpointing when appropriate.
481 * Called with j_state_lock locked for writing.
482 * Returns true if a transaction commit was started.
484 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
486 /* Return if the txn has already requested to be committed */
487 if (journal->j_commit_request == target)
491 * The only transaction we can possibly wait upon is the
492 * currently running transaction (if it exists). Otherwise,
493 * the target tid must be an old one.
495 if (journal->j_running_transaction &&
496 journal->j_running_transaction->t_tid == target) {
498 * We want a new commit: OK, mark the request and wakeup the
499 * commit thread. We do _not_ do the commit ourselves.
502 journal->j_commit_request = target;
503 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
504 journal->j_commit_request,
505 journal->j_commit_sequence);
506 journal->j_running_transaction->t_requested = jiffies;
507 wake_up(&journal->j_wait_commit);
509 } else if (!tid_geq(journal->j_commit_request, target))
510 /* This should never happen, but if it does, preserve
511 the evidence before kjournald goes into a loop and
512 increments j_commit_sequence beyond all recognition. */
513 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
514 journal->j_commit_request,
515 journal->j_commit_sequence,
516 target, journal->j_running_transaction ?
517 journal->j_running_transaction->t_tid : 0);
521 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
525 write_lock(&journal->j_state_lock);
526 ret = __jbd2_log_start_commit(journal, tid);
527 write_unlock(&journal->j_state_lock);
532 * Force and wait upon a commit if the calling process is not within
533 * transaction. This is used for forcing out undo-protected data which contains
534 * bitmaps, when the fs is running out of space.
536 * We can only force the running transaction if we don't have an active handle;
537 * otherwise, we will deadlock.
539 * Returns true if a transaction was started.
541 int jbd2_journal_force_commit_nested(journal_t *journal)
543 transaction_t *transaction = NULL;
545 int need_to_start = 0;
547 read_lock(&journal->j_state_lock);
548 if (journal->j_running_transaction && !current->journal_info) {
549 transaction = journal->j_running_transaction;
550 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
552 } else if (journal->j_committing_transaction)
553 transaction = journal->j_committing_transaction;
556 read_unlock(&journal->j_state_lock);
557 return 0; /* Nothing to retry */
560 tid = transaction->t_tid;
561 read_unlock(&journal->j_state_lock);
563 jbd2_log_start_commit(journal, tid);
564 jbd2_log_wait_commit(journal, tid);
569 * Start a commit of the current running transaction (if any). Returns true
570 * if a transaction is going to be committed (or is currently already
571 * committing), and fills its tid in at *ptid
573 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
577 write_lock(&journal->j_state_lock);
578 if (journal->j_running_transaction) {
579 tid_t tid = journal->j_running_transaction->t_tid;
581 __jbd2_log_start_commit(journal, tid);
582 /* There's a running transaction and we've just made sure
583 * it's commit has been scheduled. */
587 } else if (journal->j_committing_transaction) {
589 * If commit has been started, then we have to wait for
590 * completion of that transaction.
593 *ptid = journal->j_committing_transaction->t_tid;
596 write_unlock(&journal->j_state_lock);
601 * Return 1 if a given transaction has not yet sent barrier request
602 * connected with a transaction commit. If 0 is returned, transaction
603 * may or may not have sent the barrier. Used to avoid sending barrier
604 * twice in common cases.
606 int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
609 transaction_t *commit_trans;
611 if (!(journal->j_flags & JBD2_BARRIER))
613 read_lock(&journal->j_state_lock);
614 /* Transaction already committed? */
615 if (tid_geq(journal->j_commit_sequence, tid))
617 commit_trans = journal->j_committing_transaction;
618 if (!commit_trans || commit_trans->t_tid != tid) {
623 * Transaction is being committed and we already proceeded to
624 * submitting a flush to fs partition?
626 if (journal->j_fs_dev != journal->j_dev) {
627 if (!commit_trans->t_need_data_flush ||
628 commit_trans->t_state >= T_COMMIT_DFLUSH)
631 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
636 read_unlock(&journal->j_state_lock);
639 EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
642 * Wait for a specified commit to complete.
643 * The caller may not hold the journal lock.
645 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
649 read_lock(&journal->j_state_lock);
650 #ifdef CONFIG_JBD2_DEBUG
651 if (!tid_geq(journal->j_commit_request, tid)) {
653 "%s: error: j_commit_request=%d, tid=%d\n",
654 __func__, journal->j_commit_request, tid);
657 while (tid_gt(tid, journal->j_commit_sequence)) {
658 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
659 tid, journal->j_commit_sequence);
660 wake_up(&journal->j_wait_commit);
661 read_unlock(&journal->j_state_lock);
662 wait_event(journal->j_wait_done_commit,
663 !tid_gt(tid, journal->j_commit_sequence));
664 read_lock(&journal->j_state_lock);
666 read_unlock(&journal->j_state_lock);
668 if (unlikely(is_journal_aborted(journal))) {
669 printk(KERN_EMERG "journal commit I/O error\n");
676 * When this function returns the transaction corresponding to tid
677 * will be completed. If the transaction has currently running, start
678 * committing that transaction before waiting for it to complete. If
679 * the transaction id is stale, it is by definition already completed,
680 * so just return SUCCESS.
682 int jbd2_complete_transaction(journal_t *journal, tid_t tid)
684 int need_to_wait = 1;
686 read_lock(&journal->j_state_lock);
687 if (journal->j_running_transaction &&
688 journal->j_running_transaction->t_tid == tid) {
689 if (journal->j_commit_request != tid) {
690 /* transaction not yet started, so request it */
691 read_unlock(&journal->j_state_lock);
692 jbd2_log_start_commit(journal, tid);
695 } else if (!(journal->j_committing_transaction &&
696 journal->j_committing_transaction->t_tid == tid))
698 read_unlock(&journal->j_state_lock);
702 return jbd2_log_wait_commit(journal, tid);
704 EXPORT_SYMBOL(jbd2_complete_transaction);
707 * Log buffer allocation routines:
710 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
712 unsigned long blocknr;
714 write_lock(&journal->j_state_lock);
715 J_ASSERT(journal->j_free > 1);
717 blocknr = journal->j_head;
720 if (journal->j_head == journal->j_last)
721 journal->j_head = journal->j_first;
722 write_unlock(&journal->j_state_lock);
723 return jbd2_journal_bmap(journal, blocknr, retp);
727 * Conversion of logical to physical block numbers for the journal
729 * On external journals the journal blocks are identity-mapped, so
730 * this is a no-op. If needed, we can use j_blk_offset - everything is
733 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
734 unsigned long long *retp)
737 unsigned long long ret;
739 if (journal->j_inode) {
740 ret = bmap(journal->j_inode, blocknr);
744 printk(KERN_ALERT "%s: journal block not found "
745 "at offset %lu on %s\n",
746 __func__, blocknr, journal->j_devname);
748 __journal_abort_soft(journal, err);
751 *retp = blocknr; /* +journal->j_blk_offset */
757 * We play buffer_head aliasing tricks to write data/metadata blocks to
758 * the journal without copying their contents, but for journal
759 * descriptor blocks we do need to generate bona fide buffers.
761 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
762 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
763 * But we don't bother doing that, so there will be coherency problems with
764 * mmaps of blockdevs which hold live JBD-controlled filesystems.
766 struct buffer_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
768 struct buffer_head *bh;
769 unsigned long long blocknr;
772 err = jbd2_journal_next_log_block(journal, &blocknr);
777 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
781 memset(bh->b_data, 0, journal->j_blocksize);
782 set_buffer_uptodate(bh);
784 BUFFER_TRACE(bh, "return this buffer");
789 * Return tid of the oldest transaction in the journal and block in the journal
790 * where the transaction starts.
792 * If the journal is now empty, return which will be the next transaction ID
793 * we will write and where will that transaction start.
795 * The return value is 0 if journal tail cannot be pushed any further, 1 if
798 int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
799 unsigned long *block)
801 transaction_t *transaction;
804 read_lock(&journal->j_state_lock);
805 spin_lock(&journal->j_list_lock);
806 transaction = journal->j_checkpoint_transactions;
808 *tid = transaction->t_tid;
809 *block = transaction->t_log_start;
810 } else if ((transaction = journal->j_committing_transaction) != NULL) {
811 *tid = transaction->t_tid;
812 *block = transaction->t_log_start;
813 } else if ((transaction = journal->j_running_transaction) != NULL) {
814 *tid = transaction->t_tid;
815 *block = journal->j_head;
817 *tid = journal->j_transaction_sequence;
818 *block = journal->j_head;
820 ret = tid_gt(*tid, journal->j_tail_sequence);
821 spin_unlock(&journal->j_list_lock);
822 read_unlock(&journal->j_state_lock);
828 * Update information in journal structure and in on disk journal superblock
829 * about log tail. This function does not check whether information passed in
830 * really pushes log tail further. It's responsibility of the caller to make
831 * sure provided log tail information is valid (e.g. by holding
832 * j_checkpoint_mutex all the time between computing log tail and calling this
833 * function as is the case with jbd2_cleanup_journal_tail()).
835 * Requires j_checkpoint_mutex
837 void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
841 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
844 * We cannot afford for write to remain in drive's caches since as
845 * soon as we update j_tail, next transaction can start reusing journal
846 * space and if we lose sb update during power failure we'd replay
847 * old transaction with possibly newly overwritten data.
849 jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
850 write_lock(&journal->j_state_lock);
851 freed = block - journal->j_tail;
852 if (block < journal->j_tail)
853 freed += journal->j_last - journal->j_first;
855 trace_jbd2_update_log_tail(journal, tid, block, freed);
857 "Cleaning journal tail from %d to %d (offset %lu), "
859 journal->j_tail_sequence, tid, block, freed);
861 journal->j_free += freed;
862 journal->j_tail_sequence = tid;
863 journal->j_tail = block;
864 write_unlock(&journal->j_state_lock);
868 * This is a variaon of __jbd2_update_log_tail which checks for validity of
869 * provided log tail and locks j_checkpoint_mutex. So it is safe against races
870 * with other threads updating log tail.
872 void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
874 mutex_lock(&journal->j_checkpoint_mutex);
875 if (tid_gt(tid, journal->j_tail_sequence))
876 __jbd2_update_log_tail(journal, tid, block);
877 mutex_unlock(&journal->j_checkpoint_mutex);
880 struct jbd2_stats_proc_session {
882 struct transaction_stats_s *stats;
887 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
889 return *pos ? NULL : SEQ_START_TOKEN;
892 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
897 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
899 struct jbd2_stats_proc_session *s = seq->private;
901 if (v != SEQ_START_TOKEN)
903 seq_printf(seq, "%lu transactions (%lu requested), "
904 "each up to %u blocks\n",
905 s->stats->ts_tid, s->stats->ts_requested,
906 s->journal->j_max_transaction_buffers);
907 if (s->stats->ts_tid == 0)
909 seq_printf(seq, "average: \n %ums waiting for transaction\n",
910 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
911 seq_printf(seq, " %ums request delay\n",
912 (s->stats->ts_requested == 0) ? 0 :
913 jiffies_to_msecs(s->stats->run.rs_request_delay /
914 s->stats->ts_requested));
915 seq_printf(seq, " %ums running transaction\n",
916 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
917 seq_printf(seq, " %ums transaction was being locked\n",
918 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
919 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
920 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
921 seq_printf(seq, " %ums logging transaction\n",
922 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
923 seq_printf(seq, " %lluus average transaction commit time\n",
924 div_u64(s->journal->j_average_commit_time, 1000));
925 seq_printf(seq, " %lu handles per transaction\n",
926 s->stats->run.rs_handle_count / s->stats->ts_tid);
927 seq_printf(seq, " %lu blocks per transaction\n",
928 s->stats->run.rs_blocks / s->stats->ts_tid);
929 seq_printf(seq, " %lu logged blocks per transaction\n",
930 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
934 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
938 static const struct seq_operations jbd2_seq_info_ops = {
939 .start = jbd2_seq_info_start,
940 .next = jbd2_seq_info_next,
941 .stop = jbd2_seq_info_stop,
942 .show = jbd2_seq_info_show,
945 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
947 journal_t *journal = PDE_DATA(inode);
948 struct jbd2_stats_proc_session *s;
951 s = kmalloc(sizeof(*s), GFP_KERNEL);
954 size = sizeof(struct transaction_stats_s);
955 s->stats = kmalloc(size, GFP_KERNEL);
956 if (s->stats == NULL) {
960 spin_lock(&journal->j_history_lock);
961 memcpy(s->stats, &journal->j_stats, size);
962 s->journal = journal;
963 spin_unlock(&journal->j_history_lock);
965 rc = seq_open(file, &jbd2_seq_info_ops);
967 struct seq_file *m = file->private_data;
977 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
979 struct seq_file *seq = file->private_data;
980 struct jbd2_stats_proc_session *s = seq->private;
983 return seq_release(inode, file);
986 static const struct file_operations jbd2_seq_info_fops = {
987 .owner = THIS_MODULE,
988 .open = jbd2_seq_info_open,
991 .release = jbd2_seq_info_release,
994 static struct proc_dir_entry *proc_jbd2_stats;
996 static void jbd2_stats_proc_init(journal_t *journal)
998 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
999 if (journal->j_proc_entry) {
1000 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
1001 &jbd2_seq_info_fops, journal);
1005 static void jbd2_stats_proc_exit(journal_t *journal)
1007 remove_proc_entry("info", journal->j_proc_entry);
1008 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
1012 * Management for journal control blocks: functions to create and
1013 * destroy journal_t structures, and to initialise and read existing
1014 * journal blocks from disk. */
1016 /* First: create and setup a journal_t object in memory. We initialise
1017 * very few fields yet: that has to wait until we have created the
1018 * journal structures from from scratch, or loaded them from disk. */
1020 static journal_t * journal_init_common (void)
1025 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1029 init_waitqueue_head(&journal->j_wait_transaction_locked);
1030 init_waitqueue_head(&journal->j_wait_done_commit);
1031 init_waitqueue_head(&journal->j_wait_commit);
1032 init_waitqueue_head(&journal->j_wait_updates);
1033 mutex_init(&journal->j_barrier);
1034 mutex_init(&journal->j_checkpoint_mutex);
1035 spin_lock_init(&journal->j_revoke_lock);
1036 spin_lock_init(&journal->j_list_lock);
1037 rwlock_init(&journal->j_state_lock);
1039 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
1040 journal->j_min_batch_time = 0;
1041 journal->j_max_batch_time = 15000; /* 15ms */
1043 /* The journal is marked for error until we succeed with recovery! */
1044 journal->j_flags = JBD2_ABORT;
1046 /* Set up a default-sized revoke table for the new mount. */
1047 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1053 spin_lock_init(&journal->j_history_lock);
1058 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1060 * Create a journal structure assigned some fixed set of disk blocks to
1061 * the journal. We don't actually touch those disk blocks yet, but we
1062 * need to set up all of the mapping information to tell the journaling
1063 * system where the journal blocks are.
1068 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1069 * @bdev: Block device on which to create the journal
1070 * @fs_dev: Device which hold journalled filesystem for this journal.
1071 * @start: Block nr Start of journal.
1072 * @len: Length of the journal in blocks.
1073 * @blocksize: blocksize of journalling device
1075 * Returns: a newly created journal_t *
1077 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1078 * range of blocks on an arbitrary block device.
1081 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1082 struct block_device *fs_dev,
1083 unsigned long long start, int len, int blocksize)
1085 journal_t *journal = journal_init_common();
1086 struct buffer_head *bh;
1093 /* journal descriptor can store up to n blocks -bzzz */
1094 journal->j_blocksize = blocksize;
1095 journal->j_dev = bdev;
1096 journal->j_fs_dev = fs_dev;
1097 journal->j_blk_offset = start;
1098 journal->j_maxlen = len;
1099 bdevname(journal->j_dev, journal->j_devname);
1100 p = journal->j_devname;
1101 while ((p = strchr(p, '/')))
1103 jbd2_stats_proc_init(journal);
1104 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1105 journal->j_wbufsize = n;
1106 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1107 if (!journal->j_wbuf) {
1108 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1113 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1116 "%s: Cannot get buffer for journal superblock\n",
1120 journal->j_sb_buffer = bh;
1121 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1125 kfree(journal->j_wbuf);
1126 jbd2_stats_proc_exit(journal);
1132 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1133 * @inode: An inode to create the journal in
1135 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1136 * the journal. The inode must exist already, must support bmap() and
1137 * must have all data blocks preallocated.
1139 journal_t * jbd2_journal_init_inode (struct inode *inode)
1141 struct buffer_head *bh;
1142 journal_t *journal = journal_init_common();
1146 unsigned long long blocknr;
1151 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1152 journal->j_inode = inode;
1153 bdevname(journal->j_dev, journal->j_devname);
1154 p = journal->j_devname;
1155 while ((p = strchr(p, '/')))
1157 p = journal->j_devname + strlen(journal->j_devname);
1158 sprintf(p, "-%lu", journal->j_inode->i_ino);
1160 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1161 journal, inode->i_sb->s_id, inode->i_ino,
1162 (long long) inode->i_size,
1163 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1165 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1166 journal->j_blocksize = inode->i_sb->s_blocksize;
1167 jbd2_stats_proc_init(journal);
1169 /* journal descriptor can store up to n blocks -bzzz */
1170 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1171 journal->j_wbufsize = n;
1172 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1173 if (!journal->j_wbuf) {
1174 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1179 err = jbd2_journal_bmap(journal, 0, &blocknr);
1180 /* If that failed, give up */
1182 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
1187 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1190 "%s: Cannot get buffer for journal superblock\n",
1194 journal->j_sb_buffer = bh;
1195 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1199 kfree(journal->j_wbuf);
1200 jbd2_stats_proc_exit(journal);
1206 * If the journal init or create aborts, we need to mark the journal
1207 * superblock as being NULL to prevent the journal destroy from writing
1208 * back a bogus superblock.
1210 static void journal_fail_superblock (journal_t *journal)
1212 struct buffer_head *bh = journal->j_sb_buffer;
1214 journal->j_sb_buffer = NULL;
1218 * Given a journal_t structure, initialise the various fields for
1219 * startup of a new journaling session. We use this both when creating
1220 * a journal, and after recovering an old journal to reset it for
1224 static int journal_reset(journal_t *journal)
1226 journal_superblock_t *sb = journal->j_superblock;
1227 unsigned long long first, last;
1229 first = be32_to_cpu(sb->s_first);
1230 last = be32_to_cpu(sb->s_maxlen);
1231 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1232 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
1234 journal_fail_superblock(journal);
1238 journal->j_first = first;
1239 journal->j_last = last;
1241 journal->j_head = first;
1242 journal->j_tail = first;
1243 journal->j_free = last - first;
1245 journal->j_tail_sequence = journal->j_transaction_sequence;
1246 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1247 journal->j_commit_request = journal->j_commit_sequence;
1249 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1252 * As a special case, if the on-disk copy is already marked as needing
1253 * no recovery (s_start == 0), then we can safely defer the superblock
1254 * update until the next commit by setting JBD2_FLUSHED. This avoids
1255 * attempting a write to a potential-readonly device.
1257 if (sb->s_start == 0) {
1258 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
1259 "(start %ld, seq %d, errno %d)\n",
1260 journal->j_tail, journal->j_tail_sequence,
1262 journal->j_flags |= JBD2_FLUSHED;
1264 /* Lock here to make assertions happy... */
1265 mutex_lock(&journal->j_checkpoint_mutex);
1267 * Update log tail information. We use WRITE_FUA since new
1268 * transaction will start reusing journal space and so we
1269 * must make sure information about current log tail is on
1272 jbd2_journal_update_sb_log_tail(journal,
1273 journal->j_tail_sequence,
1276 mutex_unlock(&journal->j_checkpoint_mutex);
1278 return jbd2_journal_start_thread(journal);
1281 static void jbd2_write_superblock(journal_t *journal, int write_op)
1283 struct buffer_head *bh = journal->j_sb_buffer;
1286 trace_jbd2_write_superblock(journal, write_op);
1287 if (!(journal->j_flags & JBD2_BARRIER))
1288 write_op &= ~(REQ_FUA | REQ_FLUSH);
1290 if (buffer_write_io_error(bh)) {
1292 * Oh, dear. A previous attempt to write the journal
1293 * superblock failed. This could happen because the
1294 * USB device was yanked out. Or it could happen to
1295 * be a transient write error and maybe the block will
1296 * be remapped. Nothing we can do but to retry the
1297 * write and hope for the best.
1299 printk(KERN_ERR "JBD2: previous I/O error detected "
1300 "for journal superblock update for %s.\n",
1301 journal->j_devname);
1302 clear_buffer_write_io_error(bh);
1303 set_buffer_uptodate(bh);
1306 bh->b_end_io = end_buffer_write_sync;
1307 ret = submit_bh(write_op, bh);
1309 if (buffer_write_io_error(bh)) {
1310 clear_buffer_write_io_error(bh);
1311 set_buffer_uptodate(bh);
1315 printk(KERN_ERR "JBD2: Error %d detected when updating "
1316 "journal superblock for %s.\n", ret,
1317 journal->j_devname);
1322 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1323 * @journal: The journal to update.
1324 * @tail_tid: TID of the new transaction at the tail of the log
1325 * @tail_block: The first block of the transaction at the tail of the log
1326 * @write_op: With which operation should we write the journal sb
1328 * Update a journal's superblock information about log tail and write it to
1329 * disk, waiting for the IO to complete.
1331 void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1332 unsigned long tail_block, int write_op)
1334 journal_superblock_t *sb = journal->j_superblock;
1336 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1337 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1338 tail_block, tail_tid);
1340 sb->s_sequence = cpu_to_be32(tail_tid);
1341 sb->s_start = cpu_to_be32(tail_block);
1343 jbd2_write_superblock(journal, write_op);
1345 /* Log is no longer empty */
1346 write_lock(&journal->j_state_lock);
1347 WARN_ON(!sb->s_sequence);
1348 journal->j_flags &= ~JBD2_FLUSHED;
1349 write_unlock(&journal->j_state_lock);
1353 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1354 * @journal: The journal to update.
1356 * Update a journal's dynamic superblock fields to show that journal is empty.
1357 * Write updated superblock to disk waiting for IO to complete.
1359 static void jbd2_mark_journal_empty(journal_t *journal)
1361 journal_superblock_t *sb = journal->j_superblock;
1363 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1364 read_lock(&journal->j_state_lock);
1365 /* Is it already empty? */
1366 if (sb->s_start == 0) {
1367 read_unlock(&journal->j_state_lock);
1370 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1371 journal->j_tail_sequence);
1373 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1374 sb->s_start = cpu_to_be32(0);
1375 read_unlock(&journal->j_state_lock);
1377 jbd2_write_superblock(journal, WRITE_FUA);
1379 /* Log is no longer empty */
1380 write_lock(&journal->j_state_lock);
1381 journal->j_flags |= JBD2_FLUSHED;
1382 write_unlock(&journal->j_state_lock);
1387 * jbd2_journal_update_sb_errno() - Update error in the journal.
1388 * @journal: The journal to update.
1390 * Update a journal's errno. Write updated superblock to disk waiting for IO
1393 void jbd2_journal_update_sb_errno(journal_t *journal)
1395 journal_superblock_t *sb = journal->j_superblock;
1397 read_lock(&journal->j_state_lock);
1398 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1400 sb->s_errno = cpu_to_be32(journal->j_errno);
1401 jbd2_superblock_csum_set(journal, sb);
1402 read_unlock(&journal->j_state_lock);
1404 jbd2_write_superblock(journal, WRITE_SYNC);
1406 EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
1409 * Read the superblock for a given journal, performing initial
1410 * validation of the format.
1412 static int journal_get_superblock(journal_t *journal)
1414 struct buffer_head *bh;
1415 journal_superblock_t *sb;
1418 bh = journal->j_sb_buffer;
1420 J_ASSERT(bh != NULL);
1421 if (!buffer_uptodate(bh)) {
1422 ll_rw_block(READ, 1, &bh);
1424 if (!buffer_uptodate(bh)) {
1426 "JBD2: IO error reading journal superblock\n");
1431 if (buffer_verified(bh))
1434 sb = journal->j_superblock;
1438 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1439 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1440 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1444 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1445 case JBD2_SUPERBLOCK_V1:
1446 journal->j_format_version = 1;
1448 case JBD2_SUPERBLOCK_V2:
1449 journal->j_format_version = 2;
1452 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1456 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1457 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1458 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1459 printk(KERN_WARNING "JBD2: journal file too short\n");
1463 if (be32_to_cpu(sb->s_first) == 0 ||
1464 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1466 "JBD2: Invalid start block of journal: %u\n",
1467 be32_to_cpu(sb->s_first));
1471 if (JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM) &&
1472 JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1473 /* Can't have checksum v1 and v2 on at the same time! */
1474 printk(KERN_ERR "JBD: Can't enable checksumming v1 and v2 "
1475 "at the same time!\n");
1479 if (!jbd2_verify_csum_type(journal, sb)) {
1480 printk(KERN_ERR "JBD: Unknown checksum type\n");
1484 /* Load the checksum driver */
1485 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1486 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1487 if (IS_ERR(journal->j_chksum_driver)) {
1488 printk(KERN_ERR "JBD: Cannot load crc32c driver.\n");
1489 err = PTR_ERR(journal->j_chksum_driver);
1490 journal->j_chksum_driver = NULL;
1495 /* Check superblock checksum */
1496 if (!jbd2_superblock_csum_verify(journal, sb)) {
1497 printk(KERN_ERR "JBD: journal checksum error\n");
1501 /* Precompute checksum seed for all metadata */
1502 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
1503 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1504 sizeof(sb->s_uuid));
1506 set_buffer_verified(bh);
1511 journal_fail_superblock(journal);
1516 * Load the on-disk journal superblock and read the key fields into the
1520 static int load_superblock(journal_t *journal)
1523 journal_superblock_t *sb;
1525 err = journal_get_superblock(journal);
1529 sb = journal->j_superblock;
1531 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1532 journal->j_tail = be32_to_cpu(sb->s_start);
1533 journal->j_first = be32_to_cpu(sb->s_first);
1534 journal->j_last = be32_to_cpu(sb->s_maxlen);
1535 journal->j_errno = be32_to_cpu(sb->s_errno);
1542 * int jbd2_journal_load() - Read journal from disk.
1543 * @journal: Journal to act on.
1545 * Given a journal_t structure which tells us which disk blocks contain
1546 * a journal, read the journal from disk to initialise the in-memory
1549 int jbd2_journal_load(journal_t *journal)
1552 journal_superblock_t *sb;
1554 err = load_superblock(journal);
1558 sb = journal->j_superblock;
1559 /* If this is a V2 superblock, then we have to check the
1560 * features flags on it. */
1562 if (journal->j_format_version >= 2) {
1563 if ((sb->s_feature_ro_compat &
1564 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1565 (sb->s_feature_incompat &
1566 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1568 "JBD2: Unrecognised features on journal\n");
1574 * Create a slab for this blocksize
1576 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1580 /* Let the recovery code check whether it needs to recover any
1581 * data from the journal. */
1582 if (jbd2_journal_recover(journal))
1583 goto recovery_error;
1585 if (journal->j_failed_commit) {
1586 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1587 "is corrupt.\n", journal->j_failed_commit,
1588 journal->j_devname);
1592 /* OK, we've finished with the dynamic journal bits:
1593 * reinitialise the dynamic contents of the superblock in memory
1594 * and reset them on disk. */
1595 if (journal_reset(journal))
1596 goto recovery_error;
1598 journal->j_flags &= ~JBD2_ABORT;
1599 journal->j_flags |= JBD2_LOADED;
1603 printk(KERN_WARNING "JBD2: recovery failed\n");
1608 * void jbd2_journal_destroy() - Release a journal_t structure.
1609 * @journal: Journal to act on.
1611 * Release a journal_t structure once it is no longer in use by the
1613 * Return <0 if we couldn't clean up the journal.
1615 int jbd2_journal_destroy(journal_t *journal)
1619 /* Wait for the commit thread to wake up and die. */
1620 journal_kill_thread(journal);
1622 /* Force a final log commit */
1623 if (journal->j_running_transaction)
1624 jbd2_journal_commit_transaction(journal);
1626 /* Force any old transactions to disk */
1628 /* Totally anal locking here... */
1629 spin_lock(&journal->j_list_lock);
1630 while (journal->j_checkpoint_transactions != NULL) {
1631 spin_unlock(&journal->j_list_lock);
1632 mutex_lock(&journal->j_checkpoint_mutex);
1633 jbd2_log_do_checkpoint(journal);
1634 mutex_unlock(&journal->j_checkpoint_mutex);
1635 spin_lock(&journal->j_list_lock);
1638 J_ASSERT(journal->j_running_transaction == NULL);
1639 J_ASSERT(journal->j_committing_transaction == NULL);
1640 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1641 spin_unlock(&journal->j_list_lock);
1643 if (journal->j_sb_buffer) {
1644 if (!is_journal_aborted(journal)) {
1645 mutex_lock(&journal->j_checkpoint_mutex);
1646 jbd2_mark_journal_empty(journal);
1647 mutex_unlock(&journal->j_checkpoint_mutex);
1650 brelse(journal->j_sb_buffer);
1653 if (journal->j_proc_entry)
1654 jbd2_stats_proc_exit(journal);
1655 if (journal->j_inode)
1656 iput(journal->j_inode);
1657 if (journal->j_revoke)
1658 jbd2_journal_destroy_revoke(journal);
1659 if (journal->j_chksum_driver)
1660 crypto_free_shash(journal->j_chksum_driver);
1661 kfree(journal->j_wbuf);
1669 *int jbd2_journal_check_used_features () - Check if features specified are used.
1670 * @journal: Journal to check.
1671 * @compat: bitmask of compatible features
1672 * @ro: bitmask of features that force read-only mount
1673 * @incompat: bitmask of incompatible features
1675 * Check whether the journal uses all of a given set of
1676 * features. Return true (non-zero) if it does.
1679 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1680 unsigned long ro, unsigned long incompat)
1682 journal_superblock_t *sb;
1684 if (!compat && !ro && !incompat)
1686 /* Load journal superblock if it is not loaded yet. */
1687 if (journal->j_format_version == 0 &&
1688 journal_get_superblock(journal) != 0)
1690 if (journal->j_format_version == 1)
1693 sb = journal->j_superblock;
1695 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1696 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1697 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1704 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1705 * @journal: Journal to check.
1706 * @compat: bitmask of compatible features
1707 * @ro: bitmask of features that force read-only mount
1708 * @incompat: bitmask of incompatible features
1710 * Check whether the journaling code supports the use of
1711 * all of a given set of features on this journal. Return true
1712 * (non-zero) if it can. */
1714 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1715 unsigned long ro, unsigned long incompat)
1717 if (!compat && !ro && !incompat)
1720 /* We can support any known requested features iff the
1721 * superblock is in version 2. Otherwise we fail to support any
1722 * extended sb features. */
1724 if (journal->j_format_version != 2)
1727 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1728 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1729 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1736 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1737 * @journal: Journal to act on.
1738 * @compat: bitmask of compatible features
1739 * @ro: bitmask of features that force read-only mount
1740 * @incompat: bitmask of incompatible features
1742 * Mark a given journal feature as present on the
1743 * superblock. Returns true if the requested features could be set.
1747 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1748 unsigned long ro, unsigned long incompat)
1750 #define INCOMPAT_FEATURE_ON(f) \
1751 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1752 #define COMPAT_FEATURE_ON(f) \
1753 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
1754 journal_superblock_t *sb;
1756 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1759 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1762 /* Asking for checksumming v2 and v1? Only give them v2. */
1763 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2 &&
1764 compat & JBD2_FEATURE_COMPAT_CHECKSUM)
1765 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
1767 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1768 compat, ro, incompat);
1770 sb = journal->j_superblock;
1772 /* If enabling v2 checksums, update superblock */
1773 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1774 sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
1775 sb->s_feature_compat &=
1776 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
1778 /* Load the checksum driver */
1779 if (journal->j_chksum_driver == NULL) {
1780 journal->j_chksum_driver = crypto_alloc_shash("crc32c",
1782 if (IS_ERR(journal->j_chksum_driver)) {
1783 printk(KERN_ERR "JBD: Cannot load crc32c "
1785 journal->j_chksum_driver = NULL;
1790 /* Precompute checksum seed for all metadata */
1791 if (JBD2_HAS_INCOMPAT_FEATURE(journal,
1792 JBD2_FEATURE_INCOMPAT_CSUM_V2))
1793 journal->j_csum_seed = jbd2_chksum(journal, ~0,
1795 sizeof(sb->s_uuid));
1798 /* If enabling v1 checksums, downgrade superblock */
1799 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
1800 sb->s_feature_incompat &=
1801 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2);
1803 sb->s_feature_compat |= cpu_to_be32(compat);
1804 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1805 sb->s_feature_incompat |= cpu_to_be32(incompat);
1808 #undef COMPAT_FEATURE_ON
1809 #undef INCOMPAT_FEATURE_ON
1813 * jbd2_journal_clear_features () - Clear a given journal feature in the
1815 * @journal: Journal to act on.
1816 * @compat: bitmask of compatible features
1817 * @ro: bitmask of features that force read-only mount
1818 * @incompat: bitmask of incompatible features
1820 * Clear a given journal feature as present on the
1823 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1824 unsigned long ro, unsigned long incompat)
1826 journal_superblock_t *sb;
1828 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1829 compat, ro, incompat);
1831 sb = journal->j_superblock;
1833 sb->s_feature_compat &= ~cpu_to_be32(compat);
1834 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1835 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1837 EXPORT_SYMBOL(jbd2_journal_clear_features);
1840 * int jbd2_journal_flush () - Flush journal
1841 * @journal: Journal to act on.
1843 * Flush all data for a given journal to disk and empty the journal.
1844 * Filesystems can use this when remounting readonly to ensure that
1845 * recovery does not need to happen on remount.
1848 int jbd2_journal_flush(journal_t *journal)
1851 transaction_t *transaction = NULL;
1853 write_lock(&journal->j_state_lock);
1855 /* Force everything buffered to the log... */
1856 if (journal->j_running_transaction) {
1857 transaction = journal->j_running_transaction;
1858 __jbd2_log_start_commit(journal, transaction->t_tid);
1859 } else if (journal->j_committing_transaction)
1860 transaction = journal->j_committing_transaction;
1862 /* Wait for the log commit to complete... */
1864 tid_t tid = transaction->t_tid;
1866 write_unlock(&journal->j_state_lock);
1867 jbd2_log_wait_commit(journal, tid);
1869 write_unlock(&journal->j_state_lock);
1872 /* ...and flush everything in the log out to disk. */
1873 spin_lock(&journal->j_list_lock);
1874 while (!err && journal->j_checkpoint_transactions != NULL) {
1875 spin_unlock(&journal->j_list_lock);
1876 mutex_lock(&journal->j_checkpoint_mutex);
1877 err = jbd2_log_do_checkpoint(journal);
1878 mutex_unlock(&journal->j_checkpoint_mutex);
1879 spin_lock(&journal->j_list_lock);
1881 spin_unlock(&journal->j_list_lock);
1883 if (is_journal_aborted(journal))
1886 mutex_lock(&journal->j_checkpoint_mutex);
1887 jbd2_cleanup_journal_tail(journal);
1889 /* Finally, mark the journal as really needing no recovery.
1890 * This sets s_start==0 in the underlying superblock, which is
1891 * the magic code for a fully-recovered superblock. Any future
1892 * commits of data to the journal will restore the current
1894 jbd2_mark_journal_empty(journal);
1895 mutex_unlock(&journal->j_checkpoint_mutex);
1896 write_lock(&journal->j_state_lock);
1897 J_ASSERT(!journal->j_running_transaction);
1898 J_ASSERT(!journal->j_committing_transaction);
1899 J_ASSERT(!journal->j_checkpoint_transactions);
1900 J_ASSERT(journal->j_head == journal->j_tail);
1901 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1902 write_unlock(&journal->j_state_lock);
1907 * int jbd2_journal_wipe() - Wipe journal contents
1908 * @journal: Journal to act on.
1909 * @write: flag (see below)
1911 * Wipe out all of the contents of a journal, safely. This will produce
1912 * a warning if the journal contains any valid recovery information.
1913 * Must be called between journal_init_*() and jbd2_journal_load().
1915 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1916 * we merely suppress recovery.
1919 int jbd2_journal_wipe(journal_t *journal, int write)
1923 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1925 err = load_superblock(journal);
1929 if (!journal->j_tail)
1932 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
1933 write ? "Clearing" : "Ignoring");
1935 err = jbd2_journal_skip_recovery(journal);
1937 /* Lock to make assertions happy... */
1938 mutex_lock(&journal->j_checkpoint_mutex);
1939 jbd2_mark_journal_empty(journal);
1940 mutex_unlock(&journal->j_checkpoint_mutex);
1948 * Journal abort has very specific semantics, which we describe
1949 * for journal abort.
1951 * Two internal functions, which provide abort to the jbd layer
1956 * Quick version for internal journal use (doesn't lock the journal).
1957 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1958 * and don't attempt to make any other journal updates.
1960 void __jbd2_journal_abort_hard(journal_t *journal)
1962 transaction_t *transaction;
1964 if (journal->j_flags & JBD2_ABORT)
1967 printk(KERN_ERR "Aborting journal on device %s.\n",
1968 journal->j_devname);
1970 write_lock(&journal->j_state_lock);
1971 journal->j_flags |= JBD2_ABORT;
1972 transaction = journal->j_running_transaction;
1974 __jbd2_log_start_commit(journal, transaction->t_tid);
1975 write_unlock(&journal->j_state_lock);
1978 /* Soft abort: record the abort error status in the journal superblock,
1979 * but don't do any other IO. */
1980 static void __journal_abort_soft (journal_t *journal, int errno)
1982 if (journal->j_flags & JBD2_ABORT)
1985 if (!journal->j_errno)
1986 journal->j_errno = errno;
1988 __jbd2_journal_abort_hard(journal);
1991 jbd2_journal_update_sb_errno(journal);
1995 * void jbd2_journal_abort () - Shutdown the journal immediately.
1996 * @journal: the journal to shutdown.
1997 * @errno: an error number to record in the journal indicating
1998 * the reason for the shutdown.
2000 * Perform a complete, immediate shutdown of the ENTIRE
2001 * journal (not of a single transaction). This operation cannot be
2002 * undone without closing and reopening the journal.
2004 * The jbd2_journal_abort function is intended to support higher level error
2005 * recovery mechanisms such as the ext2/ext3 remount-readonly error
2008 * Journal abort has very specific semantics. Any existing dirty,
2009 * unjournaled buffers in the main filesystem will still be written to
2010 * disk by bdflush, but the journaling mechanism will be suspended
2011 * immediately and no further transaction commits will be honoured.
2013 * Any dirty, journaled buffers will be written back to disk without
2014 * hitting the journal. Atomicity cannot be guaranteed on an aborted
2015 * filesystem, but we _do_ attempt to leave as much data as possible
2016 * behind for fsck to use for cleanup.
2018 * Any attempt to get a new transaction handle on a journal which is in
2019 * ABORT state will just result in an -EROFS error return. A
2020 * jbd2_journal_stop on an existing handle will return -EIO if we have
2021 * entered abort state during the update.
2023 * Recursive transactions are not disturbed by journal abort until the
2024 * final jbd2_journal_stop, which will receive the -EIO error.
2026 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
2027 * which will be recorded (if possible) in the journal superblock. This
2028 * allows a client to record failure conditions in the middle of a
2029 * transaction without having to complete the transaction to record the
2030 * failure to disk. ext3_error, for example, now uses this
2033 * Errors which originate from within the journaling layer will NOT
2034 * supply an errno; a null errno implies that absolutely no further
2035 * writes are done to the journal (unless there are any already in
2040 void jbd2_journal_abort(journal_t *journal, int errno)
2042 __journal_abort_soft(journal, errno);
2046 * int jbd2_journal_errno () - returns the journal's error state.
2047 * @journal: journal to examine.
2049 * This is the errno number set with jbd2_journal_abort(), the last
2050 * time the journal was mounted - if the journal was stopped
2051 * without calling abort this will be 0.
2053 * If the journal has been aborted on this mount time -EROFS will
2056 int jbd2_journal_errno(journal_t *journal)
2060 read_lock(&journal->j_state_lock);
2061 if (journal->j_flags & JBD2_ABORT)
2064 err = journal->j_errno;
2065 read_unlock(&journal->j_state_lock);
2070 * int jbd2_journal_clear_err () - clears the journal's error state
2071 * @journal: journal to act on.
2073 * An error must be cleared or acked to take a FS out of readonly
2076 int jbd2_journal_clear_err(journal_t *journal)
2080 write_lock(&journal->j_state_lock);
2081 if (journal->j_flags & JBD2_ABORT)
2084 journal->j_errno = 0;
2085 write_unlock(&journal->j_state_lock);
2090 * void jbd2_journal_ack_err() - Ack journal err.
2091 * @journal: journal to act on.
2093 * An error must be cleared or acked to take a FS out of readonly
2096 void jbd2_journal_ack_err(journal_t *journal)
2098 write_lock(&journal->j_state_lock);
2099 if (journal->j_errno)
2100 journal->j_flags |= JBD2_ACK_ERR;
2101 write_unlock(&journal->j_state_lock);
2104 int jbd2_journal_blocks_per_page(struct inode *inode)
2106 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2110 * helper functions to deal with 32 or 64bit block numbers.
2112 size_t journal_tag_bytes(journal_t *journal)
2114 journal_block_tag_t tag;
2117 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
2118 x += sizeof(tag.t_checksum);
2120 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
2121 return x + JBD2_TAG_SIZE64;
2123 return x + JBD2_TAG_SIZE32;
2127 * JBD memory management
2129 * These functions are used to allocate block-sized chunks of memory
2130 * used for making copies of buffer_head data. Very often it will be
2131 * page-sized chunks of data, but sometimes it will be in
2132 * sub-page-size chunks. (For example, 16k pages on Power systems
2133 * with a 4k block file system.) For blocks smaller than a page, we
2134 * use a SLAB allocator. There are slab caches for each block size,
2135 * which are allocated at mount time, if necessary, and we only free
2136 * (all of) the slab caches when/if the jbd2 module is unloaded. For
2137 * this reason we don't need to a mutex to protect access to
2138 * jbd2_slab[] allocating or releasing memory; only in
2139 * jbd2_journal_create_slab().
2141 #define JBD2_MAX_SLABS 8
2142 static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
2144 static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2145 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2146 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2150 static void jbd2_journal_destroy_slabs(void)
2154 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2156 kmem_cache_destroy(jbd2_slab[i]);
2157 jbd2_slab[i] = NULL;
2161 static int jbd2_journal_create_slab(size_t size)
2163 static DEFINE_MUTEX(jbd2_slab_create_mutex);
2164 int i = order_base_2(size) - 10;
2167 if (size == PAGE_SIZE)
2170 if (i >= JBD2_MAX_SLABS)
2173 if (unlikely(i < 0))
2175 mutex_lock(&jbd2_slab_create_mutex);
2177 mutex_unlock(&jbd2_slab_create_mutex);
2178 return 0; /* Already created */
2181 slab_size = 1 << (i+10);
2182 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2183 slab_size, 0, NULL);
2184 mutex_unlock(&jbd2_slab_create_mutex);
2185 if (!jbd2_slab[i]) {
2186 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2192 static struct kmem_cache *get_slab(size_t size)
2194 int i = order_base_2(size) - 10;
2196 BUG_ON(i >= JBD2_MAX_SLABS);
2197 if (unlikely(i < 0))
2199 BUG_ON(jbd2_slab[i] == NULL);
2200 return jbd2_slab[i];
2203 void *jbd2_alloc(size_t size, gfp_t flags)
2207 BUG_ON(size & (size-1)); /* Must be a power of 2 */
2209 flags |= __GFP_REPEAT;
2210 if (size == PAGE_SIZE)
2211 ptr = (void *)__get_free_pages(flags, 0);
2212 else if (size > PAGE_SIZE) {
2213 int order = get_order(size);
2216 ptr = (void *)__get_free_pages(flags, order);
2218 ptr = vmalloc(size);
2220 ptr = kmem_cache_alloc(get_slab(size), flags);
2222 /* Check alignment; SLUB has gotten this wrong in the past,
2223 * and this can lead to user data corruption! */
2224 BUG_ON(((unsigned long) ptr) & (size-1));
2229 void jbd2_free(void *ptr, size_t size)
2231 if (size == PAGE_SIZE) {
2232 free_pages((unsigned long)ptr, 0);
2235 if (size > PAGE_SIZE) {
2236 int order = get_order(size);
2239 free_pages((unsigned long)ptr, order);
2244 kmem_cache_free(get_slab(size), ptr);
2248 * Journal_head storage management
2250 static struct kmem_cache *jbd2_journal_head_cache;
2251 #ifdef CONFIG_JBD2_DEBUG
2252 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2255 static int jbd2_journal_init_journal_head_cache(void)
2259 J_ASSERT(jbd2_journal_head_cache == NULL);
2260 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2261 sizeof(struct journal_head),
2263 SLAB_TEMPORARY, /* flags */
2266 if (!jbd2_journal_head_cache) {
2268 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
2273 static void jbd2_journal_destroy_journal_head_cache(void)
2275 if (jbd2_journal_head_cache) {
2276 kmem_cache_destroy(jbd2_journal_head_cache);
2277 jbd2_journal_head_cache = NULL;
2282 * journal_head splicing and dicing
2284 static struct journal_head *journal_alloc_journal_head(void)
2286 struct journal_head *ret;
2288 #ifdef CONFIG_JBD2_DEBUG
2289 atomic_inc(&nr_journal_heads);
2291 ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
2293 jbd_debug(1, "out of memory for journal_head\n");
2294 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
2297 ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
2303 static void journal_free_journal_head(struct journal_head *jh)
2305 #ifdef CONFIG_JBD2_DEBUG
2306 atomic_dec(&nr_journal_heads);
2307 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2309 kmem_cache_free(jbd2_journal_head_cache, jh);
2313 * A journal_head is attached to a buffer_head whenever JBD has an
2314 * interest in the buffer.
2316 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2317 * is set. This bit is tested in core kernel code where we need to take
2318 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2321 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2323 * When a buffer has its BH_JBD bit set it is immune from being released by
2324 * core kernel code, mainly via ->b_count.
2326 * A journal_head is detached from its buffer_head when the journal_head's
2327 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2328 * transaction (b_cp_transaction) hold their references to b_jcount.
2330 * Various places in the kernel want to attach a journal_head to a buffer_head
2331 * _before_ attaching the journal_head to a transaction. To protect the
2332 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2333 * journal_head's b_jcount refcount by one. The caller must call
2334 * jbd2_journal_put_journal_head() to undo this.
2336 * So the typical usage would be:
2338 * (Attach a journal_head if needed. Increments b_jcount)
2339 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2341 * (Get another reference for transaction)
2342 * jbd2_journal_grab_journal_head(bh);
2343 * jh->b_transaction = xxx;
2344 * (Put original reference)
2345 * jbd2_journal_put_journal_head(jh);
2349 * Give a buffer_head a journal_head.
2353 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2355 struct journal_head *jh;
2356 struct journal_head *new_jh = NULL;
2359 if (!buffer_jbd(bh))
2360 new_jh = journal_alloc_journal_head();
2362 jbd_lock_bh_journal_head(bh);
2363 if (buffer_jbd(bh)) {
2367 (atomic_read(&bh->b_count) > 0) ||
2368 (bh->b_page && bh->b_page->mapping));
2371 jbd_unlock_bh_journal_head(bh);
2376 new_jh = NULL; /* We consumed it */
2381 BUFFER_TRACE(bh, "added journal_head");
2384 jbd_unlock_bh_journal_head(bh);
2386 journal_free_journal_head(new_jh);
2387 return bh->b_private;
2391 * Grab a ref against this buffer_head's journal_head. If it ended up not
2392 * having a journal_head, return NULL
2394 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2396 struct journal_head *jh = NULL;
2398 jbd_lock_bh_journal_head(bh);
2399 if (buffer_jbd(bh)) {
2403 jbd_unlock_bh_journal_head(bh);
2407 static void __journal_remove_journal_head(struct buffer_head *bh)
2409 struct journal_head *jh = bh2jh(bh);
2411 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2412 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2413 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2414 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2415 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2416 J_ASSERT_BH(bh, buffer_jbd(bh));
2417 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2418 BUFFER_TRACE(bh, "remove journal_head");
2419 if (jh->b_frozen_data) {
2420 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2421 jbd2_free(jh->b_frozen_data, bh->b_size);
2423 if (jh->b_committed_data) {
2424 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2425 jbd2_free(jh->b_committed_data, bh->b_size);
2427 bh->b_private = NULL;
2428 jh->b_bh = NULL; /* debug, really */
2429 clear_buffer_jbd(bh);
2430 journal_free_journal_head(jh);
2434 * Drop a reference on the passed journal_head. If it fell to zero then
2435 * release the journal_head from the buffer_head.
2437 void jbd2_journal_put_journal_head(struct journal_head *jh)
2439 struct buffer_head *bh = jh2bh(jh);
2441 jbd_lock_bh_journal_head(bh);
2442 J_ASSERT_JH(jh, jh->b_jcount > 0);
2444 if (!jh->b_jcount) {
2445 __journal_remove_journal_head(bh);
2446 jbd_unlock_bh_journal_head(bh);
2449 jbd_unlock_bh_journal_head(bh);
2453 * Initialize jbd inode head
2455 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2457 jinode->i_transaction = NULL;
2458 jinode->i_next_transaction = NULL;
2459 jinode->i_vfs_inode = inode;
2460 jinode->i_flags = 0;
2461 INIT_LIST_HEAD(&jinode->i_list);
2465 * Function to be called before we start removing inode from memory (i.e.,
2466 * clear_inode() is a fine place to be called from). It removes inode from
2467 * transaction's lists.
2469 void jbd2_journal_release_jbd_inode(journal_t *journal,
2470 struct jbd2_inode *jinode)
2475 spin_lock(&journal->j_list_lock);
2476 /* Is commit writing out inode - we have to wait */
2477 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
2478 wait_queue_head_t *wq;
2479 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2480 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2481 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2482 spin_unlock(&journal->j_list_lock);
2484 finish_wait(wq, &wait.wait);
2488 if (jinode->i_transaction) {
2489 list_del(&jinode->i_list);
2490 jinode->i_transaction = NULL;
2492 spin_unlock(&journal->j_list_lock);
2496 #ifdef CONFIG_PROC_FS
2498 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2500 static void __init jbd2_create_jbd_stats_proc_entry(void)
2502 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2505 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2507 if (proc_jbd2_stats)
2508 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2513 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2514 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2518 struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
2520 static int __init jbd2_journal_init_handle_cache(void)
2522 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
2523 if (jbd2_handle_cache == NULL) {
2524 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2527 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2528 if (jbd2_inode_cache == NULL) {
2529 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2530 kmem_cache_destroy(jbd2_handle_cache);
2536 static void jbd2_journal_destroy_handle_cache(void)
2538 if (jbd2_handle_cache)
2539 kmem_cache_destroy(jbd2_handle_cache);
2540 if (jbd2_inode_cache)
2541 kmem_cache_destroy(jbd2_inode_cache);
2546 * Module startup and shutdown
2549 static int __init journal_init_caches(void)
2553 ret = jbd2_journal_init_revoke_caches();
2555 ret = jbd2_journal_init_journal_head_cache();
2557 ret = jbd2_journal_init_handle_cache();
2559 ret = jbd2_journal_init_transaction_cache();
2563 static void jbd2_journal_destroy_caches(void)
2565 jbd2_journal_destroy_revoke_caches();
2566 jbd2_journal_destroy_journal_head_cache();
2567 jbd2_journal_destroy_handle_cache();
2568 jbd2_journal_destroy_transaction_cache();
2569 jbd2_journal_destroy_slabs();
2572 static int __init journal_init(void)
2576 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2578 ret = journal_init_caches();
2580 jbd2_create_jbd_stats_proc_entry();
2582 jbd2_journal_destroy_caches();
2587 static void __exit journal_exit(void)
2589 #ifdef CONFIG_JBD2_DEBUG
2590 int n = atomic_read(&nr_journal_heads);
2592 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
2594 jbd2_remove_jbd_stats_proc_entry();
2595 jbd2_journal_destroy_caches();
2598 MODULE_LICENSE("GPL");
2599 module_init(journal_init);
2600 module_exit(journal_exit);