ext4: nojournal mode fixes
Subsystems (2)
Jan Kara
reviewercover ext4: nojournal mode fixes
Hello,
here are two ext4 fixes for nojournal mode. The first fix is fixing handling of uninitialized inodes in recently_deleted() used in nojournal mode which was leading to occasional fstests failures for me (which became much more likely after the second patch due to timing changes). The second patch fixes a bug in ext4_fsync() which was not properly writing out inode metadata in nojournal mode. It is kind of a band aid but proper solution is going to be rather intrusive and practically unbackportable so I think having it is worth it.
Honza
Jan Kara
reviewerPATCH 1/2 ext4: Make recently_deleted() properly work with lazy itable initialization
recently_deleted() checks whether inode has been used in the near past. However this can give false positive result when inode table is not initialized yet and we are in fact comparing to random garbage (or stale itable block of a filesystem before mkfs). Ultimately this results in uninitialized inodes being skipped during inode allocation and possibly they are never initialized and thus e2fsck complains. Verify if the inode has been initialized before checking for dtime.
--- fs/ext4/ialloc.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/ext4/ialloc.c +6 -0
@@ -686,6 +686,12 @@ static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)if (unlikely (!gdp))return 0 ; +/* Inode was never used in this filesystem? */ +if (ext4_has_group_desc_csum (sb) && + (gdp->bg_flags &cpu_to_le16 (EXT4_BG_INODE_UNINIT ) || + ino >=EXT4_INODES_PER_GROUP (sb) -ext4_itable_unused_count (sb, gdp))) +return 0 ; + bh =sb_find_get_block (sb,ext4_inode_table (sb, gdp) + (ino / inodes_per_block));if (!bh || !buffer_uptodate (bh))
Signature · 1 line
2.51.0
Re: [PATCH 1/2] ext4: Make recently_deleted() properly work with lazy itable initialization
Quoted 9 lines · Jan Kara
recently_deleted()checks whether inode has been used in the near past. However this can give false positive result when inode table is not initialized yet and we are in fact comparing to random garbage (or stale itable block of a filesystem before mkfs). Ultimately this results in uninitialized inodes being skipped during inode allocation and possibly they are never initialized and thus e2fsck complains. Verify if the inode has been initialized before checking for dtime.Signed-off-by Jan Kara <[email protected]>
Ha, this is a nice catch! Looks good to me.
Quoted 21 lines
--- fs/ext4/ialloc.c | 6 ++++++ 1 file changed, 6 insertions(+)fs/ext4/ialloc.c +6
-0@@ -686,6 +686,12 @@ static int recently_deleted(struct super_block *sb, ext4_group_t group, int ino)if (unlikely (!gdp))return 0 ; +/* Inode was never used in this filesystem? */ +if (ext4_has_group_desc_csum (sb) && + (gdp->bg_flags &cpu_to_le16 (EXT4_BG_INODE_UNINIT ) || + ino >=EXT4_INODES_PER_GROUP (sb) -ext4_itable_unused_count (sb, gdp))) +return 0 ; + bh =sb_find_get_block (sb,ext4_inode_table (sb, gdp) + (ino / inodes_per_block));if (!bh || !buffer_uptodate (bh))
Jan Kara
reviewerPATCH 2/2 ext4: Fix fsync(2) for nojournal mode
In reply to Jan KaraWhen inode metadata is changed, we sometimes just call ext4_mark_inode_dirty() to track modified metadata. This copies inode metadata into block buffer which is enough when we are journalling metadata. However when we are running in nojournal mode we currently fail to write the dirtied inode buffer during fsync(2) because the inode is not marked as dirty. Use explicit ext4_write_inode() call to make sure the inode table buffer is written to the disk. This is a band aid solution but proper solution requires a much larger rewrite including changes in metadata bh tracking infrastructure.
--- fs/ext4/fsync.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
fs/ext4/fsync.c +14 -2
@@ -83,11 +83,23 @@ static int ext4_fsync_nojournal(struct file *file, loff_t start, loff_t end, int datasync, bool *needs_barrier) { struct inode *inode = file->f_inode; +struct writeback_control wbc = { + .sync_mode =WB_SYNC_ALL , + .nr_to_write =0 , + };int ret; ret =generic_buffers_fsync_noflush (file, start, end, datasync); -if (!ret) - ret =ext4_sync_parent (inode); +if (ret) +return ret; + +/* Force writeout of inode table buffer to disk */ + ret =ext4_write_inode (inode, &wbc); +if (ret) +return ret; + + ret =ext4_sync_parent (inode); +if (test_opt (inode->i_sb,BARRIER )) *needs_barrier = true;
Signature · 1 line
2.51.0
Re: [PATCH 2/2] ext4: Fix fsync(2) for nojournal mode
Quoted 6 lines · Jan Kara
When inode metadata is changed, we sometimes just call
ext4_mark_inode_dirty()to track modified metadata. This copies inode metadata into block buffer which is enough when we are journalling metadata. However when we are running in nojournal mode we currently fail to write the dirtied inode buffer during fsync(2) because the inode is not marked as dirty.
Please let me understand this. You mean that because some places we directly call ext4_mark_inode_dirty() to mark the inode as dirty, instead of using the generic mark_inode_dirty(), this results in the inode missing the I_DIRTY_INODE flag. Consequently, generic_buffers_fsync_noflush()-> sync_inode_metadata() does not write the inode, leading to the metadata not being updated on disk after fsync(2), right?
Quoted 43 lines
Use explicit
ext4_write_inode()call to make
sure the inode table buffer is written to the disk. This is a band aid solution but proper solution requires a much larger rewrite including changes in metadata bh tracking infrastructure.Reported-by Free Ekanayaka <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/ext4/fsync.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)fs/ext4/fsync.c +14
-2@@ -83,11 +83,23 @@ static int ext4_fsync_nojournal(struct file *file, loff_t start, loff_t end, int datasync, bool *needs_barrier) { struct inode *inode = file->f_inode; +struct writeback_control wbc = { + .sync_mode =WB_SYNC_ALL , + .nr_to_write =0 , + };int ret; ret =generic_buffers_fsync_noflush (file, start, end, datasync); -if (!ret) - ret =ext4_sync_parent (inode); +if (ret) +return ret; + +/* Force writeout of inode table buffer to disk */ + ret =ext4_write_inode (inode, &wbc); +if (ret) +return ret; + + ret =ext4_sync_parent (inode); +if (test_opt (inode->i_sb,BARRIER )) *needs_barrier = true;
Jan Kara
reviewerRe: [PATCH 2/2] ext4: Fix fsync(2) for nojournal mode
Quoted 8 lines · Zhang Yi
Quoted 6 lines · Jan Kara
When inode metadata is changed, we sometimes just call
ext4_mark_inode_dirty()to track modified metadata. This copies inode metadata into block buffer which is enough when we are journalling metadata. However when we are running in nojournal mode we currently fail to write the dirtied inode buffer during fsync(2) because the inode is not marked as dirty.
Quoted 6 lines
Please let me understand this. You mean that because some places we directly call
ext4_mark_inode_dirty()to mark the inode as dirty, instead of using the genericmark_inode_dirty(), this results in the inode missing theI_DIRTY_INODEflag. Consequently, generic_buffers_fsync_noflush()->sync_inode_metadata()does not write the inode, leading to the metadata not being updated on disk after fsync(2), right?
Correct.
Quoted 1 line
Reviewed-by Zhang Yi <[email protected]>
Thanks for review!
Honza
Quoted 44 lines
Quoted 43 lines
Use explicit
ext4_write_inode()call to make
sure the inode table buffer is written to the disk. This is a band aid solution but proper solution requires a much larger rewrite including changes in metadata bh tracking infrastructure.Reported-by Free Ekanayaka <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/ext4/fsync.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)fs/ext4/fsync.c +14
-2@@ -83,11 +83,23 @@ static int ext4_fsync_nojournal(struct file *file, loff_t start, loff_t end, int datasync, bool *needs_barrier) { struct inode *inode = file->f_inode; +struct writeback_control wbc = { + .sync_mode =WB_SYNC_ALL , + .nr_to_write =0 , + };int ret; ret =generic_buffers_fsync_noflush (file, start, end, datasync); -if (!ret) - ret =ext4_sync_parent (inode); +if (ret) +return ret; + +/* Force writeout of inode table buffer to disk */ + ret =ext4_write_inode (inode, &wbc); +if (ret) +return ret; + + ret =ext4_sync_parent (inode); +if (test_opt (inode->i_sb,BARRIER )) *needs_barrier = true;
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 9 lines · Jan Kara
here are two ext4 fixes for nojournal mode. The first fix is fixing handling of uninitialized inodes in
recently_deleted()used in nojournal mode which was leading to occasional fstests failures for me (which became much more likely after the second patch due to timing changes). The second patch fixes a bug inext4_fsync()which was not properly writing out inode metadata in nojournal mode. It is kind of a band aid but proper solution is going to be rather intrusive and practically unbackportable so I think having it is worth it.[...]
Applied, thanks!
[1/2] ext4: Make recently_deleted() properly work with lazy itable initialization
commit: cec4673c65d709a0c0354c0a0bbbba5cb7508a9c
[2/2] ext4: Fix fsync(2) for nojournal mode
commit: 3a0fb9e501707760341b5f4562e0f8409bed126aBest regards,
Signature · 1 line
Theodore Ts'o <[email protected]>