From: Eric Blake Date: Fri, 30 Nov 2018 02:32:32 +0000 (-0600) Subject: nbd/client: Send NBD_CMD_DISC if open fails after connect X-Git-Tag: v3.0.1~36 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=32e0caf8ffb4e554975e8b672394e9b144fa4326;p=thirdparty%2Fqemu.git nbd/client: Send NBD_CMD_DISC if open fails after connect If nbd_client_init() fails after we are already connected, then the server will spam logs with: Disconnect client, due to: Unexpected end-of-file before all bytes were read unless we gracefully disconnect before closing the connection. Ways to trigger this: $ opts=driver=nbd,export=foo,server.type=inet,server.host=localhost,server.port=10809 $ qemu-img map --output=json --image-opts $opts,read-only=off $ qemu-img map --output=json --image-opts $opts,x-dirty-bitmap=nosuch: Signed-off-by: Eric Blake Message-Id: <20181130023232.3079982-4-eblake@redhat.com> Reviewed-by: Vladimir Sementsov-Ogievskiy (cherry picked from commit c688e6ca7b41a105241054853d250df64addbf8f) *drop functional dep. on 6c2e581d4d7 Signed-off-by: Michael Roth --- diff --git a/block/nbd-client.c b/block/nbd-client.c index f8c42a69960..1b7b5b0a883 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -995,13 +995,15 @@ int nbd_client_init(BlockDriverState *bs, if (x_dirty_bitmap && !client->info.base_allocation) { error_setg(errp, "requested x-dirty-bitmap %s not found", x_dirty_bitmap); - return -EINVAL; + ret = -EINVAL; + goto fail; } if (client->info.flags & NBD_FLAG_READ_ONLY && !bdrv_is_read_only(bs)) { error_setg(errp, "request for write access conflicts with read-only export"); - return -EACCES; + ret = -EACCES; + goto fail; } if (client->info.flags & NBD_FLAG_SEND_FUA) { bs->supported_write_flags = BDRV_REQ_FUA; @@ -1029,4 +1031,17 @@ int nbd_client_init(BlockDriverState *bs, logout("Established connection with NBD server\n"); return 0; + + fail: + /* + * We have connected, but must fail for other reasons. The + * connection is still blocking; send NBD_CMD_DISC as a courtesy + * to the server. + */ + { + NBDRequest request = { .type = NBD_CMD_DISC }; + + nbd_send_request(client->ioc ?: QIO_CHANNEL(sioc), &request); + return ret; + } }