jbd2: fix /proc setup for devices that contain '/' in their names
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jbd2 / journal.c
blobebe4940dd3d9fd0ef7dd5f16f63927ebaa79f7bb
1 /*
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
13 * journaling system.
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>
27 #include <linux/fs.h>
28 #include <linux/jbd2.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/mm.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>
42 #include <asm/page.h>
44 EXPORT_SYMBOL(jbd2_journal_start);
45 EXPORT_SYMBOL(jbd2_journal_restart);
46 EXPORT_SYMBOL(jbd2_journal_extend);
47 EXPORT_SYMBOL(jbd2_journal_stop);
48 EXPORT_SYMBOL(jbd2_journal_lock_updates);
49 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
50 EXPORT_SYMBOL(jbd2_journal_get_write_access);
51 EXPORT_SYMBOL(jbd2_journal_get_create_access);
52 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
53 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
54 EXPORT_SYMBOL(jbd2_journal_release_buffer);
55 EXPORT_SYMBOL(jbd2_journal_forget);
56 #if 0
57 EXPORT_SYMBOL(journal_sync_buffer);
58 #endif
59 EXPORT_SYMBOL(jbd2_journal_flush);
60 EXPORT_SYMBOL(jbd2_journal_revoke);
62 EXPORT_SYMBOL(jbd2_journal_init_dev);
63 EXPORT_SYMBOL(jbd2_journal_init_inode);
64 EXPORT_SYMBOL(jbd2_journal_update_format);
65 EXPORT_SYMBOL(jbd2_journal_check_used_features);
66 EXPORT_SYMBOL(jbd2_journal_check_available_features);
67 EXPORT_SYMBOL(jbd2_journal_set_features);
68 EXPORT_SYMBOL(jbd2_journal_create);
69 EXPORT_SYMBOL(jbd2_journal_load);
70 EXPORT_SYMBOL(jbd2_journal_destroy);
71 EXPORT_SYMBOL(jbd2_journal_abort);
72 EXPORT_SYMBOL(jbd2_journal_errno);
73 EXPORT_SYMBOL(jbd2_journal_ack_err);
74 EXPORT_SYMBOL(jbd2_journal_clear_err);
75 EXPORT_SYMBOL(jbd2_log_wait_commit);
76 EXPORT_SYMBOL(jbd2_journal_start_commit);
77 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
78 EXPORT_SYMBOL(jbd2_journal_wipe);
79 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
80 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
81 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
82 EXPORT_SYMBOL(jbd2_journal_force_commit);
83 EXPORT_SYMBOL(jbd2_journal_file_inode);
84 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
85 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
86 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
88 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
89 static void __journal_abort_soft (journal_t *journal, int errno);
92 * Helper function used to manage commit timeouts
95 static void commit_timeout(unsigned long __data)
97 struct task_struct * p = (struct task_struct *) __data;
99 wake_up_process(p);
103 * kjournald2: The main thread function used to manage a logging device
104 * journal.
106 * This kernel thread is responsible for two things:
108 * 1) COMMIT: Every so often we need to commit the current state of the
109 * filesystem to disk. The journal thread is responsible for writing
110 * all of the metadata buffers to disk.
112 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
113 * of the data in that part of the log has been rewritten elsewhere on
114 * the disk. Flushing these old buffers to reclaim space in the log is
115 * known as checkpointing, and this thread is responsible for that job.
118 static int kjournald2(void *arg)
120 journal_t *journal = arg;
121 transaction_t *transaction;
124 * Set up an interval timer which can be used to trigger a commit wakeup
125 * after the commit interval expires
127 setup_timer(&journal->j_commit_timer, commit_timeout,
128 (unsigned long)current);
130 /* Record that the journal thread is running */
131 journal->j_task = current;
132 wake_up(&journal->j_wait_done_commit);
134 printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
135 journal->j_commit_interval / HZ);
138 * And now, wait forever for commit wakeup events.
140 spin_lock(&journal->j_state_lock);
142 loop:
143 if (journal->j_flags & JBD2_UNMOUNT)
144 goto end_loop;
146 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
147 journal->j_commit_sequence, journal->j_commit_request);
149 if (journal->j_commit_sequence != journal->j_commit_request) {
150 jbd_debug(1, "OK, requests differ\n");
151 spin_unlock(&journal->j_state_lock);
152 del_timer_sync(&journal->j_commit_timer);
153 jbd2_journal_commit_transaction(journal);
154 spin_lock(&journal->j_state_lock);
155 goto loop;
158 wake_up(&journal->j_wait_done_commit);
159 if (freezing(current)) {
161 * The simpler the better. Flushing journal isn't a
162 * good idea, because that depends on threads that may
163 * be already stopped.
165 jbd_debug(1, "Now suspending kjournald2\n");
166 spin_unlock(&journal->j_state_lock);
167 refrigerator();
168 spin_lock(&journal->j_state_lock);
169 } else {
171 * We assume on resume that commits are already there,
172 * so we don't sleep
174 DEFINE_WAIT(wait);
175 int should_sleep = 1;
177 prepare_to_wait(&journal->j_wait_commit, &wait,
178 TASK_INTERRUPTIBLE);
179 if (journal->j_commit_sequence != journal->j_commit_request)
180 should_sleep = 0;
181 transaction = journal->j_running_transaction;
182 if (transaction && time_after_eq(jiffies,
183 transaction->t_expires))
184 should_sleep = 0;
185 if (journal->j_flags & JBD2_UNMOUNT)
186 should_sleep = 0;
187 if (should_sleep) {
188 spin_unlock(&journal->j_state_lock);
189 schedule();
190 spin_lock(&journal->j_state_lock);
192 finish_wait(&journal->j_wait_commit, &wait);
195 jbd_debug(1, "kjournald2 wakes\n");
198 * Were we woken up by a commit wakeup event?
200 transaction = journal->j_running_transaction;
201 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
202 journal->j_commit_request = transaction->t_tid;
203 jbd_debug(1, "woke because of timeout\n");
205 goto loop;
207 end_loop:
208 spin_unlock(&journal->j_state_lock);
209 del_timer_sync(&journal->j_commit_timer);
210 journal->j_task = NULL;
211 wake_up(&journal->j_wait_done_commit);
212 jbd_debug(1, "Journal thread exiting.\n");
213 return 0;
216 static int jbd2_journal_start_thread(journal_t *journal)
218 struct task_struct *t;
220 t = kthread_run(kjournald2, journal, "kjournald2");
221 if (IS_ERR(t))
222 return PTR_ERR(t);
224 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
225 return 0;
228 static void journal_kill_thread(journal_t *journal)
230 spin_lock(&journal->j_state_lock);
231 journal->j_flags |= JBD2_UNMOUNT;
233 while (journal->j_task) {
234 wake_up(&journal->j_wait_commit);
235 spin_unlock(&journal->j_state_lock);
236 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
237 spin_lock(&journal->j_state_lock);
239 spin_unlock(&journal->j_state_lock);
243 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
245 * Writes a metadata buffer to a given disk block. The actual IO is not
246 * performed but a new buffer_head is constructed which labels the data
247 * to be written with the correct destination disk block.
249 * Any magic-number escaping which needs to be done will cause a
250 * copy-out here. If the buffer happens to start with the
251 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
252 * magic number is only written to the log for descripter blocks. In
253 * this case, we copy the data and replace the first word with 0, and we
254 * return a result code which indicates that this buffer needs to be
255 * marked as an escaped buffer in the corresponding log descriptor
256 * block. The missing word can then be restored when the block is read
257 * during recovery.
259 * If the source buffer has already been modified by a new transaction
260 * since we took the last commit snapshot, we use the frozen copy of
261 * that data for IO. If we end up using the existing buffer_head's data
262 * for the write, then we *have* to lock the buffer to prevent anyone
263 * else from using and possibly modifying it while the IO is in
264 * progress.
266 * The function returns a pointer to the buffer_heads to be used for IO.
268 * We assume that the journal has already been locked in this function.
270 * Return value:
271 * <0: Error
272 * >=0: Finished OK
274 * On success:
275 * Bit 0 set == escape performed on the data
276 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
279 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
280 struct journal_head *jh_in,
281 struct journal_head **jh_out,
282 unsigned long long blocknr)
284 int need_copy_out = 0;
285 int done_copy_out = 0;
286 int do_escape = 0;
287 char *mapped_data;
288 struct buffer_head *new_bh;
289 struct journal_head *new_jh;
290 struct page *new_page;
291 unsigned int new_offset;
292 struct buffer_head *bh_in = jh2bh(jh_in);
295 * The buffer really shouldn't be locked: only the current committing
296 * transaction is allowed to write it, so nobody else is allowed
297 * to do any IO.
299 * akpm: except if we're journalling data, and write() output is
300 * also part of a shared mapping, and another thread has
301 * decided to launch a writepage() against this buffer.
303 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
305 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
308 * If a new transaction has already done a buffer copy-out, then
309 * we use that version of the data for the commit.
311 jbd_lock_bh_state(bh_in);
312 repeat:
313 if (jh_in->b_frozen_data) {
314 done_copy_out = 1;
315 new_page = virt_to_page(jh_in->b_frozen_data);
316 new_offset = offset_in_page(jh_in->b_frozen_data);
317 } else {
318 new_page = jh2bh(jh_in)->b_page;
319 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
322 mapped_data = kmap_atomic(new_page, KM_USER0);
324 * Check for escaping
326 if (*((__be32 *)(mapped_data + new_offset)) ==
327 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
328 need_copy_out = 1;
329 do_escape = 1;
331 kunmap_atomic(mapped_data, KM_USER0);
334 * Do we need to do a data copy?
336 if (need_copy_out && !done_copy_out) {
337 char *tmp;
339 jbd_unlock_bh_state(bh_in);
340 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
341 jbd_lock_bh_state(bh_in);
342 if (jh_in->b_frozen_data) {
343 jbd2_free(tmp, bh_in->b_size);
344 goto repeat;
347 jh_in->b_frozen_data = tmp;
348 mapped_data = kmap_atomic(new_page, KM_USER0);
349 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
350 kunmap_atomic(mapped_data, KM_USER0);
352 new_page = virt_to_page(tmp);
353 new_offset = offset_in_page(tmp);
354 done_copy_out = 1;
358 * Did we need to do an escaping? Now we've done all the
359 * copying, we can finally do so.
361 if (do_escape) {
362 mapped_data = kmap_atomic(new_page, KM_USER0);
363 *((unsigned int *)(mapped_data + new_offset)) = 0;
364 kunmap_atomic(mapped_data, KM_USER0);
367 /* keep subsequent assertions sane */
368 new_bh->b_state = 0;
369 init_buffer(new_bh, NULL, NULL);
370 atomic_set(&new_bh->b_count, 1);
371 jbd_unlock_bh_state(bh_in);
373 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
375 set_bh_page(new_bh, new_page, new_offset);
376 new_jh->b_transaction = NULL;
377 new_bh->b_size = jh2bh(jh_in)->b_size;
378 new_bh->b_bdev = transaction->t_journal->j_dev;
379 new_bh->b_blocknr = blocknr;
380 set_buffer_mapped(new_bh);
381 set_buffer_dirty(new_bh);
383 *jh_out = new_jh;
386 * The to-be-written buffer needs to get moved to the io queue,
387 * and the original buffer whose contents we are shadowing or
388 * copying is moved to the transaction's shadow queue.
390 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
391 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
392 JBUFFER_TRACE(new_jh, "file as BJ_IO");
393 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
395 return do_escape | (done_copy_out << 1);
399 * Allocation code for the journal file. Manage the space left in the
400 * journal, so that we can begin checkpointing when appropriate.
404 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
406 * Called with the journal already locked.
408 * Called under j_state_lock
411 int __jbd2_log_space_left(journal_t *journal)
413 int left = journal->j_free;
415 assert_spin_locked(&journal->j_state_lock);
418 * Be pessimistic here about the number of those free blocks which
419 * might be required for log descriptor control blocks.
422 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
424 left -= MIN_LOG_RESERVED_BLOCKS;
426 if (left <= 0)
427 return 0;
428 left -= (left >> 3);
429 return left;
433 * Called under j_state_lock. Returns true if a transaction was started.
435 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
438 * Are we already doing a recent enough commit?
440 if (!tid_geq(journal->j_commit_request, target)) {
442 * We want a new commit: OK, mark the request and wakup the
443 * commit thread. We do _not_ do the commit ourselves.
446 journal->j_commit_request = target;
447 jbd_debug(1, "JBD: requesting commit %d/%d\n",
448 journal->j_commit_request,
449 journal->j_commit_sequence);
450 wake_up(&journal->j_wait_commit);
451 return 1;
453 return 0;
456 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
458 int ret;
460 spin_lock(&journal->j_state_lock);
461 ret = __jbd2_log_start_commit(journal, tid);
462 spin_unlock(&journal->j_state_lock);
463 return ret;
467 * Force and wait upon a commit if the calling process is not within
468 * transaction. This is used for forcing out undo-protected data which contains
469 * bitmaps, when the fs is running out of space.
471 * We can only force the running transaction if we don't have an active handle;
472 * otherwise, we will deadlock.
474 * Returns true if a transaction was started.
476 int jbd2_journal_force_commit_nested(journal_t *journal)
478 transaction_t *transaction = NULL;
479 tid_t tid;
481 spin_lock(&journal->j_state_lock);
482 if (journal->j_running_transaction && !current->journal_info) {
483 transaction = journal->j_running_transaction;
484 __jbd2_log_start_commit(journal, transaction->t_tid);
485 } else if (journal->j_committing_transaction)
486 transaction = journal->j_committing_transaction;
488 if (!transaction) {
489 spin_unlock(&journal->j_state_lock);
490 return 0; /* Nothing to retry */
493 tid = transaction->t_tid;
494 spin_unlock(&journal->j_state_lock);
495 jbd2_log_wait_commit(journal, tid);
496 return 1;
500 * Start a commit of the current running transaction (if any). Returns true
501 * if a transaction was started, and fills its tid in at *ptid
503 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
505 int ret = 0;
507 spin_lock(&journal->j_state_lock);
508 if (journal->j_running_transaction) {
509 tid_t tid = journal->j_running_transaction->t_tid;
511 ret = __jbd2_log_start_commit(journal, tid);
512 if (ret && ptid)
513 *ptid = tid;
514 } else if (journal->j_committing_transaction && ptid) {
516 * If ext3_write_super() recently started a commit, then we
517 * have to wait for completion of that transaction
519 *ptid = journal->j_committing_transaction->t_tid;
520 ret = 1;
522 spin_unlock(&journal->j_state_lock);
523 return ret;
527 * Wait for a specified commit to complete.
528 * The caller may not hold the journal lock.
530 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
532 int err = 0;
534 #ifdef CONFIG_JBD2_DEBUG
535 spin_lock(&journal->j_state_lock);
536 if (!tid_geq(journal->j_commit_request, tid)) {
537 printk(KERN_EMERG
538 "%s: error: j_commit_request=%d, tid=%d\n",
539 __func__, journal->j_commit_request, tid);
541 spin_unlock(&journal->j_state_lock);
542 #endif
543 spin_lock(&journal->j_state_lock);
544 while (tid_gt(tid, journal->j_commit_sequence)) {
545 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
546 tid, journal->j_commit_sequence);
547 wake_up(&journal->j_wait_commit);
548 spin_unlock(&journal->j_state_lock);
549 wait_event(journal->j_wait_done_commit,
550 !tid_gt(tid, journal->j_commit_sequence));
551 spin_lock(&journal->j_state_lock);
553 spin_unlock(&journal->j_state_lock);
555 if (unlikely(is_journal_aborted(journal))) {
556 printk(KERN_EMERG "journal commit I/O error\n");
557 err = -EIO;
559 return err;
563 * Log buffer allocation routines:
566 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
568 unsigned long blocknr;
570 spin_lock(&journal->j_state_lock);
571 J_ASSERT(journal->j_free > 1);
573 blocknr = journal->j_head;
574 journal->j_head++;
575 journal->j_free--;
576 if (journal->j_head == journal->j_last)
577 journal->j_head = journal->j_first;
578 spin_unlock(&journal->j_state_lock);
579 return jbd2_journal_bmap(journal, blocknr, retp);
583 * Conversion of logical to physical block numbers for the journal
585 * On external journals the journal blocks are identity-mapped, so
586 * this is a no-op. If needed, we can use j_blk_offset - everything is
587 * ready.
589 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
590 unsigned long long *retp)
592 int err = 0;
593 unsigned long long ret;
595 if (journal->j_inode) {
596 ret = bmap(journal->j_inode, blocknr);
597 if (ret)
598 *retp = ret;
599 else {
600 char b[BDEVNAME_SIZE];
602 printk(KERN_ALERT "%s: journal block not found "
603 "at offset %lu on %s\n",
604 __func__,
605 blocknr,
606 bdevname(journal->j_dev, b));
607 err = -EIO;
608 __journal_abort_soft(journal, err);
610 } else {
611 *retp = blocknr; /* +journal->j_blk_offset */
613 return err;
617 * We play buffer_head aliasing tricks to write data/metadata blocks to
618 * the journal without copying their contents, but for journal
619 * descriptor blocks we do need to generate bona fide buffers.
621 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
622 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
623 * But we don't bother doing that, so there will be coherency problems with
624 * mmaps of blockdevs which hold live JBD-controlled filesystems.
626 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
628 struct buffer_head *bh;
629 unsigned long long blocknr;
630 int err;
632 err = jbd2_journal_next_log_block(journal, &blocknr);
634 if (err)
635 return NULL;
637 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
638 lock_buffer(bh);
639 memset(bh->b_data, 0, journal->j_blocksize);
640 set_buffer_uptodate(bh);
641 unlock_buffer(bh);
642 BUFFER_TRACE(bh, "return this buffer");
643 return jbd2_journal_add_journal_head(bh);
646 struct jbd2_stats_proc_session {
647 journal_t *journal;
648 struct transaction_stats_s *stats;
649 int start;
650 int max;
653 static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
654 struct transaction_stats_s *ts,
655 int first)
657 if (ts == s->stats + s->max)
658 ts = s->stats;
659 if (!first && ts == s->stats + s->start)
660 return NULL;
661 while (ts->ts_type == 0) {
662 ts++;
663 if (ts == s->stats + s->max)
664 ts = s->stats;
665 if (ts == s->stats + s->start)
666 return NULL;
668 return ts;
672 static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
674 struct jbd2_stats_proc_session *s = seq->private;
675 struct transaction_stats_s *ts;
676 int l = *pos;
678 if (l == 0)
679 return SEQ_START_TOKEN;
680 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
681 if (!ts)
682 return NULL;
683 l--;
684 while (l) {
685 ts = jbd2_history_skip_empty(s, ++ts, 0);
686 if (!ts)
687 break;
688 l--;
690 return ts;
693 static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
695 struct jbd2_stats_proc_session *s = seq->private;
696 struct transaction_stats_s *ts = v;
698 ++*pos;
699 if (v == SEQ_START_TOKEN)
700 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
701 else
702 return jbd2_history_skip_empty(s, ++ts, 0);
705 static int jbd2_seq_history_show(struct seq_file *seq, void *v)
707 struct transaction_stats_s *ts = v;
708 if (v == SEQ_START_TOKEN) {
709 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
710 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
711 "wait", "run", "lock", "flush", "log", "hndls",
712 "block", "inlog", "ctime", "write", "drop",
713 "close");
714 return 0;
716 if (ts->ts_type == JBD2_STATS_RUN)
717 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
718 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
719 jiffies_to_msecs(ts->u.run.rs_wait),
720 jiffies_to_msecs(ts->u.run.rs_running),
721 jiffies_to_msecs(ts->u.run.rs_locked),
722 jiffies_to_msecs(ts->u.run.rs_flushing),
723 jiffies_to_msecs(ts->u.run.rs_logging),
724 ts->u.run.rs_handle_count,
725 ts->u.run.rs_blocks,
726 ts->u.run.rs_blocks_logged);
727 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
728 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
729 "C", ts->ts_tid, " ",
730 jiffies_to_msecs(ts->u.chp.cs_chp_time),
731 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
732 ts->u.chp.cs_forced_to_close);
733 else
734 J_ASSERT(0);
735 return 0;
738 static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
742 static struct seq_operations jbd2_seq_history_ops = {
743 .start = jbd2_seq_history_start,
744 .next = jbd2_seq_history_next,
745 .stop = jbd2_seq_history_stop,
746 .show = jbd2_seq_history_show,
749 static int jbd2_seq_history_open(struct inode *inode, struct file *file)
751 journal_t *journal = PDE(inode)->data;
752 struct jbd2_stats_proc_session *s;
753 int rc, size;
755 s = kmalloc(sizeof(*s), GFP_KERNEL);
756 if (s == NULL)
757 return -ENOMEM;
758 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
759 s->stats = kmalloc(size, GFP_KERNEL);
760 if (s->stats == NULL) {
761 kfree(s);
762 return -ENOMEM;
764 spin_lock(&journal->j_history_lock);
765 memcpy(s->stats, journal->j_history, size);
766 s->max = journal->j_history_max;
767 s->start = journal->j_history_cur % s->max;
768 spin_unlock(&journal->j_history_lock);
770 rc = seq_open(file, &jbd2_seq_history_ops);
771 if (rc == 0) {
772 struct seq_file *m = file->private_data;
773 m->private = s;
774 } else {
775 kfree(s->stats);
776 kfree(s);
778 return rc;
782 static int jbd2_seq_history_release(struct inode *inode, struct file *file)
784 struct seq_file *seq = file->private_data;
785 struct jbd2_stats_proc_session *s = seq->private;
787 kfree(s->stats);
788 kfree(s);
789 return seq_release(inode, file);
792 static struct file_operations jbd2_seq_history_fops = {
793 .owner = THIS_MODULE,
794 .open = jbd2_seq_history_open,
795 .read = seq_read,
796 .llseek = seq_lseek,
797 .release = jbd2_seq_history_release,
800 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
802 return *pos ? NULL : SEQ_START_TOKEN;
805 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
807 return NULL;
810 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
812 struct jbd2_stats_proc_session *s = seq->private;
814 if (v != SEQ_START_TOKEN)
815 return 0;
816 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
817 s->stats->ts_tid,
818 s->journal->j_max_transaction_buffers);
819 if (s->stats->ts_tid == 0)
820 return 0;
821 seq_printf(seq, "average: \n %ums waiting for transaction\n",
822 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
823 seq_printf(seq, " %ums running transaction\n",
824 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
825 seq_printf(seq, " %ums transaction was being locked\n",
826 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
827 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
828 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
829 seq_printf(seq, " %ums logging transaction\n",
830 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
831 seq_printf(seq, " %lu handles per transaction\n",
832 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
833 seq_printf(seq, " %lu blocks per transaction\n",
834 s->stats->u.run.rs_blocks / s->stats->ts_tid);
835 seq_printf(seq, " %lu logged blocks per transaction\n",
836 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
837 return 0;
840 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
844 static struct seq_operations jbd2_seq_info_ops = {
845 .start = jbd2_seq_info_start,
846 .next = jbd2_seq_info_next,
847 .stop = jbd2_seq_info_stop,
848 .show = jbd2_seq_info_show,
851 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
853 journal_t *journal = PDE(inode)->data;
854 struct jbd2_stats_proc_session *s;
855 int rc, size;
857 s = kmalloc(sizeof(*s), GFP_KERNEL);
858 if (s == NULL)
859 return -ENOMEM;
860 size = sizeof(struct transaction_stats_s);
861 s->stats = kmalloc(size, GFP_KERNEL);
862 if (s->stats == NULL) {
863 kfree(s);
864 return -ENOMEM;
866 spin_lock(&journal->j_history_lock);
867 memcpy(s->stats, &journal->j_stats, size);
868 s->journal = journal;
869 spin_unlock(&journal->j_history_lock);
871 rc = seq_open(file, &jbd2_seq_info_ops);
872 if (rc == 0) {
873 struct seq_file *m = file->private_data;
874 m->private = s;
875 } else {
876 kfree(s->stats);
877 kfree(s);
879 return rc;
883 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
885 struct seq_file *seq = file->private_data;
886 struct jbd2_stats_proc_session *s = seq->private;
887 kfree(s->stats);
888 kfree(s);
889 return seq_release(inode, file);
892 static struct file_operations jbd2_seq_info_fops = {
893 .owner = THIS_MODULE,
894 .open = jbd2_seq_info_open,
895 .read = seq_read,
896 .llseek = seq_lseek,
897 .release = jbd2_seq_info_release,
900 static struct proc_dir_entry *proc_jbd2_stats;
902 static void jbd2_stats_proc_init(journal_t *journal)
904 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
905 if (journal->j_proc_entry) {
906 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
907 &jbd2_seq_history_fops, journal);
908 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
909 &jbd2_seq_info_fops, journal);
913 static void jbd2_stats_proc_exit(journal_t *journal)
915 remove_proc_entry("info", journal->j_proc_entry);
916 remove_proc_entry("history", journal->j_proc_entry);
917 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
920 static void journal_init_stats(journal_t *journal)
922 int size;
924 if (!proc_jbd2_stats)
925 return;
927 journal->j_history_max = 100;
928 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
929 journal->j_history = kzalloc(size, GFP_KERNEL);
930 if (!journal->j_history) {
931 journal->j_history_max = 0;
932 return;
934 spin_lock_init(&journal->j_history_lock);
938 * Management for journal control blocks: functions to create and
939 * destroy journal_t structures, and to initialise and read existing
940 * journal blocks from disk. */
942 /* First: create and setup a journal_t object in memory. We initialise
943 * very few fields yet: that has to wait until we have created the
944 * journal structures from from scratch, or loaded them from disk. */
946 static journal_t * journal_init_common (void)
948 journal_t *journal;
949 int err;
951 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
952 if (!journal)
953 goto fail;
955 init_waitqueue_head(&journal->j_wait_transaction_locked);
956 init_waitqueue_head(&journal->j_wait_logspace);
957 init_waitqueue_head(&journal->j_wait_done_commit);
958 init_waitqueue_head(&journal->j_wait_checkpoint);
959 init_waitqueue_head(&journal->j_wait_commit);
960 init_waitqueue_head(&journal->j_wait_updates);
961 mutex_init(&journal->j_barrier);
962 mutex_init(&journal->j_checkpoint_mutex);
963 spin_lock_init(&journal->j_revoke_lock);
964 spin_lock_init(&journal->j_list_lock);
965 spin_lock_init(&journal->j_state_lock);
967 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
969 /* The journal is marked for error until we succeed with recovery! */
970 journal->j_flags = JBD2_ABORT;
972 /* Set up a default-sized revoke table for the new mount. */
973 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
974 if (err) {
975 kfree(journal);
976 goto fail;
979 journal_init_stats(journal);
981 return journal;
982 fail:
983 return NULL;
986 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
988 * Create a journal structure assigned some fixed set of disk blocks to
989 * the journal. We don't actually touch those disk blocks yet, but we
990 * need to set up all of the mapping information to tell the journaling
991 * system where the journal blocks are.
996 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
997 * @bdev: Block device on which to create the journal
998 * @fs_dev: Device which hold journalled filesystem for this journal.
999 * @start: Block nr Start of journal.
1000 * @len: Length of the journal in blocks.
1001 * @blocksize: blocksize of journalling device
1003 * Returns: a newly created journal_t *
1005 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1006 * range of blocks on an arbitrary block device.
1009 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1010 struct block_device *fs_dev,
1011 unsigned long long start, int len, int blocksize)
1013 journal_t *journal = journal_init_common();
1014 struct buffer_head *bh;
1015 char *p;
1016 int n;
1018 if (!journal)
1019 return NULL;
1021 /* journal descriptor can store up to n blocks -bzzz */
1022 journal->j_blocksize = blocksize;
1023 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1024 journal->j_wbufsize = n;
1025 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1026 if (!journal->j_wbuf) {
1027 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1028 __func__);
1029 kfree(journal);
1030 journal = NULL;
1031 goto out;
1033 journal->j_dev = bdev;
1034 journal->j_fs_dev = fs_dev;
1035 journal->j_blk_offset = start;
1036 journal->j_maxlen = len;
1037 bdevname(journal->j_dev, journal->j_devname);
1038 p = journal->j_devname;
1039 while ((p = strchr(p, '/')))
1040 *p = '!';
1041 jbd2_stats_proc_init(journal);
1043 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1044 J_ASSERT(bh != NULL);
1045 journal->j_sb_buffer = bh;
1046 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1047 out:
1048 return journal;
1052 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1053 * @inode: An inode to create the journal in
1055 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1056 * the journal. The inode must exist already, must support bmap() and
1057 * must have all data blocks preallocated.
1059 journal_t * jbd2_journal_init_inode (struct inode *inode)
1061 struct buffer_head *bh;
1062 journal_t *journal = journal_init_common();
1063 char *p;
1064 int err;
1065 int n;
1066 unsigned long long blocknr;
1068 if (!journal)
1069 return NULL;
1071 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1072 journal->j_inode = inode;
1073 bdevname(journal->j_dev, journal->j_devname);
1074 p = journal->j_devname;
1075 while ((p = strchr(p, '/')))
1076 *p = '!';
1077 p = journal->j_devname + strlen(journal->j_devname);
1078 sprintf(p, ":%lu", journal->j_inode->i_ino);
1079 jbd_debug(1,
1080 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1081 journal, inode->i_sb->s_id, inode->i_ino,
1082 (long long) inode->i_size,
1083 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1085 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1086 journal->j_blocksize = inode->i_sb->s_blocksize;
1087 jbd2_stats_proc_init(journal);
1089 /* journal descriptor can store up to n blocks -bzzz */
1090 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1091 journal->j_wbufsize = n;
1092 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1093 if (!journal->j_wbuf) {
1094 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1095 __func__);
1096 kfree(journal);
1097 return NULL;
1100 err = jbd2_journal_bmap(journal, 0, &blocknr);
1101 /* If that failed, give up */
1102 if (err) {
1103 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
1104 __func__);
1105 kfree(journal);
1106 return NULL;
1109 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1110 J_ASSERT(bh != NULL);
1111 journal->j_sb_buffer = bh;
1112 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1114 return journal;
1118 * If the journal init or create aborts, we need to mark the journal
1119 * superblock as being NULL to prevent the journal destroy from writing
1120 * back a bogus superblock.
1122 static void journal_fail_superblock (journal_t *journal)
1124 struct buffer_head *bh = journal->j_sb_buffer;
1125 brelse(bh);
1126 journal->j_sb_buffer = NULL;
1130 * Given a journal_t structure, initialise the various fields for
1131 * startup of a new journaling session. We use this both when creating
1132 * a journal, and after recovering an old journal to reset it for
1133 * subsequent use.
1136 static int journal_reset(journal_t *journal)
1138 journal_superblock_t *sb = journal->j_superblock;
1139 unsigned long long first, last;
1141 first = be32_to_cpu(sb->s_first);
1142 last = be32_to_cpu(sb->s_maxlen);
1144 journal->j_first = first;
1145 journal->j_last = last;
1147 journal->j_head = first;
1148 journal->j_tail = first;
1149 journal->j_free = last - first;
1151 journal->j_tail_sequence = journal->j_transaction_sequence;
1152 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1153 journal->j_commit_request = journal->j_commit_sequence;
1155 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1157 /* Add the dynamic fields and write it to disk. */
1158 jbd2_journal_update_superblock(journal, 1);
1159 return jbd2_journal_start_thread(journal);
1163 * int jbd2_journal_create() - Initialise the new journal file
1164 * @journal: Journal to create. This structure must have been initialised
1166 * Given a journal_t structure which tells us which disk blocks we can
1167 * use, create a new journal superblock and initialise all of the
1168 * journal fields from scratch.
1170 int jbd2_journal_create(journal_t *journal)
1172 unsigned long long blocknr;
1173 struct buffer_head *bh;
1174 journal_superblock_t *sb;
1175 int i, err;
1177 if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
1178 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
1179 journal->j_maxlen);
1180 journal_fail_superblock(journal);
1181 return -EINVAL;
1184 if (journal->j_inode == NULL) {
1186 * We don't know what block to start at!
1188 printk(KERN_EMERG
1189 "%s: creation of journal on external device!\n",
1190 __func__);
1191 BUG();
1194 /* Zero out the entire journal on disk. We cannot afford to
1195 have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
1196 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1197 for (i = 0; i < journal->j_maxlen; i++) {
1198 err = jbd2_journal_bmap(journal, i, &blocknr);
1199 if (err)
1200 return err;
1201 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1202 lock_buffer(bh);
1203 memset (bh->b_data, 0, journal->j_blocksize);
1204 BUFFER_TRACE(bh, "marking dirty");
1205 mark_buffer_dirty(bh);
1206 BUFFER_TRACE(bh, "marking uptodate");
1207 set_buffer_uptodate(bh);
1208 unlock_buffer(bh);
1209 __brelse(bh);
1212 sync_blockdev(journal->j_dev);
1213 jbd_debug(1, "JBD: journal cleared.\n");
1215 /* OK, fill in the initial static fields in the new superblock */
1216 sb = journal->j_superblock;
1218 sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
1219 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1221 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1222 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1223 sb->s_first = cpu_to_be32(1);
1225 journal->j_transaction_sequence = 1;
1227 journal->j_flags &= ~JBD2_ABORT;
1228 journal->j_format_version = 2;
1230 return journal_reset(journal);
1234 * void jbd2_journal_update_superblock() - Update journal sb on disk.
1235 * @journal: The journal to update.
1236 * @wait: Set to '0' if you don't want to wait for IO completion.
1238 * Update a journal's dynamic superblock fields and write it to disk,
1239 * optionally waiting for the IO to complete.
1241 void jbd2_journal_update_superblock(journal_t *journal, int wait)
1243 journal_superblock_t *sb = journal->j_superblock;
1244 struct buffer_head *bh = journal->j_sb_buffer;
1247 * As a special case, if the on-disk copy is already marked as needing
1248 * no recovery (s_start == 0) and there are no outstanding transactions
1249 * in the filesystem, then we can safely defer the superblock update
1250 * until the next commit by setting JBD2_FLUSHED. This avoids
1251 * attempting a write to a potential-readonly device.
1253 if (sb->s_start == 0 && journal->j_tail_sequence ==
1254 journal->j_transaction_sequence) {
1255 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1256 "(start %ld, seq %d, errno %d)\n",
1257 journal->j_tail, journal->j_tail_sequence,
1258 journal->j_errno);
1259 goto out;
1262 spin_lock(&journal->j_state_lock);
1263 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1264 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1266 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1267 sb->s_start = cpu_to_be32(journal->j_tail);
1268 sb->s_errno = cpu_to_be32(journal->j_errno);
1269 spin_unlock(&journal->j_state_lock);
1271 BUFFER_TRACE(bh, "marking dirty");
1272 mark_buffer_dirty(bh);
1273 if (wait)
1274 sync_dirty_buffer(bh);
1275 else
1276 ll_rw_block(SWRITE, 1, &bh);
1278 out:
1279 /* If we have just flushed the log (by marking s_start==0), then
1280 * any future commit will have to be careful to update the
1281 * superblock again to re-record the true start of the log. */
1283 spin_lock(&journal->j_state_lock);
1284 if (sb->s_start)
1285 journal->j_flags &= ~JBD2_FLUSHED;
1286 else
1287 journal->j_flags |= JBD2_FLUSHED;
1288 spin_unlock(&journal->j_state_lock);
1292 * Read the superblock for a given journal, performing initial
1293 * validation of the format.
1296 static int journal_get_superblock(journal_t *journal)
1298 struct buffer_head *bh;
1299 journal_superblock_t *sb;
1300 int err = -EIO;
1302 bh = journal->j_sb_buffer;
1304 J_ASSERT(bh != NULL);
1305 if (!buffer_uptodate(bh)) {
1306 ll_rw_block(READ, 1, &bh);
1307 wait_on_buffer(bh);
1308 if (!buffer_uptodate(bh)) {
1309 printk (KERN_ERR
1310 "JBD: IO error reading journal superblock\n");
1311 goto out;
1315 sb = journal->j_superblock;
1317 err = -EINVAL;
1319 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1320 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1321 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1322 goto out;
1325 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1326 case JBD2_SUPERBLOCK_V1:
1327 journal->j_format_version = 1;
1328 break;
1329 case JBD2_SUPERBLOCK_V2:
1330 journal->j_format_version = 2;
1331 break;
1332 default:
1333 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1334 goto out;
1337 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1338 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1339 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1340 printk (KERN_WARNING "JBD: journal file too short\n");
1341 goto out;
1344 return 0;
1346 out:
1347 journal_fail_superblock(journal);
1348 return err;
1352 * Load the on-disk journal superblock and read the key fields into the
1353 * journal_t.
1356 static int load_superblock(journal_t *journal)
1358 int err;
1359 journal_superblock_t *sb;
1361 err = journal_get_superblock(journal);
1362 if (err)
1363 return err;
1365 sb = journal->j_superblock;
1367 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1368 journal->j_tail = be32_to_cpu(sb->s_start);
1369 journal->j_first = be32_to_cpu(sb->s_first);
1370 journal->j_last = be32_to_cpu(sb->s_maxlen);
1371 journal->j_errno = be32_to_cpu(sb->s_errno);
1373 return 0;
1378 * int jbd2_journal_load() - Read journal from disk.
1379 * @journal: Journal to act on.
1381 * Given a journal_t structure which tells us which disk blocks contain
1382 * a journal, read the journal from disk to initialise the in-memory
1383 * structures.
1385 int jbd2_journal_load(journal_t *journal)
1387 int err;
1388 journal_superblock_t *sb;
1390 err = load_superblock(journal);
1391 if (err)
1392 return err;
1394 sb = journal->j_superblock;
1395 /* If this is a V2 superblock, then we have to check the
1396 * features flags on it. */
1398 if (journal->j_format_version >= 2) {
1399 if ((sb->s_feature_ro_compat &
1400 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1401 (sb->s_feature_incompat &
1402 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1403 printk (KERN_WARNING
1404 "JBD: Unrecognised features on journal\n");
1405 return -EINVAL;
1409 /* Let the recovery code check whether it needs to recover any
1410 * data from the journal. */
1411 if (jbd2_journal_recover(journal))
1412 goto recovery_error;
1414 /* OK, we've finished with the dynamic journal bits:
1415 * reinitialise the dynamic contents of the superblock in memory
1416 * and reset them on disk. */
1417 if (journal_reset(journal))
1418 goto recovery_error;
1420 journal->j_flags &= ~JBD2_ABORT;
1421 journal->j_flags |= JBD2_LOADED;
1422 return 0;
1424 recovery_error:
1425 printk (KERN_WARNING "JBD: recovery failed\n");
1426 return -EIO;
1430 * void jbd2_journal_destroy() - Release a journal_t structure.
1431 * @journal: Journal to act on.
1433 * Release a journal_t structure once it is no longer in use by the
1434 * journaled object.
1436 void jbd2_journal_destroy(journal_t *journal)
1438 /* Wait for the commit thread to wake up and die. */
1439 journal_kill_thread(journal);
1441 /* Force a final log commit */
1442 if (journal->j_running_transaction)
1443 jbd2_journal_commit_transaction(journal);
1445 /* Force any old transactions to disk */
1447 /* Totally anal locking here... */
1448 spin_lock(&journal->j_list_lock);
1449 while (journal->j_checkpoint_transactions != NULL) {
1450 spin_unlock(&journal->j_list_lock);
1451 jbd2_log_do_checkpoint(journal);
1452 spin_lock(&journal->j_list_lock);
1455 J_ASSERT(journal->j_running_transaction == NULL);
1456 J_ASSERT(journal->j_committing_transaction == NULL);
1457 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1458 spin_unlock(&journal->j_list_lock);
1460 /* We can now mark the journal as empty. */
1461 journal->j_tail = 0;
1462 journal->j_tail_sequence = ++journal->j_transaction_sequence;
1463 if (journal->j_sb_buffer) {
1464 jbd2_journal_update_superblock(journal, 1);
1465 brelse(journal->j_sb_buffer);
1468 if (journal->j_proc_entry)
1469 jbd2_stats_proc_exit(journal);
1470 if (journal->j_inode)
1471 iput(journal->j_inode);
1472 if (journal->j_revoke)
1473 jbd2_journal_destroy_revoke(journal);
1474 kfree(journal->j_wbuf);
1475 kfree(journal);
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)
1496 return 1;
1497 if (journal->j_format_version == 1)
1498 return 0;
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))
1505 return 1;
1507 return 0;
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)
1527 return 1;
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)
1536 return 0;
1538 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1539 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1540 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1541 return 1;
1543 return 0;
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))
1564 return 1;
1566 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1567 return 0;
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);
1578 return 1;
1582 * jbd2_journal_clear_features () - Clear a given journal feature in the
1583 * superblock
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
1590 * superblock.
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;
1618 int err;
1620 err = journal_get_superblock(journal);
1621 if (err)
1622 return err;
1624 sb = journal->j_superblock;
1626 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1627 case JBD2_SUPERBLOCK_V2:
1628 return 0;
1629 case JBD2_SUPERBLOCK_V1:
1630 return journal_convert_superblock_v1(journal, sb);
1631 default:
1632 break;
1634 return -EINVAL;
1637 static int journal_convert_superblock_v1(journal_t *journal,
1638 journal_superblock_t *sb)
1640 int offset, blocksize;
1641 struct buffer_head *bh;
1643 printk(KERN_WARNING
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);
1659 return 0;
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)
1674 int err = 0;
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... */
1688 if (transaction) {
1689 tid_t tid = transaction->t_tid;
1691 spin_unlock(&journal->j_state_lock);
1692 jbd2_log_wait_commit(journal, tid);
1693 } else {
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 err = jbd2_log_do_checkpoint(journal);
1702 spin_lock(&journal->j_list_lock);
1704 spin_unlock(&journal->j_list_lock);
1705 jbd2_cleanup_journal_tail(journal);
1707 /* Finally, mark the journal as really needing no recovery.
1708 * This sets s_start==0 in the underlying superblock, which is
1709 * the magic code for a fully-recovered superblock. Any future
1710 * commits of data to the journal will restore the current
1711 * s_start value. */
1712 spin_lock(&journal->j_state_lock);
1713 old_tail = journal->j_tail;
1714 journal->j_tail = 0;
1715 spin_unlock(&journal->j_state_lock);
1716 jbd2_journal_update_superblock(journal, 1);
1717 spin_lock(&journal->j_state_lock);
1718 journal->j_tail = old_tail;
1720 J_ASSERT(!journal->j_running_transaction);
1721 J_ASSERT(!journal->j_committing_transaction);
1722 J_ASSERT(!journal->j_checkpoint_transactions);
1723 J_ASSERT(journal->j_head == journal->j_tail);
1724 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1725 spin_unlock(&journal->j_state_lock);
1726 return err;
1730 * int jbd2_journal_wipe() - Wipe journal contents
1731 * @journal: Journal to act on.
1732 * @write: flag (see below)
1734 * Wipe out all of the contents of a journal, safely. This will produce
1735 * a warning if the journal contains any valid recovery information.
1736 * Must be called between journal_init_*() and jbd2_journal_load().
1738 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1739 * we merely suppress recovery.
1742 int jbd2_journal_wipe(journal_t *journal, int write)
1744 journal_superblock_t *sb;
1745 int err = 0;
1747 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1749 err = load_superblock(journal);
1750 if (err)
1751 return err;
1753 sb = journal->j_superblock;
1755 if (!journal->j_tail)
1756 goto no_recovery;
1758 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1759 write ? "Clearing" : "Ignoring");
1761 err = jbd2_journal_skip_recovery(journal);
1762 if (write)
1763 jbd2_journal_update_superblock(journal, 1);
1765 no_recovery:
1766 return err;
1770 * journal_dev_name: format a character string to describe on what
1771 * device this journal is present.
1774 static const char *journal_dev_name(journal_t *journal, char *buffer)
1776 struct block_device *bdev;
1778 if (journal->j_inode)
1779 bdev = journal->j_inode->i_sb->s_bdev;
1780 else
1781 bdev = journal->j_dev;
1783 return bdevname(bdev, buffer);
1787 * Journal abort has very specific semantics, which we describe
1788 * for journal abort.
1790 * Two internal function, which provide abort to te jbd layer
1791 * itself are here.
1795 * Quick version for internal journal use (doesn't lock the journal).
1796 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1797 * and don't attempt to make any other journal updates.
1799 void __jbd2_journal_abort_hard(journal_t *journal)
1801 transaction_t *transaction;
1802 char b[BDEVNAME_SIZE];
1804 if (journal->j_flags & JBD2_ABORT)
1805 return;
1807 printk(KERN_ERR "Aborting journal on device %s.\n",
1808 journal_dev_name(journal, b));
1810 spin_lock(&journal->j_state_lock);
1811 journal->j_flags |= JBD2_ABORT;
1812 transaction = journal->j_running_transaction;
1813 if (transaction)
1814 __jbd2_log_start_commit(journal, transaction->t_tid);
1815 spin_unlock(&journal->j_state_lock);
1818 /* Soft abort: record the abort error status in the journal superblock,
1819 * but don't do any other IO. */
1820 static void __journal_abort_soft (journal_t *journal, int errno)
1822 if (journal->j_flags & JBD2_ABORT)
1823 return;
1825 if (!journal->j_errno)
1826 journal->j_errno = errno;
1828 __jbd2_journal_abort_hard(journal);
1830 if (errno)
1831 jbd2_journal_update_superblock(journal, 1);
1835 * void jbd2_journal_abort () - Shutdown the journal immediately.
1836 * @journal: the journal to shutdown.
1837 * @errno: an error number to record in the journal indicating
1838 * the reason for the shutdown.
1840 * Perform a complete, immediate shutdown of the ENTIRE
1841 * journal (not of a single transaction). This operation cannot be
1842 * undone without closing and reopening the journal.
1844 * The jbd2_journal_abort function is intended to support higher level error
1845 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1846 * mode.
1848 * Journal abort has very specific semantics. Any existing dirty,
1849 * unjournaled buffers in the main filesystem will still be written to
1850 * disk by bdflush, but the journaling mechanism will be suspended
1851 * immediately and no further transaction commits will be honoured.
1853 * Any dirty, journaled buffers will be written back to disk without
1854 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1855 * filesystem, but we _do_ attempt to leave as much data as possible
1856 * behind for fsck to use for cleanup.
1858 * Any attempt to get a new transaction handle on a journal which is in
1859 * ABORT state will just result in an -EROFS error return. A
1860 * jbd2_journal_stop on an existing handle will return -EIO if we have
1861 * entered abort state during the update.
1863 * Recursive transactions are not disturbed by journal abort until the
1864 * final jbd2_journal_stop, which will receive the -EIO error.
1866 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
1867 * which will be recorded (if possible) in the journal superblock. This
1868 * allows a client to record failure conditions in the middle of a
1869 * transaction without having to complete the transaction to record the
1870 * failure to disk. ext3_error, for example, now uses this
1871 * functionality.
1873 * Errors which originate from within the journaling layer will NOT
1874 * supply an errno; a null errno implies that absolutely no further
1875 * writes are done to the journal (unless there are any already in
1876 * progress).
1880 void jbd2_journal_abort(journal_t *journal, int errno)
1882 __journal_abort_soft(journal, errno);
1886 * int jbd2_journal_errno () - returns the journal's error state.
1887 * @journal: journal to examine.
1889 * This is the errno numbet set with jbd2_journal_abort(), the last
1890 * time the journal was mounted - if the journal was stopped
1891 * without calling abort this will be 0.
1893 * If the journal has been aborted on this mount time -EROFS will
1894 * be returned.
1896 int jbd2_journal_errno(journal_t *journal)
1898 int err;
1900 spin_lock(&journal->j_state_lock);
1901 if (journal->j_flags & JBD2_ABORT)
1902 err = -EROFS;
1903 else
1904 err = journal->j_errno;
1905 spin_unlock(&journal->j_state_lock);
1906 return err;
1910 * int jbd2_journal_clear_err () - clears the journal's error state
1911 * @journal: journal to act on.
1913 * An error must be cleared or Acked to take a FS out of readonly
1914 * mode.
1916 int jbd2_journal_clear_err(journal_t *journal)
1918 int err = 0;
1920 spin_lock(&journal->j_state_lock);
1921 if (journal->j_flags & JBD2_ABORT)
1922 err = -EROFS;
1923 else
1924 journal->j_errno = 0;
1925 spin_unlock(&journal->j_state_lock);
1926 return err;
1930 * void jbd2_journal_ack_err() - Ack journal err.
1931 * @journal: journal to act on.
1933 * An error must be cleared or Acked to take a FS out of readonly
1934 * mode.
1936 void jbd2_journal_ack_err(journal_t *journal)
1938 spin_lock(&journal->j_state_lock);
1939 if (journal->j_errno)
1940 journal->j_flags |= JBD2_ACK_ERR;
1941 spin_unlock(&journal->j_state_lock);
1944 int jbd2_journal_blocks_per_page(struct inode *inode)
1946 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1950 * helper functions to deal with 32 or 64bit block numbers.
1952 size_t journal_tag_bytes(journal_t *journal)
1954 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1955 return JBD2_TAG_SIZE64;
1956 else
1957 return JBD2_TAG_SIZE32;
1961 * Journal_head storage management
1963 static struct kmem_cache *jbd2_journal_head_cache;
1964 #ifdef CONFIG_JBD2_DEBUG
1965 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1966 #endif
1968 static int journal_init_jbd2_journal_head_cache(void)
1970 int retval;
1972 J_ASSERT(jbd2_journal_head_cache == NULL);
1973 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
1974 sizeof(struct journal_head),
1975 0, /* offset */
1976 SLAB_TEMPORARY, /* flags */
1977 NULL); /* ctor */
1978 retval = 0;
1979 if (!jbd2_journal_head_cache) {
1980 retval = -ENOMEM;
1981 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1983 return retval;
1986 static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
1988 if (jbd2_journal_head_cache) {
1989 kmem_cache_destroy(jbd2_journal_head_cache);
1990 jbd2_journal_head_cache = NULL;
1995 * journal_head splicing and dicing
1997 static struct journal_head *journal_alloc_journal_head(void)
1999 struct journal_head *ret;
2000 static unsigned long last_warning;
2002 #ifdef CONFIG_JBD2_DEBUG
2003 atomic_inc(&nr_journal_heads);
2004 #endif
2005 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2006 if (!ret) {
2007 jbd_debug(1, "out of memory for journal_head\n");
2008 if (time_after(jiffies, last_warning + 5*HZ)) {
2009 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
2010 __func__);
2011 last_warning = jiffies;
2013 while (!ret) {
2014 yield();
2015 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2018 return ret;
2021 static void journal_free_journal_head(struct journal_head *jh)
2023 #ifdef CONFIG_JBD2_DEBUG
2024 atomic_dec(&nr_journal_heads);
2025 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2026 #endif
2027 kmem_cache_free(jbd2_journal_head_cache, jh);
2031 * A journal_head is attached to a buffer_head whenever JBD has an
2032 * interest in the buffer.
2034 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2035 * is set. This bit is tested in core kernel code where we need to take
2036 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2037 * there.
2039 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2041 * When a buffer has its BH_JBD bit set it is immune from being released by
2042 * core kernel code, mainly via ->b_count.
2044 * A journal_head may be detached from its buffer_head when the journal_head's
2045 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
2046 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
2047 * journal_head can be dropped if needed.
2049 * Various places in the kernel want to attach a journal_head to a buffer_head
2050 * _before_ attaching the journal_head to a transaction. To protect the
2051 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2052 * journal_head's b_jcount refcount by one. The caller must call
2053 * jbd2_journal_put_journal_head() to undo this.
2055 * So the typical usage would be:
2057 * (Attach a journal_head if needed. Increments b_jcount)
2058 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2059 * ...
2060 * jh->b_transaction = xxx;
2061 * jbd2_journal_put_journal_head(jh);
2063 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2064 * because it has a non-zero b_transaction.
2068 * Give a buffer_head a journal_head.
2070 * Doesn't need the journal lock.
2071 * May sleep.
2073 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2075 struct journal_head *jh;
2076 struct journal_head *new_jh = NULL;
2078 repeat:
2079 if (!buffer_jbd(bh)) {
2080 new_jh = journal_alloc_journal_head();
2081 memset(new_jh, 0, sizeof(*new_jh));
2084 jbd_lock_bh_journal_head(bh);
2085 if (buffer_jbd(bh)) {
2086 jh = bh2jh(bh);
2087 } else {
2088 J_ASSERT_BH(bh,
2089 (atomic_read(&bh->b_count) > 0) ||
2090 (bh->b_page && bh->b_page->mapping));
2092 if (!new_jh) {
2093 jbd_unlock_bh_journal_head(bh);
2094 goto repeat;
2097 jh = new_jh;
2098 new_jh = NULL; /* We consumed it */
2099 set_buffer_jbd(bh);
2100 bh->b_private = jh;
2101 jh->b_bh = bh;
2102 get_bh(bh);
2103 BUFFER_TRACE(bh, "added journal_head");
2105 jh->b_jcount++;
2106 jbd_unlock_bh_journal_head(bh);
2107 if (new_jh)
2108 journal_free_journal_head(new_jh);
2109 return bh->b_private;
2113 * Grab a ref against this buffer_head's journal_head. If it ended up not
2114 * having a journal_head, return NULL
2116 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2118 struct journal_head *jh = NULL;
2120 jbd_lock_bh_journal_head(bh);
2121 if (buffer_jbd(bh)) {
2122 jh = bh2jh(bh);
2123 jh->b_jcount++;
2125 jbd_unlock_bh_journal_head(bh);
2126 return jh;
2129 static void __journal_remove_journal_head(struct buffer_head *bh)
2131 struct journal_head *jh = bh2jh(bh);
2133 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2135 get_bh(bh);
2136 if (jh->b_jcount == 0) {
2137 if (jh->b_transaction == NULL &&
2138 jh->b_next_transaction == NULL &&
2139 jh->b_cp_transaction == NULL) {
2140 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2141 J_ASSERT_BH(bh, buffer_jbd(bh));
2142 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2143 BUFFER_TRACE(bh, "remove journal_head");
2144 if (jh->b_frozen_data) {
2145 printk(KERN_WARNING "%s: freeing "
2146 "b_frozen_data\n",
2147 __func__);
2148 jbd2_free(jh->b_frozen_data, bh->b_size);
2150 if (jh->b_committed_data) {
2151 printk(KERN_WARNING "%s: freeing "
2152 "b_committed_data\n",
2153 __func__);
2154 jbd2_free(jh->b_committed_data, bh->b_size);
2156 bh->b_private = NULL;
2157 jh->b_bh = NULL; /* debug, really */
2158 clear_buffer_jbd(bh);
2159 __brelse(bh);
2160 journal_free_journal_head(jh);
2161 } else {
2162 BUFFER_TRACE(bh, "journal_head was locked");
2168 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
2169 * and has a zero b_jcount then remove and release its journal_head. If we did
2170 * see that the buffer is not used by any transaction we also "logically"
2171 * decrement ->b_count.
2173 * We in fact take an additional increment on ->b_count as a convenience,
2174 * because the caller usually wants to do additional things with the bh
2175 * after calling here.
2176 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
2177 * time. Once the caller has run __brelse(), the buffer is eligible for
2178 * reaping by try_to_free_buffers().
2180 void jbd2_journal_remove_journal_head(struct buffer_head *bh)
2182 jbd_lock_bh_journal_head(bh);
2183 __journal_remove_journal_head(bh);
2184 jbd_unlock_bh_journal_head(bh);
2188 * Drop a reference on the passed journal_head. If it fell to zero then try to
2189 * release the journal_head from the buffer_head.
2191 void jbd2_journal_put_journal_head(struct journal_head *jh)
2193 struct buffer_head *bh = jh2bh(jh);
2195 jbd_lock_bh_journal_head(bh);
2196 J_ASSERT_JH(jh, jh->b_jcount > 0);
2197 --jh->b_jcount;
2198 if (!jh->b_jcount && !jh->b_transaction) {
2199 __journal_remove_journal_head(bh);
2200 __brelse(bh);
2202 jbd_unlock_bh_journal_head(bh);
2206 * Initialize jbd inode head
2208 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2210 jinode->i_transaction = NULL;
2211 jinode->i_next_transaction = NULL;
2212 jinode->i_vfs_inode = inode;
2213 jinode->i_flags = 0;
2214 INIT_LIST_HEAD(&jinode->i_list);
2218 * Function to be called before we start removing inode from memory (i.e.,
2219 * clear_inode() is a fine place to be called from). It removes inode from
2220 * transaction's lists.
2222 void jbd2_journal_release_jbd_inode(journal_t *journal,
2223 struct jbd2_inode *jinode)
2225 int writeout = 0;
2227 if (!journal)
2228 return;
2229 restart:
2230 spin_lock(&journal->j_list_lock);
2231 /* Is commit writing out inode - we have to wait */
2232 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2233 wait_queue_head_t *wq;
2234 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2235 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2236 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2237 spin_unlock(&journal->j_list_lock);
2238 schedule();
2239 finish_wait(wq, &wait.wait);
2240 goto restart;
2243 /* Do we need to wait for data writeback? */
2244 if (journal->j_committing_transaction == jinode->i_transaction)
2245 writeout = 1;
2246 if (jinode->i_transaction) {
2247 list_del(&jinode->i_list);
2248 jinode->i_transaction = NULL;
2250 spin_unlock(&journal->j_list_lock);
2254 * debugfs tunables
2256 #ifdef CONFIG_JBD2_DEBUG
2257 u8 jbd2_journal_enable_debug __read_mostly;
2258 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2260 #define JBD2_DEBUG_NAME "jbd2-debug"
2262 static struct dentry *jbd2_debugfs_dir;
2263 static struct dentry *jbd2_debug;
2265 static void __init jbd2_create_debugfs_entry(void)
2267 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2268 if (jbd2_debugfs_dir)
2269 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2270 jbd2_debugfs_dir,
2271 &jbd2_journal_enable_debug);
2274 static void __exit jbd2_remove_debugfs_entry(void)
2276 debugfs_remove(jbd2_debug);
2277 debugfs_remove(jbd2_debugfs_dir);
2280 #else
2282 static void __init jbd2_create_debugfs_entry(void)
2286 static void __exit jbd2_remove_debugfs_entry(void)
2290 #endif
2292 #ifdef CONFIG_PROC_FS
2294 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2296 static void __init jbd2_create_jbd_stats_proc_entry(void)
2298 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2301 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2303 if (proc_jbd2_stats)
2304 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2307 #else
2309 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2310 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2312 #endif
2314 struct kmem_cache *jbd2_handle_cache;
2316 static int __init journal_init_handle_cache(void)
2318 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
2319 sizeof(handle_t),
2320 0, /* offset */
2321 SLAB_TEMPORARY, /* flags */
2322 NULL); /* ctor */
2323 if (jbd2_handle_cache == NULL) {
2324 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2325 return -ENOMEM;
2327 return 0;
2330 static void jbd2_journal_destroy_handle_cache(void)
2332 if (jbd2_handle_cache)
2333 kmem_cache_destroy(jbd2_handle_cache);
2337 * Module startup and shutdown
2340 static int __init journal_init_caches(void)
2342 int ret;
2344 ret = jbd2_journal_init_revoke_caches();
2345 if (ret == 0)
2346 ret = journal_init_jbd2_journal_head_cache();
2347 if (ret == 0)
2348 ret = journal_init_handle_cache();
2349 return ret;
2352 static void jbd2_journal_destroy_caches(void)
2354 jbd2_journal_destroy_revoke_caches();
2355 jbd2_journal_destroy_jbd2_journal_head_cache();
2356 jbd2_journal_destroy_handle_cache();
2359 static int __init journal_init(void)
2361 int ret;
2363 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2365 ret = journal_init_caches();
2366 if (ret == 0) {
2367 jbd2_create_debugfs_entry();
2368 jbd2_create_jbd_stats_proc_entry();
2369 } else {
2370 jbd2_journal_destroy_caches();
2372 return ret;
2375 static void __exit journal_exit(void)
2377 #ifdef CONFIG_JBD2_DEBUG
2378 int n = atomic_read(&nr_journal_heads);
2379 if (n)
2380 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2381 #endif
2382 jbd2_remove_debugfs_entry();
2383 jbd2_remove_jbd_stats_proc_entry();
2384 jbd2_journal_destroy_caches();
2387 MODULE_LICENSE("GPL");
2388 module_init(journal_init);
2389 module_exit(journal_exit);