nbd/server: fix space read
PATCH 1/1 nbd/server: fix space read
In case of io error in nbd_co_send_sparse_read we should not "goto reply:", as it is fatal error and common behavior is disconnect in this case. We should not try to send client an error reply, representing channel-io error on previous try to send a reply.
Fix this by handle block-status error in nbd_co_send_sparse_read, so nbd_co_send_sparse_read fails only on io error. Then just skip common "reply:" code path in nbd_trip.
Note: nbd_co_send_structured_error is moved without changes to be called from nbd_co_send_sparse_read.
---
PS: this all shows that nbd_trip is tooo complex (and yes, I remember, that its final semantics was made by myself :(.. It should be refactored into several smaller parts. Do you have any ideas?
The complexity here is that we should handle channel errors (fatal) and export errors(non fatal) in different ways, and both of error types may have errp, which should be handled differently too..
May be, the best way is to make separate functions for each command, avoiding code duplication by using helper-functions instead of common code in nbd_trip.
nbd/server.c | 64 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 27 deletions(-)
nbd/server.c +37 -27
@@ -21,6 +21,7 @@#include "qapi/error.h" #include "trace.h" #include "nbd-internal.h" +#include "qemu/error-report.h" static int system_errno_to_nbd_errno (int err) {
@@ -1341,6 +1342,30 @@ static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,return nbd_co_send_iov (client, iov,2 , errp); } +static int coroutine_fn nbd_co_send_structured_error(NBDClient *client, +uint64_t handle, +uint32_t error, +const char *msg, +Error **errp) +{ +NBDStructuredError chunk; +int nbd_err =system_errno_to_nbd_errno (error); +struct iovec iov[] = { + {.iov_base = &chunk, .iov_len =sizeof (chunk)}, + {.iov_base = (char *)msg, .iov_len = msg ?strlen (msg) :0 }, + }; + +assert (nbd_err); +trace_nbd_co_send_structured_error (handle, nbd_err, +nbd_err_lookup (nbd_err), msg ? msg :"" ); +set_be_chunk (&chunk.h,NBD_REPLY_FLAG_DONE ,NBD_REPLY_TYPE_ERROR , handle, +sizeof (chunk) -sizeof (chunk.h) + iov[1 ].iov_len); +stl_be_p (&chunk.error, nbd_err); +stw_be_p (&chunk.message_length, iov[1 ].iov_len); + +return nbd_co_send_iov (client, iov,1 + !!iov[1 ].iov_len, errp); +} + static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, uint64_t handle, uint64_t offset,
@@ -1361,8 +1386,15 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,bool final;if (status <0 ) { -error_setg_errno (errp, -status,"unable to check for holes" ); -return status; +char *msg =g_strdup_printf ("unable to check for holes: %s" , +strerror (-status)); + +error_report ("%s" , msg); + + ret =nbd_co_send_structured_error (client, handle, -status, msg, + errp); +g_free (msg); +return ret; }assert (pnum && pnum <= size - progress); final = progress + pnum == size;
@@ -1400,30 +1432,6 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,return ret; } -static int coroutine_fn nbd_co_send_structured_error(NBDClient *client, -uint64_t handle, -uint32_t error, -const char *msg, -Error **errp) -{ -NBDStructuredError chunk; -int nbd_err =system_errno_to_nbd_errno (error); -struct iovec iov[] = { - {.iov_base = &chunk, .iov_len =sizeof (chunk)}, - {.iov_base = (char *)msg, .iov_len = msg ?strlen (msg) :0 }, - }; - -assert (nbd_err); -trace_nbd_co_send_structured_error (handle, nbd_err, -nbd_err_lookup (nbd_err), msg ? msg :"" ); -set_be_chunk (&chunk.h,NBD_REPLY_FLAG_DONE ,NBD_REPLY_TYPE_ERROR , handle, -sizeof (chunk) -sizeof (chunk.h) + iov[1 ].iov_len); -stl_be_p (&chunk.error, nbd_err); -stw_be_p (&chunk.message_length, iov[1 ].iov_len); - -return nbd_co_send_iov (client, iov,1 + !!iov[1 ].iov_len, errp); -} -/* nbd_co_receive_request * Collect a client request. Return 0 if request looks valid, -EIO to drop * connection right away, and any other negative value to report an error to
@@ -1567,7 +1575,7 @@ static coroutine_fn void nbd_trip(void *opaque) request.from, req->data, request.len, &local_err);if (ret <0 ) { - goto reply; + goto replied; } goto done; }
@@ -1664,6 +1672,8 @@ reply: req->data, reply_data_len, &local_err); }g_free (msg); + +replied:if (ret <0 ) {error_prepend (&local_err,"Failed to send reply: " ); goto disconnect;
Signature · 1 line
2.11.1
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
In case of io error in
nbd_co_send_sparse_readwe should not "goto reply:", as it is fatal error and common behavior is disconnect in this case. We should not try to send client an error reply, representing channel-io error on previous try to send a reply.
Good catch.
Quoted 7 lines
Fix this by handle block-status error in
nbd_co_send_sparse_read, sonbd_co_send_sparse_readfails only on io error. Then just skip common "reply:" code path innbd_trip.Note:
nbd_co_send_structured_erroris moved without changes to be called fromnbd_co_send_sparse_read.
Might be easier to read as two patches, one for the code motion, the other for using the new code. But I'm not going to insist on a split.
Quoted 8 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
PS: this all shows that
nbd_tripis tooo complex (and yes, I remember, that its final semantics was made by myself :(.. It should be refactored into several smaller parts. Do you have any ideas?
Quoted 7 lines
The complexity here is that we should handle channel errors (fatal) and export errors(non fatal) in different ways, and both of error types may have errp, which should be handled differently too..
May be, the best way is to make separate functions for each command, avoiding code duplication by using helper-functions instead of common code in
nbd_trip.
Yes, splitting nbd_trip into smaller helper functions may be worthwhile.
nbd/server.c line 24
Quoted 13 lines
nbd/server.c | 64 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 27 deletions(-)nbd/server.c +1
-0@@ -21,6 +21,7 @@#include "qapi/error.h" #include "trace.h" #include "nbd-internal.h" +#include "qemu/error-report.h"
I'm a bit worried about this one. The server has previously not needed this, so it may be the wrong thing to pull it in now.
nbd/server.c static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, · line 1371
Quoted 4 lines
nbd/server.c +1
-0+ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, uint64_t handle, uint64_t offset,
It doesn't help that we are lacking comments on the contract of this function.
nbd/server.c static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, · line 1390
Quoted 8 lines
nbd/server.c +2
-2@@ -1361,8 +1386,15 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,bool final;if (status <0 ) { -error_setg_errno (errp, -status,"unable to check for holes" ); -return status; +char *msg =g_strdup_printf ("unable to check for holes: %s" , +strerror (-status));
Indentation looks off.
nbd/server.c static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, · line 1392
Quoted 2 lines
nbd/server.c +2
-0+ +error_report ("%s" , msg);
Do we really need to report this on the server's stderr, or is sending the reply to the client good enough?
nbd/server.c static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, · line 1397
Quoted 5 lines
nbd/server.c +5
-0+ + ret =nbd_co_send_structured_error (client, handle, -status, msg, + errp); +g_free (msg); +return ret;
So if we have an early return here, then pre-patch, we were unconditionally setting errp and returning -1 (even if the client is still alive); post-patch errp is only set if we failed to do I/O with the client. That change is right, but harder to see without comments giving a function contract.
Quoted 10 lines
nbd/server.c +1
-1@@ -1567,7 +1575,7 @@ static coroutine_fn void nbd_trip(void *opaque) request.from, req->data, request.len, &local_err);if (ret <0 ) { - goto reply; + goto replied; } goto done; }
nbd/server.c reply: · line 1676
Quoted 5 lines
nbd/server.c +2
-0@@ -1664,6 +1672,8 @@ reply: req->data, reply_data_len, &local_err); }g_free (msg); + +replied:
This part makes sense.
Quoted 4 lines
nbd/server.c +0
-0if (ret <0 ) {error_prepend (&local_err,"Failed to send reply: " ); goto disconnect;
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 1 line · Eric Blake
On 03/05/2018 12:04 PM, Vladimir Sementsov-Ogievskiy wrote:
In the subject line: s/space/sparse/
Quoted 18 lines
Quoted 5 lines
In case of io error in
nbd_co_send_sparse_readwe should not "goto reply:", as it is fatal error and common behavior is disconnect in this case. We should not try to send client an error reply, representing channel-io error on previous try to send a reply.Good catch.
Quoted 7 lines
Fix this by handle block-status error in
nbd_co_send_sparse_read, sonbd_co_send_sparse_readfails only on io error. Then just skip common "reply:" code path innbd_trip.Note:
nbd_co_send_structured_erroris moved without changes to be called fromnbd_co_send_sparse_read.Might be easier to read as two patches, one for the code motion, the other for using the new code. But I'm not going to insist on a split.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 75 lines · Eric Blake
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
In case of io error in
nbd_co_send_sparse_readwe should not "goto reply:", as it is fatal error and common behavior is disconnect in this case. We should not try to send client an error reply, representing channel-io error on previous try to send a reply.Good catch.
Quoted 7 lines
Fix this by handle block-status error in
nbd_co_send_sparse_read, sonbd_co_send_sparse_readfails only on io error. Then just skip common "reply:" code path innbd_trip.Note:
nbd_co_send_structured_erroris moved without changes to be called fromnbd_co_send_sparse_read.Might be easier to read as two patches, one for the code motion, the other for using the new code. But I'm not going to insist on a split.
Quoted 8 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
PS: this all shows that
nbd_tripis tooo complex (and yes, I remember, that its final semantics was made by myself :(.. It should be refactored into several smaller parts. Do you have any ideas?Quoted 7 lines
The complexity here is that we should handle channel errors (fatal) and export errors(non fatal) in different ways, and both of error types may have errp, which should be handled differently too..
May be, the best way is to make separate functions for each command, avoiding code duplication by using helper-functions instead of common code in
nbd_trip.Yes, splitting
nbd_tripinto smaller helper functions may be worthwhile.Quoted 10 lines
nbd/server.c| 64
+++++++++++++++++++++++++++++++++++-------------------------1 file changed, 37 insertions(+), 27 deletions(-)nbd/server.c +0
-0nbd/server.c line 24
Quoted 4 lines
nbd/server.c +1
-0@@ -21,6 +21,7 @@ #include "qapi/error.h" #include "trace.h" #include "nbd-internal.h" +#include "qemu/error-report.h" I'm a bit worried about this one. The server has previously not needed this, so it may be the wrong thing to pull it in now.
Quoted 4 lines
nbd/server.c +1
-0+ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, uint64_t handle,
uint64_toffset,It doesn't help that we are lacking comments on the contract of this function.
nbd/server.c static int coroutine_fn · line 1392
Quoted 10 lines
nbd/server.c +2
-2@@ -1361,8 +1386,15 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, bool final; if (status < 0) { - error_setg_errno(errp, -status, "unable to check for holes"); - return status; + char *msg = g_strdup_printf("unable to check for holes: %s", + strerror(-status));Indentation looks off.
Quoted 4 lines
nbd/server.c static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, · line 1392
Quoted 2 lines
nbd/server.c +2
-0+ + error_report("%s", msg);Do we really need to report this on the server's stderr, or is sending the reply to the client good enough?
I'm just trying to save current behavior in nbd_trip... Why not? Is it fair that client knows about some strange io error on server-side, but server logs lacks this information?
and it needs "qemu/error-report.h".. nbd_trip uses error_report_err instead. error_report_err calls error_report anyway.
Quoted 33 lines
nbd/server.c static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, · line 1397
Quoted 7 lines
nbd/server.c +5
-1+ + ret = nbd_co_send_structured_error(client, handle, -status, msg, + errp); + g_free(msg); + return ret;So if we have an early return here, then pre-patch, we were unconditionally setting errp and returning -1 (even if the client is still alive); post-patch errp is only set if we failed to do I/O with the client. That change is right, but harder to see without comments giving a function contract.
Quoted 10 lines
nbd/server.c +1
-1@@ -1567,7 +1575,7 @@ static coroutine_fn void nbd_trip(void *opaque) request.from, req->data, request.len, &local_err); if (ret < 0) { - goto reply; + goto replied; } goto done; }nbd/server.c reply: · line 1676
Quoted 5 lines
nbd/server.c +2
-0@@ -1664,6 +1672,8 @@ reply: req->data, reply_data_len, &local_err); } g_free(msg); + +replied:This part makes sense.
Quoted 4 lines
nbd/server.c +0
-0if (ret < 0) { error_prepend(&local_err, "Failed to send reply: "); goto disconnect;
Signature · 2 lines
Best regards, Vladimir
Quoted 76 lines · Vladimir Sementsov-Ogievskiy
Quoted 75 lines · Eric Blake
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
In case of io error in
nbd_co_send_sparse_readwe should not "goto reply:", as it is fatal error and common behavior is disconnect in this case. We should not try to send client an error reply, representing channel-io error on previous try to send a reply.Good catch.
Quoted 7 lines
Fix this by handle block-status error in
nbd_co_send_sparse_read, sonbd_co_send_sparse_readfails only on io error. Then just skip common "reply:" code path innbd_trip.Note:
nbd_co_send_structured_erroris moved without changes to be called fromnbd_co_send_sparse_read.Might be easier to read as two patches, one for the code motion, the other for using the new code. But I'm not going to insist on a split.
Quoted 8 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
PS: this all shows that
nbd_tripis tooo complex (and yes, I remember, that its final semantics was made by myself :(.. It should be refactored into several smaller parts. Do you have any ideas?Quoted 7 lines
The complexity here is that we should handle channel errors (fatal) and export errors(non fatal) in different ways, and both of error types may have errp, which should be handled differently too..
May be, the best way is to make separate functions for each command, avoiding code duplication by using helper-functions instead of common code in
nbd_trip.Yes, splitting
nbd_tripinto smaller helper functions may be worthwhile.Quoted 10 lines
nbd/server.c| 64
+++++++++++++++++++++++++++++++++++-------------------------1 file changed, 37 insertions(+), 27 deletions(-)nbd/server.c +0
-0nbd/server.c line 24
Quoted 4 lines
nbd/server.c +1
-0@@ -21,6 +21,7 @@ #include "qapi/error.h" #include "trace.h" #include "nbd-internal.h" +#include "qemu/error-report.h" I'm a bit worried about this one. The server has previously not needed this, so it may be the wrong thing to pull it in now.
Quoted 4 lines
nbd/server.c +1
-0+ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, uint64_t handle,
uint64_toffset,It doesn't help that we are lacking comments on the contract of this function.
nbd/server.c static int coroutine_fn · line 1392
Quoted 10 lines
nbd/server.c +2
-2@@ -1361,8 +1386,15 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, bool final; if (status < 0) { - error_setg_errno(errp, -status, "unable to check for holes"); - return status; + char *msg = g_strdup_printf("unable to check for holes: %s", + strerror(-status));Indentation looks off.
Quoted 7 lines
Quoted 4 lines
nbd/server.c static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, · line 1392
Quoted 2 lines
nbd/server.c +2
-0+ + error_report("%s", msg);Do we really need to report this on the server's stderr, or is sending the reply to the client good enough?
I'm just trying to save current behavior in
nbd_trip... Why not? Is it fair that client knows about some strange io error on server-side, but server logs lacks this information?
May be, it worth switching it from error_report to trace, but it should be a separate patch, changing all places around.
Quoted 38 lines
and it needs "
qemu/error-report.h"..nbd_tripuseserror_report_errinstead.error_report_errcallserror_reportanyway.Quoted 33 lines
nbd/server.c static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, · line 1397
Quoted 7 lines
nbd/server.c +5
-1+ + ret = nbd_co_send_structured_error(client, handle, -status, msg, + errp); + g_free(msg); + return ret;So if we have an early return here, then pre-patch, we were unconditionally setting errp and returning -1 (even if the client is still alive); post-patch errp is only set if we failed to do I/O with the client. That change is right, but harder to see without comments giving a function contract.
Quoted 10 lines
nbd/server.c +1
-1@@ -1567,7 +1575,7 @@ static coroutine_fn void nbd_trip(void *opaque) request.from, req->data, request.len, &local_err); if (ret < 0) { - goto reply; + goto replied; } goto done; }nbd/server.c reply: · line 1676
Quoted 5 lines
nbd/server.c +2
-0@@ -1664,6 +1672,8 @@ reply: req->data, reply_data_len, &local_err); } g_free(msg); + +replied:This part makes sense.
Quoted 4 lines
nbd/server.c +0
-0if (ret < 0) { error_prepend(&local_err, "Failed to send reply: "); goto disconnect;
Signature · 2 lines
Best regards, Vladimir
Quoted 80 lines · Vladimir Sementsov-Ogievskiy
Quoted 79 lines · Vladimir Sementsov-Ogievskiy
Quoted 78 lines · Eric Blake
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
In case of io error in
nbd_co_send_sparse_readwe should not "goto reply:", as it is fatal error and common behavior is disconnect in this case. We should not try to send client an error reply, representing channel-io error on previous try to send a reply.Good catch.
Quoted 7 lines
Fix this by handle block-status error in
nbd_co_send_sparse_read, sonbd_co_send_sparse_readfails only on io error. Then just skip common "reply:" code path innbd_trip.Note:
nbd_co_send_structured_erroris moved without changes to be called fromnbd_co_send_sparse_read.Might be easier to read as two patches, one for the code motion, the other for using the new code. But I'm not going to insist on a split.
Quoted 8 lines
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
PS: this all shows that
nbd_tripis tooo complex (and yes, I remember, that its final semantics was made by myself :(.. It should be refactored into several smaller parts. Do you have any ideas?Quoted 7 lines
The complexity here is that we should handle channel errors (fatal) and export errors(non fatal) in different ways, and both of error types may have errp, which should be handled differently too..
May be, the best way is to make separate functions for each command, avoiding code duplication by using helper-functions instead of common code in
nbd_trip.Yes, splitting
nbd_tripinto smaller helper functions may be worthwhile.Quoted 10 lines
nbd/server.c| 64
+++++++++++++++++++++++++++++++++++-------------------------1 file changed, 37 insertions(+), 27 deletions(-)nbd/server.c +0
-0nbd/server.c line 24
Quoted 4 lines
nbd/server.c +1
-0@@ -21,6 +21,7 @@ #include "qapi/error.h" #include "trace.h" #include "nbd-internal.h" +#include "qemu/error-report.h" I'm a bit worried about this one. The server has previously not needed this, so it may be the wrong thing to pull it in now.
Quoted 4 lines
nbd/server.c +1
-0+ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, uint64_t handle,
uint64_toffset,It doesn't help that we are lacking comments on the contract of this function.
nbd/server.c static int coroutine_fn · line 1392
Quoted 10 lines
nbd/server.c +2
-2@@ -1361,8 +1386,15 @@ static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, bool final; if (status < 0) { - error_setg_errno(errp, -status, "unable to check for holes"); - return status; + char *msg = g_strdup_printf("unable to check for holes: %s", + strerror(-status));Indentation looks off.
Quoted 2 lines
nbd/server.c +2
-0+ + error_report("%s", msg);
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Do we really need to report this on the server's stderr, or is sending the reply to the client good enough?
I'm just trying to save current behavior in
nbd_trip... Why not? Is it fair that client knows about some strange io error on server-side, but server logs lacks this information?May be, it worth switching it from
error_reportto trace, but it should be a separate patch, changing all places around.
stop, it is already traced in nbd_co_send_simple_reply and nbd_co_send_structured_error. So, we can safely drop it.
Quoted 41 lines
Quoted 39 lines
and it needs "
qemu/error-report.h"..nbd_tripuseserror_report_errinstead.error_report_errcallserror_reportanyway.Quoted 33 lines
nbd/server.c static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client, · line 1397
Quoted 7 lines
nbd/server.c +5
-1+ + ret = nbd_co_send_structured_error(client, handle, -status, msg, + errp); + g_free(msg); + return ret;So if we have an early return here, then pre-patch, we were unconditionally setting errp and returning -1 (even if the client is still alive); post-patch errp is only set if we failed to do I/O with the client. That change is right, but harder to see without comments giving a function contract.
Quoted 10 lines
nbd/server.c +1
-1@@ -1567,7 +1575,7 @@ static coroutine_fn void nbd_trip(void *opaque) request.from, req->data, request.len, &local_err); if (ret < 0) { - goto reply; + goto replied; } goto done; }nbd/server.c reply: · line 1676
Quoted 5 lines
nbd/server.c +2
-0@@ -1664,6 +1672,8 @@ reply: req->data, reply_data_len, &local_err); } g_free(msg); + +replied:This part makes sense.
Quoted 4 lines
nbd/server.c +0
-0if (ret < 0) { error_prepend(&local_err, "Failed to send reply: "); goto disconnect;
Signature · 2 lines
Best regards, Vladimir