1f7d0fe02SKevin Wolf /* 2f7d0fe02SKevin Wolf * Block driver for the QCOW version 2 format 3f7d0fe02SKevin Wolf * 4f7d0fe02SKevin Wolf * Copyright (c) 2004-2006 Fabrice Bellard 5f7d0fe02SKevin Wolf * 6f7d0fe02SKevin Wolf * Permission is hereby granted, free of charge, to any person obtaining a copy 7f7d0fe02SKevin Wolf * of this software and associated documentation files (the "Software"), to deal 8f7d0fe02SKevin Wolf * in the Software without restriction, including without limitation the rights 9f7d0fe02SKevin Wolf * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10f7d0fe02SKevin Wolf * copies of the Software, and to permit persons to whom the Software is 11f7d0fe02SKevin Wolf * furnished to do so, subject to the following conditions: 12f7d0fe02SKevin Wolf * 13f7d0fe02SKevin Wolf * The above copyright notice and this permission notice shall be included in 14f7d0fe02SKevin Wolf * all copies or substantial portions of the Software. 15f7d0fe02SKevin Wolf * 16f7d0fe02SKevin Wolf * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17f7d0fe02SKevin Wolf * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18f7d0fe02SKevin Wolf * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19f7d0fe02SKevin Wolf * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20f7d0fe02SKevin Wolf * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21f7d0fe02SKevin Wolf * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22f7d0fe02SKevin Wolf * THE SOFTWARE. 23f7d0fe02SKevin Wolf */ 24f7d0fe02SKevin Wolf 25f7d0fe02SKevin Wolf #ifndef BLOCK_QCOW2_H 26f7d0fe02SKevin Wolf #define BLOCK_QCOW2_H 27f7d0fe02SKevin Wolf 28f7d0fe02SKevin Wolf #include "aes.h" 2968d100e9SKevin Wolf #include "qemu-coroutine.h" 30f7d0fe02SKevin Wolf 3114899cdfSFilip Navara //#define DEBUG_ALLOC 3214899cdfSFilip Navara //#define DEBUG_ALLOC2 3314899cdfSFilip Navara //#define DEBUG_EXT 3414899cdfSFilip Navara 35f7d0fe02SKevin Wolf #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) 36f7d0fe02SKevin Wolf #define QCOW_VERSION 2 37f7d0fe02SKevin Wolf 38f7d0fe02SKevin Wolf #define QCOW_CRYPT_NONE 0 39f7d0fe02SKevin Wolf #define QCOW_CRYPT_AES 1 40f7d0fe02SKevin Wolf 41f7d0fe02SKevin Wolf #define QCOW_MAX_CRYPT_CLUSTERS 32 42f7d0fe02SKevin Wolf 43f7d0fe02SKevin Wolf /* indicate that the refcount of the referenced cluster is exactly one. */ 44f7d0fe02SKevin Wolf #define QCOW_OFLAG_COPIED (1LL << 63) 45f7d0fe02SKevin Wolf /* indicate that the cluster is compressed (they never have the copied flag) */ 46f7d0fe02SKevin Wolf #define QCOW_OFLAG_COMPRESSED (1LL << 62) 47f7d0fe02SKevin Wolf 48f7d0fe02SKevin Wolf #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */ 49f7d0fe02SKevin Wolf 50f7d0fe02SKevin Wolf #define MIN_CLUSTER_BITS 9 5180ee15a6SKevin Wolf #define MAX_CLUSTER_BITS 21 52f7d0fe02SKevin Wolf 53f7d0fe02SKevin Wolf #define L2_CACHE_SIZE 16 54f7d0fe02SKevin Wolf 5529c1a730SKevin Wolf /* Must be at least 4 to cover all cases of refcount table growth */ 5629c1a730SKevin Wolf #define REFCOUNT_CACHE_SIZE 4 5729c1a730SKevin Wolf 5899cce9faSKevin Wolf #define DEFAULT_CLUSTER_SIZE 65536 5999cce9faSKevin Wolf 60f7d0fe02SKevin Wolf typedef struct QCowHeader { 61f7d0fe02SKevin Wolf uint32_t magic; 62f7d0fe02SKevin Wolf uint32_t version; 63f7d0fe02SKevin Wolf uint64_t backing_file_offset; 64f7d0fe02SKevin Wolf uint32_t backing_file_size; 65f7d0fe02SKevin Wolf uint32_t cluster_bits; 66f7d0fe02SKevin Wolf uint64_t size; /* in bytes */ 67f7d0fe02SKevin Wolf uint32_t crypt_method; 68f7d0fe02SKevin Wolf uint32_t l1_size; /* XXX: save number of clusters instead ? */ 69f7d0fe02SKevin Wolf uint64_t l1_table_offset; 70f7d0fe02SKevin Wolf uint64_t refcount_table_offset; 71f7d0fe02SKevin Wolf uint32_t refcount_table_clusters; 72f7d0fe02SKevin Wolf uint32_t nb_snapshots; 73f7d0fe02SKevin Wolf uint64_t snapshots_offset; 74f7d0fe02SKevin Wolf } QCowHeader; 75f7d0fe02SKevin Wolf 76f7d0fe02SKevin Wolf typedef struct QCowSnapshot { 77f7d0fe02SKevin Wolf uint64_t l1_table_offset; 78f7d0fe02SKevin Wolf uint32_t l1_size; 79f7d0fe02SKevin Wolf char *id_str; 80f7d0fe02SKevin Wolf char *name; 81c2c9a466SKevin Wolf uint64_t vm_state_size; 82f7d0fe02SKevin Wolf uint32_t date_sec; 83f7d0fe02SKevin Wolf uint32_t date_nsec; 84f7d0fe02SKevin Wolf uint64_t vm_clock_nsec; 85f7d0fe02SKevin Wolf } QCowSnapshot; 86f7d0fe02SKevin Wolf 8749381094SKevin Wolf struct Qcow2Cache; 8849381094SKevin Wolf typedef struct Qcow2Cache Qcow2Cache; 8949381094SKevin Wolf 9075bab85cSKevin Wolf typedef struct Qcow2UnknownHeaderExtension { 9175bab85cSKevin Wolf uint32_t magic; 9275bab85cSKevin Wolf uint32_t len; 9375bab85cSKevin Wolf QLIST_ENTRY(Qcow2UnknownHeaderExtension) next; 9475bab85cSKevin Wolf uint8_t data[]; 9575bab85cSKevin Wolf } Qcow2UnknownHeaderExtension; 9675bab85cSKevin Wolf 97f7d0fe02SKevin Wolf typedef struct BDRVQcowState { 98f7d0fe02SKevin Wolf int cluster_bits; 99f7d0fe02SKevin Wolf int cluster_size; 100f7d0fe02SKevin Wolf int cluster_sectors; 101f7d0fe02SKevin Wolf int l2_bits; 102f7d0fe02SKevin Wolf int l2_size; 103f7d0fe02SKevin Wolf int l1_size; 104f7d0fe02SKevin Wolf int l1_vm_state_index; 105f7d0fe02SKevin Wolf int csize_shift; 106f7d0fe02SKevin Wolf int csize_mask; 107f7d0fe02SKevin Wolf uint64_t cluster_offset_mask; 108f7d0fe02SKevin Wolf uint64_t l1_table_offset; 109f7d0fe02SKevin Wolf uint64_t *l1_table; 11029c1a730SKevin Wolf 11129c1a730SKevin Wolf Qcow2Cache* l2_table_cache; 11229c1a730SKevin Wolf Qcow2Cache* refcount_block_cache; 11329c1a730SKevin Wolf 114f7d0fe02SKevin Wolf uint8_t *cluster_cache; 115f7d0fe02SKevin Wolf uint8_t *cluster_data; 116f7d0fe02SKevin Wolf uint64_t cluster_cache_offset; 11772cf2d4fSBlue Swirl QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs; 118f7d0fe02SKevin Wolf 119f7d0fe02SKevin Wolf uint64_t *refcount_table; 120f7d0fe02SKevin Wolf uint64_t refcount_table_offset; 121f7d0fe02SKevin Wolf uint32_t refcount_table_size; 122f7d0fe02SKevin Wolf int64_t free_cluster_index; 123f7d0fe02SKevin Wolf int64_t free_byte_offset; 124f7d0fe02SKevin Wolf 12568d100e9SKevin Wolf CoMutex lock; 12668d100e9SKevin Wolf 127f7d0fe02SKevin Wolf uint32_t crypt_method; /* current crypt method, 0 if no key yet */ 128f7d0fe02SKevin Wolf uint32_t crypt_method_header; 129f7d0fe02SKevin Wolf AES_KEY aes_encrypt_key; 130f7d0fe02SKevin Wolf AES_KEY aes_decrypt_key; 131f7d0fe02SKevin Wolf uint64_t snapshots_offset; 132f7d0fe02SKevin Wolf int snapshots_size; 133f7d0fe02SKevin Wolf int nb_snapshots; 134f7d0fe02SKevin Wolf QCowSnapshot *snapshots; 13506d9260fSAnthony Liguori 13606d9260fSAnthony Liguori int flags; 13775bab85cSKevin Wolf QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext; 138f7d0fe02SKevin Wolf } BDRVQcowState; 139f7d0fe02SKevin Wolf 140f7d0fe02SKevin Wolf /* XXX: use std qcow open function ? */ 141f7d0fe02SKevin Wolf typedef struct QCowCreateState { 142f7d0fe02SKevin Wolf int cluster_size; 143f7d0fe02SKevin Wolf int cluster_bits; 144f7d0fe02SKevin Wolf uint16_t *refcount_block; 145f7d0fe02SKevin Wolf uint64_t *refcount_table; 146f7d0fe02SKevin Wolf int64_t l1_table_offset; 147f7d0fe02SKevin Wolf int64_t refcount_table_offset; 148f7d0fe02SKevin Wolf int64_t refcount_block_offset; 149f7d0fe02SKevin Wolf } QCowCreateState; 150f7d0fe02SKevin Wolf 151f214978aSKevin Wolf struct QCowAIOCB; 152f214978aSKevin Wolf 15345aba42fSKevin Wolf /* XXX This could be private for qcow2-cluster.c */ 15445aba42fSKevin Wolf typedef struct QCowL2Meta 15545aba42fSKevin Wolf { 15645aba42fSKevin Wolf uint64_t offset; 157148da7eaSKevin Wolf uint64_t cluster_offset; 158*250196f1SKevin Wolf uint64_t alloc_offset; 15945aba42fSKevin Wolf int n_start; 16045aba42fSKevin Wolf int nb_available; 16145aba42fSKevin Wolf int nb_clusters; 16268d100e9SKevin Wolf CoQueue dependent_requests; 163f214978aSKevin Wolf 16472cf2d4fSBlue Swirl QLIST_ENTRY(QCowL2Meta) next_in_flight; 16545aba42fSKevin Wolf } QCowL2Meta; 16645aba42fSKevin Wolf 16745aba42fSKevin Wolf static inline int size_to_clusters(BDRVQcowState *s, int64_t size) 168f7d0fe02SKevin Wolf { 169f7d0fe02SKevin Wolf return (size + (s->cluster_size - 1)) >> s->cluster_bits; 170f7d0fe02SKevin Wolf } 171f7d0fe02SKevin Wolf 172419b19d9SStefan Hajnoczi static inline int size_to_l1(BDRVQcowState *s, int64_t size) 173419b19d9SStefan Hajnoczi { 174419b19d9SStefan Hajnoczi int shift = s->cluster_bits + s->l2_bits; 175419b19d9SStefan Hajnoczi return (size + (1ULL << shift) - 1) >> shift; 176419b19d9SStefan Hajnoczi } 177419b19d9SStefan Hajnoczi 178c142442bSKevin Wolf static inline int64_t align_offset(int64_t offset, int n) 179c142442bSKevin Wolf { 180c142442bSKevin Wolf offset = (offset + n - 1) & ~(n - 1); 181c142442bSKevin Wolf return offset; 182c142442bSKevin Wolf } 183c142442bSKevin Wolf 184c142442bSKevin Wolf 185f7d0fe02SKevin Wolf // FIXME Need qcow2_ prefix to global functions 186f7d0fe02SKevin Wolf 187f7d0fe02SKevin Wolf /* qcow2.c functions */ 188bd28f835SKevin Wolf int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, 189bd28f835SKevin Wolf int64_t sector_num, int nb_sectors); 190e24e49e6SKevin Wolf int qcow2_update_header(BlockDriverState *bs); 191f7d0fe02SKevin Wolf 192f7d0fe02SKevin Wolf /* qcow2-refcount.c functions */ 193ed6ccf0fSKevin Wolf int qcow2_refcount_init(BlockDriverState *bs); 194ed6ccf0fSKevin Wolf void qcow2_refcount_close(BlockDriverState *bs); 195f7d0fe02SKevin Wolf 196ed6ccf0fSKevin Wolf int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size); 197256900b1SKevin Wolf int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, 198256900b1SKevin Wolf int nb_clusters); 199ed6ccf0fSKevin Wolf int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size); 200ed6ccf0fSKevin Wolf void qcow2_free_clusters(BlockDriverState *bs, 201f7d0fe02SKevin Wolf int64_t offset, int64_t size); 202ed6ccf0fSKevin Wolf void qcow2_free_any_clusters(BlockDriverState *bs, 20345aba42fSKevin Wolf uint64_t cluster_offset, int nb_clusters); 204f7d0fe02SKevin Wolf 205ed6ccf0fSKevin Wolf int qcow2_update_snapshot_refcount(BlockDriverState *bs, 206ed6ccf0fSKevin Wolf int64_t l1_table_offset, int l1_size, int addend); 207f7d0fe02SKevin Wolf 2089ac228e0SKevin Wolf int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res); 209f7d0fe02SKevin Wolf 21045aba42fSKevin Wolf /* qcow2-cluster.c functions */ 21172893756SStefan Hajnoczi int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size); 212ed6ccf0fSKevin Wolf void qcow2_l2_cache_reset(BlockDriverState *bs); 21366f82ceeSKevin Wolf int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset); 214ed6ccf0fSKevin Wolf void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num, 21545aba42fSKevin Wolf uint8_t *out_buf, const uint8_t *in_buf, 21645aba42fSKevin Wolf int nb_sectors, int enc, 21745aba42fSKevin Wolf const AES_KEY *key); 21845aba42fSKevin Wolf 2191c46efaaSKevin Wolf int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, 2201c46efaaSKevin Wolf int *num, uint64_t *cluster_offset); 221f4f0d391SKevin Wolf int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset, 222f4f0d391SKevin Wolf int n_start, int n_end, int *num, QCowL2Meta *m); 223ed6ccf0fSKevin Wolf uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, 22445aba42fSKevin Wolf uint64_t offset, 22545aba42fSKevin Wolf int compressed_size); 22645aba42fSKevin Wolf 227148da7eaSKevin Wolf int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m); 2285ea929e3SKevin Wolf int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset, 2295ea929e3SKevin Wolf int nb_sectors); 23045aba42fSKevin Wolf 231c142442bSKevin Wolf /* qcow2-snapshot.c functions */ 232ed6ccf0fSKevin Wolf int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info); 233ed6ccf0fSKevin Wolf int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id); 234ed6ccf0fSKevin Wolf int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id); 235ed6ccf0fSKevin Wolf int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab); 23651ef6727Sedison int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name); 237c142442bSKevin Wolf 238ed6ccf0fSKevin Wolf void qcow2_free_snapshots(BlockDriverState *bs); 239ed6ccf0fSKevin Wolf int qcow2_read_snapshots(BlockDriverState *bs); 240c142442bSKevin Wolf 24149381094SKevin Wolf /* qcow2-cache.c functions */ 24249381094SKevin Wolf Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables, 24349381094SKevin Wolf bool writethrough); 24449381094SKevin Wolf int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c); 24593913dfdSKevin Wolf bool qcow2_cache_set_writethrough(BlockDriverState *bs, Qcow2Cache *c, 24693913dfdSKevin Wolf bool enable); 24749381094SKevin Wolf 24849381094SKevin Wolf void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); 24949381094SKevin Wolf int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c); 25049381094SKevin Wolf int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, 25149381094SKevin Wolf Qcow2Cache *dependency); 2523de0a294SKevin Wolf void qcow2_cache_depends_on_flush(Qcow2Cache *c); 25349381094SKevin Wolf 25449381094SKevin Wolf int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, 25549381094SKevin Wolf void **table); 25649381094SKevin Wolf int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset, 25749381094SKevin Wolf void **table); 25849381094SKevin Wolf int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table); 25949381094SKevin Wolf 260f7d0fe02SKevin Wolf #endif 261