* [PATCH v2 1/7] virtiofsd: Check for EINTR in preadv() and retry
2021-05-18 21:35 [PATCH v2 0/7] virtiofsd: Few cleanups in virtio_send_data_iov() Vivek Goyal
@ 2021-05-18 21:35 ` Vivek Goyal
2021-05-18 21:35 ` [PATCH v2 2/7] virtiofsd: Get rid of unreachable code in read Vivek Goyal
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Vivek Goyal @ 2021-05-18 21:35 UTC (permalink / raw)
To: qemu-devel, virtio-fs; +Cc: ckuehl, dgilbert, vgoyal
We don't seem to check for EINTR and retry. There are other places
in code where we check for EINTR. So lets add a check.
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Connor Kuehl <ckuehl@redhat.com>
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
---
tools/virtiofsd/fuse_virtio.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
index 9efdbd8ffd..755d7fb25c 100644
--- a/tools/virtiofsd/fuse_virtio.c
+++ b/tools/virtiofsd/fuse_virtio.c
@@ -421,6 +421,9 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
if (ret == -1) {
ret = errno;
+ if (ret == EINTR) {
+ continue;
+ }
fuse_log(FUSE_LOG_DEBUG, "%s: preadv failed (%m) len=%zd\n",
__func__, len);
goto err;
--
2.25.4
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 2/7] virtiofsd: Get rid of unreachable code in read
2021-05-18 21:35 [PATCH v2 0/7] virtiofsd: Few cleanups in virtio_send_data_iov() Vivek Goyal
2021-05-18 21:35 ` [PATCH v2 1/7] virtiofsd: Check for EINTR in preadv() and retry Vivek Goyal
@ 2021-05-18 21:35 ` Vivek Goyal
2021-05-18 21:35 ` [PATCH v2 3/7] virtiofsd: Use iov_discard_front() to skip bytes Vivek Goyal
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Vivek Goyal @ 2021-05-18 21:35 UTC (permalink / raw)
To: qemu-devel, virtio-fs; +Cc: ckuehl, dgilbert, vgoyal
pvreadv() can return following.
- error
- 0 in case of EOF
- short read
We seem to handle all the cases already. We are retrying read in case
of short read. So another check for short read seems like dead code.
Get rid of it.
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Connor Kuehl <ckuehl@redhat.com>
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
---
tools/virtiofsd/fuse_virtio.c | 5 -----
1 file changed, 5 deletions(-)
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
index 755d7fb25c..28e2974d1a 100644
--- a/tools/virtiofsd/fuse_virtio.c
+++ b/tools/virtiofsd/fuse_virtio.c
@@ -446,11 +446,6 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
in_sg_left);
break;
}
- if (ret != len) {
- fuse_log(FUSE_LOG_DEBUG, "%s: ret!=len\n", __func__);
- ret = EIO;
- goto err;
- }
in_sg_left -= ret;
len -= ret;
} while (in_sg_left);
--
2.25.4
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 3/7] virtiofsd: Use iov_discard_front() to skip bytes
2021-05-18 21:35 [PATCH v2 0/7] virtiofsd: Few cleanups in virtio_send_data_iov() Vivek Goyal
2021-05-18 21:35 ` [PATCH v2 1/7] virtiofsd: Check for EINTR in preadv() and retry Vivek Goyal
2021-05-18 21:35 ` [PATCH v2 2/7] virtiofsd: Get rid of unreachable code in read Vivek Goyal
@ 2021-05-18 21:35 ` Vivek Goyal
2021-05-18 21:35 ` [PATCH v2 4/7] virtiofsd: get rid of in_sg_left variable Vivek Goyal
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Vivek Goyal @ 2021-05-18 21:35 UTC (permalink / raw)
To: qemu-devel, virtio-fs; +Cc: ckuehl, dgilbert, vgoyal
There are places where we need to skip few bytes from front of the iovec
array. We have our own custom code for that. Looks like iov_discard_front()
can do same thing. So use that helper instead.
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Connor Kuehl <ckuehl@redhat.com>
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
---
tools/virtiofsd/fuse_virtio.c | 14 +++-----------
1 file changed, 3 insertions(+), 11 deletions(-)
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
index 28e2974d1a..09674f2e90 100644
--- a/tools/virtiofsd/fuse_virtio.c
+++ b/tools/virtiofsd/fuse_virtio.c
@@ -389,23 +389,15 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
memcpy(in_sg_cpy, in_sg, sizeof(struct iovec) * in_num);
/* These get updated as we skip */
struct iovec *in_sg_ptr = in_sg_cpy;
- int in_sg_cpy_count = in_num;
+ unsigned int in_sg_cpy_count = in_num;
/* skip over parts of in_sg that contained the header iov */
size_t skip_size = iov_len;
size_t in_sg_left = 0;
do {
- while (skip_size != 0 && in_sg_cpy_count) {
- if (skip_size >= in_sg_ptr[0].iov_len) {
- skip_size -= in_sg_ptr[0].iov_len;
- in_sg_ptr++;
- in_sg_cpy_count--;
- } else {
- in_sg_ptr[0].iov_len -= skip_size;
- in_sg_ptr[0].iov_base += skip_size;
- break;
- }
+ if (skip_size != 0) {
+ iov_discard_front(&in_sg_ptr, &in_sg_cpy_count, skip_size);
}
int i;
--
2.25.4
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 4/7] virtiofsd: get rid of in_sg_left variable
2021-05-18 21:35 [PATCH v2 0/7] virtiofsd: Few cleanups in virtio_send_data_iov() Vivek Goyal
` (2 preceding siblings ...)
2021-05-18 21:35 ` [PATCH v2 3/7] virtiofsd: Use iov_discard_front() to skip bytes Vivek Goyal
@ 2021-05-18 21:35 ` Vivek Goyal
2021-05-18 21:35 ` [PATCH v2 5/7] virtiofsd: Simplify skip byte logic Vivek Goyal
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Vivek Goyal @ 2021-05-18 21:35 UTC (permalink / raw)
To: qemu-devel, virtio-fs; +Cc: ckuehl, dgilbert, vgoyal
in_sg_left seems to be being used primarly for debugging purpose. It is
keeping track of how many bytes are left in the scatter list we are
reading into.
We already have another variable "len" which keeps track how many bytes
are left to be read. And in_sg_left is greater than or equal to len. We
have already ensured that in the beginning of function.
if (in_len < tosend_len) {
fuse_log(FUSE_LOG_ERR, "%s: elem %d too small for data len %zd\n",
__func__, elem->index, tosend_len);
ret = E2BIG;
goto err;
}
So in_sg_left seems like a redundant variable. It probably was useful for
debugging when code was being developed. Get rid of it. It helps simplify
this function.
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Connor Kuehl <ckuehl@redhat.com>
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
---
tools/virtiofsd/fuse_virtio.c | 17 ++++++-----------
1 file changed, 6 insertions(+), 11 deletions(-)
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
index 09674f2e90..ed5146d7a6 100644
--- a/tools/virtiofsd/fuse_virtio.c
+++ b/tools/virtiofsd/fuse_virtio.c
@@ -394,20 +394,16 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
/* skip over parts of in_sg that contained the header iov */
size_t skip_size = iov_len;
- size_t in_sg_left = 0;
do {
if (skip_size != 0) {
iov_discard_front(&in_sg_ptr, &in_sg_cpy_count, skip_size);
}
- int i;
- for (i = 0, in_sg_left = 0; i < in_sg_cpy_count; i++) {
- in_sg_left += in_sg_ptr[i].iov_len;
- }
fuse_log(FUSE_LOG_DEBUG,
"%s: after skip skip_size=%zd in_sg_cpy_count=%d "
- "in_sg_left=%zd\n",
- __func__, skip_size, in_sg_cpy_count, in_sg_left);
+ "len remaining=%zd\n", __func__, skip_size, in_sg_cpy_count,
+ len);
+
ret = preadv(buf->buf[0].fd, in_sg_ptr, in_sg_cpy_count,
buf->buf[0].pos);
@@ -434,13 +430,12 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
}
if (!ret) {
/* EOF case? */
- fuse_log(FUSE_LOG_DEBUG, "%s: !ret in_sg_left=%zd\n", __func__,
- in_sg_left);
+ fuse_log(FUSE_LOG_DEBUG, "%s: !ret len remaining=%zd\n", __func__,
+ len);
break;
}
- in_sg_left -= ret;
len -= ret;
- } while (in_sg_left);
+ } while (len);
/* Need to fix out->len on EOF */
if (len) {
--
2.25.4
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 5/7] virtiofsd: Simplify skip byte logic
2021-05-18 21:35 [PATCH v2 0/7] virtiofsd: Few cleanups in virtio_send_data_iov() Vivek Goyal
` (3 preceding siblings ...)
2021-05-18 21:35 ` [PATCH v2 4/7] virtiofsd: get rid of in_sg_left variable Vivek Goyal
@ 2021-05-18 21:35 ` Vivek Goyal
2021-05-18 21:35 ` [PATCH v2 6/7] virtiofsd: Check EOF before short read Vivek Goyal
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Vivek Goyal @ 2021-05-18 21:35 UTC (permalink / raw)
To: qemu-devel, virtio-fs; +Cc: ckuehl, dgilbert, vgoyal
We need to skip bytes in two cases.
a. Before we start reading into in_sg, we need to skip iov_len bytes
in the beginning which typically will have fuse_out_header.
b. If preadv() does a short read, then we need to retry preadv() with
remainig bytes and skip the bytes preadv() read in short read.
For case a, there is no reason that skipping logic be inside the while
loop. Move it outside. And only retain logic "b" inside while loop.
Also get rid of variable "skip_size". Looks like we can do without it.
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Connor Kuehl <ckuehl@redhat.com>
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
---
tools/virtiofsd/fuse_virtio.c | 14 ++++----------
1 file changed, 4 insertions(+), 10 deletions(-)
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
index ed5146d7a6..49c7dd788a 100644
--- a/tools/virtiofsd/fuse_virtio.c
+++ b/tools/virtiofsd/fuse_virtio.c
@@ -392,17 +392,11 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
unsigned int in_sg_cpy_count = in_num;
/* skip over parts of in_sg that contained the header iov */
- size_t skip_size = iov_len;
+ iov_discard_front(&in_sg_ptr, &in_sg_cpy_count, iov_len);
do {
- if (skip_size != 0) {
- iov_discard_front(&in_sg_ptr, &in_sg_cpy_count, skip_size);
- }
-
- fuse_log(FUSE_LOG_DEBUG,
- "%s: after skip skip_size=%zd in_sg_cpy_count=%d "
- "len remaining=%zd\n", __func__, skip_size, in_sg_cpy_count,
- len);
+ fuse_log(FUSE_LOG_DEBUG, "%s: in_sg_cpy_count=%d len remaining=%zd\n",
+ __func__, in_sg_cpy_count, len);
ret = preadv(buf->buf[0].fd, in_sg_ptr, in_sg_cpy_count,
buf->buf[0].pos);
@@ -421,7 +415,7 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
if (ret < len && ret) {
fuse_log(FUSE_LOG_DEBUG, "%s: ret < len\n", __func__);
/* Skip over this much next time around */
- skip_size = ret;
+ iov_discard_front(&in_sg_ptr, &in_sg_cpy_count, ret);
buf->buf[0].pos += ret;
len -= ret;
--
2.25.4
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 6/7] virtiofsd: Check EOF before short read
2021-05-18 21:35 [PATCH v2 0/7] virtiofsd: Few cleanups in virtio_send_data_iov() Vivek Goyal
` (4 preceding siblings ...)
2021-05-18 21:35 ` [PATCH v2 5/7] virtiofsd: Simplify skip byte logic Vivek Goyal
@ 2021-05-18 21:35 ` Vivek Goyal
2021-05-18 21:35 ` [PATCH v2 7/7] virtiofsd: Set req->reply_sent right after sending reply Vivek Goyal
2021-05-25 18:49 ` [PATCH v2 0/7] virtiofsd: Few cleanups in virtio_send_data_iov() Dr. David Alan Gilbert
7 siblings, 0 replies; 9+ messages in thread
From: Vivek Goyal @ 2021-05-18 21:35 UTC (permalink / raw)
To: qemu-devel, virtio-fs; +Cc: ckuehl, dgilbert, vgoyal
In virtio_send_data_iov() we are checking first for short read and then
EOF condition. Change the order. Basically check for error and EOF first
and last remaining piece is short ready which will lead to retry
automatically at the end of while loop.
Just that it is little simpler to read to the code. There is no need
to call "continue" and also one less call of "len-=ret".
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Connor Kuehl <ckuehl@redhat.com>
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
---
tools/virtiofsd/fuse_virtio.c | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
index 49c7dd788a..99f91c9d87 100644
--- a/tools/virtiofsd/fuse_virtio.c
+++ b/tools/virtiofsd/fuse_virtio.c
@@ -410,25 +410,24 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
__func__, len);
goto err;
}
- fuse_log(FUSE_LOG_DEBUG, "%s: preadv ret=%d len=%zd\n", __func__,
- ret, len);
- if (ret < len && ret) {
- fuse_log(FUSE_LOG_DEBUG, "%s: ret < len\n", __func__);
- /* Skip over this much next time around */
- iov_discard_front(&in_sg_ptr, &in_sg_cpy_count, ret);
- buf->buf[0].pos += ret;
- len -= ret;
- /* Lets do another read */
- continue;
- }
if (!ret) {
/* EOF case? */
fuse_log(FUSE_LOG_DEBUG, "%s: !ret len remaining=%zd\n", __func__,
len);
break;
}
+ fuse_log(FUSE_LOG_DEBUG, "%s: preadv ret=%d len=%zd\n", __func__,
+ ret, len);
+
len -= ret;
+ /* Short read. Retry reading remaining bytes */
+ if (len) {
+ fuse_log(FUSE_LOG_DEBUG, "%s: ret < len\n", __func__);
+ /* Skip over this much next time around */
+ iov_discard_front(&in_sg_ptr, &in_sg_cpy_count, ret);
+ buf->buf[0].pos += ret;
+ }
} while (len);
/* Need to fix out->len on EOF */
--
2.25.4
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v2 7/7] virtiofsd: Set req->reply_sent right after sending reply
2021-05-18 21:35 [PATCH v2 0/7] virtiofsd: Few cleanups in virtio_send_data_iov() Vivek Goyal
` (5 preceding siblings ...)
2021-05-18 21:35 ` [PATCH v2 6/7] virtiofsd: Check EOF before short read Vivek Goyal
@ 2021-05-18 21:35 ` Vivek Goyal
2021-05-25 18:49 ` [PATCH v2 0/7] virtiofsd: Few cleanups in virtio_send_data_iov() Dr. David Alan Gilbert
7 siblings, 0 replies; 9+ messages in thread
From: Vivek Goyal @ 2021-05-18 21:35 UTC (permalink / raw)
To: qemu-devel, virtio-fs; +Cc: ckuehl, dgilbert, vgoyal
There is no reason to set it in label "err". We should be able to set
it right after sending reply. It is easier to read.
Also got rid of label "err" because now only thing it was doing was
return a code. We can return from the error location itself and no
need to first jump to label "err".
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Connor Kuehl <ckuehl@redhat.com>
Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
---
tools/virtiofsd/fuse_virtio.c | 19 +++++--------------
1 file changed, 5 insertions(+), 14 deletions(-)
diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c
index 99f91c9d87..fa4aff9b0e 100644
--- a/tools/virtiofsd/fuse_virtio.c
+++ b/tools/virtiofsd/fuse_virtio.c
@@ -366,14 +366,12 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
if (in_len < sizeof(struct fuse_out_header)) {
fuse_log(FUSE_LOG_ERR, "%s: elem %d too short for out_header\n",
__func__, elem->index);
- ret = E2BIG;
- goto err;
+ return E2BIG;
}
if (in_len < tosend_len) {
fuse_log(FUSE_LOG_ERR, "%s: elem %d too small for data len %zd\n",
__func__, elem->index, tosend_len);
- ret = E2BIG;
- goto err;
+ return E2BIG;
}
/* TODO: Limit to 'len' */
@@ -408,7 +406,7 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
}
fuse_log(FUSE_LOG_DEBUG, "%s: preadv failed (%m) len=%zd\n",
__func__, len);
- goto err;
+ return ret;
}
if (!ret) {
@@ -438,21 +436,14 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
out_sg->len = tosend_len;
}
- ret = 0;
-
vu_dispatch_rdlock(qi->virtio_dev);
pthread_mutex_lock(&qi->vq_lock);
vu_queue_push(dev, q, elem, tosend_len);
vu_queue_notify(dev, q);
pthread_mutex_unlock(&qi->vq_lock);
vu_dispatch_unlock(qi->virtio_dev);
-
-err:
- if (ret == 0) {
- req->reply_sent = true;
- }
-
- return ret;
+ req->reply_sent = true;
+ return 0;
}
static __thread bool clone_fs_called;
--
2.25.4
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v2 0/7] virtiofsd: Few cleanups in virtio_send_data_iov()
2021-05-18 21:35 [PATCH v2 0/7] virtiofsd: Few cleanups in virtio_send_data_iov() Vivek Goyal
` (6 preceding siblings ...)
2021-05-18 21:35 ` [PATCH v2 7/7] virtiofsd: Set req->reply_sent right after sending reply Vivek Goyal
@ 2021-05-25 18:49 ` Dr. David Alan Gilbert
7 siblings, 0 replies; 9+ messages in thread
From: Dr. David Alan Gilbert @ 2021-05-25 18:49 UTC (permalink / raw)
To: Vivek Goyal; +Cc: virtio-fs, ckuehl, qemu-devel
* Vivek Goyal (vgoyal@redhat.com) wrote:
> Hi,
>
> This is V2 of the patches. Changes since V1 are.
>
> - Took care of coding style issue.
> - Got rid of label "err" completely in last patch. (Dave, Connor).
> - Captured Reviewed-by tags from Connor and David.
>
> Code in virtio_send_data_iov() little twisted and complicated. This
> patch series just tries to simplify it a bit to make it little easier
> to read this piece of code.
Queued
> Thanks
> Vivek
>
>
> Vivek Goyal (7):
> virtiofsd: Check for EINTR in preadv() and retry
> virtiofsd: Get rid of unreachable code in read
> virtiofsd: Use iov_discard_front() to skip bytes
> virtiofsd: get rid of in_sg_left variable
> virtiofsd: Simplify skip byte logic
> virtiofsd: Check EOF before short read
> virtiofsd: Set req->reply_sent right after sending reply
>
> tools/virtiofsd/fuse_virtio.c | 81 +++++++++++------------------------
> 1 file changed, 25 insertions(+), 56 deletions(-)
>
> --
> 2.25.4
>
>
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
^ permalink raw reply [flat|nested] 9+ messages in thread