thinkpad-acpi: remove include of acnamesp.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jbd2 / journal.c
blobecb2603730113c80646f40cf1942f632891e7633
1 /*
2 * linux/fs/jbd2/journal.c
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
12 * Generic filesystem journal-writing code; part of the ext2fs
13 * journaling system.
15 * This file manages journals: areas of disk reserved for logging
16 * transactional updates. This includes the kernel journaling thread
17 * which is responsible for scheduling updates to the log.
19 * We do not actually manage the physical storage of the journal in this
20 * file: that is left to a per-journal policy function, which allows us
21 * to store the journal within a filesystem-specified area for ext2
22 * journaling (ext2 can use a reserved inode for storing the log).
25 #include <linux/module.h>
26 #include <linux/time.h>
27 #include <linux/fs.h>
28 #include <linux/jbd2.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/mm.h>
33 #include <linux/freezer.h>
34 #include <linux/pagemap.h>
35 #include <linux/kthread.h>
36 #include <linux/poison.h>
37 #include <linux/proc_fs.h>
38 #include <linux/debugfs.h>
39 #include <linux/seq_file.h>
41 #include <asm/uaccess.h>
42 #include <asm/page.h>
44 EXPORT_SYMBOL(jbd2_journal_start);
45 EXPORT_SYMBOL(jbd2_journal_restart);
46 EXPORT_SYMBOL(jbd2_journal_extend);
47 EXPORT_SYMBOL(jbd2_journal_stop);
48 EXPORT_SYMBOL(jbd2_journal_lock_updates);
49 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
50 EXPORT_SYMBOL(jbd2_journal_get_write_access);
51 EXPORT_SYMBOL(jbd2_journal_get_create_access);
52 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
53 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
54 EXPORT_SYMBOL(jbd2_journal_release_buffer);
55 EXPORT_SYMBOL(jbd2_journal_forget);
56 #if 0
57 EXPORT_SYMBOL(journal_sync_buffer);
58 #endif
59 EXPORT_SYMBOL(jbd2_journal_flush);
60 EXPORT_SYMBOL(jbd2_journal_revoke);
62 EXPORT_SYMBOL(jbd2_journal_init_dev);
63 EXPORT_SYMBOL(jbd2_journal_init_inode);
64 EXPORT_SYMBOL(jbd2_journal_update_format);
65 EXPORT_SYMBOL(jbd2_journal_check_used_features);
66 EXPORT_SYMBOL(jbd2_journal_check_available_features);
67 EXPORT_SYMBOL(jbd2_journal_set_features);
68 EXPORT_SYMBOL(jbd2_journal_create);
69 EXPORT_SYMBOL(jbd2_journal_load);
70 EXPORT_SYMBOL(jbd2_journal_destroy);
71 EXPORT_SYMBOL(jbd2_journal_abort);
72 EXPORT_SYMBOL(jbd2_journal_errno);
73 EXPORT_SYMBOL(jbd2_journal_ack_err);
74 EXPORT_SYMBOL(jbd2_journal_clear_err);
75 EXPORT_SYMBOL(jbd2_log_wait_commit);
76 EXPORT_SYMBOL(jbd2_journal_start_commit);
77 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
78 EXPORT_SYMBOL(jbd2_journal_wipe);
79 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
80 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
81 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
82 EXPORT_SYMBOL(jbd2_journal_force_commit);
83 EXPORT_SYMBOL(jbd2_journal_file_inode);
84 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
85 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
86 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
88 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
89 static void __journal_abort_soft (journal_t *journal, int errno);
92 * Helper function used to manage commit timeouts
95 static void commit_timeout(unsigned long __data)
97 struct task_struct * p = (struct task_struct *) __data;
99 wake_up_process(p);
103 * kjournald2: The main thread function used to manage a logging device
104 * journal.
106 * This kernel thread is responsible for two things:
108 * 1) COMMIT: Every so often we need to commit the current state of the
109 * filesystem to disk. The journal thread is responsible for writing
110 * all of the metadata buffers to disk.
112 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
113 * of the data in that part of the log has been rewritten elsewhere on
114 * the disk. Flushing these old buffers to reclaim space in the log is
115 * known as checkpointing, and this thread is responsible for that job.
118 static int kjournald2(void *arg)
120 journal_t *journal = arg;
121 transaction_t *transaction;
124 * Set up an interval timer which can be used to trigger a commit wakeup
125 * after the commit interval expires
127 setup_timer(&journal->j_commit_timer, commit_timeout,
128 (unsigned long)current);
130 /* Record that the journal thread is running */
131 journal->j_task = current;
132 wake_up(&journal->j_wait_done_commit);
134 printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
135 journal->j_commit_interval / HZ);
138 * And now, wait forever for commit wakeup events.
140 spin_lock(&journal->j_state_lock);
142 loop:
143 if (journal->j_flags & JBD2_UNMOUNT)
144 goto end_loop;
146 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
147 journal->j_commit_sequence, journal->j_commit_request);
149 if (journal->j_commit_sequence != journal->j_commit_request) {
150 jbd_debug(1, "OK, requests differ\n");
151 spin_unlock(&journal->j_state_lock);
152 del_timer_sync(&journal->j_commit_timer);
153 jbd2_journal_commit_transaction(journal);
154 spin_lock(&journal->j_state_lock);
155 goto loop;
158 wake_up(&journal->j_wait_done_commit);
159 if (freezing(current)) {
161 * The simpler the better. Flushing journal isn't a
162 * good idea, because that depends on threads that may
163 * be already stopped.
165 jbd_debug(1, "Now suspending kjournald2\n");
166 spin_unlock(&journal->j_state_lock);
167 refrigerator();
168 spin_lock(&journal->j_state_lock);
169 } else {
171 * We assume on resume that commits are already there,
172 * so we don't sleep
174 DEFINE_WAIT(wait);
175 int should_sleep = 1;
177 prepare_to_wait(&journal->j_wait_commit, &wait,
178 TASK_INTERRUPTIBLE);
179 if (journal->j_commit_sequence != journal->j_commit_request)
180 should_sleep = 0;
181 transaction = journal->j_running_transaction;
182 if (transaction && time_after_eq(jiffies,
183 transaction->t_expires))
184 should_sleep = 0;
185 if (journal->j_flags & JBD2_UNMOUNT)
186 should_sleep = 0;
187 if (should_sleep) {
188 spin_unlock(&journal->j_state_lock);
189 schedule();
190 spin_lock(&journal->j_state_lock);
192 finish_wait(&journal->j_wait_commit, &wait);
195 jbd_debug(1, "kjournald2 wakes\n");
198 * Were we woken up by a commit wakeup event?
200 transaction = journal->j_running_transaction;
201 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
202 journal->j_commit_request = transaction->t_tid;
203 jbd_debug(1, "woke because of timeout\n");
205 goto loop;
207 end_loop:
208 spin_unlock(&journal->j_state_lock);
209 del_timer_sync(&journal->j_commit_timer);
210 journal->j_task = NULL;
211 wake_up(&journal->j_wait_done_commit);
212 jbd_debug(1, "Journal thread exiting.\n");
213 return 0;
216 static int jbd2_journal_start_thread(journal_t *journal)
218 struct task_struct *t;
220 t = kthread_run(kjournald2, journal, "kjournald2");
221 if (IS_ERR(t))
222 return PTR_ERR(t);
224 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
225 return 0;
228 static void journal_kill_thread(journal_t *journal)
230 spin_lock(&journal->j_state_lock);
231 journal->j_flags |= JBD2_UNMOUNT;
233 while (journal->j_task) {
234 wake_up(&journal->j_wait_commit);
235 spin_unlock(&journal->j_state_lock);
236 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
237 spin_lock(&journal->j_state_lock);
239 spin_unlock(&journal->j_state_lock);
243 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
245 * Writes a metadata buffer to a given disk block. The actual IO is not
246 * performed but a new buffer_head is constructed which labels the data
247 * to be written with the correct destination disk block.
249 * Any magic-number escaping which needs to be done will cause a
250 * copy-out here. If the buffer happens to start with the
251 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
252 * magic number is only written to the log for descripter blocks. In
253 * this case, we copy the data and replace the first word with 0, and we
254 * return a result code which indicates that this buffer needs to be
255 * marked as an escaped buffer in the corresponding log descriptor
256 * block. The missing word can then be restored when the block is read
257 * during recovery.
259 * If the source buffer has already been modified by a new transaction
260 * since we took the last commit snapshot, we use the frozen copy of
261 * that data for IO. If we end up using the existing buffer_head's data
262 * for the write, then we *have* to lock the buffer to prevent anyone
263 * else from using and possibly modifying it while the IO is in
264 * progress.
266 * The function returns a pointer to the buffer_heads to be used for IO.
268 * We assume that the journal has already been locked in this function.
270 * Return value:
271 * <0: Error
272 * >=0: Finished OK
274 * On success:
275 * Bit 0 set == escape performed on the data
276 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
279 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
280 struct journal_head *jh_in,
281 struct journal_head **jh_out,
282 unsigned long long blocknr)
284 int need_copy_out = 0;
285 int done_copy_out = 0;
286 int do_escape = 0;
287 char *mapped_data;
288 struct buffer_head *new_bh;
289 struct journal_head *new_jh;
290 struct page *new_page;
291 unsigned int new_offset;
292 struct buffer_head *bh_in = jh2bh(jh_in);
295 * The buffer really shouldn't be locked: only the current committing
296 * transaction is allowed to write it, so nobody else is allowed
297 * to do any IO.
299 * akpm: except if we're journalling data, and write() output is
300 * also part of a shared mapping, and another thread has
301 * decided to launch a writepage() against this buffer.
303 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
305 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
308 * If a new transaction has already done a buffer copy-out, then
309 * we use that version of the data for the commit.
311 jbd_lock_bh_state(bh_in);
312 repeat:
313 if (jh_in->b_frozen_data) {
314 done_copy_out = 1;
315 new_page = virt_to_page(jh_in->b_frozen_data);
316 new_offset = offset_in_page(jh_in->b_frozen_data);
317 } else {
318 new_page = jh2bh(jh_in)->b_page;
319 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
322 mapped_data = kmap_atomic(new_page, KM_USER0);
324 * Check for escaping
326 if (*((__be32 *)(mapped_data + new_offset)) ==
327 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
328 need_copy_out = 1;
329 do_escape = 1;
331 kunmap_atomic(mapped_data, KM_USER0);
334 * Do we need to do a data copy?
336 if (need_copy_out && !done_copy_out) {
337 char *tmp;
339 jbd_unlock_bh_state(bh_in);
340 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
341 jbd_lock_bh_state(bh_in);
342 if (jh_in->b_frozen_data) {
343 jbd2_free(tmp, bh_in->b_size);
344 goto repeat;
347 jh_in->b_frozen_data = tmp;
348 mapped_data = kmap_atomic(new_page, KM_USER0);
349 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
350 kunmap_atomic(mapped_data, KM_USER0);
352 new_page = virt_to_page(tmp);
353 new_offset = offset_in_page(tmp);
354 done_copy_out = 1;
358 * Did we need to do an escaping? Now we've done all the
359 * copying, we can finally do so.
361 if (do_escape) {
362 mapped_data = kmap_atomic(new_page, KM_USER0);
363 *((unsigned int *)(mapped_data + new_offset)) = 0;
364 kunmap_atomic(mapped_data, KM_USER0);
367 /* keep subsequent assertions sane */
368 new_bh->b_state = 0;
369 init_buffer(new_bh, NULL, NULL);
370 atomic_set(&new_bh->b_count, 1);
371 jbd_unlock_bh_state(bh_in);
373 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
375 set_bh_page(new_bh, new_page, new_offset);
376 new_jh->b_transaction = NULL;
377 new_bh->b_size = jh2bh(jh_in)->b_size;
378 new_bh->b_bdev = transaction->t_journal->j_dev;
379 new_bh->b_blocknr = blocknr;
380 set_buffer_mapped(new_bh);
381 set_buffer_dirty(new_bh);
383 *jh_out = new_jh;
386 * The to-be-written buffer needs to get moved to the io queue,
387 * and the original buffer whose contents we are shadowing or
388 * copying is moved to the transaction's shadow queue.
390 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
391 jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
392 JBUFFER_TRACE(new_jh, "file as BJ_IO");
393 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
395 return do_escape | (done_copy_out << 1);
399 * Allocation code for the journal file. Manage the space left in the
400 * journal, so that we can begin checkpointing when appropriate.
404 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
406 * Called with the journal already locked.
408 * Called under j_state_lock
411 int __jbd2_log_space_left(journal_t *journal)
413 int left = journal->j_free;
415 assert_spin_locked(&journal->j_state_lock);
418 * Be pessimistic here about the number of those free blocks which
419 * might be required for log descriptor control blocks.
422 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
424 left -= MIN_LOG_RESERVED_BLOCKS;
426 if (left <= 0)
427 return 0;
428 left -= (left >> 3);
429 return left;
433 * Called under j_state_lock. Returns true if a transaction commit was started.
435 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
438 * Are we already doing a recent enough commit?
440 if (!tid_geq(journal->j_commit_request, target)) {
442 * We want a new commit: OK, mark the request and wakup the
443 * commit thread. We do _not_ do the commit ourselves.
446 journal->j_commit_request = target;
447 jbd_debug(1, "JBD: requesting commit %d/%d\n",
448 journal->j_commit_request,
449 journal->j_commit_sequence);
450 wake_up(&journal->j_wait_commit);
451 return 1;
453 return 0;
456 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
458 int ret;
460 spin_lock(&journal->j_state_lock);
461 ret = __jbd2_log_start_commit(journal, tid);
462 spin_unlock(&journal->j_state_lock);
463 return ret;
467 * Force and wait upon a commit if the calling process is not within
468 * transaction. This is used for forcing out undo-protected data which contains
469 * bitmaps, when the fs is running out of space.
471 * We can only force the running transaction if we don't have an active handle;
472 * otherwise, we will deadlock.
474 * Returns true if a transaction was started.
476 int jbd2_journal_force_commit_nested(journal_t *journal)
478 transaction_t *transaction = NULL;
479 tid_t tid;
481 spin_lock(&journal->j_state_lock);
482 if (journal->j_running_transaction && !current->journal_info) {
483 transaction = journal->j_running_transaction;
484 __jbd2_log_start_commit(journal, transaction->t_tid);
485 } else if (journal->j_committing_transaction)
486 transaction = journal->j_committing_transaction;
488 if (!transaction) {
489 spin_unlock(&journal->j_state_lock);
490 return 0; /* Nothing to retry */
493 tid = transaction->t_tid;
494 spin_unlock(&journal->j_state_lock);
495 jbd2_log_wait_commit(journal, tid);
496 return 1;
500 * Start a commit of the current running transaction (if any). Returns true
501 * if a transaction is going to be committed (or is currently already
502 * committing), and fills its tid in at *ptid
504 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
506 int ret = 0;
508 spin_lock(&journal->j_state_lock);
509 if (journal->j_running_transaction) {
510 tid_t tid = journal->j_running_transaction->t_tid;
512 __jbd2_log_start_commit(journal, tid);
513 /* There's a running transaction and we've just made sure
514 * it's commit has been scheduled. */
515 if (ptid)
516 *ptid = tid;
517 ret = 1;
518 } else if (journal->j_committing_transaction) {
520 * If ext3_write_super() recently started a commit, then we
521 * have to wait for completion of that transaction
523 if (ptid)
524 *ptid = journal->j_committing_transaction->t_tid;
525 ret = 1;
527 spin_unlock(&journal->j_state_lock);
528 return ret;
532 * Wait for a specified commit to complete.
533 * The caller may not hold the journal lock.
535 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
537 int err = 0;
539 #ifdef CONFIG_JBD2_DEBUG
540 spin_lock(&journal->j_state_lock);
541 if (!tid_geq(journal->j_commit_request, tid)) {
542 printk(KERN_EMERG
543 "%s: error: j_commit_request=%d, tid=%d\n",
544 __func__, journal->j_commit_request, tid);
546 spin_unlock(&journal->j_state_lock);
547 #endif
548 spin_lock(&journal->j_state_lock);
549 while (tid_gt(tid, journal->j_commit_sequence)) {
550 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
551 tid, journal->j_commit_sequence);
552 wake_up(&journal->j_wait_commit);
553 spin_unlock(&journal->j_state_lock);
554 wait_event(journal->j_wait_done_commit,
555 !tid_gt(tid, journal->j_commit_sequence));
556 spin_lock(&journal->j_state_lock);
558 spin_unlock(&journal->j_state_lock);
560 if (unlikely(is_journal_aborted(journal))) {
561 printk(KERN_EMERG "journal commit I/O error\n");
562 err = -EIO;
564 return err;
568 * Log buffer allocation routines:
571 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
573 unsigned long blocknr;
575 spin_lock(&journal->j_state_lock);
576 J_ASSERT(journal->j_free > 1);
578 blocknr = journal->j_head;
579 journal->j_head++;
580 journal->j_free--;
581 if (journal->j_head == journal->j_last)
582 journal->j_head = journal->j_first;
583 spin_unlock(&journal->j_state_lock);
584 return jbd2_journal_bmap(journal, blocknr, retp);
588 * Conversion of logical to physical block numbers for the journal
590 * On external journals the journal blocks are identity-mapped, so
591 * this is a no-op. If needed, we can use j_blk_offset - everything is
592 * ready.
594 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
595 unsigned long long *retp)
597 int err = 0;
598 unsigned long long ret;
600 if (journal->j_inode) {
601 ret = bmap(journal->j_inode, blocknr);
602 if (ret)
603 *retp = ret;
604 else {
605 char b[BDEVNAME_SIZE];
607 printk(KERN_ALERT "%s: journal block not found "
608 "at offset %lu on %s\n",
609 __func__,
610 blocknr,
611 bdevname(journal->j_dev, b));
612 err = -EIO;
613 __journal_abort_soft(journal, err);
615 } else {
616 *retp = blocknr; /* +journal->j_blk_offset */
618 return err;
622 * We play buffer_head aliasing tricks to write data/metadata blocks to
623 * the journal without copying their contents, but for journal
624 * descriptor blocks we do need to generate bona fide buffers.
626 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
627 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
628 * But we don't bother doing that, so there will be coherency problems with
629 * mmaps of blockdevs which hold live JBD-controlled filesystems.
631 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
633 struct buffer_head *bh;
634 unsigned long long blocknr;
635 int err;
637 err = jbd2_journal_next_log_block(journal, &blocknr);
639 if (err)
640 return NULL;
642 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
643 lock_buffer(bh);
644 memset(bh->b_data, 0, journal->j_blocksize);
645 set_buffer_uptodate(bh);
646 unlock_buffer(bh);
647 BUFFER_TRACE(bh, "return this buffer");
648 return jbd2_journal_add_journal_head(bh);
651 struct jbd2_stats_proc_session {
652 journal_t *journal;
653 struct transaction_stats_s *stats;
654 int start;
655 int max;
658 static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
659 struct transaction_stats_s *ts,
660 int first)
662 if (ts == s->stats + s->max)
663 ts = s->stats;
664 if (!first && ts == s->stats + s->start)
665 return NULL;
666 while (ts->ts_type == 0) {
667 ts++;
668 if (ts == s->stats + s->max)
669 ts = s->stats;
670 if (ts == s->stats + s->start)
671 return NULL;
673 return ts;
677 static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
679 struct jbd2_stats_proc_session *s = seq->private;
680 struct transaction_stats_s *ts;
681 int l = *pos;
683 if (l == 0)
684 return SEQ_START_TOKEN;
685 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
686 if (!ts)
687 return NULL;
688 l--;
689 while (l) {
690 ts = jbd2_history_skip_empty(s, ++ts, 0);
691 if (!ts)
692 break;
693 l--;
695 return ts;
698 static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
700 struct jbd2_stats_proc_session *s = seq->private;
701 struct transaction_stats_s *ts = v;
703 ++*pos;
704 if (v == SEQ_START_TOKEN)
705 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
706 else
707 return jbd2_history_skip_empty(s, ++ts, 0);
710 static int jbd2_seq_history_show(struct seq_file *seq, void *v)
712 struct transaction_stats_s *ts = v;
713 if (v == SEQ_START_TOKEN) {
714 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
715 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
716 "wait", "run", "lock", "flush", "log", "hndls",
717 "block", "inlog", "ctime", "write", "drop",
718 "close");
719 return 0;
721 if (ts->ts_type == JBD2_STATS_RUN)
722 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
723 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
724 jiffies_to_msecs(ts->u.run.rs_wait),
725 jiffies_to_msecs(ts->u.run.rs_running),
726 jiffies_to_msecs(ts->u.run.rs_locked),
727 jiffies_to_msecs(ts->u.run.rs_flushing),
728 jiffies_to_msecs(ts->u.run.rs_logging),
729 ts->u.run.rs_handle_count,
730 ts->u.run.rs_blocks,
731 ts->u.run.rs_blocks_logged);
732 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
733 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
734 "C", ts->ts_tid, " ",
735 jiffies_to_msecs(ts->u.chp.cs_chp_time),
736 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
737 ts->u.chp.cs_forced_to_close);
738 else
739 J_ASSERT(0);
740 return 0;
743 static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
747 static struct seq_operations jbd2_seq_history_ops = {
748 .start = jbd2_seq_history_start,
749 .next = jbd2_seq_history_next,
750 .stop = jbd2_seq_history_stop,
751 .show = jbd2_seq_history_show,
754 static int jbd2_seq_history_open(struct inode *inode, struct file *file)
756 journal_t *journal = PDE(inode)->data;
757 struct jbd2_stats_proc_session *s;
758 int rc, size;
760 s = kmalloc(sizeof(*s), GFP_KERNEL);
761 if (s == NULL)
762 return -ENOMEM;
763 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
764 s->stats = kmalloc(size, GFP_KERNEL);
765 if (s->stats == NULL) {
766 kfree(s);
767 return -ENOMEM;
769 spin_lock(&journal->j_history_lock);
770 memcpy(s->stats, journal->j_history, size);
771 s->max = journal->j_history_max;
772 s->start = journal->j_history_cur % s->max;
773 spin_unlock(&journal->j_history_lock);
775 rc = seq_open(file, &jbd2_seq_history_ops);
776 if (rc == 0) {
777 struct seq_file *m = file->private_data;
778 m->private = s;
779 } else {
780 kfree(s->stats);
781 kfree(s);
783 return rc;
787 static int jbd2_seq_history_release(struct inode *inode, struct file *file)
789 struct seq_file *seq = file->private_data;
790 struct jbd2_stats_proc_session *s = seq->private;
792 kfree(s->stats);
793 kfree(s);
794 return seq_release(inode, file);
797 static struct file_operations jbd2_seq_history_fops = {
798 .owner = THIS_MODULE,
799 .open = jbd2_seq_history_open,
800 .read = seq_read,
801 .llseek = seq_lseek,
802 .release = jbd2_seq_history_release,
805 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
807 return *pos ? NULL : SEQ_START_TOKEN;
810 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
812 return NULL;
815 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
817 struct jbd2_stats_proc_session *s = seq->private;
819 if (v != SEQ_START_TOKEN)
820 return 0;
821 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
822 s->stats->ts_tid,
823 s->journal->j_max_transaction_buffers);
824 if (s->stats->ts_tid == 0)
825 return 0;
826 seq_printf(seq, "average: \n %ums waiting for transaction\n",
827 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
828 seq_printf(seq, " %ums running transaction\n",
829 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
830 seq_printf(seq, " %ums transaction was being locked\n",
831 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
832 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
833 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
834 seq_printf(seq, " %ums logging transaction\n",
835 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
836 seq_printf(seq, " %lu handles per transaction\n",
837 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
838 seq_printf(seq, " %lu blocks per transaction\n",
839 s->stats->u.run.rs_blocks / s->stats->ts_tid);
840 seq_printf(seq, " %lu logged blocks per transaction\n",
841 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
842 return 0;
845 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
849 static struct seq_operations jbd2_seq_info_ops = {
850 .start = jbd2_seq_info_start,
851 .next = jbd2_seq_info_next,
852 .stop = jbd2_seq_info_stop,
853 .show = jbd2_seq_info_show,
856 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
858 journal_t *journal = PDE(inode)->data;
859 struct jbd2_stats_proc_session *s;
860 int rc, size;
862 s = kmalloc(sizeof(*s), GFP_KERNEL);
863 if (s == NULL)
864 return -ENOMEM;
865 size = sizeof(struct transaction_stats_s);
866 s->stats = kmalloc(size, GFP_KERNEL);
867 if (s->stats == NULL) {
868 kfree(s);
869 return -ENOMEM;
871 spin_lock(&journal->j_history_lock);
872 memcpy(s->stats, &journal->j_stats, size);
873 s->journal = journal;
874 spin_unlock(&journal->j_history_lock);
876 rc = seq_open(file, &jbd2_seq_info_ops);
877 if (rc == 0) {
878 struct seq_file *m = file->private_data;
879 m->private = s;
880 } else {
881 kfree(s->stats);
882 kfree(s);
884 return rc;
888 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
890 struct seq_file *seq = file->private_data;
891 struct jbd2_stats_proc_session *s = seq->private;
892 kfree(s->stats);
893 kfree(s);
894 return seq_release(inode, file);
897 static struct file_operations jbd2_seq_info_fops = {
898 .owner = THIS_MODULE,
899 .open = jbd2_seq_info_open,
900 .read = seq_read,
901 .llseek = seq_lseek,
902 .release = jbd2_seq_info_release,
905 static struct proc_dir_entry *proc_jbd2_stats;
907 static void jbd2_stats_proc_init(journal_t *journal)
909 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
910 if (journal->j_proc_entry) {
911 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
912 &jbd2_seq_history_fops, journal);
913 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
914 &jbd2_seq_info_fops, journal);
918 static void jbd2_stats_proc_exit(journal_t *journal)
920 remove_proc_entry("info", journal->j_proc_entry);
921 remove_proc_entry("history", journal->j_proc_entry);
922 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
925 static void journal_init_stats(journal_t *journal)
927 int size;
929 if (!proc_jbd2_stats)
930 return;
932 journal->j_history_max = 100;
933 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
934 journal->j_history = kzalloc(size, GFP_KERNEL);
935 if (!journal->j_history) {
936 journal->j_history_max = 0;
937 return;
939 spin_lock_init(&journal->j_history_lock);
943 * Management for journal control blocks: functions to create and
944 * destroy journal_t structures, and to initialise and read existing
945 * journal blocks from disk. */
947 /* First: create and setup a journal_t object in memory. We initialise
948 * very few fields yet: that has to wait until we have created the
949 * journal structures from from scratch, or loaded them from disk. */
951 static journal_t * journal_init_common (void)
953 journal_t *journal;
954 int err;
956 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
957 if (!journal)
958 goto fail;
960 init_waitqueue_head(&journal->j_wait_transaction_locked);
961 init_waitqueue_head(&journal->j_wait_logspace);
962 init_waitqueue_head(&journal->j_wait_done_commit);
963 init_waitqueue_head(&journal->j_wait_checkpoint);
964 init_waitqueue_head(&journal->j_wait_commit);
965 init_waitqueue_head(&journal->j_wait_updates);
966 mutex_init(&journal->j_barrier);
967 mutex_init(&journal->j_checkpoint_mutex);
968 spin_lock_init(&journal->j_revoke_lock);
969 spin_lock_init(&journal->j_list_lock);
970 spin_lock_init(&journal->j_state_lock);
972 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
974 /* The journal is marked for error until we succeed with recovery! */
975 journal->j_flags = JBD2_ABORT;
977 /* Set up a default-sized revoke table for the new mount. */
978 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
979 if (err) {
980 kfree(journal);
981 goto fail;
984 journal_init_stats(journal);
986 return journal;
987 fail:
988 return NULL;
991 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
993 * Create a journal structure assigned some fixed set of disk blocks to
994 * the journal. We don't actually touch those disk blocks yet, but we
995 * need to set up all of the mapping information to tell the journaling
996 * system where the journal blocks are.
1001 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1002 * @bdev: Block device on which to create the journal
1003 * @fs_dev: Device which hold journalled filesystem for this journal.
1004 * @start: Block nr Start of journal.
1005 * @len: Length of the journal in blocks.
1006 * @blocksize: blocksize of journalling device
1008 * Returns: a newly created journal_t *
1010 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1011 * range of blocks on an arbitrary block device.
1014 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1015 struct block_device *fs_dev,
1016 unsigned long long start, int len, int blocksize)
1018 journal_t *journal = journal_init_common();
1019 struct buffer_head *bh;
1020 char *p;
1021 int n;
1023 if (!journal)
1024 return NULL;
1026 /* journal descriptor can store up to n blocks -bzzz */
1027 journal->j_blocksize = blocksize;
1028 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1029 journal->j_wbufsize = n;
1030 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1031 if (!journal->j_wbuf) {
1032 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1033 __func__);
1034 kfree(journal);
1035 journal = NULL;
1036 goto out;
1038 journal->j_dev = bdev;
1039 journal->j_fs_dev = fs_dev;
1040 journal->j_blk_offset = start;
1041 journal->j_maxlen = len;
1042 bdevname(journal->j_dev, journal->j_devname);
1043 p = journal->j_devname;
1044 while ((p = strchr(p, '/')))
1045 *p = '!';
1046 jbd2_stats_proc_init(journal);
1048 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1049 J_ASSERT(bh != NULL);
1050 journal->j_sb_buffer = bh;
1051 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1052 out:
1053 return journal;
1057 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1058 * @inode: An inode to create the journal in
1060 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1061 * the journal. The inode must exist already, must support bmap() and
1062 * must have all data blocks preallocated.
1064 journal_t * jbd2_journal_init_inode (struct inode *inode)
1066 struct buffer_head *bh;
1067 journal_t *journal = journal_init_common();
1068 char *p;
1069 int err;
1070 int n;
1071 unsigned long long blocknr;
1073 if (!journal)
1074 return NULL;
1076 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1077 journal->j_inode = inode;
1078 bdevname(journal->j_dev, journal->j_devname);
1079 p = journal->j_devname;
1080 while ((p = strchr(p, '/')))
1081 *p = '!';
1082 p = journal->j_devname + strlen(journal->j_devname);
1083 sprintf(p, ":%lu", journal->j_inode->i_ino);
1084 jbd_debug(1,
1085 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1086 journal, inode->i_sb->s_id, inode->i_ino,
1087 (long long) inode->i_size,
1088 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1090 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1091 journal->j_blocksize = inode->i_sb->s_blocksize;
1092 jbd2_stats_proc_init(journal);
1094 /* journal descriptor can store up to n blocks -bzzz */
1095 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1096 journal->j_wbufsize = n;
1097 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1098 if (!journal->j_wbuf) {
1099 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1100 __func__);
1101 kfree(journal);
1102 return NULL;
1105 err = jbd2_journal_bmap(journal, 0, &blocknr);
1106 /* If that failed, give up */
1107 if (err) {
1108 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
1109 __func__);
1110 kfree(journal);
1111 return NULL;
1114 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1115 J_ASSERT(bh != NULL);
1116 journal->j_sb_buffer = bh;
1117 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1119 return journal;
1123 * If the journal init or create aborts, we need to mark the journal
1124 * superblock as being NULL to prevent the journal destroy from writing
1125 * back a bogus superblock.
1127 static void journal_fail_superblock (journal_t *journal)
1129 struct buffer_head *bh = journal->j_sb_buffer;
1130 brelse(bh);
1131 journal->j_sb_buffer = NULL;
1135 * Given a journal_t structure, initialise the various fields for
1136 * startup of a new journaling session. We use this both when creating
1137 * a journal, and after recovering an old journal to reset it for
1138 * subsequent use.
1141 static int journal_reset(journal_t *journal)
1143 journal_superblock_t *sb = journal->j_superblock;
1144 unsigned long long first, last;
1146 first = be32_to_cpu(sb->s_first);
1147 last = be32_to_cpu(sb->s_maxlen);
1149 journal->j_first = first;
1150 journal->j_last = last;
1152 journal->j_head = first;
1153 journal->j_tail = first;
1154 journal->j_free = last - first;
1156 journal->j_tail_sequence = journal->j_transaction_sequence;
1157 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1158 journal->j_commit_request = journal->j_commit_sequence;
1160 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1162 /* Add the dynamic fields and write it to disk. */
1163 jbd2_journal_update_superblock(journal, 1);
1164 return jbd2_journal_start_thread(journal);
1168 * int jbd2_journal_create() - Initialise the new journal file
1169 * @journal: Journal to create. This structure must have been initialised
1171 * Given a journal_t structure which tells us which disk blocks we can
1172 * use, create a new journal superblock and initialise all of the
1173 * journal fields from scratch.
1175 int jbd2_journal_create(journal_t *journal)
1177 unsigned long long blocknr;
1178 struct buffer_head *bh;
1179 journal_superblock_t *sb;
1180 int i, err;
1182 if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
1183 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
1184 journal->j_maxlen);
1185 journal_fail_superblock(journal);
1186 return -EINVAL;
1189 if (journal->j_inode == NULL) {
1191 * We don't know what block to start at!
1193 printk(KERN_EMERG
1194 "%s: creation of journal on external device!\n",
1195 __func__);
1196 BUG();
1199 /* Zero out the entire journal on disk. We cannot afford to
1200 have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
1201 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
1202 for (i = 0; i < journal->j_maxlen; i++) {
1203 err = jbd2_journal_bmap(journal, i, &blocknr);
1204 if (err)
1205 return err;
1206 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1207 lock_buffer(bh);
1208 memset (bh->b_data, 0, journal->j_blocksize);
1209 BUFFER_TRACE(bh, "marking dirty");
1210 mark_buffer_dirty(bh);
1211 BUFFER_TRACE(bh, "marking uptodate");
1212 set_buffer_uptodate(bh);
1213 unlock_buffer(bh);
1214 __brelse(bh);
1217 sync_blockdev(journal->j_dev);
1218 jbd_debug(1, "JBD: journal cleared.\n");
1220 /* OK, fill in the initial static fields in the new superblock */
1221 sb = journal->j_superblock;
1223 sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
1224 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1226 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1227 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1228 sb->s_first = cpu_to_be32(1);
1230 journal->j_transaction_sequence = 1;
1232 journal->j_flags &= ~JBD2_ABORT;
1233 journal->j_format_version = 2;
1235 return journal_reset(journal);
1239 * void jbd2_journal_update_superblock() - Update journal sb on disk.
1240 * @journal: The journal to update.
1241 * @wait: Set to '0' if you don't want to wait for IO completion.
1243 * Update a journal's dynamic superblock fields and write it to disk,
1244 * optionally waiting for the IO to complete.
1246 void jbd2_journal_update_superblock(journal_t *journal, int wait)
1248 journal_superblock_t *sb = journal->j_superblock;
1249 struct buffer_head *bh = journal->j_sb_buffer;
1252 * As a special case, if the on-disk copy is already marked as needing
1253 * no recovery (s_start == 0) and there are no outstanding transactions
1254 * in the filesystem, then we can safely defer the superblock update
1255 * until the next commit by setting JBD2_FLUSHED. This avoids
1256 * attempting a write to a potential-readonly device.
1258 if (sb->s_start == 0 && journal->j_tail_sequence ==
1259 journal->j_transaction_sequence) {
1260 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1261 "(start %ld, seq %d, errno %d)\n",
1262 journal->j_tail, journal->j_tail_sequence,
1263 journal->j_errno);
1264 goto out;
1267 if (buffer_write_io_error(bh)) {
1269 * Oh, dear. A previous attempt to write the journal
1270 * superblock failed. This could happen because the
1271 * USB device was yanked out. Or it could happen to
1272 * be a transient write error and maybe the block will
1273 * be remapped. Nothing we can do but to retry the
1274 * write and hope for the best.
1276 printk(KERN_ERR "JBD2: previous I/O error detected "
1277 "for journal superblock update for %s.\n",
1278 journal->j_devname);
1279 clear_buffer_write_io_error(bh);
1280 set_buffer_uptodate(bh);
1283 spin_lock(&journal->j_state_lock);
1284 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1285 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1287 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1288 sb->s_start = cpu_to_be32(journal->j_tail);
1289 sb->s_errno = cpu_to_be32(journal->j_errno);
1290 spin_unlock(&journal->j_state_lock);
1292 BUFFER_TRACE(bh, "marking dirty");
1293 mark_buffer_dirty(bh);
1294 if (wait) {
1295 sync_dirty_buffer(bh);
1296 if (buffer_write_io_error(bh)) {
1297 printk(KERN_ERR "JBD2: I/O error detected "
1298 "when updating journal superblock for %s.\n",
1299 journal->j_devname);
1300 clear_buffer_write_io_error(bh);
1301 set_buffer_uptodate(bh);
1303 } else
1304 ll_rw_block(SWRITE, 1, &bh);
1306 out:
1307 /* If we have just flushed the log (by marking s_start==0), then
1308 * any future commit will have to be careful to update the
1309 * superblock again to re-record the true start of the log. */
1311 spin_lock(&journal->j_state_lock);
1312 if (sb->s_start)
1313 journal->j_flags &= ~JBD2_FLUSHED;
1314 else
1315 journal->j_flags |= JBD2_FLUSHED;
1316 spin_unlock(&journal->j_state_lock);
1320 * Read the superblock for a given journal, performing initial
1321 * validation of the format.
1324 static int journal_get_superblock(journal_t *journal)
1326 struct buffer_head *bh;
1327 journal_superblock_t *sb;
1328 int err = -EIO;
1330 bh = journal->j_sb_buffer;
1332 J_ASSERT(bh != NULL);
1333 if (!buffer_uptodate(bh)) {
1334 ll_rw_block(READ, 1, &bh);
1335 wait_on_buffer(bh);
1336 if (!buffer_uptodate(bh)) {
1337 printk (KERN_ERR
1338 "JBD: IO error reading journal superblock\n");
1339 goto out;
1343 sb = journal->j_superblock;
1345 err = -EINVAL;
1347 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1348 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1349 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1350 goto out;
1353 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1354 case JBD2_SUPERBLOCK_V1:
1355 journal->j_format_version = 1;
1356 break;
1357 case JBD2_SUPERBLOCK_V2:
1358 journal->j_format_version = 2;
1359 break;
1360 default:
1361 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1362 goto out;
1365 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1366 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1367 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1368 printk (KERN_WARNING "JBD: journal file too short\n");
1369 goto out;
1372 return 0;
1374 out:
1375 journal_fail_superblock(journal);
1376 return err;
1380 * Load the on-disk journal superblock and read the key fields into the
1381 * journal_t.
1384 static int load_superblock(journal_t *journal)
1386 int err;
1387 journal_superblock_t *sb;
1389 err = journal_get_superblock(journal);
1390 if (err)
1391 return err;
1393 sb = journal->j_superblock;
1395 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1396 journal->j_tail = be32_to_cpu(sb->s_start);
1397 journal->j_first = be32_to_cpu(sb->s_first);
1398 journal->j_last = be32_to_cpu(sb->s_maxlen);
1399 journal->j_errno = be32_to_cpu(sb->s_errno);
1401 return 0;
1406 * int jbd2_journal_load() - Read journal from disk.
1407 * @journal: Journal to act on.
1409 * Given a journal_t structure which tells us which disk blocks contain
1410 * a journal, read the journal from disk to initialise the in-memory
1411 * structures.
1413 int jbd2_journal_load(journal_t *journal)
1415 int err;
1416 journal_superblock_t *sb;
1418 err = load_superblock(journal);
1419 if (err)
1420 return err;
1422 sb = journal->j_superblock;
1423 /* If this is a V2 superblock, then we have to check the
1424 * features flags on it. */
1426 if (journal->j_format_version >= 2) {
1427 if ((sb->s_feature_ro_compat &
1428 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1429 (sb->s_feature_incompat &
1430 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1431 printk (KERN_WARNING
1432 "JBD: Unrecognised features on journal\n");
1433 return -EINVAL;
1437 /* Let the recovery code check whether it needs to recover any
1438 * data from the journal. */
1439 if (jbd2_journal_recover(journal))
1440 goto recovery_error;
1442 /* OK, we've finished with the dynamic journal bits:
1443 * reinitialise the dynamic contents of the superblock in memory
1444 * and reset them on disk. */
1445 if (journal_reset(journal))
1446 goto recovery_error;
1448 journal->j_flags &= ~JBD2_ABORT;
1449 journal->j_flags |= JBD2_LOADED;
1450 return 0;
1452 recovery_error:
1453 printk (KERN_WARNING "JBD: recovery failed\n");
1454 return -EIO;
1458 * void jbd2_journal_destroy() - Release a journal_t structure.
1459 * @journal: Journal to act on.
1461 * Release a journal_t structure once it is no longer in use by the
1462 * journaled object.
1464 void jbd2_journal_destroy(journal_t *journal)
1466 /* Wait for the commit thread to wake up and die. */
1467 journal_kill_thread(journal);
1469 /* Force a final log commit */
1470 if (journal->j_running_transaction)
1471 jbd2_journal_commit_transaction(journal);
1473 /* Force any old transactions to disk */
1475 /* Totally anal locking here... */
1476 spin_lock(&journal->j_list_lock);
1477 while (journal->j_checkpoint_transactions != NULL) {
1478 spin_unlock(&journal->j_list_lock);
1479 jbd2_log_do_checkpoint(journal);
1480 spin_lock(&journal->j_list_lock);
1483 J_ASSERT(journal->j_running_transaction == NULL);
1484 J_ASSERT(journal->j_committing_transaction == NULL);
1485 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1486 spin_unlock(&journal->j_list_lock);
1488 /* We can now mark the journal as empty. */
1489 journal->j_tail = 0;
1490 journal->j_tail_sequence = ++journal->j_transaction_sequence;
1491 if (journal->j_sb_buffer) {
1492 jbd2_journal_update_superblock(journal, 1);
1493 brelse(journal->j_sb_buffer);
1496 if (journal->j_proc_entry)
1497 jbd2_stats_proc_exit(journal);
1498 if (journal->j_inode)
1499 iput(journal->j_inode);
1500 if (journal->j_revoke)
1501 jbd2_journal_destroy_revoke(journal);
1502 kfree(journal->j_wbuf);
1503 kfree(journal);
1508 *int jbd2_journal_check_used_features () - Check if features specified are used.
1509 * @journal: Journal to check.
1510 * @compat: bitmask of compatible features
1511 * @ro: bitmask of features that force read-only mount
1512 * @incompat: bitmask of incompatible features
1514 * Check whether the journal uses all of a given set of
1515 * features. Return true (non-zero) if it does.
1518 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1519 unsigned long ro, unsigned long incompat)
1521 journal_superblock_t *sb;
1523 if (!compat && !ro && !incompat)
1524 return 1;
1525 if (journal->j_format_version == 1)
1526 return 0;
1528 sb = journal->j_superblock;
1530 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1531 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1532 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1533 return 1;
1535 return 0;
1539 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1540 * @journal: Journal to check.
1541 * @compat: bitmask of compatible features
1542 * @ro: bitmask of features that force read-only mount
1543 * @incompat: bitmask of incompatible features
1545 * Check whether the journaling code supports the use of
1546 * all of a given set of features on this journal. Return true
1547 * (non-zero) if it can. */
1549 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1550 unsigned long ro, unsigned long incompat)
1552 journal_superblock_t *sb;
1554 if (!compat && !ro && !incompat)
1555 return 1;
1557 sb = journal->j_superblock;
1559 /* We can support any known requested features iff the
1560 * superblock is in version 2. Otherwise we fail to support any
1561 * extended sb features. */
1563 if (journal->j_format_version != 2)
1564 return 0;
1566 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1567 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1568 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1569 return 1;
1571 return 0;
1575 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1576 * @journal: Journal to act on.
1577 * @compat: bitmask of compatible features
1578 * @ro: bitmask of features that force read-only mount
1579 * @incompat: bitmask of incompatible features
1581 * Mark a given journal feature as present on the
1582 * superblock. Returns true if the requested features could be set.
1586 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1587 unsigned long ro, unsigned long incompat)
1589 journal_superblock_t *sb;
1591 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1592 return 1;
1594 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1595 return 0;
1597 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1598 compat, ro, incompat);
1600 sb = journal->j_superblock;
1602 sb->s_feature_compat |= cpu_to_be32(compat);
1603 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1604 sb->s_feature_incompat |= cpu_to_be32(incompat);
1606 return 1;
1610 * jbd2_journal_clear_features () - Clear a given journal feature in the
1611 * superblock
1612 * @journal: Journal to act on.
1613 * @compat: bitmask of compatible features
1614 * @ro: bitmask of features that force read-only mount
1615 * @incompat: bitmask of incompatible features
1617 * Clear a given journal feature as present on the
1618 * superblock.
1620 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1621 unsigned long ro, unsigned long incompat)
1623 journal_superblock_t *sb;
1625 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1626 compat, ro, incompat);
1628 sb = journal->j_superblock;
1630 sb->s_feature_compat &= ~cpu_to_be32(compat);
1631 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1632 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1634 EXPORT_SYMBOL(jbd2_journal_clear_features);
1637 * int jbd2_journal_update_format () - Update on-disk journal structure.
1638 * @journal: Journal to act on.
1640 * Given an initialised but unloaded journal struct, poke about in the
1641 * on-disk structure to update it to the most recent supported version.
1643 int jbd2_journal_update_format (journal_t *journal)
1645 journal_superblock_t *sb;
1646 int err;
1648 err = journal_get_superblock(journal);
1649 if (err)
1650 return err;
1652 sb = journal->j_superblock;
1654 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1655 case JBD2_SUPERBLOCK_V2:
1656 return 0;
1657 case JBD2_SUPERBLOCK_V1:
1658 return journal_convert_superblock_v1(journal, sb);
1659 default:
1660 break;
1662 return -EINVAL;
1665 static int journal_convert_superblock_v1(journal_t *journal,
1666 journal_superblock_t *sb)
1668 int offset, blocksize;
1669 struct buffer_head *bh;
1671 printk(KERN_WARNING
1672 "JBD: Converting superblock from version 1 to 2.\n");
1674 /* Pre-initialise new fields to zero */
1675 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1676 blocksize = be32_to_cpu(sb->s_blocksize);
1677 memset(&sb->s_feature_compat, 0, blocksize-offset);
1679 sb->s_nr_users = cpu_to_be32(1);
1680 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1681 journal->j_format_version = 2;
1683 bh = journal->j_sb_buffer;
1684 BUFFER_TRACE(bh, "marking dirty");
1685 mark_buffer_dirty(bh);
1686 sync_dirty_buffer(bh);
1687 return 0;
1692 * int jbd2_journal_flush () - Flush journal
1693 * @journal: Journal to act on.
1695 * Flush all data for a given journal to disk and empty the journal.
1696 * Filesystems can use this when remounting readonly to ensure that
1697 * recovery does not need to happen on remount.
1700 int jbd2_journal_flush(journal_t *journal)
1702 int err = 0;
1703 transaction_t *transaction = NULL;
1704 unsigned long old_tail;
1706 spin_lock(&journal->j_state_lock);
1708 /* Force everything buffered to the log... */
1709 if (journal->j_running_transaction) {
1710 transaction = journal->j_running_transaction;
1711 __jbd2_log_start_commit(journal, transaction->t_tid);
1712 } else if (journal->j_committing_transaction)
1713 transaction = journal->j_committing_transaction;
1715 /* Wait for the log commit to complete... */
1716 if (transaction) {
1717 tid_t tid = transaction->t_tid;
1719 spin_unlock(&journal->j_state_lock);
1720 jbd2_log_wait_commit(journal, tid);
1721 } else {
1722 spin_unlock(&journal->j_state_lock);
1725 /* ...and flush everything in the log out to disk. */
1726 spin_lock(&journal->j_list_lock);
1727 while (!err && journal->j_checkpoint_transactions != NULL) {
1728 spin_unlock(&journal->j_list_lock);
1729 err = jbd2_log_do_checkpoint(journal);
1730 spin_lock(&journal->j_list_lock);
1732 spin_unlock(&journal->j_list_lock);
1733 jbd2_cleanup_journal_tail(journal);
1735 /* Finally, mark the journal as really needing no recovery.
1736 * This sets s_start==0 in the underlying superblock, which is
1737 * the magic code for a fully-recovered superblock. Any future
1738 * commits of data to the journal will restore the current
1739 * s_start value. */
1740 spin_lock(&journal->j_state_lock);
1741 old_tail = journal->j_tail;
1742 journal->j_tail = 0;
1743 spin_unlock(&journal->j_state_lock);
1744 jbd2_journal_update_superblock(journal, 1);
1745 spin_lock(&journal->j_state_lock);
1746 journal->j_tail = old_tail;
1748 J_ASSERT(!journal->j_running_transaction);
1749 J_ASSERT(!journal->j_committing_transaction);
1750 J_ASSERT(!journal->j_checkpoint_transactions);
1751 J_ASSERT(journal->j_head == journal->j_tail);
1752 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1753 spin_unlock(&journal->j_state_lock);
1754 return err;
1758 * int jbd2_journal_wipe() - Wipe journal contents
1759 * @journal: Journal to act on.
1760 * @write: flag (see below)
1762 * Wipe out all of the contents of a journal, safely. This will produce
1763 * a warning if the journal contains any valid recovery information.
1764 * Must be called between journal_init_*() and jbd2_journal_load().
1766 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1767 * we merely suppress recovery.
1770 int jbd2_journal_wipe(journal_t *journal, int write)
1772 journal_superblock_t *sb;
1773 int err = 0;
1775 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1777 err = load_superblock(journal);
1778 if (err)
1779 return err;
1781 sb = journal->j_superblock;
1783 if (!journal->j_tail)
1784 goto no_recovery;
1786 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1787 write ? "Clearing" : "Ignoring");
1789 err = jbd2_journal_skip_recovery(journal);
1790 if (write)
1791 jbd2_journal_update_superblock(journal, 1);
1793 no_recovery:
1794 return err;
1798 * journal_dev_name: format a character string to describe on what
1799 * device this journal is present.
1802 static const char *journal_dev_name(journal_t *journal, char *buffer)
1804 struct block_device *bdev;
1806 if (journal->j_inode)
1807 bdev = journal->j_inode->i_sb->s_bdev;
1808 else
1809 bdev = journal->j_dev;
1811 return bdevname(bdev, buffer);
1815 * Journal abort has very specific semantics, which we describe
1816 * for journal abort.
1818 * Two internal function, which provide abort to te jbd layer
1819 * itself are here.
1823 * Quick version for internal journal use (doesn't lock the journal).
1824 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1825 * and don't attempt to make any other journal updates.
1827 void __jbd2_journal_abort_hard(journal_t *journal)
1829 transaction_t *transaction;
1830 char b[BDEVNAME_SIZE];
1832 if (journal->j_flags & JBD2_ABORT)
1833 return;
1835 printk(KERN_ERR "Aborting journal on device %s.\n",
1836 journal_dev_name(journal, b));
1838 spin_lock(&journal->j_state_lock);
1839 journal->j_flags |= JBD2_ABORT;
1840 transaction = journal->j_running_transaction;
1841 if (transaction)
1842 __jbd2_log_start_commit(journal, transaction->t_tid);
1843 spin_unlock(&journal->j_state_lock);
1846 /* Soft abort: record the abort error status in the journal superblock,
1847 * but don't do any other IO. */
1848 static void __journal_abort_soft (journal_t *journal, int errno)
1850 if (journal->j_flags & JBD2_ABORT)
1851 return;
1853 if (!journal->j_errno)
1854 journal->j_errno = errno;
1856 __jbd2_journal_abort_hard(journal);
1858 if (errno)
1859 jbd2_journal_update_superblock(journal, 1);
1863 * void jbd2_journal_abort () - Shutdown the journal immediately.
1864 * @journal: the journal to shutdown.
1865 * @errno: an error number to record in the journal indicating
1866 * the reason for the shutdown.
1868 * Perform a complete, immediate shutdown of the ENTIRE
1869 * journal (not of a single transaction). This operation cannot be
1870 * undone without closing and reopening the journal.
1872 * The jbd2_journal_abort function is intended to support higher level error
1873 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1874 * mode.
1876 * Journal abort has very specific semantics. Any existing dirty,
1877 * unjournaled buffers in the main filesystem will still be written to
1878 * disk by bdflush, but the journaling mechanism will be suspended
1879 * immediately and no further transaction commits will be honoured.
1881 * Any dirty, journaled buffers will be written back to disk without
1882 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1883 * filesystem, but we _do_ attempt to leave as much data as possible
1884 * behind for fsck to use for cleanup.
1886 * Any attempt to get a new transaction handle on a journal which is in
1887 * ABORT state will just result in an -EROFS error return. A
1888 * jbd2_journal_stop on an existing handle will return -EIO if we have
1889 * entered abort state during the update.
1891 * Recursive transactions are not disturbed by journal abort until the
1892 * final jbd2_journal_stop, which will receive the -EIO error.
1894 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
1895 * which will be recorded (if possible) in the journal superblock. This
1896 * allows a client to record failure conditions in the middle of a
1897 * transaction without having to complete the transaction to record the
1898 * failure to disk. ext3_error, for example, now uses this
1899 * functionality.
1901 * Errors which originate from within the journaling layer will NOT
1902 * supply an errno; a null errno implies that absolutely no further
1903 * writes are done to the journal (unless there are any already in
1904 * progress).
1908 void jbd2_journal_abort(journal_t *journal, int errno)
1910 __journal_abort_soft(journal, errno);
1914 * int jbd2_journal_errno () - returns the journal's error state.
1915 * @journal: journal to examine.
1917 * This is the errno numbet set with jbd2_journal_abort(), the last
1918 * time the journal was mounted - if the journal was stopped
1919 * without calling abort this will be 0.
1921 * If the journal has been aborted on this mount time -EROFS will
1922 * be returned.
1924 int jbd2_journal_errno(journal_t *journal)
1926 int err;
1928 spin_lock(&journal->j_state_lock);
1929 if (journal->j_flags & JBD2_ABORT)
1930 err = -EROFS;
1931 else
1932 err = journal->j_errno;
1933 spin_unlock(&journal->j_state_lock);
1934 return err;
1938 * int jbd2_journal_clear_err () - clears the journal's error state
1939 * @journal: journal to act on.
1941 * An error must be cleared or Acked to take a FS out of readonly
1942 * mode.
1944 int jbd2_journal_clear_err(journal_t *journal)
1946 int err = 0;
1948 spin_lock(&journal->j_state_lock);
1949 if (journal->j_flags & JBD2_ABORT)
1950 err = -EROFS;
1951 else
1952 journal->j_errno = 0;
1953 spin_unlock(&journal->j_state_lock);
1954 return err;
1958 * void jbd2_journal_ack_err() - Ack journal err.
1959 * @journal: journal to act on.
1961 * An error must be cleared or Acked to take a FS out of readonly
1962 * mode.
1964 void jbd2_journal_ack_err(journal_t *journal)
1966 spin_lock(&journal->j_state_lock);
1967 if (journal->j_errno)
1968 journal->j_flags |= JBD2_ACK_ERR;
1969 spin_unlock(&journal->j_state_lock);
1972 int jbd2_journal_blocks_per_page(struct inode *inode)
1974 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1978 * helper functions to deal with 32 or 64bit block numbers.
1980 size_t journal_tag_bytes(journal_t *journal)
1982 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1983 return JBD2_TAG_SIZE64;
1984 else
1985 return JBD2_TAG_SIZE32;
1989 * Journal_head storage management
1991 static struct kmem_cache *jbd2_journal_head_cache;
1992 #ifdef CONFIG_JBD2_DEBUG
1993 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1994 #endif
1996 static int journal_init_jbd2_journal_head_cache(void)
1998 int retval;
2000 J_ASSERT(jbd2_journal_head_cache == NULL);
2001 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2002 sizeof(struct journal_head),
2003 0, /* offset */
2004 SLAB_TEMPORARY, /* flags */
2005 NULL); /* ctor */
2006 retval = 0;
2007 if (!jbd2_journal_head_cache) {
2008 retval = -ENOMEM;
2009 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
2011 return retval;
2014 static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
2016 if (jbd2_journal_head_cache) {
2017 kmem_cache_destroy(jbd2_journal_head_cache);
2018 jbd2_journal_head_cache = NULL;
2023 * journal_head splicing and dicing
2025 static struct journal_head *journal_alloc_journal_head(void)
2027 struct journal_head *ret;
2028 static unsigned long last_warning;
2030 #ifdef CONFIG_JBD2_DEBUG
2031 atomic_inc(&nr_journal_heads);
2032 #endif
2033 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2034 if (!ret) {
2035 jbd_debug(1, "out of memory for journal_head\n");
2036 if (time_after(jiffies, last_warning + 5*HZ)) {
2037 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
2038 __func__);
2039 last_warning = jiffies;
2041 while (!ret) {
2042 yield();
2043 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2046 return ret;
2049 static void journal_free_journal_head(struct journal_head *jh)
2051 #ifdef CONFIG_JBD2_DEBUG
2052 atomic_dec(&nr_journal_heads);
2053 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2054 #endif
2055 kmem_cache_free(jbd2_journal_head_cache, jh);
2059 * A journal_head is attached to a buffer_head whenever JBD has an
2060 * interest in the buffer.
2062 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2063 * is set. This bit is tested in core kernel code where we need to take
2064 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2065 * there.
2067 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2069 * When a buffer has its BH_JBD bit set it is immune from being released by
2070 * core kernel code, mainly via ->b_count.
2072 * A journal_head may be detached from its buffer_head when the journal_head's
2073 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
2074 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
2075 * journal_head can be dropped if needed.
2077 * Various places in the kernel want to attach a journal_head to a buffer_head
2078 * _before_ attaching the journal_head to a transaction. To protect the
2079 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2080 * journal_head's b_jcount refcount by one. The caller must call
2081 * jbd2_journal_put_journal_head() to undo this.
2083 * So the typical usage would be:
2085 * (Attach a journal_head if needed. Increments b_jcount)
2086 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2087 * ...
2088 * jh->b_transaction = xxx;
2089 * jbd2_journal_put_journal_head(jh);
2091 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2092 * because it has a non-zero b_transaction.
2096 * Give a buffer_head a journal_head.
2098 * Doesn't need the journal lock.
2099 * May sleep.
2101 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2103 struct journal_head *jh;
2104 struct journal_head *new_jh = NULL;
2106 repeat:
2107 if (!buffer_jbd(bh)) {
2108 new_jh = journal_alloc_journal_head();
2109 memset(new_jh, 0, sizeof(*new_jh));
2112 jbd_lock_bh_journal_head(bh);
2113 if (buffer_jbd(bh)) {
2114 jh = bh2jh(bh);
2115 } else {
2116 J_ASSERT_BH(bh,
2117 (atomic_read(&bh->b_count) > 0) ||
2118 (bh->b_page && bh->b_page->mapping));
2120 if (!new_jh) {
2121 jbd_unlock_bh_journal_head(bh);
2122 goto repeat;
2125 jh = new_jh;
2126 new_jh = NULL; /* We consumed it */
2127 set_buffer_jbd(bh);
2128 bh->b_private = jh;
2129 jh->b_bh = bh;
2130 get_bh(bh);
2131 BUFFER_TRACE(bh, "added journal_head");
2133 jh->b_jcount++;
2134 jbd_unlock_bh_journal_head(bh);
2135 if (new_jh)
2136 journal_free_journal_head(new_jh);
2137 return bh->b_private;
2141 * Grab a ref against this buffer_head's journal_head. If it ended up not
2142 * having a journal_head, return NULL
2144 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2146 struct journal_head *jh = NULL;
2148 jbd_lock_bh_journal_head(bh);
2149 if (buffer_jbd(bh)) {
2150 jh = bh2jh(bh);
2151 jh->b_jcount++;
2153 jbd_unlock_bh_journal_head(bh);
2154 return jh;
2157 static void __journal_remove_journal_head(struct buffer_head *bh)
2159 struct journal_head *jh = bh2jh(bh);
2161 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2163 get_bh(bh);
2164 if (jh->b_jcount == 0) {
2165 if (jh->b_transaction == NULL &&
2166 jh->b_next_transaction == NULL &&
2167 jh->b_cp_transaction == NULL) {
2168 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2169 J_ASSERT_BH(bh, buffer_jbd(bh));
2170 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2171 BUFFER_TRACE(bh, "remove journal_head");
2172 if (jh->b_frozen_data) {
2173 printk(KERN_WARNING "%s: freeing "
2174 "b_frozen_data\n",
2175 __func__);
2176 jbd2_free(jh->b_frozen_data, bh->b_size);
2178 if (jh->b_committed_data) {
2179 printk(KERN_WARNING "%s: freeing "
2180 "b_committed_data\n",
2181 __func__);
2182 jbd2_free(jh->b_committed_data, bh->b_size);
2184 bh->b_private = NULL;
2185 jh->b_bh = NULL; /* debug, really */
2186 clear_buffer_jbd(bh);
2187 __brelse(bh);
2188 journal_free_journal_head(jh);
2189 } else {
2190 BUFFER_TRACE(bh, "journal_head was locked");
2196 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
2197 * and has a zero b_jcount then remove and release its journal_head. If we did
2198 * see that the buffer is not used by any transaction we also "logically"
2199 * decrement ->b_count.
2201 * We in fact take an additional increment on ->b_count as a convenience,
2202 * because the caller usually wants to do additional things with the bh
2203 * after calling here.
2204 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
2205 * time. Once the caller has run __brelse(), the buffer is eligible for
2206 * reaping by try_to_free_buffers().
2208 void jbd2_journal_remove_journal_head(struct buffer_head *bh)
2210 jbd_lock_bh_journal_head(bh);
2211 __journal_remove_journal_head(bh);
2212 jbd_unlock_bh_journal_head(bh);
2216 * Drop a reference on the passed journal_head. If it fell to zero then try to
2217 * release the journal_head from the buffer_head.
2219 void jbd2_journal_put_journal_head(struct journal_head *jh)
2221 struct buffer_head *bh = jh2bh(jh);
2223 jbd_lock_bh_journal_head(bh);
2224 J_ASSERT_JH(jh, jh->b_jcount > 0);
2225 --jh->b_jcount;
2226 if (!jh->b_jcount && !jh->b_transaction) {
2227 __journal_remove_journal_head(bh);
2228 __brelse(bh);
2230 jbd_unlock_bh_journal_head(bh);
2234 * Initialize jbd inode head
2236 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2238 jinode->i_transaction = NULL;
2239 jinode->i_next_transaction = NULL;
2240 jinode->i_vfs_inode = inode;
2241 jinode->i_flags = 0;
2242 INIT_LIST_HEAD(&jinode->i_list);
2246 * Function to be called before we start removing inode from memory (i.e.,
2247 * clear_inode() is a fine place to be called from). It removes inode from
2248 * transaction's lists.
2250 void jbd2_journal_release_jbd_inode(journal_t *journal,
2251 struct jbd2_inode *jinode)
2253 int writeout = 0;
2255 if (!journal)
2256 return;
2257 restart:
2258 spin_lock(&journal->j_list_lock);
2259 /* Is commit writing out inode - we have to wait */
2260 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2261 wait_queue_head_t *wq;
2262 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2263 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2264 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2265 spin_unlock(&journal->j_list_lock);
2266 schedule();
2267 finish_wait(wq, &wait.wait);
2268 goto restart;
2271 /* Do we need to wait for data writeback? */
2272 if (journal->j_committing_transaction == jinode->i_transaction)
2273 writeout = 1;
2274 if (jinode->i_transaction) {
2275 list_del(&jinode->i_list);
2276 jinode->i_transaction = NULL;
2278 spin_unlock(&journal->j_list_lock);
2282 * debugfs tunables
2284 #ifdef CONFIG_JBD2_DEBUG
2285 u8 jbd2_journal_enable_debug __read_mostly;
2286 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2288 #define JBD2_DEBUG_NAME "jbd2-debug"
2290 static struct dentry *jbd2_debugfs_dir;
2291 static struct dentry *jbd2_debug;
2293 static void __init jbd2_create_debugfs_entry(void)
2295 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2296 if (jbd2_debugfs_dir)
2297 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2298 jbd2_debugfs_dir,
2299 &jbd2_journal_enable_debug);
2302 static void __exit jbd2_remove_debugfs_entry(void)
2304 debugfs_remove(jbd2_debug);
2305 debugfs_remove(jbd2_debugfs_dir);
2308 #else
2310 static void __init jbd2_create_debugfs_entry(void)
2314 static void __exit jbd2_remove_debugfs_entry(void)
2318 #endif
2320 #ifdef CONFIG_PROC_FS
2322 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2324 static void __init jbd2_create_jbd_stats_proc_entry(void)
2326 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2329 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2331 if (proc_jbd2_stats)
2332 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2335 #else
2337 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2338 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2340 #endif
2342 struct kmem_cache *jbd2_handle_cache;
2344 static int __init journal_init_handle_cache(void)
2346 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
2347 sizeof(handle_t),
2348 0, /* offset */
2349 SLAB_TEMPORARY, /* flags */
2350 NULL); /* ctor */
2351 if (jbd2_handle_cache == NULL) {
2352 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2353 return -ENOMEM;
2355 return 0;
2358 static void jbd2_journal_destroy_handle_cache(void)
2360 if (jbd2_handle_cache)
2361 kmem_cache_destroy(jbd2_handle_cache);
2365 * Module startup and shutdown
2368 static int __init journal_init_caches(void)
2370 int ret;
2372 ret = jbd2_journal_init_revoke_caches();
2373 if (ret == 0)
2374 ret = journal_init_jbd2_journal_head_cache();
2375 if (ret == 0)
2376 ret = journal_init_handle_cache();
2377 return ret;
2380 static void jbd2_journal_destroy_caches(void)
2382 jbd2_journal_destroy_revoke_caches();
2383 jbd2_journal_destroy_jbd2_journal_head_cache();
2384 jbd2_journal_destroy_handle_cache();
2387 static int __init journal_init(void)
2389 int ret;
2391 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2393 ret = journal_init_caches();
2394 if (ret == 0) {
2395 jbd2_create_debugfs_entry();
2396 jbd2_create_jbd_stats_proc_entry();
2397 } else {
2398 jbd2_journal_destroy_caches();
2400 return ret;
2403 static void __exit journal_exit(void)
2405 #ifdef CONFIG_JBD2_DEBUG
2406 int n = atomic_read(&nr_journal_heads);
2407 if (n)
2408 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2409 #endif
2410 jbd2_remove_debugfs_entry();
2411 jbd2_remove_jbd_stats_proc_entry();
2412 jbd2_journal_destroy_caches();
2415 MODULE_LICENSE("GPL");
2416 module_init(journal_init);
2417 module_exit(journal_exit);