return ENOMEM;
case NBD_ENOSPC:
return ENOSPC;
- case NBD_EINVAL:
default:
+ TRACE("Squashing unexpected error %d to EINVAL", err);
+ /* fallthrough */
+ case NBD_EINVAL:
return EINVAL;
}
}
*/
-static int nbd_handle_reply_err(uint32_t opt, uint32_t type, Error **errp)
+/* If type represents success, return 1 without further action.
+ * If type represents an error reply, consume the rest of the packet on ioc.
+ * Then return 0 for unsupported (so the client can fall back to
+ * other approaches), or -1 with errp set for other errors.
+ */
+static int nbd_handle_reply_err(QIOChannel *ioc, uint32_t opt, uint32_t type,
+ Error **errp)
{
+ uint32_t len;
+ char *msg = NULL;
+ int result = -1;
+
if (!(type & (1 << 31))) {
- return 0;
+ return 1;
+ }
+
+ if (read_sync(ioc, &len, sizeof(len)) != sizeof(len)) {
+ error_setg(errp, "failed to read option length");
+ return -1;
+ }
+ len = be32_to_cpu(len);
+ if (len) {
+ if (len > NBD_MAX_BUFFER_SIZE) {
+ error_setg(errp, "server's error message is too long");
+ goto cleanup;
+ }
+ msg = g_malloc(len + 1);
+ if (read_sync(ioc, msg, len) != len) {
+ error_setg(errp, "failed to read option error message");
+ goto cleanup;
+ }
+ msg[len] = '\0';
}
switch (type) {
case NBD_REP_ERR_UNSUP:
- error_setg(errp, "Unsupported option type %x", opt);
- break;
+ TRACE("server doesn't understand request %" PRIx32
+ ", attempting fallback", opt);
+ result = 0;
+ goto cleanup;
case NBD_REP_ERR_POLICY:
- error_setg(errp, "Denied by server for option %x", opt);
+ error_setg(errp, "Denied by server for option %" PRIx32, opt);
break;
case NBD_REP_ERR_INVALID:
- error_setg(errp, "Invalid data length for option %x", opt);
+ error_setg(errp, "Invalid data length for option %" PRIx32, opt);
break;
case NBD_REP_ERR_TLS_REQD:
- error_setg(errp, "TLS negotiation required before option %x", opt);
+ error_setg(errp, "TLS negotiation required before option %" PRIx32,
+ opt);
break;
default:
- error_setg(errp, "Unknown error code when asking for option %x", opt);
+ error_setg(errp, "Unknown error code when asking for option %" PRIx32,
+ opt);
break;
}
- return -1;
+ if (msg) {
+ error_append_hint(errp, "%s\n", msg);
+ }
+
+ cleanup:
+ g_free(msg);
+ return result;
}
static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
uint32_t type;
uint32_t len;
uint32_t namelen;
+ int error;
*name = NULL;
if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
}
opt = be32_to_cpu(opt);
if (opt != NBD_OPT_LIST) {
- error_setg(errp, "Unexpected option type %x expected %x",
+ error_setg(errp, "Unexpected option type %" PRIx32 " expected %x",
opt, NBD_OPT_LIST);
return -1;
}
return -1;
}
type = be32_to_cpu(type);
- if (type == NBD_REP_ERR_UNSUP) {
- return 0;
- }
- if (nbd_handle_reply_err(opt, type, errp) < 0) {
- return -1;
+ error = nbd_handle_reply_err(ioc, opt, type, errp);
+ if (error <= 0) {
+ return error;
}
if (read_sync(ioc, &len, sizeof(len)) != sizeof(len)) {
return -1;
}
} else if (type == NBD_REP_SERVER) {
+ if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
+ error_setg(errp, "incorrect option length");
+ return -1;
+ }
if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) {
error_setg(errp, "failed to read option name length");
return -1;
}
namelen = be32_to_cpu(namelen);
- if (len != (namelen + sizeof(namelen))) {
- error_setg(errp, "incorrect option mame length");
+ len -= sizeof(namelen);
+ if (len < namelen) {
+ error_setg(errp, "incorrect option name length");
return -1;
}
- if (namelen > 255) {
- error_setg(errp, "export name length too long %d", namelen);
+ if (namelen > NBD_MAX_NAME_SIZE) {
+ error_setg(errp, "export name length too long %" PRIu32, namelen);
return -1;
}
return -1;
}
(*name)[namelen] = '\0';
+ len -= namelen;
+ if (len) {
+ char *buf = g_malloc(len + 1);
+ if (read_sync(ioc, buf, len) != len) {
+ error_setg(errp, "failed to read export description");
+ g_free(*name);
+ g_free(buf);
+ *name = NULL;
+ return -1;
+ }
+ buf[len] = '\0';
+ TRACE("Ignoring export description: %s", buf);
+ g_free(buf);
+ }
} else {
- error_setg(errp, "Unexpected reply type %x expected %x",
+ error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x",
type, NBD_REP_SERVER);
return -1;
}
}
opt = be32_to_cpu(opt);
if (opt != NBD_OPT_STARTTLS) {
- error_setg(errp, "Unexpected option type %x expected %x",
+ error_setg(errp, "Unexpected option type %" PRIx32 " expected %x",
opt, NBD_OPT_STARTTLS);
return NULL;
}
}
type = be32_to_cpu(type);
if (type != NBD_REP_ACK) {
- error_setg(errp, "Server rejected request to start TLS %x",
+ error_setg(errp, "Server rejected request to start TLS %" PRIx32,
type);
return NULL;
}
}
length = be32_to_cpu(length);
if (length != 0) {
- error_setg(errp, "Start TLS reponse was not zero %x",
+ error_setg(errp, "Start TLS response was not zero %" PRIu32,
length);
return NULL;
}
return NULL;
}
data.loop = g_main_loop_new(g_main_context_default(), FALSE);
- TRACE("Starting TLS hanshake");
+ TRACE("Starting TLS handshake");
qio_channel_tls_handshake(tioc,
nbd_tls_handshake,
&data,
}
globalflags = be16_to_cpu(globalflags);
*flags = globalflags << 16;
- TRACE("Global flags are %x", globalflags);
+ TRACE("Global flags are %" PRIx32, globalflags);
if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
fixedNewStyle = true;
TRACE("Server supports fixed new style");
}
exportflags = be16_to_cpu(exportflags);
*flags |= exportflags;
- TRACE("Export flags are %x", exportflags);
+ TRACE("Export flags are %" PRIx16, exportflags);
} else if (magic == NBD_CLIENT_MAGIC) {
if (name) {
error_setg(errp, "Server does not support export names");
error_setg(errp, "Failed to read export flags");
goto fail;
}
- *flags = be32_to_cpup(flags);
+ *flags = be32_to_cpu(*flags);
} else {
error_setg(errp, "Bad magic received");
goto fail;
#ifdef __linux__
int nbd_init(int fd, QIOChannelSocket *sioc, uint32_t flags, off_t size)
{
+ unsigned long sectors = size / BDRV_SECTOR_SIZE;
+ if (size / BDRV_SECTOR_SIZE != sectors) {
+ LOG("Export size %lld too large for 32-bit kernel", (long long) size);
+ return -E2BIG;
+ }
+
TRACE("Setting NBD socket");
- if (ioctl(fd, NBD_SET_SOCK, sioc->fd) < 0) {
+ if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) {
int serrno = errno;
LOG("Failed to set NBD socket");
return -serrno;
TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);
- if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) {
+ if (ioctl(fd, NBD_SET_BLKSIZE, (unsigned long)BDRV_SECTOR_SIZE) < 0) {
int serrno = errno;
LOG("Failed setting NBD block size");
return -serrno;
}
- TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE));
+ TRACE("Setting size to %lu block(s)", sectors);
+ if (size % BDRV_SECTOR_SIZE) {
+ TRACE("Ignoring trailing %d bytes of export",
+ (int) (size % BDRV_SECTOR_SIZE));
+ }
- if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) {
+ if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) {
int serrno = errno;
LOG("Failed setting size (in blocks)");
return -serrno;
}
- if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
+ if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) flags) < 0) {
if (errno == ENOTTY) {
int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
TRACE("Setting readonly attribute");
errno = serrno;
return ret;
}
+
+int nbd_disconnect(int fd)
+{
+ ioctl(fd, NBD_CLEAR_QUE);
+ ioctl(fd, NBD_DISCONNECT);
+ ioctl(fd, NBD_CLEAR_SOCK);
+ return 0;
+}
+
#else
int nbd_init(int fd, QIOChannelSocket *ioc, uint32_t flags, off_t size)
{
{
return -ENOTSUP;
}
+int nbd_disconnect(int fd)
+{
+ return -ENOTSUP;
+}
#endif
ssize_t nbd_send_request(QIOChannel *ioc, struct nbd_request *request)
uint8_t buf[NBD_REQUEST_SIZE];
ssize_t ret;
- cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
- cpu_to_be32w((uint32_t*)(buf + 4), request->type);
- cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
- cpu_to_be64w((uint64_t*)(buf + 16), request->from);
- cpu_to_be32w((uint32_t*)(buf + 24), request->len);
-
TRACE("Sending request to server: "
- "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
+ "{ .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64
+ ", .type=%" PRIu32 " }",
request->from, request->len, request->handle, request->type);
+ stl_be_p(buf, NBD_REQUEST_MAGIC);
+ stl_be_p(buf + 4, request->type);
+ stq_be_p(buf + 8, request->handle);
+ stq_be_p(buf + 16, request->from);
+ stl_be_p(buf + 24, request->len);
+
ret = write_sync(ioc, buf, sizeof(buf));
if (ret < 0) {
return ret;
[ 7 .. 15] handle
*/
- magic = be32_to_cpup((uint32_t*)buf);
- reply->error = be32_to_cpup((uint32_t*)(buf + 4));
- reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
+ magic = ldl_be_p(buf);
+ reply->error = ldl_be_p(buf + 4);
+ reply->handle = ldq_be_p(buf + 8);
reply->error = nbd_errno_to_system_errno(reply->error);
- TRACE("Got reply: "
- "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
+ TRACE("Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32
+ ", handle = %" PRIu64" }",
magic, reply->error, reply->handle);
if (magic != NBD_REPLY_MAGIC) {
- LOG("invalid magic (got 0x%x)", magic);
+ LOG("invalid magic (got 0x%" PRIx32 ")", magic);
return -EINVAL;
}
return 0;