lib/nbd: merge read_from_socket/write_to_socket into nbd_sock_rw
Purpose: 1 Remove duplicated code. 2 Add one error return check. Change-Id: I8d2517e120818482b9bdf56e02cd901363b79aee Signed-off-by: Ziye Yang <ziye.yang@intel.com> Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/5732 Tested-by: SPDK CI Jenkins <sys_sgci@intel.com> Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com> Reviewed-by: <dongx.yi@intel.com> Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
This commit is contained in:
parent
d2704a0f48
commit
a1d4a714b2
@ -415,38 +415,25 @@ spdk_nbd_stop(struct spdk_nbd_disk *nbd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int64_t
|
static int64_t
|
||||||
read_from_socket(int fd, void *buf, size_t length)
|
nbd_socket_rw(int fd, void *buf, size_t length, bool read_op)
|
||||||
{
|
{
|
||||||
ssize_t bytes_read;
|
ssize_t rc;
|
||||||
|
|
||||||
bytes_read = read(fd, buf, length);
|
if (read_op) {
|
||||||
if (bytes_read == 0) {
|
rc = read(fd, buf, length);
|
||||||
return -EIO;
|
|
||||||
} else if (bytes_read == -1) {
|
|
||||||
if (errno != EAGAIN) {
|
|
||||||
return -errno;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
} else {
|
} else {
|
||||||
return bytes_read;
|
rc = write(fd, buf, length);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t
|
if (rc == 0) {
|
||||||
write_to_socket(int fd, void *buf, size_t length)
|
|
||||||
{
|
|
||||||
ssize_t bytes_written;
|
|
||||||
|
|
||||||
bytes_written = write(fd, buf, length);
|
|
||||||
if (bytes_written == 0) {
|
|
||||||
return -EIO;
|
return -EIO;
|
||||||
} else if (bytes_written == -1) {
|
} else if (rc == -1) {
|
||||||
if (errno != EAGAIN) {
|
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return bytes_written;
|
return rc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -608,8 +595,8 @@ nbd_io_recv_internal(struct spdk_nbd_disk *nbd)
|
|||||||
io = nbd->io_in_recv;
|
io = nbd->io_in_recv;
|
||||||
|
|
||||||
if (io->state == NBD_IO_RECV_REQ) {
|
if (io->state == NBD_IO_RECV_REQ) {
|
||||||
ret = read_from_socket(nbd->spdk_sp_fd, (char *)&io->req + io->offset,
|
ret = nbd_socket_rw(nbd->spdk_sp_fd, (char *)&io->req + io->offset,
|
||||||
sizeof(io->req) - io->offset);
|
sizeof(io->req) - io->offset, true);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
nbd_put_io(nbd, io);
|
nbd_put_io(nbd, io);
|
||||||
nbd->io_in_recv = NULL;
|
nbd->io_in_recv = NULL;
|
||||||
@ -665,7 +652,7 @@ nbd_io_recv_internal(struct spdk_nbd_disk *nbd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (io->state == NBD_IO_RECV_PAYLOAD) {
|
if (io->state == NBD_IO_RECV_PAYLOAD) {
|
||||||
ret = read_from_socket(nbd->spdk_sp_fd, io->payload + io->offset, io->payload_size - io->offset);
|
ret = nbd_socket_rw(nbd->spdk_sp_fd, io->payload + io->offset, io->payload_size - io->offset, true);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
nbd_put_io(nbd, io);
|
nbd_put_io(nbd, io);
|
||||||
nbd->io_in_recv = NULL;
|
nbd->io_in_recv = NULL;
|
||||||
@ -733,8 +720,8 @@ nbd_io_xmit_internal(struct spdk_nbd_disk *nbd)
|
|||||||
/* resp error and handler are already set in io_done */
|
/* resp error and handler are already set in io_done */
|
||||||
|
|
||||||
if (io->state == NBD_IO_XMIT_RESP) {
|
if (io->state == NBD_IO_XMIT_RESP) {
|
||||||
ret = write_to_socket(nbd->spdk_sp_fd, (char *)&io->resp + io->offset,
|
ret = nbd_socket_rw(nbd->spdk_sp_fd, (char *)&io->resp + io->offset,
|
||||||
sizeof(io->resp) - io->offset);
|
sizeof(io->resp) - io->offset, false);
|
||||||
if (ret <= 0) {
|
if (ret <= 0) {
|
||||||
goto reinsert;
|
goto reinsert;
|
||||||
}
|
}
|
||||||
@ -757,7 +744,8 @@ nbd_io_xmit_internal(struct spdk_nbd_disk *nbd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (io->state == NBD_IO_XMIT_PAYLOAD) {
|
if (io->state == NBD_IO_XMIT_PAYLOAD) {
|
||||||
ret = write_to_socket(nbd->spdk_sp_fd, io->payload + io->offset, io->payload_size - io->offset);
|
ret = nbd_socket_rw(nbd->spdk_sp_fd, io->payload + io->offset, io->payload_size - io->offset,
|
||||||
|
false);
|
||||||
if (ret <= 0) {
|
if (ret <= 0) {
|
||||||
goto reinsert;
|
goto reinsert;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user