nbd/server: add nbd_opt_invalid helper

NBD_REP_ERR_INVALID is often parameter to nbd_opt_drop and it would
be used more in following patches. So, let's add a helper.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180312152126.286890-2-vsementsov@virtuozzo.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Vladimir Sementsov-Ogievskiy 2018-03-12 18:21:19 +03:00 committed by Eric Blake
parent 44a8174e0a
commit 2e425fd568

View file

@ -218,19 +218,43 @@ nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type,
/* Drop remainder of the current option, and send a reply with the /* Drop remainder of the current option, and send a reply with the
* given error type and message. Return -errno on read or write * given error type and message. Return -errno on read or write
* failure; or 0 if connection is still live. */ * failure; or 0 if connection is still live. */
static int GCC_FMT_ATTR(4, 0)
nbd_opt_vdrop(NBDClient *client, uint32_t type, Error **errp,
const char *fmt, va_list va)
{
int ret = nbd_drop(client->ioc, client->optlen, errp);
client->optlen = 0;
if (!ret) {
ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
}
return ret;
}
static int GCC_FMT_ATTR(4, 5) static int GCC_FMT_ATTR(4, 5)
nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp, nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp,
const char *fmt, ...) const char *fmt, ...)
{ {
int ret = nbd_drop(client->ioc, client->optlen, errp); int ret;
va_list va; va_list va;
client->optlen = 0; va_start(va, fmt);
if (!ret) { ret = nbd_opt_vdrop(client, type, errp, fmt, va);
va_start(va, fmt); va_end(va);
ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
va_end(va); return ret;
} }
static int GCC_FMT_ATTR(3, 4)
nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...)
{
int ret;
va_list va;
va_start(va, fmt);
ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va);
va_end(va);
return ret; return ret;
} }
@ -241,9 +265,9 @@ static int nbd_opt_read(NBDClient *client, void *buffer, size_t size,
Error **errp) Error **errp)
{ {
if (size > client->optlen) { if (size > client->optlen) {
return nbd_opt_drop(client, NBD_REP_ERR_INVALID, errp, return nbd_opt_invalid(client, errp,
"Inconsistent lengths in option %s", "Inconsistent lengths in option %s",
nbd_opt_lookup(client->opt)); nbd_opt_lookup(client->opt));
} }
client->optlen -= size; client->optlen -= size;
return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1; return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1;
@ -398,9 +422,8 @@ static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp)
int ret; int ret;
assert(client->optlen); assert(client->optlen);
ret = nbd_opt_drop(client, NBD_REP_ERR_INVALID, errp, ret = nbd_opt_invalid(client, errp, "option '%s' has unexpected length",
"option '%s' has unexpected length", nbd_opt_lookup(client->opt));
nbd_opt_lookup(client->opt));
if (fatal && !ret) { if (fatal && !ret) {
error_setg(errp, "option '%s' has unexpected length", error_setg(errp, "option '%s' has unexpected length",
nbd_opt_lookup(client->opt)); nbd_opt_lookup(client->opt));
@ -438,8 +461,7 @@ static int nbd_negotiate_handle_info(NBDClient *client, uint16_t myflags,
} }
be32_to_cpus(&namelen); be32_to_cpus(&namelen);
if (namelen >= sizeof(name)) { if (namelen >= sizeof(name)) {
return nbd_opt_drop(client, NBD_REP_ERR_INVALID, errp, return nbd_opt_invalid(client, errp, "name too long for qemu");
"name too long for qemu");
} }
rc = nbd_opt_read(client, name, namelen, errp); rc = nbd_opt_read(client, name, namelen, errp);
if (rc <= 0) { if (rc <= 0) {