kbuild: use getopt_long(), not its _only() variant
[linux-2.6/linux-mips.git] / fs / jbd2 / journal.c
blob036880895bfc8c2e99c42f6fd900819315bd508a
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>
42 #include <linux/log2.h>
43 #include <linux/vmalloc.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/jbd2.h>
48 #include <asm/uaccess.h>
49 #include <asm/page.h>
51 EXPORT_SYMBOL(jbd2_journal_start);
52 EXPORT_SYMBOL(jbd2_journal_restart);
53 EXPORT_SYMBOL(jbd2_journal_extend);
54 EXPORT_SYMBOL(jbd2_journal_stop);
55 EXPORT_SYMBOL(jbd2_journal_lock_updates);
56 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
57 EXPORT_SYMBOL(jbd2_journal_get_write_access);
58 EXPORT_SYMBOL(jbd2_journal_get_create_access);
59 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
60 EXPORT_SYMBOL(jbd2_journal_set_triggers);
61 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
62 EXPORT_SYMBOL(jbd2_journal_release_buffer);
63 EXPORT_SYMBOL(jbd2_journal_forget);
64 #if 0
65 EXPORT_SYMBOL(journal_sync_buffer);
66 #endif
67 EXPORT_SYMBOL(jbd2_journal_flush);
68 EXPORT_SYMBOL(jbd2_journal_revoke);
70 EXPORT_SYMBOL(jbd2_journal_init_dev);
71 EXPORT_SYMBOL(jbd2_journal_init_inode);
72 EXPORT_SYMBOL(jbd2_journal_update_format);
73 EXPORT_SYMBOL(jbd2_journal_check_used_features);
74 EXPORT_SYMBOL(jbd2_journal_check_available_features);
75 EXPORT_SYMBOL(jbd2_journal_set_features);
76 EXPORT_SYMBOL(jbd2_journal_load);
77 EXPORT_SYMBOL(jbd2_journal_destroy);
78 EXPORT_SYMBOL(jbd2_journal_abort);
79 EXPORT_SYMBOL(jbd2_journal_errno);
80 EXPORT_SYMBOL(jbd2_journal_ack_err);
81 EXPORT_SYMBOL(jbd2_journal_clear_err);
82 EXPORT_SYMBOL(jbd2_log_wait_commit);
83 EXPORT_SYMBOL(jbd2_log_start_commit);
84 EXPORT_SYMBOL(jbd2_journal_start_commit);
85 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
86 EXPORT_SYMBOL(jbd2_journal_wipe);
87 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
88 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
89 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
90 EXPORT_SYMBOL(jbd2_journal_force_commit);
91 EXPORT_SYMBOL(jbd2_journal_file_inode);
92 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
93 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
94 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
96 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
97 static void __journal_abort_soft (journal_t *journal, int errno);
98 static int jbd2_journal_create_slab(size_t slab_size);
101 * Helper function used to manage commit timeouts
104 static void commit_timeout(unsigned long __data)
106 struct task_struct * p = (struct task_struct *) __data;
108 wake_up_process(p);
112 * kjournald2: The main thread function used to manage a logging device
113 * journal.
115 * This kernel thread is responsible for two things:
117 * 1) COMMIT: Every so often we need to commit the current state of the
118 * filesystem to disk. The journal thread is responsible for writing
119 * all of the metadata buffers to disk.
121 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
122 * of the data in that part of the log has been rewritten elsewhere on
123 * the disk. Flushing these old buffers to reclaim space in the log is
124 * known as checkpointing, and this thread is responsible for that job.
127 static int kjournald2(void *arg)
129 journal_t *journal = arg;
130 transaction_t *transaction;
133 * Set up an interval timer which can be used to trigger a commit wakeup
134 * after the commit interval expires
136 setup_timer(&journal->j_commit_timer, commit_timeout,
137 (unsigned long)current);
139 /* Record that the journal thread is running */
140 journal->j_task = current;
141 wake_up(&journal->j_wait_done_commit);
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, "jbd2/%s",
227 journal->j_devname);
228 if (IS_ERR(t))
229 return PTR_ERR(t);
231 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
232 return 0;
235 static void journal_kill_thread(journal_t *journal)
237 spin_lock(&journal->j_state_lock);
238 journal->j_flags |= JBD2_UNMOUNT;
240 while (journal->j_task) {
241 wake_up(&journal->j_wait_commit);
242 spin_unlock(&journal->j_state_lock);
243 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
244 spin_lock(&journal->j_state_lock);
246 spin_unlock(&journal->j_state_lock);
250 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
252 * Writes a metadata buffer to a given disk block. The actual IO is not
253 * performed but a new buffer_head is constructed which labels the data
254 * to be written with the correct destination disk block.
256 * Any magic-number escaping which needs to be done will cause a
257 * copy-out here. If the buffer happens to start with the
258 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
259 * magic number is only written to the log for descripter blocks. In
260 * this case, we copy the data and replace the first word with 0, and we
261 * return a result code which indicates that this buffer needs to be
262 * marked as an escaped buffer in the corresponding log descriptor
263 * block. The missing word can then be restored when the block is read
264 * during recovery.
266 * If the source buffer has already been modified by a new transaction
267 * since we took the last commit snapshot, we use the frozen copy of
268 * that data for IO. If we end up using the existing buffer_head's data
269 * for the write, then we *have* to lock the buffer to prevent anyone
270 * else from using and possibly modifying it while the IO is in
271 * progress.
273 * The function returns a pointer to the buffer_heads to be used for IO.
275 * We assume that the journal has already been locked in this function.
277 * Return value:
278 * <0: Error
279 * >=0: Finished OK
281 * On success:
282 * Bit 0 set == escape performed on the data
283 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
286 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
287 struct journal_head *jh_in,
288 struct journal_head **jh_out,
289 unsigned long long blocknr)
291 int need_copy_out = 0;
292 int done_copy_out = 0;
293 int do_escape = 0;
294 char *mapped_data;
295 struct buffer_head *new_bh;
296 struct journal_head *new_jh;
297 struct page *new_page;
298 unsigned int new_offset;
299 struct buffer_head *bh_in = jh2bh(jh_in);
300 journal_t *journal = transaction->t_journal;
303 * The buffer really shouldn't be locked: only the current committing
304 * transaction is allowed to write it, so nobody else is allowed
305 * to do any IO.
307 * akpm: except if we're journalling data, and write() output is
308 * also part of a shared mapping, and another thread has
309 * decided to launch a writepage() against this buffer.
311 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
313 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
314 /* keep subsequent assertions sane */
315 new_bh->b_state = 0;
316 init_buffer(new_bh, NULL, NULL);
317 atomic_set(&new_bh->b_count, 1);
318 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
321 * If a new transaction has already done a buffer copy-out, then
322 * we use that version of the data for the commit.
324 jbd_lock_bh_state(bh_in);
325 repeat:
326 if (jh_in->b_frozen_data) {
327 done_copy_out = 1;
328 new_page = virt_to_page(jh_in->b_frozen_data);
329 new_offset = offset_in_page(jh_in->b_frozen_data);
330 } else {
331 new_page = jh2bh(jh_in)->b_page;
332 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
335 mapped_data = kmap_atomic(new_page, KM_USER0);
337 * Fire data frozen trigger if data already wasn't frozen. Do this
338 * before checking for escaping, as the trigger may modify the magic
339 * offset. If a copy-out happens afterwards, it will have the correct
340 * data in the buffer.
342 if (!done_copy_out)
343 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
344 jh_in->b_triggers);
347 * Check for escaping
349 if (*((__be32 *)(mapped_data + new_offset)) ==
350 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
351 need_copy_out = 1;
352 do_escape = 1;
354 kunmap_atomic(mapped_data, KM_USER0);
357 * Do we need to do a data copy?
359 if (need_copy_out && !done_copy_out) {
360 char *tmp;
362 jbd_unlock_bh_state(bh_in);
363 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
364 if (!tmp) {
365 jbd2_journal_put_journal_head(new_jh);
366 return -ENOMEM;
368 jbd_lock_bh_state(bh_in);
369 if (jh_in->b_frozen_data) {
370 jbd2_free(tmp, bh_in->b_size);
371 goto repeat;
374 jh_in->b_frozen_data = tmp;
375 mapped_data = kmap_atomic(new_page, KM_USER0);
376 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
377 kunmap_atomic(mapped_data, KM_USER0);
379 new_page = virt_to_page(tmp);
380 new_offset = offset_in_page(tmp);
381 done_copy_out = 1;
384 * This isn't strictly necessary, as we're using frozen
385 * data for the escaping, but it keeps consistency with
386 * b_frozen_data usage.
388 jh_in->b_frozen_triggers = jh_in->b_triggers;
392 * Did we need to do an escaping? Now we've done all the
393 * copying, we can finally do so.
395 if (do_escape) {
396 mapped_data = kmap_atomic(new_page, KM_USER0);
397 *((unsigned int *)(mapped_data + new_offset)) = 0;
398 kunmap_atomic(mapped_data, KM_USER0);
401 set_bh_page(new_bh, new_page, new_offset);
402 new_jh->b_transaction = NULL;
403 new_bh->b_size = jh2bh(jh_in)->b_size;
404 new_bh->b_bdev = transaction->t_journal->j_dev;
405 new_bh->b_blocknr = blocknr;
406 set_buffer_mapped(new_bh);
407 set_buffer_dirty(new_bh);
409 *jh_out = new_jh;
412 * The to-be-written buffer needs to get moved to the io queue,
413 * and the original buffer whose contents we are shadowing or
414 * copying is moved to the transaction's shadow queue.
416 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
417 spin_lock(&journal->j_list_lock);
418 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
419 spin_unlock(&journal->j_list_lock);
420 jbd_unlock_bh_state(bh_in);
422 JBUFFER_TRACE(new_jh, "file as BJ_IO");
423 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
425 return do_escape | (done_copy_out << 1);
429 * Allocation code for the journal file. Manage the space left in the
430 * journal, so that we can begin checkpointing when appropriate.
434 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
436 * Called with the journal already locked.
438 * Called under j_state_lock
441 int __jbd2_log_space_left(journal_t *journal)
443 int left = journal->j_free;
445 assert_spin_locked(&journal->j_state_lock);
448 * Be pessimistic here about the number of those free blocks which
449 * might be required for log descriptor control blocks.
452 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
454 left -= MIN_LOG_RESERVED_BLOCKS;
456 if (left <= 0)
457 return 0;
458 left -= (left >> 3);
459 return left;
463 * Called under j_state_lock. Returns true if a transaction commit was started.
465 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
468 * Are we already doing a recent enough commit?
470 if (!tid_geq(journal->j_commit_request, target)) {
472 * We want a new commit: OK, mark the request and wakup the
473 * commit thread. We do _not_ do the commit ourselves.
476 journal->j_commit_request = target;
477 jbd_debug(1, "JBD: requesting commit %d/%d\n",
478 journal->j_commit_request,
479 journal->j_commit_sequence);
480 wake_up(&journal->j_wait_commit);
481 return 1;
483 return 0;
486 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
488 int ret;
490 spin_lock(&journal->j_state_lock);
491 ret = __jbd2_log_start_commit(journal, tid);
492 spin_unlock(&journal->j_state_lock);
493 return ret;
497 * Force and wait upon a commit if the calling process is not within
498 * transaction. This is used for forcing out undo-protected data which contains
499 * bitmaps, when the fs is running out of space.
501 * We can only force the running transaction if we don't have an active handle;
502 * otherwise, we will deadlock.
504 * Returns true if a transaction was started.
506 int jbd2_journal_force_commit_nested(journal_t *journal)
508 transaction_t *transaction = NULL;
509 tid_t tid;
511 spin_lock(&journal->j_state_lock);
512 if (journal->j_running_transaction && !current->journal_info) {
513 transaction = journal->j_running_transaction;
514 __jbd2_log_start_commit(journal, transaction->t_tid);
515 } else if (journal->j_committing_transaction)
516 transaction = journal->j_committing_transaction;
518 if (!transaction) {
519 spin_unlock(&journal->j_state_lock);
520 return 0; /* Nothing to retry */
523 tid = transaction->t_tid;
524 spin_unlock(&journal->j_state_lock);
525 jbd2_log_wait_commit(journal, tid);
526 return 1;
530 * Start a commit of the current running transaction (if any). Returns true
531 * if a transaction is going to be committed (or is currently already
532 * committing), and fills its tid in at *ptid
534 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
536 int ret = 0;
538 spin_lock(&journal->j_state_lock);
539 if (journal->j_running_transaction) {
540 tid_t tid = journal->j_running_transaction->t_tid;
542 __jbd2_log_start_commit(journal, tid);
543 /* There's a running transaction and we've just made sure
544 * it's commit has been scheduled. */
545 if (ptid)
546 *ptid = tid;
547 ret = 1;
548 } else if (journal->j_committing_transaction) {
550 * If ext3_write_super() recently started a commit, then we
551 * have to wait for completion of that transaction
553 if (ptid)
554 *ptid = journal->j_committing_transaction->t_tid;
555 ret = 1;
557 spin_unlock(&journal->j_state_lock);
558 return ret;
562 * Wait for a specified commit to complete.
563 * The caller may not hold the journal lock.
565 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
567 int err = 0;
569 #ifdef CONFIG_JBD2_DEBUG
570 spin_lock(&journal->j_state_lock);
571 if (!tid_geq(journal->j_commit_request, tid)) {
572 printk(KERN_EMERG
573 "%s: error: j_commit_request=%d, tid=%d\n",
574 __func__, journal->j_commit_request, tid);
576 spin_unlock(&journal->j_state_lock);
577 #endif
578 spin_lock(&journal->j_state_lock);
579 while (tid_gt(tid, journal->j_commit_sequence)) {
580 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
581 tid, journal->j_commit_sequence);
582 wake_up(&journal->j_wait_commit);
583 spin_unlock(&journal->j_state_lock);
584 wait_event(journal->j_wait_done_commit,
585 !tid_gt(tid, journal->j_commit_sequence));
586 spin_lock(&journal->j_state_lock);
588 spin_unlock(&journal->j_state_lock);
590 if (unlikely(is_journal_aborted(journal))) {
591 printk(KERN_EMERG "journal commit I/O error\n");
592 err = -EIO;
594 return err;
598 * Log buffer allocation routines:
601 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
603 unsigned long blocknr;
605 spin_lock(&journal->j_state_lock);
606 J_ASSERT(journal->j_free > 1);
608 blocknr = journal->j_head;
609 journal->j_head++;
610 journal->j_free--;
611 if (journal->j_head == journal->j_last)
612 journal->j_head = journal->j_first;
613 spin_unlock(&journal->j_state_lock);
614 return jbd2_journal_bmap(journal, blocknr, retp);
618 * Conversion of logical to physical block numbers for the journal
620 * On external journals the journal blocks are identity-mapped, so
621 * this is a no-op. If needed, we can use j_blk_offset - everything is
622 * ready.
624 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
625 unsigned long long *retp)
627 int err = 0;
628 unsigned long long ret;
630 if (journal->j_inode) {
631 ret = bmap(journal->j_inode, blocknr);
632 if (ret)
633 *retp = ret;
634 else {
635 printk(KERN_ALERT "%s: journal block not found "
636 "at offset %lu on %s\n",
637 __func__, blocknr, journal->j_devname);
638 err = -EIO;
639 __journal_abort_soft(journal, err);
641 } else {
642 *retp = blocknr; /* +journal->j_blk_offset */
644 return err;
648 * We play buffer_head aliasing tricks to write data/metadata blocks to
649 * the journal without copying their contents, but for journal
650 * descriptor blocks we do need to generate bona fide buffers.
652 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
653 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
654 * But we don't bother doing that, so there will be coherency problems with
655 * mmaps of blockdevs which hold live JBD-controlled filesystems.
657 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
659 struct buffer_head *bh;
660 unsigned long long blocknr;
661 int err;
663 err = jbd2_journal_next_log_block(journal, &blocknr);
665 if (err)
666 return NULL;
668 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
669 if (!bh)
670 return NULL;
671 lock_buffer(bh);
672 memset(bh->b_data, 0, journal->j_blocksize);
673 set_buffer_uptodate(bh);
674 unlock_buffer(bh);
675 BUFFER_TRACE(bh, "return this buffer");
676 return jbd2_journal_add_journal_head(bh);
679 struct jbd2_stats_proc_session {
680 journal_t *journal;
681 struct transaction_stats_s *stats;
682 int start;
683 int max;
686 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
688 return *pos ? NULL : SEQ_START_TOKEN;
691 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
693 return NULL;
696 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
698 struct jbd2_stats_proc_session *s = seq->private;
700 if (v != SEQ_START_TOKEN)
701 return 0;
702 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
703 s->stats->ts_tid,
704 s->journal->j_max_transaction_buffers);
705 if (s->stats->ts_tid == 0)
706 return 0;
707 seq_printf(seq, "average: \n %ums waiting for transaction\n",
708 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
709 seq_printf(seq, " %ums running transaction\n",
710 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
711 seq_printf(seq, " %ums transaction was being locked\n",
712 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
713 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
714 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
715 seq_printf(seq, " %ums logging transaction\n",
716 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
717 seq_printf(seq, " %lluus average transaction commit time\n",
718 div_u64(s->journal->j_average_commit_time, 1000));
719 seq_printf(seq, " %lu handles per transaction\n",
720 s->stats->run.rs_handle_count / s->stats->ts_tid);
721 seq_printf(seq, " %lu blocks per transaction\n",
722 s->stats->run.rs_blocks / s->stats->ts_tid);
723 seq_printf(seq, " %lu logged blocks per transaction\n",
724 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
725 return 0;
728 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
732 static const struct seq_operations jbd2_seq_info_ops = {
733 .start = jbd2_seq_info_start,
734 .next = jbd2_seq_info_next,
735 .stop = jbd2_seq_info_stop,
736 .show = jbd2_seq_info_show,
739 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
741 journal_t *journal = PDE(inode)->data;
742 struct jbd2_stats_proc_session *s;
743 int rc, size;
745 s = kmalloc(sizeof(*s), GFP_KERNEL);
746 if (s == NULL)
747 return -ENOMEM;
748 size = sizeof(struct transaction_stats_s);
749 s->stats = kmalloc(size, GFP_KERNEL);
750 if (s->stats == NULL) {
751 kfree(s);
752 return -ENOMEM;
754 spin_lock(&journal->j_history_lock);
755 memcpy(s->stats, &journal->j_stats, size);
756 s->journal = journal;
757 spin_unlock(&journal->j_history_lock);
759 rc = seq_open(file, &jbd2_seq_info_ops);
760 if (rc == 0) {
761 struct seq_file *m = file->private_data;
762 m->private = s;
763 } else {
764 kfree(s->stats);
765 kfree(s);
767 return rc;
771 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
773 struct seq_file *seq = file->private_data;
774 struct jbd2_stats_proc_session *s = seq->private;
775 kfree(s->stats);
776 kfree(s);
777 return seq_release(inode, file);
780 static const struct file_operations jbd2_seq_info_fops = {
781 .owner = THIS_MODULE,
782 .open = jbd2_seq_info_open,
783 .read = seq_read,
784 .llseek = seq_lseek,
785 .release = jbd2_seq_info_release,
788 static struct proc_dir_entry *proc_jbd2_stats;
790 static void jbd2_stats_proc_init(journal_t *journal)
792 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
793 if (journal->j_proc_entry) {
794 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
795 &jbd2_seq_info_fops, journal);
799 static void jbd2_stats_proc_exit(journal_t *journal)
801 remove_proc_entry("info", journal->j_proc_entry);
802 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
806 * Management for journal control blocks: functions to create and
807 * destroy journal_t structures, and to initialise and read existing
808 * journal blocks from disk. */
810 /* First: create and setup a journal_t object in memory. We initialise
811 * very few fields yet: that has to wait until we have created the
812 * journal structures from from scratch, or loaded them from disk. */
814 static journal_t * journal_init_common (void)
816 journal_t *journal;
817 int err;
819 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
820 if (!journal)
821 goto fail;
823 init_waitqueue_head(&journal->j_wait_transaction_locked);
824 init_waitqueue_head(&journal->j_wait_logspace);
825 init_waitqueue_head(&journal->j_wait_done_commit);
826 init_waitqueue_head(&journal->j_wait_checkpoint);
827 init_waitqueue_head(&journal->j_wait_commit);
828 init_waitqueue_head(&journal->j_wait_updates);
829 mutex_init(&journal->j_barrier);
830 mutex_init(&journal->j_checkpoint_mutex);
831 spin_lock_init(&journal->j_revoke_lock);
832 spin_lock_init(&journal->j_list_lock);
833 spin_lock_init(&journal->j_state_lock);
835 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
836 journal->j_min_batch_time = 0;
837 journal->j_max_batch_time = 15000; /* 15ms */
839 /* The journal is marked for error until we succeed with recovery! */
840 journal->j_flags = JBD2_ABORT;
842 /* Set up a default-sized revoke table for the new mount. */
843 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
844 if (err) {
845 kfree(journal);
846 goto fail;
849 spin_lock_init(&journal->j_history_lock);
851 return journal;
852 fail:
853 return NULL;
856 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
858 * Create a journal structure assigned some fixed set of disk blocks to
859 * the journal. We don't actually touch those disk blocks yet, but we
860 * need to set up all of the mapping information to tell the journaling
861 * system where the journal blocks are.
866 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
867 * @bdev: Block device on which to create the journal
868 * @fs_dev: Device which hold journalled filesystem for this journal.
869 * @start: Block nr Start of journal.
870 * @len: Length of the journal in blocks.
871 * @blocksize: blocksize of journalling device
873 * Returns: a newly created journal_t *
875 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
876 * range of blocks on an arbitrary block device.
879 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
880 struct block_device *fs_dev,
881 unsigned long long start, int len, int blocksize)
883 journal_t *journal = journal_init_common();
884 struct buffer_head *bh;
885 char *p;
886 int n;
888 if (!journal)
889 return NULL;
891 /* journal descriptor can store up to n blocks -bzzz */
892 journal->j_blocksize = blocksize;
893 jbd2_stats_proc_init(journal);
894 n = journal->j_blocksize / sizeof(journal_block_tag_t);
895 journal->j_wbufsize = n;
896 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
897 if (!journal->j_wbuf) {
898 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
899 __func__);
900 goto out_err;
902 journal->j_dev = bdev;
903 journal->j_fs_dev = fs_dev;
904 journal->j_blk_offset = start;
905 journal->j_maxlen = len;
906 bdevname(journal->j_dev, journal->j_devname);
907 p = journal->j_devname;
908 while ((p = strchr(p, '/')))
909 *p = '!';
911 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
912 if (!bh) {
913 printk(KERN_ERR
914 "%s: Cannot get buffer for journal superblock\n",
915 __func__);
916 goto out_err;
918 journal->j_sb_buffer = bh;
919 journal->j_superblock = (journal_superblock_t *)bh->b_data;
921 return journal;
922 out_err:
923 kfree(journal->j_wbuf);
924 jbd2_stats_proc_exit(journal);
925 kfree(journal);
926 return NULL;
930 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
931 * @inode: An inode to create the journal in
933 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
934 * the journal. The inode must exist already, must support bmap() and
935 * must have all data blocks preallocated.
937 journal_t * jbd2_journal_init_inode (struct inode *inode)
939 struct buffer_head *bh;
940 journal_t *journal = journal_init_common();
941 char *p;
942 int err;
943 int n;
944 unsigned long long blocknr;
946 if (!journal)
947 return NULL;
949 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
950 journal->j_inode = inode;
951 bdevname(journal->j_dev, journal->j_devname);
952 p = journal->j_devname;
953 while ((p = strchr(p, '/')))
954 *p = '!';
955 p = journal->j_devname + strlen(journal->j_devname);
956 sprintf(p, "-%lu", journal->j_inode->i_ino);
957 jbd_debug(1,
958 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
959 journal, inode->i_sb->s_id, inode->i_ino,
960 (long long) inode->i_size,
961 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
963 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
964 journal->j_blocksize = inode->i_sb->s_blocksize;
965 jbd2_stats_proc_init(journal);
967 /* journal descriptor can store up to n blocks -bzzz */
968 n = journal->j_blocksize / sizeof(journal_block_tag_t);
969 journal->j_wbufsize = n;
970 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
971 if (!journal->j_wbuf) {
972 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
973 __func__);
974 goto out_err;
977 err = jbd2_journal_bmap(journal, 0, &blocknr);
978 /* If that failed, give up */
979 if (err) {
980 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
981 __func__);
982 goto out_err;
985 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
986 if (!bh) {
987 printk(KERN_ERR
988 "%s: Cannot get buffer for journal superblock\n",
989 __func__);
990 goto out_err;
992 journal->j_sb_buffer = bh;
993 journal->j_superblock = (journal_superblock_t *)bh->b_data;
995 return journal;
996 out_err:
997 kfree(journal->j_wbuf);
998 jbd2_stats_proc_exit(journal);
999 kfree(journal);
1000 return NULL;
1004 * If the journal init or create aborts, we need to mark the journal
1005 * superblock as being NULL to prevent the journal destroy from writing
1006 * back a bogus superblock.
1008 static void journal_fail_superblock (journal_t *journal)
1010 struct buffer_head *bh = journal->j_sb_buffer;
1011 brelse(bh);
1012 journal->j_sb_buffer = NULL;
1016 * Given a journal_t structure, initialise the various fields for
1017 * startup of a new journaling session. We use this both when creating
1018 * a journal, and after recovering an old journal to reset it for
1019 * subsequent use.
1022 static int journal_reset(journal_t *journal)
1024 journal_superblock_t *sb = journal->j_superblock;
1025 unsigned long long first, last;
1027 first = be32_to_cpu(sb->s_first);
1028 last = be32_to_cpu(sb->s_maxlen);
1029 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1030 printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
1031 first, last);
1032 journal_fail_superblock(journal);
1033 return -EINVAL;
1036 journal->j_first = first;
1037 journal->j_last = last;
1039 journal->j_head = first;
1040 journal->j_tail = first;
1041 journal->j_free = last - first;
1043 journal->j_tail_sequence = journal->j_transaction_sequence;
1044 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1045 journal->j_commit_request = journal->j_commit_sequence;
1047 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1049 /* Add the dynamic fields and write it to disk. */
1050 jbd2_journal_update_superblock(journal, 1);
1051 return jbd2_journal_start_thread(journal);
1055 * void jbd2_journal_update_superblock() - Update journal sb on disk.
1056 * @journal: The journal to update.
1057 * @wait: Set to '0' if you don't want to wait for IO completion.
1059 * Update a journal's dynamic superblock fields and write it to disk,
1060 * optionally waiting for the IO to complete.
1062 void jbd2_journal_update_superblock(journal_t *journal, int wait)
1064 journal_superblock_t *sb = journal->j_superblock;
1065 struct buffer_head *bh = journal->j_sb_buffer;
1068 * As a special case, if the on-disk copy is already marked as needing
1069 * no recovery (s_start == 0) and there are no outstanding transactions
1070 * in the filesystem, then we can safely defer the superblock update
1071 * until the next commit by setting JBD2_FLUSHED. This avoids
1072 * attempting a write to a potential-readonly device.
1074 if (sb->s_start == 0 && journal->j_tail_sequence ==
1075 journal->j_transaction_sequence) {
1076 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1077 "(start %ld, seq %d, errno %d)\n",
1078 journal->j_tail, journal->j_tail_sequence,
1079 journal->j_errno);
1080 goto out;
1083 if (buffer_write_io_error(bh)) {
1085 * Oh, dear. A previous attempt to write the journal
1086 * superblock failed. This could happen because the
1087 * USB device was yanked out. Or it could happen to
1088 * be a transient write error and maybe the block will
1089 * be remapped. Nothing we can do but to retry the
1090 * write and hope for the best.
1092 printk(KERN_ERR "JBD2: previous I/O error detected "
1093 "for journal superblock update for %s.\n",
1094 journal->j_devname);
1095 clear_buffer_write_io_error(bh);
1096 set_buffer_uptodate(bh);
1099 spin_lock(&journal->j_state_lock);
1100 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1101 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1103 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1104 sb->s_start = cpu_to_be32(journal->j_tail);
1105 sb->s_errno = cpu_to_be32(journal->j_errno);
1106 spin_unlock(&journal->j_state_lock);
1108 BUFFER_TRACE(bh, "marking dirty");
1109 mark_buffer_dirty(bh);
1110 if (wait) {
1111 sync_dirty_buffer(bh);
1112 if (buffer_write_io_error(bh)) {
1113 printk(KERN_ERR "JBD2: I/O error detected "
1114 "when updating journal superblock for %s.\n",
1115 journal->j_devname);
1116 clear_buffer_write_io_error(bh);
1117 set_buffer_uptodate(bh);
1119 } else
1120 ll_rw_block(SWRITE, 1, &bh);
1122 out:
1123 /* If we have just flushed the log (by marking s_start==0), then
1124 * any future commit will have to be careful to update the
1125 * superblock again to re-record the true start of the log. */
1127 spin_lock(&journal->j_state_lock);
1128 if (sb->s_start)
1129 journal->j_flags &= ~JBD2_FLUSHED;
1130 else
1131 journal->j_flags |= JBD2_FLUSHED;
1132 spin_unlock(&journal->j_state_lock);
1136 * Read the superblock for a given journal, performing initial
1137 * validation of the format.
1140 static int journal_get_superblock(journal_t *journal)
1142 struct buffer_head *bh;
1143 journal_superblock_t *sb;
1144 int err = -EIO;
1146 bh = journal->j_sb_buffer;
1148 J_ASSERT(bh != NULL);
1149 if (!buffer_uptodate(bh)) {
1150 ll_rw_block(READ, 1, &bh);
1151 wait_on_buffer(bh);
1152 if (!buffer_uptodate(bh)) {
1153 printk (KERN_ERR
1154 "JBD: IO error reading journal superblock\n");
1155 goto out;
1159 sb = journal->j_superblock;
1161 err = -EINVAL;
1163 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1164 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1165 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1166 goto out;
1169 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1170 case JBD2_SUPERBLOCK_V1:
1171 journal->j_format_version = 1;
1172 break;
1173 case JBD2_SUPERBLOCK_V2:
1174 journal->j_format_version = 2;
1175 break;
1176 default:
1177 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1178 goto out;
1181 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1182 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1183 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1184 printk (KERN_WARNING "JBD: journal file too short\n");
1185 goto out;
1188 return 0;
1190 out:
1191 journal_fail_superblock(journal);
1192 return err;
1196 * Load the on-disk journal superblock and read the key fields into the
1197 * journal_t.
1200 static int load_superblock(journal_t *journal)
1202 int err;
1203 journal_superblock_t *sb;
1205 err = journal_get_superblock(journal);
1206 if (err)
1207 return err;
1209 sb = journal->j_superblock;
1211 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1212 journal->j_tail = be32_to_cpu(sb->s_start);
1213 journal->j_first = be32_to_cpu(sb->s_first);
1214 journal->j_last = be32_to_cpu(sb->s_maxlen);
1215 journal->j_errno = be32_to_cpu(sb->s_errno);
1217 return 0;
1222 * int jbd2_journal_load() - Read journal from disk.
1223 * @journal: Journal to act on.
1225 * Given a journal_t structure which tells us which disk blocks contain
1226 * a journal, read the journal from disk to initialise the in-memory
1227 * structures.
1229 int jbd2_journal_load(journal_t *journal)
1231 int err;
1232 journal_superblock_t *sb;
1234 err = load_superblock(journal);
1235 if (err)
1236 return err;
1238 sb = journal->j_superblock;
1239 /* If this is a V2 superblock, then we have to check the
1240 * features flags on it. */
1242 if (journal->j_format_version >= 2) {
1243 if ((sb->s_feature_ro_compat &
1244 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1245 (sb->s_feature_incompat &
1246 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1247 printk (KERN_WARNING
1248 "JBD: Unrecognised features on journal\n");
1249 return -EINVAL;
1254 * Create a slab for this blocksize
1256 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1257 if (err)
1258 return err;
1260 /* Let the recovery code check whether it needs to recover any
1261 * data from the journal. */
1262 if (jbd2_journal_recover(journal))
1263 goto recovery_error;
1265 if (journal->j_failed_commit) {
1266 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1267 "is corrupt.\n", journal->j_failed_commit,
1268 journal->j_devname);
1269 return -EIO;
1272 /* OK, we've finished with the dynamic journal bits:
1273 * reinitialise the dynamic contents of the superblock in memory
1274 * and reset them on disk. */
1275 if (journal_reset(journal))
1276 goto recovery_error;
1278 journal->j_flags &= ~JBD2_ABORT;
1279 journal->j_flags |= JBD2_LOADED;
1280 return 0;
1282 recovery_error:
1283 printk (KERN_WARNING "JBD: recovery failed\n");
1284 return -EIO;
1288 * void jbd2_journal_destroy() - Release a journal_t structure.
1289 * @journal: Journal to act on.
1291 * Release a journal_t structure once it is no longer in use by the
1292 * journaled object.
1293 * Return <0 if we couldn't clean up the journal.
1295 int jbd2_journal_destroy(journal_t *journal)
1297 int err = 0;
1299 /* Wait for the commit thread to wake up and die. */
1300 journal_kill_thread(journal);
1302 /* Force a final log commit */
1303 if (journal->j_running_transaction)
1304 jbd2_journal_commit_transaction(journal);
1306 /* Force any old transactions to disk */
1308 /* Totally anal locking here... */
1309 spin_lock(&journal->j_list_lock);
1310 while (journal->j_checkpoint_transactions != NULL) {
1311 spin_unlock(&journal->j_list_lock);
1312 mutex_lock(&journal->j_checkpoint_mutex);
1313 jbd2_log_do_checkpoint(journal);
1314 mutex_unlock(&journal->j_checkpoint_mutex);
1315 spin_lock(&journal->j_list_lock);
1318 J_ASSERT(journal->j_running_transaction == NULL);
1319 J_ASSERT(journal->j_committing_transaction == NULL);
1320 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1321 spin_unlock(&journal->j_list_lock);
1323 if (journal->j_sb_buffer) {
1324 if (!is_journal_aborted(journal)) {
1325 /* We can now mark the journal as empty. */
1326 journal->j_tail = 0;
1327 journal->j_tail_sequence =
1328 ++journal->j_transaction_sequence;
1329 jbd2_journal_update_superblock(journal, 1);
1330 } else {
1331 err = -EIO;
1333 brelse(journal->j_sb_buffer);
1336 if (journal->j_proc_entry)
1337 jbd2_stats_proc_exit(journal);
1338 if (journal->j_inode)
1339 iput(journal->j_inode);
1340 if (journal->j_revoke)
1341 jbd2_journal_destroy_revoke(journal);
1342 kfree(journal->j_wbuf);
1343 kfree(journal);
1345 return err;
1350 *int jbd2_journal_check_used_features () - Check if features specified are used.
1351 * @journal: Journal to check.
1352 * @compat: bitmask of compatible features
1353 * @ro: bitmask of features that force read-only mount
1354 * @incompat: bitmask of incompatible features
1356 * Check whether the journal uses all of a given set of
1357 * features. Return true (non-zero) if it does.
1360 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1361 unsigned long ro, unsigned long incompat)
1363 journal_superblock_t *sb;
1365 if (!compat && !ro && !incompat)
1366 return 1;
1367 if (journal->j_format_version == 1)
1368 return 0;
1370 sb = journal->j_superblock;
1372 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1373 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1374 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1375 return 1;
1377 return 0;
1381 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1382 * @journal: Journal to check.
1383 * @compat: bitmask of compatible features
1384 * @ro: bitmask of features that force read-only mount
1385 * @incompat: bitmask of incompatible features
1387 * Check whether the journaling code supports the use of
1388 * all of a given set of features on this journal. Return true
1389 * (non-zero) if it can. */
1391 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1392 unsigned long ro, unsigned long incompat)
1394 journal_superblock_t *sb;
1396 if (!compat && !ro && !incompat)
1397 return 1;
1399 sb = journal->j_superblock;
1401 /* We can support any known requested features iff the
1402 * superblock is in version 2. Otherwise we fail to support any
1403 * extended sb features. */
1405 if (journal->j_format_version != 2)
1406 return 0;
1408 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1409 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1410 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1411 return 1;
1413 return 0;
1417 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1418 * @journal: Journal to act on.
1419 * @compat: bitmask of compatible features
1420 * @ro: bitmask of features that force read-only mount
1421 * @incompat: bitmask of incompatible features
1423 * Mark a given journal feature as present on the
1424 * superblock. Returns true if the requested features could be set.
1428 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1429 unsigned long ro, unsigned long incompat)
1431 journal_superblock_t *sb;
1433 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1434 return 1;
1436 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1437 return 0;
1439 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1440 compat, ro, incompat);
1442 sb = journal->j_superblock;
1444 sb->s_feature_compat |= cpu_to_be32(compat);
1445 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1446 sb->s_feature_incompat |= cpu_to_be32(incompat);
1448 return 1;
1452 * jbd2_journal_clear_features () - Clear a given journal feature in the
1453 * superblock
1454 * @journal: Journal to act on.
1455 * @compat: bitmask of compatible features
1456 * @ro: bitmask of features that force read-only mount
1457 * @incompat: bitmask of incompatible features
1459 * Clear a given journal feature as present on the
1460 * superblock.
1462 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1463 unsigned long ro, unsigned long incompat)
1465 journal_superblock_t *sb;
1467 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1468 compat, ro, incompat);
1470 sb = journal->j_superblock;
1472 sb->s_feature_compat &= ~cpu_to_be32(compat);
1473 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1474 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1476 EXPORT_SYMBOL(jbd2_journal_clear_features);
1479 * int jbd2_journal_update_format () - Update on-disk journal structure.
1480 * @journal: Journal to act on.
1482 * Given an initialised but unloaded journal struct, poke about in the
1483 * on-disk structure to update it to the most recent supported version.
1485 int jbd2_journal_update_format (journal_t *journal)
1487 journal_superblock_t *sb;
1488 int err;
1490 err = journal_get_superblock(journal);
1491 if (err)
1492 return err;
1494 sb = journal->j_superblock;
1496 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1497 case JBD2_SUPERBLOCK_V2:
1498 return 0;
1499 case JBD2_SUPERBLOCK_V1:
1500 return journal_convert_superblock_v1(journal, sb);
1501 default:
1502 break;
1504 return -EINVAL;
1507 static int journal_convert_superblock_v1(journal_t *journal,
1508 journal_superblock_t *sb)
1510 int offset, blocksize;
1511 struct buffer_head *bh;
1513 printk(KERN_WARNING
1514 "JBD: Converting superblock from version 1 to 2.\n");
1516 /* Pre-initialise new fields to zero */
1517 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1518 blocksize = be32_to_cpu(sb->s_blocksize);
1519 memset(&sb->s_feature_compat, 0, blocksize-offset);
1521 sb->s_nr_users = cpu_to_be32(1);
1522 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1523 journal->j_format_version = 2;
1525 bh = journal->j_sb_buffer;
1526 BUFFER_TRACE(bh, "marking dirty");
1527 mark_buffer_dirty(bh);
1528 sync_dirty_buffer(bh);
1529 return 0;
1534 * int jbd2_journal_flush () - Flush journal
1535 * @journal: Journal to act on.
1537 * Flush all data for a given journal to disk and empty the journal.
1538 * Filesystems can use this when remounting readonly to ensure that
1539 * recovery does not need to happen on remount.
1542 int jbd2_journal_flush(journal_t *journal)
1544 int err = 0;
1545 transaction_t *transaction = NULL;
1546 unsigned long old_tail;
1548 spin_lock(&journal->j_state_lock);
1550 /* Force everything buffered to the log... */
1551 if (journal->j_running_transaction) {
1552 transaction = journal->j_running_transaction;
1553 __jbd2_log_start_commit(journal, transaction->t_tid);
1554 } else if (journal->j_committing_transaction)
1555 transaction = journal->j_committing_transaction;
1557 /* Wait for the log commit to complete... */
1558 if (transaction) {
1559 tid_t tid = transaction->t_tid;
1561 spin_unlock(&journal->j_state_lock);
1562 jbd2_log_wait_commit(journal, tid);
1563 } else {
1564 spin_unlock(&journal->j_state_lock);
1567 /* ...and flush everything in the log out to disk. */
1568 spin_lock(&journal->j_list_lock);
1569 while (!err && journal->j_checkpoint_transactions != NULL) {
1570 spin_unlock(&journal->j_list_lock);
1571 mutex_lock(&journal->j_checkpoint_mutex);
1572 err = jbd2_log_do_checkpoint(journal);
1573 mutex_unlock(&journal->j_checkpoint_mutex);
1574 spin_lock(&journal->j_list_lock);
1576 spin_unlock(&journal->j_list_lock);
1578 if (is_journal_aborted(journal))
1579 return -EIO;
1581 jbd2_cleanup_journal_tail(journal);
1583 /* Finally, mark the journal as really needing no recovery.
1584 * This sets s_start==0 in the underlying superblock, which is
1585 * the magic code for a fully-recovered superblock. Any future
1586 * commits of data to the journal will restore the current
1587 * s_start value. */
1588 spin_lock(&journal->j_state_lock);
1589 old_tail = journal->j_tail;
1590 journal->j_tail = 0;
1591 spin_unlock(&journal->j_state_lock);
1592 jbd2_journal_update_superblock(journal, 1);
1593 spin_lock(&journal->j_state_lock);
1594 journal->j_tail = old_tail;
1596 J_ASSERT(!journal->j_running_transaction);
1597 J_ASSERT(!journal->j_committing_transaction);
1598 J_ASSERT(!journal->j_checkpoint_transactions);
1599 J_ASSERT(journal->j_head == journal->j_tail);
1600 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1601 spin_unlock(&journal->j_state_lock);
1602 return 0;
1606 * int jbd2_journal_wipe() - Wipe journal contents
1607 * @journal: Journal to act on.
1608 * @write: flag (see below)
1610 * Wipe out all of the contents of a journal, safely. This will produce
1611 * a warning if the journal contains any valid recovery information.
1612 * Must be called between journal_init_*() and jbd2_journal_load().
1614 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1615 * we merely suppress recovery.
1618 int jbd2_journal_wipe(journal_t *journal, int write)
1620 journal_superblock_t *sb;
1621 int err = 0;
1623 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1625 err = load_superblock(journal);
1626 if (err)
1627 return err;
1629 sb = journal->j_superblock;
1631 if (!journal->j_tail)
1632 goto no_recovery;
1634 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1635 write ? "Clearing" : "Ignoring");
1637 err = jbd2_journal_skip_recovery(journal);
1638 if (write)
1639 jbd2_journal_update_superblock(journal, 1);
1641 no_recovery:
1642 return err;
1646 * Journal abort has very specific semantics, which we describe
1647 * for journal abort.
1649 * Two internal functions, which provide abort to the jbd layer
1650 * itself are here.
1654 * Quick version for internal journal use (doesn't lock the journal).
1655 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1656 * and don't attempt to make any other journal updates.
1658 void __jbd2_journal_abort_hard(journal_t *journal)
1660 transaction_t *transaction;
1662 if (journal->j_flags & JBD2_ABORT)
1663 return;
1665 printk(KERN_ERR "Aborting journal on device %s.\n",
1666 journal->j_devname);
1668 spin_lock(&journal->j_state_lock);
1669 journal->j_flags |= JBD2_ABORT;
1670 transaction = journal->j_running_transaction;
1671 if (transaction)
1672 __jbd2_log_start_commit(journal, transaction->t_tid);
1673 spin_unlock(&journal->j_state_lock);
1676 /* Soft abort: record the abort error status in the journal superblock,
1677 * but don't do any other IO. */
1678 static void __journal_abort_soft (journal_t *journal, int errno)
1680 if (journal->j_flags & JBD2_ABORT)
1681 return;
1683 if (!journal->j_errno)
1684 journal->j_errno = errno;
1686 __jbd2_journal_abort_hard(journal);
1688 if (errno)
1689 jbd2_journal_update_superblock(journal, 1);
1693 * void jbd2_journal_abort () - Shutdown the journal immediately.
1694 * @journal: the journal to shutdown.
1695 * @errno: an error number to record in the journal indicating
1696 * the reason for the shutdown.
1698 * Perform a complete, immediate shutdown of the ENTIRE
1699 * journal (not of a single transaction). This operation cannot be
1700 * undone without closing and reopening the journal.
1702 * The jbd2_journal_abort function is intended to support higher level error
1703 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1704 * mode.
1706 * Journal abort has very specific semantics. Any existing dirty,
1707 * unjournaled buffers in the main filesystem will still be written to
1708 * disk by bdflush, but the journaling mechanism will be suspended
1709 * immediately and no further transaction commits will be honoured.
1711 * Any dirty, journaled buffers will be written back to disk without
1712 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1713 * filesystem, but we _do_ attempt to leave as much data as possible
1714 * behind for fsck to use for cleanup.
1716 * Any attempt to get a new transaction handle on a journal which is in
1717 * ABORT state will just result in an -EROFS error return. A
1718 * jbd2_journal_stop on an existing handle will return -EIO if we have
1719 * entered abort state during the update.
1721 * Recursive transactions are not disturbed by journal abort until the
1722 * final jbd2_journal_stop, which will receive the -EIO error.
1724 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
1725 * which will be recorded (if possible) in the journal superblock. This
1726 * allows a client to record failure conditions in the middle of a
1727 * transaction without having to complete the transaction to record the
1728 * failure to disk. ext3_error, for example, now uses this
1729 * functionality.
1731 * Errors which originate from within the journaling layer will NOT
1732 * supply an errno; a null errno implies that absolutely no further
1733 * writes are done to the journal (unless there are any already in
1734 * progress).
1738 void jbd2_journal_abort(journal_t *journal, int errno)
1740 __journal_abort_soft(journal, errno);
1744 * int jbd2_journal_errno () - returns the journal's error state.
1745 * @journal: journal to examine.
1747 * This is the errno number set with jbd2_journal_abort(), the last
1748 * time the journal was mounted - if the journal was stopped
1749 * without calling abort this will be 0.
1751 * If the journal has been aborted on this mount time -EROFS will
1752 * be returned.
1754 int jbd2_journal_errno(journal_t *journal)
1756 int err;
1758 spin_lock(&journal->j_state_lock);
1759 if (journal->j_flags & JBD2_ABORT)
1760 err = -EROFS;
1761 else
1762 err = journal->j_errno;
1763 spin_unlock(&journal->j_state_lock);
1764 return err;
1768 * int jbd2_journal_clear_err () - clears the journal's error state
1769 * @journal: journal to act on.
1771 * An error must be cleared or acked to take a FS out of readonly
1772 * mode.
1774 int jbd2_journal_clear_err(journal_t *journal)
1776 int err = 0;
1778 spin_lock(&journal->j_state_lock);
1779 if (journal->j_flags & JBD2_ABORT)
1780 err = -EROFS;
1781 else
1782 journal->j_errno = 0;
1783 spin_unlock(&journal->j_state_lock);
1784 return err;
1788 * void jbd2_journal_ack_err() - Ack journal err.
1789 * @journal: journal to act on.
1791 * An error must be cleared or acked to take a FS out of readonly
1792 * mode.
1794 void jbd2_journal_ack_err(journal_t *journal)
1796 spin_lock(&journal->j_state_lock);
1797 if (journal->j_errno)
1798 journal->j_flags |= JBD2_ACK_ERR;
1799 spin_unlock(&journal->j_state_lock);
1802 int jbd2_journal_blocks_per_page(struct inode *inode)
1804 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1808 * helper functions to deal with 32 or 64bit block numbers.
1810 size_t journal_tag_bytes(journal_t *journal)
1812 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1813 return JBD2_TAG_SIZE64;
1814 else
1815 return JBD2_TAG_SIZE32;
1819 * JBD memory management
1821 * These functions are used to allocate block-sized chunks of memory
1822 * used for making copies of buffer_head data. Very often it will be
1823 * page-sized chunks of data, but sometimes it will be in
1824 * sub-page-size chunks. (For example, 16k pages on Power systems
1825 * with a 4k block file system.) For blocks smaller than a page, we
1826 * use a SLAB allocator. There are slab caches for each block size,
1827 * which are allocated at mount time, if necessary, and we only free
1828 * (all of) the slab caches when/if the jbd2 module is unloaded. For
1829 * this reason we don't need to a mutex to protect access to
1830 * jbd2_slab[] allocating or releasing memory; only in
1831 * jbd2_journal_create_slab().
1833 #define JBD2_MAX_SLABS 8
1834 static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
1835 static DECLARE_MUTEX(jbd2_slab_create_sem);
1837 static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
1838 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
1839 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
1843 static void jbd2_journal_destroy_slabs(void)
1845 int i;
1847 for (i = 0; i < JBD2_MAX_SLABS; i++) {
1848 if (jbd2_slab[i])
1849 kmem_cache_destroy(jbd2_slab[i]);
1850 jbd2_slab[i] = NULL;
1854 static int jbd2_journal_create_slab(size_t size)
1856 int i = order_base_2(size) - 10;
1857 size_t slab_size;
1859 if (size == PAGE_SIZE)
1860 return 0;
1862 if (i >= JBD2_MAX_SLABS)
1863 return -EINVAL;
1865 if (unlikely(i < 0))
1866 i = 0;
1867 down(&jbd2_slab_create_sem);
1868 if (jbd2_slab[i]) {
1869 up(&jbd2_slab_create_sem);
1870 return 0; /* Already created */
1873 slab_size = 1 << (i+10);
1874 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
1875 slab_size, 0, NULL);
1876 up(&jbd2_slab_create_sem);
1877 if (!jbd2_slab[i]) {
1878 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
1879 return -ENOMEM;
1881 return 0;
1884 static struct kmem_cache *get_slab(size_t size)
1886 int i = order_base_2(size) - 10;
1888 BUG_ON(i >= JBD2_MAX_SLABS);
1889 if (unlikely(i < 0))
1890 i = 0;
1891 BUG_ON(jbd2_slab[i] == NULL);
1892 return jbd2_slab[i];
1895 void *jbd2_alloc(size_t size, gfp_t flags)
1897 void *ptr;
1899 BUG_ON(size & (size-1)); /* Must be a power of 2 */
1901 flags |= __GFP_REPEAT;
1902 if (size == PAGE_SIZE)
1903 ptr = (void *)__get_free_pages(flags, 0);
1904 else if (size > PAGE_SIZE) {
1905 int order = get_order(size);
1907 if (order < 3)
1908 ptr = (void *)__get_free_pages(flags, order);
1909 else
1910 ptr = vmalloc(size);
1911 } else
1912 ptr = kmem_cache_alloc(get_slab(size), flags);
1914 /* Check alignment; SLUB has gotten this wrong in the past,
1915 * and this can lead to user data corruption! */
1916 BUG_ON(((unsigned long) ptr) & (size-1));
1918 return ptr;
1921 void jbd2_free(void *ptr, size_t size)
1923 if (size == PAGE_SIZE) {
1924 free_pages((unsigned long)ptr, 0);
1925 return;
1927 if (size > PAGE_SIZE) {
1928 int order = get_order(size);
1930 if (order < 3)
1931 free_pages((unsigned long)ptr, order);
1932 else
1933 vfree(ptr);
1934 return;
1936 kmem_cache_free(get_slab(size), ptr);
1940 * Journal_head storage management
1942 static struct kmem_cache *jbd2_journal_head_cache;
1943 #ifdef CONFIG_JBD2_DEBUG
1944 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1945 #endif
1947 static int journal_init_jbd2_journal_head_cache(void)
1949 int retval;
1951 J_ASSERT(jbd2_journal_head_cache == NULL);
1952 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
1953 sizeof(struct journal_head),
1954 0, /* offset */
1955 SLAB_TEMPORARY, /* flags */
1956 NULL); /* ctor */
1957 retval = 0;
1958 if (!jbd2_journal_head_cache) {
1959 retval = -ENOMEM;
1960 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1962 return retval;
1965 static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
1967 if (jbd2_journal_head_cache) {
1968 kmem_cache_destroy(jbd2_journal_head_cache);
1969 jbd2_journal_head_cache = NULL;
1974 * journal_head splicing and dicing
1976 static struct journal_head *journal_alloc_journal_head(void)
1978 struct journal_head *ret;
1979 static unsigned long last_warning;
1981 #ifdef CONFIG_JBD2_DEBUG
1982 atomic_inc(&nr_journal_heads);
1983 #endif
1984 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
1985 if (!ret) {
1986 jbd_debug(1, "out of memory for journal_head\n");
1987 if (time_after(jiffies, last_warning + 5*HZ)) {
1988 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1989 __func__);
1990 last_warning = jiffies;
1992 while (!ret) {
1993 yield();
1994 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
1997 return ret;
2000 static void journal_free_journal_head(struct journal_head *jh)
2002 #ifdef CONFIG_JBD2_DEBUG
2003 atomic_dec(&nr_journal_heads);
2004 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2005 #endif
2006 kmem_cache_free(jbd2_journal_head_cache, jh);
2010 * A journal_head is attached to a buffer_head whenever JBD has an
2011 * interest in the buffer.
2013 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2014 * is set. This bit is tested in core kernel code where we need to take
2015 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2016 * there.
2018 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2020 * When a buffer has its BH_JBD bit set it is immune from being released by
2021 * core kernel code, mainly via ->b_count.
2023 * A journal_head may be detached from its buffer_head when the journal_head's
2024 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
2025 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
2026 * journal_head can be dropped if needed.
2028 * Various places in the kernel want to attach a journal_head to a buffer_head
2029 * _before_ attaching the journal_head to a transaction. To protect the
2030 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2031 * journal_head's b_jcount refcount by one. The caller must call
2032 * jbd2_journal_put_journal_head() to undo this.
2034 * So the typical usage would be:
2036 * (Attach a journal_head if needed. Increments b_jcount)
2037 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2038 * ...
2039 * jh->b_transaction = xxx;
2040 * jbd2_journal_put_journal_head(jh);
2042 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2043 * because it has a non-zero b_transaction.
2047 * Give a buffer_head a journal_head.
2049 * Doesn't need the journal lock.
2050 * May sleep.
2052 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2054 struct journal_head *jh;
2055 struct journal_head *new_jh = NULL;
2057 repeat:
2058 if (!buffer_jbd(bh)) {
2059 new_jh = journal_alloc_journal_head();
2060 memset(new_jh, 0, sizeof(*new_jh));
2063 jbd_lock_bh_journal_head(bh);
2064 if (buffer_jbd(bh)) {
2065 jh = bh2jh(bh);
2066 } else {
2067 J_ASSERT_BH(bh,
2068 (atomic_read(&bh->b_count) > 0) ||
2069 (bh->b_page && bh->b_page->mapping));
2071 if (!new_jh) {
2072 jbd_unlock_bh_journal_head(bh);
2073 goto repeat;
2076 jh = new_jh;
2077 new_jh = NULL; /* We consumed it */
2078 set_buffer_jbd(bh);
2079 bh->b_private = jh;
2080 jh->b_bh = bh;
2081 get_bh(bh);
2082 BUFFER_TRACE(bh, "added journal_head");
2084 jh->b_jcount++;
2085 jbd_unlock_bh_journal_head(bh);
2086 if (new_jh)
2087 journal_free_journal_head(new_jh);
2088 return bh->b_private;
2092 * Grab a ref against this buffer_head's journal_head. If it ended up not
2093 * having a journal_head, return NULL
2095 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2097 struct journal_head *jh = NULL;
2099 jbd_lock_bh_journal_head(bh);
2100 if (buffer_jbd(bh)) {
2101 jh = bh2jh(bh);
2102 jh->b_jcount++;
2104 jbd_unlock_bh_journal_head(bh);
2105 return jh;
2108 static void __journal_remove_journal_head(struct buffer_head *bh)
2110 struct journal_head *jh = bh2jh(bh);
2112 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2114 get_bh(bh);
2115 if (jh->b_jcount == 0) {
2116 if (jh->b_transaction == NULL &&
2117 jh->b_next_transaction == NULL &&
2118 jh->b_cp_transaction == NULL) {
2119 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2120 J_ASSERT_BH(bh, buffer_jbd(bh));
2121 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2122 BUFFER_TRACE(bh, "remove journal_head");
2123 if (jh->b_frozen_data) {
2124 printk(KERN_WARNING "%s: freeing "
2125 "b_frozen_data\n",
2126 __func__);
2127 jbd2_free(jh->b_frozen_data, bh->b_size);
2129 if (jh->b_committed_data) {
2130 printk(KERN_WARNING "%s: freeing "
2131 "b_committed_data\n",
2132 __func__);
2133 jbd2_free(jh->b_committed_data, bh->b_size);
2135 bh->b_private = NULL;
2136 jh->b_bh = NULL; /* debug, really */
2137 clear_buffer_jbd(bh);
2138 __brelse(bh);
2139 journal_free_journal_head(jh);
2140 } else {
2141 BUFFER_TRACE(bh, "journal_head was locked");
2147 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
2148 * and has a zero b_jcount then remove and release its journal_head. If we did
2149 * see that the buffer is not used by any transaction we also "logically"
2150 * decrement ->b_count.
2152 * We in fact take an additional increment on ->b_count as a convenience,
2153 * because the caller usually wants to do additional things with the bh
2154 * after calling here.
2155 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
2156 * time. Once the caller has run __brelse(), the buffer is eligible for
2157 * reaping by try_to_free_buffers().
2159 void jbd2_journal_remove_journal_head(struct buffer_head *bh)
2161 jbd_lock_bh_journal_head(bh);
2162 __journal_remove_journal_head(bh);
2163 jbd_unlock_bh_journal_head(bh);
2167 * Drop a reference on the passed journal_head. If it fell to zero then try to
2168 * release the journal_head from the buffer_head.
2170 void jbd2_journal_put_journal_head(struct journal_head *jh)
2172 struct buffer_head *bh = jh2bh(jh);
2174 jbd_lock_bh_journal_head(bh);
2175 J_ASSERT_JH(jh, jh->b_jcount > 0);
2176 --jh->b_jcount;
2177 if (!jh->b_jcount && !jh->b_transaction) {
2178 __journal_remove_journal_head(bh);
2179 __brelse(bh);
2181 jbd_unlock_bh_journal_head(bh);
2185 * Initialize jbd inode head
2187 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2189 jinode->i_transaction = NULL;
2190 jinode->i_next_transaction = NULL;
2191 jinode->i_vfs_inode = inode;
2192 jinode->i_flags = 0;
2193 INIT_LIST_HEAD(&jinode->i_list);
2197 * Function to be called before we start removing inode from memory (i.e.,
2198 * clear_inode() is a fine place to be called from). It removes inode from
2199 * transaction's lists.
2201 void jbd2_journal_release_jbd_inode(journal_t *journal,
2202 struct jbd2_inode *jinode)
2204 int writeout = 0;
2206 if (!journal)
2207 return;
2208 restart:
2209 spin_lock(&journal->j_list_lock);
2210 /* Is commit writing out inode - we have to wait */
2211 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2212 wait_queue_head_t *wq;
2213 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2214 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2215 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2216 spin_unlock(&journal->j_list_lock);
2217 schedule();
2218 finish_wait(wq, &wait.wait);
2219 goto restart;
2222 /* Do we need to wait for data writeback? */
2223 if (journal->j_committing_transaction == jinode->i_transaction)
2224 writeout = 1;
2225 if (jinode->i_transaction) {
2226 list_del(&jinode->i_list);
2227 jinode->i_transaction = NULL;
2229 spin_unlock(&journal->j_list_lock);
2233 * debugfs tunables
2235 #ifdef CONFIG_JBD2_DEBUG
2236 u8 jbd2_journal_enable_debug __read_mostly;
2237 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2239 #define JBD2_DEBUG_NAME "jbd2-debug"
2241 static struct dentry *jbd2_debugfs_dir;
2242 static struct dentry *jbd2_debug;
2244 static void __init jbd2_create_debugfs_entry(void)
2246 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2247 if (jbd2_debugfs_dir)
2248 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2249 S_IRUGO | S_IWUSR,
2250 jbd2_debugfs_dir,
2251 &jbd2_journal_enable_debug);
2254 static void __exit jbd2_remove_debugfs_entry(void)
2256 debugfs_remove(jbd2_debug);
2257 debugfs_remove(jbd2_debugfs_dir);
2260 #else
2262 static void __init jbd2_create_debugfs_entry(void)
2266 static void __exit jbd2_remove_debugfs_entry(void)
2270 #endif
2272 #ifdef CONFIG_PROC_FS
2274 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2276 static void __init jbd2_create_jbd_stats_proc_entry(void)
2278 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2281 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2283 if (proc_jbd2_stats)
2284 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2287 #else
2289 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2290 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2292 #endif
2294 struct kmem_cache *jbd2_handle_cache;
2296 static int __init journal_init_handle_cache(void)
2298 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
2299 sizeof(handle_t),
2300 0, /* offset */
2301 SLAB_TEMPORARY, /* flags */
2302 NULL); /* ctor */
2303 if (jbd2_handle_cache == NULL) {
2304 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2305 return -ENOMEM;
2307 return 0;
2310 static void jbd2_journal_destroy_handle_cache(void)
2312 if (jbd2_handle_cache)
2313 kmem_cache_destroy(jbd2_handle_cache);
2317 * Module startup and shutdown
2320 static int __init journal_init_caches(void)
2322 int ret;
2324 ret = jbd2_journal_init_revoke_caches();
2325 if (ret == 0)
2326 ret = journal_init_jbd2_journal_head_cache();
2327 if (ret == 0)
2328 ret = journal_init_handle_cache();
2329 return ret;
2332 static void jbd2_journal_destroy_caches(void)
2334 jbd2_journal_destroy_revoke_caches();
2335 jbd2_journal_destroy_jbd2_journal_head_cache();
2336 jbd2_journal_destroy_handle_cache();
2337 jbd2_journal_destroy_slabs();
2340 static int __init journal_init(void)
2342 int ret;
2344 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2346 ret = journal_init_caches();
2347 if (ret == 0) {
2348 jbd2_create_debugfs_entry();
2349 jbd2_create_jbd_stats_proc_entry();
2350 } else {
2351 jbd2_journal_destroy_caches();
2353 return ret;
2356 static void __exit journal_exit(void)
2358 #ifdef CONFIG_JBD2_DEBUG
2359 int n = atomic_read(&nr_journal_heads);
2360 if (n)
2361 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2362 #endif
2363 jbd2_remove_debugfs_entry();
2364 jbd2_remove_jbd_stats_proc_entry();
2365 jbd2_journal_destroy_caches();
2369 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2370 * tracing infrastructure to map a dev_t to a device name.
2372 * The caller should use rcu_read_lock() in order to make sure the
2373 * device name stays valid until its done with it. We use
2374 * rcu_read_lock() as well to make sure we're safe in case the caller
2375 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2376 * nested.
2378 struct devname_cache {
2379 struct rcu_head rcu;
2380 dev_t device;
2381 char devname[BDEVNAME_SIZE];
2383 #define CACHE_SIZE_BITS 6
2384 static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2385 static DEFINE_SPINLOCK(devname_cache_lock);
2387 static void free_devcache(struct rcu_head *rcu)
2389 kfree(rcu);
2392 const char *jbd2_dev_to_name(dev_t device)
2394 int i = hash_32(device, CACHE_SIZE_BITS);
2395 char *ret;
2396 struct block_device *bd;
2397 static struct devname_cache *new_dev;
2399 rcu_read_lock();
2400 if (devcache[i] && devcache[i]->device == device) {
2401 ret = devcache[i]->devname;
2402 rcu_read_unlock();
2403 return ret;
2405 rcu_read_unlock();
2407 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2408 if (!new_dev)
2409 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
2410 spin_lock(&devname_cache_lock);
2411 if (devcache[i]) {
2412 if (devcache[i]->device == device) {
2413 kfree(new_dev);
2414 ret = devcache[i]->devname;
2415 spin_unlock(&devname_cache_lock);
2416 return ret;
2418 call_rcu(&devcache[i]->rcu, free_devcache);
2420 devcache[i] = new_dev;
2421 devcache[i]->device = device;
2422 bd = bdget(device);
2423 if (bd) {
2424 bdevname(bd, devcache[i]->devname);
2425 bdput(bd);
2426 } else
2427 __bdevname(device, devcache[i]->devname);
2428 ret = devcache[i]->devname;
2429 spin_unlock(&devname_cache_lock);
2430 return ret;
2432 EXPORT_SYMBOL(jbd2_dev_to_name);
2434 MODULE_LICENSE("GPL");
2435 module_init(journal_init);
2436 module_exit(journal_exit);