Linux 4.14.215
[linux/fpc-iii.git] / fs / jbd2 / journal.c
blob93a466cf58ba778ac9fb811c12775a074c03c783
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/seq_file.h>
39 #include <linux/math64.h>
40 #include <linux/hash.h>
41 #include <linux/log2.h>
42 #include <linux/vmalloc.h>
43 #include <linux/backing-dev.h>
44 #include <linux/bitops.h>
45 #include <linux/ratelimit.h>
46 #include <linux/sched/mm.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/jbd2.h>
51 #include <linux/uaccess.h>
52 #include <asm/page.h>
54 #ifdef CONFIG_JBD2_DEBUG
55 ushort jbd2_journal_enable_debug __read_mostly;
56 EXPORT_SYMBOL(jbd2_journal_enable_debug);
58 module_param_named(jbd2_debug, jbd2_journal_enable_debug, ushort, 0644);
59 MODULE_PARM_DESC(jbd2_debug, "Debugging level for jbd2");
60 #endif
62 EXPORT_SYMBOL(jbd2_journal_extend);
63 EXPORT_SYMBOL(jbd2_journal_stop);
64 EXPORT_SYMBOL(jbd2_journal_lock_updates);
65 EXPORT_SYMBOL(jbd2_journal_unlock_updates);
66 EXPORT_SYMBOL(jbd2_journal_get_write_access);
67 EXPORT_SYMBOL(jbd2_journal_get_create_access);
68 EXPORT_SYMBOL(jbd2_journal_get_undo_access);
69 EXPORT_SYMBOL(jbd2_journal_set_triggers);
70 EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
71 EXPORT_SYMBOL(jbd2_journal_forget);
72 #if 0
73 EXPORT_SYMBOL(journal_sync_buffer);
74 #endif
75 EXPORT_SYMBOL(jbd2_journal_flush);
76 EXPORT_SYMBOL(jbd2_journal_revoke);
78 EXPORT_SYMBOL(jbd2_journal_init_dev);
79 EXPORT_SYMBOL(jbd2_journal_init_inode);
80 EXPORT_SYMBOL(jbd2_journal_check_used_features);
81 EXPORT_SYMBOL(jbd2_journal_check_available_features);
82 EXPORT_SYMBOL(jbd2_journal_set_features);
83 EXPORT_SYMBOL(jbd2_journal_load);
84 EXPORT_SYMBOL(jbd2_journal_destroy);
85 EXPORT_SYMBOL(jbd2_journal_abort);
86 EXPORT_SYMBOL(jbd2_journal_errno);
87 EXPORT_SYMBOL(jbd2_journal_ack_err);
88 EXPORT_SYMBOL(jbd2_journal_clear_err);
89 EXPORT_SYMBOL(jbd2_log_wait_commit);
90 EXPORT_SYMBOL(jbd2_log_start_commit);
91 EXPORT_SYMBOL(jbd2_journal_start_commit);
92 EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
93 EXPORT_SYMBOL(jbd2_journal_wipe);
94 EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
95 EXPORT_SYMBOL(jbd2_journal_invalidatepage);
96 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
97 EXPORT_SYMBOL(jbd2_journal_force_commit);
98 EXPORT_SYMBOL(jbd2_journal_inode_add_write);
99 EXPORT_SYMBOL(jbd2_journal_inode_add_wait);
100 EXPORT_SYMBOL(jbd2_journal_inode_ranged_write);
101 EXPORT_SYMBOL(jbd2_journal_inode_ranged_wait);
102 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
103 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
104 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
105 EXPORT_SYMBOL(jbd2_inode_cache);
107 static void __journal_abort_soft (journal_t *journal, int errno);
108 static int jbd2_journal_create_slab(size_t slab_size);
110 #ifdef CONFIG_JBD2_DEBUG
111 void __jbd2_debug(int level, const char *file, const char *func,
112 unsigned int line, const char *fmt, ...)
114 struct va_format vaf;
115 va_list args;
117 if (level > jbd2_journal_enable_debug)
118 return;
119 va_start(args, fmt);
120 vaf.fmt = fmt;
121 vaf.va = &args;
122 printk(KERN_DEBUG "%s: (%s, %u): %pV\n", file, func, line, &vaf);
123 va_end(args);
125 EXPORT_SYMBOL(__jbd2_debug);
126 #endif
128 /* Checksumming functions */
129 static int jbd2_verify_csum_type(journal_t *j, journal_superblock_t *sb)
131 if (!jbd2_journal_has_csum_v2or3_feature(j))
132 return 1;
134 return sb->s_checksum_type == JBD2_CRC32C_CHKSUM;
137 static __be32 jbd2_superblock_csum(journal_t *j, journal_superblock_t *sb)
139 __u32 csum;
140 __be32 old_csum;
142 old_csum = sb->s_checksum;
143 sb->s_checksum = 0;
144 csum = jbd2_chksum(j, ~0, (char *)sb, sizeof(journal_superblock_t));
145 sb->s_checksum = old_csum;
147 return cpu_to_be32(csum);
150 static int jbd2_superblock_csum_verify(journal_t *j, journal_superblock_t *sb)
152 if (!jbd2_journal_has_csum_v2or3(j))
153 return 1;
155 return sb->s_checksum == jbd2_superblock_csum(j, sb);
158 static void jbd2_superblock_csum_set(journal_t *j, journal_superblock_t *sb)
160 if (!jbd2_journal_has_csum_v2or3(j))
161 return;
163 sb->s_checksum = jbd2_superblock_csum(j, sb);
167 * Helper function used to manage commit timeouts
170 static void commit_timeout(unsigned long __data)
172 struct task_struct * p = (struct task_struct *) __data;
174 wake_up_process(p);
178 * kjournald2: The main thread function used to manage a logging device
179 * journal.
181 * This kernel thread is responsible for two things:
183 * 1) COMMIT: Every so often we need to commit the current state of the
184 * filesystem to disk. The journal thread is responsible for writing
185 * all of the metadata buffers to disk.
187 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
188 * of the data in that part of the log has been rewritten elsewhere on
189 * the disk. Flushing these old buffers to reclaim space in the log is
190 * known as checkpointing, and this thread is responsible for that job.
193 static int kjournald2(void *arg)
195 journal_t *journal = arg;
196 transaction_t *transaction;
199 * Set up an interval timer which can be used to trigger a commit wakeup
200 * after the commit interval expires
202 setup_timer(&journal->j_commit_timer, commit_timeout,
203 (unsigned long)current);
205 set_freezable();
207 /* Record that the journal thread is running */
208 journal->j_task = current;
209 wake_up(&journal->j_wait_done_commit);
212 * Make sure that no allocations from this kernel thread will ever
213 * recurse to the fs layer because we are responsible for the
214 * transaction commit and any fs involvement might get stuck waiting for
215 * the trasn. commit.
217 memalloc_nofs_save();
220 * And now, wait forever for commit wakeup events.
222 write_lock(&journal->j_state_lock);
224 loop:
225 if (journal->j_flags & JBD2_UNMOUNT)
226 goto end_loop;
228 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
229 journal->j_commit_sequence, journal->j_commit_request);
231 if (journal->j_commit_sequence != journal->j_commit_request) {
232 jbd_debug(1, "OK, requests differ\n");
233 write_unlock(&journal->j_state_lock);
234 del_timer_sync(&journal->j_commit_timer);
235 jbd2_journal_commit_transaction(journal);
236 write_lock(&journal->j_state_lock);
237 goto loop;
240 wake_up(&journal->j_wait_done_commit);
241 if (freezing(current)) {
243 * The simpler the better. Flushing journal isn't a
244 * good idea, because that depends on threads that may
245 * be already stopped.
247 jbd_debug(1, "Now suspending kjournald2\n");
248 write_unlock(&journal->j_state_lock);
249 try_to_freeze();
250 write_lock(&journal->j_state_lock);
251 } else {
253 * We assume on resume that commits are already there,
254 * so we don't sleep
256 DEFINE_WAIT(wait);
257 int should_sleep = 1;
259 prepare_to_wait(&journal->j_wait_commit, &wait,
260 TASK_INTERRUPTIBLE);
261 if (journal->j_commit_sequence != journal->j_commit_request)
262 should_sleep = 0;
263 transaction = journal->j_running_transaction;
264 if (transaction && time_after_eq(jiffies,
265 transaction->t_expires))
266 should_sleep = 0;
267 if (journal->j_flags & JBD2_UNMOUNT)
268 should_sleep = 0;
269 if (should_sleep) {
270 write_unlock(&journal->j_state_lock);
271 schedule();
272 write_lock(&journal->j_state_lock);
274 finish_wait(&journal->j_wait_commit, &wait);
277 jbd_debug(1, "kjournald2 wakes\n");
280 * Were we woken up by a commit wakeup event?
282 transaction = journal->j_running_transaction;
283 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
284 journal->j_commit_request = transaction->t_tid;
285 jbd_debug(1, "woke because of timeout\n");
287 goto loop;
289 end_loop:
290 del_timer_sync(&journal->j_commit_timer);
291 journal->j_task = NULL;
292 wake_up(&journal->j_wait_done_commit);
293 jbd_debug(1, "Journal thread exiting.\n");
294 write_unlock(&journal->j_state_lock);
295 return 0;
298 static int jbd2_journal_start_thread(journal_t *journal)
300 struct task_struct *t;
302 t = kthread_run(kjournald2, journal, "jbd2/%s",
303 journal->j_devname);
304 if (IS_ERR(t))
305 return PTR_ERR(t);
307 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
308 return 0;
311 static void journal_kill_thread(journal_t *journal)
313 write_lock(&journal->j_state_lock);
314 journal->j_flags |= JBD2_UNMOUNT;
316 while (journal->j_task) {
317 write_unlock(&journal->j_state_lock);
318 wake_up(&journal->j_wait_commit);
319 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
320 write_lock(&journal->j_state_lock);
322 write_unlock(&journal->j_state_lock);
326 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
328 * Writes a metadata buffer to a given disk block. The actual IO is not
329 * performed but a new buffer_head is constructed which labels the data
330 * to be written with the correct destination disk block.
332 * Any magic-number escaping which needs to be done will cause a
333 * copy-out here. If the buffer happens to start with the
334 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
335 * magic number is only written to the log for descripter blocks. In
336 * this case, we copy the data and replace the first word with 0, and we
337 * return a result code which indicates that this buffer needs to be
338 * marked as an escaped buffer in the corresponding log descriptor
339 * block. The missing word can then be restored when the block is read
340 * during recovery.
342 * If the source buffer has already been modified by a new transaction
343 * since we took the last commit snapshot, we use the frozen copy of
344 * that data for IO. If we end up using the existing buffer_head's data
345 * for the write, then we have to make sure nobody modifies it while the
346 * IO is in progress. do_get_write_access() handles this.
348 * The function returns a pointer to the buffer_head to be used for IO.
351 * Return value:
352 * <0: Error
353 * >=0: Finished OK
355 * On success:
356 * Bit 0 set == escape performed on the data
357 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
360 int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
361 struct journal_head *jh_in,
362 struct buffer_head **bh_out,
363 sector_t blocknr)
365 int need_copy_out = 0;
366 int done_copy_out = 0;
367 int do_escape = 0;
368 char *mapped_data;
369 struct buffer_head *new_bh;
370 struct page *new_page;
371 unsigned int new_offset;
372 struct buffer_head *bh_in = jh2bh(jh_in);
373 journal_t *journal = transaction->t_journal;
376 * The buffer really shouldn't be locked: only the current committing
377 * transaction is allowed to write it, so nobody else is allowed
378 * to do any IO.
380 * akpm: except if we're journalling data, and write() output is
381 * also part of a shared mapping, and another thread has
382 * decided to launch a writepage() against this buffer.
384 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
386 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
388 /* keep subsequent assertions sane */
389 atomic_set(&new_bh->b_count, 1);
391 jbd_lock_bh_state(bh_in);
392 repeat:
394 * If a new transaction has already done a buffer copy-out, then
395 * we use that version of the data for the commit.
397 if (jh_in->b_frozen_data) {
398 done_copy_out = 1;
399 new_page = virt_to_page(jh_in->b_frozen_data);
400 new_offset = offset_in_page(jh_in->b_frozen_data);
401 } else {
402 new_page = jh2bh(jh_in)->b_page;
403 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
406 mapped_data = kmap_atomic(new_page);
408 * Fire data frozen trigger if data already wasn't frozen. Do this
409 * before checking for escaping, as the trigger may modify the magic
410 * offset. If a copy-out happens afterwards, it will have the correct
411 * data in the buffer.
413 if (!done_copy_out)
414 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
415 jh_in->b_triggers);
418 * Check for escaping
420 if (*((__be32 *)(mapped_data + new_offset)) ==
421 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
422 need_copy_out = 1;
423 do_escape = 1;
425 kunmap_atomic(mapped_data);
428 * Do we need to do a data copy?
430 if (need_copy_out && !done_copy_out) {
431 char *tmp;
433 jbd_unlock_bh_state(bh_in);
434 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
435 if (!tmp) {
436 brelse(new_bh);
437 return -ENOMEM;
439 jbd_lock_bh_state(bh_in);
440 if (jh_in->b_frozen_data) {
441 jbd2_free(tmp, bh_in->b_size);
442 goto repeat;
445 jh_in->b_frozen_data = tmp;
446 mapped_data = kmap_atomic(new_page);
447 memcpy(tmp, mapped_data + new_offset, bh_in->b_size);
448 kunmap_atomic(mapped_data);
450 new_page = virt_to_page(tmp);
451 new_offset = offset_in_page(tmp);
452 done_copy_out = 1;
455 * This isn't strictly necessary, as we're using frozen
456 * data for the escaping, but it keeps consistency with
457 * b_frozen_data usage.
459 jh_in->b_frozen_triggers = jh_in->b_triggers;
463 * Did we need to do an escaping? Now we've done all the
464 * copying, we can finally do so.
466 if (do_escape) {
467 mapped_data = kmap_atomic(new_page);
468 *((unsigned int *)(mapped_data + new_offset)) = 0;
469 kunmap_atomic(mapped_data);
472 set_bh_page(new_bh, new_page, new_offset);
473 new_bh->b_size = bh_in->b_size;
474 new_bh->b_bdev = journal->j_dev;
475 new_bh->b_blocknr = blocknr;
476 new_bh->b_private = bh_in;
477 set_buffer_mapped(new_bh);
478 set_buffer_dirty(new_bh);
480 *bh_out = new_bh;
483 * The to-be-written buffer needs to get moved to the io queue,
484 * and the original buffer whose contents we are shadowing or
485 * copying is moved to the transaction's shadow queue.
487 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
488 spin_lock(&journal->j_list_lock);
489 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
490 spin_unlock(&journal->j_list_lock);
491 set_buffer_shadow(bh_in);
492 jbd_unlock_bh_state(bh_in);
494 return do_escape | (done_copy_out << 1);
498 * Allocation code for the journal file. Manage the space left in the
499 * journal, so that we can begin checkpointing when appropriate.
503 * Called with j_state_lock locked for writing.
504 * Returns true if a transaction commit was started.
506 int __jbd2_log_start_commit(journal_t *journal, tid_t target)
508 /* Return if the txn has already requested to be committed */
509 if (journal->j_commit_request == target)
510 return 0;
513 * The only transaction we can possibly wait upon is the
514 * currently running transaction (if it exists). Otherwise,
515 * the target tid must be an old one.
517 if (journal->j_running_transaction &&
518 journal->j_running_transaction->t_tid == target) {
520 * We want a new commit: OK, mark the request and wakeup the
521 * commit thread. We do _not_ do the commit ourselves.
524 journal->j_commit_request = target;
525 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
526 journal->j_commit_request,
527 journal->j_commit_sequence);
528 journal->j_running_transaction->t_requested = jiffies;
529 wake_up(&journal->j_wait_commit);
530 return 1;
531 } else if (!tid_geq(journal->j_commit_request, target))
532 /* This should never happen, but if it does, preserve
533 the evidence before kjournald goes into a loop and
534 increments j_commit_sequence beyond all recognition. */
535 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
536 journal->j_commit_request,
537 journal->j_commit_sequence,
538 target, journal->j_running_transaction ?
539 journal->j_running_transaction->t_tid : 0);
540 return 0;
543 int jbd2_log_start_commit(journal_t *journal, tid_t tid)
545 int ret;
547 write_lock(&journal->j_state_lock);
548 ret = __jbd2_log_start_commit(journal, tid);
549 write_unlock(&journal->j_state_lock);
550 return ret;
554 * Force and wait any uncommitted transactions. We can only force the running
555 * transaction if we don't have an active handle, otherwise, we will deadlock.
556 * Returns: <0 in case of error,
557 * 0 if nothing to commit,
558 * 1 if transaction was successfully committed.
560 static int __jbd2_journal_force_commit(journal_t *journal)
562 transaction_t *transaction = NULL;
563 tid_t tid;
564 int need_to_start = 0, ret = 0;
566 read_lock(&journal->j_state_lock);
567 if (journal->j_running_transaction && !current->journal_info) {
568 transaction = journal->j_running_transaction;
569 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
570 need_to_start = 1;
571 } else if (journal->j_committing_transaction)
572 transaction = journal->j_committing_transaction;
574 if (!transaction) {
575 /* Nothing to commit */
576 read_unlock(&journal->j_state_lock);
577 return 0;
579 tid = transaction->t_tid;
580 read_unlock(&journal->j_state_lock);
581 if (need_to_start)
582 jbd2_log_start_commit(journal, tid);
583 ret = jbd2_log_wait_commit(journal, tid);
584 if (!ret)
585 ret = 1;
587 return ret;
591 * Force and wait upon a commit if the calling process is not within
592 * transaction. This is used for forcing out undo-protected data which contains
593 * bitmaps, when the fs is running out of space.
595 * @journal: journal to force
596 * Returns true if progress was made.
598 int jbd2_journal_force_commit_nested(journal_t *journal)
600 int ret;
602 ret = __jbd2_journal_force_commit(journal);
603 return ret > 0;
607 * int journal_force_commit() - force any uncommitted transactions
608 * @journal: journal to force
610 * Caller want unconditional commit. We can only force the running transaction
611 * if we don't have an active handle, otherwise, we will deadlock.
613 int jbd2_journal_force_commit(journal_t *journal)
615 int ret;
617 J_ASSERT(!current->journal_info);
618 ret = __jbd2_journal_force_commit(journal);
619 if (ret > 0)
620 ret = 0;
621 return ret;
625 * Start a commit of the current running transaction (if any). Returns true
626 * if a transaction is going to be committed (or is currently already
627 * committing), and fills its tid in at *ptid
629 int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
631 int ret = 0;
633 write_lock(&journal->j_state_lock);
634 if (journal->j_running_transaction) {
635 tid_t tid = journal->j_running_transaction->t_tid;
637 __jbd2_log_start_commit(journal, tid);
638 /* There's a running transaction and we've just made sure
639 * it's commit has been scheduled. */
640 if (ptid)
641 *ptid = tid;
642 ret = 1;
643 } else if (journal->j_committing_transaction) {
645 * If commit has been started, then we have to wait for
646 * completion of that transaction.
648 if (ptid)
649 *ptid = journal->j_committing_transaction->t_tid;
650 ret = 1;
652 write_unlock(&journal->j_state_lock);
653 return ret;
657 * Return 1 if a given transaction has not yet sent barrier request
658 * connected with a transaction commit. If 0 is returned, transaction
659 * may or may not have sent the barrier. Used to avoid sending barrier
660 * twice in common cases.
662 int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
664 int ret = 0;
665 transaction_t *commit_trans;
667 if (!(journal->j_flags & JBD2_BARRIER))
668 return 0;
669 read_lock(&journal->j_state_lock);
670 /* Transaction already committed? */
671 if (tid_geq(journal->j_commit_sequence, tid))
672 goto out;
673 commit_trans = journal->j_committing_transaction;
674 if (!commit_trans || commit_trans->t_tid != tid) {
675 ret = 1;
676 goto out;
679 * Transaction is being committed and we already proceeded to
680 * submitting a flush to fs partition?
682 if (journal->j_fs_dev != journal->j_dev) {
683 if (!commit_trans->t_need_data_flush ||
684 commit_trans->t_state >= T_COMMIT_DFLUSH)
685 goto out;
686 } else {
687 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
688 goto out;
690 ret = 1;
691 out:
692 read_unlock(&journal->j_state_lock);
693 return ret;
695 EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
698 * Wait for a specified commit to complete.
699 * The caller may not hold the journal lock.
701 int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
703 int err = 0;
705 read_lock(&journal->j_state_lock);
706 #ifdef CONFIG_PROVE_LOCKING
708 * Some callers make sure transaction is already committing and in that
709 * case we cannot block on open handles anymore. So don't warn in that
710 * case.
712 if (tid_gt(tid, journal->j_commit_sequence) &&
713 (!journal->j_committing_transaction ||
714 journal->j_committing_transaction->t_tid != tid)) {
715 read_unlock(&journal->j_state_lock);
716 jbd2_might_wait_for_commit(journal);
717 read_lock(&journal->j_state_lock);
719 #endif
720 #ifdef CONFIG_JBD2_DEBUG
721 if (!tid_geq(journal->j_commit_request, tid)) {
722 printk(KERN_ERR
723 "%s: error: j_commit_request=%d, tid=%d\n",
724 __func__, journal->j_commit_request, tid);
726 #endif
727 while (tid_gt(tid, journal->j_commit_sequence)) {
728 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
729 tid, journal->j_commit_sequence);
730 read_unlock(&journal->j_state_lock);
731 wake_up(&journal->j_wait_commit);
732 wait_event(journal->j_wait_done_commit,
733 !tid_gt(tid, journal->j_commit_sequence));
734 read_lock(&journal->j_state_lock);
736 read_unlock(&journal->j_state_lock);
738 if (unlikely(is_journal_aborted(journal)))
739 err = -EIO;
740 return err;
744 * When this function returns the transaction corresponding to tid
745 * will be completed. If the transaction has currently running, start
746 * committing that transaction before waiting for it to complete. If
747 * the transaction id is stale, it is by definition already completed,
748 * so just return SUCCESS.
750 int jbd2_complete_transaction(journal_t *journal, tid_t tid)
752 int need_to_wait = 1;
754 read_lock(&journal->j_state_lock);
755 if (journal->j_running_transaction &&
756 journal->j_running_transaction->t_tid == tid) {
757 if (journal->j_commit_request != tid) {
758 /* transaction not yet started, so request it */
759 read_unlock(&journal->j_state_lock);
760 jbd2_log_start_commit(journal, tid);
761 goto wait_commit;
763 } else if (!(journal->j_committing_transaction &&
764 journal->j_committing_transaction->t_tid == tid))
765 need_to_wait = 0;
766 read_unlock(&journal->j_state_lock);
767 if (!need_to_wait)
768 return 0;
769 wait_commit:
770 return jbd2_log_wait_commit(journal, tid);
772 EXPORT_SYMBOL(jbd2_complete_transaction);
775 * Log buffer allocation routines:
778 int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
780 unsigned long blocknr;
782 write_lock(&journal->j_state_lock);
783 J_ASSERT(journal->j_free > 1);
785 blocknr = journal->j_head;
786 journal->j_head++;
787 journal->j_free--;
788 if (journal->j_head == journal->j_last)
789 journal->j_head = journal->j_first;
790 write_unlock(&journal->j_state_lock);
791 return jbd2_journal_bmap(journal, blocknr, retp);
795 * Conversion of logical to physical block numbers for the journal
797 * On external journals the journal blocks are identity-mapped, so
798 * this is a no-op. If needed, we can use j_blk_offset - everything is
799 * ready.
801 int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
802 unsigned long long *retp)
804 int err = 0;
805 unsigned long long ret;
807 if (journal->j_inode) {
808 ret = bmap(journal->j_inode, blocknr);
809 if (ret)
810 *retp = ret;
811 else {
812 printk(KERN_ALERT "%s: journal block not found "
813 "at offset %lu on %s\n",
814 __func__, blocknr, journal->j_devname);
815 err = -EIO;
816 __journal_abort_soft(journal, err);
818 } else {
819 *retp = blocknr; /* +journal->j_blk_offset */
821 return err;
825 * We play buffer_head aliasing tricks to write data/metadata blocks to
826 * the journal without copying their contents, but for journal
827 * descriptor blocks we do need to generate bona fide buffers.
829 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
830 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
831 * But we don't bother doing that, so there will be coherency problems with
832 * mmaps of blockdevs which hold live JBD-controlled filesystems.
834 struct buffer_head *
835 jbd2_journal_get_descriptor_buffer(transaction_t *transaction, int type)
837 journal_t *journal = transaction->t_journal;
838 struct buffer_head *bh;
839 unsigned long long blocknr;
840 journal_header_t *header;
841 int err;
843 err = jbd2_journal_next_log_block(journal, &blocknr);
845 if (err)
846 return NULL;
848 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
849 if (!bh)
850 return NULL;
851 lock_buffer(bh);
852 memset(bh->b_data, 0, journal->j_blocksize);
853 header = (journal_header_t *)bh->b_data;
854 header->h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
855 header->h_blocktype = cpu_to_be32(type);
856 header->h_sequence = cpu_to_be32(transaction->t_tid);
857 set_buffer_uptodate(bh);
858 unlock_buffer(bh);
859 BUFFER_TRACE(bh, "return this buffer");
860 return bh;
863 void jbd2_descriptor_block_csum_set(journal_t *j, struct buffer_head *bh)
865 struct jbd2_journal_block_tail *tail;
866 __u32 csum;
868 if (!jbd2_journal_has_csum_v2or3(j))
869 return;
871 tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize -
872 sizeof(struct jbd2_journal_block_tail));
873 tail->t_checksum = 0;
874 csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
875 tail->t_checksum = cpu_to_be32(csum);
879 * Return tid of the oldest transaction in the journal and block in the journal
880 * where the transaction starts.
882 * If the journal is now empty, return which will be the next transaction ID
883 * we will write and where will that transaction start.
885 * The return value is 0 if journal tail cannot be pushed any further, 1 if
886 * it can.
888 int jbd2_journal_get_log_tail(journal_t *journal, tid_t *tid,
889 unsigned long *block)
891 transaction_t *transaction;
892 int ret;
894 read_lock(&journal->j_state_lock);
895 spin_lock(&journal->j_list_lock);
896 transaction = journal->j_checkpoint_transactions;
897 if (transaction) {
898 *tid = transaction->t_tid;
899 *block = transaction->t_log_start;
900 } else if ((transaction = journal->j_committing_transaction) != NULL) {
901 *tid = transaction->t_tid;
902 *block = transaction->t_log_start;
903 } else if ((transaction = journal->j_running_transaction) != NULL) {
904 *tid = transaction->t_tid;
905 *block = journal->j_head;
906 } else {
907 *tid = journal->j_transaction_sequence;
908 *block = journal->j_head;
910 ret = tid_gt(*tid, journal->j_tail_sequence);
911 spin_unlock(&journal->j_list_lock);
912 read_unlock(&journal->j_state_lock);
914 return ret;
918 * Update information in journal structure and in on disk journal superblock
919 * about log tail. This function does not check whether information passed in
920 * really pushes log tail further. It's responsibility of the caller to make
921 * sure provided log tail information is valid (e.g. by holding
922 * j_checkpoint_mutex all the time between computing log tail and calling this
923 * function as is the case with jbd2_cleanup_journal_tail()).
925 * Requires j_checkpoint_mutex
927 int __jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
929 unsigned long freed;
930 int ret;
932 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
935 * We cannot afford for write to remain in drive's caches since as
936 * soon as we update j_tail, next transaction can start reusing journal
937 * space and if we lose sb update during power failure we'd replay
938 * old transaction with possibly newly overwritten data.
940 ret = jbd2_journal_update_sb_log_tail(journal, tid, block,
941 REQ_SYNC | REQ_FUA);
942 if (ret)
943 goto out;
945 write_lock(&journal->j_state_lock);
946 freed = block - journal->j_tail;
947 if (block < journal->j_tail)
948 freed += journal->j_last - journal->j_first;
950 trace_jbd2_update_log_tail(journal, tid, block, freed);
951 jbd_debug(1,
952 "Cleaning journal tail from %d to %d (offset %lu), "
953 "freeing %lu\n",
954 journal->j_tail_sequence, tid, block, freed);
956 journal->j_free += freed;
957 journal->j_tail_sequence = tid;
958 journal->j_tail = block;
959 write_unlock(&journal->j_state_lock);
961 out:
962 return ret;
966 * This is a variation of __jbd2_update_log_tail which checks for validity of
967 * provided log tail and locks j_checkpoint_mutex. So it is safe against races
968 * with other threads updating log tail.
970 void jbd2_update_log_tail(journal_t *journal, tid_t tid, unsigned long block)
972 mutex_lock_io(&journal->j_checkpoint_mutex);
973 if (tid_gt(tid, journal->j_tail_sequence))
974 __jbd2_update_log_tail(journal, tid, block);
975 mutex_unlock(&journal->j_checkpoint_mutex);
978 struct jbd2_stats_proc_session {
979 journal_t *journal;
980 struct transaction_stats_s *stats;
981 int start;
982 int max;
985 static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
987 return *pos ? NULL : SEQ_START_TOKEN;
990 static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
992 return NULL;
995 static int jbd2_seq_info_show(struct seq_file *seq, void *v)
997 struct jbd2_stats_proc_session *s = seq->private;
999 if (v != SEQ_START_TOKEN)
1000 return 0;
1001 seq_printf(seq, "%lu transactions (%lu requested), "
1002 "each up to %u blocks\n",
1003 s->stats->ts_tid, s->stats->ts_requested,
1004 s->journal->j_max_transaction_buffers);
1005 if (s->stats->ts_tid == 0)
1006 return 0;
1007 seq_printf(seq, "average: \n %ums waiting for transaction\n",
1008 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
1009 seq_printf(seq, " %ums request delay\n",
1010 (s->stats->ts_requested == 0) ? 0 :
1011 jiffies_to_msecs(s->stats->run.rs_request_delay /
1012 s->stats->ts_requested));
1013 seq_printf(seq, " %ums running transaction\n",
1014 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
1015 seq_printf(seq, " %ums transaction was being locked\n",
1016 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
1017 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
1018 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
1019 seq_printf(seq, " %ums logging transaction\n",
1020 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
1021 seq_printf(seq, " %lluus average transaction commit time\n",
1022 div_u64(s->journal->j_average_commit_time, 1000));
1023 seq_printf(seq, " %lu handles per transaction\n",
1024 s->stats->run.rs_handle_count / s->stats->ts_tid);
1025 seq_printf(seq, " %lu blocks per transaction\n",
1026 s->stats->run.rs_blocks / s->stats->ts_tid);
1027 seq_printf(seq, " %lu logged blocks per transaction\n",
1028 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
1029 return 0;
1032 static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
1036 static const struct seq_operations jbd2_seq_info_ops = {
1037 .start = jbd2_seq_info_start,
1038 .next = jbd2_seq_info_next,
1039 .stop = jbd2_seq_info_stop,
1040 .show = jbd2_seq_info_show,
1043 static int jbd2_seq_info_open(struct inode *inode, struct file *file)
1045 journal_t *journal = PDE_DATA(inode);
1046 struct jbd2_stats_proc_session *s;
1047 int rc, size;
1049 s = kmalloc(sizeof(*s), GFP_KERNEL);
1050 if (s == NULL)
1051 return -ENOMEM;
1052 size = sizeof(struct transaction_stats_s);
1053 s->stats = kmalloc(size, GFP_KERNEL);
1054 if (s->stats == NULL) {
1055 kfree(s);
1056 return -ENOMEM;
1058 spin_lock(&journal->j_history_lock);
1059 memcpy(s->stats, &journal->j_stats, size);
1060 s->journal = journal;
1061 spin_unlock(&journal->j_history_lock);
1063 rc = seq_open(file, &jbd2_seq_info_ops);
1064 if (rc == 0) {
1065 struct seq_file *m = file->private_data;
1066 m->private = s;
1067 } else {
1068 kfree(s->stats);
1069 kfree(s);
1071 return rc;
1075 static int jbd2_seq_info_release(struct inode *inode, struct file *file)
1077 struct seq_file *seq = file->private_data;
1078 struct jbd2_stats_proc_session *s = seq->private;
1079 kfree(s->stats);
1080 kfree(s);
1081 return seq_release(inode, file);
1084 static const struct file_operations jbd2_seq_info_fops = {
1085 .owner = THIS_MODULE,
1086 .open = jbd2_seq_info_open,
1087 .read = seq_read,
1088 .llseek = seq_lseek,
1089 .release = jbd2_seq_info_release,
1092 static struct proc_dir_entry *proc_jbd2_stats;
1094 static void jbd2_stats_proc_init(journal_t *journal)
1096 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
1097 if (journal->j_proc_entry) {
1098 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
1099 &jbd2_seq_info_fops, journal);
1103 static void jbd2_stats_proc_exit(journal_t *journal)
1105 remove_proc_entry("info", journal->j_proc_entry);
1106 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
1110 * Management for journal control blocks: functions to create and
1111 * destroy journal_t structures, and to initialise and read existing
1112 * journal blocks from disk. */
1114 /* First: create and setup a journal_t object in memory. We initialise
1115 * very few fields yet: that has to wait until we have created the
1116 * journal structures from from scratch, or loaded them from disk. */
1118 static journal_t *journal_init_common(struct block_device *bdev,
1119 struct block_device *fs_dev,
1120 unsigned long long start, int len, int blocksize)
1122 static struct lock_class_key jbd2_trans_commit_key;
1123 journal_t *journal;
1124 int err;
1125 struct buffer_head *bh;
1126 int n;
1128 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1129 if (!journal)
1130 return NULL;
1132 init_waitqueue_head(&journal->j_wait_transaction_locked);
1133 init_waitqueue_head(&journal->j_wait_done_commit);
1134 init_waitqueue_head(&journal->j_wait_commit);
1135 init_waitqueue_head(&journal->j_wait_updates);
1136 init_waitqueue_head(&journal->j_wait_reserved);
1137 mutex_init(&journal->j_barrier);
1138 mutex_init(&journal->j_checkpoint_mutex);
1139 spin_lock_init(&journal->j_revoke_lock);
1140 spin_lock_init(&journal->j_list_lock);
1141 rwlock_init(&journal->j_state_lock);
1143 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
1144 journal->j_min_batch_time = 0;
1145 journal->j_max_batch_time = 15000; /* 15ms */
1146 atomic_set(&journal->j_reserved_credits, 0);
1148 /* The journal is marked for error until we succeed with recovery! */
1149 journal->j_flags = JBD2_ABORT;
1151 /* Set up a default-sized revoke table for the new mount. */
1152 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
1153 if (err)
1154 goto err_cleanup;
1156 spin_lock_init(&journal->j_history_lock);
1158 lockdep_init_map(&journal->j_trans_commit_map, "jbd2_handle",
1159 &jbd2_trans_commit_key, 0);
1161 /* journal descriptor can store up to n blocks -bzzz */
1162 journal->j_blocksize = blocksize;
1163 journal->j_dev = bdev;
1164 journal->j_fs_dev = fs_dev;
1165 journal->j_blk_offset = start;
1166 journal->j_maxlen = len;
1167 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1168 journal->j_wbufsize = n;
1169 journal->j_wbuf = kmalloc_array(n, sizeof(struct buffer_head *),
1170 GFP_KERNEL);
1171 if (!journal->j_wbuf)
1172 goto err_cleanup;
1174 bh = getblk_unmovable(journal->j_dev, start, journal->j_blocksize);
1175 if (!bh) {
1176 pr_err("%s: Cannot get buffer for journal superblock\n",
1177 __func__);
1178 goto err_cleanup;
1180 journal->j_sb_buffer = bh;
1181 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1183 return journal;
1185 err_cleanup:
1186 kfree(journal->j_wbuf);
1187 jbd2_journal_destroy_revoke(journal);
1188 kfree(journal);
1189 return NULL;
1192 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1194 * Create a journal structure assigned some fixed set of disk blocks to
1195 * the journal. We don't actually touch those disk blocks yet, but we
1196 * need to set up all of the mapping information to tell the journaling
1197 * system where the journal blocks are.
1202 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1203 * @bdev: Block device on which to create the journal
1204 * @fs_dev: Device which hold journalled filesystem for this journal.
1205 * @start: Block nr Start of journal.
1206 * @len: Length of the journal in blocks.
1207 * @blocksize: blocksize of journalling device
1209 * Returns: a newly created journal_t *
1211 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1212 * range of blocks on an arbitrary block device.
1215 journal_t *jbd2_journal_init_dev(struct block_device *bdev,
1216 struct block_device *fs_dev,
1217 unsigned long long start, int len, int blocksize)
1219 journal_t *journal;
1221 journal = journal_init_common(bdev, fs_dev, start, len, blocksize);
1222 if (!journal)
1223 return NULL;
1225 bdevname(journal->j_dev, journal->j_devname);
1226 strreplace(journal->j_devname, '/', '!');
1227 jbd2_stats_proc_init(journal);
1229 return journal;
1233 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1234 * @inode: An inode to create the journal in
1236 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1237 * the journal. The inode must exist already, must support bmap() and
1238 * must have all data blocks preallocated.
1240 journal_t *jbd2_journal_init_inode(struct inode *inode)
1242 journal_t *journal;
1243 char *p;
1244 unsigned long long blocknr;
1246 blocknr = bmap(inode, 0);
1247 if (!blocknr) {
1248 pr_err("%s: Cannot locate journal superblock\n",
1249 __func__);
1250 return NULL;
1253 jbd_debug(1, "JBD2: inode %s/%ld, size %lld, bits %d, blksize %ld\n",
1254 inode->i_sb->s_id, inode->i_ino, (long long) inode->i_size,
1255 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1257 journal = journal_init_common(inode->i_sb->s_bdev, inode->i_sb->s_bdev,
1258 blocknr, inode->i_size >> inode->i_sb->s_blocksize_bits,
1259 inode->i_sb->s_blocksize);
1260 if (!journal)
1261 return NULL;
1263 journal->j_inode = inode;
1264 bdevname(journal->j_dev, journal->j_devname);
1265 p = strreplace(journal->j_devname, '/', '!');
1266 sprintf(p, "-%lu", journal->j_inode->i_ino);
1267 jbd2_stats_proc_init(journal);
1269 return journal;
1273 * If the journal init or create aborts, we need to mark the journal
1274 * superblock as being NULL to prevent the journal destroy from writing
1275 * back a bogus superblock.
1277 static void journal_fail_superblock (journal_t *journal)
1279 struct buffer_head *bh = journal->j_sb_buffer;
1280 brelse(bh);
1281 journal->j_sb_buffer = NULL;
1285 * Given a journal_t structure, initialise the various fields for
1286 * startup of a new journaling session. We use this both when creating
1287 * a journal, and after recovering an old journal to reset it for
1288 * subsequent use.
1291 static int journal_reset(journal_t *journal)
1293 journal_superblock_t *sb = journal->j_superblock;
1294 unsigned long long first, last;
1296 first = be32_to_cpu(sb->s_first);
1297 last = be32_to_cpu(sb->s_maxlen);
1298 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
1299 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
1300 first, last);
1301 journal_fail_superblock(journal);
1302 return -EINVAL;
1305 journal->j_first = first;
1306 journal->j_last = last;
1308 journal->j_head = first;
1309 journal->j_tail = first;
1310 journal->j_free = last - first;
1312 journal->j_tail_sequence = journal->j_transaction_sequence;
1313 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1314 journal->j_commit_request = journal->j_commit_sequence;
1316 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1319 * As a special case, if the on-disk copy is already marked as needing
1320 * no recovery (s_start == 0), then we can safely defer the superblock
1321 * update until the next commit by setting JBD2_FLUSHED. This avoids
1322 * attempting a write to a potential-readonly device.
1324 if (sb->s_start == 0) {
1325 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
1326 "(start %ld, seq %d, errno %d)\n",
1327 journal->j_tail, journal->j_tail_sequence,
1328 journal->j_errno);
1329 journal->j_flags |= JBD2_FLUSHED;
1330 } else {
1331 /* Lock here to make assertions happy... */
1332 mutex_lock_io(&journal->j_checkpoint_mutex);
1334 * Update log tail information. We use REQ_FUA since new
1335 * transaction will start reusing journal space and so we
1336 * must make sure information about current log tail is on
1337 * disk before that.
1339 jbd2_journal_update_sb_log_tail(journal,
1340 journal->j_tail_sequence,
1341 journal->j_tail,
1342 REQ_SYNC | REQ_FUA);
1343 mutex_unlock(&journal->j_checkpoint_mutex);
1345 return jbd2_journal_start_thread(journal);
1349 * This function expects that the caller will have locked the journal
1350 * buffer head, and will return with it unlocked
1352 static int jbd2_write_superblock(journal_t *journal, int write_flags)
1354 struct buffer_head *bh = journal->j_sb_buffer;
1355 journal_superblock_t *sb = journal->j_superblock;
1356 int ret;
1358 /* Buffer got discarded which means block device got invalidated */
1359 if (!buffer_mapped(bh)) {
1360 unlock_buffer(bh);
1361 return -EIO;
1364 trace_jbd2_write_superblock(journal, write_flags);
1365 if (!(journal->j_flags & JBD2_BARRIER))
1366 write_flags &= ~(REQ_FUA | REQ_PREFLUSH);
1367 if (buffer_write_io_error(bh)) {
1369 * Oh, dear. A previous attempt to write the journal
1370 * superblock failed. This could happen because the
1371 * USB device was yanked out. Or it could happen to
1372 * be a transient write error and maybe the block will
1373 * be remapped. Nothing we can do but to retry the
1374 * write and hope for the best.
1376 printk(KERN_ERR "JBD2: previous I/O error detected "
1377 "for journal superblock update for %s.\n",
1378 journal->j_devname);
1379 clear_buffer_write_io_error(bh);
1380 set_buffer_uptodate(bh);
1382 jbd2_superblock_csum_set(journal, sb);
1383 get_bh(bh);
1384 bh->b_end_io = end_buffer_write_sync;
1385 ret = submit_bh(REQ_OP_WRITE, write_flags, bh);
1386 wait_on_buffer(bh);
1387 if (buffer_write_io_error(bh)) {
1388 clear_buffer_write_io_error(bh);
1389 set_buffer_uptodate(bh);
1390 ret = -EIO;
1392 if (ret) {
1393 printk(KERN_ERR "JBD2: Error %d detected when updating "
1394 "journal superblock for %s.\n", ret,
1395 journal->j_devname);
1396 jbd2_journal_abort(journal, ret);
1399 return ret;
1403 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1404 * @journal: The journal to update.
1405 * @tail_tid: TID of the new transaction at the tail of the log
1406 * @tail_block: The first block of the transaction at the tail of the log
1407 * @write_op: With which operation should we write the journal sb
1409 * Update a journal's superblock information about log tail and write it to
1410 * disk, waiting for the IO to complete.
1412 int jbd2_journal_update_sb_log_tail(journal_t *journal, tid_t tail_tid,
1413 unsigned long tail_block, int write_op)
1415 journal_superblock_t *sb = journal->j_superblock;
1416 int ret;
1418 if (is_journal_aborted(journal))
1419 return -EIO;
1421 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1422 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1423 tail_block, tail_tid);
1425 lock_buffer(journal->j_sb_buffer);
1426 sb->s_sequence = cpu_to_be32(tail_tid);
1427 sb->s_start = cpu_to_be32(tail_block);
1429 ret = jbd2_write_superblock(journal, write_op);
1430 if (ret)
1431 goto out;
1433 /* Log is no longer empty */
1434 write_lock(&journal->j_state_lock);
1435 WARN_ON(!sb->s_sequence);
1436 journal->j_flags &= ~JBD2_FLUSHED;
1437 write_unlock(&journal->j_state_lock);
1439 out:
1440 return ret;
1444 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1445 * @journal: The journal to update.
1446 * @write_op: With which operation should we write the journal sb
1448 * Update a journal's dynamic superblock fields to show that journal is empty.
1449 * Write updated superblock to disk waiting for IO to complete.
1451 static void jbd2_mark_journal_empty(journal_t *journal, int write_op)
1453 journal_superblock_t *sb = journal->j_superblock;
1455 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1456 lock_buffer(journal->j_sb_buffer);
1457 if (sb->s_start == 0) { /* Is it already empty? */
1458 unlock_buffer(journal->j_sb_buffer);
1459 return;
1462 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1463 journal->j_tail_sequence);
1465 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1466 sb->s_start = cpu_to_be32(0);
1468 jbd2_write_superblock(journal, write_op);
1470 /* Log is no longer empty */
1471 write_lock(&journal->j_state_lock);
1472 journal->j_flags |= JBD2_FLUSHED;
1473 write_unlock(&journal->j_state_lock);
1478 * jbd2_journal_update_sb_errno() - Update error in the journal.
1479 * @journal: The journal to update.
1481 * Update a journal's errno. Write updated superblock to disk waiting for IO
1482 * to complete.
1484 void jbd2_journal_update_sb_errno(journal_t *journal)
1486 journal_superblock_t *sb = journal->j_superblock;
1487 int errcode;
1489 lock_buffer(journal->j_sb_buffer);
1490 errcode = journal->j_errno;
1491 if (errcode == -ESHUTDOWN)
1492 errcode = 0;
1493 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n", errcode);
1494 sb->s_errno = cpu_to_be32(errcode);
1496 jbd2_write_superblock(journal, REQ_SYNC | REQ_FUA);
1498 EXPORT_SYMBOL(jbd2_journal_update_sb_errno);
1501 * Read the superblock for a given journal, performing initial
1502 * validation of the format.
1504 static int journal_get_superblock(journal_t *journal)
1506 struct buffer_head *bh;
1507 journal_superblock_t *sb;
1508 int err = -EIO;
1510 bh = journal->j_sb_buffer;
1512 J_ASSERT(bh != NULL);
1513 if (!buffer_uptodate(bh)) {
1514 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1515 wait_on_buffer(bh);
1516 if (!buffer_uptodate(bh)) {
1517 printk(KERN_ERR
1518 "JBD2: IO error reading journal superblock\n");
1519 goto out;
1523 if (buffer_verified(bh))
1524 return 0;
1526 sb = journal->j_superblock;
1528 err = -EINVAL;
1530 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
1531 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1532 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
1533 goto out;
1536 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1537 case JBD2_SUPERBLOCK_V1:
1538 journal->j_format_version = 1;
1539 break;
1540 case JBD2_SUPERBLOCK_V2:
1541 journal->j_format_version = 2;
1542 break;
1543 default:
1544 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
1545 goto out;
1548 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1549 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1550 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1551 printk(KERN_WARNING "JBD2: journal file too short\n");
1552 goto out;
1555 if (be32_to_cpu(sb->s_first) == 0 ||
1556 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1557 printk(KERN_WARNING
1558 "JBD2: Invalid start block of journal: %u\n",
1559 be32_to_cpu(sb->s_first));
1560 goto out;
1563 if (jbd2_has_feature_csum2(journal) &&
1564 jbd2_has_feature_csum3(journal)) {
1565 /* Can't have checksum v2 and v3 at the same time! */
1566 printk(KERN_ERR "JBD2: Can't enable checksumming v2 and v3 "
1567 "at the same time!\n");
1568 goto out;
1571 if (jbd2_journal_has_csum_v2or3_feature(journal) &&
1572 jbd2_has_feature_checksum(journal)) {
1573 /* Can't have checksum v1 and v2 on at the same time! */
1574 printk(KERN_ERR "JBD2: Can't enable checksumming v1 and v2/3 "
1575 "at the same time!\n");
1576 goto out;
1579 if (!jbd2_verify_csum_type(journal, sb)) {
1580 printk(KERN_ERR "JBD2: Unknown checksum type\n");
1581 goto out;
1584 /* Load the checksum driver */
1585 if (jbd2_journal_has_csum_v2or3_feature(journal)) {
1586 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1587 if (IS_ERR(journal->j_chksum_driver)) {
1588 printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
1589 err = PTR_ERR(journal->j_chksum_driver);
1590 journal->j_chksum_driver = NULL;
1591 goto out;
1595 /* Check superblock checksum */
1596 if (!jbd2_superblock_csum_verify(journal, sb)) {
1597 printk(KERN_ERR "JBD2: journal checksum error\n");
1598 err = -EFSBADCRC;
1599 goto out;
1602 /* Precompute checksum seed for all metadata */
1603 if (jbd2_journal_has_csum_v2or3(journal))
1604 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1605 sizeof(sb->s_uuid));
1607 set_buffer_verified(bh);
1609 return 0;
1611 out:
1612 journal_fail_superblock(journal);
1613 return err;
1617 * Load the on-disk journal superblock and read the key fields into the
1618 * journal_t.
1621 static int load_superblock(journal_t *journal)
1623 int err;
1624 journal_superblock_t *sb;
1626 err = journal_get_superblock(journal);
1627 if (err)
1628 return err;
1630 sb = journal->j_superblock;
1632 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1633 journal->j_tail = be32_to_cpu(sb->s_start);
1634 journal->j_first = be32_to_cpu(sb->s_first);
1635 journal->j_last = be32_to_cpu(sb->s_maxlen);
1636 journal->j_errno = be32_to_cpu(sb->s_errno);
1638 return 0;
1643 * int jbd2_journal_load() - Read journal from disk.
1644 * @journal: Journal to act on.
1646 * Given a journal_t structure which tells us which disk blocks contain
1647 * a journal, read the journal from disk to initialise the in-memory
1648 * structures.
1650 int jbd2_journal_load(journal_t *journal)
1652 int err;
1653 journal_superblock_t *sb;
1655 err = load_superblock(journal);
1656 if (err)
1657 return err;
1659 sb = journal->j_superblock;
1660 /* If this is a V2 superblock, then we have to check the
1661 * features flags on it. */
1663 if (journal->j_format_version >= 2) {
1664 if ((sb->s_feature_ro_compat &
1665 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
1666 (sb->s_feature_incompat &
1667 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
1668 printk(KERN_WARNING
1669 "JBD2: Unrecognised features on journal\n");
1670 return -EINVAL;
1675 * Create a slab for this blocksize
1677 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1678 if (err)
1679 return err;
1681 /* Let the recovery code check whether it needs to recover any
1682 * data from the journal. */
1683 if (jbd2_journal_recover(journal))
1684 goto recovery_error;
1686 if (journal->j_failed_commit) {
1687 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1688 "is corrupt.\n", journal->j_failed_commit,
1689 journal->j_devname);
1690 return -EFSCORRUPTED;
1693 * clear JBD2_ABORT flag initialized in journal_init_common
1694 * here to update log tail information with the newest seq.
1696 journal->j_flags &= ~JBD2_ABORT;
1698 /* OK, we've finished with the dynamic journal bits:
1699 * reinitialise the dynamic contents of the superblock in memory
1700 * and reset them on disk. */
1701 if (journal_reset(journal))
1702 goto recovery_error;
1704 journal->j_flags |= JBD2_LOADED;
1705 return 0;
1707 recovery_error:
1708 printk(KERN_WARNING "JBD2: recovery failed\n");
1709 return -EIO;
1713 * void jbd2_journal_destroy() - Release a journal_t structure.
1714 * @journal: Journal to act on.
1716 * Release a journal_t structure once it is no longer in use by the
1717 * journaled object.
1718 * Return <0 if we couldn't clean up the journal.
1720 int jbd2_journal_destroy(journal_t *journal)
1722 int err = 0;
1724 /* Wait for the commit thread to wake up and die. */
1725 journal_kill_thread(journal);
1727 /* Force a final log commit */
1728 if (journal->j_running_transaction)
1729 jbd2_journal_commit_transaction(journal);
1731 /* Force any old transactions to disk */
1733 /* Totally anal locking here... */
1734 spin_lock(&journal->j_list_lock);
1735 while (journal->j_checkpoint_transactions != NULL) {
1736 spin_unlock(&journal->j_list_lock);
1737 mutex_lock_io(&journal->j_checkpoint_mutex);
1738 err = jbd2_log_do_checkpoint(journal);
1739 mutex_unlock(&journal->j_checkpoint_mutex);
1741 * If checkpointing failed, just free the buffers to avoid
1742 * looping forever
1744 if (err) {
1745 jbd2_journal_destroy_checkpoint(journal);
1746 spin_lock(&journal->j_list_lock);
1747 break;
1749 spin_lock(&journal->j_list_lock);
1752 J_ASSERT(journal->j_running_transaction == NULL);
1753 J_ASSERT(journal->j_committing_transaction == NULL);
1754 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1755 spin_unlock(&journal->j_list_lock);
1757 if (journal->j_sb_buffer) {
1758 if (!is_journal_aborted(journal)) {
1759 mutex_lock_io(&journal->j_checkpoint_mutex);
1761 write_lock(&journal->j_state_lock);
1762 journal->j_tail_sequence =
1763 ++journal->j_transaction_sequence;
1764 write_unlock(&journal->j_state_lock);
1766 jbd2_mark_journal_empty(journal,
1767 REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
1768 mutex_unlock(&journal->j_checkpoint_mutex);
1769 } else
1770 err = -EIO;
1771 brelse(journal->j_sb_buffer);
1774 if (journal->j_proc_entry)
1775 jbd2_stats_proc_exit(journal);
1776 iput(journal->j_inode);
1777 if (journal->j_revoke)
1778 jbd2_journal_destroy_revoke(journal);
1779 if (journal->j_chksum_driver)
1780 crypto_free_shash(journal->j_chksum_driver);
1781 kfree(journal->j_wbuf);
1782 kfree(journal);
1784 return err;
1789 *int jbd2_journal_check_used_features () - Check if features specified are used.
1790 * @journal: Journal to check.
1791 * @compat: bitmask of compatible features
1792 * @ro: bitmask of features that force read-only mount
1793 * @incompat: bitmask of incompatible features
1795 * Check whether the journal uses all of a given set of
1796 * features. Return true (non-zero) if it does.
1799 int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
1800 unsigned long ro, unsigned long incompat)
1802 journal_superblock_t *sb;
1804 if (!compat && !ro && !incompat)
1805 return 1;
1806 /* Load journal superblock if it is not loaded yet. */
1807 if (journal->j_format_version == 0 &&
1808 journal_get_superblock(journal) != 0)
1809 return 0;
1810 if (journal->j_format_version == 1)
1811 return 0;
1813 sb = journal->j_superblock;
1815 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1816 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1817 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1818 return 1;
1820 return 0;
1824 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1825 * @journal: Journal to check.
1826 * @compat: bitmask of compatible features
1827 * @ro: bitmask of features that force read-only mount
1828 * @incompat: bitmask of incompatible features
1830 * Check whether the journaling code supports the use of
1831 * all of a given set of features on this journal. Return true
1832 * (non-zero) if it can. */
1834 int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
1835 unsigned long ro, unsigned long incompat)
1837 if (!compat && !ro && !incompat)
1838 return 1;
1840 /* We can support any known requested features iff the
1841 * superblock is in version 2. Otherwise we fail to support any
1842 * extended sb features. */
1844 if (journal->j_format_version != 2)
1845 return 0;
1847 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1848 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1849 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
1850 return 1;
1852 return 0;
1856 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1857 * @journal: Journal to act on.
1858 * @compat: bitmask of compatible features
1859 * @ro: bitmask of features that force read-only mount
1860 * @incompat: bitmask of incompatible features
1862 * Mark a given journal feature as present on the
1863 * superblock. Returns true if the requested features could be set.
1867 int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
1868 unsigned long ro, unsigned long incompat)
1870 #define INCOMPAT_FEATURE_ON(f) \
1871 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1872 #define COMPAT_FEATURE_ON(f) \
1873 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
1874 journal_superblock_t *sb;
1876 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
1877 return 1;
1879 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
1880 return 0;
1882 /* If enabling v2 checksums, turn on v3 instead */
1883 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V2) {
1884 incompat &= ~JBD2_FEATURE_INCOMPAT_CSUM_V2;
1885 incompat |= JBD2_FEATURE_INCOMPAT_CSUM_V3;
1888 /* Asking for checksumming v3 and v1? Only give them v3. */
1889 if (incompat & JBD2_FEATURE_INCOMPAT_CSUM_V3 &&
1890 compat & JBD2_FEATURE_COMPAT_CHECKSUM)
1891 compat &= ~JBD2_FEATURE_COMPAT_CHECKSUM;
1893 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1894 compat, ro, incompat);
1896 sb = journal->j_superblock;
1898 /* Load the checksum driver if necessary */
1899 if ((journal->j_chksum_driver == NULL) &&
1900 INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
1901 journal->j_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
1902 if (IS_ERR(journal->j_chksum_driver)) {
1903 printk(KERN_ERR "JBD2: Cannot load crc32c driver.\n");
1904 journal->j_chksum_driver = NULL;
1905 return 0;
1907 /* Precompute checksum seed for all metadata */
1908 journal->j_csum_seed = jbd2_chksum(journal, ~0, sb->s_uuid,
1909 sizeof(sb->s_uuid));
1912 lock_buffer(journal->j_sb_buffer);
1914 /* If enabling v3 checksums, update superblock */
1915 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V3)) {
1916 sb->s_checksum_type = JBD2_CRC32C_CHKSUM;
1917 sb->s_feature_compat &=
1918 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM);
1921 /* If enabling v1 checksums, downgrade superblock */
1922 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM))
1923 sb->s_feature_incompat &=
1924 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2 |
1925 JBD2_FEATURE_INCOMPAT_CSUM_V3);
1927 sb->s_feature_compat |= cpu_to_be32(compat);
1928 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1929 sb->s_feature_incompat |= cpu_to_be32(incompat);
1930 unlock_buffer(journal->j_sb_buffer);
1932 return 1;
1933 #undef COMPAT_FEATURE_ON
1934 #undef INCOMPAT_FEATURE_ON
1938 * jbd2_journal_clear_features () - Clear a given journal feature in the
1939 * superblock
1940 * @journal: Journal to act on.
1941 * @compat: bitmask of compatible features
1942 * @ro: bitmask of features that force read-only mount
1943 * @incompat: bitmask of incompatible features
1945 * Clear a given journal feature as present on the
1946 * superblock.
1948 void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1949 unsigned long ro, unsigned long incompat)
1951 journal_superblock_t *sb;
1953 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1954 compat, ro, incompat);
1956 sb = journal->j_superblock;
1958 sb->s_feature_compat &= ~cpu_to_be32(compat);
1959 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1960 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1962 EXPORT_SYMBOL(jbd2_journal_clear_features);
1965 * int jbd2_journal_flush () - Flush journal
1966 * @journal: Journal to act on.
1968 * Flush all data for a given journal to disk and empty the journal.
1969 * Filesystems can use this when remounting readonly to ensure that
1970 * recovery does not need to happen on remount.
1973 int jbd2_journal_flush(journal_t *journal)
1975 int err = 0;
1976 transaction_t *transaction = NULL;
1978 write_lock(&journal->j_state_lock);
1980 /* Force everything buffered to the log... */
1981 if (journal->j_running_transaction) {
1982 transaction = journal->j_running_transaction;
1983 __jbd2_log_start_commit(journal, transaction->t_tid);
1984 } else if (journal->j_committing_transaction)
1985 transaction = journal->j_committing_transaction;
1987 /* Wait for the log commit to complete... */
1988 if (transaction) {
1989 tid_t tid = transaction->t_tid;
1991 write_unlock(&journal->j_state_lock);
1992 jbd2_log_wait_commit(journal, tid);
1993 } else {
1994 write_unlock(&journal->j_state_lock);
1997 /* ...and flush everything in the log out to disk. */
1998 spin_lock(&journal->j_list_lock);
1999 while (!err && journal->j_checkpoint_transactions != NULL) {
2000 spin_unlock(&journal->j_list_lock);
2001 mutex_lock_io(&journal->j_checkpoint_mutex);
2002 err = jbd2_log_do_checkpoint(journal);
2003 mutex_unlock(&journal->j_checkpoint_mutex);
2004 spin_lock(&journal->j_list_lock);
2006 spin_unlock(&journal->j_list_lock);
2008 if (is_journal_aborted(journal))
2009 return -EIO;
2011 mutex_lock_io(&journal->j_checkpoint_mutex);
2012 if (!err) {
2013 err = jbd2_cleanup_journal_tail(journal);
2014 if (err < 0) {
2015 mutex_unlock(&journal->j_checkpoint_mutex);
2016 goto out;
2018 err = 0;
2021 /* Finally, mark the journal as really needing no recovery.
2022 * This sets s_start==0 in the underlying superblock, which is
2023 * the magic code for a fully-recovered superblock. Any future
2024 * commits of data to the journal will restore the current
2025 * s_start value. */
2026 jbd2_mark_journal_empty(journal, REQ_SYNC | REQ_FUA);
2027 mutex_unlock(&journal->j_checkpoint_mutex);
2028 write_lock(&journal->j_state_lock);
2029 J_ASSERT(!journal->j_running_transaction);
2030 J_ASSERT(!journal->j_committing_transaction);
2031 J_ASSERT(!journal->j_checkpoint_transactions);
2032 J_ASSERT(journal->j_head == journal->j_tail);
2033 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
2034 write_unlock(&journal->j_state_lock);
2035 out:
2036 return err;
2040 * int jbd2_journal_wipe() - Wipe journal contents
2041 * @journal: Journal to act on.
2042 * @write: flag (see below)
2044 * Wipe out all of the contents of a journal, safely. This will produce
2045 * a warning if the journal contains any valid recovery information.
2046 * Must be called between journal_init_*() and jbd2_journal_load().
2048 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
2049 * we merely suppress recovery.
2052 int jbd2_journal_wipe(journal_t *journal, int write)
2054 int err = 0;
2056 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
2058 err = load_superblock(journal);
2059 if (err)
2060 return err;
2062 if (!journal->j_tail)
2063 goto no_recovery;
2065 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
2066 write ? "Clearing" : "Ignoring");
2068 err = jbd2_journal_skip_recovery(journal);
2069 if (write) {
2070 /* Lock to make assertions happy... */
2071 mutex_lock(&journal->j_checkpoint_mutex);
2072 jbd2_mark_journal_empty(journal, REQ_SYNC | REQ_FUA);
2073 mutex_unlock(&journal->j_checkpoint_mutex);
2076 no_recovery:
2077 return err;
2081 * Journal abort has very specific semantics, which we describe
2082 * for journal abort.
2084 * Two internal functions, which provide abort to the jbd layer
2085 * itself are here.
2089 * Quick version for internal journal use (doesn't lock the journal).
2090 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
2091 * and don't attempt to make any other journal updates.
2093 void __jbd2_journal_abort_hard(journal_t *journal)
2095 transaction_t *transaction;
2097 if (journal->j_flags & JBD2_ABORT)
2098 return;
2100 printk(KERN_ERR "Aborting journal on device %s.\n",
2101 journal->j_devname);
2103 write_lock(&journal->j_state_lock);
2104 journal->j_flags |= JBD2_ABORT;
2105 transaction = journal->j_running_transaction;
2106 if (transaction)
2107 __jbd2_log_start_commit(journal, transaction->t_tid);
2108 write_unlock(&journal->j_state_lock);
2111 /* Soft abort: record the abort error status in the journal superblock,
2112 * but don't do any other IO. */
2113 static void __journal_abort_soft (journal_t *journal, int errno)
2115 int old_errno;
2117 write_lock(&journal->j_state_lock);
2118 old_errno = journal->j_errno;
2119 if (!journal->j_errno || errno == -ESHUTDOWN)
2120 journal->j_errno = errno;
2122 if (journal->j_flags & JBD2_ABORT) {
2123 write_unlock(&journal->j_state_lock);
2124 if (old_errno != -ESHUTDOWN && errno == -ESHUTDOWN)
2125 jbd2_journal_update_sb_errno(journal);
2126 return;
2128 write_unlock(&journal->j_state_lock);
2130 __jbd2_journal_abort_hard(journal);
2132 jbd2_journal_update_sb_errno(journal);
2133 write_lock(&journal->j_state_lock);
2134 journal->j_flags |= JBD2_REC_ERR;
2135 write_unlock(&journal->j_state_lock);
2139 * void jbd2_journal_abort () - Shutdown the journal immediately.
2140 * @journal: the journal to shutdown.
2141 * @errno: an error number to record in the journal indicating
2142 * the reason for the shutdown.
2144 * Perform a complete, immediate shutdown of the ENTIRE
2145 * journal (not of a single transaction). This operation cannot be
2146 * undone without closing and reopening the journal.
2148 * The jbd2_journal_abort function is intended to support higher level error
2149 * recovery mechanisms such as the ext2/ext3 remount-readonly error
2150 * mode.
2152 * Journal abort has very specific semantics. Any existing dirty,
2153 * unjournaled buffers in the main filesystem will still be written to
2154 * disk by bdflush, but the journaling mechanism will be suspended
2155 * immediately and no further transaction commits will be honoured.
2157 * Any dirty, journaled buffers will be written back to disk without
2158 * hitting the journal. Atomicity cannot be guaranteed on an aborted
2159 * filesystem, but we _do_ attempt to leave as much data as possible
2160 * behind for fsck to use for cleanup.
2162 * Any attempt to get a new transaction handle on a journal which is in
2163 * ABORT state will just result in an -EROFS error return. A
2164 * jbd2_journal_stop on an existing handle will return -EIO if we have
2165 * entered abort state during the update.
2167 * Recursive transactions are not disturbed by journal abort until the
2168 * final jbd2_journal_stop, which will receive the -EIO error.
2170 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
2171 * which will be recorded (if possible) in the journal superblock. This
2172 * allows a client to record failure conditions in the middle of a
2173 * transaction without having to complete the transaction to record the
2174 * failure to disk. ext3_error, for example, now uses this
2175 * functionality.
2179 void jbd2_journal_abort(journal_t *journal, int errno)
2181 __journal_abort_soft(journal, errno);
2185 * int jbd2_journal_errno () - returns the journal's error state.
2186 * @journal: journal to examine.
2188 * This is the errno number set with jbd2_journal_abort(), the last
2189 * time the journal was mounted - if the journal was stopped
2190 * without calling abort this will be 0.
2192 * If the journal has been aborted on this mount time -EROFS will
2193 * be returned.
2195 int jbd2_journal_errno(journal_t *journal)
2197 int err;
2199 read_lock(&journal->j_state_lock);
2200 if (journal->j_flags & JBD2_ABORT)
2201 err = -EROFS;
2202 else
2203 err = journal->j_errno;
2204 read_unlock(&journal->j_state_lock);
2205 return err;
2209 * int jbd2_journal_clear_err () - clears the journal's error state
2210 * @journal: journal to act on.
2212 * An error must be cleared or acked to take a FS out of readonly
2213 * mode.
2215 int jbd2_journal_clear_err(journal_t *journal)
2217 int err = 0;
2219 write_lock(&journal->j_state_lock);
2220 if (journal->j_flags & JBD2_ABORT)
2221 err = -EROFS;
2222 else
2223 journal->j_errno = 0;
2224 write_unlock(&journal->j_state_lock);
2225 return err;
2229 * void jbd2_journal_ack_err() - Ack journal err.
2230 * @journal: journal to act on.
2232 * An error must be cleared or acked to take a FS out of readonly
2233 * mode.
2235 void jbd2_journal_ack_err(journal_t *journal)
2237 write_lock(&journal->j_state_lock);
2238 if (journal->j_errno)
2239 journal->j_flags |= JBD2_ACK_ERR;
2240 write_unlock(&journal->j_state_lock);
2243 int jbd2_journal_blocks_per_page(struct inode *inode)
2245 return 1 << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
2249 * helper functions to deal with 32 or 64bit block numbers.
2251 size_t journal_tag_bytes(journal_t *journal)
2253 size_t sz;
2255 if (jbd2_has_feature_csum3(journal))
2256 return sizeof(journal_block_tag3_t);
2258 sz = sizeof(journal_block_tag_t);
2260 if (jbd2_has_feature_csum2(journal))
2261 sz += sizeof(__u16);
2263 if (jbd2_has_feature_64bit(journal))
2264 return sz;
2265 else
2266 return sz - sizeof(__u32);
2270 * JBD memory management
2272 * These functions are used to allocate block-sized chunks of memory
2273 * used for making copies of buffer_head data. Very often it will be
2274 * page-sized chunks of data, but sometimes it will be in
2275 * sub-page-size chunks. (For example, 16k pages on Power systems
2276 * with a 4k block file system.) For blocks smaller than a page, we
2277 * use a SLAB allocator. There are slab caches for each block size,
2278 * which are allocated at mount time, if necessary, and we only free
2279 * (all of) the slab caches when/if the jbd2 module is unloaded. For
2280 * this reason we don't need to a mutex to protect access to
2281 * jbd2_slab[] allocating or releasing memory; only in
2282 * jbd2_journal_create_slab().
2284 #define JBD2_MAX_SLABS 8
2285 static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
2287 static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
2288 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2289 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2293 static void jbd2_journal_destroy_slabs(void)
2295 int i;
2297 for (i = 0; i < JBD2_MAX_SLABS; i++) {
2298 if (jbd2_slab[i])
2299 kmem_cache_destroy(jbd2_slab[i]);
2300 jbd2_slab[i] = NULL;
2304 static int jbd2_journal_create_slab(size_t size)
2306 static DEFINE_MUTEX(jbd2_slab_create_mutex);
2307 int i = order_base_2(size) - 10;
2308 size_t slab_size;
2310 if (size == PAGE_SIZE)
2311 return 0;
2313 if (i >= JBD2_MAX_SLABS)
2314 return -EINVAL;
2316 if (unlikely(i < 0))
2317 i = 0;
2318 mutex_lock(&jbd2_slab_create_mutex);
2319 if (jbd2_slab[i]) {
2320 mutex_unlock(&jbd2_slab_create_mutex);
2321 return 0; /* Already created */
2324 slab_size = 1 << (i+10);
2325 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
2326 slab_size, 0, NULL);
2327 mutex_unlock(&jbd2_slab_create_mutex);
2328 if (!jbd2_slab[i]) {
2329 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
2330 return -ENOMEM;
2332 return 0;
2335 static struct kmem_cache *get_slab(size_t size)
2337 int i = order_base_2(size) - 10;
2339 BUG_ON(i >= JBD2_MAX_SLABS);
2340 if (unlikely(i < 0))
2341 i = 0;
2342 BUG_ON(jbd2_slab[i] == NULL);
2343 return jbd2_slab[i];
2346 void *jbd2_alloc(size_t size, gfp_t flags)
2348 void *ptr;
2350 BUG_ON(size & (size-1)); /* Must be a power of 2 */
2352 if (size < PAGE_SIZE)
2353 ptr = kmem_cache_alloc(get_slab(size), flags);
2354 else
2355 ptr = (void *)__get_free_pages(flags, get_order(size));
2357 /* Check alignment; SLUB has gotten this wrong in the past,
2358 * and this can lead to user data corruption! */
2359 BUG_ON(((unsigned long) ptr) & (size-1));
2361 return ptr;
2364 void jbd2_free(void *ptr, size_t size)
2366 if (size < PAGE_SIZE)
2367 kmem_cache_free(get_slab(size), ptr);
2368 else
2369 free_pages((unsigned long)ptr, get_order(size));
2373 * Journal_head storage management
2375 static struct kmem_cache *jbd2_journal_head_cache;
2376 #ifdef CONFIG_JBD2_DEBUG
2377 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2378 #endif
2380 static int jbd2_journal_init_journal_head_cache(void)
2382 int retval;
2384 J_ASSERT(jbd2_journal_head_cache == NULL);
2385 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
2386 sizeof(struct journal_head),
2387 0, /* offset */
2388 SLAB_TEMPORARY | SLAB_TYPESAFE_BY_RCU,
2389 NULL); /* ctor */
2390 retval = 0;
2391 if (!jbd2_journal_head_cache) {
2392 retval = -ENOMEM;
2393 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
2395 return retval;
2398 static void jbd2_journal_destroy_journal_head_cache(void)
2400 if (jbd2_journal_head_cache) {
2401 kmem_cache_destroy(jbd2_journal_head_cache);
2402 jbd2_journal_head_cache = NULL;
2407 * journal_head splicing and dicing
2409 static struct journal_head *journal_alloc_journal_head(void)
2411 struct journal_head *ret;
2413 #ifdef CONFIG_JBD2_DEBUG
2414 atomic_inc(&nr_journal_heads);
2415 #endif
2416 ret = kmem_cache_zalloc(jbd2_journal_head_cache, GFP_NOFS);
2417 if (!ret) {
2418 jbd_debug(1, "out of memory for journal_head\n");
2419 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
2420 ret = kmem_cache_zalloc(jbd2_journal_head_cache,
2421 GFP_NOFS | __GFP_NOFAIL);
2423 return ret;
2426 static void journal_free_journal_head(struct journal_head *jh)
2428 #ifdef CONFIG_JBD2_DEBUG
2429 atomic_dec(&nr_journal_heads);
2430 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
2431 #endif
2432 kmem_cache_free(jbd2_journal_head_cache, jh);
2436 * A journal_head is attached to a buffer_head whenever JBD has an
2437 * interest in the buffer.
2439 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2440 * is set. This bit is tested in core kernel code where we need to take
2441 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2442 * there.
2444 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2446 * When a buffer has its BH_JBD bit set it is immune from being released by
2447 * core kernel code, mainly via ->b_count.
2449 * A journal_head is detached from its buffer_head when the journal_head's
2450 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2451 * transaction (b_cp_transaction) hold their references to b_jcount.
2453 * Various places in the kernel want to attach a journal_head to a buffer_head
2454 * _before_ attaching the journal_head to a transaction. To protect the
2455 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2456 * journal_head's b_jcount refcount by one. The caller must call
2457 * jbd2_journal_put_journal_head() to undo this.
2459 * So the typical usage would be:
2461 * (Attach a journal_head if needed. Increments b_jcount)
2462 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2463 * ...
2464 * (Get another reference for transaction)
2465 * jbd2_journal_grab_journal_head(bh);
2466 * jh->b_transaction = xxx;
2467 * (Put original reference)
2468 * jbd2_journal_put_journal_head(jh);
2472 * Give a buffer_head a journal_head.
2474 * May sleep.
2476 struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
2478 struct journal_head *jh;
2479 struct journal_head *new_jh = NULL;
2481 repeat:
2482 if (!buffer_jbd(bh))
2483 new_jh = journal_alloc_journal_head();
2485 jbd_lock_bh_journal_head(bh);
2486 if (buffer_jbd(bh)) {
2487 jh = bh2jh(bh);
2488 } else {
2489 J_ASSERT_BH(bh,
2490 (atomic_read(&bh->b_count) > 0) ||
2491 (bh->b_page && bh->b_page->mapping));
2493 if (!new_jh) {
2494 jbd_unlock_bh_journal_head(bh);
2495 goto repeat;
2498 jh = new_jh;
2499 new_jh = NULL; /* We consumed it */
2500 set_buffer_jbd(bh);
2501 bh->b_private = jh;
2502 jh->b_bh = bh;
2503 get_bh(bh);
2504 BUFFER_TRACE(bh, "added journal_head");
2506 jh->b_jcount++;
2507 jbd_unlock_bh_journal_head(bh);
2508 if (new_jh)
2509 journal_free_journal_head(new_jh);
2510 return bh->b_private;
2514 * Grab a ref against this buffer_head's journal_head. If it ended up not
2515 * having a journal_head, return NULL
2517 struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
2519 struct journal_head *jh = NULL;
2521 jbd_lock_bh_journal_head(bh);
2522 if (buffer_jbd(bh)) {
2523 jh = bh2jh(bh);
2524 jh->b_jcount++;
2526 jbd_unlock_bh_journal_head(bh);
2527 return jh;
2530 static void __journal_remove_journal_head(struct buffer_head *bh)
2532 struct journal_head *jh = bh2jh(bh);
2534 J_ASSERT_JH(jh, jh->b_jcount >= 0);
2535 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2536 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2537 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2538 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2539 J_ASSERT_BH(bh, buffer_jbd(bh));
2540 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2541 BUFFER_TRACE(bh, "remove journal_head");
2542 if (jh->b_frozen_data) {
2543 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2544 jbd2_free(jh->b_frozen_data, bh->b_size);
2546 if (jh->b_committed_data) {
2547 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2548 jbd2_free(jh->b_committed_data, bh->b_size);
2550 bh->b_private = NULL;
2551 jh->b_bh = NULL; /* debug, really */
2552 clear_buffer_jbd(bh);
2553 journal_free_journal_head(jh);
2557 * Drop a reference on the passed journal_head. If it fell to zero then
2558 * release the journal_head from the buffer_head.
2560 void jbd2_journal_put_journal_head(struct journal_head *jh)
2562 struct buffer_head *bh = jh2bh(jh);
2564 jbd_lock_bh_journal_head(bh);
2565 J_ASSERT_JH(jh, jh->b_jcount > 0);
2566 --jh->b_jcount;
2567 if (!jh->b_jcount) {
2568 __journal_remove_journal_head(bh);
2569 jbd_unlock_bh_journal_head(bh);
2570 __brelse(bh);
2571 } else
2572 jbd_unlock_bh_journal_head(bh);
2576 * Initialize jbd inode head
2578 void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2580 jinode->i_transaction = NULL;
2581 jinode->i_next_transaction = NULL;
2582 jinode->i_vfs_inode = inode;
2583 jinode->i_flags = 0;
2584 jinode->i_dirty_start = 0;
2585 jinode->i_dirty_end = 0;
2586 INIT_LIST_HEAD(&jinode->i_list);
2590 * Function to be called before we start removing inode from memory (i.e.,
2591 * clear_inode() is a fine place to be called from). It removes inode from
2592 * transaction's lists.
2594 void jbd2_journal_release_jbd_inode(journal_t *journal,
2595 struct jbd2_inode *jinode)
2597 if (!journal)
2598 return;
2599 restart:
2600 spin_lock(&journal->j_list_lock);
2601 /* Is commit writing out inode - we have to wait */
2602 if (jinode->i_flags & JI_COMMIT_RUNNING) {
2603 wait_queue_head_t *wq;
2604 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2605 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2606 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
2607 spin_unlock(&journal->j_list_lock);
2608 schedule();
2609 finish_wait(wq, &wait.wq_entry);
2610 goto restart;
2613 if (jinode->i_transaction) {
2614 list_del(&jinode->i_list);
2615 jinode->i_transaction = NULL;
2617 spin_unlock(&journal->j_list_lock);
2621 #ifdef CONFIG_PROC_FS
2623 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2625 static void __init jbd2_create_jbd_stats_proc_entry(void)
2627 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2630 static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2632 if (proc_jbd2_stats)
2633 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2636 #else
2638 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2639 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2641 #endif
2643 struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
2645 static int __init jbd2_journal_init_handle_cache(void)
2647 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
2648 if (jbd2_handle_cache == NULL) {
2649 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2650 return -ENOMEM;
2652 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2653 if (jbd2_inode_cache == NULL) {
2654 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2655 kmem_cache_destroy(jbd2_handle_cache);
2656 return -ENOMEM;
2658 return 0;
2661 static void jbd2_journal_destroy_handle_cache(void)
2663 if (jbd2_handle_cache)
2664 kmem_cache_destroy(jbd2_handle_cache);
2665 if (jbd2_inode_cache)
2666 kmem_cache_destroy(jbd2_inode_cache);
2671 * Module startup and shutdown
2674 static int __init journal_init_caches(void)
2676 int ret;
2678 ret = jbd2_journal_init_revoke_caches();
2679 if (ret == 0)
2680 ret = jbd2_journal_init_journal_head_cache();
2681 if (ret == 0)
2682 ret = jbd2_journal_init_handle_cache();
2683 if (ret == 0)
2684 ret = jbd2_journal_init_transaction_cache();
2685 return ret;
2688 static void jbd2_journal_destroy_caches(void)
2690 jbd2_journal_destroy_revoke_caches();
2691 jbd2_journal_destroy_journal_head_cache();
2692 jbd2_journal_destroy_handle_cache();
2693 jbd2_journal_destroy_transaction_cache();
2694 jbd2_journal_destroy_slabs();
2697 static int __init journal_init(void)
2699 int ret;
2701 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2703 ret = journal_init_caches();
2704 if (ret == 0) {
2705 jbd2_create_jbd_stats_proc_entry();
2706 } else {
2707 jbd2_journal_destroy_caches();
2709 return ret;
2712 static void __exit journal_exit(void)
2714 #ifdef CONFIG_JBD2_DEBUG
2715 int n = atomic_read(&nr_journal_heads);
2716 if (n)
2717 printk(KERN_ERR "JBD2: leaked %d journal_heads!\n", n);
2718 #endif
2719 jbd2_remove_jbd_stats_proc_entry();
2720 jbd2_journal_destroy_caches();
2723 MODULE_LICENSE("GPL");
2724 module_init(journal_init);
2725 module_exit(journal_exit);