nbd/client: Refactor nbd_opt_go() to support NBD_OPT_INFO

Rename the function to nbd_opt_info_or_go() with an added parameter
and slight changes to comments and trace messages, in order to
reuse the function for NBD_OPT_INFO.

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <20190117193658.16413-17-eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
This commit is contained in:
Eric Blake 2019-01-17 13:36:53 -06:00
parent b3c9d33bc4
commit 138796d0f5
2 changed files with 26 additions and 18 deletions

View File

@ -330,11 +330,16 @@ static int nbd_receive_list(QIOChannel *ioc, char **name, char **description,
} }
/* Returns -1 if NBD_OPT_GO proves the export @info->name cannot be /*
* used, 0 if NBD_OPT_GO is unsupported (fall back to NBD_OPT_LIST and * nbd_opt_info_or_go:
* Send option for NBD_OPT_INFO or NBD_OPT_GO and parse the reply.
* Returns -1 if the option proves the export @info->name cannot be
* used, 0 if the option is unsupported (fall back to NBD_OPT_LIST and
* NBD_OPT_EXPORT_NAME in that case), and > 0 if the export is good to * NBD_OPT_EXPORT_NAME in that case), and > 0 if the export is good to
* go (with the rest of @info populated). */ * go (with the rest of @info populated).
static int nbd_opt_go(QIOChannel *ioc, NBDExportInfo *info, Error **errp) */
static int nbd_opt_info_or_go(QIOChannel *ioc, uint32_t opt,
NBDExportInfo *info, Error **errp)
{ {
NBDOptionReply reply; NBDOptionReply reply;
uint32_t len = strlen(info->name); uint32_t len = strlen(info->name);
@ -347,7 +352,8 @@ static int nbd_opt_go(QIOChannel *ioc, NBDExportInfo *info, Error **errp)
* flags still 0 is a witness of a broken server. */ * flags still 0 is a witness of a broken server. */
info->flags = 0; info->flags = 0;
trace_nbd_opt_go_start(info->name); assert(opt == NBD_OPT_GO || opt == NBD_OPT_INFO);
trace_nbd_opt_info_go_start(nbd_opt_lookup(opt), info->name);
buf = g_malloc(4 + len + 2 + 2 * info->request_sizes + 1); buf = g_malloc(4 + len + 2 + 2 * info->request_sizes + 1);
stl_be_p(buf, len); stl_be_p(buf, len);
memcpy(buf + 4, info->name, len); memcpy(buf + 4, info->name, len);
@ -356,7 +362,7 @@ static int nbd_opt_go(QIOChannel *ioc, NBDExportInfo *info, Error **errp)
if (info->request_sizes) { if (info->request_sizes) {
stw_be_p(buf + 4 + len + 2, NBD_INFO_BLOCK_SIZE); stw_be_p(buf + 4 + len + 2, NBD_INFO_BLOCK_SIZE);
} }
error = nbd_send_option_request(ioc, NBD_OPT_GO, error = nbd_send_option_request(ioc, opt,
4 + len + 2 + 2 * info->request_sizes, 4 + len + 2 + 2 * info->request_sizes,
buf, errp); buf, errp);
g_free(buf); g_free(buf);
@ -365,7 +371,7 @@ static int nbd_opt_go(QIOChannel *ioc, NBDExportInfo *info, Error **errp)
} }
while (1) { while (1) {
if (nbd_receive_option_reply(ioc, NBD_OPT_GO, &reply, errp) < 0) { if (nbd_receive_option_reply(ioc, opt, &reply, errp) < 0) {
return -1; return -1;
} }
error = nbd_handle_reply_err(ioc, &reply, errp); error = nbd_handle_reply_err(ioc, &reply, errp);
@ -375,8 +381,10 @@ static int nbd_opt_go(QIOChannel *ioc, NBDExportInfo *info, Error **errp)
len = reply.length; len = reply.length;
if (reply.type == NBD_REP_ACK) { if (reply.type == NBD_REP_ACK) {
/* Server is done sending info and moved into transmission /*
phase, but make sure it sent flags */ * Server is done sending info, and moved into transmission
* phase for NBD_OPT_GO, but make sure it sent flags
*/
if (len) { if (len) {
error_setg(errp, "server sent invalid NBD_REP_ACK"); error_setg(errp, "server sent invalid NBD_REP_ACK");
return -1; return -1;
@ -385,7 +393,7 @@ static int nbd_opt_go(QIOChannel *ioc, NBDExportInfo *info, Error **errp)
error_setg(errp, "broken server omitted NBD_INFO_EXPORT"); error_setg(errp, "broken server omitted NBD_INFO_EXPORT");
return -1; return -1;
} }
trace_nbd_opt_go_success(); trace_nbd_opt_info_go_success(nbd_opt_lookup(opt));
return 1; return 1;
} }
if (reply.type != NBD_REP_INFO) { if (reply.type != NBD_REP_INFO) {
@ -479,12 +487,12 @@ static int nbd_opt_go(QIOChannel *ioc, NBDExportInfo *info, Error **errp)
nbd_send_opt_abort(ioc); nbd_send_opt_abort(ioc);
return -1; return -1;
} }
trace_nbd_opt_go_info_block_size(info->min_block, info->opt_block, trace_nbd_opt_info_block_size(info->min_block, info->opt_block,
info->max_block); info->max_block);
break; break;
default: default:
trace_nbd_opt_go_info_unknown(type, nbd_info_lookup(type)); trace_nbd_opt_info_unknown(type, nbd_info_lookup(type));
if (nbd_drop(ioc, len, errp) < 0) { if (nbd_drop(ioc, len, errp) < 0) {
error_prepend(errp, "Failed to read info payload: "); error_prepend(errp, "Failed to read info payload: ");
nbd_send_opt_abort(ioc); nbd_send_opt_abort(ioc);
@ -993,7 +1001,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, QCryptoTLSCreds *tlscreds,
* TLS). If it is not available, fall back to * TLS). If it is not available, fall back to
* NBD_OPT_LIST for nicer error messages about a missing * NBD_OPT_LIST for nicer error messages about a missing
* export, then use NBD_OPT_EXPORT_NAME. */ * export, then use NBD_OPT_EXPORT_NAME. */
result = nbd_opt_go(ioc, info, errp); result = nbd_opt_info_or_go(ioc, NBD_OPT_GO, info, errp);
if (result < 0) { if (result < 0) {
return -EINVAL; return -EINVAL;
} }

View File

@ -4,10 +4,10 @@ nbd_receive_option_reply(uint32_t option, const char *optname, uint32_t type, co
nbd_server_error_msg(uint32_t err, const char *type, const char *msg) "server reported error 0x%" PRIx32 " (%s) with additional message: %s" nbd_server_error_msg(uint32_t err, const char *type, const char *msg) "server reported error 0x%" PRIx32 " (%s) with additional message: %s"
nbd_reply_err_unsup(uint32_t option, const char *name) "server doesn't understand request %" PRIu32 " (%s), attempting fallback" nbd_reply_err_unsup(uint32_t option, const char *name) "server doesn't understand request %" PRIu32 " (%s), attempting fallback"
nbd_receive_list(const char *name, const char *desc) "export list includes '%s', description '%s'" nbd_receive_list(const char *name, const char *desc) "export list includes '%s', description '%s'"
nbd_opt_go_start(const char *name) "Attempting NBD_OPT_GO for export '%s'" nbd_opt_info_go_start(const char *opt, const char *name) "Attempting %s for export '%s'"
nbd_opt_go_success(void) "Export is good to go" nbd_opt_info_go_success(const char *opt) "Export is ready after %s request"
nbd_opt_go_info_unknown(int info, const char *name) "Ignoring unknown info %d (%s)" nbd_opt_info_unknown(int info, const char *name) "Ignoring unknown info %d (%s)"
nbd_opt_go_info_block_size(uint32_t minimum, uint32_t preferred, uint32_t maximum) "Block sizes are 0x%" PRIx32 ", 0x%" PRIx32 ", 0x%" PRIx32 nbd_opt_info_block_size(uint32_t minimum, uint32_t preferred, uint32_t maximum) "Block sizes are 0x%" PRIx32 ", 0x%" PRIx32 ", 0x%" PRIx32
nbd_receive_query_exports_start(const char *wantname) "Querying export list for '%s'" nbd_receive_query_exports_start(const char *wantname) "Querying export list for '%s'"
nbd_receive_query_exports_success(const char *wantname) "Found desired export name '%s'" nbd_receive_query_exports_success(const char *wantname) "Found desired export name '%s'"
nbd_receive_starttls_new_client(void) "Setting up TLS" nbd_receive_starttls_new_client(void) "Setting up TLS"