Exposing backing-chain allocation over NBD
cover Exposing backing-chain allocation over NBD
I'm working on preparing my KVM Forum presentation on NBD, and ran into a situation where I really wanted to do the equivalent of 'qemu-img map' over NBD for determining which portions of an overlay image are changed from the backing layer. So after less than 24 hours hacking, I'm pretty pleased with the results.
Known caveats:
- Probably has lots of conflicts with Kevin's pending work on refactoring NBD for nicer use in qemu-storage-daemon
- Not yet tested with Vladimir's patches to fix bdrv_block_status bugs when the backing file is short (and therefore, applying this series without that is likely to make it possible to expose the same bugs of wrong information)
- I _still_ want to get QMP 'block-dirty-bitmap-populate' in qemu 5.2; with that in place, you could avoid the need for this series by instead populating a temporary bitmap and exposing that bitmap over NBD instead. But that requires more work, both in coding (Peter Krempa and I still need to make sure we have the ideal QMP interface) and in usage (managing temporary bitmaps is more effort than a new bool toggle).
- And if we _did_ use block-dirty-bitmap-populate, I find myself wanting to be able to expose more than one bitmap at a time over NBD, which in turn means revisiting our current QAPI for nbd-server-add of '*bitmap':'str' to instead allow an alternate type that permits either "'str'" or "['str']", except that our QAPI generator does not yet support arrays in alternates, and is undergoing changes from John Snow for python cleanups...
- I am aware of long-standing qemu bugs where when we advertise a large minimum block size (say 4k) but the backing file has smaller granularity (such as 512), then we can violate NBD protocol by sending a reply to NBD_CMD_BLOCK_STATUS with misaligned data. This adds yet another instance of being able to tickle that (rare) bug. I really need to revisit my patches to add bdrv_block_status_aligned...
Also available at:
https://repo.or.cz/qemu/ericb.git/shortlog/refs/tags/nbd-alloc-depth-v1
Eric Blake (3):
nbd: Simplify meta-context parsing
nbd: Add new qemu:allocation-depth metacontext
nbd: Add 'qemu-nbd -A' to expose allocation depth
docs/interop/nbd.txt | 22 ++- docs/tools/qemu-nbd.rst | 6 + qapi/block-core.json | 13 +- include/block/nbd.h | 15 +- blockdev-nbd.c | 3 +- nbd/server.c | 294 ++++++++++++++++++++----------------- qemu-nbd.c | 16 +- tests/qemu-iotests/309 | 73 +++++++++ tests/qemu-iotests/309.out | 22 +++ tests/qemu-iotests/group | 1 + 10 files changed, 310 insertions(+), 155 deletions(-) create mode 100755 tests/qemu-iotests/309 create mode 100644 tests/qemu-iotests/309.out
Signature · 1 line
2.28.0
PATCH 1/3 nbd: Simplify meta-context parsing
We had a premature optimization of trying to read as little from the wire as possible while handling NBD_OPT_SET_META_CONTEXT in phases. But in reality, we HAVE to read the entire string from the client before we can get to the next command, and it is easier to just read it all at once than it is to read it in pieces. And once we do that, several functions end up no longer performing I/O, and no longer need to return a value.
While simplifying this, take advantage of g_str_has_prefix for less repetition of boilerplate string length computation.
Our iotests still pass; I also checked that libnbd's testsuite (which covers more corner cases of odd meta context requests) still passes.
--- nbd/server.c | 172 ++++++++++++++------------------------------------- 1 file changed, 47 insertions(+), 125 deletions(-)
nbd/server.c +47 -125
@@ -1,5 +1,5 @@/* -* Copyright (C) 2016-2018 Red Hat, Inc. +* Copyright (C) 2016-2020 Red Hat, Inc. * Copyright (C) 2005 Anthony Liguori <[email protected]> * * Network Block Device Server Side
@@ -792,135 +792,64 @@ static int nbd_negotiate_send_meta_context(NBDClient *client,return qio_channel_writev_all (client->ioc, iov,2 , errp) <0 ? -EIO :0 ; } -/* Read strlen(@pattern) bytes, and set @match to true if they match @pattern. -* @match is never set to false. -* -* Return -errno on I/O error, 0 if option was completely handled by -* sending a reply about inconsistent lengths, or 1 on success. -* -* Note: return code = 1 doesn't mean that we've read exactly @pattern. -* It only means that there are no errors. + +/* +* Check @ns with @len bytes, and set @match to true if it matches @pattern, +* or if @len is 0 and the client is performing _LIST_. @match is never set +* to false. */ -static int nbd_meta_pattern (NBDClient *client,const char *pattern,bool *match, -Error **errp) +static void nbd_meta_empty_or_pattern (NBDClient *client,const char *pattern, +const char *ns,uint32_t len, +bool *match,Error **errp) { -int ret; -char *query; -size_t len =strlen (pattern); - -assert (len); - - query =g_malloc (len); - ret =nbd_opt_read (client, query, len, errp); -if (ret <=0 ) { -g_free (query); -return ret; - } - -if (strncmp (query, pattern, len) ==0 ) { +if (len ==0 ) { +if (client->opt ==NBD_OPT_LIST_META_CONTEXT ) { + *match = true; + } +trace_nbd_negotiate_meta_query_parse ("empty" ); + }else if (strcmp (pattern, ns) ==0 ) {trace_nbd_negotiate_meta_query_parse (pattern); *match = true; }else {trace_nbd_negotiate_meta_query_skip ("pattern not matched" ); } -g_free (query); - -return 1 ; -} - -/* -* Read @len bytes, and set @match to true if they match @pattern, or if @len -* is 0 and the client is performing _LIST_. @match is never set to false. -* -* Return -errno on I/O error, 0 if option was completely handled by -* sending a reply about inconsistent lengths, or 1 on success. -* -* Note: return code = 1 doesn't mean that we've read exactly @pattern. -* It only means that there are no errors. -*/ -static int nbd_meta_empty_or_pattern (NBDClient *client,const char *pattern, -uint32_t len,bool *match,Error **errp) -{ -if (len ==0 ) { -if (client->opt ==NBD_OPT_LIST_META_CONTEXT ) { - *match = true; - } -trace_nbd_negotiate_meta_query_parse ("empty" ); -return 1 ; - } - -if (len !=strlen (pattern)) { -trace_nbd_negotiate_meta_query_skip ("different lengths" ); -return nbd_opt_skip (client, len, errp); - } - -return nbd_meta_pattern (client, pattern, match, errp); }/* nbd_meta_base_query * * Handle queries to 'base' namespace. For now, only the base:allocation -* context is available. 'len' is the amount of text remaining to be read from -* the current name, after the 'base:' portion has been stripped. -* -* Return -errno on I/O error, 0 if option was completely handled by -* sending a reply about inconsistent lengths, or 1 on success. +* context is available. @len is the length of @ns, including the 'base:' +* prefix. */ -static int nbd_meta_base_query (NBDClient *client,NBDExportMetaContexts *meta, -uint32_t len,Error **errp) +static void nbd_meta_base_query (NBDClient *client,NBDExportMetaContexts *meta, +const char *ns,uint32_t len,Error **errp) { -return nbd_meta_empty_or_pattern (client,"allocation" , len, - &meta->base_allocation, errp); +nbd_meta_empty_or_pattern (client,"allocation" , ns +5 , len -5 , + &meta->base_allocation, errp); }/* nbd_meta_bitmap_query * * Handle query to 'qemu:' namespace. -* @len is the amount of text remaining to be read from the current name, after -* the 'qemu:' portion has been stripped. -* -* Return -errno on I/O error, 0 if option was completely handled by -* sending a reply about inconsistent lengths, or 1 on success. */ -static int nbd_meta_qemu_query (NBDClient *client,NBDExportMetaContexts *meta, -uint32_t len,Error **errp) +* @len is the length of @ns, including the `qemu:' prefix. +*/ +static void nbd_meta_qemu_query (NBDClient *client,NBDExportMetaContexts *meta, +const char *ns,uint32_t len,Error **errp) { -bool dirty_bitmap = false; -size_t dirty_bitmap_len =strlen ("dirty-bitmap:" ); -int ret; -if (!meta->exp->export_bitmap) {trace_nbd_negotiate_meta_query_skip ("no dirty-bitmap exported" ); -return nbd_opt_skip (client, len, errp); - } - -if (len ==0 ) { + }else if (len ==0 ) {if (client->opt ==NBD_OPT_LIST_META_CONTEXT ) { meta->bitmap = true; }trace_nbd_negotiate_meta_query_parse ("empty" ); -return 1 ; - } - -if (len < dirty_bitmap_len) { -trace_nbd_negotiate_meta_query_skip ("not dirty-bitmap:" ); -return nbd_opt_skip (client, len, errp); - } - - len -= dirty_bitmap_len; - ret =nbd_meta_pattern (client,"dirty-bitmap:" , &dirty_bitmap, errp); -if (ret <=0 ) { -return ret; - } -if (!dirty_bitmap) { + }else if (!g_str_has_prefix (ns +5 ,"dirty-bitmap:" )) {trace_nbd_negotiate_meta_query_skip ("not dirty-bitmap:" ); -return nbd_opt_skip (client, len, errp); + }else { +trace_nbd_negotiate_meta_query_parse ("dirty-bitmap:" ); +nbd_meta_empty_or_pattern (client, meta->exp->export_bitmap_context, + ns, len, &meta->bitmap, errp); } - -trace_nbd_negotiate_meta_query_parse ("dirty-bitmap:" ); - -return nbd_meta_empty_or_pattern ( - client, meta->exp->export_bitmap_context + -strlen ("qemu:dirty_bitmap:" ), len, &meta->bitmap, errp); }/* nbd_negotiate_meta_query
@@ -930,22 +859,13 @@ static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,* * The only supported namespaces are 'base' and 'qemu'. * -* The function aims not wasting time and memory to read long unknown namespace -* names. -* * Return -errno on I/O error, 0 if option was completely handled by * sending a reply about inconsistent lengths, or 1 on success. */ static int nbd_negotiate_meta_query (NBDClient *client,NBDExportMetaContexts *meta,Error **errp) { -/* -* Both 'qemu' and 'base' namespaces have length = 5 including a -* colon. If another length namespace is later introduced, this -* should certainly be refactored. -*/ int ret; -size_t ns_len =5 ; -char ns[5 ]; + g_autofree char *ns = NULL;uint32_t len; ret =nbd_opt_read (client, &len,sizeof (len), errp);
@@ -958,27 +878,29 @@ static int nbd_negotiate_meta_query(NBDClient *client,trace_nbd_negotiate_meta_query_skip ("length too long" );return nbd_opt_skip (client, len, errp); } -if (len < ns_len) { -trace_nbd_negotiate_meta_query_skip ("length too short" ); -return nbd_opt_skip (client, len, errp); - } - len -= ns_len; - ret =nbd_opt_read (client, ns, ns_len, errp); + ns =g_malloc (len +1 ); + ret =nbd_opt_read (client, ns, len, errp);if (ret <=0 ) {return ret; } + ns[len] ='\0' ; +if (strlen (ns) != len) { +return nbd_opt_invalid (client, errp, +"Embedded NUL in query for option %s" , +nbd_opt_lookup (client->opt)); + } -if (!strncmp (ns,"base:" , ns_len)) { +if (g_str_has_prefix (ns,"base:" )) {trace_nbd_negotiate_meta_query_parse ("base:" ); -return nbd_meta_base_query (client, meta, len, errp); - }else if (!strncmp (ns,"qemu:" , ns_len)) { +nbd_meta_base_query (client, meta, ns, len, errp); + }else if (g_str_has_prefix (ns,"qemu:" )) {trace_nbd_negotiate_meta_query_parse ("qemu:" ); -return nbd_meta_qemu_query (client, meta, len, errp); +nbd_meta_qemu_query (client, meta, ns, len, errp); + }else { +trace_nbd_negotiate_meta_query_skip ("unknown namespace" ); } - -trace_nbd_negotiate_meta_query_skip ("unknown namespace" ); -return nbd_opt_skip (client, len, errp); +return 1 ; }/* nbd_negotiate_meta_queries
Signature · 1 line
2.28.0
Re: [PATCH 1/3] nbd: Simplify meta-context parsing
Quoted 42 lines · Eric Blake
We had a premature optimization of trying to read as little from the wire as possible while handling
NBD_OPT_SET_META_CONTEXTin phases. But in reality, we HAVE to read the entire string from the client before we can get to the next command, and it is easier to just read it all at once than it is to read it in pieces. And once we do that, several functions end up no longer performing I/O, and no longer need to return a value.While simplifying this, take advantage of
g_str_has_prefixfor less repetition of boilerplate string length computation.Our iotests still pass; I also checked that libnbd's testsuite (which covers more corner cases of odd meta context requests) still passes.
Signed-off-by Eric Blake <[email protected]>--- nbd/server.c | 172 ++++++++++++++------------------------------------- 1 file changed, 47 insertions(+), 125 deletions(-)nbd/server.c +1
-9@@ -1,5 +1,5 @@/* -* Copyright (C) 2016-2018 Red Hat, Inc. +* Copyright (C) 2016-2020 Red Hat, Inc. * Copyright (C) 2005 Anthony Liguori <[email protected]> * * Network Block Device Server Side @@ -792,135 +792,64 @@ static int nbd_negotiate_send_meta_context(NBDClient *client,return qio_channel_writev_all (client->ioc, iov,2 , errp) <0 ? -EIO :0 ; } -/* Read strlen(@pattern) bytes, and set @match to true if they match @pattern. -* @match is never set to false. -* -* Return -errno on I/O error, 0 if option was completely handled by -* sending a reply about inconsistent lengths, or 1 on success. -* -* Note: return code = 1 doesn't mean that we've read exactly @pattern. -* It only means that there are no errors.
nbd/server.c static int nbd_negotiate_send_meta_context(NBDClient *client, · line 802
Quoted 10 lines
nbd/server.c +7
-2+ +/* +* Check @ns with @len bytes, and set @match to true if it matches @pattern, +* or if @len is 0 and the client is performing _LIST_. @match is never set +* to false. */ -static int nbd_meta_pattern (NBDClient *client,const char *pattern,bool *match, -Error **errp) +static void nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern, + const char *ns, uint32_t len,
ns changed its meaning, it's not just a namespace, but the whole query. I think, better to rename it.
Also, it's unusual to pass length together with nul-terminated string, it seems redundant. And, it's used only to compare with zero, strlen(ns) == 0 or ns[0] == 0 is not slower.
Also, errp is unused argument. And it violate Error API recommendation to not create void functions with errp.
Also we can use bool return instead of return through pointer.
Quoted 70 lines
nbd/server.c +8
-51+ bool *match, Error **errp) { -int ret; -char *query; -size_t len =strlen (pattern); - -assert (len); - - query =g_malloc (len); - ret =nbd_opt_read (client, query, len, errp); -if (ret <=0 ) { -g_free (query); -return ret; - } - -if (strncmp (query, pattern, len) ==0 ) { +if (len ==0 ) { +if (client->opt ==NBD_OPT_LIST_META_CONTEXT ) { + *match = true; + } +trace_nbd_negotiate_meta_query_parse ("empty" ); + }else if (strcmp (pattern, ns) ==0 ) {trace_nbd_negotiate_meta_query_parse (pattern); *match = true; }else {trace_nbd_negotiate_meta_query_skip ("pattern not matched" ); } -g_free (query); - -return 1 ; -} - -/* -* Read @len bytes, and set @match to true if they match @pattern, or if @len -* is 0 and the client is performing _LIST_. @match is never set to false. -* -* Return -errno on I/O error, 0 if option was completely handled by -* sending a reply about inconsistent lengths, or 1 on success. -* -* Note: return code = 1 doesn't mean that we've read exactly @pattern. -* It only means that there are no errors. -*/ -static int nbd_meta_empty_or_pattern (NBDClient *client,const char *pattern, -uint32_t len,bool *match,Error **errp) -{ -if (len ==0 ) { -if (client->opt ==NBD_OPT_LIST_META_CONTEXT ) { - *match = true; - } -trace_nbd_negotiate_meta_query_parse ("empty" ); -return 1 ; - } - -if (len !=strlen (pattern)) { -trace_nbd_negotiate_meta_query_skip ("different lengths" ); -return nbd_opt_skip (client, len, errp); - } - -return nbd_meta_pattern (client, pattern, match, errp); }/* nbd_meta_base_query * * Handle queries to 'base' namespace. For now, only the base:allocation -* context is available. 'len' is the amount of text remaining to be read from -* the current name, after the 'base:' portion has been stripped. -* -* Return -errno on I/O error, 0 if option was completely handled by -* sending a reply about inconsistent lengths, or 1 on success. +* context is available. @len is the length of @ns, including the 'base:'
nbd/server.c static int nbd_negotiate_send_meta_context(NBDClient *client, · line 827
Quoted 10 lines
nbd/server.c +4
-4+* prefix. */ -static int nbd_meta_base_query (NBDClient *client,NBDExportMetaContexts *meta, -uint32_t len,Error **errp) +static void nbd_meta_base_query (NBDClient *client,NBDExportMetaContexts *meta, +const char *ns,uint32_t len,Error **errp) { -return nbd_meta_empty_or_pattern (client,"allocation" , len, - &meta->base_allocation, errp); + nbd_meta_empty_or_pattern(client, "allocation", ns + 5, len - 5,
This one is correct, but would be simpler, if only subquery (after colon) passed here. (Really, no reason to pass 'base:' to _base_ func)
Hmm, I see that it gives a possibility to use meta->exp->export_bitmap_context directly.
Quoted 19 lines
nbd/server.c +5
-8+ &meta->base_allocation, errp); }/* nbd_meta_bitmap_query * * Handle query to 'qemu:' namespace. -* @len is the amount of text remaining to be read from the current name, after -* the 'qemu:' portion has been stripped. -* -* Return -errno on I/O error, 0 if option was completely handled by -* sending a reply about inconsistent lengths, or 1 on success. */ -static int nbd_meta_qemu_query (NBDClient *client,NBDExportMetaContexts *meta, -uint32_t len,Error **errp) +* @len is the length of @ns, including the `qemu:' prefix. +*/ +static void nbd_meta_qemu_query (NBDClient *client,NBDExportMetaContexts *meta, +const char *ns,uint32_t len,Error **errp) { -bool dirty_bitmap = false;
nbd/server.c static int nbd_negotiate_send_meta_context(NBDClient *client, · line 841
Quoted 10 lines
nbd/server.c +1
-7-size_t dirty_bitmap_len =strlen ("dirty-bitmap:" ); -int ret; -if (!meta->exp->export_bitmap) {trace_nbd_negotiate_meta_query_skip ("no dirty-bitmap exported" ); -return nbd_opt_skip (client, len, errp); - } - -if (len ==0 ) { + }else if (len ==0 ) {
s/0/5/ ?
Quoted 15 lines
nbd/server.c +0
-11if (client->opt ==NBD_OPT_LIST_META_CONTEXT ) { meta->bitmap = true; }trace_nbd_negotiate_meta_query_parse ("empty" ); -return 1 ; - } - -if (len < dirty_bitmap_len) { -trace_nbd_negotiate_meta_query_skip ("not dirty-bitmap:" ); -return nbd_opt_skip (client, len, errp); - } - - len -= dirty_bitmap_len; - ret =nbd_meta_pattern (client,"dirty-bitmap:" , &dirty_bitmap, errp); -if (ret <=0 ) {
nbd/server.c static int nbd_negotiate_send_meta_context(NBDClient *client, · line 851
Quoted 10 lines
nbd/server.c +5
-4-return ret; - } -if (!dirty_bitmap) { + }else if (!g_str_has_prefix (ns +5 ,"dirty-bitmap:" )) {trace_nbd_negotiate_meta_query_skip ("not dirty-bitmap:" ); -return nbd_opt_skip (client, len, errp); + }else { +trace_nbd_negotiate_meta_query_parse ("dirty-bitmap:" ); +nbd_meta_empty_or_pattern (client, meta->exp->export_bitmap_context, + ns, len, &meta->bitmap, errp);
hmm. _empty_ is impossible here, as ns includes "qemu:dirty-bitmap"..
Quoted 37 lines
nbd/server.c +1
-16} - -trace_nbd_negotiate_meta_query_parse ("dirty-bitmap:" ); - -return nbd_meta_empty_or_pattern ( - client, meta->exp->export_bitmap_context + -strlen ("qemu:dirty_bitmap:" ), len, &meta->bitmap, errp); }/* nbd_negotiate_meta_query @@ -930,22 +859,13 @@ static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,* * The only supported namespaces are 'base' and 'qemu'. * -* The function aims not wasting time and memory to read long unknown namespace -* names. -* * Return -errno on I/O error, 0 if option was completely handled by * sending a reply about inconsistent lengths, or 1 on success. */ static int nbd_negotiate_meta_query (NBDClient *client,NBDExportMetaContexts *meta,Error **errp) { -/* -* Both 'qemu' and 'base' namespaces have length = 5 including a -* colon. If another length namespace is later introduced, this -* should certainly be refactored. -*/ int ret; -size_t ns_len =5 ; -char ns[5 ]; + g_autofree char *ns = NULL;uint32_t len; ret =nbd_opt_read (client, &len,sizeof (len), errp);@@ -958,27 +878,29 @@ static int nbd_negotiate_meta_query(NBDClient *client,trace_nbd_negotiate_meta_query_skip ("length too long" );return nbd_opt_skip (client, len, errp);
nbd/server.c static int nbd_negotiate_meta_query(NBDClient *client, · line 883
Quoted 10 lines
nbd/server.c +2
-6} -if (len < ns_len) { -trace_nbd_negotiate_meta_query_skip ("length too short" ); -return nbd_opt_skip (client, len, errp); - } - len -= ns_len; - ret =nbd_opt_read (client, ns, ns_len, errp); + ns =g_malloc (len +1 ); + ret =nbd_opt_read (client, ns, len, errp);
Now ns is not a namespace but the whole query. I'd rename a variable.
nbd/server.c static int nbd_negotiate_meta_query(NBDClient *client, · line 892
Quoted 9 lines
nbd/server.c +6
-0if (ret <=0 ) {return ret; } + ns[len] ='\0' ; +if (strlen (ns) != len) { +return nbd_opt_invalid (client, errp, +"Embedded NUL in query for option %s" , +nbd_opt_lookup (client->opt)); + }
Hmm, that's a new good check. Intersting, is it specified in NBD protocol, that NUL shouldn't be in a string.. Probably it can be a separate patch, with new nbd_opt_string_read, which will check this thing. But I'm OK with it as is in this patch.
nbd/server.c static int nbd_negotiate_meta_query(NBDClient *client, · line 899
Quoted 11 lines
nbd/server.c +4
-4-if (!strncmp (ns,"base:" , ns_len)) { +if (g_str_has_prefix (ns,"base:" )) {trace_nbd_negotiate_meta_query_parse ("base:" ); -return nbd_meta_base_query (client, meta, len, errp); - }else if (!strncmp (ns,"qemu:" , ns_len)) { +nbd_meta_base_query (client, meta, ns, len, errp); + }else if (g_str_has_prefix (ns,"qemu:" )) {trace_nbd_negotiate_meta_query_parse ("qemu:" ); -return nbd_meta_qemu_query (client, meta, len, errp); +nbd_meta_qemu_query (client, meta, ns, len, errp);
passing length of string together with string seems redundant (and error prone).
nbd/server.c static int nbd_negotiate_meta_query(NBDClient *client, · line 979
Quoted 4 lines
nbd/server.c +2
-1+ }else { +trace_nbd_negotiate_meta_query_skip ("unknown namespace" ); } -
Seems you don't like my new-line before final return statement.
nbd/server.c static int nbd_negotiate_meta_query(NBDClient *client,
Quoted 7 lines
nbd/server.c +1
-2-trace_nbd_negotiate_meta_query_skip ("unknown namespace" ); -return nbd_opt_skip (client, len, errp); +return 1 ; }/* nbd_negotiate_meta_queries
To avoid all this pointer arithmetic, what about something like this (I didn't check, just an idea):
/*
* Return true if @query matches @pattern of if @query is empty and the client
* is performing _LIST_. Otherwise return false.
*/
static bool nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
const char *query)
{
if (!strcmp(query, "")) {
trace_nbd_negotiate_meta_query_parse("empty");
return client->opt == NBD_OPT_LIST_META_CONTEXT;
} else if (!strcmp(query, pattern)) {
trace_nbd_negotiate_meta_query_parse(pattern);
return true;
} else {
trace_nbd_negotiate_meta_query_skip("pattern not matched");
return false;
}
}bool strshift(const char **str, const char *prefix)
{
if (g_str_has_prefix(*str, prefix)) {
*str += strlen(prefix);
return true;
}
return false;
}/*
* nbd_meta_base_query
*
* If it's 'base' namespace parse the query and return true. If not return
* false.
*/
static bool nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
const char *query)
{
if (!strshift(&query, "base:")) {
return false;
}
trace_nbd_negotiate_meta_query_parse("base:");
meta->base_allocation =
nbd_meta_empty_or_pattern(client, "allocation", query);
return true;
}/*
* nbd_meta_bitmap_query
*
* If it's 'qemu' namespace parse the query and return true. If not return
* false.
*/
static bool nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
const char *query)
{
if (!strshift(&query, "qemu:")) {
return false;
}
trace_nbd_negotiate_meta_query_parse("qemu:");
if (!meta->exp->export_bitmap) {
trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported");
return true;
}
if (!query[0]) {
if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
meta->bitmap = true;
}
trace_nbd_negotiate_meta_query_parse("empty");
return true;
}
if (!strshift(&query, "dirty-bitmap:")) {
trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
return true;
}
trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
meta->bitmap = nbd_meta_empty_or_pattern(
client, meta->exp->export_bitmap_context +
strlen("qemu:dirty-bitmap:"), query);
return true;
}/*
* nbd_negotiate_meta_query
*
* Parse namespace name and call corresponding function to parse body of the
* query.
*
* The only supported namespaces are 'base' and 'qemu'.
*
* Return -errno on I/O error, 0 if option was completely handled by
* sending a reply about inconsistent lengths, or 1 on success. */
static int nbd_negotiate_meta_query(NBDClient *client,
NBDExportMetaContexts *meta, Error **errp)
{
int ret;
g_autofree char *query = NULL;
uint32_t len;
ret = nbd_opt_read(client, &len, sizeof(len), errp);
if (ret <= 0) {
return ret;
}
len = cpu_to_be32(len);
if (len > NBD_MAX_STRING_SIZE) {
trace_nbd_negotiate_meta_query_skip("length too long");
return nbd_opt_skip(client, len, errp);
}
query = g_malloc(len + 1);
ret = nbd_opt_read(client, query, len, errp);
if (ret <= 0) {
return ret;
}
query[len] = '\0';
if (strlen(query) != len) {
return nbd_opt_invalid(client, errp,
"Embedded NUL in query for option %s",
nbd_opt_lookup(client->opt));
}
if (nbd_meta_base_query(client, meta, query)) {
return 1;
if (nbd_meta_qemu_query(client, meta, query)) {
return 1;
}
trace_nbd_negotiate_meta_query_skip("unknown namespace");
return 1;
}Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 1/3] nbd: Simplify meta-context parsing
Quoted 36 lines · Vladimir Sementsov-Ogievskiy
Quoted 35 lines · Eric Blake
We had a premature optimization of trying to read as little from the wire as possible while handling
NBD_OPT_SET_META_CONTEXTin phases. But in reality, we HAVE to read the entire string from the client before we can get to the next command, and it is easier to just read it all at once than it is to read it in pieces. And once we do that, several functions end up no longer performing I/O, and no longer need to return a value.While simplifying this, take advantage of
g_str_has_prefixfor less repetition of boilerplate string length computation.Our iotests still pass; I also checked that libnbd's testsuite (which covers more corner cases of odd meta context requests) still passes.
Signed-off-by Eric Blake <[email protected]>---
-/* Read strlen(@pattern) bytes, and set @match to true if they match @pattern.
- * @match is never set to false.
- *
- * Return -errno on I/O error, 0 if option was completely handled by
- * sending a reply about inconsistent lengths, or 1 on success.
- *
- * Note: return code = 1 doesn't mean that we've read exactly @pattern.
- * It only means that there are no errors.nbd/server.c +3
-0+ +/* +* Check @ns with @len bytes, and set @match to true if it matches @pattern,
+ * or if @len is 0 and the client is performing _LIST_. @match is never set
+ * to false.
*/
Quoted 8 lines
Quoted 6 lines
-static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match,
- Error **errp)
+static void nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
+ const char *ns,uint32_tlen,ns changed its meaning, it's not just a namespace, but the whole query. I think, better to rename it.
Sure, that makes sense.
Quoted 3 lines
Also, it's unusual to pass length together with nul-terminated string, it seems redundant. And, it's used only to compare with zero, strlen(ns) == 0 or ns[0] == 0 is not slower.
Good point.
Quoted 2 lines
Also, errp is unused argument. And it violate Error API recommendation to not create void functions with errp.
Another simplification. Looks like I'll be spinning v2.
Quoted 2 lines
Also we can use bool return instead of return through pointer.
That changes the callers, but it's not necessarily a bad thing; whereas we were doing:
if (cond) {
nbd_meta_pattern(..., &match, ...);
}which either leaves match unchanged or sets it to true, we could instead do:
if (nbd_meta_pattern(...)) {
match = true;
}My only worry is that the more changes I make, the harder the patch is to read. I don't know if it's worth breaking it into steps, or just doing all the simplifications in one blow.
Quoted 9 lines
Quoted 8 lines
+static void nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
+ const char *ns,uint32_tlen, Error **errp)
{
- return nbd_meta_empty_or_pattern(client, "allocation", len, -&meta->base_allocation, errp); + nbd_meta_empty_or_pattern(client, "allocation", ns + 5, len - 5,
Quoted 4 lines
This one is correct, but would be simpler, if only subquery (after colon) passed here. (Really, no reason to pass 'base:' to _base_ func)
Hmm, I see that it gives a possibility to use
meta->exp->export_bitmap_contextdirectly.
Yeah, that's where it got interesting. A direct strcmp() is nice, but if we strip a prefix in one place, we have to strip it in the other. I could go either way.
Quoted 12 lines
Quoted 12 lines
+ * @len is the length of @ns, including the `qemu:' prefix.
+ */
+static void nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
+ const char *ns,uint32_tlen, Error **errp)
{
- booldirty_bitmap= false;
-size_tdirty_bitmap_len=strlen("dirty-bitmap:");
- int ret;
-
if (!meta->exp->export_bitmap) {
Quoted 8 lines
Quoted 6 lines
trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported"); - returnnbd_opt_skip(client, len, errp);
- }
-
- if (len == 0) {
+ } else if (len == 0) {s/0/5/ ?
Oh, good catch. Using 0 is an unintended logic change, but none of our iotests caught it, so we're also missing test coverage.
I'm working on adding 'nbd_opt_list_meta_context()' to libnbd, which will add testsuite coverage of this.
Quoted 10 lines
Quoted 8 lines
+ } else if (!g_str_has_prefix(ns + 5, "dirty-bitmap:")) {
trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:"); - returnnbd_opt_skip(client, len, errp);
+ } else {
+trace_nbd_negotiate_meta_query_parse("dirty-bitmap:"); + nbd_meta_empty_or_pattern(client,meta->exp->export_bitmap_context,
+ ns, len,&meta->bitmap, errp);hmm. _empty_ is impossible here, as ns includes "qemu:dirty-bitmap"..
Ultimately, we want something like:
if starts with "base:":
if nothing else:
if list mode:
return "base:allocation"
else:
return empty list
else if "base:allocation":
return "base:allocation"
else
return empty list
else if starts with "qemu:":
if nothing else:
if list mode:
return all possible sub-qemu contexts
else:
return empty list
else if starts with "qemu:dirty-bitmap:":
if nothing else:
if list mode:
return all possible dirty-bitmap contexts (right now, just one,
but I want to allow an array of bitmaps in the future)
else:
return empty list
else if exact match:
return that bitmap
else:
return empty list
else if "qemu:allocation-depth" (from next patch):
return "allocation-depth"
else:
return empty list
else:
return empty list
Maybe some function renames will help.
Quoted 10 lines
Quoted 9 lines
if (ret <= 0) {
return ret;
}
+ ns[len] = '\0';
+ if (strlen(ns) != len) {
+ return nbd_opt_invalid(client, errp,
+ "Embedded NUL in query for option %s", +nbd_opt_lookup(client->opt)); + }
Quoted 4 lines
Hmm, that's a new good check. Intersting, is it specified in NBD protocol, that NUL shouldn't be in a string.. Probably it can be a separate patch, with new
nbd_opt_string_read, which will check this thing. But I'm OK with it as is in this patch.
I'll separate it.
Quoted 2 lines
To avoid all this pointer arithmetic, what about something like this (I didn't check, just an idea):
I'll see what parts of that I can use in v2.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
PATCH 2/3 nbd: Add new qemu:allocation-depth metacontext
In reply to Eric Blake'qemu-img map' provides a way to determine which extents of an image come from the top layer vs. inherited from a backing chain. This is useful information worth exposing over NBD. There is a proposal to add a QMP command block-dirty-bitmap-populate which can create a dirty bitmap that reflects allocation information, at which point qemu:dirty-bitmap:NAME can expose that information via the creation of a temporary bitmap, but we can shorten the effort by adding a new qemu:allocation-depth context that does the same thing without an intermediate bitmap (this patch does not eliminate the need for that proposal, as it will have other uses as well).
For this patch, I just encoded a tri-state value (unallocated, from this layer, from any of the backing layers); we could instead or in addition report an actual depth count per extent, if that proves more useful.
Note that this patch does not actually enable any way to request a server to enable this context; that will come in the next patch.
--- docs/interop/nbd.txt | 22 +++++++-- qapi/block-core.json | 6 ++- include/block/nbd.h | 8 +++- nbd/server.c | 110 +++++++++++++++++++++++++++++++++++++++---- 4 files changed, 132 insertions(+), 14 deletions(-)
docs/interop/nbd.txt +18 -4
@@ -17,9 +17,9 @@ namespace "qemu". == "qemu" namespace == -The "qemu" namespace currently contains only one type of context, -related to exposing the contents of a dirty bitmap alongside the -associated disk contents. That context has the following form: +The "qemu" namespace currently contains two types of context. The +first is related to exposing the contents of a dirty bitmap alongside +the associated disk contents. That context has the following form: qemu:dirty-bitmap:<dirty-bitmap-export-name>
@@ -28,8 +28,21 @@ in reply for NBD_CMD_BLOCK_STATUS: bit 0: NBD_STATE_DIRTY, means that the extent is "dirty" +The second is related to exposing the source of various extents within +the image, with a single context named: + + qemu:allocation-depth + +In the allocation depth context, bits 0 and 1 form a tri-state value: + + bits 0-1 clear: NBD_STATE_DEPTH_UNALLOC, means the extent is unallocated + bit 0 set: NBD_STATE_DEPTH_LOCAL, the extent is allocated in this image + bit 1 set: NBD_STATE_DEPTH_BACKING, the extent is inherited from a + backing layer + For NBD_OPT_LIST_META_CONTEXT the following queries are supported -in addition to "qemu:dirty-bitmap:<dirty-bitmap-export-name>": +in addition to the specific "qemu:allocation-depth" and +"qemu:dirty-bitmap:<dirty-bitmap-export-name>": * "qemu:" - returns list of all available metadata contexts in the namespace.
@@ -55,3 +68,4 @@ the operation of that feature. NBD_CMD_BLOCK_STATUS for "qemu:dirty-bitmap:", NBD_CMD_CACHE * 4.2: NBD_FLAG_CAN_MULTI_CONN for shareable read-only exports, NBD_CMD_FLAG_FAST_ZERO +* 5.2: NBD_CMD_BLOCK_STATUS for "qemu:allocation-depth"
qapi/block-core.json +5 -1
@@ -5261,11 +5261,15 @@ # NBD client can use NBD_OPT_SET_META_CONTEXT with # "qemu:dirty-bitmap:NAME" to inspect the bitmap. (since 4.0) # +# @alloc: Also export the allocation map for @device, so the NBD client +# can use NBD_OPT_SET_META_CONTEXT with "qemu:allocation-depth" +# to inspect allocation details. (since 5.2) +# # Since: 5.0 ## { 'struct': 'BlockExportNbd', 'data': {'device': 'str', '*name': 'str', '*description': 'str', - '*writable': 'bool', '*bitmap': 'str' } } + '*writable': 'bool', '*bitmap': 'str', '*alloc': 'bool' } } ## # @nbd-server-add:
include/block/nbd.h +7 -1
@@ -1,5 +1,5 @@/* -* Copyright (C) 2016-2019 Red Hat, Inc. +* Copyright (C) 2016-2020 Red Hat, Inc. * Copyright (C) 2005 Anthony Liguori <[email protected]> * * Network Block Device
@@ -257,6 +257,12 @@ enum {/* Extent flags for qemu:dirty-bitmap in NBD_REPLY_TYPE_BLOCK_STATUS */ #define NBD_STATE_DIRTY (1 << 0) +/* Extent flags for qemu:allocation-depth in NBD_REPLY_TYPE_BLOCK_STATUS */ +#define NBD_STATE_DEPTH_UNALLOC (0 << 0) +#define NBD_STATE_DEPTH_LOCAL (1 << 0) +#define NBD_STATE_DEPTH_BACKING (2 << 0) +#define NBD_STATE_DEPTH_MASK (0x3) +static inline bool nbd_reply_type_is_error (int type) {return type & (1 <<15 );
nbd/server.c +102 -8
192 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH 2/3] nbd: Add new qemu:allocation-depth metacontext
docs/interop/nbd.txt in reply for NBD_CMD_BLOCK_STATUS: · line 41
Quoted 11 lines · Eric Blake
docs/interop/nbd.txt +11
-0+The second is related to exposing the source of various extents within +the image, with a single context named: + + qemu:allocation-depth + +In the allocation depth context, bits 0 and 1 form a tri-state value: + + bits 0-1 clear: NBD_STATE_DEPTH_UNALLOC, means the extent is unallocated + bit 0 set: NBD_STATE_DEPTH_LOCAL, the extent is allocated in this image + bit 1 set: NBD_STATE_DEPTH_BACKING, the extent is inherited from a + backing layer
From the cover description I imagined it would show the actual depth, ie:
top -> backing -> backing -> backing
depth: 1 2 3 .... (0 = unallocated)
I wonder if that is possible? (Perhaps there's something I don't understand here.)
Rich.
Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Re: [PATCH 2/3] nbd: Add new qemu:allocation-depth metacontext
Quoted 15 lines · Richard W.M. Jones
docs/interop/nbd.txt in reply for NBD_CMD_BLOCK_STATUS: · line 41
Quoted 11 lines · Eric Blake
docs/interop/nbd.txt +11
-0+The second is related to exposing the source of various extents within +the image, with a single context named: + + qemu:allocation-depth + +In the allocation depth context, bits 0 and 1 form a tri-state value: + + bits 0-1 clear: NBD_STATE_DEPTH_UNALLOC, means the extent is unallocated + bit 0 set: NBD_STATE_DEPTH_LOCAL, the extent is allocated in this image + bit 1 set: NBD_STATE_DEPTH_BACKING, the extent is inherited from a + backing layerFrom the cover description I imagined it would show the actual depth, ie:
Quoted 6 lines
top -> backing -> backing -> backingdepth: 1 2 3 .... (0 = unallocated)
I wonder if that is possible? (Perhaps there's something I don't understand here.)
That's possible if we want it. Probably the best way is to add *depth parameter to bdrv_is_allocated_above (and better on top of my "[PATCH v7 0/5] fix & merge block_status_above and is_allocated_above")
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 2/3] nbd: Add new qemu:allocation-depth metacontext
In reply to Richard W.M. JonesQuoted 13 lines · Richard W.M. Jones
Quoted 11 lines · Eric Blake
docs/interop/nbd.txt +11
-0+The second is related to exposing the source of various extents within +the image, with a single context named: + + qemu:allocation-depth + +In the allocation depth context, bits 0 and 1 form a tri-state value: + + bits 0-1 clear: NBD_STATE_DEPTH_UNALLOC, means the extent is unallocated + bit 0 set: NBD_STATE_DEPTH_LOCAL, the extent is allocated in this image + bit 1 set: NBD_STATE_DEPTH_BACKING, the extent is inherited from a + backing layer
Quoted 7 lines
Quoted 1 line
From the cover description I imagined it would show the actual depth, ie:
top -> backing -> backing -> backingdepth: 1 2 3 .... (0 = unallocated)
I wonder if that is possible? (Perhaps there's something I don't understand here.)
The real reason I don't want to do a straight depth number is that 'qemu-img map' combined with x-dirty-bitmap is still a very convenient way to get at bits 0 and 1 (even if it requires decoding). But if we plumb in a way for bdrv_get_status to return depth counts (rather than reimplementing the depth count ourselves), I would have no problem with returning a struct:
bits 31-4: the depth of the chain
bits 3-2: reserved (to make reading hex values easier...) bits 1-0: tri-state of unalloc, local, or backing
where it would look like:
0x0000 -> unallocated0x0011 -> depth 1, local0x0022 -> depth 2, from the first backing layer0x0032 -> depth 3, from the second backing layer0x0042 ...
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [PATCH 2/3] nbd: Add new qemu:allocation-depth metacontext
Quoted 33 lines · Eric Blake
Quoted 13 lines · Richard W.M. Jones
Quoted 11 lines · Eric Blake
docs/interop/nbd.txt +11
-0+The second is related to exposing the source of various extents within +the image, with a single context named: + + qemu:allocation-depth + +In the allocation depth context, bits 0 and 1 form a tri-state value: + + bits 0-1 clear: NBD_STATE_DEPTH_UNALLOC, means the extent is unallocated + bit 0 set: NBD_STATE_DEPTH_LOCAL, the extent is allocated in this image + bit 1 set: NBD_STATE_DEPTH_BACKING, the extent is inherited from a + backing layerQuoted 7 lines
Quoted 1 line
From the cover description I imagined it would show the actual depth, ie:
top -> backing -> backing -> backingdepth: 1 2 3 .... (0 = unallocated)
I wonder if that is possible? (Perhaps there's something I don't understand here.)
The real reason I don't want to do a straight depth number is that 'qemu-img map' combined with x-dirty-bitmap is still a very convenient way to get at bits 0 and 1 (even if it requires decoding). But if we plumb in a way for
bdrv_get_statusto return depth counts (rather than reimplementing the depth count ourselves), I would have no problem with returning a struct:bits 31-4: the depth of the chain
bits 3-2: reserved (to make reading hex values easier...) bits 1-0: tri-state of unalloc, local, or backing
Quoted 7 lines
where it would look like:
0x0000-> unallocated0x0011-> depth 1, local0x0022-> depth 2, from the first backing layer0x0032-> depth 3, from the second backing layer0x0042...
This looks nice too. However I was only bikeshedding so if any of this is hard to do then don't worry too much.
Would like to add support for this map to nbdinfo too :-)
Rich.
Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
Re: [PATCH 2/3] nbd: Add new qemu:allocation-depth metacontext
In reply to Eric BlakeQuoted 16 lines · Eric Blake
'qemu-img map' provides a way to determine which extents of an image come from the top layer vs. inherited from a backing chain. This is useful information worth exposing over NBD. There is a proposal to add a QMP command block-dirty-bitmap-populate which can create a dirty bitmap that reflects allocation information, at which point qemu:dirty-bitmap:NAME can expose that information via the creation of a temporary bitmap, but we can shorten the effort by adding a new qemu:allocation-depth context that does the same thing without an intermediate bitmap (this patch does not eliminate the need for that proposal, as it will have other uses as well).
For this patch, I just encoded a tri-state value (unallocated, from this layer, from any of the backing layers); we could instead or in addition report an actual depth count per extent, if that proves more useful.
Quoted 4 lines
Note that this patch does not actually enable any way to request a server to enable this context; that will come in the next patch.
Signed-off-by Eric Blake <[email protected]>
Looks good to me overall, need to rebase if patch 01 changed (as I propose or in some better way).
Signature · 2 lines
Best regards, Vladimir
PATCH 3/3 nbd: Add 'qemu-nbd -A' to expose allocation depth
In reply to Eric BlakeAllow the server to expose an additional metacontext to be requested by savvy clients. qemu-nbd adds a new option -A to expose the qemu:allocation-depth metacontext through NBD_CMD_BLOCK_STATUS; this can also be set via QMP when using nbd-server-add.
qemu as client can be hacked into viewing this new context by using the now-misnamed x-dirty-bitmap option when creating an NBD blockdev; although it is worth noting the decoding of how such context information will appear in 'qemu-img map --output=json':
NBD_STATE_DEPTH_UNALLOC => "zero":false, "data":true NBD_STATE_DEPTH_LOCAL => "zero":false, "data":false NBD_STATE_DEPTH_BACKING => "zero":true, "data":true
libnbd as client is probably a nicer way to get at the information without having to decipher such hacks in qemu as client. ;)
--- docs/tools/qemu-nbd.rst | 6 ++++ qapi/block-core.json | 7 ++-- include/block/nbd.h | 7 ++-- blockdev-nbd.c | 3 +- nbd/server.c | 8 +++-- qemu-nbd.c | 16 ++++++--- tests/qemu-iotests/309 | 73 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/309.out | 22 ++++++++++++ tests/qemu-iotests/group | 1 + 9 files changed, 129 insertions(+), 14 deletions(-) create mode 100755 tests/qemu-iotests/309 create mode 100644 tests/qemu-iotests/309.out
docs/tools/qemu-nbd.rst +6 -0
@@ -72,6 +72,12 @@ driver options if ``--image-opts`` is specified. Export the disk as read-only. +.. option:: -A, --allocation-depth + + Expose allocation depth information via the + ``qemu:allocation-depth`` context accessible through + NBD_OPT_SET_META_CONTEXT. + .. option:: -B, --bitmap=NAME If *filename* has a qcow2 persistent bitmap *NAME*, expose
qapi/block-core.json +5 -2
@@ -3874,9 +3874,12 @@ # # @tls-creds: TLS credentials ID # -# @x-dirty-bitmap: A "qemu:dirty-bitmap:NAME" string to query in place of +# @x-dirty-bitmap: A metacontext name such as "qemu:dirty-bitmap:NAME" or +# "qemu:allocation-depth" to query in place of the # traditional "base:allocation" block status (see -# NBD_OPT_LIST_META_CONTEXT in the NBD protocol) (since 3.0) +# NBD_OPT_LIST_META_CONTEXT in the NBD protocol; and +# yes, naming this option x-context would have made +# more sense) (since 3.0) # # @reconnect-delay: On an unexpected disconnect, the nbd client tries to # connect again until succeeding or encountering a serious
include/block/nbd.h +4 -3
@@ -336,9 +336,10 @@ typedef struct NBDClient NBDClient;NBDExport *nbd_export_new (BlockDriverState *bs,uint64_t dev_offset,uint64_t size,const char *name,const char *desc, -const char *bitmap,bool readonly,bool shared, -void (*close)(NBDExport *),bool writethrough, -BlockBackend *on_eject_blk,Error **errp); +bool alloc_depth,const char *bitmap,bool readonly, +bool shared,void (*close)(NBDExport *), +bool writethrough,BlockBackend *on_eject_blk, +Error **errp);void nbd_export_close (NBDExport *exp);void nbd_export_remove (NBDExport *exp,NbdServerRemoveMode mode,Error **errp);void nbd_export_get (NBDExport *exp);
blockdev-nbd.c +2 -1
@@ -203,7 +203,8 @@ void qmp_nbd_server_add(BlockExportNbd *arg, Error **errp) arg->writable = false; } - exp =nbd_export_new (bs,0 , len, arg->name, arg->description, arg->bitmap, + exp =nbd_export_new (bs,0 , len, arg->name, arg->description, + arg->alloc, arg->bitmap, !arg->writable, !arg->writable,NULL , false, on_eject_blk, errp);if (!exp) {
nbd/server.c +5 -3
@@ -1450,9 +1450,10 @@ static void nbd_eject_notifier(Notifier *n, void *data)NBDExport *nbd_export_new (BlockDriverState *bs,uint64_t dev_offset,uint64_t size,const char *name,const char *desc, -const char *bitmap,bool readonly,bool shared, -void (*close)(NBDExport *),bool writethrough, -BlockBackend *on_eject_blk,Error **errp) +bool alloc_depth,const char *bitmap,bool readonly, +bool shared,void (*close)(NBDExport *), +bool writethrough,BlockBackend *on_eject_blk, +Error **errp) {AioContext *ctx;BlockBackend *blk;
@@ -1545,6 +1546,7 @@ NBDExport *nbd_export_new(BlockDriverState *bs, uint64_t dev_offset,assert (strlen (exp->export_bitmap_context) <NBD_MAX_STRING_SIZE ); } + exp->alloc_context = alloc_depth; exp->close = close; exp->ctx = ctx;blk_add_aio_context_notifier (blk, blk_aio_attached, blk_aio_detach, exp);
qemu-nbd.c +11 -5
@@ -100,6 +100,7 @@ static void usage(const char *name)"\n" "Exposing part of the image:\n" " -o, --offset=OFFSET offset into the image\n" +" -A, --allocation-depth expose the allocation depth\n" " -B, --bitmap=NAME expose a persistent dirty bitmap\n" "\n" "General purpose options:\n"
@@ -527,7 +528,7 @@ int main(int argc, char **argv)int64_t fd_size;QemuOpts *sn_opts =NULL ;const char *sn_id_or_name =NULL ; -const char *sopt ="hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:B:L" ; +const char *sopt ="hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L" ;struct option lopt[] = { {"help" , no_argument,NULL ,'h' }, {"version" , no_argument,NULL ,'V' },
@@ -536,6 +537,7 @@ int main(int argc, char **argv) { "socket", required_argument, NULL, 'k' }, { "offset", required_argument, NULL, 'o' }, { "read-only", no_argument, NULL, 'r' }, + { "allocation-depth", no_argument, NULL, 'A' }, { "bitmap", required_argument, NULL, 'B' }, { "connect", required_argument, NULL, 'c' }, { "disconnect", no_argument, NULL, 'd' },
@@ -577,6 +579,7 @@ int main(int argc, char **argv)QDict *options =NULL ;const char *export_name =NULL ;/* defaults to "" later for server mode */ const char *export_description =NULL ; +bool alloc_depth = false;const char *bitmap =NULL ;const char *tlscredsid =NULL ;bool imageOpts = false;
@@ -700,6 +703,9 @@ int main(int argc, char **argv) readonly = true; flags &= ~BDRV_O_RDWR ;break ; +case 'A' : + alloc_depth = true; +break ;case 'B' : bitmap = optarg;break ;
@@ -797,8 +803,8 @@ int main(int argc, char **argv)exit (EXIT_FAILURE ); }if (export_name || export_description || dev_offset || - device || disconnect || fmt || sn_id_or_name || bitmap || - seen_aio || seen_discard || seen_cache) { + device || disconnect || fmt || sn_id_or_name || alloc_depth || + bitmap || seen_aio || seen_discard || seen_cache) {error_report ("List mode is incompatible with per-device settings" );exit (EXIT_FAILURE ); }
@@ -1069,8 +1075,8 @@ int main(int argc, char **argv) fd_size -= dev_offset; export =nbd_export_new (bs, dev_offset, fd_size, export_name, - export_description, bitmap, readonly, shared >1 , - nbd_export_closed, writethrough,NULL , + export_description, alloc_depth, bitmap, readonly, + shared >1 , nbd_export_closed, writethrough,NULL , &error_fatal);if (device) {
tests/qemu-iotests/309 new file +73 -0
73 lines not on this page · raw message
tests/qemu-iotests/309.out new file +22 -0
@@ -0,0 +1,22 @@ +QA output created by 309 + +=== Initial image setup === + +Formatting 'TEST_DIR/t.IMGFMT.base', fmt=IMGFMT size=4194304 +wrote 2097152/2097152 bytes at offset 0 +2 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=4194304 backing_file=TEST_DIR/t.IMGFMT.base backing_fmt=IMGFMT +wrote 2097152/2097152 bytes at offset 1048576 +2 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +=== Check allocation over NBD === + +[{ "start": 0, "length": 1048576, "depth": 1, "zero": false, "data": true, "offset": 327680}, +{ "start": 1048576, "length": 2097152, "depth": 0, "zero": false, "data": true, "offset": 327680}, +{ "start": 3145728, "length": 1048576, "depth": 1, "zero": true, "data": false}] +[{ "start": 0, "length": 3145728, "depth": 0, "zero": false, "data": true, "offset": OFFSET}, +{ "start": 3145728, "length": 1048576, "depth": 0, "zero": true, "data": false, "offset": OFFSET}] +[{ "start": 0, "length": 1048576, "depth": 0, "zero": true, "data": true, "offset": OFFSET}, +{ "start": 1048576, "length": 2097152, "depth": 0, "zero": false, "data": false}, +{ "start": 3145728, "length": 1048576, "depth": 0, "zero": false, "data": true, "offset": OFFSET}] +*** done
tests/qemu-iotests/group +1 -0
@@ -314,3 +314,4 @@ 303 rw quick 304 rw quick 305 rw quick +309 rw auto quick
Signature · 1 line
2.28.0
Re: [PATCH 3/3] nbd: Add 'qemu-nbd -A' to expose allocation depth
Quoted 10 lines · Eric Blake
Allow the server to expose an additional metacontext to be requested by savvy clients. qemu-nbd adds a new option -A to expose the qemu:allocation-depth metacontext through
NBD_CMD_BLOCK_STATUS; this can also be set via QMP when using nbd-server-add.qemu as client can be hacked into viewing this new context by using the now-misnamed x-dirty-bitmap option when creating an NBD blockdev; although it is worth noting the decoding of how such context information will appear in 'qemu-img map --output=json':
Quoted 6 lines
NBD_STATE_DEPTH_UNALLOC=> "zero":false, "data":trueNBD_STATE_DEPTH_LOCAL=> "zero":false, "data":falseNBD_STATE_DEPTH_BACKING=> "zero":true, "data":truelibnbd as client is probably a nicer way to get at the information without having to decipher such hacks in qemu as client. ;)
I've been meaning to add extents information to nbdinfo, or perhaps a new tool ("nbdmap").
Rich.
Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
Re: [PATCH 3/3] nbd: Add 'qemu-nbd -A' to expose allocation depth
In reply to Eric BlakeQuoted 7 lines · Eric Blake
Allow the server to expose an additional metacontext to be requested by savvy clients. qemu-nbd adds a new option -A to expose the qemu:allocation-depth metacontext through
NBD_CMD_BLOCK_STATUS; this can also be set via QMP when using nbd-server-add.qemu as client can be hacked into viewing this new context by using the now-misnamed x-dirty-bitmap option when creating an NBD blockdev;
may be rename it to x-block-status ?
Quoted 6 lines
although it is worth noting the decoding of how such context information will appear in 'qemu-img map --output=json':
NBD_STATE_DEPTH_UNALLOC=> "zero":false, "data":trueNBD_STATE_DEPTH_LOCAL=> "zero":false, "data":falseNBD_STATE_DEPTH_BACKING=> "zero":true, "data":true
It wouldn't be so simple if we decide to export exact depth number..
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 3/3] nbd: Add 'qemu-nbd -A' to expose allocation depth
Quoted 10 lines · Vladimir Sementsov-Ogievskiy
Quoted 7 lines · Eric Blake
Allow the server to expose an additional metacontext to be requested by savvy clients. qemu-nbd adds a new option -A to expose the qemu:allocation-depth metacontext through
NBD_CMD_BLOCK_STATUS; this can also be set via QMP when using nbd-server-add.qemu as client can be hacked into viewing this new context by using the now-misnamed x-dirty-bitmap option when creating an NBD blockdev;
may be rename it to x-block-status ?
I thought about it. We don't promise any back-compat with x- prefix, but at the same time, if there's not a strong reason to change it, scripts written against older qemu will not break as long as we still have the hack.
Quoted 9 lines
Quoted 7 lines
although it is worth noting the decoding of how such context information will appear in 'qemu-img map --output=json':
NBD_STATE_DEPTH_UNALLOC=> "zero":false, "data":trueNBD_STATE_DEPTH_LOCAL=> "zero":false, "data":falseNBD_STATE_DEPTH_BACKING=> "zero":true, "data":trueIt wouldn't be so simple if we decide to export exact depth number..
It's still simple to do tri-state results if we separate the exact depth number to bits 31-4, leaving bits 1-0 as the tri-state summary...
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org