2010-12-06 17:08:00 +01:00
|
|
|
/*
|
|
|
|
* QEMU Enhanced Disk Format
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2010
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-18 19:01:42 +01: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 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/timer.h"
|
2016-03-15 17:22:36 +01:00
|
|
|
#include "qemu/bswap.h"
|
2010-12-06 17:08:02 +01:00
|
|
|
#include "trace.h"
|
2010-12-06 17:08:00 +01:00
|
|
|
#include "qed.h"
|
2012-12-17 18:19:43 +01:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2016-03-08 15:57:05 +01:00
|
|
|
#include "sysemu/block-backend.h"
|
2010-12-06 17:08:00 +01:00
|
|
|
|
2012-10-31 16:34:37 +01:00
|
|
|
static const AIOCBInfo qed_aiocb_info = {
|
2010-12-06 17:08:02 +01:00
|
|
|
.aiocb_size = sizeof(QEDAIOCB),
|
|
|
|
};
|
|
|
|
|
2010-12-06 17:08:00 +01:00
|
|
|
static int bdrv_qed_probe(const uint8_t *buf, int buf_size,
|
|
|
|
const char *filename)
|
|
|
|
{
|
|
|
|
const QEDHeader *header = (const QEDHeader *)buf;
|
|
|
|
|
|
|
|
if (buf_size < sizeof(*header)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (le32_to_cpu(header->magic) != QED_MAGIC) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether an image format is raw
|
|
|
|
*
|
|
|
|
* @fmt: Backing file format, may be NULL
|
|
|
|
*/
|
|
|
|
static bool qed_fmt_is_raw(const char *fmt)
|
|
|
|
{
|
|
|
|
return fmt && strcmp(fmt, "raw") == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_header_le_to_cpu(const QEDHeader *le, QEDHeader *cpu)
|
|
|
|
{
|
|
|
|
cpu->magic = le32_to_cpu(le->magic);
|
|
|
|
cpu->cluster_size = le32_to_cpu(le->cluster_size);
|
|
|
|
cpu->table_size = le32_to_cpu(le->table_size);
|
|
|
|
cpu->header_size = le32_to_cpu(le->header_size);
|
|
|
|
cpu->features = le64_to_cpu(le->features);
|
|
|
|
cpu->compat_features = le64_to_cpu(le->compat_features);
|
|
|
|
cpu->autoclear_features = le64_to_cpu(le->autoclear_features);
|
|
|
|
cpu->l1_table_offset = le64_to_cpu(le->l1_table_offset);
|
|
|
|
cpu->image_size = le64_to_cpu(le->image_size);
|
|
|
|
cpu->backing_filename_offset = le32_to_cpu(le->backing_filename_offset);
|
|
|
|
cpu->backing_filename_size = le32_to_cpu(le->backing_filename_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_header_cpu_to_le(const QEDHeader *cpu, QEDHeader *le)
|
|
|
|
{
|
|
|
|
le->magic = cpu_to_le32(cpu->magic);
|
|
|
|
le->cluster_size = cpu_to_le32(cpu->cluster_size);
|
|
|
|
le->table_size = cpu_to_le32(cpu->table_size);
|
|
|
|
le->header_size = cpu_to_le32(cpu->header_size);
|
|
|
|
le->features = cpu_to_le64(cpu->features);
|
|
|
|
le->compat_features = cpu_to_le64(cpu->compat_features);
|
|
|
|
le->autoclear_features = cpu_to_le64(cpu->autoclear_features);
|
|
|
|
le->l1_table_offset = cpu_to_le64(cpu->l1_table_offset);
|
|
|
|
le->image_size = cpu_to_le64(cpu->image_size);
|
|
|
|
le->backing_filename_offset = cpu_to_le32(cpu->backing_filename_offset);
|
|
|
|
le->backing_filename_size = cpu_to_le32(cpu->backing_filename_size);
|
|
|
|
}
|
|
|
|
|
2012-08-09 14:05:54 +02:00
|
|
|
int qed_write_header_sync(BDRVQEDState *s)
|
2010-12-06 17:08:00 +01:00
|
|
|
{
|
|
|
|
QEDHeader le;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
qed_header_cpu_to_le(&s->header, &le);
|
2016-06-20 20:09:15 +02:00
|
|
|
ret = bdrv_pwrite(s->bs->file, 0, &le, sizeof(le));
|
2010-12-06 17:08:00 +01:00
|
|
|
if (ret != sizeof(le)) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:03 +01:00
|
|
|
typedef struct {
|
|
|
|
GenericCB gencb;
|
|
|
|
BDRVQEDState *s;
|
|
|
|
struct iovec iov;
|
|
|
|
QEMUIOVector qiov;
|
|
|
|
int nsectors;
|
|
|
|
uint8_t *buf;
|
|
|
|
} QEDWriteHeaderCB;
|
|
|
|
|
|
|
|
static void qed_write_header_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
QEDWriteHeaderCB *write_header_cb = opaque;
|
|
|
|
|
|
|
|
qemu_vfree(write_header_cb->buf);
|
|
|
|
gencb_complete(write_header_cb, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_write_header_read_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
QEDWriteHeaderCB *write_header_cb = opaque;
|
|
|
|
BDRVQEDState *s = write_header_cb->s;
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
qed_write_header_cb(write_header_cb, ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update header */
|
|
|
|
qed_header_cpu_to_le(&s->header, (QEDHeader *)write_header_cb->buf);
|
|
|
|
|
2016-05-30 12:06:33 +02:00
|
|
|
bdrv_aio_writev(s->bs->file, 0, &write_header_cb->qiov,
|
2011-11-30 09:12:30 +01:00
|
|
|
write_header_cb->nsectors, qed_write_header_cb,
|
|
|
|
write_header_cb);
|
2010-12-06 17:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update header in-place (does not rewrite backing filename or other strings)
|
|
|
|
*
|
|
|
|
* This function only updates known header fields in-place and does not affect
|
|
|
|
* extra data after the QED header.
|
|
|
|
*/
|
2014-10-07 13:59:15 +02:00
|
|
|
static void qed_write_header(BDRVQEDState *s, BlockCompletionFunc cb,
|
2010-12-06 17:08:03 +01:00
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
/* We must write full sectors for O_DIRECT but cannot necessarily generate
|
|
|
|
* the data following the header if an unrecognized compat feature is
|
|
|
|
* active. Therefore, first read the sectors containing the header, update
|
|
|
|
* them, and write back.
|
|
|
|
*/
|
|
|
|
|
2016-05-31 18:35:53 +02:00
|
|
|
int nsectors = DIV_ROUND_UP(sizeof(QEDHeader), BDRV_SECTOR_SIZE);
|
2010-12-06 17:08:03 +01:00
|
|
|
size_t len = nsectors * BDRV_SECTOR_SIZE;
|
|
|
|
QEDWriteHeaderCB *write_header_cb = gencb_alloc(sizeof(*write_header_cb),
|
|
|
|
cb, opaque);
|
|
|
|
|
|
|
|
write_header_cb->s = s;
|
|
|
|
write_header_cb->nsectors = nsectors;
|
|
|
|
write_header_cb->buf = qemu_blockalign(s->bs, len);
|
|
|
|
write_header_cb->iov.iov_base = write_header_cb->buf;
|
|
|
|
write_header_cb->iov.iov_len = len;
|
|
|
|
qemu_iovec_init_external(&write_header_cb->qiov, &write_header_cb->iov, 1);
|
|
|
|
|
2016-05-30 12:06:33 +02:00
|
|
|
bdrv_aio_readv(s->bs->file, 0, &write_header_cb->qiov, nsectors,
|
2011-11-30 09:12:30 +01:00
|
|
|
qed_write_header_read_cb, write_header_cb);
|
2010-12-06 17:08:03 +01:00
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:00 +01:00
|
|
|
static uint64_t qed_max_image_size(uint32_t cluster_size, uint32_t table_size)
|
|
|
|
{
|
|
|
|
uint64_t table_entries;
|
|
|
|
uint64_t l2_size;
|
|
|
|
|
|
|
|
table_entries = (table_size * cluster_size) / sizeof(uint64_t);
|
|
|
|
l2_size = table_entries * cluster_size;
|
|
|
|
|
|
|
|
return l2_size * table_entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool qed_is_cluster_size_valid(uint32_t cluster_size)
|
|
|
|
{
|
|
|
|
if (cluster_size < QED_MIN_CLUSTER_SIZE ||
|
|
|
|
cluster_size > QED_MAX_CLUSTER_SIZE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (cluster_size & (cluster_size - 1)) {
|
|
|
|
return false; /* not power of 2 */
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool qed_is_table_size_valid(uint32_t table_size)
|
|
|
|
{
|
|
|
|
if (table_size < QED_MIN_TABLE_SIZE ||
|
|
|
|
table_size > QED_MAX_TABLE_SIZE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (table_size & (table_size - 1)) {
|
|
|
|
return false; /* not power of 2 */
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
|
|
|
|
uint32_t table_size)
|
|
|
|
{
|
|
|
|
if (image_size % BDRV_SECTOR_SIZE != 0) {
|
|
|
|
return false; /* not multiple of sector size */
|
|
|
|
}
|
|
|
|
if (image_size > qed_max_image_size(cluster_size, table_size)) {
|
|
|
|
return false; /* image is too large */
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a string of known length from the image file
|
|
|
|
*
|
|
|
|
* @file: Image file
|
|
|
|
* @offset: File offset to start of string, in bytes
|
|
|
|
* @n: String length in bytes
|
|
|
|
* @buf: Destination buffer
|
|
|
|
* @buflen: Destination buffer length in bytes
|
|
|
|
* @ret: 0 on success, -errno on failure
|
|
|
|
*
|
|
|
|
* The string is NUL-terminated.
|
|
|
|
*/
|
2016-06-20 18:24:02 +02:00
|
|
|
static int qed_read_string(BdrvChild *file, uint64_t offset, size_t n,
|
2010-12-06 17:08:00 +01:00
|
|
|
char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if (n >= buflen) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
ret = bdrv_pread(file, offset, buf, n);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
buf[n] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:02 +01:00
|
|
|
/**
|
|
|
|
* Allocate new clusters
|
|
|
|
*
|
|
|
|
* @s: QED state
|
|
|
|
* @n: Number of contiguous clusters to allocate
|
|
|
|
* @ret: Offset of first allocated cluster
|
|
|
|
*
|
|
|
|
* This function only produces the offset where the new clusters should be
|
|
|
|
* written. It updates BDRVQEDState but does not make any changes to the image
|
|
|
|
* file.
|
|
|
|
*/
|
|
|
|
static uint64_t qed_alloc_clusters(BDRVQEDState *s, unsigned int n)
|
|
|
|
{
|
|
|
|
uint64_t offset = s->file_size;
|
|
|
|
s->file_size += n * s->header.cluster_size;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:01 +01:00
|
|
|
QEDTable *qed_alloc_table(BDRVQEDState *s)
|
|
|
|
{
|
|
|
|
/* Honor O_DIRECT memory alignment requirements */
|
|
|
|
return qemu_blockalign(s->bs,
|
|
|
|
s->header.cluster_size * s->header.table_size);
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:02 +01:00
|
|
|
/**
|
|
|
|
* Allocate a new zeroed L2 table
|
|
|
|
*/
|
|
|
|
static CachedL2Table *qed_new_l2_table(BDRVQEDState *s)
|
|
|
|
{
|
|
|
|
CachedL2Table *l2_table = qed_alloc_l2_cache_entry(&s->l2_cache);
|
|
|
|
|
|
|
|
l2_table->table = qed_alloc_table(s);
|
|
|
|
l2_table->offset = qed_alloc_clusters(s, s->header.table_size);
|
|
|
|
|
|
|
|
memset(l2_table->table->offsets, 0,
|
|
|
|
s->header.cluster_size * s->header.table_size);
|
|
|
|
return l2_table;
|
|
|
|
}
|
|
|
|
|
2017-02-13 14:52:27 +01:00
|
|
|
static void qed_aio_next_io(QEDAIOCB *acb, int ret);
|
|
|
|
|
|
|
|
static void qed_aio_start_io(QEDAIOCB *acb)
|
|
|
|
{
|
|
|
|
qed_aio_next_io(acb, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_aio_next_io_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
|
|
|
|
|
|
|
qed_aio_next_io(acb, ret);
|
|
|
|
}
|
2010-12-06 17:08:02 +01:00
|
|
|
|
2011-05-09 17:45:40 +02:00
|
|
|
static void qed_plug_allocating_write_reqs(BDRVQEDState *s)
|
|
|
|
{
|
|
|
|
assert(!s->allocating_write_reqs_plugged);
|
|
|
|
|
|
|
|
s->allocating_write_reqs_plugged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_unplug_allocating_write_reqs(BDRVQEDState *s)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb;
|
|
|
|
|
|
|
|
assert(s->allocating_write_reqs_plugged);
|
|
|
|
|
|
|
|
s->allocating_write_reqs_plugged = false;
|
|
|
|
|
|
|
|
acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
|
|
|
|
if (acb) {
|
2017-02-13 14:52:27 +01:00
|
|
|
qed_aio_start_io(acb);
|
2011-05-09 17:45:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_finish_clear_need_check(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
/* Do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_flush_after_clear_need_check(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
BDRVQEDState *s = opaque;
|
|
|
|
|
|
|
|
bdrv_aio_flush(s->bs, qed_finish_clear_need_check, s);
|
|
|
|
|
|
|
|
/* No need to wait until flush completes */
|
|
|
|
qed_unplug_allocating_write_reqs(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_clear_need_check(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
BDRVQEDState *s = opaque;
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
qed_unplug_allocating_write_reqs(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->header.features &= ~QED_F_NEED_CHECK;
|
|
|
|
qed_write_header(s, qed_flush_after_clear_need_check, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_need_check_timer_cb(void *opaque)
|
|
|
|
{
|
|
|
|
BDRVQEDState *s = opaque;
|
|
|
|
|
|
|
|
/* The timer should only fire when allocating writes have drained */
|
|
|
|
assert(!QSIMPLEQ_FIRST(&s->allocating_write_reqs));
|
|
|
|
|
|
|
|
trace_qed_need_check_timer_cb(s);
|
|
|
|
|
2017-02-13 14:52:29 +01:00
|
|
|
qed_acquire(s);
|
2011-05-09 17:45:40 +02:00
|
|
|
qed_plug_allocating_write_reqs(s);
|
|
|
|
|
|
|
|
/* Ensure writes are on disk before clearing flag */
|
2016-10-27 12:48:54 +02:00
|
|
|
bdrv_aio_flush(s->bs->file->bs, qed_clear_need_check, s);
|
2017-02-13 14:52:29 +01:00
|
|
|
qed_release(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qed_acquire(BDRVQEDState *s)
|
|
|
|
{
|
|
|
|
aio_context_acquire(bdrv_get_aio_context(s->bs));
|
|
|
|
}
|
|
|
|
|
|
|
|
void qed_release(BDRVQEDState *s)
|
|
|
|
{
|
|
|
|
aio_context_release(bdrv_get_aio_context(s->bs));
|
2011-05-09 17:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_start_need_check_timer(BDRVQEDState *s)
|
|
|
|
{
|
|
|
|
trace_qed_start_need_check_timer(s);
|
|
|
|
|
2013-08-21 17:03:08 +02:00
|
|
|
/* Use QEMU_CLOCK_VIRTUAL so we don't alter the image file while suspended for
|
2011-05-09 17:45:40 +02:00
|
|
|
* migration.
|
|
|
|
*/
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_mod(s->need_check_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
|
2016-03-21 17:02:30 +01:00
|
|
|
NANOSECONDS_PER_SECOND * QED_NEED_CHECK_TIMEOUT);
|
2011-05-09 17:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* It's okay to call this multiple times or when no timer is started */
|
|
|
|
static void qed_cancel_need_check_timer(BDRVQEDState *s)
|
|
|
|
{
|
|
|
|
trace_qed_cancel_need_check_timer(s);
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_del(s->need_check_timer);
|
2011-05-09 17:45:40 +02:00
|
|
|
}
|
|
|
|
|
2014-05-08 16:34:45 +02:00
|
|
|
static void bdrv_qed_detach_aio_context(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
|
|
|
|
qed_cancel_need_check_timer(s);
|
|
|
|
timer_free(s->need_check_timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bdrv_qed_attach_aio_context(BlockDriverState *bs,
|
|
|
|
AioContext *new_context)
|
|
|
|
{
|
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
|
|
|
|
s->need_check_timer = aio_timer_new(new_context,
|
|
|
|
QEMU_CLOCK_VIRTUAL, SCALE_NS,
|
|
|
|
qed_need_check_timer_cb, s);
|
|
|
|
if (s->header.features & QED_F_NEED_CHECK) {
|
|
|
|
qed_start_need_check_timer(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 12:48:54 +02:00
|
|
|
static void bdrv_qed_drain(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
|
|
|
|
/* Fire the timer immediately in order to start doing I/O as soon as the
|
|
|
|
* header is flushed.
|
|
|
|
*/
|
|
|
|
if (s->need_check_timer && timer_pending(s->need_check_timer)) {
|
|
|
|
qed_cancel_need_check_timer(s);
|
|
|
|
qed_need_check_timer_cb(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:52:37 +01:00
|
|
|
static int bdrv_qed_do_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
2010-12-06 17:08:00 +01:00
|
|
|
{
|
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
QEDHeader le_header;
|
|
|
|
int64_t file_size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
s->bs = bs;
|
2010-12-06 17:08:02 +01:00
|
|
|
QSIMPLEQ_INIT(&s->allocating_write_reqs);
|
2010-12-06 17:08:00 +01:00
|
|
|
|
2016-06-20 18:24:02 +02:00
|
|
|
ret = bdrv_pread(bs->file, 0, &le_header, sizeof(le_header));
|
2010-12-06 17:08:00 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
qed_header_le_to_cpu(&le_header, &s->header);
|
|
|
|
|
|
|
|
if (s->header.magic != QED_MAGIC) {
|
2014-02-17 14:44:06 +01:00
|
|
|
error_setg(errp, "Image not in QED format");
|
|
|
|
return -EINVAL;
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
if (s->header.features & ~QED_FEATURE_MASK) {
|
2011-02-09 11:13:26 +01:00
|
|
|
/* image uses unsupported feature bits */
|
2016-03-16 19:54:33 +01:00
|
|
|
error_setg(errp, "Unsupported QED features: %" PRIx64,
|
|
|
|
s->header.features & ~QED_FEATURE_MASK);
|
2011-02-09 11:13:26 +01:00
|
|
|
return -ENOTSUP;
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
if (!qed_is_cluster_size_valid(s->header.cluster_size)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Round down file size to the last cluster */
|
2015-06-16 14:19:22 +02:00
|
|
|
file_size = bdrv_getlength(bs->file->bs);
|
2010-12-06 17:08:00 +01:00
|
|
|
if (file_size < 0) {
|
|
|
|
return file_size;
|
|
|
|
}
|
|
|
|
s->file_size = qed_start_of_cluster(s, file_size);
|
|
|
|
|
|
|
|
if (!qed_is_table_size_valid(s->header.table_size)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (!qed_is_image_size_valid(s->header.image_size,
|
|
|
|
s->header.cluster_size,
|
|
|
|
s->header.table_size)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (!qed_check_table_offset(s, s->header.l1_table_offset)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->table_nelems = (s->header.cluster_size * s->header.table_size) /
|
|
|
|
sizeof(uint64_t);
|
2015-03-23 16:29:26 +01:00
|
|
|
s->l2_shift = ctz32(s->header.cluster_size);
|
2010-12-06 17:08:00 +01:00
|
|
|
s->l2_mask = s->table_nelems - 1;
|
2015-03-23 16:29:26 +01:00
|
|
|
s->l1_shift = s->l2_shift + ctz32(s->table_nelems);
|
2010-12-06 17:08:00 +01:00
|
|
|
|
2015-01-12 13:31:32 +01:00
|
|
|
/* Header size calculation must not overflow uint32_t */
|
|
|
|
if (s->header.header_size > UINT32_MAX / s->header.cluster_size) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:00 +01:00
|
|
|
if ((s->header.features & QED_F_BACKING_FILE)) {
|
|
|
|
if ((uint64_t)s->header.backing_filename_offset +
|
|
|
|
s->header.backing_filename_size >
|
|
|
|
s->header.cluster_size * s->header.header_size) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-06-20 18:24:02 +02:00
|
|
|
ret = qed_read_string(bs->file, s->header.backing_filename_offset,
|
2010-12-06 17:08:00 +01:00
|
|
|
s->header.backing_filename_size, bs->backing_file,
|
|
|
|
sizeof(bs->backing_file));
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->header.features & QED_F_BACKING_FORMAT_NO_PROBE) {
|
|
|
|
pstrcpy(bs->backing_format, sizeof(bs->backing_format), "raw");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset unknown autoclear feature bits. This is a backwards
|
|
|
|
* compatibility mechanism that allows images to be opened by older
|
|
|
|
* programs, which "knock out" unknown feature bits. When an image is
|
|
|
|
* opened by a newer program again it can detect that the autoclear
|
|
|
|
* feature is no longer valid.
|
|
|
|
*/
|
|
|
|
if ((s->header.autoclear_features & ~QED_AUTOCLEAR_FEATURE_MASK) != 0 &&
|
2016-01-13 15:56:06 +01:00
|
|
|
!bdrv_is_read_only(bs->file->bs) && !(flags & BDRV_O_INACTIVE)) {
|
2010-12-06 17:08:00 +01:00
|
|
|
s->header.autoclear_features &= QED_AUTOCLEAR_FEATURE_MASK;
|
|
|
|
|
|
|
|
ret = qed_write_header_sync(s);
|
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* From here on only known autoclear feature bits are valid */
|
2015-06-16 14:19:22 +02:00
|
|
|
bdrv_flush(bs->file->bs);
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:01 +01:00
|
|
|
s->l1_table = qed_alloc_table(s);
|
|
|
|
qed_init_l2_cache(&s->l2_cache);
|
|
|
|
|
|
|
|
ret = qed_read_l1_table_sync(s);
|
2010-12-06 17:08:03 +01:00
|
|
|
if (ret) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If image was not closed cleanly, check consistency */
|
2012-08-09 14:05:56 +02:00
|
|
|
if (!(flags & BDRV_O_CHECK) && (s->header.features & QED_F_NEED_CHECK)) {
|
2010-12-06 17:08:03 +01:00
|
|
|
/* Read-only images cannot be fixed. There is no risk of corruption
|
|
|
|
* since write operations are not possible. Therefore, allow
|
|
|
|
* potentially inconsistent images to be opened read-only. This can
|
|
|
|
* aid data recovery from an otherwise inconsistent image.
|
|
|
|
*/
|
2015-06-16 14:19:22 +02:00
|
|
|
if (!bdrv_is_read_only(bs->file->bs) &&
|
2016-01-13 15:56:06 +01:00
|
|
|
!(flags & BDRV_O_INACTIVE)) {
|
2010-12-06 17:08:03 +01:00
|
|
|
BdrvCheckResult result = {0};
|
|
|
|
|
|
|
|
ret = qed_check(s, &result, true);
|
2011-05-09 17:45:40 +02:00
|
|
|
if (ret) {
|
|
|
|
goto out;
|
|
|
|
}
|
2010-12-06 17:08:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:34:45 +02:00
|
|
|
bdrv_qed_attach_aio_context(bs, bdrv_get_aio_context(bs));
|
2011-05-09 17:45:40 +02:00
|
|
|
|
2010-12-06 17:08:03 +01:00
|
|
|
out:
|
2010-12-06 17:08:01 +01:00
|
|
|
if (ret) {
|
|
|
|
qed_free_l2_cache(&s->l2_cache);
|
|
|
|
qemu_vfree(s->l1_table);
|
|
|
|
}
|
2010-12-06 17:08:00 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:52:37 +01:00
|
|
|
static int bdrv_qed_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
|
|
|
|
false, errp);
|
|
|
|
if (!bs->file) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bdrv_qed_do_open(bs, options, flags, errp);
|
|
|
|
}
|
|
|
|
|
2014-07-16 17:48:16 +02:00
|
|
|
static void bdrv_qed_refresh_limits(BlockDriverState *bs, Error **errp)
|
2013-12-11 19:26:16 +01:00
|
|
|
{
|
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
|
2016-06-01 23:10:02 +02:00
|
|
|
bs->bl.pwrite_zeroes_alignment = s->header.cluster_size;
|
2013-12-11 19:26:16 +01:00
|
|
|
}
|
|
|
|
|
2012-09-20 21:13:27 +02:00
|
|
|
/* We have nothing to do for QED reopen, stubs just return
|
|
|
|
* success */
|
|
|
|
static int bdrv_qed_reopen_prepare(BDRVReopenState *state,
|
|
|
|
BlockReopenQueue *queue, Error **errp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:00 +01:00
|
|
|
static void bdrv_qed_close(BlockDriverState *bs)
|
|
|
|
{
|
2010-12-06 17:08:01 +01:00
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
|
2014-05-08 16:34:45 +02:00
|
|
|
bdrv_qed_detach_aio_context(bs);
|
2011-05-09 17:45:40 +02:00
|
|
|
|
2010-12-06 17:08:03 +01:00
|
|
|
/* Ensure writes reach stable storage */
|
2015-06-16 14:19:22 +02:00
|
|
|
bdrv_flush(bs->file->bs);
|
2010-12-06 17:08:03 +01:00
|
|
|
|
|
|
|
/* Clean shutdown, no check required on next open */
|
|
|
|
if (s->header.features & QED_F_NEED_CHECK) {
|
|
|
|
s->header.features &= ~QED_F_NEED_CHECK;
|
|
|
|
qed_write_header_sync(s);
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:01 +01:00
|
|
|
qed_free_l2_cache(&s->l2_cache);
|
|
|
|
qemu_vfree(s->l1_table);
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int qed_create(const char *filename, uint32_t cluster_size,
|
|
|
|
uint64_t image_size, uint32_t table_size,
|
2014-02-17 14:43:59 +01:00
|
|
|
const char *backing_file, const char *backing_fmt,
|
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 08:29:58 +02:00
|
|
|
QemuOpts *opts, Error **errp)
|
2010-12-06 17:08:00 +01:00
|
|
|
{
|
|
|
|
QEDHeader header = {
|
|
|
|
.magic = QED_MAGIC,
|
|
|
|
.cluster_size = cluster_size,
|
|
|
|
.table_size = table_size,
|
|
|
|
.header_size = 1,
|
|
|
|
.features = 0,
|
|
|
|
.compat_features = 0,
|
|
|
|
.l1_table_offset = cluster_size,
|
|
|
|
.image_size = image_size,
|
|
|
|
};
|
|
|
|
QEDHeader le_header;
|
|
|
|
uint8_t *l1_table = NULL;
|
|
|
|
size_t l1_size = header.cluster_size * header.table_size;
|
2013-09-05 14:45:29 +02:00
|
|
|
Error *local_err = NULL;
|
2010-12-06 17:08:00 +01:00
|
|
|
int ret = 0;
|
2016-03-08 15:57:05 +01:00
|
|
|
BlockBackend *blk;
|
2010-12-06 17:08:00 +01:00
|
|
|
|
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 08:29:58 +02:00
|
|
|
ret = bdrv_create_file(filename, opts, &local_err);
|
2010-12-06 17:08:00 +01:00
|
|
|
if (ret < 0) {
|
2014-02-17 14:43:59 +01:00
|
|
|
error_propagate(errp, local_err);
|
2010-12-06 17:08:00 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-16 19:54:38 +01:00
|
|
|
blk = blk_new_open(filename, NULL, NULL,
|
2017-02-17 15:07:38 +01:00
|
|
|
BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
|
|
|
|
&local_err);
|
2016-03-08 15:57:05 +01:00
|
|
|
if (blk == NULL) {
|
2014-02-17 14:43:59 +01:00
|
|
|
error_propagate(errp, local_err);
|
2016-03-08 15:57:05 +01:00
|
|
|
return -EIO;
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-08 15:57:05 +01:00
|
|
|
blk_set_allow_write_beyond_eof(blk, true);
|
|
|
|
|
2011-01-14 23:44:33 +01:00
|
|
|
/* File must start empty and grow, check truncate is supported */
|
2017-03-28 22:51:27 +02:00
|
|
|
ret = blk_truncate(blk, 0, errp);
|
2011-01-14 23:44:33 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:00 +01:00
|
|
|
if (backing_file) {
|
|
|
|
header.features |= QED_F_BACKING_FILE;
|
|
|
|
header.backing_filename_offset = sizeof(le_header);
|
|
|
|
header.backing_filename_size = strlen(backing_file);
|
|
|
|
|
|
|
|
if (qed_fmt_is_raw(backing_fmt)) {
|
|
|
|
header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qed_header_cpu_to_le(&header, &le_header);
|
2016-05-06 18:26:27 +02:00
|
|
|
ret = blk_pwrite(blk, 0, &le_header, sizeof(le_header), 0);
|
2010-12-06 17:08:00 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
2016-03-08 15:57:05 +01:00
|
|
|
ret = blk_pwrite(blk, sizeof(le_header), backing_file,
|
2016-05-06 18:26:27 +02:00
|
|
|
header.backing_filename_size, 0);
|
2010-12-06 17:08:00 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-08-21 05:09:37 +02:00
|
|
|
l1_table = g_malloc0(l1_size);
|
2016-05-06 18:26:27 +02:00
|
|
|
ret = blk_pwrite(blk, header.l1_table_offset, l1_table, l1_size, 0);
|
2010-12-06 17:08:00 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0; /* success */
|
|
|
|
out:
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(l1_table);
|
2016-03-08 15:57:05 +01:00
|
|
|
blk_unref(blk);
|
2010-12-06 17:08:00 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-05 11:21:00 +02:00
|
|
|
static int bdrv_qed_create(const char *filename, QemuOpts *opts, Error **errp)
|
2010-12-06 17:08:00 +01:00
|
|
|
{
|
|
|
|
uint64_t image_size = 0;
|
|
|
|
uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE;
|
|
|
|
uint32_t table_size = QED_DEFAULT_TABLE_SIZE;
|
2014-06-05 11:21:00 +02:00
|
|
|
char *backing_file = NULL;
|
|
|
|
char *backing_fmt = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2014-09-10 11:05:45 +02:00
|
|
|
image_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
|
|
|
BDRV_SECTOR_SIZE);
|
2014-06-05 11:21:00 +02:00
|
|
|
backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
|
|
|
|
backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
|
|
|
|
cluster_size = qemu_opt_get_size_del(opts,
|
|
|
|
BLOCK_OPT_CLUSTER_SIZE,
|
|
|
|
QED_DEFAULT_CLUSTER_SIZE);
|
|
|
|
table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE,
|
|
|
|
QED_DEFAULT_TABLE_SIZE);
|
2010-12-06 17:08:00 +01:00
|
|
|
|
|
|
|
if (!qed_is_cluster_size_valid(cluster_size)) {
|
2014-03-15 10:35:23 +01:00
|
|
|
error_setg(errp, "QED cluster size must be within range [%u, %u] "
|
|
|
|
"and power of 2",
|
|
|
|
QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE);
|
2014-06-05 11:21:00 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto finish;
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
if (!qed_is_table_size_valid(table_size)) {
|
2014-03-15 10:35:23 +01:00
|
|
|
error_setg(errp, "QED table size must be within range [%u, %u] "
|
|
|
|
"and power of 2",
|
|
|
|
QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE);
|
2014-06-05 11:21:00 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto finish;
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) {
|
2014-03-15 10:35:23 +01:00
|
|
|
error_setg(errp, "QED image size must be a non-zero multiple of "
|
|
|
|
"cluster size and less than %" PRIu64 " bytes",
|
|
|
|
qed_max_image_size(cluster_size, table_size));
|
2014-06-05 11:21:00 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto finish;
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
|
2014-06-05 11:21:00 +02:00
|
|
|
ret = qed_create(filename, cluster_size, image_size, table_size,
|
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 08:29:58 +02:00
|
|
|
backing_file, backing_fmt, opts, errp);
|
2014-06-05 11:21:00 +02:00
|
|
|
|
|
|
|
finish:
|
|
|
|
g_free(backing_file);
|
|
|
|
g_free(backing_fmt);
|
|
|
|
return ret;
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:01 +01:00
|
|
|
typedef struct {
|
2013-09-04 19:00:30 +02:00
|
|
|
BlockDriverState *bs;
|
2011-11-14 13:44:20 +01:00
|
|
|
Coroutine *co;
|
2013-09-04 19:00:30 +02:00
|
|
|
uint64_t pos;
|
|
|
|
int64_t status;
|
2010-12-06 17:08:01 +01:00
|
|
|
int *pnum;
|
2016-01-26 04:58:54 +01:00
|
|
|
BlockDriverState **file;
|
2010-12-06 17:08:01 +01:00
|
|
|
} QEDIsAllocatedCB;
|
|
|
|
|
|
|
|
static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t len)
|
|
|
|
{
|
|
|
|
QEDIsAllocatedCB *cb = opaque;
|
2013-09-04 19:00:30 +02:00
|
|
|
BDRVQEDState *s = cb->bs->opaque;
|
2010-12-06 17:08:01 +01:00
|
|
|
*cb->pnum = len / BDRV_SECTOR_SIZE;
|
2013-09-04 19:00:30 +02:00
|
|
|
switch (ret) {
|
|
|
|
case QED_CLUSTER_FOUND:
|
|
|
|
offset |= qed_offset_into_cluster(s, cb->pos);
|
|
|
|
cb->status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
|
2016-01-26 04:58:54 +01:00
|
|
|
*cb->file = cb->bs->file->bs;
|
2013-09-04 19:00:30 +02:00
|
|
|
break;
|
|
|
|
case QED_CLUSTER_ZERO:
|
|
|
|
cb->status = BDRV_BLOCK_ZERO;
|
|
|
|
break;
|
|
|
|
case QED_CLUSTER_L2:
|
|
|
|
case QED_CLUSTER_L1:
|
|
|
|
cb->status = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(ret < 0);
|
|
|
|
cb->status = ret;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-11-14 13:44:20 +01:00
|
|
|
if (cb->co) {
|
2017-02-13 14:52:32 +01:00
|
|
|
aio_co_wake(cb->co);
|
2011-11-14 13:44:20 +01:00
|
|
|
}
|
2010-12-06 17:08:01 +01:00
|
|
|
}
|
|
|
|
|
2013-09-04 19:00:28 +02:00
|
|
|
static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs,
|
2011-11-14 13:44:20 +01:00
|
|
|
int64_t sector_num,
|
2016-01-26 04:58:48 +01:00
|
|
|
int nb_sectors, int *pnum,
|
|
|
|
BlockDriverState **file)
|
2010-12-06 17:08:00 +01:00
|
|
|
{
|
2010-12-06 17:08:01 +01:00
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
size_t len = (size_t)nb_sectors * BDRV_SECTOR_SIZE;
|
|
|
|
QEDIsAllocatedCB cb = {
|
2013-09-04 19:00:30 +02:00
|
|
|
.bs = bs,
|
|
|
|
.pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE,
|
|
|
|
.status = BDRV_BLOCK_OFFSET_MASK,
|
2010-12-06 17:08:01 +01:00
|
|
|
.pnum = pnum,
|
2016-01-26 04:58:54 +01:00
|
|
|
.file = file,
|
2010-12-06 17:08:01 +01:00
|
|
|
};
|
|
|
|
QEDRequest request = { .l2_table = NULL };
|
|
|
|
|
2013-09-04 19:00:30 +02:00
|
|
|
qed_find_cluster(s, &request, cb.pos, len, qed_is_allocated_cb, &cb);
|
2010-12-06 17:08:01 +01:00
|
|
|
|
2011-11-14 13:44:20 +01:00
|
|
|
/* Now sleep if the callback wasn't invoked immediately */
|
2013-09-04 19:00:30 +02:00
|
|
|
while (cb.status == BDRV_BLOCK_OFFSET_MASK) {
|
2011-11-14 13:44:20 +01:00
|
|
|
cb.co = qemu_coroutine_self();
|
|
|
|
qemu_coroutine_yield();
|
2010-12-06 17:08:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
qed_unref_l2_cache_entry(request.l2_table);
|
|
|
|
|
2013-09-04 19:00:30 +02:00
|
|
|
return cb.status;
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:02 +01:00
|
|
|
static BDRVQEDState *acb_to_s(QEDAIOCB *acb)
|
|
|
|
{
|
|
|
|
return acb->common.bs->opaque;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read from the backing file or zero-fill if no backing file
|
|
|
|
*
|
2014-07-04 17:11:28 +02:00
|
|
|
* @s: QED state
|
|
|
|
* @pos: Byte position in device
|
|
|
|
* @qiov: Destination I/O vector
|
|
|
|
* @backing_qiov: Possibly shortened copy of qiov, to be allocated here
|
|
|
|
* @cb: Completion function
|
|
|
|
* @opaque: User data for completion function
|
2010-12-06 17:08:02 +01:00
|
|
|
*
|
|
|
|
* This function reads qiov->size bytes starting at pos from the backing file.
|
|
|
|
* If there is no backing file then zeroes are read.
|
|
|
|
*/
|
|
|
|
static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos,
|
|
|
|
QEMUIOVector *qiov,
|
2014-07-04 17:11:28 +02:00
|
|
|
QEMUIOVector **backing_qiov,
|
2014-10-07 13:59:15 +02:00
|
|
|
BlockCompletionFunc *cb, void *opaque)
|
2010-12-06 17:08:02 +01:00
|
|
|
{
|
|
|
|
uint64_t backing_length = 0;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
/* If there is a backing file, get its length. Treat the absence of a
|
|
|
|
* backing file like a zero length backing file.
|
|
|
|
*/
|
2015-06-17 14:55:21 +02:00
|
|
|
if (s->bs->backing) {
|
|
|
|
int64_t l = bdrv_getlength(s->bs->backing->bs);
|
2010-12-06 17:08:02 +01:00
|
|
|
if (l < 0) {
|
|
|
|
cb(opaque, l);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
backing_length = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Zero all sectors if reading beyond the end of the backing file */
|
|
|
|
if (pos >= backing_length ||
|
|
|
|
pos + qiov->size > backing_length) {
|
consolidate qemu_iovec_memset{,_skip}() into single function and use existing iov_memset()
This patch combines two functions into one, and replaces
the implementation with already existing iov_memset() from
iov.c.
The new prototype of qemu_iovec_memset():
size_t qemu_iovec_memset(qiov, size_t offset, int fillc, size_t bytes)
It is different from former qemu_iovec_memset_skip(), and
I want to make other functions to be consistent with it
too: first how much to skip, second what, and 3rd how many
of it. It also returns actual number of bytes filled in,
which may be less than the requested `bytes' if qiov is
smaller than offset+bytes, in the same way iov_memset()
does.
While at it, use utility function iov_memset() from
iov.h in posix-aio-compat.c, where qiov was used.
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2012-03-10 13:54:23 +01:00
|
|
|
qemu_iovec_memset(qiov, 0, 0, qiov->size);
|
2010-12-06 17:08:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Complete now if there are no backing file sectors to read */
|
|
|
|
if (pos >= backing_length) {
|
|
|
|
cb(opaque, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the read straddles the end of the backing file, shorten it */
|
|
|
|
size = MIN((uint64_t)backing_length - pos, qiov->size);
|
|
|
|
|
2014-07-04 17:11:28 +02:00
|
|
|
assert(*backing_qiov == NULL);
|
|
|
|
*backing_qiov = g_new(QEMUIOVector, 1);
|
|
|
|
qemu_iovec_init(*backing_qiov, qiov->niov);
|
|
|
|
qemu_iovec_concat(*backing_qiov, qiov, 0, size);
|
|
|
|
|
2012-06-06 08:10:39 +02:00
|
|
|
BLKDBG_EVENT(s->bs->file, BLKDBG_READ_BACKING_AIO);
|
2016-05-30 12:06:33 +02:00
|
|
|
bdrv_aio_readv(s->bs->backing, pos / BDRV_SECTOR_SIZE,
|
2014-07-04 17:11:28 +02:00
|
|
|
*backing_qiov, size / BDRV_SECTOR_SIZE, cb, opaque);
|
2010-12-06 17:08:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GenericCB gencb;
|
|
|
|
BDRVQEDState *s;
|
|
|
|
QEMUIOVector qiov;
|
2014-07-04 17:11:28 +02:00
|
|
|
QEMUIOVector *backing_qiov;
|
2010-12-06 17:08:02 +01:00
|
|
|
struct iovec iov;
|
|
|
|
uint64_t offset;
|
|
|
|
} CopyFromBackingFileCB;
|
|
|
|
|
|
|
|
static void qed_copy_from_backing_file_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
CopyFromBackingFileCB *copy_cb = opaque;
|
|
|
|
qemu_vfree(copy_cb->iov.iov_base);
|
|
|
|
gencb_complete(©_cb->gencb, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_copy_from_backing_file_write(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
CopyFromBackingFileCB *copy_cb = opaque;
|
|
|
|
BDRVQEDState *s = copy_cb->s;
|
|
|
|
|
2014-07-04 17:11:28 +02:00
|
|
|
if (copy_cb->backing_qiov) {
|
|
|
|
qemu_iovec_destroy(copy_cb->backing_qiov);
|
|
|
|
g_free(copy_cb->backing_qiov);
|
|
|
|
copy_cb->backing_qiov = NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:02 +01:00
|
|
|
if (ret) {
|
|
|
|
qed_copy_from_backing_file_cb(copy_cb, ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BLKDBG_EVENT(s->bs->file, BLKDBG_COW_WRITE);
|
2016-05-30 12:06:33 +02:00
|
|
|
bdrv_aio_writev(s->bs->file, copy_cb->offset / BDRV_SECTOR_SIZE,
|
2011-11-30 09:12:30 +01:00
|
|
|
©_cb->qiov, copy_cb->qiov.size / BDRV_SECTOR_SIZE,
|
|
|
|
qed_copy_from_backing_file_cb, copy_cb);
|
2010-12-06 17:08:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy data from backing file into the image
|
|
|
|
*
|
|
|
|
* @s: QED state
|
|
|
|
* @pos: Byte position in device
|
|
|
|
* @len: Number of bytes
|
|
|
|
* @offset: Byte offset in image file
|
|
|
|
* @cb: Completion function
|
|
|
|
* @opaque: User data for completion function
|
|
|
|
*/
|
|
|
|
static void qed_copy_from_backing_file(BDRVQEDState *s, uint64_t pos,
|
|
|
|
uint64_t len, uint64_t offset,
|
2014-10-07 13:59:15 +02:00
|
|
|
BlockCompletionFunc *cb,
|
2010-12-06 17:08:02 +01:00
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
CopyFromBackingFileCB *copy_cb;
|
|
|
|
|
|
|
|
/* Skip copy entirely if there is no work to do */
|
|
|
|
if (len == 0) {
|
|
|
|
cb(opaque, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_cb = gencb_alloc(sizeof(*copy_cb), cb, opaque);
|
|
|
|
copy_cb->s = s;
|
|
|
|
copy_cb->offset = offset;
|
2014-07-04 17:11:28 +02:00
|
|
|
copy_cb->backing_qiov = NULL;
|
2010-12-06 17:08:02 +01:00
|
|
|
copy_cb->iov.iov_base = qemu_blockalign(s->bs, len);
|
|
|
|
copy_cb->iov.iov_len = len;
|
|
|
|
qemu_iovec_init_external(©_cb->qiov, ©_cb->iov, 1);
|
|
|
|
|
2014-07-04 17:11:28 +02:00
|
|
|
qed_read_backing_file(s, pos, ©_cb->qiov, ©_cb->backing_qiov,
|
2010-12-06 17:08:02 +01:00
|
|
|
qed_copy_from_backing_file_write, copy_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Link one or more contiguous clusters into a table
|
|
|
|
*
|
|
|
|
* @s: QED state
|
|
|
|
* @table: L2 table
|
|
|
|
* @index: First cluster index
|
|
|
|
* @n: Number of contiguous clusters
|
2010-12-17 16:58:22 +01:00
|
|
|
* @cluster: First cluster offset
|
|
|
|
*
|
|
|
|
* The cluster offset may be an allocated byte offset in the image file, the
|
|
|
|
* zero cluster marker, or the unallocated cluster marker.
|
2010-12-06 17:08:02 +01:00
|
|
|
*/
|
|
|
|
static void qed_update_l2_table(BDRVQEDState *s, QEDTable *table, int index,
|
|
|
|
unsigned int n, uint64_t cluster)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = index; i < index + n; i++) {
|
|
|
|
table->offsets[i] = cluster;
|
2010-12-17 16:58:22 +01:00
|
|
|
if (!qed_offset_is_unalloc_cluster(cluster) &&
|
|
|
|
!qed_offset_is_zero_cluster(cluster)) {
|
|
|
|
cluster += s->header.cluster_size;
|
|
|
|
}
|
2010-12-06 17:08:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_aio_complete_bh(void *opaque)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
2017-02-13 14:52:31 +01:00
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
2014-10-07 13:59:15 +02:00
|
|
|
BlockCompletionFunc *cb = acb->common.cb;
|
2010-12-06 17:08:02 +01:00
|
|
|
void *user_opaque = acb->common.opaque;
|
|
|
|
int ret = acb->bh_ret;
|
|
|
|
|
2014-09-11 07:41:28 +02:00
|
|
|
qemu_aio_unref(acb);
|
2010-12-06 17:08:02 +01:00
|
|
|
|
|
|
|
/* Invoke callback */
|
2017-02-13 14:52:31 +01:00
|
|
|
qed_acquire(s);
|
2010-12-06 17:08:02 +01:00
|
|
|
cb(user_opaque, ret);
|
2017-02-13 14:52:31 +01:00
|
|
|
qed_release(s);
|
2010-12-06 17:08:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qed_aio_complete(QEDAIOCB *acb, int ret)
|
|
|
|
{
|
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
|
|
|
|
|
|
|
trace_qed_aio_complete(s, acb, ret);
|
|
|
|
|
|
|
|
/* Free resources */
|
|
|
|
qemu_iovec_destroy(&acb->cur_qiov);
|
|
|
|
qed_unref_l2_cache_entry(acb->request.l2_table);
|
|
|
|
|
2012-02-07 14:27:28 +01:00
|
|
|
/* Free the buffer we may have allocated for zero writes */
|
|
|
|
if (acb->flags & QED_AIOCB_ZERO) {
|
|
|
|
qemu_vfree(acb->qiov->iov[0].iov_base);
|
|
|
|
acb->qiov->iov[0].iov_base = NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:02 +01:00
|
|
|
/* Arrange for a bh to invoke the completion function */
|
|
|
|
acb->bh_ret = ret;
|
2016-10-03 18:14:16 +02:00
|
|
|
aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
|
|
|
|
qed_aio_complete_bh, acb);
|
2010-12-06 17:08:02 +01:00
|
|
|
|
|
|
|
/* Start next allocating write request waiting behind this one. Note that
|
|
|
|
* requests enqueue themselves when they first hit an unallocated cluster
|
|
|
|
* but they wait until the entire request is finished before waking up the
|
|
|
|
* next request in the queue. This ensures that we don't cycle through
|
|
|
|
* requests multiple times but rather finish one at a time completely.
|
|
|
|
*/
|
|
|
|
if (acb == QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
|
|
|
|
QSIMPLEQ_REMOVE_HEAD(&s->allocating_write_reqs, next);
|
|
|
|
acb = QSIMPLEQ_FIRST(&s->allocating_write_reqs);
|
|
|
|
if (acb) {
|
2017-02-13 14:52:27 +01:00
|
|
|
qed_aio_start_io(acb);
|
2011-05-09 17:45:40 +02:00
|
|
|
} else if (s->header.features & QED_F_NEED_CHECK) {
|
|
|
|
qed_start_need_check_timer(s);
|
2010-12-06 17:08:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit the current L2 table to the cache
|
|
|
|
*/
|
|
|
|
static void qed_commit_l2_update(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
|
|
|
CachedL2Table *l2_table = acb->request.l2_table;
|
2011-09-30 12:39:11 +02:00
|
|
|
uint64_t l2_offset = l2_table->offset;
|
2010-12-06 17:08:02 +01:00
|
|
|
|
|
|
|
qed_commit_l2_cache_entry(&s->l2_cache, l2_table);
|
|
|
|
|
|
|
|
/* This is guaranteed to succeed because we just committed the entry to the
|
|
|
|
* cache.
|
|
|
|
*/
|
2011-09-30 12:39:11 +02:00
|
|
|
acb->request.l2_table = qed_find_l2_cache_entry(&s->l2_cache, l2_offset);
|
2010-12-06 17:08:02 +01:00
|
|
|
assert(acb->request.l2_table != NULL);
|
|
|
|
|
2017-02-13 14:52:27 +01:00
|
|
|
qed_aio_next_io(acb, ret);
|
2010-12-06 17:08:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update L1 table with new L2 table offset and write it out
|
|
|
|
*/
|
|
|
|
static void qed_aio_write_l1_update(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
|
|
|
int index;
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
qed_aio_complete(acb, ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = qed_l1_index(s, acb->cur_pos);
|
|
|
|
s->l1_table->offsets[index] = acb->request.l2_table->offset;
|
|
|
|
|
|
|
|
qed_write_l1_table(s, index, 1, qed_commit_l2_update, acb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update L2 table with new cluster offsets and write them out
|
|
|
|
*/
|
2012-02-07 14:27:28 +01:00
|
|
|
static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset)
|
2010-12-06 17:08:02 +01:00
|
|
|
{
|
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
|
|
|
bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_alloc) {
|
|
|
|
qed_unref_l2_cache_entry(acb->request.l2_table);
|
|
|
|
acb->request.l2_table = qed_new_l2_table(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
index = qed_l2_index(s, acb->cur_pos);
|
|
|
|
qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters,
|
2012-02-07 14:27:28 +01:00
|
|
|
offset);
|
2010-12-06 17:08:02 +01:00
|
|
|
|
|
|
|
if (need_alloc) {
|
|
|
|
/* Write out the whole new L2 table */
|
|
|
|
qed_write_l2_table(s, &acb->request, 0, s->table_nelems, true,
|
2017-02-13 14:52:27 +01:00
|
|
|
qed_aio_write_l1_update, acb);
|
2010-12-06 17:08:02 +01:00
|
|
|
} else {
|
|
|
|
/* Write out only the updated part of the L2 table */
|
|
|
|
qed_write_l2_table(s, &acb->request, index, acb->cur_nclusters, false,
|
2017-02-13 14:52:27 +01:00
|
|
|
qed_aio_next_io_cb, acb);
|
2010-12-06 17:08:02 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
qed_aio_complete(acb, ret);
|
|
|
|
}
|
|
|
|
|
2012-02-07 14:27:28 +01:00
|
|
|
static void qed_aio_write_l2_update_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
|
|
|
qed_aio_write_l2_update(acb, ret, acb->cur_cluster);
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:02 +01:00
|
|
|
/**
|
|
|
|
* Flush new data clusters before updating the L2 table
|
|
|
|
*
|
|
|
|
* This flush is necessary when a backing file is in use. A crash during an
|
|
|
|
* allocating write could result in empty clusters in the image. If the write
|
|
|
|
* only touched a subregion of the cluster, then backing image sectors have
|
|
|
|
* been lost in the untouched region. The solution is to flush after writing a
|
|
|
|
* new data cluster and before updating the L2 table.
|
|
|
|
*/
|
|
|
|
static void qed_aio_write_flush_before_l2_update(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
|
|
|
|
2015-06-16 14:19:22 +02:00
|
|
|
if (!bdrv_aio_flush(s->bs->file->bs, qed_aio_write_l2_update_cb, opaque)) {
|
2010-12-06 17:08:02 +01:00
|
|
|
qed_aio_complete(acb, -EIO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write data to the image file
|
|
|
|
*/
|
|
|
|
static void qed_aio_write_main(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
|
|
|
uint64_t offset = acb->cur_cluster +
|
|
|
|
qed_offset_into_cluster(s, acb->cur_pos);
|
2014-10-07 13:59:15 +02:00
|
|
|
BlockCompletionFunc *next_fn;
|
2010-12-06 17:08:02 +01:00
|
|
|
|
|
|
|
trace_qed_aio_write_main(s, acb, ret, offset, acb->cur_qiov.size);
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
qed_aio_complete(acb, ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acb->find_cluster_ret == QED_CLUSTER_FOUND) {
|
2017-02-13 14:52:27 +01:00
|
|
|
next_fn = qed_aio_next_io_cb;
|
2010-12-06 17:08:02 +01:00
|
|
|
} else {
|
2015-06-17 14:55:21 +02:00
|
|
|
if (s->bs->backing) {
|
2010-12-06 17:08:02 +01:00
|
|
|
next_fn = qed_aio_write_flush_before_l2_update;
|
|
|
|
} else {
|
2012-02-07 14:27:28 +01:00
|
|
|
next_fn = qed_aio_write_l2_update_cb;
|
2010-12-06 17:08:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BLKDBG_EVENT(s->bs->file, BLKDBG_WRITE_AIO);
|
2016-05-30 12:06:33 +02:00
|
|
|
bdrv_aio_writev(s->bs->file, offset / BDRV_SECTOR_SIZE,
|
2011-11-30 09:12:30 +01:00
|
|
|
&acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
|
|
|
|
next_fn, acb);
|
2010-12-06 17:08:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate back untouched region of new data cluster
|
|
|
|
*/
|
|
|
|
static void qed_aio_write_postfill(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
|
|
|
uint64_t start = acb->cur_pos + acb->cur_qiov.size;
|
|
|
|
uint64_t len =
|
|
|
|
qed_start_of_cluster(s, start + s->header.cluster_size - 1) - start;
|
|
|
|
uint64_t offset = acb->cur_cluster +
|
|
|
|
qed_offset_into_cluster(s, acb->cur_pos) +
|
|
|
|
acb->cur_qiov.size;
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
qed_aio_complete(acb, ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_qed_aio_write_postfill(s, acb, start, len, offset);
|
|
|
|
qed_copy_from_backing_file(s, start, len, offset,
|
|
|
|
qed_aio_write_main, acb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate front untouched region of new data cluster
|
|
|
|
*/
|
|
|
|
static void qed_aio_write_prefill(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
|
|
|
uint64_t start = qed_start_of_cluster(s, acb->cur_pos);
|
|
|
|
uint64_t len = qed_offset_into_cluster(s, acb->cur_pos);
|
|
|
|
|
|
|
|
trace_qed_aio_write_prefill(s, acb, start, len, acb->cur_cluster);
|
|
|
|
qed_copy_from_backing_file(s, start, len, acb->cur_cluster,
|
|
|
|
qed_aio_write_postfill, acb);
|
|
|
|
}
|
|
|
|
|
2011-01-28 18:11:59 +01:00
|
|
|
/**
|
|
|
|
* Check if the QED_F_NEED_CHECK bit should be set during allocating write
|
|
|
|
*/
|
|
|
|
static bool qed_should_set_need_check(BDRVQEDState *s)
|
|
|
|
{
|
|
|
|
/* The flush before L2 update path ensures consistency */
|
2015-06-17 14:55:21 +02:00
|
|
|
if (s->bs->backing) {
|
2011-01-28 18:11:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !(s->header.features & QED_F_NEED_CHECK);
|
|
|
|
}
|
|
|
|
|
2012-02-07 14:27:28 +01:00
|
|
|
static void qed_aio_write_zero_cluster(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
qed_aio_complete(acb, ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qed_aio_write_l2_update(acb, 0, 1);
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:02 +01:00
|
|
|
/**
|
|
|
|
* Write new data cluster
|
|
|
|
*
|
|
|
|
* @acb: Write request
|
|
|
|
* @len: Length in bytes
|
|
|
|
*
|
|
|
|
* This path is taken when writing to previously unallocated clusters.
|
|
|
|
*/
|
|
|
|
static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)
|
|
|
|
{
|
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
2014-10-07 13:59:15 +02:00
|
|
|
BlockCompletionFunc *cb;
|
2010-12-06 17:08:02 +01:00
|
|
|
|
2011-05-09 17:45:40 +02:00
|
|
|
/* Cancel timer when the first allocating request comes in */
|
|
|
|
if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) {
|
|
|
|
qed_cancel_need_check_timer(s);
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:02 +01:00
|
|
|
/* Freeze this request if another allocating write is in progress */
|
|
|
|
if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs)) {
|
|
|
|
QSIMPLEQ_INSERT_TAIL(&s->allocating_write_reqs, acb, next);
|
|
|
|
}
|
2011-05-09 17:45:40 +02:00
|
|
|
if (acb != QSIMPLEQ_FIRST(&s->allocating_write_reqs) ||
|
|
|
|
s->allocating_write_reqs_plugged) {
|
2010-12-06 17:08:02 +01:00
|
|
|
return; /* wait for existing request to finish */
|
|
|
|
}
|
|
|
|
|
|
|
|
acb->cur_nclusters = qed_bytes_to_clusters(s,
|
|
|
|
qed_offset_into_cluster(s, acb->cur_pos) + len);
|
consolidate qemu_iovec_copy() and qemu_iovec_concat() and make them consistent
qemu_iovec_concat() is currently a wrapper for
qemu_iovec_copy(), use the former (with extra
"0" arg) in a few places where it is used.
Change skip argument of qemu_iovec_copy() from
uint64_t to size_t, since size of qiov itself
is size_t, so there's no way to skip larger
sizes. Rename it to soffset, to make it clear
that the offset is applied to src.
Also change the only usage of uint64_t in
hw/9pfs/virtio-9p.c, in v9fs_init_qiov_from_pdu() -
all callers of it actually uses size_t too,
not uint64_t.
One added restriction: as for all other iovec-related
functions, soffset must point inside src.
Order of argumens is already good:
qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
int c, size_t bytes)
vs:
qemu_iovec_concat(QEMUIOVector *dst,
QEMUIOVector *src,
size_t soffset, size_t sbytes)
(note soffset is after _src_ not dst, since it applies to src;
for memset it applies to qiov).
Note that in many places where this function is used,
the previous call is qemu_iovec_reset(), which means
many callers actually want copy (replacing dst content),
not concat. So we may want to add a wrapper like
qemu_iovec_copy() with the same arguments but which
calls qemu_iovec_reset() before _concat().
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2012-03-12 18:28:06 +01:00
|
|
|
qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
|
2010-12-06 17:08:02 +01:00
|
|
|
|
2012-02-07 14:27:28 +01:00
|
|
|
if (acb->flags & QED_AIOCB_ZERO) {
|
|
|
|
/* Skip ahead if the clusters are already zero */
|
|
|
|
if (acb->find_cluster_ret == QED_CLUSTER_ZERO) {
|
2017-02-13 14:52:27 +01:00
|
|
|
qed_aio_start_io(acb);
|
2012-02-07 14:27:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb = qed_aio_write_zero_cluster;
|
|
|
|
} else {
|
|
|
|
cb = qed_aio_write_prefill;
|
|
|
|
acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters);
|
|
|
|
}
|
|
|
|
|
2011-01-28 18:11:59 +01:00
|
|
|
if (qed_should_set_need_check(s)) {
|
|
|
|
s->header.features |= QED_F_NEED_CHECK;
|
2012-02-07 14:27:28 +01:00
|
|
|
qed_write_header(s, cb, acb);
|
2011-01-28 18:11:59 +01:00
|
|
|
} else {
|
2012-02-07 14:27:28 +01:00
|
|
|
cb(acb, 0);
|
2010-12-06 17:08:03 +01:00
|
|
|
}
|
2010-12-06 17:08:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write data cluster in place
|
|
|
|
*
|
|
|
|
* @acb: Write request
|
|
|
|
* @offset: Cluster offset in bytes
|
|
|
|
* @len: Length in bytes
|
|
|
|
*
|
|
|
|
* This path is taken when writing to already allocated clusters.
|
|
|
|
*/
|
|
|
|
static void qed_aio_write_inplace(QEDAIOCB *acb, uint64_t offset, size_t len)
|
|
|
|
{
|
2012-02-07 14:27:28 +01:00
|
|
|
/* Allocate buffer for zero writes */
|
|
|
|
if (acb->flags & QED_AIOCB_ZERO) {
|
|
|
|
struct iovec *iov = acb->qiov->iov;
|
|
|
|
|
|
|
|
if (!iov->iov_base) {
|
2014-05-20 13:39:57 +02:00
|
|
|
iov->iov_base = qemu_try_blockalign(acb->common.bs, iov->iov_len);
|
|
|
|
if (iov->iov_base == NULL) {
|
|
|
|
qed_aio_complete(acb, -ENOMEM);
|
|
|
|
return;
|
|
|
|
}
|
2012-02-07 14:27:28 +01:00
|
|
|
memset(iov->iov_base, 0, iov->iov_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:02 +01:00
|
|
|
/* Calculate the I/O vector */
|
|
|
|
acb->cur_cluster = offset;
|
consolidate qemu_iovec_copy() and qemu_iovec_concat() and make them consistent
qemu_iovec_concat() is currently a wrapper for
qemu_iovec_copy(), use the former (with extra
"0" arg) in a few places where it is used.
Change skip argument of qemu_iovec_copy() from
uint64_t to size_t, since size of qiov itself
is size_t, so there's no way to skip larger
sizes. Rename it to soffset, to make it clear
that the offset is applied to src.
Also change the only usage of uint64_t in
hw/9pfs/virtio-9p.c, in v9fs_init_qiov_from_pdu() -
all callers of it actually uses size_t too,
not uint64_t.
One added restriction: as for all other iovec-related
functions, soffset must point inside src.
Order of argumens is already good:
qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
int c, size_t bytes)
vs:
qemu_iovec_concat(QEMUIOVector *dst,
QEMUIOVector *src,
size_t soffset, size_t sbytes)
(note soffset is after _src_ not dst, since it applies to src;
for memset it applies to qiov).
Note that in many places where this function is used,
the previous call is qemu_iovec_reset(), which means
many callers actually want copy (replacing dst content),
not concat. So we may want to add a wrapper like
qemu_iovec_copy() with the same arguments but which
calls qemu_iovec_reset() before _concat().
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2012-03-12 18:28:06 +01:00
|
|
|
qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
|
2010-12-06 17:08:02 +01:00
|
|
|
|
|
|
|
/* Do the actual write */
|
|
|
|
qed_aio_write_main(acb, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write data cluster
|
|
|
|
*
|
|
|
|
* @opaque: Write request
|
|
|
|
* @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
|
|
|
|
* or -errno
|
|
|
|
* @offset: Cluster offset in bytes
|
|
|
|
* @len: Length in bytes
|
|
|
|
*
|
|
|
|
* Callback from qed_find_cluster().
|
|
|
|
*/
|
|
|
|
static void qed_aio_write_data(void *opaque, int ret,
|
|
|
|
uint64_t offset, size_t len)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
|
|
|
|
|
|
|
trace_qed_aio_write_data(acb_to_s(acb), acb, ret, offset, len);
|
|
|
|
|
|
|
|
acb->find_cluster_ret = ret;
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case QED_CLUSTER_FOUND:
|
|
|
|
qed_aio_write_inplace(acb, offset, len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QED_CLUSTER_L2:
|
|
|
|
case QED_CLUSTER_L1:
|
2010-12-17 16:58:22 +01:00
|
|
|
case QED_CLUSTER_ZERO:
|
2010-12-06 17:08:02 +01:00
|
|
|
qed_aio_write_alloc(acb, len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
qed_aio_complete(acb, ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read data cluster
|
|
|
|
*
|
|
|
|
* @opaque: Read request
|
|
|
|
* @ret: QED_CLUSTER_FOUND, QED_CLUSTER_L2, QED_CLUSTER_L1,
|
|
|
|
* or -errno
|
|
|
|
* @offset: Cluster offset in bytes
|
|
|
|
* @len: Length in bytes
|
|
|
|
*
|
|
|
|
* Callback from qed_find_cluster().
|
|
|
|
*/
|
|
|
|
static void qed_aio_read_data(void *opaque, int ret,
|
|
|
|
uint64_t offset, size_t len)
|
|
|
|
{
|
|
|
|
QEDAIOCB *acb = opaque;
|
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
|
|
|
BlockDriverState *bs = acb->common.bs;
|
|
|
|
|
|
|
|
/* Adjust offset into cluster */
|
|
|
|
offset += qed_offset_into_cluster(s, acb->cur_pos);
|
|
|
|
|
|
|
|
trace_qed_aio_read_data(s, acb, ret, offset, len);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
consolidate qemu_iovec_copy() and qemu_iovec_concat() and make them consistent
qemu_iovec_concat() is currently a wrapper for
qemu_iovec_copy(), use the former (with extra
"0" arg) in a few places where it is used.
Change skip argument of qemu_iovec_copy() from
uint64_t to size_t, since size of qiov itself
is size_t, so there's no way to skip larger
sizes. Rename it to soffset, to make it clear
that the offset is applied to src.
Also change the only usage of uint64_t in
hw/9pfs/virtio-9p.c, in v9fs_init_qiov_from_pdu() -
all callers of it actually uses size_t too,
not uint64_t.
One added restriction: as for all other iovec-related
functions, soffset must point inside src.
Order of argumens is already good:
qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
int c, size_t bytes)
vs:
qemu_iovec_concat(QEMUIOVector *dst,
QEMUIOVector *src,
size_t soffset, size_t sbytes)
(note soffset is after _src_ not dst, since it applies to src;
for memset it applies to qiov).
Note that in many places where this function is used,
the previous call is qemu_iovec_reset(), which means
many callers actually want copy (replacing dst content),
not concat. So we may want to add a wrapper like
qemu_iovec_copy() with the same arguments but which
calls qemu_iovec_reset() before _concat().
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2012-03-12 18:28:06 +01:00
|
|
|
qemu_iovec_concat(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len);
|
2010-12-06 17:08:02 +01:00
|
|
|
|
2010-12-17 16:58:22 +01:00
|
|
|
/* Handle zero cluster and backing file reads */
|
|
|
|
if (ret == QED_CLUSTER_ZERO) {
|
consolidate qemu_iovec_memset{,_skip}() into single function and use existing iov_memset()
This patch combines two functions into one, and replaces
the implementation with already existing iov_memset() from
iov.c.
The new prototype of qemu_iovec_memset():
size_t qemu_iovec_memset(qiov, size_t offset, int fillc, size_t bytes)
It is different from former qemu_iovec_memset_skip(), and
I want to make other functions to be consistent with it
too: first how much to skip, second what, and 3rd how many
of it. It also returns actual number of bytes filled in,
which may be less than the requested `bytes' if qiov is
smaller than offset+bytes, in the same way iov_memset()
does.
While at it, use utility function iov_memset() from
iov.h in posix-aio-compat.c, where qiov was used.
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2012-03-10 13:54:23 +01:00
|
|
|
qemu_iovec_memset(&acb->cur_qiov, 0, 0, acb->cur_qiov.size);
|
2017-02-13 14:52:27 +01:00
|
|
|
qed_aio_start_io(acb);
|
2010-12-17 16:58:22 +01:00
|
|
|
return;
|
|
|
|
} else if (ret != QED_CLUSTER_FOUND) {
|
2010-12-06 17:08:02 +01:00
|
|
|
qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov,
|
2017-02-13 14:52:27 +01:00
|
|
|
&acb->backing_qiov, qed_aio_next_io_cb, acb);
|
2010-12-06 17:08:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
|
2016-05-30 12:06:33 +02:00
|
|
|
bdrv_aio_readv(bs->file, offset / BDRV_SECTOR_SIZE,
|
2011-11-30 09:12:30 +01:00
|
|
|
&acb->cur_qiov, acb->cur_qiov.size / BDRV_SECTOR_SIZE,
|
2017-02-13 14:52:27 +01:00
|
|
|
qed_aio_next_io_cb, acb);
|
2010-12-06 17:08:02 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
|
|
|
qed_aio_complete(acb, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begin next I/O or complete the request
|
|
|
|
*/
|
2017-02-13 14:52:27 +01:00
|
|
|
static void qed_aio_next_io(QEDAIOCB *acb, int ret)
|
2010-12-06 17:08:02 +01:00
|
|
|
{
|
|
|
|
BDRVQEDState *s = acb_to_s(acb);
|
2012-02-07 14:27:27 +01:00
|
|
|
QEDFindClusterFunc *io_fn = (acb->flags & QED_AIOCB_WRITE) ?
|
|
|
|
qed_aio_write_data : qed_aio_read_data;
|
2010-12-06 17:08:02 +01:00
|
|
|
|
|
|
|
trace_qed_aio_next_io(s, acb, ret, acb->cur_pos + acb->cur_qiov.size);
|
|
|
|
|
2014-07-04 17:11:28 +02:00
|
|
|
if (acb->backing_qiov) {
|
|
|
|
qemu_iovec_destroy(acb->backing_qiov);
|
|
|
|
g_free(acb->backing_qiov);
|
|
|
|
acb->backing_qiov = NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:08:02 +01:00
|
|
|
/* Handle I/O error */
|
|
|
|
if (ret) {
|
|
|
|
qed_aio_complete(acb, ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
acb->qiov_offset += acb->cur_qiov.size;
|
|
|
|
acb->cur_pos += acb->cur_qiov.size;
|
|
|
|
qemu_iovec_reset(&acb->cur_qiov);
|
|
|
|
|
|
|
|
/* Complete request */
|
|
|
|
if (acb->cur_pos >= acb->end_pos) {
|
|
|
|
qed_aio_complete(acb, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find next cluster and start I/O */
|
|
|
|
qed_find_cluster(s, &acb->request,
|
|
|
|
acb->cur_pos, acb->end_pos - acb->cur_pos,
|
|
|
|
io_fn, acb);
|
|
|
|
}
|
|
|
|
|
2014-10-07 13:59:14 +02:00
|
|
|
static BlockAIOCB *qed_aio_setup(BlockDriverState *bs,
|
|
|
|
int64_t sector_num,
|
|
|
|
QEMUIOVector *qiov, int nb_sectors,
|
2014-10-07 13:59:15 +02:00
|
|
|
BlockCompletionFunc *cb,
|
2014-10-07 13:59:14 +02:00
|
|
|
void *opaque, int flags)
|
2010-12-06 17:08:02 +01:00
|
|
|
{
|
2012-10-31 16:34:37 +01:00
|
|
|
QEDAIOCB *acb = qemu_aio_get(&qed_aiocb_info, bs, cb, opaque);
|
2010-12-06 17:08:02 +01:00
|
|
|
|
|
|
|
trace_qed_aio_setup(bs->opaque, acb, sector_num, nb_sectors,
|
2012-02-07 14:27:27 +01:00
|
|
|
opaque, flags);
|
2010-12-06 17:08:02 +01:00
|
|
|
|
2012-02-07 14:27:27 +01:00
|
|
|
acb->flags = flags;
|
2010-12-06 17:08:02 +01:00
|
|
|
acb->qiov = qiov;
|
|
|
|
acb->qiov_offset = 0;
|
|
|
|
acb->cur_pos = (uint64_t)sector_num * BDRV_SECTOR_SIZE;
|
|
|
|
acb->end_pos = acb->cur_pos + nb_sectors * BDRV_SECTOR_SIZE;
|
2014-07-04 17:11:28 +02:00
|
|
|
acb->backing_qiov = NULL;
|
2010-12-06 17:08:02 +01:00
|
|
|
acb->request.l2_table = NULL;
|
|
|
|
qemu_iovec_init(&acb->cur_qiov, qiov->niov);
|
|
|
|
|
|
|
|
/* Start request */
|
2017-02-13 14:52:27 +01:00
|
|
|
qed_aio_start_io(acb);
|
2010-12-06 17:08:02 +01:00
|
|
|
return &acb->common;
|
|
|
|
}
|
|
|
|
|
2014-10-07 13:59:14 +02:00
|
|
|
static BlockAIOCB *bdrv_qed_aio_readv(BlockDriverState *bs,
|
|
|
|
int64_t sector_num,
|
|
|
|
QEMUIOVector *qiov, int nb_sectors,
|
2014-10-07 13:59:15 +02:00
|
|
|
BlockCompletionFunc *cb,
|
2014-10-07 13:59:14 +02:00
|
|
|
void *opaque)
|
2010-12-06 17:08:00 +01:00
|
|
|
{
|
2012-02-07 14:27:27 +01:00
|
|
|
return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
|
2014-10-07 13:59:14 +02:00
|
|
|
static BlockAIOCB *bdrv_qed_aio_writev(BlockDriverState *bs,
|
|
|
|
int64_t sector_num,
|
|
|
|
QEMUIOVector *qiov, int nb_sectors,
|
2014-10-07 13:59:15 +02:00
|
|
|
BlockCompletionFunc *cb,
|
2014-10-07 13:59:14 +02:00
|
|
|
void *opaque)
|
2010-12-06 17:08:00 +01:00
|
|
|
{
|
2012-02-07 14:27:27 +01:00
|
|
|
return qed_aio_setup(bs, sector_num, qiov, nb_sectors, cb,
|
|
|
|
opaque, QED_AIOCB_WRITE);
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
|
2012-02-07 14:27:28 +01:00
|
|
|
typedef struct {
|
|
|
|
Coroutine *co;
|
|
|
|
int ret;
|
|
|
|
bool done;
|
|
|
|
} QEDWriteZeroesCB;
|
|
|
|
|
2016-06-01 23:10:09 +02:00
|
|
|
static void coroutine_fn qed_co_pwrite_zeroes_cb(void *opaque, int ret)
|
2012-02-07 14:27:28 +01:00
|
|
|
{
|
|
|
|
QEDWriteZeroesCB *cb = opaque;
|
|
|
|
|
|
|
|
cb->done = true;
|
|
|
|
cb->ret = ret;
|
|
|
|
if (cb->co) {
|
2017-02-13 14:52:32 +01:00
|
|
|
aio_co_wake(cb->co);
|
2012-02-07 14:27:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:10:09 +02:00
|
|
|
static int coroutine_fn bdrv_qed_co_pwrite_zeroes(BlockDriverState *bs,
|
|
|
|
int64_t offset,
|
|
|
|
int count,
|
|
|
|
BdrvRequestFlags flags)
|
2012-02-07 14:27:28 +01:00
|
|
|
{
|
2014-10-07 13:59:14 +02:00
|
|
|
BlockAIOCB *blockacb;
|
2012-08-28 15:04:27 +02:00
|
|
|
BDRVQEDState *s = bs->opaque;
|
2012-02-07 14:27:28 +01:00
|
|
|
QEDWriteZeroesCB cb = { .done = false };
|
|
|
|
QEMUIOVector qiov;
|
|
|
|
struct iovec iov;
|
|
|
|
|
2016-06-01 23:10:09 +02:00
|
|
|
/* Fall back if the request is not aligned */
|
|
|
|
if (qed_offset_into_cluster(s, offset) ||
|
|
|
|
qed_offset_into_cluster(s, count)) {
|
|
|
|
return -ENOTSUP;
|
2012-08-28 15:04:27 +02:00
|
|
|
}
|
|
|
|
|
2012-02-07 14:27:28 +01:00
|
|
|
/* Zero writes start without an I/O buffer. If a buffer becomes necessary
|
|
|
|
* then it will be allocated during request processing.
|
|
|
|
*/
|
2016-06-01 23:10:09 +02:00
|
|
|
iov.iov_base = NULL;
|
|
|
|
iov.iov_len = count;
|
2012-02-07 14:27:28 +01:00
|
|
|
|
|
|
|
qemu_iovec_init_external(&qiov, &iov, 1);
|
2016-06-01 23:10:09 +02:00
|
|
|
blockacb = qed_aio_setup(bs, offset >> BDRV_SECTOR_BITS, &qiov,
|
|
|
|
count >> BDRV_SECTOR_BITS,
|
|
|
|
qed_co_pwrite_zeroes_cb, &cb,
|
2012-02-07 14:27:28 +01:00
|
|
|
QED_AIOCB_WRITE | QED_AIOCB_ZERO);
|
|
|
|
if (!blockacb) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
if (!cb.done) {
|
|
|
|
cb.co = qemu_coroutine_self();
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
}
|
|
|
|
assert(cb.done);
|
|
|
|
return cb.ret;
|
|
|
|
}
|
|
|
|
|
2017-03-28 22:51:28 +02:00
|
|
|
static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset, Error **errp)
|
2010-12-06 17:08:00 +01:00
|
|
|
{
|
2011-05-16 14:56:53 +02:00
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
uint64_t old_image_size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!qed_is_image_size_valid(offset, s->header.cluster_size,
|
|
|
|
s->header.table_size)) {
|
2017-03-28 22:51:29 +02:00
|
|
|
error_setg(errp, "Invalid image size specified");
|
2011-05-16 14:56:53 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((uint64_t)offset < s->header.image_size) {
|
2017-03-28 22:51:29 +02:00
|
|
|
error_setg(errp, "Shrinking images is currently not supported");
|
2011-05-16 14:56:53 +02:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
old_image_size = s->header.image_size;
|
|
|
|
s->header.image_size = offset;
|
|
|
|
ret = qed_write_header_sync(s);
|
|
|
|
if (ret < 0) {
|
|
|
|
s->header.image_size = old_image_size;
|
2017-03-28 22:51:29 +02:00
|
|
|
error_setg_errno(errp, -ret, "Failed to update the image size");
|
2011-05-16 14:56:53 +02:00
|
|
|
}
|
|
|
|
return ret;
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t bdrv_qed_getlength(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
return s->header.image_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bdrv_qed_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
|
|
|
{
|
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
|
|
|
|
memset(bdi, 0, sizeof(*bdi));
|
|
|
|
bdi->cluster_size = s->header.cluster_size;
|
2012-03-15 13:13:34 +01:00
|
|
|
bdi->is_dirty = s->header.features & QED_F_NEED_CHECK;
|
2013-11-22 13:39:50 +01:00
|
|
|
bdi->unallocated_blocks_are_zero = true;
|
|
|
|
bdi->can_write_zeroes_with_unmap = true;
|
2010-12-06 17:08:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bdrv_qed_change_backing_file(BlockDriverState *bs,
|
|
|
|
const char *backing_file,
|
|
|
|
const char *backing_fmt)
|
|
|
|
{
|
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
QEDHeader new_header, le_header;
|
|
|
|
void *buffer;
|
|
|
|
size_t buffer_len, backing_file_len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Refuse to set backing filename if unknown compat feature bits are
|
|
|
|
* active. If the image uses an unknown compat feature then we may not
|
|
|
|
* know the layout of data following the header structure and cannot safely
|
|
|
|
* add a new string.
|
|
|
|
*/
|
|
|
|
if (backing_file && (s->header.compat_features &
|
|
|
|
~QED_COMPAT_FEATURE_MASK)) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&new_header, &s->header, sizeof(new_header));
|
|
|
|
|
|
|
|
new_header.features &= ~(QED_F_BACKING_FILE |
|
|
|
|
QED_F_BACKING_FORMAT_NO_PROBE);
|
|
|
|
|
|
|
|
/* Adjust feature flags */
|
|
|
|
if (backing_file) {
|
|
|
|
new_header.features |= QED_F_BACKING_FILE;
|
|
|
|
|
|
|
|
if (qed_fmt_is_raw(backing_fmt)) {
|
|
|
|
new_header.features |= QED_F_BACKING_FORMAT_NO_PROBE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate new header size */
|
|
|
|
backing_file_len = 0;
|
|
|
|
|
|
|
|
if (backing_file) {
|
|
|
|
backing_file_len = strlen(backing_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_len = sizeof(new_header);
|
|
|
|
new_header.backing_filename_offset = buffer_len;
|
|
|
|
new_header.backing_filename_size = backing_file_len;
|
|
|
|
buffer_len += backing_file_len;
|
|
|
|
|
|
|
|
/* Make sure we can rewrite header without failing */
|
|
|
|
if (buffer_len > new_header.header_size * new_header.cluster_size) {
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare new header */
|
2011-08-21 05:09:37 +02:00
|
|
|
buffer = g_malloc(buffer_len);
|
2010-12-06 17:08:00 +01:00
|
|
|
|
|
|
|
qed_header_cpu_to_le(&new_header, &le_header);
|
|
|
|
memcpy(buffer, &le_header, sizeof(le_header));
|
|
|
|
buffer_len = sizeof(le_header);
|
|
|
|
|
2011-10-18 19:17:35 +02:00
|
|
|
if (backing_file) {
|
|
|
|
memcpy(buffer + buffer_len, backing_file, backing_file_len);
|
|
|
|
buffer_len += backing_file_len;
|
|
|
|
}
|
2010-12-06 17:08:00 +01:00
|
|
|
|
|
|
|
/* Write new header */
|
2016-06-20 20:09:15 +02:00
|
|
|
ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len);
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(buffer);
|
2010-12-06 17:08:00 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
memcpy(&s->header, &new_header, sizeof(new_header));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-12 15:59:16 +01:00
|
|
|
static void bdrv_qed_invalidate_cache(BlockDriverState *bs, Error **errp)
|
2012-03-23 08:36:51 +01:00
|
|
|
{
|
|
|
|
BDRVQEDState *s = bs->opaque;
|
2014-03-12 15:59:16 +01:00
|
|
|
Error *local_err = NULL;
|
|
|
|
int ret;
|
2012-03-23 08:36:51 +01:00
|
|
|
|
|
|
|
bdrv_qed_close(bs);
|
2014-03-11 10:58:39 +01:00
|
|
|
|
2012-03-23 08:36:51 +01:00
|
|
|
memset(s, 0, sizeof(BDRVQEDState));
|
2016-12-16 18:52:37 +01:00
|
|
|
ret = bdrv_qed_do_open(bs, NULL, bs->open_flags, &local_err);
|
2014-03-12 15:59:16 +01:00
|
|
|
if (local_err) {
|
error: Use error_prepend() where it makes obvious sense
Done with this Coccinelle semantic patch
@@
expression FMT, E1, E2;
expression list ARGS;
@@
- error_setg(E1, FMT, ARGS, error_get_pretty(E2));
+ error_propagate(E1, E2);/*###*/
+ error_prepend(E1, FMT/*@@@*/, ARGS);
followed by manual cleanup, first because I can't figure out how to
make Coccinelle transform strings, and second to get rid of now
superfluous error_propagate().
We now use or propagate the original error whole instead of just its
message obtained with error_get_pretty(). This avoids suppressing its
hint (see commit 50b7b00), but I can't see how the errors touched in
this commit could come with hints. It also improves the message
printed with &error_abort when we screw up (see commit 1e9b65b).
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
2015-12-18 16:35:15 +01:00
|
|
|
error_propagate(errp, local_err);
|
|
|
|
error_prepend(errp, "Could not reopen qed layer: ");
|
2014-03-12 15:59:16 +01:00
|
|
|
return;
|
|
|
|
} else if (ret < 0) {
|
|
|
|
error_setg_errno(errp, -ret, "Could not reopen qed layer");
|
|
|
|
return;
|
|
|
|
}
|
2012-03-23 08:36:51 +01:00
|
|
|
}
|
|
|
|
|
2012-05-11 16:07:02 +02:00
|
|
|
static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result,
|
|
|
|
BdrvCheckMode fix)
|
2010-12-06 17:08:00 +01:00
|
|
|
{
|
2010-12-06 17:08:03 +01:00
|
|
|
BDRVQEDState *s = bs->opaque;
|
|
|
|
|
2012-05-11 16:07:02 +02:00
|
|
|
return qed_check(s, result, !!fix);
|
2010-12-06 17:08:00 +01:00
|
|
|
}
|
|
|
|
|
2014-06-05 11:21:00 +02:00
|
|
|
static QemuOptsList qed_create_opts = {
|
|
|
|
.name = "qed-create-opts",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_SIZE,
|
|
|
|
.type = QEMU_OPT_SIZE,
|
|
|
|
.help = "Virtual disk size"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_BACKING_FILE,
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "File name of a base image"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_BACKING_FMT,
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
.help = "Image format of the base image"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_CLUSTER_SIZE,
|
|
|
|
.type = QEMU_OPT_SIZE,
|
|
|
|
.help = "Cluster size (in bytes)",
|
|
|
|
.def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_TABLE_SIZE,
|
|
|
|
.type = QEMU_OPT_SIZE,
|
|
|
|
.help = "L1/L2 table size (in clusters)"
|
|
|
|
},
|
|
|
|
{ /* end of list */ }
|
|
|
|
}
|
2010-12-06 17:08:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static BlockDriver bdrv_qed = {
|
|
|
|
.format_name = "qed",
|
|
|
|
.instance_size = sizeof(BDRVQEDState),
|
2014-06-05 11:21:00 +02:00
|
|
|
.create_opts = &qed_create_opts,
|
2014-06-04 15:09:35 +02:00
|
|
|
.supports_backing = true,
|
2010-12-06 17:08:00 +01:00
|
|
|
|
|
|
|
.bdrv_probe = bdrv_qed_probe,
|
|
|
|
.bdrv_open = bdrv_qed_open,
|
|
|
|
.bdrv_close = bdrv_qed_close,
|
2012-09-20 21:13:27 +02:00
|
|
|
.bdrv_reopen_prepare = bdrv_qed_reopen_prepare,
|
2016-12-19 16:36:02 +01:00
|
|
|
.bdrv_child_perm = bdrv_format_default_perms,
|
2014-06-05 11:21:11 +02:00
|
|
|
.bdrv_create = bdrv_qed_create,
|
2013-06-28 12:47:42 +02:00
|
|
|
.bdrv_has_zero_init = bdrv_has_zero_init_1,
|
2013-09-04 19:00:28 +02:00
|
|
|
.bdrv_co_get_block_status = bdrv_qed_co_get_block_status,
|
2010-12-06 17:08:00 +01:00
|
|
|
.bdrv_aio_readv = bdrv_qed_aio_readv,
|
|
|
|
.bdrv_aio_writev = bdrv_qed_aio_writev,
|
2016-06-01 23:10:09 +02:00
|
|
|
.bdrv_co_pwrite_zeroes = bdrv_qed_co_pwrite_zeroes,
|
2010-12-06 17:08:00 +01:00
|
|
|
.bdrv_truncate = bdrv_qed_truncate,
|
|
|
|
.bdrv_getlength = bdrv_qed_getlength,
|
|
|
|
.bdrv_get_info = bdrv_qed_get_info,
|
2013-12-11 19:26:16 +01:00
|
|
|
.bdrv_refresh_limits = bdrv_qed_refresh_limits,
|
2010-12-06 17:08:00 +01:00
|
|
|
.bdrv_change_backing_file = bdrv_qed_change_backing_file,
|
2012-03-23 08:36:51 +01:00
|
|
|
.bdrv_invalidate_cache = bdrv_qed_invalidate_cache,
|
2010-12-06 17:08:00 +01:00
|
|
|
.bdrv_check = bdrv_qed_check,
|
2014-05-08 16:34:45 +02:00
|
|
|
.bdrv_detach_aio_context = bdrv_qed_detach_aio_context,
|
|
|
|
.bdrv_attach_aio_context = bdrv_qed_attach_aio_context,
|
2016-10-27 12:48:54 +02:00
|
|
|
.bdrv_drain = bdrv_qed_drain,
|
2010-12-06 17:08:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void bdrv_qed_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_qed);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_qed_init);
|