diff mbox

[4/4,V2] kvm tools: Complete missing segments in a iov op using regular op

Message ID 1303131959-25309-1-git-send-email-levinsasha928@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Sasha Levin April 18, 2011, 1:05 p.m. UTC
If any of the iov operations return mid-block, use regular ops to complete the current block and continue using iov ops.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
---
 tools/kvm/read-write.c |   58 ++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 51 insertions(+), 7 deletions(-)

Comments

Pekka Enberg April 20, 2011, 7:28 a.m. UTC | #1
Hi,

[ Sasha, please remember to CC people who were involved in discussions! ]

On Mon, Apr 18, 2011 at 4:05 PM, Sasha Levin <levinsasha928@gmail.com> wrote:
> If any of the iov operations return mid-block, use regular ops to complete the current block and continue using iov ops.
>
> Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
> ---
>  tools/kvm/read-write.c |   58 ++++++++++++++++++++++++++++++++++++++++++-----
>  1 files changed, 51 insertions(+), 7 deletions(-)
>
> diff --git a/tools/kvm/read-write.c b/tools/kvm/read-write.c
> index 0c995c8..bf2e4a0 100644
> --- a/tools/kvm/read-write.c
> +++ b/tools/kvm/read-write.c
> @@ -189,10 +189,10 @@ static inline ssize_t get_iov_size(const struct iovec *iov, int iovcnt)
>  }
>
>  static inline void shift_iovec(const struct iovec **iov, int *iovcnt,
> -                               size_t nr, ssize_t *total, size_t *count, off_t *offset)
> +                               ssize_t *nr, ssize_t *total, size_t *count, off_t *offset)
>  {
> -       while (nr >= (*iov)->iov_len) {
> -               nr -= (*iov)->iov_len;
> +       while ((size_t)*nr >= (*iov)->iov_len) {
> +               *nr -= (*iov)->iov_len;
>                *total += (*iov)->iov_len;
>                *count -= (*iov)->iov_len;
>                if (offset)
> @@ -218,7 +218,18 @@ ssize_t readv_in_full(int fd, const struct iovec *iov, int iovcnt)
>                        return -1;
>                }
>
> -               shift_iovec(&iov, &iovcnt, nr, &total, &count, NULL);
> +               shift_iovec(&iov, &iovcnt, &nr, &total, &count, NULL);
> +
> +               while (nr > 0) {
> +                       ssize_t nr_readagain;
> +                       nr_readagain = xread(fd, iov->iov_base + nr,
> +                                                                       iov->iov_len - nr);
> +                       if (nr_readagain <= 0)
> +                               return total;
> +
> +                       nr += nr_readagain;
> +                       shift_iovec(&iov, &iovcnt, &nr, &total, &count, NULL);
> +               }
>        }
>
>        return total;

As mentioned on IRC, I hate this patch with a passion. ;-)

We don't do O_DIRECT now so this doesn't help with performance and the
extra complexity it brings to the table isn't very appealing.
Modifying the struct iovec (or making a copy of it) seems to be much
nicer approach.

                        Pekka
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/tools/kvm/read-write.c b/tools/kvm/read-write.c
index 0c995c8..bf2e4a0 100644
--- a/tools/kvm/read-write.c
+++ b/tools/kvm/read-write.c
@@ -189,10 +189,10 @@  static inline ssize_t get_iov_size(const struct iovec *iov, int iovcnt)
 }
 
 static inline void shift_iovec(const struct iovec **iov, int *iovcnt,
-				size_t nr, ssize_t *total, size_t *count, off_t *offset)
+				ssize_t *nr, ssize_t *total, size_t *count, off_t *offset)
 {
-	while (nr >= (*iov)->iov_len) {
-		nr -= (*iov)->iov_len;
+	while ((size_t)*nr >= (*iov)->iov_len) {
+		*nr -= (*iov)->iov_len;
 		*total += (*iov)->iov_len;
 		*count -= (*iov)->iov_len;
 		if (offset)
@@ -218,7 +218,18 @@  ssize_t readv_in_full(int fd, const struct iovec *iov, int iovcnt)
 			return -1;
 		}
 
-		shift_iovec(&iov, &iovcnt, nr, &total, &count, NULL);
+		shift_iovec(&iov, &iovcnt, &nr, &total, &count, NULL);
+
+		while (nr > 0) {
+			ssize_t nr_readagain;
+			nr_readagain = xread(fd, iov->iov_base + nr,
+									iov->iov_len - nr);
+			if (nr_readagain <= 0)
+				return total;
+
+			nr += nr_readagain;
+			shift_iovec(&iov, &iovcnt, &nr, &total, &count, NULL);
+		}
 	}
 
 	return total;
@@ -240,7 +251,18 @@  ssize_t writev_in_full(int fd, const struct iovec *iov, int iovcnt)
 			return -1;
 		}
 
-		shift_iovec(&iov, &iovcnt, nr, &total, &count, NULL);
+		shift_iovec(&iov, &iovcnt, &nr, &total, &count, NULL);
+
+		while (nr > 0) {
+			ssize_t nr_writeagain;
+			nr_writeagain = xwrite(fd, iov->iov_base + nr,
+									iov->iov_len - nr);
+			if (nr_writeagain <= 0)
+				return total;
+
+			nr += nr_writeagain;
+			shift_iovec(&iov, &iovcnt, &nr, &total, &count, NULL);
+		}
 	}
 
 	return total;
@@ -288,7 +310,18 @@  ssize_t preadv_in_full(int fd, const struct iovec *iov, int iovcnt, off_t offset
 			return -1;
 		}
 
-		shift_iovec(&iov, &iovcnt, nr, &total, &count, &offset);
+		shift_iovec(&iov, &iovcnt, &nr, &total, &count, &offset);
+
+		while (nr > 0) {
+			ssize_t nr_readagain;
+			nr_readagain = xpread(fd, iov->iov_base + nr,
+									iov->iov_len - nr, offset + nr);
+			if (nr_readagain <= 0)
+				return total;
+
+			nr += nr_readagain;
+			shift_iovec(&iov, &iovcnt, &nr, &total, &count, &offset);
+		}
 	}
 
 	return total;
@@ -310,7 +343,18 @@  ssize_t pwritev_in_full(int fd, const struct iovec *iov, int iovcnt, off_t offse
 			return -1;
 		}
 
-		shift_iovec(&iov, &iovcnt, nr, &total, &count, &offset);
+		shift_iovec(&iov, &iovcnt, &nr, &total, &count, &offset);
+
+		while (nr > 0) {
+			ssize_t nr_writeagain;
+			nr_writeagain = xpwrite(fd, iov->iov_base + nr,
+									iov->iov_len - nr, offset + nr);
+			if (nr_writeagain <= 0)
+				return total;
+
+			nr += nr_writeagain;
+			shift_iovec(&iov, &iovcnt, &nr, &total, &count, &offset);
+		}
 	}
 
 	return total;