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>
43 #include <asm/div64.h>
45 EXPORT_SYMBOL(jbd2_journal_start);
46 EXPORT_SYMBOL(jbd2_journal_restart);
47 EXPORT_SYMBOL(jbd2_journal_extend);
48 EXPORT_SYMBOL(jbd2_journal_stop);
49 EXPORT_SYMBOL(jbd2_journal_lock_updates);
50 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
51 EXPORT_SYMBOL(jbd2_journal_get_write_access);
52 EXPORT_SYMBOL(jbd2_journal_get_create_access);
53 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
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_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: pid %d, dev %s, "
135 "commit interval %ld seconds\n", current->pid,
136 journal->j_devname, journal->j_commit_interval / HZ);
139 * And now, wait forever for commit wakeup events.
141 spin_lock(&journal->j_state_lock);
144 if (journal->j_flags & JBD2_UNMOUNT)
147 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
148 journal->j_commit_sequence, journal->j_commit_request);
150 if (journal->j_commit_sequence != journal->j_commit_request) {
151 jbd_debug(1, "OK, requests differ\n");
152 spin_unlock(&journal->j_state_lock);
153 del_timer_sync(&journal->j_commit_timer);
154 jbd2_journal_commit_transaction(journal);
155 spin_lock(&journal->j_state_lock);
159 wake_up(&journal->j_wait_done_commit);
160 if (freezing(current)) {
162 * The simpler the better. Flushing journal isn't a
163 * good idea, because that depends on threads that may
164 * be already stopped.
166 jbd_debug(1, "Now suspending kjournald2\n");
167 spin_unlock(&journal->j_state_lock);
169 spin_lock(&journal->j_state_lock);
172 * We assume on resume that commits are already there,
176 int should_sleep = 1;
178 prepare_to_wait(&journal->j_wait_commit, &wait,
180 if (journal->j_commit_sequence != journal->j_commit_request)
182 transaction = journal->j_running_transaction;
183 if (transaction && time_after_eq(jiffies,
184 transaction->t_expires))
186 if (journal->j_flags & JBD2_UNMOUNT)
189 spin_unlock(&journal->j_state_lock);
191 spin_lock(&journal->j_state_lock);
193 finish_wait(&journal->j_wait_commit, &wait);
196 jbd_debug(1, "kjournald2 wakes\n");
199 * Were we woken up by a commit wakeup event?
201 transaction = journal->j_running_transaction;
202 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
203 journal->j_commit_request = transaction->t_tid;
204 jbd_debug(1, "woke because of timeout\n");
209 spin_unlock(&journal->j_state_lock);
210 del_timer_sync(&journal->j_commit_timer);
211 journal->j_task = NULL;
212 wake_up(&journal->j_wait_done_commit);
213 jbd_debug(1, "Journal thread exiting.\n");
217 static int jbd2_journal_start_thread(journal_t *journal)
219 struct task_struct *t;
221 t = kthread_run(kjournald2, journal, "kjournald2");
225 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
229 static void journal_kill_thread(journal_t *journal)
231 spin_lock(&journal->j_state_lock);
232 journal->j_flags |= JBD2_UNMOUNT;
234 while (journal->j_task) {
235 wake_up(&journal->j_wait_commit);
236 spin_unlock(&journal->j_state_lock);
237 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
238 spin_lock(&journal->j_state_lock);
240 spin_unlock(&journal->j_state_lock);
244 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
246 * Writes a metadata buffer to a given disk block. The actual IO is not
247 * performed but a new buffer_head is constructed which labels the data
248 * to be written with the correct destination disk block.
250 * Any magic-number escaping which needs to be done will cause a
251 * copy-out here. If the buffer happens to start with the
252 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
253 * magic number is only written to the log for descripter blocks. In
254 * this case, we copy the data and replace the first word with 0, and we
255 * return a result code which indicates that this buffer needs to be
256 * marked as an escaped buffer in the corresponding log descriptor
257 * block. The missing word can then be restored when the block is read
260 * If the source buffer has already been modified by a new transaction
261 * since we took the last commit snapshot, we use the frozen copy of
262 * that data for IO. If we end up using the existing buffer_head's data
263 * for the write, then we *have* to lock the buffer to prevent anyone
264 * else from using and possibly modifying it while the IO is in
267 * The function returns a pointer to the buffer_heads to be used for IO.
269 * We assume that the journal has already been locked in this function.
276 * Bit 0 set == escape performed on the data
277 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
280 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
281 struct journal_head *jh_in,
282 struct journal_head **jh_out,
283 unsigned long long blocknr)
285 int need_copy_out = 0;
286 int done_copy_out = 0;
289 struct buffer_head *new_bh;
290 struct journal_head *new_jh;
291 struct page *new_page;
292 unsigned int new_offset;
293 struct buffer_head *bh_in = jh2bh(jh_in);
296 * The buffer really shouldn't be locked: only the current committing
297 * transaction is allowed to write it, so nobody else is allowed
300 * akpm: except if we're journalling data, and write() output is
301 * also part of a shared mapping, and another thread has
302 * decided to launch a writepage() against this buffer.
304 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
306 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
309 * If a new transaction has already done a buffer copy-out, then
310 * we use that version of the data for the commit.
312 jbd_lock_bh_state(bh_in);
314 if (jh_in->b_frozen_data) {
316 new_page = virt_to_page(jh_in->b_frozen_data);
317 new_offset = offset_in_page(jh_in->b_frozen_data);
319 new_page = jh2bh(jh_in)->b_page;
320 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
323 mapped_data = kmap_atomic(new_page, KM_USER0);
327 if (*((__be32 *)(mapped_data + new_offset)) ==
328 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
332 kunmap_atomic(mapped_data, KM_USER0);
335 * Do we need to do a data copy?
337 if (need_copy_out && !done_copy_out) {
340 jbd_unlock_bh_state(bh_in);
341 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
342 jbd_lock_bh_state(bh_in);
343 if (jh_in->b_frozen_data) {
344 jbd2_free(tmp, bh_in->b_size);
348 jh_in->b_frozen_data = tmp;
349 mapped_data = kmap_atomic(new_page, KM_USER0);
350 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
351 kunmap_atomic(mapped_data, KM_USER0);
353 new_page = virt_to_page(tmp);
354 new_offset = offset_in_page(tmp);
359 * Did we need to do an escaping? Now we've done all the
360 * copying, we can finally do so.
363 mapped_data = kmap_atomic(new_page, KM_USER0);
364 *((unsigned int *)(mapped_data + new_offset)) = 0;
365 kunmap_atomic(mapped_data, KM_USER0);
368 /* keep subsequent assertions sane */
370 init_buffer(new_bh, NULL, NULL);
371 atomic_set(&new_bh->b_count, 1);
372 jbd_unlock_bh_state(bh_in);
374 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
376 set_bh_page(new_bh, new_page, new_offset);
377 new_jh->b_transaction = NULL;
378 new_bh->b_size = jh2bh(jh_in)->b_size;
379 new_bh->b_bdev = transaction->t_journal->j_dev;
380 new_bh->b_blocknr = blocknr;
381 set_buffer_mapped(new_bh);
382 set_buffer_dirty(new_bh);
387 * The to-be-written buffer needs to get moved to the io queue,
388 * and the original buffer whose contents we are shadowing or
389 * copying is moved to the transaction's shadow queue.
391 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
392 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
393 JBUFFER_TRACE(new_jh, "file as BJ_IO");
394 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
396 return do_escape | (done_copy_out << 1);
400 * Allocation code for the journal file. Manage the space left in the
401 * journal, so that we can begin checkpointing when appropriate.
405 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
407 * Called with the journal already locked.
409 * Called under j_state_lock
412 int __jbd2_log_space_left(journal_t *journal)
414 int left = journal->j_free;
416 assert_spin_locked(&journal->j_state_lock);
419 * Be pessimistic here about the number of those free blocks which
420 * might be required for log descriptor control blocks.
423 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
425 left -= MIN_LOG_RESERVED_BLOCKS;
434 * Called under j_state_lock. Returns true if a transaction was started.
436 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
439 * Are we already doing a recent enough commit?
441 if (!tid_geq(journal->j_commit_request, target)) {
443 * We want a new commit: OK, mark the request and wakup the
444 * commit thread. We do _not_ do the commit ourselves.
447 journal->j_commit_request = target;
448 jbd_debug(1, "JBD: requesting commit %d/%d\n",
449 journal->j_commit_request,
450 journal->j_commit_sequence);
451 wake_up(&journal->j_wait_commit);
457 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
461 spin_lock(&journal->j_state_lock);
462 ret = __jbd2_log_start_commit(journal, tid);
463 spin_unlock(&journal->j_state_lock);
468 * Force and wait upon a commit if the calling process is not within
469 * transaction. This is used for forcing out undo-protected data which contains
470 * bitmaps, when the fs is running out of space.
472 * We can only force the running transaction if we don't have an active handle;
473 * otherwise, we will deadlock.
475 * Returns true if a transaction was started.
477 int jbd2_journal_force_commit_nested(journal_t *journal)
479 transaction_t *transaction = NULL;
482 spin_lock(&journal->j_state_lock);
483 if (journal->j_running_transaction && !current->journal_info) {
484 transaction = journal->j_running_transaction;
485 __jbd2_log_start_commit(journal, transaction->t_tid);
486 } else if (journal->j_committing_transaction)
487 transaction = journal->j_committing_transaction;
490 spin_unlock(&journal->j_state_lock);
491 return 0; /* Nothing to retry */
494 tid = transaction->t_tid;
495 spin_unlock(&journal->j_state_lock);
496 jbd2_log_wait_commit(journal, tid);
501 * Start a commit of the current running transaction (if any). Returns true
502 * if a transaction was started, and fills its tid in at *ptid
504 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
508 spin_lock(&journal->j_state_lock);
509 if (journal->j_running_transaction) {
510 tid_t tid = journal->j_running_transaction->t_tid;
512 ret = __jbd2_log_start_commit(journal, tid);
515 } else if (journal->j_committing_transaction && ptid) {
517 * If ext3_write_super() recently started a commit, then we
518 * have to wait for completion of that transaction
520 *ptid = journal->j_committing_transaction->t_tid;
523 spin_unlock(&journal->j_state_lock);
528 * Wait for a specified commit to complete.
529 * The caller may not hold the journal lock.
531 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
535 #ifdef CONFIG_JBD2_DEBUG
536 spin_lock(&journal->j_state_lock);
537 if (!tid_geq(journal->j_commit_request, tid)) {
539 "%s: error: j_commit_request=%d, tid=%d\n",
540 __func__, journal->j_commit_request, tid);
542 spin_unlock(&journal->j_state_lock);
544 spin_lock(&journal->j_state_lock);
545 while (tid_gt(tid, journal->j_commit_sequence)) {
546 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
547 tid, journal->j_commit_sequence);
548 wake_up(&journal->j_wait_commit);
549 spin_unlock(&journal->j_state_lock);
550 wait_event(journal->j_wait_done_commit,
551 !tid_gt(tid, journal->j_commit_sequence));
552 spin_lock(&journal->j_state_lock);
554 spin_unlock(&journal->j_state_lock);
556 if (unlikely(is_journal_aborted(journal))) {
557 printk(KERN_EMERG "journal commit I/O error\n");
564 * Log buffer allocation routines:
567 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
569 unsigned long blocknr;
571 spin_lock(&journal->j_state_lock);
572 J_ASSERT(journal->j_free > 1);
574 blocknr = journal->j_head;
577 if (journal->j_head == journal->j_last)
578 journal->j_head = journal->j_first;
579 spin_unlock(&journal->j_state_lock);
580 return jbd2_journal_bmap(journal, blocknr, retp);
584 * Conversion of logical to physical block numbers for the journal
586 * On external journals the journal blocks are identity-mapped, so
587 * this is a no-op. If needed, we can use j_blk_offset - everything is
590 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
591 unsigned long long *retp)
594 unsigned long long ret;
596 if (journal->j_inode) {
597 ret = bmap(journal->j_inode, blocknr);
601 printk(KERN_ALERT "%s: journal block not found "
602 "at offset %lu on %s\n",
603 __func__, blocknr, journal->j_devname);
605 __journal_abort_soft(journal, err);
608 *retp = blocknr; /* +journal->j_blk_offset */
614 * We play buffer_head aliasing tricks to write data/metadata blocks to
615 * the journal without copying their contents, but for journal
616 * descriptor blocks we do need to generate bona fide buffers.
618 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
619 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
620 * But we don't bother doing that, so there will be coherency problems with
621 * mmaps of blockdevs which hold live JBD-controlled filesystems.
623 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
625 struct buffer_head *bh;
626 unsigned long long blocknr;
629 err = jbd2_journal_next_log_block(journal, &blocknr);
634 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
636 memset(bh->b_data, 0, journal->j_blocksize);
637 set_buffer_uptodate(bh);
639 BUFFER_TRACE(bh, "return this buffer");
640 return jbd2_journal_add_journal_head(bh);
643 struct jbd2_stats_proc_session {
645 struct transaction_stats_s *stats;
650 static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
651 struct transaction_stats_s *ts,
654 if (ts == s->stats + s->max)
656 if (!first && ts == s->stats + s->start)
658 while (ts->ts_type == 0) {
660 if (ts == s->stats + s->max)
662 if (ts == s->stats + s->start)
669 static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
671 struct jbd2_stats_proc_session *s = seq->private;
672 struct transaction_stats_s *ts;
676 return SEQ_START_TOKEN;
677 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
682 ts = jbd2_history_skip_empty(s, ++ts, 0);
690 static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
692 struct jbd2_stats_proc_session *s = seq->private;
693 struct transaction_stats_s *ts = v;
696 if (v == SEQ_START_TOKEN)
697 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
699 return jbd2_history_skip_empty(s, ++ts, 0);
702 static int jbd2_seq_history_show(struct seq_file *seq, void *v)
704 struct transaction_stats_s *ts = v;
705 if (v == SEQ_START_TOKEN) {
706 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
707 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
708 "wait", "run", "lock", "flush", "log", "hndls",
709 "block", "inlog", "ctime", "write", "drop",
713 if (ts->ts_type == JBD2_STATS_RUN)
714 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
715 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
716 jiffies_to_msecs(ts->u.run.rs_wait),
717 jiffies_to_msecs(ts->u.run.rs_running),
718 jiffies_to_msecs(ts->u.run.rs_locked),
719 jiffies_to_msecs(ts->u.run.rs_flushing),
720 jiffies_to_msecs(ts->u.run.rs_logging),
721 ts->u.run.rs_handle_count,
723 ts->u.run.rs_blocks_logged);
724 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
725 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
726 "C", ts->ts_tid, " ",
727 jiffies_to_msecs(ts->u.chp.cs_chp_time),
728 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
729 ts->u.chp.cs_forced_to_close);
735 static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
739 static struct seq_operations jbd2_seq_history_ops = {
740 .start = jbd2_seq_history_start,
741 .next = jbd2_seq_history_next,
742 .stop = jbd2_seq_history_stop,
743 .show = jbd2_seq_history_show,
746 static int jbd2_seq_history_open(struct inode *inode, struct file *file)
748 journal_t *journal = PDE(inode)->data;
749 struct jbd2_stats_proc_session *s;
752 s = kmalloc(sizeof(*s), GFP_KERNEL);
755 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
756 s->stats = kmalloc(size, GFP_KERNEL);
757 if (s->stats == NULL) {
761 spin_lock(&journal->j_history_lock);
762 memcpy(s->stats, journal->j_history, size);
763 s->max = journal->j_history_max;
764 s->start = journal->j_history_cur % s->max;
765 spin_unlock(&journal->j_history_lock);
767 rc = seq_open(file, &jbd2_seq_history_ops);
769 struct seq_file *m = file->private_data;
779 static int jbd2_seq_history_release(struct inode *inode, struct file *file)
781 struct seq_file *seq = file->private_data;
782 struct jbd2_stats_proc_session *s = seq->private;
786 return seq_release(inode, file);
789 static struct file_operations jbd2_seq_history_fops = {
790 .owner = THIS_MODULE,
791 .open = jbd2_seq_history_open,
794 .release = jbd2_seq_history_release,
797 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
799 return *pos ? NULL : SEQ_START_TOKEN;
802 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
807 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
809 struct jbd2_stats_proc_session *s = seq->private;
811 if (v != SEQ_START_TOKEN)
813 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
815 s->journal->j_max_transaction_buffers);
816 if (s->stats->ts_tid == 0)
818 seq_printf(seq, "average: \n %ums waiting for transaction\n",
819 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
820 seq_printf(seq, " %ums running transaction\n",
821 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
822 seq_printf(seq, " %ums transaction was being locked\n",
823 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
824 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
825 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
826 seq_printf(seq, " %ums logging transaction\n",
827 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
828 seq_printf(seq, " %luus average transaction commit time\n",
829 do_div(s->journal->j_average_commit_time, 1000));
830 seq_printf(seq, " %lu handles per transaction\n",
831 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
832 seq_printf(seq, " %lu blocks per transaction\n",
833 s->stats->u.run.rs_blocks / s->stats->ts_tid);
834 seq_printf(seq, " %lu logged blocks per transaction\n",
835 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
839 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
843 static struct seq_operations jbd2_seq_info_ops = {
844 .start = jbd2_seq_info_start,
845 .next = jbd2_seq_info_next,
846 .stop = jbd2_seq_info_stop,
847 .show = jbd2_seq_info_show,
850 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
852 journal_t *journal = PDE(inode)->data;
853 struct jbd2_stats_proc_session *s;
856 s = kmalloc(sizeof(*s), GFP_KERNEL);
859 size = sizeof(struct transaction_stats_s);
860 s->stats = kmalloc(size, GFP_KERNEL);
861 if (s->stats == NULL) {
865 spin_lock(&journal->j_history_lock);
866 memcpy(s->stats, &journal->j_stats, size);
867 s->journal = journal;
868 spin_unlock(&journal->j_history_lock);
870 rc = seq_open(file, &jbd2_seq_info_ops);
872 struct seq_file *m = file->private_data;
882 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
884 struct seq_file *seq = file->private_data;
885 struct jbd2_stats_proc_session *s = seq->private;
888 return seq_release(inode, file);
891 static struct file_operations jbd2_seq_info_fops = {
892 .owner = THIS_MODULE,
893 .open = jbd2_seq_info_open,
896 .release = jbd2_seq_info_release,
899 static struct proc_dir_entry *proc_jbd2_stats;
901 static void jbd2_stats_proc_init(journal_t *journal)
903 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
904 if (journal->j_proc_entry) {
905 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
906 &jbd2_seq_history_fops, journal);
907 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
908 &jbd2_seq_info_fops, journal);
912 static void jbd2_stats_proc_exit(journal_t *journal)
914 remove_proc_entry("info", journal->j_proc_entry);
915 remove_proc_entry("history", journal->j_proc_entry);
916 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
919 static void journal_init_stats(journal_t *journal)
923 if (!proc_jbd2_stats)
926 journal->j_history_max = 100;
927 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
928 journal->j_history = kzalloc(size, GFP_KERNEL);
929 if (!journal->j_history) {
930 journal->j_history_max = 0;
933 spin_lock_init(&journal->j_history_lock);
937 * Management for journal control blocks: functions to create and
938 * destroy journal_t structures, and to initialise and read existing
939 * journal blocks from disk. */
941 /* First: create and setup a journal_t object in memory. We initialise
942 * very few fields yet: that has to wait until we have created the
943 * journal structures from from scratch, or loaded them from disk. */
945 static journal_t * journal_init_common (void)
950 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
954 init_waitqueue_head(&journal->j_wait_transaction_locked);
955 init_waitqueue_head(&journal->j_wait_logspace);
956 init_waitqueue_head(&journal->j_wait_done_commit);
957 init_waitqueue_head(&journal->j_wait_checkpoint);
958 init_waitqueue_head(&journal->j_wait_commit);
959 init_waitqueue_head(&journal->j_wait_updates);
960 mutex_init(&journal->j_barrier);
961 mutex_init(&journal->j_checkpoint_mutex);
962 spin_lock_init(&journal->j_revoke_lock);
963 spin_lock_init(&journal->j_list_lock);
964 spin_lock_init(&journal->j_state_lock);
966 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
967 journal->j_min_batch_time = 0;
968 journal->j_max_batch_time = 15000; /* 15ms */
970 /* The journal is marked for error until we succeed with recovery! */
971 journal->j_flags = JBD2_ABORT;
973 /* Set up a default-sized revoke table for the new mount. */
974 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
980 journal_init_stats(journal);
987 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
989 * Create a journal structure assigned some fixed set of disk blocks to
990 * the journal. We don't actually touch those disk blocks yet, but we
991 * need to set up all of the mapping information to tell the journaling
992 * system where the journal blocks are.
997 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
998 * @bdev: Block device on which to create the journal
999 * @fs_dev: Device which hold journalled filesystem for this journal.
1000 * @start: Block nr Start of journal.
1001 * @len: Length of the journal in blocks.
1002 * @blocksize: blocksize of journalling device
1004 * Returns: a newly created journal_t *
1006 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1007 * range of blocks on an arbitrary block device.
1010 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1011 struct block_device *fs_dev,
1012 unsigned long long start, int len, int blocksize)
1014 journal_t *journal = journal_init_common();
1015 struct buffer_head *bh;
1022 /* journal descriptor can store up to n blocks -bzzz */
1023 journal->j_blocksize = blocksize;
1024 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1025 journal->j_wbufsize = n;
1026 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1027 if (!journal->j_wbuf) {
1028 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1034 journal->j_dev = bdev;
1035 journal->j_fs_dev = fs_dev;
1036 journal->j_blk_offset = start;
1037 journal->j_maxlen = len;
1038 bdevname(journal->j_dev, journal->j_devname);
1039 p = journal->j_devname;
1040 while ((p = strchr(p, '/')))
1042 jbd2_stats_proc_init(journal);
1044 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1045 J_ASSERT(bh != NULL);
1046 journal->j_sb_buffer = bh;
1047 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1053 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1054 * @inode: An inode to create the journal in
1056 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1057 * the journal. The inode must exist already, must support bmap() and
1058 * must have all data blocks preallocated.
1060 journal_t * jbd2_journal_init_inode (struct inode *inode)
1062 struct buffer_head *bh;
1063 journal_t *journal = journal_init_common();
1067 unsigned long long blocknr;
1072 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1073 journal->j_inode = inode;
1074 bdevname(journal->j_dev, journal->j_devname);
1075 p = journal->j_devname;
1076 while ((p = strchr(p, '/')))
1078 p = journal->j_devname + strlen(journal->j_devname);
1079 sprintf(p, ":%lu", journal->j_inode->i_ino);
1081 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1082 journal, inode->i_sb->s_id, inode->i_ino,
1083 (long long) inode->i_size,
1084 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1086 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1087 journal->j_blocksize = inode->i_sb->s_blocksize;
1088 jbd2_stats_proc_init(journal);
1090 /* journal descriptor can store up to n blocks -bzzz */
1091 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1092 journal->j_wbufsize = n;
1093 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1094 if (!journal->j_wbuf) {
1095 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1097 jbd2_stats_proc_exit(journal);
1102 err = jbd2_journal_bmap(journal, 0, &blocknr);
1103 /* If that failed, give up */
1105 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
1107 jbd2_stats_proc_exit(journal);
1112 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1113 J_ASSERT(bh != NULL);
1114 journal->j_sb_buffer = bh;
1115 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1121 * If the journal init or create aborts, we need to mark the journal
1122 * superblock as being NULL to prevent the journal destroy from writing
1123 * back a bogus superblock.
1125 static void journal_fail_superblock (journal_t *journal)
1127 struct buffer_head *bh = journal->j_sb_buffer;
1129 journal->j_sb_buffer = NULL;
1133 * Given a journal_t structure, initialise the various fields for
1134 * startup of a new journaling session. We use this both when creating
1135 * a journal, and after recovering an old journal to reset it for
1139 static int journal_reset(journal_t *journal)
1141 journal_superblock_t *sb = journal->j_superblock;
1142 unsigned long long first, last;
1144 first = be32_to_cpu(sb->s_first);
1145 last = be32_to_cpu(sb->s_maxlen);
1147 journal->j_first = first;
1148 journal->j_last = last;
1150 journal->j_head = first;
1151 journal->j_tail = first;
1152 journal->j_free = last - first;
1154 journal->j_tail_sequence = journal->j_transaction_sequence;
1155 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1156 journal->j_commit_request = journal->j_commit_sequence;
1158 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1160 /* Add the dynamic fields and write it to disk. */
1161 jbd2_journal_update_superblock(journal, 1);
1162 return jbd2_journal_start_thread(journal);
1166 * void jbd2_journal_update_superblock() - Update journal sb on disk.
1167 * @journal: The journal to update.
1168 * @wait: Set to '0' if you don't want to wait for IO completion.
1170 * Update a journal's dynamic superblock fields and write it to disk,
1171 * optionally waiting for the IO to complete.
1173 void jbd2_journal_update_superblock(journal_t *journal, int wait)
1175 journal_superblock_t *sb = journal->j_superblock;
1176 struct buffer_head *bh = journal->j_sb_buffer;
1179 * As a special case, if the on-disk copy is already marked as needing
1180 * no recovery (s_start == 0) and there are no outstanding transactions
1181 * in the filesystem, then we can safely defer the superblock update
1182 * until the next commit by setting JBD2_FLUSHED. This avoids
1183 * attempting a write to a potential-readonly device.
1185 if (sb->s_start == 0 && journal->j_tail_sequence ==
1186 journal->j_transaction_sequence) {
1187 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1188 "(start %ld, seq %d, errno %d)\n",
1189 journal->j_tail, journal->j_tail_sequence,
1194 if (buffer_write_io_error(bh)) {
1196 * Oh, dear. A previous attempt to write the journal
1197 * superblock failed. This could happen because the
1198 * USB device was yanked out. Or it could happen to
1199 * be a transient write error and maybe the block will
1200 * be remapped. Nothing we can do but to retry the
1201 * write and hope for the best.
1203 printk(KERN_ERR "JBD2: previous I/O error detected "
1204 "for journal superblock update for %s.\n",
1205 journal->j_devname);
1206 clear_buffer_write_io_error(bh);
1207 set_buffer_uptodate(bh);
1210 spin_lock(&journal->j_state_lock);
1211 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1212 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1214 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1215 sb->s_start = cpu_to_be32(journal->j_tail);
1216 sb->s_errno = cpu_to_be32(journal->j_errno);
1217 spin_unlock(&journal->j_state_lock);
1219 BUFFER_TRACE(bh, "marking dirty");
1220 mark_buffer_dirty(bh);
1222 sync_dirty_buffer(bh);
1223 if (buffer_write_io_error(bh)) {
1224 printk(KERN_ERR "JBD2: I/O error detected "
1225 "when updating journal superblock for %s.\n",
1226 journal->j_devname);
1227 clear_buffer_write_io_error(bh);
1228 set_buffer_uptodate(bh);
1231 ll_rw_block(SWRITE, 1, &bh);
1234 /* If we have just flushed the log (by marking s_start==0), then
1235 * any future commit will have to be careful to update the
1236 * superblock again to re-record the true start of the log. */
1238 spin_lock(&journal->j_state_lock);
1240 journal->j_flags &= ~JBD2_FLUSHED;
1242 journal->j_flags |= JBD2_FLUSHED;
1243 spin_unlock(&journal->j_state_lock);
1247 * Read the superblock for a given journal, performing initial
1248 * validation of the format.
1251 static int journal_get_superblock(journal_t *journal)
1253 struct buffer_head *bh;
1254 journal_superblock_t *sb;
1257 bh = journal->j_sb_buffer;
1259 J_ASSERT(bh != NULL);
1260 if (!buffer_uptodate(bh)) {
1261 ll_rw_block(READ, 1, &bh);
1263 if (!buffer_uptodate(bh)) {
1265 "JBD: IO error reading journal superblock\n");
1270 sb = journal->j_superblock;
1274 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1275 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1276 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1280 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1281 case JBD2_SUPERBLOCK_V1:
1282 journal->j_format_version = 1;
1284 case JBD2_SUPERBLOCK_V2:
1285 journal->j_format_version = 2;
1288 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1292 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1293 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1294 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1295 printk (KERN_WARNING "JBD: journal file too short\n");
1302 journal_fail_superblock(journal);
1307 * Load the on-disk journal superblock and read the key fields into the
1311 static int load_superblock(journal_t *journal)
1314 journal_superblock_t *sb;
1316 err = journal_get_superblock(journal);
1320 sb = journal->j_superblock;
1322 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1323 journal->j_tail = be32_to_cpu(sb->s_start);
1324 journal->j_first = be32_to_cpu(sb->s_first);
1325 journal->j_last = be32_to_cpu(sb->s_maxlen);
1326 journal->j_errno = be32_to_cpu(sb->s_errno);
1333 * int jbd2_journal_load() - Read journal from disk.
1334 * @journal: Journal to act on.
1336 * Given a journal_t structure which tells us which disk blocks contain
1337 * a journal, read the journal from disk to initialise the in-memory
1340 int jbd2_journal_load(journal_t *journal)
1343 journal_superblock_t *sb;
1345 err = load_superblock(journal);
1349 sb = journal->j_superblock;
1350 /* If this is a V2 superblock, then we have to check the
1351 * features flags on it. */
1353 if (journal->j_format_version >= 2) {
1354 if ((sb->s_feature_ro_compat &
1355 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1356 (sb->s_feature_incompat &
1357 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1358 printk (KERN_WARNING
1359 "JBD: Unrecognised features on journal\n");
1364 /* Let the recovery code check whether it needs to recover any
1365 * data from the journal. */
1366 if (jbd2_journal_recover(journal))
1367 goto recovery_error;
1369 /* OK, we've finished with the dynamic journal bits:
1370 * reinitialise the dynamic contents of the superblock in memory
1371 * and reset them on disk. */
1372 if (journal_reset(journal))
1373 goto recovery_error;
1375 journal->j_flags &= ~JBD2_ABORT;
1376 journal->j_flags |= JBD2_LOADED;
1380 printk (KERN_WARNING "JBD: recovery failed\n");
1385 * void jbd2_journal_destroy() - Release a journal_t structure.
1386 * @journal: Journal to act on.
1388 * Release a journal_t structure once it is no longer in use by the
1390 * Return <0 if we couldn't clean up the journal.
1392 int jbd2_journal_destroy(journal_t *journal)
1396 /* Wait for the commit thread to wake up and die. */
1397 journal_kill_thread(journal);
1399 /* Force a final log commit */
1400 if (journal->j_running_transaction)
1401 jbd2_journal_commit_transaction(journal);
1403 /* Force any old transactions to disk */
1405 /* Totally anal locking here... */
1406 spin_lock(&journal->j_list_lock);
1407 while (journal->j_checkpoint_transactions != NULL) {
1408 spin_unlock(&journal->j_list_lock);
1409 mutex_lock(&journal->j_checkpoint_mutex);
1410 jbd2_log_do_checkpoint(journal);
1411 mutex_unlock(&journal->j_checkpoint_mutex);
1412 spin_lock(&journal->j_list_lock);
1415 J_ASSERT(journal->j_running_transaction == NULL);
1416 J_ASSERT(journal->j_committing_transaction == NULL);
1417 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1418 spin_unlock(&journal->j_list_lock);
1420 if (journal->j_sb_buffer) {
1421 if (!is_journal_aborted(journal)) {
1422 /* We can now mark the journal as empty. */
1423 journal->j_tail = 0;
1424 journal->j_tail_sequence =
1425 ++journal->j_transaction_sequence;
1426 jbd2_journal_update_superblock(journal, 1);
1430 brelse(journal->j_sb_buffer);
1433 if (journal->j_proc_entry)
1434 jbd2_stats_proc_exit(journal);
1435 if (journal->j_inode)
1436 iput(journal->j_inode);
1437 if (journal->j_revoke)
1438 jbd2_journal_destroy_revoke(journal);
1439 kfree(journal->j_wbuf);
1447 *int jbd2_journal_check_used_features () - Check if features specified are used.
1448 * @journal: Journal to check.
1449 * @compat: bitmask of compatible features
1450 * @ro: bitmask of features that force read-only mount
1451 * @incompat: bitmask of incompatible features
1453 * Check whether the journal uses all of a given set of
1454 * features. Return true (non-zero) if it does.
1457 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1458 unsigned long ro, unsigned long incompat)
1460 journal_superblock_t *sb;
1462 if (!compat && !ro && !incompat)
1464 if (journal->j_format_version == 1)
1467 sb = journal->j_superblock;
1469 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1470 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1471 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1478 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1479 * @journal: Journal to check.
1480 * @compat: bitmask of compatible features
1481 * @ro: bitmask of features that force read-only mount
1482 * @incompat: bitmask of incompatible features
1484 * Check whether the journaling code supports the use of
1485 * all of a given set of features on this journal. Return true
1486 * (non-zero) if it can. */
1488 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1489 unsigned long ro, unsigned long incompat)
1491 journal_superblock_t *sb;
1493 if (!compat && !ro && !incompat)
1496 sb = journal->j_superblock;
1498 /* We can support any known requested features iff the
1499 * superblock is in version 2. Otherwise we fail to support any
1500 * extended sb features. */
1502 if (journal->j_format_version != 2)
1505 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1506 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1507 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1514 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1515 * @journal: Journal to act on.
1516 * @compat: bitmask of compatible features
1517 * @ro: bitmask of features that force read-only mount
1518 * @incompat: bitmask of incompatible features
1520 * Mark a given journal feature as present on the
1521 * superblock. Returns true if the requested features could be set.
1525 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1526 unsigned long ro, unsigned long incompat)
1528 journal_superblock_t *sb;
1530 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1533 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1536 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1537 compat, ro, incompat);
1539 sb = journal->j_superblock;
1541 sb->s_feature_compat |= cpu_to_be32(compat);
1542 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1543 sb->s_feature_incompat |= cpu_to_be32(incompat);
1549 * jbd2_journal_clear_features () - Clear a given journal feature in the
1551 * @journal: Journal to act on.
1552 * @compat: bitmask of compatible features
1553 * @ro: bitmask of features that force read-only mount
1554 * @incompat: bitmask of incompatible features
1556 * Clear a given journal feature as present on the
1559 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1560 unsigned long ro, unsigned long incompat)
1562 journal_superblock_t *sb;
1564 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1565 compat, ro, incompat);
1567 sb = journal->j_superblock;
1569 sb->s_feature_compat &= ~cpu_to_be32(compat);
1570 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1571 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1573 EXPORT_SYMBOL(jbd2_journal_clear_features);
1576 * int jbd2_journal_update_format () - Update on-disk journal structure.
1577 * @journal: Journal to act on.
1579 * Given an initialised but unloaded journal struct, poke about in the
1580 * on-disk structure to update it to the most recent supported version.
1582 int jbd2_journal_update_format (journal_t *journal)
1584 journal_superblock_t *sb;
1587 err = journal_get_superblock(journal);
1591 sb = journal->j_superblock;
1593 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1594 case JBD2_SUPERBLOCK_V2:
1596 case JBD2_SUPERBLOCK_V1:
1597 return journal_convert_superblock_v1(journal, sb);
1604 static int journal_convert_superblock_v1(journal_t *journal,
1605 journal_superblock_t *sb)
1607 int offset, blocksize;
1608 struct buffer_head *bh;
1611 "JBD: Converting superblock from version 1 to 2.\n");
1613 /* Pre-initialise new fields to zero */
1614 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1615 blocksize = be32_to_cpu(sb->s_blocksize);
1616 memset(&sb->s_feature_compat, 0, blocksize-offset);
1618 sb->s_nr_users = cpu_to_be32(1);
1619 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1620 journal->j_format_version = 2;
1622 bh = journal->j_sb_buffer;
1623 BUFFER_TRACE(bh, "marking dirty");
1624 mark_buffer_dirty(bh);
1625 sync_dirty_buffer(bh);
1631 * int jbd2_journal_flush () - Flush journal
1632 * @journal: Journal to act on.
1634 * Flush all data for a given journal to disk and empty the journal.
1635 * Filesystems can use this when remounting readonly to ensure that
1636 * recovery does not need to happen on remount.
1639 int jbd2_journal_flush(journal_t *journal)
1642 transaction_t *transaction = NULL;
1643 unsigned long old_tail;
1645 spin_lock(&journal->j_state_lock);
1647 /* Force everything buffered to the log... */
1648 if (journal->j_running_transaction) {
1649 transaction = journal->j_running_transaction;
1650 __jbd2_log_start_commit(journal, transaction->t_tid);
1651 } else if (journal->j_committing_transaction)
1652 transaction = journal->j_committing_transaction;
1654 /* Wait for the log commit to complete... */
1656 tid_t tid = transaction->t_tid;
1658 spin_unlock(&journal->j_state_lock);
1659 jbd2_log_wait_commit(journal, tid);
1661 spin_unlock(&journal->j_state_lock);
1664 /* ...and flush everything in the log out to disk. */
1665 spin_lock(&journal->j_list_lock);
1666 while (!err && journal->j_checkpoint_transactions != NULL) {
1667 spin_unlock(&journal->j_list_lock);
1668 mutex_lock(&journal->j_checkpoint_mutex);
1669 err = jbd2_log_do_checkpoint(journal);
1670 mutex_unlock(&journal->j_checkpoint_mutex);
1671 spin_lock(&journal->j_list_lock);
1673 spin_unlock(&journal->j_list_lock);
1675 if (is_journal_aborted(journal))
1678 jbd2_cleanup_journal_tail(journal);
1680 /* Finally, mark the journal as really needing no recovery.
1681 * This sets s_start==0 in the underlying superblock, which is
1682 * the magic code for a fully-recovered superblock. Any future
1683 * commits of data to the journal will restore the current
1685 spin_lock(&journal->j_state_lock);
1686 old_tail = journal->j_tail;
1687 journal->j_tail = 0;
1688 spin_unlock(&journal->j_state_lock);
1689 jbd2_journal_update_superblock(journal, 1);
1690 spin_lock(&journal->j_state_lock);
1691 journal->j_tail = old_tail;
1693 J_ASSERT(!journal->j_running_transaction);
1694 J_ASSERT(!journal->j_committing_transaction);
1695 J_ASSERT(!journal->j_checkpoint_transactions);
1696 J_ASSERT(journal->j_head == journal->j_tail);
1697 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1698 spin_unlock(&journal->j_state_lock);
1703 * int jbd2_journal_wipe() - Wipe journal contents
1704 * @journal: Journal to act on.
1705 * @write: flag (see below)
1707 * Wipe out all of the contents of a journal, safely. This will produce
1708 * a warning if the journal contains any valid recovery information.
1709 * Must be called between journal_init_*() and jbd2_journal_load().
1711 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1712 * we merely suppress recovery.
1715 int jbd2_journal_wipe(journal_t *journal, int write)
1717 journal_superblock_t *sb;
1720 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1722 err = load_superblock(journal);
1726 sb = journal->j_superblock;
1728 if (!journal->j_tail)
1731 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1732 write ? "Clearing" : "Ignoring");
1734 err = jbd2_journal_skip_recovery(journal);
1736 jbd2_journal_update_superblock(journal, 1);
1743 * Journal abort has very specific semantics, which we describe
1744 * for journal abort.
1746 * Two internal function, which provide abort to te jbd layer
1751 * Quick version for internal journal use (doesn't lock the journal).
1752 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1753 * and don't attempt to make any other journal updates.
1755 void __jbd2_journal_abort_hard(journal_t *journal)
1757 transaction_t *transaction;
1759 if (journal->j_flags & JBD2_ABORT)
1762 printk(KERN_ERR "Aborting journal on device %s.\n",
1763 journal->j_devname);
1765 spin_lock(&journal->j_state_lock);
1766 journal->j_flags |= JBD2_ABORT;
1767 transaction = journal->j_running_transaction;
1769 __jbd2_log_start_commit(journal, transaction->t_tid);
1770 spin_unlock(&journal->j_state_lock);
1773 /* Soft abort: record the abort error status in the journal superblock,
1774 * but don't do any other IO. */
1775 static void __journal_abort_soft (journal_t *journal, int errno)
1777 if (journal->j_flags & JBD2_ABORT)
1780 if (!journal->j_errno)
1781 journal->j_errno = errno;
1783 __jbd2_journal_abort_hard(journal);
1786 jbd2_journal_update_superblock(journal, 1);
1790 * void jbd2_journal_abort () - Shutdown the journal immediately.
1791 * @journal: the journal to shutdown.
1792 * @errno: an error number to record in the journal indicating
1793 * the reason for the shutdown.
1795 * Perform a complete, immediate shutdown of the ENTIRE
1796 * journal (not of a single transaction). This operation cannot be
1797 * undone without closing and reopening the journal.
1799 * The jbd2_journal_abort function is intended to support higher level error
1800 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1803 * Journal abort has very specific semantics. Any existing dirty,
1804 * unjournaled buffers in the main filesystem will still be written to
1805 * disk by bdflush, but the journaling mechanism will be suspended
1806 * immediately and no further transaction commits will be honoured.
1808 * Any dirty, journaled buffers will be written back to disk without
1809 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1810 * filesystem, but we _do_ attempt to leave as much data as possible
1811 * behind for fsck to use for cleanup.
1813 * Any attempt to get a new transaction handle on a journal which is in
1814 * ABORT state will just result in an -EROFS error return. A
1815 * jbd2_journal_stop on an existing handle will return -EIO if we have
1816 * entered abort state during the update.
1818 * Recursive transactions are not disturbed by journal abort until the
1819 * final jbd2_journal_stop, which will receive the -EIO error.
1821 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
1822 * which will be recorded (if possible) in the journal superblock. This
1823 * allows a client to record failure conditions in the middle of a
1824 * transaction without having to complete the transaction to record the
1825 * failure to disk. ext3_error, for example, now uses this
1828 * Errors which originate from within the journaling layer will NOT
1829 * supply an errno; a null errno implies that absolutely no further
1830 * writes are done to the journal (unless there are any already in
1835 void jbd2_journal_abort(journal_t *journal, int errno)
1837 __journal_abort_soft(journal, errno);
1841 * int jbd2_journal_errno () - returns the journal's error state.
1842 * @journal: journal to examine.
1844 * This is the errno numbet set with jbd2_journal_abort(), the last
1845 * time the journal was mounted - if the journal was stopped
1846 * without calling abort this will be 0.
1848 * If the journal has been aborted on this mount time -EROFS will
1851 int jbd2_journal_errno(journal_t *journal)
1855 spin_lock(&journal->j_state_lock);
1856 if (journal->j_flags & JBD2_ABORT)
1859 err = journal->j_errno;
1860 spin_unlock(&journal->j_state_lock);
1865 * int jbd2_journal_clear_err () - clears the journal's error state
1866 * @journal: journal to act on.
1868 * An error must be cleared or Acked to take a FS out of readonly
1871 int jbd2_journal_clear_err(journal_t *journal)
1875 spin_lock(&journal->j_state_lock);
1876 if (journal->j_flags & JBD2_ABORT)
1879 journal->j_errno = 0;
1880 spin_unlock(&journal->j_state_lock);
1885 * void jbd2_journal_ack_err() - Ack journal err.
1886 * @journal: journal to act on.
1888 * An error must be cleared or Acked to take a FS out of readonly
1891 void jbd2_journal_ack_err(journal_t *journal)
1893 spin_lock(&journal->j_state_lock);
1894 if (journal->j_errno)
1895 journal->j_flags |= JBD2_ACK_ERR;
1896 spin_unlock(&journal->j_state_lock);
1899 int jbd2_journal_blocks_per_page(struct inode *inode)
1901 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1905 * helper functions to deal with 32 or 64bit block numbers.
1907 size_t journal_tag_bytes(journal_t *journal)
1909 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1910 return JBD2_TAG_SIZE64;
1912 return JBD2_TAG_SIZE32;
1916 * Journal_head storage management
1918 static struct kmem_cache *jbd2_journal_head_cache;
1919 #ifdef CONFIG_JBD2_DEBUG
1920 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1923 static int journal_init_jbd2_journal_head_cache(void)
1927 J_ASSERT(jbd2_journal_head_cache == NULL);
1928 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
1929 sizeof(struct journal_head),
1931 SLAB_TEMPORARY, /* flags */
1934 if (!jbd2_journal_head_cache) {
1936 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1941 static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
1943 if (jbd2_journal_head_cache) {
1944 kmem_cache_destroy(jbd2_journal_head_cache);
1945 jbd2_journal_head_cache = NULL;
1950 * journal_head splicing and dicing
1952 static struct journal_head *journal_alloc_journal_head(void)
1954 struct journal_head *ret;
1955 static unsigned long last_warning;
1957 #ifdef CONFIG_JBD2_DEBUG
1958 atomic_inc(&nr_journal_heads);
1960 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
1962 jbd_debug(1, "out of memory for journal_head\n");
1963 if (time_after(jiffies, last_warning + 5*HZ)) {
1964 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1966 last_warning = jiffies;
1970 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
1976 static void journal_free_journal_head(struct journal_head *jh)
1978 #ifdef CONFIG_JBD2_DEBUG
1979 atomic_dec(&nr_journal_heads);
1980 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
1982 kmem_cache_free(jbd2_journal_head_cache, jh);
1986 * A journal_head is attached to a buffer_head whenever JBD has an
1987 * interest in the buffer.
1989 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1990 * is set. This bit is tested in core kernel code where we need to take
1991 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1994 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1996 * When a buffer has its BH_JBD bit set it is immune from being released by
1997 * core kernel code, mainly via ->b_count.
1999 * A journal_head may be detached from its buffer_head when the journal_head's
2000 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
2001 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
2002 * journal_head can be dropped if needed.
2004 * Various places in the kernel want to attach a journal_head to a buffer_head
2005 * _before_ attaching the journal_head to a transaction. To protect the
2006 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2007 * journal_head's b_jcount refcount by one. The caller must call
2008 * jbd2_journal_put_journal_head() to undo this.
2010 * So the typical usage would be:
2012 * (Attach a journal_head if needed. Increments b_jcount)
2013 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2015 * jh->b_transaction = xxx;
2016 * jbd2_journal_put_journal_head(jh);
2018 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2019 * because it has a non-zero b_transaction.
2023 * Give a buffer_head a journal_head.
2025 * Doesn't need the journal lock.
2028 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2030 struct journal_head *jh;
2031 struct journal_head *new_jh = NULL;
2034 if (!buffer_jbd(bh)) {
2035 new_jh = journal_alloc_journal_head();
2036 memset(new_jh, 0, sizeof(*new_jh));
2039 jbd_lock_bh_journal_head(bh);
2040 if (buffer_jbd(bh)) {
2044 (atomic_read(&bh->b_count) > 0) ||
2045 (bh->b_page && bh->b_page->mapping));
2048 jbd_unlock_bh_journal_head(bh);
2053 new_jh = NULL; /* We consumed it */
2058 BUFFER_TRACE(bh, "added journal_head");
2061 jbd_unlock_bh_journal_head(bh);
2063 journal_free_journal_head(new_jh);
2064 return bh->b_private;
2068 * Grab a ref against this buffer_head's journal_head. If it ended up not
2069 * having a journal_head, return NULL
2071 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2073 struct journal_head *jh = NULL;
2075 jbd_lock_bh_journal_head(bh);
2076 if (buffer_jbd(bh)) {
2080 jbd_unlock_bh_journal_head(bh);
2084 static void __journal_remove_journal_head(struct buffer_head *bh)
2086 struct journal_head *jh = bh2jh(bh);
2088 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2091 if (jh->b_jcount == 0) {
2092 if (jh->b_transaction == NULL &&
2093 jh->b_next_transaction == NULL &&
2094 jh->b_cp_transaction == NULL) {
2095 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2096 J_ASSERT_BH(bh, buffer_jbd(bh));
2097 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2098 BUFFER_TRACE(bh, "remove journal_head");
2099 if (jh->b_frozen_data) {
2100 printk(KERN_WARNING "%s: freeing "
2103 jbd2_free(jh->b_frozen_data, bh->b_size);
2105 if (jh->b_committed_data) {
2106 printk(KERN_WARNING "%s: freeing "
2107 "b_committed_data\n",
2109 jbd2_free(jh->b_committed_data, bh->b_size);
2111 bh->b_private = NULL;
2112 jh->b_bh = NULL; /* debug, really */
2113 clear_buffer_jbd(bh);
2115 journal_free_journal_head(jh);
2117 BUFFER_TRACE(bh, "journal_head was locked");
2123 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
2124 * and has a zero b_jcount then remove and release its journal_head. If we did
2125 * see that the buffer is not used by any transaction we also "logically"
2126 * decrement ->b_count.
2128 * We in fact take an additional increment on ->b_count as a convenience,
2129 * because the caller usually wants to do additional things with the bh
2130 * after calling here.
2131 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
2132 * time. Once the caller has run __brelse(), the buffer is eligible for
2133 * reaping by try_to_free_buffers().
2135 void jbd2_journal_remove_journal_head(struct buffer_head *bh)
2137 jbd_lock_bh_journal_head(bh);
2138 __journal_remove_journal_head(bh);
2139 jbd_unlock_bh_journal_head(bh);
2143 * Drop a reference on the passed journal_head. If it fell to zero then try to
2144 * release the journal_head from the buffer_head.
2146 void jbd2_journal_put_journal_head(struct journal_head *jh)
2148 struct buffer_head *bh = jh2bh(jh);
2150 jbd_lock_bh_journal_head(bh);
2151 J_ASSERT_JH(jh, jh->b_jcount > 0);
2153 if (!jh->b_jcount && !jh->b_transaction) {
2154 __journal_remove_journal_head(bh);
2157 jbd_unlock_bh_journal_head(bh);
2161 * Initialize jbd inode head
2163 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2165 jinode->i_transaction = NULL;
2166 jinode->i_next_transaction = NULL;
2167 jinode->i_vfs_inode = inode;
2168 jinode->i_flags = 0;
2169 INIT_LIST_HEAD(&jinode->i_list);
2173 * Function to be called before we start removing inode from memory (i.e.,
2174 * clear_inode() is a fine place to be called from). It removes inode from
2175 * transaction's lists.
2177 void jbd2_journal_release_jbd_inode(journal_t *journal,
2178 struct jbd2_inode *jinode)
2185 spin_lock(&journal->j_list_lock);
2186 /* Is commit writing out inode - we have to wait */
2187 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2188 wait_queue_head_t *wq;
2189 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2190 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2191 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2192 spin_unlock(&journal->j_list_lock);
2194 finish_wait(wq, &wait.wait);
2198 /* Do we need to wait for data writeback? */
2199 if (journal->j_committing_transaction == jinode->i_transaction)
2201 if (jinode->i_transaction) {
2202 list_del(&jinode->i_list);
2203 jinode->i_transaction = NULL;
2205 spin_unlock(&journal->j_list_lock);
2211 #ifdef CONFIG_JBD2_DEBUG
2212 u8 jbd2_journal_enable_debug __read_mostly;
2213 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2215 #define JBD2_DEBUG_NAME "jbd2-debug"
2217 static struct dentry *jbd2_debugfs_dir;
2218 static struct dentry *jbd2_debug;
2220 static void __init jbd2_create_debugfs_entry(void)
2222 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2223 if (jbd2_debugfs_dir)
2224 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2226 &jbd2_journal_enable_debug);
2229 static void __exit jbd2_remove_debugfs_entry(void)
2231 debugfs_remove(jbd2_debug);
2232 debugfs_remove(jbd2_debugfs_dir);
2237 static void __init jbd2_create_debugfs_entry(void)
2241 static void __exit jbd2_remove_debugfs_entry(void)
2247 #ifdef CONFIG_PROC_FS
2249 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2251 static void __init jbd2_create_jbd_stats_proc_entry(void)
2253 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2256 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2258 if (proc_jbd2_stats)
2259 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2264 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2265 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2269 struct kmem_cache *jbd2_handle_cache;
2271 static int __init journal_init_handle_cache(void)
2273 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
2276 SLAB_TEMPORARY, /* flags */
2278 if (jbd2_handle_cache == NULL) {
2279 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2285 static void jbd2_journal_destroy_handle_cache(void)
2287 if (jbd2_handle_cache)
2288 kmem_cache_destroy(jbd2_handle_cache);
2292 * Module startup and shutdown
2295 static int __init journal_init_caches(void)
2299 ret = jbd2_journal_init_revoke_caches();
2301 ret = journal_init_jbd2_journal_head_cache();
2303 ret = journal_init_handle_cache();
2307 static void jbd2_journal_destroy_caches(void)
2309 jbd2_journal_destroy_revoke_caches();
2310 jbd2_journal_destroy_jbd2_journal_head_cache();
2311 jbd2_journal_destroy_handle_cache();
2314 static int __init journal_init(void)
2318 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2320 ret = journal_init_caches();
2322 jbd2_create_debugfs_entry();
2323 jbd2_create_jbd_stats_proc_entry();
2325 jbd2_journal_destroy_caches();
2330 static void __exit journal_exit(void)
2332 #ifdef CONFIG_JBD2_DEBUG
2333 int n = atomic_read(&nr_journal_heads);
2335 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2337 jbd2_remove_debugfs_entry();
2338 jbd2_remove_jbd_stats_proc_entry();
2339 jbd2_journal_destroy_caches();
2342 MODULE_LICENSE("GPL");
2343 module_init(journal_init);
2344 module_exit(journal_exit);