提交 185b4338 编写于 作者: P Paolo Bonzini

nbd: consistently return negative errno values

In the next patch we need to look at the return code of nbd_wr_sync.
To avoid percolating the socket_error() ugliness all around, let's
handle errors by returning negative errno values.
Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
上级 fc19f8a0
...@@ -200,8 +200,7 @@ static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request, ...@@ -200,8 +200,7 @@ static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request,
if (rc >= 0 && iov) { if (rc >= 0 && iov) {
ret = qemu_co_sendv(s->sock, iov, request->len, offset); ret = qemu_co_sendv(s->sock, iov, request->len, offset);
if (ret != request->len) { if (ret != request->len) {
errno = -EIO; return -EIO;
rc = -1;
} }
} }
qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL, qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL,
...@@ -271,7 +270,7 @@ static int nbd_establish_connection(BlockDriverState *bs) ...@@ -271,7 +270,7 @@ static int nbd_establish_connection(BlockDriverState *bs)
if (ret < 0) { if (ret < 0) {
logout("Failed to negotiate with the NBD server\n"); logout("Failed to negotiate with the NBD server\n");
closesocket(sock); closesocket(sock);
return -errno; return ret;
} }
/* Now that we're connected, set the socket to be non-blocking and /* Now that we're connected, set the socket to be non-blocking and
...@@ -340,7 +339,7 @@ static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num, ...@@ -340,7 +339,7 @@ static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
nbd_coroutine_start(s, &request); nbd_coroutine_start(s, &request);
ret = nbd_co_send_request(s, &request, NULL, 0); ret = nbd_co_send_request(s, &request, NULL, 0);
if (ret < 0) { if (ret < 0) {
reply.error = errno; reply.error = -ret;
} else { } else {
nbd_co_receive_reply(s, &request, &reply, qiov->iov, offset); nbd_co_receive_reply(s, &request, &reply, qiov->iov, offset);
} }
...@@ -369,7 +368,7 @@ static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num, ...@@ -369,7 +368,7 @@ static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
nbd_coroutine_start(s, &request); nbd_coroutine_start(s, &request);
ret = nbd_co_send_request(s, &request, qiov->iov, offset); ret = nbd_co_send_request(s, &request, qiov->iov, offset);
if (ret < 0) { if (ret < 0) {
reply.error = errno; reply.error = -ret;
} else { } else {
nbd_co_receive_reply(s, &request, &reply, NULL, 0); nbd_co_receive_reply(s, &request, &reply, NULL, 0);
} }
...@@ -437,7 +436,7 @@ static int nbd_co_flush(BlockDriverState *bs) ...@@ -437,7 +436,7 @@ static int nbd_co_flush(BlockDriverState *bs)
nbd_coroutine_start(s, &request); nbd_coroutine_start(s, &request);
ret = nbd_co_send_request(s, &request, NULL, 0); ret = nbd_co_send_request(s, &request, NULL, 0);
if (ret < 0) { if (ret < 0) {
reply.error = errno; reply.error = -ret;
} else { } else {
nbd_co_receive_reply(s, &request, &reply, NULL, 0); nbd_co_receive_reply(s, &request, &reply, NULL, 0);
} }
...@@ -463,7 +462,7 @@ static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num, ...@@ -463,7 +462,7 @@ static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
nbd_coroutine_start(s, &request); nbd_coroutine_start(s, &request);
ret = nbd_co_send_request(s, &request, NULL, 0); ret = nbd_co_send_request(s, &request, NULL, 0);
if (ret < 0) { if (ret < 0) {
reply.error = errno; reply.error = -ret;
} else { } else {
nbd_co_receive_reply(s, &request, &reply, NULL, 0); nbd_co_receive_reply(s, &request, &reply, NULL, 0);
} }
......
...@@ -81,9 +81,10 @@ ...@@ -81,9 +81,10 @@
#define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true) #define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
#define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false) #define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read) ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
{ {
size_t offset = 0; size_t offset = 0;
int err;
if (qemu_in_coroutine()) { if (qemu_in_coroutine()) {
if (do_read) { if (do_read) {
...@@ -103,15 +104,15 @@ size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read) ...@@ -103,15 +104,15 @@ size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
} }
if (len < 0) { if (len < 0) {
errno = socket_error(); err = socket_error();
/* recoverable error */ /* recoverable error */
if (errno == EINTR || errno == EAGAIN) { if (err == EINTR || err == EAGAIN) {
continue; continue;
} }
/* unrecoverable error */ /* unrecoverable error */
return 0; return -err;
} }
/* eof */ /* eof */
...@@ -192,6 +193,7 @@ int unix_socket_outgoing(const char *path) ...@@ -192,6 +193,7 @@ int unix_socket_outgoing(const char *path)
static int nbd_send_negotiate(int csock, off_t size, uint32_t flags) static int nbd_send_negotiate(int csock, off_t size, uint32_t flags)
{ {
char buf[8 + 8 + 8 + 128]; char buf[8 + 8 + 8 + 128];
int rc;
/* Negotiate /* Negotiate
[ 0 .. 7] passwd ("NBDMAGIC") [ 0 .. 7] passwd ("NBDMAGIC")
...@@ -201,6 +203,8 @@ static int nbd_send_negotiate(int csock, off_t size, uint32_t flags) ...@@ -201,6 +203,8 @@ static int nbd_send_negotiate(int csock, off_t size, uint32_t flags)
[28 .. 151] reserved (0) [28 .. 151] reserved (0)
*/ */
rc = -EINVAL;
TRACE("Beginning negotiation."); TRACE("Beginning negotiation.");
memcpy(buf, "NBDMAGIC", 8); memcpy(buf, "NBDMAGIC", 8);
cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL); cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
...@@ -212,13 +216,13 @@ static int nbd_send_negotiate(int csock, off_t size, uint32_t flags) ...@@ -212,13 +216,13 @@ static int nbd_send_negotiate(int csock, off_t size, uint32_t flags)
if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) { if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
LOG("write failed"); LOG("write failed");
errno = EINVAL; goto fail;
return -1;
} }
TRACE("Negotiation succeeded."); TRACE("Negotiation succeeded.");
rc = 0;
return 0; fail:
return rc;
} }
int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags, int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
...@@ -227,20 +231,21 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags, ...@@ -227,20 +231,21 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
char buf[256]; char buf[256];
uint64_t magic, s; uint64_t magic, s;
uint16_t tmp; uint16_t tmp;
int rc;
TRACE("Receiving negotiation."); TRACE("Receiving negotiation.");
rc = -EINVAL;
if (read_sync(csock, buf, 8) != 8) { if (read_sync(csock, buf, 8) != 8) {
LOG("read failed"); LOG("read failed");
errno = EINVAL; goto fail;
return -1;
} }
buf[8] = '\0'; buf[8] = '\0';
if (strlen(buf) == 0) { if (strlen(buf) == 0) {
LOG("server connection closed"); LOG("server connection closed");
errno = EINVAL; goto fail;
return -1;
} }
TRACE("Magic is %c%c%c%c%c%c%c%c", TRACE("Magic is %c%c%c%c%c%c%c%c",
...@@ -255,14 +260,12 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags, ...@@ -255,14 +260,12 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
if (memcmp(buf, "NBDMAGIC", 8) != 0) { if (memcmp(buf, "NBDMAGIC", 8) != 0) {
LOG("Invalid magic received"); LOG("Invalid magic received");
errno = EINVAL; goto fail;
return -1;
} }
if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) { if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
LOG("read failed"); LOG("read failed");
errno = EINVAL; goto fail;
return -1;
} }
magic = be64_to_cpu(magic); magic = be64_to_cpu(magic);
TRACE("Magic is 0x%" PRIx64, magic); TRACE("Magic is 0x%" PRIx64, magic);
...@@ -275,61 +278,52 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags, ...@@ -275,61 +278,52 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
TRACE("Checking magic (opts_magic)"); TRACE("Checking magic (opts_magic)");
if (magic != 0x49484156454F5054LL) { if (magic != 0x49484156454F5054LL) {
LOG("Bad magic received"); LOG("Bad magic received");
errno = EINVAL; goto fail;
return -1;
} }
if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) { if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
LOG("flags read failed"); LOG("flags read failed");
errno = EINVAL; goto fail;
return -1;
} }
*flags = be16_to_cpu(tmp) << 16; *flags = be16_to_cpu(tmp) << 16;
/* reserved for future use */ /* reserved for future use */
if (write_sync(csock, &reserved, sizeof(reserved)) != if (write_sync(csock, &reserved, sizeof(reserved)) !=
sizeof(reserved)) { sizeof(reserved)) {
LOG("write failed (reserved)"); LOG("write failed (reserved)");
errno = EINVAL; goto fail;
return -1;
} }
/* write the export name */ /* write the export name */
magic = cpu_to_be64(magic); magic = cpu_to_be64(magic);
if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) { if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
LOG("write failed (magic)"); LOG("write failed (magic)");
errno = EINVAL; goto fail;
return -1;
} }
opt = cpu_to_be32(NBD_OPT_EXPORT_NAME); opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) { if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
LOG("write failed (opt)"); LOG("write failed (opt)");
errno = EINVAL; goto fail;
return -1;
} }
namesize = cpu_to_be32(strlen(name)); namesize = cpu_to_be32(strlen(name));
if (write_sync(csock, &namesize, sizeof(namesize)) != if (write_sync(csock, &namesize, sizeof(namesize)) !=
sizeof(namesize)) { sizeof(namesize)) {
LOG("write failed (namesize)"); LOG("write failed (namesize)");
errno = EINVAL; goto fail;
return -1;
} }
if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) { if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
LOG("write failed (name)"); LOG("write failed (name)");
errno = EINVAL; goto fail;
return -1;
} }
} else { } else {
TRACE("Checking magic (cli_magic)"); TRACE("Checking magic (cli_magic)");
if (magic != 0x00420281861253LL) { if (magic != 0x00420281861253LL) {
LOG("Bad magic received"); LOG("Bad magic received");
errno = EINVAL; goto fail;
return -1;
} }
} }
if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) { if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
LOG("read failed"); LOG("read failed");
errno = EINVAL; goto fail;
return -1;
} }
*size = be64_to_cpu(s); *size = be64_to_cpu(s);
*blocksize = 1024; *blocksize = 1024;
...@@ -338,24 +332,24 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags, ...@@ -338,24 +332,24 @@ int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
if (!name) { if (!name) {
if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) { if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
LOG("read failed (flags)"); LOG("read failed (flags)");
errno = EINVAL; goto fail;
return -1;
} }
*flags = be32_to_cpup(flags); *flags = be32_to_cpup(flags);
} else { } else {
if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) { if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
LOG("read failed (tmp)"); LOG("read failed (tmp)");
errno = EINVAL; goto fail;
return -1;
} }
*flags |= be32_to_cpu(tmp); *flags |= be32_to_cpu(tmp);
} }
if (read_sync(csock, &buf, 124) != 124) { if (read_sync(csock, &buf, 124) != 124) {
LOG("read failed (buf)"); LOG("read failed (buf)");
errno = EINVAL; goto fail;
return -1;
} }
return 0; rc = 0;
fail:
return rc;
} }
#ifdef __linux__ #ifdef __linux__
...@@ -366,8 +360,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize) ...@@ -366,8 +360,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
if (ioctl(fd, NBD_SET_SOCK, csock) < 0) { if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
int serrno = errno; int serrno = errno;
LOG("Failed to set NBD socket"); LOG("Failed to set NBD socket");
errno = serrno; return -serrno;
return -1;
} }
TRACE("Setting block size to %lu", (unsigned long)blocksize); TRACE("Setting block size to %lu", (unsigned long)blocksize);
...@@ -375,8 +368,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize) ...@@ -375,8 +368,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) { if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {
int serrno = errno; int serrno = errno;
LOG("Failed setting NBD block size"); LOG("Failed setting NBD block size");
errno = serrno; return -serrno;
return -1;
} }
TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize)); TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
...@@ -384,8 +376,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize) ...@@ -384,8 +376,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) { if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {
int serrno = errno; int serrno = errno;
LOG("Failed setting size (in blocks)"); LOG("Failed setting size (in blocks)");
errno = serrno; return -serrno;
return -1;
} }
if (flags & NBD_FLAG_READ_ONLY) { if (flags & NBD_FLAG_READ_ONLY) {
...@@ -395,8 +386,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize) ...@@ -395,8 +386,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) { if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
int serrno = errno; int serrno = errno;
LOG("Failed setting read-only attribute"); LOG("Failed setting read-only attribute");
errno = serrno; return -serrno;
return -1;
} }
} }
...@@ -404,8 +394,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize) ...@@ -404,8 +394,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
&& errno != ENOTTY) { && errno != ENOTTY) {
int serrno = errno; int serrno = errno;
LOG("Failed setting flags"); LOG("Failed setting flags");
errno = serrno; return -serrno;
return -1;
} }
TRACE("Negotiation ended"); TRACE("Negotiation ended");
...@@ -452,26 +441,24 @@ int nbd_client(int fd) ...@@ -452,26 +441,24 @@ int nbd_client(int fd)
#else #else
int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize) int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
{ {
errno = ENOTSUP; return -ENOTSUP;
return -1;
} }
int nbd_disconnect(int fd) int nbd_disconnect(int fd)
{ {
errno = ENOTSUP; return -ENOTSUP;
return -1;
} }
int nbd_client(int fd) int nbd_client(int fd)
{ {
errno = ENOTSUP; return -ENOTSUP;
return -1;
} }
#endif #endif
ssize_t nbd_send_request(int csock, struct nbd_request *request) ssize_t nbd_send_request(int csock, struct nbd_request *request)
{ {
uint8_t buf[4 + 4 + 8 + 8 + 4]; uint8_t buf[4 + 4 + 8 + 8 + 4];
ssize_t ret;
cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC); cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
cpu_to_be32w((uint32_t*)(buf + 4), request->type); cpu_to_be32w((uint32_t*)(buf + 4), request->type);
...@@ -483,10 +470,14 @@ ssize_t nbd_send_request(int csock, struct nbd_request *request) ...@@ -483,10 +470,14 @@ ssize_t nbd_send_request(int csock, struct nbd_request *request)
"{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}", "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
request->from, request->len, request->handle, request->type); request->from, request->len, request->handle, request->type);
if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) { ret = write_sync(csock, buf, sizeof(buf));
if (ret < 0) {
return ret;
}
if (ret != sizeof(buf)) {
LOG("writing to socket failed"); LOG("writing to socket failed");
errno = EINVAL; return -EINVAL;
return -1;
} }
return 0; return 0;
} }
...@@ -495,11 +486,16 @@ static ssize_t nbd_receive_request(int csock, struct nbd_request *request) ...@@ -495,11 +486,16 @@ static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
{ {
uint8_t buf[4 + 4 + 8 + 8 + 4]; uint8_t buf[4 + 4 + 8 + 8 + 4];
uint32_t magic; uint32_t magic;
ssize_t ret;
if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) { ret = read_sync(csock, buf, sizeof(buf));
if (ret < 0) {
return ret;
}
if (ret != sizeof(buf)) {
LOG("read failed"); LOG("read failed");
errno = EINVAL; return -EINVAL;
return -1;
} }
/* Request /* Request
...@@ -522,8 +518,7 @@ static ssize_t nbd_receive_request(int csock, struct nbd_request *request) ...@@ -522,8 +518,7 @@ static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
if (magic != NBD_REQUEST_MAGIC) { if (magic != NBD_REQUEST_MAGIC) {
LOG("invalid magic (got 0x%x)", magic); LOG("invalid magic (got 0x%x)", magic);
errno = EINVAL; return -EINVAL;
return -1;
} }
return 0; return 0;
} }
...@@ -532,11 +527,16 @@ ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) ...@@ -532,11 +527,16 @@ ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
{ {
uint8_t buf[NBD_REPLY_SIZE]; uint8_t buf[NBD_REPLY_SIZE];
uint32_t magic; uint32_t magic;
ssize_t ret;
ret = read_sync(csock, buf, sizeof(buf));
if (ret < 0) {
return ret;
}
if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) { if (ret != sizeof(buf)) {
LOG("read failed"); LOG("read failed");
errno = EINVAL; return -EINVAL;
return -1;
} }
/* Reply /* Reply
...@@ -555,8 +555,7 @@ ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) ...@@ -555,8 +555,7 @@ ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
if (magic != NBD_REPLY_MAGIC) { if (magic != NBD_REPLY_MAGIC) {
LOG("invalid magic (got 0x%x)", magic); LOG("invalid magic (got 0x%x)", magic);
errno = EINVAL; return -EINVAL;
return -1;
} }
return 0; return 0;
} }
...@@ -564,6 +563,7 @@ ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) ...@@ -564,6 +563,7 @@ ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply) static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
{ {
uint8_t buf[4 + 4 + 8]; uint8_t buf[4 + 4 + 8];
ssize_t ret;
/* Reply /* Reply
[ 0 .. 3] magic (NBD_REPLY_MAGIC) [ 0 .. 3] magic (NBD_REPLY_MAGIC)
...@@ -576,10 +576,14 @@ static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply) ...@@ -576,10 +576,14 @@ static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
TRACE("Sending response to client"); TRACE("Sending response to client");
if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) { ret = write_sync(csock, buf, sizeof(buf));
if (ret < 0) {
return ret;
}
if (ret != sizeof(buf)) {
LOG("writing to socket failed"); LOG("writing to socket failed");
errno = EINVAL; return -EINVAL;
return -1;
} }
return 0; return 0;
} }
...@@ -713,22 +717,15 @@ static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply, ...@@ -713,22 +717,15 @@ static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
if (!len) { if (!len) {
rc = nbd_send_reply(csock, reply); rc = nbd_send_reply(csock, reply);
if (rc < 0) {
rc = -errno;
}
} else { } else {
socket_set_cork(csock, 1); socket_set_cork(csock, 1);
rc = nbd_send_reply(csock, reply); rc = nbd_send_reply(csock, reply);
if (rc >= 0) { if (rc >= 0) {
ret = qemu_co_send(csock, req->data, len); ret = qemu_co_send(csock, req->data, len);
if (ret != len) { if (ret != len) {
errno = EIO; rc = -EIO;
rc = -1;
} }
} }
if (rc < 0) {
rc = -errno;
}
socket_set_cork(csock, 0); socket_set_cork(csock, 0);
} }
......
...@@ -59,7 +59,7 @@ enum { ...@@ -59,7 +59,7 @@ enum {
#define NBD_BUFFER_SIZE (1024*1024) #define NBD_BUFFER_SIZE (1024*1024)
size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read); ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read);
int tcp_socket_outgoing(const char *address, uint16_t port); int tcp_socket_outgoing(const char *address, uint16_t port);
int tcp_socket_incoming(const char *address, uint16_t port); int tcp_socket_incoming(const char *address, uint16_t port);
int tcp_socket_outgoing_spec(const char *address_and_port); int tcp_socket_outgoing_spec(const char *address_and_port);
......
...@@ -126,8 +126,7 @@ static int find_partition(BlockDriverState *bs, int partition, ...@@ -126,8 +126,7 @@ static int find_partition(BlockDriverState *bs, int partition,
} }
if (data[510] != 0x55 || data[511] != 0xaa) { if (data[510] != 0x55 || data[511] != 0xaa) {
errno = -EINVAL; return -EINVAL;
return -1;
} }
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
...@@ -165,8 +164,7 @@ static int find_partition(BlockDriverState *bs, int partition, ...@@ -165,8 +164,7 @@ static int find_partition(BlockDriverState *bs, int partition,
} }
} }
errno = -ENOENT; return -ENOENT;
return -1;
} }
static void termsig_handler(int signum) static void termsig_handler(int signum)
...@@ -491,9 +489,12 @@ int main(int argc, char **argv) ...@@ -491,9 +489,12 @@ int main(int argc, char **argv)
fd_size = bs->total_sectors * 512; fd_size = bs->total_sectors * 512;
if (partition != -1 && if (partition != -1) {
find_partition(bs, partition, &dev_offset, &fd_size)) { ret = find_partition(bs, partition, &dev_offset, &fd_size);
err(EXIT_FAILURE, "Could not find partition %d", partition); if (ret < 0) {
errno = -ret;
err(EXIT_FAILURE, "Could not find partition %d", partition);
}
} }
exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags); exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册