conn rcv_lock converted to spinlock, struct cor_sock created, kernel_packet skb_clone...
[cor_2_6_31.git] / fs / jbd2 / journal.c
blobe378cb383979a3e8e54234b14aa354e890c54e26
1 /*
2 * linux/fs/jbd2/journal.c
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
12 * Generic filesystem journal-writing code; part of the ext2fs
13 * journaling system.
15 * This file manages journals: areas of disk reserved for logging
16 * transactional updates. This includes the kernel journaling thread
17 * which is responsible for scheduling updates to the log.
19 * We do not actually manage the physical storage of the journal in this
20 * file: that is left to a per-journal policy function, which allows us
21 * to store the journal within a filesystem-specified area for ext2
22 * journaling (ext2 can use a reserved inode for storing the log).
25 #include <linux/module.h>
26 #include <linux/time.h>
27 #include <linux/fs.h>
28 #include <linux/jbd2.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/mm.h>
33 #include <linux/freezer.h>
34 #include <linux/pagemap.h>
35 #include <linux/kthread.h>
36 #include <linux/poison.h>
37 #include <linux/proc_fs.h>
38 #include <linux/debugfs.h>
39 #include <linux/seq_file.h>
40 #include <linux/math64.h>
41 #include <linux/hash.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/jbd2.h>
46 #include <asm/uaccess.h>
47 #include <asm/page.h>
49 EXPORT_SYMBOL(jbd2_journal_start);
50 EXPORT_SYMBOL(jbd2_journal_restart);
51 EXPORT_SYMBOL(jbd2_journal_extend);
52 EXPORT_SYMBOL(jbd2_journal_stop);
53 EXPORT_SYMBOL(jbd2_journal_lock_updates);
54 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
55 EXPORT_SYMBOL(jbd2_journal_get_write_access);
56 EXPORT_SYMBOL(jbd2_journal_get_create_access);
57 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
58 EXPORT_SYMBOL(jbd2_journal_set_triggers);
59 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
60 EXPORT_SYMBOL(jbd2_journal_release_buffer);
61 EXPORT_SYMBOL(jbd2_journal_forget);
62 #if 0
63 EXPORT_SYMBOL(journal_sync_buffer);
64 #endif
65 EXPORT_SYMBOL(jbd2_journal_flush);
66 EXPORT_SYMBOL(jbd2_journal_revoke);
68 EXPORT_SYMBOL(jbd2_journal_init_dev);
69 EXPORT_SYMBOL(jbd2_journal_init_inode);
70 EXPORT_SYMBOL(jbd2_journal_update_format);
71 EXPORT_SYMBOL(jbd2_journal_check_used_features);
72 EXPORT_SYMBOL(jbd2_journal_check_available_features);
73 EXPORT_SYMBOL(jbd2_journal_set_features);
74 EXPORT_SYMBOL(jbd2_journal_load);
75 EXPORT_SYMBOL(jbd2_journal_destroy);
76 EXPORT_SYMBOL(jbd2_journal_abort);
77 EXPORT_SYMBOL(jbd2_journal_errno);
78 EXPORT_SYMBOL(jbd2_journal_ack_err);
79 EXPORT_SYMBOL(jbd2_journal_clear_err);
80 EXPORT_SYMBOL(jbd2_log_wait_commit);
81 EXPORT_SYMBOL(jbd2_journal_start_commit);
82 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
83 EXPORT_SYMBOL(jbd2_journal_wipe);
84 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
85 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
86 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
87 EXPORT_SYMBOL(jbd2_journal_force_commit);
88 EXPORT_SYMBOL(jbd2_journal_file_inode);
89 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
90 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
91 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
93 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
94 static void __journal_abort_soft (journal_t *journal, int errno);
97 * Helper function used to manage commit timeouts
100 static void commit_timeout(unsigned long __data)
102 struct task_struct * p = (struct task_struct *) __data;
104 wake_up_process(p);
108 * kjournald2: The main thread function used to manage a logging device
109 * journal.
111 * This kernel thread is responsible for two things:
113 * 1) COMMIT: Every so often we need to commit the current state of the
114 * filesystem to disk. The journal thread is responsible for writing
115 * all of the metadata buffers to disk.
117 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
118 * of the data in that part of the log has been rewritten elsewhere on
119 * the disk. Flushing these old buffers to reclaim space in the log is
120 * known as checkpointing, and this thread is responsible for that job.
123 static int kjournald2(void *arg)
125 journal_t *journal = arg;
126 transaction_t *transaction;
129 * Set up an interval timer which can be used to trigger a commit wakeup
130 * after the commit interval expires
132 setup_timer(&journal->j_commit_timer, commit_timeout,
133 (unsigned long)current);
135 /* Record that the journal thread is running */
136 journal->j_task = current;
137 wake_up(&journal->j_wait_done_commit);
139 printk(KERN_INFO "kjournald2 starting: pid %d, dev %s, "
140 "commit interval %ld seconds\n", current->pid,
141 journal->j_devname, journal->j_commit_interval / HZ);
144 * And now, wait forever for commit wakeup events.
146 spin_lock(&journal->j_state_lock);
148 loop:
149 if (journal->j_flags & JBD2_UNMOUNT)
150 goto end_loop;
152 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
153 journal->j_commit_sequence, journal->j_commit_request);
155 if (journal->j_commit_sequence != journal->j_commit_request) {
156 jbd_debug(1, "OK, requests differ\n");
157 spin_unlock(&journal->j_state_lock);
158 del_timer_sync(&journal->j_commit_timer);
159 jbd2_journal_commit_transaction(journal);
160 spin_lock(&journal->j_state_lock);
161 goto loop;
164 wake_up(&journal->j_wait_done_commit);
165 if (freezing(current)) {
167 * The simpler the better. Flushing journal isn't a
168 * good idea, because that depends on threads that may
169 * be already stopped.
171 jbd_debug(1, "Now suspending kjournald2\n");
172 spin_unlock(&journal->j_state_lock);
173 refrigerator();
174 spin_lock(&journal->j_state_lock);
175 } else {
177 * We assume on resume that commits are already there,
178 * so we don't sleep
180 DEFINE_WAIT(wait);
181 int should_sleep = 1;
183 prepare_to_wait(&journal->j_wait_commit, &wait,
184 TASK_INTERRUPTIBLE);
185 if (journal->j_commit_sequence != journal->j_commit_request)
186 should_sleep = 0;
187 transaction = journal->j_running_transaction;
188 if (transaction && time_after_eq(jiffies,
189 transaction->t_expires))
190 should_sleep = 0;
191 if (journal->j_flags & JBD2_UNMOUNT)
192 should_sleep = 0;
193 if (should_sleep) {
194 spin_unlock(&journal->j_state_lock);
195 schedule();
196 spin_lock(&journal->j_state_lock);
198 finish_wait(&journal->j_wait_commit, &wait);
201 jbd_debug(1, "kjournald2 wakes\n");
204 * Were we woken up by a commit wakeup event?
206 transaction = journal->j_running_transaction;
207 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
208 journal->j_commit_request = transaction->t_tid;
209 jbd_debug(1, "woke because of timeout\n");
211 goto loop;
213 end_loop:
214 spin_unlock(&journal->j_state_lock);
215 del_timer_sync(&journal->j_commit_timer);
216 journal->j_task = NULL;
217 wake_up(&journal->j_wait_done_commit);
218 jbd_debug(1, "Journal thread exiting.\n");
219 return 0;
222 static int jbd2_journal_start_thread(journal_t *journal)
224 struct task_struct *t;
226 t = kthread_run(kjournald2, journal, "kjournald2");
227 if (IS_ERR(t))
228 return PTR_ERR(t);
230 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
231 return 0;
234 static void journal_kill_thread(journal_t *journal)
236 spin_lock(&journal->j_state_lock);
237 journal->j_flags |= JBD2_UNMOUNT;
239 while (journal->j_task) {
240 wake_up(&journal->j_wait_commit);
241 spin_unlock(&journal->j_state_lock);
242 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
243 spin_lock(&journal->j_state_lock);
245 spin_unlock(&journal->j_state_lock);
249 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
251 * Writes a metadata buffer to a given disk block. The actual IO is not
252 * performed but a new buffer_head is constructed which labels the data
253 * to be written with the correct destination disk block.
255 * Any magic-number escaping which needs to be done will cause a
256 * copy-out here. If the buffer happens to start with the
257 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
258 * magic number is only written to the log for descripter blocks. In
259 * this case, we copy the data and replace the first word with 0, and we
260 * return a result code which indicates that this buffer needs to be
261 * marked as an escaped buffer in the corresponding log descriptor
262 * block. The missing word can then be restored when the block is read
263 * during recovery.
265 * If the source buffer has already been modified by a new transaction
266 * since we took the last commit snapshot, we use the frozen copy of
267 * that data for IO. If we end up using the existing buffer_head's data
268 * for the write, then we *have* to lock the buffer to prevent anyone
269 * else from using and possibly modifying it while the IO is in
270 * progress.
272 * The function returns a pointer to the buffer_heads to be used for IO.
274 * We assume that the journal has already been locked in this function.
276 * Return value:
277 * <0: Error
278 * >=0: Finished OK
280 * On success:
281 * Bit 0 set == escape performed on the data
282 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
285 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
286 struct journal_head *jh_in,
287 struct journal_head **jh_out,
288 unsigned long long blocknr)
290 int need_copy_out = 0;
291 int done_copy_out = 0;
292 int do_escape = 0;
293 char *mapped_data;
294 struct buffer_head *new_bh;
295 struct journal_head *new_jh;
296 struct page *new_page;
297 unsigned int new_offset;
298 struct buffer_head *bh_in = jh2bh(jh_in);
299 struct jbd2_buffer_trigger_type *triggers;
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 triggers = jh_in->b_frozen_triggers;
331 } else {
332 new_page = jh2bh(jh_in)->b_page;
333 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
334 triggers = jh_in->b_triggers;
337 mapped_data = kmap_atomic(new_page, KM_USER0);
339 * Fire any commit trigger. Do this before checking for escaping,
340 * as the trigger may modify the magic offset. If a copy-out
341 * happens afterwards, it will have the correct data in the buffer.
343 jbd2_buffer_commit_trigger(jh_in, mapped_data + new_offset,
344 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 jbd_lock_bh_state(bh_in);
365 if (jh_in->b_frozen_data) {
366 jbd2_free(tmp, bh_in->b_size);
367 goto repeat;
370 jh_in->b_frozen_data = tmp;
371 mapped_data = kmap_atomic(new_page, KM_USER0);
372 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
373 kunmap_atomic(mapped_data, KM_USER0);
375 new_page = virt_to_page(tmp);
376 new_offset = offset_in_page(tmp);
377 done_copy_out = 1;
380 * This isn't strictly necessary, as we're using frozen
381 * data for the escaping, but it keeps consistency with
382 * b_frozen_data usage.
384 jh_in->b_frozen_triggers = jh_in->b_triggers;
388 * Did we need to do an escaping? Now we've done all the
389 * copying, we can finally do so.
391 if (do_escape) {
392 mapped_data = kmap_atomic(new_page, KM_USER0);
393 *((unsigned int *)(mapped_data + new_offset)) = 0;
394 kunmap_atomic(mapped_data, KM_USER0);
397 set_bh_page(new_bh, new_page, new_offset);
398 new_jh->b_transaction = NULL;
399 new_bh->b_size = jh2bh(jh_in)->b_size;
400 new_bh->b_bdev = transaction->t_journal->j_dev;
401 new_bh->b_blocknr = blocknr;
402 set_buffer_mapped(new_bh);
403 set_buffer_dirty(new_bh);
405 *jh_out = new_jh;
408 * The to-be-written buffer needs to get moved to the io queue,
409 * and the original buffer whose contents we are shadowing or
410 * copying is moved to the transaction's shadow queue.
412 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
413 spin_lock(&journal->j_list_lock);
414 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
415 spin_unlock(&journal->j_list_lock);
416 jbd_unlock_bh_state(bh_in);
418 JBUFFER_TRACE(new_jh, "file as BJ_IO");
419 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
421 return do_escape | (done_copy_out << 1);
425 * Allocation code for the journal file. Manage the space left in the
426 * journal, so that we can begin checkpointing when appropriate.
430 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
432 * Called with the journal already locked.
434 * Called under j_state_lock
437 int __jbd2_log_space_left(journal_t *journal)
439 int left = journal->j_free;
441 assert_spin_locked(&journal->j_state_lock);
444 * Be pessimistic here about the number of those free blocks which
445 * might be required for log descriptor control blocks.
448 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
450 left -= MIN_LOG_RESERVED_BLOCKS;
452 if (left <= 0)
453 return 0;
454 left -= (left >> 3);
455 return left;
459 * Called under j_state_lock. Returns true if a transaction commit was started.
461 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
464 * Are we already doing a recent enough commit?
466 if (!tid_geq(journal->j_commit_request, target)) {
468 * We want a new commit: OK, mark the request and wakup the
469 * commit thread. We do _not_ do the commit ourselves.
472 journal->j_commit_request = target;
473 jbd_debug(1, "JBD: requesting commit %d/%d\n",
474 journal->j_commit_request,
475 journal->j_commit_sequence);
476 wake_up(&journal->j_wait_commit);
477 return 1;
479 return 0;
482 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
484 int ret;
486 spin_lock(&journal->j_state_lock);
487 ret = __jbd2_log_start_commit(journal, tid);
488 spin_unlock(&journal->j_state_lock);
489 return ret;
493 * Force and wait upon a commit if the calling process is not within
494 * transaction. This is used for forcing out undo-protected data which contains
495 * bitmaps, when the fs is running out of space.
497 * We can only force the running transaction if we don't have an active handle;
498 * otherwise, we will deadlock.
500 * Returns true if a transaction was started.
502 int jbd2_journal_force_commit_nested(journal_t *journal)
504 transaction_t *transaction = NULL;
505 tid_t tid;
507 spin_lock(&journal->j_state_lock);
508 if (journal->j_running_transaction && !current->journal_info) {
509 transaction = journal->j_running_transaction;
510 __jbd2_log_start_commit(journal, transaction->t_tid);
511 } else if (journal->j_committing_transaction)
512 transaction = journal->j_committing_transaction;
514 if (!transaction) {
515 spin_unlock(&journal->j_state_lock);
516 return 0; /* Nothing to retry */
519 tid = transaction->t_tid;
520 spin_unlock(&journal->j_state_lock);
521 jbd2_log_wait_commit(journal, tid);
522 return 1;
526 * Start a commit of the current running transaction (if any). Returns true
527 * if a transaction is going to be committed (or is currently already
528 * committing), and fills its tid in at *ptid
530 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
532 int ret = 0;
534 spin_lock(&journal->j_state_lock);
535 if (journal->j_running_transaction) {
536 tid_t tid = journal->j_running_transaction->t_tid;
538 __jbd2_log_start_commit(journal, tid);
539 /* There's a running transaction and we've just made sure
540 * it's commit has been scheduled. */
541 if (ptid)
542 *ptid = tid;
543 ret = 1;
544 } else if (journal->j_committing_transaction) {
546 * If ext3_write_super() recently started a commit, then we
547 * have to wait for completion of that transaction
549 if (ptid)
550 *ptid = journal->j_committing_transaction->t_tid;
551 ret = 1;
553 spin_unlock(&journal->j_state_lock);
554 return ret;
558 * Wait for a specified commit to complete.
559 * The caller may not hold the journal lock.
561 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
563 int err = 0;
565 #ifdef CONFIG_JBD2_DEBUG
566 spin_lock(&journal->j_state_lock);
567 if (!tid_geq(journal->j_commit_request, tid)) {
568 printk(KERN_EMERG
569 "%s: error: j_commit_request=%d, tid=%d\n",
570 __func__, journal->j_commit_request, tid);
572 spin_unlock(&journal->j_state_lock);
573 #endif
574 spin_lock(&journal->j_state_lock);
575 while (tid_gt(tid, journal->j_commit_sequence)) {
576 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
577 tid, journal->j_commit_sequence);
578 wake_up(&journal->j_wait_commit);
579 spin_unlock(&journal->j_state_lock);
580 wait_event(journal->j_wait_done_commit,
581 !tid_gt(tid, journal->j_commit_sequence));
582 spin_lock(&journal->j_state_lock);
584 spin_unlock(&journal->j_state_lock);
586 if (unlikely(is_journal_aborted(journal))) {
587 printk(KERN_EMERG "journal commit I/O error\n");
588 err = -EIO;
590 return err;
594 * Log buffer allocation routines:
597 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
599 unsigned long blocknr;
601 spin_lock(&journal->j_state_lock);
602 J_ASSERT(journal->j_free > 1);
604 blocknr = journal->j_head;
605 journal->j_head++;
606 journal->j_free--;
607 if (journal->j_head == journal->j_last)
608 journal->j_head = journal->j_first;
609 spin_unlock(&journal->j_state_lock);
610 return jbd2_journal_bmap(journal, blocknr, retp);
614 * Conversion of logical to physical block numbers for the journal
616 * On external journals the journal blocks are identity-mapped, so
617 * this is a no-op. If needed, we can use j_blk_offset - everything is
618 * ready.
620 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
621 unsigned long long *retp)
623 int err = 0;
624 unsigned long long ret;
626 if (journal->j_inode) {
627 ret = bmap(journal->j_inode, blocknr);
628 if (ret)
629 *retp = ret;
630 else {
631 printk(KERN_ALERT "%s: journal block not found "
632 "at offset %lu on %s\n",
633 __func__, blocknr, journal->j_devname);
634 err = -EIO;
635 __journal_abort_soft(journal, err);
637 } else {
638 *retp = blocknr; /* +journal->j_blk_offset */
640 return err;
644 * We play buffer_head aliasing tricks to write data/metadata blocks to
645 * the journal without copying their contents, but for journal
646 * descriptor blocks we do need to generate bona fide buffers.
648 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
649 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
650 * But we don't bother doing that, so there will be coherency problems with
651 * mmaps of blockdevs which hold live JBD-controlled filesystems.
653 struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
655 struct buffer_head *bh;
656 unsigned long long blocknr;
657 int err;
659 err = jbd2_journal_next_log_block(journal, &blocknr);
661 if (err)
662 return NULL;
664 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
665 if (!bh)
666 return NULL;
667 lock_buffer(bh);
668 memset(bh->b_data, 0, journal->j_blocksize);
669 set_buffer_uptodate(bh);
670 unlock_buffer(bh);
671 BUFFER_TRACE(bh, "return this buffer");
672 return jbd2_journal_add_journal_head(bh);
675 struct jbd2_stats_proc_session {
676 journal_t *journal;
677 struct transaction_stats_s *stats;
678 int start;
679 int max;
682 static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
683 struct transaction_stats_s *ts,
684 int first)
686 if (ts == s->stats + s->max)
687 ts = s->stats;
688 if (!first && ts == s->stats + s->start)
689 return NULL;
690 while (ts->ts_type == 0) {
691 ts++;
692 if (ts == s->stats + s->max)
693 ts = s->stats;
694 if (ts == s->stats + s->start)
695 return NULL;
697 return ts;
701 static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
703 struct jbd2_stats_proc_session *s = seq->private;
704 struct transaction_stats_s *ts;
705 int l = *pos;
707 if (l == 0)
708 return SEQ_START_TOKEN;
709 ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
710 if (!ts)
711 return NULL;
712 l--;
713 while (l) {
714 ts = jbd2_history_skip_empty(s, ++ts, 0);
715 if (!ts)
716 break;
717 l--;
719 return ts;
722 static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
724 struct jbd2_stats_proc_session *s = seq->private;
725 struct transaction_stats_s *ts = v;
727 ++*pos;
728 if (v == SEQ_START_TOKEN)
729 return jbd2_history_skip_empty(s, s->stats + s->start, 1);
730 else
731 return jbd2_history_skip_empty(s, ++ts, 0);
734 static int jbd2_seq_history_show(struct seq_file *seq, void *v)
736 struct transaction_stats_s *ts = v;
737 if (v == SEQ_START_TOKEN) {
738 seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
739 "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
740 "wait", "run", "lock", "flush", "log", "hndls",
741 "block", "inlog", "ctime", "write", "drop",
742 "close");
743 return 0;
745 if (ts->ts_type == JBD2_STATS_RUN)
746 seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
747 "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
748 jiffies_to_msecs(ts->u.run.rs_wait),
749 jiffies_to_msecs(ts->u.run.rs_running),
750 jiffies_to_msecs(ts->u.run.rs_locked),
751 jiffies_to_msecs(ts->u.run.rs_flushing),
752 jiffies_to_msecs(ts->u.run.rs_logging),
753 ts->u.run.rs_handle_count,
754 ts->u.run.rs_blocks,
755 ts->u.run.rs_blocks_logged);
756 else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
757 seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
758 "C", ts->ts_tid, " ",
759 jiffies_to_msecs(ts->u.chp.cs_chp_time),
760 ts->u.chp.cs_written, ts->u.chp.cs_dropped,
761 ts->u.chp.cs_forced_to_close);
762 else
763 J_ASSERT(0);
764 return 0;
767 static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
771 static struct seq_operations jbd2_seq_history_ops = {
772 .start = jbd2_seq_history_start,
773 .next = jbd2_seq_history_next,
774 .stop = jbd2_seq_history_stop,
775 .show = jbd2_seq_history_show,
778 static int jbd2_seq_history_open(struct inode *inode, struct file *file)
780 journal_t *journal = PDE(inode)->data;
781 struct jbd2_stats_proc_session *s;
782 int rc, size;
784 s = kmalloc(sizeof(*s), GFP_KERNEL);
785 if (s == NULL)
786 return -ENOMEM;
787 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
788 s->stats = kmalloc(size, GFP_KERNEL);
789 if (s->stats == NULL) {
790 kfree(s);
791 return -ENOMEM;
793 spin_lock(&journal->j_history_lock);
794 memcpy(s->stats, journal->j_history, size);
795 s->max = journal->j_history_max;
796 s->start = journal->j_history_cur % s->max;
797 spin_unlock(&journal->j_history_lock);
799 rc = seq_open(file, &jbd2_seq_history_ops);
800 if (rc == 0) {
801 struct seq_file *m = file->private_data;
802 m->private = s;
803 } else {
804 kfree(s->stats);
805 kfree(s);
807 return rc;
811 static int jbd2_seq_history_release(struct inode *inode, struct file *file)
813 struct seq_file *seq = file->private_data;
814 struct jbd2_stats_proc_session *s = seq->private;
816 kfree(s->stats);
817 kfree(s);
818 return seq_release(inode, file);
821 static struct file_operations jbd2_seq_history_fops = {
822 .owner = THIS_MODULE,
823 .open = jbd2_seq_history_open,
824 .read = seq_read,
825 .llseek = seq_lseek,
826 .release = jbd2_seq_history_release,
829 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
831 return *pos ? NULL : SEQ_START_TOKEN;
834 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
836 return NULL;
839 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
841 struct jbd2_stats_proc_session *s = seq->private;
843 if (v != SEQ_START_TOKEN)
844 return 0;
845 seq_printf(seq, "%lu transaction, each upto %u blocks\n",
846 s->stats->ts_tid,
847 s->journal->j_max_transaction_buffers);
848 if (s->stats->ts_tid == 0)
849 return 0;
850 seq_printf(seq, "average: \n %ums waiting for transaction\n",
851 jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
852 seq_printf(seq, " %ums running transaction\n",
853 jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
854 seq_printf(seq, " %ums transaction was being locked\n",
855 jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
856 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
857 jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
858 seq_printf(seq, " %ums logging transaction\n",
859 jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
860 seq_printf(seq, " %lluus average transaction commit time\n",
861 div_u64(s->journal->j_average_commit_time, 1000));
862 seq_printf(seq, " %lu handles per transaction\n",
863 s->stats->u.run.rs_handle_count / s->stats->ts_tid);
864 seq_printf(seq, " %lu blocks per transaction\n",
865 s->stats->u.run.rs_blocks / s->stats->ts_tid);
866 seq_printf(seq, " %lu logged blocks per transaction\n",
867 s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
868 return 0;
871 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
875 static struct seq_operations jbd2_seq_info_ops = {
876 .start = jbd2_seq_info_start,
877 .next = jbd2_seq_info_next,
878 .stop = jbd2_seq_info_stop,
879 .show = jbd2_seq_info_show,
882 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
884 journal_t *journal = PDE(inode)->data;
885 struct jbd2_stats_proc_session *s;
886 int rc, size;
888 s = kmalloc(sizeof(*s), GFP_KERNEL);
889 if (s == NULL)
890 return -ENOMEM;
891 size = sizeof(struct transaction_stats_s);
892 s->stats = kmalloc(size, GFP_KERNEL);
893 if (s->stats == NULL) {
894 kfree(s);
895 return -ENOMEM;
897 spin_lock(&journal->j_history_lock);
898 memcpy(s->stats, &journal->j_stats, size);
899 s->journal = journal;
900 spin_unlock(&journal->j_history_lock);
902 rc = seq_open(file, &jbd2_seq_info_ops);
903 if (rc == 0) {
904 struct seq_file *m = file->private_data;
905 m->private = s;
906 } else {
907 kfree(s->stats);
908 kfree(s);
910 return rc;
914 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
916 struct seq_file *seq = file->private_data;
917 struct jbd2_stats_proc_session *s = seq->private;
918 kfree(s->stats);
919 kfree(s);
920 return seq_release(inode, file);
923 static struct file_operations jbd2_seq_info_fops = {
924 .owner = THIS_MODULE,
925 .open = jbd2_seq_info_open,
926 .read = seq_read,
927 .llseek = seq_lseek,
928 .release = jbd2_seq_info_release,
931 static struct proc_dir_entry *proc_jbd2_stats;
933 static void jbd2_stats_proc_init(journal_t *journal)
935 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
936 if (journal->j_proc_entry) {
937 proc_create_data("history", S_IRUGO, journal->j_proc_entry,
938 &jbd2_seq_history_fops, journal);
939 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
940 &jbd2_seq_info_fops, journal);
944 static void jbd2_stats_proc_exit(journal_t *journal)
946 remove_proc_entry("info", journal->j_proc_entry);
947 remove_proc_entry("history", journal->j_proc_entry);
948 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
951 static void journal_init_stats(journal_t *journal)
953 int size;
955 if (!proc_jbd2_stats)
956 return;
958 journal->j_history_max = 100;
959 size = sizeof(struct transaction_stats_s) * journal->j_history_max;
960 journal->j_history = kzalloc(size, GFP_KERNEL);
961 if (!journal->j_history) {
962 journal->j_history_max = 0;
963 return;
965 spin_lock_init(&journal->j_history_lock);
969 * Management for journal control blocks: functions to create and
970 * destroy journal_t structures, and to initialise and read existing
971 * journal blocks from disk. */
973 /* First: create and setup a journal_t object in memory. We initialise
974 * very few fields yet: that has to wait until we have created the
975 * journal structures from from scratch, or loaded them from disk. */
977 static journal_t * journal_init_common (void)
979 journal_t *journal;
980 int err;
982 journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
983 if (!journal)
984 goto fail;
986 init_waitqueue_head(&journal->j_wait_transaction_locked);
987 init_waitqueue_head(&journal->j_wait_logspace);
988 init_waitqueue_head(&journal->j_wait_done_commit);
989 init_waitqueue_head(&journal->j_wait_checkpoint);
990 init_waitqueue_head(&journal->j_wait_commit);
991 init_waitqueue_head(&journal->j_wait_updates);
992 mutex_init(&journal->j_barrier);
993 mutex_init(&journal->j_checkpoint_mutex);
994 spin_lock_init(&journal->j_revoke_lock);
995 spin_lock_init(&journal->j_list_lock);
996 spin_lock_init(&journal->j_state_lock);
998 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
999 journal->j_min_batch_time = 0;
1000 journal->j_max_batch_time = 15000; /* 15ms */
1002 /* The journal is marked for error until we succeed with recovery! */
1003 journal->j_flags = JBD2_ABORT;
1005 /* Set up a default-sized revoke table for the new mount. */
1006 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1007 if (err) {
1008 kfree(journal);
1009 goto fail;
1012 journal_init_stats(journal);
1014 return journal;
1015 fail:
1016 return NULL;
1019 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1021 * Create a journal structure assigned some fixed set of disk blocks to
1022 * the journal. We don't actually touch those disk blocks yet, but we
1023 * need to set up all of the mapping information to tell the journaling
1024 * system where the journal blocks are.
1029 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1030 * @bdev: Block device on which to create the journal
1031 * @fs_dev: Device which hold journalled filesystem for this journal.
1032 * @start: Block nr Start of journal.
1033 * @len: Length of the journal in blocks.
1034 * @blocksize: blocksize of journalling device
1036 * Returns: a newly created journal_t *
1038 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1039 * range of blocks on an arbitrary block device.
1042 journal_t * jbd2_journal_init_dev(struct block_device *bdev,
1043 struct block_device *fs_dev,
1044 unsigned long long start, int len, int blocksize)
1046 journal_t *journal = journal_init_common();
1047 struct buffer_head *bh;
1048 char *p;
1049 int n;
1051 if (!journal)
1052 return NULL;
1054 /* journal descriptor can store up to n blocks -bzzz */
1055 journal->j_blocksize = blocksize;
1056 jbd2_stats_proc_init(journal);
1057 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1058 journal->j_wbufsize = n;
1059 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1060 if (!journal->j_wbuf) {
1061 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1062 __func__);
1063 goto out_err;
1065 journal->j_dev = bdev;
1066 journal->j_fs_dev = fs_dev;
1067 journal->j_blk_offset = start;
1068 journal->j_maxlen = len;
1069 bdevname(journal->j_dev, journal->j_devname);
1070 p = journal->j_devname;
1071 while ((p = strchr(p, '/')))
1072 *p = '!';
1074 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
1075 if (!bh) {
1076 printk(KERN_ERR
1077 "%s: Cannot get buffer for journal superblock\n",
1078 __func__);
1079 goto out_err;
1081 journal->j_sb_buffer = bh;
1082 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1084 return journal;
1085 out_err:
1086 jbd2_stats_proc_exit(journal);
1087 kfree(journal);
1088 return NULL;
1092 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1093 * @inode: An inode to create the journal in
1095 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1096 * the journal. The inode must exist already, must support bmap() and
1097 * must have all data blocks preallocated.
1099 journal_t * jbd2_journal_init_inode (struct inode *inode)
1101 struct buffer_head *bh;
1102 journal_t *journal = journal_init_common();
1103 char *p;
1104 int err;
1105 int n;
1106 unsigned long long blocknr;
1108 if (!journal)
1109 return NULL;
1111 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1112 journal->j_inode = inode;
1113 bdevname(journal->j_dev, journal->j_devname);
1114 p = journal->j_devname;
1115 while ((p = strchr(p, '/')))
1116 *p = '!';
1117 p = journal->j_devname + strlen(journal->j_devname);
1118 sprintf(p, ":%lu", journal->j_inode->i_ino);
1119 jbd_debug(1,
1120 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1121 journal, inode->i_sb->s_id, inode->i_ino,
1122 (long long) inode->i_size,
1123 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1125 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1126 journal->j_blocksize = inode->i_sb->s_blocksize;
1127 jbd2_stats_proc_init(journal);
1129 /* journal descriptor can store up to n blocks -bzzz */
1130 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1131 journal->j_wbufsize = n;
1132 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1133 if (!journal->j_wbuf) {
1134 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
1135 __func__);
1136 goto out_err;
1139 err = jbd2_journal_bmap(journal, 0, &blocknr);
1140 /* If that failed, give up */
1141 if (err) {
1142 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
1143 __func__);
1144 goto out_err;
1147 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
1148 if (!bh) {
1149 printk(KERN_ERR
1150 "%s: Cannot get buffer for journal superblock\n",
1151 __func__);
1152 goto out_err;
1154 journal->j_sb_buffer = bh;
1155 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1157 return journal;
1158 out_err:
1159 jbd2_stats_proc_exit(journal);
1160 kfree(journal);
1161 return NULL;
1165 * If the journal init or create aborts, we need to mark the journal
1166 * superblock as being NULL to prevent the journal destroy from writing
1167 * back a bogus superblock.
1169 static void journal_fail_superblock (journal_t *journal)
1171 struct buffer_head *bh = journal->j_sb_buffer;
1172 brelse(bh);
1173 journal->j_sb_buffer = NULL;
1177 * Given a journal_t structure, initialise the various fields for
1178 * startup of a new journaling session. We use this both when creating
1179 * a journal, and after recovering an old journal to reset it for
1180 * subsequent use.
1183 static int journal_reset(journal_t *journal)
1185 journal_superblock_t *sb = journal->j_superblock;
1186 unsigned long long first, last;
1188 first = be32_to_cpu(sb->s_first);
1189 last = be32_to_cpu(sb->s_maxlen);
1191 journal->j_first = first;
1192 journal->j_last = last;
1194 journal->j_head = first;
1195 journal->j_tail = first;
1196 journal->j_free = last - first;
1198 journal->j_tail_sequence = journal->j_transaction_sequence;
1199 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1200 journal->j_commit_request = journal->j_commit_sequence;
1202 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1204 /* Add the dynamic fields and write it to disk. */
1205 jbd2_journal_update_superblock(journal, 1);
1206 return jbd2_journal_start_thread(journal);
1210 * void jbd2_journal_update_superblock() - Update journal sb on disk.
1211 * @journal: The journal to update.
1212 * @wait: Set to '0' if you don't want to wait for IO completion.
1214 * Update a journal's dynamic superblock fields and write it to disk,
1215 * optionally waiting for the IO to complete.
1217 void jbd2_journal_update_superblock(journal_t *journal, int wait)
1219 journal_superblock_t *sb = journal->j_superblock;
1220 struct buffer_head *bh = journal->j_sb_buffer;
1223 * As a special case, if the on-disk copy is already marked as needing
1224 * no recovery (s_start == 0) and there are no outstanding transactions
1225 * in the filesystem, then we can safely defer the superblock update
1226 * until the next commit by setting JBD2_FLUSHED. This avoids
1227 * attempting a write to a potential-readonly device.
1229 if (sb->s_start == 0 && journal->j_tail_sequence ==
1230 journal->j_transaction_sequence) {
1231 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1232 "(start %ld, seq %d, errno %d)\n",
1233 journal->j_tail, journal->j_tail_sequence,
1234 journal->j_errno);
1235 goto out;
1238 if (buffer_write_io_error(bh)) {
1240 * Oh, dear. A previous attempt to write the journal
1241 * superblock failed. This could happen because the
1242 * USB device was yanked out. Or it could happen to
1243 * be a transient write error and maybe the block will
1244 * be remapped. Nothing we can do but to retry the
1245 * write and hope for the best.
1247 printk(KERN_ERR "JBD2: previous I/O error detected "
1248 "for journal superblock update for %s.\n",
1249 journal->j_devname);
1250 clear_buffer_write_io_error(bh);
1251 set_buffer_uptodate(bh);
1254 spin_lock(&journal->j_state_lock);
1255 jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
1256 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1258 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1259 sb->s_start = cpu_to_be32(journal->j_tail);
1260 sb->s_errno = cpu_to_be32(journal->j_errno);
1261 spin_unlock(&journal->j_state_lock);
1263 BUFFER_TRACE(bh, "marking dirty");
1264 mark_buffer_dirty(bh);
1265 if (wait) {
1266 sync_dirty_buffer(bh);
1267 if (buffer_write_io_error(bh)) {
1268 printk(KERN_ERR "JBD2: I/O error detected "
1269 "when updating journal superblock for %s.\n",
1270 journal->j_devname);
1271 clear_buffer_write_io_error(bh);
1272 set_buffer_uptodate(bh);
1274 } else
1275 ll_rw_block(SWRITE, 1, &bh);
1277 out:
1278 /* If we have just flushed the log (by marking s_start==0), then
1279 * any future commit will have to be careful to update the
1280 * superblock again to re-record the true start of the log. */
1282 spin_lock(&journal->j_state_lock);
1283 if (sb->s_start)
1284 journal->j_flags &= ~JBD2_FLUSHED;
1285 else
1286 journal->j_flags |= JBD2_FLUSHED;
1287 spin_unlock(&journal->j_state_lock);
1291 * Read the superblock for a given journal, performing initial
1292 * validation of the format.
1295 static int journal_get_superblock(journal_t *journal)
1297 struct buffer_head *bh;
1298 journal_superblock_t *sb;
1299 int err = -EIO;
1301 bh = journal->j_sb_buffer;
1303 J_ASSERT(bh != NULL);
1304 if (!buffer_uptodate(bh)) {
1305 ll_rw_block(READ, 1, &bh);
1306 wait_on_buffer(bh);
1307 if (!buffer_uptodate(bh)) {
1308 printk (KERN_ERR
1309 "JBD: IO error reading journal superblock\n");
1310 goto out;
1314 sb = journal->j_superblock;
1316 err = -EINVAL;
1318 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1319 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1320 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1321 goto out;
1324 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1325 case JBD2_SUPERBLOCK_V1:
1326 journal->j_format_version = 1;
1327 break;
1328 case JBD2_SUPERBLOCK_V2:
1329 journal->j_format_version = 2;
1330 break;
1331 default:
1332 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1333 goto out;
1336 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1337 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1338 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1339 printk (KERN_WARNING "JBD: journal file too short\n");
1340 goto out;
1343 return 0;
1345 out:
1346 journal_fail_superblock(journal);
1347 return err;
1351 * Load the on-disk journal superblock and read the key fields into the
1352 * journal_t.
1355 static int load_superblock(journal_t *journal)
1357 int err;
1358 journal_superblock_t *sb;
1360 err = journal_get_superblock(journal);
1361 if (err)
1362 return err;
1364 sb = journal->j_superblock;
1366 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1367 journal->j_tail = be32_to_cpu(sb->s_start);
1368 journal->j_first = be32_to_cpu(sb->s_first);
1369 journal->j_last = be32_to_cpu(sb->s_maxlen);
1370 journal->j_errno = be32_to_cpu(sb->s_errno);
1372 return 0;
1377 * int jbd2_journal_load() - Read journal from disk.
1378 * @journal: Journal to act on.
1380 * Given a journal_t structure which tells us which disk blocks contain
1381 * a journal, read the journal from disk to initialise the in-memory
1382 * structures.
1384 int jbd2_journal_load(journal_t *journal)
1386 int err;
1387 journal_superblock_t *sb;
1389 err = load_superblock(journal);
1390 if (err)
1391 return err;
1393 sb = journal->j_superblock;
1394 /* If this is a V2 superblock, then we have to check the
1395 * features flags on it. */
1397 if (journal->j_format_version >= 2) {
1398 if ((sb->s_feature_ro_compat &
1399 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1400 (sb->s_feature_incompat &
1401 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1402 printk (KERN_WARNING
1403 "JBD: Unrecognised features on journal\n");
1404 return -EINVAL;
1408 /* Let the recovery code check whether it needs to recover any
1409 * data from the journal. */
1410 if (jbd2_journal_recover(journal))
1411 goto recovery_error;
1413 /* OK, we've finished with the dynamic journal bits:
1414 * reinitialise the dynamic contents of the superblock in memory
1415 * and reset them on disk. */
1416 if (journal_reset(journal))
1417 goto recovery_error;
1419 journal->j_flags &= ~JBD2_ABORT;
1420 journal->j_flags |= JBD2_LOADED;
1421 return 0;
1423 recovery_error:
1424 printk (KERN_WARNING "JBD: recovery failed\n");
1425 return -EIO;
1429 * void jbd2_journal_destroy() - Release a journal_t structure.
1430 * @journal: Journal to act on.
1432 * Release a journal_t structure once it is no longer in use by the
1433 * journaled object.
1434 * Return <0 if we couldn't clean up the journal.
1436 int jbd2_journal_destroy(journal_t *journal)
1438 int err = 0;
1440 /* Wait for the commit thread to wake up and die. */
1441 journal_kill_thread(journal);
1443 /* Force a final log commit */
1444 if (journal->j_running_transaction)
1445 jbd2_journal_commit_transaction(journal);
1447 /* Force any old transactions to disk */
1449 /* Totally anal locking here... */
1450 spin_lock(&journal->j_list_lock);
1451 while (journal->j_checkpoint_transactions != NULL) {
1452 spin_unlock(&journal->j_list_lock);
1453 mutex_lock(&journal->j_checkpoint_mutex);
1454 jbd2_log_do_checkpoint(journal);
1455 mutex_unlock(&journal->j_checkpoint_mutex);
1456 spin_lock(&journal->j_list_lock);
1459 J_ASSERT(journal->j_running_transaction == NULL);
1460 J_ASSERT(journal->j_committing_transaction == NULL);
1461 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1462 spin_unlock(&journal->j_list_lock);
1464 if (journal->j_sb_buffer) {
1465 if (!is_journal_aborted(journal)) {
1466 /* We can now mark the journal as empty. */
1467 journal->j_tail = 0;
1468 journal->j_tail_sequence =
1469 ++journal->j_transaction_sequence;
1470 jbd2_journal_update_superblock(journal, 1);
1471 } else {
1472 err = -EIO;
1474 brelse(journal->j_sb_buffer);
1477 if (journal->j_proc_entry)
1478 jbd2_stats_proc_exit(journal);
1479 if (journal->j_inode)
1480 iput(journal->j_inode);
1481 if (journal->j_revoke)
1482 jbd2_journal_destroy_revoke(journal);
1483 kfree(journal->j_wbuf);
1484 kfree(journal);
1486 return err;
1491 *int jbd2_journal_check_used_features () - Check if features specified are used.
1492 * @journal: Journal to check.
1493 * @compat: bitmask of compatible features
1494 * @ro: bitmask of features that force read-only mount
1495 * @incompat: bitmask of incompatible features
1497 * Check whether the journal uses all of a given set of
1498 * features. Return true (non-zero) if it does.
1501 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1502 unsigned long ro, unsigned long incompat)
1504 journal_superblock_t *sb;
1506 if (!compat && !ro && !incompat)
1507 return 1;
1508 if (journal->j_format_version == 1)
1509 return 0;
1511 sb = journal->j_superblock;
1513 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1514 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1515 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1516 return 1;
1518 return 0;
1522 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1523 * @journal: Journal to check.
1524 * @compat: bitmask of compatible features
1525 * @ro: bitmask of features that force read-only mount
1526 * @incompat: bitmask of incompatible features
1528 * Check whether the journaling code supports the use of
1529 * all of a given set of features on this journal. Return true
1530 * (non-zero) if it can. */
1532 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1533 unsigned long ro, unsigned long incompat)
1535 journal_superblock_t *sb;
1537 if (!compat && !ro && !incompat)
1538 return 1;
1540 sb = journal->j_superblock;
1542 /* We can support any known requested features iff the
1543 * superblock is in version 2. Otherwise we fail to support any
1544 * extended sb features. */
1546 if (journal->j_format_version != 2)
1547 return 0;
1549 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1550 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1551 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1552 return 1;
1554 return 0;
1558 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1559 * @journal: Journal to act on.
1560 * @compat: bitmask of compatible features
1561 * @ro: bitmask of features that force read-only mount
1562 * @incompat: bitmask of incompatible features
1564 * Mark a given journal feature as present on the
1565 * superblock. Returns true if the requested features could be set.
1569 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1570 unsigned long ro, unsigned long incompat)
1572 journal_superblock_t *sb;
1574 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1575 return 1;
1577 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1578 return 0;
1580 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1581 compat, ro, incompat);
1583 sb = journal->j_superblock;
1585 sb->s_feature_compat |= cpu_to_be32(compat);
1586 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1587 sb->s_feature_incompat |= cpu_to_be32(incompat);
1589 return 1;
1593 * jbd2_journal_clear_features () - Clear a given journal feature in the
1594 * superblock
1595 * @journal: Journal to act on.
1596 * @compat: bitmask of compatible features
1597 * @ro: bitmask of features that force read-only mount
1598 * @incompat: bitmask of incompatible features
1600 * Clear a given journal feature as present on the
1601 * superblock.
1603 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1604 unsigned long ro, unsigned long incompat)
1606 journal_superblock_t *sb;
1608 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1609 compat, ro, incompat);
1611 sb = journal->j_superblock;
1613 sb->s_feature_compat &= ~cpu_to_be32(compat);
1614 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1615 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1617 EXPORT_SYMBOL(jbd2_journal_clear_features);
1620 * int jbd2_journal_update_format () - Update on-disk journal structure.
1621 * @journal: Journal to act on.
1623 * Given an initialised but unloaded journal struct, poke about in the
1624 * on-disk structure to update it to the most recent supported version.
1626 int jbd2_journal_update_format (journal_t *journal)
1628 journal_superblock_t *sb;
1629 int err;
1631 err = journal_get_superblock(journal);
1632 if (err)
1633 return err;
1635 sb = journal->j_superblock;
1637 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1638 case JBD2_SUPERBLOCK_V2:
1639 return 0;
1640 case JBD2_SUPERBLOCK_V1:
1641 return journal_convert_superblock_v1(journal, sb);
1642 default:
1643 break;
1645 return -EINVAL;
1648 static int journal_convert_superblock_v1(journal_t *journal,
1649 journal_superblock_t *sb)
1651 int offset, blocksize;
1652 struct buffer_head *bh;
1654 printk(KERN_WARNING
1655 "JBD: Converting superblock from version 1 to 2.\n");
1657 /* Pre-initialise new fields to zero */
1658 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1659 blocksize = be32_to_cpu(sb->s_blocksize);
1660 memset(&sb->s_feature_compat, 0, blocksize-offset);
1662 sb->s_nr_users = cpu_to_be32(1);
1663 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
1664 journal->j_format_version = 2;
1666 bh = journal->j_sb_buffer;
1667 BUFFER_TRACE(bh, "marking dirty");
1668 mark_buffer_dirty(bh);
1669 sync_dirty_buffer(bh);
1670 return 0;
1675 * int jbd2_journal_flush () - Flush journal
1676 * @journal: Journal to act on.
1678 * Flush all data for a given journal to disk and empty the journal.
1679 * Filesystems can use this when remounting readonly to ensure that
1680 * recovery does not need to happen on remount.
1683 int jbd2_journal_flush(journal_t *journal)
1685 int err = 0;
1686 transaction_t *transaction = NULL;
1687 unsigned long old_tail;
1689 spin_lock(&journal->j_state_lock);
1691 /* Force everything buffered to the log... */
1692 if (journal->j_running_transaction) {
1693 transaction = journal->j_running_transaction;
1694 __jbd2_log_start_commit(journal, transaction->t_tid);
1695 } else if (journal->j_committing_transaction)
1696 transaction = journal->j_committing_transaction;
1698 /* Wait for the log commit to complete... */
1699 if (transaction) {
1700 tid_t tid = transaction->t_tid;
1702 spin_unlock(&journal->j_state_lock);
1703 jbd2_log_wait_commit(journal, tid);
1704 } else {
1705 spin_unlock(&journal->j_state_lock);
1708 /* ...and flush everything in the log out to disk. */
1709 spin_lock(&journal->j_list_lock);
1710 while (!err && journal->j_checkpoint_transactions != NULL) {
1711 spin_unlock(&journal->j_list_lock);
1712 mutex_lock(&journal->j_checkpoint_mutex);
1713 err = jbd2_log_do_checkpoint(journal);
1714 mutex_unlock(&journal->j_checkpoint_mutex);
1715 spin_lock(&journal->j_list_lock);
1717 spin_unlock(&journal->j_list_lock);
1719 if (is_journal_aborted(journal))
1720 return -EIO;
1722 jbd2_cleanup_journal_tail(journal);
1724 /* Finally, mark the journal as really needing no recovery.
1725 * This sets s_start==0 in the underlying superblock, which is
1726 * the magic code for a fully-recovered superblock. Any future
1727 * commits of data to the journal will restore the current
1728 * s_start value. */
1729 spin_lock(&journal->j_state_lock);
1730 old_tail = journal->j_tail;
1731 journal->j_tail = 0;
1732 spin_unlock(&journal->j_state_lock);
1733 jbd2_journal_update_superblock(journal, 1);
1734 spin_lock(&journal->j_state_lock);
1735 journal->j_tail = old_tail;
1737 J_ASSERT(!journal->j_running_transaction);
1738 J_ASSERT(!journal->j_committing_transaction);
1739 J_ASSERT(!journal->j_checkpoint_transactions);
1740 J_ASSERT(journal->j_head == journal->j_tail);
1741 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1742 spin_unlock(&journal->j_state_lock);
1743 return 0;
1747 * int jbd2_journal_wipe() - Wipe journal contents
1748 * @journal: Journal to act on.
1749 * @write: flag (see below)
1751 * Wipe out all of the contents of a journal, safely. This will produce
1752 * a warning if the journal contains any valid recovery information.
1753 * Must be called between journal_init_*() and jbd2_journal_load().
1755 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1756 * we merely suppress recovery.
1759 int jbd2_journal_wipe(journal_t *journal, int write)
1761 journal_superblock_t *sb;
1762 int err = 0;
1764 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
1766 err = load_superblock(journal);
1767 if (err)
1768 return err;
1770 sb = journal->j_superblock;
1772 if (!journal->j_tail)
1773 goto no_recovery;
1775 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1776 write ? "Clearing" : "Ignoring");
1778 err = jbd2_journal_skip_recovery(journal);
1779 if (write)
1780 jbd2_journal_update_superblock(journal, 1);
1782 no_recovery:
1783 return err;
1787 * Journal abort has very specific semantics, which we describe
1788 * for journal abort.
1790 * Two internal functions, which provide abort to the jbd layer
1791 * itself are here.
1795 * Quick version for internal journal use (doesn't lock the journal).
1796 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1797 * and don't attempt to make any other journal updates.
1799 void __jbd2_journal_abort_hard(journal_t *journal)
1801 transaction_t *transaction;
1803 if (journal->j_flags & JBD2_ABORT)
1804 return;
1806 printk(KERN_ERR "Aborting journal on device %s.\n",
1807 journal->j_devname);
1809 spin_lock(&journal->j_state_lock);
1810 journal->j_flags |= JBD2_ABORT;
1811 transaction = journal->j_running_transaction;
1812 if (transaction)
1813 __jbd2_log_start_commit(journal, transaction->t_tid);
1814 spin_unlock(&journal->j_state_lock);
1817 /* Soft abort: record the abort error status in the journal superblock,
1818 * but don't do any other IO. */
1819 static void __journal_abort_soft (journal_t *journal, int errno)
1821 if (journal->j_flags & JBD2_ABORT)
1822 return;
1824 if (!journal->j_errno)
1825 journal->j_errno = errno;
1827 __jbd2_journal_abort_hard(journal);
1829 if (errno)
1830 jbd2_journal_update_superblock(journal, 1);
1834 * void jbd2_journal_abort () - Shutdown the journal immediately.
1835 * @journal: the journal to shutdown.
1836 * @errno: an error number to record in the journal indicating
1837 * the reason for the shutdown.
1839 * Perform a complete, immediate shutdown of the ENTIRE
1840 * journal (not of a single transaction). This operation cannot be
1841 * undone without closing and reopening the journal.
1843 * The jbd2_journal_abort function is intended to support higher level error
1844 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1845 * mode.
1847 * Journal abort has very specific semantics. Any existing dirty,
1848 * unjournaled buffers in the main filesystem will still be written to
1849 * disk by bdflush, but the journaling mechanism will be suspended
1850 * immediately and no further transaction commits will be honoured.
1852 * Any dirty, journaled buffers will be written back to disk without
1853 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1854 * filesystem, but we _do_ attempt to leave as much data as possible
1855 * behind for fsck to use for cleanup.
1857 * Any attempt to get a new transaction handle on a journal which is in
1858 * ABORT state will just result in an -EROFS error return. A
1859 * jbd2_journal_stop on an existing handle will return -EIO if we have
1860 * entered abort state during the update.
1862 * Recursive transactions are not disturbed by journal abort until the
1863 * final jbd2_journal_stop, which will receive the -EIO error.
1865 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
1866 * which will be recorded (if possible) in the journal superblock. This
1867 * allows a client to record failure conditions in the middle of a
1868 * transaction without having to complete the transaction to record the
1869 * failure to disk. ext3_error, for example, now uses this
1870 * functionality.
1872 * Errors which originate from within the journaling layer will NOT
1873 * supply an errno; a null errno implies that absolutely no further
1874 * writes are done to the journal (unless there are any already in
1875 * progress).
1879 void jbd2_journal_abort(journal_t *journal, int errno)
1881 __journal_abort_soft(journal, errno);
1885 * int jbd2_journal_errno () - returns the journal's error state.
1886 * @journal: journal to examine.
1888 * This is the errno number set with jbd2_journal_abort(), the last
1889 * time the journal was mounted - if the journal was stopped
1890 * without calling abort this will be 0.
1892 * If the journal has been aborted on this mount time -EROFS will
1893 * be returned.
1895 int jbd2_journal_errno(journal_t *journal)
1897 int err;
1899 spin_lock(&journal->j_state_lock);
1900 if (journal->j_flags & JBD2_ABORT)
1901 err = -EROFS;
1902 else
1903 err = journal->j_errno;
1904 spin_unlock(&journal->j_state_lock);
1905 return err;
1909 * int jbd2_journal_clear_err () - clears the journal's error state
1910 * @journal: journal to act on.
1912 * An error must be cleared or acked to take a FS out of readonly
1913 * mode.
1915 int jbd2_journal_clear_err(journal_t *journal)
1917 int err = 0;
1919 spin_lock(&journal->j_state_lock);
1920 if (journal->j_flags & JBD2_ABORT)
1921 err = -EROFS;
1922 else
1923 journal->j_errno = 0;
1924 spin_unlock(&journal->j_state_lock);
1925 return err;
1929 * void jbd2_journal_ack_err() - Ack journal err.
1930 * @journal: journal to act on.
1932 * An error must be cleared or acked to take a FS out of readonly
1933 * mode.
1935 void jbd2_journal_ack_err(journal_t *journal)
1937 spin_lock(&journal->j_state_lock);
1938 if (journal->j_errno)
1939 journal->j_flags |= JBD2_ACK_ERR;
1940 spin_unlock(&journal->j_state_lock);
1943 int jbd2_journal_blocks_per_page(struct inode *inode)
1945 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1949 * helper functions to deal with 32 or 64bit block numbers.
1951 size_t journal_tag_bytes(journal_t *journal)
1953 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
1954 return JBD2_TAG_SIZE64;
1955 else
1956 return JBD2_TAG_SIZE32;
1960 * Journal_head storage management
1962 static struct kmem_cache *jbd2_journal_head_cache;
1963 #ifdef CONFIG_JBD2_DEBUG
1964 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1965 #endif
1967 static int journal_init_jbd2_journal_head_cache(void)
1969 int retval;
1971 J_ASSERT(jbd2_journal_head_cache == NULL);
1972 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
1973 sizeof(struct journal_head),
1974 0, /* offset */
1975 SLAB_TEMPORARY, /* flags */
1976 NULL); /* ctor */
1977 retval = 0;
1978 if (!jbd2_journal_head_cache) {
1979 retval = -ENOMEM;
1980 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1982 return retval;
1985 static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
1987 if (jbd2_journal_head_cache) {
1988 kmem_cache_destroy(jbd2_journal_head_cache);
1989 jbd2_journal_head_cache = NULL;
1994 * journal_head splicing and dicing
1996 static struct journal_head *journal_alloc_journal_head(void)
1998 struct journal_head *ret;
1999 static unsigned long last_warning;
2001 #ifdef CONFIG_JBD2_DEBUG
2002 atomic_inc(&nr_journal_heads);
2003 #endif
2004 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2005 if (!ret) {
2006 jbd_debug(1, "out of memory for journal_head\n");
2007 if (time_after(jiffies, last_warning + 5*HZ)) {
2008 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
2009 __func__);
2010 last_warning = jiffies;
2012 while (!ret) {
2013 yield();
2014 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
2017 return ret;
2020 static void journal_free_journal_head(struct journal_head *jh)
2022 #ifdef CONFIG_JBD2_DEBUG
2023 atomic_dec(&nr_journal_heads);
2024 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2025 #endif
2026 kmem_cache_free(jbd2_journal_head_cache, jh);
2030 * A journal_head is attached to a buffer_head whenever JBD has an
2031 * interest in the buffer.
2033 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2034 * is set. This bit is tested in core kernel code where we need to take
2035 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2036 * there.
2038 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2040 * When a buffer has its BH_JBD bit set it is immune from being released by
2041 * core kernel code, mainly via ->b_count.
2043 * A journal_head may be detached from its buffer_head when the journal_head's
2044 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
2045 * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
2046 * journal_head can be dropped if needed.
2048 * Various places in the kernel want to attach a journal_head to a buffer_head
2049 * _before_ attaching the journal_head to a transaction. To protect the
2050 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2051 * journal_head's b_jcount refcount by one. The caller must call
2052 * jbd2_journal_put_journal_head() to undo this.
2054 * So the typical usage would be:
2056 * (Attach a journal_head if needed. Increments b_jcount)
2057 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2058 * ...
2059 * jh->b_transaction = xxx;
2060 * jbd2_journal_put_journal_head(jh);
2062 * Now, the journal_head's b_jcount is zero, but it is safe from being released
2063 * because it has a non-zero b_transaction.
2067 * Give a buffer_head a journal_head.
2069 * Doesn't need the journal lock.
2070 * May sleep.
2072 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2074 struct journal_head *jh;
2075 struct journal_head *new_jh = NULL;
2077 repeat:
2078 if (!buffer_jbd(bh)) {
2079 new_jh = journal_alloc_journal_head();
2080 memset(new_jh, 0, sizeof(*new_jh));
2083 jbd_lock_bh_journal_head(bh);
2084 if (buffer_jbd(bh)) {
2085 jh = bh2jh(bh);
2086 } else {
2087 J_ASSERT_BH(bh,
2088 (atomic_read(&bh->b_count) > 0) ||
2089 (bh->b_page && bh->b_page->mapping));
2091 if (!new_jh) {
2092 jbd_unlock_bh_journal_head(bh);
2093 goto repeat;
2096 jh = new_jh;
2097 new_jh = NULL; /* We consumed it */
2098 set_buffer_jbd(bh);
2099 bh->b_private = jh;
2100 jh->b_bh = bh;
2101 get_bh(bh);
2102 BUFFER_TRACE(bh, "added journal_head");
2104 jh->b_jcount++;
2105 jbd_unlock_bh_journal_head(bh);
2106 if (new_jh)
2107 journal_free_journal_head(new_jh);
2108 return bh->b_private;
2112 * Grab a ref against this buffer_head's journal_head. If it ended up not
2113 * having a journal_head, return NULL
2115 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2117 struct journal_head *jh = NULL;
2119 jbd_lock_bh_journal_head(bh);
2120 if (buffer_jbd(bh)) {
2121 jh = bh2jh(bh);
2122 jh->b_jcount++;
2124 jbd_unlock_bh_journal_head(bh);
2125 return jh;
2128 static void __journal_remove_journal_head(struct buffer_head *bh)
2130 struct journal_head *jh = bh2jh(bh);
2132 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2134 get_bh(bh);
2135 if (jh->b_jcount == 0) {
2136 if (jh->b_transaction == NULL &&
2137 jh->b_next_transaction == NULL &&
2138 jh->b_cp_transaction == NULL) {
2139 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2140 J_ASSERT_BH(bh, buffer_jbd(bh));
2141 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2142 BUFFER_TRACE(bh, "remove journal_head");
2143 if (jh->b_frozen_data) {
2144 printk(KERN_WARNING "%s: freeing "
2145 "b_frozen_data\n",
2146 __func__);
2147 jbd2_free(jh->b_frozen_data, bh->b_size);
2149 if (jh->b_committed_data) {
2150 printk(KERN_WARNING "%s: freeing "
2151 "b_committed_data\n",
2152 __func__);
2153 jbd2_free(jh->b_committed_data, bh->b_size);
2155 bh->b_private = NULL;
2156 jh->b_bh = NULL; /* debug, really */
2157 clear_buffer_jbd(bh);
2158 __brelse(bh);
2159 journal_free_journal_head(jh);
2160 } else {
2161 BUFFER_TRACE(bh, "journal_head was locked");
2167 * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
2168 * and has a zero b_jcount then remove and release its journal_head. If we did
2169 * see that the buffer is not used by any transaction we also "logically"
2170 * decrement ->b_count.
2172 * We in fact take an additional increment on ->b_count as a convenience,
2173 * because the caller usually wants to do additional things with the bh
2174 * after calling here.
2175 * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
2176 * time. Once the caller has run __brelse(), the buffer is eligible for
2177 * reaping by try_to_free_buffers().
2179 void jbd2_journal_remove_journal_head(struct buffer_head *bh)
2181 jbd_lock_bh_journal_head(bh);
2182 __journal_remove_journal_head(bh);
2183 jbd_unlock_bh_journal_head(bh);
2187 * Drop a reference on the passed journal_head. If it fell to zero then try to
2188 * release the journal_head from the buffer_head.
2190 void jbd2_journal_put_journal_head(struct journal_head *jh)
2192 struct buffer_head *bh = jh2bh(jh);
2194 jbd_lock_bh_journal_head(bh);
2195 J_ASSERT_JH(jh, jh->b_jcount > 0);
2196 --jh->b_jcount;
2197 if (!jh->b_jcount && !jh->b_transaction) {
2198 __journal_remove_journal_head(bh);
2199 __brelse(bh);
2201 jbd_unlock_bh_journal_head(bh);
2205 * Initialize jbd inode head
2207 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2209 jinode->i_transaction = NULL;
2210 jinode->i_next_transaction = NULL;
2211 jinode->i_vfs_inode = inode;
2212 jinode->i_flags = 0;
2213 INIT_LIST_HEAD(&jinode->i_list);
2217 * Function to be called before we start removing inode from memory (i.e.,
2218 * clear_inode() is a fine place to be called from). It removes inode from
2219 * transaction's lists.
2221 void jbd2_journal_release_jbd_inode(journal_t *journal,
2222 struct jbd2_inode *jinode)
2224 int writeout = 0;
2226 if (!journal)
2227 return;
2228 restart:
2229 spin_lock(&journal->j_list_lock);
2230 /* Is commit writing out inode - we have to wait */
2231 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2232 wait_queue_head_t *wq;
2233 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2234 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2235 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2236 spin_unlock(&journal->j_list_lock);
2237 schedule();
2238 finish_wait(wq, &wait.wait);
2239 goto restart;
2242 /* Do we need to wait for data writeback? */
2243 if (journal->j_committing_transaction == jinode->i_transaction)
2244 writeout = 1;
2245 if (jinode->i_transaction) {
2246 list_del(&jinode->i_list);
2247 jinode->i_transaction = NULL;
2249 spin_unlock(&journal->j_list_lock);
2253 * debugfs tunables
2255 #ifdef CONFIG_JBD2_DEBUG
2256 u8 jbd2_journal_enable_debug __read_mostly;
2257 EXPORT_SYMBOL(jbd2_journal_enable_debug);
2259 #define JBD2_DEBUG_NAME "jbd2-debug"
2261 static struct dentry *jbd2_debugfs_dir;
2262 static struct dentry *jbd2_debug;
2264 static void __init jbd2_create_debugfs_entry(void)
2266 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2267 if (jbd2_debugfs_dir)
2268 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
2269 jbd2_debugfs_dir,
2270 &jbd2_journal_enable_debug);
2273 static void __exit jbd2_remove_debugfs_entry(void)
2275 debugfs_remove(jbd2_debug);
2276 debugfs_remove(jbd2_debugfs_dir);
2279 #else
2281 static void __init jbd2_create_debugfs_entry(void)
2285 static void __exit jbd2_remove_debugfs_entry(void)
2289 #endif
2291 #ifdef CONFIG_PROC_FS
2293 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2295 static void __init jbd2_create_jbd_stats_proc_entry(void)
2297 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2300 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2302 if (proc_jbd2_stats)
2303 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2306 #else
2308 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2309 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2311 #endif
2313 struct kmem_cache *jbd2_handle_cache;
2315 static int __init journal_init_handle_cache(void)
2317 jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
2318 sizeof(handle_t),
2319 0, /* offset */
2320 SLAB_TEMPORARY, /* flags */
2321 NULL); /* ctor */
2322 if (jbd2_handle_cache == NULL) {
2323 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2324 return -ENOMEM;
2326 return 0;
2329 static void jbd2_journal_destroy_handle_cache(void)
2331 if (jbd2_handle_cache)
2332 kmem_cache_destroy(jbd2_handle_cache);
2336 * Module startup and shutdown
2339 static int __init journal_init_caches(void)
2341 int ret;
2343 ret = jbd2_journal_init_revoke_caches();
2344 if (ret == 0)
2345 ret = journal_init_jbd2_journal_head_cache();
2346 if (ret == 0)
2347 ret = journal_init_handle_cache();
2348 return ret;
2351 static void jbd2_journal_destroy_caches(void)
2353 jbd2_journal_destroy_revoke_caches();
2354 jbd2_journal_destroy_jbd2_journal_head_cache();
2355 jbd2_journal_destroy_handle_cache();
2358 static int __init journal_init(void)
2360 int ret;
2362 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2364 ret = journal_init_caches();
2365 if (ret == 0) {
2366 jbd2_create_debugfs_entry();
2367 jbd2_create_jbd_stats_proc_entry();
2368 } else {
2369 jbd2_journal_destroy_caches();
2371 return ret;
2374 static void __exit journal_exit(void)
2376 #ifdef CONFIG_JBD2_DEBUG
2377 int n = atomic_read(&nr_journal_heads);
2378 if (n)
2379 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2380 #endif
2381 jbd2_remove_debugfs_entry();
2382 jbd2_remove_jbd_stats_proc_entry();
2383 jbd2_journal_destroy_caches();
2387 * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
2388 * tracing infrastructure to map a dev_t to a device name.
2390 * The caller should use rcu_read_lock() in order to make sure the
2391 * device name stays valid until its done with it. We use
2392 * rcu_read_lock() as well to make sure we're safe in case the caller
2393 * gets sloppy, and because rcu_read_lock() is cheap and can be safely
2394 * nested.
2396 struct devname_cache {
2397 struct rcu_head rcu;
2398 dev_t device;
2399 char devname[BDEVNAME_SIZE];
2401 #define CACHE_SIZE_BITS 6
2402 static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
2403 static DEFINE_SPINLOCK(devname_cache_lock);
2405 static void free_devcache(struct rcu_head *rcu)
2407 kfree(rcu);
2410 const char *jbd2_dev_to_name(dev_t device)
2412 int i = hash_32(device, CACHE_SIZE_BITS);
2413 char *ret;
2414 struct block_device *bd;
2415 static struct devname_cache *new_dev;
2417 rcu_read_lock();
2418 if (devcache[i] && devcache[i]->device == device) {
2419 ret = devcache[i]->devname;
2420 rcu_read_unlock();
2421 return ret;
2423 rcu_read_unlock();
2425 new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
2426 if (!new_dev)
2427 return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
2428 spin_lock(&devname_cache_lock);
2429 if (devcache[i]) {
2430 if (devcache[i]->device == device) {
2431 kfree(new_dev);
2432 ret = devcache[i]->devname;
2433 spin_unlock(&devname_cache_lock);
2434 return ret;
2436 call_rcu(&devcache[i]->rcu, free_devcache);
2438 devcache[i] = new_dev;
2439 devcache[i]->device = device;
2440 bd = bdget(device);
2441 if (bd) {
2442 bdevname(bd, devcache[i]->devname);
2443 bdput(bd);
2444 } else
2445 __bdevname(device, devcache[i]->devname);
2446 ret = devcache[i]->devname;
2447 spin_unlock(&devname_cache_lock);
2448 return ret;
2450 EXPORT_SYMBOL(jbd2_dev_to_name);
2452 MODULE_LICENSE("GPL");
2453 module_init(journal_init);
2454 module_exit(journal_exit);