Skip to content

Commit cc3e1be

Browse files
committed
ext4, jbd2: Add barriers for file systems with exernal journals
This is a bit complicated because we are trying to optimize when we send barriers to the fs data disk. We could just throw in an extra barrier to the data disk whenever we send a barrier to the journal disk, but that's not always strictly necessary. We only need to send a barrier during a commit when there are data blocks which are must be written out due to an inode written in ordered mode, or if fsync() depends on the commit to force data blocks to disk. Finally, before we drop transactions from the beginning of the journal during a checkpoint operation, we need to guarantee that any blocks that were flushed out to the data disk are firmly on the rust platter before we drop the transaction from the journal. Thanks to Oleg Drokin for pointing out this flaw in ext3/ext4. Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent 034fb4c commit cc3e1be

4 files changed

Lines changed: 41 additions & 10 deletions

File tree

‎fs/ext4/fsync.c‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,21 @@ int ext4_sync_file(struct file *file, struct dentry *dentry, int datasync)
8888
return ext4_force_commit(inode->i_sb);
8989

9090
commit_tid = datasync ? ei->i_datasync_tid : ei->i_sync_tid;
91-
if (jbd2_log_start_commit(journal, commit_tid))
91+
if (jbd2_log_start_commit(journal, commit_tid)) {
92+
/*
93+
* When the journal is on a different device than the
94+
* fs data disk, we need to issue the barrier in
95+
* writeback mode. (In ordered mode, the jbd2 layer
96+
* will take care of issuing the barrier. In
97+
* data=journal, all of the data blocks are written to
98+
* the journal device.)
99+
*/
100+
if (ext4_should_writeback_data(inode) &&
101+
(journal->j_fs_dev != journal->j_dev) &&
102+
(journal->j_flags & JBD2_BARRIER))
103+
blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
92104
jbd2_log_wait_commit(journal, commit_tid);
93-
else if (journal->j_flags & JBD2_BARRIER)
105+
} else if (journal->j_flags & JBD2_BARRIER)
94106
blkdev_issue_flush(inode->i_sb->s_bdev, NULL);
95107
return ret;
96108
}

‎fs/jbd2/checkpoint.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/jbd2.h>
2323
#include <linux/errno.h>
2424
#include <linux/slab.h>
25+
#include <linux/blkdev.h>
2526
#include <trace/events/jbd2.h>
2627

2728
/*
@@ -515,6 +516,20 @@ int jbd2_cleanup_journal_tail(journal_t *journal)
515516
journal->j_tail_sequence = first_tid;
516517
journal->j_tail = blocknr;
517518
spin_unlock(&journal->j_state_lock);
519+
520+
/*
521+
* If there is an external journal, we need to make sure that
522+
* any data blocks that were recently written out --- perhaps
523+
* by jbd2_log_do_checkpoint() --- are flushed out before we
524+
* drop the transactions from the external journal. It's
525+
* unlikely this will be necessary, especially with a
526+
* appropriately sized journal, but we need this to guarantee
527+
* correctness. Fortunately jbd2_cleanup_journal_tail()
528+
* doesn't get called all that often.
529+
*/
530+
if ((journal->j_fs_dev != journal->j_dev) &&
531+
(journal->j_flags & JBD2_BARRIER))
532+
blkdev_issue_flush(journal->j_fs_dev, NULL);
518533
if (!(journal->j_flags & JBD2_ABORT))
519534
jbd2_journal_update_superblock(journal, 1);
520535
return 0;

‎fs/jbd2/commit.c‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ static int journal_submit_data_buffers(journal_t *journal,
259259
ret = err;
260260
spin_lock(&journal->j_list_lock);
261261
J_ASSERT(jinode->i_transaction == commit_transaction);
262+
commit_transaction->t_flushed_data_blocks = 1;
262263
jinode->i_flags &= ~JI_COMMIT_RUNNING;
263264
wake_up_bit(&jinode->i_flags, __JI_COMMIT_RUNNING);
264265
}
@@ -708,8 +709,17 @@ void jbd2_journal_commit_transaction(journal_t *journal)
708709
}
709710
}
710711

711-
/* Done it all: now write the commit record asynchronously. */
712+
/*
713+
* If the journal is not located on the file system device,
714+
* then we must flush the file system device before we issue
715+
* the commit record
716+
*/
717+
if (commit_transaction->t_flushed_data_blocks &&
718+
(journal->j_fs_dev != journal->j_dev) &&
719+
(journal->j_flags & JBD2_BARRIER))
720+
blkdev_issue_flush(journal->j_fs_dev, NULL);
712721

722+
/* Done it all: now write the commit record asynchronously. */
713723
if (JBD2_HAS_INCOMPAT_FEATURE(journal,
714724
JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT)) {
715725
err = journal_submit_commit_record(journal, commit_transaction,
@@ -720,13 +730,6 @@ void jbd2_journal_commit_transaction(journal_t *journal)
720730
blkdev_issue_flush(journal->j_dev, NULL);
721731
}
722732

723-
/*
724-
* This is the right place to wait for data buffers both for ASYNC
725-
* and !ASYNC commit. If commit is ASYNC, we need to wait only after
726-
* the commit block went to disk (which happens above). If commit is
727-
* SYNC, we need to wait for data buffers before we start writing
728-
* commit block, which happens below in such setting.
729-
*/
730733
err = journal_finish_inode_data_buffers(journal, commit_transaction);
731734
if (err) {
732735
printk(KERN_WARNING

‎include/linux/jbd2.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ struct transaction_s
653653
* waiting for it to finish.
654654
*/
655655
unsigned int t_synchronous_commit:1;
656+
unsigned int t_flushed_data_blocks:1;
656657

657658
/*
658659
* For use by the filesystem to store fs-specific data

0 commit comments

Comments
 (0)