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/debugfs.h>
39 #include <linux/seq_file.h>
40 #include <linux/math64.h>
41 #include <linux/hash.h>
42 #include <linux/log2.h>
43 #include <linux/vmalloc.h>
44 #include <linux/backing-dev.h>
45 #include <linux/bitops.h>
46 #include <linux/ratelimit.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/jbd2.h>
51 #include <asm/uaccess.h>
53 #include <asm/system.h>
55 EXPORT_SYMBOL(jbd2_journal_extend);
56 EXPORT_SYMBOL(jbd2_journal_stop);
57 EXPORT_SYMBOL(jbd2_journal_lock_updates);
58 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
59 EXPORT_SYMBOL(jbd2_journal_get_write_access);
60 EXPORT_SYMBOL(jbd2_journal_get_create_access);
61 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
62 EXPORT_SYMBOL(jbd2_journal_set_triggers);
63 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
64 EXPORT_SYMBOL(jbd2_journal_release_buffer);
65 EXPORT_SYMBOL(jbd2_journal_forget);
67 EXPORT_SYMBOL(journal_sync_buffer);
69 EXPORT_SYMBOL(jbd2_journal_flush);
70 EXPORT_SYMBOL(jbd2_journal_revoke);
72 EXPORT_SYMBOL(jbd2_journal_init_dev);
73 EXPORT_SYMBOL(jbd2_journal_init_inode);
74 EXPORT_SYMBOL(jbd2_journal_update_format);
75 EXPORT_SYMBOL(jbd2_journal_check_used_features);
76 EXPORT_SYMBOL(jbd2_journal_check_available_features);
77 EXPORT_SYMBOL(jbd2_journal_set_features);
78 EXPORT_SYMBOL(jbd2_journal_load);
79 EXPORT_SYMBOL(jbd2_journal_destroy);
80 EXPORT_SYMBOL(jbd2_journal_abort);
81 EXPORT_SYMBOL(jbd2_journal_errno);
82 EXPORT_SYMBOL(jbd2_journal_ack_err);
83 EXPORT_SYMBOL(jbd2_journal_clear_err);
84 EXPORT_SYMBOL(jbd2_log_wait_commit);
85 EXPORT_SYMBOL(jbd2_log_start_commit);
86 EXPORT_SYMBOL(jbd2_journal_start_commit);
87 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
88 EXPORT_SYMBOL(jbd2_journal_wipe);
89 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
90 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
91 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
92 EXPORT_SYMBOL(jbd2_journal_force_commit);
93 EXPORT_SYMBOL(jbd2_journal_file_inode);
94 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
95 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
96 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
97 EXPORT_SYMBOL(jbd2_inode_cache);
99 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
100 static void __journal_abort_soft (journal_t *journal, int errno);
101 static int jbd2_journal_create_slab(size_t slab_size);
104 * Helper function used to manage commit timeouts
107 static void commit_timeout(unsigned long __data)
109 struct task_struct * p = (struct task_struct *) __data;
115 * kjournald2: The main thread function used to manage a logging device
118 * This kernel thread is responsible for two things:
120 * 1) COMMIT: Every so often we need to commit the current state of the
121 * filesystem to disk. The journal thread is responsible for writing
122 * all of the metadata buffers to disk.
124 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
125 * of the data in that part of the log has been rewritten elsewhere on
126 * the disk. Flushing these old buffers to reclaim space in the log is
127 * known as checkpointing, and this thread is responsible for that job.
130 static int kjournald2(void *arg)
132 journal_t *journal = arg;
133 transaction_t *transaction;
136 * Set up an interval timer which can be used to trigger a commit wakeup
137 * after the commit interval expires
139 setup_timer(&journal->j_commit_timer, commit_timeout,
140 (unsigned long)current);
142 /* Record that the journal thread is running */
143 journal->j_task = current;
144 wake_up(&journal->j_wait_done_commit);
147 * And now, wait forever for commit wakeup events.
149 write_lock(&journal->j_state_lock);
152 if (journal->j_flags & JBD2_UNMOUNT)
155 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
156 journal->j_commit_sequence, journal->j_commit_request);
158 if (journal->j_commit_sequence != journal->j_commit_request) {
159 jbd_debug(1, "OK, requests differ\n");
160 write_unlock(&journal->j_state_lock);
161 del_timer_sync(&journal->j_commit_timer);
162 jbd2_journal_commit_transaction(journal);
163 write_lock(&journal->j_state_lock);
167 wake_up(&journal->j_wait_done_commit);
168 if (freezing(current)) {
170 * The simpler the better. Flushing journal isn't a
171 * good idea, because that depends on threads that may
172 * be already stopped.
174 jbd_debug(1, "Now suspending kjournald2\n");
175 write_unlock(&journal->j_state_lock);
177 write_lock(&journal->j_state_lock);
180 * We assume on resume that commits are already there,
184 int should_sleep = 1;
186 prepare_to_wait(&journal->j_wait_commit, &wait,
188 if (journal->j_commit_sequence != journal->j_commit_request)
190 transaction = journal->j_running_transaction;
191 if (transaction && time_after_eq(jiffies,
192 transaction->t_expires))
194 if (journal->j_flags & JBD2_UNMOUNT)
197 write_unlock(&journal->j_state_lock);
199 write_lock(&journal->j_state_lock);
201 finish_wait(&journal->j_wait_commit, &wait);
204 jbd_debug(1, "kjournald2 wakes\n");
207 * Were we woken up by a commit wakeup event?
209 transaction = journal->j_running_transaction;
210 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
211 journal->j_commit_request = transaction->t_tid;
212 jbd_debug(1, "woke because of timeout\n");
217 write_unlock(&journal->j_state_lock);
218 del_timer_sync(&journal->j_commit_timer);
219 journal->j_task = NULL;
220 wake_up(&journal->j_wait_done_commit);
221 jbd_debug(1, "Journal thread exiting.\n");
225 static int jbd2_journal_start_thread(journal_t *journal)
227 struct task_struct *t;
229 t = kthread_run(kjournald2, journal, "jbd2/%s",
234 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
238 static void journal_kill_thread(journal_t *journal)
240 write_lock(&journal->j_state_lock);
241 journal->j_flags |= JBD2_UNMOUNT;
243 while (journal->j_task) {
244 wake_up(&journal->j_wait_commit);
245 write_unlock(&journal->j_state_lock);
246 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
247 write_lock(&journal->j_state_lock);
249 write_unlock(&journal->j_state_lock);
253 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
255 * Writes a metadata buffer to a given disk block. The actual IO is not
256 * performed but a new buffer_head is constructed which labels the data
257 * to be written with the correct destination disk block.
259 * Any magic-number escaping which needs to be done will cause a
260 * copy-out here. If the buffer happens to start with the
261 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
262 * magic number is only written to the log for descripter blocks. In
263 * this case, we copy the data and replace the first word with 0, and we
264 * return a result code which indicates that this buffer needs to be
265 * marked as an escaped buffer in the corresponding log descriptor
266 * block. The missing word can then be restored when the block is read
269 * If the source buffer has already been modified by a new transaction
270 * since we took the last commit snapshot, we use the frozen copy of
271 * that data for IO. If we end up using the existing buffer_head's data
272 * for the write, then we *have* to lock the buffer to prevent anyone
273 * else from using and possibly modifying it while the IO is in
276 * The function returns a pointer to the buffer_heads to be used for IO.
278 * We assume that the journal has already been locked in this function.
285 * Bit 0 set == escape performed on the data
286 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
289 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
290 struct journal_head *jh_in,
291 struct journal_head **jh_out,
292 unsigned long long blocknr)
294 int need_copy_out = 0;
295 int done_copy_out = 0;
298 struct buffer_head *new_bh;
299 struct journal_head *new_jh;
300 struct page *new_page;
301 unsigned int new_offset;
302 struct buffer_head *bh_in = jh2bh(jh_in);
303 journal_t *journal = transaction->t_journal;
306 * The buffer really shouldn't be locked: only the current committing
307 * transaction is allowed to write it, so nobody else is allowed
310 * akpm: except if we're journalling data, and write() output is
311 * also part of a shared mapping, and another thread has
312 * decided to launch a writepage() against this buffer.
314 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
317 new_bh = alloc_buffer_head(GFP_NOFS);
320 * Failure is not an option, but __GFP_NOFAIL is going
321 * away; so we retry ourselves here.
323 congestion_wait(BLK_RW_ASYNC, HZ/50);
327 /* keep subsequent assertions sane */
329 init_buffer(new_bh, NULL, NULL);
330 atomic_set(&new_bh->b_count, 1);
331 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
334 * If a new transaction has already done a buffer copy-out, then
335 * we use that version of the data for the commit.
337 jbd_lock_bh_state(bh_in);
339 if (jh_in->b_frozen_data) {
341 new_page = virt_to_page(jh_in->b_frozen_data);
342 new_offset = offset_in_page(jh_in->b_frozen_data);
344 new_page = jh2bh(jh_in)->b_page;
345 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
348 mapped_data = kmap_atomic(new_page, KM_USER0);
350 * Fire data frozen trigger if data already wasn't frozen. Do this
351 * before checking for escaping, as the trigger may modify the magic
352 * offset. If a copy-out happens afterwards, it will have the correct
353 * data in the buffer.
356 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
362 if (*((__be32 *)(mapped_data + new_offset)) ==
363 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
367 kunmap_atomic(mapped_data, KM_USER0);
370 * Do we need to do a data copy?
372 if (need_copy_out && !done_copy_out) {
375 jbd_unlock_bh_state(bh_in);
376 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
378 jbd2_journal_put_journal_head(new_jh);
381 jbd_lock_bh_state(bh_in);
382 if (jh_in->b_frozen_data) {
383 jbd2_free(tmp, bh_in->b_size);
387 jh_in->b_frozen_data = tmp;
388 mapped_data = kmap_atomic(new_page, KM_USER0);
389 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
390 kunmap_atomic(mapped_data, KM_USER0);
392 new_page = virt_to_page(tmp);
393 new_offset = offset_in_page(tmp);
397 * This isn't strictly necessary, as we're using frozen
398 * data for the escaping, but it keeps consistency with
399 * b_frozen_data usage.
401 jh_in->b_frozen_triggers = jh_in->b_triggers;
405 * Did we need to do an escaping? Now we've done all the
406 * copying, we can finally do so.
409 mapped_data = kmap_atomic(new_page, KM_USER0);
410 *((unsigned int *)(mapped_data + new_offset)) = 0;
411 kunmap_atomic(mapped_data, KM_USER0);
414 set_bh_page(new_bh, new_page, new_offset);
415 new_jh->b_transaction = NULL;
416 new_bh->b_size = jh2bh(jh_in)->b_size;
417 new_bh->b_bdev = transaction->t_journal->j_dev;
418 new_bh->b_blocknr = blocknr;
419 set_buffer_mapped(new_bh);
420 set_buffer_dirty(new_bh);
425 * The to-be-written buffer needs to get moved to the io queue,
426 * and the original buffer whose contents we are shadowing or
427 * copying is moved to the transaction's shadow queue.
429 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
430 spin_lock(&journal->j_list_lock);
431 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
432 spin_unlock(&journal->j_list_lock);
433 jbd_unlock_bh_state(bh_in);
435 JBUFFER_TRACE(new_jh, "file as BJ_IO");
436 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
438 return do_escape | (done_copy_out << 1);
442 * Allocation code for the journal file. Manage the space left in the
443 * journal, so that we can begin checkpointing when appropriate.
447 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
449 * Called with the journal already locked.
451 * Called under j_state_lock
454 int __jbd2_log_space_left(journal_t *journal)
456 int left = journal->j_free;
458 /* assert_spin_locked(&journal->j_state_lock); */
461 * Be pessimistic here about the number of those free blocks which
462 * might be required for log descriptor control blocks.
465 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
467 left -= MIN_LOG_RESERVED_BLOCKS;
476 * Called with j_state_lock locked for writing.
477 * Returns true if a transaction commit was started.
479 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
482 * The only transaction we can possibly wait upon is the
483 * currently running transaction (if it exists). Otherwise,
484 * the target tid must be an old one.
486 if (journal->j_running_transaction &&
487 journal->j_running_transaction->t_tid == target) {
489 * We want a new commit: OK, mark the request and wakeup the
490 * commit thread. We do _not_ do the commit ourselves.
493 journal->j_commit_request = target;
494 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
495 journal->j_commit_request,
496 journal->j_commit_sequence);
497 wake_up(&journal->j_wait_commit);
499 } else if (!tid_geq(journal->j_commit_request, target))
500 /* This should never happen, but if it does, preserve
501 the evidence before kjournald goes into a loop and
502 increments j_commit_sequence beyond all recognition. */
503 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
504 journal->j_commit_request,
505 journal->j_commit_sequence,
506 target, journal->j_running_transaction ?
507 journal->j_running_transaction->t_tid : 0);
511 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
515 write_lock(&journal->j_state_lock);
516 ret = __jbd2_log_start_commit(journal, tid);
517 write_unlock(&journal->j_state_lock);
522 * Force and wait upon a commit if the calling process is not within
523 * transaction. This is used for forcing out undo-protected data which contains
524 * bitmaps, when the fs is running out of space.
526 * We can only force the running transaction if we don't have an active handle;
527 * otherwise, we will deadlock.
529 * Returns true if a transaction was started.
531 int jbd2_journal_force_commit_nested(journal_t *journal)
533 transaction_t *transaction = NULL;
535 int need_to_start = 0;
537 read_lock(&journal->j_state_lock);
538 if (journal->j_running_transaction && !current->journal_info) {
539 transaction = journal->j_running_transaction;
540 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
542 } else if (journal->j_committing_transaction)
543 transaction = journal->j_committing_transaction;
546 read_unlock(&journal->j_state_lock);
547 return 0; /* Nothing to retry */
550 tid = transaction->t_tid;
551 read_unlock(&journal->j_state_lock);
553 jbd2_log_start_commit(journal, tid);
554 jbd2_log_wait_commit(journal, tid);
559 * Start a commit of the current running transaction (if any). Returns true
560 * if a transaction is going to be committed (or is currently already
561 * committing), and fills its tid in at *ptid
563 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
567 write_lock(&journal->j_state_lock);
568 if (journal->j_running_transaction) {
569 tid_t tid = journal->j_running_transaction->t_tid;
571 __jbd2_log_start_commit(journal, tid);
572 /* There's a running transaction and we've just made sure
573 * it's commit has been scheduled. */
577 } else if (journal->j_committing_transaction) {
579 * If ext3_write_super() recently started a commit, then we
580 * have to wait for completion of that transaction
583 *ptid = journal->j_committing_transaction->t_tid;
586 write_unlock(&journal->j_state_lock);
591 * Return 1 if a given transaction has not yet sent barrier request
592 * connected with a transaction commit. If 0 is returned, transaction
593 * may or may not have sent the barrier. Used to avoid sending barrier
594 * twice in common cases.
596 int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
599 transaction_t *commit_trans;
601 if (!(journal->j_flags & JBD2_BARRIER))
603 read_lock(&journal->j_state_lock);
604 /* Transaction already committed? */
605 if (tid_geq(journal->j_commit_sequence, tid))
607 commit_trans = journal->j_committing_transaction;
608 if (!commit_trans || commit_trans->t_tid != tid) {
613 * Transaction is being committed and we already proceeded to
614 * submitting a flush to fs partition?
616 if (journal->j_fs_dev != journal->j_dev) {
617 if (!commit_trans->t_need_data_flush ||
618 commit_trans->t_state >= T_COMMIT_DFLUSH)
621 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
626 read_unlock(&journal->j_state_lock);
629 EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
632 * Wait for a specified commit to complete.
633 * The caller may not hold the journal lock.
635 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
639 read_lock(&journal->j_state_lock);
640 #ifdef CONFIG_JBD2_DEBUG
641 if (!tid_geq(journal->j_commit_request, tid)) {
643 "%s: error: j_commit_request=%d, tid=%d\n",
644 __func__, journal->j_commit_request, tid);
647 while (tid_gt(tid, journal->j_commit_sequence)) {
648 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
649 tid, journal->j_commit_sequence);
650 wake_up(&journal->j_wait_commit);
651 read_unlock(&journal->j_state_lock);
652 wait_event(journal->j_wait_done_commit,
653 !tid_gt(tid, journal->j_commit_sequence));
654 read_lock(&journal->j_state_lock);
656 read_unlock(&journal->j_state_lock);
658 if (unlikely(is_journal_aborted(journal))) {
659 printk(KERN_EMERG "journal commit I/O error\n");
666 * When this function returns the transaction corresponding to tid
667 * will be completed. If the transaction has currently running, start
668 * committing that transaction before waiting for it to complete. If
669 * the transaction id is stale, it is by definition already completed,
670 * so just return SUCCESS.
672 int jbd2_complete_transaction(journal_t *journal, tid_t tid)
674 int need_to_wait = 1;
676 read_lock(&journal->j_state_lock);
677 if (journal->j_running_transaction &&
678 journal->j_running_transaction->t_tid == tid) {
679 if (journal->j_commit_request != tid) {
680 /* transaction not yet started, so request it */
681 read_unlock(&journal->j_state_lock);
682 jbd2_log_start_commit(journal, tid);
685 } else if (!(journal->j_committing_transaction &&
686 journal->j_committing_transaction->t_tid == tid))
688 read_unlock(&journal->j_state_lock);
692 return jbd2_log_wait_commit(journal, tid);
694 EXPORT_SYMBOL(jbd2_complete_transaction);
697 * Log buffer allocation routines:
700 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
702 unsigned long blocknr;
704 write_lock(&journal->j_state_lock);
705 J_ASSERT(journal->j_free > 1);
707 blocknr = journal->j_head;
710 if (journal->j_head == journal->j_last)
711 journal->j_head = journal->j_first;
712 write_unlock(&journal->j_state_lock);
713 return jbd2_journal_bmap(journal, blocknr, retp);
717 * Conversion of logical to physical block numbers for the journal
719 * On external journals the journal blocks are identity-mapped, so
720 * this is a no-op. If needed, we can use j_blk_offset - everything is
723 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
724 unsigned long long *retp)
727 unsigned long long ret;
729 if (journal->j_inode) {
730 ret = bmap(journal->j_inode, blocknr);
734 printk(KERN_ALERT "%s: journal block not found "
735 "at offset %lu on %s\n",
736 __func__, blocknr, journal->j_devname);
738 __journal_abort_soft(journal, err);
741 *retp = blocknr; /* +journal->j_blk_offset */
747 * We play buffer_head aliasing tricks to write data/metadata blocks to
748 * the journal without copying their contents, but for journal
749 * descriptor blocks we do need to generate bona fide buffers.
751 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
752 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
753 * But we don't bother doing that, so there will be coherency problems with
754 * mmaps of blockdevs which hold live JBD-controlled filesystems.
756 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
758 struct buffer_head *bh;
759 unsigned long long blocknr;
762 err = jbd2_journal_next_log_block(journal, &blocknr);
767 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
771 memset(bh->b_data, 0, journal->j_blocksize);
772 set_buffer_uptodate(bh);
774 BUFFER_TRACE(bh, "return this buffer");
775 return jbd2_journal_add_journal_head(bh);
779 * Return tid of the oldest transaction in the journal and block in the journal
780 * where the transaction starts.
782 * If the journal is now empty, return which will be the next transaction ID
783 * we will write and where will that transaction start.
785 * The return value is 0 if journal tail cannot be pushed any further, 1 if
788 int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
789 unsigned long *block)
791 transaction_t *transaction;
794 read_lock(&journal->j_state_lock);
795 spin_lock(&journal->j_list_lock);
796 transaction = journal->j_checkpoint_transactions;
798 *tid = transaction->t_tid;
799 *block = transaction->t_log_start;
800 } else if ((transaction = journal->j_committing_transaction) != NULL) {
801 *tid = transaction->t_tid;
802 *block = transaction->t_log_start;
803 } else if ((transaction = journal->j_running_transaction) != NULL) {
804 *tid = transaction->t_tid;
805 *block = journal->j_head;
807 *tid = journal->j_transaction_sequence;
808 *block = journal->j_head;
810 ret = tid_gt(*tid, journal->j_tail_sequence);
811 spin_unlock(&journal->j_list_lock);
812 read_unlock(&journal->j_state_lock);
818 * Update information in journal structure and in on disk journal superblock
819 * about log tail. This function does not check whether information passed in
820 * really pushes log tail further. It's responsibility of the caller to make
821 * sure provided log tail information is valid (e.g. by holding
822 * j_checkpoint_mutex all the time between computing log tail and calling this
823 * function as is the case with jbd2_cleanup_journal_tail()).
825 * Requires j_checkpoint_mutex
827 int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
832 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
835 * We cannot afford for write to remain in drive's caches since as
836 * soon as we update j_tail, next transaction can start reusing journal
837 * space and if we lose sb update during power failure we'd replay
838 * old transaction with possibly newly overwritten data.
840 ret = jbd2_journal_update_sb_log_tail(journal, tid, block, WRITE_FUA);
844 write_lock(&journal->j_state_lock);
845 freed = block - journal->j_tail;
846 if (block < journal->j_tail)
847 freed += journal->j_last - journal->j_first;
849 trace_jbd2_update_log_tail(journal, tid, block, freed);
851 "Cleaning journal tail from %d to %d (offset %lu), "
853 journal->j_tail_sequence, tid, block, freed);
855 journal->j_free += freed;
856 journal->j_tail_sequence = tid;
857 journal->j_tail = block;
858 write_unlock(&journal->j_state_lock);
864 struct jbd2_stats_proc_session {
866 struct transaction_stats_s *stats;
871 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
873 return *pos ? NULL : SEQ_START_TOKEN;
876 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
881 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
883 struct jbd2_stats_proc_session *s = seq->private;
885 if (v != SEQ_START_TOKEN)
887 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
889 s->journal->j_max_transaction_buffers);
890 if (s->stats->ts_tid == 0)
892 seq_printf(seq, "average: \n %ums waiting for transaction\n",
893 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
894 seq_printf(seq, " %ums running transaction\n",
895 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
896 seq_printf(seq, " %ums transaction was being locked\n",
897 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
898 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
899 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
900 seq_printf(seq, " %ums logging transaction\n",
901 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
902 seq_printf(seq, " %lluus average transaction commit time\n",
903 div_u64(s->journal->j_average_commit_time, 1000));
904 seq_printf(seq, " %lu handles per transaction\n",
905 s->stats->run.rs_handle_count / s->stats->ts_tid);
906 seq_printf(seq, " %lu blocks per transaction\n",
907 s->stats->run.rs_blocks / s->stats->ts_tid);
908 seq_printf(seq, " %lu logged blocks per transaction\n",
909 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
913 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
917 static const struct seq_operations jbd2_seq_info_ops = {
918 .start = jbd2_seq_info_start,
919 .next = jbd2_seq_info_next,
920 .stop = jbd2_seq_info_stop,
921 .show = jbd2_seq_info_show,
924 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
926 journal_t *journal = PDE(inode)->data;
927 struct jbd2_stats_proc_session *s;
930 s = kmalloc(sizeof(*s), GFP_KERNEL);
933 size = sizeof(struct transaction_stats_s);
934 s->stats = kmalloc(size, GFP_KERNEL);
935 if (s->stats == NULL) {
939 spin_lock(&journal->j_history_lock);
940 memcpy(s->stats, &journal->j_stats, size);
941 s->journal = journal;
942 spin_unlock(&journal->j_history_lock);
944 rc = seq_open(file, &jbd2_seq_info_ops);
946 struct seq_file *m = file->private_data;
956 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
958 struct seq_file *seq = file->private_data;
959 struct jbd2_stats_proc_session *s = seq->private;
962 return seq_release(inode, file);
965 static const struct file_operations jbd2_seq_info_fops = {
966 .owner = THIS_MODULE,
967 .open = jbd2_seq_info_open,
970 .release = jbd2_seq_info_release,
973 static struct proc_dir_entry *proc_jbd2_stats;
975 static void jbd2_stats_proc_init(journal_t *journal)
977 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
978 if (journal->j_proc_entry) {
979 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
980 &jbd2_seq_info_fops, journal);
984 static void jbd2_stats_proc_exit(journal_t *journal)
986 remove_proc_entry("info", journal->j_proc_entry);
987 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
991 * Management for journal control blocks: functions to create and
992 * destroy journal_t structures, and to initialise and read existing
993 * journal blocks from disk. */
995 /* First: create and setup a journal_t object in memory. We initialise
996 * very few fields yet: that has to wait until we have created the
997 * journal structures from from scratch, or loaded them from disk. */
999 static journal_t * journal_init_common (void)
1004 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1008 init_waitqueue_head(&journal->j_wait_transaction_locked);
1009 init_waitqueue_head(&journal->j_wait_logspace);
1010 init_waitqueue_head(&journal->j_wait_done_commit);
1011 init_waitqueue_head(&journal->j_wait_checkpoint);
1012 init_waitqueue_head(&journal->j_wait_commit);
1013 init_waitqueue_head(&journal->j_wait_updates);
1014 mutex_init(&journal->j_barrier);
1015 mutex_init(&journal->j_checkpoint_mutex);
1016 spin_lock_init(&journal->j_revoke_lock);
1017 spin_lock_init(&journal->j_list_lock);
1018 rwlock_init(&journal->j_state_lock);
1020 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
1021 journal->j_min_batch_time = 0;
1022 journal->j_max_batch_time = 15000; /* 15ms */
1024 /* The journal is marked for error until we succeed with recovery! */
1025 journal->j_flags = JBD2_ABORT;
1027 /* Set up a default-sized revoke table for the new mount. */
1028 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1034 spin_lock_init(&journal->j_history_lock);
1039 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1041 * Create a journal structure assigned some fixed set of disk blocks to
1042 * the journal. We don't actually touch those disk blocks yet, but we
1043 * need to set up all of the mapping information to tell the journaling
1044 * system where the journal blocks are.
1049 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1050 * @bdev: Block device on which to create the journal
1051 * @fs_dev: Device which hold journalled filesystem for this journal.
1052 * @start: Block nr Start of journal.
1053 * @len: Length of the journal in blocks.
1054 * @blocksize: blocksize of journalling device
1056 * Returns: a newly created journal_t *
1058 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1059 * range of blocks on an arbitrary block device.
1062 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1063 struct block_device *fs_dev,
1064 unsigned long long start, int len, int blocksize)
1066 journal_t *journal = journal_init_common();
1067 struct buffer_head *bh;
1074 /* journal descriptor can store up to n blocks -bzzz */
1075 journal->j_blocksize = blocksize;
1076 journal->j_dev = bdev;
1077 journal->j_fs_dev = fs_dev;
1078 journal->j_blk_offset = start;
1079 journal->j_maxlen = len;
1080 bdevname(journal->j_dev, journal->j_devname);
1081 p = journal->j_devname;
1082 while ((p = strchr(p, '/')))
1084 jbd2_stats_proc_init(journal);
1085 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1086 journal->j_wbufsize = n;
1087 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1088 if (!journal->j_wbuf) {
1089 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1094 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1097 "%s: Cannot get buffer for journal superblock\n",
1101 journal->j_sb_buffer = bh;
1102 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1106 kfree(journal->j_wbuf);
1107 jbd2_stats_proc_exit(journal);
1113 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1114 * @inode: An inode to create the journal in
1116 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1117 * the journal. The inode must exist already, must support bmap() and
1118 * must have all data blocks preallocated.
1120 journal_t * jbd2_journal_init_inode (struct inode *inode)
1122 struct buffer_head *bh;
1123 journal_t *journal = journal_init_common();
1127 unsigned long long blocknr;
1132 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1133 journal->j_inode = inode;
1134 bdevname(journal->j_dev, journal->j_devname);
1135 p = journal->j_devname;
1136 while ((p = strchr(p, '/')))
1138 p = journal->j_devname + strlen(journal->j_devname);
1139 sprintf(p, "-%lu", journal->j_inode->i_ino);
1141 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1142 journal, inode->i_sb->s_id, inode->i_ino,
1143 (long long) inode->i_size,
1144 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1146 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1147 journal->j_blocksize = inode->i_sb->s_blocksize;
1148 jbd2_stats_proc_init(journal);
1150 /* journal descriptor can store up to n blocks -bzzz */
1151 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1152 journal->j_wbufsize = n;
1153 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1154 if (!journal->j_wbuf) {
1155 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
1160 err = jbd2_journal_bmap(journal, 0, &blocknr);
1161 /* If that failed, give up */
1163 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
1168 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1171 "%s: Cannot get buffer for journal superblock\n",
1175 journal->j_sb_buffer = bh;
1176 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1180 kfree(journal->j_wbuf);
1181 jbd2_stats_proc_exit(journal);
1187 * If the journal init or create aborts, we need to mark the journal
1188 * superblock as being NULL to prevent the journal destroy from writing
1189 * back a bogus superblock.
1191 static void journal_fail_superblock (journal_t *journal)
1193 struct buffer_head *bh = journal->j_sb_buffer;
1195 journal->j_sb_buffer = NULL;
1199 * Given a journal_t structure, initialise the various fields for
1200 * startup of a new journaling session. We use this both when creating
1201 * a journal, and after recovering an old journal to reset it for
1205 static int journal_reset(journal_t *journal)
1207 journal_superblock_t *sb = journal->j_superblock;
1208 unsigned long long first, last;
1210 first = be32_to_cpu(sb->s_first);
1211 last = be32_to_cpu(sb->s_maxlen);
1212 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1213 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
1215 journal_fail_superblock(journal);
1219 journal->j_first = first;
1220 journal->j_last = last;
1222 journal->j_head = first;
1223 journal->j_tail = first;
1224 journal->j_free = last - first;
1226 journal->j_tail_sequence = journal->j_transaction_sequence;
1227 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1228 journal->j_commit_request = journal->j_commit_sequence;
1230 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1233 * As a special case, if the on-disk copy is already marked as needing
1234 * no recovery (s_start == 0), then we can safely defer the superblock
1235 * update until the next commit by setting JBD2_FLUSHED. This avoids
1236 * attempting a write to a potential-readonly device.
1238 if (sb->s_start == 0) {
1239 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
1240 "(start %ld, seq %d, errno %d)\n",
1241 journal->j_tail, journal->j_tail_sequence,
1243 journal->j_flags |= JBD2_FLUSHED;
1245 /* Lock here to make assertions happy... */
1246 mutex_lock(&journal->j_checkpoint_mutex);
1248 * Update log tail information. We use WRITE_FUA since new
1249 * transaction will start reusing journal space and so we
1250 * must make sure information about current log tail is on
1253 jbd2_journal_update_sb_log_tail(journal,
1254 journal->j_tail_sequence,
1257 mutex_unlock(&journal->j_checkpoint_mutex);
1259 return jbd2_journal_start_thread(journal);
1262 static int jbd2_write_superblock(journal_t *journal, int write_op)
1264 struct buffer_head *bh = journal->j_sb_buffer;
1267 if (!(journal->j_flags & JBD2_BARRIER))
1268 write_op &= ~(REQ_FUA | REQ_FLUSH);
1270 if (buffer_write_io_error(bh)) {
1272 * Oh, dear. A previous attempt to write the journal
1273 * superblock failed. This could happen because the
1274 * USB device was yanked out. Or it could happen to
1275 * be a transient write error and maybe the block will
1276 * be remapped. Nothing we can do but to retry the
1277 * write and hope for the best.
1279 printk(KERN_ERR "JBD2: previous I/O error detected "
1280 "for journal superblock update for %s.\n",
1281 journal->j_devname);
1282 clear_buffer_write_io_error(bh);
1283 set_buffer_uptodate(bh);
1286 bh->b_end_io = end_buffer_write_sync;
1287 ret = submit_bh(write_op, bh);
1289 if (buffer_write_io_error(bh)) {
1290 clear_buffer_write_io_error(bh);
1291 set_buffer_uptodate(bh);
1295 printk(KERN_ERR "JBD2: Error %d detected when updating "
1296 "journal superblock for %s.\n", ret,
1297 journal->j_devname);
1298 jbd2_journal_abort(journal, ret);
1305 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1306 * @journal: The journal to update.
1307 * @tail_tid: TID of the new transaction at the tail of the log
1308 * @tail_block: The first block of the transaction at the tail of the log
1309 * @write_op: With which operation should we write the journal sb
1311 * Update a journal's superblock information about log tail and write it to
1312 * disk, waiting for the IO to complete.
1314 int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1315 unsigned long tail_block, int write_op)
1317 journal_superblock_t *sb = journal->j_superblock;
1320 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1321 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1322 tail_block, tail_tid);
1324 sb->s_sequence = cpu_to_be32(tail_tid);
1325 sb->s_start = cpu_to_be32(tail_block);
1327 ret = jbd2_write_superblock(journal, write_op);
1330 /* Log is no longer empty */
1331 write_lock(&journal->j_state_lock);
1332 WARN_ON(!sb->s_sequence);
1333 journal->j_flags &= ~JBD2_FLUSHED;
1334 write_unlock(&journal->j_state_lock);
1341 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1342 * @journal: The journal to update.
1343 * @write_op: With which operation should we write the journal sb
1345 * Update a journal's dynamic superblock fields to show that journal is empty.
1346 * Write updated superblock to disk waiting for IO to complete.
1348 static void jbd2_mark_journal_empty(journal_t *journal, int write_op)
1350 journal_superblock_t *sb = journal->j_superblock;
1352 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1353 read_lock(&journal->j_state_lock);
1354 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1355 journal->j_tail_sequence);
1357 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1358 sb->s_start = cpu_to_be32(0);
1359 read_unlock(&journal->j_state_lock);
1361 jbd2_write_superblock(journal, write_op);
1363 /* Log is no longer empty */
1364 write_lock(&journal->j_state_lock);
1365 journal->j_flags |= JBD2_FLUSHED;
1366 write_unlock(&journal->j_state_lock);
1371 * jbd2_journal_update_sb_errno() - Update error in the journal.
1372 * @journal: The journal to update.
1374 * Update a journal's errno. Write updated superblock to disk waiting for IO
1377 static void jbd2_journal_update_sb_errno(journal_t *journal)
1379 journal_superblock_t *sb = journal->j_superblock;
1381 read_lock(&journal->j_state_lock);
1382 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1384 sb->s_errno = cpu_to_be32(journal->j_errno);
1385 read_unlock(&journal->j_state_lock);
1387 jbd2_write_superblock(journal, WRITE_SYNC);
1391 * Read the superblock for a given journal, performing initial
1392 * validation of the format.
1394 static int journal_get_superblock(journal_t *journal)
1396 struct buffer_head *bh;
1397 journal_superblock_t *sb;
1400 bh = journal->j_sb_buffer;
1402 J_ASSERT(bh != NULL);
1403 if (!buffer_uptodate(bh)) {
1404 ll_rw_block(READ, 1, &bh);
1406 if (!buffer_uptodate(bh)) {
1408 "JBD2: IO error reading journal superblock\n");
1413 sb = journal->j_superblock;
1417 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1418 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1419 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1423 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1424 case JBD2_SUPERBLOCK_V1:
1425 journal->j_format_version = 1;
1427 case JBD2_SUPERBLOCK_V2:
1428 journal->j_format_version = 2;
1431 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1435 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1436 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1437 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1438 printk(KERN_WARNING "JBD2: journal file too short\n");
1442 if (be32_to_cpu(sb->s_first) == 0 ||
1443 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1445 "JBD2: Invalid start block of journal: %u\n",
1446 be32_to_cpu(sb->s_first));
1453 journal_fail_superblock(journal);
1458 * Load the on-disk journal superblock and read the key fields into the
1462 static int load_superblock(journal_t *journal)
1465 journal_superblock_t *sb;
1467 err = journal_get_superblock(journal);
1471 sb = journal->j_superblock;
1473 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1474 journal->j_tail = be32_to_cpu(sb->s_start);
1475 journal->j_first = be32_to_cpu(sb->s_first);
1476 journal->j_last = be32_to_cpu(sb->s_maxlen);
1477 journal->j_errno = be32_to_cpu(sb->s_errno);
1484 * int jbd2_journal_load() - Read journal from disk.
1485 * @journal: Journal to act on.
1487 * Given a journal_t structure which tells us which disk blocks contain
1488 * a journal, read the journal from disk to initialise the in-memory
1491 int jbd2_journal_load(journal_t *journal)
1494 journal_superblock_t *sb;
1496 err = load_superblock(journal);
1500 sb = journal->j_superblock;
1501 /* If this is a V2 superblock, then we have to check the
1502 * features flags on it. */
1504 if (journal->j_format_version >= 2) {
1505 if ((sb->s_feature_ro_compat &
1506 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1507 (sb->s_feature_incompat &
1508 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1510 "JBD2: Unrecognised features on journal\n");
1516 * Create a slab for this blocksize
1518 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1522 /* Let the recovery code check whether it needs to recover any
1523 * data from the journal. */
1524 if (jbd2_journal_recover(journal))
1525 goto recovery_error;
1527 if (journal->j_failed_commit) {
1528 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1529 "is corrupt.\n", journal->j_failed_commit,
1530 journal->j_devname);
1534 /* OK, we've finished with the dynamic journal bits:
1535 * reinitialise the dynamic contents of the superblock in memory
1536 * and reset them on disk. */
1537 if (journal_reset(journal))
1538 goto recovery_error;
1540 journal->j_flags &= ~JBD2_ABORT;
1541 journal->j_flags |= JBD2_LOADED;
1545 printk(KERN_WARNING "JBD2: recovery failed\n");
1550 * void jbd2_journal_destroy() - Release a journal_t structure.
1551 * @journal: Journal to act on.
1553 * Release a journal_t structure once it is no longer in use by the
1555 * Return <0 if we couldn't clean up the journal.
1557 int jbd2_journal_destroy(journal_t *journal)
1561 /* Wait for the commit thread to wake up and die. */
1562 journal_kill_thread(journal);
1564 /* Force a final log commit */
1565 if (journal->j_running_transaction)
1566 jbd2_journal_commit_transaction(journal);
1568 /* Force any old transactions to disk */
1570 /* Totally anal locking here... */
1571 spin_lock(&journal->j_list_lock);
1572 while (journal->j_checkpoint_transactions != NULL) {
1573 spin_unlock(&journal->j_list_lock);
1574 mutex_lock(&journal->j_checkpoint_mutex);
1575 err = jbd2_log_do_checkpoint(journal);
1576 mutex_unlock(&journal->j_checkpoint_mutex);
1578 * If checkpointing failed, just free the buffers to avoid
1582 jbd2_journal_destroy_checkpoint(journal);
1583 spin_lock(&journal->j_list_lock);
1586 spin_lock(&journal->j_list_lock);
1589 J_ASSERT(journal->j_running_transaction == NULL);
1590 J_ASSERT(journal->j_committing_transaction == NULL);
1591 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1592 spin_unlock(&journal->j_list_lock);
1594 if (journal->j_sb_buffer) {
1595 if (!is_journal_aborted(journal)) {
1596 mutex_lock(&journal->j_checkpoint_mutex);
1598 write_lock(&journal->j_state_lock);
1599 journal->j_tail_sequence =
1600 ++journal->j_transaction_sequence;
1601 write_unlock(&journal->j_state_lock);
1603 jbd2_mark_journal_empty(journal, WRITE_FLUSH_FUA);
1604 mutex_unlock(&journal->j_checkpoint_mutex);
1607 brelse(journal->j_sb_buffer);
1610 if (journal->j_proc_entry)
1611 jbd2_stats_proc_exit(journal);
1612 if (journal->j_inode)
1613 iput(journal->j_inode);
1614 if (journal->j_revoke)
1615 jbd2_journal_destroy_revoke(journal);
1616 kfree(journal->j_wbuf);
1624 *int jbd2_journal_check_used_features () - Check if features specified are used.
1625 * @journal: Journal to check.
1626 * @compat: bitmask of compatible features
1627 * @ro: bitmask of features that force read-only mount
1628 * @incompat: bitmask of incompatible features
1630 * Check whether the journal uses all of a given set of
1631 * features. Return true (non-zero) if it does.
1634 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1635 unsigned long ro, unsigned long incompat)
1637 journal_superblock_t *sb;
1639 if (!compat && !ro && !incompat)
1641 /* Load journal superblock if it is not loaded yet. */
1642 if (journal->j_format_version == 0 &&
1643 journal_get_superblock(journal) != 0)
1645 if (journal->j_format_version == 1)
1648 sb = journal->j_superblock;
1650 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1651 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1652 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1659 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1660 * @journal: Journal to check.
1661 * @compat: bitmask of compatible features
1662 * @ro: bitmask of features that force read-only mount
1663 * @incompat: bitmask of incompatible features
1665 * Check whether the journaling code supports the use of
1666 * all of a given set of features on this journal. Return true
1667 * (non-zero) if it can. */
1669 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1670 unsigned long ro, unsigned long incompat)
1672 if (!compat && !ro && !incompat)
1675 /* We can support any known requested features iff the
1676 * superblock is in version 2. Otherwise we fail to support any
1677 * extended sb features. */
1679 if (journal->j_format_version != 2)
1682 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1683 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1684 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1691 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1692 * @journal: Journal to act on.
1693 * @compat: bitmask of compatible features
1694 * @ro: bitmask of features that force read-only mount
1695 * @incompat: bitmask of incompatible features
1697 * Mark a given journal feature as present on the
1698 * superblock. Returns true if the requested features could be set.
1702 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1703 unsigned long ro, unsigned long incompat)
1705 journal_superblock_t *sb;
1707 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1710 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1713 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1714 compat, ro, incompat);
1716 sb = journal->j_superblock;
1718 sb->s_feature_compat |= cpu_to_be32(compat);
1719 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1720 sb->s_feature_incompat |= cpu_to_be32(incompat);
1726 * jbd2_journal_clear_features () - Clear a given journal feature in the
1728 * @journal: Journal to act on.
1729 * @compat: bitmask of compatible features
1730 * @ro: bitmask of features that force read-only mount
1731 * @incompat: bitmask of incompatible features
1733 * Clear a given journal feature as present on the
1736 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1737 unsigned long ro, unsigned long incompat)
1739 journal_superblock_t *sb;
1741 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1742 compat, ro, incompat);
1744 sb = journal->j_superblock;
1746 sb->s_feature_compat &= ~cpu_to_be32(compat);
1747 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1748 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1750 EXPORT_SYMBOL(jbd2_journal_clear_features);
1753 * int jbd2_journal_update_format () - Update on-disk journal structure.
1754 * @journal: Journal to act on.
1756 * Given an initialised but unloaded journal struct, poke about in the
1757 * on-disk structure to update it to the most recent supported version.
1759 int jbd2_journal_update_format (journal_t *journal)
1761 journal_superblock_t *sb;
1764 err = journal_get_superblock(journal);
1768 sb = journal->j_superblock;
1770 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1771 case JBD2_SUPERBLOCK_V2:
1773 case JBD2_SUPERBLOCK_V1:
1774 return journal_convert_superblock_v1(journal, sb);
1781 static int journal_convert_superblock_v1(journal_t *journal,
1782 journal_superblock_t *sb)
1784 int offset, blocksize;
1785 struct buffer_head *bh;
1788 "JBD2: Converting superblock from version 1 to 2.\n");
1790 /* Pre-initialise new fields to zero */
1791 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1792 blocksize = be32_to_cpu(sb->s_blocksize);
1793 memset(&sb->s_feature_compat, 0, blocksize-offset);
1795 sb->s_nr_users = cpu_to_be32(1);
1796 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1797 journal->j_format_version = 2;
1799 bh = journal->j_sb_buffer;
1800 BUFFER_TRACE(bh, "marking dirty");
1801 mark_buffer_dirty(bh);
1802 sync_dirty_buffer(bh);
1808 * int jbd2_journal_flush () - Flush journal
1809 * @journal: Journal to act on.
1811 * Flush all data for a given journal to disk and empty the journal.
1812 * Filesystems can use this when remounting readonly to ensure that
1813 * recovery does not need to happen on remount.
1816 int jbd2_journal_flush(journal_t *journal)
1819 transaction_t *transaction = NULL;
1821 write_lock(&journal->j_state_lock);
1823 /* Force everything buffered to the log... */
1824 if (journal->j_running_transaction) {
1825 transaction = journal->j_running_transaction;
1826 __jbd2_log_start_commit(journal, transaction->t_tid);
1827 } else if (journal->j_committing_transaction)
1828 transaction = journal->j_committing_transaction;
1830 /* Wait for the log commit to complete... */
1832 tid_t tid = transaction->t_tid;
1834 write_unlock(&journal->j_state_lock);
1835 jbd2_log_wait_commit(journal, tid);
1837 write_unlock(&journal->j_state_lock);
1840 /* ...and flush everything in the log out to disk. */
1841 spin_lock(&journal->j_list_lock);
1842 while (!err && journal->j_checkpoint_transactions != NULL) {
1843 spin_unlock(&journal->j_list_lock);
1844 mutex_lock(&journal->j_checkpoint_mutex);
1845 err = jbd2_log_do_checkpoint(journal);
1846 mutex_unlock(&journal->j_checkpoint_mutex);
1847 spin_lock(&journal->j_list_lock);
1849 spin_unlock(&journal->j_list_lock);
1851 if (is_journal_aborted(journal))
1854 mutex_lock(&journal->j_checkpoint_mutex);
1856 err = jbd2_cleanup_journal_tail(journal);
1858 mutex_unlock(&journal->j_checkpoint_mutex);
1864 /* Finally, mark the journal as really needing no recovery.
1865 * This sets s_start==0 in the underlying superblock, which is
1866 * the magic code for a fully-recovered superblock. Any future
1867 * commits of data to the journal will restore the current
1869 jbd2_mark_journal_empty(journal, WRITE_FUA);
1870 mutex_unlock(&journal->j_checkpoint_mutex);
1871 write_lock(&journal->j_state_lock);
1872 J_ASSERT(!journal->j_running_transaction);
1873 J_ASSERT(!journal->j_committing_transaction);
1874 J_ASSERT(!journal->j_checkpoint_transactions);
1875 J_ASSERT(journal->j_head == journal->j_tail);
1876 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1877 write_unlock(&journal->j_state_lock);
1883 * int jbd2_journal_wipe() - Wipe journal contents
1884 * @journal: Journal to act on.
1885 * @write: flag (see below)
1887 * Wipe out all of the contents of a journal, safely. This will produce
1888 * a warning if the journal contains any valid recovery information.
1889 * Must be called between journal_init_*() and jbd2_journal_load().
1891 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1892 * we merely suppress recovery.
1895 int jbd2_journal_wipe(journal_t *journal, int write)
1899 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1901 err = load_superblock(journal);
1905 if (!journal->j_tail)
1908 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
1909 write ? "Clearing" : "Ignoring");
1911 err = jbd2_journal_skip_recovery(journal);
1913 /* Lock to make assertions happy... */
1914 mutex_lock(&journal->j_checkpoint_mutex);
1915 jbd2_mark_journal_empty(journal, WRITE_FUA);
1916 mutex_unlock(&journal->j_checkpoint_mutex);
1924 * Journal abort has very specific semantics, which we describe
1925 * for journal abort.
1927 * Two internal functions, which provide abort to the jbd layer
1932 * Quick version for internal journal use (doesn't lock the journal).
1933 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1934 * and don't attempt to make any other journal updates.
1936 void __jbd2_journal_abort_hard(journal_t *journal)
1938 transaction_t *transaction;
1940 if (journal->j_flags & JBD2_ABORT)
1943 printk(KERN_ERR "Aborting journal on device %s.\n",
1944 journal->j_devname);
1946 write_lock(&journal->j_state_lock);
1947 journal->j_flags |= JBD2_ABORT;
1948 transaction = journal->j_running_transaction;
1950 __jbd2_log_start_commit(journal, transaction->t_tid);
1951 write_unlock(&journal->j_state_lock);
1954 /* Soft abort: record the abort error status in the journal superblock,
1955 * but don't do any other IO. */
1956 static void __journal_abort_soft (journal_t *journal, int errno)
1958 if (journal->j_flags & JBD2_ABORT)
1961 if (!journal->j_errno)
1962 journal->j_errno = errno;
1964 __jbd2_journal_abort_hard(journal);
1967 jbd2_journal_update_sb_errno(journal);
1968 write_lock(&journal->j_state_lock);
1969 journal->j_flags |= JBD2_REC_ERR;
1970 write_unlock(&journal->j_state_lock);
1975 * void jbd2_journal_abort () - Shutdown the journal immediately.
1976 * @journal: the journal to shutdown.
1977 * @errno: an error number to record in the journal indicating
1978 * the reason for the shutdown.
1980 * Perform a complete, immediate shutdown of the ENTIRE
1981 * journal (not of a single transaction). This operation cannot be
1982 * undone without closing and reopening the journal.
1984 * The jbd2_journal_abort function is intended to support higher level error
1985 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1988 * Journal abort has very specific semantics. Any existing dirty,
1989 * unjournaled buffers in the main filesystem will still be written to
1990 * disk by bdflush, but the journaling mechanism will be suspended
1991 * immediately and no further transaction commits will be honoured.
1993 * Any dirty, journaled buffers will be written back to disk without
1994 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1995 * filesystem, but we _do_ attempt to leave as much data as possible
1996 * behind for fsck to use for cleanup.
1998 * Any attempt to get a new transaction handle on a journal which is in
1999 * ABORT state will just result in an -EROFS error return. A
2000 * jbd2_journal_stop on an existing handle will return -EIO if we have
2001 * entered abort state during the update.
2003 * Recursive transactions are not disturbed by journal abort until the
2004 * final jbd2_journal_stop, which will receive the -EIO error.
2006 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
2007 * which will be recorded (if possible) in the journal superblock. This
2008 * allows a client to record failure conditions in the middle of a
2009 * transaction without having to complete the transaction to record the
2010 * failure to disk. ext3_error, for example, now uses this
2013 * Errors which originate from within the journaling layer will NOT
2014 * supply an errno; a null errno implies that absolutely no further
2015 * writes are done to the journal (unless there are any already in
2020 void jbd2_journal_abort(journal_t *journal, int errno)
2022 __journal_abort_soft(journal, errno);
2026 * int jbd2_journal_errno () - returns the journal's error state.
2027 * @journal: journal to examine.
2029 * This is the errno number set with jbd2_journal_abort(), the last
2030 * time the journal was mounted - if the journal was stopped
2031 * without calling abort this will be 0.
2033 * If the journal has been aborted on this mount time -EROFS will
2036 int jbd2_journal_errno(journal_t *journal)
2040 read_lock(&journal->j_state_lock);
2041 if (journal->j_flags & JBD2_ABORT)
2044 err = journal->j_errno;
2045 read_unlock(&journal->j_state_lock);
2050 * int jbd2_journal_clear_err () - clears the journal's error state
2051 * @journal: journal to act on.
2053 * An error must be cleared or acked to take a FS out of readonly
2056 int jbd2_journal_clear_err(journal_t *journal)
2060 write_lock(&journal->j_state_lock);
2061 if (journal->j_flags & JBD2_ABORT)
2064 journal->j_errno = 0;
2065 write_unlock(&journal->j_state_lock);
2070 * void jbd2_journal_ack_err() - Ack journal err.
2071 * @journal: journal to act on.
2073 * An error must be cleared or acked to take a FS out of readonly
2076 void jbd2_journal_ack_err(journal_t *journal)
2078 write_lock(&journal->j_state_lock);
2079 if (journal->j_errno)
2080 journal->j_flags |= JBD2_ACK_ERR;
2081 write_unlock(&journal->j_state_lock);
2084 int jbd2_journal_blocks_per_page(struct inode *inode)
2086 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2090 * helper functions to deal with 32 or 64bit block numbers.
2092 size_t journal_tag_bytes(journal_t *journal)
2094 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
2095 return JBD2_TAG_SIZE64;
2097 return JBD2_TAG_SIZE32;
2101 * JBD memory management
2103 * These functions are used to allocate block-sized chunks of memory
2104 * used for making copies of buffer_head data. Very often it will be
2105 * page-sized chunks of data, but sometimes it will be in
2106 * sub-page-size chunks. (For example, 16k pages on Power systems
2107 * with a 4k block file system.) For blocks smaller than a page, we
2108 * use a SLAB allocator. There are slab caches for each block size,
2109 * which are allocated at mount time, if necessary, and we only free
2110 * (all of) the slab caches when/if the jbd2 module is unloaded. For
2111 * this reason we don't need to a mutex to protect access to
2112 * jbd2_slab[] allocating or releasing memory; only in
2113 * jbd2_journal_create_slab().
2115 #define JBD2_MAX_SLABS 8
2116 static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
2118 static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2119 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2120 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2124 static void jbd2_journal_destroy_slabs(void)
2128 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2130 kmem_cache_destroy(jbd2_slab[i]);
2131 jbd2_slab[i] = NULL;
2135 static int jbd2_journal_create_slab(size_t size)
2137 static DEFINE_MUTEX(jbd2_slab_create_mutex);
2138 int i = order_base_2(size) - 10;
2141 if (size == PAGE_SIZE)
2144 if (i >= JBD2_MAX_SLABS)
2147 if (unlikely(i < 0))
2149 mutex_lock(&jbd2_slab_create_mutex);
2151 mutex_unlock(&jbd2_slab_create_mutex);
2152 return 0; /* Already created */
2155 slab_size = 1 << (i+10);
2156 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2157 slab_size, 0, NULL);
2158 mutex_unlock(&jbd2_slab_create_mutex);
2159 if (!jbd2_slab[i]) {
2160 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2166 static struct kmem_cache *get_slab(size_t size)
2168 int i = order_base_2(size) - 10;
2170 BUG_ON(i >= JBD2_MAX_SLABS);
2171 if (unlikely(i < 0))
2173 BUG_ON(jbd2_slab[i] == NULL);
2174 return jbd2_slab[i];
2177 void *jbd2_alloc(size_t size, gfp_t flags)
2181 BUG_ON(size & (size-1)); /* Must be a power of 2 */
2183 flags |= __GFP_REPEAT;
2184 if (size == PAGE_SIZE)
2185 ptr = (void *)__get_free_pages(flags, 0);
2186 else if (size > PAGE_SIZE) {
2187 int order = get_order(size);
2190 ptr = (void *)__get_free_pages(flags, order);
2192 ptr = vmalloc(size);
2194 ptr = kmem_cache_alloc(get_slab(size), flags);
2196 /* Check alignment; SLUB has gotten this wrong in the past,
2197 * and this can lead to user data corruption! */
2198 BUG_ON(((unsigned long) ptr) & (size-1));
2203 void jbd2_free(void *ptr, size_t size)
2205 if (size == PAGE_SIZE) {
2206 free_pages((unsigned long)ptr, 0);
2209 if (size > PAGE_SIZE) {
2210 int order = get_order(size);
2213 free_pages((unsigned long)ptr, order);
2218 kmem_cache_free(get_slab(size), ptr);
2222 * Journal_head storage management
2224 static struct kmem_cache *jbd2_journal_head_cache;
2225 #ifdef CONFIG_JBD2_DEBUG
2226 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2229 static int journal_init_jbd2_journal_head_cache(void)
2233 J_ASSERT(jbd2_journal_head_cache == NULL);
2234 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2235 sizeof(struct journal_head),
2237 SLAB_TEMPORARY, /* flags */
2240 if (!jbd2_journal_head_cache) {
2242 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
2247 static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
2249 if (jbd2_journal_head_cache) {
2250 kmem_cache_destroy(jbd2_journal_head_cache);
2251 jbd2_journal_head_cache = NULL;
2256 * journal_head splicing and dicing
2258 static struct journal_head *journal_alloc_journal_head(void)
2260 struct journal_head *ret;
2262 #ifdef CONFIG_JBD2_DEBUG
2263 atomic_inc(&nr_journal_heads);
2265 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2267 jbd_debug(1, "out of memory for journal_head\n");
2268 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
2271 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2277 static void journal_free_journal_head(struct journal_head *jh)
2279 #ifdef CONFIG_JBD2_DEBUG
2280 atomic_dec(&nr_journal_heads);
2281 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2283 kmem_cache_free(jbd2_journal_head_cache, jh);
2287 * A journal_head is attached to a buffer_head whenever JBD has an
2288 * interest in the buffer.
2290 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2291 * is set. This bit is tested in core kernel code where we need to take
2292 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2295 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2297 * When a buffer has its BH_JBD bit set it is immune from being released by
2298 * core kernel code, mainly via ->b_count.
2300 * A journal_head is detached from its buffer_head when the journal_head's
2301 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2302 * transaction (b_cp_transaction) hold their references to b_jcount.
2304 * Various places in the kernel want to attach a journal_head to a buffer_head
2305 * _before_ attaching the journal_head to a transaction. To protect the
2306 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2307 * journal_head's b_jcount refcount by one. The caller must call
2308 * jbd2_journal_put_journal_head() to undo this.
2310 * So the typical usage would be:
2312 * (Attach a journal_head if needed. Increments b_jcount)
2313 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2315 * (Get another reference for transaction)
2316 * jbd2_journal_grab_journal_head(bh);
2317 * jh->b_transaction = xxx;
2318 * (Put original reference)
2319 * jbd2_journal_put_journal_head(jh);
2323 * Give a buffer_head a journal_head.
2327 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2329 struct journal_head *jh;
2330 struct journal_head *new_jh = NULL;
2333 if (!buffer_jbd(bh)) {
2334 new_jh = journal_alloc_journal_head();
2335 memset(new_jh, 0, sizeof(*new_jh));
2338 jbd_lock_bh_journal_head(bh);
2339 if (buffer_jbd(bh)) {
2343 (atomic_read(&bh->b_count) > 0) ||
2344 (bh->b_page && bh->b_page->mapping));
2347 jbd_unlock_bh_journal_head(bh);
2352 new_jh = NULL; /* We consumed it */
2357 BUFFER_TRACE(bh, "added journal_head");
2360 jbd_unlock_bh_journal_head(bh);
2362 journal_free_journal_head(new_jh);
2363 return bh->b_private;
2367 * Grab a ref against this buffer_head's journal_head. If it ended up not
2368 * having a journal_head, return NULL
2370 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2372 struct journal_head *jh = NULL;
2374 jbd_lock_bh_journal_head(bh);
2375 if (buffer_jbd(bh)) {
2379 jbd_unlock_bh_journal_head(bh);
2383 static void __journal_remove_journal_head(struct buffer_head *bh)
2385 struct journal_head *jh = bh2jh(bh);
2387 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2388 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2389 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2390 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2391 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2392 J_ASSERT_BH(bh, buffer_jbd(bh));
2393 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2394 BUFFER_TRACE(bh, "remove journal_head");
2395 if (jh->b_frozen_data) {
2396 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2397 jbd2_free(jh->b_frozen_data, bh->b_size);
2399 if (jh->b_committed_data) {
2400 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2401 jbd2_free(jh->b_committed_data, bh->b_size);
2403 bh->b_private = NULL;
2404 jh->b_bh = NULL; /* debug, really */
2405 clear_buffer_jbd(bh);
2406 journal_free_journal_head(jh);
2410 * Drop a reference on the passed journal_head. If it fell to zero then
2411 * release the journal_head from the buffer_head.
2413 void jbd2_journal_put_journal_head(struct journal_head *jh)
2415 struct buffer_head *bh = jh2bh(jh);
2417 jbd_lock_bh_journal_head(bh);
2418 J_ASSERT_JH(jh, jh->b_jcount > 0);
2420 if (!jh->b_jcount) {
2421 __journal_remove_journal_head(bh);
2422 jbd_unlock_bh_journal_head(bh);
2425 jbd_unlock_bh_journal_head(bh);
2429 * Initialize jbd inode head
2431 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2433 jinode->i_transaction = NULL;
2434 jinode->i_next_transaction = NULL;
2435 jinode->i_vfs_inode = inode;
2436 jinode->i_flags = 0;
2437 INIT_LIST_HEAD(&jinode->i_list);
2441 * Function to be called before we start removing inode from memory (i.e.,
2442 * clear_inode() is a fine place to be called from). It removes inode from
2443 * transaction's lists.
2445 void jbd2_journal_release_jbd_inode(journal_t *journal,
2446 struct jbd2_inode *jinode)
2451 spin_lock(&journal->j_list_lock);
2452 /* Is commit writing out inode - we have to wait */
2453 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
2454 wait_queue_head_t *wq;
2455 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2456 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2457 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2458 spin_unlock(&journal->j_list_lock);
2460 finish_wait(wq, &wait.wait);
2464 if (jinode->i_transaction) {
2465 list_del(&jinode->i_list);
2466 jinode->i_transaction = NULL;
2468 spin_unlock(&journal->j_list_lock);
2474 #ifdef CONFIG_JBD2_DEBUG
2475 u8 jbd2_journal_enable_debug __read_mostly;
2476 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2478 #define JBD2_DEBUG_NAME "jbd2-debug"
2480 static struct dentry *jbd2_debugfs_dir;
2481 static struct dentry *jbd2_debug;
2483 static void __init jbd2_create_debugfs_entry(void)
2485 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2486 if (jbd2_debugfs_dir)
2487 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2490 &jbd2_journal_enable_debug);
2493 static void __exit jbd2_remove_debugfs_entry(void)
2495 debugfs_remove(jbd2_debug);
2496 debugfs_remove(jbd2_debugfs_dir);
2501 static void __init jbd2_create_debugfs_entry(void)
2505 static void __exit jbd2_remove_debugfs_entry(void)
2511 #ifdef CONFIG_PROC_FS
2513 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2515 static void __init jbd2_create_jbd_stats_proc_entry(void)
2517 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2520 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2522 if (proc_jbd2_stats)
2523 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2528 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2529 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2533 struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
2535 static int __init journal_init_handle_cache(void)
2537 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
2538 if (jbd2_handle_cache == NULL) {
2539 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2542 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2543 if (jbd2_inode_cache == NULL) {
2544 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2545 kmem_cache_destroy(jbd2_handle_cache);
2551 static void jbd2_journal_destroy_handle_cache(void)
2553 if (jbd2_handle_cache)
2554 kmem_cache_destroy(jbd2_handle_cache);
2555 if (jbd2_inode_cache)
2556 kmem_cache_destroy(jbd2_inode_cache);
2561 * Module startup and shutdown
2564 static int __init journal_init_caches(void)
2568 ret = jbd2_journal_init_revoke_caches();
2570 ret = journal_init_jbd2_journal_head_cache();
2572 ret = journal_init_handle_cache();
2576 static void jbd2_journal_destroy_caches(void)
2578 jbd2_journal_destroy_revoke_caches();
2579 jbd2_journal_destroy_jbd2_journal_head_cache();
2580 jbd2_journal_destroy_handle_cache();
2581 jbd2_journal_destroy_slabs();
2584 static int __init journal_init(void)
2588 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2590 ret = journal_init_caches();
2592 jbd2_create_debugfs_entry();
2593 jbd2_create_jbd_stats_proc_entry();
2595 jbd2_journal_destroy_caches();
2600 static void __exit journal_exit(void)
2602 #ifdef CONFIG_JBD2_DEBUG
2603 int n = atomic_read(&nr_journal_heads);
2605 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
2607 jbd2_remove_debugfs_entry();
2608 jbd2_remove_jbd_stats_proc_entry();
2609 jbd2_journal_destroy_caches();
2612 MODULE_LICENSE("GPL");
2613 module_init(journal_init);
2614 module_exit(journal_exit);