Skip to content

Commit 2931cdc

Browse files
djwongtorvalds
authored andcommitted
ocfs2: improve fsync efficiency and fix deadlock between aio_write and sync_file
Currently, ocfs2_sync_file grabs i_mutex and forces the current journal transaction to complete. This isn't terribly efficient, since sync_file really only needs to wait for the last transaction involving that inode to complete, and this doesn't require i_mutex. Therefore, implement the necessary bits to track the newest tid associated with an inode, and teach sync_file to wait for that instead of waiting for everything in the journal to commit. Furthermore, only issue the flush request to the drive if jbd2 hasn't already done so. This also eliminates the deadlock between ocfs2_file_aio_write() and ocfs2_sync_file(). aio_write takes i_mutex then calls ocfs2_aiodio_wait() to wait for unaligned dio writes to finish. However, if that dio completion involves calling fsync, then we can get into trouble when some ocfs2_sync_file tries to take i_mutex. Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> Reviewed-by: Mark Fasheh <mfasheh@suse.de> Cc: Joel Becker <jlbec@evilplan.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a75fe48 commit 2931cdc

9 files changed

Lines changed: 74 additions & 21 deletions

File tree

‎fs/ocfs2/alloc.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6932,6 +6932,7 @@ int ocfs2_convert_inline_data_to_extents(struct inode *inode,
69326932
di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
69336933
spin_unlock(&oi->ip_lock);
69346934

6935+
ocfs2_update_inode_fsync_trans(handle, inode, 1);
69356936
ocfs2_dinode_new_extent_list(inode, di);
69366937

69376938
ocfs2_journal_dirty(handle, di_bh);

‎fs/ocfs2/aops.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,7 @@ int ocfs2_write_end_nolock(struct address_space *mapping,
20392039
inode->i_mtime = inode->i_ctime = CURRENT_TIME;
20402040
di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
20412041
di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
2042+
ocfs2_update_inode_fsync_trans(handle, inode, 1);
20422043
ocfs2_journal_dirty(handle, wc->w_di_bh);
20432044

20442045
ocfs2_commit_trans(osb, handle);

‎fs/ocfs2/dir.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,6 +2957,7 @@ static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
29572957
ocfs2_init_dir_trailer(dir, dirdata_bh, i);
29582958
}
29592959

2960+
ocfs2_update_inode_fsync_trans(handle, dir, 1);
29602961
ocfs2_journal_dirty(handle, dirdata_bh);
29612962

29622963
if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
@@ -3338,6 +3339,7 @@ static int ocfs2_extend_dir(struct ocfs2_super *osb,
33383339
} else {
33393340
de->rec_len = cpu_to_le16(sb->s_blocksize);
33403341
}
3342+
ocfs2_update_inode_fsync_trans(handle, dir, 1);
33413343
ocfs2_journal_dirty(handle, new_bh);
33423344

33433345
dir_i_size += dir->i_sb->s_blocksize;
@@ -3896,6 +3898,7 @@ static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
38963898
dquot_free_space_nodirty(dir,
38973899
ocfs2_clusters_to_bytes(dir->i_sb, 1));
38983900

3901+
ocfs2_update_inode_fsync_trans(handle, dir, 1);
38993902
ocfs2_commit_trans(osb, handle);
39003903

39013904
out:
@@ -4134,6 +4137,7 @@ static int ocfs2_expand_inline_dx_root(struct inode *dir,
41344137
mlog_errno(ret);
41354138
did_quota = 0;
41364139

4140+
ocfs2_update_inode_fsync_trans(handle, dir, 1);
41374141
ocfs2_journal_dirty(handle, dx_root_bh);
41384142

41394143
out_commit:

‎fs/ocfs2/file.c‎

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,13 @@ static int ocfs2_sync_file(struct file *file, loff_t start, loff_t end,
175175
int datasync)
176176
{
177177
int err = 0;
178-
journal_t *journal;
179178
struct inode *inode = file->f_mapping->host;
180179
struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
180+
struct ocfs2_inode_info *oi = OCFS2_I(inode);
181+
journal_t *journal = osb->journal->j_journal;
182+
int ret;
183+
tid_t commit_tid;
184+
bool needs_barrier = false;
181185

182186
trace_ocfs2_sync_file(inode, file, file->f_path.dentry,
183187
OCFS2_I(inode)->ip_blkno,
@@ -192,29 +196,19 @@ static int ocfs2_sync_file(struct file *file, loff_t start, loff_t end,
192196
if (err)
193197
return err;
194198

195-
/*
196-
* Probably don't need the i_mutex at all in here, just putting it here
197-
* to be consistent with how fsync used to be called, someone more
198-
* familiar with the fs could possibly remove it.
199-
*/
200-
mutex_lock(&inode->i_mutex);
201-
if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) {
202-
/*
203-
* We still have to flush drive's caches to get data to the
204-
* platter
205-
*/
206-
if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
207-
blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
208-
goto bail;
199+
commit_tid = datasync ? oi->i_datasync_tid : oi->i_sync_tid;
200+
if (journal->j_flags & JBD2_BARRIER &&
201+
!jbd2_trans_will_send_data_barrier(journal, commit_tid))
202+
needs_barrier = true;
203+
err = jbd2_complete_transaction(journal, commit_tid);
204+
if (needs_barrier) {
205+
ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
206+
if (!err)
207+
err = ret;
209208
}
210209

211-
journal = osb->journal->j_journal;
212-
err = jbd2_journal_force_commit(journal);
213-
214-
bail:
215210
if (err)
216211
mlog_errno(err);
217-
mutex_unlock(&inode->i_mutex);
218212

219213
return (err < 0) ? -EIO : 0;
220214
}
@@ -650,7 +644,7 @@ static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
650644
mlog_errno(status);
651645
goto leave;
652646
}
653-
647+
ocfs2_update_inode_fsync_trans(handle, inode, 1);
654648
ocfs2_journal_dirty(handle, bh);
655649

656650
spin_lock(&OCFS2_I(inode)->ip_lock);

‎fs/ocfs2/inode.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
130130
struct inode *inode = NULL;
131131
struct super_block *sb = osb->sb;
132132
struct ocfs2_find_inode_args args;
133+
journal_t *journal = OCFS2_SB(sb)->journal->j_journal;
133134

134135
trace_ocfs2_iget_begin((unsigned long long)blkno, flags,
135136
sysfile_type);
@@ -169,6 +170,32 @@ struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
169170
goto bail;
170171
}
171172

173+
/*
174+
* Set transaction id's of transactions that have to be committed
175+
* to finish f[data]sync. We set them to currently running transaction
176+
* as we cannot be sure that the inode or some of its metadata isn't
177+
* part of the transaction - the inode could have been reclaimed and
178+
* now it is reread from disk.
179+
*/
180+
if (journal) {
181+
transaction_t *transaction;
182+
tid_t tid;
183+
struct ocfs2_inode_info *oi = OCFS2_I(inode);
184+
185+
read_lock(&journal->j_state_lock);
186+
if (journal->j_running_transaction)
187+
transaction = journal->j_running_transaction;
188+
else
189+
transaction = journal->j_committing_transaction;
190+
if (transaction)
191+
tid = transaction->t_tid;
192+
else
193+
tid = journal->j_commit_sequence;
194+
read_unlock(&journal->j_state_lock);
195+
oi->i_sync_tid = tid;
196+
oi->i_datasync_tid = tid;
197+
}
198+
172199
bail:
173200
if (!IS_ERR(inode)) {
174201
trace_ocfs2_iget_end(inode,
@@ -1260,6 +1287,7 @@ int ocfs2_mark_inode_dirty(handle_t *handle,
12601287
fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
12611288

12621289
ocfs2_journal_dirty(handle, bh);
1290+
ocfs2_update_inode_fsync_trans(handle, inode, 1);
12631291
leave:
12641292
return status;
12651293
}

‎fs/ocfs2/inode.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ struct ocfs2_inode_info
7373
u32 ip_dir_lock_gen;
7474

7575
struct ocfs2_alloc_reservation ip_la_data_resv;
76+
77+
/*
78+
* Transactions that contain inode's metadata needed to complete
79+
* fsync and fdatasync, respectively.
80+
*/
81+
tid_t i_sync_tid;
82+
tid_t i_datasync_tid;
7683
};
7784

7885
/*

‎fs/ocfs2/journal.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,4 +626,15 @@ static inline int ocfs2_begin_ordered_truncate(struct inode *inode,
626626
new_size);
627627
}
628628

629+
static inline void ocfs2_update_inode_fsync_trans(handle_t *handle,
630+
struct inode *inode,
631+
int datasync)
632+
{
633+
struct ocfs2_inode_info *oi = OCFS2_I(inode);
634+
635+
oi->i_sync_tid = handle->h_transaction->t_tid;
636+
if (datasync)
637+
oi->i_datasync_tid = handle->h_transaction->t_tid;
638+
}
639+
629640
#endif /* OCFS2_JOURNAL_H */

‎fs/ocfs2/namei.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ static int __ocfs2_mknod_locked(struct inode *dir,
495495
struct ocfs2_dinode *fe = NULL;
496496
struct ocfs2_extent_list *fel;
497497
u16 feat;
498+
struct ocfs2_inode_info *oi = OCFS2_I(inode);
498499

499500
*new_fe_bh = NULL;
500501

@@ -576,6 +577,9 @@ static int __ocfs2_mknod_locked(struct inode *dir,
576577
mlog_errno(status);
577578
}
578579

580+
oi->i_sync_tid = handle->h_transaction->t_tid;
581+
oi->i_datasync_tid = handle->h_transaction->t_tid;
582+
579583
status = 0; /* error in ocfs2_create_new_inode_locks is not
580584
* critical */
581585

‎fs/ocfs2/super.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,9 @@ static struct inode *ocfs2_alloc_inode(struct super_block *sb)
561561
if (!oi)
562562
return NULL;
563563

564+
oi->i_sync_tid = 0;
565+
oi->i_datasync_tid = 0;
566+
564567
jbd2_journal_init_jbd_inode(&oi->ip_jinode, &oi->vfs_inode);
565568
return &oi->vfs_inode;
566569
}

0 commit comments

Comments
 (0)