jbd2: Fix return value of jbd2_journal_start_commit()
Subsystems (3)
Theodore Ts'o
maintainerFrom: Jan Kara <[email protected]>
The function jbd2_journal_start_commit() returns 1 if either a transaction is committing or the function has queued a transaction commit. But it returns 0 if we raced with somebody queueing the transaction commit as well. This resulted in ext4_sync_fs() not functioning correctly (description from Arthur Jones):
In the case of a data=ordered umount with pending long symlinks which are delayed due to a long list of other I/O on the backing block device, this causes the buffer associated with the long symlinks to not be moved to the inode dirty list in the second phase of fsync_super. Then, before they can be dirtied again, kjournald exits, seeing the UMOUNT flag and the dirty pages are never written to the backing block device, causing long symlink corruption and exposing new or previously freed block data to userspace.
This can be reproduced with a script created by Eric Sandeen <[email protected]>:
#!/bin/bash
umount /mnt/test2
mount /dev/sdb4 /mnt/test2
rm -f /mnt/test2/*
dd if=/dev/zero of=/mnt/test2/bigfile bs=1M count=512
touch /mnt/test2/thisisveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongfilename
ln -s /mnt/test2/thisisveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongfilename
/mnt/test2/link
umount /mnt/test2
mount /dev/sdb4 /mnt/test2
ls /mnt/test2/This patch fixes jbd2_journal_start_commit() to always return 1 when there's a transaction committing or queued for commit.
--- fs/jbd2/journal.c | 17 +++++++++++------ 1 files changed, 11 insertions(+), 6 deletions(-)
fs/jbd2/journal.c +11 -6
@@ -430,7 +430,7 @@ int __jbd2_log_space_left(journal_t *journal) }/* -* Called under j_state_lock. Returns true if a transaction was started. +* Called under j_state_lock. Returns true if a transaction commit was started. */ int __jbd2_log_start_commit (journal_t *journal,tid_t target) {
@@ -498,7 +498,8 @@ int jbd2_journal_force_commit_nested(journal_t *journal)/* * Start a commit of the current running transaction (if any). Returns true -* if a transaction was started, and fills its tid in at *ptid +* if a transaction is going to be committed (or is currently already +* committing), and fills its tid in at *ptid */ int jbd2_journal_start_commit (journal_t *journal,tid_t *ptid) {
@@ -508,15 +509,19 @@ int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)if (journal->j_running_transaction) {tid_t tid = journal->j_running_transaction->t_tid; - ret =__jbd2_log_start_commit (journal, tid); -if (ret && ptid) +__jbd2_log_start_commit (journal, tid); +/* There's a running transaction and we've just made sure +* it's commit has been scheduled. */ +if (ptid) *ptid = tid; - }else if (journal->j_committing_transaction && ptid) { + ret =1 ; + }else if (journal->j_committing_transaction) {/* * If ext3_write_super() recently started a commit, then we * have to wait for completion of that transaction */ - *ptid = journal->j_committing_transaction->t_tid; +if (ptid) + *ptid = journal->j_committing_transaction->t_tid; ret =1 ; }spin_unlock (&journal->j_state_lock);
Signature · 1 line
1.5.6.3
Theodore Ts'o
maintainer[STABLE, 2.6.28.y] Revert "ext4: wait on all pending commits in ext4_sync_fs()"
From: Jan Kara <[email protected]>
This undoes commit 14ce0cb411c88681ab8f3a4c9caa7f42e97a3184.
Since jbd2_journal_start_commit() is now fixed to return 1 when we started a transaction commit, there's some transaction waiting to be committed or there's a transaction already committing, we don't need to call ext4_force_commit() in ext4_sync_fs(). Furthermore ext4_force_commit() can unnecessarily create sync transaction which is expensive so it's worthwhile to remove it when we can.
http://bugzilla.kernel.org/show_bug.cgi?id=12224
--- fs/ext4/super.c | 12 ++++++------ 1 files changed, 6 insertions(+), 6 deletions(-)
fs/ext4/super.c +6 -6
@@ -2914,15 +2914,15 @@ static void ext4_write_super(struct super_block *sb)static int ext4_sync_fs (struct super_block *sb,int wait) { -int ret =0 ; +tid_t target;trace_mark (ext4_sync_fs,"dev %s wait %d" , sb->s_id, wait); sb->s_dirt =0 ; -if (wait) - ret =ext4_force_commit (sb); -else -jbd2_journal_start_commit (EXT4_SB (sb)->s_journal,NULL ); -return ret; +if (jbd2_journal_start_commit (EXT4_SB (sb)->s_journal, &target)) { +if (wait) +jbd2_log_wait_commit (EXT4_SB (sb)->s_journal, target); + } +return 0 ; }/*
Signature · 1 line
1.5.6.3
Theodore Ts'o
maintainer[STABLE, 2.6.28.y] jbd2: Avoid possible NULL dereference in jbd2_journal_begin_ordered_truncate()
From: Jan Kara <[email protected]>
If we race with commit code setting i_transaction to NULL, we could possibly dereference it. Proper locking requires the journal pointer (to access journal->j_list_lock), which we don't have. So we have to change the prototype of the function so that filesystem passes us the journal pointer. Also add a more detailed comment about why the function jbd2_journal_begin_ordered_truncate() does what it does and how it should be used.
Thanks to Dan Carpenter <[email protected]> for pointing to the suspitious code.
--- fs/ext4/inode.c | 6 ++++-- fs/jbd2/transaction.c | 42 +++++++++++++++++++++++++++++++----------- fs/ocfs2/journal.h | 6 ++++-- include/linux/jbd2.h | 3 ++- 4 files changed, 41 insertions(+), 16 deletions(-)
fs/ext4/inode.c +4 -2
@@ -46,8 +46,10 @@static inline int ext4_begin_ordered_truncate (struct inode *inode,loff_t new_size) { -return jbd2_journal_begin_ordered_truncate (&EXT4_I (inode)->jinode, - new_size); +return jbd2_journal_begin_ordered_truncate ( +EXT4_SB (inode->i_sb)->s_journal, + &EXT4_I (inode)->jinode, + new_size); }static void ext4_invalidatepage (struct page *page,unsigned long offset);
fs/jbd2/transaction.c +31 -11
57 lines not on this page · raw message
fs/ocfs2/journal.h +4 -2
@@ -445,8 +445,10 @@ static inline int ocfs2_jbd2_file_inode(handle_t *handle, struct inode *inode)static inline int ocfs2_begin_ordered_truncate (struct inode *inode,loff_t new_size) { -return jbd2_journal_begin_ordered_truncate (&OCFS2_I (inode)->ip_jinode, - new_size); +return jbd2_journal_begin_ordered_truncate ( +OCFS2_SB (inode->i_sb)->journal->j_journal, + &OCFS2_I (inode)->ip_jinode, + new_size); }#endif /* OCFS2_JOURNAL_H */
include/linux/jbd2.h +2 -1
@@ -1087,7 +1087,8 @@ extern int jbd2_journal_clear_err (journal_t *);extern int jbd2_journal_bmap (journal_t *,unsigned long ,unsigned long long *);extern int jbd2_journal_force_commit (journal_t *);extern int jbd2_journal_file_inode (handle_t *handle,struct jbd2_inode *inode); -extern int jbd2_journal_begin_ordered_truncate (struct jbd2_inode *inode,loff_t new_size); +extern int jbd2_journal_begin_ordered_truncate (journal_t *journal, +struct jbd2_inode *inode,loff_t new_size);extern void jbd2_journal_init_jbd_inode (struct jbd2_inode *jinode,struct inode *inode);extern void jbd2_journal_release_jbd_inode (journal_t *journal,struct jbd2_inode *jinode);
Signature · 1 line
1.5.6.3