Skip to content

Commit 3eaf9cc

Browse files
matthewbobrowskitytso
authored andcommitted
ext4: update ext4_sync_file() to not use __generic_file_fsync()
When the filesystem is created without a journal, we eventually call into __generic_file_fsync() in order to write out all the modified in-core data to the permanent storage device. This function happens to try and obtain an inode_lock() while synchronizing the files buffer and it's associated metadata. Generally, this is fine, however it becomes a problem when there is higher level code that has already obtained an inode_lock() as this leads to a recursive lock situation. This case is especially true when porting across direct I/O to iomap infrastructure as we obtain an inode_lock() early on in the I/O within ext4_dio_write_iter() and hold it until the I/O has been completed. Consequently, to not run into this specific issue, we move away from calling into __generic_file_fsync() and perform the necessary synchronization tasks within ext4_sync_file(). Signed-off-by: Matthew Bobrowski <[email protected]> Reviewed-by: Ritesh Harjani <[email protected]> Reviewed-by: Jan Kara <[email protected]> Link: https://lore.kernel.org/r/3495f35ef67f2021b567e28e6f59222e583689b8.1572949325.git.mbobrowski@mbobrowski.org Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 0b9f230 commit 3eaf9cc

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

fs/ext4/fsync.c

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,43 @@ static int ext4_sync_parent(struct inode *inode)
8080
return ret;
8181
}
8282

83+
static int ext4_fsync_nojournal(struct inode *inode, bool datasync,
84+
bool *needs_barrier)
85+
{
86+
int ret, err;
87+
88+
ret = sync_mapping_buffers(inode->i_mapping);
89+
if (!(inode->i_state & I_DIRTY_ALL))
90+
return ret;
91+
if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
92+
return ret;
93+
94+
err = sync_inode_metadata(inode, 1);
95+
if (!ret)
96+
ret = err;
97+
98+
if (!ret)
99+
ret = ext4_sync_parent(inode);
100+
if (test_opt(inode->i_sb, BARRIER))
101+
*needs_barrier = true;
102+
103+
return ret;
104+
}
105+
106+
static int ext4_fsync_journal(struct inode *inode, bool datasync,
107+
bool *needs_barrier)
108+
{
109+
struct ext4_inode_info *ei = EXT4_I(inode);
110+
journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
111+
tid_t commit_tid = datasync ? ei->i_datasync_tid : ei->i_sync_tid;
112+
113+
if (journal->j_flags & JBD2_BARRIER &&
114+
!jbd2_trans_will_send_data_barrier(journal, commit_tid))
115+
*needs_barrier = true;
116+
117+
return jbd2_complete_transaction(journal, commit_tid);
118+
}
119+
83120
/*
84121
* akpm: A new design for ext4_sync_file().
85122
*
@@ -91,17 +128,14 @@ static int ext4_sync_parent(struct inode *inode)
91128
* What we do is just kick off a commit and wait on it. This will snapshot the
92129
* inode to disk.
93130
*/
94-
95131
int ext4_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
96132
{
97-
struct inode *inode = file->f_mapping->host;
98-
struct ext4_inode_info *ei = EXT4_I(inode);
99-
journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
100133
int ret = 0, err;
101-
tid_t commit_tid;
102134
bool needs_barrier = false;
135+
struct inode *inode = file->f_mapping->host;
136+
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
103137

104-
if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
138+
if (unlikely(ext4_forced_shutdown(sbi)))
105139
return -EIO;
106140

107141
J_ASSERT(ext4_journal_current_handle() == NULL);
@@ -111,23 +145,15 @@ int ext4_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
111145
if (sb_rdonly(inode->i_sb)) {
112146
/* Make sure that we read updated s_mount_flags value */
113147
smp_rmb();
114-
if (EXT4_SB(inode->i_sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
148+
if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
115149
ret = -EROFS;
116150
goto out;
117151
}
118152

119-
if (!journal) {
120-
ret = __generic_file_fsync(file, start, end, datasync);
121-
if (!ret)
122-
ret = ext4_sync_parent(inode);
123-
if (test_opt(inode->i_sb, BARRIER))
124-
goto issue_flush;
125-
goto out;
126-
}
127-
128153
ret = file_write_and_wait_range(file, start, end);
129154
if (ret)
130155
return ret;
156+
131157
/*
132158
* data=writeback,ordered:
133159
* The caller's filemap_fdatawrite()/wait will sync the data.
@@ -142,18 +168,14 @@ int ext4_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
142168
* (they were dirtied by commit). But that's OK - the blocks are
143169
* safe in-journal, which is all fsync() needs to ensure.
144170
*/
145-
if (ext4_should_journal_data(inode)) {
171+
if (!sbi->s_journal)
172+
ret = ext4_fsync_nojournal(inode, datasync, &needs_barrier);
173+
else if (ext4_should_journal_data(inode))
146174
ret = ext4_force_commit(inode->i_sb);
147-
goto out;
148-
}
175+
else
176+
ret = ext4_fsync_journal(inode, datasync, &needs_barrier);
149177

150-
commit_tid = datasync ? ei->i_datasync_tid : ei->i_sync_tid;
151-
if (journal->j_flags & JBD2_BARRIER &&
152-
!jbd2_trans_will_send_data_barrier(journal, commit_tid))
153-
needs_barrier = true;
154-
ret = jbd2_complete_transaction(journal, commit_tid);
155178
if (needs_barrier) {
156-
issue_flush:
157179
err = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
158180
if (!ret)
159181
ret = err;

0 commit comments

Comments
 (0)