diff mbox series

[08/12] file-posix: Move read/write operation logic out of aio_worker()

Message ID 20181031215622.27690-9-kwolf@redhat.com (mailing list archive)
State New, archived
Headers show
Series file-posix: Simplify delegation to worker thread | expand

Commit Message

Kevin Wolf Oct. 31, 2018, 9:56 p.m. UTC
aio_worker() for reads and writes isn't boring enough yet. It still does
some postprocessing for handling short reads and turning the result into
the right return value.

However, there is no reason why handle_aiocb_rw() couldn't do the same,
and even without duplicating code between the read and write path. So
move the code there.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/file-posix.c | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)

Comments

Kevin Wolf Nov. 15, 2018, 4:17 p.m. UTC | #1
Am 31.10.2018 um 22:56 hat Kevin Wolf geschrieben:
> aio_worker() for reads and writes isn't boring enough yet. It still does
> some postprocessing for handling short reads and turning the result into
> the right return value.
> 
> However, there is no reason why handle_aiocb_rw() couldn't do the same,
> and even without duplicating code between the read and write path. So
> move the code there.
> 
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>

> @@ -1354,7 +1356,21 @@ static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
>      }
>      qemu_vfree(buf);
>  
> -    return nbytes;
> +out:
> +    if (nbytes == aiocb->aio_nbytes) {
> +        return 0;
> +    } else if (nbytes >= 0 && nbytes < aiocb->aio_nbytes) {
> +        if (aiocb->aio_type & QEMU_AIO_WRITE) {
> +            return -EINVAL;
> +        } else {
> +            iov_memset(aiocb->io.iov, aiocb->io.niov, nbytes,
> +                      0, aiocb->aio_nbytes - nbytes);
> +            return aiocb->aio_nbytes;

While reviewing Eric's patches, I noticed that this should be return 0.
Fixing it in my queue.

Kevin
diff mbox series

Patch

diff --git a/block/file-posix.c b/block/file-posix.c
index 074848ed1f..4989208ba3 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -1290,7 +1290,8 @@  static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
          * we can just use plain pread/pwrite without any problems.
          */
         if (aiocb->io.niov == 1) {
-             return handle_aiocb_rw_linear(aiocb, aiocb->io.iov->iov_base);
+             nbytes = handle_aiocb_rw_linear(aiocb, aiocb->io.iov->iov_base);
+             goto out;
         }
         /*
          * We have more than one iovec, and all are properly aligned.
@@ -1302,7 +1303,7 @@  static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
             nbytes = handle_aiocb_rw_vector(aiocb);
             if (nbytes == aiocb->aio_nbytes ||
                 (nbytes < 0 && nbytes != -ENOSYS)) {
-                return nbytes;
+                goto out;
             }
             preadv_present = false;
         }
@@ -1320,7 +1321,8 @@  static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
      */
     buf = qemu_try_blockalign(aiocb->bs, aiocb->aio_nbytes);
     if (buf == NULL) {
-        return -ENOMEM;
+        nbytes = -ENOMEM;
+        goto out;
     }
 
     if (aiocb->aio_type & QEMU_AIO_WRITE) {
@@ -1354,7 +1356,21 @@  static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
     }
     qemu_vfree(buf);
 
-    return nbytes;
+out:
+    if (nbytes == aiocb->aio_nbytes) {
+        return 0;
+    } else if (nbytes >= 0 && nbytes < aiocb->aio_nbytes) {
+        if (aiocb->aio_type & QEMU_AIO_WRITE) {
+            return -EINVAL;
+        } else {
+            iov_memset(aiocb->io.iov, aiocb->io.niov, nbytes,
+                      0, aiocb->aio_nbytes - nbytes);
+            return aiocb->aio_nbytes;
+        }
+    } else {
+        assert(nbytes < 0);
+        return nbytes;
+    }
 }
 
 #ifdef CONFIG_XFS
@@ -1758,25 +1774,9 @@  static int aio_worker(void *arg)
     switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
     case QEMU_AIO_READ:
         ret = handle_aiocb_rw(aiocb);
-        if (ret >= 0 && ret < aiocb->aio_nbytes) {
-            iov_memset(aiocb->io.iov, aiocb->io.niov, ret,
-                      0, aiocb->aio_nbytes - ret);
-
-            ret = aiocb->aio_nbytes;
-        }
-        if (ret == aiocb->aio_nbytes) {
-            ret = 0;
-        } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
-            ret = -EINVAL;
-        }
         break;
     case QEMU_AIO_WRITE:
         ret = handle_aiocb_rw(aiocb);
-        if (ret == aiocb->aio_nbytes) {
-            ret = 0;
-        } else if (ret >= 0 && ret < aiocb->aio_nbytes) {
-            ret = -EINVAL;
-        }
         break;
     case QEMU_AIO_IOCTL:
         ret = handle_aiocb_ioctl(aiocb);