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_metadata);
54 EXPORT_SYMBOL(jbd2_journal_release_buffer);
55 EXPORT_SYMBOL(jbd2_journal_forget);
57 EXPORT_SYMBOL(journal_sync_buffer);
59 EXPORT_SYMBOL(jbd2_journal_flush);
60 EXPORT_SYMBOL(jbd2_journal_revoke);
62 EXPORT_SYMBOL(jbd2_journal_init_dev);
63 EXPORT_SYMBOL(jbd2_journal_init_inode);
64 EXPORT_SYMBOL(jbd2_journal_update_format);
65 EXPORT_SYMBOL(jbd2_journal_check_used_features);
66 EXPORT_SYMBOL(jbd2_journal_check_available_features);
67 EXPORT_SYMBOL(jbd2_journal_set_features);
68 EXPORT_SYMBOL(jbd2_journal_create);
69 EXPORT_SYMBOL(jbd2_journal_load);
70 EXPORT_SYMBOL(jbd2_journal_destroy);
71 EXPORT_SYMBOL(jbd2_journal_abort);
72 EXPORT_SYMBOL(jbd2_journal_errno);
73 EXPORT_SYMBOL(jbd2_journal_ack_err);
74 EXPORT_SYMBOL(jbd2_journal_clear_err);
75 EXPORT_SYMBOL(jbd2_log_wait_commit);
76 EXPORT_SYMBOL(jbd2_journal_start_commit);
77 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
78 EXPORT_SYMBOL(jbd2_journal_wipe);
79 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
80 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
81 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
82 EXPORT_SYMBOL(jbd2_journal_force_commit);
83 EXPORT_SYMBOL(jbd2_journal_file_inode);
84 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
85 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
86 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
88 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
89 static void __journal_abort_soft (journal_t *journal, int errno);
92 * Helper function used to manage commit timeouts
95 static void commit_timeout(unsigned long __data)
97 struct task_struct * p = (struct task_struct *) __data;
103 * kjournald2: The main thread function used to manage a logging device
106 * This kernel thread is responsible for two things:
108 * 1) COMMIT: Every so often we need to commit the current state of the
109 * filesystem to disk. The journal thread is responsible for writing
110 * all of the metadata buffers to disk.
112 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
113 * of the data in that part of the log has been rewritten elsewhere on
114 * the disk. Flushing these old buffers to reclaim space in the log is
115 * known as checkpointing, and this thread is responsible for that job.
118 static int kjournald2(void *arg)
120 journal_t *journal = arg;
121 transaction_t *transaction;
124 * Set up an interval timer which can be used to trigger a commit wakeup
125 * after the commit interval expires
127 setup_timer(&journal->j_commit_timer, commit_timeout,
128 (unsigned long)current);
130 /* Record that the journal thread is running */
131 journal->j_task = current;
132 wake_up(&journal->j_wait_done_commit);
134 printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
135 journal->j_commit_interval / HZ);
138 * And now, wait forever for commit wakeup events.
140 spin_lock(&journal->j_state_lock);
143 if (journal->j_flags & JBD2_UNMOUNT)
146 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
147 journal->j_commit_sequence, journal->j_commit_request);
149 if (journal->j_commit_sequence != journal->j_commit_request) {
150 jbd_debug(1, "OK, requests differ\n");
151 spin_unlock(&journal->j_state_lock);
152 del_timer_sync(&journal->j_commit_timer);
153 jbd2_journal_commit_transaction(journal);
154 spin_lock(&journal->j_state_lock);
158 wake_up(&journal->j_wait_done_commit);
159 if (freezing(current)) {
161 * The simpler the better. Flushing journal isn't a
162 * good idea, because that depends on threads that may
163 * be already stopped.
165 jbd_debug(1, "Now suspending kjournald2\n");
166 spin_unlock(&journal->j_state_lock);
168 spin_lock(&journal->j_state_lock);
171 * We assume on resume that commits are already there,
175 int should_sleep = 1;
177 prepare_to_wait(&journal->j_wait_commit, &wait,
179 if (journal->j_commit_sequence != journal->j_commit_request)
181 transaction = journal->j_running_transaction;
182 if (transaction && time_after_eq(jiffies,
183 transaction->t_expires))
185 if (journal->j_flags & JBD2_UNMOUNT)
188 spin_unlock(&journal->j_state_lock);
190 spin_lock(&journal->j_state_lock);
192 finish_wait(&journal->j_wait_commit, &wait);
195 jbd_debug(1, "kjournald2 wakes\n");
198 * Were we woken up by a commit wakeup event?
200 transaction = journal->j_running_transaction;
201 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
202 journal->j_commit_request = transaction->t_tid;
203 jbd_debug(1, "woke because of timeout\n");
208 spin_unlock(&journal->j_state_lock);
209 del_timer_sync(&journal->j_commit_timer);
210 journal->j_task = NULL;
211 wake_up(&journal->j_wait_done_commit);
212 jbd_debug(1, "Journal thread exiting.\n");
216 static int jbd2_journal_start_thread(journal_t *journal)
218 struct task_struct *t;
220 t = kthread_run(kjournald2, journal, "kjournald2");
224 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
228 static void journal_kill_thread(journal_t *journal)
230 spin_lock(&journal->j_state_lock);
231 journal->j_flags |= JBD2_UNMOUNT;
233 while (journal->j_task) {
234 wake_up(&journal->j_wait_commit);
235 spin_unlock(&journal->j_state_lock);
236 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
237 spin_lock(&journal->j_state_lock);
239 spin_unlock(&journal->j_state_lock);
243 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
245 * Writes a metadata buffer to a given disk block. The actual IO is not
246 * performed but a new buffer_head is constructed which labels the data
247 * to be written with the correct destination disk block.
249 * Any magic-number escaping which needs to be done will cause a
250 * copy-out here. If the buffer happens to start with the
251 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
252 * magic number is only written to the log for descripter blocks. In
253 * this case, we copy the data and replace the first word with 0, and we
254 * return a result code which indicates that this buffer needs to be
255 * marked as an escaped buffer in the corresponding log descriptor
256 * block. The missing word can then be restored when the block is read
259 * If the source buffer has already been modified by a new transaction
260 * since we took the last commit snapshot, we use the frozen copy of
261 * that data for IO. If we end up using the existing buffer_head's data
262 * for the write, then we *have* to lock the buffer to prevent anyone
263 * else from using and possibly modifying it while the IO is in
266 * The function returns a pointer to the buffer_heads to be used for IO.
268 * We assume that the journal has already been locked in this function.
275 * Bit 0 set == escape performed on the data
276 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
279 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
280 struct journal_head *jh_in,
281 struct journal_head **jh_out,
282 unsigned long long blocknr)
284 int need_copy_out = 0;
285 int done_copy_out = 0;
288 struct buffer_head *new_bh;
289 struct journal_head *new_jh;
290 struct page *new_page;
291 unsigned int new_offset;
292 struct buffer_head *bh_in = jh2bh(jh_in);
295 * The buffer really shouldn't be locked: only the current committing
296 * transaction is allowed to write it, so nobody else is allowed
299 * akpm: except if we're journalling data, and write() output is
300 * also part of a shared mapping, and another thread has
301 * decided to launch a writepage() against this buffer.
303 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
305 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
308 * If a new transaction has already done a buffer copy-out, then
309 * we use that version of the data for the commit.
311 jbd_lock_bh_state(bh_in);
313 if (jh_in->b_frozen_data) {
315 new_page = virt_to_page(jh_in->b_frozen_data);
316 new_offset = offset_in_page(jh_in->b_frozen_data);
318 new_page = jh2bh(jh_in)->b_page;
319 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
322 mapped_data = kmap_atomic(new_page, KM_USER0);
326 if (*((__be32 *)(mapped_data + new_offset)) ==
327 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
331 kunmap_atomic(mapped_data, KM_USER0);
334 * Do we need to do a data copy?
336 if (need_copy_out && !done_copy_out) {
339 jbd_unlock_bh_state(bh_in);
340 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
341 jbd_lock_bh_state(bh_in);
342 if (jh_in->b_frozen_data) {
343 jbd2_free(tmp, bh_in->b_size);
347 jh_in->b_frozen_data = tmp;
348 mapped_data = kmap_atomic(new_page, KM_USER0);
349 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
350 kunmap_atomic(mapped_data, KM_USER0);
352 new_page = virt_to_page(tmp);
353 new_offset = offset_in_page(tmp);
358 * Did we need to do an escaping? Now we've done all the
359 * copying, we can finally do so.
362 mapped_data = kmap_atomic(new_page, KM_USER0);
363 *((unsigned int *)(mapped_data + new_offset)) = 0;
364 kunmap_atomic(mapped_data, KM_USER0);
367 /* keep subsequent assertions sane */
369 init_buffer(new_bh, NULL, NULL);
370 atomic_set(&new_bh->b_count, 1);
371 jbd_unlock_bh_state(bh_in);
373 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
375 set_bh_page(new_bh, new_page, new_offset);
376 new_jh->b_transaction = NULL;
377 new_bh->b_size = jh2bh(jh_in)->b_size;
378 new_bh->b_bdev = transaction->t_journal->j_dev;
379 new_bh->b_blocknr = blocknr;
380 set_buffer_mapped(new_bh);
381 set_buffer_dirty(new_bh);
386 * The to-be-written buffer needs to get moved to the io queue,
387 * and the original buffer whose contents we are shadowing or
388 * copying is moved to the transaction's shadow queue.
390 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
391 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
392 JBUFFER_TRACE(new_jh, "file as BJ_IO");
393 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
395 return do_escape | (done_copy_out << 1);
399 * Allocation code for the journal file. Manage the space left in the
400 * journal, so that we can begin checkpointing when appropriate.
404 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
406 * Called with the journal already locked.
408 * Called under j_state_lock
411 int __jbd2_log_space_left(journal_t *journal)
413 int left = journal->j_free;
415 assert_spin_locked(&journal->j_state_lock);
418 * Be pessimistic here about the number of those free blocks which
419 * might be required for log descriptor control blocks.
422 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
424 left -= MIN_LOG_RESERVED_BLOCKS;
433 * Called under j_state_lock. Returns true if a transaction was started.
435 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
438 * Are we already doing a recent enough commit?
440 if (!tid_geq(journal->j_commit_request, target)) {
442 * We want a new commit: OK, mark the request and wakup the
443 * commit thread. We do _not_ do the commit ourselves.
446 journal->j_commit_request = target;
447 jbd_debug(1, "JBD: requesting commit %d/%d\n",
448 journal->j_commit_request,
449 journal->j_commit_sequence);
450 wake_up(&journal->j_wait_commit);
456 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
460 spin_lock(&journal->j_state_lock);
461 ret = __jbd2_log_start_commit(journal, tid);
462 spin_unlock(&journal->j_state_lock);
467 * Force and wait upon a commit if the calling process is not within
468 * transaction. This is used for forcing out undo-protected data which contains
469 * bitmaps, when the fs is running out of space.
471 * We can only force the running transaction if we don't have an active handle;
472 * otherwise, we will deadlock.
474 * Returns true if a transaction was started.
476 int jbd2_journal_force_commit_nested(journal_t *journal)
478 transaction_t *transaction = NULL;
481 spin_lock(&journal->j_state_lock);
482 if (journal->j_running_transaction && !current->journal_info) {
483 transaction = journal->j_running_transaction;
484 __jbd2_log_start_commit(journal, transaction->t_tid);
485 } else if (journal->j_committing_transaction)
486 transaction = journal->j_committing_transaction;
489 spin_unlock(&journal->j_state_lock);
490 return 0; /* Nothing to retry */
493 tid = transaction->t_tid;
494 spin_unlock(&journal->j_state_lock);
495 jbd2_log_wait_commit(journal, tid);
500 * Start a commit of the current running transaction (if any). Returns true
501 * if a transaction was started, and fills its tid in at *ptid
503 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
507 spin_lock(&journal->j_state_lock);
508 if (journal->j_running_transaction) {
509 tid_t tid = journal->j_running_transaction->t_tid;
511 ret = __jbd2_log_start_commit(journal, tid);
514 } else if (journal->j_committing_transaction && ptid) {
516 * If ext3_write_super() recently started a commit, then we
517 * have to wait for completion of that transaction
519 *ptid = journal->j_committing_transaction->t_tid;
522 spin_unlock(&journal->j_state_lock);
527 * Wait for a specified commit to complete.
528 * The caller may not hold the journal lock.
530 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
534 #ifdef CONFIG_JBD2_DEBUG
535 spin_lock(&journal->j_state_lock);
536 if (!tid_geq(journal->j_commit_request, tid)) {
538 "%s: error: j_commit_request=%d, tid=%d\n",
539 __func__, journal->j_commit_request, tid);
541 spin_unlock(&journal->j_state_lock);
543 spin_lock(&journal->j_state_lock);
544 while (tid_gt(tid, journal->j_commit_sequence)) {
545 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
546 tid, journal->j_commit_sequence);
547 wake_up(&journal->j_wait_commit);
548 spin_unlock(&journal->j_state_lock);
549 wait_event(journal->j_wait_done_commit,
550 !tid_gt(tid, journal->j_commit_sequence));
551 spin_lock(&journal->j_state_lock);
553 spin_unlock(&journal->j_state_lock);
555 if (unlikely(is_journal_aborted(journal))) {
556 printk(KERN_EMERG "journal commit I/O error\n");
563 * Log buffer allocation routines:
566 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
568 unsigned long blocknr;
570 spin_lock(&journal->j_state_lock);
571 J_ASSERT(journal->j_free > 1);
573 blocknr = journal->j_head;
576 if (journal->j_head == journal->j_last)
577 journal->j_head = journal->j_first;
578 spin_unlock(&journal->j_state_lock);
579 return jbd2_journal_bmap(journal, blocknr, retp);
583 * Conversion of logical to physical block numbers for the journal
585 * On external journals the journal blocks are identity-mapped, so
586 * this is a no-op. If needed, we can use j_blk_offset - everything is
589 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
590 unsigned long long *retp)
593 unsigned long long ret;
595 if (journal->j_inode) {
596 ret = bmap(journal->j_inode, blocknr);
600 printk(KERN_ALERT "%s: journal block not found "
601 "at offset %lu on %s\n",
602 __func__, blocknr, journal->j_devname);
604 __journal_abort_soft(journal, err);
607 *retp = blocknr; /* +journal->j_blk_offset */
613 * We play buffer_head aliasing tricks to write data/metadata blocks to
614 * the journal without copying their contents, but for journal
615 * descriptor blocks we do need to generate bona fide buffers.
617 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
618 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
619 * But we don't bother doing that, so there will be coherency problems with
620 * mmaps of blockdevs which hold live JBD-controlled filesystems.
622 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
624 struct buffer_head *bh;
625 unsigned long long blocknr;
628 err = jbd2_journal_next_log_block(journal, &blocknr);
633 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
635 memset(bh->b_data, 0, journal->j_blocksize);
636 set_buffer_uptodate(bh);
638 BUFFER_TRACE(bh, "return this buffer");
639 return jbd2_journal_add_journal_head(bh);
642 struct jbd2_stats_proc_session {
644 struct transaction_stats_s *stats;
649 static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
650 struct transaction_stats_s *ts,
653 if (ts == s->stats + s->max)
655 if (!first && ts == s->stats + s->start)
657 while (ts->ts_type == 0) {
659 if (ts == s->stats + s->max)
661 if (ts == s->stats + s->start)
668 static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
670 struct jbd2_stats_proc_session *s = seq->private;
671 struct transaction_stats_s *ts;
675 return SEQ_START_TOKEN;
676 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
681 ts = jbd2_history_skip_empty(s, ++ts, 0);
689 static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
691 struct jbd2_stats_proc_session *s = seq->private;
692 struct transaction_stats_s *ts = v;
695 if (v == SEQ_START_TOKEN)
696 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
698 return jbd2_history_skip_empty(s, ++ts, 0);
701 static int jbd2_seq_history_show(struct seq_file *seq, void *v)
703 struct transaction_stats_s *ts = v;
704 if (v == SEQ_START_TOKEN) {
705 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
706 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
707 "wait", "run", "lock", "flush", "log", "hndls",
708 "block", "inlog", "ctime", "write", "drop",
712 if (ts->ts_type == JBD2_STATS_RUN)
713 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
714 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
715 jiffies_to_msecs(ts->u.run.rs_wait),
716 jiffies_to_msecs(ts->u.run.rs_running),
717 jiffies_to_msecs(ts->u.run.rs_locked),
718 jiffies_to_msecs(ts->u.run.rs_flushing),
719 jiffies_to_msecs(ts->u.run.rs_logging),
720 ts->u.run.rs_handle_count,
722 ts->u.run.rs_blocks_logged);
723 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
724 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
725 "C", ts->ts_tid, " ",
726 jiffies_to_msecs(ts->u.chp.cs_chp_time),
727 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
728 ts->u.chp.cs_forced_to_close);
734 static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
738 static struct seq_operations jbd2_seq_history_ops = {
739 .start = jbd2_seq_history_start,
740 .next = jbd2_seq_history_next,
741 .stop = jbd2_seq_history_stop,
742 .show = jbd2_seq_history_show,
745 static int jbd2_seq_history_open(struct inode *inode, struct file *file)
747 journal_t *journal = PDE(inode)->data;
748 struct jbd2_stats_proc_session *s;
751 s = kmalloc(sizeof(*s), GFP_KERNEL);
754 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
755 s->stats = kmalloc(size, GFP_KERNEL);
756 if (s->stats == NULL) {
760 spin_lock(&journal->j_history_lock);
761 memcpy(s->stats, journal->j_history, size);
762 s->max = journal->j_history_max;
763 s->start = journal->j_history_cur % s->max;
764 spin_unlock(&journal->j_history_lock);
766 rc = seq_open(file, &jbd2_seq_history_ops);
768 struct seq_file *m = file->private_data;
778 static int jbd2_seq_history_release(struct inode *inode, struct file *file)
780 struct seq_file *seq = file->private_data;
781 struct jbd2_stats_proc_session *s = seq->private;
785 return seq_release(inode, file);
788 static struct file_operations jbd2_seq_history_fops = {
789 .owner = THIS_MODULE,
790 .open = jbd2_seq_history_open,
793 .release = jbd2_seq_history_release,
796 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
798 return *pos ? NULL : SEQ_START_TOKEN;
801 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
806 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
808 struct jbd2_stats_proc_session *s = seq->private;
810 if (v != SEQ_START_TOKEN)
812 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
814 s->journal->j_max_transaction_buffers);
815 if (s->stats->ts_tid == 0)
817 seq_printf(seq, "average: \n %ums waiting for transaction\n",
818 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
819 seq_printf(seq, " %ums running transaction\n",
820 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
821 seq_printf(seq, " %ums transaction was being locked\n",
822 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
823 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
824 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
825 seq_printf(seq, " %ums logging transaction\n",
826 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
827 seq_printf(seq, " %lu handles per transaction\n",
828 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
829 seq_printf(seq, " %lu blocks per transaction\n",
830 s->stats->u.run.rs_blocks / s->stats->ts_tid);
831 seq_printf(seq, " %lu logged blocks per transaction\n",
832 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
836 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
840 static struct seq_operations jbd2_seq_info_ops = {
841 .start = jbd2_seq_info_start,
842 .next = jbd2_seq_info_next,
843 .stop = jbd2_seq_info_stop,
844 .show = jbd2_seq_info_show,
847 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
849 journal_t *journal = PDE(inode)->data;
850 struct jbd2_stats_proc_session *s;
853 s = kmalloc(sizeof(*s), GFP_KERNEL);
856 size = sizeof(struct transaction_stats_s);
857 s->stats = kmalloc(size, GFP_KERNEL);
858 if (s->stats == NULL) {
862 spin_lock(&journal->j_history_lock);
863 memcpy(s->stats, &journal->j_stats, size);
864 s->journal = journal;
865 spin_unlock(&journal->j_history_lock);
867 rc = seq_open(file, &jbd2_seq_info_ops);
869 struct seq_file *m = file->private_data;
879 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
881 struct seq_file *seq = file->private_data;
882 struct jbd2_stats_proc_session *s = seq->private;
885 return seq_release(inode, file);
888 static struct file_operations jbd2_seq_info_fops = {
889 .owner = THIS_MODULE,
890 .open = jbd2_seq_info_open,
893 .release = jbd2_seq_info_release,
896 static struct proc_dir_entry *proc_jbd2_stats;
898 static void jbd2_stats_proc_init(journal_t *journal)
900 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
901 if (journal->j_proc_entry) {
902 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
903 &jbd2_seq_history_fops, journal);
904 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
905 &jbd2_seq_info_fops, journal);
909 static void jbd2_stats_proc_exit(journal_t *journal)
911 remove_proc_entry("info", journal->j_proc_entry);
912 remove_proc_entry("history", journal->j_proc_entry);
913 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
916 static void journal_init_stats(journal_t *journal)
920 if (!proc_jbd2_stats)
923 journal->j_history_max = 100;
924 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
925 journal->j_history = kzalloc(size, GFP_KERNEL);
926 if (!journal->j_history) {
927 journal->j_history_max = 0;
930 spin_lock_init(&journal->j_history_lock);
934 * Management for journal control blocks: functions to create and
935 * destroy journal_t structures, and to initialise and read existing
936 * journal blocks from disk. */
938 /* First: create and setup a journal_t object in memory. We initialise
939 * very few fields yet: that has to wait until we have created the
940 * journal structures from from scratch, or loaded them from disk. */
942 static journal_t * journal_init_common (void)
947 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
951 init_waitqueue_head(&journal->j_wait_transaction_locked);
952 init_waitqueue_head(&journal->j_wait_logspace);
953 init_waitqueue_head(&journal->j_wait_done_commit);
954 init_waitqueue_head(&journal->j_wait_checkpoint);
955 init_waitqueue_head(&journal->j_wait_commit);
956 init_waitqueue_head(&journal->j_wait_updates);
957 mutex_init(&journal->j_barrier);
958 mutex_init(&journal->j_checkpoint_mutex);
959 spin_lock_init(&journal->j_revoke_lock);
960 spin_lock_init(&journal->j_list_lock);
961 spin_lock_init(&journal->j_state_lock);
963 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
965 /* The journal is marked for error until we succeed with recovery! */
966 journal->j_flags = JBD2_ABORT;
968 /* Set up a default-sized revoke table for the new mount. */
969 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
975 journal_init_stats(journal);
982 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
984 * Create a journal structure assigned some fixed set of disk blocks to
985 * the journal. We don't actually touch those disk blocks yet, but we
986 * need to set up all of the mapping information to tell the journaling
987 * system where the journal blocks are.
992 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
993 * @bdev: Block device on which to create the journal
994 * @fs_dev: Device which hold journalled filesystem for this journal.
995 * @start: Block nr Start of journal.
996 * @len: Length of the journal in blocks.
997 * @blocksize: blocksize of journalling device
999 * Returns: a newly created journal_t *
1001 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1002 * range of blocks on an arbitrary block device.
1005 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1006 struct block_device *fs_dev,
1007 unsigned long long start, int len, int blocksize)
1009 journal_t *journal = journal_init_common();
1010 struct buffer_head *bh;
1017 /* journal descriptor can store up to n blocks -bzzz */
1018 journal->j_blocksize = blocksize;
1019 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1020 journal->j_wbufsize = n;
1021 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1022 if (!journal->j_wbuf) {
1023 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1029 journal->j_dev = bdev;
1030 journal->j_fs_dev = fs_dev;
1031 journal->j_blk_offset = start;
1032 journal->j_maxlen = len;
1033 bdevname(journal->j_dev, journal->j_devname);
1034 p = journal->j_devname;
1035 while ((p = strchr(p, '/')))
1037 jbd2_stats_proc_init(journal);
1039 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1040 J_ASSERT(bh != NULL);
1041 journal->j_sb_buffer = bh;
1042 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1048 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1049 * @inode: An inode to create the journal in
1051 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1052 * the journal. The inode must exist already, must support bmap() and
1053 * must have all data blocks preallocated.
1055 journal_t * jbd2_journal_init_inode (struct inode *inode)
1057 struct buffer_head *bh;
1058 journal_t *journal = journal_init_common();
1062 unsigned long long blocknr;
1067 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1068 journal->j_inode = inode;
1069 bdevname(journal->j_dev, journal->j_devname);
1070 p = journal->j_devname;
1071 while ((p = strchr(p, '/')))
1073 p = journal->j_devname + strlen(journal->j_devname);
1074 sprintf(p, ":%lu", journal->j_inode->i_ino);
1076 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1077 journal, inode->i_sb->s_id, inode->i_ino,
1078 (long long) inode->i_size,
1079 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1081 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1082 journal->j_blocksize = inode->i_sb->s_blocksize;
1083 jbd2_stats_proc_init(journal);
1085 /* journal descriptor can store up to n blocks -bzzz */
1086 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1087 journal->j_wbufsize = n;
1088 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1089 if (!journal->j_wbuf) {
1090 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1092 jbd2_stats_proc_exit(journal);
1097 err = jbd2_journal_bmap(journal, 0, &blocknr);
1098 /* If that failed, give up */
1100 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
1102 jbd2_stats_proc_exit(journal);
1107 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1108 J_ASSERT(bh != NULL);
1109 journal->j_sb_buffer = bh;
1110 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1116 * If the journal init or create aborts, we need to mark the journal
1117 * superblock as being NULL to prevent the journal destroy from writing
1118 * back a bogus superblock.
1120 static void journal_fail_superblock (journal_t *journal)
1122 struct buffer_head *bh = journal->j_sb_buffer;
1124 journal->j_sb_buffer = NULL;
1128 * Given a journal_t structure, initialise the various fields for
1129 * startup of a new journaling session. We use this both when creating
1130 * a journal, and after recovering an old journal to reset it for
1134 static int journal_reset(journal_t *journal)
1136 journal_superblock_t *sb = journal->j_superblock;
1137 unsigned long long first, last;
1139 first = be32_to_cpu(sb->s_first);
1140 last = be32_to_cpu(sb->s_maxlen);
1142 journal->j_first = first;
1143 journal->j_last = last;
1145 journal->j_head = first;
1146 journal->j_tail = first;
1147 journal->j_free = last - first;
1149 journal->j_tail_sequence = journal->j_transaction_sequence;
1150 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1151 journal->j_commit_request = journal->j_commit_sequence;
1153 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1155 /* Add the dynamic fields and write it to disk. */
1156 jbd2_journal_update_superblock(journal, 1);
1157 return jbd2_journal_start_thread(journal);
1161 * int jbd2_journal_create() - Initialise the new journal file
1162 * @journal: Journal to create. This structure must have been initialised
1164 * Given a journal_t structure which tells us which disk blocks we can
1165 * use, create a new journal superblock and initialise all of the
1166 * journal fields from scratch.
1168 int jbd2_journal_create(journal_t *journal)
1170 unsigned long long blocknr;
1171 struct buffer_head *bh;
1172 journal_superblock_t *sb;
1175 if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
1176 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
1178 journal_fail_superblock(journal);
1182 if (journal->j_inode == NULL) {
1184 * We don't know what block to start at!
1187 "%s: creation of journal on external device!\n",
1192 /* Zero out the entire journal on disk. We cannot afford to
1193 have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
1194 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1195 for (i = 0; i < journal->j_maxlen; i++) {
1196 err = jbd2_journal_bmap(journal, i, &blocknr);
1199 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1201 memset (bh->b_data, 0, journal->j_blocksize);
1202 BUFFER_TRACE(bh, "marking dirty");
1203 mark_buffer_dirty(bh);
1204 BUFFER_TRACE(bh, "marking uptodate");
1205 set_buffer_uptodate(bh);
1210 sync_blockdev(journal->j_dev);
1211 jbd_debug(1, "JBD: journal cleared.\n");
1213 /* OK, fill in the initial static fields in the new superblock */
1214 sb = journal->j_superblock;
1216 sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
1217 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1219 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1220 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1221 sb->s_first = cpu_to_be32(1);
1223 journal->j_transaction_sequence = 1;
1225 journal->j_flags &= ~JBD2_ABORT;
1226 journal->j_format_version = 2;
1228 return journal_reset(journal);
1232 * void jbd2_journal_update_superblock() - Update journal sb on disk.
1233 * @journal: The journal to update.
1234 * @wait: Set to '0' if you don't want to wait for IO completion.
1236 * Update a journal's dynamic superblock fields and write it to disk,
1237 * optionally waiting for the IO to complete.
1239 void jbd2_journal_update_superblock(journal_t *journal, int wait)
1241 journal_superblock_t *sb = journal->j_superblock;
1242 struct buffer_head *bh = journal->j_sb_buffer;
1245 * As a special case, if the on-disk copy is already marked as needing
1246 * no recovery (s_start == 0) and there are no outstanding transactions
1247 * in the filesystem, then we can safely defer the superblock update
1248 * until the next commit by setting JBD2_FLUSHED. This avoids
1249 * attempting a write to a potential-readonly device.
1251 if (sb->s_start == 0 && journal->j_tail_sequence ==
1252 journal->j_transaction_sequence) {
1253 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1254 "(start %ld, seq %d, errno %d)\n",
1255 journal->j_tail, journal->j_tail_sequence,
1260 if (buffer_write_io_error(bh)) {
1262 * Oh, dear. A previous attempt to write the journal
1263 * superblock failed. This could happen because the
1264 * USB device was yanked out. Or it could happen to
1265 * be a transient write error and maybe the block will
1266 * be remapped. Nothing we can do but to retry the
1267 * write and hope for the best.
1269 printk(KERN_ERR "JBD2: previous I/O error detected "
1270 "for journal superblock update for %s.\n",
1271 journal->j_devname);
1272 clear_buffer_write_io_error(bh);
1273 set_buffer_uptodate(bh);
1276 spin_lock(&journal->j_state_lock);
1277 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1278 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1280 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1281 sb->s_start = cpu_to_be32(journal->j_tail);
1282 sb->s_errno = cpu_to_be32(journal->j_errno);
1283 spin_unlock(&journal->j_state_lock);
1285 BUFFER_TRACE(bh, "marking dirty");
1286 mark_buffer_dirty(bh);
1288 sync_dirty_buffer(bh);
1289 if (buffer_write_io_error(bh)) {
1290 printk(KERN_ERR "JBD2: I/O error detected "
1291 "when updating journal superblock for %s.\n",
1292 journal->j_devname);
1293 clear_buffer_write_io_error(bh);
1294 set_buffer_uptodate(bh);
1297 ll_rw_block(SWRITE, 1, &bh);
1300 /* If we have just flushed the log (by marking s_start==0), then
1301 * any future commit will have to be careful to update the
1302 * superblock again to re-record the true start of the log. */
1304 spin_lock(&journal->j_state_lock);
1306 journal->j_flags &= ~JBD2_FLUSHED;
1308 journal->j_flags |= JBD2_FLUSHED;
1309 spin_unlock(&journal->j_state_lock);
1313 * Read the superblock for a given journal, performing initial
1314 * validation of the format.
1317 static int journal_get_superblock(journal_t *journal)
1319 struct buffer_head *bh;
1320 journal_superblock_t *sb;
1323 bh = journal->j_sb_buffer;
1325 J_ASSERT(bh != NULL);
1326 if (!buffer_uptodate(bh)) {
1327 ll_rw_block(READ, 1, &bh);
1329 if (!buffer_uptodate(bh)) {
1331 "JBD: IO error reading journal superblock\n");
1336 sb = journal->j_superblock;
1340 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1341 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1342 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1346 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1347 case JBD2_SUPERBLOCK_V1:
1348 journal->j_format_version = 1;
1350 case JBD2_SUPERBLOCK_V2:
1351 journal->j_format_version = 2;
1354 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1358 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1359 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1360 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1361 printk (KERN_WARNING "JBD: journal file too short\n");
1368 journal_fail_superblock(journal);
1373 * Load the on-disk journal superblock and read the key fields into the
1377 static int load_superblock(journal_t *journal)
1380 journal_superblock_t *sb;
1382 err = journal_get_superblock(journal);
1386 sb = journal->j_superblock;
1388 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1389 journal->j_tail = be32_to_cpu(sb->s_start);
1390 journal->j_first = be32_to_cpu(sb->s_first);
1391 journal->j_last = be32_to_cpu(sb->s_maxlen);
1392 journal->j_errno = be32_to_cpu(sb->s_errno);
1399 * int jbd2_journal_load() - Read journal from disk.
1400 * @journal: Journal to act on.
1402 * Given a journal_t structure which tells us which disk blocks contain
1403 * a journal, read the journal from disk to initialise the in-memory
1406 int jbd2_journal_load(journal_t *journal)
1409 journal_superblock_t *sb;
1411 err = load_superblock(journal);
1415 sb = journal->j_superblock;
1416 /* If this is a V2 superblock, then we have to check the
1417 * features flags on it. */
1419 if (journal->j_format_version >= 2) {
1420 if ((sb->s_feature_ro_compat &
1421 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1422 (sb->s_feature_incompat &
1423 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1424 printk (KERN_WARNING
1425 "JBD: Unrecognised features on journal\n");
1430 /* Let the recovery code check whether it needs to recover any
1431 * data from the journal. */
1432 if (jbd2_journal_recover(journal))
1433 goto recovery_error;
1435 /* OK, we've finished with the dynamic journal bits:
1436 * reinitialise the dynamic contents of the superblock in memory
1437 * and reset them on disk. */
1438 if (journal_reset(journal))
1439 goto recovery_error;
1441 journal->j_flags &= ~JBD2_ABORT;
1442 journal->j_flags |= JBD2_LOADED;
1446 printk (KERN_WARNING "JBD: recovery failed\n");
1451 * void jbd2_journal_destroy() - Release a journal_t structure.
1452 * @journal: Journal to act on.
1454 * Release a journal_t structure once it is no longer in use by the
1456 * Return <0 if we couldn't clean up the journal.
1458 int jbd2_journal_destroy(journal_t *journal)
1462 /* Wait for the commit thread to wake up and die. */
1463 journal_kill_thread(journal);
1465 /* Force a final log commit */
1466 if (journal->j_running_transaction)
1467 jbd2_journal_commit_transaction(journal);
1469 /* Force any old transactions to disk */
1471 /* Totally anal locking here... */
1472 spin_lock(&journal->j_list_lock);
1473 while (journal->j_checkpoint_transactions != NULL) {
1474 spin_unlock(&journal->j_list_lock);
1475 jbd2_log_do_checkpoint(journal);
1476 spin_lock(&journal->j_list_lock);
1479 J_ASSERT(journal->j_running_transaction == NULL);
1480 J_ASSERT(journal->j_committing_transaction == NULL);
1481 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1482 spin_unlock(&journal->j_list_lock);
1484 if (journal->j_sb_buffer) {
1485 if (!is_journal_aborted(journal)) {
1486 /* We can now mark the journal as empty. */
1487 journal->j_tail = 0;
1488 journal->j_tail_sequence =
1489 ++journal->j_transaction_sequence;
1490 jbd2_journal_update_superblock(journal, 1);
1494 brelse(journal->j_sb_buffer);
1497 if (journal->j_proc_entry)
1498 jbd2_stats_proc_exit(journal);
1499 if (journal->j_inode)
1500 iput(journal->j_inode);
1501 if (journal->j_revoke)
1502 jbd2_journal_destroy_revoke(journal);
1503 kfree(journal->j_wbuf);
1511 *int jbd2_journal_check_used_features () - Check if features specified are used.
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 journal uses all of a given set of
1518 * features. Return true (non-zero) if it does.
1521 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1522 unsigned long ro, unsigned long incompat)
1524 journal_superblock_t *sb;
1526 if (!compat && !ro && !incompat)
1528 if (journal->j_format_version == 1)
1531 sb = journal->j_superblock;
1533 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1534 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1535 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1542 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1543 * @journal: Journal to check.
1544 * @compat: bitmask of compatible features
1545 * @ro: bitmask of features that force read-only mount
1546 * @incompat: bitmask of incompatible features
1548 * Check whether the journaling code supports the use of
1549 * all of a given set of features on this journal. Return true
1550 * (non-zero) if it can. */
1552 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1553 unsigned long ro, unsigned long incompat)
1555 journal_superblock_t *sb;
1557 if (!compat && !ro && !incompat)
1560 sb = journal->j_superblock;
1562 /* We can support any known requested features iff the
1563 * superblock is in version 2. Otherwise we fail to support any
1564 * extended sb features. */
1566 if (journal->j_format_version != 2)
1569 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1570 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1571 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1578 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1579 * @journal: Journal to act on.
1580 * @compat: bitmask of compatible features
1581 * @ro: bitmask of features that force read-only mount
1582 * @incompat: bitmask of incompatible features
1584 * Mark a given journal feature as present on the
1585 * superblock. Returns true if the requested features could be set.
1589 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1590 unsigned long ro, unsigned long incompat)
1592 journal_superblock_t *sb;
1594 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1597 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1600 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1601 compat, ro, incompat);
1603 sb = journal->j_superblock;
1605 sb->s_feature_compat |= cpu_to_be32(compat);
1606 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1607 sb->s_feature_incompat |= cpu_to_be32(incompat);
1613 * jbd2_journal_clear_features () - Clear a given journal feature in the
1615 * @journal: Journal to act on.
1616 * @compat: bitmask of compatible features
1617 * @ro: bitmask of features that force read-only mount
1618 * @incompat: bitmask of incompatible features
1620 * Clear a given journal feature as present on the
1623 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1624 unsigned long ro, unsigned long incompat)
1626 journal_superblock_t *sb;
1628 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1629 compat, ro, incompat);
1631 sb = journal->j_superblock;
1633 sb->s_feature_compat &= ~cpu_to_be32(compat);
1634 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1635 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1637 EXPORT_SYMBOL(jbd2_journal_clear_features);
1640 * int jbd2_journal_update_format () - Update on-disk journal structure.
1641 * @journal: Journal to act on.
1643 * Given an initialised but unloaded journal struct, poke about in the
1644 * on-disk structure to update it to the most recent supported version.
1646 int jbd2_journal_update_format (journal_t *journal)
1648 journal_superblock_t *sb;
1651 err = journal_get_superblock(journal);
1655 sb = journal->j_superblock;
1657 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1658 case JBD2_SUPERBLOCK_V2:
1660 case JBD2_SUPERBLOCK_V1:
1661 return journal_convert_superblock_v1(journal, sb);
1668 static int journal_convert_superblock_v1(journal_t *journal,
1669 journal_superblock_t *sb)
1671 int offset, blocksize;
1672 struct buffer_head *bh;
1675 "JBD: Converting superblock from version 1 to 2.\n");
1677 /* Pre-initialise new fields to zero */
1678 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1679 blocksize = be32_to_cpu(sb->s_blocksize);
1680 memset(&sb->s_feature_compat, 0, blocksize-offset);
1682 sb->s_nr_users = cpu_to_be32(1);
1683 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1684 journal->j_format_version = 2;
1686 bh = journal->j_sb_buffer;
1687 BUFFER_TRACE(bh, "marking dirty");
1688 mark_buffer_dirty(bh);
1689 sync_dirty_buffer(bh);
1695 * int jbd2_journal_flush () - Flush journal
1696 * @journal: Journal to act on.
1698 * Flush all data for a given journal to disk and empty the journal.
1699 * Filesystems can use this when remounting readonly to ensure that
1700 * recovery does not need to happen on remount.
1703 int jbd2_journal_flush(journal_t *journal)
1706 transaction_t *transaction = NULL;
1707 unsigned long old_tail;
1709 spin_lock(&journal->j_state_lock);
1711 /* Force everything buffered to the log... */
1712 if (journal->j_running_transaction) {
1713 transaction = journal->j_running_transaction;
1714 __jbd2_log_start_commit(journal, transaction->t_tid);
1715 } else if (journal->j_committing_transaction)
1716 transaction = journal->j_committing_transaction;
1718 /* Wait for the log commit to complete... */
1720 tid_t tid = transaction->t_tid;
1722 spin_unlock(&journal->j_state_lock);
1723 jbd2_log_wait_commit(journal, tid);
1725 spin_unlock(&journal->j_state_lock);
1728 /* ...and flush everything in the log out to disk. */
1729 spin_lock(&journal->j_list_lock);
1730 while (!err && journal->j_checkpoint_transactions != NULL) {
1731 spin_unlock(&journal->j_list_lock);
1732 mutex_lock(&journal->j_checkpoint_mutex);
1733 err = jbd2_log_do_checkpoint(journal);
1734 mutex_unlock(&journal->j_checkpoint_mutex);
1735 spin_lock(&journal->j_list_lock);
1737 spin_unlock(&journal->j_list_lock);
1739 if (is_journal_aborted(journal))
1742 jbd2_cleanup_journal_tail(journal);
1744 /* Finally, mark the journal as really needing no recovery.
1745 * This sets s_start==0 in the underlying superblock, which is
1746 * the magic code for a fully-recovered superblock. Any future
1747 * commits of data to the journal will restore the current
1749 spin_lock(&journal->j_state_lock);
1750 old_tail = journal->j_tail;
1751 journal->j_tail = 0;
1752 spin_unlock(&journal->j_state_lock);
1753 jbd2_journal_update_superblock(journal, 1);
1754 spin_lock(&journal->j_state_lock);
1755 journal->j_tail = old_tail;
1757 J_ASSERT(!journal->j_running_transaction);
1758 J_ASSERT(!journal->j_committing_transaction);
1759 J_ASSERT(!journal->j_checkpoint_transactions);
1760 J_ASSERT(journal->j_head == journal->j_tail);
1761 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1762 spin_unlock(&journal->j_state_lock);
1767 * int jbd2_journal_wipe() - Wipe journal contents
1768 * @journal: Journal to act on.
1769 * @write: flag (see below)
1771 * Wipe out all of the contents of a journal, safely. This will produce
1772 * a warning if the journal contains any valid recovery information.
1773 * Must be called between journal_init_*() and jbd2_journal_load().
1775 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1776 * we merely suppress recovery.
1779 int jbd2_journal_wipe(journal_t *journal, int write)
1781 journal_superblock_t *sb;
1784 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1786 err = load_superblock(journal);
1790 sb = journal->j_superblock;
1792 if (!journal->j_tail)
1795 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1796 write ? "Clearing" : "Ignoring");
1798 err = jbd2_journal_skip_recovery(journal);
1800 jbd2_journal_update_superblock(journal, 1);
1807 * Journal abort has very specific semantics, which we describe
1808 * for journal abort.
1810 * Two internal function, which provide abort to te jbd layer
1815 * Quick version for internal journal use (doesn't lock the journal).
1816 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1817 * and don't attempt to make any other journal updates.
1819 void __jbd2_journal_abort_hard(journal_t *journal)
1821 transaction_t *transaction;
1823 if (journal->j_flags & JBD2_ABORT)
1826 printk(KERN_ERR "Aborting journal on device %s.\n",
1827 journal->j_devname);
1829 spin_lock(&journal->j_state_lock);
1830 journal->j_flags |= JBD2_ABORT;
1831 transaction = journal->j_running_transaction;
1833 __jbd2_log_start_commit(journal, transaction->t_tid);
1834 spin_unlock(&journal->j_state_lock);
1837 /* Soft abort: record the abort error status in the journal superblock,
1838 * but don't do any other IO. */
1839 static void __journal_abort_soft (journal_t *journal, int errno)
1841 if (journal->j_flags & JBD2_ABORT)
1844 if (!journal->j_errno)
1845 journal->j_errno = errno;
1847 __jbd2_journal_abort_hard(journal);
1850 jbd2_journal_update_superblock(journal, 1);
1854 * void jbd2_journal_abort () - Shutdown the journal immediately.
1855 * @journal: the journal to shutdown.
1856 * @errno: an error number to record in the journal indicating
1857 * the reason for the shutdown.
1859 * Perform a complete, immediate shutdown of the ENTIRE
1860 * journal (not of a single transaction). This operation cannot be
1861 * undone without closing and reopening the journal.
1863 * The jbd2_journal_abort function is intended to support higher level error
1864 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1867 * Journal abort has very specific semantics. Any existing dirty,
1868 * unjournaled buffers in the main filesystem will still be written to
1869 * disk by bdflush, but the journaling mechanism will be suspended
1870 * immediately and no further transaction commits will be honoured.
1872 * Any dirty, journaled buffers will be written back to disk without
1873 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1874 * filesystem, but we _do_ attempt to leave as much data as possible
1875 * behind for fsck to use for cleanup.
1877 * Any attempt to get a new transaction handle on a journal which is in
1878 * ABORT state will just result in an -EROFS error return. A
1879 * jbd2_journal_stop on an existing handle will return -EIO if we have
1880 * entered abort state during the update.
1882 * Recursive transactions are not disturbed by journal abort until the
1883 * final jbd2_journal_stop, which will receive the -EIO error.
1885 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
1886 * which will be recorded (if possible) in the journal superblock. This
1887 * allows a client to record failure conditions in the middle of a
1888 * transaction without having to complete the transaction to record the
1889 * failure to disk. ext3_error, for example, now uses this
1892 * Errors which originate from within the journaling layer will NOT
1893 * supply an errno; a null errno implies that absolutely no further
1894 * writes are done to the journal (unless there are any already in
1899 void jbd2_journal_abort(journal_t *journal, int errno)
1901 __journal_abort_soft(journal, errno);
1905 * int jbd2_journal_errno () - returns the journal's error state.
1906 * @journal: journal to examine.
1908 * This is the errno numbet set with jbd2_journal_abort(), the last
1909 * time the journal was mounted - if the journal was stopped
1910 * without calling abort this will be 0.
1912 * If the journal has been aborted on this mount time -EROFS will
1915 int jbd2_journal_errno(journal_t *journal)
1919 spin_lock(&journal->j_state_lock);
1920 if (journal->j_flags & JBD2_ABORT)
1923 err = journal->j_errno;
1924 spin_unlock(&journal->j_state_lock);
1929 * int jbd2_journal_clear_err () - clears the journal's error state
1930 * @journal: journal to act on.
1932 * An error must be cleared or Acked to take a FS out of readonly
1935 int jbd2_journal_clear_err(journal_t *journal)
1939 spin_lock(&journal->j_state_lock);
1940 if (journal->j_flags & JBD2_ABORT)
1943 journal->j_errno = 0;
1944 spin_unlock(&journal->j_state_lock);
1949 * void jbd2_journal_ack_err() - Ack journal err.
1950 * @journal: journal to act on.
1952 * An error must be cleared or Acked to take a FS out of readonly
1955 void jbd2_journal_ack_err(journal_t *journal)
1957 spin_lock(&journal->j_state_lock);
1958 if (journal->j_errno)
1959 journal->j_flags |= JBD2_ACK_ERR;
1960 spin_unlock(&journal->j_state_lock);
1963 int jbd2_journal_blocks_per_page(struct inode *inode)
1965 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1969 * helper functions to deal with 32 or 64bit block numbers.
1971 size_t journal_tag_bytes(journal_t *journal)
1973 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1974 return JBD2_TAG_SIZE64;
1976 return JBD2_TAG_SIZE32;
1980 * Journal_head storage management
1982 static struct kmem_cache *jbd2_journal_head_cache;
1983 #ifdef CONFIG_JBD2_DEBUG
1984 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1987 static int journal_init_jbd2_journal_head_cache(void)
1991 J_ASSERT(jbd2_journal_head_cache == NULL);
1992 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
1993 sizeof(struct journal_head),
1995 SLAB_TEMPORARY, /* flags */
1998 if (!jbd2_journal_head_cache) {
2000 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
2005 static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
2007 if (jbd2_journal_head_cache) {
2008 kmem_cache_destroy(jbd2_journal_head_cache);
2009 jbd2_journal_head_cache = NULL;
2014 * journal_head splicing and dicing
2016 static struct journal_head *journal_alloc_journal_head(void)
2018 struct journal_head *ret;
2019 static unsigned long last_warning;
2021 #ifdef CONFIG_JBD2_DEBUG
2022 atomic_inc(&nr_journal_heads);
2024 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2026 jbd_debug(1, "out of memory for journal_head\n");
2027 if (time_after(jiffies, last_warning + 5*HZ)) {
2028 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
2030 last_warning = jiffies;
2034 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2040 static void journal_free_journal_head(struct journal_head *jh)
2042 #ifdef CONFIG_JBD2_DEBUG
2043 atomic_dec(&nr_journal_heads);
2044 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2046 kmem_cache_free(jbd2_journal_head_cache, jh);
2050 * A journal_head is attached to a buffer_head whenever JBD has an
2051 * interest in the buffer.
2053 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2054 * is set. This bit is tested in core kernel code where we need to take
2055 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2058 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2060 * When a buffer has its BH_JBD bit set it is immune from being released by
2061 * core kernel code, mainly via ->b_count.
2063 * A journal_head may be detached from its buffer_head when the journal_head's
2064 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
2065 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
2066 * journal_head can be dropped if needed.
2068 * Various places in the kernel want to attach a journal_head to a buffer_head
2069 * _before_ attaching the journal_head to a transaction. To protect the
2070 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2071 * journal_head's b_jcount refcount by one. The caller must call
2072 * jbd2_journal_put_journal_head() to undo this.
2074 * So the typical usage would be:
2076 * (Attach a journal_head if needed. Increments b_jcount)
2077 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2079 * jh->b_transaction = xxx;
2080 * jbd2_journal_put_journal_head(jh);
2082 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2083 * because it has a non-zero b_transaction.
2087 * Give a buffer_head a journal_head.
2089 * Doesn't need the journal lock.
2092 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2094 struct journal_head *jh;
2095 struct journal_head *new_jh = NULL;
2098 if (!buffer_jbd(bh)) {
2099 new_jh = journal_alloc_journal_head();
2100 memset(new_jh, 0, sizeof(*new_jh));
2103 jbd_lock_bh_journal_head(bh);
2104 if (buffer_jbd(bh)) {
2108 (atomic_read(&bh->b_count) > 0) ||
2109 (bh->b_page && bh->b_page->mapping));
2112 jbd_unlock_bh_journal_head(bh);
2117 new_jh = NULL; /* We consumed it */
2122 BUFFER_TRACE(bh, "added journal_head");
2125 jbd_unlock_bh_journal_head(bh);
2127 journal_free_journal_head(new_jh);
2128 return bh->b_private;
2132 * Grab a ref against this buffer_head's journal_head. If it ended up not
2133 * having a journal_head, return NULL
2135 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2137 struct journal_head *jh = NULL;
2139 jbd_lock_bh_journal_head(bh);
2140 if (buffer_jbd(bh)) {
2144 jbd_unlock_bh_journal_head(bh);
2148 static void __journal_remove_journal_head(struct buffer_head *bh)
2150 struct journal_head *jh = bh2jh(bh);
2152 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2155 if (jh->b_jcount == 0) {
2156 if (jh->b_transaction == NULL &&
2157 jh->b_next_transaction == NULL &&
2158 jh->b_cp_transaction == NULL) {
2159 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2160 J_ASSERT_BH(bh, buffer_jbd(bh));
2161 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2162 BUFFER_TRACE(bh, "remove journal_head");
2163 if (jh->b_frozen_data) {
2164 printk(KERN_WARNING "%s: freeing "
2167 jbd2_free(jh->b_frozen_data, bh->b_size);
2169 if (jh->b_committed_data) {
2170 printk(KERN_WARNING "%s: freeing "
2171 "b_committed_data\n",
2173 jbd2_free(jh->b_committed_data, bh->b_size);
2175 bh->b_private = NULL;
2176 jh->b_bh = NULL; /* debug, really */
2177 clear_buffer_jbd(bh);
2179 journal_free_journal_head(jh);
2181 BUFFER_TRACE(bh, "journal_head was locked");
2187 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
2188 * and has a zero b_jcount then remove and release its journal_head. If we did
2189 * see that the buffer is not used by any transaction we also "logically"
2190 * decrement ->b_count.
2192 * We in fact take an additional increment on ->b_count as a convenience,
2193 * because the caller usually wants to do additional things with the bh
2194 * after calling here.
2195 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
2196 * time. Once the caller has run __brelse(), the buffer is eligible for
2197 * reaping by try_to_free_buffers().
2199 void jbd2_journal_remove_journal_head(struct buffer_head *bh)
2201 jbd_lock_bh_journal_head(bh);
2202 __journal_remove_journal_head(bh);
2203 jbd_unlock_bh_journal_head(bh);
2207 * Drop a reference on the passed journal_head. If it fell to zero then try to
2208 * release the journal_head from the buffer_head.
2210 void jbd2_journal_put_journal_head(struct journal_head *jh)
2212 struct buffer_head *bh = jh2bh(jh);
2214 jbd_lock_bh_journal_head(bh);
2215 J_ASSERT_JH(jh, jh->b_jcount > 0);
2217 if (!jh->b_jcount && !jh->b_transaction) {
2218 __journal_remove_journal_head(bh);
2221 jbd_unlock_bh_journal_head(bh);
2225 * Initialize jbd inode head
2227 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2229 jinode->i_transaction = NULL;
2230 jinode->i_next_transaction = NULL;
2231 jinode->i_vfs_inode = inode;
2232 jinode->i_flags = 0;
2233 INIT_LIST_HEAD(&jinode->i_list);
2237 * Function to be called before we start removing inode from memory (i.e.,
2238 * clear_inode() is a fine place to be called from). It removes inode from
2239 * transaction's lists.
2241 void jbd2_journal_release_jbd_inode(journal_t *journal,
2242 struct jbd2_inode *jinode)
2249 spin_lock(&journal->j_list_lock);
2250 /* Is commit writing out inode - we have to wait */
2251 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2252 wait_queue_head_t *wq;
2253 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2254 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2255 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2256 spin_unlock(&journal->j_list_lock);
2258 finish_wait(wq, &wait.wait);
2262 /* Do we need to wait for data writeback? */
2263 if (journal->j_committing_transaction == jinode->i_transaction)
2265 if (jinode->i_transaction) {
2266 list_del(&jinode->i_list);
2267 jinode->i_transaction = NULL;
2269 spin_unlock(&journal->j_list_lock);
2275 #ifdef CONFIG_JBD2_DEBUG
2276 u8 jbd2_journal_enable_debug __read_mostly;
2277 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2279 #define JBD2_DEBUG_NAME "jbd2-debug"
2281 static struct dentry *jbd2_debugfs_dir;
2282 static struct dentry *jbd2_debug;
2284 static void __init jbd2_create_debugfs_entry(void)
2286 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2287 if (jbd2_debugfs_dir)
2288 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2290 &jbd2_journal_enable_debug);
2293 static void __exit jbd2_remove_debugfs_entry(void)
2295 debugfs_remove(jbd2_debug);
2296 debugfs_remove(jbd2_debugfs_dir);
2301 static void __init jbd2_create_debugfs_entry(void)
2305 static void __exit jbd2_remove_debugfs_entry(void)
2311 #ifdef CONFIG_PROC_FS
2313 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2315 static void __init jbd2_create_jbd_stats_proc_entry(void)
2317 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2320 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2322 if (proc_jbd2_stats)
2323 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2328 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2329 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2333 struct kmem_cache *jbd2_handle_cache;
2335 static int __init journal_init_handle_cache(void)
2337 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
2340 SLAB_TEMPORARY, /* flags */
2342 if (jbd2_handle_cache == NULL) {
2343 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2349 static void jbd2_journal_destroy_handle_cache(void)
2351 if (jbd2_handle_cache)
2352 kmem_cache_destroy(jbd2_handle_cache);
2356 * Module startup and shutdown
2359 static int __init journal_init_caches(void)
2363 ret = jbd2_journal_init_revoke_caches();
2365 ret = journal_init_jbd2_journal_head_cache();
2367 ret = journal_init_handle_cache();
2371 static void jbd2_journal_destroy_caches(void)
2373 jbd2_journal_destroy_revoke_caches();
2374 jbd2_journal_destroy_jbd2_journal_head_cache();
2375 jbd2_journal_destroy_handle_cache();
2378 static int __init journal_init(void)
2382 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2384 ret = journal_init_caches();
2386 jbd2_create_debugfs_entry();
2387 jbd2_create_jbd_stats_proc_entry();
2389 jbd2_journal_destroy_caches();
2394 static void __exit journal_exit(void)
2396 #ifdef CONFIG_JBD2_DEBUG
2397 int n = atomic_read(&nr_journal_heads);
2399 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2401 jbd2_remove_debugfs_entry();
2402 jbd2_remove_jbd_stats_proc_entry();
2403 jbd2_journal_destroy_caches();
2406 MODULE_LICENSE("GPL");
2407 module_init(journal_init);
2408 module_exit(journal_exit);