Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
51e8c2d3b5 | ||
|
|
7b5d7ee8a8 | ||
|
|
c8d42adbf6 |
No files matched your search
+373
-165
@@ -25,6 +25,8 @@
|
||||
#include "qemu/osdep.h"
|
||||
#include "qapi/error.h"
|
||||
#include "qemu/cutils.h"
|
||||
#include "qemu/host-utils.h"
|
||||
#include "qemu/memalign.h"
|
||||
#include "block/block-io.h"
|
||||
#include "block/block_int.h"
|
||||
#include "qemu/module.h"
|
||||
@@ -43,12 +45,11 @@
|
||||
#define FTYPE_HARDDISK 2
|
||||
|
||||
typedef struct RawWin32AIOData {
|
||||
BlockDriverState *bs;
|
||||
HANDLE hfile;
|
||||
struct iovec *aio_iov;
|
||||
int aio_niov;
|
||||
QEMUIOVector *qiov;
|
||||
void *bounce_buf;
|
||||
size_t aio_nbytes;
|
||||
off64_t aio_offset;
|
||||
int64_t aio_offset;
|
||||
int aio_type;
|
||||
} RawWin32AIOData;
|
||||
|
||||
@@ -57,85 +58,117 @@ typedef struct BDRVRawState {
|
||||
int type;
|
||||
char drive_path[16]; /* format: "d:\" */
|
||||
QEMUWin32AIOState *aio;
|
||||
bool needs_alignment;
|
||||
uint32_t probed_req_align;
|
||||
uint32_t probed_mem_align;
|
||||
} BDRVRawState;
|
||||
|
||||
typedef struct BDRVRawReopenState {
|
||||
HANDLE hfile;
|
||||
bool needs_alignment;
|
||||
uint32_t probed_req_align;
|
||||
uint32_t probed_mem_align;
|
||||
} BDRVRawReopenState;
|
||||
|
||||
/*
|
||||
* Read/writes the data to/from a given linear buffer.
|
||||
*
|
||||
* Returns the number of bytes handles or -errno in case of an error. Short
|
||||
* reads are only returned if the end of the file is reached.
|
||||
*/
|
||||
static size_t handle_aiocb_rw(RawWin32AIOData *aiocb)
|
||||
static int win32_error_to_errno(DWORD err)
|
||||
{
|
||||
size_t offset = 0;
|
||||
int i;
|
||||
switch (err) {
|
||||
case ERROR_DISK_FULL:
|
||||
case ERROR_HANDLE_DISK_FULL:
|
||||
return -ENOSPC;
|
||||
case ERROR_ACCESS_DENIED:
|
||||
return -EACCES;
|
||||
case ERROR_INVALID_PARAMETER:
|
||||
return -EINVAL;
|
||||
case ERROR_OPERATION_ABORTED:
|
||||
return -ECANCELED;
|
||||
case ERROR_NOT_ENOUGH_MEMORY:
|
||||
case ERROR_OUTOFMEMORY:
|
||||
return -ENOMEM;
|
||||
default:
|
||||
return -EIO;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < aiocb->aio_niov; i++) {
|
||||
OVERLAPPED ov;
|
||||
DWORD ret, ret_count, len;
|
||||
static int handle_aiocb_rw(RawWin32AIOData *aiocb, size_t *transferred)
|
||||
{
|
||||
QEMUIOVector *qiov = aiocb->qiov;
|
||||
struct iovec bounce_iov = {
|
||||
.iov_base = aiocb->bounce_buf,
|
||||
.iov_len = aiocb->aio_nbytes,
|
||||
};
|
||||
const struct iovec *iov = aiocb->bounce_buf ? &bounce_iov : qiov->iov;
|
||||
int niov = aiocb->bounce_buf ? 1 : qiov->niov;
|
||||
bool is_read = (aiocb->aio_type & QEMU_AIO_TYPE_MASK) == QEMU_AIO_READ;
|
||||
|
||||
memset(&ov, 0, sizeof(ov));
|
||||
ov.Offset = (aiocb->aio_offset + offset);
|
||||
ov.OffsetHigh = (aiocb->aio_offset + offset) >> 32;
|
||||
len = aiocb->aio_iov[i].iov_len;
|
||||
if (aiocb->aio_type & QEMU_AIO_WRITE) {
|
||||
ret = WriteFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
|
||||
len, &ret_count, &ov);
|
||||
*transferred = 0;
|
||||
for (int i = 0; i < niov; i++) {
|
||||
uint64_t offset = aiocb->aio_offset + *transferred;
|
||||
OVERLAPPED ov = {
|
||||
.Offset = (DWORD)offset,
|
||||
.OffsetHigh = (DWORD)(offset >> 32),
|
||||
};
|
||||
DWORD len = (DWORD)iov[i].iov_len;
|
||||
DWORD count;
|
||||
BOOL ok;
|
||||
|
||||
if (len == 0) {
|
||||
continue;
|
||||
}
|
||||
if (is_read) {
|
||||
ok = ReadFile(aiocb->hfile, iov[i].iov_base, len, &count, &ov);
|
||||
} else {
|
||||
ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
|
||||
len, &ret_count, &ov);
|
||||
ok = WriteFile(aiocb->hfile, iov[i].iov_base, len, &count, &ov);
|
||||
}
|
||||
if (!ret) {
|
||||
ret_count = 0;
|
||||
if (!ok) {
|
||||
DWORD err = GetLastError();
|
||||
|
||||
if (is_read && err == ERROR_HANDLE_EOF) {
|
||||
return 0;
|
||||
}
|
||||
return win32_error_to_errno(err);
|
||||
}
|
||||
if (ret_count != len) {
|
||||
offset += ret_count;
|
||||
if (count > len) {
|
||||
return -EIO;
|
||||
}
|
||||
*transferred += count;
|
||||
if (count < len) {
|
||||
break;
|
||||
}
|
||||
offset += len;
|
||||
}
|
||||
|
||||
return offset;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int aio_worker(void *arg)
|
||||
{
|
||||
RawWin32AIOData *aiocb = arg;
|
||||
ssize_t ret = 0;
|
||||
int ret = 0;
|
||||
size_t count;
|
||||
QEMUIOVector *qiov = aiocb->qiov;
|
||||
|
||||
switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
|
||||
case QEMU_AIO_READ:
|
||||
count = handle_aiocb_rw(aiocb);
|
||||
if (count < aiocb->aio_nbytes) {
|
||||
/* A short read means that we have reached EOF. Pad the buffer
|
||||
* with zeros for bytes after EOF. */
|
||||
iov_memset(aiocb->aio_iov, aiocb->aio_niov, count,
|
||||
0, aiocb->aio_nbytes - count);
|
||||
|
||||
count = aiocb->aio_nbytes;
|
||||
ret = handle_aiocb_rw(aiocb, &count);
|
||||
if (ret < 0) {
|
||||
break;
|
||||
}
|
||||
if (count == aiocb->aio_nbytes) {
|
||||
ret = 0;
|
||||
} else {
|
||||
ret = -EINVAL;
|
||||
if (aiocb->bounce_buf && count) {
|
||||
iov_from_buf(qiov->iov, qiov->niov, 0, aiocb->bounce_buf, count);
|
||||
}
|
||||
if (count < aiocb->aio_nbytes) {
|
||||
qemu_iovec_memset(qiov, count, 0, aiocb->aio_nbytes - count);
|
||||
}
|
||||
break;
|
||||
case QEMU_AIO_WRITE:
|
||||
count = handle_aiocb_rw(aiocb);
|
||||
if (count == aiocb->aio_nbytes) {
|
||||
ret = 0;
|
||||
} else {
|
||||
ret = -EINVAL;
|
||||
ret = handle_aiocb_rw(aiocb, &count);
|
||||
if (ret == 0 && count < aiocb->aio_nbytes) {
|
||||
ret = -EIO;
|
||||
}
|
||||
break;
|
||||
case QEMU_AIO_FLUSH:
|
||||
if (!FlushFileBuffers(aiocb->hfile)) {
|
||||
return -EIO;
|
||||
ret = win32_error_to_errno(GetLastError());
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -144,30 +177,47 @@ static int aio_worker(void *arg)
|
||||
break;
|
||||
}
|
||||
|
||||
g_free(aiocb);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
|
||||
int64_t offset, QEMUIOVector *qiov, int count,
|
||||
BlockCompletionFunc *cb, void *opaque, int type)
|
||||
static int coroutine_fn paio_co_submit(BlockDriverState *bs, HANDLE hfile,
|
||||
int64_t offset, QEMUIOVector *qiov, int64_t bytes, int type)
|
||||
{
|
||||
RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
|
||||
RawWin32AIOData acb = {
|
||||
.hfile = hfile,
|
||||
.aio_type = type,
|
||||
.aio_offset = offset,
|
||||
.qiov = qiov,
|
||||
};
|
||||
int ret;
|
||||
|
||||
acb->bs = bs;
|
||||
acb->hfile = hfile;
|
||||
acb->aio_type = type;
|
||||
|
||||
if (qiov) {
|
||||
acb->aio_iov = qiov->iov;
|
||||
acb->aio_niov = qiov->niov;
|
||||
assert(qiov->size == count);
|
||||
if (bytes < 0 || bytes > BDRV_REQUEST_MAX_BYTES ||
|
||||
(qiov && qiov->size != (uint64_t)bytes)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
acb.aio_nbytes = bytes;
|
||||
if (bytes && (bs->open_flags & BDRV_O_NOCACHE)) {
|
||||
if (!QEMU_IS_ALIGNED(offset, bs->bl.request_alignment) ||
|
||||
!QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
if (qiov->niov > 1 ||
|
||||
!QEMU_PTR_IS_ALIGNED(qiov->iov[0].iov_base,
|
||||
bdrv_min_mem_align(bs))) {
|
||||
acb.bounce_buf = qemu_try_blockalign(bs, bytes);
|
||||
if (!acb.bounce_buf) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (type & QEMU_AIO_WRITE) {
|
||||
iov_to_buf(qiov->iov, qiov->niov, 0, acb.bounce_buf, bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
acb->aio_nbytes = count;
|
||||
acb->aio_offset = offset;
|
||||
|
||||
trace_file_paio_submit(acb, opaque, offset, count, type);
|
||||
return thread_pool_submit_aio(aio_worker, acb, cb, opaque);
|
||||
trace_file_paio_submit(&acb, NULL, offset, (int)acb.aio_nbytes, type);
|
||||
ret = thread_pool_submit_co(aio_worker, &acb);
|
||||
qemu_vfree(acb.bounce_buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int set_sparse(int fd)
|
||||
@@ -196,37 +246,184 @@ static void raw_attach_aio_context(BlockDriverState *bs,
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct RawIoStatusBlock {
|
||||
union {
|
||||
LONG Status;
|
||||
PVOID Pointer;
|
||||
};
|
||||
ULONG_PTR Information;
|
||||
} RawIoStatusBlock;
|
||||
|
||||
typedef struct RawFileFsSizeInfo {
|
||||
LARGE_INTEGER TotalAllocationUnits;
|
||||
LARGE_INTEGER AvailableAllocationUnits;
|
||||
ULONG SectorsPerAllocationUnit;
|
||||
ULONG BytesPerSector;
|
||||
} RawFileFsSizeInfo;
|
||||
|
||||
typedef LONG (NTAPI * nt_query_volume_info_file_t)(HANDLE, RawIoStatusBlock *,
|
||||
PVOID, ULONG, ULONG);
|
||||
|
||||
static bool raw_query_fs_sector_size(HANDLE hfile, uint32_t *logical)
|
||||
{
|
||||
enum { RAW_FILE_FS_SIZE_INFORMATION = 3 };
|
||||
HMODULE ntdll = GetModuleHandleA("ntdll.dll");
|
||||
nt_query_volume_info_file_t query_vol;
|
||||
RawIoStatusBlock iosb = { 0 };
|
||||
RawFileFsSizeInfo info = { 0 };
|
||||
LONG status;
|
||||
|
||||
if (!ntdll) {
|
||||
return false;
|
||||
}
|
||||
query_vol = (nt_query_volume_info_file_t)
|
||||
GetProcAddress(ntdll, "NtQueryVolumeInformationFile");
|
||||
if (!query_vol) {
|
||||
return false;
|
||||
}
|
||||
/* Called before hfile is attached to an IOCP. */
|
||||
status = query_vol(hfile, &iosb, &info, sizeof(info),
|
||||
RAW_FILE_FS_SIZE_INFORMATION);
|
||||
if (status == (LONG)STATUS_PENDING) {
|
||||
if (WaitForSingleObject(hfile, INFINITE) != WAIT_OBJECT_0) {
|
||||
return false;
|
||||
}
|
||||
status = iosb.Status;
|
||||
}
|
||||
if (status < 0 || iosb.Information < sizeof(info)) {
|
||||
return false;
|
||||
}
|
||||
*logical = info.BytesPerSector;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int raw_probe_volume_alignment(HANDLE hfile, const char *filename,
|
||||
uint32_t *logical, uint32_t *physical,
|
||||
Error **errp)
|
||||
{
|
||||
DWORD path_size = GetFinalPathNameByHandleW(hfile, NULL, 0,
|
||||
VOLUME_NAME_GUID);
|
||||
|
||||
if (path_size > 0) {
|
||||
g_autofree wchar_t *path = g_new(wchar_t, path_size);
|
||||
DWORD length = GetFinalPathNameByHandleW(hfile, path, path_size,
|
||||
VOLUME_NAME_GUID);
|
||||
wchar_t *slash = (length > 4 && length < path_size)
|
||||
? wcschr(path + 4, L'\\') : NULL;
|
||||
|
||||
if (slash) {
|
||||
HANDLE volume_handle;
|
||||
|
||||
*slash = L'\0';
|
||||
volume_handle = CreateFileW(path, 0,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
NULL, OPEN_EXISTING, 0, NULL);
|
||||
if (volume_handle != INVALID_HANDLE_VALUE) {
|
||||
STORAGE_PROPERTY_QUERY query = {
|
||||
.PropertyId = StorageAccessAlignmentProperty,
|
||||
.QueryType = PropertyStandardQuery,
|
||||
};
|
||||
STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR info;
|
||||
DWORD returned;
|
||||
BOOL ok = DeviceIoControl(volume_handle,
|
||||
IOCTL_STORAGE_QUERY_PROPERTY,
|
||||
&query, sizeof(query),
|
||||
&info, sizeof(info),
|
||||
&returned, NULL);
|
||||
|
||||
CloseHandle(volume_handle);
|
||||
if (ok && returned >= sizeof(info) &&
|
||||
info.Size >= sizeof(info) &&
|
||||
info.BytesPerLogicalSector >= BDRV_SECTOR_SIZE &&
|
||||
info.BytesPerLogicalSector <= BDRV_MAX_ALIGNMENT &&
|
||||
is_power_of_2(info.BytesPerLogicalSector)) {
|
||||
*logical = info.BytesPerLogicalSector;
|
||||
*physical = info.BytesPerPhysicalSector;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (raw_query_fs_sector_size(hfile, logical)) {
|
||||
*physical = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
error_setg(errp, "Could not query sector size for '%s'", filename);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static int raw_probe_direct_alignment(HANDLE hfile, const char *drive_path,
|
||||
uint32_t *req_align, uint32_t *mem_align,
|
||||
Error **errp)
|
||||
{
|
||||
FILE_STORAGE_INFO fsi;
|
||||
FILE_ALIGNMENT_INFO fai;
|
||||
uint32_t logical, physical;
|
||||
uint64_t file_align = 0;
|
||||
bool have_storage_info;
|
||||
|
||||
have_storage_info = GetFileInformationByHandleEx(hfile, FileStorageInfo,
|
||||
&fsi, sizeof(fsi));
|
||||
if (have_storage_info) {
|
||||
logical = fsi.LogicalBytesPerSector;
|
||||
physical = 0;
|
||||
if (is_power_of_2(fsi.PhysicalBytesPerSectorForAtomicity) &&
|
||||
fsi.PhysicalBytesPerSectorForAtomicity <= BDRV_MAX_ALIGNMENT) {
|
||||
physical = fsi.PhysicalBytesPerSectorForAtomicity;
|
||||
}
|
||||
} else {
|
||||
int ret = raw_probe_volume_alignment(hfile, drive_path, &logical,
|
||||
&physical, errp);
|
||||
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
if (!is_power_of_2(physical) || physical > BDRV_MAX_ALIGNMENT) {
|
||||
physical = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (GetFileInformationByHandleEx(hfile, FileAlignmentInfo,
|
||||
&fai, sizeof(fai))) {
|
||||
file_align = (uint64_t)fai.AlignmentRequirement + 1;
|
||||
if (!is_power_of_2(file_align) || file_align > BDRV_MAX_ALIGNMENT) {
|
||||
error_setg(errp, "Unsupported buffer alignment for '%s'",
|
||||
drive_path);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
if (logical < BDRV_SECTOR_SIZE || logical > BDRV_MAX_ALIGNMENT ||
|
||||
!is_power_of_2(logical)) {
|
||||
error_setg(errp, "Unsupported logical sector size %" PRIu32
|
||||
" for '%s'", logical, drive_path);
|
||||
return -EINVAL;
|
||||
}
|
||||
if (!have_storage_info && !physical) {
|
||||
if (!file_align) {
|
||||
error_setg(errp, "Could not determine direct I/O buffer alignment "
|
||||
"for '%s'", drive_path);
|
||||
return -EINVAL;
|
||||
}
|
||||
physical = qemu_real_host_page_size();
|
||||
}
|
||||
|
||||
*req_align = logical;
|
||||
*mem_align = MAX(logical, MAX(physical, (uint32_t)file_align));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
|
||||
{
|
||||
BDRVRawState *s = bs->opaque;
|
||||
DWORD sectorsPerCluster, freeClusters, totalClusters, count;
|
||||
DISK_GEOMETRY_EX dg;
|
||||
BOOL status;
|
||||
|
||||
if (s->type == FTYPE_CD) {
|
||||
bs->bl.request_alignment = 2048;
|
||||
return;
|
||||
bs->bl.request_alignment = s->needs_alignment ? s->probed_req_align : 1;
|
||||
if (s->needs_alignment) {
|
||||
bs->bl.min_mem_alignment = s->probed_mem_align;
|
||||
bs->bl.opt_mem_alignment = MAX(s->probed_mem_align,
|
||||
qemu_real_host_page_size());
|
||||
}
|
||||
if (s->type == FTYPE_HARDDISK) {
|
||||
status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
||||
NULL, 0, &dg, sizeof(dg), &count, NULL);
|
||||
if (status != 0) {
|
||||
bs->bl.request_alignment = dg.Geometry.BytesPerSector;
|
||||
return;
|
||||
}
|
||||
/* try GetDiskFreeSpace too */
|
||||
}
|
||||
|
||||
if (s->drive_path[0]) {
|
||||
GetDiskFreeSpace(s->drive_path, §orsPerCluster,
|
||||
&dg.Geometry.BytesPerSector,
|
||||
&freeClusters, &totalClusters);
|
||||
bs->bl.request_alignment = dg.Geometry.BytesPerSector;
|
||||
return;
|
||||
}
|
||||
|
||||
/* XXX Does Windows support AIO on less than 512-byte alignment? */
|
||||
bs->bl.request_alignment = 512;
|
||||
}
|
||||
|
||||
static void raw_parse_flags(int flags, bool use_aio, int *access_flags,
|
||||
@@ -351,17 +548,6 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags,
|
||||
|
||||
raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
|
||||
|
||||
if (filename[0] && filename[1] == ':') {
|
||||
snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", filename[0]);
|
||||
} else if (filename[0] == '\\' && filename[1] == '\\') {
|
||||
s->drive_path[0] = 0;
|
||||
} else {
|
||||
/* Relative path. */
|
||||
char buf[MAX_PATH];
|
||||
GetCurrentDirectory(MAX_PATH, buf);
|
||||
snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", buf[0]);
|
||||
}
|
||||
|
||||
s->hfile = CreateFile(filename, access_flags,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
|
||||
OPEN_EXISTING, overlapped, NULL);
|
||||
@@ -377,21 +563,30 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags,
|
||||
goto fail;
|
||||
}
|
||||
|
||||
s->needs_alignment = flags & BDRV_O_NOCACHE;
|
||||
if (s->needs_alignment) {
|
||||
ret = raw_probe_direct_alignment(s->hfile, filename,
|
||||
&s->probed_req_align,
|
||||
&s->probed_mem_align, errp);
|
||||
if (ret < 0) {
|
||||
goto fail_handle;
|
||||
}
|
||||
}
|
||||
|
||||
if (use_aio) {
|
||||
s->aio = win32_aio_init();
|
||||
if (s->aio == NULL) {
|
||||
CloseHandle(s->hfile);
|
||||
error_setg(errp, "Could not initialize AIO");
|
||||
ret = -EINVAL;
|
||||
goto fail;
|
||||
DWORD err = GetLastError();
|
||||
|
||||
error_setg_win32(errp, err, "Could not initialize AIO");
|
||||
ret = win32_error_to_errno(err);
|
||||
goto fail_handle;
|
||||
}
|
||||
|
||||
ret = win32_aio_attach(s->aio, s->hfile);
|
||||
if (ret < 0) {
|
||||
win32_aio_cleanup(s->aio);
|
||||
CloseHandle(s->hfile);
|
||||
error_setg_errno(errp, -ret, "Could not enable AIO");
|
||||
goto fail;
|
||||
error_setg_win32(errp, GetLastError(), "Could not enable AIO");
|
||||
goto fail_handle;
|
||||
}
|
||||
|
||||
win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
|
||||
@@ -400,47 +595,54 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags,
|
||||
/* When extending regular files, we get zeros from the OS */
|
||||
bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
|
||||
|
||||
ret = 0;
|
||||
qemu_opts_del(opts);
|
||||
return 0;
|
||||
|
||||
fail_handle:
|
||||
CloseHandle(s->hfile);
|
||||
s->hfile = INVALID_HANDLE_VALUE;
|
||||
if (s->aio) {
|
||||
win32_aio_cleanup(s->aio);
|
||||
s->aio = NULL;
|
||||
}
|
||||
fail:
|
||||
qemu_opts_del(opts);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static BlockAIOCB *raw_aio_preadv(BlockDriverState *bs,
|
||||
int64_t offset, int64_t bytes,
|
||||
QEMUIOVector *qiov, BdrvRequestFlags flags,
|
||||
BlockCompletionFunc *cb, void *opaque)
|
||||
static int coroutine_fn raw_co_preadv(BlockDriverState *bs,
|
||||
int64_t offset, int64_t bytes,
|
||||
QEMUIOVector *qiov,
|
||||
BdrvRequestFlags flags)
|
||||
{
|
||||
BDRVRawState *s = bs->opaque;
|
||||
|
||||
if (s->aio) {
|
||||
return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
|
||||
cb, opaque, QEMU_AIO_READ);
|
||||
} else {
|
||||
return paio_submit(bs, s->hfile, offset, qiov, bytes,
|
||||
cb, opaque, QEMU_AIO_READ);
|
||||
return win32_aio_co_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
|
||||
QEMU_AIO_READ);
|
||||
}
|
||||
return paio_co_submit(bs, s->hfile, offset, qiov, bytes, QEMU_AIO_READ);
|
||||
}
|
||||
|
||||
static BlockAIOCB *raw_aio_pwritev(BlockDriverState *bs,
|
||||
int64_t offset, int64_t bytes,
|
||||
QEMUIOVector *qiov, BdrvRequestFlags flags,
|
||||
BlockCompletionFunc *cb, void *opaque)
|
||||
static int coroutine_fn raw_co_pwritev(BlockDriverState *bs,
|
||||
int64_t offset, int64_t bytes,
|
||||
QEMUIOVector *qiov,
|
||||
BdrvRequestFlags flags)
|
||||
{
|
||||
BDRVRawState *s = bs->opaque;
|
||||
|
||||
if (s->aio) {
|
||||
return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
|
||||
cb, opaque, QEMU_AIO_WRITE);
|
||||
} else {
|
||||
return paio_submit(bs, s->hfile, offset, qiov, bytes,
|
||||
cb, opaque, QEMU_AIO_WRITE);
|
||||
return win32_aio_co_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
|
||||
QEMU_AIO_WRITE);
|
||||
}
|
||||
return paio_co_submit(bs, s->hfile, offset, qiov, bytes, QEMU_AIO_WRITE);
|
||||
}
|
||||
|
||||
static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
|
||||
BlockCompletionFunc *cb, void *opaque)
|
||||
static int coroutine_fn raw_co_flush_to_disk(BlockDriverState *bs)
|
||||
{
|
||||
BDRVRawState *s = bs->opaque;
|
||||
return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
|
||||
|
||||
return paio_co_submit(bs, s->hfile, 0, NULL, 0, QEMU_AIO_FLUSH);
|
||||
}
|
||||
|
||||
static void raw_close(BlockDriverState *bs)
|
||||
@@ -449,11 +651,12 @@ static void raw_close(BlockDriverState *bs)
|
||||
|
||||
if (s->aio) {
|
||||
win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
|
||||
}
|
||||
CloseHandle(s->hfile);
|
||||
if (s->aio) {
|
||||
win32_aio_cleanup(s->aio);
|
||||
s->aio = NULL;
|
||||
}
|
||||
|
||||
CloseHandle(s->hfile);
|
||||
if (bs->open_flags & BDRV_O_TEMPORARY) {
|
||||
unlink(bs->filename);
|
||||
}
|
||||
@@ -464,8 +667,7 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
|
||||
BdrvRequestFlags flags, Error **errp)
|
||||
{
|
||||
BDRVRawState *s = bs->opaque;
|
||||
LONG low, high;
|
||||
DWORD dwPtrLow;
|
||||
FILE_END_OF_FILE_INFO info = { .EndOfFile.QuadPart = offset };
|
||||
|
||||
if (prealloc != PREALLOC_MODE_OFF) {
|
||||
error_setg(errp, "Unsupported preallocation mode '%s'",
|
||||
@@ -473,21 +675,12 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset,
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
low = offset;
|
||||
high = offset >> 32;
|
||||
if (!SetFileInformationByHandle(s->hfile, FileEndOfFileInfo,
|
||||
&info, sizeof(info))) {
|
||||
DWORD err = GetLastError();
|
||||
|
||||
/*
|
||||
* An error has occurred if the return value is INVALID_SET_FILE_POINTER
|
||||
* and GetLastError doesn't return NO_ERROR.
|
||||
*/
|
||||
dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
|
||||
if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
|
||||
error_setg_win32(errp, GetLastError(), "SetFilePointer error");
|
||||
return -EIO;
|
||||
}
|
||||
if (SetEndOfFile(s->hfile) == 0) {
|
||||
error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
|
||||
return -EIO;
|
||||
error_setg_win32(errp, err, "Could not set end of file");
|
||||
return win32_error_to_errno(err);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -611,7 +804,7 @@ static int raw_reopen_prepare(BDRVReopenState *state,
|
||||
BDRVRawReopenState *rs;
|
||||
int access_flags;
|
||||
DWORD overlapped;
|
||||
int ret = 0;
|
||||
int ret;
|
||||
|
||||
if (s->type != FTYPE_FILE) {
|
||||
error_setg(errp, "Can only reopen files");
|
||||
@@ -645,12 +838,21 @@ static int raw_reopen_prepare(BDRVReopenState *state,
|
||||
goto fail;
|
||||
}
|
||||
|
||||
rs->needs_alignment = state->flags & BDRV_O_NOCACHE;
|
||||
if (rs->needs_alignment) {
|
||||
ret = raw_probe_direct_alignment(rs->hfile, state->bs->filename,
|
||||
&rs->probed_req_align,
|
||||
&rs->probed_mem_align, errp);
|
||||
if (ret < 0) {
|
||||
goto fail_handle;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
error_setg_win32(errp, GetLastError(), "Could not enable AIO");
|
||||
goto fail_handle;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -658,6 +860,8 @@ static int raw_reopen_prepare(BDRVReopenState *state,
|
||||
|
||||
return 0;
|
||||
|
||||
fail_handle:
|
||||
CloseHandle(rs->hfile);
|
||||
fail:
|
||||
g_free(rs);
|
||||
state->opaque = NULL;
|
||||
@@ -674,6 +878,9 @@ static void raw_reopen_commit(BDRVReopenState *state)
|
||||
|
||||
CloseHandle(s->hfile);
|
||||
s->hfile = rs->hfile;
|
||||
s->needs_alignment = rs->needs_alignment;
|
||||
s->probed_req_align = rs->probed_req_align;
|
||||
s->probed_mem_align = rs->probed_mem_align;
|
||||
|
||||
g_free(rs);
|
||||
state->opaque = NULL;
|
||||
@@ -687,9 +894,7 @@ static void raw_reopen_abort(BDRVReopenState *state)
|
||||
return;
|
||||
}
|
||||
|
||||
if (rs->hfile != INVALID_HANDLE_VALUE) {
|
||||
CloseHandle(rs->hfile);
|
||||
}
|
||||
CloseHandle(rs->hfile);
|
||||
|
||||
g_free(rs);
|
||||
state->opaque = NULL;
|
||||
@@ -724,9 +929,12 @@ BlockDriver bdrv_file = {
|
||||
.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_preadv = raw_co_preadv,
|
||||
.bdrv_co_pwritev = raw_co_pwritev,
|
||||
.bdrv_co_flush_to_disk = raw_co_flush_to_disk,
|
||||
|
||||
.bdrv_detach_aio_context = raw_detach_aio_context,
|
||||
.bdrv_attach_aio_context = raw_attach_aio_context,
|
||||
|
||||
.bdrv_co_truncate = raw_co_truncate,
|
||||
.bdrv_co_getlength = raw_co_getlength,
|
||||
@@ -892,9 +1100,9 @@ static BlockDriver bdrv_host_device = {
|
||||
.bdrv_close = raw_close,
|
||||
.bdrv_refresh_limits = hdev_refresh_limits,
|
||||
|
||||
.bdrv_aio_preadv = raw_aio_preadv,
|
||||
.bdrv_aio_pwritev = raw_aio_pwritev,
|
||||
.bdrv_aio_flush = raw_aio_flush,
|
||||
.bdrv_co_preadv = raw_co_preadv,
|
||||
.bdrv_co_pwritev = raw_co_pwritev,
|
||||
.bdrv_co_flush_to_disk = raw_co_flush_to_disk,
|
||||
|
||||
.bdrv_detach_aio_context = raw_detach_aio_context,
|
||||
.bdrv_attach_aio_context = raw_attach_aio_context,
|
||||
|
||||
+257
-152
@@ -23,214 +23,319 @@
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu/timer.h"
|
||||
#include "block/block-io.h"
|
||||
#include "block/block_int.h"
|
||||
#include "qemu/aio.h"
|
||||
#include "block/raw-aio.h"
|
||||
#include "qemu/aiocb.h"
|
||||
#include "qemu/event_notifier.h"
|
||||
#include "qemu/error-report.h"
|
||||
#include "qemu/iov.h"
|
||||
#include "qemu/lockable.h"
|
||||
#include "qemu/memalign.h"
|
||||
#include "qemu/thread.h"
|
||||
#include "qemu/units.h"
|
||||
#include <windows.h>
|
||||
#include <winioctl.h>
|
||||
|
||||
#define FTYPE_FILE 0
|
||||
#define FTYPE_CD 1
|
||||
#define FTYPE_HARDDISK 2
|
||||
enum {
|
||||
WIN32_AIO_KEY_IO = 1,
|
||||
WIN32_AIO_KEY_STOP,
|
||||
};
|
||||
|
||||
#define WIN32_AIO_BOUNCE_MAX_CACHED 16
|
||||
|
||||
static const struct {
|
||||
size_t size;
|
||||
unsigned int capacity;
|
||||
} bounce_buckets[] = {
|
||||
{ 64 * KiB, WIN32_AIO_BOUNCE_MAX_CACHED },
|
||||
{ 256 * KiB, 8 },
|
||||
{ MiB, 8 },
|
||||
};
|
||||
|
||||
typedef struct Win32AioBounceBucket {
|
||||
void *buffers[WIN32_AIO_BOUNCE_MAX_CACHED];
|
||||
unsigned int count;
|
||||
} Win32AioBounceBucket;
|
||||
|
||||
struct QEMUWin32AIOState {
|
||||
HANDLE hIOCP;
|
||||
EventNotifier e;
|
||||
int count;
|
||||
QemuThread waiter_thread;
|
||||
AioContext *aio_ctx;
|
||||
bool skip_iocp_on_success;
|
||||
QemuMutex pool_lock;
|
||||
size_t pool_align;
|
||||
Win32AioBounceBucket pool[ARRAY_SIZE(bounce_buckets)];
|
||||
};
|
||||
|
||||
typedef struct QEMUWin32AIOCB {
|
||||
BlockAIOCB common;
|
||||
struct QEMUWin32AIOState *ctx;
|
||||
AioContext *req_ctx;
|
||||
int nbytes;
|
||||
typedef struct Win32AioCoReq {
|
||||
OVERLAPPED ov;
|
||||
QEMUIOVector *qiov;
|
||||
Coroutine *co;
|
||||
AioContext *req_ctx;
|
||||
DWORD win32_err;
|
||||
DWORD transferred;
|
||||
} Win32AioCoReq;
|
||||
|
||||
static int win32_error_to_errno(DWORD err)
|
||||
{
|
||||
switch (err) {
|
||||
case ERROR_DISK_FULL:
|
||||
case ERROR_HANDLE_DISK_FULL:
|
||||
return -ENOSPC;
|
||||
case ERROR_ACCESS_DENIED:
|
||||
return -EACCES;
|
||||
case ERROR_INVALID_PARAMETER:
|
||||
return -EINVAL;
|
||||
case ERROR_OPERATION_ABORTED:
|
||||
return -ECANCELED;
|
||||
case ERROR_NOT_ENOUGH_MEMORY:
|
||||
case ERROR_OUTOFMEMORY:
|
||||
return -ENOMEM;
|
||||
default:
|
||||
return -EIO;
|
||||
}
|
||||
}
|
||||
|
||||
static void *win32_aio_bounce_alloc(QEMUWin32AIOState *aio,
|
||||
BlockDriverState *bs, size_t bytes,
|
||||
int *bucket_out)
|
||||
{
|
||||
int bucket = 0;
|
||||
void *buf;
|
||||
bool is_read;
|
||||
bool is_linear;
|
||||
int ret;
|
||||
} QEMUWin32AIOCB;
|
||||
|
||||
static void win32_aio_completion_cb_bh(void *opaque)
|
||||
{
|
||||
QEMUWin32AIOCB *waiocb = opaque;
|
||||
while (bucket < ARRAY_SIZE(bounce_buckets) &&
|
||||
bytes > bounce_buckets[bucket].size) {
|
||||
bucket++;
|
||||
}
|
||||
if (bucket == ARRAY_SIZE(bounce_buckets) ||
|
||||
bdrv_min_mem_align(bs) > aio->pool_align) {
|
||||
*bucket_out = -1;
|
||||
return qemu_try_blockalign(bs, bytes);
|
||||
}
|
||||
|
||||
waiocb->common.cb(waiocb->common.opaque, waiocb->ret);
|
||||
aio_context_unref(waiocb->req_ctx);
|
||||
qemu_aio_unref(waiocb);
|
||||
WITH_QEMU_LOCK_GUARD(&aio->pool_lock) {
|
||||
Win32AioBounceBucket *pool = &aio->pool[bucket];
|
||||
|
||||
if (pool->count) {
|
||||
*bucket_out = bucket;
|
||||
return pool->buffers[--pool->count];
|
||||
}
|
||||
}
|
||||
buf = qemu_try_memalign(aio->pool_align, bounce_buckets[bucket].size);
|
||||
if (buf) {
|
||||
*bucket_out = bucket;
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
/*
|
||||
* Completes an AIO request (calls the callback and frees the ACB).
|
||||
*/
|
||||
static void win32_aio_process_completion(QEMUWin32AIOState *s,
|
||||
QEMUWin32AIOCB *waiocb, DWORD count)
|
||||
static void win32_aio_bounce_free(QEMUWin32AIOState *aio, void *buf, int bucket)
|
||||
{
|
||||
s->count--;
|
||||
if (!buf) {
|
||||
return;
|
||||
}
|
||||
if (bucket >= 0) {
|
||||
Win32AioBounceBucket *pool = &aio->pool[bucket];
|
||||
QEMU_LOCK_GUARD(&aio->pool_lock);
|
||||
|
||||
if (waiocb->ov.Internal != 0) {
|
||||
waiocb->ret = -EIO;
|
||||
} else {
|
||||
waiocb->ret = 0;
|
||||
if (count < waiocb->nbytes) {
|
||||
/* Short reads mean EOF, pad with zeros. */
|
||||
if (waiocb->is_read) {
|
||||
qemu_iovec_memset(waiocb->qiov, count, 0,
|
||||
waiocb->qiov->size - count);
|
||||
} else {
|
||||
waiocb->ret = -EINVAL;
|
||||
if (pool->count < bounce_buckets[bucket].capacity) {
|
||||
pool->buffers[pool->count++] = buf;
|
||||
return;
|
||||
}
|
||||
}
|
||||
qemu_vfree(buf);
|
||||
}
|
||||
|
||||
static void *win32_aio_waiter(void *opaque)
|
||||
{
|
||||
QEMUWin32AIOState *s = opaque;
|
||||
|
||||
for (;;) {
|
||||
DWORD count = 0;
|
||||
ULONG_PTR key;
|
||||
OVERLAPPED *ov;
|
||||
Win32AioCoReq *req;
|
||||
AioContext *ctx;
|
||||
Coroutine *co;
|
||||
BOOL ok = GetQueuedCompletionStatus(s->hIOCP, &count, &key, &ov,
|
||||
INFINITE);
|
||||
DWORD err = ok ? ERROR_SUCCESS : GetLastError();
|
||||
|
||||
if (!ov) {
|
||||
if (!ok) {
|
||||
error_report("GetQueuedCompletionStatus failed: %lu", err);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!waiocb->is_linear) {
|
||||
if (waiocb->ret == 0 && waiocb->is_read) {
|
||||
QEMUIOVector *qiov = waiocb->qiov;
|
||||
iov_from_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
|
||||
assert(key == WIN32_AIO_KEY_STOP);
|
||||
break;
|
||||
}
|
||||
qemu_vfree(waiocb->buf);
|
||||
|
||||
assert(key == WIN32_AIO_KEY_IO);
|
||||
req = container_of(ov, Win32AioCoReq, ov);
|
||||
ctx = req->req_ctx;
|
||||
co = req->co;
|
||||
req->win32_err = err;
|
||||
req->transferred = err == ERROR_HANDLE_EOF ? 0 : count;
|
||||
aio_co_schedule(ctx, co);
|
||||
}
|
||||
|
||||
if (waiocb->req_ctx == s->aio_ctx) {
|
||||
win32_aio_completion_cb_bh(waiocb);
|
||||
} else {
|
||||
aio_bh_schedule_oneshot(waiocb->req_ctx, win32_aio_completion_cb_bh,
|
||||
waiocb);
|
||||
}
|
||||
}
|
||||
|
||||
static void win32_aio_completion_cb(EventNotifier *e)
|
||||
{
|
||||
QEMUWin32AIOState *s = container_of(e, QEMUWin32AIOState, e);
|
||||
DWORD count;
|
||||
ULONG_PTR key;
|
||||
OVERLAPPED *ov;
|
||||
|
||||
event_notifier_test_and_clear(&s->e);
|
||||
while (GetQueuedCompletionStatus(s->hIOCP, &count, &key, &ov, 0)) {
|
||||
QEMUWin32AIOCB *waiocb = container_of(ov, QEMUWin32AIOCB, ov);
|
||||
|
||||
win32_aio_process_completion(s, waiocb, count);
|
||||
}
|
||||
}
|
||||
|
||||
static const AIOCBInfo win32_aiocb_info = {
|
||||
.aiocb_size = sizeof(QEMUWin32AIOCB),
|
||||
};
|
||||
|
||||
BlockAIOCB *win32_aio_submit(BlockDriverState *bs,
|
||||
QEMUWin32AIOState *aio, HANDLE hfile,
|
||||
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov,
|
||||
BlockCompletionFunc *cb, void *opaque, int type)
|
||||
{
|
||||
struct QEMUWin32AIOCB *waiocb;
|
||||
DWORD rc;
|
||||
|
||||
waiocb = qemu_aio_get(&win32_aiocb_info, bs, cb, opaque);
|
||||
waiocb->req_ctx = qemu_get_current_aio_context();
|
||||
waiocb->nbytes = bytes;
|
||||
waiocb->qiov = qiov;
|
||||
waiocb->is_read = (type == QEMU_AIO_READ);
|
||||
|
||||
aio_context_ref(waiocb->req_ctx);
|
||||
|
||||
if (qiov->niov > 1) {
|
||||
waiocb->buf = qemu_try_blockalign(bs, qiov->size);
|
||||
if (waiocb->buf == NULL) {
|
||||
goto out;
|
||||
}
|
||||
if (type & QEMU_AIO_WRITE) {
|
||||
iov_to_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
|
||||
}
|
||||
waiocb->is_linear = false;
|
||||
} else {
|
||||
waiocb->buf = qiov->iov[0].iov_base;
|
||||
waiocb->is_linear = true;
|
||||
}
|
||||
|
||||
memset(&waiocb->ov, 0, sizeof(waiocb->ov));
|
||||
waiocb->ov.Offset = (DWORD)offset;
|
||||
waiocb->ov.OffsetHigh = (DWORD)(offset >> 32);
|
||||
waiocb->ov.hEvent = event_notifier_get_handle(&aio->e);
|
||||
|
||||
aio->count++;
|
||||
|
||||
if (type & QEMU_AIO_READ) {
|
||||
rc = ReadFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
|
||||
} else {
|
||||
rc = WriteFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
|
||||
}
|
||||
if(rc == 0 && GetLastError() != ERROR_IO_PENDING) {
|
||||
goto out_dec_count;
|
||||
}
|
||||
return &waiocb->common;
|
||||
|
||||
out_dec_count:
|
||||
aio->count--;
|
||||
out:
|
||||
qemu_aio_unref(waiocb);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int coroutine_fn win32_aio_co_submit(BlockDriverState *bs,
|
||||
QEMUWin32AIOState *aio, HANDLE hfile,
|
||||
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int type)
|
||||
{
|
||||
Win32AioCoReq req;
|
||||
void *bounce_buf = NULL;
|
||||
int bucket = -1;
|
||||
void *buf;
|
||||
BOOL ok;
|
||||
DWORD sync_transferred = 0;
|
||||
DWORD transferred;
|
||||
DWORD win32_err;
|
||||
int ret = 0;
|
||||
bool direct = bs->open_flags & BDRV_O_NOCACHE;
|
||||
bool is_read = (type & QEMU_AIO_TYPE_MASK) == QEMU_AIO_READ;
|
||||
|
||||
assert(aio->aio_ctx);
|
||||
if (bytes != qiov->size || bytes > BDRV_REQUEST_MAX_BYTES ||
|
||||
bytes > UINT32_MAX) {
|
||||
return -EINVAL;
|
||||
}
|
||||
if (bytes == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (direct && (!QEMU_IS_ALIGNED(offset, bs->bl.request_alignment) ||
|
||||
!QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment))) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (qiov->niov > 1 ||
|
||||
(direct && !QEMU_PTR_IS_ALIGNED(qiov->iov[0].iov_base,
|
||||
bdrv_min_mem_align(bs)))) {
|
||||
bounce_buf = win32_aio_bounce_alloc(aio, bs, bytes, &bucket);
|
||||
if (!bounce_buf) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (!is_read) {
|
||||
iov_to_buf(qiov->iov, qiov->niov, 0, bounce_buf, bytes);
|
||||
}
|
||||
}
|
||||
buf = bounce_buf ? bounce_buf : qiov->iov[0].iov_base;
|
||||
|
||||
/* The waiter can queue the coroutine before the system call returns. */
|
||||
req = (Win32AioCoReq) {
|
||||
.ov = {
|
||||
.Offset = (DWORD)offset,
|
||||
.OffsetHigh = (DWORD)(offset >> 32),
|
||||
.hEvent = NULL,
|
||||
},
|
||||
.co = qemu_coroutine_self(),
|
||||
.req_ctx = qemu_get_current_aio_context(),
|
||||
};
|
||||
if (is_read) {
|
||||
ok = ReadFile(hfile, buf, (DWORD)bytes,
|
||||
aio->skip_iocp_on_success ? &sync_transferred : NULL,
|
||||
&req.ov);
|
||||
} else {
|
||||
ok = WriteFile(hfile, buf, (DWORD)bytes,
|
||||
aio->skip_iocp_on_success ? &sync_transferred : NULL,
|
||||
&req.ov);
|
||||
}
|
||||
win32_err = ok ? ERROR_SUCCESS : GetLastError();
|
||||
if (win32_err == ERROR_IO_PENDING || (ok && !aio->skip_iocp_on_success)) {
|
||||
qemu_coroutine_yield();
|
||||
win32_err = req.win32_err;
|
||||
transferred = req.transferred;
|
||||
} else {
|
||||
transferred = ok ? sync_transferred : 0;
|
||||
}
|
||||
|
||||
if (win32_err != ERROR_SUCCESS &&
|
||||
!(is_read && win32_err == ERROR_HANDLE_EOF)) {
|
||||
ret = win32_error_to_errno(win32_err);
|
||||
} else if (transferred > bytes || (!is_read && transferred < bytes)) {
|
||||
ret = -EIO;
|
||||
} else if (is_read) {
|
||||
if (bounce_buf && transferred) {
|
||||
iov_from_buf(qiov->iov, qiov->niov, 0, bounce_buf, transferred);
|
||||
}
|
||||
if (transferred < bytes) {
|
||||
qemu_iovec_memset(qiov, transferred, 0, bytes - transferred);
|
||||
}
|
||||
}
|
||||
|
||||
win32_aio_bounce_free(aio, bounce_buf, bucket);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile)
|
||||
{
|
||||
if (CreateIoCompletionPort(hfile, aio->hIOCP, (ULONG_PTR) 0, 0) == NULL) {
|
||||
return -EINVAL;
|
||||
} else {
|
||||
return 0;
|
||||
UCHAR modes = FILE_SKIP_COMPLETION_PORT_ON_SUCCESS |
|
||||
FILE_SKIP_SET_EVENT_ON_HANDLE;
|
||||
|
||||
if (!CreateIoCompletionPort(hfile, aio->hIOCP, WIN32_AIO_KEY_IO, 0)) {
|
||||
return win32_error_to_errno(GetLastError());
|
||||
}
|
||||
if (!aio->aio_ctx) {
|
||||
aio->skip_iocp_on_success =
|
||||
SetFileCompletionNotificationModes(hfile, modes) != 0;
|
||||
} else if (aio->skip_iocp_on_success) {
|
||||
if (!SetFileCompletionNotificationModes(hfile, modes)) {
|
||||
return win32_error_to_errno(GetLastError());
|
||||
}
|
||||
} else {
|
||||
SetFileCompletionNotificationModes(hfile,
|
||||
FILE_SKIP_SET_EVENT_ON_HANDLE);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void win32_aio_detach_aio_context(QEMUWin32AIOState *aio,
|
||||
AioContext *old_context)
|
||||
{
|
||||
aio_set_event_notifier(old_context, &aio->e, NULL, NULL, NULL);
|
||||
assert(aio->aio_ctx == old_context);
|
||||
/* Block layer drain has waited for all requests to finish. */
|
||||
if (!PostQueuedCompletionStatus(aio->hIOCP, 0, WIN32_AIO_KEY_STOP, NULL)) {
|
||||
error_report("PostQueuedCompletionStatus failed: %lu", GetLastError());
|
||||
abort();
|
||||
}
|
||||
qemu_thread_join(&aio->waiter_thread);
|
||||
aio->aio_ctx = NULL;
|
||||
}
|
||||
|
||||
void win32_aio_attach_aio_context(QEMUWin32AIOState *aio,
|
||||
AioContext *new_context)
|
||||
{
|
||||
assert(!aio->aio_ctx);
|
||||
aio->aio_ctx = new_context;
|
||||
aio_set_event_notifier(new_context, &aio->e, win32_aio_completion_cb,
|
||||
NULL, NULL);
|
||||
qemu_thread_create(&aio->waiter_thread, "win32-aio", win32_aio_waiter, aio,
|
||||
QEMU_THREAD_JOINABLE);
|
||||
}
|
||||
|
||||
QEMUWin32AIOState *win32_aio_init(void)
|
||||
{
|
||||
HANDLE port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
||||
QEMUWin32AIOState *s;
|
||||
|
||||
s = g_malloc0(sizeof(*s));
|
||||
if (event_notifier_init(&s->e, false) < 0) {
|
||||
goto out_free_state;
|
||||
if (!port) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
s->hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
||||
if (s->hIOCP == NULL) {
|
||||
goto out_close_efd;
|
||||
}
|
||||
|
||||
s = g_new0(QEMUWin32AIOState, 1);
|
||||
s->hIOCP = port;
|
||||
qemu_mutex_init(&s->pool_lock);
|
||||
s->pool_align = qemu_real_host_page_size();
|
||||
return s;
|
||||
|
||||
out_close_efd:
|
||||
event_notifier_cleanup(&s->e);
|
||||
out_free_state:
|
||||
g_free(s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void win32_aio_cleanup(QEMUWin32AIOState *aio)
|
||||
{
|
||||
assert(!aio->aio_ctx);
|
||||
for (int i = 0; i < ARRAY_SIZE(aio->pool); i++) {
|
||||
Win32AioBounceBucket *pool = &aio->pool[i];
|
||||
|
||||
for (unsigned int j = 0; j < pool->count; j++) {
|
||||
qemu_vfree(pool->buffers[j]);
|
||||
}
|
||||
}
|
||||
qemu_mutex_destroy(&aio->pool_lock);
|
||||
CloseHandle(aio->hIOCP);
|
||||
event_notifier_cleanup(&aio->e);
|
||||
g_free(aio);
|
||||
}
|
||||
@@ -92,10 +92,9 @@ typedef struct QEMUWin32AIOState QEMUWin32AIOState;
|
||||
QEMUWin32AIOState *win32_aio_init(void);
|
||||
void win32_aio_cleanup(QEMUWin32AIOState *aio);
|
||||
int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile);
|
||||
BlockAIOCB *win32_aio_submit(BlockDriverState *bs,
|
||||
int coroutine_fn win32_aio_co_submit(BlockDriverState *bs,
|
||||
QEMUWin32AIOState *aio, HANDLE hfile,
|
||||
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov,
|
||||
BlockCompletionFunc *cb, void *opaque, int type);
|
||||
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int type);
|
||||
void win32_aio_detach_aio_context(QEMUWin32AIOState *aio,
|
||||
AioContext *old_context);
|
||||
void win32_aio_attach_aio_context(QEMUWin32AIOState *aio,
|
||||
|
||||
+5
-2
@@ -877,8 +877,11 @@ static MachineClass *find_default_machine(GSList *machines)
|
||||
|
||||
static void version(void)
|
||||
{
|
||||
printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
|
||||
QEMU_COPYRIGHT "\n");
|
||||
printf("QEMU emulator version " QEMU_FULL_VERSION
|
||||
#ifdef _WIN32
|
||||
" [reims-win32-native-aio]"
|
||||
#endif
|
||||
"\n" QEMU_COPYRIGHT "\n");
|
||||
}
|
||||
|
||||
static void help(int exitcode)
|
||||
|
||||
Reference in new issue
Block a user