Minor refactoring, since all callers already have a struct backend
available when manipulating a context associated with a connection.
---
server/internal.h | 21 +++-------
server/backend.c | 62 ++++++++++++++--------------
server/connections.c | 17 +++++++-
server/filters.c | 2 +-
server/protocol-handshake-newstyle.c | 2 +-
server/protocol-handshake.c | 2 +-
6 files changed, 55 insertions(+), 51 deletions(-)
diff --git a/server/internal.h b/server/internal.h
index ff543200..28c65629 100644
--- a/server/internal.h
+++ b/server/internal.h
@@ -195,10 +195,6 @@ typedef void (*connection_close_function) (void);
/* struct context stores data per connection and backend. Primarily
* this is the filter or plugin handle, but other state is also stored
* here.
- *
- * Use get_context (conn, 0) to return the struct context for the
- * plugin, and get_context (conn, b->i) to return the struct context for
- * the i'th backend (if b->i >= 1 then for a filter).
*/
enum {
HANDLE_OPEN = 1, /* Set if .open passed, so .close is needed */
@@ -257,18 +253,11 @@ struct connection {
connection_close_function close;
};
-static inline struct context *
-get_context (struct connection *conn, int i)
-{
- return conn->contexts[i];
-}
-
-static inline void
-set_context (struct connection *conn, int i, struct context *context)
-{
- conn->contexts[i] = context;
-}
-
+extern struct context *get_context (struct connection *conn, struct backend *b)
+ __attribute__((__nonnull__(1)));
+extern void set_context (struct connection *conn, struct backend *b,
+ struct context *c)
+ __attribute__((__nonnull__(1)));
extern void handle_single_connection (int sockin, int sockout);
extern int connection_get_status (void);
extern int connection_set_status (int value);
diff --git a/server/backend.c b/server/backend.c
index e89f0ab4..c382c71d 100644
--- a/server/backend.c
+++ b/server/backend.c
@@ -163,7 +163,7 @@ backend_list_exports (struct backend *b, int readonly,
struct nbdkit_exports *exports)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
size_t count;
controlpath_debug ("%s: list_exports readonly=%d tls=%d",
@@ -186,7 +186,7 @@ const char *
backend_default_export (struct backend *b, int readonly)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
const char *s;
controlpath_debug ("%s: default_export readonly=%d tls=%d",
@@ -272,7 +272,7 @@ int
backend_prepare (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
assert (c->handle);
assert ((c->state & (HANDLE_OPEN | HANDLE_CONNECTED)) == HANDLE_OPEN);
@@ -281,7 +281,7 @@ backend_prepare (struct backend *b)
* plugin, similar to typical .open order. But remember that
* a filter may skip opening its backend.
*/
- if (b->i && get_context (conn, b->i-1) != NULL &&
+ if (b->i && get_context (conn, b->next) != NULL &&
backend_prepare (b->next) == -1)
return -1;
@@ -297,7 +297,7 @@ int
backend_finalize (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
/* Call these in reverse order to .prepare above, starting from the
* filter furthest away from the plugin, and matching .close order.
@@ -316,7 +316,7 @@ backend_finalize (struct backend *b)
}
}
- if (b->i && get_context (conn, b->i-1))
+ if (b->i && get_context (conn, b->next))
return backend_finalize (b->next);
return 0;
}
@@ -325,7 +325,7 @@ void
backend_close (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
/* outer-to-inner order, opposite .open */
assert (c->handle);
@@ -333,8 +333,8 @@ backend_close (struct backend *b)
controlpath_debug ("%s: close", b->name);
b->close (b, c->handle);
free (c);
- set_context (conn, b->i, NULL);
- if (b->i && get_context (conn, b->i-1))
+ set_context (conn, b, NULL);
+ if (b->i && get_context (conn, b->next))
backend_close (b->next);
}
@@ -342,7 +342,7 @@ bool
backend_valid_range (struct backend *b, uint64_t offset, uint32_t count)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
assert (c->exportsize <= INT64_MAX); /* Guaranteed by negotiation phase */
return count > 0 && offset <= c->exportsize &&
@@ -360,7 +360,7 @@ backend_reopen (struct backend *b, int readonly, const char
*exportname)
controlpath_debug ("%s: reopen readonly=%d exportname=\"%s\"",
b->name, readonly, exportname);
- if (get_context (conn, b->i)) {
+ if (get_context (conn, b)) {
if (backend_finalize (b) == -1)
return -1;
backend_close (b);
@@ -368,7 +368,7 @@ backend_reopen (struct backend *b, int readonly, const char
*exportname)
h = backend_open (b, readonly, exportname);
if (h == NULL)
return -1;
- set_context (conn, b->i, h);
+ set_context (conn, b, h);
if (backend_prepare (b) == -1) {
backend_finalize (b);
backend_close (b);
@@ -383,7 +383,7 @@ const char *
backend_export_description (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
const char *s;
controlpath_debug ("%s: export_description", b->name);
@@ -405,7 +405,7 @@ int64_t
backend_get_size (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
assert (c->handle && (c->state & HANDLE_CONNECTED));
if (c->exportsize == -1) {
@@ -419,7 +419,7 @@ int
backend_can_write (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
assert (c->handle && (c->state & HANDLE_CONNECTED));
if (c->can_write == -1) {
@@ -433,7 +433,7 @@ int
backend_can_flush (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
assert (c->handle && (c->state & HANDLE_CONNECTED));
if (c->can_flush == -1) {
@@ -447,7 +447,7 @@ int
backend_is_rotational (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
assert (c->handle && (c->state & HANDLE_CONNECTED));
if (c->is_rotational == -1) {
@@ -461,7 +461,7 @@ int
backend_can_trim (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
int r;
assert (c->handle && (c->state & HANDLE_CONNECTED));
@@ -481,7 +481,7 @@ int
backend_can_zero (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
int r;
assert (c->handle && (c->state & HANDLE_CONNECTED));
@@ -501,7 +501,7 @@ int
backend_can_fast_zero (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
int r;
assert (c->handle && (c->state & HANDLE_CONNECTED));
@@ -521,7 +521,7 @@ int
backend_can_extents (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
assert (c->handle && (c->state & HANDLE_CONNECTED));
if (c->can_extents == -1) {
@@ -535,7 +535,7 @@ int
backend_can_fua (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
int r;
assert (c->handle && (c->state & HANDLE_CONNECTED));
@@ -555,7 +555,7 @@ int
backend_can_multi_conn (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
assert (c->handle && (c->state & HANDLE_CONNECTED));
if (c->can_multi_conn == -1) {
@@ -569,7 +569,7 @@ int
backend_can_cache (struct backend *b)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
assert (c->handle && (c->state & HANDLE_CONNECTED));
if (c->can_cache == -1) {
@@ -585,7 +585,7 @@ backend_pread (struct backend *b,
uint32_t flags, int *err)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
int r;
assert (c->handle && (c->state & HANDLE_CONNECTED));
@@ -606,7 +606,7 @@ backend_pwrite (struct backend *b,
uint32_t flags, int *err)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
bool fua = !!(flags & NBDKIT_FLAG_FUA);
int r;
@@ -630,7 +630,7 @@ backend_flush (struct backend *b,
uint32_t flags, int *err)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
int r;
assert (c->handle && (c->state & HANDLE_CONNECTED));
@@ -650,7 +650,7 @@ backend_trim (struct backend *b,
int *err)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
bool fua = !!(flags & NBDKIT_FLAG_FUA);
int r;
@@ -676,7 +676,7 @@ backend_zero (struct backend *b,
int *err)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
bool fua = !!(flags & NBDKIT_FLAG_FUA);
bool fast = !!(flags & NBDKIT_FLAG_FAST_ZERO);
int r;
@@ -711,7 +711,7 @@ backend_extents (struct backend *b,
struct nbdkit_extents *extents, int *err)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
int r;
assert (c->handle && (c->state & HANDLE_CONNECTED));
@@ -742,7 +742,7 @@ backend_cache (struct backend *b,
uint32_t flags, int *err)
{
GET_CONN;
- struct context *c = get_context (conn, b->i);
+ struct context *c = get_context (conn, b);
int r;
assert (c->handle && (c->state & HANDLE_CONNECTED));
diff --git a/server/connections.c b/server/connections.c
index e3fdc649..0115875b 100644
--- a/server/connections.c
+++ b/server/connections.c
@@ -360,7 +360,7 @@ free_connection (struct connection *conn)
*/
if (!quit) {
lock_request ();
- if (get_context (conn, top->i))
+ if (get_context (conn, top))
backend_close (top);
unlock_request ();
}
@@ -499,3 +499,18 @@ raw_close (void)
if (conn->sockout >= 0 && conn->sockin != conn->sockout)
closesocket (conn->sockout);
}
+
+struct context *
+get_context (struct connection *conn, struct backend *b)
+{
+ struct context *c = conn->contexts[b->i];
+ assert (!c || c->b == b);
+ return c;
+}
+
+void
+set_context (struct connection *conn, struct backend *b, struct context *c)
+{
+ assert (!c || c->b == b);
+ conn->contexts[b->i] = c;
+}
diff --git a/server/filters.c b/server/filters.c
index dde36306..fb123670 100644
--- a/server/filters.c
+++ b/server/filters.c
@@ -268,7 +268,7 @@ next_open (struct backend *b, int readonly, const char *exportname)
if (c == NULL)
return -1;
- set_context (conn, b->i, c);
+ set_context (conn, b, c);
return 0;
}
diff --git a/server/protocol-handshake-newstyle.c b/server/protocol-handshake-newstyle.c
index 015e7f9b..4bc802cc 100644
--- a/server/protocol-handshake-newstyle.c
+++ b/server/protocol-handshake-newstyle.c
@@ -567,7 +567,7 @@ negotiate_handshake_newstyle_options (void)
*/
if (finish_newstyle_options (&exportsize,
&data[4], exportnamelen) == -1) {
- if (get_context (conn, top->i)) {
+ if (get_context (conn, top)) {
if (backend_finalize (top) == -1)
return -1;
backend_close (top);
diff --git a/server/protocol-handshake.c b/server/protocol-handshake.c
index 82e7e647..69f8b530 100644
--- a/server/protocol-handshake.c
+++ b/server/protocol-handshake.c
@@ -84,7 +84,7 @@ protocol_common_open (uint64_t *exportsize, uint16_t *flags,
c = backend_open (top, read_only, exportname);
if (c == NULL)
return -1;
- set_context (conn, top->i, c);
+ set_context (conn, top, c);
/* Prepare (for filters), called just after open. */
if (backend_prepare (top) == -1)
--
2.30.1