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 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
483 * Called with the journal already locked.
485 * Called under j_state_lock
488 int __jbd2_log_space_left(journal_t *journal)
490 int left = journal->j_free;
492 /* assert_spin_locked(&journal->j_state_lock); */
495 * Be pessimistic here about the number of those free blocks which
496 * might be required for log descriptor control blocks.
499 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
501 left -= MIN_LOG_RESERVED_BLOCKS;
510 * Called with j_state_lock locked for writing.
511 * Returns true if a transaction commit was started.
513 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
515 /* Return if the txn has already requested to be committed */
516 if (journal->j_commit_request == target)
520 * The only transaction we can possibly wait upon is the
521 * currently running transaction (if it exists). Otherwise,
522 * the target tid must be an old one.
524 if (journal->j_running_transaction &&
525 journal->j_running_transaction->t_tid == target) {
527 * We want a new commit: OK, mark the request and wakeup the
528 * commit thread. We do _not_ do the commit ourselves.
531 journal->j_commit_request = target;
532 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
533 journal->j_commit_request,
534 journal->j_commit_sequence);
535 journal->j_running_transaction->t_requested = jiffies;
536 wake_up(&journal->j_wait_commit);
538 } else if (!tid_geq(journal->j_commit_request, target))
539 /* This should never happen, but if it does, preserve
540 the evidence before kjournald goes into a loop and
541 increments j_commit_sequence beyond all recognition. */
542 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
543 journal->j_commit_request,
544 journal->j_commit_sequence,
545 target, journal->j_running_transaction ?
546 journal->j_running_transaction->t_tid : 0);
550 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
554 write_lock(&journal->j_state_lock);
555 ret = __jbd2_log_start_commit(journal, tid);
556 write_unlock(&journal->j_state_lock);
561 * Force and wait upon a commit if the calling process is not within
562 * transaction. This is used for forcing out undo-protected data which contains
563 * bitmaps, when the fs is running out of space.
565 * We can only force the running transaction if we don't have an active handle;
566 * otherwise, we will deadlock.
568 * Returns true if a transaction was started.
570 int jbd2_journal_force_commit_nested(journal_t *journal)
572 transaction_t *transaction = NULL;
574 int need_to_start = 0;
576 read_lock(&journal->j_state_lock);
577 if (journal->j_running_transaction && !current->journal_info) {
578 transaction = journal->j_running_transaction;
579 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
581 } else if (journal->j_committing_transaction)
582 transaction = journal->j_committing_transaction;
585 read_unlock(&journal->j_state_lock);
586 return 0; /* Nothing to retry */
589 tid = transaction->t_tid;
590 read_unlock(&journal->j_state_lock);
592 jbd2_log_start_commit(journal, tid);
593 jbd2_log_wait_commit(journal, tid);
598 * Start a commit of the current running transaction (if any). Returns true
599 * if a transaction is going to be committed (or is currently already
600 * committing), and fills its tid in at *ptid
602 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
606 write_lock(&journal->j_state_lock);
607 if (journal->j_running_transaction) {
608 tid_t tid = journal->j_running_transaction->t_tid;
610 __jbd2_log_start_commit(journal, tid);
611 /* There's a running transaction and we've just made sure
612 * it's commit has been scheduled. */
616 } else if (journal->j_committing_transaction) {
618 * If commit has been started, then we have to wait for
619 * completion of that transaction.
622 *ptid = journal->j_committing_transaction->t_tid;
625 write_unlock(&journal->j_state_lock);
630 * Return 1 if a given transaction has not yet sent barrier request
631 * connected with a transaction commit. If 0 is returned, transaction
632 * may or may not have sent the barrier. Used to avoid sending barrier
633 * twice in common cases.
635 int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
638 transaction_t *commit_trans;
640 if (!(journal->j_flags & JBD2_BARRIER))
642 read_lock(&journal->j_state_lock);
643 /* Transaction already committed? */
644 if (tid_geq(journal->j_commit_sequence, tid))
646 commit_trans = journal->j_committing_transaction;
647 if (!commit_trans || commit_trans->t_tid != tid) {
652 * Transaction is being committed and we already proceeded to
653 * submitting a flush to fs partition?
655 if (journal->j_fs_dev != journal->j_dev) {
656 if (!commit_trans->t_need_data_flush ||
657 commit_trans->t_state >= T_COMMIT_DFLUSH)
660 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
665 read_unlock(&journal->j_state_lock);
668 EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
671 * Wait for a specified commit to complete.
672 * The caller may not hold the journal lock.
674 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
678 read_lock(&journal->j_state_lock);
679 #ifdef CONFIG_JBD2_DEBUG
680 if (!tid_geq(journal->j_commit_request, tid)) {
682 "%s: error: j_commit_request=%d, tid=%d\n",
683 __func__, journal->j_commit_request, tid);
686 while (tid_gt(tid, journal->j_commit_sequence)) {
687 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
688 tid, journal->j_commit_sequence);
689 wake_up(&journal->j_wait_commit);
690 read_unlock(&journal->j_state_lock);
691 wait_event(journal->j_wait_done_commit,
692 !tid_gt(tid, journal->j_commit_sequence));
693 read_lock(&journal->j_state_lock);
695 read_unlock(&journal->j_state_lock);
697 if (unlikely(is_journal_aborted(journal))) {
698 printk(KERN_EMERG "journal commit I/O error\n");
705 * When this function returns the transaction corresponding to tid
706 * will be completed. If the transaction has currently running, start
707 * committing that transaction before waiting for it to complete. If
708 * the transaction id is stale, it is by definition already completed,
709 * so just return SUCCESS.
711 int jbd2_complete_transaction(journal_t *journal, tid_t tid)
713 int need_to_wait = 1;
715 read_lock(&journal->j_state_lock);
716 if (journal->j_running_transaction &&
717 journal->j_running_transaction->t_tid == tid) {
718 if (journal->j_commit_request != tid) {
719 /* transaction not yet started, so request it */
720 read_unlock(&journal->j_state_lock);
721 jbd2_log_start_commit(journal, tid);
724 } else if (!(journal->j_committing_transaction &&
725 journal->j_committing_transaction->t_tid == tid))
727 read_unlock(&journal->j_state_lock);
731 return jbd2_log_wait_commit(journal, tid);
733 EXPORT_SYMBOL(jbd2_complete_transaction);
736 * Log buffer allocation routines:
739 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
741 unsigned long blocknr;
743 write_lock(&journal->j_state_lock);
744 J_ASSERT(journal->j_free > 1);
746 blocknr = journal->j_head;
749 if (journal->j_head == journal->j_last)
750 journal->j_head = journal->j_first;
751 write_unlock(&journal->j_state_lock);
752 return jbd2_journal_bmap(journal, blocknr, retp);
756 * Conversion of logical to physical block numbers for the journal
758 * On external journals the journal blocks are identity-mapped, so
759 * this is a no-op. If needed, we can use j_blk_offset - everything is
762 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
763 unsigned long long *retp)
766 unsigned long long ret;
768 if (journal->j_inode) {
769 ret = bmap(journal->j_inode, blocknr);
773 printk(KERN_ALERT "%s: journal block not found "
774 "at offset %lu on %s\n",
775 __func__, blocknr, journal->j_devname);
777 __journal_abort_soft(journal, err);
780 *retp = blocknr; /* +journal->j_blk_offset */
786 * We play buffer_head aliasing tricks to write data/metadata blocks to
787 * the journal without copying their contents, but for journal
788 * descriptor blocks we do need to generate bona fide buffers.
790 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
791 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
792 * But we don't bother doing that, so there will be coherency problems with
793 * mmaps of blockdevs which hold live JBD-controlled filesystems.
795 struct buffer_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
797 struct buffer_head *bh;
798 unsigned long long blocknr;
801 err = jbd2_journal_next_log_block(journal, &blocknr);
806 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
810 memset(bh->b_data, 0, journal->j_blocksize);
811 set_buffer_uptodate(bh);
813 BUFFER_TRACE(bh, "return this buffer");
818 * Return tid of the oldest transaction in the journal and block in the journal
819 * where the transaction starts.
821 * If the journal is now empty, return which will be the next transaction ID
822 * we will write and where will that transaction start.
824 * The return value is 0 if journal tail cannot be pushed any further, 1 if
827 int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
828 unsigned long *block)
830 transaction_t *transaction;
833 read_lock(&journal->j_state_lock);
834 spin_lock(&journal->j_list_lock);
835 transaction = journal->j_checkpoint_transactions;
837 *tid = transaction->t_tid;
838 *block = transaction->t_log_start;
839 } else if ((transaction = journal->j_committing_transaction) != NULL) {
840 *tid = transaction->t_tid;
841 *block = transaction->t_log_start;
842 } else if ((transaction = journal->j_running_transaction) != NULL) {
843 *tid = transaction->t_tid;
844 *block = journal->j_head;
846 *tid = journal->j_transaction_sequence;
847 *block = journal->j_head;
849 ret = tid_gt(*tid, journal->j_tail_sequence);
850 spin_unlock(&journal->j_list_lock);
851 read_unlock(&journal->j_state_lock);
857 * Update information in journal structure and in on disk journal superblock
858 * about log tail. This function does not check whether information passed in
859 * really pushes log tail further. It's responsibility of the caller to make
860 * sure provided log tail information is valid (e.g. by holding
861 * j_checkpoint_mutex all the time between computing log tail and calling this
862 * function as is the case with jbd2_cleanup_journal_tail()).
864 * Requires j_checkpoint_mutex
866 void __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
870 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
873 * We cannot afford for write to remain in drive's caches since as
874 * soon as we update j_tail, next transaction can start reusing journal
875 * space and if we lose sb update during power failure we'd replay
876 * old transaction with possibly newly overwritten data.
878 jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
879 write_lock(&journal->j_state_lock);
880 freed = block - journal->j_tail;
881 if (block < journal->j_tail)
882 freed += journal->j_last - journal->j_first;
884 trace_jbd2_update_log_tail(journal, tid, block, freed);
886 "Cleaning journal tail from %d to %d (offset %lu), "
888 journal->j_tail_sequence, tid, block, freed);
890 journal->j_free += freed;
891 journal->j_tail_sequence = tid;
892 journal->j_tail = block;
893 write_unlock(&journal->j_state_lock);
897 * This is a variaon of __jbd2_update_log_tail which checks for validity of
898 * provided log tail and locks j_checkpoint_mutex. So it is safe against races
899 * with other threads updating log tail.
901 void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
903 mutex_lock(&journal->j_checkpoint_mutex);
904 if (tid_gt(tid, journal->j_tail_sequence))
905 __jbd2_update_log_tail(journal, tid, block);
906 mutex_unlock(&journal->j_checkpoint_mutex);
909 struct jbd2_stats_proc_session {
911 struct transaction_stats_s *stats;
916 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
918 return *pos ? NULL : SEQ_START_TOKEN;
921 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
926 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
928 struct jbd2_stats_proc_session *s = seq->private;
930 if (v != SEQ_START_TOKEN)
932 seq_printf(seq, "%lu transactions (%lu requested), "
933 "each up to %u blocks\n",
934 s->stats->ts_tid, s->stats->ts_requested,
935 s->journal->j_max_transaction_buffers);
936 if (s->stats->ts_tid == 0)
938 seq_printf(seq, "average: \n %ums waiting for transaction\n",
939 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
940 seq_printf(seq, " %ums request delay\n",
941 (s->stats->ts_requested == 0) ? 0 :
942 jiffies_to_msecs(s->stats->run.rs_request_delay /
943 s->stats->ts_requested));
944 seq_printf(seq, " %ums running transaction\n",
945 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
946 seq_printf(seq, " %ums transaction was being locked\n",
947 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
948 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
949 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
950 seq_printf(seq, " %ums logging transaction\n",
951 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
952 seq_printf(seq, " %lluus average transaction commit time\n",
953 div_u64(s->journal->j_average_commit_time, 1000));
954 seq_printf(seq, " %lu handles per transaction\n",
955 s->stats->run.rs_handle_count / s->stats->ts_tid);
956 seq_printf(seq, " %lu blocks per transaction\n",
957 s->stats->run.rs_blocks / s->stats->ts_tid);
958 seq_printf(seq, " %lu logged blocks per transaction\n",
959 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
963 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
967 static const struct seq_operations jbd2_seq_info_ops = {
968 .start = jbd2_seq_info_start,
969 .next = jbd2_seq_info_next,
970 .stop = jbd2_seq_info_stop,
971 .show = jbd2_seq_info_show,
974 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
976 journal_t *journal = PDE_DATA(inode);
977 struct jbd2_stats_proc_session *s;
980 s = kmalloc(sizeof(*s), GFP_KERNEL);
983 size = sizeof(struct transaction_stats_s);
984 s->stats = kmalloc(size, GFP_KERNEL);
985 if (s->stats == NULL) {
989 spin_lock(&journal->j_history_lock);
990 memcpy(s->stats, &journal->j_stats, size);
991 s->journal = journal;
992 spin_unlock(&journal->j_history_lock);
994 rc = seq_open(file, &jbd2_seq_info_ops);
996 struct seq_file *m = file->private_data;
1006 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
1008 struct seq_file *seq = file->private_data;
1009 struct jbd2_stats_proc_session *s = seq->private;
1012 return seq_release(inode, file);
1015 static const struct file_operations jbd2_seq_info_fops = {
1016 .owner = THIS_MODULE,
1017 .open = jbd2_seq_info_open,
1019 .llseek = seq_lseek,
1020 .release = jbd2_seq_info_release,
1023 static struct proc_dir_entry *proc_jbd2_stats;
1025 static void jbd2_stats_proc_init(journal_t *journal)
1027 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
1028 if (journal->j_proc_entry) {
1029 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
1030 &jbd2_seq_info_fops, journal);
1034 static void jbd2_stats_proc_exit(journal_t *journal)
1036 remove_proc_entry("info", journal->j_proc_entry);
1037 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
1041 * Management for journal control blocks: functions to create and
1042 * destroy journal_t structures, and to initialise and read existing
1043 * journal blocks from disk. */
1045 /* First: create and setup a journal_t object in memory. We initialise
1046 * very few fields yet: that has to wait until we have created the
1047 * journal structures from from scratch, or loaded them from disk. */
1049 static journal_t * journal_init_common (void)
1054 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1058 init_waitqueue_head(&journal->j_wait_transaction_locked);
1059 init_waitqueue_head(&journal->j_wait_logspace);
1060 init_waitqueue_head(&journal->j_wait_done_commit);
1061 init_waitqueue_head(&journal->j_wait_checkpoint);
1062 init_waitqueue_head(&journal->j_wait_commit);
1063 init_waitqueue_head(&journal->j_wait_updates);
1064 mutex_init(&journal->j_barrier);
1065 mutex_init(&journal->j_checkpoint_mutex);
1066 spin_lock_init(&journal->j_revoke_lock);
1067 spin_lock_init(&journal->j_list_lock);
1068 rwlock_init(&journal->j_state_lock);
1070 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
1071 journal->j_min_batch_time = 0;
1072 journal->j_max_batch_time = 15000; /* 15ms */
1074 /* The journal is marked for error until we succeed with recovery! */
1075 journal->j_flags = JBD2_ABORT;
1077 /* Set up a default-sized revoke table for the new mount. */
1078 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1084 spin_lock_init(&journal->j_history_lock);
1089 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1091 * Create a journal structure assigned some fixed set of disk blocks to
1092 * the journal. We don't actually touch those disk blocks yet, but we
1093 * need to set up all of the mapping information to tell the journaling
1094 * system where the journal blocks are.
1099 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1100 * @bdev: Block device on which to create the journal
1101 * @fs_dev: Device which hold journalled filesystem for this journal.
1102 * @start: Block nr Start of journal.
1103 * @len: Length of the journal in blocks.
1104 * @blocksize: blocksize of journalling device
1106 * Returns: a newly created journal_t *
1108 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1109 * range of blocks on an arbitrary block device.
1112 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1113 struct block_device *fs_dev,
1114 unsigned long long start, int len, int blocksize)
1116 journal_t *journal = journal_init_common();
1117 struct buffer_head *bh;
1124 /* journal descriptor can store up to n blocks -bzzz */
1125 journal->j_blocksize = blocksize;
1126 journal->j_dev = bdev;
1127 journal->j_fs_dev = fs_dev;
1128 journal->j_blk_offset = start;
1129 journal->j_maxlen = len;
1130 bdevname(journal->j_dev, journal->j_devname);
1131 p = journal->j_devname;
1132 while ((p = strchr(p, '/')))
1134 jbd2_stats_proc_init(journal);
1135 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1136 journal->j_wbufsize = n;
1137 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1138 if (!journal->j_wbuf) {
1139 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1144 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1147 "%s: Cannot get buffer for journal superblock\n",
1151 journal->j_sb_buffer = bh;
1152 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1156 kfree(journal->j_wbuf);
1157 jbd2_stats_proc_exit(journal);
1163 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1164 * @inode: An inode to create the journal in
1166 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1167 * the journal. The inode must exist already, must support bmap() and
1168 * must have all data blocks preallocated.
1170 journal_t * jbd2_journal_init_inode (struct inode *inode)
1172 struct buffer_head *bh;
1173 journal_t *journal = journal_init_common();
1177 unsigned long long blocknr;
1182 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1183 journal->j_inode = inode;
1184 bdevname(journal->j_dev, journal->j_devname);
1185 p = journal->j_devname;
1186 while ((p = strchr(p, '/')))
1188 p = journal->j_devname + strlen(journal->j_devname);
1189 sprintf(p, "-%lu", journal->j_inode->i_ino);
1191 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1192 journal, inode->i_sb->s_id, inode->i_ino,
1193 (long long) inode->i_size,
1194 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1196 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1197 journal->j_blocksize = inode->i_sb->s_blocksize;
1198 jbd2_stats_proc_init(journal);
1200 /* journal descriptor can store up to n blocks -bzzz */
1201 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1202 journal->j_wbufsize = n;
1203 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1204 if (!journal->j_wbuf) {
1205 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1210 err = jbd2_journal_bmap(journal, 0, &blocknr);
1211 /* If that failed, give up */
1213 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
1218 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1221 "%s: Cannot get buffer for journal superblock\n",
1225 journal->j_sb_buffer = bh;
1226 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1230 kfree(journal->j_wbuf);
1231 jbd2_stats_proc_exit(journal);
1237 * If the journal init or create aborts, we need to mark the journal
1238 * superblock as being NULL to prevent the journal destroy from writing
1239 * back a bogus superblock.
1241 static void journal_fail_superblock (journal_t *journal)
1243 struct buffer_head *bh = journal->j_sb_buffer;
1245 journal->j_sb_buffer = NULL;
1249 * Given a journal_t structure, initialise the various fields for
1250 * startup of a new journaling session. We use this both when creating
1251 * a journal, and after recovering an old journal to reset it for
1255 static int journal_reset(journal_t *journal)
1257 journal_superblock_t *sb = journal->j_superblock;
1258 unsigned long long first, last;
1260 first = be32_to_cpu(sb->s_first);
1261 last = be32_to_cpu(sb->s_maxlen);
1262 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1263 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
1265 journal_fail_superblock(journal);
1269 journal->j_first = first;
1270 journal->j_last = last;
1272 journal->j_head = first;
1273 journal->j_tail = first;
1274 journal->j_free = last - first;
1276 journal->j_tail_sequence = journal->j_transaction_sequence;
1277 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1278 journal->j_commit_request = journal->j_commit_sequence;
1280 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1283 * As a special case, if the on-disk copy is already marked as needing
1284 * no recovery (s_start == 0), then we can safely defer the superblock
1285 * update until the next commit by setting JBD2_FLUSHED. This avoids
1286 * attempting a write to a potential-readonly device.
1288 if (sb->s_start == 0) {
1289 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
1290 "(start %ld, seq %d, errno %d)\n",
1291 journal->j_tail, journal->j_tail_sequence,
1293 journal->j_flags |= JBD2_FLUSHED;
1295 /* Lock here to make assertions happy... */
1296 mutex_lock(&journal->j_checkpoint_mutex);
1298 * Update log tail information. We use WRITE_FUA since new
1299 * transaction will start reusing journal space and so we
1300 * must make sure information about current log tail is on
1303 jbd2_journal_update_sb_log_tail(journal,
1304 journal->j_tail_sequence,
1307 mutex_unlock(&journal->j_checkpoint_mutex);
1309 return jbd2_journal_start_thread(journal);
1312 static void jbd2_write_superblock(journal_t *journal, int write_op)
1314 struct buffer_head *bh = journal->j_sb_buffer;
1317 trace_jbd2_write_superblock(journal, write_op);
1318 if (!(journal->j_flags & JBD2_BARRIER))
1319 write_op &= ~(REQ_FUA | REQ_FLUSH);
1321 if (buffer_write_io_error(bh)) {
1323 * Oh, dear. A previous attempt to write the journal
1324 * superblock failed. This could happen because the
1325 * USB device was yanked out. Or it could happen to
1326 * be a transient write error and maybe the block will
1327 * be remapped. Nothing we can do but to retry the
1328 * write and hope for the best.
1330 printk(KERN_ERR "JBD2: previous I/O error detected "
1331 "for journal superblock update for %s.\n",
1332 journal->j_devname);
1333 clear_buffer_write_io_error(bh);
1334 set_buffer_uptodate(bh);
1337 bh->b_end_io = end_buffer_write_sync;
1338 ret = submit_bh(write_op, bh);
1340 if (buffer_write_io_error(bh)) {
1341 clear_buffer_write_io_error(bh);
1342 set_buffer_uptodate(bh);
1346 printk(KERN_ERR "JBD2: Error %d detected when updating "
1347 "journal superblock for %s.\n", ret,
1348 journal->j_devname);
1353 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1354 * @journal: The journal to update.
1355 * @tail_tid: TID of the new transaction at the tail of the log
1356 * @tail_block: The first block of the transaction at the tail of the log
1357 * @write_op: With which operation should we write the journal sb
1359 * Update a journal's superblock information about log tail and write it to
1360 * disk, waiting for the IO to complete.
1362 void jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1363 unsigned long tail_block, int write_op)
1365 journal_superblock_t *sb = journal->j_superblock;
1367 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1368 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1369 tail_block, tail_tid);
1371 sb->s_sequence = cpu_to_be32(tail_tid);
1372 sb->s_start = cpu_to_be32(tail_block);
1374 jbd2_write_superblock(journal, write_op);
1376 /* Log is no longer empty */
1377 write_lock(&journal->j_state_lock);
1378 WARN_ON(!sb->s_sequence);
1379 journal->j_flags &= ~JBD2_FLUSHED;
1380 write_unlock(&journal->j_state_lock);
1384 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1385 * @journal: The journal to update.
1387 * Update a journal's dynamic superblock fields to show that journal is empty.
1388 * Write updated superblock to disk waiting for IO to complete.
1390 static void jbd2_mark_journal_empty(journal_t *journal)
1392 journal_superblock_t *sb = journal->j_superblock;
1394 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1395 read_lock(&journal->j_state_lock);
1396 /* Is it already empty? */
1397 if (sb->s_start == 0) {
1398 read_unlock(&journal->j_state_lock);
1401 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1402 journal->j_tail_sequence);
1404 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1405 sb->s_start = cpu_to_be32(0);
1406 read_unlock(&journal->j_state_lock);
1408 jbd2_write_superblock(journal, WRITE_FUA);
1410 /* Log is no longer empty */
1411 write_lock(&journal->j_state_lock);
1412 journal->j_flags |= JBD2_FLUSHED;
1413 write_unlock(&journal->j_state_lock);
1418 * jbd2_journal_update_sb_errno() - Update error in the journal.
1419 * @journal: The journal to update.
1421 * Update a journal's errno. Write updated superblock to disk waiting for IO
1424 void jbd2_journal_update_sb_errno(journal_t *journal)
1426 journal_superblock_t *sb = journal->j_superblock;
1428 read_lock(&journal->j_state_lock);
1429 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1431 sb->s_errno = cpu_to_be32(journal->j_errno);
1432 jbd2_superblock_csum_set(journal, sb);
1433 read_unlock(&journal->j_state_lock);
1435 jbd2_write_superblock(journal, WRITE_SYNC);
1437 EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
1440 * Read the superblock for a given journal, performing initial
1441 * validation of the format.
1443 static int journal_get_superblock(journal_t *journal)
1445 struct buffer_head *bh;
1446 journal_superblock_t *sb;
1449 bh = journal->j_sb_buffer;
1451 J_ASSERT(bh != NULL);
1452 if (!buffer_uptodate(bh)) {
1453 ll_rw_block(READ, 1, &bh);
1455 if (!buffer_uptodate(bh)) {
1457 "JBD2: IO error reading journal superblock\n");
1462 if (buffer_verified(bh))
1465 sb = journal->j_superblock;
1469 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1470 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1471 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1475 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1476 case JBD2_SUPERBLOCK_V1:
1477 journal->j_format_version = 1;
1479 case JBD2_SUPERBLOCK_V2:
1480 journal->j_format_version = 2;
1483 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1487 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1488 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1489 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1490 printk(KERN_WARNING "JBD2: journal file too short\n");
1494 if (be32_to_cpu(sb->s_first) == 0 ||
1495 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1497 "JBD2: Invalid start block of journal: %u\n",
1498 be32_to_cpu(sb->s_first));
1502 if (JBD2_HAS_COMPAT_FEATURE(journal, JBD2_FEATURE_COMPAT_CHECKSUM) &&
1503 JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1504 /* Can't have checksum v1 and v2 on at the same time! */
1505 printk(KERN_ERR "JBD: Can't enable checksumming v1 and v2 "
1506 "at the same time!\n");
1510 if (!jbd2_verify_csum_type(journal, sb)) {
1511 printk(KERN_ERR "JBD: Unknown checksum type\n");
1515 /* Load the checksum driver */
1516 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1517 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1518 if (IS_ERR(journal->j_chksum_driver)) {
1519 printk(KERN_ERR "JBD: Cannot load crc32c driver.\n");
1520 err = PTR_ERR(journal->j_chksum_driver);
1521 journal->j_chksum_driver = NULL;
1526 /* Check superblock checksum */
1527 if (!jbd2_superblock_csum_verify(journal, sb)) {
1528 printk(KERN_ERR "JBD: journal checksum error\n");
1532 /* Precompute checksum seed for all metadata */
1533 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
1534 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1535 sizeof(sb->s_uuid));
1537 set_buffer_verified(bh);
1542 journal_fail_superblock(journal);
1547 * Load the on-disk journal superblock and read the key fields into the
1551 static int load_superblock(journal_t *journal)
1554 journal_superblock_t *sb;
1556 err = journal_get_superblock(journal);
1560 sb = journal->j_superblock;
1562 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1563 journal->j_tail = be32_to_cpu(sb->s_start);
1564 journal->j_first = be32_to_cpu(sb->s_first);
1565 journal->j_last = be32_to_cpu(sb->s_maxlen);
1566 journal->j_errno = be32_to_cpu(sb->s_errno);
1573 * int jbd2_journal_load() - Read journal from disk.
1574 * @journal: Journal to act on.
1576 * Given a journal_t structure which tells us which disk blocks contain
1577 * a journal, read the journal from disk to initialise the in-memory
1580 int jbd2_journal_load(journal_t *journal)
1583 journal_superblock_t *sb;
1585 err = load_superblock(journal);
1589 sb = journal->j_superblock;
1590 /* If this is a V2 superblock, then we have to check the
1591 * features flags on it. */
1593 if (journal->j_format_version >= 2) {
1594 if ((sb->s_feature_ro_compat &
1595 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1596 (sb->s_feature_incompat &
1597 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1599 "JBD2: Unrecognised features on journal\n");
1605 * Create a slab for this blocksize
1607 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1611 /* Let the recovery code check whether it needs to recover any
1612 * data from the journal. */
1613 if (jbd2_journal_recover(journal))
1614 goto recovery_error;
1616 if (journal->j_failed_commit) {
1617 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1618 "is corrupt.\n", journal->j_failed_commit,
1619 journal->j_devname);
1623 /* OK, we've finished with the dynamic journal bits:
1624 * reinitialise the dynamic contents of the superblock in memory
1625 * and reset them on disk. */
1626 if (journal_reset(journal))
1627 goto recovery_error;
1629 journal->j_flags &= ~JBD2_ABORT;
1630 journal->j_flags |= JBD2_LOADED;
1634 printk(KERN_WARNING "JBD2: recovery failed\n");
1639 * void jbd2_journal_destroy() - Release a journal_t structure.
1640 * @journal: Journal to act on.
1642 * Release a journal_t structure once it is no longer in use by the
1644 * Return <0 if we couldn't clean up the journal.
1646 int jbd2_journal_destroy(journal_t *journal)
1650 /* Wait for the commit thread to wake up and die. */
1651 journal_kill_thread(journal);
1653 /* Force a final log commit */
1654 if (journal->j_running_transaction)
1655 jbd2_journal_commit_transaction(journal);
1657 /* Force any old transactions to disk */
1659 /* Totally anal locking here... */
1660 spin_lock(&journal->j_list_lock);
1661 while (journal->j_checkpoint_transactions != NULL) {
1662 spin_unlock(&journal->j_list_lock);
1663 mutex_lock(&journal->j_checkpoint_mutex);
1664 jbd2_log_do_checkpoint(journal);
1665 mutex_unlock(&journal->j_checkpoint_mutex);
1666 spin_lock(&journal->j_list_lock);
1669 J_ASSERT(journal->j_running_transaction == NULL);
1670 J_ASSERT(journal->j_committing_transaction == NULL);
1671 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1672 spin_unlock(&journal->j_list_lock);
1674 if (journal->j_sb_buffer) {
1675 if (!is_journal_aborted(journal)) {
1676 mutex_lock(&journal->j_checkpoint_mutex);
1677 jbd2_mark_journal_empty(journal);
1678 mutex_unlock(&journal->j_checkpoint_mutex);
1681 brelse(journal->j_sb_buffer);
1684 if (journal->j_proc_entry)
1685 jbd2_stats_proc_exit(journal);
1686 if (journal->j_inode)
1687 iput(journal->j_inode);
1688 if (journal->j_revoke)
1689 jbd2_journal_destroy_revoke(journal);
1690 if (journal->j_chksum_driver)
1691 crypto_free_shash(journal->j_chksum_driver);
1692 kfree(journal->j_wbuf);
1700 *int jbd2_journal_check_used_features () - Check if features specified are used.
1701 * @journal: Journal to check.
1702 * @compat: bitmask of compatible features
1703 * @ro: bitmask of features that force read-only mount
1704 * @incompat: bitmask of incompatible features
1706 * Check whether the journal uses all of a given set of
1707 * features. Return true (non-zero) if it does.
1710 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1711 unsigned long ro, unsigned long incompat)
1713 journal_superblock_t *sb;
1715 if (!compat && !ro && !incompat)
1717 /* Load journal superblock if it is not loaded yet. */
1718 if (journal->j_format_version == 0 &&
1719 journal_get_superblock(journal) != 0)
1721 if (journal->j_format_version == 1)
1724 sb = journal->j_superblock;
1726 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1727 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1728 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1735 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1736 * @journal: Journal to check.
1737 * @compat: bitmask of compatible features
1738 * @ro: bitmask of features that force read-only mount
1739 * @incompat: bitmask of incompatible features
1741 * Check whether the journaling code supports the use of
1742 * all of a given set of features on this journal. Return true
1743 * (non-zero) if it can. */
1745 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1746 unsigned long ro, unsigned long incompat)
1748 if (!compat && !ro && !incompat)
1751 /* We can support any known requested features iff the
1752 * superblock is in version 2. Otherwise we fail to support any
1753 * extended sb features. */
1755 if (journal->j_format_version != 2)
1758 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1759 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1760 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1767 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1768 * @journal: Journal to act on.
1769 * @compat: bitmask of compatible features
1770 * @ro: bitmask of features that force read-only mount
1771 * @incompat: bitmask of incompatible features
1773 * Mark a given journal feature as present on the
1774 * superblock. Returns true if the requested features could be set.
1778 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1779 unsigned long ro, unsigned long incompat)
1781 #define INCOMPAT_FEATURE_ON(f) \
1782 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1783 #define COMPAT_FEATURE_ON(f) \
1784 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
1785 journal_superblock_t *sb;
1787 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1790 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1793 /* Asking for checksumming v2 and v1? Only give them v2. */
1794 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2 &&
1795 compat & JBD2_FEATURE_COMPAT_CHECKSUM)
1796 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
1798 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1799 compat, ro, incompat);
1801 sb = journal->j_superblock;
1803 /* If enabling v2 checksums, update superblock */
1804 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2)) {
1805 sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
1806 sb->s_feature_compat &=
1807 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
1809 /* Load the checksum driver */
1810 if (journal->j_chksum_driver == NULL) {
1811 journal->j_chksum_driver = crypto_alloc_shash("crc32c",
1813 if (IS_ERR(journal->j_chksum_driver)) {
1814 printk(KERN_ERR "JBD: Cannot load crc32c "
1816 journal->j_chksum_driver = NULL;
1821 /* Precompute checksum seed for all metadata */
1822 if (JBD2_HAS_INCOMPAT_FEATURE(journal,
1823 JBD2_FEATURE_INCOMPAT_CSUM_V2))
1824 journal->j_csum_seed = jbd2_chksum(journal, ~0,
1826 sizeof(sb->s_uuid));
1829 /* If enabling v1 checksums, downgrade superblock */
1830 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
1831 sb->s_feature_incompat &=
1832 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2);
1834 sb->s_feature_compat |= cpu_to_be32(compat);
1835 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1836 sb->s_feature_incompat |= cpu_to_be32(incompat);
1839 #undef COMPAT_FEATURE_ON
1840 #undef INCOMPAT_FEATURE_ON
1844 * jbd2_journal_clear_features () - Clear a given journal feature in the
1846 * @journal: Journal to act on.
1847 * @compat: bitmask of compatible features
1848 * @ro: bitmask of features that force read-only mount
1849 * @incompat: bitmask of incompatible features
1851 * Clear a given journal feature as present on the
1854 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1855 unsigned long ro, unsigned long incompat)
1857 journal_superblock_t *sb;
1859 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1860 compat, ro, incompat);
1862 sb = journal->j_superblock;
1864 sb->s_feature_compat &= ~cpu_to_be32(compat);
1865 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1866 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1868 EXPORT_SYMBOL(jbd2_journal_clear_features);
1871 * int jbd2_journal_flush () - Flush journal
1872 * @journal: Journal to act on.
1874 * Flush all data for a given journal to disk and empty the journal.
1875 * Filesystems can use this when remounting readonly to ensure that
1876 * recovery does not need to happen on remount.
1879 int jbd2_journal_flush(journal_t *journal)
1882 transaction_t *transaction = NULL;
1884 write_lock(&journal->j_state_lock);
1886 /* Force everything buffered to the log... */
1887 if (journal->j_running_transaction) {
1888 transaction = journal->j_running_transaction;
1889 __jbd2_log_start_commit(journal, transaction->t_tid);
1890 } else if (journal->j_committing_transaction)
1891 transaction = journal->j_committing_transaction;
1893 /* Wait for the log commit to complete... */
1895 tid_t tid = transaction->t_tid;
1897 write_unlock(&journal->j_state_lock);
1898 jbd2_log_wait_commit(journal, tid);
1900 write_unlock(&journal->j_state_lock);
1903 /* ...and flush everything in the log out to disk. */
1904 spin_lock(&journal->j_list_lock);
1905 while (!err && journal->j_checkpoint_transactions != NULL) {
1906 spin_unlock(&journal->j_list_lock);
1907 mutex_lock(&journal->j_checkpoint_mutex);
1908 err = jbd2_log_do_checkpoint(journal);
1909 mutex_unlock(&journal->j_checkpoint_mutex);
1910 spin_lock(&journal->j_list_lock);
1912 spin_unlock(&journal->j_list_lock);
1914 if (is_journal_aborted(journal))
1917 mutex_lock(&journal->j_checkpoint_mutex);
1918 jbd2_cleanup_journal_tail(journal);
1920 /* Finally, mark the journal as really needing no recovery.
1921 * This sets s_start==0 in the underlying superblock, which is
1922 * the magic code for a fully-recovered superblock. Any future
1923 * commits of data to the journal will restore the current
1925 jbd2_mark_journal_empty(journal);
1926 mutex_unlock(&journal->j_checkpoint_mutex);
1927 write_lock(&journal->j_state_lock);
1928 J_ASSERT(!journal->j_running_transaction);
1929 J_ASSERT(!journal->j_committing_transaction);
1930 J_ASSERT(!journal->j_checkpoint_transactions);
1931 J_ASSERT(journal->j_head == journal->j_tail);
1932 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1933 write_unlock(&journal->j_state_lock);
1938 * int jbd2_journal_wipe() - Wipe journal contents
1939 * @journal: Journal to act on.
1940 * @write: flag (see below)
1942 * Wipe out all of the contents of a journal, safely. This will produce
1943 * a warning if the journal contains any valid recovery information.
1944 * Must be called between journal_init_*() and jbd2_journal_load().
1946 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1947 * we merely suppress recovery.
1950 int jbd2_journal_wipe(journal_t *journal, int write)
1954 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1956 err = load_superblock(journal);
1960 if (!journal->j_tail)
1963 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
1964 write ? "Clearing" : "Ignoring");
1966 err = jbd2_journal_skip_recovery(journal);
1968 /* Lock to make assertions happy... */
1969 mutex_lock(&journal->j_checkpoint_mutex);
1970 jbd2_mark_journal_empty(journal);
1971 mutex_unlock(&journal->j_checkpoint_mutex);
1979 * Journal abort has very specific semantics, which we describe
1980 * for journal abort.
1982 * Two internal functions, which provide abort to the jbd layer
1987 * Quick version for internal journal use (doesn't lock the journal).
1988 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1989 * and don't attempt to make any other journal updates.
1991 void __jbd2_journal_abort_hard(journal_t *journal)
1993 transaction_t *transaction;
1995 if (journal->j_flags & JBD2_ABORT)
1998 printk(KERN_ERR "Aborting journal on device %s.\n",
1999 journal->j_devname);
2001 write_lock(&journal->j_state_lock);
2002 journal->j_flags |= JBD2_ABORT;
2003 transaction = journal->j_running_transaction;
2005 __jbd2_log_start_commit(journal, transaction->t_tid);
2006 write_unlock(&journal->j_state_lock);
2009 /* Soft abort: record the abort error status in the journal superblock,
2010 * but don't do any other IO. */
2011 static void __journal_abort_soft (journal_t *journal, int errno)
2013 if (journal->j_flags & JBD2_ABORT)
2016 if (!journal->j_errno)
2017 journal->j_errno = errno;
2019 __jbd2_journal_abort_hard(journal);
2022 jbd2_journal_update_sb_errno(journal);
2026 * void jbd2_journal_abort () - Shutdown the journal immediately.
2027 * @journal: the journal to shutdown.
2028 * @errno: an error number to record in the journal indicating
2029 * the reason for the shutdown.
2031 * Perform a complete, immediate shutdown of the ENTIRE
2032 * journal (not of a single transaction). This operation cannot be
2033 * undone without closing and reopening the journal.
2035 * The jbd2_journal_abort function is intended to support higher level error
2036 * recovery mechanisms such as the ext2/ext3 remount-readonly error
2039 * Journal abort has very specific semantics. Any existing dirty,
2040 * unjournaled buffers in the main filesystem will still be written to
2041 * disk by bdflush, but the journaling mechanism will be suspended
2042 * immediately and no further transaction commits will be honoured.
2044 * Any dirty, journaled buffers will be written back to disk without
2045 * hitting the journal. Atomicity cannot be guaranteed on an aborted
2046 * filesystem, but we _do_ attempt to leave as much data as possible
2047 * behind for fsck to use for cleanup.
2049 * Any attempt to get a new transaction handle on a journal which is in
2050 * ABORT state will just result in an -EROFS error return. A
2051 * jbd2_journal_stop on an existing handle will return -EIO if we have
2052 * entered abort state during the update.
2054 * Recursive transactions are not disturbed by journal abort until the
2055 * final jbd2_journal_stop, which will receive the -EIO error.
2057 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
2058 * which will be recorded (if possible) in the journal superblock. This
2059 * allows a client to record failure conditions in the middle of a
2060 * transaction without having to complete the transaction to record the
2061 * failure to disk. ext3_error, for example, now uses this
2064 * Errors which originate from within the journaling layer will NOT
2065 * supply an errno; a null errno implies that absolutely no further
2066 * writes are done to the journal (unless there are any already in
2071 void jbd2_journal_abort(journal_t *journal, int errno)
2073 __journal_abort_soft(journal, errno);
2077 * int jbd2_journal_errno () - returns the journal's error state.
2078 * @journal: journal to examine.
2080 * This is the errno number set with jbd2_journal_abort(), the last
2081 * time the journal was mounted - if the journal was stopped
2082 * without calling abort this will be 0.
2084 * If the journal has been aborted on this mount time -EROFS will
2087 int jbd2_journal_errno(journal_t *journal)
2091 read_lock(&journal->j_state_lock);
2092 if (journal->j_flags & JBD2_ABORT)
2095 err = journal->j_errno;
2096 read_unlock(&journal->j_state_lock);
2101 * int jbd2_journal_clear_err () - clears the journal's error state
2102 * @journal: journal to act on.
2104 * An error must be cleared or acked to take a FS out of readonly
2107 int jbd2_journal_clear_err(journal_t *journal)
2111 write_lock(&journal->j_state_lock);
2112 if (journal->j_flags & JBD2_ABORT)
2115 journal->j_errno = 0;
2116 write_unlock(&journal->j_state_lock);
2121 * void jbd2_journal_ack_err() - Ack journal err.
2122 * @journal: journal to act on.
2124 * An error must be cleared or acked to take a FS out of readonly
2127 void jbd2_journal_ack_err(journal_t *journal)
2129 write_lock(&journal->j_state_lock);
2130 if (journal->j_errno)
2131 journal->j_flags |= JBD2_ACK_ERR;
2132 write_unlock(&journal->j_state_lock);
2135 int jbd2_journal_blocks_per_page(struct inode *inode)
2137 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2141 * helper functions to deal with 32 or 64bit block numbers.
2143 size_t journal_tag_bytes(journal_t *journal)
2145 journal_block_tag_t tag;
2148 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_CSUM_V2))
2149 x += sizeof(tag.t_checksum);
2151 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
2152 return x + JBD2_TAG_SIZE64;
2154 return x + JBD2_TAG_SIZE32;
2158 * JBD memory management
2160 * These functions are used to allocate block-sized chunks of memory
2161 * used for making copies of buffer_head data. Very often it will be
2162 * page-sized chunks of data, but sometimes it will be in
2163 * sub-page-size chunks. (For example, 16k pages on Power systems
2164 * with a 4k block file system.) For blocks smaller than a page, we
2165 * use a SLAB allocator. There are slab caches for each block size,
2166 * which are allocated at mount time, if necessary, and we only free
2167 * (all of) the slab caches when/if the jbd2 module is unloaded. For
2168 * this reason we don't need to a mutex to protect access to
2169 * jbd2_slab[] allocating or releasing memory; only in
2170 * jbd2_journal_create_slab().
2172 #define JBD2_MAX_SLABS 8
2173 static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
2175 static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2176 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2177 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2181 static void jbd2_journal_destroy_slabs(void)
2185 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2187 kmem_cache_destroy(jbd2_slab[i]);
2188 jbd2_slab[i] = NULL;
2192 static int jbd2_journal_create_slab(size_t size)
2194 static DEFINE_MUTEX(jbd2_slab_create_mutex);
2195 int i = order_base_2(size) - 10;
2198 if (size == PAGE_SIZE)
2201 if (i >= JBD2_MAX_SLABS)
2204 if (unlikely(i < 0))
2206 mutex_lock(&jbd2_slab_create_mutex);
2208 mutex_unlock(&jbd2_slab_create_mutex);
2209 return 0; /* Already created */
2212 slab_size = 1 << (i+10);
2213 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2214 slab_size, 0, NULL);
2215 mutex_unlock(&jbd2_slab_create_mutex);
2216 if (!jbd2_slab[i]) {
2217 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2223 static struct kmem_cache *get_slab(size_t size)
2225 int i = order_base_2(size) - 10;
2227 BUG_ON(i >= JBD2_MAX_SLABS);
2228 if (unlikely(i < 0))
2230 BUG_ON(jbd2_slab[i] == NULL);
2231 return jbd2_slab[i];
2234 void *jbd2_alloc(size_t size, gfp_t flags)
2238 BUG_ON(size & (size-1)); /* Must be a power of 2 */
2240 flags |= __GFP_REPEAT;
2241 if (size == PAGE_SIZE)
2242 ptr = (void *)__get_free_pages(flags, 0);
2243 else if (size > PAGE_SIZE) {
2244 int order = get_order(size);
2247 ptr = (void *)__get_free_pages(flags, order);
2249 ptr = vmalloc(size);
2251 ptr = kmem_cache_alloc(get_slab(size), flags);
2253 /* Check alignment; SLUB has gotten this wrong in the past,
2254 * and this can lead to user data corruption! */
2255 BUG_ON(((unsigned long) ptr) & (size-1));
2260 void jbd2_free(void *ptr, size_t size)
2262 if (size == PAGE_SIZE) {
2263 free_pages((unsigned long)ptr, 0);
2266 if (size > PAGE_SIZE) {
2267 int order = get_order(size);
2270 free_pages((unsigned long)ptr, order);
2275 kmem_cache_free(get_slab(size), ptr);
2279 * Journal_head storage management
2281 static struct kmem_cache *jbd2_journal_head_cache;
2282 #ifdef CONFIG_JBD2_DEBUG
2283 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2286 static int jbd2_journal_init_journal_head_cache(void)
2290 J_ASSERT(jbd2_journal_head_cache == NULL);
2291 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2292 sizeof(struct journal_head),
2294 SLAB_TEMPORARY, /* flags */
2297 if (!jbd2_journal_head_cache) {
2299 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
2304 static void jbd2_journal_destroy_journal_head_cache(void)
2306 if (jbd2_journal_head_cache) {
2307 kmem_cache_destroy(jbd2_journal_head_cache);
2308 jbd2_journal_head_cache = NULL;
2313 * journal_head splicing and dicing
2315 static struct journal_head *journal_alloc_journal_head(void)
2317 struct journal_head *ret;
2319 #ifdef CONFIG_JBD2_DEBUG
2320 atomic_inc(&nr_journal_heads);
2322 ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
2324 jbd_debug(1, "out of memory for journal_head\n");
2325 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
2328 ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
2334 static void journal_free_journal_head(struct journal_head *jh)
2336 #ifdef CONFIG_JBD2_DEBUG
2337 atomic_dec(&nr_journal_heads);
2338 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2340 kmem_cache_free(jbd2_journal_head_cache, jh);
2344 * A journal_head is attached to a buffer_head whenever JBD has an
2345 * interest in the buffer.
2347 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2348 * is set. This bit is tested in core kernel code where we need to take
2349 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2352 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2354 * When a buffer has its BH_JBD bit set it is immune from being released by
2355 * core kernel code, mainly via ->b_count.
2357 * A journal_head is detached from its buffer_head when the journal_head's
2358 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2359 * transaction (b_cp_transaction) hold their references to b_jcount.
2361 * Various places in the kernel want to attach a journal_head to a buffer_head
2362 * _before_ attaching the journal_head to a transaction. To protect the
2363 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2364 * journal_head's b_jcount refcount by one. The caller must call
2365 * jbd2_journal_put_journal_head() to undo this.
2367 * So the typical usage would be:
2369 * (Attach a journal_head if needed. Increments b_jcount)
2370 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2372 * (Get another reference for transaction)
2373 * jbd2_journal_grab_journal_head(bh);
2374 * jh->b_transaction = xxx;
2375 * (Put original reference)
2376 * jbd2_journal_put_journal_head(jh);
2380 * Give a buffer_head a journal_head.
2384 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2386 struct journal_head *jh;
2387 struct journal_head *new_jh = NULL;
2390 if (!buffer_jbd(bh))
2391 new_jh = journal_alloc_journal_head();
2393 jbd_lock_bh_journal_head(bh);
2394 if (buffer_jbd(bh)) {
2398 (atomic_read(&bh->b_count) > 0) ||
2399 (bh->b_page && bh->b_page->mapping));
2402 jbd_unlock_bh_journal_head(bh);
2407 new_jh = NULL; /* We consumed it */
2412 BUFFER_TRACE(bh, "added journal_head");
2415 jbd_unlock_bh_journal_head(bh);
2417 journal_free_journal_head(new_jh);
2418 return bh->b_private;
2422 * Grab a ref against this buffer_head's journal_head. If it ended up not
2423 * having a journal_head, return NULL
2425 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2427 struct journal_head *jh = NULL;
2429 jbd_lock_bh_journal_head(bh);
2430 if (buffer_jbd(bh)) {
2434 jbd_unlock_bh_journal_head(bh);
2438 static void __journal_remove_journal_head(struct buffer_head *bh)
2440 struct journal_head *jh = bh2jh(bh);
2442 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2443 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2444 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2445 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2446 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2447 J_ASSERT_BH(bh, buffer_jbd(bh));
2448 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2449 BUFFER_TRACE(bh, "remove journal_head");
2450 if (jh->b_frozen_data) {
2451 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2452 jbd2_free(jh->b_frozen_data, bh->b_size);
2454 if (jh->b_committed_data) {
2455 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2456 jbd2_free(jh->b_committed_data, bh->b_size);
2458 bh->b_private = NULL;
2459 jh->b_bh = NULL; /* debug, really */
2460 clear_buffer_jbd(bh);
2461 journal_free_journal_head(jh);
2465 * Drop a reference on the passed journal_head. If it fell to zero then
2466 * release the journal_head from the buffer_head.
2468 void jbd2_journal_put_journal_head(struct journal_head *jh)
2470 struct buffer_head *bh = jh2bh(jh);
2472 jbd_lock_bh_journal_head(bh);
2473 J_ASSERT_JH(jh, jh->b_jcount > 0);
2475 if (!jh->b_jcount) {
2476 __journal_remove_journal_head(bh);
2477 jbd_unlock_bh_journal_head(bh);
2480 jbd_unlock_bh_journal_head(bh);
2484 * Initialize jbd inode head
2486 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2488 jinode->i_transaction = NULL;
2489 jinode->i_next_transaction = NULL;
2490 jinode->i_vfs_inode = inode;
2491 jinode->i_flags = 0;
2492 INIT_LIST_HEAD(&jinode->i_list);
2496 * Function to be called before we start removing inode from memory (i.e.,
2497 * clear_inode() is a fine place to be called from). It removes inode from
2498 * transaction's lists.
2500 void jbd2_journal_release_jbd_inode(journal_t *journal,
2501 struct jbd2_inode *jinode)
2506 spin_lock(&journal->j_list_lock);
2507 /* Is commit writing out inode - we have to wait */
2508 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
2509 wait_queue_head_t *wq;
2510 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2511 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2512 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2513 spin_unlock(&journal->j_list_lock);
2515 finish_wait(wq, &wait.wait);
2519 if (jinode->i_transaction) {
2520 list_del(&jinode->i_list);
2521 jinode->i_transaction = NULL;
2523 spin_unlock(&journal->j_list_lock);
2527 #ifdef CONFIG_PROC_FS
2529 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2531 static void __init jbd2_create_jbd_stats_proc_entry(void)
2533 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2536 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2538 if (proc_jbd2_stats)
2539 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2544 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2545 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2549 struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
2551 static int __init jbd2_journal_init_handle_cache(void)
2553 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
2554 if (jbd2_handle_cache == NULL) {
2555 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2558 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2559 if (jbd2_inode_cache == NULL) {
2560 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2561 kmem_cache_destroy(jbd2_handle_cache);
2567 static void jbd2_journal_destroy_handle_cache(void)
2569 if (jbd2_handle_cache)
2570 kmem_cache_destroy(jbd2_handle_cache);
2571 if (jbd2_inode_cache)
2572 kmem_cache_destroy(jbd2_inode_cache);
2577 * Module startup and shutdown
2580 static int __init journal_init_caches(void)
2584 ret = jbd2_journal_init_revoke_caches();
2586 ret = jbd2_journal_init_journal_head_cache();
2588 ret = jbd2_journal_init_handle_cache();
2590 ret = jbd2_journal_init_transaction_cache();
2594 static void jbd2_journal_destroy_caches(void)
2596 jbd2_journal_destroy_revoke_caches();
2597 jbd2_journal_destroy_journal_head_cache();
2598 jbd2_journal_destroy_handle_cache();
2599 jbd2_journal_destroy_transaction_cache();
2600 jbd2_journal_destroy_slabs();
2603 static int __init journal_init(void)
2607 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2609 ret = journal_init_caches();
2611 jbd2_create_jbd_stats_proc_entry();
2613 jbd2_journal_destroy_caches();
2618 static void __exit journal_exit(void)
2620 #ifdef CONFIG_JBD2_DEBUG
2621 int n = atomic_read(&nr_journal_heads);
2623 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
2625 jbd2_remove_jbd_stats_proc_entry();
2626 jbd2_journal_destroy_caches();
2629 MODULE_LICENSE("GPL");
2630 module_init(journal_init);
2631 module_exit(journal_exit);