* [PATCH v5] block/file-win32: add reopen handlers
@ 2021-08-25 17:36 Viktor Prutyanov
2021-09-01 11:27 ` Hanna Reitz
0 siblings, 1 reply; 2+ messages in thread
From: Viktor Prutyanov @ 2021-08-25 17:36 UTC (permalink / raw)
To: philmd, kwolf, hreitz, sw, yan, qemu-block, qemu-devel
Cc: Helge Konetzka, Viktor Prutyanov
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>
Tested-by: Helge Konetzka <hk@zapateado.de>
---
v2:
- fix indentation in raw_reopen_prepare
- free rs if raw_reopen_prepare fails
v3:
- restore suggested-by field missed in v2
v4:
- add file type check
- add comment about options
- replace rs check with assert in raw_reopen_commit
v5:
- add CloseHandle at AIO attach fail
block/file-win32.c | 101 ++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 100 insertions(+), 1 deletion(-)
diff --git a/block/file-win32.c b/block/file-win32.c
index 2642088bd6..b97c58d642 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,97 @@ 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)
+{
+ BDRVRawState *s = state->bs->opaque;
+ BDRVRawReopenState *rs;
+ int access_flags;
+ DWORD overlapped;
+ int ret = 0;
+
+ if (s->type != FTYPE_FILE) {
+ error_setg(errp, "Can only reopen files");
+ return -EINVAL;
+ }
+
+ rs = g_new0(BDRVRawReopenState, 1);
+
+ /*
+ * We do not support changing any options (only flags). By leaving
+ * all options in state->options, we tell the generic reopen code
+ * that we do not support changing any of them, so it will verify
+ * that their values did not change.
+ */
+
+ 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");
+ CloseHandle(rs->hfile);
+ goto fail;
+ }
+ }
+
+ state->opaque = rs;
+
+ return 0;
+
+fail:
+ g_free(rs);
+ state->opaque = NULL;
+
+ return ret;
+}
+
+static void raw_reopen_commit(BDRVReopenState *state)
+{
+ BDRVRawState *s = state->bs->opaque;
+ BDRVRawReopenState *rs = state->opaque;
+
+ assert(rs != NULL);
+
+ 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 +754,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,
--
2.21.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v5] block/file-win32: add reopen handlers
2021-08-25 17:36 [PATCH v5] block/file-win32: add reopen handlers Viktor Prutyanov
@ 2021-09-01 11:27 ` Hanna Reitz
0 siblings, 0 replies; 2+ messages in thread
From: Hanna Reitz @ 2021-09-01 11:27 UTC (permalink / raw)
To: Viktor Prutyanov, philmd, kwolf, sw, yan, qemu-block, qemu-devel
Cc: Helge Konetzka
On 25.08.21 19:36, 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>
> Tested-by: Helge Konetzka <hk@zapateado.de>
> ---
> v2:
> - fix indentation in raw_reopen_prepare
> - free rs if raw_reopen_prepare fails
> v3:
> - restore suggested-by field missed in v2
> v4:
> - add file type check
> - add comment about options
> - replace rs check with assert in raw_reopen_commit
> v5:
> - add CloseHandle at AIO attach fail
>
> block/file-win32.c | 101 ++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 100 insertions(+), 1 deletion(-)
Thanks! I’ve applied this patch to my block branch:
https://github.com/XanClic/qemu/commits/block
Hanna
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-09-01 11:33 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-25 17:36 [PATCH v5] block/file-win32: add reopen handlers Viktor Prutyanov
2021-09-01 11:27 ` Hanna Reitz
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.