2009-07-31 23:45:56 +04:00
|
|
|
/*
|
|
|
|
* Block driver for the Virtual Disk Image (VDI) format
|
|
|
|
*
|
2012-01-21 16:54:24 +04:00
|
|
|
* Copyright (c) 2009, 2012 Stefan Weil
|
2009-07-31 23:45:56 +04:00
|
|
|
*
|
2024-09-11 18:12:03 +03:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*
|
2009-07-31 23:45:56 +04:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
2024-09-11 18:12:03 +03:00
|
|
|
* (at your option) any later version.
|
2009-07-31 23:45:56 +04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Reference:
|
|
|
|
* http://forums.virtualbox.org/viewtopic.php?t=8046
|
|
|
|
*
|
|
|
|
* This driver supports create / read / write operations on VDI images.
|
|
|
|
*
|
|
|
|
* Todo (see also TODO in code):
|
|
|
|
*
|
|
|
|
* Some features like snapshots are still missing.
|
|
|
|
*
|
|
|
|
* Deallocation of zero-filled blocks and shrinking images are missing, too
|
|
|
|
* (might be added to common block layer).
|
|
|
|
*
|
|
|
|
* Allocation of blocks could be optimized (less writes to block map and
|
|
|
|
* header).
|
|
|
|
*
|
2014-03-24 12:30:17 +04:00
|
|
|
* Read and write of adjacent blocks could be done in one operation
|
2009-07-31 23:45:56 +04:00
|
|
|
* (current code uses one operation per block (1 MiB).
|
|
|
|
*
|
|
|
|
* The code is not thread safe (missing locks for changes in header and
|
|
|
|
* block table, no problem with current QEMU).
|
|
|
|
*
|
|
|
|
* Hints:
|
|
|
|
*
|
|
|
|
* Blocks (VDI documentation) correspond to clusters (QEMU).
|
|
|
|
* QEMU's backing files could be implemented using VDI snapshot files (TODO).
|
|
|
|
* VDI snapshot files may also contain the complete machine state.
|
|
|
|
* Maybe this machine state can be converted to QEMU PC machine snapshot data.
|
|
|
|
*
|
|
|
|
* The driver keeps a block cache (little endian entries) in memory.
|
|
|
|
* For the standard block size (1 MiB), a 1 TiB disk will use 4 MiB RAM,
|
|
|
|
* so this seems to be reasonable.
|
|
|
|
*/
|
|
|
|
|
2016-01-18 21:01:42 +03:00
|
|
|
#include "qemu/osdep.h"
|
2018-06-25 15:41:54 +03:00
|
|
|
#include "qemu/units.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"
|
2018-03-12 19:55:26 +03:00
|
|
|
#include "qapi/qobject-input-visitor.h"
|
|
|
|
#include "qapi/qapi-visit-block-core.h"
|
2012-12-17 21:19:44 +04:00
|
|
|
#include "block/block_int.h"
|
2018-06-14 22:14:34 +03:00
|
|
|
#include "block/qdict.h"
|
2016-03-08 17:57:05 +03:00
|
|
|
#include "sysemu/block-backend.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-03-15 19:22:36 +03:00
|
|
|
#include "qemu/bswap.h"
|
2017-04-06 13:00:28 +03:00
|
|
|
#include "migration/blocker.h"
|
2015-09-01 16:48:02 +03:00
|
|
|
#include "qemu/coroutine.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/cutils.h"
|
2016-09-21 07:27:17 +03:00
|
|
|
#include "qemu/uuid.h"
|
2022-02-26 21:07:23 +03:00
|
|
|
#include "qemu/memalign.h"
|
2009-07-31 23:45:56 +04:00
|
|
|
|
|
|
|
/* Code configuration options. */
|
|
|
|
|
|
|
|
/* Enable debug messages. */
|
|
|
|
//~ #define CONFIG_VDI_DEBUG
|
|
|
|
|
|
|
|
/* Support write operations on VDI images. */
|
|
|
|
#define CONFIG_VDI_WRITE
|
|
|
|
|
|
|
|
/* Support non-standard block (cluster) size. This is untested.
|
|
|
|
* Maybe it will be needed for very large images.
|
|
|
|
*/
|
|
|
|
//~ #define CONFIG_VDI_BLOCK_SIZE
|
|
|
|
|
|
|
|
/* Support static (fixed, pre-allocated) images. */
|
|
|
|
#define CONFIG_VDI_STATIC_IMAGE
|
|
|
|
|
|
|
|
/* Command line option for static images. */
|
|
|
|
#define BLOCK_OPT_STATIC "static"
|
|
|
|
|
2024-10-08 19:47:08 +03:00
|
|
|
#define SECTOR_SIZE 512ULL
|
2019-01-11 22:14:01 +03:00
|
|
|
#define DEFAULT_CLUSTER_SIZE 1048576
|
|
|
|
/* Note: can't use 1 * MiB, because it's passed to stringify() */
|
2009-07-31 23:45:56 +04:00
|
|
|
|
|
|
|
#if defined(CONFIG_VDI_DEBUG)
|
2018-02-13 23:26:56 +03:00
|
|
|
#define VDI_DEBUG 1
|
2009-07-31 23:45:56 +04:00
|
|
|
#else
|
2018-02-13 23:26:56 +03:00
|
|
|
#define VDI_DEBUG 0
|
2009-07-31 23:45:56 +04:00
|
|
|
#endif
|
|
|
|
|
2018-02-13 23:26:56 +03:00
|
|
|
#define logout(fmt, ...) \
|
|
|
|
do { \
|
|
|
|
if (VDI_DEBUG) { \
|
|
|
|
fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2009-07-31 23:45:56 +04:00
|
|
|
/* Image signature. */
|
|
|
|
#define VDI_SIGNATURE 0xbeda107f
|
|
|
|
|
|
|
|
/* Image version. */
|
|
|
|
#define VDI_VERSION_1_1 0x00010001
|
|
|
|
|
|
|
|
/* Image type. */
|
|
|
|
#define VDI_TYPE_DYNAMIC 1
|
|
|
|
#define VDI_TYPE_STATIC 2
|
|
|
|
|
|
|
|
/* Innotek / SUN images use these strings in header.text:
|
|
|
|
* "<<< innotek VirtualBox Disk Image >>>\n"
|
|
|
|
* "<<< Sun xVM VirtualBox Disk Image >>>\n"
|
|
|
|
* "<<< Sun VirtualBox Disk Image >>>\n"
|
|
|
|
* The value does not matter, so QEMU created images use a different text.
|
|
|
|
*/
|
|
|
|
#define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
|
|
|
|
|
2011-10-26 23:51:18 +04:00
|
|
|
/* A never-allocated block; semantically arbitrary content. */
|
|
|
|
#define VDI_UNALLOCATED 0xffffffffU
|
|
|
|
|
|
|
|
/* A discarded (no longer allocated) block; semantically zero-filled. */
|
|
|
|
#define VDI_DISCARDED 0xfffffffeU
|
|
|
|
|
|
|
|
#define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2014-10-28 13:12:32 +03:00
|
|
|
/* The bmap will take up VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) bytes; since
|
|
|
|
* the bmap is read and written in a single operation, its size needs to be
|
|
|
|
* limited to INT_MAX; furthermore, when opening an image, the bmap size is
|
|
|
|
* rounded up to be aligned on BDRV_SECTOR_SIZE.
|
|
|
|
* Therefore this should satisfy the following:
|
|
|
|
* VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) + BDRV_SECTOR_SIZE == INT_MAX + 1
|
|
|
|
* (INT_MAX + 1 is the first value not representable as an int)
|
|
|
|
* This guarantees that any value below or equal to the constant will, when
|
|
|
|
* multiplied by sizeof(uint32_t) and rounded up to a BDRV_SECTOR_SIZE boundary,
|
|
|
|
* still be below or equal to INT_MAX. */
|
|
|
|
#define VDI_BLOCKS_IN_IMAGE_MAX \
|
|
|
|
((unsigned)((INT_MAX + 1u - BDRV_SECTOR_SIZE) / sizeof(uint32_t)))
|
2014-03-28 19:42:24 +04:00
|
|
|
#define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \
|
|
|
|
(uint64_t)DEFAULT_CLUSTER_SIZE)
|
|
|
|
|
2018-03-12 19:55:26 +03:00
|
|
|
static QemuOptsList vdi_create_opts;
|
|
|
|
|
2009-07-31 23:45:56 +04:00
|
|
|
typedef struct {
|
|
|
|
char text[0x40];
|
|
|
|
uint32_t signature;
|
|
|
|
uint32_t version;
|
|
|
|
uint32_t header_size;
|
|
|
|
uint32_t image_type;
|
|
|
|
uint32_t image_flags;
|
|
|
|
char description[256];
|
|
|
|
uint32_t offset_bmap;
|
|
|
|
uint32_t offset_data;
|
|
|
|
uint32_t cylinders; /* disk geometry, unused here */
|
|
|
|
uint32_t heads; /* disk geometry, unused here */
|
|
|
|
uint32_t sectors; /* disk geometry, unused here */
|
|
|
|
uint32_t sector_size;
|
|
|
|
uint32_t unused1;
|
|
|
|
uint64_t disk_size;
|
|
|
|
uint32_t block_size;
|
|
|
|
uint32_t block_extra; /* unused here */
|
|
|
|
uint32_t blocks_in_image;
|
|
|
|
uint32_t blocks_allocated;
|
2016-09-21 07:27:17 +03:00
|
|
|
QemuUUID uuid_image;
|
|
|
|
QemuUUID uuid_last_snap;
|
|
|
|
QemuUUID uuid_link;
|
|
|
|
QemuUUID uuid_parent;
|
2009-07-31 23:45:56 +04:00
|
|
|
uint64_t unused2[7];
|
2013-09-25 20:08:48 +04:00
|
|
|
} QEMU_PACKED VdiHeader;
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2019-05-01 21:13:56 +03:00
|
|
|
QEMU_BUILD_BUG_ON(sizeof(VdiHeader) != 512);
|
|
|
|
|
2009-07-31 23:45:56 +04:00
|
|
|
typedef struct {
|
|
|
|
/* The block map entries are little endian (even in memory). */
|
|
|
|
uint32_t *bmap;
|
|
|
|
/* Size of block (bytes). */
|
|
|
|
uint32_t block_size;
|
|
|
|
/* First sector of block map. */
|
|
|
|
uint32_t bmap_sector;
|
2011-03-13 17:44:02 +03:00
|
|
|
/* VDI header (converted to host endianness). */
|
2009-07-31 23:45:56 +04:00
|
|
|
VdiHeader header;
|
2011-11-22 19:46:26 +04:00
|
|
|
|
2017-06-29 16:27:41 +03:00
|
|
|
CoRwlock bmap_lock;
|
2015-02-27 22:54:39 +03:00
|
|
|
|
2011-11-22 19:46:26 +04:00
|
|
|
Error *migration_blocker;
|
2009-07-31 23:45:56 +04:00
|
|
|
} BDRVVdiState;
|
|
|
|
|
|
|
|
static void vdi_header_to_cpu(VdiHeader *header)
|
|
|
|
{
|
2018-10-16 20:25:03 +03:00
|
|
|
header->signature = le32_to_cpu(header->signature);
|
|
|
|
header->version = le32_to_cpu(header->version);
|
|
|
|
header->header_size = le32_to_cpu(header->header_size);
|
|
|
|
header->image_type = le32_to_cpu(header->image_type);
|
|
|
|
header->image_flags = le32_to_cpu(header->image_flags);
|
|
|
|
header->offset_bmap = le32_to_cpu(header->offset_bmap);
|
|
|
|
header->offset_data = le32_to_cpu(header->offset_data);
|
|
|
|
header->cylinders = le32_to_cpu(header->cylinders);
|
|
|
|
header->heads = le32_to_cpu(header->heads);
|
|
|
|
header->sectors = le32_to_cpu(header->sectors);
|
|
|
|
header->sector_size = le32_to_cpu(header->sector_size);
|
|
|
|
header->disk_size = le64_to_cpu(header->disk_size);
|
|
|
|
header->block_size = le32_to_cpu(header->block_size);
|
|
|
|
header->block_extra = le32_to_cpu(header->block_extra);
|
|
|
|
header->blocks_in_image = le32_to_cpu(header->blocks_in_image);
|
|
|
|
header->blocks_allocated = le32_to_cpu(header->blocks_allocated);
|
2018-12-10 14:26:49 +03:00
|
|
|
header->uuid_image = qemu_uuid_bswap(header->uuid_image);
|
|
|
|
header->uuid_last_snap = qemu_uuid_bswap(header->uuid_last_snap);
|
|
|
|
header->uuid_link = qemu_uuid_bswap(header->uuid_link);
|
|
|
|
header->uuid_parent = qemu_uuid_bswap(header->uuid_parent);
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vdi_header_to_le(VdiHeader *header)
|
|
|
|
{
|
2018-10-16 20:25:03 +03:00
|
|
|
header->signature = cpu_to_le32(header->signature);
|
|
|
|
header->version = cpu_to_le32(header->version);
|
|
|
|
header->header_size = cpu_to_le32(header->header_size);
|
|
|
|
header->image_type = cpu_to_le32(header->image_type);
|
|
|
|
header->image_flags = cpu_to_le32(header->image_flags);
|
|
|
|
header->offset_bmap = cpu_to_le32(header->offset_bmap);
|
|
|
|
header->offset_data = cpu_to_le32(header->offset_data);
|
|
|
|
header->cylinders = cpu_to_le32(header->cylinders);
|
|
|
|
header->heads = cpu_to_le32(header->heads);
|
|
|
|
header->sectors = cpu_to_le32(header->sectors);
|
|
|
|
header->sector_size = cpu_to_le32(header->sector_size);
|
|
|
|
header->disk_size = cpu_to_le64(header->disk_size);
|
|
|
|
header->block_size = cpu_to_le32(header->block_size);
|
|
|
|
header->block_extra = cpu_to_le32(header->block_extra);
|
|
|
|
header->blocks_in_image = cpu_to_le32(header->blocks_in_image);
|
|
|
|
header->blocks_allocated = cpu_to_le32(header->blocks_allocated);
|
2018-12-10 14:26:49 +03:00
|
|
|
header->uuid_image = qemu_uuid_bswap(header->uuid_image);
|
|
|
|
header->uuid_last_snap = qemu_uuid_bswap(header->uuid_last_snap);
|
|
|
|
header->uuid_link = qemu_uuid_bswap(header->uuid_link);
|
|
|
|
header->uuid_parent = qemu_uuid_bswap(header->uuid_parent);
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vdi_header_print(VdiHeader *header)
|
|
|
|
{
|
2023-10-26 10:06:34 +03:00
|
|
|
char uuidstr[UUID_STR_LEN];
|
2018-12-10 14:26:48 +03:00
|
|
|
QemuUUID uuid;
|
2009-07-31 23:45:56 +04:00
|
|
|
logout("text %s", header->text);
|
2013-01-18 00:45:26 +04:00
|
|
|
logout("signature 0x%08x\n", header->signature);
|
2009-07-31 23:45:56 +04:00
|
|
|
logout("header size 0x%04x\n", header->header_size);
|
|
|
|
logout("image type 0x%04x\n", header->image_type);
|
|
|
|
logout("image flags 0x%04x\n", header->image_flags);
|
|
|
|
logout("description %s\n", header->description);
|
|
|
|
logout("offset bmap 0x%04x\n", header->offset_bmap);
|
|
|
|
logout("offset data 0x%04x\n", header->offset_data);
|
|
|
|
logout("cylinders 0x%04x\n", header->cylinders);
|
|
|
|
logout("heads 0x%04x\n", header->heads);
|
|
|
|
logout("sectors 0x%04x\n", header->sectors);
|
|
|
|
logout("sector size 0x%04x\n", header->sector_size);
|
|
|
|
logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n",
|
|
|
|
header->disk_size, header->disk_size / MiB);
|
|
|
|
logout("block size 0x%04x\n", header->block_size);
|
|
|
|
logout("block extra 0x%04x\n", header->block_extra);
|
|
|
|
logout("blocks tot. 0x%04x\n", header->blocks_in_image);
|
|
|
|
logout("blocks all. 0x%04x\n", header->blocks_allocated);
|
2018-12-10 14:26:48 +03:00
|
|
|
uuid = header->uuid_image;
|
|
|
|
qemu_uuid_unparse(&uuid, uuidstr);
|
|
|
|
logout("uuid image %s\n", uuidstr);
|
|
|
|
uuid = header->uuid_last_snap;
|
|
|
|
qemu_uuid_unparse(&uuid, uuidstr);
|
|
|
|
logout("uuid snap %s\n", uuidstr);
|
|
|
|
uuid = header->uuid_link;
|
|
|
|
qemu_uuid_unparse(&uuid, uuidstr);
|
|
|
|
logout("uuid link %s\n", uuidstr);
|
|
|
|
uuid = header->uuid_parent;
|
|
|
|
qemu_uuid_unparse(&uuid, uuidstr);
|
|
|
|
logout("uuid parent %s\n", uuidstr);
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2018-03-01 19:36:19 +03:00
|
|
|
static int coroutine_fn vdi_co_check(BlockDriverState *bs, BdrvCheckResult *res,
|
|
|
|
BdrvCheckMode fix)
|
2009-07-31 23:45:56 +04:00
|
|
|
{
|
|
|
|
/* TODO: additional checks possible. */
|
|
|
|
BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
|
|
|
|
uint32_t blocks_allocated = 0;
|
|
|
|
uint32_t block;
|
|
|
|
uint32_t *bmap;
|
|
|
|
logout("\n");
|
|
|
|
|
2012-05-11 18:07:02 +04:00
|
|
|
if (fix) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
block: Use g_new() & friends where that makes obvious sense
g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer,
for two reasons. One, it catches multiplication overflowing size_t.
Two, it returns T * rather than void *, which lets the compiler catch
more type errors.
Patch created with Coccinelle, with two manual changes on top:
* Add const to bdrv_iterate_format() to keep the types straight
* Convert the allocation in bdrv_drop_intermediate(), which Coccinelle
inexplicably misses
Coccinelle semantic patch:
@@
type T;
@@
-g_malloc(sizeof(T))
+g_new(T, 1)
@@
type T;
@@
-g_try_malloc(sizeof(T))
+g_try_new(T, 1)
@@
type T;
@@
-g_malloc0(sizeof(T))
+g_new0(T, 1)
@@
type T;
@@
-g_try_malloc0(sizeof(T))
+g_try_new0(T, 1)
@@
type T;
expression n;
@@
-g_malloc(sizeof(T) * (n))
+g_new(T, n)
@@
type T;
expression n;
@@
-g_try_malloc(sizeof(T) * (n))
+g_try_new(T, n)
@@
type T;
expression n;
@@
-g_malloc0(sizeof(T) * (n))
+g_new0(T, n)
@@
type T;
expression n;
@@
-g_try_malloc0(sizeof(T) * (n))
+g_try_new0(T, n)
@@
type T;
expression p, n;
@@
-g_realloc(p, sizeof(T) * (n))
+g_renew(T, p, n)
@@
type T;
expression p, n;
@@
-g_try_realloc(p, sizeof(T) * (n))
+g_try_renew(T, p, n)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Jeff Cody <jcody@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2014-08-19 12:31:08 +04:00
|
|
|
bmap = g_try_new(uint32_t, s->header.blocks_in_image);
|
2014-05-20 15:25:43 +04:00
|
|
|
if (s->header.blocks_in_image && bmap == NULL) {
|
|
|
|
res->check_errors++;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2009-07-31 23:45:56 +04:00
|
|
|
memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
|
|
|
|
|
|
|
|
/* Check block map and value of blocks_allocated. */
|
|
|
|
for (block = 0; block < s->header.blocks_in_image; block++) {
|
|
|
|
uint32_t bmap_entry = le32_to_cpu(s->bmap[block]);
|
2011-10-26 23:51:18 +04:00
|
|
|
if (VDI_IS_ALLOCATED(bmap_entry)) {
|
2009-07-31 23:45:56 +04:00
|
|
|
if (bmap_entry < s->header.blocks_in_image) {
|
|
|
|
blocks_allocated++;
|
2011-10-26 23:51:18 +04:00
|
|
|
if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
|
2009-07-31 23:45:56 +04:00
|
|
|
bmap[bmap_entry] = bmap_entry;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "ERROR: block index %" PRIu32
|
|
|
|
" also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
|
2010-06-29 14:37:54 +04:00
|
|
|
res->corruptions++;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "ERROR: block index %" PRIu32
|
|
|
|
" too large, is %" PRIu32 "\n", block, bmap_entry);
|
2010-06-29 14:37:54 +04:00
|
|
|
res->corruptions++;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (blocks_allocated != s->header.blocks_allocated) {
|
|
|
|
fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32
|
|
|
|
", should be %" PRIu32 "\n",
|
|
|
|
blocks_allocated, s->header.blocks_allocated);
|
2010-06-29 14:37:54 +04:00
|
|
|
res->corruptions++;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(bmap);
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2010-06-29 14:37:54 +04:00
|
|
|
return 0;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2023-01-13 23:42:08 +03:00
|
|
|
static int coroutine_fn
|
|
|
|
vdi_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
2009-07-31 23:45:56 +04:00
|
|
|
{
|
2023-01-13 23:42:08 +03:00
|
|
|
/* TODO: vdi_co_get_info would be needed for machine snapshots.
|
2009-07-31 23:45:56 +04:00
|
|
|
vm_state_offset is still missing. */
|
|
|
|
BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
|
|
|
|
logout("\n");
|
|
|
|
bdi->cluster_size = s->block_size;
|
|
|
|
bdi->vm_state_offset = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vdi_make_empty(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
/* TODO: missing code. */
|
|
|
|
logout("\n");
|
|
|
|
/* The return value for missing code must be 0, see block.c. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
|
|
|
|
{
|
|
|
|
const VdiHeader *header = (const VdiHeader *)buf;
|
2014-07-24 01:22:59 +04:00
|
|
|
int ret = 0;
|
2009-07-31 23:45:56 +04:00
|
|
|
|
|
|
|
logout("\n");
|
|
|
|
|
|
|
|
if (buf_size < sizeof(*header)) {
|
|
|
|
/* Header too small, no VDI. */
|
|
|
|
} else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
|
2014-07-24 01:22:59 +04:00
|
|
|
ret = 100;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2014-07-24 01:22:59 +04:00
|
|
|
if (ret == 0) {
|
2009-07-31 23:45:56 +04:00
|
|
|
logout("no vdi image\n");
|
|
|
|
} else {
|
|
|
|
logout("%s", header->text);
|
|
|
|
}
|
|
|
|
|
2014-07-24 01:22:59 +04:00
|
|
|
return ret;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2013-09-05 16:22:29 +04:00
|
|
|
static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
2009-07-31 23:45:56 +04:00
|
|
|
{
|
|
|
|
BDRVVdiState *s = bs->opaque;
|
|
|
|
VdiHeader header;
|
|
|
|
size_t bmap_size;
|
2013-01-18 00:45:27 +04:00
|
|
|
int ret;
|
2018-12-10 14:26:48 +03:00
|
|
|
QemuUUID uuid_link, uuid_parent;
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2022-07-26 23:11:21 +03:00
|
|
|
ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
2016-12-16 20:52:37 +03:00
|
|
|
}
|
|
|
|
|
2023-10-27 18:53:32 +03:00
|
|
|
GRAPH_RDLOCK_GUARD_MAINLOOP();
|
|
|
|
|
2009-07-31 23:45:56 +04:00
|
|
|
logout("\n");
|
|
|
|
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 18:27:36 +03:00
|
|
|
ret = bdrv_pread(bs->file, 0, sizeof(header), &header, 0);
|
2013-01-18 00:45:27 +04:00
|
|
|
if (ret < 0) {
|
2009-07-31 23:45:56 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
vdi_header_to_cpu(&header);
|
2018-03-20 16:41:53 +03:00
|
|
|
if (VDI_DEBUG) {
|
|
|
|
vdi_header_print(&header);
|
|
|
|
}
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2014-03-28 19:42:24 +04:00
|
|
|
if (header.disk_size > VDI_DISK_SIZE_MAX) {
|
|
|
|
error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64
|
|
|
|
", max supported is 0x%" PRIx64 ")",
|
|
|
|
header.disk_size, VDI_DISK_SIZE_MAX);
|
|
|
|
ret = -ENOTSUP;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-12-10 14:26:48 +03:00
|
|
|
uuid_link = header.uuid_link;
|
|
|
|
uuid_parent = header.uuid_parent;
|
|
|
|
|
2010-05-12 22:25:45 +04:00
|
|
|
if (header.disk_size % SECTOR_SIZE != 0) {
|
|
|
|
/* 'VBoxManage convertfromraw' can create images with odd disk sizes.
|
|
|
|
We accept them but round the disk size to the next multiple of
|
|
|
|
SECTOR_SIZE. */
|
|
|
|
logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size);
|
2014-10-21 12:51:25 +04:00
|
|
|
header.disk_size = ROUND_UP(header.disk_size, SECTOR_SIZE);
|
2010-05-12 22:25:45 +04:00
|
|
|
}
|
|
|
|
|
2013-01-18 00:45:28 +04:00
|
|
|
if (header.signature != VDI_SIGNATURE) {
|
2014-04-29 21:03:12 +04:00
|
|
|
error_setg(errp, "Image not in VDI format (bad signature %08" PRIx32
|
|
|
|
")", header.signature);
|
2014-02-17 17:44:06 +04:00
|
|
|
ret = -EINVAL;
|
2013-01-18 00:45:28 +04:00
|
|
|
goto fail;
|
|
|
|
} else if (header.version != VDI_VERSION_1_1) {
|
2014-04-29 21:03:12 +04:00
|
|
|
error_setg(errp, "unsupported VDI image (version %" PRIu32 ".%" PRIu32
|
|
|
|
")", header.version >> 16, header.version & 0xffff);
|
2013-01-18 00:45:27 +04:00
|
|
|
ret = -ENOTSUP;
|
2009-07-31 23:45:56 +04:00
|
|
|
goto fail;
|
|
|
|
} else if (header.offset_bmap % SECTOR_SIZE != 0) {
|
|
|
|
/* We only support block maps which start on a sector boundary. */
|
2014-02-17 17:44:07 +04:00
|
|
|
error_setg(errp, "unsupported VDI image (unaligned block map offset "
|
2014-04-29 21:03:12 +04:00
|
|
|
"0x%" PRIx32 ")", header.offset_bmap);
|
2013-01-18 00:45:27 +04:00
|
|
|
ret = -ENOTSUP;
|
2009-07-31 23:45:56 +04:00
|
|
|
goto fail;
|
|
|
|
} else if (header.offset_data % SECTOR_SIZE != 0) {
|
|
|
|
/* We only support data blocks which start on a sector boundary. */
|
2014-04-29 21:03:12 +04:00
|
|
|
error_setg(errp, "unsupported VDI image (unaligned data offset 0x%"
|
|
|
|
PRIx32 ")", header.offset_data);
|
2013-01-18 00:45:27 +04:00
|
|
|
ret = -ENOTSUP;
|
2009-07-31 23:45:56 +04:00
|
|
|
goto fail;
|
|
|
|
} else if (header.sector_size != SECTOR_SIZE) {
|
2014-04-29 21:03:12 +04:00
|
|
|
error_setg(errp, "unsupported VDI image (sector size %" PRIu32
|
2024-10-08 19:47:08 +03:00
|
|
|
" is not %llu)", header.sector_size, SECTOR_SIZE);
|
2013-01-18 00:45:27 +04:00
|
|
|
ret = -ENOTSUP;
|
2009-07-31 23:45:56 +04:00
|
|
|
goto fail;
|
2014-03-28 19:42:24 +04:00
|
|
|
} else if (header.block_size != DEFAULT_CLUSTER_SIZE) {
|
2014-04-29 21:03:12 +04:00
|
|
|
error_setg(errp, "unsupported VDI image (block size %" PRIu32
|
2018-11-04 21:09:28 +03:00
|
|
|
" is not %" PRIu32 ")",
|
2018-06-25 15:41:54 +03:00
|
|
|
header.block_size, DEFAULT_CLUSTER_SIZE);
|
2013-01-18 00:45:27 +04:00
|
|
|
ret = -ENOTSUP;
|
2009-07-31 23:45:56 +04:00
|
|
|
goto fail;
|
2010-05-12 22:25:45 +04:00
|
|
|
} else if (header.disk_size >
|
|
|
|
(uint64_t)header.blocks_in_image * header.block_size) {
|
2014-02-17 17:44:07 +04:00
|
|
|
error_setg(errp, "unsupported VDI image (disk size %" PRIu64 ", "
|
|
|
|
"image bitmap has room for %" PRIu64 ")",
|
|
|
|
header.disk_size,
|
|
|
|
(uint64_t)header.blocks_in_image * header.block_size);
|
2013-01-18 00:45:27 +04:00
|
|
|
ret = -ENOTSUP;
|
2009-07-31 23:45:56 +04:00
|
|
|
goto fail;
|
2018-12-10 14:26:48 +03:00
|
|
|
} else if (!qemu_uuid_is_null(&uuid_link)) {
|
2014-02-17 17:44:07 +04:00
|
|
|
error_setg(errp, "unsupported VDI image (non-NULL link UUID)");
|
2013-01-18 00:45:27 +04:00
|
|
|
ret = -ENOTSUP;
|
2009-07-31 23:45:56 +04:00
|
|
|
goto fail;
|
2018-12-10 14:26:48 +03:00
|
|
|
} else if (!qemu_uuid_is_null(&uuid_parent)) {
|
2014-02-17 17:44:07 +04:00
|
|
|
error_setg(errp, "unsupported VDI image (non-NULL parent UUID)");
|
2013-01-18 00:45:27 +04:00
|
|
|
ret = -ENOTSUP;
|
2009-07-31 23:45:56 +04:00
|
|
|
goto fail;
|
2014-03-28 19:42:24 +04:00
|
|
|
} else if (header.blocks_in_image > VDI_BLOCKS_IN_IMAGE_MAX) {
|
|
|
|
error_setg(errp, "unsupported VDI image "
|
|
|
|
"(too many blocks %u, max is %u)",
|
|
|
|
header.blocks_in_image, VDI_BLOCKS_IN_IMAGE_MAX);
|
|
|
|
ret = -ENOTSUP;
|
|
|
|
goto fail;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bs->total_sectors = header.disk_size / SECTOR_SIZE;
|
|
|
|
|
|
|
|
s->block_size = header.block_size;
|
|
|
|
s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
|
|
|
|
s->header = header;
|
|
|
|
|
|
|
|
bmap_size = header.blocks_in_image * sizeof(uint32_t);
|
2014-10-21 12:51:25 +04:00
|
|
|
bmap_size = DIV_ROUND_UP(bmap_size, SECTOR_SIZE);
|
2015-06-16 15:19:22 +03:00
|
|
|
s->bmap = qemu_try_blockalign(bs->file->bs, bmap_size * SECTOR_SIZE);
|
2014-05-20 15:25:43 +04:00
|
|
|
if (s->bmap == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
block: Change bdrv_{pread,pwrite,pwrite_sync}() param order
Swap 'buf' and 'bytes' around for consistency with
bdrv_co_{pread,pwrite}(), and in preparation to implement these
functions using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pread(child, offset, buf, bytes, flags)
+ bdrv_pread(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite(child, offset, buf, bytes, flags)
+ bdrv_pwrite(child, offset, bytes, buf, flags)
@@ expression child, offset, buf, bytes, flags; @@
- bdrv_pwrite_sync(child, offset, buf, bytes, flags)
+ bdrv_pwrite_sync(child, offset, bytes, buf, flags)
Resulting overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Message-Id: <20220609152744.3891847-3-afaria@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-06-09 18:27:36 +03:00
|
|
|
ret = bdrv_pread(bs->file, header.offset_bmap, bmap_size * SECTOR_SIZE,
|
|
|
|
s->bmap, 0);
|
2013-01-18 00:45:27 +04:00
|
|
|
if (ret < 0) {
|
2009-07-31 23:45:56 +04:00
|
|
|
goto fail_free_bmap;
|
|
|
|
}
|
|
|
|
|
2011-11-22 19:46:26 +04:00
|
|
|
/* Disable migration when vdi images are used */
|
2015-04-08 12:29:19 +03:00
|
|
|
error_setg(&s->migration_blocker, "The vdi format used by node '%s' "
|
|
|
|
"does not support live migration",
|
|
|
|
bdrv_get_device_or_node_name(bs));
|
2023-09-29 17:51:47 +03:00
|
|
|
|
2023-10-25 22:44:26 +03:00
|
|
|
ret = migrate_add_blocker_normal(&s->migration_blocker, errp);
|
error: Avoid error_propagate() after migrate_add_blocker()
When migrate_add_blocker(blocker, &errp) is followed by
error_propagate(errp, err), we can often just as well do
migrate_add_blocker(..., errp).
Do that with this Coccinelle script:
@@
expression blocker, err, errp;
expression ret;
@@
- ret = migrate_add_blocker(blocker, &err);
- if (err) {
+ ret = migrate_add_blocker(blocker, errp);
+ if (ret < 0) {
... when != err;
- error_propagate(errp, err);
...
}
@@
expression blocker, err, errp;
@@
- migrate_add_blocker(blocker, &err);
- if (err) {
+ if (migrate_add_blocker(blocker, errp) < 0) {
... when != err;
- error_propagate(errp, err);
...
}
Double-check @err is not used afterwards. Dereferencing it would be
use after free, but checking whether it's null would be legitimate.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20200707160613.848843-43-armbru@redhat.com>
2020-07-07 19:06:10 +03:00
|
|
|
if (ret < 0) {
|
2017-01-16 14:31:53 +03:00
|
|
|
goto fail_free_bmap;
|
|
|
|
}
|
2011-11-22 19:46:26 +04:00
|
|
|
|
2017-06-29 16:27:41 +03:00
|
|
|
qemu_co_rwlock_init(&s->bmap_lock);
|
2015-02-27 22:54:39 +03:00
|
|
|
|
2009-07-31 23:45:56 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail_free_bmap:
|
2014-05-20 15:25:43 +04:00
|
|
|
qemu_vfree(s->bmap);
|
2009-07-31 23:45:56 +04:00
|
|
|
|
|
|
|
fail:
|
2013-01-18 00:45:27 +04:00
|
|
|
return ret;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2012-09-20 23:13:32 +04:00
|
|
|
static int vdi_reopen_prepare(BDRVReopenState *state,
|
|
|
|
BlockReopenQueue *queue, Error **errp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-27 18:53:29 +03:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
|
|
|
vdi_co_block_status(BlockDriverState *bs, bool want_zero, int64_t offset,
|
|
|
|
int64_t bytes, int64_t *pnum, int64_t *map,
|
|
|
|
BlockDriverState **file)
|
2009-07-31 23:45:56 +04:00
|
|
|
{
|
|
|
|
BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
|
2018-02-13 23:26:57 +03:00
|
|
|
size_t bmap_index = offset / s->block_size;
|
|
|
|
size_t index_in_block = offset % s->block_size;
|
2009-07-31 23:45:56 +04:00
|
|
|
uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]);
|
2013-09-04 21:00:30 +04:00
|
|
|
int result;
|
|
|
|
|
2018-02-13 23:26:57 +03:00
|
|
|
logout("%p, %" PRId64 ", %" PRId64 ", %p\n", bs, offset, bytes, pnum);
|
|
|
|
*pnum = MIN(s->block_size - index_in_block, bytes);
|
2013-09-04 21:00:30 +04:00
|
|
|
result = VDI_IS_ALLOCATED(bmap_entry);
|
|
|
|
if (!result) {
|
block/vdi: return ZERO block-status when appropriate
In case of !VDI_IS_ALLOCATED[], we do zero out the corresponding chunk
of qiov. So, this should be reported as ZERO.
Note that this changes visible output of "qemu-img map --output=json"
and "qemu-io -c map" commands. For qemu-img map, the change is obvious:
we just mark as zero what is really zero. For qemu-io it's less
obvious: what was unallocated now is allocated.
There is an inconsistency in understanding of unallocated regions in
Qemu: backing-supporting format-drivers return 0 block-status to report
go-to-backing logic for this area. Some protocol-drivers (iscsi) return
0 to report fs-unallocated-non-zero status (i.e., don't occupy space on
disk, read result is undefined).
BDRV_BLOCK_ALLOCATED is defined as something more close to
go-to-backing logic. Still it is calculated as ZERO | DATA, so 0 from
iscsi is treated as unallocated. It doesn't influence backing-chain
behavior, as iscsi can't have backing file. But it does influence
"qemu-io -c map".
We should solve this inconsistency at some future point. Now, let's
just make backing-not-supporting format drivers (vdi at this patch and
vpc with the following) to behave more like backing-supporting drivers
and not report 0 block-status. More over, returning ZERO status is
absolutely valid thing, and again, corresponds to how the other
format-drivers (backing-supporting) work.
After block-status update, it never reports 0, so setting
unallocated_blocks_are_zero doesn't make sense (as the only user of it
is bdrv_co_block_status and it checks unallocated_blocks_are_zero only
for unallocated areas). Drop it.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200528094405.145708-4-vsementsov@virtuozzo.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2020-05-28 12:43:58 +03:00
|
|
|
return BDRV_BLOCK_ZERO;
|
2013-09-04 21:00:30 +04:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:26:57 +03:00
|
|
|
*map = s->header.offset_data + (uint64_t)bmap_entry * s->block_size +
|
|
|
|
index_in_block;
|
2016-01-26 06:58:56 +03:00
|
|
|
*file = bs->file->bs;
|
2019-07-25 18:55:10 +03:00
|
|
|
return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID |
|
|
|
|
(s->header.image_type == VDI_TYPE_STATIC ? BDRV_BLOCK_RECURSE : 0);
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2023-02-03 18:21:50 +03:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
block: use int64_t instead of uint64_t in driver read handlers
We are generally moving to int64_t for both offset and bytes parameters
on all io paths.
Main motivation is realization of 64-bit write_zeroes operation for
fast zeroing large disk chunks, up to the whole disk.
We chose signed type, to be consistent with off_t (which is signed) and
with possibility for signed return type (where negative value means
error).
So, convert driver read handlers parameters which are already 64bit to
signed type.
While being here, convert also flags parameter to be BdrvRequestFlags.
Now let's consider all callers. Simple
git grep '\->bdrv_\(aio\|co\)_preadv\(_part\)\?'
shows that's there three callers of driver function:
bdrv_driver_preadv() in block/io.c, passes int64_t, checked by
bdrv_check_qiov_request() to be non-negative.
qcow2_load_vmstate() does bdrv_check_qiov_request().
do_perform_cow_read() has uint64_t argument. And a lot of things in
qcow2 driver are uint64_t, so converting it is big job. But we must
not work with requests that don't satisfy bdrv_check_qiov_request(),
so let's just assert it here.
Still, the functions may be called directly, not only by drv->...
Let's check:
git grep '\.bdrv_\(aio\|co\)_preadv\(_part\)\?\s*=' | \
awk '{print $4}' | sed 's/,//' | sed 's/&//' | sort | uniq | \
while read func; do git grep "$func(" | \
grep -v "$func(BlockDriverState"; done
The only one such caller:
QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, &data, 1);
...
ret = bdrv_replace_test_co_preadv(bs, 0, 1, &qiov, 0);
in tests/unit/test-bdrv-drain.c, and it's OK obviously.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20210903102807.27127-4-vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
[eblake: fix typos]
Signed-off-by: Eric Blake <eblake@redhat.com>
2021-09-03 13:27:59 +03:00
|
|
|
vdi_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
|
|
|
|
QEMUIOVector *qiov, BdrvRequestFlags flags)
|
2009-07-31 23:45:56 +04:00
|
|
|
{
|
|
|
|
BDRVVdiState *s = bs->opaque;
|
2016-04-25 17:22:39 +03:00
|
|
|
QEMUIOVector local_qiov;
|
2009-07-31 23:45:56 +04:00
|
|
|
uint32_t bmap_entry;
|
|
|
|
uint32_t block_index;
|
2016-04-25 17:22:39 +03:00
|
|
|
uint32_t offset_in_block;
|
|
|
|
uint32_t n_bytes;
|
|
|
|
uint64_t bytes_done = 0;
|
2012-03-19 21:07:51 +04:00
|
|
|
int ret = 0;
|
2012-03-19 21:07:47 +04:00
|
|
|
|
|
|
|
logout("\n");
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2016-04-25 17:22:39 +03:00
|
|
|
qemu_iovec_init(&local_qiov, qiov->niov);
|
2012-03-19 21:07:46 +04:00
|
|
|
|
2016-04-25 17:22:39 +03:00
|
|
|
while (ret >= 0 && bytes > 0) {
|
|
|
|
block_index = offset / s->block_size;
|
|
|
|
offset_in_block = offset % s->block_size;
|
|
|
|
n_bytes = MIN(bytes, s->block_size - offset_in_block);
|
|
|
|
|
|
|
|
logout("will read %u bytes starting at offset %" PRIu64 "\n",
|
|
|
|
n_bytes, offset);
|
2012-03-19 21:07:51 +04:00
|
|
|
|
|
|
|
/* prepare next AIO request */
|
2017-06-29 16:27:41 +03:00
|
|
|
qemu_co_rwlock_rdlock(&s->bmap_lock);
|
2012-03-19 21:07:51 +04:00
|
|
|
bmap_entry = le32_to_cpu(s->bmap[block_index]);
|
2017-06-29 16:27:41 +03:00
|
|
|
qemu_co_rwlock_unlock(&s->bmap_lock);
|
2012-03-19 21:07:51 +04:00
|
|
|
if (!VDI_IS_ALLOCATED(bmap_entry)) {
|
|
|
|
/* Block not allocated, return zeros, no need to wait. */
|
2016-04-25 17:22:39 +03:00
|
|
|
qemu_iovec_memset(qiov, bytes_done, 0, n_bytes);
|
2012-03-19 21:07:51 +04:00
|
|
|
ret = 0;
|
|
|
|
} else {
|
2016-04-25 17:22:39 +03:00
|
|
|
uint64_t data_offset = s->header.offset_data +
|
|
|
|
(uint64_t)bmap_entry * s->block_size +
|
|
|
|
offset_in_block;
|
|
|
|
|
|
|
|
qemu_iovec_reset(&local_qiov);
|
|
|
|
qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
|
|
|
|
|
2016-06-20 22:31:46 +03:00
|
|
|
ret = bdrv_co_preadv(bs->file, data_offset, n_bytes,
|
2016-04-25 17:22:39 +03:00
|
|
|
&local_qiov, 0);
|
2012-03-19 21:07:51 +04:00
|
|
|
}
|
2016-04-25 17:22:39 +03:00
|
|
|
logout("%u bytes read\n", n_bytes);
|
2012-03-19 21:07:46 +04:00
|
|
|
|
2016-04-25 17:22:39 +03:00
|
|
|
bytes -= n_bytes;
|
|
|
|
offset += n_bytes;
|
|
|
|
bytes_done += n_bytes;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
2012-03-19 21:07:45 +04:00
|
|
|
|
2016-04-25 17:22:39 +03:00
|
|
|
qemu_iovec_destroy(&local_qiov);
|
|
|
|
|
2012-03-19 21:07:45 +04:00
|
|
|
return ret;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2023-02-03 18:21:50 +03:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
block: use int64_t instead of uint64_t in driver write handlers
We are generally moving to int64_t for both offset and bytes parameters
on all io paths.
Main motivation is realization of 64-bit write_zeroes operation for
fast zeroing large disk chunks, up to the whole disk.
We chose signed type, to be consistent with off_t (which is signed) and
with possibility for signed return type (where negative value means
error).
So, convert driver write handlers parameters which are already 64bit to
signed type.
While being here, convert also flags parameter to be BdrvRequestFlags.
Now let's consider all callers. Simple
git grep '\->bdrv_\(aio\|co\)_pwritev\(_part\)\?'
shows that's there three callers of driver function:
bdrv_driver_pwritev() and bdrv_driver_pwritev_compressed() in
block/io.c, both pass int64_t, checked by bdrv_check_qiov_request() to
be non-negative.
qcow2_save_vmstate() does bdrv_check_qiov_request().
Still, the functions may be called directly, not only by drv->...
Let's check:
git grep '\.bdrv_\(aio\|co\)_pwritev\(_part\)\?\s*=' | \
awk '{print $4}' | sed 's/,//' | sed 's/&//' | sort | uniq | \
while read func; do git grep "$func(" | \
grep -v "$func(BlockDriverState"; done
shows several callers:
qcow2:
qcow2_co_truncate() write at most up to @offset, which is checked in
generic qcow2_co_truncate() by bdrv_check_request().
qcow2_co_pwritev_compressed_task() pass the request (or part of the
request) that already went through normal write path, so it should
be OK
qcow:
qcow_co_pwritev_compressed() pass int64_t, it's updated by this patch
quorum:
quorum_co_pwrite_zeroes() pass int64_t and int - OK
throttle:
throttle_co_pwritev_compressed() pass int64_t, it's updated by this
patch
vmdk:
vmdk_co_pwritev_compressed() pass int64_t, it's updated by this
patch
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20210903102807.27127-5-vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
2021-09-03 13:28:00 +03:00
|
|
|
vdi_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
|
|
|
|
QEMUIOVector *qiov, BdrvRequestFlags flags)
|
2009-07-31 23:45:56 +04:00
|
|
|
{
|
|
|
|
BDRVVdiState *s = bs->opaque;
|
2016-04-25 17:22:39 +03:00
|
|
|
QEMUIOVector local_qiov;
|
2009-07-31 23:45:56 +04:00
|
|
|
uint32_t bmap_entry;
|
|
|
|
uint32_t block_index;
|
2016-04-25 17:22:39 +03:00
|
|
|
uint32_t offset_in_block;
|
|
|
|
uint32_t n_bytes;
|
2017-06-29 16:27:41 +03:00
|
|
|
uint64_t data_offset;
|
2012-03-19 21:07:48 +04:00
|
|
|
uint32_t bmap_first = VDI_UNALLOCATED;
|
|
|
|
uint32_t bmap_last = VDI_UNALLOCATED;
|
|
|
|
uint8_t *block = NULL;
|
2016-04-25 17:22:39 +03:00
|
|
|
uint64_t bytes_done = 0;
|
2012-03-19 21:07:51 +04:00
|
|
|
int ret = 0;
|
2012-03-19 21:07:47 +04:00
|
|
|
|
|
|
|
logout("\n");
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2016-04-25 17:22:39 +03:00
|
|
|
qemu_iovec_init(&local_qiov, qiov->niov);
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2016-04-25 17:22:39 +03:00
|
|
|
while (ret >= 0 && bytes > 0) {
|
|
|
|
block_index = offset / s->block_size;
|
|
|
|
offset_in_block = offset % s->block_size;
|
|
|
|
n_bytes = MIN(bytes, s->block_size - offset_in_block);
|
|
|
|
|
|
|
|
logout("will write %u bytes starting at offset %" PRIu64 "\n",
|
|
|
|
n_bytes, offset);
|
2012-03-19 21:07:51 +04:00
|
|
|
|
|
|
|
/* prepare next AIO request */
|
2017-06-29 16:27:41 +03:00
|
|
|
qemu_co_rwlock_rdlock(&s->bmap_lock);
|
2012-03-19 21:07:51 +04:00
|
|
|
bmap_entry = le32_to_cpu(s->bmap[block_index]);
|
|
|
|
if (!VDI_IS_ALLOCATED(bmap_entry)) {
|
|
|
|
/* Allocate new block and write to it. */
|
2017-06-29 16:27:41 +03:00
|
|
|
qemu_co_rwlock_upgrade(&s->bmap_lock);
|
|
|
|
bmap_entry = le32_to_cpu(s->bmap[block_index]);
|
|
|
|
if (VDI_IS_ALLOCATED(bmap_entry)) {
|
|
|
|
/* A concurrent allocation did the work for us. */
|
|
|
|
qemu_co_rwlock_downgrade(&s->bmap_lock);
|
|
|
|
goto nonallocating_write;
|
|
|
|
}
|
|
|
|
|
2012-03-19 21:07:51 +04:00
|
|
|
bmap_entry = s->header.blocks_allocated;
|
|
|
|
s->bmap[block_index] = cpu_to_le32(bmap_entry);
|
|
|
|
s->header.blocks_allocated++;
|
2016-04-25 17:22:39 +03:00
|
|
|
data_offset = s->header.offset_data +
|
|
|
|
(uint64_t)bmap_entry * s->block_size;
|
2012-03-19 21:07:51 +04:00
|
|
|
if (block == NULL) {
|
|
|
|
block = g_malloc(s->block_size);
|
|
|
|
bmap_first = block_index;
|
|
|
|
}
|
|
|
|
bmap_last = block_index;
|
|
|
|
/* Copy data to be written to new block and zero unused parts. */
|
2016-04-25 17:22:39 +03:00
|
|
|
memset(block, 0, offset_in_block);
|
|
|
|
qemu_iovec_to_buf(qiov, bytes_done, block + offset_in_block,
|
|
|
|
n_bytes);
|
|
|
|
memset(block + offset_in_block + n_bytes, 0,
|
|
|
|
s->block_size - n_bytes - offset_in_block);
|
2015-02-27 22:54:39 +03:00
|
|
|
|
2017-06-29 16:27:41 +03:00
|
|
|
/* Write the new block under CoRwLock write-side protection,
|
|
|
|
* so this full-cluster write does not overlap a partial write
|
|
|
|
* of the same cluster, issued from the "else" branch.
|
|
|
|
*/
|
2022-10-13 15:37:08 +03:00
|
|
|
ret = bdrv_co_pwrite(bs->file, data_offset, s->block_size, block,
|
|
|
|
0);
|
2017-06-29 16:27:41 +03:00
|
|
|
qemu_co_rwlock_unlock(&s->bmap_lock);
|
2012-03-19 21:07:51 +04:00
|
|
|
} else {
|
2017-06-29 16:27:41 +03:00
|
|
|
nonallocating_write:
|
|
|
|
data_offset = s->header.offset_data +
|
|
|
|
(uint64_t)bmap_entry * s->block_size +
|
|
|
|
offset_in_block;
|
|
|
|
qemu_co_rwlock_unlock(&s->bmap_lock);
|
2016-04-25 17:22:39 +03:00
|
|
|
|
|
|
|
qemu_iovec_reset(&local_qiov);
|
|
|
|
qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
|
|
|
|
|
2016-06-20 22:31:46 +03:00
|
|
|
ret = bdrv_co_pwritev(bs->file, data_offset, n_bytes,
|
2016-04-25 17:22:39 +03:00
|
|
|
&local_qiov, 0);
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
2012-03-19 21:07:46 +04:00
|
|
|
|
2016-04-25 17:22:39 +03:00
|
|
|
bytes -= n_bytes;
|
|
|
|
offset += n_bytes;
|
|
|
|
bytes_done += n_bytes;
|
2012-03-19 21:07:46 +04:00
|
|
|
|
2016-04-25 17:22:39 +03:00
|
|
|
logout("%u bytes written\n", n_bytes);
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2016-04-25 17:22:39 +03:00
|
|
|
qemu_iovec_destroy(&local_qiov);
|
|
|
|
|
2012-03-19 21:07:46 +04:00
|
|
|
logout("finished data write\n");
|
2012-03-19 21:07:50 +04:00
|
|
|
if (ret < 0) {
|
2021-03-25 14:29:36 +03:00
|
|
|
g_free(block);
|
2012-03-19 21:07:50 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block) {
|
|
|
|
/* One or more new blocks were allocated. */
|
2021-03-25 14:29:37 +03:00
|
|
|
VdiHeader *header;
|
2012-03-19 21:07:50 +04:00
|
|
|
uint8_t *base;
|
2023-09-21 15:13:10 +03:00
|
|
|
uint64_t bmap_offset;
|
2016-04-25 17:22:39 +03:00
|
|
|
uint32_t n_sectors;
|
2012-03-19 21:07:50 +04:00
|
|
|
|
2021-03-25 14:29:37 +03:00
|
|
|
g_free(block);
|
|
|
|
header = g_malloc(sizeof(*header));
|
|
|
|
|
2012-03-19 21:07:50 +04:00
|
|
|
logout("now writing modified header\n");
|
|
|
|
assert(VDI_IS_ALLOCATED(bmap_first));
|
|
|
|
*header = s->header;
|
|
|
|
vdi_header_to_le(header);
|
2022-10-13 15:37:08 +03:00
|
|
|
ret = bdrv_co_pwrite(bs->file, 0, sizeof(*header), header, 0);
|
2021-03-25 14:29:37 +03:00
|
|
|
g_free(header);
|
2012-03-19 21:07:50 +04:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
2012-03-19 21:07:46 +04:00
|
|
|
}
|
2012-03-19 21:07:50 +04:00
|
|
|
|
|
|
|
logout("now writing modified block map entry %u...%u\n",
|
|
|
|
bmap_first, bmap_last);
|
|
|
|
/* Write modified sectors from block map. */
|
|
|
|
bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
|
|
|
|
bmap_last /= (SECTOR_SIZE / sizeof(uint32_t));
|
|
|
|
n_sectors = bmap_last - bmap_first + 1;
|
2023-09-21 15:13:10 +03:00
|
|
|
bmap_offset = s->bmap_sector + bmap_first;
|
2012-03-19 21:07:50 +04:00
|
|
|
base = ((uint8_t *)&s->bmap[0]) + bmap_first * SECTOR_SIZE;
|
|
|
|
logout("will write %u block map sectors starting from entry %u\n",
|
|
|
|
n_sectors, bmap_first);
|
2023-09-21 15:13:10 +03:00
|
|
|
ret = bdrv_co_pwrite(bs->file, bmap_offset * SECTOR_SIZE,
|
2022-10-13 15:37:08 +03:00
|
|
|
n_sectors * SECTOR_SIZE, base, 0);
|
2012-03-19 21:07:46 +04:00
|
|
|
}
|
|
|
|
|
2022-06-09 18:27:37 +03:00
|
|
|
return ret;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 23:35:54 +03:00
|
|
|
static int coroutine_fn GRAPH_UNLOCKED
|
|
|
|
vdi_co_do_create(BlockdevCreateOptions *create_options, size_t block_size,
|
|
|
|
Error **errp)
|
2009-07-31 23:45:56 +04:00
|
|
|
{
|
block/vdi: Fix missing ERRP_GUARD() for error_prepend()
As the comment in qapi/error, passing @errp to error_prepend() requires
ERRP_GUARD():
* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
...
* - It should not be passed to error_prepend(), error_vprepend() or
* error_append_hint(), because that doesn't work with &error_fatal.
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
ERRP_GUARD() could avoid the case when @errp is &error_fatal, the user
can't see this additional information, because exit() happens in
error_setg earlier than information is added [1].
The vdi_co_do_create() passes @errp to error_prepend() without
ERRP_GUARD(), and its @errp parameter is so widely sourced that it is
necessary to protect it with ERRP_GUARD().
To avoid the potential issues as [1] said, add missing ERRP_GUARD() at
the beginning of this function.
[1]: Issue description in the commit message of commit ae7c80a7bd73
("error: New macro ERRP_GUARD()").
Cc: Stefan Weil <sw@weilnetz.de>
Cc: Kevin Wolf <kwolf@redhat.com>
Cc: Hanna Reitz <hreitz@redhat.com>
Cc: qemu-block@nongnu.org
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Message-ID: <20240311033822.3142585-12-zhao1.liu@linux.intel.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
2024-03-11 06:38:04 +03:00
|
|
|
ERRP_GUARD();
|
2018-03-12 19:55:28 +03:00
|
|
|
BlockdevCreateOptionsVdi *vdi_opts;
|
2014-07-24 01:22:59 +04:00
|
|
|
int ret = 0;
|
2009-07-31 23:45:56 +04:00
|
|
|
uint64_t bytes = 0;
|
|
|
|
uint32_t blocks;
|
2018-03-20 17:08:00 +03:00
|
|
|
uint32_t image_type;
|
2009-07-31 23:45:56 +04:00
|
|
|
VdiHeader header;
|
|
|
|
size_t i;
|
|
|
|
size_t bmap_size;
|
2014-07-24 01:22:58 +04:00
|
|
|
int64_t offset = 0;
|
2018-03-12 19:55:27 +03:00
|
|
|
BlockDriverState *bs_file = NULL;
|
2016-03-08 17:57:05 +03:00
|
|
|
BlockBackend *blk = NULL;
|
2014-07-24 01:22:58 +04:00
|
|
|
uint32_t *bmap = NULL;
|
2018-12-10 14:26:48 +03:00
|
|
|
QemuUUID uuid;
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2018-03-12 19:55:28 +03:00
|
|
|
assert(create_options->driver == BLOCKDEV_DRIVER_VDI);
|
|
|
|
vdi_opts = &create_options->u.vdi;
|
|
|
|
|
2009-07-31 23:45:56 +04:00
|
|
|
logout("\n");
|
|
|
|
|
2018-03-09 21:53:19 +03:00
|
|
|
/* Validate options and set default values */
|
2018-03-12 19:55:26 +03:00
|
|
|
bytes = vdi_opts->size;
|
2018-03-20 17:08:00 +03:00
|
|
|
|
|
|
|
if (!vdi_opts->has_preallocation) {
|
|
|
|
vdi_opts->preallocation = PREALLOC_MODE_OFF;
|
|
|
|
}
|
|
|
|
switch (vdi_opts->preallocation) {
|
|
|
|
case PREALLOC_MODE_OFF:
|
|
|
|
image_type = VDI_TYPE_DYNAMIC;
|
|
|
|
break;
|
|
|
|
case PREALLOC_MODE_METADATA:
|
2014-06-05 13:21:07 +04:00
|
|
|
image_type = VDI_TYPE_STATIC;
|
2018-03-20 17:08:00 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error_setg(errp, "Preallocation mode not supported for vdi");
|
|
|
|
return -EINVAL;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
2018-03-20 17:08:00 +03:00
|
|
|
|
2018-03-12 19:55:26 +03:00
|
|
|
#ifndef CONFIG_VDI_STATIC_IMAGE
|
|
|
|
if (image_type == VDI_TYPE_STATIC) {
|
|
|
|
ret = -ENOTSUP;
|
|
|
|
error_setg(errp, "Statically allocated images cannot be created in "
|
|
|
|
"this build");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_VDI_BLOCK_SIZE
|
|
|
|
if (block_size != DEFAULT_CLUSTER_SIZE) {
|
|
|
|
ret = -ENOTSUP;
|
|
|
|
error_setg(errp,
|
|
|
|
"A non-default cluster size is not supported in this build");
|
|
|
|
goto exit;
|
|
|
|
}
|
2014-06-05 13:21:07 +04:00
|
|
|
#endif
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2014-03-28 19:42:24 +04:00
|
|
|
if (bytes > VDI_DISK_SIZE_MAX) {
|
2014-07-24 01:22:59 +04:00
|
|
|
ret = -ENOTSUP;
|
2014-03-28 19:42:24 +04:00
|
|
|
error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64
|
|
|
|
", max supported is 0x%" PRIx64 ")",
|
|
|
|
bytes, VDI_DISK_SIZE_MAX);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2018-03-09 21:53:19 +03:00
|
|
|
/* Create BlockBackend to write to the image */
|
2023-01-26 20:24:27 +03:00
|
|
|
bs_file = bdrv_co_open_blockdev_ref(vdi_opts->file, errp);
|
2018-03-12 19:55:27 +03:00
|
|
|
if (!bs_file) {
|
|
|
|
ret = -EIO;
|
2014-03-28 19:42:24 +04:00
|
|
|
goto exit;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
2016-03-08 17:57:05 +03:00
|
|
|
|
2023-01-26 20:24:27 +03:00
|
|
|
blk = blk_co_new_with_bs(bs_file, BLK_PERM_WRITE | BLK_PERM_RESIZE,
|
|
|
|
BLK_PERM_ALL, errp);
|
2020-04-28 22:26:46 +03:00
|
|
|
if (!blk) {
|
|
|
|
ret = -EPERM;
|
2014-07-24 01:22:58 +04:00
|
|
|
goto exit;
|
qemu-img create: add 'nocow' option
Add 'nocow' option so that users could have a chance to set NOCOW flag to
newly created files. It's useful on btrfs file system to enhance performance.
Btrfs has low performance when hosting VM images, even more when the guest
in those VM are also using btrfs as file system. One way to mitigate this bad
performance is to turn off COW attributes on VM files. Generally, there are
two ways to turn off NOCOW on btrfs: a) by mounting fs with nodatacow, then
all newly created files will be NOCOW. b) per file. Add the NOCOW file
attribute. It could only be done to empty or new files.
This patch tries the second way, according to the option, it could add NOCOW
per file.
For most block drivers, since the create file step is in raw-posix.c, so we
can do setting NOCOW flag ioctl in raw-posix.c only.
But there are some exceptions, like block/vpc.c and block/vdi.c, they are
creating file by calling qemu_open directly. For them, do the same setting
NOCOW flag ioctl work in them separately.
[Fixed up 082.out due to the new 'nocow' creation option
--Stefan]
Signed-off-by: Chunyan Liu <cyliu@suse.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2014-06-30 10:29:58 +04:00
|
|
|
}
|
|
|
|
|
2016-03-08 17:57:05 +03:00
|
|
|
blk_set_allow_write_beyond_eof(blk, true);
|
|
|
|
|
2010-05-12 22:25:45 +04:00
|
|
|
/* We need enough blocks to store the given disk size,
|
|
|
|
so always round up. */
|
2014-10-21 12:51:25 +04:00
|
|
|
blocks = DIV_ROUND_UP(bytes, block_size);
|
2010-05-12 22:25:45 +04:00
|
|
|
|
2009-07-31 23:45:56 +04:00
|
|
|
bmap_size = blocks * sizeof(uint32_t);
|
2014-10-21 12:51:25 +04:00
|
|
|
bmap_size = ROUND_UP(bmap_size, SECTOR_SIZE);
|
2009-07-31 23:45:56 +04:00
|
|
|
|
|
|
|
memset(&header, 0, sizeof(header));
|
2009-08-15 15:33:58 +04:00
|
|
|
pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
|
2009-07-31 23:45:56 +04:00
|
|
|
header.signature = VDI_SIGNATURE;
|
|
|
|
header.version = VDI_VERSION_1_1;
|
|
|
|
header.header_size = 0x180;
|
|
|
|
header.image_type = image_type;
|
|
|
|
header.offset_bmap = 0x200;
|
|
|
|
header.offset_data = 0x200 + bmap_size;
|
|
|
|
header.sector_size = SECTOR_SIZE;
|
|
|
|
header.disk_size = bytes;
|
|
|
|
header.block_size = block_size;
|
|
|
|
header.blocks_in_image = blocks;
|
2009-08-14 23:50:02 +04:00
|
|
|
if (image_type == VDI_TYPE_STATIC) {
|
|
|
|
header.blocks_allocated = blocks;
|
|
|
|
}
|
2018-12-10 14:26:48 +03:00
|
|
|
qemu_uuid_generate(&uuid);
|
|
|
|
header.uuid_image = uuid;
|
|
|
|
qemu_uuid_generate(&uuid);
|
|
|
|
header.uuid_last_snap = uuid;
|
2009-07-31 23:45:56 +04:00
|
|
|
/* There is no need to set header.uuid_link or header.uuid_parent here. */
|
2018-03-20 16:41:53 +03:00
|
|
|
if (VDI_DEBUG) {
|
|
|
|
vdi_header_print(&header);
|
|
|
|
}
|
2009-07-31 23:45:56 +04:00
|
|
|
vdi_header_to_le(&header);
|
2022-10-13 15:37:08 +03:00
|
|
|
ret = blk_co_pwrite(blk, offset, sizeof(header), &header, 0);
|
2014-07-24 01:22:59 +04:00
|
|
|
if (ret < 0) {
|
2018-03-12 19:55:27 +03:00
|
|
|
error_setg(errp, "Error writing header");
|
2014-07-24 01:22:58 +04:00
|
|
|
goto exit;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
2014-07-24 01:22:58 +04:00
|
|
|
offset += sizeof(header);
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2010-05-06 22:53:47 +04:00
|
|
|
if (bmap_size > 0) {
|
2014-05-20 15:25:43 +04:00
|
|
|
bmap = g_try_malloc0(bmap_size);
|
|
|
|
if (bmap == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
error_setg(errp, "Could not allocate bmap");
|
|
|
|
goto exit;
|
|
|
|
}
|
2012-08-17 17:23:24 +04:00
|
|
|
for (i = 0; i < blocks; i++) {
|
|
|
|
if (image_type == VDI_TYPE_STATIC) {
|
|
|
|
bmap[i] = i;
|
|
|
|
} else {
|
|
|
|
bmap[i] = VDI_UNALLOCATED;
|
|
|
|
}
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
2022-10-13 15:37:08 +03:00
|
|
|
ret = blk_co_pwrite(blk, offset, bmap_size, bmap, 0);
|
2014-07-24 01:22:59 +04:00
|
|
|
if (ret < 0) {
|
2018-03-12 19:55:27 +03:00
|
|
|
error_setg(errp, "Error writing bmap");
|
2014-07-24 01:22:58 +04:00
|
|
|
goto exit;
|
2012-08-17 17:23:24 +04:00
|
|
|
}
|
2014-07-24 01:22:58 +04:00
|
|
|
offset += bmap_size;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
2012-08-17 17:23:24 +04:00
|
|
|
|
2009-07-31 23:45:56 +04:00
|
|
|
if (image_type == VDI_TYPE_STATIC) {
|
2022-10-13 15:37:08 +03:00
|
|
|
ret = blk_co_truncate(blk, offset + blocks * block_size, false,
|
|
|
|
PREALLOC_MODE_OFF, 0, errp);
|
2014-07-24 01:22:59 +04:00
|
|
|
if (ret < 0) {
|
2018-03-12 19:55:27 +03:00
|
|
|
error_prepend(errp, "Failed to statically allocate file");
|
2014-07-24 01:22:58 +04:00
|
|
|
goto exit;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 15:48:16 +03:00
|
|
|
ret = 0;
|
2014-03-28 19:42:24 +04:00
|
|
|
exit:
|
2023-05-04 14:57:33 +03:00
|
|
|
blk_co_unref(blk);
|
|
|
|
bdrv_co_unref(bs_file);
|
2014-07-24 01:22:58 +04:00
|
|
|
g_free(bmap);
|
2014-07-24 01:22:59 +04:00
|
|
|
return ret;
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 23:35:54 +03:00
|
|
|
static int coroutine_fn GRAPH_UNLOCKED
|
|
|
|
vdi_co_create(BlockdevCreateOptions *create_options, Error **errp)
|
2018-03-12 19:55:28 +03:00
|
|
|
{
|
|
|
|
return vdi_co_do_create(create_options, DEFAULT_CLUSTER_SIZE, errp);
|
|
|
|
}
|
|
|
|
|
2023-05-10 23:35:54 +03:00
|
|
|
static int coroutine_fn GRAPH_UNLOCKED
|
2023-02-03 18:21:55 +03:00
|
|
|
vdi_co_create_opts(BlockDriver *drv, const char *filename,
|
|
|
|
QemuOpts *opts, Error **errp)
|
2018-03-12 19:55:26 +03:00
|
|
|
{
|
|
|
|
QDict *qdict = NULL;
|
2018-03-12 19:55:28 +03:00
|
|
|
BlockdevCreateOptions *create_options = NULL;
|
2018-03-12 19:55:27 +03:00
|
|
|
BlockDriverState *bs_file = NULL;
|
2018-03-12 19:55:26 +03:00
|
|
|
uint64_t block_size = DEFAULT_CLUSTER_SIZE;
|
2018-03-20 17:08:00 +03:00
|
|
|
bool is_static = false;
|
2018-03-12 19:55:26 +03:00
|
|
|
Visitor *v;
|
|
|
|
int ret;
|
|
|
|
|
2018-03-09 21:53:19 +03:00
|
|
|
/* Parse options and convert legacy syntax.
|
|
|
|
*
|
|
|
|
* Since CONFIG_VDI_BLOCK_SIZE is disabled by default,
|
2018-03-12 19:55:26 +03:00
|
|
|
* cluster-size is not part of the QAPI schema; therefore we have
|
|
|
|
* to parse it before creating the QAPI object. */
|
|
|
|
#if defined(CONFIG_VDI_BLOCK_SIZE)
|
|
|
|
block_size = qemu_opt_get_size_del(opts,
|
|
|
|
BLOCK_OPT_CLUSTER_SIZE,
|
|
|
|
DEFAULT_CLUSTER_SIZE);
|
|
|
|
if (block_size < BDRV_SECTOR_SIZE || block_size > UINT32_MAX ||
|
|
|
|
!is_power_of_2(block_size))
|
|
|
|
{
|
|
|
|
error_setg(errp, "Invalid cluster size");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
#endif
|
2018-03-20 17:08:00 +03:00
|
|
|
if (qemu_opt_get_bool_del(opts, BLOCK_OPT_STATIC, false)) {
|
|
|
|
is_static = true;
|
|
|
|
}
|
2018-03-12 19:55:26 +03:00
|
|
|
|
|
|
|
qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vdi_create_opts, true);
|
|
|
|
|
2018-03-09 21:53:19 +03:00
|
|
|
/* Create and open the file (protocol layer) */
|
2022-11-28 17:23:31 +03:00
|
|
|
ret = bdrv_co_create_file(filename, opts, errp);
|
2018-03-12 19:55:27 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2023-01-26 20:24:27 +03:00
|
|
|
bs_file = bdrv_co_open(filename, NULL, NULL,
|
|
|
|
BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
|
2018-03-12 19:55:27 +03:00
|
|
|
if (!bs_file) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2018-03-12 19:55:28 +03:00
|
|
|
qdict_put_str(qdict, "driver", "vdi");
|
2018-03-12 19:55:27 +03:00
|
|
|
qdict_put_str(qdict, "file", bs_file->node_name);
|
2018-03-20 17:08:00 +03:00
|
|
|
if (is_static) {
|
|
|
|
qdict_put_str(qdict, "preallocation", "metadata");
|
|
|
|
}
|
2018-03-12 19:55:26 +03:00
|
|
|
|
|
|
|
/* Get the QAPI object */
|
2018-06-14 22:14:34 +03:00
|
|
|
v = qobject_input_visitor_new_flat_confused(qdict, errp);
|
|
|
|
if (!v) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2020-07-07 19:06:07 +03:00
|
|
|
visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
|
2018-03-12 19:55:26 +03:00
|
|
|
visit_free(v);
|
2020-07-07 19:06:07 +03:00
|
|
|
if (!create_options) {
|
2018-03-12 19:55:26 +03:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2018-03-09 21:53:19 +03:00
|
|
|
/* Silently round up size */
|
2018-03-12 19:55:28 +03:00
|
|
|
assert(create_options->driver == BLOCKDEV_DRIVER_VDI);
|
|
|
|
create_options->u.vdi.size = ROUND_UP(create_options->u.vdi.size,
|
|
|
|
BDRV_SECTOR_SIZE);
|
2018-03-12 19:55:26 +03:00
|
|
|
|
2018-03-09 21:53:19 +03:00
|
|
|
/* Create the vdi image (format layer) */
|
2018-03-12 19:55:27 +03:00
|
|
|
ret = vdi_co_do_create(create_options, block_size, errp);
|
2018-03-12 19:55:26 +03:00
|
|
|
done:
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(qdict);
|
2018-03-12 19:55:28 +03:00
|
|
|
qapi_free_BlockdevCreateOptions(create_options);
|
2023-05-04 14:57:33 +03:00
|
|
|
bdrv_co_unref(bs_file);
|
2018-03-12 19:55:26 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-07-31 23:45:56 +04:00
|
|
|
static void vdi_close(BlockDriverState *bs)
|
|
|
|
{
|
2011-11-22 19:46:26 +04:00
|
|
|
BDRVVdiState *s = bs->opaque;
|
2011-11-22 19:57:34 +04:00
|
|
|
|
2014-05-20 15:25:43 +04:00
|
|
|
qemu_vfree(s->bmap);
|
2011-11-22 19:57:34 +04:00
|
|
|
|
2023-10-18 16:03:36 +03:00
|
|
|
migrate_del_blocker(&s->migration_blocker);
|
2009-07-31 23:45:56 +04:00
|
|
|
}
|
|
|
|
|
2023-10-27 18:53:11 +03:00
|
|
|
static int GRAPH_RDLOCK vdi_has_zero_init(BlockDriverState *bs)
|
2019-07-24 20:12:35 +03:00
|
|
|
{
|
|
|
|
BDRVVdiState *s = bs->opaque;
|
|
|
|
|
|
|
|
if (s->header.image_type == VDI_TYPE_STATIC) {
|
|
|
|
return bdrv_has_zero_init(bs->file->bs);
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 13:21:07 +04:00
|
|
|
static QemuOptsList vdi_create_opts = {
|
|
|
|
.name = "vdi-create-opts",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(vdi_create_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_SIZE,
|
|
|
|
.type = QEMU_OPT_SIZE,
|
|
|
|
.help = "Virtual disk size"
|
|
|
|
},
|
2009-07-31 23:45:56 +04:00
|
|
|
#if defined(CONFIG_VDI_BLOCK_SIZE)
|
2014-06-05 13:21:07 +04:00
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_CLUSTER_SIZE,
|
|
|
|
.type = QEMU_OPT_SIZE,
|
|
|
|
.help = "VDI cluster (block) size",
|
|
|
|
.def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
|
|
|
|
},
|
2009-07-31 23:45:56 +04:00
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_VDI_STATIC_IMAGE)
|
2014-06-05 13:21:07 +04:00
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_STATIC,
|
|
|
|
.type = QEMU_OPT_BOOL,
|
|
|
|
.help = "VDI static (pre-allocated) image",
|
|
|
|
.def_value_str = "off"
|
|
|
|
},
|
2009-07-31 23:45:56 +04:00
|
|
|
#endif
|
2014-06-05 13:21:07 +04:00
|
|
|
/* TODO: An additional option to set UUID values might be useful. */
|
|
|
|
{ /* end of list */ }
|
|
|
|
}
|
2009-07-31 23:45:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static BlockDriver bdrv_vdi = {
|
|
|
|
.format_name = "vdi",
|
|
|
|
.instance_size = sizeof(BDRVVdiState),
|
|
|
|
.bdrv_probe = vdi_probe,
|
|
|
|
.bdrv_open = vdi_open,
|
|
|
|
.bdrv_close = vdi_close,
|
2012-09-20 23:13:32 +04:00
|
|
|
.bdrv_reopen_prepare = vdi_reopen_prepare,
|
2020-05-13 14:05:39 +03:00
|
|
|
.bdrv_child_perm = bdrv_default_perms,
|
2018-03-12 19:55:28 +03:00
|
|
|
.bdrv_co_create = vdi_co_create,
|
2018-03-09 21:53:19 +03:00
|
|
|
.bdrv_co_create_opts = vdi_co_create_opts,
|
2019-07-24 20:12:35 +03:00
|
|
|
.bdrv_has_zero_init = vdi_has_zero_init,
|
2018-02-13 23:26:57 +03:00
|
|
|
.bdrv_co_block_status = vdi_co_block_status,
|
2009-07-31 23:45:56 +04:00
|
|
|
.bdrv_make_empty = vdi_make_empty,
|
|
|
|
|
2016-04-25 17:22:39 +03:00
|
|
|
.bdrv_co_preadv = vdi_co_preadv,
|
2009-07-31 23:45:56 +04:00
|
|
|
#if defined(CONFIG_VDI_WRITE)
|
2016-04-25 17:22:39 +03:00
|
|
|
.bdrv_co_pwritev = vdi_co_pwritev,
|
2009-07-31 23:45:56 +04:00
|
|
|
#endif
|
|
|
|
|
2023-01-13 23:42:08 +03:00
|
|
|
.bdrv_co_get_info = vdi_co_get_info,
|
2009-07-31 23:45:56 +04:00
|
|
|
|
2020-05-13 14:05:12 +03:00
|
|
|
.is_format = true,
|
2014-06-05 13:21:07 +04:00
|
|
|
.create_opts = &vdi_create_opts,
|
2018-03-01 19:36:19 +03:00
|
|
|
.bdrv_co_check = vdi_co_check,
|
2009-07-31 23:45:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void bdrv_vdi_init(void)
|
|
|
|
{
|
|
|
|
logout("\n");
|
|
|
|
bdrv_register(&bdrv_vdi);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_vdi_init);
|