2018-07-03 16:48:48 +02:00
|
|
|
/*
|
|
|
|
* Write logging blk driver based on blkverify and blkdebug.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Tuomas Tynkkynen <tuomas@tuxera.com>
|
|
|
|
* Copyright (c) 2018 Aapo Vienamo <aapo@tuxera.com>
|
|
|
|
* Copyright (c) 2018 Ari Sundholm <ari@tuxera.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
|
2022-12-21 14:35:49 +01:00
|
|
|
#include "block/block-io.h"
|
2018-07-03 16:48:48 +02:00
|
|
|
#include "block/block_int.h"
|
|
|
|
#include "qapi/qmp/qdict.h"
|
|
|
|
#include "qapi/qmp/qstring.h"
|
|
|
|
#include "qemu/cutils.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2018-07-03 16:48:48 +02:00
|
|
|
#include "qemu/option.h"
|
|
|
|
|
|
|
|
/* Disk format stuff - taken from Linux drivers/md/dm-log-writes.c */
|
|
|
|
|
|
|
|
#define LOG_FLUSH_FLAG (1 << 0)
|
|
|
|
#define LOG_FUA_FLAG (1 << 1)
|
|
|
|
#define LOG_DISCARD_FLAG (1 << 2)
|
|
|
|
#define LOG_MARK_FLAG (1 << 3)
|
2018-07-04 16:59:35 +02:00
|
|
|
#define LOG_FLAG_MASK (LOG_FLUSH_FLAG \
|
|
|
|
| LOG_FUA_FLAG \
|
|
|
|
| LOG_DISCARD_FLAG \
|
|
|
|
| LOG_MARK_FLAG)
|
2018-07-03 16:48:48 +02:00
|
|
|
|
|
|
|
#define WRITE_LOG_VERSION 1ULL
|
|
|
|
#define WRITE_LOG_MAGIC 0x6a736677736872ULL
|
|
|
|
|
|
|
|
/* All fields are little-endian. */
|
|
|
|
struct log_write_super {
|
|
|
|
uint64_t magic;
|
|
|
|
uint64_t version;
|
|
|
|
uint64_t nr_entries;
|
|
|
|
uint32_t sectorsize;
|
|
|
|
} QEMU_PACKED;
|
|
|
|
|
|
|
|
struct log_write_entry {
|
|
|
|
uint64_t sector;
|
|
|
|
uint64_t nr_sectors;
|
|
|
|
uint64_t flags;
|
|
|
|
uint64_t data_len;
|
|
|
|
} QEMU_PACKED;
|
|
|
|
|
|
|
|
/* End of disk format structures. */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
BdrvChild *log_file;
|
|
|
|
uint32_t sectorsize;
|
|
|
|
uint32_t sectorbits;
|
|
|
|
uint64_t cur_log_sector;
|
|
|
|
uint64_t nr_entries;
|
2018-07-04 16:59:36 +02:00
|
|
|
uint64_t update_interval;
|
2018-07-03 16:48:48 +02:00
|
|
|
} BDRVBlkLogWritesState;
|
|
|
|
|
|
|
|
static QemuOptsList runtime_opts = {
|
|
|
|
.name = "blklogwrites",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
|
|
.desc = {
|
2018-07-04 16:59:35 +02:00
|
|
|
{
|
|
|
|
.name = "log-append",
|
|
|
|
.type = QEMU_OPT_BOOL,
|
|
|
|
.help = "Append to an existing log",
|
|
|
|
},
|
2018-07-03 16:48:48 +02:00
|
|
|
{
|
|
|
|
.name = "log-sector-size",
|
|
|
|
.type = QEMU_OPT_SIZE,
|
|
|
|
.help = "Log sector size",
|
|
|
|
},
|
2018-07-04 16:59:36 +02:00
|
|
|
{
|
|
|
|
.name = "log-super-update-interval",
|
|
|
|
.type = QEMU_OPT_NUMBER,
|
|
|
|
.help = "Log superblock update interval (# of write requests)",
|
|
|
|
},
|
2018-07-03 16:48:48 +02:00
|
|
|
{ /* end of list */ }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline uint32_t blk_log_writes_log2(uint32_t value)
|
|
|
|
{
|
|
|
|
assert(value > 0);
|
|
|
|
return 31 - clz32(value);
|
|
|
|
}
|
|
|
|
|
2018-07-04 16:59:35 +02:00
|
|
|
static inline bool blk_log_writes_sector_size_valid(uint32_t sector_size)
|
|
|
|
{
|
2018-07-06 14:00:38 +02:00
|
|
|
return is_power_of_2(sector_size) &&
|
|
|
|
sector_size >= sizeof(struct log_write_super) &&
|
|
|
|
sector_size >= sizeof(struct log_write_entry) &&
|
|
|
|
sector_size < (1ull << 24);
|
2018-07-04 16:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t blk_log_writes_find_cur_log_sector(BdrvChild *log,
|
|
|
|
uint32_t sector_size,
|
|
|
|
uint64_t nr_entries,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
uint64_t cur_sector = 1;
|
|
|
|
uint64_t cur_idx = 0;
|
|
|
|
uint32_t sector_bits = blk_log_writes_log2(sector_size);
|
|
|
|
struct log_write_entry cur_entry;
|
|
|
|
|
|
|
|
while (cur_idx < nr_entries) {
|
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 17:27:36 +02:00
|
|
|
int read_ret = bdrv_pread(log, cur_sector << sector_bits,
|
|
|
|
sizeof(cur_entry), &cur_entry, 0);
|
2018-07-04 16:59:35 +02:00
|
|
|
if (read_ret < 0) {
|
|
|
|
error_setg_errno(errp, -read_ret,
|
|
|
|
"Failed to read log entry %"PRIu64, cur_idx);
|
|
|
|
return (uint64_t)-1ull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cur_entry.flags & ~cpu_to_le64(LOG_FLAG_MASK)) {
|
|
|
|
error_setg(errp, "Invalid flags 0x%"PRIx64" in log entry %"PRIu64,
|
|
|
|
le64_to_cpu(cur_entry.flags), cur_idx);
|
|
|
|
return (uint64_t)-1ull;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Account for the sector of the entry itself */
|
|
|
|
++cur_sector;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Account for the data of the write.
|
|
|
|
* For discards, this data is not present.
|
|
|
|
*/
|
|
|
|
if (!(cur_entry.flags & cpu_to_le64(LOG_DISCARD_FLAG))) {
|
|
|
|
cur_sector += le64_to_cpu(cur_entry.nr_sectors);
|
|
|
|
}
|
|
|
|
|
|
|
|
++cur_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cur_sector;
|
|
|
|
}
|
|
|
|
|
2018-07-03 16:48:48 +02:00
|
|
|
static int blk_log_writes_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
BDRVBlkLogWritesState *s = bs->opaque;
|
|
|
|
QemuOpts *opts;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int ret;
|
2018-07-04 16:59:34 +02:00
|
|
|
uint64_t log_sector_size;
|
2018-07-04 16:59:35 +02:00
|
|
|
bool log_append;
|
2018-07-03 16:48:48 +02:00
|
|
|
|
|
|
|
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
|
2020-07-07 18:06:03 +02:00
|
|
|
if (!qemu_opts_absorb_qdict(opts, options, errp)) {
|
2018-07-03 16:48:48 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the file */
|
2022-07-26 22:11:21 +02:00
|
|
|
ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
|
|
|
|
if (ret < 0) {
|
2018-07-03 16:48:48 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the log file */
|
2020-05-13 13:05:37 +02:00
|
|
|
s->log_file = bdrv_open_child(NULL, options, "log", bs, &child_of_bds,
|
2021-02-02 13:49:45 +01:00
|
|
|
BDRV_CHILD_METADATA, false, errp);
|
|
|
|
if (!s->log_file) {
|
2018-07-03 16:48:48 +02:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-07-04 16:59:35 +02:00
|
|
|
log_append = qemu_opt_get_bool(opts, "log-append", false);
|
|
|
|
|
|
|
|
if (log_append) {
|
|
|
|
struct log_write_super log_sb = { 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
if (qemu_opt_find(opts, "log-sector-size")) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
error_setg(errp, "log-append and log-sector-size are mutually "
|
|
|
|
"exclusive");
|
|
|
|
goto fail_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read log superblock or fake one for an empty log */
|
|
|
|
if (!bdrv_getlength(s->log_file->bs)) {
|
|
|
|
log_sb.magic = cpu_to_le64(WRITE_LOG_MAGIC);
|
|
|
|
log_sb.version = cpu_to_le64(WRITE_LOG_VERSION);
|
|
|
|
log_sb.nr_entries = cpu_to_le64(0);
|
|
|
|
log_sb.sectorsize = cpu_to_le32(BDRV_SECTOR_SIZE);
|
|
|
|
} else {
|
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 17:27:36 +02:00
|
|
|
ret = bdrv_pread(s->log_file, 0, sizeof(log_sb), &log_sb, 0);
|
2018-07-04 16:59:35 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
error_setg_errno(errp, -ret, "Could not read log superblock");
|
|
|
|
goto fail_log;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (log_sb.magic != cpu_to_le64(WRITE_LOG_MAGIC)) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
error_setg(errp, "Invalid log superblock magic");
|
|
|
|
goto fail_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (log_sb.version != cpu_to_le64(WRITE_LOG_VERSION)) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
error_setg(errp, "Unsupported log version %"PRIu64,
|
|
|
|
le64_to_cpu(log_sb.version));
|
|
|
|
goto fail_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_sector_size = le32_to_cpu(log_sb.sectorsize);
|
|
|
|
s->cur_log_sector = 1;
|
|
|
|
s->nr_entries = 0;
|
|
|
|
|
|
|
|
if (blk_log_writes_sector_size_valid(log_sector_size)) {
|
|
|
|
s->cur_log_sector =
|
|
|
|
blk_log_writes_find_cur_log_sector(s->log_file, log_sector_size,
|
|
|
|
le64_to_cpu(log_sb.nr_entries), &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
goto fail_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->nr_entries = le64_to_cpu(log_sb.nr_entries);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log_sector_size = qemu_opt_get_size(opts, "log-sector-size",
|
|
|
|
BDRV_SECTOR_SIZE);
|
|
|
|
s->cur_log_sector = 1;
|
|
|
|
s->nr_entries = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!blk_log_writes_sector_size_valid(log_sector_size)) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
error_setg(errp, "Invalid log sector size %"PRIu64, log_sector_size);
|
|
|
|
goto fail_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->sectorsize = log_sector_size;
|
|
|
|
s->sectorbits = blk_log_writes_log2(log_sector_size);
|
2018-07-04 16:59:36 +02:00
|
|
|
s->update_interval = qemu_opt_get_number(opts, "log-super-update-interval",
|
|
|
|
4096);
|
|
|
|
if (!s->update_interval) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
error_setg(errp, "Invalid log superblock update interval %"PRIu64,
|
|
|
|
s->update_interval);
|
|
|
|
goto fail_log;
|
|
|
|
}
|
2018-07-04 16:59:35 +02:00
|
|
|
|
2018-07-03 16:48:48 +02:00
|
|
|
ret = 0;
|
2018-07-04 16:59:35 +02:00
|
|
|
fail_log:
|
|
|
|
if (ret < 0) {
|
|
|
|
bdrv_unref_child(bs, s->log_file);
|
|
|
|
s->log_file = NULL;
|
|
|
|
}
|
2018-07-03 16:48:48 +02:00
|
|
|
fail:
|
|
|
|
qemu_opts_del(opts);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void blk_log_writes_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVBlkLogWritesState *s = bs->opaque;
|
|
|
|
|
|
|
|
bdrv_unref_child(bs, s->log_file);
|
|
|
|
s->log_file = NULL;
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:22:02 +01:00
|
|
|
static int64_t coroutine_fn GRAPH_RDLOCK
|
|
|
|
blk_log_writes_co_getlength(BlockDriverState *bs)
|
2018-07-03 16:48:48 +02:00
|
|
|
{
|
2023-01-13 21:42:04 +01:00
|
|
|
return bdrv_co_getlength(bs->file->bs);
|
2018-07-03 16:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void blk_log_writes_child_perm(BlockDriverState *bs, BdrvChild *c,
|
2020-05-13 13:05:16 +02:00
|
|
|
BdrvChildRole role,
|
2018-07-03 16:48:48 +02:00
|
|
|
BlockReopenQueue *ro_q,
|
|
|
|
uint64_t perm, uint64_t shrd,
|
|
|
|
uint64_t *nperm, uint64_t *nshrd)
|
|
|
|
{
|
|
|
|
if (!c) {
|
|
|
|
*nperm = perm & DEFAULT_PERM_PASSTHROUGH;
|
|
|
|
*nshrd = (shrd & DEFAULT_PERM_PASSTHROUGH) | DEFAULT_PERM_UNCHANGED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-13 13:05:44 +02:00
|
|
|
bdrv_default_perms(bs, c, role, ro_q, perm, shrd,
|
2020-05-13 13:05:39 +02:00
|
|
|
nperm, nshrd);
|
2018-07-03 16:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void blk_log_writes_refresh_limits(BlockDriverState *bs, Error **errp)
|
|
|
|
{
|
|
|
|
BDRVBlkLogWritesState *s = bs->opaque;
|
|
|
|
bs->bl.request_alignment = s->sectorsize;
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:50 +01: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 12:27:59 +02:00
|
|
|
blk_log_writes_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
|
|
|
|
QEMUIOVector *qiov, BdrvRequestFlags flags)
|
2018-07-03 16:48:48 +02:00
|
|
|
{
|
|
|
|
return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct BlkLogWritesFileReq {
|
|
|
|
BlockDriverState *bs;
|
|
|
|
uint64_t offset;
|
|
|
|
uint64_t bytes;
|
|
|
|
int file_flags;
|
|
|
|
QEMUIOVector *qiov;
|
2023-02-03 16:21:46 +01:00
|
|
|
int GRAPH_RDLOCK_PTR (*func)(struct BlkLogWritesFileReq *r);
|
2018-07-03 16:48:48 +02:00
|
|
|
int file_ret;
|
|
|
|
} BlkLogWritesFileReq;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
BlockDriverState *bs;
|
|
|
|
QEMUIOVector *qiov;
|
|
|
|
struct log_write_entry entry;
|
|
|
|
uint64_t zero_size;
|
|
|
|
int log_ret;
|
|
|
|
} BlkLogWritesLogReq;
|
|
|
|
|
2023-02-03 16:21:46 +01:00
|
|
|
static void coroutine_fn GRAPH_RDLOCK
|
|
|
|
blk_log_writes_co_do_log(BlkLogWritesLogReq *lr)
|
2018-07-03 16:48:48 +02:00
|
|
|
{
|
|
|
|
BDRVBlkLogWritesState *s = lr->bs->opaque;
|
|
|
|
uint64_t cur_log_offset = s->cur_log_sector << s->sectorbits;
|
|
|
|
|
|
|
|
s->nr_entries++;
|
|
|
|
s->cur_log_sector +=
|
|
|
|
ROUND_UP(lr->qiov->size, s->sectorsize) >> s->sectorbits;
|
|
|
|
|
|
|
|
lr->log_ret = bdrv_co_pwritev(s->log_file, cur_log_offset, lr->qiov->size,
|
|
|
|
lr->qiov, 0);
|
|
|
|
|
|
|
|
/* Logging for the "write zeroes" operation */
|
|
|
|
if (lr->log_ret == 0 && lr->zero_size) {
|
|
|
|
cur_log_offset = s->cur_log_sector << s->sectorbits;
|
|
|
|
s->cur_log_sector +=
|
|
|
|
ROUND_UP(lr->zero_size, s->sectorsize) >> s->sectorbits;
|
|
|
|
|
|
|
|
lr->log_ret = bdrv_co_pwrite_zeroes(s->log_file, cur_log_offset,
|
|
|
|
lr->zero_size, 0);
|
|
|
|
}
|
|
|
|
|
2018-07-04 16:59:36 +02:00
|
|
|
/* Update super block on flush or every update interval */
|
|
|
|
if (lr->log_ret == 0 && ((lr->entry.flags & LOG_FLUSH_FLAG)
|
|
|
|
|| (s->nr_entries % s->update_interval == 0)))
|
|
|
|
{
|
2018-07-03 16:48:48 +02:00
|
|
|
struct log_write_super super = {
|
|
|
|
.magic = cpu_to_le64(WRITE_LOG_MAGIC),
|
|
|
|
.version = cpu_to_le64(WRITE_LOG_VERSION),
|
|
|
|
.nr_entries = cpu_to_le64(s->nr_entries),
|
|
|
|
.sectorsize = cpu_to_le32(s->sectorsize),
|
|
|
|
};
|
|
|
|
void *zeroes = g_malloc0(s->sectorsize - sizeof(super));
|
|
|
|
QEMUIOVector qiov;
|
|
|
|
|
|
|
|
qemu_iovec_init(&qiov, 2);
|
|
|
|
qemu_iovec_add(&qiov, &super, sizeof(super));
|
|
|
|
qemu_iovec_add(&qiov, zeroes, s->sectorsize - sizeof(super));
|
|
|
|
|
|
|
|
lr->log_ret =
|
|
|
|
bdrv_co_pwritev(s->log_file, 0, s->sectorsize, &qiov, 0);
|
|
|
|
if (lr->log_ret == 0) {
|
|
|
|
lr->log_ret = bdrv_co_flush(s->log_file->bs);
|
|
|
|
}
|
|
|
|
qemu_iovec_destroy(&qiov);
|
|
|
|
g_free(zeroes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:46 +01:00
|
|
|
static void coroutine_fn GRAPH_RDLOCK
|
|
|
|
blk_log_writes_co_do_file(BlkLogWritesFileReq *fr)
|
2018-07-03 16:48:48 +02:00
|
|
|
{
|
|
|
|
fr->file_ret = fr->func(fr);
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:46 +01:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
2018-07-03 16:48:48 +02:00
|
|
|
blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
|
|
|
QEMUIOVector *qiov, int flags,
|
2023-02-03 16:21:46 +01:00
|
|
|
int /*GRAPH_RDLOCK*/ (*file_func)(BlkLogWritesFileReq *r),
|
2018-07-03 16:48:48 +02:00
|
|
|
uint64_t entry_flags, bool is_zero_write)
|
|
|
|
{
|
|
|
|
QEMUIOVector log_qiov;
|
|
|
|
size_t niov = qiov ? qiov->niov : 0;
|
|
|
|
BDRVBlkLogWritesState *s = bs->opaque;
|
|
|
|
BlkLogWritesFileReq fr = {
|
|
|
|
.bs = bs,
|
|
|
|
.offset = offset,
|
|
|
|
.bytes = bytes,
|
|
|
|
.file_flags = flags,
|
|
|
|
.qiov = qiov,
|
|
|
|
.func = file_func,
|
|
|
|
};
|
|
|
|
BlkLogWritesLogReq lr = {
|
|
|
|
.bs = bs,
|
|
|
|
.qiov = &log_qiov,
|
|
|
|
.entry = {
|
|
|
|
.sector = cpu_to_le64(offset >> s->sectorbits),
|
|
|
|
.nr_sectors = cpu_to_le64(bytes >> s->sectorbits),
|
|
|
|
.flags = cpu_to_le64(entry_flags),
|
|
|
|
.data_len = 0,
|
|
|
|
},
|
|
|
|
.zero_size = is_zero_write ? bytes : 0,
|
|
|
|
};
|
|
|
|
void *zeroes = g_malloc0(s->sectorsize - sizeof(lr.entry));
|
|
|
|
|
|
|
|
assert((1 << s->sectorbits) == s->sectorsize);
|
|
|
|
assert(bs->bl.request_alignment == s->sectorsize);
|
|
|
|
assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
|
|
|
|
assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
|
|
|
|
|
|
|
|
qemu_iovec_init(&log_qiov, niov + 2);
|
|
|
|
qemu_iovec_add(&log_qiov, &lr.entry, sizeof(lr.entry));
|
|
|
|
qemu_iovec_add(&log_qiov, zeroes, s->sectorsize - sizeof(lr.entry));
|
|
|
|
if (qiov) {
|
|
|
|
qemu_iovec_concat(&log_qiov, qiov, 0, qiov->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
blk_log_writes_co_do_file(&fr);
|
|
|
|
blk_log_writes_co_do_log(&lr);
|
|
|
|
|
|
|
|
qemu_iovec_destroy(&log_qiov);
|
|
|
|
g_free(zeroes);
|
|
|
|
|
|
|
|
if (lr.log_ret < 0) {
|
|
|
|
return lr.log_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fr.file_ret;
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:50 +01:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
2018-07-03 16:48:48 +02:00
|
|
|
blk_log_writes_co_do_file_pwritev(BlkLogWritesFileReq *fr)
|
|
|
|
{
|
|
|
|
return bdrv_co_pwritev(fr->bs->file, fr->offset, fr->bytes,
|
|
|
|
fr->qiov, fr->file_flags);
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:48 +01:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
2018-07-03 16:48:48 +02:00
|
|
|
blk_log_writes_co_do_file_pwrite_zeroes(BlkLogWritesFileReq *fr)
|
|
|
|
{
|
|
|
|
return bdrv_co_pwrite_zeroes(fr->bs->file, fr->offset, fr->bytes,
|
|
|
|
fr->file_flags);
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:46 +01:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
|
|
|
blk_log_writes_co_do_file_flush(BlkLogWritesFileReq *fr)
|
2018-07-03 16:48:48 +02:00
|
|
|
{
|
|
|
|
return bdrv_co_flush(fr->bs->file->bs);
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:47 +01:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
2018-07-03 16:48:48 +02:00
|
|
|
blk_log_writes_co_do_file_pdiscard(BlkLogWritesFileReq *fr)
|
|
|
|
{
|
2018-07-10 08:31:17 +02:00
|
|
|
return bdrv_co_pdiscard(fr->bs->file, fr->offset, fr->bytes);
|
2018-07-03 16:48:48 +02:00
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:50 +01: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 12:28:00 +02:00
|
|
|
blk_log_writes_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
|
|
|
|
QEMUIOVector *qiov, BdrvRequestFlags flags)
|
2018-07-03 16:48:48 +02:00
|
|
|
{
|
|
|
|
return blk_log_writes_co_log(bs, offset, bytes, qiov, flags,
|
|
|
|
blk_log_writes_co_do_file_pwritev, 0, false);
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:48 +01:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
block: use int64_t instead of int in driver write_zeroes 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_zeroes handlers bytes parameter to int64_t.
The only caller of all updated function is bdrv_co_do_pwrite_zeroes().
bdrv_co_do_pwrite_zeroes() itself is of course OK with widening of
callee parameter type. Also, bdrv_co_do_pwrite_zeroes()'s
max_write_zeroes is limited to INT_MAX. So, updated functions all are
safe, they will not get "bytes" larger than before.
Still, let's look through all updated functions, and add assertions to
the ones which are actually unprepared to values larger than INT_MAX.
For these drivers also set explicit max_pwrite_zeroes limit.
Let's go:
blkdebug: calculations can't overflow, thanks to
bdrv_check_qiov_request() in generic layer. rule_check() and
bdrv_co_pwrite_zeroes() both have 64bit argument.
blklogwrites: pass to blk_log_writes_co_log() with 64bit argument.
blkreplay, copy-on-read, filter-compress: pass to
bdrv_co_pwrite_zeroes() which is OK
copy-before-write: Calls cbw_do_copy_before_write() and
bdrv_co_pwrite_zeroes, both have 64bit argument.
file-posix: both handler calls raw_do_pwrite_zeroes, which is updated.
In raw_do_pwrite_zeroes() calculations are OK due to
bdrv_check_qiov_request(), bytes go to RawPosixAIOData::aio_nbytes
which is uint64_t.
Check also where that uint64_t gets handed:
handle_aiocb_write_zeroes_block() passes a uint64_t[2] to
ioctl(BLKZEROOUT), handle_aiocb_write_zeroes() calls do_fallocate()
which takes off_t (and we compile to always have 64-bit off_t), as
does handle_aiocb_write_zeroes_unmap. All look safe.
gluster: bytes go to GlusterAIOCB::size which is int64_t and to
glfs_zerofill_async works with off_t.
iscsi: Aha, here we deal with iscsi_writesame16_task() that has
uint32_t num_blocks argument and iscsi_writesame16_task() has
uint16_t argument. Make comments, add assertions and clarify
max_pwrite_zeroes calculation.
iscsi_allocmap_() functions already has int64_t argument
is_byte_request_lun_aligned is simple to update, do it.
mirror_top: pass to bdrv_mirror_top_do_write which has uint64_t
argument
nbd: Aha, here we have protocol limitation, and NBDRequest::len is
uint32_t. max_pwrite_zeroes is cleanly set to 32bit value, so we are
OK for now.
nvme: Again, protocol limitation. And no inherent limit for
write-zeroes at all. But from code that calculates cdw12 it's obvious
that we do have limit and alignment. Let's clarify it. Also,
obviously the code is not prepared to handle bytes=0. Let's handle
this case too.
trace events already 64bit
preallocate: pass to handle_write() and bdrv_co_pwrite_zeroes(), both
64bit.
rbd: pass to qemu_rbd_start_co() which is 64bit.
qcow2: offset + bytes and alignment still works good (thanks to
bdrv_check_qiov_request()), so tail calculation is OK
qcow2_subcluster_zeroize() has 64bit argument, should be OK
trace events updated
qed: qed_co_request wants int nb_sectors. Also in code we have size_t
used for request length which may be 32bit. So, let's just keep
INT_MAX as a limit (aligning it down to pwrite_zeroes_alignment) and
don't care.
raw-format: Is OK. raw_adjust_offset and bdrv_co_pwrite_zeroes are both
64bit.
throttle: Both throttle_group_co_io_limits_intercept() and
bdrv_co_pwrite_zeroes() are 64bit.
vmdk: pass to vmdk_pwritev which is 64bit
quorum: pass to quorum_co_pwritev() which is 64bit
Hooray!
At this point all block drivers are prepared to support 64bit
write-zero requests, or have explicitly set max_pwrite_zeroes.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20210903102807.27127-8-vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
[eblake: use <= rather than < in assertions relying on max_pwrite_zeroes]
Signed-off-by: Eric Blake <eblake@redhat.com>
2021-09-03 12:28:03 +02:00
|
|
|
blk_log_writes_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
|
|
|
|
int64_t bytes, BdrvRequestFlags flags)
|
2018-07-03 16:48:48 +02:00
|
|
|
{
|
|
|
|
return blk_log_writes_co_log(bs, offset, bytes, NULL, flags,
|
|
|
|
blk_log_writes_co_do_file_pwrite_zeroes, 0,
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:46 +01:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
|
|
|
blk_log_writes_co_flush_to_disk(BlockDriverState *bs)
|
2018-07-03 16:48:48 +02:00
|
|
|
{
|
|
|
|
return blk_log_writes_co_log(bs, 0, 0, NULL, 0,
|
|
|
|
blk_log_writes_co_do_file_flush,
|
|
|
|
LOG_FLUSH_FLAG, false);
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:47 +01:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
block: use int64_t instead of int in driver discard 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 discard handlers bytes parameter to int64_t.
The only caller of all updated function is bdrv_co_pdiscard in
block/io.c. It is already prepared to work with 64bit requests, but
pass at most max(bs->bl.max_pdiscard, INT_MAX) to the driver.
Let's look at all updated functions:
blkdebug: all calculations are still OK, thanks to
bdrv_check_qiov_request().
both rule_check and bdrv_co_pdiscard are 64bit
blklogwrites: pass to blk_loc_writes_co_log which is 64bit
blkreplay, copy-on-read, filter-compress: pass to bdrv_co_pdiscard, OK
copy-before-write: pass to bdrv_co_pdiscard which is 64bit and to
cbw_do_copy_before_write which is 64bit
file-posix: one handler calls raw_account_discard() is 64bit and both
handlers calls raw_do_pdiscard(). Update raw_do_pdiscard, which pass
to RawPosixAIOData::aio_nbytes, which is 64bit (and calls
raw_account_discard())
gluster: somehow, third argument of glfs_discard_async is size_t.
Let's set max_pdiscard accordingly.
iscsi: iscsi_allocmap_set_invalid is 64bit,
!is_byte_request_lun_aligned is 64bit.
list.num is uint32_t. Let's clarify max_pdiscard and
pdiscard_alignment.
mirror_top: pass to bdrv_mirror_top_do_write() which is
64bit
nbd: protocol limitation. max_pdiscard is alredy set strict enough,
keep it as is for now.
nvme: buf.nlb is uint32_t and we do shift. So, add corresponding limits
to nvme_refresh_limits().
preallocate: pass to bdrv_co_pdiscard() which is 64bit.
rbd: pass to qemu_rbd_start_co() which is 64bit.
qcow2: calculations are still OK, thanks to bdrv_check_qiov_request(),
qcow2_cluster_discard() is 64bit.
raw-format: raw_adjust_offset() is 64bit, bdrv_co_pdiscard too.
throttle: pass to bdrv_co_pdiscard() which is 64bit and to
throttle_group_co_io_limits_intercept() which is 64bit as well.
test-block-iothread: bytes argument is unused
Great! Now all drivers are prepared to handle 64bit discard requests,
or else have explicit max_pdiscard limits.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20210903102807.27127-11-vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
2021-09-03 12:28:06 +02:00
|
|
|
blk_log_writes_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
|
2018-07-03 16:48:48 +02:00
|
|
|
{
|
block: use int64_t instead of int in driver discard 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 discard handlers bytes parameter to int64_t.
The only caller of all updated function is bdrv_co_pdiscard in
block/io.c. It is already prepared to work with 64bit requests, but
pass at most max(bs->bl.max_pdiscard, INT_MAX) to the driver.
Let's look at all updated functions:
blkdebug: all calculations are still OK, thanks to
bdrv_check_qiov_request().
both rule_check and bdrv_co_pdiscard are 64bit
blklogwrites: pass to blk_loc_writes_co_log which is 64bit
blkreplay, copy-on-read, filter-compress: pass to bdrv_co_pdiscard, OK
copy-before-write: pass to bdrv_co_pdiscard which is 64bit and to
cbw_do_copy_before_write which is 64bit
file-posix: one handler calls raw_account_discard() is 64bit and both
handlers calls raw_do_pdiscard(). Update raw_do_pdiscard, which pass
to RawPosixAIOData::aio_nbytes, which is 64bit (and calls
raw_account_discard())
gluster: somehow, third argument of glfs_discard_async is size_t.
Let's set max_pdiscard accordingly.
iscsi: iscsi_allocmap_set_invalid is 64bit,
!is_byte_request_lun_aligned is 64bit.
list.num is uint32_t. Let's clarify max_pdiscard and
pdiscard_alignment.
mirror_top: pass to bdrv_mirror_top_do_write() which is
64bit
nbd: protocol limitation. max_pdiscard is alredy set strict enough,
keep it as is for now.
nvme: buf.nlb is uint32_t and we do shift. So, add corresponding limits
to nvme_refresh_limits().
preallocate: pass to bdrv_co_pdiscard() which is 64bit.
rbd: pass to qemu_rbd_start_co() which is 64bit.
qcow2: calculations are still OK, thanks to bdrv_check_qiov_request(),
qcow2_cluster_discard() is 64bit.
raw-format: raw_adjust_offset() is 64bit, bdrv_co_pdiscard too.
throttle: pass to bdrv_co_pdiscard() which is 64bit and to
throttle_group_co_io_limits_intercept() which is 64bit as well.
test-block-iothread: bytes argument is unused
Great! Now all drivers are prepared to handle 64bit discard requests,
or else have explicit max_pdiscard limits.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20210903102807.27127-11-vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
2021-09-03 12:28:06 +02:00
|
|
|
return blk_log_writes_co_log(bs, offset, bytes, NULL, 0,
|
2018-07-03 16:48:48 +02:00
|
|
|
blk_log_writes_co_do_file_pdiscard,
|
|
|
|
LOG_DISCARD_FLAG, false);
|
|
|
|
}
|
|
|
|
|
2019-02-01 20:29:25 +01:00
|
|
|
static const char *const blk_log_writes_strong_runtime_opts[] = {
|
|
|
|
"log-append",
|
|
|
|
"log-sector-size",
|
|
|
|
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-07-03 16:48:48 +02:00
|
|
|
static BlockDriver bdrv_blk_log_writes = {
|
|
|
|
.format_name = "blklogwrites",
|
|
|
|
.instance_size = sizeof(BDRVBlkLogWritesState),
|
|
|
|
|
|
|
|
.bdrv_open = blk_log_writes_open,
|
|
|
|
.bdrv_close = blk_log_writes_close,
|
2023-01-13 21:42:04 +01:00
|
|
|
.bdrv_co_getlength = blk_log_writes_co_getlength,
|
2018-07-03 16:48:48 +02:00
|
|
|
.bdrv_child_perm = blk_log_writes_child_perm,
|
|
|
|
.bdrv_refresh_limits = blk_log_writes_refresh_limits,
|
|
|
|
|
|
|
|
.bdrv_co_preadv = blk_log_writes_co_preadv,
|
|
|
|
.bdrv_co_pwritev = blk_log_writes_co_pwritev,
|
|
|
|
.bdrv_co_pwrite_zeroes = blk_log_writes_co_pwrite_zeroes,
|
|
|
|
.bdrv_co_flush_to_disk = blk_log_writes_co_flush_to_disk,
|
|
|
|
.bdrv_co_pdiscard = blk_log_writes_co_pdiscard,
|
|
|
|
|
|
|
|
.is_filter = true,
|
2019-02-01 20:29:25 +01:00
|
|
|
.strong_runtime_opts = blk_log_writes_strong_runtime_opts,
|
2018-07-03 16:48:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void bdrv_blk_log_writes_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_blk_log_writes);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_blk_log_writes_init);
|