Also add an inline accessor function (get_handle). This also removes the unused function connection_get_handle. --- server/internal.h | 24 ++++++++++++++++----- server/backend.c | 50 ++++++++++++++++++++++---------------------- server/connections.c | 11 +--------- 3 files changed, 45 insertions(+), 40 deletions(-) diff --git a/server/internal.h b/server/internal.h index c3622671..9d314bf8 100644 --- a/server/internal.h +++ b/server/internal.h @@ -170,16 +170,24 @@ typedef int (*connection_send_function) (const void *buf, size_t len, __attribute__((__nonnull__ (1))); typedef void (*connection_close_function) (void); +/* struct handle stores data per connection and backend. Primarily + * this is the filter or plugin handle, but other state is also stored + * here. + * + * Use get_handle (conn, 0) to return the struct handle for the + * plugin, and get_handle (conn, b->i) to return the struct handle 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 */ HANDLE_CONNECTED = 2, /* Set if .prepare passed, so .finalize is needed */ HANDLE_FAILED = 4, /* Set if .finalize failed */ }; -struct b_conn_handle { - void *handle; +struct handle { + void *handle; /* Plugin or filter handle. */ - unsigned char state; /* Bitmask of HANDLE_* values */ + unsigned char state; /* Bitmask of HANDLE_* values */ uint64_t exportsize; int can_write; @@ -195,7 +203,7 @@ struct b_conn_handle { }; static inline void -reset_b_conn_handle (struct b_conn_handle *h) +reset_handle (struct handle *h) { h->handle = NULL; h->state = 0; @@ -222,7 +230,7 @@ struct connection { void *crypto_session; int nworkers; - struct b_conn_handle *handles; + struct handle *handles; /* One per plugin and filter. */ size_t nr_handles; char exportname[NBD_MAX_STRING + 1]; @@ -239,6 +247,12 @@ struct connection { connection_close_function close; }; +static inline struct handle * +get_handle (struct connection *conn, int i) +{ + return &conn->handles[i]; +} + 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 616c24d8..9669ada1 100644 --- a/server/backend.c +++ b/server/backend.c @@ -154,7 +154,7 @@ int backend_open (struct backend *b, int readonly) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); controlpath_debug ("%s: open readonly=%d", b->name, readonly); @@ -178,7 +178,7 @@ backend_open (struct backend *b, int readonly) h->state |= HANDLE_OPEN; if (b->i) /* A filter must not succeed unless its backend did also */ - assert (conn->handles[b->i - 1].handle); + assert (get_handle (conn, b->i-1)->handle != NULL); return 0; } @@ -186,7 +186,7 @@ int backend_prepare (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); assert (h->handle); assert ((h->state & (HANDLE_OPEN | HANDLE_CONNECTED)) == HANDLE_OPEN); @@ -209,7 +209,7 @@ int backend_finalize (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); /* Call these in reverse order to .prepare above, starting from the * filter furthest away from the plugin, and matching .close order. @@ -238,7 +238,7 @@ void backend_close (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); /* outer-to-inner order, opposite .open */ controlpath_debug ("%s: close", b->name); @@ -249,7 +249,7 @@ backend_close (struct backend *b) } else assert (! (h->state & HANDLE_OPEN)); - reset_b_conn_handle (h); + reset_handle (h); if (b->i) backend_close (b->next); } @@ -258,7 +258,7 @@ bool backend_valid_range (struct backend *b, uint64_t offset, uint32_t count) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); assert (h->exportsize <= INT64_MAX); /* Guaranteed by negotiation phase */ return count > 0 && offset <= h->exportsize && @@ -291,7 +291,7 @@ int64_t backend_get_size (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); controlpath_debug ("%s: get_size", b->name); @@ -305,7 +305,7 @@ int backend_can_write (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); controlpath_debug ("%s: can_write", b->name); @@ -319,7 +319,7 @@ int backend_can_flush (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); controlpath_debug ("%s: can_flush", b->name); @@ -333,7 +333,7 @@ int backend_is_rotational (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); controlpath_debug ("%s: is_rotational", b->name); @@ -347,7 +347,7 @@ int backend_can_trim (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); int r; controlpath_debug ("%s: can_trim", b->name); @@ -368,7 +368,7 @@ int backend_can_zero (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); int r; controlpath_debug ("%s: can_zero", b->name); @@ -389,7 +389,7 @@ int backend_can_fast_zero (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); int r; controlpath_debug ("%s: can_fast_zero", b->name); @@ -410,7 +410,7 @@ int backend_can_extents (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); controlpath_debug ("%s: can_extents", b->name); @@ -424,7 +424,7 @@ int backend_can_fua (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); int r; controlpath_debug ("%s: can_fua", b->name); @@ -445,7 +445,7 @@ int backend_can_multi_conn (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); assert (h->handle && (h->state & HANDLE_CONNECTED)); controlpath_debug ("%s: can_multi_conn", b->name); @@ -459,7 +459,7 @@ int backend_can_cache (struct backend *b) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); controlpath_debug ("%s: can_cache", b->name); @@ -475,7 +475,7 @@ backend_pread (struct backend *b, uint32_t flags, int *err) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); int r; assert (h->handle && (h->state & HANDLE_CONNECTED)); @@ -496,7 +496,7 @@ backend_pwrite (struct backend *b, uint32_t flags, int *err) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); bool fua = !!(flags & NBDKIT_FLAG_FUA); int r; @@ -520,7 +520,7 @@ backend_flush (struct backend *b, uint32_t flags, int *err) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); int r; assert (h->handle && (h->state & HANDLE_CONNECTED)); @@ -540,7 +540,7 @@ backend_trim (struct backend *b, int *err) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); bool fua = !!(flags & NBDKIT_FLAG_FUA); int r; @@ -566,7 +566,7 @@ backend_zero (struct backend *b, int *err) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); bool fua = !!(flags & NBDKIT_FLAG_FUA); bool fast = !!(flags & NBDKIT_FLAG_FAST_ZERO); int r; @@ -601,7 +601,7 @@ backend_extents (struct backend *b, struct nbdkit_extents *extents, int *err) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); int r; assert (h->handle && (h->state & HANDLE_CONNECTED)); @@ -632,7 +632,7 @@ backend_cache (struct backend *b, uint32_t flags, int *err) { GET_CONN; - struct b_conn_handle *h = &conn->handles[b->i]; + struct handle *h = get_handle (conn, b->i); int r; assert (h->handle && (h->state & HANDLE_CONNECTED)); diff --git a/server/connections.c b/server/connections.c index 7e9584b3..d4cdf994 100644 --- a/server/connections.c +++ b/server/connections.c @@ -58,15 +58,6 @@ static int raw_send_socket (const void *buf, size_t len, int flags); static int raw_send_other (const void *buf, size_t len, int flags); static void raw_close (void); -void * -connection_get_handle (size_t i) -{ - GET_CONN; - - assert (i < conn->nr_handles); - return conn->handles[i].handle; -} - int connection_get_status (void) { @@ -258,7 +249,7 @@ new_connection (int sockin, int sockout, int nworkers) } conn->nr_handles = top->i + 1; for_each_backend (b) - reset_b_conn_handle (&conn->handles[b->i]); + reset_handle (get_handle (conn, b->i)); conn->status = 1; conn->nworkers = nworkers; -- 2.25.0