Skip to contentSkip to conversation

iomap: Waiting for IO in iomap_dio_rw()

PATCH In review Jan Kara 3 participants 7 messages Oct 14, 2019

Subsystems (4)
lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

v3 cover iomap: Waiting for IO in iomap_dio_rw()

Hello,

here is new version of the series with the small change requested by Darrick.

Changes since v2:
* Changed iomap_dio_rw() to return -EIO in case caller doesn't ask it to wait for sync kiocb.

Changes since v1:
* The new function argument of iomap_dio_rw() does not get overridden by is_sync_kiocb() the caller is responsible for this.

---
Original motivation:

when doing the ext4 conversion of direct IO code to iomap, we found it very difficult to handle inode extension with what iomap code currently provides. Ext4 wants to do inode extension as sync IO (so that the whole duration of IO is protected by inode->i_rwsem), also we need to truncate blocks beyond end of file in case of error or short write. Now in ->end_io handler we don't have the information how long originally the write was (to judge whether we may have allocated more blocks than we actually used) and in ->write_iter we don't know whether / how much of the IO actually succeeded in case of AIO.

Thinking about it for some time I think iomap code makes it unnecessarily complex for the filesystem in case it decides it doesn't want to perform AIO and wants to fall back to good old synchronous IO. In such case it is much easier for the filesystem if it just gets normal error return from iomap_dio_rw() and not just -EIOCBQUEUED.

The first patch in the series adds argument to iomap_dio_rw() to wait for IO completion (internally iomap_dio_rw() already supports this!) and the second patch converts XFS waiting for unaligned DIO write to this new API.

Honza

Previous versions:

author reviewer
#1 lore raw

PATCH v3 1/2 iomap: Allow forcing of waiting for running DIO in iomap_dio_rw()

Filesystems do not support doing IO as asynchronous in some cases. For example in case of unaligned writes or in case file size needs to be extended (e.g. for ext4). Instead of forcing filesystem to wait for AIO in such cases, add argument to iomap_dio_rw() which makes the function wait for IO completion. This also results in executing iomap_dio_complete() inline in iomap_dio_rw() providing its return value to the caller as for ordinary sync IO.

---
 fs/gfs2/file.c        | 6 ++++--
 fs/iomap/direct-io.c  | 7 +++++--
 fs/xfs/xfs_file.c     | 5 +++--
 include/linux/iomap.h | 3 ++-
 4 files changed, 14 insertions(+), 7 deletions(-)
fs/gfs2/file.c +4 -2
index 997b326247e2..f0caee2b7c00 100644
@@ -732,7 +732,8 @@ static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to)
 	if (ret)
 		goto out_uninit;
 
-	ret = iomap_dio_rw(iocb, to, &gfs2_iomap_ops, NULL);
+	ret = iomap_dio_rw(iocb, to, &gfs2_iomap_ops, NULL,
+			   is_sync_kiocb(iocb));
 
 	gfs2_glock_dq(&gh);
 out_uninit:
@@ -767,7 +768,8 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
 	if (offset + len > i_size_read(&ip->i_inode))
 		goto out;
 
-	ret = iomap_dio_rw(iocb, from, &gfs2_iomap_ops, NULL);
+	ret = iomap_dio_rw(iocb, from, &gfs2_iomap_ops, NULL,
+			   is_sync_kiocb(iocb));
 
 out:
 	gfs2_glock_dq(&gh);
fs/iomap/direct-io.c +5 -2
index 1fc28c2da279..da124cee1783 100644
@@ -392,7 +392,8 @@ iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
  */
 ssize_t
 iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
