#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qemu/cutils.h"
+#include "block/block-io.h"
#include "block/block_int.h"
#include "qemu/module.h"
#include "qemu/option.h"
QEMUWin32AIOState *aio;
} BDRVRawState;
+typedef struct BDRVRawReopenState {
+ HANDLE hfile;
+} BDRVRawReopenState;
+
/*
* Read/writes the data to/from a given linear buffer.
*
}
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();
}
static BlockAIOCB *raw_aio_preadv(BlockDriverState *bs,
- uint64_t offset, uint64_t bytes,
- QEMUIOVector *qiov, int flags,
+ int64_t offset, int64_t bytes,
+ QEMUIOVector *qiov, BdrvRequestFlags flags,
BlockCompletionFunc *cb, void *opaque)
{
BDRVRawState *s = bs->opaque;
}
static BlockAIOCB *raw_aio_pwritev(BlockDriverState *bs,
- uint64_t offset, uint64_t bytes,
- QEMUIOVector *qiov, int flags,
+ int64_t offset, int64_t bytes,
+ QEMUIOVector *qiov, BdrvRequestFlags flags,
BlockCompletionFunc *cb, void *opaque)
{
BDRVRawState *s = bs->opaque;
return 0;
}
-static int64_t raw_getlength(BlockDriverState *bs)
+static int64_t coroutine_fn raw_co_getlength(BlockDriverState *bs)
{
BDRVRawState *s = bs->opaque;
LARGE_INTEGER l;
return l.QuadPart;
}
-static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
+static int64_t coroutine_fn raw_co_get_allocated_file_size(BlockDriverState *bs)
{
typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
DWORD * high);
return -EINVAL;
}
- fd = qemu_open(file_opts->filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
- 0644);
+ fd = qemu_create(file_opts->filename, O_WRONLY | O_TRUNC | O_BINARY,
+ 0644, errp);
if (fd < 0) {
- error_setg_errno(errp, errno, "Could not create file");
return -EIO;
}
set_sparse(fd);
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),
.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,
.bdrv_co_truncate = raw_co_truncate,
- .bdrv_getlength = raw_getlength,
- .bdrv_get_allocated_file_size
- = raw_get_allocated_file_size,
+ .bdrv_co_getlength = raw_co_getlength,
+ .bdrv_co_get_allocated_file_size
+ = raw_co_get_allocated_file_size,
.create_opts = &raw_create_opts,
};
.bdrv_detach_aio_context = raw_detach_aio_context,
.bdrv_attach_aio_context = raw_attach_aio_context,
- .bdrv_getlength = raw_getlength,
- .has_variable_length = true,
-
- .bdrv_get_allocated_file_size
- = raw_get_allocated_file_size,
+ .bdrv_co_getlength = raw_co_getlength,
+ .has_variable_length = true,
+ .bdrv_co_get_allocated_file_size = raw_co_get_allocated_file_size,
};
static void bdrv_file_init(void)