Refactor all internal backend callbacks that take a 'struct backend *'/'void *handle' pair to instead take a 'struct context *'. This serves as a step towards making it easier for filters to create a standalone context independent of the current client connection. --- server/internal.h | 44 ++++++++-------- server/backend.c | 44 ++++++++-------- server/filters.c | 113 ++++++++++++++++++++++++---------------- server/plugins.c | 128 +++++++++++++++++++++++++++------------------- 4 files changed, 186 insertions(+), 143 deletions(-) diff --git a/server/internal.h b/server/internal.h index 28c65629..e8828506 100644 --- a/server/internal.h +++ b/server/internal.h @@ -366,38 +366,38 @@ struct backend { const char *(*default_export) (struct backend *, int readonly, int is_tls); void *(*open) (struct backend *, int readonly, const char *exportname, int is_tls); - int (*prepare) (struct backend *, void *handle, int readonly); - int (*finalize) (struct backend *, void *handle); - void (*close) (struct backend *, void *handle); + int (*prepare) (struct context *, int readonly); + int (*finalize) (struct context *); + void (*close) (struct context *); - const char *(*export_description) (struct backend *, void *handle); - int64_t (*get_size) (struct backend *, void *handle); - int (*can_write) (struct backend *, void *handle); - int (*can_flush) (struct backend *, void *handle); - int (*is_rotational) (struct backend *, void *handle); - int (*can_trim) (struct backend *, void *handle); - int (*can_zero) (struct backend *, void *handle); - int (*can_fast_zero) (struct backend *, void *handle); - int (*can_extents) (struct backend *, void *handle); - int (*can_fua) (struct backend *, void *handle); - int (*can_multi_conn) (struct backend *, void *handle); - int (*can_cache) (struct backend *, void *handle); + const char *(*export_description) (struct context *); + int64_t (*get_size) (struct context *); + int (*can_write) (struct context *); + int (*can_flush) (struct context *); + int (*is_rotational) (struct context *); + int (*can_trim) (struct context *); + int (*can_zero) (struct context *); + int (*can_fast_zero) (struct context *); + int (*can_extents) (struct context *); + int (*can_fua) (struct context *); + int (*can_multi_conn) (struct context *); + int (*can_cache) (struct context *); - int (*pread) (struct backend *, void *handle, + int (*pread) (struct context *, void *buf, uint32_t count, uint64_t offset, uint32_t flags, int *err); - int (*pwrite) (struct backend *, void *handle, + int (*pwrite) (struct context *, const void *buf, uint32_t count, uint64_t offset, uint32_t flags, int *err); - int (*flush) (struct backend *, void *handle, uint32_t flags, int *err); - int (*trim) (struct backend *, void *handle, + int (*flush) (struct context *, uint32_t flags, int *err); + int (*trim) (struct context *, uint32_t count, uint64_t offset, uint32_t flags, int *err); - int (*zero) (struct backend *, void *handle, + int (*zero) (struct context *, uint32_t count, uint64_t offset, uint32_t flags, int *err); - int (*extents) (struct backend *, void *handle, + int (*extents) (struct context *, uint32_t count, uint64_t offset, uint32_t flags, struct nbdkit_extents *extents, int *err); - int (*cache) (struct backend *, void *handle, + int (*cache) (struct context *, uint32_t count, uint64_t offset, uint32_t flags, int *err); }; diff --git a/server/backend.c b/server/backend.c index c382c71d..45a6da2f 100644 --- a/server/backend.c +++ b/server/backend.c @@ -287,7 +287,7 @@ backend_prepare (struct backend *b) controlpath_debug ("%s: prepare readonly=%d", b->name, c->can_write == 0); - if (b->prepare (b, c->handle, c->can_write == 0) == -1) + if (b->prepare (c, c->can_write == 0) == -1) return -1; c->state |= HANDLE_CONNECTED; return 0; @@ -310,7 +310,7 @@ backend_finalize (struct backend *b) if (c->state & HANDLE_CONNECTED) { assert (c->state & HANDLE_OPEN && c->handle); controlpath_debug ("%s: finalize", b->name); - if (b->finalize (b, c->handle) == -1) { + if (b->finalize (c) == -1) { c->state |= HANDLE_FAILED; return -1; } @@ -331,7 +331,7 @@ backend_close (struct backend *b) assert (c->handle); assert (c->state & HANDLE_OPEN); controlpath_debug ("%s: close", b->name); - b->close (b, c->handle); + b->close (c); free (c); set_context (conn, b, NULL); if (b->i && get_context (conn, b->next)) @@ -390,7 +390,7 @@ backend_export_description (struct backend *b) assert (c->handle && (c->state & HANDLE_CONNECTED)); /* Caching is not useful for this value. */ - s = b->export_description (b, c->handle); + s = b->export_description (c); /* Ignore over-length strings. XXX Also ignore non-UTF8? */ if (s && strnlen (s, NBD_MAX_STRING + 1) > NBD_MAX_STRING) { @@ -410,7 +410,7 @@ backend_get_size (struct backend *b) assert (c->handle && (c->state & HANDLE_CONNECTED)); if (c->exportsize == -1) { controlpath_debug ("%s: get_size", b->name); - c->exportsize = b->get_size (b, c->handle); + c->exportsize = b->get_size (c); } return c->exportsize; } @@ -424,7 +424,7 @@ backend_can_write (struct backend *b) assert (c->handle && (c->state & HANDLE_CONNECTED)); if (c->can_write == -1) { controlpath_debug ("%s: can_write", b->name); - c->can_write = b->can_write (b, c->handle); + c->can_write = b->can_write (c); } return c->can_write; } @@ -438,7 +438,7 @@ backend_can_flush (struct backend *b) assert (c->handle && (c->state & HANDLE_CONNECTED)); if (c->can_flush == -1) { controlpath_debug ("%s: can_flush", b->name); - c->can_flush = b->can_flush (b, c->handle); + c->can_flush = b->can_flush (c); } return c->can_flush; } @@ -452,7 +452,7 @@ backend_is_rotational (struct backend *b) assert (c->handle && (c->state & HANDLE_CONNECTED)); if (c->is_rotational == -1) { controlpath_debug ("%s: is_rotational", b->name); - c->is_rotational = b->is_rotational (b, c->handle); + c->is_rotational = b->is_rotational (c); } return c->is_rotational; } @@ -472,7 +472,7 @@ backend_can_trim (struct backend *b) c->can_trim = 0; return r; } - c->can_trim = b->can_trim (b, c->handle); + c->can_trim = b->can_trim (c); } return c->can_trim; } @@ -492,7 +492,7 @@ backend_can_zero (struct backend *b) c->can_zero = NBDKIT_ZERO_NONE; return r; /* Relies on 0 == NBDKIT_ZERO_NONE */ } - c->can_zero = b->can_zero (b, c->handle); + c->can_zero = b->can_zero (c); } return c->can_zero; } @@ -512,7 +512,7 @@ backend_can_fast_zero (struct backend *b) c->can_fast_zero = 0; return r; /* Relies on 0 == NBDKIT_ZERO_NONE */ } - c->can_fast_zero = b->can_fast_zero (b, c->handle); + c->can_fast_zero = b->can_fast_zero (c); } return c->can_fast_zero; } @@ -526,7 +526,7 @@ backend_can_extents (struct backend *b) assert (c->handle && (c->state & HANDLE_CONNECTED)); if (c->can_extents == -1) { controlpath_debug ("%s: can_extents", b->name); - c->can_extents = b->can_extents (b, c->handle); + c->can_extents = b->can_extents (c); } return c->can_extents; } @@ -546,7 +546,7 @@ backend_can_fua (struct backend *b) c->can_fua = NBDKIT_FUA_NONE; return r; /* Relies on 0 == NBDKIT_FUA_NONE */ } - c->can_fua = b->can_fua (b, c->handle); + c->can_fua = b->can_fua (c); } return c->can_fua; } @@ -560,7 +560,7 @@ backend_can_multi_conn (struct backend *b) assert (c->handle && (c->state & HANDLE_CONNECTED)); if (c->can_multi_conn == -1) { controlpath_debug ("%s: can_multi_conn", b->name); - c->can_multi_conn = b->can_multi_conn (b, c->handle); + c->can_multi_conn = b->can_multi_conn (c); } return c->can_multi_conn; } @@ -574,7 +574,7 @@ backend_can_cache (struct backend *b) assert (c->handle && (c->state & HANDLE_CONNECTED)); if (c->can_cache == -1) { controlpath_debug ("%s: can_cache", b->name); - c->can_cache = b->can_cache (b, c->handle); + c->can_cache = b->can_cache (c); } return c->can_cache; } @@ -594,7 +594,7 @@ backend_pread (struct backend *b, datapath_debug ("%s: pread count=%" PRIu32 " offset=%" PRIu64, b->name, count, offset); - r = b->pread (b, c->handle, buf, count, offset, flags, err); + r = b->pread (c, buf, count, offset, flags, err); if (r == -1) assert (*err); return r; @@ -619,7 +619,7 @@ backend_pwrite (struct backend *b, datapath_debug ("%s: pwrite count=%" PRIu32 " offset=%" PRIu64 " fua=%d", b->name, count, offset, fua); - r = b->pwrite (b, c->handle, buf, count, offset, flags, err); + r = b->pwrite (c, buf, count, offset, flags, err); if (r == -1) assert (*err); return r; @@ -638,7 +638,7 @@ backend_flush (struct backend *b, assert (flags == 0); datapath_debug ("%s: flush", b->name); - r = b->flush (b, c->handle, flags, err); + r = b->flush (c, flags, err); if (r == -1) assert (*err); return r; @@ -664,7 +664,7 @@ backend_trim (struct backend *b, datapath_debug ("%s: trim count=%" PRIu32 " offset=%" PRIu64 " fua=%d", b->name, count, offset, fua); - r = b->trim (b, c->handle, count, offset, flags, err); + r = b->trim (c, count, offset, flags, err); if (r == -1) assert (*err); return r; @@ -696,7 +696,7 @@ backend_zero (struct backend *b, b->name, count, offset, !!(flags & NBDKIT_FLAG_MAY_TRIM), fua, fast); - r = b->zero (b, c->handle, count, offset, flags, err); + r = b->zero (c, count, offset, flags, err); if (r == -1) { assert (*err); if (!fast) @@ -730,7 +730,7 @@ backend_extents (struct backend *b, *err = errno; return r; } - r = b->extents (b, c->handle, count, offset, flags, extents, err); + r = b->extents (c, count, offset, flags, extents, err); if (r == -1) assert (*err); return r; @@ -764,7 +764,7 @@ backend_cache (struct backend *b, } return 0; } - r = b->cache (b, c->handle, count, offset, flags, err); + r = b->cache (c, count, offset, flags, err); if (r == -1) assert (*err); return r; diff --git a/server/filters.c b/server/filters.c index fb123670..7986e56f 100644 --- a/server/filters.c +++ b/server/filters.c @@ -293,12 +293,14 @@ filter_open (struct backend *b, int readonly, const char *exportname, } static void -filter_close (struct backend *b, void *handle) +filter_close (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); - if (handle && f->filter.close) - f->filter.close (handle); + assert (c->handle); + if (f->filter.close) + f->filter.close (c->handle); } static struct nbdkit_next_ops next_ops = { @@ -324,236 +326,256 @@ static struct nbdkit_next_ops next_ops = { }; static int -filter_prepare (struct backend *b, void *handle, int readonly) +filter_prepare (struct context *c, int readonly) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.prepare && - f->filter.prepare (&next_ops, b->next, handle, readonly) == -1) + f->filter.prepare (&next_ops, b->next, c->handle, readonly) == -1) return -1; return 0; } static int -filter_finalize (struct backend *b, void *handle) +filter_finalize (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.finalize && - f->filter.finalize (&next_ops, b->next, handle) == -1) + f->filter.finalize (&next_ops, b->next, c->handle) == -1) return -1; return 0; } static const char * -filter_export_description (struct backend *b, void *handle) +filter_export_description (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.export_description) - return f->filter.export_description (&next_ops, b->next, handle); + return f->filter.export_description (&next_ops, b->next, c->handle); else return backend_export_description (b->next); } static int64_t -filter_get_size (struct backend *b, void *handle) +filter_get_size (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.get_size) - return f->filter.get_size (&next_ops, b->next, handle); + return f->filter.get_size (&next_ops, b->next, c->handle); else return backend_get_size (b->next); } static int -filter_can_write (struct backend *b, void *handle) +filter_can_write (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.can_write) - return f->filter.can_write (&next_ops, b->next, handle); + return f->filter.can_write (&next_ops, b->next, c->handle); else return backend_can_write (b->next); } static int -filter_can_flush (struct backend *b, void *handle) +filter_can_flush (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.can_flush) - return f->filter.can_flush (&next_ops, b->next, handle); + return f->filter.can_flush (&next_ops, b->next, c->handle); else return backend_can_flush (b->next); } static int -filter_is_rotational (struct backend *b, void *handle) +filter_is_rotational (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.is_rotational) - return f->filter.is_rotational (&next_ops, b->next, handle); + return f->filter.is_rotational (&next_ops, b->next, c->handle); else return backend_is_rotational (b->next); } static int -filter_can_trim (struct backend *b, void *handle) +filter_can_trim (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.can_trim) - return f->filter.can_trim (&next_ops, b->next, handle); + return f->filter.can_trim (&next_ops, b->next, c->handle); else return backend_can_trim (b->next); } static int -filter_can_zero (struct backend *b, void *handle) +filter_can_zero (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.can_zero) - return f->filter.can_zero (&next_ops, b->next, handle); + return f->filter.can_zero (&next_ops, b->next, c->handle); else return backend_can_zero (b->next); } static int -filter_can_fast_zero (struct backend *b, void *handle) +filter_can_fast_zero (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.can_fast_zero) - return f->filter.can_fast_zero (&next_ops, b->next, handle); + return f->filter.can_fast_zero (&next_ops, b->next, c->handle); else return backend_can_fast_zero (b->next); } static int -filter_can_extents (struct backend *b, void *handle) +filter_can_extents (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.can_extents) - return f->filter.can_extents (&next_ops, b->next, handle); + return f->filter.can_extents (&next_ops, b->next, c->handle); else return backend_can_extents (b->next); } static int -filter_can_fua (struct backend *b, void *handle) +filter_can_fua (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.can_fua) - return f->filter.can_fua (&next_ops, b->next, handle); + return f->filter.can_fua (&next_ops, b->next, c->handle); else return backend_can_fua (b->next); } static int -filter_can_multi_conn (struct backend *b, void *handle) +filter_can_multi_conn (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.can_multi_conn) - return f->filter.can_multi_conn (&next_ops, b->next, handle); + return f->filter.can_multi_conn (&next_ops, b->next, c->handle); else return backend_can_multi_conn (b->next); } static int -filter_can_cache (struct backend *b, void *handle) +filter_can_cache (struct context *c) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.can_cache) - return f->filter.can_cache (&next_ops, b->next, handle); + return f->filter.can_cache (&next_ops, b->next, c->handle); else return backend_can_cache (b->next); } static int -filter_pread (struct backend *b, void *handle, +filter_pread (struct context *c, void *buf, uint32_t count, uint64_t offset, uint32_t flags, int *err) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.pread) - return f->filter.pread (&next_ops, b->next, handle, + return f->filter.pread (&next_ops, b->next, c->handle, buf, count, offset, flags, err); else return backend_pread (b->next, buf, count, offset, flags, err); } static int -filter_pwrite (struct backend *b, void *handle, +filter_pwrite (struct context *c, const void *buf, uint32_t count, uint64_t offset, uint32_t flags, int *err) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.pwrite) - return f->filter.pwrite (&next_ops, b->next, handle, + return f->filter.pwrite (&next_ops, b->next, c->handle, buf, count, offset, flags, err); else return backend_pwrite (b->next, buf, count, offset, flags, err); } static int -filter_flush (struct backend *b, void *handle, +filter_flush (struct context *c, uint32_t flags, int *err) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.flush) - return f->filter.flush (&next_ops, b->next, handle, flags, err); + return f->filter.flush (&next_ops, b->next, c->handle, flags, err); else return backend_flush (b->next, flags, err); } static int -filter_trim (struct backend *b, void *handle, +filter_trim (struct context *c, uint32_t count, uint64_t offset, uint32_t flags, int *err) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.trim) - return f->filter.trim (&next_ops, b->next, handle, count, offset, + return f->filter.trim (&next_ops, b->next, c->handle, count, offset, flags, err); else return backend_trim (b->next, count, offset, flags, err); } static int -filter_zero (struct backend *b, void *handle, +filter_zero (struct context *c, uint32_t count, uint64_t offset, uint32_t flags, int *err) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.zero) - return f->filter.zero (&next_ops, b->next, handle, + return f->filter.zero (&next_ops, b->next, c->handle, count, offset, flags, err); else return backend_zero (b->next, count, offset, flags, err); } static int -filter_extents (struct backend *b, void *handle, +filter_extents (struct context *c, uint32_t count, uint64_t offset, uint32_t flags, struct nbdkit_extents *extents, int *err) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.extents) - return f->filter.extents (&next_ops, b->next, handle, + return f->filter.extents (&next_ops, b->next, c->handle, count, offset, flags, extents, err); else @@ -562,14 +584,15 @@ filter_extents (struct backend *b, void *handle, } static int -filter_cache (struct backend *b, void *handle, +filter_cache (struct context *c, uint32_t count, uint64_t offset, uint32_t flags, int *err) { + struct backend *b = c->b; struct backend_filter *f = container_of (b, struct backend_filter, backend); if (f->filter.cache) - return f->filter.cache (&next_ops, b->next, handle, + return f->filter.cache (&next_ops, b->next, c->handle, count, offset, flags, err); else return backend_cache (b->next, count, offset, flags, err); diff --git a/server/plugins.c b/server/plugins.c index 010595c7..17126ea1 100644 --- a/server/plugins.c +++ b/server/plugins.c @@ -1,5 +1,5 @@ /* nbdkit - * Copyright (C) 2013-2020 Red Hat Inc. + * Copyright (C) 2013-2021 Red Hat Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -347,48 +347,51 @@ plugin_open (struct backend *b, int readonly, const char *exportname, * .close. */ static int -plugin_prepare (struct backend *b, void *handle, - int readonly) +plugin_prepare (struct context *c, int readonly) { return 0; } static int -plugin_finalize (struct backend *b, void *handle) +plugin_finalize (struct context *c) { return 0; } static void -plugin_close (struct backend *b, void *handle) +plugin_close (struct context *c) { GET_CONN; + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); - if (handle && p->plugin.close) - p->plugin.close (handle); + assert (c->handle); + if (p->plugin.close) + p->plugin.close (c->handle); conn->exportname = NULL; } static const char * -plugin_export_description (struct backend *b, void *handle) +plugin_export_description (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); if (p->plugin.export_description) - return p->plugin.export_description (handle); + return p->plugin.export_description (c->handle); else return NULL; } static int64_t -plugin_get_size (struct backend *b, void *handle) +plugin_get_size (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); assert (p->plugin.get_size != NULL); - return p->plugin.get_size (handle); + return p->plugin.get_size (c->handle); } static int @@ -401,52 +404,57 @@ normalize_bool (int value) } static int -plugin_can_write (struct backend *b, void *handle) +plugin_can_write (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); if (p->plugin.can_write) - return normalize_bool (p->plugin.can_write (handle)); + return normalize_bool (p->plugin.can_write (c->handle)); else return p->plugin.pwrite || p->plugin._pwrite_v1; } static int -plugin_can_flush (struct backend *b, void *handle) +plugin_can_flush (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); if (p->plugin.can_flush) - return normalize_bool (p->plugin.can_flush (handle)); + return normalize_bool (p->plugin.can_flush (c->handle)); else return p->plugin.flush || p->plugin._flush_v1; } static int -plugin_is_rotational (struct backend *b, void *handle) +plugin_is_rotational (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); if (p->plugin.is_rotational) - return normalize_bool (p->plugin.is_rotational (handle)); + return normalize_bool (p->plugin.is_rotational (c->handle)); else return 0; /* assume false */ } static int -plugin_can_trim (struct backend *b, void *handle) +plugin_can_trim (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); if (p->plugin.can_trim) - return normalize_bool (p->plugin.can_trim (handle)); + return normalize_bool (p->plugin.can_trim (c->handle)); else return p->plugin.trim || p->plugin._trim_v1; } static int -plugin_can_zero (struct backend *b, void *handle) +plugin_can_zero (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); int r; @@ -455,7 +463,7 @@ plugin_can_zero (struct backend *b, void *handle) * expects .can_zero to return a tri-state on level of support. */ if (p->plugin.can_zero) { - r = p->plugin.can_zero (handle); + r = p->plugin.can_zero (c->handle); if (r == -1) return -1; return r ? NBDKIT_ZERO_NATIVE : NBDKIT_ZERO_EMULATE; @@ -466,13 +474,14 @@ plugin_can_zero (struct backend *b, void *handle) } static int -plugin_can_fast_zero (struct backend *b, void *handle) +plugin_can_fast_zero (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); int r; if (p->plugin.can_fast_zero) - return normalize_bool (p->plugin.can_fast_zero (handle)); + return normalize_bool (p->plugin.can_fast_zero (c->handle)); /* Advertise support for fast zeroes if no .zero or .can_zero is * false: in those cases, we fail fast instead of using .pwrite. * This also works when v1 plugin has only ._zero_v1. @@ -486,26 +495,28 @@ plugin_can_fast_zero (struct backend *b, void *handle) } static int -plugin_can_extents (struct backend *b, void *handle) +plugin_can_extents (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); if (p->plugin.can_extents) - return normalize_bool (p->plugin.can_extents (handle)); + return normalize_bool (p->plugin.can_extents (c->handle)); else return p->plugin.extents != NULL; } static int -plugin_can_fua (struct backend *b, void *handle) +plugin_can_fua (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); int r; /* The plugin must use API version 2 and have .can_fua return NBDKIT_FUA_NATIVE before we will pass the FUA flag on. */ if (p->plugin.can_fua) { - r = p->plugin.can_fua (handle); + r = p->plugin.can_fua (c->handle); if (r > NBDKIT_FUA_EMULATE && p->plugin._api_version == 1) r = NBDKIT_FUA_EMULATE; return r; @@ -517,23 +528,25 @@ plugin_can_fua (struct backend *b, void *handle) } static int -plugin_can_multi_conn (struct backend *b, void *handle) +plugin_can_multi_conn (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); if (p->plugin.can_multi_conn) - return normalize_bool (p->plugin.can_multi_conn (handle)); + return normalize_bool (p->plugin.can_multi_conn (c->handle)); else return 0; /* assume false */ } static int -plugin_can_cache (struct backend *b, void *handle) +plugin_can_cache (struct context *c) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); if (p->plugin.can_cache) - return p->plugin.can_cache (handle); + return p->plugin.can_cache (c->handle); if (p->plugin.cache) return NBDKIT_CACHE_NATIVE; return NBDKIT_CACHE_NONE; @@ -561,35 +574,37 @@ get_error (struct backend_plugin *p) } static int -plugin_pread (struct backend *b, void *handle, +plugin_pread (struct context *c, void *buf, uint32_t count, uint64_t offset, uint32_t flags, int *err) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); int r; assert (p->plugin.pread || p->plugin._pread_v1); if (p->plugin.pread) - r = p->plugin.pread (handle, buf, count, offset, 0); + r = p->plugin.pread (c->handle, buf, count, offset, 0); else - r = p->plugin._pread_v1 (handle, buf, count, offset); + r = p->plugin._pread_v1 (c->handle, buf, count, offset); if (r == -1) *err = get_error (p); return r; } static int -plugin_flush (struct backend *b, void *handle, +plugin_flush (struct context *c, uint32_t flags, int *err) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); int r; if (p->plugin.flush) - r = p->plugin.flush (handle, 0); + r = p->plugin.flush (c->handle, 0); else if (p->plugin._flush_v1) - r = p->plugin._flush_v1 (handle); + r = p->plugin._flush_v1 (c->handle); else { *err = EINVAL; return -1; @@ -600,10 +615,11 @@ plugin_flush (struct backend *b, void *handle, } static int -plugin_pwrite (struct backend *b, void *handle, +plugin_pwrite (struct context *c, const void *buf, uint32_t count, uint64_t offset, uint32_t flags, int *err) { + struct backend *b = c->b; int r; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); bool fua = flags & NBDKIT_FLAG_FUA; @@ -614,24 +630,25 @@ plugin_pwrite (struct backend *b, void *handle, need_flush = true; } if (p->plugin.pwrite) - r = p->plugin.pwrite (handle, buf, count, offset, flags); + r = p->plugin.pwrite (c->handle, buf, count, offset, flags); else if (p->plugin._pwrite_v1) - r = p->plugin._pwrite_v1 (handle, buf, count, offset); + r = p->plugin._pwrite_v1 (c->handle, buf, count, offset); else { *err = EROFS; return -1; } if (r != -1 && need_flush) - r = plugin_flush (b, handle, 0, err); + r = plugin_flush (c, 0, err); if (r == -1 && !*err) *err = get_error (p); return r; } static int -plugin_trim (struct backend *b, void *handle, +plugin_trim (struct context *c, uint32_t count, uint64_t offset, uint32_t flags, int *err) { + struct backend *b = c->b; int r; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); bool fua = flags & NBDKIT_FLAG_FUA; @@ -642,24 +659,25 @@ plugin_trim (struct backend *b, void *handle, need_flush = true; } if (p->plugin.trim) - r = p->plugin.trim (handle, count, offset, flags); + r = p->plugin.trim (c->handle, count, offset, flags); else if (p->plugin._trim_v1) - r = p->plugin._trim_v1 (handle, count, offset); + r = p->plugin._trim_v1 (c->handle, count, offset); else { *err = EINVAL; return -1; } if (r != -1 && need_flush) - r = plugin_flush (b, handle, 0, err); + r = plugin_flush (c, 0, err); if (r == -1 && !*err) *err = get_error (p); return r; } static int -plugin_zero (struct backend *b, void *handle, +plugin_zero (struct context *c, uint32_t count, uint64_t offset, uint32_t flags, int *err) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); int r = -1; bool may_trim = flags & NBDKIT_FLAG_MAY_TRIM; @@ -678,13 +696,13 @@ plugin_zero (struct backend *b, void *handle, if (backend_can_zero (b) == NBDKIT_ZERO_NATIVE) { errno = 0; if (p->plugin.zero) - r = p->plugin.zero (handle, count, offset, flags); + r = p->plugin.zero (c->handle, count, offset, flags); else if (p->plugin._zero_v1) { if (fast_zero) { *err = EOPNOTSUPP; return -1; } - r = p->plugin._zero_v1 (handle, count, offset, may_trim); + r = p->plugin._zero_v1 (c->handle, count, offset, may_trim); } else emulate = true; @@ -711,7 +729,7 @@ plugin_zero (struct backend *b, void *handle, static /* const */ char buf[MAX_REQUEST_SIZE]; uint32_t limit = MIN (count, sizeof buf); - r = plugin_pwrite (b, handle, buf, limit, offset, flags, err); + r = plugin_pwrite (c, buf, limit, offset, flags, err); if (r == -1) break; count -= limit; @@ -719,17 +737,18 @@ plugin_zero (struct backend *b, void *handle, done: if (r != -1 && need_flush) - r = plugin_flush (b, handle, 0, err); + r = plugin_flush (c, 0, err); if (r == -1 && !*err) *err = get_error (p); return r; } static int -plugin_extents (struct backend *b, void *handle, +plugin_extents (struct context *c, uint32_t count, uint64_t offset, uint32_t flags, struct nbdkit_extents *extents, int *err) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); int r; @@ -738,7 +757,7 @@ plugin_extents (struct backend *b, void *handle, return -1; } - r = p->plugin.extents (handle, count, offset, flags, extents); + r = p->plugin.extents (c->handle, count, offset, flags, extents); if (r >= 0 && nbdkit_extents_count (extents) < 1) { nbdkit_error ("extents: plugin must return at least one extent"); nbdkit_set_error (EINVAL); @@ -750,10 +769,11 @@ plugin_extents (struct backend *b, void *handle, } static int -plugin_cache (struct backend *b, void *handle, +plugin_cache (struct context *c, uint32_t count, uint64_t offset, uint32_t flags, int *err) { + struct backend *b = c->b; struct backend_plugin *p = container_of (b, struct backend_plugin, backend); int r; @@ -762,7 +782,7 @@ plugin_cache (struct backend *b, void *handle, if (!p->plugin.cache) return 0; - r = p->plugin.cache (handle, count, offset, flags); + r = p->plugin.cache (c->handle, count, offset, flags); if (r == -1) *err = get_error (p); return r; -- 2.30.1