-		const struct iomap_ops *ops, const struct iomap_dio_ops *dops)
+		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
+		bool wait_for_completion)
 {
 	struct address_space *mapping = iocb->ki_filp->f_mapping;
 	struct inode *inode = file_inode(iocb->ki_filp);
@@ -400,7 +401,6 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 	loff_t pos = iocb->ki_pos, start = pos;
 	loff_t end = iocb->ki_pos + count - 1, ret = 0;
 	unsigned int flags = IOMAP_DIRECT;
-	bool wait_for_completion = is_sync_kiocb(iocb);
 	struct blk_plug plug;
 	struct iomap_dio *dio;
 
@@ -409,6 +409,9 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
 	if (!count)
 		return 0;
 
+	if (WARN_ON(is_sync_kiocb(iocb) && !wait_for_completion))
+		return -EIO;
+
 	dio = kmalloc(sizeof(*dio), GFP_KERNEL);
 	if (!dio)
 		return -ENOMEM;
fs/xfs/xfs_file.c +3 -2
index 1ffb179f35d2..0739ba72a82e 100644
@@ -188,7 +188,7 @@ xfs_file_dio_aio_read(
 	file_accessed(iocb->ki_filp);
 
 	xfs_ilock(ip, XFS_IOLOCK_SHARED);
-	ret = iomap_dio_rw(iocb, to, &xfs_iomap_ops, NULL);
+	ret = iomap_dio_rw(iocb, to, &xfs_iomap_ops, NULL, is_sync_kiocb(iocb));
 	xfs_iunlock(ip, XFS_IOLOCK_SHARED);
 
 	return ret;
@@ -547,7 +547,8 @@ xfs_file_dio_aio_write(
 	}
 
 	trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
-	ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, &xfs_dio_write_ops);
+	ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, &xfs_dio_write_ops,
+			   is_sync_kiocb(iocb));
 
 	/*
 	 * If unaligned, this is the only IO in-flight. If it has not yet
include/linux/iomap.h +2 -1
index 7aa5d6117936..76b14cb729dc 100644
@@ -195,7 +195,8 @@ struct iomap_dio_ops {
 };
 
 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
-		const struct iomap_ops *ops, const struct iomap_dio_ops *dops);
+		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
+		bool wait_for_completion);
 int iomap_dio_iopoll(struct kiocb *kiocb, bool spin);
 
 #ifdef CONFIG_SWAP
Signature · 1 line
2.16.4
author reviewer
#3 lore raw

PATCH v3 2/2 xfs: Use iomap_dio_rw_wait()

In reply to Jan Kara

Use iomap_dio_rw() to wait for unaligned direct IO instead of opencoding the wait.

---
 fs/xfs/xfs_file.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
fs/xfs/xfs_file.c +4 -8
index 0739ba72a82e..c0620135a279 100644
@@ -547,16 +547,12 @@ xfs_file_dio_aio_write(
 	}
 
 	trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
-	ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, &xfs_dio_write_ops,
-			   is_sync_kiocb(iocb));
-
 	/*
-	 * If unaligned, this is the only IO in-flight. If it has not yet
-	 * completed, wait on it before we release the iolock to prevent
-	 * subsequent overlapping IO.
+	 * If unaligned, this is the only IO in-flight. Wait on it before we
+	 * release the iolock to prevent subsequent overlapping IO.
 	 */
-	if (ret == -EIOCBQUEUED && unaligned_io)
-		inode_dio_wait(inode);
+	ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, &xfs_dio_write_ops,
+			   is_sync_kiocb(iocb) || unaligned_io);
 out:
 	xfs_iunlock(ip, iolock);
 
Signature · 1 line
2.16.4
#5 lore raw
In reply to Jan Kara
Quoted 17 lines · Jan Kara

Original motivation:

when doing the ext4 conversion of direct IO code to iomap, we found it very difficult to handle inode extension with what iomap code currently provides. Ext4 wants to do inode extension as sync IO (so that the whole duration of IO is protected by inode->i_rwsem), also we need to truncate blocks beyond end of file in case of error or short write. Now in ->end_io handler we don't have the information how long originally the write was (to judge whether we may have allocated more blocks than we actually used) and in ->write_iter we don't know whether / how much of the IO actually succeeded in case of AIO.

Thinking about it for some time I think iomap code makes it unnecessarily complex for the filesystem in case it decides it doesn't want to perform AIO and wants to fall back to good old synchronous IO. In such case it is much easier for the filesystem if it just gets normal error return from iomap_dio_rw() and not just -EIOCBQUEUED.

Quoted 3 lines

The first patch in the series adds argument to iomap_dio_rw() to wait for IO completion (internally iomap_dio_rw() already supports this!) and the second patch converts XFS waiting for unaligned DIO write to this new API.

Ah, wonderful, I was waiting for this to come through.

I'll rebase my EXT4 direct I/O port on top of these patches and apply the discussed changes. Any objections? :)

--<M>--

author reviewer
#6 lore raw
Quoted 18 lines · Matthew Bobrowski
Quoted 17 lines · Jan Kara

Original motivation:

when doing the ext4 conversion of direct IO code to iomap, we found it very difficult to handle inode extension with what iomap code currently provides. Ext4 wants to do inode extension as sync IO (so that the whole duration of IO is protected by inode->i_rwsem), also we need to truncate blocks beyond end of file in case of error or short write. Now in ->end_io handler we don't have the information how long originally the write was (to judge whether we may have allocated more blocks than we actually used) and in ->write_iter we don't know whether / how much of the IO actually succeeded in case of AIO.

Thinking about it for some time I think iomap code makes it unnecessarily complex for the filesystem in case it decides it doesn't want to perform AIO and wants to fall back to good old synchronous IO. In such case it is much easier for the filesystem if it just gets normal error return from iomap_dio_rw() and not just -EIOCBQUEUED.

Quoted 8 lines
Quoted 3 lines

The first patch in the series adds argument to iomap_dio_rw() to wait for IO completion (internally iomap_dio_rw() already supports this!) and the second patch converts XFS waiting for unaligned DIO write to this new API.

Ah, wonderful, I was waiting for this to come through.

I'll rebase my EXT4 direct I/O port on top of these patches and apply the discussed changes. Any objections? :)

No, go ahead. Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR