Re: [RFC v5 099/126] nbd: introduce ERRP_AUTO_PROPAGATE

2019-10-11 Thread Eric Blake

On 10/11/19 11:05 AM, Vladimir Sementsov-Ogievskiy wrote:

If we want to add some info to errp (by error_prepend() or
error_append_hint()), we must use the ERRP_AUTO_PROPAGATE macro.
Otherwise, this info will not be added when errp == _err
(the program will exit prior to the error_append_hint() or
error_prepend() call).  Fix such cases.

If we want to check error after errp-function call, we need to
introduce local_err and than propagate it to errp. Instead, use
ERRP_AUTO_PROPAGATE macro, benefits are:
1. No need of explicit error_propagate call
2. No need of explicit local_err variable: use errp directly
3. ERRP_AUTO_PROPAGATE leaves errp as is if it's not NULL or
_fatel, this means that we don't break error_abort
(we'll abort on error_set, not on error_propagate)




Reported-by: Kevin Wolf 
Reported-by: Greg Kurz 
Signed-off-by: Vladimir Sementsov-Ogievskiy 
---
 include/block/nbd.h |  1 +
 block/nbd.c | 49 +
 nbd/client.c|  5 +
 nbd/server.c|  5 +
 4 files changed, 34 insertions(+), 26 deletions(-)


One of the few patches with a net gain rather than loss of lines, but 
that's because of lots of error_prepend use (where you add the macro 
without removing error_propagate).


Reviewed-by: Eric Blake 

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.   +1-919-301-3226
Virtualization:  qemu.org | libvirt.org



[RFC v5 099/126] nbd: introduce ERRP_AUTO_PROPAGATE

2019-10-11 Thread Vladimir Sementsov-Ogievskiy
If we want to add some info to errp (by error_prepend() or
error_append_hint()), we must use the ERRP_AUTO_PROPAGATE macro.
Otherwise, this info will not be added when errp == _err
(the program will exit prior to the error_append_hint() or
error_prepend() call).  Fix such cases.

If we want to check error after errp-function call, we need to
introduce local_err and than propagate it to errp. Instead, use
ERRP_AUTO_PROPAGATE macro, benefits are:
1. No need of explicit error_propagate call
2. No need of explicit local_err variable: use errp directly
3. ERRP_AUTO_PROPAGATE leaves errp as is if it's not NULL or
   _fatel, this means that we don't break error_abort
   (we'll abort on error_set, not on error_propagate)

This commit (together with its neighbors) was generated by

for f in $(git grep -l errp \*.[ch]); do \
spatch --sp-file scripts/coccinelle/auto-propagated-errp.cocci \
--macro-file scripts/cocci-macro-file.h --in-place --no-show-diff $f; \
done;

then fix a bit of compilation problems: coccinelle for some reason
leaves several
f() {
...
goto out;
...
out:
}
patterns, with "out:" at function end.

then
./python/commit-per-subsystem.py MAINTAINERS "$(< auto-msg)"

(auto-msg was a file with this commit message)

Still, for backporting it may be more comfortable to use only the first
command and then do one huge commit.

Reported-by: Kevin Wolf 
Reported-by: Greg Kurz 
Signed-off-by: Vladimir Sementsov-Ogievskiy 
---
 include/block/nbd.h |  1 +
 block/nbd.c | 49 +
 nbd/client.c|  5 +
 nbd/server.c|  5 +
 4 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/include/block/nbd.h b/include/block/nbd.h
index 316fd705a9..330f40142a 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -360,6 +360,7 @@ void nbd_server_start(SocketAddress *addr, const char 
*tls_creds,
 static inline int nbd_read(QIOChannel *ioc, void *buffer, size_t size,
const char *desc, Error **errp)
 {
+ERRP_AUTO_PROPAGATE();
 int ret = qio_channel_read_all(ioc, buffer, size, errp) < 0 ? -EIO : 0;
 
 if (ret < 0) {
diff --git a/block/nbd.c b/block/nbd.c
index c66fdf54b9..2f8a924562 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -805,10 +805,10 @@ static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, 
uint64_t handle,
 uint64_t offset, QEMUIOVector *qiov,
 int *request_ret, Error **errp)
 {
+ERRP_AUTO_PROPAGATE();
 NBDReplyChunkIter iter;
 NBDReply reply;
 void *payload = NULL;
-Error *local_err = NULL;
 
 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply,
 qiov, , )
@@ -827,20 +827,20 @@ static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, 
uint64_t handle,
 break;
 case NBD_REPLY_TYPE_OFFSET_HOLE:
 ret = nbd_parse_offset_hole_payload(s, , payload,
-offset, qiov, _err);
+offset, qiov, errp);
 if (ret < 0) {
 nbd_channel_error(s, ret);
-nbd_iter_channel_error(, ret, _err);
+nbd_iter_channel_error(, ret, errp);
 }
 break;
 default:
 if (!nbd_reply_type_is_error(chunk->type)) {
 /* not allowed reply type */
 nbd_channel_error(s, -EINVAL);
-error_setg(_err,
+error_setg(errp,
"Unexpected reply type: %d (%s) for CMD_READ",
chunk->type, nbd_reply_type_lookup(chunk->type));
-nbd_iter_channel_error(, -EINVAL, _err);
+nbd_iter_channel_error(, -EINVAL, errp);
 }
 }
 
@@ -858,10 +858,10 @@ static int nbd_co_receive_blockstatus_reply(BDRVNBDState 
*s,
 NBDExtent *extent,
 int *request_ret, Error **errp)
 {
+ERRP_AUTO_PROPAGATE();
 NBDReplyChunkIter iter;
 NBDReply reply;
 void *payload = NULL;
-Error *local_err = NULL;
 bool received = false;
 
 assert(!extent->length);
@@ -875,27 +875,27 @@ static int nbd_co_receive_blockstatus_reply(BDRVNBDState 
*s,
 case NBD_REPLY_TYPE_BLOCK_STATUS:
 if (received) {
 nbd_channel_error(s, -EINVAL);
-error_setg(_err, "Several BLOCK_STATUS chunks in reply");
-nbd_iter_channel_error(, -EINVAL, _err);
+error_setg(errp, "Several BLOCK_STATUS chunks in reply");
+nbd_iter_channel_error(, -EINVAL, errp);
 }
 received = true;
 
 ret = nbd_parse_blockstatus_payload(s, ,
 payload, length, extent,
-