qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@redhat.com>
To: Viktor Prutyanov <viktor.prutyanov@phystech.edu>,
	kwolf@redhat.com, hreitz@redhat.com, sw@weilnetz.de,
	qemu-block@nongnu.org, qemu-devel@nongnu.org
Subject: Re: [PATCH] block/file-win32: add reopen handlers
Date: Tue, 17 Aug 2021 12:23:09 +0200	[thread overview]
Message-ID: <9ac4fad6-d328-36e7-6d6c-d772150c19a8@redhat.com> (raw)
In-Reply-To: <20210817101251.28438-1-viktor.prutyanov@phystech.edu>

Hi Viktor,

Thanks for the patch :)

On 8/17/21 12:12 PM, Viktor Prutyanov wrote:
> Make 'qemu-img commit' work on Windows.
> 
> Command 'commit' requires reopening backing file in RW mode. So,
> add reopen prepare/commit/abort handlers and change dwShareMode
> for CreateFile call in order to allow further read/write reopening.
> 
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/418
> Suggested-by: Hanna Reitz <hreitz@redhat.com>
> Signed-off-by: Viktor Prutyanov <viktor.prutyanov@phystech.edu>
> ---
>  block/file-win32.c | 85 +++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 84 insertions(+), 1 deletion(-)
> 
> diff --git a/block/file-win32.c b/block/file-win32.c
> index 2642088bd6..e44878e6be 100644
> --- a/block/file-win32.c
> +++ b/block/file-win32.c
> @@ -58,6 +58,10 @@ typedef struct BDRVRawState {
>      QEMUWin32AIOState *aio;
>  } BDRVRawState;
>  
> +typedef struct BDRVRawReopenState {
> +    HANDLE hfile;
> +} BDRVRawReopenState;
> +
>  /*
>   * Read/writes the data to/from a given linear buffer.
>   *
> @@ -392,7 +396,7 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags,
>      }
>  
>      s->hfile = CreateFile(filename, access_flags,
> -                          FILE_SHARE_READ, NULL,
> +                          FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
>                            OPEN_EXISTING, overlapped, NULL);
>      if (s->hfile == INVALID_HANDLE_VALUE) {
>          int err = GetLastError();
> @@ -634,6 +638,81 @@ static int coroutine_fn raw_co_create_opts(BlockDriver *drv,
>      return raw_co_create(&options, errp);
>  }
>  
> +static int raw_reopen_prepare(BDRVReopenState *state,
> +        BlockReopenQueue *queue, Error **errp)

Invalid style alignment.

> +{
> +    BDRVRawState *s = state->bs->opaque;
> +    BDRVRawReopenState *rs;
> +    int access_flags;
> +    DWORD overlapped;
> +    int ret = 0;
> +
> +    rs = state->opaque = g_new0(BDRVRawReopenState, 1);
> +
> +    raw_parse_flags(state->flags, s->aio != NULL, &access_flags, &overlapped);
> +    rs->hfile = CreateFile(state->bs->filename, access_flags,
> +                           FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
> +                           OPEN_EXISTING, overlapped, NULL);
> +
> +    if (rs->hfile == INVALID_HANDLE_VALUE) {
> +        int err = GetLastError();
> +
> +        error_setg_win32(errp, err, "Could not reopen '%s'",
> +                         state->bs->filename);
> +        if (err == ERROR_ACCESS_DENIED) {
> +            ret = -EACCES;
> +        } else {
> +            ret = -EINVAL;
> +        }
> +        goto fail;
> +    }
> +
> +    if (s->aio) {
> +        ret = win32_aio_attach(s->aio, rs->hfile);
> +        if (ret < 0) {
> +            error_setg_errno(errp, -ret, "Could not enable AIO");
> +            goto fail;
> +        }
> +    }
To avoid non-NULL value in state->opaque on failure,
I'd use in prologue:

       rs = g_new0(BDRVRawReopenState, 1);

and here:

       state->opaque = rs;

> +    return 0;
> +
> +fail:

Shouldn't we free state->opaque on failure? (I'm not sure because
I don't know well the bdrv_reopen_prepare handler).

> +    return ret;
> +}
> +
> +static void raw_reopen_commit(BDRVReopenState *state)
> +{
> +    BDRVRawState *s = state->bs->opaque;
> +    BDRVRawReopenState *rs = state->opaque;
> +
> +    if (!rs) {

Indeed, raw_reopen_prepare() could let state->opaque non-NULL.

> +        return;
> +    }
> +
> +    CloseHandle(s->hfile);
> +    s->hfile = rs->hfile;
> +
> +    g_free(rs);
> +    state->opaque = NULL;
> +}
> +
> +static void raw_reopen_abort(BDRVReopenState *state)
> +{
> +    BDRVRawReopenState *rs = state->opaque;
> +
> +    if (!rs) {
> +        return;
> +    }
> +
> +    if (rs->hfile != INVALID_HANDLE_VALUE) {
> +        CloseHandle(rs->hfile);
> +    }
> +
> +    g_free(rs);
> +    state->opaque = NULL;
> +}
> +
>  static QemuOptsList raw_create_opts = {
>      .name = "raw-create-opts",
>      .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
> @@ -659,6 +738,10 @@ BlockDriver bdrv_file = {
>      .bdrv_co_create_opts = raw_co_create_opts,
>      .bdrv_has_zero_init = bdrv_has_zero_init_1,
>  
> +    .bdrv_reopen_prepare = raw_reopen_prepare,
> +    .bdrv_reopen_commit  = raw_reopen_commit,
> +    .bdrv_reopen_abort   = raw_reopen_abort,
> +
>      .bdrv_aio_preadv    = raw_aio_preadv,
>      .bdrv_aio_pwritev   = raw_aio_pwritev,
>      .bdrv_aio_flush     = raw_aio_flush,
> 



      reply	other threads:[~2021-08-17 10:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-17 10:12 [PATCH] block/file-win32: add reopen handlers Viktor Prutyanov
2021-08-17 10:23 ` Philippe Mathieu-Daudé [this message]

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=9ac4fad6-d328-36e7-6d6c-d772150c19a8@redhat.com \
    --to=philmd@redhat.com \
    --cc=hreitz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sw@weilnetz.de \
    --cc=viktor.prutyanov@phystech.edu \
    /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).