2007-12-15 20:28:36 +03:00
|
|
|
/*
|
|
|
|
* Block driver for RAW files (win32)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2018-02-01 14:18:46 +03:00
|
|
|
|
2016-01-18 21:01:42 +03:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/cutils.h"
|
2012-12-17 21:19:44 +04:00
|
|
|
#include "block/block_int.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/module.h"
|
2018-02-01 14:18:46 +03:00
|
|
|
#include "qemu/option.h"
|
2016-07-04 19:33:20 +03:00
|
|
|
#include "block/raw-aio.h"
|
2012-06-09 06:48:28 +04:00
|
|
|
#include "trace.h"
|
2012-12-17 21:19:44 +04:00
|
|
|
#include "block/thread-pool.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/iov.h"
|
2018-02-01 14:18:39 +03:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2015-03-17 20:29:20 +03:00
|
|
|
#include "qapi/qmp/qstring.h"
|
2009-03-08 19:26:59 +03:00
|
|
|
#include <windows.h>
|
2007-12-15 20:28:36 +03:00
|
|
|
#include <winioctl.h>
|
|
|
|
|
|
|
|
#define FTYPE_FILE 0
|
|
|
|
#define FTYPE_CD 1
|
|
|
|
#define FTYPE_HARDDISK 2
|
|
|
|
|
2012-06-09 06:48:28 +04:00
|
|
|
typedef struct RawWin32AIOData {
|
|
|
|
BlockDriverState *bs;
|
|
|
|
HANDLE hfile;
|
|
|
|
struct iovec *aio_iov;
|
|
|
|
int aio_niov;
|
|
|
|
size_t aio_nbytes;
|
|
|
|
off64_t aio_offset;
|
|
|
|
int aio_type;
|
|
|
|
} RawWin32AIOData;
|
|
|
|
|
2007-12-15 20:28:36 +03:00
|
|
|
typedef struct BDRVRawState {
|
|
|
|
HANDLE hfile;
|
|
|
|
int type;
|
|
|
|
char drive_path[16]; /* format: "d:\" */
|
2012-10-26 13:43:58 +04:00
|
|
|
QEMUWin32AIOState *aio;
|
2007-12-15 20:28:36 +03:00
|
|
|
} BDRVRawState;
|
|
|
|
|
2012-06-09 06:48:28 +04:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
size_t offset = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < aiocb->aio_niov; i++) {
|
|
|
|
OVERLAPPED ov;
|
|
|
|
DWORD ret, ret_count, len;
|
|
|
|
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
ret = ReadFile(aiocb->hfile, aiocb->aio_iov[i].iov_base,
|
|
|
|
len, &ret_count, &ov);
|
|
|
|
}
|
|
|
|
if (!ret) {
|
|
|
|
ret_count = 0;
|
|
|
|
}
|
|
|
|
if (ret_count != len) {
|
2013-09-09 13:14:55 +04:00
|
|
|
offset += ret_count;
|
2012-06-09 06:48:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aio_worker(void *arg)
|
|
|
|
{
|
|
|
|
RawWin32AIOData *aiocb = arg;
|
|
|
|
ssize_t ret = 0;
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
|
|
|
|
case QEMU_AIO_READ:
|
|
|
|
count = handle_aiocb_rw(aiocb);
|
2015-02-05 21:58:24 +03:00
|
|
|
if (count < aiocb->aio_nbytes) {
|
2012-06-09 06:48:28 +04:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
if (count == aiocb->aio_nbytes) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QEMU_AIO_WRITE:
|
|
|
|
count = handle_aiocb_rw(aiocb);
|
|
|
|
if (count == aiocb->aio_nbytes) {
|
2015-09-23 15:58:21 +03:00
|
|
|
ret = 0;
|
2012-06-09 06:48:28 +04:00
|
|
|
} else {
|
2015-09-23 15:58:21 +03:00
|
|
|
ret = -EINVAL;
|
2012-06-09 06:48:28 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QEMU_AIO_FLUSH:
|
|
|
|
if (!FlushFileBuffers(aiocb->hfile)) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-01 14:04:39 +03:00
|
|
|
g_free(aiocb);
|
2012-06-09 06:48:28 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:59:14 +04:00
|
|
|
static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
|
2016-07-16 02:22:55 +03:00
|
|
|
int64_t offset, QEMUIOVector *qiov, int count,
|
2014-10-07 15:59:15 +04:00
|
|
|
BlockCompletionFunc *cb, void *opaque, int type)
|
2012-06-09 06:48:28 +04:00
|
|
|
{
|
2015-10-01 14:04:39 +03:00
|
|
|
RawWin32AIOData *acb = g_new(RawWin32AIOData, 1);
|
2013-03-07 16:41:49 +04:00
|
|
|
ThreadPool *pool;
|
2012-06-09 06:48:28 +04:00
|
|
|
|
|
|
|
acb->bs = bs;
|
|
|
|
acb->hfile = hfile;
|
|
|
|
acb->aio_type = type;
|
|
|
|
|
|
|
|
if (qiov) {
|
|
|
|
acb->aio_iov = qiov->iov;
|
|
|
|
acb->aio_niov = qiov->niov;
|
2016-07-16 02:22:55 +03:00
|
|
|
assert(qiov->size == count);
|
2012-06-09 06:48:28 +04:00
|
|
|
}
|
2016-07-16 02:22:55 +03:00
|
|
|
acb->aio_nbytes = count;
|
|
|
|
acb->aio_offset = offset;
|
2012-06-09 06:48:28 +04:00
|
|
|
|
2016-07-16 02:22:55 +03:00
|
|
|
trace_paio_submit(acb, opaque, offset, count, type);
|
2013-03-07 16:41:49 +04:00
|
|
|
pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
|
|
|
|
return thread_pool_submit_aio(pool, aio_worker, acb, cb, opaque);
|
2012-06-09 06:48:28 +04:00
|
|
|
}
|
|
|
|
|
2007-12-15 20:28:36 +03:00
|
|
|
int qemu_ftruncate64(int fd, int64_t length)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER li;
|
2011-08-20 14:10:05 +04:00
|
|
|
DWORD dw;
|
2007-12-15 20:28:36 +03:00
|
|
|
LONG high;
|
|
|
|
HANDLE h;
|
|
|
|
BOOL res;
|
|
|
|
|
|
|
|
if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
h = (HANDLE)_get_osfhandle(fd);
|
|
|
|
|
|
|
|
/* get current position, ftruncate do not change position */
|
|
|
|
li.HighPart = 0;
|
|
|
|
li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
|
2011-08-20 14:10:05 +04:00
|
|
|
if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
|
2007-12-15 20:28:36 +03:00
|
|
|
return -1;
|
2011-08-20 14:10:05 +04:00
|
|
|
}
|
2007-12-15 20:28:36 +03:00
|
|
|
|
|
|
|
high = length >> 32;
|
2011-08-20 14:10:05 +04:00
|
|
|
dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
|
|
|
|
if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
|
2007-12-15 20:28:36 +03:00
|
|
|
return -1;
|
2011-08-20 14:10:05 +04:00
|
|
|
}
|
2007-12-15 20:28:36 +03:00
|
|
|
res = SetEndOfFile(h);
|
|
|
|
|
|
|
|
/* back to old position */
|
|
|
|
SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
|
|
|
|
return res ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_sparse(int fd)
|
|
|
|
{
|
|
|
|
DWORD returned;
|
|
|
|
return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
|
|
|
|
NULL, 0, NULL, 0, &returned, NULL);
|
|
|
|
}
|
|
|
|
|
2014-05-08 18:34:50 +04:00
|
|
|
static void raw_detach_aio_context(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
|
|
|
|
if (s->aio) {
|
|
|
|
win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void raw_attach_aio_context(BlockDriverState *bs,
|
|
|
|
AioContext *new_context)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
|
|
|
|
if (s->aio) {
|
|
|
|
win32_aio_attach_aio_context(s->aio, new_context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 01:37:16 +03:00
|
|
|
static void raw_probe_alignment(BlockDriverState *bs, Error **errp)
|
2011-11-29 15:42:20 +04:00
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
DWORD sectorsPerCluster, freeClusters, totalClusters, count;
|
|
|
|
DISK_GEOMETRY_EX dg;
|
|
|
|
BOOL status;
|
|
|
|
|
|
|
|
if (s->type == FTYPE_CD) {
|
2016-06-24 01:37:24 +03:00
|
|
|
bs->bl.request_alignment = 2048;
|
2011-11-29 15:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
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) {
|
2016-06-24 01:37:24 +03:00
|
|
|
bs->bl.request_alignment = dg.Geometry.BytesPerSector;
|
2011-11-29 15:42:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* try GetDiskFreeSpace too */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->drive_path[0]) {
|
|
|
|
GetDiskFreeSpace(s->drive_path, §orsPerCluster,
|
|
|
|
&dg.Geometry.BytesPerSector,
|
|
|
|
&freeClusters, &totalClusters);
|
2016-06-24 01:37:24 +03:00
|
|
|
bs->bl.request_alignment = dg.Geometry.BytesPerSector;
|
2011-11-29 15:42:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 16:09:01 +03:00
|
|
|
static void raw_parse_flags(int flags, bool use_aio, int *access_flags,
|
|
|
|
DWORD *overlapped)
|
2012-09-20 23:13:21 +04:00
|
|
|
{
|
|
|
|
assert(access_flags != NULL);
|
|
|
|
assert(overlapped != NULL);
|
|
|
|
|
|
|
|
if (flags & BDRV_O_RDWR) {
|
|
|
|
*access_flags = GENERIC_READ | GENERIC_WRITE;
|
|
|
|
} else {
|
|
|
|
*access_flags = GENERIC_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
*overlapped = FILE_ATTRIBUTE_NORMAL;
|
2016-09-08 16:09:01 +03:00
|
|
|
if (use_aio) {
|
2012-10-26 13:43:58 +04:00
|
|
|
*overlapped |= FILE_FLAG_OVERLAPPED;
|
|
|
|
}
|
2012-09-20 23:13:21 +04:00
|
|
|
if (flags & BDRV_O_NOCACHE) {
|
|
|
|
*overlapped |= FILE_FLAG_NO_BUFFERING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 01:41:39 +04:00
|
|
|
static void raw_parse_filename(const char *filename, QDict *options,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2017-05-22 22:52:16 +03:00
|
|
|
bdrv_parse_filename_strip_prefix(filename, "file:", options);
|
2014-03-06 01:41:39 +04:00
|
|
|
}
|
|
|
|
|
2013-04-10 13:34:56 +04:00
|
|
|
static QemuOptsList raw_runtime_opts = {
|
|
|
|
.name = "raw",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = "filename",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "File name of the image",
|
|
|
|
},
|
2016-09-08 16:09:01 +03:00
|
|
|
{
|
|
|
|
.name = "aio",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "host AIO implementation (threads, native)",
|
|
|
|
},
|
2013-04-10 13:34:56 +04:00
|
|
|
{ /* end of list */ }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-09-08 16:09:01 +03:00
|
|
|
static bool get_aio_option(QemuOpts *opts, int flags, Error **errp)
|
|
|
|
{
|
|
|
|
BlockdevAioOptions aio, aio_default;
|
|
|
|
|
|
|
|
aio_default = (flags & BDRV_O_NATIVE_AIO) ? BLOCKDEV_AIO_OPTIONS_NATIVE
|
|
|
|
: BLOCKDEV_AIO_OPTIONS_THREADS;
|
2017-08-24 11:46:10 +03:00
|
|
|
aio = qapi_enum_parse(&BlockdevAioOptions_lookup, qemu_opt_get(opts, "aio"),
|
2017-08-24 11:45:57 +03:00
|
|
|
aio_default, errp);
|
2016-09-08 16:09:01 +03:00
|
|
|
|
|
|
|
switch (aio) {
|
|
|
|
case BLOCKDEV_AIO_OPTIONS_NATIVE:
|
|
|
|
return true;
|
|
|
|
case BLOCKDEV_AIO_OPTIONS_THREADS:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
error_setg(errp, "Invalid AIO option");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-05 16:22:29 +04:00
|
|
|
static int raw_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
2007-12-15 20:28:36 +03:00
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
2010-01-20 20:13:42 +03:00
|
|
|
int access_flags;
|
2007-12-15 20:28:36 +03:00
|
|
|
DWORD overlapped;
|
2013-04-10 13:34:56 +04:00
|
|
|
QemuOpts *opts;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
const char *filename;
|
2016-09-08 16:09:01 +03:00
|
|
|
bool use_aio;
|
2013-04-10 13:34:56 +04:00
|
|
|
int ret;
|
2007-12-15 20:28:36 +03:00
|
|
|
|
|
|
|
s->type = FTYPE_FILE;
|
|
|
|
|
2014-01-02 06:49:17 +04:00
|
|
|
opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
|
2013-04-10 13:34:56 +04:00
|
|
|
qemu_opts_absorb_qdict(opts, options, &local_err);
|
2014-01-30 18:07:28 +04:00
|
|
|
if (local_err) {
|
2013-10-10 17:44:02 +04:00
|
|
|
error_propagate(errp, local_err);
|
2013-04-10 13:34:56 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-05-02 19:35:51 +03:00
|
|
|
if (qdict_get_try_bool(options, "locking", false)) {
|
|
|
|
error_setg(errp, "locking=on is not supported on Windows");
|
2017-05-16 10:42:55 +03:00
|
|
|
ret = -EINVAL;
|
2017-05-02 19:35:51 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-04-10 13:34:56 +04:00
|
|
|
filename = qemu_opt_get(opts, "filename");
|
|
|
|
|
2016-09-08 16:09:01 +03:00
|
|
|
use_aio = get_aio_option(opts, flags, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
|
2013-04-10 13:34:56 +04:00
|
|
|
|
2011-11-29 15:42:20 +04:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2007-12-15 20:28:36 +03:00
|
|
|
s->hfile = CreateFile(filename, access_flags,
|
|
|
|
FILE_SHARE_READ, NULL,
|
2010-01-20 20:13:42 +03:00
|
|
|
OPEN_EXISTING, overlapped, NULL);
|
2007-12-15 20:28:36 +03:00
|
|
|
if (s->hfile == INVALID_HANDLE_VALUE) {
|
|
|
|
int err = GetLastError();
|
|
|
|
|
2016-10-11 17:12:35 +03:00
|
|
|
error_setg_win32(errp, err, "Could not open '%s'", filename);
|
2013-04-10 13:34:56 +04:00
|
|
|
if (err == ERROR_ACCESS_DENIED) {
|
|
|
|
ret = -EACCES;
|
|
|
|
} else {
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
goto fail;
|
2012-10-26 13:43:58 +04:00
|
|
|
}
|
|
|
|
|
2016-09-08 16:09:01 +03:00
|
|
|
if (use_aio) {
|
2014-05-08 18:34:49 +04:00
|
|
|
s->aio = win32_aio_init();
|
|
|
|
if (s->aio == NULL) {
|
|
|
|
CloseHandle(s->hfile);
|
|
|
|
error_setg(errp, "Could not initialize AIO");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = win32_aio_attach(s->aio, s->hfile);
|
2012-10-26 13:43:58 +04:00
|
|
|
if (ret < 0) {
|
2014-05-08 18:34:49 +04:00
|
|
|
win32_aio_cleanup(s->aio);
|
2012-10-26 13:43:58 +04:00
|
|
|
CloseHandle(s->hfile);
|
2013-10-10 17:44:02 +04:00
|
|
|
error_setg_errno(errp, -ret, "Could not enable AIO");
|
2013-04-10 13:34:56 +04:00
|
|
|
goto fail;
|
2012-10-26 13:43:58 +04:00
|
|
|
}
|
2014-05-08 18:34:50 +04:00
|
|
|
|
|
|
|
win32_aio_attach_aio_context(s->aio, bdrv_get_aio_context(bs));
|
2007-12-15 20:28:36 +03:00
|
|
|
}
|
2013-04-10 13:34:56 +04:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
fail:
|
|
|
|
qemu_opts_del(opts);
|
|
|
|
return ret;
|
2007-12-15 20:28:36 +03:00
|
|
|
}
|
|
|
|
|
2014-10-07 15:59:14 +04:00
|
|
|
static BlockAIOCB *raw_aio_readv(BlockDriverState *bs,
|
2012-06-09 06:48:28 +04:00
|
|
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
2014-10-07 15:59:15 +04:00
|
|
|
BlockCompletionFunc *cb, void *opaque)
|
2007-12-15 20:28:36 +03:00
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
2012-10-26 13:43:58 +04:00
|
|
|
if (s->aio) {
|
|
|
|
return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
|
2016-07-16 02:22:55 +03:00
|
|
|
nb_sectors, cb, opaque, QEMU_AIO_READ);
|
2012-10-26 13:43:58 +04:00
|
|
|
} else {
|
2016-07-16 02:22:55 +03:00
|
|
|
return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov,
|
|
|
|
nb_sectors << BDRV_SECTOR_BITS,
|
2012-10-26 13:43:58 +04:00
|
|
|
cb, opaque, QEMU_AIO_READ);
|
|
|
|
}
|
2007-12-15 20:28:36 +03:00
|
|
|
}
|
|
|
|
|
2014-10-07 15:59:14 +04:00
|
|
|
static BlockAIOCB *raw_aio_writev(BlockDriverState *bs,
|
2012-06-09 06:48:28 +04:00
|
|
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
2014-10-07 15:59:15 +04:00
|
|
|
BlockCompletionFunc *cb, void *opaque)
|
2007-12-15 20:28:36 +03:00
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
2012-10-26 13:43:58 +04:00
|
|
|
if (s->aio) {
|
|
|
|
return win32_aio_submit(bs, s->aio, s->hfile, sector_num, qiov,
|
2016-07-16 02:22:55 +03:00
|
|
|
nb_sectors, cb, opaque, QEMU_AIO_WRITE);
|
2012-10-26 13:43:58 +04:00
|
|
|
} else {
|
2016-07-16 02:22:55 +03:00
|
|
|
return paio_submit(bs, s->hfile, sector_num << BDRV_SECTOR_BITS, qiov,
|
|
|
|
nb_sectors << BDRV_SECTOR_BITS,
|
2012-10-26 13:43:58 +04:00
|
|
|
cb, opaque, QEMU_AIO_WRITE);
|
|
|
|
}
|
2007-12-15 20:28:36 +03:00
|
|
|
}
|
|
|
|
|
2014-10-07 15:59:14 +04:00
|
|
|
static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
|
2014-10-07 15:59:15 +04:00
|
|
|
BlockCompletionFunc *cb, void *opaque)
|
2007-12-15 20:28:36 +03:00
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
2012-06-09 06:48:28 +04:00
|
|
|
return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
|
2007-12-15 20:28:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void raw_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
2014-05-08 18:34:49 +04:00
|
|
|
|
|
|
|
if (s->aio) {
|
2014-05-08 18:34:50 +04:00
|
|
|
win32_aio_detach_aio_context(s->aio, bdrv_get_aio_context(bs));
|
2014-05-08 18:34:49 +04:00
|
|
|
win32_aio_cleanup(s->aio);
|
|
|
|
s->aio = NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-15 20:28:36 +03:00
|
|
|
CloseHandle(s->hfile);
|
2014-04-11 21:16:36 +04:00
|
|
|
if (bs->open_flags & BDRV_O_TEMPORARY) {
|
|
|
|
unlink(bs->filename);
|
|
|
|
}
|
2007-12-15 20:28:36 +03:00
|
|
|
}
|
|
|
|
|
2017-06-13 23:20:52 +03:00
|
|
|
static int raw_truncate(BlockDriverState *bs, int64_t offset,
|
|
|
|
PreallocMode prealloc, Error **errp)
|
2007-12-15 20:28:36 +03:00
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
2009-04-05 22:03:31 +04:00
|
|
|
LONG low, high;
|
2012-12-10 15:56:22 +04:00
|
|
|
DWORD dwPtrLow;
|
2007-12-15 20:28:36 +03:00
|
|
|
|
2017-06-13 23:20:52 +03:00
|
|
|
if (prealloc != PREALLOC_MODE_OFF) {
|
|
|
|
error_setg(errp, "Unsupported preallocation mode '%s'",
|
2017-08-24 11:46:08 +03:00
|
|
|
PreallocMode_str(prealloc));
|
2017-06-13 23:20:52 +03:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2007-12-15 20:28:36 +03:00
|
|
|
low = offset;
|
|
|
|
high = offset >> 32;
|
2012-12-10 15:56:22 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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) {
|
2017-03-28 23:51:28 +03:00
|
|
|
error_setg_win32(errp, GetLastError(), "SetFilePointer error");
|
2012-12-10 15:56:22 +04:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
if (SetEndOfFile(s->hfile) == 0) {
|
2017-03-28 23:51:28 +03:00
|
|
|
error_setg_win32(errp, GetLastError(), "SetEndOfFile error");
|
2007-12-15 20:28:36 +03:00
|
|
|
return -EIO;
|
2012-12-10 15:56:22 +04:00
|
|
|
}
|
2007-12-15 20:28:36 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t raw_getlength(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
LARGE_INTEGER l;
|
|
|
|
ULARGE_INTEGER available, total, total_free;
|
|
|
|
DISK_GEOMETRY_EX dg;
|
|
|
|
DWORD count;
|
|
|
|
BOOL status;
|
|
|
|
|
|
|
|
switch(s->type) {
|
|
|
|
case FTYPE_FILE:
|
2009-04-05 22:03:31 +04:00
|
|
|
l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
|
2007-12-15 20:28:36 +03:00
|
|
|
if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
|
|
|
|
return -EIO;
|
|
|
|
break;
|
|
|
|
case FTYPE_CD:
|
|
|
|
if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
|
|
|
|
return -EIO;
|
|
|
|
l.QuadPart = total.QuadPart;
|
|
|
|
break;
|
|
|
|
case FTYPE_HARDDISK:
|
|
|
|
status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
|
|
|
NULL, 0, &dg, sizeof(dg), &count, NULL);
|
|
|
|
if (status != 0) {
|
|
|
|
l = dg.DiskSize;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
return l.QuadPart;
|
|
|
|
}
|
|
|
|
|
2011-07-12 15:56:39 +04:00
|
|
|
static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
|
|
|
|
DWORD * high);
|
|
|
|
get_compressed_t get_compressed;
|
|
|
|
struct _stati64 st;
|
|
|
|
const char *filename = bs->filename;
|
|
|
|
/* WinNT support GetCompressedFileSize to determine allocate size */
|
|
|
|
get_compressed =
|
|
|
|
(get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
|
|
|
|
"GetCompressedFileSizeA");
|
|
|
|
if (get_compressed) {
|
|
|
|
DWORD high, low;
|
|
|
|
low = get_compressed(filename, &high);
|
|
|
|
if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
|
|
|
|
return (((int64_t) high) << 32) + low;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_stati64(filename, &st) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return st.st_size;
|
|
|
|
}
|
|
|
|
|
2014-06-05 13:21:02 +04:00
|
|
|
static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
|
2007-12-15 20:28:36 +03:00
|
|
|
{
|
|
|
|
int fd;
|
2009-05-18 18:42:10 +04:00
|
|
|
int64_t total_size = 0;
|
2007-12-15 20:28:36 +03:00
|
|
|
|
2014-03-06 01:41:40 +04:00
|
|
|
strstart(filename, "file:", &filename);
|
|
|
|
|
2009-05-18 18:42:10 +04:00
|
|
|
/* Read out options */
|
2014-09-10 13:05:46 +04:00
|
|
|
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
|
|
|
BDRV_SECTOR_SIZE);
|
2007-12-15 20:28:36 +03:00
|
|
|
|
2012-08-15 00:43:45 +04:00
|
|
|
fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
|
|
|
|
0644);
|
2013-10-10 17:44:02 +04:00
|
|
|
if (fd < 0) {
|
|
|
|
error_setg_errno(errp, errno, "Could not create file");
|
2007-12-15 20:28:36 +03:00
|
|
|
return -EIO;
|
2013-10-10 17:44:02 +04:00
|
|
|
}
|
2007-12-15 20:28:36 +03:00
|
|
|
set_sparse(fd);
|
2014-09-10 13:05:46 +04:00
|
|
|
ftruncate(fd, total_size);
|
2012-08-15 00:43:46 +04:00
|
|
|
qemu_close(fd);
|
2007-12-15 20:28:36 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-05 13:21:02 +04:00
|
|
|
|
|
|
|
static QemuOptsList raw_create_opts = {
|
|
|
|
.name = "raw-create-opts",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_SIZE,
|
|
|
|
.type = QEMU_OPT_SIZE,
|
|
|
|
.help = "Virtual disk size"
|
|
|
|
},
|
|
|
|
{ /* end of list */ }
|
|
|
|
}
|
2009-05-18 18:42:10 +04:00
|
|
|
};
|
|
|
|
|
2014-12-02 20:32:41 +03:00
|
|
|
BlockDriver bdrv_file = {
|
2010-04-08 00:30:24 +04:00
|
|
|
.format_name = "file",
|
|
|
|
.protocol_name = "file",
|
2009-04-07 22:23:51 +04:00
|
|
|
.instance_size = sizeof(BDRVRawState),
|
2013-09-24 19:07:04 +04:00
|
|
|
.bdrv_needs_filename = true,
|
2014-03-06 01:41:39 +04:00
|
|
|
.bdrv_parse_filename = raw_parse_filename,
|
2014-06-05 13:21:02 +04:00
|
|
|
.bdrv_file_open = raw_open,
|
2016-06-24 01:37:16 +03:00
|
|
|
.bdrv_refresh_limits = raw_probe_alignment,
|
2014-06-05 13:21:02 +04:00
|
|
|
.bdrv_close = raw_close,
|
2014-06-05 13:21:11 +04:00
|
|
|
.bdrv_create = raw_create,
|
2013-06-28 14:47:42 +04:00
|
|
|
.bdrv_has_zero_init = bdrv_has_zero_init_1,
|
2011-11-10 20:25:44 +04:00
|
|
|
|
2012-06-09 06:48:28 +04:00
|
|
|
.bdrv_aio_readv = raw_aio_readv,
|
|
|
|
.bdrv_aio_writev = raw_aio_writev,
|
|
|
|
.bdrv_aio_flush = raw_aio_flush,
|
2011-11-10 20:25:44 +04:00
|
|
|
|
2009-04-07 22:23:51 +04:00
|
|
|
.bdrv_truncate = raw_truncate,
|
|
|
|
.bdrv_getlength = raw_getlength,
|
2011-07-12 15:56:39 +04:00
|
|
|
.bdrv_get_allocated_file_size
|
|
|
|
= raw_get_allocated_file_size,
|
2009-05-18 18:42:10 +04:00
|
|
|
|
2014-06-05 13:21:02 +04:00
|
|
|
.create_opts = &raw_create_opts,
|
2007-12-15 20:28:36 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/***********************************************/
|
|
|
|
/* host device */
|
|
|
|
|
|
|
|
static int find_cdrom(char *cdrom_name, int cdrom_name_size)
|
|
|
|
{
|
|
|
|
char drives[256], *pdrv = drives;
|
|
|
|
UINT type;
|
|
|
|
|
|
|
|
memset(drives, 0, sizeof(drives));
|
|
|
|
GetLogicalDriveStrings(sizeof(drives), drives);
|
|
|
|
while(pdrv[0] != '\0') {
|
|
|
|
type = GetDriveType(pdrv);
|
|
|
|
switch(type) {
|
|
|
|
case DRIVE_CDROM:
|
|
|
|
snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pdrv += lstrlen(pdrv) + 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_device_type(BlockDriverState *bs, const char *filename)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
UINT type;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (strstart(filename, "\\\\.\\", &p) ||
|
|
|
|
strstart(filename, "//./", &p)) {
|
|
|
|
if (stristart(p, "PhysicalDrive", NULL))
|
|
|
|
return FTYPE_HARDDISK;
|
|
|
|
snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
|
|
|
|
type = GetDriveType(s->drive_path);
|
2009-04-07 05:24:53 +04:00
|
|
|
switch (type) {
|
|
|
|
case DRIVE_REMOVABLE:
|
|
|
|
case DRIVE_FIXED:
|
|
|
|
return FTYPE_HARDDISK;
|
|
|
|
case DRIVE_CDROM:
|
2007-12-15 20:28:36 +03:00
|
|
|
return FTYPE_CD;
|
2009-04-07 05:24:53 +04:00
|
|
|
default:
|
2007-12-15 20:28:36 +03:00
|
|
|
return FTYPE_FILE;
|
2009-04-07 05:24:53 +04:00
|
|
|
}
|
2007-12-15 20:28:36 +03:00
|
|
|
} else {
|
|
|
|
return FTYPE_FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-15 16:04:22 +04:00
|
|
|
static int hdev_probe_device(const char *filename)
|
|
|
|
{
|
|
|
|
if (strstart(filename, "/dev/cdrom", NULL))
|
|
|
|
return 100;
|
|
|
|
if (is_windows_drive(filename))
|
|
|
|
return 100;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-08 03:39:45 +04:00
|
|
|
static void hdev_parse_filename(const char *filename, QDict *options,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2017-05-22 22:52:16 +03:00
|
|
|
bdrv_parse_filename_strip_prefix(filename, "host_device:", options);
|
2014-03-08 03:39:45 +04:00
|
|
|
}
|
|
|
|
|
2013-09-05 16:22:29 +04:00
|
|
|
static int hdev_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
2007-12-15 20:28:36 +03:00
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
int access_flags, create_flags;
|
2013-09-02 00:59:25 +04:00
|
|
|
int ret = 0;
|
2007-12-15 20:28:36 +03:00
|
|
|
DWORD overlapped;
|
|
|
|
char device_name[64];
|
2013-09-02 00:59:25 +04:00
|
|
|
|
|
|
|
Error *local_err = NULL;
|
|
|
|
const char *filename;
|
2016-09-08 16:09:01 +03:00
|
|
|
bool use_aio;
|
2013-09-02 00:59:25 +04:00
|
|
|
|
2014-01-02 06:49:17 +04:00
|
|
|
QemuOpts *opts = qemu_opts_create(&raw_runtime_opts, NULL, 0,
|
|
|
|
&error_abort);
|
2013-09-02 00:59:25 +04:00
|
|
|
qemu_opts_absorb_qdict(opts, options, &local_err);
|
2014-01-30 18:07:28 +04:00
|
|
|
if (local_err) {
|
2013-10-10 17:44:02 +04:00
|
|
|
error_propagate(errp, local_err);
|
2013-09-02 00:59:25 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = qemu_opt_get(opts, "filename");
|
2007-12-15 20:28:36 +03:00
|
|
|
|
2016-09-08 16:09:01 +03:00
|
|
|
use_aio = get_aio_option(opts, flags, &local_err);
|
|
|
|
if (!local_err && use_aio) {
|
|
|
|
error_setg(&local_err, "AIO is not supported on Windows host devices");
|
|
|
|
}
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2007-12-15 20:28:36 +03:00
|
|
|
if (strstart(filename, "/dev/cdrom", NULL)) {
|
2013-09-02 00:59:25 +04:00
|
|
|
if (find_cdrom(device_name, sizeof(device_name)) < 0) {
|
2013-10-10 17:44:02 +04:00
|
|
|
error_setg(errp, "Could not open CD-ROM drive");
|
2013-09-02 00:59:25 +04:00
|
|
|
ret = -ENOENT;
|
|
|
|
goto done;
|
|
|
|
}
|
2007-12-15 20:28:36 +03:00
|
|
|
filename = device_name;
|
|
|
|
} else {
|
|
|
|
/* transform drive letters into device name */
|
|
|
|
if (((filename[0] >= 'a' && filename[0] <= 'z') ||
|
|
|
|
(filename[0] >= 'A' && filename[0] <= 'Z')) &&
|
|
|
|
filename[1] == ':' && filename[2] == '\0') {
|
|
|
|
snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
|
|
|
|
filename = device_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->type = find_device_type(bs, filename);
|
|
|
|
|
2016-09-08 16:09:01 +03:00
|
|
|
raw_parse_flags(flags, use_aio, &access_flags, &overlapped);
|
2012-09-20 23:13:21 +04:00
|
|
|
|
2007-12-15 20:28:36 +03:00
|
|
|
create_flags = OPEN_EXISTING;
|
|
|
|
|
|
|
|
s->hfile = CreateFile(filename, access_flags,
|
|
|
|
FILE_SHARE_READ, NULL,
|
|
|
|
create_flags, overlapped, NULL);
|
|
|
|
if (s->hfile == INVALID_HANDLE_VALUE) {
|
|
|
|
int err = GetLastError();
|
|
|
|
|
2013-09-02 00:59:25 +04:00
|
|
|
if (err == ERROR_ACCESS_DENIED) {
|
|
|
|
ret = -EACCES;
|
|
|
|
} else {
|
2013-10-11 16:30:16 +04:00
|
|
|
ret = -EINVAL;
|
2013-09-02 00:59:25 +04:00
|
|
|
}
|
2013-10-11 16:30:16 +04:00
|
|
|
error_setg_errno(errp, -ret, "Could not open device");
|
2013-09-02 00:59:25 +04:00
|
|
|
goto done;
|
2007-12-15 20:28:36 +03:00
|
|
|
}
|
2013-09-02 00:59:25 +04:00
|
|
|
|
|
|
|
done:
|
|
|
|
qemu_opts_del(opts);
|
|
|
|
return ret;
|
2007-12-15 20:28:36 +03:00
|
|
|
}
|
|
|
|
|
2009-05-10 02:03:42 +04:00
|
|
|
static BlockDriver bdrv_host_device = {
|
2009-03-08 01:00:29 +03:00
|
|
|
.format_name = "host_device",
|
2010-04-08 00:30:24 +04:00
|
|
|
.protocol_name = "host_device",
|
2009-03-08 01:00:29 +03:00
|
|
|
.instance_size = sizeof(BDRVRawState),
|
2013-09-24 19:07:04 +04:00
|
|
|
.bdrv_needs_filename = true,
|
2014-03-08 03:39:45 +04:00
|
|
|
.bdrv_parse_filename = hdev_parse_filename,
|
2009-06-15 16:04:22 +04:00
|
|
|
.bdrv_probe_device = hdev_probe_device,
|
2010-04-14 16:17:38 +04:00
|
|
|
.bdrv_file_open = hdev_open,
|
2009-03-08 01:00:29 +03:00
|
|
|
.bdrv_close = raw_close,
|
2007-12-15 20:28:36 +03:00
|
|
|
|
2012-06-09 06:48:28 +04:00
|
|
|
.bdrv_aio_readv = raw_aio_readv,
|
|
|
|
.bdrv_aio_writev = raw_aio_writev,
|
|
|
|
.bdrv_aio_flush = raw_aio_flush,
|
2011-11-10 20:25:44 +04:00
|
|
|
|
2014-05-08 18:34:50 +04:00
|
|
|
.bdrv_detach_aio_context = raw_detach_aio_context,
|
|
|
|
.bdrv_attach_aio_context = raw_attach_aio_context,
|
|
|
|
|
block: Avoid unecessary drv->bdrv_getlength() calls
The block layer generally keeps the size of an image cached in
bs->total_sectors so that it doesn't have to perform expensive
operations to get the size whenever it needs it.
This doesn't work however when using a backend that can change its size
without qemu being aware of it, i.e. passthrough of removable media like
CD-ROMs or floppy disks. For this reason, the caching is disabled when a
removable device is used.
It is obvious that checking whether the _guest_ device has removable
media isn't the right thing to do when we want to know whether the size
of the host backend can change. To make things worse, non-top-level
BlockDriverStates never have any device attached, which makes qemu
assume they are removable, so drv->bdrv_getlength() is always called on
the protocol layer. In the case of raw-posix, this causes unnecessary
lseek() system calls, which turned out to be rather expensive.
This patch completely changes the logic and disables bs->total_sectors
caching only for certain block driver types, for which a size change is
expected: host_cdrom and host_floppy on POSIX, host_device on win32; also
the raw format in case it sits on top of one of these protocols, but in
the common case the nested bdrv_getlength() call on the protocol driver
will use the cache again and avoid an expensive drv->bdrv_getlength()
call.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
2013-10-29 15:18:58 +04:00
|
|
|
.bdrv_getlength = raw_getlength,
|
|
|
|
.has_variable_length = true,
|
|
|
|
|
2011-07-12 15:56:39 +04:00
|
|
|
.bdrv_get_allocated_file_size
|
|
|
|
= raw_get_allocated_file_size,
|
2007-12-15 20:28:36 +03:00
|
|
|
};
|
2009-05-10 02:03:42 +04:00
|
|
|
|
2010-04-08 00:30:24 +04:00
|
|
|
static void bdrv_file_init(void)
|
2009-05-10 02:03:42 +04:00
|
|
|
{
|
2010-04-08 00:30:24 +04:00
|
|
|
bdrv_register(&bdrv_file);
|
2009-05-10 02:03:42 +04:00
|
|
|
bdrv_register(&bdrv_host_device);
|
|
|
|
}
|
|
|
|
|
2010-04-08 00:30:24 +04:00
|
|
|
block_init(bdrv_file_init);
|