x86: atomic64: Improve atomic64_add_return()
[linux-2.6/verdex.git] / fs / jbd2 / journal.c
blob18bfd5dab64220d7d11fc90b6efb7c79e807c16a
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>
40 #include <linux/math64.h>
41 #include <linux/hash.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/jbd2.h>
46 #include <asm/uaccess.h>
47 #include <asm/page.h>
49 EXPORT_SYMBOL(jbd2_journal_start);
50 EXPORT_SYMBOL(jbd2_journal_restart);
51 EXPORT_SYMBOL(jbd2_journal_extend);
52 EXPORT_SYMBOL(jbd2_journal_stop);
53 EXPORT_SYMBOL(jbd2_journal_lock_updates);
54 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
55 EXPORT_SYMBOL(jbd2_journal_get_write_access);
56 EXPORT_SYMBOL(jbd2_journal_get_create_access);
57 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
58 EXPORT_SYMBOL(jbd2_journal_set_triggers);
59 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
60 EXPORT_SYMBOL(jbd2_journal_release_buffer);
61 EXPORT_SYMBOL(jbd2_journal_forget);
62 #if 0
63 EXPORT_SYMBOL(journal_sync_buffer);
64 #endif
65 EXPORT_SYMBOL(jbd2_journal_flush);
66 EXPORT_SYMBOL(jbd2_journal_revoke);
68 EXPORT_SYMBOL(jbd2_journal_init_dev);
69 EXPORT_SYMBOL(jbd2_journal_init_inode);
70 EXPORT_SYMBOL(jbd2_journal_update_format);
71 EXPORT_SYMBOL(jbd2_journal_check_used_features);
72 EXPORT_SYMBOL(jbd2_journal_check_available_features);
73 EXPORT_SYMBOL(jbd2_journal_set_features);
74 EXPORT_SYMBOL(jbd2_journal_load);
75 EXPORT_SYMBOL(jbd2_journal_destroy);
76 EXPORT_SYMBOL(jbd2_journal_abort);
77 EXPORT_SYMBOL(jbd2_journal_errno);
78 EXPORT_SYMBOL(jbd2_journal_ack_err);
79 EXPORT_SYMBOL(jbd2_journal_clear_err);
80 EXPORT_SYMBOL(jbd2_log_wait_commit);
81 EXPORT_SYMBOL(jbd2_journal_start_commit);
82 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
83 EXPORT_SYMBOL(jbd2_journal_wipe);
84 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
85 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
86 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
87 EXPORT_SYMBOL(jbd2_journal_force_commit);
88 EXPORT_SYMBOL(jbd2_journal_file_inode);
89 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
90 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
91 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
93 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
94 static void __journal_abort_soft (journal_t *journal, int errno);
97 * Helper function used to manage commit timeouts
100 static void commit_timeout(unsigned long __data)
102 struct task_struct * p = (struct task_struct *) __data;
104 wake_up_process(p);
108 * kjournald2: The main thread function used to manage a logging device
109 * journal.
111 * This kernel thread is responsible for two things:
113 * 1) COMMIT: Every so often we need to commit the current state of the
114 * filesystem to disk. The journal thread is responsible for writing
115 * all of the metadata buffers to disk.
117 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
118 * of the data in that part of the log has been rewritten elsewhere on
119 * the disk. Flushing these old buffers to reclaim space in the log is
120 * known as checkpointing, and this thread is responsible for that job.
123 static int kjournald2(void *arg)
125 journal_t *journal = arg;
126 transaction_t *transaction;
129 * Set up an interval timer which can be used to trigger a commit wakeup
130 * after the commit interval expires
132 setup_timer(&journal->j_commit_timer, commit_timeout,
133 (unsigned long)current);
135 /* Record that the journal thread is running */
136 journal->j_task = current;
137 wake_up(&journal->j_wait_done_commit);
139 printk(KERN_INFO "kjournald2 starting: pid %d, dev %s, "
140 "commit interval %ld seconds\n", current->pid,
141 journal->j_devname, journal->j_commit_interval / HZ);
144 * And now, wait forever for commit wakeup events.
146 spin_lock(&journal->j_state_lock);
148 loop:
149 if (journal->j_flags & JBD2_UNMOUNT)
150 goto end_loop;
152 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
153 journal->j_commit_sequence, journal->j_commit_request);
155 if (journal->j_commit_sequence != journal->j_commit_request) {
156 jbd_debug(1, "OK, requests differ\n");
157 spin_unlock(&journal->j_state_lock);
158 del_timer_sync(&journal->j_commit_timer);
159 jbd2_journal_commit_transaction(journal);
160 spin_lock(&journal->j_state_lock);
161 goto loop;
164 wake_up(&journal->j_wait_done_commit);
165 if (freezing(current)) {
167 * The simpler the better. Flushing journal isn't a
168 * good idea, because that depends on threads that may
169 * be already stopped.
171 jbd_debug(1, "Now suspending kjournald2\n");
172 spin_unlock(&journal->j_state_lock);
173 refrigerator();
174 spin_lock(&journal->j_state_lock);
175 } else {
177 * We assume on resume that commits are already there,
178 * so we don't sleep
180 DEFINE_WAIT(wait);
181 int should_sleep = 1;
183 prepare_to_wait(&journal->j_wait_commit, &wait,
184 TASK_INTERRUPTIBLE);
185 if (journal->j_commit_sequence != journal->j_commit_request)
186 should_sleep = 0;
187 transaction = journal->j_running_transaction;
188 if (transaction && time_after_eq(jiffies,
189 transaction->t_expires))
190 should_sleep = 0;
191 if (journal->j_flags & JBD2_UNMOUNT)
192 should_sleep = 0;
193 if (should_sleep) {
194 spin_unlock(&journal->j_state_lock);
195 schedule();
196 spin_lock(&journal->j_state_lock);
198 finish_wait(&journal->j_wait_commit, &wait);
201 jbd_debug(1, "kjournald2 wakes\n");
204 * Were we woken up by a commit wakeup event?
206 transaction = journal->j_running_transaction;
207 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
208 journal->j_commit_request = transaction->t_tid;
209 jbd_debug(1, "woke because of timeout\n");
211 goto loop;
213 end_loop:
214 spin_unlock(&journal->j_state_lock);
215 del_timer_sync(&journal->j_commit_timer);
216 journal->j_task = NULL;
217 wake_up(&journal->j_wait_done_commit);
218 jbd_debug(1, "Journal thread exiting.\n");
219 return 0;
222 static int jbd2_journal_start_thread(journal_t *journal)
224 struct task_struct *t;
226 t = kthread_run(kjournald2, journal, "kjournald2");
227 if (IS_ERR(t))
228 return PTR_ERR(t);
230 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
231 return 0;
234 static void journal_kill_thread(journal_t *journal)
236 spin_lock(&journal->j_state_lock);
237 journal->j_flags |= JBD2_UNMOUNT;
239 while (journal->j_task) {
240 wake_up(&journal->j_wait_commit);
241 spin_unlock(&journal->j_state_lock);
242 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
243 spin_lock(&journal->j_state_lock);
245 spin_unlock(&journal->j_state_lock);
249 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
251 * Writes a metadata buffer to a given disk block. The actual IO is not
252 * performed but a new buffer_head is constructed which labels the data
253 * to be written with the correct destination disk block.
255 * Any magic-number escaping which needs to be done will cause a
256 * copy-out here. If the buffer happens to start with the
257 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
258 * magic number is only written to the log for descripter blocks. In
259 * this case, we copy the data and replace the first word with 0, and we
260 * return a result code which indicates that this buffer needs to be
261 * marked as an escaped buffer in the corresponding log descriptor
262 * block. The missing word can then be restored when the block is read
263 * during recovery.
265 * If the source buffer has already been modified by a new transaction
266 * since we took the last commit snapshot, we use the frozen copy of
267 * that data for IO. If we end up using the existing buffer_head's data
268 * for the write, then we *have* to lock the buffer to prevent anyone
269 * else from using and possibly modifying it while the IO is in
270 * progress.
272 * The function returns a pointer to the buffer_heads to be used for IO.
274 * We assume that the journal has already been locked in this function.
276 * Return value:
277 * <0: Error
278 * >=0: Finished OK
280 * On success:
281 * Bit 0 set == escape performed on the data
282 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
285 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
286 struct journal_head *jh_in,
287 struct journal_head **jh_out,
288 unsigned long long blocknr)
290 int need_copy_out = 0;
291 int done_copy_out = 0;
292 int do_escape = 0;
293 char *mapped_data;
294 struct buffer_head *new_bh;
295 struct journal_head *new_jh;
296 struct page *new_page;
297 unsigned int new_offset;
298 struct buffer_head *bh_in = jh2bh(jh_in);
299 struct jbd2_buffer_trigger_type *triggers;
302 * The buffer really shouldn't be locked: only the current committing
303 * transaction is allowed to write it, so nobody else is allowed
304 * to do any IO.
306 * akpm: except if we're journalling data, and write() output is
307 * also part of a shared mapping, and another thread has
308 * decided to launch a writepage() against this buffer.
310 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
312 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
315 * If a new transaction has already done a buffer copy-out, then
316 * we use that version of the data for the commit.
318 jbd_lock_bh_state(bh_in);
319 repeat:
320 if (jh_in->b_frozen_data) {
321 done_copy_out = 1;
322 new_page = virt_to_page(jh_in->b_frozen_data);
323 new_offset = offset_in_page(jh_in->b_frozen_data);
324 triggers = jh_in->b_frozen_triggers;
325 } else {
326 new_page = jh2bh(jh_in)->b_page;
327 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
328 triggers = jh_in->b_triggers;
331 mapped_data = kmap_atomic(new_page, KM_USER0);
333 * Fire any commit trigger. Do this before checking for escaping,
334 * as the trigger may modify the magic offset. If a copy-out
335 * happens afterwards, it will have the correct data in the buffer.
337 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
338 triggers);
341 * Check for escaping
343 if (*((__be32 *)(mapped_data + new_offset)) ==
344 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
345 need_copy_out = 1;
346 do_escape = 1;
348 kunmap_atomic(mapped_data, KM_USER0);
351 * Do we need to do a data copy?
353 if (need_copy_out && !done_copy_out) {
354 char *tmp;
356 jbd_unlock_bh_state(bh_in);
357 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
358 jbd_lock_bh_state(bh_in);
359 if (jh_in->b_frozen_data) {
360 jbd2_free(tmp, bh_in->b_size);
361 goto repeat;
364 jh_in->b_frozen_data = tmp;
365 mapped_data = kmap_atomic(new_page, KM_USER0);
366 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
367 kunmap_atomic(mapped_data, KM_USER0);
369 new_page = virt_to_page(tmp);
370 new_offset = offset_in_page(tmp);
371 done_copy_out = 1;
374 * This isn't strictly necessary, as we're using frozen
375 * data for the escaping, but it keeps consistency with
376 * b_frozen_data usage.
378 jh_in->b_frozen_triggers = jh_in->b_triggers;
382 * Did we need to do an escaping? Now we've done all the
383 * copying, we can finally do so.
385 if (do_escape) {
386 mapped_data = kmap_atomic(new_page, KM_USER0);
387 *((unsigned int *)(mapped_data + new_offset)) = 0;
388 kunmap_atomic(mapped_data, KM_USER0);
391 /* keep subsequent assertions sane */
392 new_bh->b_state = 0;
393 init_buffer(new_bh, NULL, NULL);
394 atomic_set(&new_bh->b_count, 1);
395 jbd_unlock_bh_state(bh_in);
397 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
399 set_bh_page(new_bh, new_page, new_offset);
400 new_jh->b_transaction = NULL;
401 new_bh->b_size = jh2bh(jh_in)->b_size;
402 new_bh->b_bdev = transaction->t_journal->j_dev;
403 new_bh->b_blocknr = blocknr;
404 set_buffer_mapped(new_bh);
405 set_buffer_dirty(new_bh);
407 *jh_out = new_jh;
410 * The to-be-written buffer needs to get moved to the io queue,
411 * and the original buffer whose contents we are shadowing or
412 * copying is moved to the transaction's shadow queue.
414 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
415 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
416 JBUFFER_TRACE(new_jh, "file as BJ_IO");
417 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
419 return do_escape | (done_copy_out << 1);
423 * Allocation code for the journal file. Manage the space left in the
424 * journal, so that we can begin checkpointing when appropriate.
428 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
430 * Called with the journal already locked.
432 * Called under j_state_lock
435 int __jbd2_log_space_left(journal_t *journal)
437 int left = journal->j_free;
439 assert_spin_locked(&journal->j_state_lock);
442 * Be pessimistic here about the number of those free blocks which
443 * might be required for log descriptor control blocks.
446 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
448 left -= MIN_LOG_RESERVED_BLOCKS;
450 if (left <= 0)
451 return 0;
452 left -= (left >> 3);
453 return left;
457 * Called under j_state_lock. Returns true if a transaction commit was started.
459 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
462 * Are we already doing a recent enough commit?
464 if (!tid_geq(journal->j_commit_request, target)) {
466 * We want a new commit: OK, mark the request and wakup the
467 * commit thread. We do _not_ do the commit ourselves.
470 journal->j_commit_request = target;
471 jbd_debug(1, "JBD: requesting commit %d/%d\n",
472 journal->j_commit_request,
473 journal->j_commit_sequence);
474 wake_up(&journal->j_wait_commit);
475 return 1;
477 return 0;
480 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
482 int ret;
484 spin_lock(&journal->j_state_lock);
485 ret = __jbd2_log_start_commit(journal, tid);
486 spin_unlock(&journal->j_state_lock);
487 return ret;
491 * Force and wait upon a commit if the calling process is not within
492 * transaction. This is used for forcing out undo-protected data which contains
493 * bitmaps, when the fs is running out of space.
495 * We can only force the running transaction if we don't have an active handle;
496 * otherwise, we will deadlock.
498 * Returns true if a transaction was started.
500 int jbd2_journal_force_commit_nested(journal_t *journal)
502 transaction_t *transaction = NULL;
503 tid_t tid;
505 spin_lock(&journal->j_state_lock);
506 if (journal->j_running_transaction && !current->journal_info) {
507 transaction = journal->j_running_transaction;
508 __jbd2_log_start_commit(journal, transaction->t_tid);
509 } else if (journal->j_committing_transaction)
510 transaction = journal->j_committing_transaction;
512 if (!transaction) {
513 spin_unlock(&journal->j_state_lock);
514 return 0; /* Nothing to retry */
517 tid = transaction->t_tid;
518 spin_unlock(&journal->j_state_lock);
519 jbd2_log_wait_commit(journal, tid);
520 return 1;
524 * Start a commit of the current running transaction (if any). Returns true
525 * if a transaction is going to be committed (or is currently already
526 * committing), and fills its tid in at *ptid
528 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
530 int ret = 0;
532 spin_lock(&journal->j_state_lock);
533 if (journal->j_running_transaction) {
534 tid_t tid = journal->j_running_transaction->t_tid;
536 __jbd2_log_start_commit(journal, tid);
537 /* There's a running transaction and we've just made sure
538 * it's commit has been scheduled. */
539 if (ptid)
540 *ptid = tid;
541 ret = 1;
542 } else if (journal->j_committing_transaction) {
544 * If ext3_write_super() recently started a commit, then we
545 * have to wait for completion of that transaction
547 if (ptid)
548 *ptid = journal->j_committing_transaction->t_tid;
549 ret = 1;
551 spin_unlock(&journal->j_state_lock);
552 return ret;
556 * Wait for a specified commit to complete.
557 * The caller may not hold the journal lock.
559 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
561 int err = 0;
563 #ifdef CONFIG_JBD2_DEBUG
564 spin_lock(&journal->j_state_lock);
565 if (!tid_geq(journal->j_commit_request, tid)) {
566 printk(KERN_EMERG
567 "%s: error: j_commit_request=%d, tid=%d\n",
568 __func__, journal->j_commit_request, tid);
570 spin_unlock(&journal->j_state_lock);
571 #endif
572 spin_lock(&journal->j_state_lock);
573 while (tid_gt(tid, journal->j_commit_sequence)) {
574 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
575 tid, journal->j_commit_sequence);
576 wake_up(&journal->j_wait_commit);
577 spin_unlock(&journal->j_state_lock);
578 wait_event(journal->j_wait_done_commit,
579 !tid_gt(tid, journal->j_commit_sequence));
580 spin_lock(&journal->j_state_lock);
582 spin_unlock(&journal->j_state_lock);
584 if (unlikely(is_journal_aborted(journal))) {
585 printk(KERN_EMERG "journal commit I/O error\n");
586 err = -EIO;
588 return err;
592 * Log buffer allocation routines:
595 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
597 unsigned long blocknr;
599 spin_lock(&journal->j_state_lock);
600 J_ASSERT(journal->j_free > 1);
602 blocknr = journal->j_head;
603 journal->j_head++;
604 journal->j_free--;
605 if (journal->j_head == journal->j_last)
606 journal->j_head = journal->j_first;
607 spin_unlock(&journal->j_state_lock);
608 return jbd2_journal_bmap(journal, blocknr, retp);
612 * Conversion of logical to physical block numbers for the journal
614 * On external journals the journal blocks are identity-mapped, so
615 * this is a no-op. If needed, we can use j_blk_offset - everything is
616 * ready.
618 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
619 unsigned long long *retp)
621 int err = 0;
622 unsigned long long ret;
624 if (journal->j_inode) {
625 ret = bmap(journal->j_inode, blocknr);
626 if (ret)
627 *retp = ret;
628 else {
629 printk(KERN_ALERT "%s: journal block not found "
630 "at offset %lu on %s\n",
631 __func__, blocknr, journal->j_devname);
632 err = -EIO;
633 __journal_abort_soft(journal, err);
635 } else {
636 *retp = blocknr; /* +journal->j_blk_offset */
638 return err;
642 * We play buffer_head aliasing tricks to write data/metadata blocks to
643 * the journal without copying their contents, but for journal
644 * descriptor blocks we do need to generate bona fide buffers.
646 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
647 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
648 * But we don't bother doing that, so there will be coherency problems with
649 * mmaps of blockdevs which hold live JBD-controlled filesystems.
651 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
653 struct buffer_head *bh;
654 unsigned long long blocknr;
655 int err;
657 err = jbd2_journal_next_log_block(journal, &blocknr);
659 if (err)
660 return NULL;
662 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
663 if (!bh)
664 return NULL;
665 lock_buffer(bh);
666 memset(bh->b_data, 0, journal->j_blocksize);
667 set_buffer_uptodate(bh);
668 unlock_buffer(bh);
669 BUFFER_TRACE(bh, "return this buffer");
670 return jbd2_journal_add_journal_head(bh);
673 struct jbd2_stats_proc_session {
674 journal_t *journal;
675 struct transaction_stats_s *stats;
676 int start;
677 int max;
680 static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
681 struct transaction_stats_s *ts,
682 int first)
684 if (ts == s->stats + s->max)
685 ts = s->stats;
686 if (!first && ts == s->stats + s->start)
687 return NULL;
688 while (ts->ts_type == 0) {
689 ts++;
690 if (ts == s->stats + s->max)
691 ts = s->stats;
692 if (ts == s->stats + s->start)
693 return NULL;
695 return ts;
699 static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
701 struct jbd2_stats_proc_session *s = seq->private;
702 struct transaction_stats_s *ts;
703 int l = *pos;
705 if (l == 0)
706 return SEQ_START_TOKEN;
707 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
708 if (!ts)
709 return NULL;
710 l--;
711 while (l) {
712 ts = jbd2_history_skip_empty(s, ++ts, 0);
713 if (!ts)
714 break;
715 l--;
717 return ts;
720 static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
722 struct jbd2_stats_proc_session *s = seq->private;
723 struct transaction_stats_s *ts = v;
725 ++*pos;
726 if (v == SEQ_START_TOKEN)
727 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
728 else
729 return jbd2_history_skip_empty(s, ++ts, 0);
732 static int jbd2_seq_history_show(struct seq_file *seq, void *v)
734 struct transaction_stats_s *ts = v;
735 if (v == SEQ_START_TOKEN) {
736 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
737 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
738 "wait", "run", "lock", "flush", "log", "hndls",
739 "block", "inlog", "ctime", "write", "drop",
740 "close");
741 return 0;
743 if (ts->ts_type == JBD2_STATS_RUN)
744 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
745 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
746 jiffies_to_msecs(ts->u.run.rs_wait),
747 jiffies_to_msecs(ts->u.run.rs_running),
748 jiffies_to_msecs(ts->u.run.rs_locked),
749 jiffies_to_msecs(ts->u.run.rs_flushing),
750 jiffies_to_msecs(ts->u.run.rs_logging),
751 ts->u.run.rs_handle_count,
752 ts->u.run.rs_blocks,
753 ts->u.run.rs_blocks_logged);
754 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
755 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
756 "C", ts->ts_tid, " ",
757 jiffies_to_msecs(ts->u.chp.cs_chp_time),
758 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
759 ts->u.chp.cs_forced_to_close);
760 else
761 J_ASSERT(0);
762 return 0;
765 static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
769 static struct seq_operations jbd2_seq_history_ops = {
770 .start = jbd2_seq_history_start,
771 .next = jbd2_seq_history_next,
772 .stop = jbd2_seq_history_stop,
773 .show = jbd2_seq_history_show,
776 static int jbd2_seq_history_open(struct inode *inode, struct file *file)
778 journal_t *journal = PDE(inode)->data;
779 struct jbd2_stats_proc_session *s;
780 int rc, size;
782 s = kmalloc(sizeof(*s), GFP_KERNEL);
783 if (s == NULL)
784 return -ENOMEM;
785 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
786 s->stats = kmalloc(size, GFP_KERNEL);
787 if (s->stats == NULL) {
788 kfree(s);
789 return -ENOMEM;
791 spin_lock(&journal->j_history_lock);
792 memcpy(s->stats, journal->j_history, size);
793 s->max = journal->j_history_max;
794 s->start = journal->j_history_cur % s->max;
795 spin_unlock(&journal->j_history_lock);
797 rc = seq_open(file, &jbd2_seq_history_ops);
798 if (rc == 0) {
799 struct seq_file *m = file->private_data;
800 m->private = s;
801 } else {
802 kfree(s->stats);
803 kfree(s);
805 return rc;
809 static int jbd2_seq_history_release(struct inode *inode, struct file *file)
811 struct seq_file *seq = file->private_data;
812 struct jbd2_stats_proc_session *s = seq->private;
814 kfree(s->stats);
815 kfree(s);
816 return seq_release(inode, file);
819 static struct file_operations jbd2_seq_history_fops = {
820 .owner = THIS_MODULE,
821 .open = jbd2_seq_history_open,
822 .read = seq_read,
823 .llseek = seq_lseek,
824 .release = jbd2_seq_history_release,
827 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
829 return *pos ? NULL : SEQ_START_TOKEN;
832 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
834 return NULL;
837 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
839 struct jbd2_stats_proc_session *s = seq->private;
841 if (v != SEQ_START_TOKEN)
842 return 0;
843 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
844 s->stats->ts_tid,
845 s->journal->j_max_transaction_buffers);
846 if (s->stats->ts_tid == 0)
847 return 0;
848 seq_printf(seq, "average: \n %ums waiting for transaction\n",
849 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
850 seq_printf(seq, " %ums running transaction\n",
851 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
852 seq_printf(seq, " %ums transaction was being locked\n",
853 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
854 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
855 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
856 seq_printf(seq, " %ums logging transaction\n",
857 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
858 seq_printf(seq, " %lluus average transaction commit time\n",
859 div_u64(s->journal->j_average_commit_time, 1000));
860 seq_printf(seq, " %lu handles per transaction\n",
861 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
862 seq_printf(seq, " %lu blocks per transaction\n",
863 s->stats->u.run.rs_blocks / s->stats->ts_tid);
864 seq_printf(seq, " %lu logged blocks per transaction\n",
865 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
866 return 0;
869 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
873 static struct seq_operations jbd2_seq_info_ops = {
874 .start = jbd2_seq_info_start,
875 .next = jbd2_seq_info_next,
876 .stop = jbd2_seq_info_stop,
877 .show = jbd2_seq_info_show,
880 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
882 journal_t *journal = PDE(inode)->data;
883 struct jbd2_stats_proc_session *s;
884 int rc, size;
886 s = kmalloc(sizeof(*s), GFP_KERNEL);
887 if (s == NULL)
888 return -ENOMEM;
889 size = sizeof(struct transaction_stats_s);
890 s->stats = kmalloc(size, GFP_KERNEL);
891 if (s->stats == NULL) {
892 kfree(s);
893 return -ENOMEM;
895 spin_lock(&journal->j_history_lock);
896 memcpy(s->stats, &journal->j_stats, size);
897 s->journal = journal;
898 spin_unlock(&journal->j_history_lock);
900 rc = seq_open(file, &jbd2_seq_info_ops);
901 if (rc == 0) {
902 struct seq_file *m = file->private_data;
903 m->private = s;
904 } else {
905 kfree(s->stats);
906 kfree(s);
908 return rc;
912 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
914 struct seq_file *seq = file->private_data;
915 struct jbd2_stats_proc_session *s = seq->private;
916 kfree(s->stats);
917 kfree(s);
918 return seq_release(inode, file);
921 static struct file_operations jbd2_seq_info_fops = {
922 .owner = THIS_MODULE,
923 .open = jbd2_seq_info_open,
924 .read = seq_read,
925 .llseek = seq_lseek,
926 .release = jbd2_seq_info_release,
929 static struct proc_dir_entry *proc_jbd2_stats;
931 static void jbd2_stats_proc_init(journal_t *journal)
933 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
934 if (journal->j_proc_entry) {
935 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
936 &jbd2_seq_history_fops, journal);
937 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
938 &jbd2_seq_info_fops, journal);
942 static void jbd2_stats_proc_exit(journal_t *journal)
944 remove_proc_entry("info", journal->j_proc_entry);
945 remove_proc_entry("history", journal->j_proc_entry);
946 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
949 static void journal_init_stats(journal_t *journal)
951 int size;
953 if (!proc_jbd2_stats)
954 return;
956 journal->j_history_max = 100;
957 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
958 journal->j_history = kzalloc(size, GFP_KERNEL);
959 if (!journal->j_history) {
960 journal->j_history_max = 0;
961 return;
963 spin_lock_init(&journal->j_history_lock);
967 * Management for journal control blocks: functions to create and
968 * destroy journal_t structures, and to initialise and read existing
969 * journal blocks from disk. */
971 /* First: create and setup a journal_t object in memory. We initialise
972 * very few fields yet: that has to wait until we have created the
973 * journal structures from from scratch, or loaded them from disk. */
975 static journal_t * journal_init_common (void)
977 journal_t *journal;
978 int err;
980 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
981 if (!journal)
982 goto fail;
984 init_waitqueue_head(&journal->j_wait_transaction_locked);
985 init_waitqueue_head(&journal->j_wait_logspace);
986 init_waitqueue_head(&journal->j_wait_done_commit);
987 init_waitqueue_head(&journal->j_wait_checkpoint);
988 init_waitqueue_head(&journal->j_wait_commit);
989 init_waitqueue_head(&journal->j_wait_updates);
990 mutex_init(&journal->j_barrier);
991 mutex_init(&journal->j_checkpoint_mutex);
992 spin_lock_init(&journal->j_revoke_lock);
993 spin_lock_init(&journal->j_list_lock);
994 spin_lock_init(&journal->j_state_lock);
996 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
997 journal->j_min_batch_time = 0;
998 journal->j_max_batch_time = 15000; /* 15ms */
1000 /* The journal is marked for error until we succeed with recovery! */
1001 journal->j_flags = JBD2_ABORT;
1003 /* Set up a default-sized revoke table for the new mount. */
1004 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1005 if (err) {
1006 kfree(journal);
1007 goto fail;
1010 journal_init_stats(journal);
1012 return journal;
1013 fail:
1014 return NULL;
1017 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1019 * Create a journal structure assigned some fixed set of disk blocks to
1020 * the journal. We don't actually touch those disk blocks yet, but we
1021 * need to set up all of the mapping information to tell the journaling
1022 * system where the journal blocks are.
1027 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1028 * @bdev: Block device on which to create the journal
1029 * @fs_dev: Device which hold journalled filesystem for this journal.
1030 * @start: Block nr Start of journal.
1031 * @len: Length of the journal in blocks.
1032 * @blocksize: blocksize of journalling device
1034 * Returns: a newly created journal_t *
1036 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1037 * range of blocks on an arbitrary block device.
1040 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1041 struct block_device *fs_dev,
1042 unsigned long long start, int len, int blocksize)
1044 journal_t *journal = journal_init_common();
1045 struct buffer_head *bh;
1046 char *p;
1047 int n;
1049 if (!journal)
1050 return NULL;
1052 /* journal descriptor can store up to n blocks -bzzz */
1053 journal->j_blocksize = blocksize;
1054 jbd2_stats_proc_init(journal);
1055 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1056 journal->j_wbufsize = n;
1057 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1058 if (!journal->j_wbuf) {
1059 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1060 __func__);
1061 goto out_err;
1063 journal->j_dev = bdev;
1064 journal->j_fs_dev = fs_dev;
1065 journal->j_blk_offset = start;
1066 journal->j_maxlen = len;
1067 bdevname(journal->j_dev, journal->j_devname);
1068 p = journal->j_devname;
1069 while ((p = strchr(p, '/')))
1070 *p = '!';
1072 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1073 if (!bh) {
1074 printk(KERN_ERR
1075 "%s: Cannot get buffer for journal superblock\n",
1076 __func__);
1077 goto out_err;
1079 journal->j_sb_buffer = bh;
1080 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1082 return journal;
1083 out_err:
1084 jbd2_stats_proc_exit(journal);
1085 kfree(journal);
1086 return NULL;
1090 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1091 * @inode: An inode to create the journal in
1093 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1094 * the journal. The inode must exist already, must support bmap() and
1095 * must have all data blocks preallocated.
1097 journal_t * jbd2_journal_init_inode (struct inode *inode)
1099 struct buffer_head *bh;
1100 journal_t *journal = journal_init_common();
1101 char *p;
1102 int err;
1103 int n;
1104 unsigned long long blocknr;
1106 if (!journal)
1107 return NULL;
1109 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1110 journal->j_inode = inode;
1111 bdevname(journal->j_dev, journal->j_devname);
1112 p = journal->j_devname;
1113 while ((p = strchr(p, '/')))
1114 *p = '!';
1115 p = journal->j_devname + strlen(journal->j_devname);
1116 sprintf(p, ":%lu", journal->j_inode->i_ino);
1117 jbd_debug(1,
1118 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1119 journal, inode->i_sb->s_id, inode->i_ino,
1120 (long long) inode->i_size,
1121 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1123 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1124 journal->j_blocksize = inode->i_sb->s_blocksize;
1125 jbd2_stats_proc_init(journal);
1127 /* journal descriptor can store up to n blocks -bzzz */
1128 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1129 journal->j_wbufsize = n;
1130 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1131 if (!journal->j_wbuf) {
1132 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1133 __func__);
1134 goto out_err;
1137 err = jbd2_journal_bmap(journal, 0, &blocknr);
1138 /* If that failed, give up */
1139 if (err) {
1140 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
1141 __func__);
1142 goto out_err;
1145 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1146 if (!bh) {
1147 printk(KERN_ERR
1148 "%s: Cannot get buffer for journal superblock\n",
1149 __func__);
1150 goto out_err;
1152 journal->j_sb_buffer = bh;
1153 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1155 return journal;
1156 out_err:
1157 jbd2_stats_proc_exit(journal);
1158 kfree(journal);
1159 return NULL;
1163 * If the journal init or create aborts, we need to mark the journal
1164 * superblock as being NULL to prevent the journal destroy from writing
1165 * back a bogus superblock.
1167 static void journal_fail_superblock (journal_t *journal)
1169 struct buffer_head *bh = journal->j_sb_buffer;
1170 brelse(bh);
1171 journal->j_sb_buffer = NULL;
1175 * Given a journal_t structure, initialise the various fields for
1176 * startup of a new journaling session. We use this both when creating
1177 * a journal, and after recovering an old journal to reset it for
1178 * subsequent use.
1181 static int journal_reset(journal_t *journal)
1183 journal_superblock_t *sb = journal->j_superblock;
1184 unsigned long long first, last;
1186 first = be32_to_cpu(sb->s_first);
1187 last = be32_to_cpu(sb->s_maxlen);
1189 journal->j_first = first;
1190 journal->j_last = last;
1192 journal->j_head = first;
1193 journal->j_tail = first;
1194 journal->j_free = last - first;
1196 journal->j_tail_sequence = journal->j_transaction_sequence;
1197 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1198 journal->j_commit_request = journal->j_commit_sequence;
1200 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1202 /* Add the dynamic fields and write it to disk. */
1203 jbd2_journal_update_superblock(journal, 1);
1204 return jbd2_journal_start_thread(journal);
1208 * void jbd2_journal_update_superblock() - Update journal sb on disk.
1209 * @journal: The journal to update.
1210 * @wait: Set to '0' if you don't want to wait for IO completion.
1212 * Update a journal's dynamic superblock fields and write it to disk,
1213 * optionally waiting for the IO to complete.
1215 void jbd2_journal_update_superblock(journal_t *journal, int wait)
1217 journal_superblock_t *sb = journal->j_superblock;
1218 struct buffer_head *bh = journal->j_sb_buffer;
1221 * As a special case, if the on-disk copy is already marked as needing
1222 * no recovery (s_start == 0) and there are no outstanding transactions
1223 * in the filesystem, then we can safely defer the superblock update
1224 * until the next commit by setting JBD2_FLUSHED. This avoids
1225 * attempting a write to a potential-readonly device.
1227 if (sb->s_start == 0 && journal->j_tail_sequence ==
1228 journal->j_transaction_sequence) {
1229 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1230 "(start %ld, seq %d, errno %d)\n",
1231 journal->j_tail, journal->j_tail_sequence,
1232 journal->j_errno);
1233 goto out;
1236 if (buffer_write_io_error(bh)) {
1238 * Oh, dear. A previous attempt to write the journal
1239 * superblock failed. This could happen because the
1240 * USB device was yanked out. Or it could happen to
1241 * be a transient write error and maybe the block will
1242 * be remapped. Nothing we can do but to retry the
1243 * write and hope for the best.
1245 printk(KERN_ERR "JBD2: previous I/O error detected "
1246 "for journal superblock update for %s.\n",
1247 journal->j_devname);
1248 clear_buffer_write_io_error(bh);
1249 set_buffer_uptodate(bh);
1252 spin_lock(&journal->j_state_lock);
1253 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1254 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1256 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1257 sb->s_start = cpu_to_be32(journal->j_tail);
1258 sb->s_errno = cpu_to_be32(journal->j_errno);
1259 spin_unlock(&journal->j_state_lock);
1261 BUFFER_TRACE(bh, "marking dirty");
1262 mark_buffer_dirty(bh);
1263 if (wait) {
1264 sync_dirty_buffer(bh);
1265 if (buffer_write_io_error(bh)) {
1266 printk(KERN_ERR "JBD2: I/O error detected "
1267 "when updating journal superblock for %s.\n",
1268 journal->j_devname);
1269 clear_buffer_write_io_error(bh);
1270 set_buffer_uptodate(bh);
1272 } else
1273 ll_rw_block(SWRITE, 1, &bh);
1275 out:
1276 /* If we have just flushed the log (by marking s_start==0), then
1277 * any future commit will have to be careful to update the
1278 * superblock again to re-record the true start of the log. */
1280 spin_lock(&journal->j_state_lock);
1281 if (sb->s_start)
1282 journal->j_flags &= ~JBD2_FLUSHED;
1283 else
1284 journal->j_flags |= JBD2_FLUSHED;
1285 spin_unlock(&journal->j_state_lock);
1289 * Read the superblock for a given journal, performing initial
1290 * validation of the format.
1293 static int journal_get_superblock(journal_t *journal)
1295 struct buffer_head *bh;
1296 journal_superblock_t *sb;
1297 int err = -EIO;
1299 bh = journal->j_sb_buffer;
1301 J_ASSERT(bh != NULL);
1302 if (!buffer_uptodate(bh)) {
1303 ll_rw_block(READ, 1, &bh);
1304 wait_on_buffer(bh);
1305 if (!buffer_uptodate(bh)) {
1306 printk (KERN_ERR
1307 "JBD: IO error reading journal superblock\n");
1308 goto out;
1312 sb = journal->j_superblock;
1314 err = -EINVAL;
1316 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1317 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1318 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1319 goto out;
1322 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1323 case JBD2_SUPERBLOCK_V1:
1324 journal->j_format_version = 1;
1325 break;
1326 case JBD2_SUPERBLOCK_V2:
1327 journal->j_format_version = 2;
1328 break;
1329 default:
1330 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1331 goto out;
1334 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1335 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1336 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1337 printk (KERN_WARNING "JBD: journal file too short\n");
1338 goto out;
1341 return 0;
1343 out:
1344 journal_fail_superblock(journal);
1345 return err;
1349 * Load the on-disk journal superblock and read the key fields into the
1350 * journal_t.
1353 static int load_superblock(journal_t *journal)
1355 int err;
1356 journal_superblock_t *sb;
1358 err = journal_get_superblock(journal);
1359 if (err)
1360 return err;
1362 sb = journal->j_superblock;
1364 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1365 journal->j_tail = be32_to_cpu(sb->s_start);
1366 journal->j_first = be32_to_cpu(sb->s_first);
1367 journal->j_last = be32_to_cpu(sb->s_maxlen);
1368 journal->j_errno = be32_to_cpu(sb->s_errno);
1370 return 0;
1375 * int jbd2_journal_load() - Read journal from disk.
1376 * @journal: Journal to act on.
1378 * Given a journal_t structure which tells us which disk blocks contain
1379 * a journal, read the journal from disk to initialise the in-memory
1380 * structures.
1382 int jbd2_journal_load(journal_t *journal)
1384 int err;
1385 journal_superblock_t *sb;
1387 err = load_superblock(journal);
1388 if (err)
1389 return err;
1391 sb = journal->j_superblock;
1392 /* If this is a V2 superblock, then we have to check the
1393 * features flags on it. */
1395 if (journal->j_format_version >= 2) {
1396 if ((sb->s_feature_ro_compat &
1397 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1398 (sb->s_feature_incompat &
1399 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1400 printk (KERN_WARNING
1401 "JBD: Unrecognised features on journal\n");
1402 return -EINVAL;
1406 /* Let the recovery code check whether it needs to recover any
1407 * data from the journal. */
1408 if (jbd2_journal_recover(journal))
1409 goto recovery_error;
1411 /* OK, we've finished with the dynamic journal bits:
1412 * reinitialise the dynamic contents of the superblock in memory
1413 * and reset them on disk. */
1414 if (journal_reset(journal))
1415 goto recovery_error;
1417 journal->j_flags &= ~JBD2_ABORT;
1418 journal->j_flags |= JBD2_LOADED;
1419 return 0;
1421 recovery_error:
1422 printk (KERN_WARNING "JBD: recovery failed\n");
1423 return -EIO;
1427 * void jbd2_journal_destroy() - Release a journal_t structure.
1428 * @journal: Journal to act on.
1430 * Release a journal_t structure once it is no longer in use by the
1431 * journaled object.
1432 * Return <0 if we couldn't clean up the journal.
1434 int jbd2_journal_destroy(journal_t *journal)
1436 int err = 0;
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 mutex_lock(&journal->j_checkpoint_mutex);
1452 jbd2_log_do_checkpoint(journal);
1453 mutex_unlock(&journal->j_checkpoint_mutex);
1454 spin_lock(&journal->j_list_lock);
1457 J_ASSERT(journal->j_running_transaction == NULL);
1458 J_ASSERT(journal->j_committing_transaction == NULL);
1459 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1460 spin_unlock(&journal->j_list_lock);
1462 if (journal->j_sb_buffer) {
1463 if (!is_journal_aborted(journal)) {
1464 /* We can now mark the journal as empty. */
1465 journal->j_tail = 0;
1466 journal->j_tail_sequence =
1467 ++journal->j_transaction_sequence;
1468 jbd2_journal_update_superblock(journal, 1);
1469 } else {
1470 err = -EIO;
1472 brelse(journal->j_sb_buffer);
1475 if (journal->j_proc_entry)
1476 jbd2_stats_proc_exit(journal);
1477 if (journal->j_inode)
1478 iput(journal->j_inode);
1479 if (journal->j_revoke)
1480 jbd2_journal_destroy_revoke(journal);
1481 kfree(journal->j_wbuf);
1482 kfree(journal);
1484 return err;
1489 *int jbd2_journal_check_used_features () - Check if features specified are used.
1490 * @journal: Journal to check.
1491 * @compat: bitmask of compatible features
1492 * @ro: bitmask of features that force read-only mount
1493 * @incompat: bitmask of incompatible features
1495 * Check whether the journal uses all of a given set of
1496 * features. Return true (non-zero) if it does.
1499 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1500 unsigned long ro, unsigned long incompat)
1502 journal_superblock_t *sb;
1504 if (!compat && !ro && !incompat)
1505 return 1;
1506 if (journal->j_format_version == 1)
1507 return 0;
1509 sb = journal->j_superblock;
1511 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1512 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1513 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1514 return 1;
1516 return 0;
1520 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1521 * @journal: Journal to check.
1522 * @compat: bitmask of compatible features
1523 * @ro: bitmask of features that force read-only mount
1524 * @incompat: bitmask of incompatible features
1526 * Check whether the journaling code supports the use of
1527 * all of a given set of features on this journal. Return true
1528 * (non-zero) if it can. */
1530 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1531 unsigned long ro, unsigned long incompat)
1533 journal_superblock_t *sb;
1535 if (!compat && !ro && !incompat)
1536 return 1;
1538 sb = journal->j_superblock;
1540 /* We can support any known requested features iff the
1541 * superblock is in version 2. Otherwise we fail to support any
1542 * extended sb features. */
1544 if (journal->j_format_version != 2)
1545 return 0;
1547 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1548 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1549 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1550 return 1;
1552 return 0;
1556 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1557 * @journal: Journal to act on.
1558 * @compat: bitmask of compatible features
1559 * @ro: bitmask of features that force read-only mount
1560 * @incompat: bitmask of incompatible features
1562 * Mark a given journal feature as present on the
1563 * superblock. Returns true if the requested features could be set.
1567 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1568 unsigned long ro, unsigned long incompat)
1570 journal_superblock_t *sb;
1572 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1573 return 1;
1575 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1576 return 0;
1578 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1579 compat, ro, incompat);
1581 sb = journal->j_superblock;
1583 sb->s_feature_compat |= cpu_to_be32(compat);
1584 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1585 sb->s_feature_incompat |= cpu_to_be32(incompat);
1587 return 1;
1591 * jbd2_journal_clear_features () - Clear a given journal feature in the
1592 * superblock
1593 * @journal: Journal to act on.
1594 * @compat: bitmask of compatible features
1595 * @ro: bitmask of features that force read-only mount
1596 * @incompat: bitmask of incompatible features
1598 * Clear a given journal feature as present on the
1599 * superblock.
1601 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1602 unsigned long ro, unsigned long incompat)
1604 journal_superblock_t *sb;
1606 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1607 compat, ro, incompat);
1609 sb = journal->j_superblock;
1611 sb->s_feature_compat &= ~cpu_to_be32(compat);
1612 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1613 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1615 EXPORT_SYMBOL(jbd2_journal_clear_features);
1618 * int jbd2_journal_update_format () - Update on-disk journal structure.
1619 * @journal: Journal to act on.
1621 * Given an initialised but unloaded journal struct, poke about in the
1622 * on-disk structure to update it to the most recent supported version.
1624 int jbd2_journal_update_format (journal_t *journal)
1626 journal_superblock_t *sb;
1627 int err;
1629 err = journal_get_superblock(journal);
1630 if (err)
1631 return err;
1633 sb = journal->j_superblock;
1635 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1636 case JBD2_SUPERBLOCK_V2:
1637 return 0;
1638 case JBD2_SUPERBLOCK_V1:
1639 return journal_convert_superblock_v1(journal, sb);
1640 default:
1641 break;
1643 return -EINVAL;
1646 static int journal_convert_superblock_v1(journal_t *journal,
1647 journal_superblock_t *sb)
1649 int offset, blocksize;
1650 struct buffer_head *bh;
1652 printk(KERN_WARNING
1653 "JBD: Converting superblock from version 1 to 2.\n");
1655 /* Pre-initialise new fields to zero */
1656 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1657 blocksize = be32_to_cpu(sb->s_blocksize);
1658 memset(&sb->s_feature_compat, 0, blocksize-offset);
1660 sb->s_nr_users = cpu_to_be32(1);
1661 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1662 journal->j_format_version = 2;
1664 bh = journal->j_sb_buffer;
1665 BUFFER_TRACE(bh, "marking dirty");
1666 mark_buffer_dirty(bh);
1667 sync_dirty_buffer(bh);
1668 return 0;
1673 * int jbd2_journal_flush () - Flush journal
1674 * @journal: Journal to act on.
1676 * Flush all data for a given journal to disk and empty the journal.
1677 * Filesystems can use this when remounting readonly to ensure that
1678 * recovery does not need to happen on remount.
1681 int jbd2_journal_flush(journal_t *journal)
1683 int err = 0;
1684 transaction_t *transaction = NULL;
1685 unsigned long old_tail;
1687 spin_lock(&journal->j_state_lock);
1689 /* Force everything buffered to the log... */
1690 if (journal->j_running_transaction) {
1691 transaction = journal->j_running_transaction;
1692 __jbd2_log_start_commit(journal, transaction->t_tid);
1693 } else if (journal->j_committing_transaction)
1694 transaction = journal->j_committing_transaction;
1696 /* Wait for the log commit to complete... */
1697 if (transaction) {
1698 tid_t tid = transaction->t_tid;
1700 spin_unlock(&journal->j_state_lock);
1701 jbd2_log_wait_commit(journal, tid);
1702 } else {
1703 spin_unlock(&journal->j_state_lock);
1706 /* ...and flush everything in the log out to disk. */
1707 spin_lock(&journal->j_list_lock);
1708 while (!err && journal->j_checkpoint_transactions != NULL) {
1709 spin_unlock(&journal->j_list_lock);
1710 mutex_lock(&journal->j_checkpoint_mutex);
1711 err = jbd2_log_do_checkpoint(journal);
1712 mutex_unlock(&journal->j_checkpoint_mutex);
1713 spin_lock(&journal->j_list_lock);
1715 spin_unlock(&journal->j_list_lock);
1717 if (is_journal_aborted(journal))
1718 return -EIO;
1720 jbd2_cleanup_journal_tail(journal);
1722 /* Finally, mark the journal as really needing no recovery.
1723 * This sets s_start==0 in the underlying superblock, which is
1724 * the magic code for a fully-recovered superblock. Any future
1725 * commits of data to the journal will restore the current
1726 * s_start value. */
1727 spin_lock(&journal->j_state_lock);
1728 old_tail = journal->j_tail;
1729 journal->j_tail = 0;
1730 spin_unlock(&journal->j_state_lock);
1731 jbd2_journal_update_superblock(journal, 1);
1732 spin_lock(&journal->j_state_lock);
1733 journal->j_tail = old_tail;
1735 J_ASSERT(!journal->j_running_transaction);
1736 J_ASSERT(!journal->j_committing_transaction);
1737 J_ASSERT(!journal->j_checkpoint_transactions);
1738 J_ASSERT(journal->j_head == journal->j_tail);
1739 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1740 spin_unlock(&journal->j_state_lock);
1741 return 0;
1745 * int jbd2_journal_wipe() - Wipe journal contents
1746 * @journal: Journal to act on.
1747 * @write: flag (see below)
1749 * Wipe out all of the contents of a journal, safely. This will produce
1750 * a warning if the journal contains any valid recovery information.
1751 * Must be called between journal_init_*() and jbd2_journal_load().
1753 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1754 * we merely suppress recovery.
1757 int jbd2_journal_wipe(journal_t *journal, int write)
1759 journal_superblock_t *sb;
1760 int err = 0;
1762 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1764 err = load_superblock(journal);
1765 if (err)
1766 return err;
1768 sb = journal->j_superblock;
1770 if (!journal->j_tail)
1771 goto no_recovery;
1773 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1774 write ? "Clearing" : "Ignoring");
1776 err = jbd2_journal_skip_recovery(journal);
1777 if (write)
1778 jbd2_journal_update_superblock(journal, 1);
1780 no_recovery:
1781 return err;
1785 * Journal abort has very specific semantics, which we describe
1786 * for journal abort.
1788 * Two internal functions, which provide abort to the jbd layer
1789 * itself are here.
1793 * Quick version for internal journal use (doesn't lock the journal).
1794 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1795 * and don't attempt to make any other journal updates.
1797 void __jbd2_journal_abort_hard(journal_t *journal)
1799 transaction_t *transaction;
1801 if (journal->j_flags & JBD2_ABORT)
1802 return;
1804 printk(KERN_ERR "Aborting journal on device %s.\n",
1805 journal->j_devname);
1807 spin_lock(&journal->j_state_lock);
1808 journal->j_flags |= JBD2_ABORT;
1809 transaction = journal->j_running_transaction;
1810 if (transaction)
1811 __jbd2_log_start_commit(journal, transaction->t_tid);
1812 spin_unlock(&journal->j_state_lock);
1815 /* Soft abort: record the abort error status in the journal superblock,
1816 * but don't do any other IO. */
1817 static void __journal_abort_soft (journal_t *journal, int errno)
1819 if (journal->j_flags & JBD2_ABORT)
1820 return;
1822 if (!journal->j_errno)
1823 journal->j_errno = errno;
1825 __jbd2_journal_abort_hard(journal);
1827 if (errno)
1828 jbd2_journal_update_superblock(journal, 1);
1832 * void jbd2_journal_abort () - Shutdown the journal immediately.
1833 * @journal: the journal to shutdown.
1834 * @errno: an error number to record in the journal indicating
1835 * the reason for the shutdown.
1837 * Perform a complete, immediate shutdown of the ENTIRE
1838 * journal (not of a single transaction). This operation cannot be
1839 * undone without closing and reopening the journal.
1841 * The jbd2_journal_abort function is intended to support higher level error
1842 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1843 * mode.
1845 * Journal abort has very specific semantics. Any existing dirty,
1846 * unjournaled buffers in the main filesystem will still be written to
1847 * disk by bdflush, but the journaling mechanism will be suspended
1848 * immediately and no further transaction commits will be honoured.
1850 * Any dirty, journaled buffers will be written back to disk without
1851 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1852 * filesystem, but we _do_ attempt to leave as much data as possible
1853 * behind for fsck to use for cleanup.
1855 * Any attempt to get a new transaction handle on a journal which is in
1856 * ABORT state will just result in an -EROFS error return. A
1857 * jbd2_journal_stop on an existing handle will return -EIO if we have
1858 * entered abort state during the update.
1860 * Recursive transactions are not disturbed by journal abort until the
1861 * final jbd2_journal_stop, which will receive the -EIO error.
1863 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
1864 * which will be recorded (if possible) in the journal superblock. This
1865 * allows a client to record failure conditions in the middle of a
1866 * transaction without having to complete the transaction to record the
1867 * failure to disk. ext3_error, for example, now uses this
1868 * functionality.
1870 * Errors which originate from within the journaling layer will NOT
1871 * supply an errno; a null errno implies that absolutely no further
1872 * writes are done to the journal (unless there are any already in
1873 * progress).
1877 void jbd2_journal_abort(journal_t *journal, int errno)
1879 __journal_abort_soft(journal, errno);
1883 * int jbd2_journal_errno () - returns the journal's error state.
1884 * @journal: journal to examine.
1886 * This is the errno number set with jbd2_journal_abort(), the last
1887 * time the journal was mounted - if the journal was stopped
1888 * without calling abort this will be 0.
1890 * If the journal has been aborted on this mount time -EROFS will
1891 * be returned.
1893 int jbd2_journal_errno(journal_t *journal)
1895 int err;
1897 spin_lock(&journal->j_state_lock);
1898 if (journal->j_flags & JBD2_ABORT)
1899 err = -EROFS;
1900 else
1901 err = journal->j_errno;
1902 spin_unlock(&journal->j_state_lock);
1903 return err;
1907 * int jbd2_journal_clear_err () - clears the journal's error state
1908 * @journal: journal to act on.
1910 * An error must be cleared or acked to take a FS out of readonly
1911 * mode.
1913 int jbd2_journal_clear_err(journal_t *journal)
1915 int err = 0;
1917 spin_lock(&journal->j_state_lock);
1918 if (journal->j_flags & JBD2_ABORT)
1919 err = -EROFS;
1920 else
1921 journal->j_errno = 0;
1922 spin_unlock(&journal->j_state_lock);
1923 return err;
1927 * void jbd2_journal_ack_err() - Ack journal err.
1928 * @journal: journal to act on.
1930 * An error must be cleared or acked to take a FS out of readonly
1931 * mode.
1933 void jbd2_journal_ack_err(journal_t *journal)
1935 spin_lock(&journal->j_state_lock);
1936 if (journal->j_errno)
1937 journal->j_flags |= JBD2_ACK_ERR;
1938 spin_unlock(&journal->j_state_lock);
1941 int jbd2_journal_blocks_per_page(struct inode *inode)
1943 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1947 * helper functions to deal with 32 or 64bit block numbers.
1949 size_t journal_tag_bytes(journal_t *journal)
1951 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1952 return JBD2_TAG_SIZE64;
1953 else
1954 return JBD2_TAG_SIZE32;
1958 * Journal_head storage management
1960 static struct kmem_cache *jbd2_journal_head_cache;
1961 #ifdef CONFIG_JBD2_DEBUG
1962 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1963 #endif
1965 static int journal_init_jbd2_journal_head_cache(void)
1967 int retval;
1969 J_ASSERT(jbd2_journal_head_cache == NULL);
1970 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
1971 sizeof(struct journal_head),
1972 0, /* offset */
1973 SLAB_TEMPORARY, /* flags */
1974 NULL); /* ctor */
1975 retval = 0;
1976 if (!jbd2_journal_head_cache) {
1977 retval = -ENOMEM;
1978 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1980 return retval;
1983 static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
1985 if (jbd2_journal_head_cache) {
1986 kmem_cache_destroy(jbd2_journal_head_cache);
1987 jbd2_journal_head_cache = NULL;
1992 * journal_head splicing and dicing
1994 static struct journal_head *journal_alloc_journal_head(void)
1996 struct journal_head *ret;
1997 static unsigned long last_warning;
1999 #ifdef CONFIG_JBD2_DEBUG
2000 atomic_inc(&nr_journal_heads);
2001 #endif
2002 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2003 if (!ret) {
2004 jbd_debug(1, "out of memory for journal_head\n");
2005 if (time_after(jiffies, last_warning + 5*HZ)) {
2006 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
2007 __func__);
2008 last_warning = jiffies;
2010 while (!ret) {
2011 yield();
2012 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2015 return ret;
2018 static void journal_free_journal_head(struct journal_head *jh)
2020 #ifdef CONFIG_JBD2_DEBUG
2021 atomic_dec(&nr_journal_heads);
2022 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2023 #endif
2024 kmem_cache_free(jbd2_journal_head_cache, jh);
2028 * A journal_head is attached to a buffer_head whenever JBD has an
2029 * interest in the buffer.
2031 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2032 * is set. This bit is tested in core kernel code where we need to take
2033 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2034 * there.
2036 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2038 * When a buffer has its BH_JBD bit set it is immune from being released by
2039 * core kernel code, mainly via ->b_count.
2041 * A journal_head may be detached from its buffer_head when the journal_head's
2042 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
2043 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
2044 * journal_head can be dropped if needed.
2046 * Various places in the kernel want to attach a journal_head to a buffer_head
2047 * _before_ attaching the journal_head to a transaction. To protect the
2048 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2049 * journal_head's b_jcount refcount by one. The caller must call
2050 * jbd2_journal_put_journal_head() to undo this.
2052 * So the typical usage would be:
2054 * (Attach a journal_head if needed. Increments b_jcount)
2055 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2056 * ...
2057 * jh->b_transaction = xxx;
2058 * jbd2_journal_put_journal_head(jh);
2060 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2061 * because it has a non-zero b_transaction.
2065 * Give a buffer_head a journal_head.
2067 * Doesn't need the journal lock.
2068 * May sleep.
2070 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2072 struct journal_head *jh;
2073 struct journal_head *new_jh = NULL;
2075 repeat:
2076 if (!buffer_jbd(bh)) {
2077 new_jh = journal_alloc_journal_head();
2078 memset(new_jh, 0, sizeof(*new_jh));
2081 jbd_lock_bh_journal_head(bh);
2082 if (buffer_jbd(bh)) {
2083 jh = bh2jh(bh);
2084 } else {
2085 J_ASSERT_BH(bh,
2086 (atomic_read(&bh->b_count) > 0) ||
2087 (bh->b_page && bh->b_page->mapping));
2089 if (!new_jh) {
2090 jbd_unlock_bh_journal_head(bh);
2091 goto repeat;
2094 jh = new_jh;
2095 new_jh = NULL; /* We consumed it */
2096 set_buffer_jbd(bh);
2097 bh->b_private = jh;
2098 jh->b_bh = bh;
2099 get_bh(bh);
2100 BUFFER_TRACE(bh, "added journal_head");
2102 jh->b_jcount++;
2103 jbd_unlock_bh_journal_head(bh);
2104 if (new_jh)
2105 journal_free_journal_head(new_jh);
2106 return bh->b_private;
2110 * Grab a ref against this buffer_head's journal_head. If it ended up not
2111 * having a journal_head, return NULL
2113 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2115 struct journal_head *jh = NULL;
2117 jbd_lock_bh_journal_head(bh);
2118 if (buffer_jbd(bh)) {
2119 jh = bh2jh(bh);
2120 jh->b_jcount++;
2122 jbd_unlock_bh_journal_head(bh);
2123 return jh;
2126 static void __journal_remove_journal_head(struct buffer_head *bh)
2128 struct journal_head *jh = bh2jh(bh);
2130 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2132 get_bh(bh);
2133 if (jh->b_jcount == 0) {
2134 if (jh->b_transaction == NULL &&
2135 jh->b_next_transaction == NULL &&
2136 jh->b_cp_transaction == NULL) {
2137 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2138 J_ASSERT_BH(bh, buffer_jbd(bh));
2139 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2140 BUFFER_TRACE(bh, "remove journal_head");
2141 if (jh->b_frozen_data) {
2142 printk(KERN_WARNING "%s: freeing "
2143 "b_frozen_data\n",
2144 __func__);
2145 jbd2_free(jh->b_frozen_data, bh->b_size);
2147 if (jh->b_committed_data) {
2148 printk(KERN_WARNING "%s: freeing "
2149 "b_committed_data\n",
2150 __func__);
2151 jbd2_free(jh->b_committed_data, bh->b_size);
2153 bh->b_private = NULL;
2154 jh->b_bh = NULL; /* debug, really */
2155 clear_buffer_jbd(bh);
2156 __brelse(bh);
2157 journal_free_journal_head(jh);
2158 } else {
2159 BUFFER_TRACE(bh, "journal_head was locked");
2165 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
2166 * and has a zero b_jcount then remove and release its journal_head. If we did
2167 * see that the buffer is not used by any transaction we also "logically"
2168 * decrement ->b_count.
2170 * We in fact take an additional increment on ->b_count as a convenience,
2171 * because the caller usually wants to do additional things with the bh
2172 * after calling here.
2173 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
2174 * time. Once the caller has run __brelse(), the buffer is eligible for
2175 * reaping by try_to_free_buffers().
2177 void jbd2_journal_remove_journal_head(struct buffer_head *bh)
2179 jbd_lock_bh_journal_head(bh);
2180 __journal_remove_journal_head(bh);
2181 jbd_unlock_bh_journal_head(bh);
2185 * Drop a reference on the passed journal_head. If it fell to zero then try to
2186 * release the journal_head from the buffer_head.
2188 void jbd2_journal_put_journal_head(struct journal_head *jh)
2190 struct buffer_head *bh = jh2bh(jh);
2192 jbd_lock_bh_journal_head(bh);
2193 J_ASSERT_JH(jh, jh->b_jcount > 0);
2194 --jh->b_jcount;
2195 if (!jh->b_jcount && !jh->b_transaction) {
2196 __journal_remove_journal_head(bh);
2197 __brelse(bh);
2199 jbd_unlock_bh_journal_head(bh);
2203 * Initialize jbd inode head
2205 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2207 jinode->i_transaction = NULL;
2208 jinode->i_next_transaction = NULL;
2209 jinode->i_vfs_inode = inode;
2210 jinode->i_flags = 0;
2211 INIT_LIST_HEAD(&jinode->i_list);
2215 * Function to be called before we start removing inode from memory (i.e.,
2216 * clear_inode() is a fine place to be called from). It removes inode from
2217 * transaction's lists.
2219 void jbd2_journal_release_jbd_inode(journal_t *journal,
2220 struct jbd2_inode *jinode)
2222 int writeout = 0;
2224 if (!journal)
2225 return;
2226 restart:
2227 spin_lock(&journal->j_list_lock);
2228 /* Is commit writing out inode - we have to wait */
2229 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2230 wait_queue_head_t *wq;
2231 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2232 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2233 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2234 spin_unlock(&journal->j_list_lock);
2235 schedule();
2236 finish_wait(wq, &wait.wait);
2237 goto restart;
2240 /* Do we need to wait for data writeback? */
2241 if (journal->j_committing_transaction == jinode->i_transaction)
2242 writeout = 1;
2243 if (jinode->i_transaction) {
2244 list_del(&jinode->i_list);
2245 jinode->i_transaction = NULL;
2247 spin_unlock(&journal->j_list_lock);
2251 * debugfs tunables
2253 #ifdef CONFIG_JBD2_DEBUG
2254 u8 jbd2_journal_enable_debug __read_mostly;
2255 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2257 #define JBD2_DEBUG_NAME "jbd2-debug"
2259 static struct dentry *jbd2_debugfs_dir;
2260 static struct dentry *jbd2_debug;
2262 static void __init jbd2_create_debugfs_entry(void)
2264 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2265 if (jbd2_debugfs_dir)
2266 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2267 jbd2_debugfs_dir,
2268 &jbd2_journal_enable_debug);
2271 static void __exit jbd2_remove_debugfs_entry(void)
2273 debugfs_remove(jbd2_debug);
2274 debugfs_remove(jbd2_debugfs_dir);
2277 #else
2279 static void __init jbd2_create_debugfs_entry(void)
2283 static void __exit jbd2_remove_debugfs_entry(void)
2287 #endif
2289 #ifdef CONFIG_PROC_FS
2291 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2293 static void __init jbd2_create_jbd_stats_proc_entry(void)
2295 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2298 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2300 if (proc_jbd2_stats)
2301 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2304 #else
2306 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2307 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2309 #endif
2311 struct kmem_cache *jbd2_handle_cache;
2313 static int __init journal_init_handle_cache(void)
2315 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
2316 sizeof(handle_t),
2317 0, /* offset */
2318 SLAB_TEMPORARY, /* flags */
2319 NULL); /* ctor */
2320 if (jbd2_handle_cache == NULL) {
2321 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2322 return -ENOMEM;
2324 return 0;
2327 static void jbd2_journal_destroy_handle_cache(void)
2329 if (jbd2_handle_cache)
2330 kmem_cache_destroy(jbd2_handle_cache);
2334 * Module startup and shutdown
2337 static int __init journal_init_caches(void)
2339 int ret;
2341 ret = jbd2_journal_init_revoke_caches();
2342 if (ret == 0)
2343 ret = journal_init_jbd2_journal_head_cache();
2344 if (ret == 0)
2345 ret = journal_init_handle_cache();
2346 return ret;
2349 static void jbd2_journal_destroy_caches(void)
2351 jbd2_journal_destroy_revoke_caches();
2352 jbd2_journal_destroy_jbd2_journal_head_cache();
2353 jbd2_journal_destroy_handle_cache();
2356 static int __init journal_init(void)
2358 int ret;
2360 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2362 ret = journal_init_caches();
2363 if (ret == 0) {
2364 jbd2_create_debugfs_entry();
2365 jbd2_create_jbd_stats_proc_entry();
2366 } else {
2367 jbd2_journal_destroy_caches();
2369 return ret;
2372 static void __exit journal_exit(void)
2374 #ifdef CONFIG_JBD2_DEBUG
2375 int n = atomic_read(&nr_journal_heads);
2376 if (n)
2377 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2378 #endif
2379 jbd2_remove_debugfs_entry();
2380 jbd2_remove_jbd_stats_proc_entry();
2381 jbd2_journal_destroy_caches();
2385 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2386 * tracing infrastructure to map a dev_t to a device name.
2388 * The caller should use rcu_read_lock() in order to make sure the
2389 * device name stays valid until its done with it. We use
2390 * rcu_read_lock() as well to make sure we're safe in case the caller
2391 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2392 * nested.
2394 struct devname_cache {
2395 struct rcu_head rcu;
2396 dev_t device;
2397 char devname[BDEVNAME_SIZE];
2399 #define CACHE_SIZE_BITS 6
2400 static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2401 static DEFINE_SPINLOCK(devname_cache_lock);
2403 static void free_devcache(struct rcu_head *rcu)
2405 kfree(rcu);
2408 const char *jbd2_dev_to_name(dev_t device)
2410 int i = hash_32(device, CACHE_SIZE_BITS);
2411 char *ret;
2412 struct block_device *bd;
2414 rcu_read_lock();
2415 if (devcache[i] && devcache[i]->device == device) {
2416 ret = devcache[i]->devname;
2417 rcu_read_unlock();
2418 return ret;
2420 rcu_read_unlock();
2422 spin_lock(&devname_cache_lock);
2423 if (devcache[i]) {
2424 if (devcache[i]->device == device) {
2425 ret = devcache[i]->devname;
2426 spin_unlock(&devname_cache_lock);
2427 return ret;
2429 call_rcu(&devcache[i]->rcu, free_devcache);
2431 devcache[i] = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2432 if (!devcache[i]) {
2433 spin_unlock(&devname_cache_lock);
2434 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
2436 devcache[i]->device = device;
2437 bd = bdget(device);
2438 if (bd) {
2439 bdevname(bd, devcache[i]->devname);
2440 bdput(bd);
2441 } else
2442 __bdevname(device, devcache[i]->devname);
2443 ret = devcache[i]->devname;
2444 spin_unlock(&devname_cache_lock);
2445 return ret;
2447 EXPORT_SYMBOL(jbd2_dev_to_name);
2449 MODULE_LICENSE("GPL");
2450 module_init(journal_init);
2451 module_exit(journal_exit);