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_set_triggers);
55 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
56 EXPORT_SYMBOL(jbd2_journal_release_buffer);
57 EXPORT_SYMBOL(jbd2_journal_forget);
59 EXPORT_SYMBOL(journal_sync_buffer);
61 EXPORT_SYMBOL(jbd2_journal_flush);
62 EXPORT_SYMBOL(jbd2_journal_revoke);
64 EXPORT_SYMBOL(jbd2_journal_init_dev);
65 EXPORT_SYMBOL(jbd2_journal_init_inode);
66 EXPORT_SYMBOL(jbd2_journal_update_format);
67 EXPORT_SYMBOL(jbd2_journal_check_used_features);
68 EXPORT_SYMBOL(jbd2_journal_check_available_features);
69 EXPORT_SYMBOL(jbd2_journal_set_features);
70 EXPORT_SYMBOL(jbd2_journal_load);
71 EXPORT_SYMBOL(jbd2_journal_destroy);
72 EXPORT_SYMBOL(jbd2_journal_abort);
73 EXPORT_SYMBOL(jbd2_journal_errno);
74 EXPORT_SYMBOL(jbd2_journal_ack_err);
75 EXPORT_SYMBOL(jbd2_journal_clear_err);
76 EXPORT_SYMBOL(jbd2_log_wait_commit);
77 EXPORT_SYMBOL(jbd2_journal_start_commit);
78 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
79 EXPORT_SYMBOL(jbd2_journal_wipe);
80 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
81 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
82 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
83 EXPORT_SYMBOL(jbd2_journal_force_commit);
84 EXPORT_SYMBOL(jbd2_journal_file_inode);
85 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
86 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
87 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
89 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
90 static void __journal_abort_soft (journal_t *journal, int errno);
93 * Helper function used to manage commit timeouts
96 static void commit_timeout(unsigned long __data)
98 struct task_struct * p = (struct task_struct *) __data;
104 * kjournald2: The main thread function used to manage a logging device
107 * This kernel thread is responsible for two things:
109 * 1) COMMIT: Every so often we need to commit the current state of the
110 * filesystem to disk. The journal thread is responsible for writing
111 * all of the metadata buffers to disk.
113 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
114 * of the data in that part of the log has been rewritten elsewhere on
115 * the disk. Flushing these old buffers to reclaim space in the log is
116 * known as checkpointing, and this thread is responsible for that job.
119 static int kjournald2(void *arg)
121 journal_t *journal = arg;
122 transaction_t *transaction;
125 * Set up an interval timer which can be used to trigger a commit wakeup
126 * after the commit interval expires
128 setup_timer(&journal->j_commit_timer, commit_timeout,
129 (unsigned long)current);
131 /* Record that the journal thread is running */
132 journal->j_task = current;
133 wake_up(&journal->j_wait_done_commit);
135 printk(KERN_INFO "kjournald2 starting: pid %d, dev %s, "
136 "commit interval %ld seconds\n", current->pid,
137 journal->j_devname, journal->j_commit_interval / HZ);
140 * And now, wait forever for commit wakeup events.
142 spin_lock(&journal->j_state_lock);
145 if (journal->j_flags & JBD2_UNMOUNT)
148 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
149 journal->j_commit_sequence, journal->j_commit_request);
151 if (journal->j_commit_sequence != journal->j_commit_request) {
152 jbd_debug(1, "OK, requests differ\n");
153 spin_unlock(&journal->j_state_lock);
154 del_timer_sync(&journal->j_commit_timer);
155 jbd2_journal_commit_transaction(journal);
156 spin_lock(&journal->j_state_lock);
160 wake_up(&journal->j_wait_done_commit);
161 if (freezing(current)) {
163 * The simpler the better. Flushing journal isn't a
164 * good idea, because that depends on threads that may
165 * be already stopped.
167 jbd_debug(1, "Now suspending kjournald2\n");
168 spin_unlock(&journal->j_state_lock);
170 spin_lock(&journal->j_state_lock);
173 * We assume on resume that commits are already there,
177 int should_sleep = 1;
179 prepare_to_wait(&journal->j_wait_commit, &wait,
181 if (journal->j_commit_sequence != journal->j_commit_request)
183 transaction = journal->j_running_transaction;
184 if (transaction && time_after_eq(jiffies,
185 transaction->t_expires))
187 if (journal->j_flags & JBD2_UNMOUNT)
190 spin_unlock(&journal->j_state_lock);
192 spin_lock(&journal->j_state_lock);
194 finish_wait(&journal->j_wait_commit, &wait);
197 jbd_debug(1, "kjournald2 wakes\n");
200 * Were we woken up by a commit wakeup event?
202 transaction = journal->j_running_transaction;
203 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
204 journal->j_commit_request = transaction->t_tid;
205 jbd_debug(1, "woke because of timeout\n");
210 spin_unlock(&journal->j_state_lock);
211 del_timer_sync(&journal->j_commit_timer);
212 journal->j_task = NULL;
213 wake_up(&journal->j_wait_done_commit);
214 jbd_debug(1, "Journal thread exiting.\n");
218 static int jbd2_journal_start_thread(journal_t *journal)
220 struct task_struct *t;
222 t = kthread_run(kjournald2, journal, "kjournald2");
226 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
230 static void journal_kill_thread(journal_t *journal)
232 spin_lock(&journal->j_state_lock);
233 journal->j_flags |= JBD2_UNMOUNT;
235 while (journal->j_task) {
236 wake_up(&journal->j_wait_commit);
237 spin_unlock(&journal->j_state_lock);
238 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
239 spin_lock(&journal->j_state_lock);
241 spin_unlock(&journal->j_state_lock);
245 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
247 * Writes a metadata buffer to a given disk block. The actual IO is not
248 * performed but a new buffer_head is constructed which labels the data
249 * to be written with the correct destination disk block.
251 * Any magic-number escaping which needs to be done will cause a
252 * copy-out here. If the buffer happens to start with the
253 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
254 * magic number is only written to the log for descripter blocks. In
255 * this case, we copy the data and replace the first word with 0, and we
256 * return a result code which indicates that this buffer needs to be
257 * marked as an escaped buffer in the corresponding log descriptor
258 * block. The missing word can then be restored when the block is read
261 * If the source buffer has already been modified by a new transaction
262 * since we took the last commit snapshot, we use the frozen copy of
263 * that data for IO. If we end up using the existing buffer_head's data
264 * for the write, then we *have* to lock the buffer to prevent anyone
265 * else from using and possibly modifying it while the IO is in
268 * The function returns a pointer to the buffer_heads to be used for IO.
270 * We assume that the journal has already been locked in this function.
277 * Bit 0 set == escape performed on the data
278 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
281 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
282 struct journal_head *jh_in,
283 struct journal_head **jh_out,
284 unsigned long long blocknr)
286 int need_copy_out = 0;
287 int done_copy_out = 0;
290 struct buffer_head *new_bh;
291 struct journal_head *new_jh;
292 struct page *new_page;
293 unsigned int new_offset;
294 struct buffer_head *bh_in = jh2bh(jh_in);
295 struct jbd2_buffer_trigger_type *triggers;
298 * The buffer really shouldn't be locked: only the current committing
299 * transaction is allowed to write it, so nobody else is allowed
302 * akpm: except if we're journalling data, and write() output is
303 * also part of a shared mapping, and another thread has
304 * decided to launch a writepage() against this buffer.
306 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
308 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
311 * If a new transaction has already done a buffer copy-out, then
312 * we use that version of the data for the commit.
314 jbd_lock_bh_state(bh_in);
316 if (jh_in->b_frozen_data) {
318 new_page = virt_to_page(jh_in->b_frozen_data);
319 new_offset = offset_in_page(jh_in->b_frozen_data);
320 triggers = jh_in->b_frozen_triggers;
322 new_page = jh2bh(jh_in)->b_page;
323 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
324 triggers = jh_in->b_triggers;
327 mapped_data = kmap_atomic(new_page, KM_USER0);
329 * Fire any commit trigger. Do this before checking for escaping,
330 * as the trigger may modify the magic offset. If a copy-out
331 * happens afterwards, it will have the correct data in the buffer.
333 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
339 if (*((__be32 *)(mapped_data + new_offset)) ==
340 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
344 kunmap_atomic(mapped_data, KM_USER0);
347 * Do we need to do a data copy?
349 if (need_copy_out && !done_copy_out) {
352 jbd_unlock_bh_state(bh_in);
353 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
354 jbd_lock_bh_state(bh_in);
355 if (jh_in->b_frozen_data) {
356 jbd2_free(tmp, bh_in->b_size);
360 jh_in->b_frozen_data = tmp;
361 mapped_data = kmap_atomic(new_page, KM_USER0);
362 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
363 kunmap_atomic(mapped_data, KM_USER0);
365 new_page = virt_to_page(tmp);
366 new_offset = offset_in_page(tmp);
370 * This isn't strictly necessary, as we're using frozen
371 * data for the escaping, but it keeps consistency with
372 * b_frozen_data usage.
374 jh_in->b_frozen_triggers = jh_in->b_triggers;
378 * Did we need to do an escaping? Now we've done all the
379 * copying, we can finally do so.
382 mapped_data = kmap_atomic(new_page, KM_USER0);
383 *((unsigned int *)(mapped_data + new_offset)) = 0;
384 kunmap_atomic(mapped_data, KM_USER0);
387 /* keep subsequent assertions sane */
389 init_buffer(new_bh, NULL, NULL);
390 atomic_set(&new_bh->b_count, 1);
391 jbd_unlock_bh_state(bh_in);
393 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
395 set_bh_page(new_bh, new_page, new_offset);
396 new_jh->b_transaction = NULL;
397 new_bh->b_size = jh2bh(jh_in)->b_size;
398 new_bh->b_bdev = transaction->t_journal->j_dev;
399 new_bh->b_blocknr = blocknr;
400 set_buffer_mapped(new_bh);
401 set_buffer_dirty(new_bh);
406 * The to-be-written buffer needs to get moved to the io queue,
407 * and the original buffer whose contents we are shadowing or
408 * copying is moved to the transaction's shadow queue.
410 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
411 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
412 JBUFFER_TRACE(new_jh, "file as BJ_IO");
413 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
415 return do_escape | (done_copy_out << 1);
419 * Allocation code for the journal file. Manage the space left in the
420 * journal, so that we can begin checkpointing when appropriate.
424 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
426 * Called with the journal already locked.
428 * Called under j_state_lock
431 int __jbd2_log_space_left(journal_t *journal)
433 int left = journal->j_free;
435 assert_spin_locked(&journal->j_state_lock);
438 * Be pessimistic here about the number of those free blocks which
439 * might be required for log descriptor control blocks.
442 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
444 left -= MIN_LOG_RESERVED_BLOCKS;
453 * Called under j_state_lock. Returns true if a transaction was started.
455 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
458 * Are we already doing a recent enough commit?
460 if (!tid_geq(journal->j_commit_request, target)) {
462 * We want a new commit: OK, mark the request and wakup the
463 * commit thread. We do _not_ do the commit ourselves.
466 journal->j_commit_request = target;
467 jbd_debug(1, "JBD: requesting commit %d/%d\n",
468 journal->j_commit_request,
469 journal->j_commit_sequence);
470 wake_up(&journal->j_wait_commit);
476 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
480 spin_lock(&journal->j_state_lock);
481 ret = __jbd2_log_start_commit(journal, tid);
482 spin_unlock(&journal->j_state_lock);
487 * Force and wait upon a commit if the calling process is not within
488 * transaction. This is used for forcing out undo-protected data which contains
489 * bitmaps, when the fs is running out of space.
491 * We can only force the running transaction if we don't have an active handle;
492 * otherwise, we will deadlock.
494 * Returns true if a transaction was started.
496 int jbd2_journal_force_commit_nested(journal_t *journal)
498 transaction_t *transaction = NULL;
501 spin_lock(&journal->j_state_lock);
502 if (journal->j_running_transaction && !current->journal_info) {
503 transaction = journal->j_running_transaction;
504 __jbd2_log_start_commit(journal, transaction->t_tid);
505 } else if (journal->j_committing_transaction)
506 transaction = journal->j_committing_transaction;
509 spin_unlock(&journal->j_state_lock);
510 return 0; /* Nothing to retry */
513 tid = transaction->t_tid;
514 spin_unlock(&journal->j_state_lock);
515 jbd2_log_wait_commit(journal, tid);
520 * Start a commit of the current running transaction (if any). Returns true
521 * if a transaction was started, and fills its tid in at *ptid
523 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
527 spin_lock(&journal->j_state_lock);
528 if (journal->j_running_transaction) {
529 tid_t tid = journal->j_running_transaction->t_tid;
531 ret = __jbd2_log_start_commit(journal, tid);
534 } else if (journal->j_committing_transaction && ptid) {
536 * If ext3_write_super() recently started a commit, then we
537 * have to wait for completion of that transaction
539 *ptid = journal->j_committing_transaction->t_tid;
542 spin_unlock(&journal->j_state_lock);
547 * Wait for a specified commit to complete.
548 * The caller may not hold the journal lock.
550 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
554 #ifdef CONFIG_JBD2_DEBUG
555 spin_lock(&journal->j_state_lock);
556 if (!tid_geq(journal->j_commit_request, tid)) {
558 "%s: error: j_commit_request=%d, tid=%d\n",
559 __func__, journal->j_commit_request, tid);
561 spin_unlock(&journal->j_state_lock);
563 spin_lock(&journal->j_state_lock);
564 while (tid_gt(tid, journal->j_commit_sequence)) {
565 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
566 tid, journal->j_commit_sequence);
567 wake_up(&journal->j_wait_commit);
568 spin_unlock(&journal->j_state_lock);
569 wait_event(journal->j_wait_done_commit,
570 !tid_gt(tid, journal->j_commit_sequence));
571 spin_lock(&journal->j_state_lock);
573 spin_unlock(&journal->j_state_lock);
575 if (unlikely(is_journal_aborted(journal))) {
576 printk(KERN_EMERG "journal commit I/O error\n");
583 * Log buffer allocation routines:
586 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
588 unsigned long blocknr;
590 spin_lock(&journal->j_state_lock);
591 J_ASSERT(journal->j_free > 1);
593 blocknr = journal->j_head;
596 if (journal->j_head == journal->j_last)
597 journal->j_head = journal->j_first;
598 spin_unlock(&journal->j_state_lock);
599 return jbd2_journal_bmap(journal, blocknr, retp);
603 * Conversion of logical to physical block numbers for the journal
605 * On external journals the journal blocks are identity-mapped, so
606 * this is a no-op. If needed, we can use j_blk_offset - everything is
609 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
610 unsigned long long *retp)
613 unsigned long long ret;
615 if (journal->j_inode) {
616 ret = bmap(journal->j_inode, blocknr);
620 printk(KERN_ALERT "%s: journal block not found "
621 "at offset %lu on %s\n",
622 __func__, blocknr, journal->j_devname);
624 __journal_abort_soft(journal, err);
627 *retp = blocknr; /* +journal->j_blk_offset */
633 * We play buffer_head aliasing tricks to write data/metadata blocks to
634 * the journal without copying their contents, but for journal
635 * descriptor blocks we do need to generate bona fide buffers.
637 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
638 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
639 * But we don't bother doing that, so there will be coherency problems with
640 * mmaps of blockdevs which hold live JBD-controlled filesystems.
642 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
644 struct buffer_head *bh;
645 unsigned long long blocknr;
648 err = jbd2_journal_next_log_block(journal, &blocknr);
653 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
657 memset(bh->b_data, 0, journal->j_blocksize);
658 set_buffer_uptodate(bh);
660 BUFFER_TRACE(bh, "return this buffer");
661 return jbd2_journal_add_journal_head(bh);
664 struct jbd2_stats_proc_session {
666 struct transaction_stats_s *stats;
671 static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
672 struct transaction_stats_s *ts,
675 if (ts == s->stats + s->max)
677 if (!first && ts == s->stats + s->start)
679 while (ts->ts_type == 0) {
681 if (ts == s->stats + s->max)
683 if (ts == s->stats + s->start)
690 static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
692 struct jbd2_stats_proc_session *s = seq->private;
693 struct transaction_stats_s *ts;
697 return SEQ_START_TOKEN;
698 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
703 ts = jbd2_history_skip_empty(s, ++ts, 0);
711 static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
713 struct jbd2_stats_proc_session *s = seq->private;
714 struct transaction_stats_s *ts = v;
717 if (v == SEQ_START_TOKEN)
718 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
720 return jbd2_history_skip_empty(s, ++ts, 0);
723 static int jbd2_seq_history_show(struct seq_file *seq, void *v)
725 struct transaction_stats_s *ts = v;
726 if (v == SEQ_START_TOKEN) {
727 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
728 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
729 "wait", "run", "lock", "flush", "log", "hndls",
730 "block", "inlog", "ctime", "write", "drop",
734 if (ts->ts_type == JBD2_STATS_RUN)
735 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
736 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
737 jiffies_to_msecs(ts->u.run.rs_wait),
738 jiffies_to_msecs(ts->u.run.rs_running),
739 jiffies_to_msecs(ts->u.run.rs_locked),
740 jiffies_to_msecs(ts->u.run.rs_flushing),
741 jiffies_to_msecs(ts->u.run.rs_logging),
742 ts->u.run.rs_handle_count,
744 ts->u.run.rs_blocks_logged);
745 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
746 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
747 "C", ts->ts_tid, " ",
748 jiffies_to_msecs(ts->u.chp.cs_chp_time),
749 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
750 ts->u.chp.cs_forced_to_close);
756 static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
760 static struct seq_operations jbd2_seq_history_ops = {
761 .start = jbd2_seq_history_start,
762 .next = jbd2_seq_history_next,
763 .stop = jbd2_seq_history_stop,
764 .show = jbd2_seq_history_show,
767 static int jbd2_seq_history_open(struct inode *inode, struct file *file)
769 journal_t *journal = PDE(inode)->data;
770 struct jbd2_stats_proc_session *s;
773 s = kmalloc(sizeof(*s), GFP_KERNEL);
776 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
777 s->stats = kmalloc(size, GFP_KERNEL);
778 if (s->stats == NULL) {
782 spin_lock(&journal->j_history_lock);
783 memcpy(s->stats, journal->j_history, size);
784 s->max = journal->j_history_max;
785 s->start = journal->j_history_cur % s->max;
786 spin_unlock(&journal->j_history_lock);
788 rc = seq_open(file, &jbd2_seq_history_ops);
790 struct seq_file *m = file->private_data;
800 static int jbd2_seq_history_release(struct inode *inode, struct file *file)
802 struct seq_file *seq = file->private_data;
803 struct jbd2_stats_proc_session *s = seq->private;
807 return seq_release(inode, file);
810 static struct file_operations jbd2_seq_history_fops = {
811 .owner = THIS_MODULE,
812 .open = jbd2_seq_history_open,
815 .release = jbd2_seq_history_release,
818 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
820 return *pos ? NULL : SEQ_START_TOKEN;
823 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
828 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
830 struct jbd2_stats_proc_session *s = seq->private;
832 if (v != SEQ_START_TOKEN)
834 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
836 s->journal->j_max_transaction_buffers);
837 if (s->stats->ts_tid == 0)
839 seq_printf(seq, "average: \n %ums waiting for transaction\n",
840 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
841 seq_printf(seq, " %ums running transaction\n",
842 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
843 seq_printf(seq, " %ums transaction was being locked\n",
844 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
845 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
846 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
847 seq_printf(seq, " %ums logging transaction\n",
848 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
849 seq_printf(seq, " %luus average transaction commit time\n",
850 do_div(s->journal->j_average_commit_time, 1000));
851 seq_printf(seq, " %lu handles per transaction\n",
852 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
853 seq_printf(seq, " %lu blocks per transaction\n",
854 s->stats->u.run.rs_blocks / s->stats->ts_tid);
855 seq_printf(seq, " %lu logged blocks per transaction\n",
856 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
860 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
864 static struct seq_operations jbd2_seq_info_ops = {
865 .start = jbd2_seq_info_start,
866 .next = jbd2_seq_info_next,
867 .stop = jbd2_seq_info_stop,
868 .show = jbd2_seq_info_show,
871 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
873 journal_t *journal = PDE(inode)->data;
874 struct jbd2_stats_proc_session *s;
877 s = kmalloc(sizeof(*s), GFP_KERNEL);
880 size = sizeof(struct transaction_stats_s);
881 s->stats = kmalloc(size, GFP_KERNEL);
882 if (s->stats == NULL) {
886 spin_lock(&journal->j_history_lock);
887 memcpy(s->stats, &journal->j_stats, size);
888 s->journal = journal;
889 spin_unlock(&journal->j_history_lock);
891 rc = seq_open(file, &jbd2_seq_info_ops);
893 struct seq_file *m = file->private_data;
903 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
905 struct seq_file *seq = file->private_data;
906 struct jbd2_stats_proc_session *s = seq->private;
909 return seq_release(inode, file);
912 static struct file_operations jbd2_seq_info_fops = {
913 .owner = THIS_MODULE,
914 .open = jbd2_seq_info_open,
917 .release = jbd2_seq_info_release,
920 static struct proc_dir_entry *proc_jbd2_stats;
922 static void jbd2_stats_proc_init(journal_t *journal)
924 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
925 if (journal->j_proc_entry) {
926 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
927 &jbd2_seq_history_fops, journal);
928 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
929 &jbd2_seq_info_fops, journal);
933 static void jbd2_stats_proc_exit(journal_t *journal)
935 remove_proc_entry("info", journal->j_proc_entry);
936 remove_proc_entry("history", journal->j_proc_entry);
937 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
940 static void journal_init_stats(journal_t *journal)
944 if (!proc_jbd2_stats)
947 journal->j_history_max = 100;
948 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
949 journal->j_history = kzalloc(size, GFP_KERNEL);
950 if (!journal->j_history) {
951 journal->j_history_max = 0;
954 spin_lock_init(&journal->j_history_lock);
958 * Management for journal control blocks: functions to create and
959 * destroy journal_t structures, and to initialise and read existing
960 * journal blocks from disk. */
962 /* First: create and setup a journal_t object in memory. We initialise
963 * very few fields yet: that has to wait until we have created the
964 * journal structures from from scratch, or loaded them from disk. */
966 static journal_t * journal_init_common (void)
971 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
975 init_waitqueue_head(&journal->j_wait_transaction_locked);
976 init_waitqueue_head(&journal->j_wait_logspace);
977 init_waitqueue_head(&journal->j_wait_done_commit);
978 init_waitqueue_head(&journal->j_wait_checkpoint);
979 init_waitqueue_head(&journal->j_wait_commit);
980 init_waitqueue_head(&journal->j_wait_updates);
981 mutex_init(&journal->j_barrier);
982 mutex_init(&journal->j_checkpoint_mutex);
983 spin_lock_init(&journal->j_revoke_lock);
984 spin_lock_init(&journal->j_list_lock);
985 spin_lock_init(&journal->j_state_lock);
987 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
988 journal->j_min_batch_time = 0;
989 journal->j_max_batch_time = 15000; /* 15ms */
991 /* The journal is marked for error until we succeed with recovery! */
992 journal->j_flags = JBD2_ABORT;
994 /* Set up a default-sized revoke table for the new mount. */
995 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1001 journal_init_stats(journal);
1008 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1010 * Create a journal structure assigned some fixed set of disk blocks to
1011 * the journal. We don't actually touch those disk blocks yet, but we
1012 * need to set up all of the mapping information to tell the journaling
1013 * system where the journal blocks are.
1018 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1019 * @bdev: Block device on which to create the journal
1020 * @fs_dev: Device which hold journalled filesystem for this journal.
1021 * @start: Block nr Start of journal.
1022 * @len: Length of the journal in blocks.
1023 * @blocksize: blocksize of journalling device
1025 * Returns: a newly created journal_t *
1027 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1028 * range of blocks on an arbitrary block device.
1031 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1032 struct block_device *fs_dev,
1033 unsigned long long start, int len, int blocksize)
1035 journal_t *journal = journal_init_common();
1036 struct buffer_head *bh;
1043 /* journal descriptor can store up to n blocks -bzzz */
1044 journal->j_blocksize = blocksize;
1045 jbd2_stats_proc_init(journal);
1046 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1047 journal->j_wbufsize = n;
1048 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1049 if (!journal->j_wbuf) {
1050 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1054 journal->j_dev = bdev;
1055 journal->j_fs_dev = fs_dev;
1056 journal->j_blk_offset = start;
1057 journal->j_maxlen = len;
1058 bdevname(journal->j_dev, journal->j_devname);
1059 p = journal->j_devname;
1060 while ((p = strchr(p, '/')))
1063 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1066 "%s: Cannot get buffer for journal superblock\n",
1070 journal->j_sb_buffer = bh;
1071 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1075 jbd2_stats_proc_exit(journal);
1081 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1082 * @inode: An inode to create the journal in
1084 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1085 * the journal. The inode must exist already, must support bmap() and
1086 * must have all data blocks preallocated.
1088 journal_t * jbd2_journal_init_inode (struct inode *inode)
1090 struct buffer_head *bh;
1091 journal_t *journal = journal_init_common();
1095 unsigned long long blocknr;
1100 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1101 journal->j_inode = inode;
1102 bdevname(journal->j_dev, journal->j_devname);
1103 p = journal->j_devname;
1104 while ((p = strchr(p, '/')))
1106 p = journal->j_devname + strlen(journal->j_devname);
1107 sprintf(p, ":%lu", journal->j_inode->i_ino);
1109 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1110 journal, inode->i_sb->s_id, inode->i_ino,
1111 (long long) inode->i_size,
1112 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1114 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1115 journal->j_blocksize = inode->i_sb->s_blocksize;
1116 jbd2_stats_proc_init(journal);
1118 /* journal descriptor can store up to n blocks -bzzz */
1119 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1120 journal->j_wbufsize = n;
1121 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1122 if (!journal->j_wbuf) {
1123 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1128 err = jbd2_journal_bmap(journal, 0, &blocknr);
1129 /* If that failed, give up */
1131 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
1136 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1139 "%s: Cannot get buffer for journal superblock\n",
1143 journal->j_sb_buffer = bh;
1144 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1148 jbd2_stats_proc_exit(journal);
1154 * If the journal init or create aborts, we need to mark the journal
1155 * superblock as being NULL to prevent the journal destroy from writing
1156 * back a bogus superblock.
1158 static void journal_fail_superblock (journal_t *journal)
1160 struct buffer_head *bh = journal->j_sb_buffer;
1162 journal->j_sb_buffer = NULL;
1166 * Given a journal_t structure, initialise the various fields for
1167 * startup of a new journaling session. We use this both when creating
1168 * a journal, and after recovering an old journal to reset it for
1172 static int journal_reset(journal_t *journal)
1174 journal_superblock_t *sb = journal->j_superblock;
1175 unsigned long long first, last;
1177 first = be32_to_cpu(sb->s_first);
1178 last = be32_to_cpu(sb->s_maxlen);
1180 journal->j_first = first;
1181 journal->j_last = last;
1183 journal->j_head = first;
1184 journal->j_tail = first;
1185 journal->j_free = last - first;
1187 journal->j_tail_sequence = journal->j_transaction_sequence;
1188 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1189 journal->j_commit_request = journal->j_commit_sequence;
1191 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1193 /* Add the dynamic fields and write it to disk. */
1194 jbd2_journal_update_superblock(journal, 1);
1195 return jbd2_journal_start_thread(journal);
1199 * void jbd2_journal_update_superblock() - Update journal sb on disk.
1200 * @journal: The journal to update.
1201 * @wait: Set to '0' if you don't want to wait for IO completion.
1203 * Update a journal's dynamic superblock fields and write it to disk,
1204 * optionally waiting for the IO to complete.
1206 void jbd2_journal_update_superblock(journal_t *journal, int wait)
1208 journal_superblock_t *sb = journal->j_superblock;
1209 struct buffer_head *bh = journal->j_sb_buffer;
1212 * As a special case, if the on-disk copy is already marked as needing
1213 * no recovery (s_start == 0) and there are no outstanding transactions
1214 * in the filesystem, then we can safely defer the superblock update
1215 * until the next commit by setting JBD2_FLUSHED. This avoids
1216 * attempting a write to a potential-readonly device.
1218 if (sb->s_start == 0 && journal->j_tail_sequence ==
1219 journal->j_transaction_sequence) {
1220 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1221 "(start %ld, seq %d, errno %d)\n",
1222 journal->j_tail, journal->j_tail_sequence,
1227 if (buffer_write_io_error(bh)) {
1229 * Oh, dear. A previous attempt to write the journal
1230 * superblock failed. This could happen because the
1231 * USB device was yanked out. Or it could happen to
1232 * be a transient write error and maybe the block will
1233 * be remapped. Nothing we can do but to retry the
1234 * write and hope for the best.
1236 printk(KERN_ERR "JBD2: previous I/O error detected "
1237 "for journal superblock update for %s.\n",
1238 journal->j_devname);
1239 clear_buffer_write_io_error(bh);
1240 set_buffer_uptodate(bh);
1243 spin_lock(&journal->j_state_lock);
1244 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1245 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1247 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1248 sb->s_start = cpu_to_be32(journal->j_tail);
1249 sb->s_errno = cpu_to_be32(journal->j_errno);
1250 spin_unlock(&journal->j_state_lock);
1252 BUFFER_TRACE(bh, "marking dirty");
1253 mark_buffer_dirty(bh);
1255 sync_dirty_buffer(bh);
1256 if (buffer_write_io_error(bh)) {
1257 printk(KERN_ERR "JBD2: I/O error detected "
1258 "when updating journal superblock for %s.\n",
1259 journal->j_devname);
1260 clear_buffer_write_io_error(bh);
1261 set_buffer_uptodate(bh);
1264 ll_rw_block(SWRITE, 1, &bh);
1267 /* If we have just flushed the log (by marking s_start==0), then
1268 * any future commit will have to be careful to update the
1269 * superblock again to re-record the true start of the log. */
1271 spin_lock(&journal->j_state_lock);
1273 journal->j_flags &= ~JBD2_FLUSHED;
1275 journal->j_flags |= JBD2_FLUSHED;
1276 spin_unlock(&journal->j_state_lock);
1280 * Read the superblock for a given journal, performing initial
1281 * validation of the format.
1284 static int journal_get_superblock(journal_t *journal)
1286 struct buffer_head *bh;
1287 journal_superblock_t *sb;
1290 bh = journal->j_sb_buffer;
1292 J_ASSERT(bh != NULL);
1293 if (!buffer_uptodate(bh)) {
1294 ll_rw_block(READ, 1, &bh);
1296 if (!buffer_uptodate(bh)) {
1298 "JBD: IO error reading journal superblock\n");
1303 sb = journal->j_superblock;
1307 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1308 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1309 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1313 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1314 case JBD2_SUPERBLOCK_V1:
1315 journal->j_format_version = 1;
1317 case JBD2_SUPERBLOCK_V2:
1318 journal->j_format_version = 2;
1321 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1325 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1326 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1327 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1328 printk (KERN_WARNING "JBD: journal file too short\n");
1335 journal_fail_superblock(journal);
1340 * Load the on-disk journal superblock and read the key fields into the
1344 static int load_superblock(journal_t *journal)
1347 journal_superblock_t *sb;
1349 err = journal_get_superblock(journal);
1353 sb = journal->j_superblock;
1355 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1356 journal->j_tail = be32_to_cpu(sb->s_start);
1357 journal->j_first = be32_to_cpu(sb->s_first);
1358 journal->j_last = be32_to_cpu(sb->s_maxlen);
1359 journal->j_errno = be32_to_cpu(sb->s_errno);
1366 * int jbd2_journal_load() - Read journal from disk.
1367 * @journal: Journal to act on.
1369 * Given a journal_t structure which tells us which disk blocks contain
1370 * a journal, read the journal from disk to initialise the in-memory
1373 int jbd2_journal_load(journal_t *journal)
1376 journal_superblock_t *sb;
1378 err = load_superblock(journal);
1382 sb = journal->j_superblock;
1383 /* If this is a V2 superblock, then we have to check the
1384 * features flags on it. */
1386 if (journal->j_format_version >= 2) {
1387 if ((sb->s_feature_ro_compat &
1388 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1389 (sb->s_feature_incompat &
1390 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1391 printk (KERN_WARNING
1392 "JBD: Unrecognised features on journal\n");
1397 /* Let the recovery code check whether it needs to recover any
1398 * data from the journal. */
1399 if (jbd2_journal_recover(journal))
1400 goto recovery_error;
1402 /* OK, we've finished with the dynamic journal bits:
1403 * reinitialise the dynamic contents of the superblock in memory
1404 * and reset them on disk. */
1405 if (journal_reset(journal))
1406 goto recovery_error;
1408 journal->j_flags &= ~JBD2_ABORT;
1409 journal->j_flags |= JBD2_LOADED;
1413 printk (KERN_WARNING "JBD: recovery failed\n");
1418 * void jbd2_journal_destroy() - Release a journal_t structure.
1419 * @journal: Journal to act on.
1421 * Release a journal_t structure once it is no longer in use by the
1423 * Return <0 if we couldn't clean up the journal.
1425 int jbd2_journal_destroy(journal_t *journal)
1429 /* Wait for the commit thread to wake up and die. */
1430 journal_kill_thread(journal);
1432 /* Force a final log commit */
1433 if (journal->j_running_transaction)
1434 jbd2_journal_commit_transaction(journal);
1436 /* Force any old transactions to disk */
1438 /* Totally anal locking here... */
1439 spin_lock(&journal->j_list_lock);
1440 while (journal->j_checkpoint_transactions != NULL) {
1441 spin_unlock(&journal->j_list_lock);
1442 mutex_lock(&journal->j_checkpoint_mutex);
1443 jbd2_log_do_checkpoint(journal);
1444 mutex_unlock(&journal->j_checkpoint_mutex);
1445 spin_lock(&journal->j_list_lock);
1448 J_ASSERT(journal->j_running_transaction == NULL);
1449 J_ASSERT(journal->j_committing_transaction == NULL);
1450 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1451 spin_unlock(&journal->j_list_lock);
1453 if (journal->j_sb_buffer) {
1454 if (!is_journal_aborted(journal)) {
1455 /* We can now mark the journal as empty. */
1456 journal->j_tail = 0;
1457 journal->j_tail_sequence =
1458 ++journal->j_transaction_sequence;
1459 jbd2_journal_update_superblock(journal, 1);
1463 brelse(journal->j_sb_buffer);
1466 if (journal->j_proc_entry)
1467 jbd2_stats_proc_exit(journal);
1468 if (journal->j_inode)
1469 iput(journal->j_inode);
1470 if (journal->j_revoke)
1471 jbd2_journal_destroy_revoke(journal);
1472 kfree(journal->j_wbuf);
1480 *int jbd2_journal_check_used_features () - Check if features specified are used.
1481 * @journal: Journal to check.
1482 * @compat: bitmask of compatible features
1483 * @ro: bitmask of features that force read-only mount
1484 * @incompat: bitmask of incompatible features
1486 * Check whether the journal uses all of a given set of
1487 * features. Return true (non-zero) if it does.
1490 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1491 unsigned long ro, unsigned long incompat)
1493 journal_superblock_t *sb;
1495 if (!compat && !ro && !incompat)
1497 if (journal->j_format_version == 1)
1500 sb = journal->j_superblock;
1502 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1503 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1504 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1511 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1512 * @journal: Journal to check.
1513 * @compat: bitmask of compatible features
1514 * @ro: bitmask of features that force read-only mount
1515 * @incompat: bitmask of incompatible features
1517 * Check whether the journaling code supports the use of
1518 * all of a given set of features on this journal. Return true
1519 * (non-zero) if it can. */
1521 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1522 unsigned long ro, unsigned long incompat)
1524 journal_superblock_t *sb;
1526 if (!compat && !ro && !incompat)
1529 sb = journal->j_superblock;
1531 /* We can support any known requested features iff the
1532 * superblock is in version 2. Otherwise we fail to support any
1533 * extended sb features. */
1535 if (journal->j_format_version != 2)
1538 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1539 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1540 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1547 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1548 * @journal: Journal to act on.
1549 * @compat: bitmask of compatible features
1550 * @ro: bitmask of features that force read-only mount
1551 * @incompat: bitmask of incompatible features
1553 * Mark a given journal feature as present on the
1554 * superblock. Returns true if the requested features could be set.
1558 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1559 unsigned long ro, unsigned long incompat)
1561 journal_superblock_t *sb;
1563 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1566 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1569 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1570 compat, ro, incompat);
1572 sb = journal->j_superblock;
1574 sb->s_feature_compat |= cpu_to_be32(compat);
1575 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1576 sb->s_feature_incompat |= cpu_to_be32(incompat);
1582 * jbd2_journal_clear_features () - Clear a given journal feature in the
1584 * @journal: Journal to act on.
1585 * @compat: bitmask of compatible features
1586 * @ro: bitmask of features that force read-only mount
1587 * @incompat: bitmask of incompatible features
1589 * Clear a given journal feature as present on the
1592 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1593 unsigned long ro, unsigned long incompat)
1595 journal_superblock_t *sb;
1597 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1598 compat, ro, incompat);
1600 sb = journal->j_superblock;
1602 sb->s_feature_compat &= ~cpu_to_be32(compat);
1603 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1604 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1606 EXPORT_SYMBOL(jbd2_journal_clear_features);
1609 * int jbd2_journal_update_format () - Update on-disk journal structure.
1610 * @journal: Journal to act on.
1612 * Given an initialised but unloaded journal struct, poke about in the
1613 * on-disk structure to update it to the most recent supported version.
1615 int jbd2_journal_update_format (journal_t *journal)
1617 journal_superblock_t *sb;
1620 err = journal_get_superblock(journal);
1624 sb = journal->j_superblock;
1626 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1627 case JBD2_SUPERBLOCK_V2:
1629 case JBD2_SUPERBLOCK_V1:
1630 return journal_convert_superblock_v1(journal, sb);
1637 static int journal_convert_superblock_v1(journal_t *journal,
1638 journal_superblock_t *sb)
1640 int offset, blocksize;
1641 struct buffer_head *bh;
1644 "JBD: Converting superblock from version 1 to 2.\n");
1646 /* Pre-initialise new fields to zero */
1647 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1648 blocksize = be32_to_cpu(sb->s_blocksize);
1649 memset(&sb->s_feature_compat, 0, blocksize-offset);
1651 sb->s_nr_users = cpu_to_be32(1);
1652 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1653 journal->j_format_version = 2;
1655 bh = journal->j_sb_buffer;
1656 BUFFER_TRACE(bh, "marking dirty");
1657 mark_buffer_dirty(bh);
1658 sync_dirty_buffer(bh);
1664 * int jbd2_journal_flush () - Flush journal
1665 * @journal: Journal to act on.
1667 * Flush all data for a given journal to disk and empty the journal.
1668 * Filesystems can use this when remounting readonly to ensure that
1669 * recovery does not need to happen on remount.
1672 int jbd2_journal_flush(journal_t *journal)
1675 transaction_t *transaction = NULL;
1676 unsigned long old_tail;
1678 spin_lock(&journal->j_state_lock);
1680 /* Force everything buffered to the log... */
1681 if (journal->j_running_transaction) {
1682 transaction = journal->j_running_transaction;
1683 __jbd2_log_start_commit(journal, transaction->t_tid);
1684 } else if (journal->j_committing_transaction)
1685 transaction = journal->j_committing_transaction;
1687 /* Wait for the log commit to complete... */
1689 tid_t tid = transaction->t_tid;
1691 spin_unlock(&journal->j_state_lock);
1692 jbd2_log_wait_commit(journal, tid);
1694 spin_unlock(&journal->j_state_lock);
1697 /* ...and flush everything in the log out to disk. */
1698 spin_lock(&journal->j_list_lock);
1699 while (!err && journal->j_checkpoint_transactions != NULL) {
1700 spin_unlock(&journal->j_list_lock);
1701 mutex_lock(&journal->j_checkpoint_mutex);
1702 err = jbd2_log_do_checkpoint(journal);
1703 mutex_unlock(&journal->j_checkpoint_mutex);
1704 spin_lock(&journal->j_list_lock);
1706 spin_unlock(&journal->j_list_lock);
1708 if (is_journal_aborted(journal))
1711 jbd2_cleanup_journal_tail(journal);
1713 /* Finally, mark the journal as really needing no recovery.
1714 * This sets s_start==0 in the underlying superblock, which is
1715 * the magic code for a fully-recovered superblock. Any future
1716 * commits of data to the journal will restore the current
1718 spin_lock(&journal->j_state_lock);
1719 old_tail = journal->j_tail;
1720 journal->j_tail = 0;
1721 spin_unlock(&journal->j_state_lock);
1722 jbd2_journal_update_superblock(journal, 1);
1723 spin_lock(&journal->j_state_lock);
1724 journal->j_tail = old_tail;
1726 J_ASSERT(!journal->j_running_transaction);
1727 J_ASSERT(!journal->j_committing_transaction);
1728 J_ASSERT(!journal->j_checkpoint_transactions);
1729 J_ASSERT(journal->j_head == journal->j_tail);
1730 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1731 spin_unlock(&journal->j_state_lock);
1736 * int jbd2_journal_wipe() - Wipe journal contents
1737 * @journal: Journal to act on.
1738 * @write: flag (see below)
1740 * Wipe out all of the contents of a journal, safely. This will produce
1741 * a warning if the journal contains any valid recovery information.
1742 * Must be called between journal_init_*() and jbd2_journal_load().
1744 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1745 * we merely suppress recovery.
1748 int jbd2_journal_wipe(journal_t *journal, int write)
1750 journal_superblock_t *sb;
1753 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1755 err = load_superblock(journal);
1759 sb = journal->j_superblock;
1761 if (!journal->j_tail)
1764 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1765 write ? "Clearing" : "Ignoring");
1767 err = jbd2_journal_skip_recovery(journal);
1769 jbd2_journal_update_superblock(journal, 1);
1776 * Journal abort has very specific semantics, which we describe
1777 * for journal abort.
1779 * Two internal function, which provide abort to te jbd layer
1784 * Quick version for internal journal use (doesn't lock the journal).
1785 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1786 * and don't attempt to make any other journal updates.
1788 void __jbd2_journal_abort_hard(journal_t *journal)
1790 transaction_t *transaction;
1792 if (journal->j_flags & JBD2_ABORT)
1795 printk(KERN_ERR "Aborting journal on device %s.\n",
1796 journal->j_devname);
1798 spin_lock(&journal->j_state_lock);
1799 journal->j_flags |= JBD2_ABORT;
1800 transaction = journal->j_running_transaction;
1802 __jbd2_log_start_commit(journal, transaction->t_tid);
1803 spin_unlock(&journal->j_state_lock);
1806 /* Soft abort: record the abort error status in the journal superblock,
1807 * but don't do any other IO. */
1808 static void __journal_abort_soft (journal_t *journal, int errno)
1810 if (journal->j_flags & JBD2_ABORT)
1813 if (!journal->j_errno)
1814 journal->j_errno = errno;
1816 __jbd2_journal_abort_hard(journal);
1819 jbd2_journal_update_superblock(journal, 1);
1823 * void jbd2_journal_abort () - Shutdown the journal immediately.
1824 * @journal: the journal to shutdown.
1825 * @errno: an error number to record in the journal indicating
1826 * the reason for the shutdown.
1828 * Perform a complete, immediate shutdown of the ENTIRE
1829 * journal (not of a single transaction). This operation cannot be
1830 * undone without closing and reopening the journal.
1832 * The jbd2_journal_abort function is intended to support higher level error
1833 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1836 * Journal abort has very specific semantics. Any existing dirty,
1837 * unjournaled buffers in the main filesystem will still be written to
1838 * disk by bdflush, but the journaling mechanism will be suspended
1839 * immediately and no further transaction commits will be honoured.
1841 * Any dirty, journaled buffers will be written back to disk without
1842 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1843 * filesystem, but we _do_ attempt to leave as much data as possible
1844 * behind for fsck to use for cleanup.
1846 * Any attempt to get a new transaction handle on a journal which is in
1847 * ABORT state will just result in an -EROFS error return. A
1848 * jbd2_journal_stop on an existing handle will return -EIO if we have
1849 * entered abort state during the update.
1851 * Recursive transactions are not disturbed by journal abort until the
1852 * final jbd2_journal_stop, which will receive the -EIO error.
1854 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
1855 * which will be recorded (if possible) in the journal superblock. This
1856 * allows a client to record failure conditions in the middle of a
1857 * transaction without having to complete the transaction to record the
1858 * failure to disk. ext3_error, for example, now uses this
1861 * Errors which originate from within the journaling layer will NOT
1862 * supply an errno; a null errno implies that absolutely no further
1863 * writes are done to the journal (unless there are any already in
1868 void jbd2_journal_abort(journal_t *journal, int errno)
1870 __journal_abort_soft(journal, errno);
1874 * int jbd2_journal_errno () - returns the journal's error state.
1875 * @journal: journal to examine.
1877 * This is the errno numbet set with jbd2_journal_abort(), the last
1878 * time the journal was mounted - if the journal was stopped
1879 * without calling abort this will be 0.
1881 * If the journal has been aborted on this mount time -EROFS will
1884 int jbd2_journal_errno(journal_t *journal)
1888 spin_lock(&journal->j_state_lock);
1889 if (journal->j_flags & JBD2_ABORT)
1892 err = journal->j_errno;
1893 spin_unlock(&journal->j_state_lock);
1898 * int jbd2_journal_clear_err () - clears the journal's error state
1899 * @journal: journal to act on.
1901 * An error must be cleared or Acked to take a FS out of readonly
1904 int jbd2_journal_clear_err(journal_t *journal)
1908 spin_lock(&journal->j_state_lock);
1909 if (journal->j_flags & JBD2_ABORT)
1912 journal->j_errno = 0;
1913 spin_unlock(&journal->j_state_lock);
1918 * void jbd2_journal_ack_err() - Ack journal err.
1919 * @journal: journal to act on.
1921 * An error must be cleared or Acked to take a FS out of readonly
1924 void jbd2_journal_ack_err(journal_t *journal)
1926 spin_lock(&journal->j_state_lock);
1927 if (journal->j_errno)
1928 journal->j_flags |= JBD2_ACK_ERR;
1929 spin_unlock(&journal->j_state_lock);
1932 int jbd2_journal_blocks_per_page(struct inode *inode)
1934 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1938 * helper functions to deal with 32 or 64bit block numbers.
1940 size_t journal_tag_bytes(journal_t *journal)
1942 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1943 return JBD2_TAG_SIZE64;
1945 return JBD2_TAG_SIZE32;
1949 * Journal_head storage management
1951 static struct kmem_cache *jbd2_journal_head_cache;
1952 #ifdef CONFIG_JBD2_DEBUG
1953 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1956 static int journal_init_jbd2_journal_head_cache(void)
1960 J_ASSERT(jbd2_journal_head_cache == NULL);
1961 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
1962 sizeof(struct journal_head),
1964 SLAB_TEMPORARY, /* flags */
1967 if (!jbd2_journal_head_cache) {
1969 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1974 static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
1976 if (jbd2_journal_head_cache) {
1977 kmem_cache_destroy(jbd2_journal_head_cache);
1978 jbd2_journal_head_cache = NULL;
1983 * journal_head splicing and dicing
1985 static struct journal_head *journal_alloc_journal_head(void)
1987 struct journal_head *ret;
1988 static unsigned long last_warning;
1990 #ifdef CONFIG_JBD2_DEBUG
1991 atomic_inc(&nr_journal_heads);
1993 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
1995 jbd_debug(1, "out of memory for journal_head\n");
1996 if (time_after(jiffies, last_warning + 5*HZ)) {
1997 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1999 last_warning = jiffies;
2003 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2009 static void journal_free_journal_head(struct journal_head *jh)
2011 #ifdef CONFIG_JBD2_DEBUG
2012 atomic_dec(&nr_journal_heads);
2013 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2015 kmem_cache_free(jbd2_journal_head_cache, jh);
2019 * A journal_head is attached to a buffer_head whenever JBD has an
2020 * interest in the buffer.
2022 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2023 * is set. This bit is tested in core kernel code where we need to take
2024 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2027 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2029 * When a buffer has its BH_JBD bit set it is immune from being released by
2030 * core kernel code, mainly via ->b_count.
2032 * A journal_head may be detached from its buffer_head when the journal_head's
2033 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
2034 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
2035 * journal_head can be dropped if needed.
2037 * Various places in the kernel want to attach a journal_head to a buffer_head
2038 * _before_ attaching the journal_head to a transaction. To protect the
2039 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2040 * journal_head's b_jcount refcount by one. The caller must call
2041 * jbd2_journal_put_journal_head() to undo this.
2043 * So the typical usage would be:
2045 * (Attach a journal_head if needed. Increments b_jcount)
2046 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2048 * jh->b_transaction = xxx;
2049 * jbd2_journal_put_journal_head(jh);
2051 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2052 * because it has a non-zero b_transaction.
2056 * Give a buffer_head a journal_head.
2058 * Doesn't need the journal lock.
2061 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2063 struct journal_head *jh;
2064 struct journal_head *new_jh = NULL;
2067 if (!buffer_jbd(bh)) {
2068 new_jh = journal_alloc_journal_head();
2069 memset(new_jh, 0, sizeof(*new_jh));
2072 jbd_lock_bh_journal_head(bh);
2073 if (buffer_jbd(bh)) {
2077 (atomic_read(&bh->b_count) > 0) ||
2078 (bh->b_page && bh->b_page->mapping));
2081 jbd_unlock_bh_journal_head(bh);
2086 new_jh = NULL; /* We consumed it */
2091 BUFFER_TRACE(bh, "added journal_head");
2094 jbd_unlock_bh_journal_head(bh);
2096 journal_free_journal_head(new_jh);
2097 return bh->b_private;
2101 * Grab a ref against this buffer_head's journal_head. If it ended up not
2102 * having a journal_head, return NULL
2104 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2106 struct journal_head *jh = NULL;
2108 jbd_lock_bh_journal_head(bh);
2109 if (buffer_jbd(bh)) {
2113 jbd_unlock_bh_journal_head(bh);
2117 static void __journal_remove_journal_head(struct buffer_head *bh)
2119 struct journal_head *jh = bh2jh(bh);
2121 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2124 if (jh->b_jcount == 0) {
2125 if (jh->b_transaction == NULL &&
2126 jh->b_next_transaction == NULL &&
2127 jh->b_cp_transaction == NULL) {
2128 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2129 J_ASSERT_BH(bh, buffer_jbd(bh));
2130 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2131 BUFFER_TRACE(bh, "remove journal_head");
2132 if (jh->b_frozen_data) {
2133 printk(KERN_WARNING "%s: freeing "
2136 jbd2_free(jh->b_frozen_data, bh->b_size);
2138 if (jh->b_committed_data) {
2139 printk(KERN_WARNING "%s: freeing "
2140 "b_committed_data\n",
2142 jbd2_free(jh->b_committed_data, bh->b_size);
2144 bh->b_private = NULL;
2145 jh->b_bh = NULL; /* debug, really */
2146 clear_buffer_jbd(bh);
2148 journal_free_journal_head(jh);
2150 BUFFER_TRACE(bh, "journal_head was locked");
2156 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
2157 * and has a zero b_jcount then remove and release its journal_head. If we did
2158 * see that the buffer is not used by any transaction we also "logically"
2159 * decrement ->b_count.
2161 * We in fact take an additional increment on ->b_count as a convenience,
2162 * because the caller usually wants to do additional things with the bh
2163 * after calling here.
2164 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
2165 * time. Once the caller has run __brelse(), the buffer is eligible for
2166 * reaping by try_to_free_buffers().
2168 void jbd2_journal_remove_journal_head(struct buffer_head *bh)
2170 jbd_lock_bh_journal_head(bh);
2171 __journal_remove_journal_head(bh);
2172 jbd_unlock_bh_journal_head(bh);
2176 * Drop a reference on the passed journal_head. If it fell to zero then try to
2177 * release the journal_head from the buffer_head.
2179 void jbd2_journal_put_journal_head(struct journal_head *jh)
2181 struct buffer_head *bh = jh2bh(jh);
2183 jbd_lock_bh_journal_head(bh);
2184 J_ASSERT_JH(jh, jh->b_jcount > 0);
2186 if (!jh->b_jcount && !jh->b_transaction) {
2187 __journal_remove_journal_head(bh);
2190 jbd_unlock_bh_journal_head(bh);
2194 * Initialize jbd inode head
2196 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2198 jinode->i_transaction = NULL;
2199 jinode->i_next_transaction = NULL;
2200 jinode->i_vfs_inode = inode;
2201 jinode->i_flags = 0;
2202 INIT_LIST_HEAD(&jinode->i_list);
2206 * Function to be called before we start removing inode from memory (i.e.,
2207 * clear_inode() is a fine place to be called from). It removes inode from
2208 * transaction's lists.
2210 void jbd2_journal_release_jbd_inode(journal_t *journal,
2211 struct jbd2_inode *jinode)
2218 spin_lock(&journal->j_list_lock);
2219 /* Is commit writing out inode - we have to wait */
2220 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2221 wait_queue_head_t *wq;
2222 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2223 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2224 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2225 spin_unlock(&journal->j_list_lock);
2227 finish_wait(wq, &wait.wait);
2231 /* Do we need to wait for data writeback? */
2232 if (journal->j_committing_transaction == jinode->i_transaction)
2234 if (jinode->i_transaction) {
2235 list_del(&jinode->i_list);
2236 jinode->i_transaction = NULL;
2238 spin_unlock(&journal->j_list_lock);
2244 #ifdef CONFIG_JBD2_DEBUG
2245 u8 jbd2_journal_enable_debug __read_mostly;
2246 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2248 #define JBD2_DEBUG_NAME "jbd2-debug"
2250 static struct dentry *jbd2_debugfs_dir;
2251 static struct dentry *jbd2_debug;
2253 static void __init jbd2_create_debugfs_entry(void)
2255 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2256 if (jbd2_debugfs_dir)
2257 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2259 &jbd2_journal_enable_debug);
2262 static void __exit jbd2_remove_debugfs_entry(void)
2264 debugfs_remove(jbd2_debug);
2265 debugfs_remove(jbd2_debugfs_dir);
2270 static void __init jbd2_create_debugfs_entry(void)
2274 static void __exit jbd2_remove_debugfs_entry(void)
2280 #ifdef CONFIG_PROC_FS
2282 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2284 static void __init jbd2_create_jbd_stats_proc_entry(void)
2286 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2289 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2291 if (proc_jbd2_stats)
2292 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2297 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2298 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2302 struct kmem_cache *jbd2_handle_cache;
2304 static int __init journal_init_handle_cache(void)
2306 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
2309 SLAB_TEMPORARY, /* flags */
2311 if (jbd2_handle_cache == NULL) {
2312 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2318 static void jbd2_journal_destroy_handle_cache(void)
2320 if (jbd2_handle_cache)
2321 kmem_cache_destroy(jbd2_handle_cache);
2325 * Module startup and shutdown
2328 static int __init journal_init_caches(void)
2332 ret = jbd2_journal_init_revoke_caches();
2334 ret = journal_init_jbd2_journal_head_cache();
2336 ret = journal_init_handle_cache();
2340 static void jbd2_journal_destroy_caches(void)
2342 jbd2_journal_destroy_revoke_caches();
2343 jbd2_journal_destroy_jbd2_journal_head_cache();
2344 jbd2_journal_destroy_handle_cache();
2347 static int __init journal_init(void)
2351 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2353 ret = journal_init_caches();
2355 jbd2_create_debugfs_entry();
2356 jbd2_create_jbd_stats_proc_entry();
2358 jbd2_journal_destroy_caches();
2363 static void __exit journal_exit(void)
2365 #ifdef CONFIG_JBD2_DEBUG
2366 int n = atomic_read(&nr_journal_heads);
2368 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2370 jbd2_remove_debugfs_entry();
2371 jbd2_remove_jbd_stats_proc_entry();
2372 jbd2_journal_destroy_caches();
2375 MODULE_LICENSE("GPL");
2376 module_init(journal_init);
2377 module_exit(journal_exit);