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
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>
28 #include <linux/jbd2.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
33 #include <linux/freezer.h>
34 #include <linux/pagemap.h>
35 #include <linux/kthread.h>
36 #include <linux/poison.h>
37 #include <linux/proc_fs.h>
38 #include <linux/debugfs.h>
39 #include <linux/seq_file.h>
40 #include <linux/math64.h>
41 #include <linux/hash.h>
42 #include <linux/log2.h>
43 #include <linux/vmalloc.h>
44 #include <linux/backing-dev.h>
45 #include <linux/bitops.h>
46 #include <linux/ratelimit.h>
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/jbd2.h>
51 #include <asm/uaccess.h>
54 EXPORT_SYMBOL(jbd2_journal_extend
);
55 EXPORT_SYMBOL(jbd2_journal_stop
);
56 EXPORT_SYMBOL(jbd2_journal_lock_updates
);
57 EXPORT_SYMBOL(jbd2_journal_unlock_updates
);
58 EXPORT_SYMBOL(jbd2_journal_get_write_access
);
59 EXPORT_SYMBOL(jbd2_journal_get_create_access
);
60 EXPORT_SYMBOL(jbd2_journal_get_undo_access
);
61 EXPORT_SYMBOL(jbd2_journal_set_triggers
);
62 EXPORT_SYMBOL(jbd2_journal_dirty_metadata
);
63 EXPORT_SYMBOL(jbd2_journal_release_buffer
);
64 EXPORT_SYMBOL(jbd2_journal_forget
);
66 EXPORT_SYMBOL(journal_sync_buffer
);
68 EXPORT_SYMBOL(jbd2_journal_flush
);
69 EXPORT_SYMBOL(jbd2_journal_revoke
);
71 EXPORT_SYMBOL(jbd2_journal_init_dev
);
72 EXPORT_SYMBOL(jbd2_journal_init_inode
);
73 EXPORT_SYMBOL(jbd2_journal_check_used_features
);
74 EXPORT_SYMBOL(jbd2_journal_check_available_features
);
75 EXPORT_SYMBOL(jbd2_journal_set_features
);
76 EXPORT_SYMBOL(jbd2_journal_load
);
77 EXPORT_SYMBOL(jbd2_journal_destroy
);
78 EXPORT_SYMBOL(jbd2_journal_abort
);
79 EXPORT_SYMBOL(jbd2_journal_errno
);
80 EXPORT_SYMBOL(jbd2_journal_ack_err
);
81 EXPORT_SYMBOL(jbd2_journal_clear_err
);
82 EXPORT_SYMBOL(jbd2_log_wait_commit
);
83 EXPORT_SYMBOL(jbd2_log_start_commit
);
84 EXPORT_SYMBOL(jbd2_journal_start_commit
);
85 EXPORT_SYMBOL(jbd2_journal_force_commit_nested
);
86 EXPORT_SYMBOL(jbd2_journal_wipe
);
87 EXPORT_SYMBOL(jbd2_journal_blocks_per_page
);
88 EXPORT_SYMBOL(jbd2_journal_invalidatepage
);
89 EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers
);
90 EXPORT_SYMBOL(jbd2_journal_force_commit
);
91 EXPORT_SYMBOL(jbd2_journal_file_inode
);
92 EXPORT_SYMBOL(jbd2_journal_init_jbd_inode
);
93 EXPORT_SYMBOL(jbd2_journal_release_jbd_inode
);
94 EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate
);
95 EXPORT_SYMBOL(jbd2_inode_cache
);
97 static void __journal_abort_soft (journal_t
*journal
, int errno
);
98 static int jbd2_journal_create_slab(size_t slab_size
);
100 /* Checksumming functions */
101 int jbd2_verify_csum_type(journal_t
*j
, journal_superblock_t
*sb
)
103 if (!JBD2_HAS_INCOMPAT_FEATURE(j
, JBD2_FEATURE_INCOMPAT_CSUM_V2
))
106 return sb
->s_checksum_type
== JBD2_CRC32C_CHKSUM
;
109 static __u32
jbd2_superblock_csum(journal_t
*j
, journal_superblock_t
*sb
)
111 __u32 csum
, old_csum
;
113 old_csum
= sb
->s_checksum
;
115 csum
= jbd2_chksum(j
, ~0, (char *)sb
, sizeof(journal_superblock_t
));
116 sb
->s_checksum
= old_csum
;
118 return cpu_to_be32(csum
);
121 int jbd2_superblock_csum_verify(journal_t
*j
, journal_superblock_t
*sb
)
123 if (!JBD2_HAS_INCOMPAT_FEATURE(j
, JBD2_FEATURE_INCOMPAT_CSUM_V2
))
126 return sb
->s_checksum
== jbd2_superblock_csum(j
, sb
);
129 void jbd2_superblock_csum_set(journal_t
*j
, journal_superblock_t
*sb
)
131 if (!JBD2_HAS_INCOMPAT_FEATURE(j
, JBD2_FEATURE_INCOMPAT_CSUM_V2
))
134 sb
->s_checksum
= jbd2_superblock_csum(j
, sb
);
138 * Helper function used to manage commit timeouts
141 static void commit_timeout(unsigned long __data
)
143 struct task_struct
* p
= (struct task_struct
*) __data
;
149 * kjournald2: The main thread function used to manage a logging device
152 * This kernel thread is responsible for two things:
154 * 1) COMMIT: Every so often we need to commit the current state of the
155 * filesystem to disk. The journal thread is responsible for writing
156 * all of the metadata buffers to disk.
158 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
159 * of the data in that part of the log has been rewritten elsewhere on
160 * the disk. Flushing these old buffers to reclaim space in the log is
161 * known as checkpointing, and this thread is responsible for that job.
164 static int kjournald2(void *arg
)
166 journal_t
*journal
= arg
;
167 transaction_t
*transaction
;
170 * Set up an interval timer which can be used to trigger a commit wakeup
171 * after the commit interval expires
173 setup_timer(&journal
->j_commit_timer
, commit_timeout
,
174 (unsigned long)current
);
178 /* Record that the journal thread is running */
179 journal
->j_task
= current
;
180 wake_up(&journal
->j_wait_done_commit
);
183 * And now, wait forever for commit wakeup events.
185 write_lock(&journal
->j_state_lock
);
188 if (journal
->j_flags
& JBD2_UNMOUNT
)
191 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
192 journal
->j_commit_sequence
, journal
->j_commit_request
);
194 if (journal
->j_commit_sequence
!= journal
->j_commit_request
) {
195 jbd_debug(1, "OK, requests differ\n");
196 write_unlock(&journal
->j_state_lock
);
197 del_timer_sync(&journal
->j_commit_timer
);
198 jbd2_journal_commit_transaction(journal
);
199 write_lock(&journal
->j_state_lock
);
203 wake_up(&journal
->j_wait_done_commit
);
204 if (freezing(current
)) {
206 * The simpler the better. Flushing journal isn't a
207 * good idea, because that depends on threads that may
208 * be already stopped.
210 jbd_debug(1, "Now suspending kjournald2\n");
211 write_unlock(&journal
->j_state_lock
);
213 write_lock(&journal
->j_state_lock
);
216 * We assume on resume that commits are already there,
220 int should_sleep
= 1;
222 prepare_to_wait(&journal
->j_wait_commit
, &wait
,
224 if (journal
->j_commit_sequence
!= journal
->j_commit_request
)
226 transaction
= journal
->j_running_transaction
;
227 if (transaction
&& time_after_eq(jiffies
,
228 transaction
->t_expires
))
230 if (journal
->j_flags
& JBD2_UNMOUNT
)
233 write_unlock(&journal
->j_state_lock
);
235 write_lock(&journal
->j_state_lock
);
237 finish_wait(&journal
->j_wait_commit
, &wait
);
240 jbd_debug(1, "kjournald2 wakes\n");
243 * Were we woken up by a commit wakeup event?
245 transaction
= journal
->j_running_transaction
;
246 if (transaction
&& time_after_eq(jiffies
, transaction
->t_expires
)) {
247 journal
->j_commit_request
= transaction
->t_tid
;
248 jbd_debug(1, "woke because of timeout\n");
253 write_unlock(&journal
->j_state_lock
);
254 del_timer_sync(&journal
->j_commit_timer
);
255 journal
->j_task
= NULL
;
256 wake_up(&journal
->j_wait_done_commit
);
257 jbd_debug(1, "Journal thread exiting.\n");
261 static int jbd2_journal_start_thread(journal_t
*journal
)
263 struct task_struct
*t
;
265 t
= kthread_run(kjournald2
, journal
, "jbd2/%s",
270 wait_event(journal
->j_wait_done_commit
, journal
->j_task
!= NULL
);
274 static void journal_kill_thread(journal_t
*journal
)
276 write_lock(&journal
->j_state_lock
);
277 journal
->j_flags
|= JBD2_UNMOUNT
;
279 while (journal
->j_task
) {
280 wake_up(&journal
->j_wait_commit
);
281 write_unlock(&journal
->j_state_lock
);
282 wait_event(journal
->j_wait_done_commit
, journal
->j_task
== NULL
);
283 write_lock(&journal
->j_state_lock
);
285 write_unlock(&journal
->j_state_lock
);
289 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
291 * Writes a metadata buffer to a given disk block. The actual IO is not
292 * performed but a new buffer_head is constructed which labels the data
293 * to be written with the correct destination disk block.
295 * Any magic-number escaping which needs to be done will cause a
296 * copy-out here. If the buffer happens to start with the
297 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
298 * magic number is only written to the log for descripter blocks. In
299 * this case, we copy the data and replace the first word with 0, and we
300 * return a result code which indicates that this buffer needs to be
301 * marked as an escaped buffer in the corresponding log descriptor
302 * block. The missing word can then be restored when the block is read
305 * If the source buffer has already been modified by a new transaction
306 * since we took the last commit snapshot, we use the frozen copy of
307 * that data for IO. If we end up using the existing buffer_head's data
308 * for the write, then we *have* to lock the buffer to prevent anyone
309 * else from using and possibly modifying it while the IO is in
312 * The function returns a pointer to the buffer_heads to be used for IO.
314 * We assume that the journal has already been locked in this function.
321 * Bit 0 set == escape performed on the data
322 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
325 int jbd2_journal_write_metadata_buffer(transaction_t
*transaction
,
326 struct journal_head
*jh_in
,
327 struct journal_head
**jh_out
,
328 unsigned long long blocknr
)
330 int need_copy_out
= 0;
331 int done_copy_out
= 0;
334 struct buffer_head
*new_bh
;
335 struct journal_head
*new_jh
;
336 struct page
*new_page
;
337 unsigned int new_offset
;
338 struct buffer_head
*bh_in
= jh2bh(jh_in
);
339 journal_t
*journal
= transaction
->t_journal
;
342 * The buffer really shouldn't be locked: only the current committing
343 * transaction is allowed to write it, so nobody else is allowed
346 * akpm: except if we're journalling data, and write() output is
347 * also part of a shared mapping, and another thread has
348 * decided to launch a writepage() against this buffer.
350 J_ASSERT_BH(bh_in
, buffer_jbddirty(bh_in
));
353 new_bh
= alloc_buffer_head(GFP_NOFS
);
356 * Failure is not an option, but __GFP_NOFAIL is going
357 * away; so we retry ourselves here.
359 congestion_wait(BLK_RW_ASYNC
, HZ
/50);
363 /* keep subsequent assertions sane */
365 init_buffer(new_bh
, NULL
, NULL
);
366 atomic_set(&new_bh
->b_count
, 1);
367 new_jh
= jbd2_journal_add_journal_head(new_bh
); /* This sleeps */
370 * If a new transaction has already done a buffer copy-out, then
371 * we use that version of the data for the commit.
373 jbd_lock_bh_state(bh_in
);
375 if (jh_in
->b_frozen_data
) {
377 new_page
= virt_to_page(jh_in
->b_frozen_data
);
378 new_offset
= offset_in_page(jh_in
->b_frozen_data
);
380 new_page
= jh2bh(jh_in
)->b_page
;
381 new_offset
= offset_in_page(jh2bh(jh_in
)->b_data
);
384 mapped_data
= kmap_atomic(new_page
);
386 * Fire data frozen trigger if data already wasn't frozen. Do this
387 * before checking for escaping, as the trigger may modify the magic
388 * offset. If a copy-out happens afterwards, it will have the correct
389 * data in the buffer.
392 jbd2_buffer_frozen_trigger(jh_in
, mapped_data
+ new_offset
,
398 if (*((__be32
*)(mapped_data
+ new_offset
)) ==
399 cpu_to_be32(JBD2_MAGIC_NUMBER
)) {
403 kunmap_atomic(mapped_data
);
406 * Do we need to do a data copy?
408 if (need_copy_out
&& !done_copy_out
) {
411 jbd_unlock_bh_state(bh_in
);
412 tmp
= jbd2_alloc(bh_in
->b_size
, GFP_NOFS
);
414 jbd2_journal_put_journal_head(new_jh
);
417 jbd_lock_bh_state(bh_in
);
418 if (jh_in
->b_frozen_data
) {
419 jbd2_free(tmp
, bh_in
->b_size
);
423 jh_in
->b_frozen_data
= tmp
;
424 mapped_data
= kmap_atomic(new_page
);
425 memcpy(tmp
, mapped_data
+ new_offset
, jh2bh(jh_in
)->b_size
);
426 kunmap_atomic(mapped_data
);
428 new_page
= virt_to_page(tmp
);
429 new_offset
= offset_in_page(tmp
);
433 * This isn't strictly necessary, as we're using frozen
434 * data for the escaping, but it keeps consistency with
435 * b_frozen_data usage.
437 jh_in
->b_frozen_triggers
= jh_in
->b_triggers
;
441 * Did we need to do an escaping? Now we've done all the
442 * copying, we can finally do so.
445 mapped_data
= kmap_atomic(new_page
);
446 *((unsigned int *)(mapped_data
+ new_offset
)) = 0;
447 kunmap_atomic(mapped_data
);
450 set_bh_page(new_bh
, new_page
, new_offset
);
451 new_jh
->b_transaction
= NULL
;
452 new_bh
->b_size
= jh2bh(jh_in
)->b_size
;
453 new_bh
->b_bdev
= transaction
->t_journal
->j_dev
;
454 new_bh
->b_blocknr
= blocknr
;
455 set_buffer_mapped(new_bh
);
456 set_buffer_dirty(new_bh
);
461 * The to-be-written buffer needs to get moved to the io queue,
462 * and the original buffer whose contents we are shadowing or
463 * copying is moved to the transaction's shadow queue.
465 JBUFFER_TRACE(jh_in
, "file as BJ_Shadow");
466 spin_lock(&journal
->j_list_lock
);
467 __jbd2_journal_file_buffer(jh_in
, transaction
, BJ_Shadow
);
468 spin_unlock(&journal
->j_list_lock
);
469 jbd_unlock_bh_state(bh_in
);
471 JBUFFER_TRACE(new_jh
, "file as BJ_IO");
472 jbd2_journal_file_buffer(new_jh
, transaction
, BJ_IO
);
474 return do_escape
| (done_copy_out
<< 1);
478 * Allocation code for the journal file. Manage the space left in the
479 * journal, so that we can begin checkpointing when appropriate.
483 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
485 * Called with the journal already locked.
487 * Called under j_state_lock
490 int __jbd2_log_space_left(journal_t
*journal
)
492 int left
= journal
->j_free
;
494 /* assert_spin_locked(&journal->j_state_lock); */
497 * Be pessimistic here about the number of those free blocks which
498 * might be required for log descriptor control blocks.
501 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
503 left
-= MIN_LOG_RESERVED_BLOCKS
;
512 * Called with j_state_lock locked for writing.
513 * Returns true if a transaction commit was started.
515 int __jbd2_log_start_commit(journal_t
*journal
, tid_t target
)
518 * The only transaction we can possibly wait upon is the
519 * currently running transaction (if it exists). Otherwise,
520 * the target tid must be an old one.
522 if (journal
->j_running_transaction
&&
523 journal
->j_running_transaction
->t_tid
== target
) {
525 * We want a new commit: OK, mark the request and wakeup the
526 * commit thread. We do _not_ do the commit ourselves.
529 journal
->j_commit_request
= target
;
530 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
531 journal
->j_commit_request
,
532 journal
->j_commit_sequence
);
533 wake_up(&journal
->j_wait_commit
);
535 } else if (!tid_geq(journal
->j_commit_request
, target
))
536 /* This should never happen, but if it does, preserve
537 the evidence before kjournald goes into a loop and
538 increments j_commit_sequence beyond all recognition. */
539 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
540 journal
->j_commit_request
,
541 journal
->j_commit_sequence
,
542 target
, journal
->j_running_transaction
?
543 journal
->j_running_transaction
->t_tid
: 0);
547 int jbd2_log_start_commit(journal_t
*journal
, tid_t tid
)
551 write_lock(&journal
->j_state_lock
);
552 ret
= __jbd2_log_start_commit(journal
, tid
);
553 write_unlock(&journal
->j_state_lock
);
558 * Force and wait upon a commit if the calling process is not within
559 * transaction. This is used for forcing out undo-protected data which contains
560 * bitmaps, when the fs is running out of space.
562 * We can only force the running transaction if we don't have an active handle;
563 * otherwise, we will deadlock.
565 * Returns true if a transaction was started.
567 int jbd2_journal_force_commit_nested(journal_t
*journal
)
569 transaction_t
*transaction
= NULL
;
571 int need_to_start
= 0;
573 read_lock(&journal
->j_state_lock
);
574 if (journal
->j_running_transaction
&& !current
->journal_info
) {
575 transaction
= journal
->j_running_transaction
;
576 if (!tid_geq(journal
->j_commit_request
, transaction
->t_tid
))
578 } else if (journal
->j_committing_transaction
)
579 transaction
= journal
->j_committing_transaction
;
582 read_unlock(&journal
->j_state_lock
);
583 return 0; /* Nothing to retry */
586 tid
= transaction
->t_tid
;
587 read_unlock(&journal
->j_state_lock
);
589 jbd2_log_start_commit(journal
, tid
);
590 jbd2_log_wait_commit(journal
, tid
);
595 * Start a commit of the current running transaction (if any). Returns true
596 * if a transaction is going to be committed (or is currently already
597 * committing), and fills its tid in at *ptid
599 int jbd2_journal_start_commit(journal_t
*journal
, tid_t
*ptid
)
603 write_lock(&journal
->j_state_lock
);
604 if (journal
->j_running_transaction
) {
605 tid_t tid
= journal
->j_running_transaction
->t_tid
;
607 __jbd2_log_start_commit(journal
, tid
);
608 /* There's a running transaction and we've just made sure
609 * it's commit has been scheduled. */
613 } else if (journal
->j_committing_transaction
) {
615 * If ext3_write_super() recently started a commit, then we
616 * have to wait for completion of that transaction
619 *ptid
= journal
->j_committing_transaction
->t_tid
;
622 write_unlock(&journal
->j_state_lock
);
627 * Return 1 if a given transaction has not yet sent barrier request
628 * connected with a transaction commit. If 0 is returned, transaction
629 * may or may not have sent the barrier. Used to avoid sending barrier
630 * twice in common cases.
632 int jbd2_trans_will_send_data_barrier(journal_t
*journal
, tid_t tid
)
635 transaction_t
*commit_trans
;
637 if (!(journal
->j_flags
& JBD2_BARRIER
))
639 read_lock(&journal
->j_state_lock
);
640 /* Transaction already committed? */
641 if (tid_geq(journal
->j_commit_sequence
, tid
))
643 commit_trans
= journal
->j_committing_transaction
;
644 if (!commit_trans
|| commit_trans
->t_tid
!= tid
) {
649 * Transaction is being committed and we already proceeded to
650 * submitting a flush to fs partition?
652 if (journal
->j_fs_dev
!= journal
->j_dev
) {
653 if (!commit_trans
->t_need_data_flush
||
654 commit_trans
->t_state
>= T_COMMIT_DFLUSH
)
657 if (commit_trans
->t_state
>= T_COMMIT_JFLUSH
)
662 read_unlock(&journal
->j_state_lock
);
665 EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier
);
668 * Wait for a specified commit to complete.
669 * The caller may not hold the journal lock.
671 int jbd2_log_wait_commit(journal_t
*journal
, tid_t tid
)
675 read_lock(&journal
->j_state_lock
);
676 #ifdef CONFIG_JBD2_DEBUG
677 if (!tid_geq(journal
->j_commit_request
, tid
)) {
679 "%s: error: j_commit_request=%d, tid=%d\n",
680 __func__
, journal
->j_commit_request
, tid
);
683 while (tid_gt(tid
, journal
->j_commit_sequence
)) {
684 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
685 tid
, journal
->j_commit_sequence
);
686 wake_up(&journal
->j_wait_commit
);
687 read_unlock(&journal
->j_state_lock
);
688 wait_event(journal
->j_wait_done_commit
,
689 !tid_gt(tid
, journal
->j_commit_sequence
));
690 read_lock(&journal
->j_state_lock
);
692 read_unlock(&journal
->j_state_lock
);
694 if (unlikely(is_journal_aborted(journal
))) {
695 printk(KERN_EMERG
"journal commit I/O error\n");
702 * Log buffer allocation routines:
705 int jbd2_journal_next_log_block(journal_t
*journal
, unsigned long long *retp
)
707 unsigned long blocknr
;
709 write_lock(&journal
->j_state_lock
);
710 J_ASSERT(journal
->j_free
> 1);
712 blocknr
= journal
->j_head
;
715 if (journal
->j_head
== journal
->j_last
)
716 journal
->j_head
= journal
->j_first
;
717 write_unlock(&journal
->j_state_lock
);
718 return jbd2_journal_bmap(journal
, blocknr
, retp
);
722 * Conversion of logical to physical block numbers for the journal
724 * On external journals the journal blocks are identity-mapped, so
725 * this is a no-op. If needed, we can use j_blk_offset - everything is
728 int jbd2_journal_bmap(journal_t
*journal
, unsigned long blocknr
,
729 unsigned long long *retp
)
732 unsigned long long ret
;
734 if (journal
->j_inode
) {
735 ret
= bmap(journal
->j_inode
, blocknr
);
739 printk(KERN_ALERT
"%s: journal block not found "
740 "at offset %lu on %s\n",
741 __func__
, blocknr
, journal
->j_devname
);
743 __journal_abort_soft(journal
, err
);
746 *retp
= blocknr
; /* +journal->j_blk_offset */
752 * We play buffer_head aliasing tricks to write data/metadata blocks to
753 * the journal without copying their contents, but for journal
754 * descriptor blocks we do need to generate bona fide buffers.
756 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
757 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
758 * But we don't bother doing that, so there will be coherency problems with
759 * mmaps of blockdevs which hold live JBD-controlled filesystems.
761 struct journal_head
*jbd2_journal_get_descriptor_buffer(journal_t
*journal
)
763 struct buffer_head
*bh
;
764 unsigned long long blocknr
;
767 err
= jbd2_journal_next_log_block(journal
, &blocknr
);
772 bh
= __getblk(journal
->j_dev
, blocknr
, journal
->j_blocksize
);
776 memset(bh
->b_data
, 0, journal
->j_blocksize
);
777 set_buffer_uptodate(bh
);
779 BUFFER_TRACE(bh
, "return this buffer");
780 return jbd2_journal_add_journal_head(bh
);
784 * Return tid of the oldest transaction in the journal and block in the journal
785 * where the transaction starts.
787 * If the journal is now empty, return which will be the next transaction ID
788 * we will write and where will that transaction start.
790 * The return value is 0 if journal tail cannot be pushed any further, 1 if
793 int jbd2_journal_get_log_tail(journal_t
*journal
, tid_t
*tid
,
794 unsigned long *block
)
796 transaction_t
*transaction
;
799 read_lock(&journal
->j_state_lock
);
800 spin_lock(&journal
->j_list_lock
);
801 transaction
= journal
->j_checkpoint_transactions
;
803 *tid
= transaction
->t_tid
;
804 *block
= transaction
->t_log_start
;
805 } else if ((transaction
= journal
->j_committing_transaction
) != NULL
) {
806 *tid
= transaction
->t_tid
;
807 *block
= transaction
->t_log_start
;
808 } else if ((transaction
= journal
->j_running_transaction
) != NULL
) {
809 *tid
= transaction
->t_tid
;
810 *block
= journal
->j_head
;
812 *tid
= journal
->j_transaction_sequence
;
813 *block
= journal
->j_head
;
815 ret
= tid_gt(*tid
, journal
->j_tail_sequence
);
816 spin_unlock(&journal
->j_list_lock
);
817 read_unlock(&journal
->j_state_lock
);
823 * Update information in journal structure and in on disk journal superblock
824 * about log tail. This function does not check whether information passed in
825 * really pushes log tail further. It's responsibility of the caller to make
826 * sure provided log tail information is valid (e.g. by holding
827 * j_checkpoint_mutex all the time between computing log tail and calling this
828 * function as is the case with jbd2_cleanup_journal_tail()).
830 * Requires j_checkpoint_mutex
832 void __jbd2_update_log_tail(journal_t
*journal
, tid_t tid
, unsigned long block
)
836 BUG_ON(!mutex_is_locked(&journal
->j_checkpoint_mutex
));
839 * We cannot afford for write to remain in drive's caches since as
840 * soon as we update j_tail, next transaction can start reusing journal
841 * space and if we lose sb update during power failure we'd replay
842 * old transaction with possibly newly overwritten data.
844 jbd2_journal_update_sb_log_tail(journal
, tid
, block
, WRITE_FUA
);
845 write_lock(&journal
->j_state_lock
);
846 freed
= block
- journal
->j_tail
;
847 if (block
< journal
->j_tail
)
848 freed
+= journal
->j_last
- journal
->j_first
;
850 trace_jbd2_update_log_tail(journal
, tid
, block
, freed
);
852 "Cleaning journal tail from %d to %d (offset %lu), "
854 journal
->j_tail_sequence
, tid
, block
, freed
);
856 journal
->j_free
+= freed
;
857 journal
->j_tail_sequence
= tid
;
858 journal
->j_tail
= block
;
859 write_unlock(&journal
->j_state_lock
);
863 * This is a variaon of __jbd2_update_log_tail which checks for validity of
864 * provided log tail and locks j_checkpoint_mutex. So it is safe against races
865 * with other threads updating log tail.
867 void jbd2_update_log_tail(journal_t
*journal
, tid_t tid
, unsigned long block
)
869 mutex_lock(&journal
->j_checkpoint_mutex
);
870 if (tid_gt(tid
, journal
->j_tail_sequence
))
871 __jbd2_update_log_tail(journal
, tid
, block
);
872 mutex_unlock(&journal
->j_checkpoint_mutex
);
875 struct jbd2_stats_proc_session
{
877 struct transaction_stats_s
*stats
;
882 static void *jbd2_seq_info_start(struct seq_file
*seq
, loff_t
*pos
)
884 return *pos
? NULL
: SEQ_START_TOKEN
;
887 static void *jbd2_seq_info_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
892 static int jbd2_seq_info_show(struct seq_file
*seq
, void *v
)
894 struct jbd2_stats_proc_session
*s
= seq
->private;
896 if (v
!= SEQ_START_TOKEN
)
898 seq_printf(seq
, "%lu transaction, each up to %u blocks\n",
900 s
->journal
->j_max_transaction_buffers
);
901 if (s
->stats
->ts_tid
== 0)
903 seq_printf(seq
, "average: \n %ums waiting for transaction\n",
904 jiffies_to_msecs(s
->stats
->run
.rs_wait
/ s
->stats
->ts_tid
));
905 seq_printf(seq
, " %ums running transaction\n",
906 jiffies_to_msecs(s
->stats
->run
.rs_running
/ s
->stats
->ts_tid
));
907 seq_printf(seq
, " %ums transaction was being locked\n",
908 jiffies_to_msecs(s
->stats
->run
.rs_locked
/ s
->stats
->ts_tid
));
909 seq_printf(seq
, " %ums flushing data (in ordered mode)\n",
910 jiffies_to_msecs(s
->stats
->run
.rs_flushing
/ s
->stats
->ts_tid
));
911 seq_printf(seq
, " %ums logging transaction\n",
912 jiffies_to_msecs(s
->stats
->run
.rs_logging
/ s
->stats
->ts_tid
));
913 seq_printf(seq
, " %lluus average transaction commit time\n",
914 div_u64(s
->journal
->j_average_commit_time
, 1000));
915 seq_printf(seq
, " %lu handles per transaction\n",
916 s
->stats
->run
.rs_handle_count
/ s
->stats
->ts_tid
);
917 seq_printf(seq
, " %lu blocks per transaction\n",
918 s
->stats
->run
.rs_blocks
/ s
->stats
->ts_tid
);
919 seq_printf(seq
, " %lu logged blocks per transaction\n",
920 s
->stats
->run
.rs_blocks_logged
/ s
->stats
->ts_tid
);
924 static void jbd2_seq_info_stop(struct seq_file
*seq
, void *v
)
928 static const struct seq_operations jbd2_seq_info_ops
= {
929 .start
= jbd2_seq_info_start
,
930 .next
= jbd2_seq_info_next
,
931 .stop
= jbd2_seq_info_stop
,
932 .show
= jbd2_seq_info_show
,
935 static int jbd2_seq_info_open(struct inode
*inode
, struct file
*file
)
937 journal_t
*journal
= PDE(inode
)->data
;
938 struct jbd2_stats_proc_session
*s
;
941 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
944 size
= sizeof(struct transaction_stats_s
);
945 s
->stats
= kmalloc(size
, GFP_KERNEL
);
946 if (s
->stats
== NULL
) {
950 spin_lock(&journal
->j_history_lock
);
951 memcpy(s
->stats
, &journal
->j_stats
, size
);
952 s
->journal
= journal
;
953 spin_unlock(&journal
->j_history_lock
);
955 rc
= seq_open(file
, &jbd2_seq_info_ops
);
957 struct seq_file
*m
= file
->private_data
;
967 static int jbd2_seq_info_release(struct inode
*inode
, struct file
*file
)
969 struct seq_file
*seq
= file
->private_data
;
970 struct jbd2_stats_proc_session
*s
= seq
->private;
973 return seq_release(inode
, file
);
976 static const struct file_operations jbd2_seq_info_fops
= {
977 .owner
= THIS_MODULE
,
978 .open
= jbd2_seq_info_open
,
981 .release
= jbd2_seq_info_release
,
984 static struct proc_dir_entry
*proc_jbd2_stats
;
986 static void jbd2_stats_proc_init(journal_t
*journal
)
988 journal
->j_proc_entry
= proc_mkdir(journal
->j_devname
, proc_jbd2_stats
);
989 if (journal
->j_proc_entry
) {
990 proc_create_data("info", S_IRUGO
, journal
->j_proc_entry
,
991 &jbd2_seq_info_fops
, journal
);
995 static void jbd2_stats_proc_exit(journal_t
*journal
)
997 remove_proc_entry("info", journal
->j_proc_entry
);
998 remove_proc_entry(journal
->j_devname
, proc_jbd2_stats
);
1002 * Management for journal control blocks: functions to create and
1003 * destroy journal_t structures, and to initialise and read existing
1004 * journal blocks from disk. */
1006 /* First: create and setup a journal_t object in memory. We initialise
1007 * very few fields yet: that has to wait until we have created the
1008 * journal structures from from scratch, or loaded them from disk. */
1010 static journal_t
* journal_init_common (void)
1015 journal
= kzalloc(sizeof(*journal
), GFP_KERNEL
);
1019 init_waitqueue_head(&journal
->j_wait_transaction_locked
);
1020 init_waitqueue_head(&journal
->j_wait_logspace
);
1021 init_waitqueue_head(&journal
->j_wait_done_commit
);
1022 init_waitqueue_head(&journal
->j_wait_checkpoint
);
1023 init_waitqueue_head(&journal
->j_wait_commit
);
1024 init_waitqueue_head(&journal
->j_wait_updates
);
1025 mutex_init(&journal
->j_barrier
);
1026 mutex_init(&journal
->j_checkpoint_mutex
);
1027 spin_lock_init(&journal
->j_revoke_lock
);
1028 spin_lock_init(&journal
->j_list_lock
);
1029 rwlock_init(&journal
->j_state_lock
);
1031 journal
->j_commit_interval
= (HZ
* JBD2_DEFAULT_MAX_COMMIT_AGE
);
1032 journal
->j_min_batch_time
= 0;
1033 journal
->j_max_batch_time
= 15000; /* 15ms */
1035 /* The journal is marked for error until we succeed with recovery! */
1036 journal
->j_flags
= JBD2_ABORT
;
1038 /* Set up a default-sized revoke table for the new mount. */
1039 err
= jbd2_journal_init_revoke(journal
, JOURNAL_REVOKE_DEFAULT_HASH
);
1045 spin_lock_init(&journal
->j_history_lock
);
1050 /* jbd2_journal_init_dev and jbd2_journal_init_inode:
1052 * Create a journal structure assigned some fixed set of disk blocks to
1053 * the journal. We don't actually touch those disk blocks yet, but we
1054 * need to set up all of the mapping information to tell the journaling
1055 * system where the journal blocks are.
1060 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
1061 * @bdev: Block device on which to create the journal
1062 * @fs_dev: Device which hold journalled filesystem for this journal.
1063 * @start: Block nr Start of journal.
1064 * @len: Length of the journal in blocks.
1065 * @blocksize: blocksize of journalling device
1067 * Returns: a newly created journal_t *
1069 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
1070 * range of blocks on an arbitrary block device.
1073 journal_t
* jbd2_journal_init_dev(struct block_device
*bdev
,
1074 struct block_device
*fs_dev
,
1075 unsigned long long start
, int len
, int blocksize
)
1077 journal_t
*journal
= journal_init_common();
1078 struct buffer_head
*bh
;
1085 /* journal descriptor can store up to n blocks -bzzz */
1086 journal
->j_blocksize
= blocksize
;
1087 journal
->j_dev
= bdev
;
1088 journal
->j_fs_dev
= fs_dev
;
1089 journal
->j_blk_offset
= start
;
1090 journal
->j_maxlen
= len
;
1091 bdevname(journal
->j_dev
, journal
->j_devname
);
1092 p
= journal
->j_devname
;
1093 while ((p
= strchr(p
, '/')))
1095 jbd2_stats_proc_init(journal
);
1096 n
= journal
->j_blocksize
/ sizeof(journal_block_tag_t
);
1097 journal
->j_wbufsize
= n
;
1098 journal
->j_wbuf
= kmalloc(n
* sizeof(struct buffer_head
*), GFP_KERNEL
);
1099 if (!journal
->j_wbuf
) {
1100 printk(KERN_ERR
"%s: Can't allocate bhs for commit thread\n",
1105 bh
= __getblk(journal
->j_dev
, start
, journal
->j_blocksize
);
1108 "%s: Cannot get buffer for journal superblock\n",
1112 journal
->j_sb_buffer
= bh
;
1113 journal
->j_superblock
= (journal_superblock_t
*)bh
->b_data
;
1117 kfree(journal
->j_wbuf
);
1118 jbd2_stats_proc_exit(journal
);
1124 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
1125 * @inode: An inode to create the journal in
1127 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
1128 * the journal. The inode must exist already, must support bmap() and
1129 * must have all data blocks preallocated.
1131 journal_t
* jbd2_journal_init_inode (struct inode
*inode
)
1133 struct buffer_head
*bh
;
1134 journal_t
*journal
= journal_init_common();
1138 unsigned long long blocknr
;
1143 journal
->j_dev
= journal
->j_fs_dev
= inode
->i_sb
->s_bdev
;
1144 journal
->j_inode
= inode
;
1145 bdevname(journal
->j_dev
, journal
->j_devname
);
1146 p
= journal
->j_devname
;
1147 while ((p
= strchr(p
, '/')))
1149 p
= journal
->j_devname
+ strlen(journal
->j_devname
);
1150 sprintf(p
, "-%lu", journal
->j_inode
->i_ino
);
1152 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1153 journal
, inode
->i_sb
->s_id
, inode
->i_ino
,
1154 (long long) inode
->i_size
,
1155 inode
->i_sb
->s_blocksize_bits
, inode
->i_sb
->s_blocksize
);
1157 journal
->j_maxlen
= inode
->i_size
>> inode
->i_sb
->s_blocksize_bits
;
1158 journal
->j_blocksize
= inode
->i_sb
->s_blocksize
;
1159 jbd2_stats_proc_init(journal
);
1161 /* journal descriptor can store up to n blocks -bzzz */
1162 n
= journal
->j_blocksize
/ sizeof(journal_block_tag_t
);
1163 journal
->j_wbufsize
= n
;
1164 journal
->j_wbuf
= kmalloc(n
* sizeof(struct buffer_head
*), GFP_KERNEL
);
1165 if (!journal
->j_wbuf
) {
1166 printk(KERN_ERR
"%s: Can't allocate bhs for commit thread\n",
1171 err
= jbd2_journal_bmap(journal
, 0, &blocknr
);
1172 /* If that failed, give up */
1174 printk(KERN_ERR
"%s: Cannot locate journal superblock\n",
1179 bh
= __getblk(journal
->j_dev
, blocknr
, journal
->j_blocksize
);
1182 "%s: Cannot get buffer for journal superblock\n",
1186 journal
->j_sb_buffer
= bh
;
1187 journal
->j_superblock
= (journal_superblock_t
*)bh
->b_data
;
1191 kfree(journal
->j_wbuf
);
1192 jbd2_stats_proc_exit(journal
);
1198 * If the journal init or create aborts, we need to mark the journal
1199 * superblock as being NULL to prevent the journal destroy from writing
1200 * back a bogus superblock.
1202 static void journal_fail_superblock (journal_t
*journal
)
1204 struct buffer_head
*bh
= journal
->j_sb_buffer
;
1206 journal
->j_sb_buffer
= NULL
;
1210 * Given a journal_t structure, initialise the various fields for
1211 * startup of a new journaling session. We use this both when creating
1212 * a journal, and after recovering an old journal to reset it for
1216 static int journal_reset(journal_t
*journal
)
1218 journal_superblock_t
*sb
= journal
->j_superblock
;
1219 unsigned long long first
, last
;
1221 first
= be32_to_cpu(sb
->s_first
);
1222 last
= be32_to_cpu(sb
->s_maxlen
);
1223 if (first
+ JBD2_MIN_JOURNAL_BLOCKS
> last
+ 1) {
1224 printk(KERN_ERR
"JBD2: Journal too short (blocks %llu-%llu).\n",
1226 journal_fail_superblock(journal
);
1230 journal
->j_first
= first
;
1231 journal
->j_last
= last
;
1233 journal
->j_head
= first
;
1234 journal
->j_tail
= first
;
1235 journal
->j_free
= last
- first
;
1237 journal
->j_tail_sequence
= journal
->j_transaction_sequence
;
1238 journal
->j_commit_sequence
= journal
->j_transaction_sequence
- 1;
1239 journal
->j_commit_request
= journal
->j_commit_sequence
;
1241 journal
->j_max_transaction_buffers
= journal
->j_maxlen
/ 4;
1244 * As a special case, if the on-disk copy is already marked as needing
1245 * no recovery (s_start == 0), then we can safely defer the superblock
1246 * update until the next commit by setting JBD2_FLUSHED. This avoids
1247 * attempting a write to a potential-readonly device.
1249 if (sb
->s_start
== 0) {
1250 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
1251 "(start %ld, seq %d, errno %d)\n",
1252 journal
->j_tail
, journal
->j_tail_sequence
,
1254 journal
->j_flags
|= JBD2_FLUSHED
;
1256 /* Lock here to make assertions happy... */
1257 mutex_lock(&journal
->j_checkpoint_mutex
);
1259 * Update log tail information. We use WRITE_FUA since new
1260 * transaction will start reusing journal space and so we
1261 * must make sure information about current log tail is on
1264 jbd2_journal_update_sb_log_tail(journal
,
1265 journal
->j_tail_sequence
,
1268 mutex_unlock(&journal
->j_checkpoint_mutex
);
1270 return jbd2_journal_start_thread(journal
);
1273 static void jbd2_write_superblock(journal_t
*journal
, int write_op
)
1275 struct buffer_head
*bh
= journal
->j_sb_buffer
;
1278 trace_jbd2_write_superblock(journal
, write_op
);
1279 if (!(journal
->j_flags
& JBD2_BARRIER
))
1280 write_op
&= ~(REQ_FUA
| REQ_FLUSH
);
1282 if (buffer_write_io_error(bh
)) {
1284 * Oh, dear. A previous attempt to write the journal
1285 * superblock failed. This could happen because the
1286 * USB device was yanked out. Or it could happen to
1287 * be a transient write error and maybe the block will
1288 * be remapped. Nothing we can do but to retry the
1289 * write and hope for the best.
1291 printk(KERN_ERR
"JBD2: previous I/O error detected "
1292 "for journal superblock update for %s.\n",
1293 journal
->j_devname
);
1294 clear_buffer_write_io_error(bh
);
1295 set_buffer_uptodate(bh
);
1298 bh
->b_end_io
= end_buffer_write_sync
;
1299 ret
= submit_bh(write_op
, bh
);
1301 if (buffer_write_io_error(bh
)) {
1302 clear_buffer_write_io_error(bh
);
1303 set_buffer_uptodate(bh
);
1307 printk(KERN_ERR
"JBD2: Error %d detected when updating "
1308 "journal superblock for %s.\n", ret
,
1309 journal
->j_devname
);
1314 * jbd2_journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1315 * @journal: The journal to update.
1316 * @tail_tid: TID of the new transaction at the tail of the log
1317 * @tail_block: The first block of the transaction at the tail of the log
1318 * @write_op: With which operation should we write the journal sb
1320 * Update a journal's superblock information about log tail and write it to
1321 * disk, waiting for the IO to complete.
1323 void jbd2_journal_update_sb_log_tail(journal_t
*journal
, tid_t tail_tid
,
1324 unsigned long tail_block
, int write_op
)
1326 journal_superblock_t
*sb
= journal
->j_superblock
;
1328 BUG_ON(!mutex_is_locked(&journal
->j_checkpoint_mutex
));
1329 jbd_debug(1, "JBD2: updating superblock (start %lu, seq %u)\n",
1330 tail_block
, tail_tid
);
1332 sb
->s_sequence
= cpu_to_be32(tail_tid
);
1333 sb
->s_start
= cpu_to_be32(tail_block
);
1335 jbd2_write_superblock(journal
, write_op
);
1337 /* Log is no longer empty */
1338 write_lock(&journal
->j_state_lock
);
1339 WARN_ON(!sb
->s_sequence
);
1340 journal
->j_flags
&= ~JBD2_FLUSHED
;
1341 write_unlock(&journal
->j_state_lock
);
1345 * jbd2_mark_journal_empty() - Mark on disk journal as empty.
1346 * @journal: The journal to update.
1348 * Update a journal's dynamic superblock fields to show that journal is empty.
1349 * Write updated superblock to disk waiting for IO to complete.
1351 static void jbd2_mark_journal_empty(journal_t
*journal
)
1353 journal_superblock_t
*sb
= journal
->j_superblock
;
1355 BUG_ON(!mutex_is_locked(&journal
->j_checkpoint_mutex
));
1356 read_lock(&journal
->j_state_lock
);
1357 jbd_debug(1, "JBD2: Marking journal as empty (seq %d)\n",
1358 journal
->j_tail_sequence
);
1360 sb
->s_sequence
= cpu_to_be32(journal
->j_tail_sequence
);
1361 sb
->s_start
= cpu_to_be32(0);
1362 read_unlock(&journal
->j_state_lock
);
1364 jbd2_write_superblock(journal
, WRITE_FUA
);
1366 /* Log is no longer empty */
1367 write_lock(&journal
->j_state_lock
);
1368 journal
->j_flags
|= JBD2_FLUSHED
;
1369 write_unlock(&journal
->j_state_lock
);
1374 * jbd2_journal_update_sb_errno() - Update error in the journal.
1375 * @journal: The journal to update.
1377 * Update a journal's errno. Write updated superblock to disk waiting for IO
1380 static void jbd2_journal_update_sb_errno(journal_t
*journal
)
1382 journal_superblock_t
*sb
= journal
->j_superblock
;
1384 read_lock(&journal
->j_state_lock
);
1385 jbd_debug(1, "JBD2: updating superblock error (errno %d)\n",
1387 sb
->s_errno
= cpu_to_be32(journal
->j_errno
);
1388 jbd2_superblock_csum_set(journal
, sb
);
1389 read_unlock(&journal
->j_state_lock
);
1391 jbd2_write_superblock(journal
, WRITE_SYNC
);
1395 * Read the superblock for a given journal, performing initial
1396 * validation of the format.
1398 static int journal_get_superblock(journal_t
*journal
)
1400 struct buffer_head
*bh
;
1401 journal_superblock_t
*sb
;
1404 bh
= journal
->j_sb_buffer
;
1406 J_ASSERT(bh
!= NULL
);
1407 if (!buffer_uptodate(bh
)) {
1408 ll_rw_block(READ
, 1, &bh
);
1410 if (!buffer_uptodate(bh
)) {
1412 "JBD2: IO error reading journal superblock\n");
1417 if (buffer_verified(bh
))
1420 sb
= journal
->j_superblock
;
1424 if (sb
->s_header
.h_magic
!= cpu_to_be32(JBD2_MAGIC_NUMBER
) ||
1425 sb
->s_blocksize
!= cpu_to_be32(journal
->j_blocksize
)) {
1426 printk(KERN_WARNING
"JBD2: no valid journal superblock found\n");
1430 switch(be32_to_cpu(sb
->s_header
.h_blocktype
)) {
1431 case JBD2_SUPERBLOCK_V1
:
1432 journal
->j_format_version
= 1;
1434 case JBD2_SUPERBLOCK_V2
:
1435 journal
->j_format_version
= 2;
1438 printk(KERN_WARNING
"JBD2: unrecognised superblock format ID\n");
1442 if (be32_to_cpu(sb
->s_maxlen
) < journal
->j_maxlen
)
1443 journal
->j_maxlen
= be32_to_cpu(sb
->s_maxlen
);
1444 else if (be32_to_cpu(sb
->s_maxlen
) > journal
->j_maxlen
) {
1445 printk(KERN_WARNING
"JBD2: journal file too short\n");
1449 if (be32_to_cpu(sb
->s_first
) == 0 ||
1450 be32_to_cpu(sb
->s_first
) >= journal
->j_maxlen
) {
1452 "JBD2: Invalid start block of journal: %u\n",
1453 be32_to_cpu(sb
->s_first
));
1457 if (JBD2_HAS_COMPAT_FEATURE(journal
, JBD2_FEATURE_COMPAT_CHECKSUM
) &&
1458 JBD2_HAS_INCOMPAT_FEATURE(journal
, JBD2_FEATURE_INCOMPAT_CSUM_V2
)) {
1459 /* Can't have checksum v1 and v2 on at the same time! */
1460 printk(KERN_ERR
"JBD: Can't enable checksumming v1 and v2 "
1461 "at the same time!\n");
1465 if (!jbd2_verify_csum_type(journal
, sb
)) {
1466 printk(KERN_ERR
"JBD: Unknown checksum type\n");
1470 /* Load the checksum driver */
1471 if (JBD2_HAS_INCOMPAT_FEATURE(journal
, JBD2_FEATURE_INCOMPAT_CSUM_V2
)) {
1472 journal
->j_chksum_driver
= crypto_alloc_shash("crc32c", 0, 0);
1473 if (IS_ERR(journal
->j_chksum_driver
)) {
1474 printk(KERN_ERR
"JBD: Cannot load crc32c driver.\n");
1475 err
= PTR_ERR(journal
->j_chksum_driver
);
1476 journal
->j_chksum_driver
= NULL
;
1481 /* Check superblock checksum */
1482 if (!jbd2_superblock_csum_verify(journal
, sb
)) {
1483 printk(KERN_ERR
"JBD: journal checksum error\n");
1487 /* Precompute checksum seed for all metadata */
1488 if (JBD2_HAS_INCOMPAT_FEATURE(journal
, JBD2_FEATURE_INCOMPAT_CSUM_V2
))
1489 journal
->j_csum_seed
= jbd2_chksum(journal
, ~0, sb
->s_uuid
,
1490 sizeof(sb
->s_uuid
));
1492 set_buffer_verified(bh
);
1497 journal_fail_superblock(journal
);
1502 * Load the on-disk journal superblock and read the key fields into the
1506 static int load_superblock(journal_t
*journal
)
1509 journal_superblock_t
*sb
;
1511 err
= journal_get_superblock(journal
);
1515 sb
= journal
->j_superblock
;
1517 journal
->j_tail_sequence
= be32_to_cpu(sb
->s_sequence
);
1518 journal
->j_tail
= be32_to_cpu(sb
->s_start
);
1519 journal
->j_first
= be32_to_cpu(sb
->s_first
);
1520 journal
->j_last
= be32_to_cpu(sb
->s_maxlen
);
1521 journal
->j_errno
= be32_to_cpu(sb
->s_errno
);
1528 * int jbd2_journal_load() - Read journal from disk.
1529 * @journal: Journal to act on.
1531 * Given a journal_t structure which tells us which disk blocks contain
1532 * a journal, read the journal from disk to initialise the in-memory
1535 int jbd2_journal_load(journal_t
*journal
)
1538 journal_superblock_t
*sb
;
1540 err
= load_superblock(journal
);
1544 sb
= journal
->j_superblock
;
1545 /* If this is a V2 superblock, then we have to check the
1546 * features flags on it. */
1548 if (journal
->j_format_version
>= 2) {
1549 if ((sb
->s_feature_ro_compat
&
1550 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES
)) ||
1551 (sb
->s_feature_incompat
&
1552 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES
))) {
1554 "JBD2: Unrecognised features on journal\n");
1560 * Create a slab for this blocksize
1562 err
= jbd2_journal_create_slab(be32_to_cpu(sb
->s_blocksize
));
1566 /* Let the recovery code check whether it needs to recover any
1567 * data from the journal. */
1568 if (jbd2_journal_recover(journal
))
1569 goto recovery_error
;
1571 if (journal
->j_failed_commit
) {
1572 printk(KERN_ERR
"JBD2: journal transaction %u on %s "
1573 "is corrupt.\n", journal
->j_failed_commit
,
1574 journal
->j_devname
);
1578 /* OK, we've finished with the dynamic journal bits:
1579 * reinitialise the dynamic contents of the superblock in memory
1580 * and reset them on disk. */
1581 if (journal_reset(journal
))
1582 goto recovery_error
;
1584 journal
->j_flags
&= ~JBD2_ABORT
;
1585 journal
->j_flags
|= JBD2_LOADED
;
1589 printk(KERN_WARNING
"JBD2: recovery failed\n");
1594 * void jbd2_journal_destroy() - Release a journal_t structure.
1595 * @journal: Journal to act on.
1597 * Release a journal_t structure once it is no longer in use by the
1599 * Return <0 if we couldn't clean up the journal.
1601 int jbd2_journal_destroy(journal_t
*journal
)
1605 /* Wait for the commit thread to wake up and die. */
1606 journal_kill_thread(journal
);
1608 /* Force a final log commit */
1609 if (journal
->j_running_transaction
)
1610 jbd2_journal_commit_transaction(journal
);
1612 /* Force any old transactions to disk */
1614 /* Totally anal locking here... */
1615 spin_lock(&journal
->j_list_lock
);
1616 while (journal
->j_checkpoint_transactions
!= NULL
) {
1617 spin_unlock(&journal
->j_list_lock
);
1618 mutex_lock(&journal
->j_checkpoint_mutex
);
1619 jbd2_log_do_checkpoint(journal
);
1620 mutex_unlock(&journal
->j_checkpoint_mutex
);
1621 spin_lock(&journal
->j_list_lock
);
1624 J_ASSERT(journal
->j_running_transaction
== NULL
);
1625 J_ASSERT(journal
->j_committing_transaction
== NULL
);
1626 J_ASSERT(journal
->j_checkpoint_transactions
== NULL
);
1627 spin_unlock(&journal
->j_list_lock
);
1629 if (journal
->j_sb_buffer
) {
1630 if (!is_journal_aborted(journal
)) {
1631 mutex_lock(&journal
->j_checkpoint_mutex
);
1632 jbd2_mark_journal_empty(journal
);
1633 mutex_unlock(&journal
->j_checkpoint_mutex
);
1636 brelse(journal
->j_sb_buffer
);
1639 if (journal
->j_proc_entry
)
1640 jbd2_stats_proc_exit(journal
);
1641 if (journal
->j_inode
)
1642 iput(journal
->j_inode
);
1643 if (journal
->j_revoke
)
1644 jbd2_journal_destroy_revoke(journal
);
1645 if (journal
->j_chksum_driver
)
1646 crypto_free_shash(journal
->j_chksum_driver
);
1647 kfree(journal
->j_wbuf
);
1655 *int jbd2_journal_check_used_features () - Check if features specified are used.
1656 * @journal: Journal to check.
1657 * @compat: bitmask of compatible features
1658 * @ro: bitmask of features that force read-only mount
1659 * @incompat: bitmask of incompatible features
1661 * Check whether the journal uses all of a given set of
1662 * features. Return true (non-zero) if it does.
1665 int jbd2_journal_check_used_features (journal_t
*journal
, unsigned long compat
,
1666 unsigned long ro
, unsigned long incompat
)
1668 journal_superblock_t
*sb
;
1670 if (!compat
&& !ro
&& !incompat
)
1672 /* Load journal superblock if it is not loaded yet. */
1673 if (journal
->j_format_version
== 0 &&
1674 journal_get_superblock(journal
) != 0)
1676 if (journal
->j_format_version
== 1)
1679 sb
= journal
->j_superblock
;
1681 if (((be32_to_cpu(sb
->s_feature_compat
) & compat
) == compat
) &&
1682 ((be32_to_cpu(sb
->s_feature_ro_compat
) & ro
) == ro
) &&
1683 ((be32_to_cpu(sb
->s_feature_incompat
) & incompat
) == incompat
))
1690 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
1691 * @journal: Journal to check.
1692 * @compat: bitmask of compatible features
1693 * @ro: bitmask of features that force read-only mount
1694 * @incompat: bitmask of incompatible features
1696 * Check whether the journaling code supports the use of
1697 * all of a given set of features on this journal. Return true
1698 * (non-zero) if it can. */
1700 int jbd2_journal_check_available_features (journal_t
*journal
, unsigned long compat
,
1701 unsigned long ro
, unsigned long incompat
)
1703 if (!compat
&& !ro
&& !incompat
)
1706 /* We can support any known requested features iff the
1707 * superblock is in version 2. Otherwise we fail to support any
1708 * extended sb features. */
1710 if (journal
->j_format_version
!= 2)
1713 if ((compat
& JBD2_KNOWN_COMPAT_FEATURES
) == compat
&&
1714 (ro
& JBD2_KNOWN_ROCOMPAT_FEATURES
) == ro
&&
1715 (incompat
& JBD2_KNOWN_INCOMPAT_FEATURES
) == incompat
)
1722 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
1723 * @journal: Journal to act on.
1724 * @compat: bitmask of compatible features
1725 * @ro: bitmask of features that force read-only mount
1726 * @incompat: bitmask of incompatible features
1728 * Mark a given journal feature as present on the
1729 * superblock. Returns true if the requested features could be set.
1733 int jbd2_journal_set_features (journal_t
*journal
, unsigned long compat
,
1734 unsigned long ro
, unsigned long incompat
)
1736 #define INCOMPAT_FEATURE_ON(f) \
1737 ((incompat & (f)) && !(sb->s_feature_incompat & cpu_to_be32(f)))
1738 #define COMPAT_FEATURE_ON(f) \
1739 ((compat & (f)) && !(sb->s_feature_compat & cpu_to_be32(f)))
1740 journal_superblock_t
*sb
;
1742 if (jbd2_journal_check_used_features(journal
, compat
, ro
, incompat
))
1745 if (!jbd2_journal_check_available_features(journal
, compat
, ro
, incompat
))
1748 /* Asking for checksumming v2 and v1? Only give them v2. */
1749 if (incompat
& JBD2_FEATURE_INCOMPAT_CSUM_V2
&&
1750 compat
& JBD2_FEATURE_COMPAT_CHECKSUM
)
1751 compat
&= ~JBD2_FEATURE_COMPAT_CHECKSUM
;
1753 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1754 compat
, ro
, incompat
);
1756 sb
= journal
->j_superblock
;
1758 /* If enabling v2 checksums, update superblock */
1759 if (INCOMPAT_FEATURE_ON(JBD2_FEATURE_INCOMPAT_CSUM_V2
)) {
1760 sb
->s_checksum_type
= JBD2_CRC32C_CHKSUM
;
1761 sb
->s_feature_compat
&=
1762 ~cpu_to_be32(JBD2_FEATURE_COMPAT_CHECKSUM
);
1764 /* Load the checksum driver */
1765 if (journal
->j_chksum_driver
== NULL
) {
1766 journal
->j_chksum_driver
= crypto_alloc_shash("crc32c",
1768 if (IS_ERR(journal
->j_chksum_driver
)) {
1769 printk(KERN_ERR
"JBD: Cannot load crc32c "
1771 journal
->j_chksum_driver
= NULL
;
1776 /* Precompute checksum seed for all metadata */
1777 if (JBD2_HAS_INCOMPAT_FEATURE(journal
,
1778 JBD2_FEATURE_INCOMPAT_CSUM_V2
))
1779 journal
->j_csum_seed
= jbd2_chksum(journal
, ~0,
1781 sizeof(sb
->s_uuid
));
1784 /* If enabling v1 checksums, downgrade superblock */
1785 if (COMPAT_FEATURE_ON(JBD2_FEATURE_COMPAT_CHECKSUM
))
1786 sb
->s_feature_incompat
&=
1787 ~cpu_to_be32(JBD2_FEATURE_INCOMPAT_CSUM_V2
);
1789 sb
->s_feature_compat
|= cpu_to_be32(compat
);
1790 sb
->s_feature_ro_compat
|= cpu_to_be32(ro
);
1791 sb
->s_feature_incompat
|= cpu_to_be32(incompat
);
1794 #undef COMPAT_FEATURE_ON
1795 #undef INCOMPAT_FEATURE_ON
1799 * jbd2_journal_clear_features () - Clear a given journal feature in the
1801 * @journal: Journal to act on.
1802 * @compat: bitmask of compatible features
1803 * @ro: bitmask of features that force read-only mount
1804 * @incompat: bitmask of incompatible features
1806 * Clear a given journal feature as present on the
1809 void jbd2_journal_clear_features(journal_t
*journal
, unsigned long compat
,
1810 unsigned long ro
, unsigned long incompat
)
1812 journal_superblock_t
*sb
;
1814 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1815 compat
, ro
, incompat
);
1817 sb
= journal
->j_superblock
;
1819 sb
->s_feature_compat
&= ~cpu_to_be32(compat
);
1820 sb
->s_feature_ro_compat
&= ~cpu_to_be32(ro
);
1821 sb
->s_feature_incompat
&= ~cpu_to_be32(incompat
);
1823 EXPORT_SYMBOL(jbd2_journal_clear_features
);
1826 * int jbd2_journal_flush () - Flush journal
1827 * @journal: Journal to act on.
1829 * Flush all data for a given journal to disk and empty the journal.
1830 * Filesystems can use this when remounting readonly to ensure that
1831 * recovery does not need to happen on remount.
1834 int jbd2_journal_flush(journal_t
*journal
)
1837 transaction_t
*transaction
= NULL
;
1839 write_lock(&journal
->j_state_lock
);
1841 /* Force everything buffered to the log... */
1842 if (journal
->j_running_transaction
) {
1843 transaction
= journal
->j_running_transaction
;
1844 __jbd2_log_start_commit(journal
, transaction
->t_tid
);
1845 } else if (journal
->j_committing_transaction
)
1846 transaction
= journal
->j_committing_transaction
;
1848 /* Wait for the log commit to complete... */
1850 tid_t tid
= transaction
->t_tid
;
1852 write_unlock(&journal
->j_state_lock
);
1853 jbd2_log_wait_commit(journal
, tid
);
1855 write_unlock(&journal
->j_state_lock
);
1858 /* ...and flush everything in the log out to disk. */
1859 spin_lock(&journal
->j_list_lock
);
1860 while (!err
&& journal
->j_checkpoint_transactions
!= NULL
) {
1861 spin_unlock(&journal
->j_list_lock
);
1862 mutex_lock(&journal
->j_checkpoint_mutex
);
1863 err
= jbd2_log_do_checkpoint(journal
);
1864 mutex_unlock(&journal
->j_checkpoint_mutex
);
1865 spin_lock(&journal
->j_list_lock
);
1867 spin_unlock(&journal
->j_list_lock
);
1869 if (is_journal_aborted(journal
))
1872 mutex_lock(&journal
->j_checkpoint_mutex
);
1873 jbd2_cleanup_journal_tail(journal
);
1875 /* Finally, mark the journal as really needing no recovery.
1876 * This sets s_start==0 in the underlying superblock, which is
1877 * the magic code for a fully-recovered superblock. Any future
1878 * commits of data to the journal will restore the current
1880 jbd2_mark_journal_empty(journal
);
1881 mutex_unlock(&journal
->j_checkpoint_mutex
);
1882 write_lock(&journal
->j_state_lock
);
1883 J_ASSERT(!journal
->j_running_transaction
);
1884 J_ASSERT(!journal
->j_committing_transaction
);
1885 J_ASSERT(!journal
->j_checkpoint_transactions
);
1886 J_ASSERT(journal
->j_head
== journal
->j_tail
);
1887 J_ASSERT(journal
->j_tail_sequence
== journal
->j_transaction_sequence
);
1888 write_unlock(&journal
->j_state_lock
);
1893 * int jbd2_journal_wipe() - Wipe journal contents
1894 * @journal: Journal to act on.
1895 * @write: flag (see below)
1897 * Wipe out all of the contents of a journal, safely. This will produce
1898 * a warning if the journal contains any valid recovery information.
1899 * Must be called between journal_init_*() and jbd2_journal_load().
1901 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1902 * we merely suppress recovery.
1905 int jbd2_journal_wipe(journal_t
*journal
, int write
)
1909 J_ASSERT (!(journal
->j_flags
& JBD2_LOADED
));
1911 err
= load_superblock(journal
);
1915 if (!journal
->j_tail
)
1918 printk(KERN_WARNING
"JBD2: %s recovery information on journal\n",
1919 write
? "Clearing" : "Ignoring");
1921 err
= jbd2_journal_skip_recovery(journal
);
1923 /* Lock to make assertions happy... */
1924 mutex_lock(&journal
->j_checkpoint_mutex
);
1925 jbd2_mark_journal_empty(journal
);
1926 mutex_unlock(&journal
->j_checkpoint_mutex
);
1934 * Journal abort has very specific semantics, which we describe
1935 * for journal abort.
1937 * Two internal functions, which provide abort to the jbd layer
1942 * Quick version for internal journal use (doesn't lock the journal).
1943 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1944 * and don't attempt to make any other journal updates.
1946 void __jbd2_journal_abort_hard(journal_t
*journal
)
1948 transaction_t
*transaction
;
1950 if (journal
->j_flags
& JBD2_ABORT
)
1953 printk(KERN_ERR
"Aborting journal on device %s.\n",
1954 journal
->j_devname
);
1956 write_lock(&journal
->j_state_lock
);
1957 journal
->j_flags
|= JBD2_ABORT
;
1958 transaction
= journal
->j_running_transaction
;
1960 __jbd2_log_start_commit(journal
, transaction
->t_tid
);
1961 write_unlock(&journal
->j_state_lock
);
1964 /* Soft abort: record the abort error status in the journal superblock,
1965 * but don't do any other IO. */
1966 static void __journal_abort_soft (journal_t
*journal
, int errno
)
1968 if (journal
->j_flags
& JBD2_ABORT
)
1971 if (!journal
->j_errno
)
1972 journal
->j_errno
= errno
;
1974 __jbd2_journal_abort_hard(journal
);
1977 jbd2_journal_update_sb_errno(journal
);
1981 * void jbd2_journal_abort () - Shutdown the journal immediately.
1982 * @journal: the journal to shutdown.
1983 * @errno: an error number to record in the journal indicating
1984 * the reason for the shutdown.
1986 * Perform a complete, immediate shutdown of the ENTIRE
1987 * journal (not of a single transaction). This operation cannot be
1988 * undone without closing and reopening the journal.
1990 * The jbd2_journal_abort function is intended to support higher level error
1991 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1994 * Journal abort has very specific semantics. Any existing dirty,
1995 * unjournaled buffers in the main filesystem will still be written to
1996 * disk by bdflush, but the journaling mechanism will be suspended
1997 * immediately and no further transaction commits will be honoured.
1999 * Any dirty, journaled buffers will be written back to disk without
2000 * hitting the journal. Atomicity cannot be guaranteed on an aborted
2001 * filesystem, but we _do_ attempt to leave as much data as possible
2002 * behind for fsck to use for cleanup.
2004 * Any attempt to get a new transaction handle on a journal which is in
2005 * ABORT state will just result in an -EROFS error return. A
2006 * jbd2_journal_stop on an existing handle will return -EIO if we have
2007 * entered abort state during the update.
2009 * Recursive transactions are not disturbed by journal abort until the
2010 * final jbd2_journal_stop, which will receive the -EIO error.
2012 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
2013 * which will be recorded (if possible) in the journal superblock. This
2014 * allows a client to record failure conditions in the middle of a
2015 * transaction without having to complete the transaction to record the
2016 * failure to disk. ext3_error, for example, now uses this
2019 * Errors which originate from within the journaling layer will NOT
2020 * supply an errno; a null errno implies that absolutely no further
2021 * writes are done to the journal (unless there are any already in
2026 void jbd2_journal_abort(journal_t
*journal
, int errno
)
2028 __journal_abort_soft(journal
, errno
);
2032 * int jbd2_journal_errno () - returns the journal's error state.
2033 * @journal: journal to examine.
2035 * This is the errno number set with jbd2_journal_abort(), the last
2036 * time the journal was mounted - if the journal was stopped
2037 * without calling abort this will be 0.
2039 * If the journal has been aborted on this mount time -EROFS will
2042 int jbd2_journal_errno(journal_t
*journal
)
2046 read_lock(&journal
->j_state_lock
);
2047 if (journal
->j_flags
& JBD2_ABORT
)
2050 err
= journal
->j_errno
;
2051 read_unlock(&journal
->j_state_lock
);
2056 * int jbd2_journal_clear_err () - clears the journal's error state
2057 * @journal: journal to act on.
2059 * An error must be cleared or acked to take a FS out of readonly
2062 int jbd2_journal_clear_err(journal_t
*journal
)
2066 write_lock(&journal
->j_state_lock
);
2067 if (journal
->j_flags
& JBD2_ABORT
)
2070 journal
->j_errno
= 0;
2071 write_unlock(&journal
->j_state_lock
);
2076 * void jbd2_journal_ack_err() - Ack journal err.
2077 * @journal: journal to act on.
2079 * An error must be cleared or acked to take a FS out of readonly
2082 void jbd2_journal_ack_err(journal_t
*journal
)
2084 write_lock(&journal
->j_state_lock
);
2085 if (journal
->j_errno
)
2086 journal
->j_flags
|= JBD2_ACK_ERR
;
2087 write_unlock(&journal
->j_state_lock
);
2090 int jbd2_journal_blocks_per_page(struct inode
*inode
)
2092 return 1 << (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
2096 * helper functions to deal with 32 or 64bit block numbers.
2098 size_t journal_tag_bytes(journal_t
*journal
)
2100 if (JBD2_HAS_INCOMPAT_FEATURE(journal
, JBD2_FEATURE_INCOMPAT_64BIT
))
2101 return JBD2_TAG_SIZE64
;
2103 return JBD2_TAG_SIZE32
;
2107 * JBD memory management
2109 * These functions are used to allocate block-sized chunks of memory
2110 * used for making copies of buffer_head data. Very often it will be
2111 * page-sized chunks of data, but sometimes it will be in
2112 * sub-page-size chunks. (For example, 16k pages on Power systems
2113 * with a 4k block file system.) For blocks smaller than a page, we
2114 * use a SLAB allocator. There are slab caches for each block size,
2115 * which are allocated at mount time, if necessary, and we only free
2116 * (all of) the slab caches when/if the jbd2 module is unloaded. For
2117 * this reason we don't need to a mutex to protect access to
2118 * jbd2_slab[] allocating or releasing memory; only in
2119 * jbd2_journal_create_slab().
2121 #define JBD2_MAX_SLABS 8
2122 static struct kmem_cache
*jbd2_slab
[JBD2_MAX_SLABS
];
2124 static const char *jbd2_slab_names
[JBD2_MAX_SLABS
] = {
2125 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
2126 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
2130 static void jbd2_journal_destroy_slabs(void)
2134 for (i
= 0; i
< JBD2_MAX_SLABS
; i
++) {
2136 kmem_cache_destroy(jbd2_slab
[i
]);
2137 jbd2_slab
[i
] = NULL
;
2141 static int jbd2_journal_create_slab(size_t size
)
2143 static DEFINE_MUTEX(jbd2_slab_create_mutex
);
2144 int i
= order_base_2(size
) - 10;
2147 if (size
== PAGE_SIZE
)
2150 if (i
>= JBD2_MAX_SLABS
)
2153 if (unlikely(i
< 0))
2155 mutex_lock(&jbd2_slab_create_mutex
);
2157 mutex_unlock(&jbd2_slab_create_mutex
);
2158 return 0; /* Already created */
2161 slab_size
= 1 << (i
+10);
2162 jbd2_slab
[i
] = kmem_cache_create(jbd2_slab_names
[i
], slab_size
,
2163 slab_size
, 0, NULL
);
2164 mutex_unlock(&jbd2_slab_create_mutex
);
2165 if (!jbd2_slab
[i
]) {
2166 printk(KERN_EMERG
"JBD2: no memory for jbd2_slab cache\n");
2172 static struct kmem_cache
*get_slab(size_t size
)
2174 int i
= order_base_2(size
) - 10;
2176 BUG_ON(i
>= JBD2_MAX_SLABS
);
2177 if (unlikely(i
< 0))
2179 BUG_ON(jbd2_slab
[i
] == NULL
);
2180 return jbd2_slab
[i
];
2183 void *jbd2_alloc(size_t size
, gfp_t flags
)
2187 BUG_ON(size
& (size
-1)); /* Must be a power of 2 */
2189 flags
|= __GFP_REPEAT
;
2190 if (size
== PAGE_SIZE
)
2191 ptr
= (void *)__get_free_pages(flags
, 0);
2192 else if (size
> PAGE_SIZE
) {
2193 int order
= get_order(size
);
2196 ptr
= (void *)__get_free_pages(flags
, order
);
2198 ptr
= vmalloc(size
);
2200 ptr
= kmem_cache_alloc(get_slab(size
), flags
);
2202 /* Check alignment; SLUB has gotten this wrong in the past,
2203 * and this can lead to user data corruption! */
2204 BUG_ON(((unsigned long) ptr
) & (size
-1));
2209 void jbd2_free(void *ptr
, size_t size
)
2211 if (size
== PAGE_SIZE
) {
2212 free_pages((unsigned long)ptr
, 0);
2215 if (size
> PAGE_SIZE
) {
2216 int order
= get_order(size
);
2219 free_pages((unsigned long)ptr
, order
);
2224 kmem_cache_free(get_slab(size
), ptr
);
2228 * Journal_head storage management
2230 static struct kmem_cache
*jbd2_journal_head_cache
;
2231 #ifdef CONFIG_JBD2_DEBUG
2232 static atomic_t nr_journal_heads
= ATOMIC_INIT(0);
2235 static int jbd2_journal_init_journal_head_cache(void)
2239 J_ASSERT(jbd2_journal_head_cache
== NULL
);
2240 jbd2_journal_head_cache
= kmem_cache_create("jbd2_journal_head",
2241 sizeof(struct journal_head
),
2243 SLAB_TEMPORARY
, /* flags */
2246 if (!jbd2_journal_head_cache
) {
2248 printk(KERN_EMERG
"JBD2: no memory for journal_head cache\n");
2253 static void jbd2_journal_destroy_journal_head_cache(void)
2255 if (jbd2_journal_head_cache
) {
2256 kmem_cache_destroy(jbd2_journal_head_cache
);
2257 jbd2_journal_head_cache
= NULL
;
2262 * journal_head splicing and dicing
2264 static struct journal_head
*journal_alloc_journal_head(void)
2266 struct journal_head
*ret
;
2268 #ifdef CONFIG_JBD2_DEBUG
2269 atomic_inc(&nr_journal_heads
);
2271 ret
= kmem_cache_alloc(jbd2_journal_head_cache
, GFP_NOFS
);
2273 jbd_debug(1, "out of memory for journal_head\n");
2274 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__
);
2277 ret
= kmem_cache_alloc(jbd2_journal_head_cache
, GFP_NOFS
);
2283 static void journal_free_journal_head(struct journal_head
*jh
)
2285 #ifdef CONFIG_JBD2_DEBUG
2286 atomic_dec(&nr_journal_heads
);
2287 memset(jh
, JBD2_POISON_FREE
, sizeof(*jh
));
2289 kmem_cache_free(jbd2_journal_head_cache
, jh
);
2293 * A journal_head is attached to a buffer_head whenever JBD has an
2294 * interest in the buffer.
2296 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2297 * is set. This bit is tested in core kernel code where we need to take
2298 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2301 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2303 * When a buffer has its BH_JBD bit set it is immune from being released by
2304 * core kernel code, mainly via ->b_count.
2306 * A journal_head is detached from its buffer_head when the journal_head's
2307 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2308 * transaction (b_cp_transaction) hold their references to b_jcount.
2310 * Various places in the kernel want to attach a journal_head to a buffer_head
2311 * _before_ attaching the journal_head to a transaction. To protect the
2312 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
2313 * journal_head's b_jcount refcount by one. The caller must call
2314 * jbd2_journal_put_journal_head() to undo this.
2316 * So the typical usage would be:
2318 * (Attach a journal_head if needed. Increments b_jcount)
2319 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
2321 * (Get another reference for transaction)
2322 * jbd2_journal_grab_journal_head(bh);
2323 * jh->b_transaction = xxx;
2324 * (Put original reference)
2325 * jbd2_journal_put_journal_head(jh);
2329 * Give a buffer_head a journal_head.
2333 struct journal_head
*jbd2_journal_add_journal_head(struct buffer_head
*bh
)
2335 struct journal_head
*jh
;
2336 struct journal_head
*new_jh
= NULL
;
2339 if (!buffer_jbd(bh
)) {
2340 new_jh
= journal_alloc_journal_head();
2341 memset(new_jh
, 0, sizeof(*new_jh
));
2344 jbd_lock_bh_journal_head(bh
);
2345 if (buffer_jbd(bh
)) {
2349 (atomic_read(&bh
->b_count
) > 0) ||
2350 (bh
->b_page
&& bh
->b_page
->mapping
));
2353 jbd_unlock_bh_journal_head(bh
);
2358 new_jh
= NULL
; /* We consumed it */
2363 BUFFER_TRACE(bh
, "added journal_head");
2366 jbd_unlock_bh_journal_head(bh
);
2368 journal_free_journal_head(new_jh
);
2369 return bh
->b_private
;
2373 * Grab a ref against this buffer_head's journal_head. If it ended up not
2374 * having a journal_head, return NULL
2376 struct journal_head
*jbd2_journal_grab_journal_head(struct buffer_head
*bh
)
2378 struct journal_head
*jh
= NULL
;
2380 jbd_lock_bh_journal_head(bh
);
2381 if (buffer_jbd(bh
)) {
2385 jbd_unlock_bh_journal_head(bh
);
2389 static void __journal_remove_journal_head(struct buffer_head
*bh
)
2391 struct journal_head
*jh
= bh2jh(bh
);
2393 J_ASSERT_JH(jh
, jh
->b_jcount
>= 0);
2394 J_ASSERT_JH(jh
, jh
->b_transaction
== NULL
);
2395 J_ASSERT_JH(jh
, jh
->b_next_transaction
== NULL
);
2396 J_ASSERT_JH(jh
, jh
->b_cp_transaction
== NULL
);
2397 J_ASSERT_JH(jh
, jh
->b_jlist
== BJ_None
);
2398 J_ASSERT_BH(bh
, buffer_jbd(bh
));
2399 J_ASSERT_BH(bh
, jh2bh(jh
) == bh
);
2400 BUFFER_TRACE(bh
, "remove journal_head");
2401 if (jh
->b_frozen_data
) {
2402 printk(KERN_WARNING
"%s: freeing b_frozen_data\n", __func__
);
2403 jbd2_free(jh
->b_frozen_data
, bh
->b_size
);
2405 if (jh
->b_committed_data
) {
2406 printk(KERN_WARNING
"%s: freeing b_committed_data\n", __func__
);
2407 jbd2_free(jh
->b_committed_data
, bh
->b_size
);
2409 bh
->b_private
= NULL
;
2410 jh
->b_bh
= NULL
; /* debug, really */
2411 clear_buffer_jbd(bh
);
2412 journal_free_journal_head(jh
);
2416 * Drop a reference on the passed journal_head. If it fell to zero then
2417 * release the journal_head from the buffer_head.
2419 void jbd2_journal_put_journal_head(struct journal_head
*jh
)
2421 struct buffer_head
*bh
= jh2bh(jh
);
2423 jbd_lock_bh_journal_head(bh
);
2424 J_ASSERT_JH(jh
, jh
->b_jcount
> 0);
2426 if (!jh
->b_jcount
) {
2427 __journal_remove_journal_head(bh
);
2428 jbd_unlock_bh_journal_head(bh
);
2431 jbd_unlock_bh_journal_head(bh
);
2435 * Initialize jbd inode head
2437 void jbd2_journal_init_jbd_inode(struct jbd2_inode
*jinode
, struct inode
*inode
)
2439 jinode
->i_transaction
= NULL
;
2440 jinode
->i_next_transaction
= NULL
;
2441 jinode
->i_vfs_inode
= inode
;
2442 jinode
->i_flags
= 0;
2443 INIT_LIST_HEAD(&jinode
->i_list
);
2447 * Function to be called before we start removing inode from memory (i.e.,
2448 * clear_inode() is a fine place to be called from). It removes inode from
2449 * transaction's lists.
2451 void jbd2_journal_release_jbd_inode(journal_t
*journal
,
2452 struct jbd2_inode
*jinode
)
2457 spin_lock(&journal
->j_list_lock
);
2458 /* Is commit writing out inode - we have to wait */
2459 if (test_bit(__JI_COMMIT_RUNNING
, &jinode
->i_flags
)) {
2460 wait_queue_head_t
*wq
;
2461 DEFINE_WAIT_BIT(wait
, &jinode
->i_flags
, __JI_COMMIT_RUNNING
);
2462 wq
= bit_waitqueue(&jinode
->i_flags
, __JI_COMMIT_RUNNING
);
2463 prepare_to_wait(wq
, &wait
.wait
, TASK_UNINTERRUPTIBLE
);
2464 spin_unlock(&journal
->j_list_lock
);
2466 finish_wait(wq
, &wait
.wait
);
2470 if (jinode
->i_transaction
) {
2471 list_del(&jinode
->i_list
);
2472 jinode
->i_transaction
= NULL
;
2474 spin_unlock(&journal
->j_list_lock
);
2480 #ifdef CONFIG_JBD2_DEBUG
2481 u8 jbd2_journal_enable_debug __read_mostly
;
2482 EXPORT_SYMBOL(jbd2_journal_enable_debug
);
2484 #define JBD2_DEBUG_NAME "jbd2-debug"
2486 static struct dentry
*jbd2_debugfs_dir
;
2487 static struct dentry
*jbd2_debug
;
2489 static void __init
jbd2_create_debugfs_entry(void)
2491 jbd2_debugfs_dir
= debugfs_create_dir("jbd2", NULL
);
2492 if (jbd2_debugfs_dir
)
2493 jbd2_debug
= debugfs_create_u8(JBD2_DEBUG_NAME
,
2496 &jbd2_journal_enable_debug
);
2499 static void __exit
jbd2_remove_debugfs_entry(void)
2501 debugfs_remove(jbd2_debug
);
2502 debugfs_remove(jbd2_debugfs_dir
);
2507 static void __init
jbd2_create_debugfs_entry(void)
2511 static void __exit
jbd2_remove_debugfs_entry(void)
2517 #ifdef CONFIG_PROC_FS
2519 #define JBD2_STATS_PROC_NAME "fs/jbd2"
2521 static void __init
jbd2_create_jbd_stats_proc_entry(void)
2523 proc_jbd2_stats
= proc_mkdir(JBD2_STATS_PROC_NAME
, NULL
);
2526 static void __exit
jbd2_remove_jbd_stats_proc_entry(void)
2528 if (proc_jbd2_stats
)
2529 remove_proc_entry(JBD2_STATS_PROC_NAME
, NULL
);
2534 #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2535 #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2539 struct kmem_cache
*jbd2_handle_cache
, *jbd2_inode_cache
;
2541 static int __init
jbd2_journal_init_handle_cache(void)
2543 jbd2_handle_cache
= KMEM_CACHE(jbd2_journal_handle
, SLAB_TEMPORARY
);
2544 if (jbd2_handle_cache
== NULL
) {
2545 printk(KERN_EMERG
"JBD2: failed to create handle cache\n");
2548 jbd2_inode_cache
= KMEM_CACHE(jbd2_inode
, 0);
2549 if (jbd2_inode_cache
== NULL
) {
2550 printk(KERN_EMERG
"JBD2: failed to create inode cache\n");
2551 kmem_cache_destroy(jbd2_handle_cache
);
2557 static void jbd2_journal_destroy_handle_cache(void)
2559 if (jbd2_handle_cache
)
2560 kmem_cache_destroy(jbd2_handle_cache
);
2561 if (jbd2_inode_cache
)
2562 kmem_cache_destroy(jbd2_inode_cache
);
2567 * Module startup and shutdown
2570 static int __init
journal_init_caches(void)
2574 ret
= jbd2_journal_init_revoke_caches();
2576 ret
= jbd2_journal_init_journal_head_cache();
2578 ret
= jbd2_journal_init_handle_cache();
2580 ret
= jbd2_journal_init_transaction_cache();
2584 static void jbd2_journal_destroy_caches(void)
2586 jbd2_journal_destroy_revoke_caches();
2587 jbd2_journal_destroy_journal_head_cache();
2588 jbd2_journal_destroy_handle_cache();
2589 jbd2_journal_destroy_transaction_cache();
2590 jbd2_journal_destroy_slabs();
2593 static int __init
journal_init(void)
2597 BUILD_BUG_ON(sizeof(struct journal_superblock_s
) != 1024);
2599 ret
= journal_init_caches();
2601 jbd2_create_debugfs_entry();
2602 jbd2_create_jbd_stats_proc_entry();
2604 jbd2_journal_destroy_caches();
2609 static void __exit
journal_exit(void)
2611 #ifdef CONFIG_JBD2_DEBUG
2612 int n
= atomic_read(&nr_journal_heads
);
2614 printk(KERN_EMERG
"JBD2: leaked %d journal_heads!\n", n
);
2616 jbd2_remove_debugfs_entry();
2617 jbd2_remove_jbd_stats_proc_entry();
2618 jbd2_journal_destroy_caches();
2621 MODULE_LICENSE("GPL");
2622 module_init(journal_init
);
2623 module_exit(journal_exit
);