Some filesystems (e.g. ext4) need to know in it's writeback path, that whether DIO is in progress or not. This info may be needed to avoid the stale data exposure race with DIO reads. Signed-off-by: Ritesh Harjani <riteshh@xxxxxxxxxxxxx> --- fs/iomap/direct-io.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c index 23837926c0c5..d1c159bd3854 100644 --- a/fs/iomap/direct-io.c +++ b/fs/iomap/direct-io.c @@ -468,9 +468,18 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, flags |= IOMAP_NOWAIT; } + /* + * Call inode_dio_begin() before we write out and wait for writeback to + * complete. This may be needed by some filesystems to prevent race + * like stale data exposure by DIO reads. + */ + inode_dio_begin(inode); + /* So that i_dio_count is incremented before below operation */ + smp_mb__after_atomic(); + ret = filemap_write_and_wait_range(mapping, pos, end); if (ret) - goto out_free_dio; + goto out_end_dio; /* * Try to invalidate cache pages for the range we're direct @@ -488,11 +497,9 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, !inode->i_sb->s_dio_done_wq) { ret = sb_init_dio_done_wq(inode->i_sb); if (ret < 0) - goto out_free_dio; + goto out_end_dio; } - inode_dio_begin(inode); - blk_start_plug(&plug); do { ret = iomap_apply(inode, pos, count, flags, ops, dio, @@ -568,6 +575,8 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, return iomap_dio_complete(dio); +out_end_dio: + inode_dio_end(inode); out_free_dio: kfree(dio); return ret; -- 2.21.0