nbd: consistently check for <0 or >=0

This prepares for the following patch, which changes -1 return values
to negative errno.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2012-03-07 11:05:34 +01:00
parent 94e7340b5d
commit fc19f8a02e
3 changed files with 53 additions and 43 deletions

View File

@ -197,7 +197,7 @@ static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request,
qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, nbd_restart_write,
nbd_have_request, NULL, s);
rc = nbd_send_request(s->sock, request);
if (rc != -1 && iov) {
if (rc >= 0 && iov) {
ret = qemu_co_sendv(s->sock, iov, request->len, offset);
if (ret != request->len) {
errno = -EIO;
@ -260,7 +260,7 @@ static int nbd_establish_connection(BlockDriverState *bs)
}
/* Failed to establish connection */
if (sock == -1) {
if (sock < 0) {
logout("Failed to establish connection to NBD server\n");
return -errno;
}
@ -268,7 +268,7 @@ static int nbd_establish_connection(BlockDriverState *bs)
/* NBD handshake */
ret = nbd_receive_negotiate(sock, s->export_name, &s->nbdflags, &size,
&blocksize);
if (ret == -1) {
if (ret < 0) {
logout("Failed to negotiate with the NBD server\n");
closesocket(sock);
return -errno;
@ -331,13 +331,15 @@ static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
BDRVNBDState *s = bs->opaque;
struct nbd_request request;
struct nbd_reply reply;
ssize_t ret;
request.type = NBD_CMD_READ;
request.from = sector_num * 512;
request.len = nb_sectors * 512;
nbd_coroutine_start(s, &request);
if (nbd_co_send_request(s, &request, NULL, 0) == -1) {
ret = nbd_co_send_request(s, &request, NULL, 0);
if (ret < 0) {
reply.error = errno;
} else {
nbd_co_receive_reply(s, &request, &reply, qiov->iov, offset);
@ -354,6 +356,7 @@ static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
BDRVNBDState *s = bs->opaque;
struct nbd_request request;
struct nbd_reply reply;
ssize_t ret;
request.type = NBD_CMD_WRITE;
if (!bdrv_enable_write_cache(bs) && (s->nbdflags & NBD_FLAG_SEND_FUA)) {
@ -364,7 +367,8 @@ static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
request.len = nb_sectors * 512;
nbd_coroutine_start(s, &request);
if (nbd_co_send_request(s, &request, qiov->iov, offset) == -1) {
ret = nbd_co_send_request(s, &request, qiov->iov, offset);
if (ret < 0) {
reply.error = errno;
} else {
nbd_co_receive_reply(s, &request, &reply, NULL, 0);
@ -416,6 +420,7 @@ static int nbd_co_flush(BlockDriverState *bs)
BDRVNBDState *s = bs->opaque;
struct nbd_request request;
struct nbd_reply reply;
ssize_t ret;
if (!(s->nbdflags & NBD_FLAG_SEND_FLUSH)) {
return 0;
@ -430,7 +435,8 @@ static int nbd_co_flush(BlockDriverState *bs)
request.len = 0;
nbd_coroutine_start(s, &request);
if (nbd_co_send_request(s, &request, NULL, 0) == -1) {
ret = nbd_co_send_request(s, &request, NULL, 0);
if (ret < 0) {
reply.error = errno;
} else {
nbd_co_receive_reply(s, &request, &reply, NULL, 0);
@ -445,6 +451,7 @@ static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
BDRVNBDState *s = bs->opaque;
struct nbd_request request;
struct nbd_reply reply;
ssize_t ret;
if (!(s->nbdflags & NBD_FLAG_SEND_TRIM)) {
return 0;
@ -454,7 +461,8 @@ static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
request.len = nb_sectors * 512;
nbd_coroutine_start(s, &request);
if (nbd_co_send_request(s, &request, NULL, 0) == -1) {
ret = nbd_co_send_request(s, &request, NULL, 0);
if (ret < 0) {
reply.error = errno;
} else {
nbd_co_receive_reply(s, &request, &reply, NULL, 0);

48
nbd.c
View File

@ -102,12 +102,16 @@ size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
len = send(fd, buffer + offset, size - offset, 0);
}
if (len == -1)
if (len < 0) {
errno = socket_error();
/* recoverable error */
if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
continue;
/* recoverable error */
if (errno == EINTR || errno == EAGAIN) {
continue;
}
/* unrecoverable error */
return 0;
}
/* eof */
@ -115,11 +119,6 @@ size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
break;
}
/* unrecoverable error */
if (len == -1) {
return 0;
}
offset += len;
}
@ -364,7 +363,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
{
TRACE("Setting NBD socket");
if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
int serrno = errno;
LOG("Failed to set NBD socket");
errno = serrno;
@ -373,7 +372,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
TRACE("Setting block size to %lu", (unsigned long)blocksize);
if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {
int serrno = errno;
LOG("Failed setting NBD block size");
errno = serrno;
@ -382,7 +381,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {
int serrno = errno;
LOG("Failed setting size (in blocks)");
errno = serrno;
@ -430,7 +429,7 @@ int nbd_client(int fd)
TRACE("Doing NBD loop");
ret = ioctl(fd, NBD_DO_IT);
if (ret == -1 && errno == EPIPE) {
if (ret < 0 && errno == EPIPE) {
/* NBD_DO_IT normally returns EPIPE when someone has disconnected
* the socket via NBD_DISCONNECT. We do not want to return 1 in
* that case.
@ -714,20 +713,20 @@ static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
if (!len) {
rc = nbd_send_reply(csock, reply);
if (rc == -1) {
if (rc < 0) {
rc = -errno;
}
} else {
socket_set_cork(csock, 1);
rc = nbd_send_reply(csock, reply);
if (rc != -1) {
if (rc >= 0) {
ret = qemu_co_send(csock, req->data, len);
if (ret != len) {
errno = EIO;
rc = -1;
}
}
if (rc == -1) {
if (rc < 0) {
rc = -errno;
}
socket_set_cork(csock, 0);
@ -746,7 +745,7 @@ static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *reque
ssize_t rc;
client->recv_coroutine = qemu_coroutine_self();
if (nbd_receive_request(csock, request) == -1) {
if (nbd_receive_request(csock, request) < 0) {
rc = -EIO;
goto out;
}
@ -860,8 +859,9 @@ static void nbd_trip(void *opaque)
}
}
if (nbd_co_send_reply(req, &reply, 0) < 0)
if (nbd_co_send_reply(req, &reply, 0) < 0) {
goto out;
}
break;
case NBD_CMD_DISC:
TRACE("Request type is DISCONNECT");
@ -875,9 +875,9 @@ static void nbd_trip(void *opaque)
LOG("flush failed");
reply.error = -ret;
}
if (nbd_co_send_reply(req, &reply, 0) < 0)
if (nbd_co_send_reply(req, &reply, 0) < 0) {
goto out;
}
break;
case NBD_CMD_TRIM:
TRACE("Request type is TRIM");
@ -887,16 +887,18 @@ static void nbd_trip(void *opaque)
LOG("discard failed");
reply.error = -ret;
}
if (nbd_co_send_reply(req, &reply, 0) < 0)
if (nbd_co_send_reply(req, &reply, 0) < 0) {
goto out;
}
break;
default:
LOG("invalid request type (%u) received", request.type);
invalid_request:
reply.error = -EINVAL;
error_reply:
if (nbd_co_send_reply(req, &reply, 0) == -1)
if (nbd_co_send_reply(req, &reply, 0) < 0) {
goto out;
}
break;
}
@ -939,7 +941,7 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock,
void (*close)(NBDClient *))
{
NBDClient *client;
if (nbd_send_negotiate(csock, exp->size, exp->nbdflags) == -1) {
if (nbd_send_negotiate(csock, exp->size, exp->nbdflags) < 0) {
return NULL;
}
client = g_malloc0(sizeof(NBDClient));

View File

@ -186,7 +186,7 @@ static void *show_parts(void *arg)
* modprobe nbd max_part=63
*/
nbd = open(device, O_RDWR);
if (nbd != -1) {
if (nbd >= 0) {
close(nbd);
}
return NULL;
@ -203,25 +203,25 @@ static void *nbd_client_thread(void *arg)
pthread_t show_parts_thread;
sock = unix_socket_outgoing(sockpath);
if (sock == -1) {
if (sock < 0) {
goto out;
}
ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
&size, &blocksize);
if (ret == -1) {
if (ret < 0) {
goto out;
}
fd = open(device, O_RDWR);
if (fd == -1) {
if (fd < 0) {
/* Linux-only, we can use %m in printf. */
fprintf(stderr, "Failed to open %s: %m", device);
goto out;
}
ret = nbd_init(fd, sock, nbdflags, size, blocksize);
if (ret == -1) {
if (ret < 0) {
goto out;
}
@ -268,7 +268,7 @@ static void nbd_accept(void *opaque)
int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
nbd_started = true;
if (fd != -1 && nbd_client_new(exp, fd, nbd_client_closed)) {
if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
nb_fds++;
}
}
@ -410,9 +410,9 @@ int main(int argc, char **argv)
if (disconnect) {
fd = open(argv[optind], O_RDWR);
if (fd == -1)
if (fd < 0) {
err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
}
nbd_disconnect(fd);
close(fd);
@ -427,7 +427,7 @@ int main(int argc, char **argv)
pid_t pid;
int ret;
if (qemu_pipe(stderr_fd) == -1) {
if (qemu_pipe(stderr_fd) < 0) {
err(EXIT_FAILURE, "Error setting up communication pipe");
}
@ -441,7 +441,7 @@ int main(int argc, char **argv)
/* Temporarily redirect stderr to the parent's pipe... */
dup2(stderr_fd[1], STDERR_FILENO);
if (ret == -1) {
if (ret < 0) {
err(EXIT_FAILURE, "Failed to daemonize");
}
@ -459,11 +459,11 @@ int main(int argc, char **argv)
while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
errors = true;
ret = qemu_write_full(STDERR_FILENO, buf, ret);
if (ret == -1) {
if (ret < 0) {
exit(EXIT_FAILURE);
}
}
if (ret == -1) {
if (ret < 0) {
err(EXIT_FAILURE, "Cannot read from daemon");
}
@ -504,7 +504,7 @@ int main(int argc, char **argv)
fd = tcp_socket_incoming(bindto, port);
}
if (fd == -1) {
if (fd < 0) {
return 1;
}