Alter struct types to reflect reality, avoiding mixing "host" and "raw" structs

This commit is contained in:
Patrick J Cherry
2018-02-08 15:46:34 +00:00
parent 1672b4b88b
commit f8fec5f57e
2 changed files with 24 additions and 18 deletions

1
debian/changelog vendored
View File

@@ -14,6 +14,7 @@ flexnbd (0.2.0) UNRELEASED; urgency=medium
filesystem, and EINVAL to unknown commands. (#36, !40) filesystem, and EINVAL to unknown commands. (#36, !40)
* Proxy passes all NBD protocol errors through to the client instead of * Proxy passes all NBD protocol errors through to the client instead of
disconnecting and retrying (#36, !40) disconnecting and retrying (#36, !40)
* Fix struct types in readwrite.c (#35, !41)
-- James Carter <james.carter@bytemark.co.uk> Thu, 11 Jan 2018 10:05:35 +0000 -- James Carter <james.carter@bytemark.co.uk> Thu, 11 Jan 2018 10:05:35 +0000

View File

@@ -106,14 +106,14 @@ int socket_nbd_write_hello( int fd, off64_t out_size, uint32_t out_flags )
return 1; return 1;
} }
void fill_request(struct nbd_request *request, uint16_t type, uint16_t flags, uint64_t from, uint32_t len) void fill_request(struct nbd_request_raw *request_raw, uint16_t type, uint16_t flags, uint64_t from, uint32_t len)
{ {
request->magic = htobe32(REQUEST_MAGIC); request_raw->magic = htobe32(REQUEST_MAGIC);
request->type = htobe16(type); request_raw->type = htobe16(type);
request->flags = htobe16(flags); request_raw->flags = htobe16(flags);
request->handle.w = (((uint64_t)rand()) << 32) | ((uint64_t)rand()); request_raw->handle.w = (((uint64_t)rand()) << 32) | ((uint64_t)rand());
request->from = htobe64(from); request_raw->from = htobe64(from);
request->len = htobe32(len); request_raw->len = htobe32(len);
} }
void read_reply(int fd, struct nbd_request *request, struct nbd_reply *reply) void read_reply(int fd, struct nbd_request *request, struct nbd_reply *reply)
@@ -156,14 +156,17 @@ void wait_for_data( int fd, int timeout_secs )
void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd, void* out_buf, int timeout_secs) void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd, void* out_buf, int timeout_secs)
{ {
struct nbd_request request; struct nbd_request_raw request_raw;
struct nbd_reply reply; struct nbd_request request;
struct nbd_reply reply;
fill_request(&request, REQUEST_READ, 0, from, len); fill_request(&request_raw, REQUEST_READ, 0, from, len);
FATAL_IF_NEGATIVE(writeloop(fd, &request, sizeof(request)), FATAL_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request)),
"Couldn't write request"); "Couldn't write request");
wait_for_data( fd, timeout_secs ); wait_for_data( fd, timeout_secs );
nbd_r2h_request( &request_raw, &request );
read_reply(fd, &request, &reply); read_reply(fd, &request, &reply);
if (out_buf) { if (out_buf) {
@@ -180,11 +183,12 @@ void socket_nbd_read(int fd, uint64_t from, uint32_t len, int out_fd, void* out_
void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd, void* in_buf, int timeout_secs) void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd, void* in_buf, int timeout_secs)
{ {
struct nbd_request request; struct nbd_request_raw request_raw;
struct nbd_reply reply; struct nbd_request request;
struct nbd_reply reply;
fill_request(&request, REQUEST_WRITE, 0, from, len); fill_request(&request_raw, REQUEST_WRITE, 0, from, len);
ERROR_IF_NEGATIVE(writeloop(fd, &request, sizeof(request)), ERROR_IF_NEGATIVE(writeloop(fd, &request_raw, sizeof(request_raw)),
"Couldn't write request"); "Couldn't write request");
if (in_buf) { if (in_buf) {
@@ -199,6 +203,7 @@ void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd, void* in_b
} }
wait_for_data( fd, timeout_secs ); wait_for_data( fd, timeout_secs );
nbd_r2h_request( &request_raw, &request );
read_reply(fd, &request, &reply); read_reply(fd, &request, &reply);
} }
@@ -206,13 +211,13 @@ void socket_nbd_write(int fd, uint64_t from, uint32_t len, int in_fd, void* in_b
int socket_nbd_disconnect( int fd ) int socket_nbd_disconnect( int fd )
{ {
int success = 1; int success = 1;
struct nbd_request request; struct nbd_request_raw request_raw;
fill_request( &request, REQUEST_DISCONNECT, 0, 0, 0 ); fill_request( &request_raw, REQUEST_DISCONNECT, 0, 0, 0 );
/* FIXME: This shouldn't be a FATAL error. We should just drop /* FIXME: This shouldn't be a FATAL error. We should just drop
* the mirror without affecting the main server. * the mirror without affecting the main server.
*/ */
FATAL_IF_NEGATIVE( writeloop( fd, &request, sizeof( request ) ), FATAL_IF_NEGATIVE( writeloop( fd, &request_raw, sizeof( request_raw ) ),
"Failed to write the disconnect request." ); "Failed to write the disconnect request." );
return success; return success;
} }