From: Eric Blake <eblake@redhat.com>
To: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
qemu-block@nongnu.org
Cc: kwolf@redhat.com, fam@euphon.net, qemu-devel@nongnu.org,
mreitz@redhat.com, den@openvz.org, jsnow@redhat.com
Subject: Re: [PATCH 08/10] nbd/server: introduce NBDExtentArray
Date: Wed, 9 Oct 2019 12:02:13 -0500 [thread overview]
Message-ID: <c56a7e0c-50df-1ad7-6c6e-d4c3fe52132f@redhat.com> (raw)
In-Reply-To: <20190930151502.7829-9-vsementsov@virtuozzo.com>
On 9/30/19 10:15 AM, Vladimir Sementsov-Ogievskiy wrote:
> Introduce NBDExtentArray class, to handle extents list creation in more
> controlled way and with less OUT parameters in functions.
>
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> ---
> nbd/server.c | 184 +++++++++++++++++++++++++--------------------------
> 1 file changed, 90 insertions(+), 94 deletions(-)
>
> +static void nbd_extent_array_free(NBDExtentArray *ea)
> +{
> + g_free(ea->extents);
> + g_free(ea);
> +}
> +G_DEFINE_AUTOPTR_CLEANUP_FUNC(NBDExtentArray, nbd_extent_array_free);
Nice to see this getting more popular :)
> +
> +static int nbd_extent_array_add(NBDExtentArray *ea,
> + uint32_t length, uint32_t flags)
> {
> - assert(*nb_extents);
> - while (remaining_bytes) {
> + if (ea->count >= ea->nb_alloc) {
> + return -1;
> + }
Returning -1 is not a failure in the protocol, just failure to add any
more information to the reply. A function comment might help, but this
looks like a good helper function.
> +static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset,
> + uint64_t bytes, NBDExtentArray *ea)
> +{
> + while (bytes) {
> uint32_t flags;
> int64_t num;
> - int ret = bdrv_block_status_above(bs, NULL, offset, remaining_bytes,
> - &num, NULL, NULL);
> + int ret = bdrv_block_status_above(bs, NULL, offset, bytes, &num,
> + NULL, NULL);
>
> + if (nbd_extent_array_add(ea, num, flags) < 0) {
> + return 0;
> }
> - offset += num;
> - remaining_bytes -= num;
> - }
>
> - extents_end = extent + 1;
> -
> - for (extent = extents; extent < extents_end; extent++) {
> - extent->flags = cpu_to_be32(extent->flags);
> - extent->length = cpu_to_be32(extent->length);
> + offset += num;
> + bytes -= num;
> }
>
> - *bytes -= remaining_bytes;
> - *nb_extents = extents_end - extents;
> -
> return 0;
Also looks good (return 0 if we populated until we either ran out of
reply space or out of bytes to report on).
> static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
> - NBDExtent *extents, unsigned int nb_extents,
> - uint64_t length, bool last,
> - uint32_t context_id, Error **errp)
> + NBDExtentArray *ea,
> + bool last, uint32_t context_id, Error **errp)
> {
> NBDStructuredMeta chunk;
> -
> + size_t len = ea->count * sizeof(ea->extents[0]);
> + g_autofree NBDExtent *extents = g_memdup(ea->extents, len);
Why do we need memdup here? What's wrong with modifying ea->extents in
place?...
> + NBDExtent *extent, *extents_end = extents + ea->count;
> struct iovec iov[] = {
> {.iov_base = &chunk, .iov_len = sizeof(chunk)},
> - {.iov_base = extents, .iov_len = nb_extents * sizeof(extents[0])}
> + {.iov_base = extents, .iov_len = len}
> };
>
> - trace_nbd_co_send_extents(handle, nb_extents, context_id, length, last);
> + for (extent = extents; extent < extents_end; extent++) {
> + extent->flags = cpu_to_be32(extent->flags);
> + extent->length = cpu_to_be32(extent->length);
> + }
> +
> + trace_nbd_co_send_extents(handle, ea->count, context_id, ea->total_length,
> + last);
> set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0,
> NBD_REPLY_TYPE_BLOCK_STATUS,
> handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
> @@ -1994,39 +2012,27 @@ static int nbd_co_send_block_status(NBDClient *client, uint64_t handle,
> {
> int ret;
> unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
> - NBDExtent *extents = g_new(NBDExtent, nb_extents);
> - uint64_t final_length = length;
> + g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
>
> - ret = blockstatus_to_extents(bs, offset, &final_length, extents,
> - &nb_extents);
> + ret = blockstatus_to_extents(bs, offset, length, ea);
> if (ret < 0) {
> - g_free(extents);
> return nbd_co_send_structured_error(
> client, handle, -ret, "can't get block status", errp);
> }
>
> - ret = nbd_co_send_extents(client, handle, extents, nb_extents,
> - final_length, last, context_id, errp);
> -
> - g_free(extents);
> -
> - return ret;
> + return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
...especially since ea goes out of scope right after the helper function
finishes?
Overall looks like a nice refactoring.
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
next prev parent reply other threads:[~2019-10-09 20:06 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-30 15:14 [PATCH 00/10] Further bitmaps improvements Vladimir Sementsov-Ogievskiy
2019-09-30 15:14 ` [PATCH 01/10] hbitmap: introduce HBITMAP_MAX_ORIG_SIZE Vladimir Sementsov-Ogievskiy
2019-10-09 15:34 ` Eric Blake
2019-10-09 16:04 ` Vladimir Sementsov-Ogievskiy
2019-09-30 15:14 ` [PATCH 02/10] hbitmap: move hbitmap_iter_next_word to hbitmap.c Vladimir Sementsov-Ogievskiy
2019-09-30 15:14 ` [PATCH 03/10] hbitmap: unpublish hbitmap_iter_skip_words Vladimir Sementsov-Ogievskiy
2019-09-30 15:14 ` [PATCH 04/10] hbitmap: drop meta bitmaps as they are unused Vladimir Sementsov-Ogievskiy
2019-09-30 15:14 ` [PATCH 05/10] block/dirty-bitmap: switch _next_dirty_area and _next_zero to int64_t Vladimir Sementsov-Ogievskiy
2019-09-30 15:14 ` [PATCH 06/10] block/dirty-bitmap: add _next_dirty API Vladimir Sementsov-Ogievskiy
2019-09-30 15:14 ` [PATCH 07/10] block/dirty-bitmap: improve _next_dirty_area API Vladimir Sementsov-Ogievskiy
2019-09-30 15:15 ` [PATCH 08/10] nbd/server: introduce NBDExtentArray Vladimir Sementsov-Ogievskiy
2019-10-09 17:02 ` Eric Blake [this message]
2019-10-18 16:07 ` Vladimir Sementsov-Ogievskiy
2019-10-18 16:34 ` Eric Blake
2019-09-30 15:15 ` [PATCH 09/10] nbd/server: use bdrv_dirty_bitmap_next_dirty_area Vladimir Sementsov-Ogievskiy
2019-10-09 18:26 ` Eric Blake
2019-09-30 15:15 ` [PATCH 10/10] block/qcow2-bitmap: use bdrv_dirty_bitmap_next_dirty Vladimir Sementsov-Ogievskiy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c56a7e0c-50df-1ad7-6c6e-d4c3fe52132f@redhat.com \
--to=eblake@redhat.com \
--cc=den@openvz.org \
--cc=fam@euphon.net \
--cc=jsnow@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=vsementsov@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).