[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libnbd PATCH 02/13] block_status: Refactor array storage
From: |
Eric Blake |
Subject: |
[libnbd PATCH 02/13] block_status: Refactor array storage |
Date: |
Fri, 3 Dec 2021 17:17:30 -0600 |
For 32-bit block status, we were able to cheat and use an array with
an odd number of elements, with array[0] holding the context id, and
passing &array[1] to the user's callback. But once we have 64-bit
extents, we can no longer abuse array element 0 like that. Split out
a new state to receive the context id separately from the extents
array. No behavioral change, other than the rare possibility of
landing in the new state.
---
lib/internal.h | 1 +
generator/state_machine.ml | 11 +++++-
generator/states-reply-structured.c | 58 ++++++++++++++++++++---------
3 files changed, 51 insertions(+), 19 deletions(-)
diff --git a/lib/internal.h b/lib/internal.h
index 0e205aba..7e96e8e9 100644
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -274,6 +274,7 @@ struct nbd_handle {
size_t querynum;
/* When receiving block status, this is used. */
+ uint32_t bs_contextid;
uint32_t *bs_entries;
/* Commands which are waiting to be issued [meaning the request
diff --git a/generator/state_machine.ml b/generator/state_machine.ml
index 3bc77f24..99652948 100644
--- a/generator/state_machine.ml
+++ b/generator/state_machine.ml
@@ -1,6 +1,6 @@
(* hey emacs, this is OCaml code: -*- tuareg -*- *)
(* nbd client library in userspace: state machine definition
- * Copyright (C) 2013-2020 Red Hat Inc.
+ * Copyright (C) 2013-2021 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -862,10 +862,17 @@ and
external_events = [];
};
+ State {
+ default_state with
+ name = "RECV_BS_CONTEXTID";
+ comment = "Receive contextid of structured reply block-status payload";
+ external_events = [];
+ };
+
State {
default_state with
name = "RECV_BS_ENTRIES";
- comment = "Receive a structured reply block-status payload";
+ comment = "Receive entries array of structured reply block-status payload";
external_events = [];
};
diff --git a/generator/states-reply-structured.c
b/generator/states-reply-structured.c
index 70010474..e1da850d 100644
--- a/generator/states-reply-structured.c
+++ b/generator/states-reply-structured.c
@@ -1,5 +1,5 @@
/* nbd client library in userspace: state machine
- * Copyright (C) 2013-2019 Red Hat Inc.
+ * Copyright (C) 2013-2021 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -185,19 +185,10 @@ STATE_MACHINE {
set_error (0, "not expecting NBD_REPLY_TYPE_BLOCK_STATUS here");
return 0;
}
- /* We read the context ID followed by all the entries into a
- * single array and deal with it at the end.
- */
- free (h->bs_entries);
- h->bs_entries = malloc (length);
- if (h->bs_entries == NULL) {
- SET_NEXT_STATE (%.DEAD);
- set_error (errno, "malloc");
- return 0;
- }
- h->rbuf = h->bs_entries;
- h->rlen = length;
- SET_NEXT_STATE (%RECV_BS_ENTRIES);
+ /* Start by reading the context ID. */
+ h->rbuf = &h->bs_contextid;
+ h->rlen = sizeof h->bs_contextid;
+ SET_NEXT_STATE (%RECV_BS_CONTEXTID);
return 0;
}
else {
@@ -452,9 +443,41 @@ STATE_MACHINE {
}
return 0;
+ REPLY.STRUCTURED_REPLY.RECV_BS_CONTEXTID:
+ struct command *cmd = h->reply_cmd;
+ uint32_t length;
+
+ switch (recv_into_rbuf (h)) {
+ case -1: SET_NEXT_STATE (%.DEAD); return 0;
+ case 1:
+ save_reply_state (h);
+ SET_NEXT_STATE (%.READY);
+ return 0;
+ case 0:
+ length = be32toh (h->sbuf.sr.structured_reply.length);
+
+ assert (cmd); /* guaranteed by CHECK */
+ assert (cmd->type == NBD_CMD_BLOCK_STATUS);
+ assert (length >= 12);
+ length -= sizeof h->bs_contextid;
+
+ free (h->bs_entries);
+ h->bs_entries = malloc (length);
+ if (h->bs_entries == NULL) {
+ SET_NEXT_STATE (%.DEAD);
+ set_error (errno, "malloc");
+ return 0;
+ }
+ h->rbuf = h->bs_entries;
+ h->rlen = length;
+ SET_NEXT_STATE (%RECV_BS_ENTRIES);
+ }
+ return 0;
+
REPLY.STRUCTURED_REPLY.RECV_BS_ENTRIES:
struct command *cmd = h->reply_cmd;
uint32_t length;
+ uint32_t count;
size_t i;
uint32_t context_id;
struct meta_context *meta_context;
@@ -473,15 +496,16 @@ STATE_MACHINE {
assert (CALLBACK_IS_NOT_NULL (cmd->cb.fn.extent));
assert (h->bs_entries);
assert (length >= 12);
+ count = (length - sizeof h->bs_contextid) / sizeof *h->bs_entries;
/* Need to byte-swap the entries returned, but apart from that we
* don't validate them.
*/
- for (i = 0; i < length/4; ++i)
+ for (i = 0; i < count; ++i)
h->bs_entries[i] = be32toh (h->bs_entries[i]);
/* Look up the context ID. */
- context_id = h->bs_entries[0];
+ context_id = be32toh (h->bs_contextid);
for (meta_context = h->meta_contexts;
meta_context;
meta_context = meta_context->next)
@@ -494,7 +518,7 @@ STATE_MACHINE {
if (CALL_CALLBACK (cmd->cb.fn.extent,
meta_context->name, cmd->offset,
- &h->bs_entries[1], (length-4) / 4,
+ h->bs_entries, count,
&error) == -1)
if (cmd->error == 0)
cmd->error = error ? error : EPROTO;
--
2.33.1
- [PATCH 08/14] nbd/server: Initial support for extended headers, (continued)
- [PATCH 08/14] nbd/server: Initial support for extended headers, Eric Blake, 2021/12/03
- [PATCH 07/14] nbd: Add types for extended headers, Eric Blake, 2021/12/03
- [PATCH 09/14] nbd/server: Support 64-bit block status, Eric Blake, 2021/12/03
- [PATCH 11/14] nbd/client: Accept 64-bit hole chunks, Eric Blake, 2021/12/03
- [PATCH 10/14] nbd/client: Initial support for extended headers, Eric Blake, 2021/12/03
- [PATCH 12/14] nbd/client: Accept 64-bit block status chunks, Eric Blake, 2021/12/03
- [PATCH 13/14] nbd/client: Request extended headers during negotiation, Eric Blake, 2021/12/03
- [PATCH 14/14] do not apply: nbd/server: Send 64-bit hole chunk, Eric Blake, 2021/12/03
- [libnbd PATCH 00/13] libnbd patches for NBD_OPT_EXTENDED_HEADERS, Eric Blake, 2021/12/03
- [libnbd PATCH 01/13] golang: Simplify nbd_block_status callback array copy, Eric Blake, 2021/12/03
- [libnbd PATCH 02/13] block_status: Refactor array storage,
Eric Blake <=
- [libnbd PATCH 03/13] protocol: Add definitions for extended headers, Eric Blake, 2021/12/03
- [libnbd PATCH 05/13] protocol: Prepare to receive 64-bit replies, Eric Blake, 2021/12/03
- [libnbd PATCH 04/13] protocol: Prepare to send 64-bit requests, Eric Blake, 2021/12/03
- [libnbd PATCH 06/13] protocol: Accept 64-bit holes during pread, Eric Blake, 2021/12/03
- [libnbd PATCH 08/13] block_status: Track 64-bit extents internally, Eric Blake, 2021/12/03
- [libnbd PATCH 10/13] api: Add [aio_]nbd_block_status_64, Eric Blake, 2021/12/03
- [libnbd PATCH 07/13] generator: Add struct nbd_extent in prep for 64-bit extents, Eric Blake, 2021/12/03
- [libnbd PATCH 09/13] block_status: Accept 64-bit extents during block status, Eric Blake, 2021/12/03
- [libnbd PATCH 11/13] api: Add three functions for controlling extended headers, Eric Blake, 2021/12/03
- [libnbd PATCH 12/13] generator: Actually request extended headers, Eric Blake, 2021/12/03