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>
41 #include <asm/uaccess.h>
44 EXPORT_SYMBOL(jbd2_journal_start);
45 EXPORT_SYMBOL(jbd2_journal_restart);
46 EXPORT_SYMBOL(jbd2_journal_extend);
47 EXPORT_SYMBOL(jbd2_journal_stop);
48 EXPORT_SYMBOL(jbd2_journal_lock_updates);
49 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
50 EXPORT_SYMBOL(jbd2_journal_get_write_access);
51 EXPORT_SYMBOL(jbd2_journal_get_create_access);
52 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
53 EXPORT_SYMBOL(jbd2_journal_dirty_data);
54 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
55 EXPORT_SYMBOL(jbd2_journal_release_buffer);
56 EXPORT_SYMBOL(jbd2_journal_forget);
58 EXPORT_SYMBOL(journal_sync_buffer);
60 EXPORT_SYMBOL(jbd2_journal_flush);
61 EXPORT_SYMBOL(jbd2_journal_revoke);
63 EXPORT_SYMBOL(jbd2_journal_init_dev);
64 EXPORT_SYMBOL(jbd2_journal_init_inode);
65 EXPORT_SYMBOL(jbd2_journal_update_format);
66 EXPORT_SYMBOL(jbd2_journal_check_used_features);
67 EXPORT_SYMBOL(jbd2_journal_check_available_features);
68 EXPORT_SYMBOL(jbd2_journal_set_features);
69 EXPORT_SYMBOL(jbd2_journal_create);
70 EXPORT_SYMBOL(jbd2_journal_load);
71 EXPORT_SYMBOL(jbd2_journal_destroy);
72 EXPORT_SYMBOL(jbd2_journal_update_superblock);
73 EXPORT_SYMBOL(jbd2_journal_abort);
74 EXPORT_SYMBOL(jbd2_journal_errno);
75 EXPORT_SYMBOL(jbd2_journal_ack_err);
76 EXPORT_SYMBOL(jbd2_journal_clear_err);
77 EXPORT_SYMBOL(jbd2_log_wait_commit);
78 EXPORT_SYMBOL(jbd2_journal_start_commit);
79 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
80 EXPORT_SYMBOL(jbd2_journal_wipe);
81 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
82 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
83 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
84 EXPORT_SYMBOL(jbd2_journal_force_commit);
86 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
87 static void __journal_abort_soft (journal_t *journal, int errno);
90 * Helper function used to manage commit timeouts
93 static void commit_timeout(unsigned long __data)
95 struct task_struct * p = (struct task_struct *) __data;
101 * kjournald2: The main thread function used to manage a logging device
104 * This kernel thread is responsible for two things:
106 * 1) COMMIT: Every so often we need to commit the current state of the
107 * filesystem to disk. The journal thread is responsible for writing
108 * all of the metadata buffers to disk.
110 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
111 * of the data in that part of the log has been rewritten elsewhere on
112 * the disk. Flushing these old buffers to reclaim space in the log is
113 * known as checkpointing, and this thread is responsible for that job.
116 static int kjournald2(void *arg)
118 journal_t *journal = arg;
119 transaction_t *transaction;
122 * Set up an interval timer which can be used to trigger a commit wakeup
123 * after the commit interval expires
125 setup_timer(&journal->j_commit_timer, commit_timeout,
126 (unsigned long)current);
128 /* Record that the journal thread is running */
129 journal->j_task = current;
130 wake_up(&journal->j_wait_done_commit);
132 printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
133 journal->j_commit_interval / HZ);
136 * And now, wait forever for commit wakeup events.
138 spin_lock(&journal->j_state_lock);
141 if (journal->j_flags & JBD2_UNMOUNT)
144 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
145 journal->j_commit_sequence, journal->j_commit_request);
147 if (journal->j_commit_sequence != journal->j_commit_request) {
148 jbd_debug(1, "OK, requests differ\n");
149 spin_unlock(&journal->j_state_lock);
150 del_timer_sync(&journal->j_commit_timer);
151 jbd2_journal_commit_transaction(journal);
152 spin_lock(&journal->j_state_lock);
156 wake_up(&journal->j_wait_done_commit);
157 if (freezing(current)) {
159 * The simpler the better. Flushing journal isn't a
160 * good idea, because that depends on threads that may
161 * be already stopped.
163 jbd_debug(1, "Now suspending kjournald2\n");
164 spin_unlock(&journal->j_state_lock);
166 spin_lock(&journal->j_state_lock);
169 * We assume on resume that commits are already there,
173 int should_sleep = 1;
175 prepare_to_wait(&journal->j_wait_commit, &wait,
177 if (journal->j_commit_sequence != journal->j_commit_request)
179 transaction = journal->j_running_transaction;
180 if (transaction && time_after_eq(jiffies,
181 transaction->t_expires))
183 if (journal->j_flags & JBD2_UNMOUNT)
186 spin_unlock(&journal->j_state_lock);
188 spin_lock(&journal->j_state_lock);
190 finish_wait(&journal->j_wait_commit, &wait);
193 jbd_debug(1, "kjournald2 wakes\n");
196 * Were we woken up by a commit wakeup event?
198 transaction = journal->j_running_transaction;
199 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
200 journal->j_commit_request = transaction->t_tid;
201 jbd_debug(1, "woke because of timeout\n");
206 spin_unlock(&journal->j_state_lock);
207 del_timer_sync(&journal->j_commit_timer);
208 journal->j_task = NULL;
209 wake_up(&journal->j_wait_done_commit);
210 jbd_debug(1, "Journal thread exiting.\n");
214 static int jbd2_journal_start_thread(journal_t *journal)
216 struct task_struct *t;
218 t = kthread_run(kjournald2, journal, "kjournald2");
222 wait_event(journal->j_wait_done_commit, journal->j_task != 0);
226 static void journal_kill_thread(journal_t *journal)
228 spin_lock(&journal->j_state_lock);
229 journal->j_flags |= JBD2_UNMOUNT;
231 while (journal->j_task) {
232 wake_up(&journal->j_wait_commit);
233 spin_unlock(&journal->j_state_lock);
234 wait_event(journal->j_wait_done_commit, journal->j_task == 0);
235 spin_lock(&journal->j_state_lock);
237 spin_unlock(&journal->j_state_lock);
241 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
243 * Writes a metadata buffer to a given disk block. The actual IO is not
244 * performed but a new buffer_head is constructed which labels the data
245 * to be written with the correct destination disk block.
247 * Any magic-number escaping which needs to be done will cause a
248 * copy-out here. If the buffer happens to start with the
249 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
250 * magic number is only written to the log for descripter blocks. In
251 * this case, we copy the data and replace the first word with 0, and we
252 * return a result code which indicates that this buffer needs to be
253 * marked as an escaped buffer in the corresponding log descriptor
254 * block. The missing word can then be restored when the block is read
257 * If the source buffer has already been modified by a new transaction
258 * since we took the last commit snapshot, we use the frozen copy of
259 * that data for IO. If we end up using the existing buffer_head's data
260 * for the write, then we *have* to lock the buffer to prevent anyone
261 * else from using and possibly modifying it while the IO is in
264 * The function returns a pointer to the buffer_heads to be used for IO.
266 * We assume that the journal has already been locked in this function.
273 * Bit 0 set == escape performed on the data
274 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
277 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
278 struct journal_head *jh_in,
279 struct journal_head **jh_out,
280 unsigned long long blocknr)
282 int need_copy_out = 0;
283 int done_copy_out = 0;
286 struct buffer_head *new_bh;
287 struct journal_head *new_jh;
288 struct page *new_page;
289 unsigned int new_offset;
290 struct buffer_head *bh_in = jh2bh(jh_in);
293 * The buffer really shouldn't be locked: only the current committing
294 * transaction is allowed to write it, so nobody else is allowed
297 * akpm: except if we're journalling data, and write() output is
298 * also part of a shared mapping, and another thread has
299 * decided to launch a writepage() against this buffer.
301 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
303 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
306 * If a new transaction has already done a buffer copy-out, then
307 * we use that version of the data for the commit.
309 jbd_lock_bh_state(bh_in);
311 if (jh_in->b_frozen_data) {
313 new_page = virt_to_page(jh_in->b_frozen_data);
314 new_offset = offset_in_page(jh_in->b_frozen_data);
316 new_page = jh2bh(jh_in)->b_page;
317 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
320 mapped_data = kmap_atomic(new_page, KM_USER0);
324 if (*((__be32 *)(mapped_data + new_offset)) ==
325 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
329 kunmap_atomic(mapped_data, KM_USER0);
332 * Do we need to do a data copy?
334 if (need_copy_out && !done_copy_out) {
337 jbd_unlock_bh_state(bh_in);
338 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
339 jbd_lock_bh_state(bh_in);
340 if (jh_in->b_frozen_data) {
341 jbd2_free(tmp, bh_in->b_size);
345 jh_in->b_frozen_data = tmp;
346 mapped_data = kmap_atomic(new_page, KM_USER0);
347 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
348 kunmap_atomic(mapped_data, KM_USER0);
350 new_page = virt_to_page(tmp);
351 new_offset = offset_in_page(tmp);
356 * Did we need to do an escaping? Now we've done all the
357 * copying, we can finally do so.
360 mapped_data = kmap_atomic(new_page, KM_USER0);
361 *((unsigned int *)(mapped_data + new_offset)) = 0;
362 kunmap_atomic(mapped_data, KM_USER0);
365 /* keep subsequent assertions sane */
367 init_buffer(new_bh, NULL, NULL);
368 atomic_set(&new_bh->b_count, 1);
369 jbd_unlock_bh_state(bh_in);
371 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
373 set_bh_page(new_bh, new_page, new_offset);
374 new_jh->b_transaction = NULL;
375 new_bh->b_size = jh2bh(jh_in)->b_size;
376 new_bh->b_bdev = transaction->t_journal->j_dev;
377 new_bh->b_blocknr = blocknr;
378 set_buffer_mapped(new_bh);
379 set_buffer_dirty(new_bh);
384 * The to-be-written buffer needs to get moved to the io queue,
385 * and the original buffer whose contents we are shadowing or
386 * copying is moved to the transaction's shadow queue.
388 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
389 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
390 JBUFFER_TRACE(new_jh, "file as BJ_IO");
391 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
393 return do_escape | (done_copy_out << 1);
397 * Allocation code for the journal file. Manage the space left in the
398 * journal, so that we can begin checkpointing when appropriate.
402 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
404 * Called with the journal already locked.
406 * Called under j_state_lock
409 int __jbd2_log_space_left(journal_t *journal)
411 int left = journal->j_free;
413 assert_spin_locked(&journal->j_state_lock);
416 * Be pessimistic here about the number of those free blocks which
417 * might be required for log descriptor control blocks.
420 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
422 left -= MIN_LOG_RESERVED_BLOCKS;
431 * Called under j_state_lock. Returns true if a transaction was started.
433 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
436 * Are we already doing a recent enough commit?
438 if (!tid_geq(journal->j_commit_request, target)) {
440 * We want a new commit: OK, mark the request and wakup the
441 * commit thread. We do _not_ do the commit ourselves.
444 journal->j_commit_request = target;
445 jbd_debug(1, "JBD: requesting commit %d/%d\n",
446 journal->j_commit_request,
447 journal->j_commit_sequence);
448 wake_up(&journal->j_wait_commit);
454 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
458 spin_lock(&journal->j_state_lock);
459 ret = __jbd2_log_start_commit(journal, tid);
460 spin_unlock(&journal->j_state_lock);
465 * Force and wait upon a commit if the calling process is not within
466 * transaction. This is used for forcing out undo-protected data which contains
467 * bitmaps, when the fs is running out of space.
469 * We can only force the running transaction if we don't have an active handle;
470 * otherwise, we will deadlock.
472 * Returns true if a transaction was started.
474 int jbd2_journal_force_commit_nested(journal_t *journal)
476 transaction_t *transaction = NULL;
479 spin_lock(&journal->j_state_lock);
480 if (journal->j_running_transaction && !current->journal_info) {
481 transaction = journal->j_running_transaction;
482 __jbd2_log_start_commit(journal, transaction->t_tid);
483 } else if (journal->j_committing_transaction)
484 transaction = journal->j_committing_transaction;
487 spin_unlock(&journal->j_state_lock);
488 return 0; /* Nothing to retry */
491 tid = transaction->t_tid;
492 spin_unlock(&journal->j_state_lock);
493 jbd2_log_wait_commit(journal, tid);
498 * Start a commit of the current running transaction (if any). Returns true
499 * if a transaction was started, and fills its tid in at *ptid
501 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
505 spin_lock(&journal->j_state_lock);
506 if (journal->j_running_transaction) {
507 tid_t tid = journal->j_running_transaction->t_tid;
509 ret = __jbd2_log_start_commit(journal, tid);
512 } else if (journal->j_committing_transaction && ptid) {
514 * If ext3_write_super() recently started a commit, then we
515 * have to wait for completion of that transaction
517 *ptid = journal->j_committing_transaction->t_tid;
520 spin_unlock(&journal->j_state_lock);
525 * Wait for a specified commit to complete.
526 * The caller may not hold the journal lock.
528 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
532 #ifdef CONFIG_JBD2_DEBUG
533 spin_lock(&journal->j_state_lock);
534 if (!tid_geq(journal->j_commit_request, tid)) {
536 "%s: error: j_commit_request=%d, tid=%d\n",
537 __FUNCTION__, journal->j_commit_request, tid);
539 spin_unlock(&journal->j_state_lock);
541 spin_lock(&journal->j_state_lock);
542 while (tid_gt(tid, journal->j_commit_sequence)) {
543 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
544 tid, journal->j_commit_sequence);
545 wake_up(&journal->j_wait_commit);
546 spin_unlock(&journal->j_state_lock);
547 wait_event(journal->j_wait_done_commit,
548 !tid_gt(tid, journal->j_commit_sequence));
549 spin_lock(&journal->j_state_lock);
551 spin_unlock(&journal->j_state_lock);
553 if (unlikely(is_journal_aborted(journal))) {
554 printk(KERN_EMERG "journal commit I/O error\n");
561 * Log buffer allocation routines:
564 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
566 unsigned long blocknr;
568 spin_lock(&journal->j_state_lock);
569 J_ASSERT(journal->j_free > 1);
571 blocknr = journal->j_head;
574 if (journal->j_head == journal->j_last)
575 journal->j_head = journal->j_first;
576 spin_unlock(&journal->j_state_lock);
577 return jbd2_journal_bmap(journal, blocknr, retp);
581 * Conversion of logical to physical block numbers for the journal
583 * On external journals the journal blocks are identity-mapped, so
584 * this is a no-op. If needed, we can use j_blk_offset - everything is
587 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
588 unsigned long long *retp)
591 unsigned long long ret;
593 if (journal->j_inode) {
594 ret = bmap(journal->j_inode, blocknr);
598 char b[BDEVNAME_SIZE];
600 printk(KERN_ALERT "%s: journal block not found "
601 "at offset %lu on %s\n",
604 bdevname(journal->j_dev, b));
606 __journal_abort_soft(journal, err);
609 *retp = blocknr; /* +journal->j_blk_offset */
615 * We play buffer_head aliasing tricks to write data/metadata blocks to
616 * the journal without copying their contents, but for journal
617 * descriptor blocks we do need to generate bona fide buffers.
619 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
620 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
621 * But we don't bother doing that, so there will be coherency problems with
622 * mmaps of blockdevs which hold live JBD-controlled filesystems.
624 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
626 struct buffer_head *bh;
627 unsigned long long blocknr;
630 err = jbd2_journal_next_log_block(journal, &blocknr);
635 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
637 memset(bh->b_data, 0, journal->j_blocksize);
638 set_buffer_uptodate(bh);
640 BUFFER_TRACE(bh, "return this buffer");
641 return jbd2_journal_add_journal_head(bh);
644 struct jbd2_stats_proc_session {
646 struct transaction_stats_s *stats;
651 static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
652 struct transaction_stats_s *ts,
655 if (ts == s->stats + s->max)
657 if (!first && ts == s->stats + s->start)
659 while (ts->ts_type == 0) {
661 if (ts == s->stats + s->max)
663 if (ts == s->stats + s->start)
670 static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
672 struct jbd2_stats_proc_session *s = seq->private;
673 struct transaction_stats_s *ts;
677 return SEQ_START_TOKEN;
678 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
683 ts = jbd2_history_skip_empty(s, ++ts, 0);
691 static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
693 struct jbd2_stats_proc_session *s = seq->private;
694 struct transaction_stats_s *ts = v;
697 if (v == SEQ_START_TOKEN)
698 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
700 return jbd2_history_skip_empty(s, ++ts, 0);
703 static int jbd2_seq_history_show(struct seq_file *seq, void *v)
705 struct transaction_stats_s *ts = v;
706 if (v == SEQ_START_TOKEN) {
707 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
708 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
709 "wait", "run", "lock", "flush", "log", "hndls",
710 "block", "inlog", "ctime", "write", "drop",
714 if (ts->ts_type == JBD2_STATS_RUN)
715 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
716 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
717 jiffies_to_msecs(ts->u.run.rs_wait),
718 jiffies_to_msecs(ts->u.run.rs_running),
719 jiffies_to_msecs(ts->u.run.rs_locked),
720 jiffies_to_msecs(ts->u.run.rs_flushing),
721 jiffies_to_msecs(ts->u.run.rs_logging),
722 ts->u.run.rs_handle_count,
724 ts->u.run.rs_blocks_logged);
725 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
726 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
727 "C", ts->ts_tid, " ",
728 jiffies_to_msecs(ts->u.chp.cs_chp_time),
729 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
730 ts->u.chp.cs_forced_to_close);
736 static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
740 static struct seq_operations jbd2_seq_history_ops = {
741 .start = jbd2_seq_history_start,
742 .next = jbd2_seq_history_next,
743 .stop = jbd2_seq_history_stop,
744 .show = jbd2_seq_history_show,
747 static int jbd2_seq_history_open(struct inode *inode, struct file *file)
749 journal_t *journal = PDE(inode)->data;
750 struct jbd2_stats_proc_session *s;
753 s = kmalloc(sizeof(*s), GFP_KERNEL);
756 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
757 s->stats = kmalloc(size, GFP_KERNEL);
758 if (s->stats == NULL) {
762 spin_lock(&journal->j_history_lock);
763 memcpy(s->stats, journal->j_history, size);
764 s->max = journal->j_history_max;
765 s->start = journal->j_history_cur % s->max;
766 spin_unlock(&journal->j_history_lock);
768 rc = seq_open(file, &jbd2_seq_history_ops);
770 struct seq_file *m = file->private_data;
780 static int jbd2_seq_history_release(struct inode *inode, struct file *file)
782 struct seq_file *seq = file->private_data;
783 struct jbd2_stats_proc_session *s = seq->private;
787 return seq_release(inode, file);
790 static struct file_operations jbd2_seq_history_fops = {
791 .owner = THIS_MODULE,
792 .open = jbd2_seq_history_open,
795 .release = jbd2_seq_history_release,
798 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
800 return *pos ? NULL : SEQ_START_TOKEN;
803 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
808 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
810 struct jbd2_stats_proc_session *s = seq->private;
812 if (v != SEQ_START_TOKEN)
814 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
816 s->journal->j_max_transaction_buffers);
817 if (s->stats->ts_tid == 0)
819 seq_printf(seq, "average: \n %ums waiting for transaction\n",
820 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
821 seq_printf(seq, " %ums running transaction\n",
822 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
823 seq_printf(seq, " %ums transaction was being locked\n",
824 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
825 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
826 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
827 seq_printf(seq, " %ums logging transaction\n",
828 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
829 seq_printf(seq, " %lu handles per transaction\n",
830 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
831 seq_printf(seq, " %lu blocks per transaction\n",
832 s->stats->u.run.rs_blocks / s->stats->ts_tid);
833 seq_printf(seq, " %lu logged blocks per transaction\n",
834 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
838 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
842 static struct seq_operations jbd2_seq_info_ops = {
843 .start = jbd2_seq_info_start,
844 .next = jbd2_seq_info_next,
845 .stop = jbd2_seq_info_stop,
846 .show = jbd2_seq_info_show,
849 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
851 journal_t *journal = PDE(inode)->data;
852 struct jbd2_stats_proc_session *s;
855 s = kmalloc(sizeof(*s), GFP_KERNEL);
858 size = sizeof(struct transaction_stats_s);
859 s->stats = kmalloc(size, GFP_KERNEL);
860 if (s->stats == NULL) {
864 spin_lock(&journal->j_history_lock);
865 memcpy(s->stats, &journal->j_stats, size);
866 s->journal = journal;
867 spin_unlock(&journal->j_history_lock);
869 rc = seq_open(file, &jbd2_seq_info_ops);
871 struct seq_file *m = file->private_data;
881 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
883 struct seq_file *seq = file->private_data;
884 struct jbd2_stats_proc_session *s = seq->private;
887 return seq_release(inode, file);
890 static struct file_operations jbd2_seq_info_fops = {
891 .owner = THIS_MODULE,
892 .open = jbd2_seq_info_open,
895 .release = jbd2_seq_info_release,
898 static struct proc_dir_entry *proc_jbd2_stats;
900 static void jbd2_stats_proc_init(journal_t *journal)
902 char name[BDEVNAME_SIZE];
904 snprintf(name, sizeof(name) - 1, "%s", bdevname(journal->j_dev, name));
905 journal->j_proc_entry = proc_mkdir(name, proc_jbd2_stats);
906 if (journal->j_proc_entry) {
907 struct proc_dir_entry *p;
908 p = create_proc_entry("history", S_IRUGO,
909 journal->j_proc_entry);
911 p->proc_fops = &jbd2_seq_history_fops;
913 p = create_proc_entry("info", S_IRUGO,
914 journal->j_proc_entry);
916 p->proc_fops = &jbd2_seq_info_fops;
923 static void jbd2_stats_proc_exit(journal_t *journal)
925 char name[BDEVNAME_SIZE];
927 snprintf(name, sizeof(name) - 1, "%s", bdevname(journal->j_dev, name));
928 remove_proc_entry("info", journal->j_proc_entry);
929 remove_proc_entry("history", journal->j_proc_entry);
930 remove_proc_entry(name, proc_jbd2_stats);
933 static void journal_init_stats(journal_t *journal)
937 if (!proc_jbd2_stats)
940 journal->j_history_max = 100;
941 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
942 journal->j_history = kzalloc(size, GFP_KERNEL);
943 if (!journal->j_history) {
944 journal->j_history_max = 0;
947 spin_lock_init(&journal->j_history_lock);
951 * Management for journal control blocks: functions to create and
952 * destroy journal_t structures, and to initialise and read existing
953 * journal blocks from disk. */
955 /* First: create and setup a journal_t object in memory. We initialise
956 * very few fields yet: that has to wait until we have created the
957 * journal structures from from scratch, or loaded them from disk. */
959 static journal_t * journal_init_common (void)
964 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
968 init_waitqueue_head(&journal->j_wait_transaction_locked);
969 init_waitqueue_head(&journal->j_wait_logspace);
970 init_waitqueue_head(&journal->j_wait_done_commit);
971 init_waitqueue_head(&journal->j_wait_checkpoint);
972 init_waitqueue_head(&journal->j_wait_commit);
973 init_waitqueue_head(&journal->j_wait_updates);
974 mutex_init(&journal->j_barrier);
975 mutex_init(&journal->j_checkpoint_mutex);
976 spin_lock_init(&journal->j_revoke_lock);
977 spin_lock_init(&journal->j_list_lock);
978 spin_lock_init(&journal->j_state_lock);
980 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
982 /* The journal is marked for error until we succeed with recovery! */
983 journal->j_flags = JBD2_ABORT;
985 /* Set up a default-sized revoke table for the new mount. */
986 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
992 journal_init_stats(journal);
999 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1001 * Create a journal structure assigned some fixed set of disk blocks to
1002 * the journal. We don't actually touch those disk blocks yet, but we
1003 * need to set up all of the mapping information to tell the journaling
1004 * system where the journal blocks are.
1009 * journal_t * jbd2_journal_init_dev() - creates an initialises a journal structure
1010 * @bdev: Block device on which to create the journal
1011 * @fs_dev: Device which hold journalled filesystem for this journal.
1012 * @start: Block nr Start of journal.
1013 * @len: Length of the journal in blocks.
1014 * @blocksize: blocksize of journalling device
1015 * @returns: a newly created journal_t *
1017 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1018 * range of blocks on an arbitrary block device.
1021 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1022 struct block_device *fs_dev,
1023 unsigned long long start, int len, int blocksize)
1025 journal_t *journal = journal_init_common();
1026 struct buffer_head *bh;
1032 /* journal descriptor can store up to n blocks -bzzz */
1033 journal->j_blocksize = blocksize;
1034 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1035 journal->j_wbufsize = n;
1036 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1037 if (!journal->j_wbuf) {
1038 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1044 journal->j_dev = bdev;
1045 journal->j_fs_dev = fs_dev;
1046 journal->j_blk_offset = start;
1047 journal->j_maxlen = len;
1048 jbd2_stats_proc_init(journal);
1050 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1051 J_ASSERT(bh != NULL);
1052 journal->j_sb_buffer = bh;
1053 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1059 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1060 * @inode: An inode to create the journal in
1062 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1063 * the journal. The inode must exist already, must support bmap() and
1064 * must have all data blocks preallocated.
1066 journal_t * jbd2_journal_init_inode (struct inode *inode)
1068 struct buffer_head *bh;
1069 journal_t *journal = journal_init_common();
1072 unsigned long long blocknr;
1077 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1078 journal->j_inode = inode;
1080 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1081 journal, inode->i_sb->s_id, inode->i_ino,
1082 (long long) inode->i_size,
1083 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1085 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1086 journal->j_blocksize = inode->i_sb->s_blocksize;
1087 jbd2_stats_proc_init(journal);
1089 /* journal descriptor can store up to n blocks -bzzz */
1090 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1091 journal->j_wbufsize = n;
1092 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1093 if (!journal->j_wbuf) {
1094 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1100 err = jbd2_journal_bmap(journal, 0, &blocknr);
1101 /* If that failed, give up */
1103 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
1109 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1110 J_ASSERT(bh != NULL);
1111 journal->j_sb_buffer = bh;
1112 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1118 * If the journal init or create aborts, we need to mark the journal
1119 * superblock as being NULL to prevent the journal destroy from writing
1120 * back a bogus superblock.
1122 static void journal_fail_superblock (journal_t *journal)
1124 struct buffer_head *bh = journal->j_sb_buffer;
1126 journal->j_sb_buffer = NULL;
1130 * Given a journal_t structure, initialise the various fields for
1131 * startup of a new journaling session. We use this both when creating
1132 * a journal, and after recovering an old journal to reset it for
1136 static int journal_reset(journal_t *journal)
1138 journal_superblock_t *sb = journal->j_superblock;
1139 unsigned long long first, last;
1141 first = be32_to_cpu(sb->s_first);
1142 last = be32_to_cpu(sb->s_maxlen);
1144 journal->j_first = first;
1145 journal->j_last = last;
1147 journal->j_head = first;
1148 journal->j_tail = first;
1149 journal->j_free = last - first;
1151 journal->j_tail_sequence = journal->j_transaction_sequence;
1152 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1153 journal->j_commit_request = journal->j_commit_sequence;
1155 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1157 /* Add the dynamic fields and write it to disk. */
1158 jbd2_journal_update_superblock(journal, 1);
1159 return jbd2_journal_start_thread(journal);
1163 * int jbd2_journal_create() - Initialise the new journal file
1164 * @journal: Journal to create. This structure must have been initialised
1166 * Given a journal_t structure which tells us which disk blocks we can
1167 * use, create a new journal superblock and initialise all of the
1168 * journal fields from scratch.
1170 int jbd2_journal_create(journal_t *journal)
1172 unsigned long long blocknr;
1173 struct buffer_head *bh;
1174 journal_superblock_t *sb;
1177 if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
1178 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
1180 journal_fail_superblock(journal);
1184 if (journal->j_inode == NULL) {
1186 * We don't know what block to start at!
1189 "%s: creation of journal on external device!\n",
1194 /* Zero out the entire journal on disk. We cannot afford to
1195 have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
1196 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1197 for (i = 0; i < journal->j_maxlen; i++) {
1198 err = jbd2_journal_bmap(journal, i, &blocknr);
1201 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1203 memset (bh->b_data, 0, journal->j_blocksize);
1204 BUFFER_TRACE(bh, "marking dirty");
1205 mark_buffer_dirty(bh);
1206 BUFFER_TRACE(bh, "marking uptodate");
1207 set_buffer_uptodate(bh);
1212 sync_blockdev(journal->j_dev);
1213 jbd_debug(1, "JBD: journal cleared.\n");
1215 /* OK, fill in the initial static fields in the new superblock */
1216 sb = journal->j_superblock;
1218 sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
1219 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1221 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1222 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1223 sb->s_first = cpu_to_be32(1);
1225 journal->j_transaction_sequence = 1;
1227 journal->j_flags &= ~JBD2_ABORT;
1228 journal->j_format_version = 2;
1230 return journal_reset(journal);
1234 * void jbd2_journal_update_superblock() - Update journal sb on disk.
1235 * @journal: The journal to update.
1236 * @wait: Set to '0' if you don't want to wait for IO completion.
1238 * Update a journal's dynamic superblock fields and write it to disk,
1239 * optionally waiting for the IO to complete.
1241 void jbd2_journal_update_superblock(journal_t *journal, int wait)
1243 journal_superblock_t *sb = journal->j_superblock;
1244 struct buffer_head *bh = journal->j_sb_buffer;
1247 * As a special case, if the on-disk copy is already marked as needing
1248 * no recovery (s_start == 0) and there are no outstanding transactions
1249 * in the filesystem, then we can safely defer the superblock update
1250 * until the next commit by setting JBD2_FLUSHED. This avoids
1251 * attempting a write to a potential-readonly device.
1253 if (sb->s_start == 0 && journal->j_tail_sequence ==
1254 journal->j_transaction_sequence) {
1255 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1256 "(start %ld, seq %d, errno %d)\n",
1257 journal->j_tail, journal->j_tail_sequence,
1262 spin_lock(&journal->j_state_lock);
1263 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1264 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1266 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1267 sb->s_start = cpu_to_be32(journal->j_tail);
1268 sb->s_errno = cpu_to_be32(journal->j_errno);
1269 spin_unlock(&journal->j_state_lock);
1271 BUFFER_TRACE(bh, "marking dirty");
1272 mark_buffer_dirty(bh);
1274 sync_dirty_buffer(bh);
1276 ll_rw_block(SWRITE, 1, &bh);
1279 /* If we have just flushed the log (by marking s_start==0), then
1280 * any future commit will have to be careful to update the
1281 * superblock again to re-record the true start of the log. */
1283 spin_lock(&journal->j_state_lock);
1285 journal->j_flags &= ~JBD2_FLUSHED;
1287 journal->j_flags |= JBD2_FLUSHED;
1288 spin_unlock(&journal->j_state_lock);
1292 * Read the superblock for a given journal, performing initial
1293 * validation of the format.
1296 static int journal_get_superblock(journal_t *journal)
1298 struct buffer_head *bh;
1299 journal_superblock_t *sb;
1302 bh = journal->j_sb_buffer;
1304 J_ASSERT(bh != NULL);
1305 if (!buffer_uptodate(bh)) {
1306 ll_rw_block(READ, 1, &bh);
1308 if (!buffer_uptodate(bh)) {
1310 "JBD: IO error reading journal superblock\n");
1315 sb = journal->j_superblock;
1319 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1320 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1321 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1325 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1326 case JBD2_SUPERBLOCK_V1:
1327 journal->j_format_version = 1;
1329 case JBD2_SUPERBLOCK_V2:
1330 journal->j_format_version = 2;
1333 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1337 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1338 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1339 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1340 printk (KERN_WARNING "JBD: journal file too short\n");
1347 journal_fail_superblock(journal);
1352 * Load the on-disk journal superblock and read the key fields into the
1356 static int load_superblock(journal_t *journal)
1359 journal_superblock_t *sb;
1361 err = journal_get_superblock(journal);
1365 sb = journal->j_superblock;
1367 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1368 journal->j_tail = be32_to_cpu(sb->s_start);
1369 journal->j_first = be32_to_cpu(sb->s_first);
1370 journal->j_last = be32_to_cpu(sb->s_maxlen);
1371 journal->j_errno = be32_to_cpu(sb->s_errno);
1378 * int jbd2_journal_load() - Read journal from disk.
1379 * @journal: Journal to act on.
1381 * Given a journal_t structure which tells us which disk blocks contain
1382 * a journal, read the journal from disk to initialise the in-memory
1385 int jbd2_journal_load(journal_t *journal)
1388 journal_superblock_t *sb;
1390 err = load_superblock(journal);
1394 sb = journal->j_superblock;
1395 /* If this is a V2 superblock, then we have to check the
1396 * features flags on it. */
1398 if (journal->j_format_version >= 2) {
1399 if ((sb->s_feature_ro_compat &
1400 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1401 (sb->s_feature_incompat &
1402 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1403 printk (KERN_WARNING
1404 "JBD: Unrecognised features on journal\n");
1409 /* Let the recovery code check whether it needs to recover any
1410 * data from the journal. */
1411 if (jbd2_journal_recover(journal))
1412 goto recovery_error;
1414 /* OK, we've finished with the dynamic journal bits:
1415 * reinitialise the dynamic contents of the superblock in memory
1416 * and reset them on disk. */
1417 if (journal_reset(journal))
1418 goto recovery_error;
1420 journal->j_flags &= ~JBD2_ABORT;
1421 journal->j_flags |= JBD2_LOADED;
1425 printk (KERN_WARNING "JBD: recovery failed\n");
1430 * void jbd2_journal_destroy() - Release a journal_t structure.
1431 * @journal: Journal to act on.
1433 * Release a journal_t structure once it is no longer in use by the
1436 void jbd2_journal_destroy(journal_t *journal)
1438 /* Wait for the commit thread to wake up and die. */
1439 journal_kill_thread(journal);
1441 /* Force a final log commit */
1442 if (journal->j_running_transaction)
1443 jbd2_journal_commit_transaction(journal);
1445 /* Force any old transactions to disk */
1447 /* Totally anal locking here... */
1448 spin_lock(&journal->j_list_lock);
1449 while (journal->j_checkpoint_transactions != NULL) {
1450 spin_unlock(&journal->j_list_lock);
1451 jbd2_log_do_checkpoint(journal);
1452 spin_lock(&journal->j_list_lock);
1455 J_ASSERT(journal->j_running_transaction == NULL);
1456 J_ASSERT(journal->j_committing_transaction == NULL);
1457 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1458 spin_unlock(&journal->j_list_lock);
1460 /* We can now mark the journal as empty. */
1461 journal->j_tail = 0;
1462 journal->j_tail_sequence = ++journal->j_transaction_sequence;
1463 if (journal->j_sb_buffer) {
1464 jbd2_journal_update_superblock(journal, 1);
1465 brelse(journal->j_sb_buffer);
1468 if (journal->j_proc_entry)
1469 jbd2_stats_proc_exit(journal);
1470 if (journal->j_inode)
1471 iput(journal->j_inode);
1472 if (journal->j_revoke)
1473 jbd2_journal_destroy_revoke(journal);
1474 kfree(journal->j_wbuf);
1480 *int jbd2_journal_check_used_features () - Check if features specified are used.
1481 * @journal: Journal to check.
1482 * @compat: bitmask of compatible features
1483 * @ro: bitmask of features that force read-only mount
1484 * @incompat: bitmask of incompatible features
1486 * Check whether the journal uses all of a given set of
1487 * features. Return true (non-zero) if it does.
1490 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1491 unsigned long ro, unsigned long incompat)
1493 journal_superblock_t *sb;
1495 if (!compat && !ro && !incompat)
1497 if (journal->j_format_version == 1)
1500 sb = journal->j_superblock;
1502 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1503 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1504 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1511 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1512 * @journal: Journal to check.
1513 * @compat: bitmask of compatible features
1514 * @ro: bitmask of features that force read-only mount
1515 * @incompat: bitmask of incompatible features
1517 * Check whether the journaling code supports the use of
1518 * all of a given set of features on this journal. Return true
1519 * (non-zero) if it can. */
1521 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1522 unsigned long ro, unsigned long incompat)
1524 journal_superblock_t *sb;
1526 if (!compat && !ro && !incompat)
1529 sb = journal->j_superblock;
1531 /* We can support any known requested features iff the
1532 * superblock is in version 2. Otherwise we fail to support any
1533 * extended sb features. */
1535 if (journal->j_format_version != 2)
1538 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1539 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1540 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1547 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1548 * @journal: Journal to act on.
1549 * @compat: bitmask of compatible features
1550 * @ro: bitmask of features that force read-only mount
1551 * @incompat: bitmask of incompatible features
1553 * Mark a given journal feature as present on the
1554 * superblock. Returns true if the requested features could be set.
1558 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1559 unsigned long ro, unsigned long incompat)
1561 journal_superblock_t *sb;
1563 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1566 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1569 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1570 compat, ro, incompat);
1572 sb = journal->j_superblock;
1574 sb->s_feature_compat |= cpu_to_be32(compat);
1575 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1576 sb->s_feature_incompat |= cpu_to_be32(incompat);
1583 * int jbd2_journal_update_format () - Update on-disk journal structure.
1584 * @journal: Journal to act on.
1586 * Given an initialised but unloaded journal struct, poke about in the
1587 * on-disk structure to update it to the most recent supported version.
1589 int jbd2_journal_update_format (journal_t *journal)
1591 journal_superblock_t *sb;
1594 err = journal_get_superblock(journal);
1598 sb = journal->j_superblock;
1600 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1601 case JBD2_SUPERBLOCK_V2:
1603 case JBD2_SUPERBLOCK_V1:
1604 return journal_convert_superblock_v1(journal, sb);
1611 static int journal_convert_superblock_v1(journal_t *journal,
1612 journal_superblock_t *sb)
1614 int offset, blocksize;
1615 struct buffer_head *bh;
1618 "JBD: Converting superblock from version 1 to 2.\n");
1620 /* Pre-initialise new fields to zero */
1621 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1622 blocksize = be32_to_cpu(sb->s_blocksize);
1623 memset(&sb->s_feature_compat, 0, blocksize-offset);
1625 sb->s_nr_users = cpu_to_be32(1);
1626 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1627 journal->j_format_version = 2;
1629 bh = journal->j_sb_buffer;
1630 BUFFER_TRACE(bh, "marking dirty");
1631 mark_buffer_dirty(bh);
1632 sync_dirty_buffer(bh);
1638 * int jbd2_journal_flush () - Flush journal
1639 * @journal: Journal to act on.
1641 * Flush all data for a given journal to disk and empty the journal.
1642 * Filesystems can use this when remounting readonly to ensure that
1643 * recovery does not need to happen on remount.
1646 int jbd2_journal_flush(journal_t *journal)
1649 transaction_t *transaction = NULL;
1650 unsigned long old_tail;
1652 spin_lock(&journal->j_state_lock);
1654 /* Force everything buffered to the log... */
1655 if (journal->j_running_transaction) {
1656 transaction = journal->j_running_transaction;
1657 __jbd2_log_start_commit(journal, transaction->t_tid);
1658 } else if (journal->j_committing_transaction)
1659 transaction = journal->j_committing_transaction;
1661 /* Wait for the log commit to complete... */
1663 tid_t tid = transaction->t_tid;
1665 spin_unlock(&journal->j_state_lock);
1666 jbd2_log_wait_commit(journal, tid);
1668 spin_unlock(&journal->j_state_lock);
1671 /* ...and flush everything in the log out to disk. */
1672 spin_lock(&journal->j_list_lock);
1673 while (!err && journal->j_checkpoint_transactions != NULL) {
1674 spin_unlock(&journal->j_list_lock);
1675 err = jbd2_log_do_checkpoint(journal);
1676 spin_lock(&journal->j_list_lock);
1678 spin_unlock(&journal->j_list_lock);
1679 jbd2_cleanup_journal_tail(journal);
1681 /* Finally, mark the journal as really needing no recovery.
1682 * This sets s_start==0 in the underlying superblock, which is
1683 * the magic code for a fully-recovered superblock. Any future
1684 * commits of data to the journal will restore the current
1686 spin_lock(&journal->j_state_lock);
1687 old_tail = journal->j_tail;
1688 journal->j_tail = 0;
1689 spin_unlock(&journal->j_state_lock);
1690 jbd2_journal_update_superblock(journal, 1);
1691 spin_lock(&journal->j_state_lock);
1692 journal->j_tail = old_tail;
1694 J_ASSERT(!journal->j_running_transaction);
1695 J_ASSERT(!journal->j_committing_transaction);
1696 J_ASSERT(!journal->j_checkpoint_transactions);
1697 J_ASSERT(journal->j_head == journal->j_tail);
1698 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1699 spin_unlock(&journal->j_state_lock);
1704 * int jbd2_journal_wipe() - Wipe journal contents
1705 * @journal: Journal to act on.
1706 * @write: flag (see below)
1708 * Wipe out all of the contents of a journal, safely. This will produce
1709 * a warning if the journal contains any valid recovery information.
1710 * Must be called between journal_init_*() and jbd2_journal_load().
1712 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1713 * we merely suppress recovery.
1716 int jbd2_journal_wipe(journal_t *journal, int write)
1718 journal_superblock_t *sb;
1721 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1723 err = load_superblock(journal);
1727 sb = journal->j_superblock;
1729 if (!journal->j_tail)
1732 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1733 write ? "Clearing" : "Ignoring");
1735 err = jbd2_journal_skip_recovery(journal);
1737 jbd2_journal_update_superblock(journal, 1);
1744 * journal_dev_name: format a character string to describe on what
1745 * device this journal is present.
1748 static const char *journal_dev_name(journal_t *journal, char *buffer)
1750 struct block_device *bdev;
1752 if (journal->j_inode)
1753 bdev = journal->j_inode->i_sb->s_bdev;
1755 bdev = journal->j_dev;
1757 return bdevname(bdev, buffer);
1761 * Journal abort has very specific semantics, which we describe
1762 * for journal abort.
1764 * Two internal function, which provide abort to te jbd layer
1769 * Quick version for internal journal use (doesn't lock the journal).
1770 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1771 * and don't attempt to make any other journal updates.
1773 void __jbd2_journal_abort_hard(journal_t *journal)
1775 transaction_t *transaction;
1776 char b[BDEVNAME_SIZE];
1778 if (journal->j_flags & JBD2_ABORT)
1781 printk(KERN_ERR "Aborting journal on device %s.\n",
1782 journal_dev_name(journal, b));
1784 spin_lock(&journal->j_state_lock);
1785 journal->j_flags |= JBD2_ABORT;
1786 transaction = journal->j_running_transaction;
1788 __jbd2_log_start_commit(journal, transaction->t_tid);
1789 spin_unlock(&journal->j_state_lock);
1792 /* Soft abort: record the abort error status in the journal superblock,
1793 * but don't do any other IO. */
1794 static void __journal_abort_soft (journal_t *journal, int errno)
1796 if (journal->j_flags & JBD2_ABORT)
1799 if (!journal->j_errno)
1800 journal->j_errno = errno;
1802 __jbd2_journal_abort_hard(journal);
1805 jbd2_journal_update_superblock(journal, 1);
1809 * void jbd2_journal_abort () - Shutdown the journal immediately.
1810 * @journal: the journal to shutdown.
1811 * @errno: an error number to record in the journal indicating
1812 * the reason for the shutdown.
1814 * Perform a complete, immediate shutdown of the ENTIRE
1815 * journal (not of a single transaction). This operation cannot be
1816 * undone without closing and reopening the journal.
1818 * The jbd2_journal_abort function is intended to support higher level error
1819 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1822 * Journal abort has very specific semantics. Any existing dirty,
1823 * unjournaled buffers in the main filesystem will still be written to
1824 * disk by bdflush, but the journaling mechanism will be suspended
1825 * immediately and no further transaction commits will be honoured.
1827 * Any dirty, journaled buffers will be written back to disk without
1828 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1829 * filesystem, but we _do_ attempt to leave as much data as possible
1830 * behind for fsck to use for cleanup.
1832 * Any attempt to get a new transaction handle on a journal which is in
1833 * ABORT state will just result in an -EROFS error return. A
1834 * jbd2_journal_stop on an existing handle will return -EIO if we have
1835 * entered abort state during the update.
1837 * Recursive transactions are not disturbed by journal abort until the
1838 * final jbd2_journal_stop, which will receive the -EIO error.
1840 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
1841 * which will be recorded (if possible) in the journal superblock. This
1842 * allows a client to record failure conditions in the middle of a
1843 * transaction without having to complete the transaction to record the
1844 * failure to disk. ext3_error, for example, now uses this
1847 * Errors which originate from within the journaling layer will NOT
1848 * supply an errno; a null errno implies that absolutely no further
1849 * writes are done to the journal (unless there are any already in
1854 void jbd2_journal_abort(journal_t *journal, int errno)
1856 __journal_abort_soft(journal, errno);
1860 * int jbd2_journal_errno () - returns the journal's error state.
1861 * @journal: journal to examine.
1863 * This is the errno numbet set with jbd2_journal_abort(), the last
1864 * time the journal was mounted - if the journal was stopped
1865 * without calling abort this will be 0.
1867 * If the journal has been aborted on this mount time -EROFS will
1870 int jbd2_journal_errno(journal_t *journal)
1874 spin_lock(&journal->j_state_lock);
1875 if (journal->j_flags & JBD2_ABORT)
1878 err = journal->j_errno;
1879 spin_unlock(&journal->j_state_lock);
1884 * int jbd2_journal_clear_err () - clears the journal's error state
1885 * @journal: journal to act on.
1887 * An error must be cleared or Acked to take a FS out of readonly
1890 int jbd2_journal_clear_err(journal_t *journal)
1894 spin_lock(&journal->j_state_lock);
1895 if (journal->j_flags & JBD2_ABORT)
1898 journal->j_errno = 0;
1899 spin_unlock(&journal->j_state_lock);
1904 * void jbd2_journal_ack_err() - Ack journal err.
1905 * @journal: journal to act on.
1907 * An error must be cleared or Acked to take a FS out of readonly
1910 void jbd2_journal_ack_err(journal_t *journal)
1912 spin_lock(&journal->j_state_lock);
1913 if (journal->j_errno)
1914 journal->j_flags |= JBD2_ACK_ERR;
1915 spin_unlock(&journal->j_state_lock);
1918 int jbd2_journal_blocks_per_page(struct inode *inode)
1920 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1924 * helper functions to deal with 32 or 64bit block numbers.
1926 size_t journal_tag_bytes(journal_t *journal)
1928 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1929 return JBD2_TAG_SIZE64;
1931 return JBD2_TAG_SIZE32;
1935 * Journal_head storage management
1937 static struct kmem_cache *jbd2_journal_head_cache;
1938 #ifdef CONFIG_JBD2_DEBUG
1939 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1942 static int journal_init_jbd2_journal_head_cache(void)
1946 J_ASSERT(jbd2_journal_head_cache == 0);
1947 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
1948 sizeof(struct journal_head),
1953 if (jbd2_journal_head_cache == 0) {
1955 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1960 static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
1962 J_ASSERT(jbd2_journal_head_cache != NULL);
1963 kmem_cache_destroy(jbd2_journal_head_cache);
1964 jbd2_journal_head_cache = NULL;
1968 * journal_head splicing and dicing
1970 static struct journal_head *journal_alloc_journal_head(void)
1972 struct journal_head *ret;
1973 static unsigned long last_warning;
1975 #ifdef CONFIG_JBD2_DEBUG
1976 atomic_inc(&nr_journal_heads);
1978 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
1980 jbd_debug(1, "out of memory for journal_head\n");
1981 if (time_after(jiffies, last_warning + 5*HZ)) {
1982 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1984 last_warning = jiffies;
1988 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
1994 static void journal_free_journal_head(struct journal_head *jh)
1996 #ifdef CONFIG_JBD2_DEBUG
1997 atomic_dec(&nr_journal_heads);
1998 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2000 kmem_cache_free(jbd2_journal_head_cache, jh);
2004 * A journal_head is attached to a buffer_head whenever JBD has an
2005 * interest in the buffer.
2007 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2008 * is set. This bit is tested in core kernel code where we need to take
2009 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2012 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2014 * When a buffer has its BH_JBD bit set it is immune from being released by
2015 * core kernel code, mainly via ->b_count.
2017 * A journal_head may be detached from its buffer_head when the journal_head's
2018 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
2019 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
2020 * journal_head can be dropped if needed.
2022 * Various places in the kernel want to attach a journal_head to a buffer_head
2023 * _before_ attaching the journal_head to a transaction. To protect the
2024 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2025 * journal_head's b_jcount refcount by one. The caller must call
2026 * jbd2_journal_put_journal_head() to undo this.
2028 * So the typical usage would be:
2030 * (Attach a journal_head if needed. Increments b_jcount)
2031 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2033 * jh->b_transaction = xxx;
2034 * jbd2_journal_put_journal_head(jh);
2036 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2037 * because it has a non-zero b_transaction.
2041 * Give a buffer_head a journal_head.
2043 * Doesn't need the journal lock.
2046 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2048 struct journal_head *jh;
2049 struct journal_head *new_jh = NULL;
2052 if (!buffer_jbd(bh)) {
2053 new_jh = journal_alloc_journal_head();
2054 memset(new_jh, 0, sizeof(*new_jh));
2057 jbd_lock_bh_journal_head(bh);
2058 if (buffer_jbd(bh)) {
2062 (atomic_read(&bh->b_count) > 0) ||
2063 (bh->b_page && bh->b_page->mapping));
2066 jbd_unlock_bh_journal_head(bh);
2071 new_jh = NULL; /* We consumed it */
2076 BUFFER_TRACE(bh, "added journal_head");
2079 jbd_unlock_bh_journal_head(bh);
2081 journal_free_journal_head(new_jh);
2082 return bh->b_private;
2086 * Grab a ref against this buffer_head's journal_head. If it ended up not
2087 * having a journal_head, return NULL
2089 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2091 struct journal_head *jh = NULL;
2093 jbd_lock_bh_journal_head(bh);
2094 if (buffer_jbd(bh)) {
2098 jbd_unlock_bh_journal_head(bh);
2102 static void __journal_remove_journal_head(struct buffer_head *bh)
2104 struct journal_head *jh = bh2jh(bh);
2106 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2109 if (jh->b_jcount == 0) {
2110 if (jh->b_transaction == NULL &&
2111 jh->b_next_transaction == NULL &&
2112 jh->b_cp_transaction == NULL) {
2113 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2114 J_ASSERT_BH(bh, buffer_jbd(bh));
2115 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2116 BUFFER_TRACE(bh, "remove journal_head");
2117 if (jh->b_frozen_data) {
2118 printk(KERN_WARNING "%s: freeing "
2121 jbd2_free(jh->b_frozen_data, bh->b_size);
2123 if (jh->b_committed_data) {
2124 printk(KERN_WARNING "%s: freeing "
2125 "b_committed_data\n",
2127 jbd2_free(jh->b_committed_data, bh->b_size);
2129 bh->b_private = NULL;
2130 jh->b_bh = NULL; /* debug, really */
2131 clear_buffer_jbd(bh);
2133 journal_free_journal_head(jh);
2135 BUFFER_TRACE(bh, "journal_head was locked");
2141 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
2142 * and has a zero b_jcount then remove and release its journal_head. If we did
2143 * see that the buffer is not used by any transaction we also "logically"
2144 * decrement ->b_count.
2146 * We in fact take an additional increment on ->b_count as a convenience,
2147 * because the caller usually wants to do additional things with the bh
2148 * after calling here.
2149 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
2150 * time. Once the caller has run __brelse(), the buffer is eligible for
2151 * reaping by try_to_free_buffers().
2153 void jbd2_journal_remove_journal_head(struct buffer_head *bh)
2155 jbd_lock_bh_journal_head(bh);
2156 __journal_remove_journal_head(bh);
2157 jbd_unlock_bh_journal_head(bh);
2161 * Drop a reference on the passed journal_head. If it fell to zero then try to
2162 * release the journal_head from the buffer_head.
2164 void jbd2_journal_put_journal_head(struct journal_head *jh)
2166 struct buffer_head *bh = jh2bh(jh);
2168 jbd_lock_bh_journal_head(bh);
2169 J_ASSERT_JH(jh, jh->b_jcount > 0);
2171 if (!jh->b_jcount && !jh->b_transaction) {
2172 __journal_remove_journal_head(bh);
2175 jbd_unlock_bh_journal_head(bh);
2181 #ifdef CONFIG_JBD2_DEBUG
2182 u8 jbd2_journal_enable_debug __read_mostly;
2183 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2185 #define JBD2_DEBUG_NAME "jbd2-debug"
2187 static struct dentry *jbd2_debugfs_dir;
2188 static struct dentry *jbd2_debug;
2190 static void __init jbd2_create_debugfs_entry(void)
2192 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2193 if (jbd2_debugfs_dir)
2194 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2196 &jbd2_journal_enable_debug);
2199 static void __exit jbd2_remove_debugfs_entry(void)
2201 debugfs_remove(jbd2_debug);
2202 debugfs_remove(jbd2_debugfs_dir);
2207 static void __init jbd2_create_debugfs_entry(void)
2211 static void __exit jbd2_remove_debugfs_entry(void)
2217 #ifdef CONFIG_PROC_FS
2219 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2221 static void __init jbd2_create_jbd_stats_proc_entry(void)
2223 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2226 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2228 if (proc_jbd2_stats)
2229 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2234 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2235 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2239 struct kmem_cache *jbd2_handle_cache;
2241 static int __init journal_init_handle_cache(void)
2243 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
2248 if (jbd2_handle_cache == NULL) {
2249 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2255 static void jbd2_journal_destroy_handle_cache(void)
2257 if (jbd2_handle_cache)
2258 kmem_cache_destroy(jbd2_handle_cache);
2262 * Module startup and shutdown
2265 static int __init journal_init_caches(void)
2269 ret = jbd2_journal_init_revoke_caches();
2271 ret = journal_init_jbd2_journal_head_cache();
2273 ret = journal_init_handle_cache();
2277 static void jbd2_journal_destroy_caches(void)
2279 jbd2_journal_destroy_revoke_caches();
2280 jbd2_journal_destroy_jbd2_journal_head_cache();
2281 jbd2_journal_destroy_handle_cache();
2284 static int __init journal_init(void)
2288 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2290 ret = journal_init_caches();
2292 jbd2_journal_destroy_caches();
2293 jbd2_create_debugfs_entry();
2294 jbd2_create_jbd_stats_proc_entry();
2298 static void __exit journal_exit(void)
2300 #ifdef CONFIG_JBD2_DEBUG
2301 int n = atomic_read(&nr_journal_heads);
2303 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2305 jbd2_remove_debugfs_entry();
2306 jbd2_remove_jbd_stats_proc_entry();
2307 jbd2_journal_destroy_caches();
2310 MODULE_LICENSE("GPL");
2311 module_init(journal_init);
2312 module_exit(journal_exit);