2009-05-28 22:07:04 +08:00
|
|
|
/*
|
|
|
|
* Block driver for the QCOW version 2 format
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004-2006 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLOCK_QCOW2_H
|
|
|
|
#define BLOCK_QCOW2_H
|
|
|
|
|
2015-07-02 01:10:37 +08:00
|
|
|
#include "crypto/cipher.h"
|
2012-12-18 01:19:44 +08:00
|
|
|
#include "block/coroutine.h"
|
2009-05-28 22:07:04 +08:00
|
|
|
|
2009-06-24 08:17:30 +08:00
|
|
|
//#define DEBUG_ALLOC
|
|
|
|
//#define DEBUG_ALLOC2
|
|
|
|
//#define DEBUG_EXT
|
|
|
|
|
2009-05-28 22:07:04 +08:00
|
|
|
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
|
|
|
|
|
|
|
|
#define QCOW_CRYPT_NONE 0
|
|
|
|
#define QCOW_CRYPT_AES 1
|
|
|
|
|
|
|
|
#define QCOW_MAX_CRYPT_CLUSTERS 32
|
2014-03-26 20:05:45 +08:00
|
|
|
#define QCOW_MAX_SNAPSHOTS 65536
|
2009-05-28 22:07:04 +08:00
|
|
|
|
2014-03-26 20:05:50 +08:00
|
|
|
/* 8 MB refcount table is enough for 2 PB images at 64k cluster size
|
|
|
|
* (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
|
|
|
|
#define QCOW_MAX_REFTABLE_SIZE 0x800000
|
|
|
|
|
2014-03-26 20:06:06 +08:00
|
|
|
/* 32 MB L1 table is enough for 2 PB images at 64k cluster size
|
|
|
|
* (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
|
|
|
|
#define QCOW_MAX_L1_SIZE 0x2000000
|
|
|
|
|
2014-03-26 20:06:07 +08:00
|
|
|
/* Allow for an average of 1k per snapshot table entry, should be plenty of
|
|
|
|
* space for snapshot names and IDs */
|
|
|
|
#define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS)
|
|
|
|
|
2009-05-28 22:07:04 +08:00
|
|
|
/* indicate that the refcount of the referenced cluster is exactly one. */
|
2013-08-24 00:35:45 +08:00
|
|
|
#define QCOW_OFLAG_COPIED (1ULL << 63)
|
2009-05-28 22:07:04 +08:00
|
|
|
/* indicate that the cluster is compressed (they never have the copied flag) */
|
2013-08-24 00:35:45 +08:00
|
|
|
#define QCOW_OFLAG_COMPRESSED (1ULL << 62)
|
2012-03-16 22:02:38 +08:00
|
|
|
/* The cluster reads as all zeros */
|
2013-08-24 00:35:45 +08:00
|
|
|
#define QCOW_OFLAG_ZERO (1ULL << 0)
|
2009-05-28 22:07:04 +08:00
|
|
|
|
|
|
|
#define MIN_CLUSTER_BITS 9
|
2009-09-15 18:30:43 +08:00
|
|
|
#define MAX_CLUSTER_BITS 21
|
2009-05-28 22:07:04 +08:00
|
|
|
|
2015-06-02 00:09:17 +08:00
|
|
|
/* Must be at least 2 to cover COW */
|
|
|
|
#define MIN_L2_CACHE_SIZE 2 /* clusters */
|
2009-05-28 22:07:04 +08:00
|
|
|
|
2011-01-11 00:17:28 +08:00
|
|
|
/* Must be at least 4 to cover all cases of refcount table growth */
|
2014-08-19 04:07:31 +08:00
|
|
|
#define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
|
|
|
|
|
2015-06-02 00:09:19 +08:00
|
|
|
/* Whichever is more */
|
|
|
|
#define DEFAULT_L2_CACHE_CLUSTERS 8 /* clusters */
|
2014-08-19 04:07:31 +08:00
|
|
|
#define DEFAULT_L2_CACHE_BYTE_SIZE 1048576 /* bytes */
|
|
|
|
|
|
|
|
/* The refblock cache needs only a fourth of the L2 cache size to cover as many
|
|
|
|
* clusters */
|
|
|
|
#define DEFAULT_L2_REFCOUNT_SIZE_RATIO 4
|
2011-01-11 00:17:28 +08:00
|
|
|
|
2011-05-31 21:01:46 +08:00
|
|
|
#define DEFAULT_CLUSTER_SIZE 65536
|
|
|
|
|
2013-03-18 20:08:10 +08:00
|
|
|
|
2013-07-17 20:45:34 +08:00
|
|
|
#define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
|
|
|
|
#define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
|
|
|
|
#define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
|
|
|
|
#define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
|
2013-10-10 17:09:25 +08:00
|
|
|
#define QCOW2_OPT_OVERLAP "overlap-check"
|
2014-08-21 01:59:35 +08:00
|
|
|
#define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
|
2013-10-10 17:09:25 +08:00
|
|
|
#define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
|
|
|
|
#define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
|
|
|
|
#define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
|
|
|
|
#define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
|
|
|
|
#define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
|
|
|
|
#define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
|
|
|
|
#define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
|
|
|
|
#define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
|
2014-08-19 04:07:33 +08:00
|
|
|
#define QCOW2_OPT_CACHE_SIZE "cache-size"
|
|
|
|
#define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
|
|
|
|
#define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
|
2015-08-04 20:14:40 +08:00
|
|
|
#define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
|
2013-03-18 20:08:10 +08:00
|
|
|
|
2009-05-28 22:07:04 +08:00
|
|
|
typedef struct QCowHeader {
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t version;
|
|
|
|
uint64_t backing_file_offset;
|
|
|
|
uint32_t backing_file_size;
|
|
|
|
uint32_t cluster_bits;
|
|
|
|
uint64_t size; /* in bytes */
|
|
|
|
uint32_t crypt_method;
|
|
|
|
uint32_t l1_size; /* XXX: save number of clusters instead ? */
|
|
|
|
uint64_t l1_table_offset;
|
|
|
|
uint64_t refcount_table_offset;
|
|
|
|
uint32_t refcount_table_clusters;
|
|
|
|
uint32_t nb_snapshots;
|
|
|
|
uint64_t snapshots_offset;
|
2011-12-15 19:20:58 +08:00
|
|
|
|
|
|
|
/* The following fields are only valid for version >= 3 */
|
|
|
|
uint64_t incompatible_features;
|
|
|
|
uint64_t compatible_features;
|
|
|
|
uint64_t autoclear_features;
|
|
|
|
|
|
|
|
uint32_t refcount_order;
|
|
|
|
uint32_t header_length;
|
2013-09-26 00:08:50 +08:00
|
|
|
} QEMU_PACKED QCowHeader;
|
2009-05-28 22:07:04 +08:00
|
|
|
|
2014-03-26 20:05:45 +08:00
|
|
|
typedef struct QEMU_PACKED QCowSnapshotHeader {
|
|
|
|
/* header is 8 byte aligned */
|
|
|
|
uint64_t l1_table_offset;
|
|
|
|
|
|
|
|
uint32_t l1_size;
|
|
|
|
uint16_t id_str_size;
|
|
|
|
uint16_t name_size;
|
|
|
|
|
|
|
|
uint32_t date_sec;
|
|
|
|
uint32_t date_nsec;
|
|
|
|
|
|
|
|
uint64_t vm_clock_nsec;
|
|
|
|
|
|
|
|
uint32_t vm_state_size;
|
|
|
|
uint32_t extra_data_size; /* for extension */
|
|
|
|
/* extra data follows */
|
|
|
|
/* id_str follows */
|
|
|
|
/* name follows */
|
|
|
|
} QCowSnapshotHeader;
|
|
|
|
|
|
|
|
typedef struct QEMU_PACKED QCowSnapshotExtraData {
|
|
|
|
uint64_t vm_state_size_large;
|
|
|
|
uint64_t disk_size;
|
|
|
|
} QCowSnapshotExtraData;
|
|
|
|
|
|
|
|
|
2009-05-28 22:07:04 +08:00
|
|
|
typedef struct QCowSnapshot {
|
|
|
|
uint64_t l1_table_offset;
|
|
|
|
uint32_t l1_size;
|
|
|
|
char *id_str;
|
|
|
|
char *name;
|
2012-04-11 22:33:50 +08:00
|
|
|
uint64_t disk_size;
|
2011-11-16 18:35:54 +08:00
|
|
|
uint64_t vm_state_size;
|
2009-05-28 22:07:04 +08:00
|
|
|
uint32_t date_sec;
|
|
|
|
uint32_t date_nsec;
|
|
|
|
uint64_t vm_clock_nsec;
|
|
|
|
} QCowSnapshot;
|
|
|
|
|
2011-01-11 00:15:10 +08:00
|
|
|
struct Qcow2Cache;
|
|
|
|
typedef struct Qcow2Cache Qcow2Cache;
|
|
|
|
|
2012-02-02 21:52:08 +08:00
|
|
|
typedef struct Qcow2UnknownHeaderExtension {
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t len;
|
|
|
|
QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
|
|
|
|
uint8_t data[];
|
|
|
|
} Qcow2UnknownHeaderExtension;
|
|
|
|
|
2012-04-12 21:20:27 +08:00
|
|
|
enum {
|
|
|
|
QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
|
|
|
|
QCOW2_FEAT_TYPE_COMPATIBLE = 1,
|
|
|
|
QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
|
|
|
|
};
|
|
|
|
|
2012-07-27 16:05:19 +08:00
|
|
|
/* Incompatible feature bits */
|
|
|
|
enum {
|
|
|
|
QCOW2_INCOMPAT_DIRTY_BITNR = 0,
|
2013-08-30 20:34:24 +08:00
|
|
|
QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
|
2012-07-27 16:05:19 +08:00
|
|
|
QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
|
2013-08-30 20:34:24 +08:00
|
|
|
QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
|
2012-07-27 16:05:19 +08:00
|
|
|
|
2013-08-30 20:34:24 +08:00
|
|
|
QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY
|
|
|
|
| QCOW2_INCOMPAT_CORRUPT,
|
2012-07-27 16:05:19 +08:00
|
|
|
};
|
|
|
|
|
2012-07-27 16:05:22 +08:00
|
|
|
/* Compatible feature bits */
|
|
|
|
enum {
|
|
|
|
QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
|
|
|
|
QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
|
|
|
|
|
|
|
|
QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS,
|
|
|
|
};
|
|
|
|
|
2013-06-19 19:44:18 +08:00
|
|
|
enum qcow2_discard_type {
|
|
|
|
QCOW2_DISCARD_NEVER = 0,
|
|
|
|
QCOW2_DISCARD_ALWAYS,
|
|
|
|
QCOW2_DISCARD_REQUEST,
|
|
|
|
QCOW2_DISCARD_SNAPSHOT,
|
|
|
|
QCOW2_DISCARD_OTHER,
|
|
|
|
QCOW2_DISCARD_MAX
|
|
|
|
};
|
|
|
|
|
2012-04-12 21:20:27 +08:00
|
|
|
typedef struct Qcow2Feature {
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t bit;
|
|
|
|
char name[46];
|
|
|
|
} QEMU_PACKED Qcow2Feature;
|
|
|
|
|
2013-06-19 19:44:20 +08:00
|
|
|
typedef struct Qcow2DiscardRegion {
|
|
|
|
BlockDriverState *bs;
|
|
|
|
uint64_t offset;
|
|
|
|
uint64_t bytes;
|
|
|
|
QTAILQ_ENTRY(Qcow2DiscardRegion) next;
|
|
|
|
} Qcow2DiscardRegion;
|
|
|
|
|
2015-02-11 04:28:50 +08:00
|
|
|
typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array,
|
|
|
|
uint64_t index);
|
|
|
|
typedef void Qcow2SetRefcountFunc(void *refcount_array,
|
|
|
|
uint64_t index, uint64_t value);
|
|
|
|
|
2009-05-28 22:07:04 +08:00
|
|
|
typedef struct BDRVQcowState {
|
|
|
|
int cluster_bits;
|
|
|
|
int cluster_size;
|
|
|
|
int cluster_sectors;
|
|
|
|
int l2_bits;
|
|
|
|
int l2_size;
|
|
|
|
int l1_size;
|
|
|
|
int l1_vm_state_index;
|
2014-10-22 20:09:28 +08:00
|
|
|
int refcount_block_bits;
|
|
|
|
int refcount_block_size;
|
2009-05-28 22:07:04 +08:00
|
|
|
int csize_shift;
|
|
|
|
int csize_mask;
|
|
|
|
uint64_t cluster_offset_mask;
|
|
|
|
uint64_t l1_table_offset;
|
|
|
|
uint64_t *l1_table;
|
2011-01-11 00:17:28 +08:00
|
|
|
|
|
|
|
Qcow2Cache* l2_table_cache;
|
|
|
|
Qcow2Cache* refcount_block_cache;
|
2015-08-04 20:14:40 +08:00
|
|
|
QEMUTimer *cache_clean_timer;
|
|
|
|
unsigned cache_clean_interval;
|
2011-01-11 00:17:28 +08:00
|
|
|
|
2009-05-28 22:07:04 +08:00
|
|
|
uint8_t *cluster_cache;
|
|
|
|
uint8_t *cluster_data;
|
|
|
|
uint64_t cluster_cache_offset;
|
2009-09-12 15:36:22 +08:00
|
|
|
QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
|
2009-05-28 22:07:04 +08:00
|
|
|
|
|
|
|
uint64_t *refcount_table;
|
|
|
|
uint64_t refcount_table_offset;
|
|
|
|
uint32_t refcount_table_size;
|
2014-03-26 20:05:51 +08:00
|
|
|
uint64_t free_cluster_index;
|
|
|
|
uint64_t free_byte_offset;
|
2009-05-28 22:07:04 +08:00
|
|
|
|
2011-06-30 23:42:09 +08:00
|
|
|
CoMutex lock;
|
|
|
|
|
2015-07-02 01:10:37 +08:00
|
|
|
QCryptoCipher *cipher; /* current cipher, NULL if no key yet */
|
2009-05-28 22:07:04 +08:00
|
|
|
uint32_t crypt_method_header;
|
|
|
|
uint64_t snapshots_offset;
|
|
|
|
int snapshots_size;
|
2014-03-26 20:05:45 +08:00
|
|
|
unsigned int nb_snapshots;
|
2009-05-28 22:07:04 +08:00
|
|
|
QCowSnapshot *snapshots;
|
2011-11-15 05:09:46 +08:00
|
|
|
|
|
|
|
int flags;
|
2011-12-15 19:20:58 +08:00
|
|
|
int qcow_version;
|
2013-03-15 17:35:08 +08:00
|
|
|
bool use_lazy_refcounts;
|
2013-09-03 16:09:53 +08:00
|
|
|
int refcount_order;
|
2015-02-11 04:28:43 +08:00
|
|
|
int refcount_bits;
|
|
|
|
uint64_t refcount_max;
|
2011-12-15 19:20:58 +08:00
|
|
|
|
2015-02-11 04:28:50 +08:00
|
|
|
Qcow2GetRefcountFunc *get_refcount;
|
|
|
|
Qcow2SetRefcountFunc *set_refcount;
|
|
|
|
|
2013-06-19 19:44:19 +08:00
|
|
|
bool discard_passthrough[QCOW2_DISCARD_MAX];
|
|
|
|
|
2013-10-10 17:09:24 +08:00
|
|
|
int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
|
2014-09-05 22:07:16 +08:00
|
|
|
bool signaled_corruption;
|
2013-10-10 17:09:24 +08:00
|
|
|
|
2011-12-15 19:20:58 +08:00
|
|
|
uint64_t incompatible_features;
|
|
|
|
uint64_t compatible_features;
|
|
|
|
uint64_t autoclear_features;
|
|
|
|
|
|
|
|
size_t unknown_header_fields_size;
|
|
|
|
void* unknown_header_fields;
|
2012-02-02 21:52:08 +08:00
|
|
|
QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
|
2013-06-19 19:44:20 +08:00
|
|
|
QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
|
|
|
|
bool cache_discards;
|
2015-04-07 21:03:16 +08:00
|
|
|
|
|
|
|
/* Backing file path and format as stored in the image (this is not the
|
|
|
|
* effective path/format, which may be the result of a runtime option
|
|
|
|
* override) */
|
|
|
|
char *image_backing_file;
|
|
|
|
char *image_backing_format;
|
2009-05-28 22:07:04 +08:00
|
|
|
} BDRVQcowState;
|
|
|
|
|
2009-08-31 22:48:49 +08:00
|
|
|
struct QCowAIOCB;
|
|
|
|
|
2012-12-08 01:08:43 +08:00
|
|
|
typedef struct Qcow2COWRegion {
|
|
|
|
/**
|
|
|
|
* Offset of the COW region in bytes from the start of the first cluster
|
|
|
|
* touched by the request.
|
|
|
|
*/
|
|
|
|
uint64_t offset;
|
|
|
|
|
|
|
|
/** Number of sectors to copy */
|
|
|
|
int nb_sectors;
|
|
|
|
} Qcow2COWRegion;
|
|
|
|
|
2012-12-08 01:08:46 +08:00
|
|
|
/**
|
|
|
|
* Describes an in-flight (part of a) write request that writes to clusters
|
|
|
|
* that are not referenced in their L2 table yet.
|
|
|
|
*/
|
2009-05-28 22:07:05 +08:00
|
|
|
typedef struct QCowL2Meta
|
|
|
|
{
|
2012-12-08 01:08:42 +08:00
|
|
|
/** Guest offset of the first newly allocated cluster */
|
2009-05-28 22:07:05 +08:00
|
|
|
uint64_t offset;
|
2012-12-08 01:08:42 +08:00
|
|
|
|
|
|
|
/** Host offset of the first newly allocated cluster */
|
2012-03-02 21:10:54 +08:00
|
|
|
uint64_t alloc_offset;
|
2012-12-08 01:08:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of sectors from the start of the first allocated cluster to
|
|
|
|
* the end of the (possibly shortened) request
|
|
|
|
*/
|
2009-05-28 22:07:05 +08:00
|
|
|
int nb_available;
|
2012-12-08 01:08:42 +08:00
|
|
|
|
|
|
|
/** Number of newly allocated clusters */
|
2009-05-28 22:07:05 +08:00
|
|
|
int nb_clusters;
|
2012-12-08 01:08:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Requests that overlap with this allocation and wait to be restarted
|
|
|
|
* when the allocating request has completed.
|
|
|
|
*/
|
2011-06-30 23:42:09 +08:00
|
|
|
CoQueue dependent_requests;
|
2009-08-31 22:48:49 +08:00
|
|
|
|
2012-12-08 01:08:43 +08:00
|
|
|
/**
|
|
|
|
* The COW Region between the start of the first allocated cluster and the
|
|
|
|
* area the guest actually writes to.
|
|
|
|
*/
|
|
|
|
Qcow2COWRegion cow_start;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The COW Region between the area the guest actually writes to and the
|
|
|
|
* end of the last allocated cluster.
|
|
|
|
*/
|
|
|
|
Qcow2COWRegion cow_end;
|
|
|
|
|
2013-03-27 00:50:11 +08:00
|
|
|
/** Pointer to next L2Meta of the same write request */
|
|
|
|
struct QCowL2Meta *next;
|
|
|
|
|
2009-09-12 15:36:22 +08:00
|
|
|
QLIST_ENTRY(QCowL2Meta) next_in_flight;
|
2009-05-28 22:07:05 +08:00
|
|
|
} QCowL2Meta;
|
|
|
|
|
2012-03-15 02:15:03 +08:00
|
|
|
enum {
|
|
|
|
QCOW2_CLUSTER_UNALLOCATED,
|
|
|
|
QCOW2_CLUSTER_NORMAL,
|
|
|
|
QCOW2_CLUSTER_COMPRESSED,
|
2012-03-16 22:02:38 +08:00
|
|
|
QCOW2_CLUSTER_ZERO
|
2012-03-15 02:15:03 +08:00
|
|
|
};
|
|
|
|
|
2013-08-30 20:34:25 +08:00
|
|
|
typedef enum QCow2MetadataOverlap {
|
|
|
|
QCOW2_OL_MAIN_HEADER_BITNR = 0,
|
|
|
|
QCOW2_OL_ACTIVE_L1_BITNR = 1,
|
|
|
|
QCOW2_OL_ACTIVE_L2_BITNR = 2,
|
|
|
|
QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
|
|
|
|
QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
|
|
|
|
QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
|
|
|
|
QCOW2_OL_INACTIVE_L1_BITNR = 6,
|
|
|
|
QCOW2_OL_INACTIVE_L2_BITNR = 7,
|
|
|
|
|
|
|
|
QCOW2_OL_MAX_BITNR = 8,
|
|
|
|
|
|
|
|
QCOW2_OL_NONE = 0,
|
|
|
|
QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
|
|
|
|
QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
|
|
|
|
QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
|
|
|
|
QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
|
|
|
|
QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
|
|
|
|
QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
|
|
|
|
QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
|
|
|
|
/* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
|
|
|
|
* reads. */
|
|
|
|
QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
|
|
|
|
} QCow2MetadataOverlap;
|
|
|
|
|
2013-10-10 17:09:27 +08:00
|
|
|
/* Perform all overlap checks which can be done in constant time */
|
|
|
|
#define QCOW2_OL_CONSTANT \
|
|
|
|
(QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
|
|
|
|
QCOW2_OL_SNAPSHOT_TABLE)
|
|
|
|
|
2013-08-30 20:34:25 +08:00
|
|
|
/* Perform all overlap checks which don't require disk access */
|
|
|
|
#define QCOW2_OL_CACHED \
|
2013-10-10 17:09:27 +08:00
|
|
|
(QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
|
|
|
|
QCOW2_OL_INACTIVE_L1)
|
|
|
|
|
|
|
|
/* Perform all overlap checks */
|
|
|
|
#define QCOW2_OL_ALL \
|
|
|
|
(QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
|
2013-08-30 20:34:25 +08:00
|
|
|
|
2014-01-21 11:30:02 +08:00
|
|
|
#define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
|
|
|
|
#define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
|
2012-03-15 02:15:03 +08:00
|
|
|
#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
|
|
|
|
|
2014-01-21 11:30:02 +08:00
|
|
|
#define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
|
2012-03-16 21:09:08 +08:00
|
|
|
|
2013-03-27 00:50:02 +08:00
|
|
|
static inline int64_t start_of_cluster(BDRVQcowState *s, int64_t offset)
|
|
|
|
{
|
|
|
|
return offset & ~(s->cluster_size - 1);
|
|
|
|
}
|
|
|
|
|
2013-03-27 00:50:03 +08:00
|
|
|
static inline int64_t offset_into_cluster(BDRVQcowState *s, int64_t offset)
|
|
|
|
{
|
|
|
|
return offset & (s->cluster_size - 1);
|
|
|
|
}
|
|
|
|
|
2009-05-28 22:07:05 +08:00
|
|
|
static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
|
2009-05-28 22:07:04 +08:00
|
|
|
{
|
|
|
|
return (size + (s->cluster_size - 1)) >> s->cluster_bits;
|
|
|
|
}
|
|
|
|
|
2013-05-14 22:14:33 +08:00
|
|
|
static inline int64_t size_to_l1(BDRVQcowState *s, int64_t size)
|
2010-04-28 18:36:11 +08:00
|
|
|
{
|
|
|
|
int shift = s->cluster_bits + s->l2_bits;
|
|
|
|
return (size + (1ULL << shift) - 1) >> shift;
|
|
|
|
}
|
|
|
|
|
2013-03-27 00:49:56 +08:00
|
|
|
static inline int offset_to_l2_index(BDRVQcowState *s, int64_t offset)
|
|
|
|
{
|
|
|
|
return (offset >> s->cluster_bits) & (s->l2_size - 1);
|
|
|
|
}
|
|
|
|
|
2009-05-28 22:07:06 +08:00
|
|
|
static inline int64_t align_offset(int64_t offset, int n)
|
|
|
|
{
|
|
|
|
offset = (offset + n - 1) & ~(n - 1);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2013-09-06 18:20:08 +08:00
|
|
|
static inline int64_t qcow2_vm_state_offset(BDRVQcowState *s)
|
|
|
|
{
|
|
|
|
return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
|
|
|
|
}
|
|
|
|
|
2014-03-26 20:05:50 +08:00
|
|
|
static inline uint64_t qcow2_max_refcount_clusters(BDRVQcowState *s)
|
|
|
|
{
|
|
|
|
return QCOW_MAX_REFTABLE_SIZE >> s->cluster_bits;
|
|
|
|
}
|
|
|
|
|
2012-03-15 02:15:03 +08:00
|
|
|
static inline int qcow2_get_cluster_type(uint64_t l2_entry)
|
|
|
|
{
|
|
|
|
if (l2_entry & QCOW_OFLAG_COMPRESSED) {
|
|
|
|
return QCOW2_CLUSTER_COMPRESSED;
|
2012-03-16 22:02:38 +08:00
|
|
|
} else if (l2_entry & QCOW_OFLAG_ZERO) {
|
|
|
|
return QCOW2_CLUSTER_ZERO;
|
2012-03-15 02:15:03 +08:00
|
|
|
} else if (!(l2_entry & L2E_OFFSET_MASK)) {
|
|
|
|
return QCOW2_CLUSTER_UNALLOCATED;
|
|
|
|
} else {
|
|
|
|
return QCOW2_CLUSTER_NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:22 +08:00
|
|
|
/* Check whether refcounts are eager or lazy */
|
|
|
|
static inline bool qcow2_need_accurate_refcounts(BDRVQcowState *s)
|
|
|
|
{
|
|
|
|
return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
|
|
|
|
}
|
2009-05-28 22:07:06 +08:00
|
|
|
|
2013-03-27 00:49:58 +08:00
|
|
|
static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
|
|
|
|
{
|
|
|
|
return m->offset + m->cow_start.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
|
|
|
|
{
|
|
|
|
return m->offset + m->cow_end.offset
|
|
|
|
+ (m->cow_end.nb_sectors << BDRV_SECTOR_BITS);
|
|
|
|
}
|
|
|
|
|
2015-02-11 04:28:48 +08:00
|
|
|
static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2)
|
2015-02-11 04:28:47 +08:00
|
|
|
{
|
|
|
|
return r1 > r2 ? r1 - r2 : r2 - r1;
|
|
|
|
}
|
|
|
|
|
2009-05-28 22:07:04 +08:00
|
|
|
// FIXME Need qcow2_ prefix to global functions
|
|
|
|
|
|
|
|
/* qcow2.c functions */
|
2010-09-14 00:08:52 +08:00
|
|
|
int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
|
|
|
|
int64_t sector_num, int nb_sectors);
|
2012-12-08 01:08:47 +08:00
|
|
|
|
|
|
|
int qcow2_mark_dirty(BlockDriverState *bs);
|
2013-08-30 20:34:24 +08:00
|
|
|
int qcow2_mark_corrupt(BlockDriverState *bs);
|
|
|
|
int qcow2_mark_consistent(BlockDriverState *bs);
|
2012-02-02 19:32:31 +08:00
|
|
|
int qcow2_update_header(BlockDriverState *bs);
|
2009-05-28 22:07:04 +08:00
|
|
|
|
2014-09-05 22:07:16 +08:00
|
|
|
void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
|
|
|
|
int64_t size, const char *message_format, ...)
|
|
|
|
GCC_FMT_ATTR(5, 6);
|
|
|
|
|
2009-05-28 22:07:04 +08:00
|
|
|
/* qcow2-refcount.c functions */
|
2009-05-28 22:07:07 +08:00
|
|
|
int qcow2_refcount_init(BlockDriverState *bs);
|
|
|
|
void qcow2_refcount_close(BlockDriverState *bs);
|
2009-05-28 22:07:04 +08:00
|
|
|
|
2015-02-11 04:28:46 +08:00
|
|
|
int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
|
2015-02-11 04:28:48 +08:00
|
|
|
uint64_t *refcount);
|
2014-10-27 18:12:54 +08:00
|
|
|
|
2013-09-03 16:09:52 +08:00
|
|
|
int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
|
2015-02-11 04:28:48 +08:00
|
|
|
uint64_t addend, bool decrease,
|
2015-02-11 04:28:47 +08:00
|
|
|
enum qcow2_discard_type type);
|
2013-09-03 16:09:52 +08:00
|
|
|
|
2014-03-26 20:05:51 +08:00
|
|
|
int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
|
2012-03-03 02:35:58 +08:00
|
|
|
int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
|
|
|
|
int nb_clusters);
|
2009-05-28 22:07:07 +08:00
|
|
|
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
|
|
|
|
void qcow2_free_clusters(BlockDriverState *bs,
|
2013-06-19 19:44:18 +08:00
|
|
|
int64_t offset, int64_t size,
|
|
|
|
enum qcow2_discard_type type);
|
|
|
|
void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
|
|
|
|
int nb_clusters, enum qcow2_discard_type type);
|
2009-05-28 22:07:04 +08:00
|
|
|
|
2009-05-28 22:07:07 +08:00
|
|
|
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
|
|
|
|
int64_t l1_table_offset, int l1_size, int addend);
|
2009-05-28 22:07:04 +08:00
|
|
|
|
2012-05-12 00:18:36 +08:00
|
|
|
int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
|
|
|
|
BdrvCheckMode fix);
|
2009-05-28 22:07:04 +08:00
|
|
|
|
2013-06-19 19:44:20 +08:00
|
|
|
void qcow2_process_discards(BlockDriverState *bs, int ret);
|
|
|
|
|
2013-10-10 17:09:23 +08:00
|
|
|
int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
|
2013-08-30 20:34:25 +08:00
|
|
|
int64_t size);
|
2013-10-10 17:09:23 +08:00
|
|
|
int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
|
2013-08-30 20:34:25 +08:00
|
|
|
int64_t size);
|
|
|
|
|
2009-05-28 22:07:05 +08:00
|
|
|
/* qcow2-cluster.c functions */
|
2013-05-14 22:14:33 +08:00
|
|
|
int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
|
|
|
|
bool exact_size);
|
2013-08-30 20:34:28 +08:00
|
|
|
int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
|
2009-05-28 22:07:07 +08:00
|
|
|
void qcow2_l2_cache_reset(BlockDriverState *bs);
|
2010-04-14 20:17:38 +08:00
|
|
|
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
|
2015-07-02 01:10:37 +08:00
|
|
|
int qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
|
|
|
|
uint8_t *out_buf, const uint8_t *in_buf,
|
|
|
|
int nb_sectors, bool enc, Error **errp);
|
2009-05-28 22:07:05 +08:00
|
|
|
|
2010-05-21 23:59:36 +08:00
|
|
|
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
|
|
|
int *num, uint64_t *cluster_offset);
|
2010-02-02 22:20:57 +08:00
|
|
|
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
2014-01-26 11:12:37 +08:00
|
|
|
int *num, uint64_t *host_offset, QCowL2Meta **m);
|
2009-05-28 22:07:07 +08:00
|
|
|
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
|
2009-05-28 22:07:05 +08:00
|
|
|
uint64_t offset,
|
|
|
|
int compressed_size);
|
|
|
|
|
2010-01-20 22:03:01 +08:00
|
|
|
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
|
2011-01-26 23:56:48 +08:00
|
|
|
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
2014-10-24 21:57:30 +08:00
|
|
|
int nb_sectors, enum qcow2_discard_type type, bool full_discard);
|
2012-03-20 22:12:58 +08:00
|
|
|
int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
|
2009-05-28 22:07:05 +08:00
|
|
|
|
2014-10-27 18:12:53 +08:00
|
|
|
int qcow2_expand_zero_clusters(BlockDriverState *bs,
|
|
|
|
BlockDriverAmendStatusCB *status_cb);
|
2013-09-03 16:09:52 +08:00
|
|
|
|
2009-05-28 22:07:06 +08:00
|
|
|
/* qcow2-snapshot.c functions */
|
2009-05-28 22:07:07 +08:00
|
|
|
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
|
|
|
|
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
|
snapshot: distinguish id and name in snapshot delete
Snapshot creation actually already distinguish id and name since it take
a structured parameter *sn, but delete can't. Later an accurate delete
is needed in qmp_transaction abort and blockdev-snapshot-delete-sync,
so change its prototype. Also *errp is added to tip error, but return
value is kepted to let caller check what kind of error happens. Existing
caller for it are savevm, delvm and qemu-img, they are not impacted by
introducing a new function bdrv_snapshot_delete_by_id_or_name(), which
check the return value and do the operation again.
Before this patch:
For qcow2, it search id first then name to find the one to delete.
For rbd, it search name.
For sheepdog, it does nothing.
After this patch:
For qcow2, logic is the same by call it twice in caller.
For rbd, it always fails in delete with id, but still search for name
in second try, no change to user.
Some code for *errp is based on Pavel's patch.
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
2013-09-11 14:04:33 +08:00
|
|
|
int qcow2_snapshot_delete(BlockDriverState *bs,
|
|
|
|
const char *snapshot_id,
|
|
|
|
const char *name,
|
|
|
|
Error **errp);
|
2009-05-28 22:07:07 +08:00
|
|
|
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
|
2013-12-04 17:10:54 +08:00
|
|
|
int qcow2_snapshot_load_tmp(BlockDriverState *bs,
|
|
|
|
const char *snapshot_id,
|
|
|
|
const char *name,
|
|
|
|
Error **errp);
|
2009-05-28 22:07:06 +08:00
|
|
|
|
2009-05-28 22:07:07 +08:00
|
|
|
void qcow2_free_snapshots(BlockDriverState *bs);
|
|
|
|
int qcow2_read_snapshots(BlockDriverState *bs);
|
2009-05-28 22:07:06 +08:00
|
|
|
|
2011-01-11 00:15:10 +08:00
|
|
|
/* qcow2-cache.c functions */
|
2012-06-06 06:04:55 +08:00
|
|
|
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
|
2011-01-11 00:15:10 +08:00
|
|
|
int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
|
|
|
|
|
2015-05-11 20:54:53 +08:00
|
|
|
void qcow2_cache_entry_mark_dirty(BlockDriverState *bs, Qcow2Cache *c,
|
|
|
|
void *table);
|
2011-01-11 00:15:10 +08:00
|
|
|
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
|
|
|
|
int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
|
|
|
|
Qcow2Cache *dependency);
|
2011-01-14 22:55:38 +08:00
|
|
|
void qcow2_cache_depends_on_flush(Qcow2Cache *c);
|
2011-01-11 00:15:10 +08:00
|
|
|
|
2015-08-04 20:14:40 +08:00
|
|
|
void qcow2_cache_clean_unused(BlockDriverState *bs, Qcow2Cache *c);
|
2013-09-03 16:09:51 +08:00
|
|
|
int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
|
|
|
|
|
2011-01-11 00:15:10 +08:00
|
|
|
int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
|
|
|
void **table);
|
|
|
|
int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
|
|
|
|
void **table);
|
2015-05-11 20:54:58 +08:00
|
|
|
void qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
|
2011-01-11 00:15:10 +08:00
|
|
|
|
2009-05-28 22:07:04 +08:00
|
|
|
#endif
|