2016-03-08 12:44:55 +08:00
|
|
|
/*
|
|
|
|
* Block Dirty Bitmap
|
|
|
|
*
|
2017-09-25 22:55:11 +08:00
|
|
|
* Copyright (c) 2016-2017 Red Hat. Inc
|
2016-03-08 12:44:55 +08:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#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 16:01:28 +08:00
|
|
|
#include "qapi/error.h"
|
2016-03-08 12:44:55 +08:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "trace.h"
|
|
|
|
#include "block/block_int.h"
|
|
|
|
#include "block/blockjob.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A BdrvDirtyBitmap can be in three possible states:
|
|
|
|
* (1) successor is NULL and disabled is false: full r/w mode
|
|
|
|
* (2) successor is NULL and disabled is true: read only mode ("disabled")
|
|
|
|
* (3) successor is set: frozen mode.
|
|
|
|
* A frozen bitmap cannot be renamed, deleted, anonymized, cleared, set,
|
|
|
|
* or enabled. A frozen bitmap can only abdicate() or reclaim().
|
|
|
|
*/
|
|
|
|
struct BdrvDirtyBitmap {
|
2017-06-05 20:39:05 +08:00
|
|
|
QemuMutex *mutex;
|
2017-09-25 22:55:26 +08:00
|
|
|
HBitmap *bitmap; /* Dirty bitmap implementation */
|
2016-10-14 05:58:24 +08:00
|
|
|
HBitmap *meta; /* Meta dirty bitmap */
|
2018-03-14 03:34:00 +08:00
|
|
|
bool qmp_locked; /* Bitmap is locked, it can't be modified
|
|
|
|
through QMP */
|
2016-03-08 12:44:55 +08:00
|
|
|
BdrvDirtyBitmap *successor; /* Anonymous child; implies frozen status */
|
|
|
|
char *name; /* Optional non-empty unique ID */
|
2017-09-25 22:55:13 +08:00
|
|
|
int64_t size; /* Size of the bitmap, in bytes */
|
2017-06-28 20:05:09 +08:00
|
|
|
bool disabled; /* Bitmap is disabled. It ignores all writes to
|
|
|
|
the device */
|
2016-10-14 05:58:21 +08:00
|
|
|
int active_iterators; /* How many iterators are active */
|
2017-06-28 20:05:10 +08:00
|
|
|
bool readonly; /* Bitmap is read-only. This field also
|
|
|
|
prevents the respective image from being
|
|
|
|
modified (i.e. blocks writes and discards).
|
|
|
|
Such operations must fail and both the image
|
|
|
|
and this bitmap must remain unchanged while
|
|
|
|
this flag is set. */
|
2017-06-28 20:05:17 +08:00
|
|
|
bool persistent; /* bitmap must be saved to owner disk image */
|
2016-03-08 12:44:55 +08:00
|
|
|
QLIST_ENTRY(BdrvDirtyBitmap) list;
|
|
|
|
};
|
|
|
|
|
2016-10-14 05:58:21 +08:00
|
|
|
struct BdrvDirtyBitmapIter {
|
|
|
|
HBitmapIter hbi;
|
|
|
|
BdrvDirtyBitmap *bitmap;
|
|
|
|
};
|
|
|
|
|
2017-06-05 20:39:03 +08:00
|
|
|
static inline void bdrv_dirty_bitmaps_lock(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
qemu_mutex_lock(&bs->dirty_bitmap_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bdrv_dirty_bitmaps_unlock(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
qemu_mutex_unlock(&bs->dirty_bitmap_mutex);
|
|
|
|
}
|
|
|
|
|
2017-06-05 20:39:05 +08:00
|
|
|
void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
qemu_mutex_lock(bitmap->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bdrv_dirty_bitmap_unlock(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
qemu_mutex_unlock(bitmap->mutex);
|
|
|
|
}
|
|
|
|
|
2017-06-05 20:39:03 +08:00
|
|
|
/* Called with BQL or dirty_bitmap lock taken. */
|
2016-03-08 12:44:55 +08:00
|
|
|
BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name)
|
|
|
|
{
|
|
|
|
BdrvDirtyBitmap *bm;
|
|
|
|
|
|
|
|
assert(name);
|
|
|
|
QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
|
|
|
|
if (bm->name && !strcmp(name, bm->name)) {
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-05 20:39:03 +08:00
|
|
|
/* Called with BQL taken. */
|
2016-03-08 12:44:55 +08:00
|
|
|
BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs,
|
|
|
|
uint32_t granularity,
|
|
|
|
const char *name,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
int64_t bitmap_size;
|
|
|
|
BdrvDirtyBitmap *bitmap;
|
|
|
|
|
2017-09-25 22:55:13 +08:00
|
|
|
assert(is_power_of_2(granularity) && granularity >= BDRV_SECTOR_SIZE);
|
2016-03-08 12:44:55 +08:00
|
|
|
|
|
|
|
if (name && bdrv_find_dirty_bitmap(bs, name)) {
|
|
|
|
error_setg(errp, "Bitmap already exists: %s", name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-25 22:55:13 +08:00
|
|
|
bitmap_size = bdrv_getlength(bs);
|
2016-03-08 12:44:55 +08:00
|
|
|
if (bitmap_size < 0) {
|
|
|
|
error_setg_errno(errp, -bitmap_size, "could not get length of device");
|
|
|
|
errno = -bitmap_size;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bitmap = g_new0(BdrvDirtyBitmap, 1);
|
2017-06-05 20:39:05 +08:00
|
|
|
bitmap->mutex = &bs->dirty_bitmap_mutex;
|
2017-09-25 22:55:26 +08:00
|
|
|
bitmap->bitmap = hbitmap_alloc(bitmap_size, ctz32(granularity));
|
2016-03-08 12:44:55 +08:00
|
|
|
bitmap->size = bitmap_size;
|
|
|
|
bitmap->name = g_strdup(name);
|
|
|
|
bitmap->disabled = false;
|
2017-06-05 20:39:03 +08:00
|
|
|
bdrv_dirty_bitmaps_lock(bs);
|
2016-03-08 12:44:55 +08:00
|
|
|
QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list);
|
2017-06-05 20:39:03 +08:00
|
|
|
bdrv_dirty_bitmaps_unlock(bs);
|
2016-03-08 12:44:55 +08:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2016-10-14 05:58:24 +08:00
|
|
|
/* bdrv_create_meta_dirty_bitmap
|
|
|
|
*
|
|
|
|
* Create a meta dirty bitmap that tracks the changes of bits in @bitmap. I.e.
|
|
|
|
* when a dirty status bit in @bitmap is changed (either from reset to set or
|
|
|
|
* the other way around), its respective meta dirty bitmap bit will be marked
|
|
|
|
* dirty as well.
|
|
|
|
*
|
|
|
|
* @bitmap: the block dirty bitmap for which to create a meta dirty bitmap.
|
|
|
|
* @chunk_size: how many bytes of bitmap data does each bit in the meta bitmap
|
|
|
|
* track.
|
|
|
|
*/
|
|
|
|
void bdrv_create_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap,
|
|
|
|
int chunk_size)
|
|
|
|
{
|
|
|
|
assert(!bitmap->meta);
|
2017-06-05 20:39:05 +08:00
|
|
|
qemu_mutex_lock(bitmap->mutex);
|
2016-10-14 05:58:24 +08:00
|
|
|
bitmap->meta = hbitmap_create_meta(bitmap->bitmap,
|
|
|
|
chunk_size * BITS_PER_BYTE);
|
2017-06-05 20:39:05 +08:00
|
|
|
qemu_mutex_unlock(bitmap->mutex);
|
2016-10-14 05:58:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bdrv_release_meta_dirty_bitmap(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
assert(bitmap->meta);
|
2017-06-05 20:39:05 +08:00
|
|
|
qemu_mutex_lock(bitmap->mutex);
|
2016-10-14 05:58:24 +08:00
|
|
|
hbitmap_free_meta(bitmap->bitmap);
|
|
|
|
bitmap->meta = NULL;
|
2017-06-05 20:39:05 +08:00
|
|
|
qemu_mutex_unlock(bitmap->mutex);
|
2016-10-14 05:58:24 +08:00
|
|
|
}
|
|
|
|
|
2016-10-14 05:58:25 +08:00
|
|
|
int64_t bdrv_dirty_bitmap_size(const BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
2017-09-25 22:55:13 +08:00
|
|
|
return bitmap->size;
|
2016-10-14 05:58:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *bdrv_dirty_bitmap_name(const BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
return bitmap->name;
|
|
|
|
}
|
|
|
|
|
2017-06-05 20:39:03 +08:00
|
|
|
/* Called with BQL taken. */
|
2016-03-08 12:44:55 +08:00
|
|
|
bool bdrv_dirty_bitmap_frozen(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
return bitmap->successor;
|
|
|
|
}
|
|
|
|
|
2018-03-14 03:34:00 +08:00
|
|
|
void bdrv_dirty_bitmap_set_qmp_locked(BdrvDirtyBitmap *bitmap, bool qmp_locked)
|
|
|
|
{
|
|
|
|
qemu_mutex_lock(bitmap->mutex);
|
|
|
|
bitmap->qmp_locked = qmp_locked;
|
|
|
|
qemu_mutex_unlock(bitmap->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bdrv_dirty_bitmap_qmp_locked(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
return bitmap->qmp_locked;
|
|
|
|
}
|
|
|
|
|
2017-06-05 20:39:03 +08:00
|
|
|
/* Called with BQL taken. */
|
2016-03-08 12:44:55 +08:00
|
|
|
bool bdrv_dirty_bitmap_enabled(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
return !(bitmap->disabled || bitmap->successor);
|
|
|
|
}
|
|
|
|
|
2017-06-05 20:39:03 +08:00
|
|
|
/* Called with BQL taken. */
|
2016-03-08 12:44:55 +08:00
|
|
|
DirtyBitmapStatus bdrv_dirty_bitmap_status(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
if (bdrv_dirty_bitmap_frozen(bitmap)) {
|
|
|
|
return DIRTY_BITMAP_STATUS_FROZEN;
|
2018-03-14 03:34:00 +08:00
|
|
|
} else if (bdrv_dirty_bitmap_qmp_locked(bitmap)) {
|
|
|
|
return DIRTY_BITMAP_STATUS_LOCKED;
|
2016-03-08 12:44:55 +08:00
|
|
|
} else if (!bdrv_dirty_bitmap_enabled(bitmap)) {
|
|
|
|
return DIRTY_BITMAP_STATUS_DISABLED;
|
|
|
|
} else {
|
|
|
|
return DIRTY_BITMAP_STATUS_ACTIVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a successor bitmap destined to replace this bitmap after an operation.
|
|
|
|
* Requires that the bitmap is not frozen and has no successor.
|
2017-06-05 20:39:03 +08:00
|
|
|
* Called with BQL taken.
|
2016-03-08 12:44:55 +08:00
|
|
|
*/
|
|
|
|
int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs,
|
|
|
|
BdrvDirtyBitmap *bitmap, Error **errp)
|
|
|
|
{
|
|
|
|
uint64_t granularity;
|
|
|
|
BdrvDirtyBitmap *child;
|
|
|
|
|
|
|
|
if (bdrv_dirty_bitmap_frozen(bitmap)) {
|
|
|
|
error_setg(errp, "Cannot create a successor for a bitmap that is "
|
|
|
|
"currently frozen");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
assert(!bitmap->successor);
|
|
|
|
|
|
|
|
/* Create an anonymous successor */
|
|
|
|
granularity = bdrv_dirty_bitmap_granularity(bitmap);
|
|
|
|
child = bdrv_create_dirty_bitmap(bs, granularity, NULL, errp);
|
|
|
|
if (!child) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Successor will be on or off based on our current state. */
|
|
|
|
child->disabled = bitmap->disabled;
|
|
|
|
|
|
|
|
/* Install the successor and freeze the parent */
|
|
|
|
bitmap->successor = child;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-04 14:12:49 +08:00
|
|
|
void bdrv_enable_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
assert(!bdrv_dirty_bitmap_frozen(bitmap));
|
|
|
|
bitmap->disabled = false;
|
|
|
|
}
|
|
|
|
|
2018-03-14 03:33:59 +08:00
|
|
|
/* Called with BQL taken. */
|
|
|
|
void bdrv_dirty_bitmap_enable_successor(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
2018-07-04 14:12:49 +08:00
|
|
|
assert(bitmap->mutex == bitmap->successor->mutex);
|
2018-03-14 03:33:59 +08:00
|
|
|
qemu_mutex_lock(bitmap->mutex);
|
2018-07-04 14:12:49 +08:00
|
|
|
bdrv_enable_dirty_bitmap_locked(bitmap->successor);
|
2018-03-14 03:33:59 +08:00
|
|
|
qemu_mutex_unlock(bitmap->mutex);
|
|
|
|
}
|
|
|
|
|
2018-06-12 02:53:31 +08:00
|
|
|
/* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken. */
|
|
|
|
static void bdrv_release_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap)
|
2018-03-14 03:34:00 +08:00
|
|
|
{
|
2018-06-12 02:53:31 +08:00
|
|
|
assert(!bitmap->active_iterators);
|
|
|
|
assert(!bdrv_dirty_bitmap_frozen(bitmap));
|
|
|
|
assert(!bitmap->meta);
|
|
|
|
QLIST_REMOVE(bitmap, list);
|
|
|
|
hbitmap_free(bitmap->bitmap);
|
|
|
|
g_free(bitmap->name);
|
|
|
|
g_free(bitmap);
|
2018-03-14 03:34:00 +08:00
|
|
|
}
|
|
|
|
|
2016-03-08 12:44:55 +08:00
|
|
|
/**
|
|
|
|
* For a bitmap with a successor, yield our name to the successor,
|
|
|
|
* delete the old bitmap, and return a handle to the new bitmap.
|
2017-06-05 20:39:03 +08:00
|
|
|
* Called with BQL taken.
|
2016-03-08 12:44:55 +08:00
|
|
|
*/
|
|
|
|
BdrvDirtyBitmap *bdrv_dirty_bitmap_abdicate(BlockDriverState *bs,
|
|
|
|
BdrvDirtyBitmap *bitmap,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
BdrvDirtyBitmap *successor = bitmap->successor;
|
|
|
|
|
|
|
|
if (successor == NULL) {
|
|
|
|
error_setg(errp, "Cannot relinquish control if "
|
|
|
|
"there's no successor present");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = bitmap->name;
|
|
|
|
bitmap->name = NULL;
|
|
|
|
successor->name = name;
|
|
|
|
bitmap->successor = NULL;
|
2017-06-28 20:05:17 +08:00
|
|
|
successor->persistent = bitmap->persistent;
|
|
|
|
bitmap->persistent = false;
|
2016-03-08 12:44:55 +08:00
|
|
|
bdrv_release_dirty_bitmap(bs, bitmap);
|
|
|
|
|
|
|
|
return successor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In cases of failure where we can no longer safely delete the parent,
|
|
|
|
* we may wish to re-join the parent and child/successor.
|
|
|
|
* The merged parent will be un-frozen, but not explicitly re-enabled.
|
2018-03-14 03:34:00 +08:00
|
|
|
* Called within bdrv_dirty_bitmap_lock..unlock and with BQL taken.
|
2016-03-08 12:44:55 +08:00
|
|
|
*/
|
2018-03-14 03:34:00 +08:00
|
|
|
BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs,
|
|
|
|
BdrvDirtyBitmap *parent,
|
|
|
|
Error **errp)
|
2016-03-08 12:44:55 +08:00
|
|
|
{
|
2018-03-14 03:34:00 +08:00
|
|
|
BdrvDirtyBitmap *successor = parent->successor;
|
2016-03-08 12:44:55 +08:00
|
|
|
|
|
|
|
if (!successor) {
|
|
|
|
error_setg(errp, "Cannot reclaim a successor when none is present");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hbitmap_merge(parent->bitmap, successor->bitmap)) {
|
|
|
|
error_setg(errp, "Merging of parent and successor bitmap failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-06-12 02:53:31 +08:00
|
|
|
bdrv_release_dirty_bitmap_locked(successor);
|
2016-03-08 12:44:55 +08:00
|
|
|
parent->successor = NULL;
|
|
|
|
|
2018-03-14 03:34:00 +08:00
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called with BQL taken. */
|
|
|
|
BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs,
|
|
|
|
BdrvDirtyBitmap *parent,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
BdrvDirtyBitmap *ret;
|
|
|
|
|
|
|
|
qemu_mutex_lock(parent->mutex);
|
|
|
|
ret = bdrv_reclaim_dirty_bitmap_locked(bs, parent, errp);
|
2018-03-14 03:34:00 +08:00
|
|
|
qemu_mutex_unlock(parent->mutex);
|
|
|
|
|
2018-03-14 03:34:00 +08:00
|
|
|
return ret;
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Truncates _all_ bitmaps attached to a BDS.
|
2017-06-05 20:39:03 +08:00
|
|
|
* Called with BQL taken.
|
2016-03-08 12:44:55 +08:00
|
|
|
*/
|
2017-09-25 22:55:11 +08:00
|
|
|
void bdrv_dirty_bitmap_truncate(BlockDriverState *bs, int64_t bytes)
|
2016-03-08 12:44:55 +08:00
|
|
|
{
|
|
|
|
BdrvDirtyBitmap *bitmap;
|
|
|
|
|
2017-06-05 20:39:03 +08:00
|
|
|
bdrv_dirty_bitmaps_lock(bs);
|
2016-03-08 12:44:55 +08:00
|
|
|
QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
|
|
|
|
assert(!bdrv_dirty_bitmap_frozen(bitmap));
|
2016-10-14 05:58:21 +08:00
|
|
|
assert(!bitmap->active_iterators);
|
2017-09-25 22:55:26 +08:00
|
|
|
hbitmap_truncate(bitmap->bitmap, bytes);
|
2017-09-25 22:55:13 +08:00
|
|
|
bitmap->size = bytes;
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
2017-06-05 20:39:03 +08:00
|
|
|
bdrv_dirty_bitmaps_unlock(bs);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
2017-06-05 20:39:03 +08:00
|
|
|
/* Called with BQL taken. */
|
2016-03-08 12:44:55 +08:00
|
|
|
void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
2018-06-12 02:53:31 +08:00
|
|
|
bdrv_dirty_bitmaps_lock(bs);
|
|
|
|
bdrv_release_dirty_bitmap_locked(bitmap);
|
|
|
|
bdrv_dirty_bitmaps_unlock(bs);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release all named dirty bitmaps attached to a BDS (for use in bdrv_close()).
|
|
|
|
* There must not be any frozen bitmaps attached.
|
2017-06-28 20:05:27 +08:00
|
|
|
* This function does not remove persistent bitmaps from the storage.
|
2017-06-05 20:39:03 +08:00
|
|
|
* Called with BQL taken.
|
2016-03-08 12:44:55 +08:00
|
|
|
*/
|
|
|
|
void bdrv_release_named_dirty_bitmaps(BlockDriverState *bs)
|
|
|
|
{
|
2018-06-12 02:53:31 +08:00
|
|
|
BdrvDirtyBitmap *bm, *next;
|
|
|
|
|
|
|
|
bdrv_dirty_bitmaps_lock(bs);
|
|
|
|
QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
|
|
|
|
if (bdrv_dirty_bitmap_name(bm)) {
|
|
|
|
bdrv_release_dirty_bitmap_locked(bm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bdrv_dirty_bitmaps_unlock(bs);
|
2017-06-28 20:05:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release all persistent dirty bitmaps attached to a BDS (for use in
|
|
|
|
* bdrv_inactivate_recurse()).
|
|
|
|
* There must not be any frozen bitmaps attached.
|
|
|
|
* This function does not remove persistent bitmaps from the storage.
|
2018-06-12 02:53:31 +08:00
|
|
|
* Called with BQL taken.
|
2017-06-28 20:05:30 +08:00
|
|
|
*/
|
|
|
|
void bdrv_release_persistent_dirty_bitmaps(BlockDriverState *bs)
|
|
|
|
{
|
2018-06-12 02:53:31 +08:00
|
|
|
BdrvDirtyBitmap *bm, *next;
|
|
|
|
|
|
|
|
bdrv_dirty_bitmaps_lock(bs);
|
|
|
|
QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) {
|
|
|
|
if (bdrv_dirty_bitmap_get_persistance(bm)) {
|
|
|
|
bdrv_release_dirty_bitmap_locked(bm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bdrv_dirty_bitmaps_unlock(bs);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
2017-06-28 20:05:27 +08:00
|
|
|
/**
|
|
|
|
* Remove persistent dirty bitmap from the storage if it exists.
|
|
|
|
* Absence of bitmap is not an error, because we have the following scenario:
|
|
|
|
* BdrvDirtyBitmap can have .persistent = true but not yet saved and have no
|
|
|
|
* stored version. For such bitmap bdrv_remove_persistent_dirty_bitmap() should
|
|
|
|
* not fail.
|
|
|
|
* This function doesn't release corresponding BdrvDirtyBitmap.
|
|
|
|
*/
|
|
|
|
void bdrv_remove_persistent_dirty_bitmap(BlockDriverState *bs,
|
|
|
|
const char *name,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
if (bs->drv && bs->drv->bdrv_remove_persistent_dirty_bitmap) {
|
|
|
|
bs->drv->bdrv_remove_persistent_dirty_bitmap(bs, name, errp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 12:44:55 +08:00
|
|
|
void bdrv_disable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
2018-06-12 02:53:31 +08:00
|
|
|
bdrv_dirty_bitmap_lock(bitmap);
|
2016-03-08 12:44:55 +08:00
|
|
|
assert(!bdrv_dirty_bitmap_frozen(bitmap));
|
|
|
|
bitmap->disabled = true;
|
2018-06-12 02:53:31 +08:00
|
|
|
bdrv_dirty_bitmap_unlock(bitmap);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bdrv_enable_dirty_bitmap(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
2018-06-12 02:53:31 +08:00
|
|
|
bdrv_dirty_bitmap_lock(bitmap);
|
2018-07-04 14:12:49 +08:00
|
|
|
bdrv_enable_dirty_bitmap_locked(bitmap);
|
2018-06-12 02:53:31 +08:00
|
|
|
bdrv_dirty_bitmap_unlock(bitmap);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BdrvDirtyBitmap *bm;
|
|
|
|
BlockDirtyInfoList *list = NULL;
|
|
|
|
BlockDirtyInfoList **plist = &list;
|
|
|
|
|
2017-06-05 20:39:03 +08:00
|
|
|
bdrv_dirty_bitmaps_lock(bs);
|
2016-03-08 12:44:55 +08:00
|
|
|
QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
|
|
|
|
BlockDirtyInfo *info = g_new0(BlockDirtyInfo, 1);
|
|
|
|
BlockDirtyInfoList *entry = g_new0(BlockDirtyInfoList, 1);
|
2017-09-25 22:55:18 +08:00
|
|
|
info->count = bdrv_get_dirty_count(bm);
|
2016-03-08 12:44:55 +08:00
|
|
|
info->granularity = bdrv_dirty_bitmap_granularity(bm);
|
|
|
|
info->has_name = !!bm->name;
|
|
|
|
info->name = g_strdup(bm->name);
|
|
|
|
info->status = bdrv_dirty_bitmap_status(bm);
|
|
|
|
entry->value = info;
|
|
|
|
*plist = entry;
|
|
|
|
plist = &entry->next;
|
|
|
|
}
|
2017-06-05 20:39:03 +08:00
|
|
|
bdrv_dirty_bitmaps_unlock(bs);
|
2016-03-08 12:44:55 +08:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-06-05 20:39:05 +08:00
|
|
|
/* Called within bdrv_dirty_bitmap_lock..unlock */
|
2017-09-25 22:55:19 +08:00
|
|
|
bool bdrv_get_dirty_locked(BlockDriverState *bs, BdrvDirtyBitmap *bitmap,
|
|
|
|
int64_t offset)
|
2016-03-08 12:44:55 +08:00
|
|
|
{
|
|
|
|
if (bitmap) {
|
2017-09-25 22:55:26 +08:00
|
|
|
return hbitmap_get(bitmap->bitmap, offset);
|
2016-03-08 12:44:55 +08:00
|
|
|
} else {
|
2017-09-25 22:55:19 +08:00
|
|
|
return false;
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Chooses a default granularity based on the existing cluster size,
|
|
|
|
* but clamped between [4K, 64K]. Defaults to 64K in the case that there
|
|
|
|
* is no cluster size information available.
|
|
|
|
*/
|
|
|
|
uint32_t bdrv_get_default_bitmap_granularity(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BlockDriverInfo bdi;
|
|
|
|
uint32_t granularity;
|
|
|
|
|
|
|
|
if (bdrv_get_info(bs, &bdi) >= 0 && bdi.cluster_size > 0) {
|
|
|
|
granularity = MAX(4096, bdi.cluster_size);
|
|
|
|
granularity = MIN(65536, granularity);
|
|
|
|
} else {
|
|
|
|
granularity = 65536;
|
|
|
|
}
|
|
|
|
|
|
|
|
return granularity;
|
|
|
|
}
|
|
|
|
|
2017-06-28 20:05:05 +08:00
|
|
|
uint32_t bdrv_dirty_bitmap_granularity(const BdrvDirtyBitmap *bitmap)
|
2016-03-08 12:44:55 +08:00
|
|
|
{
|
2017-09-25 22:55:26 +08:00
|
|
|
return 1U << hbitmap_granularity(bitmap->bitmap);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
2017-09-25 22:55:16 +08:00
|
|
|
BdrvDirtyBitmapIter *bdrv_dirty_iter_new(BdrvDirtyBitmap *bitmap)
|
2016-10-14 05:58:21 +08:00
|
|
|
{
|
|
|
|
BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
|
2017-09-25 22:55:16 +08:00
|
|
|
hbitmap_iter_init(&iter->hbi, bitmap->bitmap, 0);
|
2016-10-14 05:58:21 +08:00
|
|
|
iter->bitmap = bitmap;
|
|
|
|
bitmap->active_iterators++;
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2016-10-14 05:58:30 +08:00
|
|
|
BdrvDirtyBitmapIter *bdrv_dirty_meta_iter_new(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
BdrvDirtyBitmapIter *iter = g_new(BdrvDirtyBitmapIter, 1);
|
|
|
|
hbitmap_iter_init(&iter->hbi, bitmap->meta, 0);
|
|
|
|
iter->bitmap = bitmap;
|
|
|
|
bitmap->active_iterators++;
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2016-10-14 05:58:21 +08:00
|
|
|
void bdrv_dirty_iter_free(BdrvDirtyBitmapIter *iter)
|
|
|
|
{
|
|
|
|
if (!iter) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(iter->bitmap->active_iterators > 0);
|
|
|
|
iter->bitmap->active_iterators--;
|
|
|
|
g_free(iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
|
2016-03-08 12:44:55 +08:00
|
|
|
{
|
2018-06-14 02:18:16 +08:00
|
|
|
return hbitmap_iter_next(&iter->hbi, true);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
2018-06-14 02:18:18 +08:00
|
|
|
/**
|
|
|
|
* Return the next consecutively dirty area in the dirty bitmap
|
|
|
|
* belonging to the given iterator @iter.
|
|
|
|
*
|
|
|
|
* @max_offset: Maximum value that may be returned for
|
|
|
|
* *offset + *bytes
|
|
|
|
* @offset: Will contain the start offset of the next dirty area
|
|
|
|
* @bytes: Will contain the length of the next dirty area
|
|
|
|
*
|
|
|
|
* Returns: True if a dirty area could be found before max_offset
|
|
|
|
* (which means that *offset and *bytes then contain valid
|
|
|
|
* values), false otherwise.
|
|
|
|
*
|
|
|
|
* Note that @iter is never advanced if false is returned. If an area
|
|
|
|
* is found (which means that true is returned), it will be advanced
|
|
|
|
* past that area.
|
|
|
|
*/
|
|
|
|
bool bdrv_dirty_iter_next_area(BdrvDirtyBitmapIter *iter, uint64_t max_offset,
|
|
|
|
uint64_t *offset, int *bytes)
|
|
|
|
{
|
|
|
|
uint32_t granularity = bdrv_dirty_bitmap_granularity(iter->bitmap);
|
|
|
|
uint64_t gran_max_offset;
|
|
|
|
int64_t ret;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (max_offset == iter->bitmap->size) {
|
|
|
|
/* If max_offset points to the image end, round it up by the
|
|
|
|
* bitmap granularity */
|
|
|
|
gran_max_offset = ROUND_UP(max_offset, granularity);
|
|
|
|
} else {
|
|
|
|
gran_max_offset = max_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = hbitmap_iter_next(&iter->hbi, false);
|
|
|
|
if (ret < 0 || ret + granularity > gran_max_offset) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*offset = ret;
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
assert(granularity <= INT_MAX);
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* Advance iterator */
|
|
|
|
ret = hbitmap_iter_next(&iter->hbi, true);
|
|
|
|
size += granularity;
|
|
|
|
} while (ret + granularity <= gran_max_offset &&
|
|
|
|
hbitmap_iter_next(&iter->hbi, false) == ret + granularity &&
|
|
|
|
size <= INT_MAX - granularity);
|
|
|
|
|
|
|
|
*bytes = MIN(size, max_offset - *offset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-05 20:39:05 +08:00
|
|
|
/* Called within bdrv_dirty_bitmap_lock..unlock */
|
|
|
|
void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
|
2017-09-25 22:55:20 +08:00
|
|
|
int64_t offset, int64_t bytes)
|
2017-06-05 20:39:05 +08:00
|
|
|
{
|
|
|
|
assert(bdrv_dirty_bitmap_enabled(bitmap));
|
2017-06-28 20:05:10 +08:00
|
|
|
assert(!bdrv_dirty_bitmap_readonly(bitmap));
|
2017-09-25 22:55:26 +08:00
|
|
|
hbitmap_set(bitmap->bitmap, offset, bytes);
|
2017-06-05 20:39:05 +08:00
|
|
|
}
|
|
|
|
|
2016-03-08 12:44:55 +08:00
|
|
|
void bdrv_set_dirty_bitmap(BdrvDirtyBitmap *bitmap,
|
2017-09-25 22:55:20 +08:00
|
|
|
int64_t offset, int64_t bytes)
|
2017-06-05 20:39:05 +08:00
|
|
|
{
|
|
|
|
bdrv_dirty_bitmap_lock(bitmap);
|
2017-09-25 22:55:20 +08:00
|
|
|
bdrv_set_dirty_bitmap_locked(bitmap, offset, bytes);
|
2017-06-05 20:39:05 +08:00
|
|
|
bdrv_dirty_bitmap_unlock(bitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called within bdrv_dirty_bitmap_lock..unlock */
|
|
|
|
void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
|
2017-09-25 22:55:20 +08:00
|
|
|
int64_t offset, int64_t bytes)
|
2016-03-08 12:44:55 +08:00
|
|
|
{
|
|
|
|
assert(bdrv_dirty_bitmap_enabled(bitmap));
|
2017-06-28 20:05:10 +08:00
|
|
|
assert(!bdrv_dirty_bitmap_readonly(bitmap));
|
2017-09-25 22:55:26 +08:00
|
|
|
hbitmap_reset(bitmap->bitmap, offset, bytes);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bdrv_reset_dirty_bitmap(BdrvDirtyBitmap *bitmap,
|
2017-09-25 22:55:20 +08:00
|
|
|
int64_t offset, int64_t bytes)
|
2016-03-08 12:44:55 +08:00
|
|
|
{
|
2017-06-05 20:39:05 +08:00
|
|
|
bdrv_dirty_bitmap_lock(bitmap);
|
2017-09-25 22:55:20 +08:00
|
|
|
bdrv_reset_dirty_bitmap_locked(bitmap, offset, bytes);
|
2017-06-05 20:39:05 +08:00
|
|
|
bdrv_dirty_bitmap_unlock(bitmap);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out)
|
|
|
|
{
|
|
|
|
assert(bdrv_dirty_bitmap_enabled(bitmap));
|
2017-06-28 20:05:10 +08:00
|
|
|
assert(!bdrv_dirty_bitmap_readonly(bitmap));
|
2017-06-05 20:39:05 +08:00
|
|
|
bdrv_dirty_bitmap_lock(bitmap);
|
2016-03-08 12:44:55 +08:00
|
|
|
if (!out) {
|
|
|
|
hbitmap_reset_all(bitmap->bitmap);
|
|
|
|
} else {
|
|
|
|
HBitmap *backup = bitmap->bitmap;
|
2017-09-25 22:55:26 +08:00
|
|
|
bitmap->bitmap = hbitmap_alloc(bitmap->size,
|
2016-03-08 12:44:55 +08:00
|
|
|
hbitmap_granularity(backup));
|
|
|
|
*out = backup;
|
|
|
|
}
|
2017-06-05 20:39:05 +08:00
|
|
|
bdrv_dirty_bitmap_unlock(bitmap);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bdrv_undo_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *in)
|
|
|
|
{
|
|
|
|
HBitmap *tmp = bitmap->bitmap;
|
|
|
|
assert(bdrv_dirty_bitmap_enabled(bitmap));
|
2017-06-28 20:05:10 +08:00
|
|
|
assert(!bdrv_dirty_bitmap_readonly(bitmap));
|
2016-03-08 12:44:55 +08:00
|
|
|
bitmap->bitmap = in;
|
|
|
|
hbitmap_free(tmp);
|
|
|
|
}
|
|
|
|
|
2016-10-14 05:58:28 +08:00
|
|
|
uint64_t bdrv_dirty_bitmap_serialization_size(const BdrvDirtyBitmap *bitmap,
|
2017-09-25 22:55:14 +08:00
|
|
|
uint64_t offset, uint64_t bytes)
|
2016-10-14 05:58:28 +08:00
|
|
|
{
|
2017-09-25 22:55:26 +08:00
|
|
|
return hbitmap_serialization_size(bitmap->bitmap, offset, bytes);
|
2016-10-14 05:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t bdrv_dirty_bitmap_serialization_align(const BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
2017-09-25 22:55:26 +08:00
|
|
|
return hbitmap_serialization_align(bitmap->bitmap);
|
2016-10-14 05:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bdrv_dirty_bitmap_serialize_part(const BdrvDirtyBitmap *bitmap,
|
2017-09-25 22:55:14 +08:00
|
|
|
uint8_t *buf, uint64_t offset,
|
|
|
|
uint64_t bytes)
|
2016-10-14 05:58:28 +08:00
|
|
|
{
|
2017-09-25 22:55:26 +08:00
|
|
|
hbitmap_serialize_part(bitmap->bitmap, buf, offset, bytes);
|
2016-10-14 05:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bdrv_dirty_bitmap_deserialize_part(BdrvDirtyBitmap *bitmap,
|
2017-09-25 22:55:14 +08:00
|
|
|
uint8_t *buf, uint64_t offset,
|
|
|
|
uint64_t bytes, bool finish)
|
2016-10-14 05:58:28 +08:00
|
|
|
{
|
2017-09-25 22:55:26 +08:00
|
|
|
hbitmap_deserialize_part(bitmap->bitmap, buf, offset, bytes, finish);
|
2016-10-14 05:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bdrv_dirty_bitmap_deserialize_zeroes(BdrvDirtyBitmap *bitmap,
|
2017-09-25 22:55:14 +08:00
|
|
|
uint64_t offset, uint64_t bytes,
|
2016-10-14 05:58:28 +08:00
|
|
|
bool finish)
|
|
|
|
{
|
2017-09-25 22:55:26 +08:00
|
|
|
hbitmap_deserialize_zeroes(bitmap->bitmap, offset, bytes, finish);
|
2017-06-28 20:05:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bdrv_dirty_bitmap_deserialize_ones(BdrvDirtyBitmap *bitmap,
|
2017-09-25 22:55:14 +08:00
|
|
|
uint64_t offset, uint64_t bytes,
|
2017-06-28 20:05:06 +08:00
|
|
|
bool finish)
|
|
|
|
{
|
2017-09-25 22:55:26 +08:00
|
|
|
hbitmap_deserialize_ones(bitmap->bitmap, offset, bytes, finish);
|
2016-10-14 05:58:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bdrv_dirty_bitmap_deserialize_finish(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
hbitmap_deserialize_finish(bitmap->bitmap);
|
|
|
|
}
|
|
|
|
|
2017-09-25 22:55:25 +08:00
|
|
|
void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes)
|
2016-03-08 12:44:55 +08:00
|
|
|
{
|
|
|
|
BdrvDirtyBitmap *bitmap;
|
2017-06-05 20:39:03 +08:00
|
|
|
|
|
|
|
if (QLIST_EMPTY(&bs->dirty_bitmaps)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bdrv_dirty_bitmaps_lock(bs);
|
2016-03-08 12:44:55 +08:00
|
|
|
QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) {
|
|
|
|
if (!bdrv_dirty_bitmap_enabled(bitmap)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-28 20:05:10 +08:00
|
|
|
assert(!bdrv_dirty_bitmap_readonly(bitmap));
|
2017-09-25 22:55:26 +08:00
|
|
|
hbitmap_set(bitmap->bitmap, offset, bytes);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
2017-06-05 20:39:03 +08:00
|
|
|
bdrv_dirty_bitmaps_unlock(bs);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-14 05:58:21 +08:00
|
|
|
* Advance a BdrvDirtyBitmapIter to an arbitrary offset.
|
2016-03-08 12:44:55 +08:00
|
|
|
*/
|
2017-09-25 22:55:16 +08:00
|
|
|
void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *iter, int64_t offset)
|
2016-03-08 12:44:55 +08:00
|
|
|
{
|
2017-09-25 22:55:26 +08:00
|
|
|
hbitmap_iter_init(&iter->hbi, iter->hbi.hb, offset);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
2017-09-25 22:55:26 +08:00
|
|
|
return hbitmap_count(bitmap->bitmap);
|
2016-03-08 12:44:55 +08:00
|
|
|
}
|
2016-10-14 05:58:30 +08:00
|
|
|
|
|
|
|
int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
return hbitmap_count(bitmap->meta);
|
|
|
|
}
|
2017-06-28 20:05:10 +08:00
|
|
|
|
|
|
|
bool bdrv_dirty_bitmap_readonly(const BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
return bitmap->readonly;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called with BQL taken. */
|
|
|
|
void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value)
|
|
|
|
{
|
|
|
|
qemu_mutex_lock(bitmap->mutex);
|
|
|
|
bitmap->readonly = value;
|
|
|
|
qemu_mutex_unlock(bitmap->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bdrv_has_readonly_bitmaps(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BdrvDirtyBitmap *bm;
|
|
|
|
QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
|
|
|
|
if (bm->readonly) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-28 20:05:15 +08:00
|
|
|
|
2017-06-28 20:05:17 +08:00
|
|
|
/* Called with BQL taken. */
|
|
|
|
void bdrv_dirty_bitmap_set_persistance(BdrvDirtyBitmap *bitmap, bool persistent)
|
|
|
|
{
|
|
|
|
qemu_mutex_lock(bitmap->mutex);
|
|
|
|
bitmap->persistent = persistent;
|
|
|
|
qemu_mutex_unlock(bitmap->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bdrv_dirty_bitmap_get_persistance(BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
return bitmap->persistent;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bdrv_has_changed_persistent_bitmaps(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BdrvDirtyBitmap *bm;
|
|
|
|
QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) {
|
|
|
|
if (bm->persistent && !bm->readonly) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-28 20:05:18 +08:00
|
|
|
|
|
|
|
BdrvDirtyBitmap *bdrv_dirty_bitmap_next(BlockDriverState *bs,
|
|
|
|
BdrvDirtyBitmap *bitmap)
|
|
|
|
{
|
|
|
|
return bitmap == NULL ? QLIST_FIRST(&bs->dirty_bitmaps) :
|
|
|
|
QLIST_NEXT(bitmap, list);
|
|
|
|
}
|
2017-06-28 20:05:25 +08:00
|
|
|
|
|
|
|
char *bdrv_dirty_bitmap_sha256(const BdrvDirtyBitmap *bitmap, Error **errp)
|
|
|
|
{
|
|
|
|
return hbitmap_sha256(bitmap->bitmap, errp);
|
|
|
|
}
|
2017-10-12 21:53:09 +08:00
|
|
|
|
|
|
|
int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset)
|
|
|
|
{
|
|
|
|
return hbitmap_next_zero(bitmap->bitmap, offset);
|
|
|
|
}
|
2018-06-12 02:53:32 +08:00
|
|
|
|
|
|
|
void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
/* only bitmaps from one bds are supported */
|
|
|
|
assert(dest->mutex == src->mutex);
|
|
|
|
|
|
|
|
qemu_mutex_lock(dest->mutex);
|
|
|
|
|
|
|
|
assert(bdrv_dirty_bitmap_enabled(dest));
|
|
|
|
assert(!bdrv_dirty_bitmap_readonly(dest));
|
|
|
|
|
|
|
|
if (!hbitmap_merge(dest->bitmap, src->bitmap)) {
|
|
|
|
error_setg(errp, "Bitmaps are incompatible and can't be merged");
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_mutex_unlock(dest->mutex);
|
|
|
|
}
|