2009-05-28 18:07:04 +04: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
|
|
|
|
|
|
|
|
#include "aes.h"
|
2011-06-30 19:42:09 +04:00
|
|
|
#include "qemu-coroutine.h"
|
2009-05-28 18:07:04 +04:00
|
|
|
|
2009-06-24 04:17:30 +04:00
|
|
|
//#define DEBUG_ALLOC
|
|
|
|
//#define DEBUG_ALLOC2
|
|
|
|
//#define DEBUG_EXT
|
|
|
|
|
2009-05-28 18:07:04 +04: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
|
|
|
|
|
|
|
|
/* indicate that the refcount of the referenced cluster is exactly one. */
|
|
|
|
#define QCOW_OFLAG_COPIED (1LL << 63)
|
|
|
|
/* indicate that the cluster is compressed (they never have the copied flag) */
|
|
|
|
#define QCOW_OFLAG_COMPRESSED (1LL << 62)
|
2012-03-16 18:02:38 +04:00
|
|
|
/* The cluster reads as all zeros */
|
|
|
|
#define QCOW_OFLAG_ZERO (1LL << 0)
|
2009-05-28 18:07:04 +04:00
|
|
|
|
|
|
|
#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
|
|
|
|
|
|
|
|
#define MIN_CLUSTER_BITS 9
|
2009-09-15 14:30:43 +04:00
|
|
|
#define MAX_CLUSTER_BITS 21
|
2009-05-28 18:07:04 +04:00
|
|
|
|
|
|
|
#define L2_CACHE_SIZE 16
|
|
|
|
|
2011-01-10 19:17:28 +03:00
|
|
|
/* Must be at least 4 to cover all cases of refcount table growth */
|
|
|
|
#define REFCOUNT_CACHE_SIZE 4
|
|
|
|
|
2011-05-31 17:01:46 +04:00
|
|
|
#define DEFAULT_CLUSTER_SIZE 65536
|
|
|
|
|
2009-05-28 18:07:04 +04: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 15:20:58 +04: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;
|
2009-05-28 18:07:04 +04:00
|
|
|
} QCowHeader;
|
|
|
|
|
|
|
|
typedef struct QCowSnapshot {
|
|
|
|
uint64_t l1_table_offset;
|
|
|
|
uint32_t l1_size;
|
|
|
|
char *id_str;
|
|
|
|
char *name;
|
2012-04-11 18:33:50 +04:00
|
|
|
uint64_t disk_size;
|
2011-11-16 14:35:54 +04:00
|
|
|
uint64_t vm_state_size;
|
2009-05-28 18:07:04 +04:00
|
|
|
uint32_t date_sec;
|
|
|
|
uint32_t date_nsec;
|
|
|
|
uint64_t vm_clock_nsec;
|
|
|
|
} QCowSnapshot;
|
|
|
|
|
2011-01-10 19:15:10 +03:00
|
|
|
struct Qcow2Cache;
|
|
|
|
typedef struct Qcow2Cache Qcow2Cache;
|
|
|
|
|
2012-02-02 17:52:08 +04:00
|
|
|
typedef struct Qcow2UnknownHeaderExtension {
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t len;
|
|
|
|
QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
|
|
|
|
uint8_t data[];
|
|
|
|
} Qcow2UnknownHeaderExtension;
|
|
|
|
|
2012-04-12 17:20:27 +04:00
|
|
|
enum {
|
|
|
|
QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
|
|
|
|
QCOW2_FEAT_TYPE_COMPATIBLE = 1,
|
|
|
|
QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
|
|
|
|
};
|
|
|
|
|
2012-07-27 12:05:19 +04:00
|
|
|
/* Incompatible feature bits */
|
|
|
|
enum {
|
|
|
|
QCOW2_INCOMPAT_DIRTY_BITNR = 0,
|
|
|
|
QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
|
|
|
|
|
|
|
|
QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY,
|
|
|
|
};
|
|
|
|
|
2012-07-27 12:05:22 +04: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,
|
|
|
|
};
|
|
|
|
|
2012-04-12 17:20:27 +04:00
|
|
|
typedef struct Qcow2Feature {
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t bit;
|
|
|
|
char name[46];
|
|
|
|
} QEMU_PACKED Qcow2Feature;
|
|
|
|
|
2009-05-28 18:07:04 +04: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;
|
|
|
|
int csize_shift;
|
|
|
|
int csize_mask;
|
|
|
|
uint64_t cluster_offset_mask;
|
|
|
|
uint64_t l1_table_offset;
|
|
|
|
uint64_t *l1_table;
|
2011-01-10 19:17:28 +03:00
|
|
|
|
|
|
|
Qcow2Cache* l2_table_cache;
|
|
|
|
Qcow2Cache* refcount_block_cache;
|
|
|
|
|
2009-05-28 18:07:04 +04:00
|
|
|
uint8_t *cluster_cache;
|
|
|
|
uint8_t *cluster_data;
|
|
|
|
uint64_t cluster_cache_offset;
|
2009-09-12 11:36:22 +04:00
|
|
|
QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
|
2009-05-28 18:07:04 +04:00
|
|
|
|
|
|
|
uint64_t *refcount_table;
|
|
|
|
uint64_t refcount_table_offset;
|
|
|
|
uint32_t refcount_table_size;
|
|
|
|
int64_t free_cluster_index;
|
|
|
|
int64_t free_byte_offset;
|
|
|
|
|
2011-06-30 19:42:09 +04:00
|
|
|
CoMutex lock;
|
|
|
|
|
2009-05-28 18:07:04 +04:00
|
|
|
uint32_t crypt_method; /* current crypt method, 0 if no key yet */
|
|
|
|
uint32_t crypt_method_header;
|
|
|
|
AES_KEY aes_encrypt_key;
|
|
|
|
AES_KEY aes_decrypt_key;
|
|
|
|
uint64_t snapshots_offset;
|
|
|
|
int snapshots_size;
|
|
|
|
int nb_snapshots;
|
|
|
|
QCowSnapshot *snapshots;
|
2011-11-15 01:09:46 +04:00
|
|
|
|
|
|
|
int flags;
|
2011-12-15 15:20:58 +04:00
|
|
|
int qcow_version;
|
|
|
|
|
|
|
|
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 17:52:08 +04:00
|
|
|
QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
|
2009-05-28 18:07:04 +04:00
|
|
|
} BDRVQcowState;
|
|
|
|
|
|
|
|
/* XXX: use std qcow open function ? */
|
|
|
|
typedef struct QCowCreateState {
|
|
|
|
int cluster_size;
|
|
|
|
int cluster_bits;
|
|
|
|
uint16_t *refcount_block;
|
|
|
|
uint64_t *refcount_table;
|
|
|
|
int64_t l1_table_offset;
|
|
|
|
int64_t refcount_table_offset;
|
|
|
|
int64_t refcount_block_offset;
|
|
|
|
} QCowCreateState;
|
|
|
|
|
2009-08-31 18:48:49 +04:00
|
|
|
struct QCowAIOCB;
|
|
|
|
|
2009-05-28 18:07:05 +04:00
|
|
|
/* XXX This could be private for qcow2-cluster.c */
|
|
|
|
typedef struct QCowL2Meta
|
|
|
|
{
|
|
|
|
uint64_t offset;
|
2010-01-20 17:03:01 +03:00
|
|
|
uint64_t cluster_offset;
|
2012-03-02 17:10:54 +04:00
|
|
|
uint64_t alloc_offset;
|
2009-05-28 18:07:05 +04:00
|
|
|
int n_start;
|
|
|
|
int nb_available;
|
|
|
|
int nb_clusters;
|
2011-06-30 19:42:09 +04:00
|
|
|
CoQueue dependent_requests;
|
2009-08-31 18:48:49 +04:00
|
|
|
|
2009-09-12 11:36:22 +04:00
|
|
|
QLIST_ENTRY(QCowL2Meta) next_in_flight;
|
2009-05-28 18:07:05 +04:00
|
|
|
} QCowL2Meta;
|
|
|
|
|
2012-03-14 22:15:03 +04:00
|
|
|
enum {
|
|
|
|
QCOW2_CLUSTER_UNALLOCATED,
|
|
|
|
QCOW2_CLUSTER_NORMAL,
|
|
|
|
QCOW2_CLUSTER_COMPRESSED,
|
2012-03-16 18:02:38 +04:00
|
|
|
QCOW2_CLUSTER_ZERO
|
2012-03-14 22:15:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define L1E_OFFSET_MASK 0x00ffffffffffff00ULL
|
|
|
|
#define L2E_OFFSET_MASK 0x00ffffffffffff00ULL
|
|
|
|
#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
|
|
|
|
|
2012-03-16 17:09:08 +04:00
|
|
|
#define REFT_OFFSET_MASK 0xffffffffffffff00ULL
|
|
|
|
|
2009-05-28 18:07:05 +04:00
|
|
|
static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
|
2009-05-28 18:07:04 +04:00
|
|
|
{
|
|
|
|
return (size + (s->cluster_size - 1)) >> s->cluster_bits;
|
|
|
|
}
|
|
|
|
|
2010-04-28 14:36:11 +04:00
|
|
|
static inline int size_to_l1(BDRVQcowState *s, int64_t size)
|
|
|
|
{
|
|
|
|
int shift = s->cluster_bits + s->l2_bits;
|
|
|
|
return (size + (1ULL << shift) - 1) >> shift;
|
|
|
|
}
|
|
|
|
|
2009-05-28 18:07:06 +04:00
|
|
|
static inline int64_t align_offset(int64_t offset, int n)
|
|
|
|
{
|
|
|
|
offset = (offset + n - 1) & ~(n - 1);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2012-03-14 22:15:03 +04: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 18:02:38 +04:00
|
|
|
} else if (l2_entry & QCOW_OFLAG_ZERO) {
|
|
|
|
return QCOW2_CLUSTER_ZERO;
|
2012-03-14 22:15:03 +04:00
|
|
|
} else if (!(l2_entry & L2E_OFFSET_MASK)) {
|
|
|
|
return QCOW2_CLUSTER_UNALLOCATED;
|
|
|
|
} else {
|
|
|
|
return QCOW2_CLUSTER_NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-27 12:05:22 +04: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 18:07:06 +04:00
|
|
|
|
2009-05-28 18:07:04 +04:00
|
|
|
// FIXME Need qcow2_ prefix to global functions
|
|
|
|
|
|
|
|
/* qcow2.c functions */
|
2010-09-13 20:08:52 +04:00
|
|
|
int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
|
|
|
|
int64_t sector_num, int nb_sectors);
|
2012-02-02 15:32:31 +04:00
|
|
|
int qcow2_update_header(BlockDriverState *bs);
|
2009-05-28 18:07:04 +04:00
|
|
|
|
|
|
|
/* qcow2-refcount.c functions */
|
2009-05-28 18:07:07 +04:00
|
|
|
int qcow2_refcount_init(BlockDriverState *bs);
|
|
|
|
void qcow2_refcount_close(BlockDriverState *bs);
|
2009-05-28 18:07:04 +04:00
|
|
|
|
2009-05-28 18:07:07 +04:00
|
|
|
int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
|
2012-03-02 22:35:58 +04:00
|
|
|
int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
|
|
|
|
int nb_clusters);
|
2009-05-28 18:07:07 +04:00
|
|
|
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
|
|
|
|
void qcow2_free_clusters(BlockDriverState *bs,
|
2009-05-28 18:07:05 +04:00
|
|
|
int64_t offset, int64_t size);
|
2009-05-28 18:07:07 +04:00
|
|
|
void qcow2_free_any_clusters(BlockDriverState *bs,
|
2009-05-28 18:07:05 +04:00
|
|
|
uint64_t cluster_offset, int nb_clusters);
|
2009-05-28 18:07:04 +04:00
|
|
|
|
2009-05-28 18:07:07 +04:00
|
|
|
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
|
|
|
|
int64_t l1_table_offset, int l1_size, int addend);
|
2009-05-28 18:07:04 +04:00
|
|
|
|
2012-05-11 20:18:36 +04:00
|
|
|
int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
|
|
|
|
BdrvCheckMode fix);
|
2009-05-28 18:07:04 +04:00
|
|
|
|
2009-05-28 18:07:05 +04:00
|
|
|
/* qcow2-cluster.c functions */
|
2010-10-18 19:53:53 +04:00
|
|
|
int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
|
2009-05-28 18:07:07 +04:00
|
|
|
void qcow2_l2_cache_reset(BlockDriverState *bs);
|
2010-04-14 16:17:38 +04:00
|
|
|
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
|
2009-05-28 18:07:07 +04:00
|
|
|
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
|
2009-05-28 18:07:05 +04:00
|
|
|
uint8_t *out_buf, const uint8_t *in_buf,
|
|
|
|
int nb_sectors, int enc,
|
|
|
|
const AES_KEY *key);
|
|
|
|
|
2010-05-21 19:59:36 +04:00
|
|
|
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
|
|
|
int *num, uint64_t *cluster_offset);
|
2010-02-02 17:20:57 +03:00
|
|
|
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
|
|
|
|
int n_start, int n_end, int *num, QCowL2Meta *m);
|
2009-05-28 18:07:07 +04:00
|
|
|
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
|
2009-05-28 18:07:05 +04:00
|
|
|
uint64_t offset,
|
|
|
|
int compressed_size);
|
|
|
|
|
2010-01-20 17:03:01 +03:00
|
|
|
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
|
2011-01-26 18:56:48 +03:00
|
|
|
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
|
|
|
|
int nb_sectors);
|
2012-03-20 18:12:58 +04:00
|
|
|
int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
|
2009-05-28 18:07:05 +04:00
|
|
|
|
2009-05-28 18:07:06 +04:00
|
|
|
/* qcow2-snapshot.c functions */
|
2009-05-28 18:07:07 +04:00
|
|
|
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
|
|
|
|
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
|
|
|
|
int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
|
|
|
|
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
|
2010-09-22 06:58:41 +04:00
|
|
|
int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
|
2009-05-28 18:07:06 +04:00
|
|
|
|
2009-05-28 18:07:07 +04:00
|
|
|
void qcow2_free_snapshots(BlockDriverState *bs);
|
|
|
|
int qcow2_read_snapshots(BlockDriverState *bs);
|
2009-05-28 18:07:06 +04:00
|
|
|
|
2011-01-10 19:15:10 +03:00
|
|
|
/* qcow2-cache.c functions */
|
2012-06-06 02:04:55 +04:00
|
|
|
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
|
2011-01-10 19:15:10 +03:00
|
|
|
int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
|
|
|
|
|
|
|
|
void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
|
|
|
|
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
|
|
|
|
int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
|
|
|
|
Qcow2Cache *dependency);
|
2011-01-14 17:55:38 +03:00
|
|
|
void qcow2_cache_depends_on_flush(Qcow2Cache *c);
|
2011-01-10 19:15:10 +03: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);
|
|
|
|
int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
|
|
|
|
|
2009-05-28 18:07:04 +04:00
|
|
|
#endif
|