Skip to content

Commit 4c54659

Browse files
daehojeongtytso
authored andcommitted
ext4: handle unwritten or delalloc buffers before enabling data journaling
We already allocate delalloc blocks before changing the inode mode into "per-file data journal" mode to prevent delalloc blocks from remaining not allocated, but another issue concerned with "BH_Unwritten" status still exists. For example, by fallocate(), several buffers' status change into "BH_Unwritten", but these buffers cannot be processed by ext4_alloc_da_blocks(). So, they still remain in unwritten status after per-file data journaling is enabled and they cannot be changed into written status any more and, if they are journaled and eventually checkpointed, these unwritten buffer will cause a kernel panic by the below BUG_ON() function of submit_bh_wbc() when they are submitted during checkpointing. static int submit_bh_wbc(int rw, struct buffer_head *bh,... { ... BUG_ON(buffer_unwritten(bh)); Moreover, when "dioread_nolock" option is enabled, the status of a buffer is changed into "BH_Unwritten" after write_begin() completes and the "BH_Unwritten" status will be cleared after I/O is done. Therefore, if a buffer's status is changed into unwrutten but the buffer's I/O is not submitted and completed, it can cause the same problem after enabling per-file data journaling. You can easily generate this bug by executing the following command. ./kvm-xfstests -C 10000 -m nodelalloc,dioread_nolock generic/269 To resolve these problems and define a boundary between the previous mode and per-file data journaling mode, we need to flush and wait all the I/O of buffers of a file before enabling per-file data journaling of the file. Signed-off-by: Daeho Jeong <daeho.jeong@samsung.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Reviewed-by: Jan Kara <jack@suse.cz>
1 parent 7b80819 commit 4c54659

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

‎fs/ext4/inode.c‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5452,22 +5452,29 @@ int ext4_change_inode_journal_flag(struct inode *inode, int val)
54525452
return 0;
54535453
if (is_journal_aborted(journal))
54545454
return -EROFS;
5455-
/* We have to allocate physical blocks for delalloc blocks
5456-
* before flushing journal. otherwise delalloc blocks can not
5457-
* be allocated any more. even more truncate on delalloc blocks
5458-
* could trigger BUG by flushing delalloc blocks in journal.
5459-
* There is no delalloc block in non-journal data mode.
5460-
*/
5461-
if (val && test_opt(inode->i_sb, DELALLOC)) {
5462-
err = ext4_alloc_da_blocks(inode);
5463-
if (err < 0)
5464-
return err;
5465-
}
54665455

54675456
/* Wait for all existing dio workers */
54685457
ext4_inode_block_unlocked_dio(inode);
54695458
inode_dio_wait(inode);
54705459

5460+
/*
5461+
* Before flushing the journal and switching inode's aops, we have
5462+
* to flush all dirty data the inode has. There can be outstanding
5463+
* delayed allocations, there can be unwritten extents created by
5464+
* fallocate or buffered writes in dioread_nolock mode covered by
5465+
* dirty data which can be converted only after flushing the dirty
5466+
* data (and journalled aops don't know how to handle these cases).
5467+
*/
5468+
if (val) {
5469+
down_write(&EXT4_I(inode)->i_mmap_sem);
5470+
err = filemap_write_and_wait(inode->i_mapping);
5471+
if (err < 0) {
5472+
up_write(&EXT4_I(inode)->i_mmap_sem);
5473+
ext4_inode_resume_unlocked_dio(inode);
5474+
return err;
5475+
}
5476+
}
5477+
54715478
jbd2_journal_lock_updates(journal);
54725479

54735480
/*
@@ -5492,6 +5499,8 @@ int ext4_change_inode_journal_flag(struct inode *inode, int val)
54925499
ext4_set_aops(inode);
54935500

54945501
jbd2_journal_unlock_updates(journal);
5502+
if (val)
5503+
up_write(&EXT4_I(inode)->i_mmap_sem);
54955504
ext4_inode_resume_unlocked_dio(inode);
54965505

54975506
/* Finally we can mark the inode as dirty. */

0 commit comments

Comments
 (0)