xref: /qemu/block/qcow2.h (revision 166acf54)
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 
37f7d0fe02SKevin Wolf #define QCOW_CRYPT_NONE 0
38f7d0fe02SKevin Wolf #define QCOW_CRYPT_AES  1
39f7d0fe02SKevin Wolf 
40f7d0fe02SKevin Wolf #define QCOW_MAX_CRYPT_CLUSTERS 32
41f7d0fe02SKevin Wolf 
42f7d0fe02SKevin Wolf /* indicate that the refcount of the referenced cluster is exactly one. */
43f7d0fe02SKevin Wolf #define QCOW_OFLAG_COPIED     (1LL << 63)
44f7d0fe02SKevin Wolf /* indicate that the cluster is compressed (they never have the copied flag) */
45f7d0fe02SKevin Wolf #define QCOW_OFLAG_COMPRESSED (1LL << 62)
466377af48SKevin Wolf /* The cluster reads as all zeros */
476377af48SKevin Wolf #define QCOW_OFLAG_ZERO (1LL << 0)
48f7d0fe02SKevin Wolf 
49f7d0fe02SKevin Wolf #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
50f7d0fe02SKevin Wolf 
51f7d0fe02SKevin Wolf #define MIN_CLUSTER_BITS 9
5280ee15a6SKevin Wolf #define MAX_CLUSTER_BITS 21
53f7d0fe02SKevin Wolf 
54f7d0fe02SKevin Wolf #define L2_CACHE_SIZE 16
55f7d0fe02SKevin Wolf 
5629c1a730SKevin Wolf /* Must be at least 4 to cover all cases of refcount table growth */
5729c1a730SKevin Wolf #define REFCOUNT_CACHE_SIZE 4
5829c1a730SKevin Wolf 
5999cce9faSKevin Wolf #define DEFAULT_CLUSTER_SIZE 65536
6099cce9faSKevin Wolf 
61f7d0fe02SKevin Wolf typedef struct QCowHeader {
62f7d0fe02SKevin Wolf     uint32_t magic;
63f7d0fe02SKevin Wolf     uint32_t version;
64f7d0fe02SKevin Wolf     uint64_t backing_file_offset;
65f7d0fe02SKevin Wolf     uint32_t backing_file_size;
66f7d0fe02SKevin Wolf     uint32_t cluster_bits;
67f7d0fe02SKevin Wolf     uint64_t size; /* in bytes */
68f7d0fe02SKevin Wolf     uint32_t crypt_method;
69f7d0fe02SKevin Wolf     uint32_t l1_size; /* XXX: save number of clusters instead ? */
70f7d0fe02SKevin Wolf     uint64_t l1_table_offset;
71f7d0fe02SKevin Wolf     uint64_t refcount_table_offset;
72f7d0fe02SKevin Wolf     uint32_t refcount_table_clusters;
73f7d0fe02SKevin Wolf     uint32_t nb_snapshots;
74f7d0fe02SKevin Wolf     uint64_t snapshots_offset;
756744cbabSKevin Wolf 
766744cbabSKevin Wolf     /* The following fields are only valid for version >= 3 */
776744cbabSKevin Wolf     uint64_t incompatible_features;
786744cbabSKevin Wolf     uint64_t compatible_features;
796744cbabSKevin Wolf     uint64_t autoclear_features;
806744cbabSKevin Wolf 
816744cbabSKevin Wolf     uint32_t refcount_order;
826744cbabSKevin Wolf     uint32_t header_length;
83f7d0fe02SKevin Wolf } QCowHeader;
84f7d0fe02SKevin Wolf 
85f7d0fe02SKevin Wolf typedef struct QCowSnapshot {
86f7d0fe02SKevin Wolf     uint64_t l1_table_offset;
87f7d0fe02SKevin Wolf     uint32_t l1_size;
88f7d0fe02SKevin Wolf     char *id_str;
89f7d0fe02SKevin Wolf     char *name;
9090b27759SKevin Wolf     uint64_t disk_size;
91c2c9a466SKevin Wolf     uint64_t vm_state_size;
92f7d0fe02SKevin Wolf     uint32_t date_sec;
93f7d0fe02SKevin Wolf     uint32_t date_nsec;
94f7d0fe02SKevin Wolf     uint64_t vm_clock_nsec;
95f7d0fe02SKevin Wolf } QCowSnapshot;
96f7d0fe02SKevin Wolf 
9749381094SKevin Wolf struct Qcow2Cache;
9849381094SKevin Wolf typedef struct Qcow2Cache Qcow2Cache;
9949381094SKevin Wolf 
10075bab85cSKevin Wolf typedef struct Qcow2UnknownHeaderExtension {
10175bab85cSKevin Wolf     uint32_t magic;
10275bab85cSKevin Wolf     uint32_t len;
10375bab85cSKevin Wolf     QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
10475bab85cSKevin Wolf     uint8_t data[];
10575bab85cSKevin Wolf } Qcow2UnknownHeaderExtension;
10675bab85cSKevin Wolf 
107cfcc4c62SKevin Wolf enum {
108cfcc4c62SKevin Wolf     QCOW2_FEAT_TYPE_INCOMPATIBLE    = 0,
109cfcc4c62SKevin Wolf     QCOW2_FEAT_TYPE_COMPATIBLE      = 1,
110cfcc4c62SKevin Wolf     QCOW2_FEAT_TYPE_AUTOCLEAR       = 2,
111cfcc4c62SKevin Wolf };
112cfcc4c62SKevin Wolf 
113cfcc4c62SKevin Wolf typedef struct Qcow2Feature {
114cfcc4c62SKevin Wolf     uint8_t type;
115cfcc4c62SKevin Wolf     uint8_t bit;
116cfcc4c62SKevin Wolf     char    name[46];
117cfcc4c62SKevin Wolf } QEMU_PACKED Qcow2Feature;
118cfcc4c62SKevin Wolf 
119f7d0fe02SKevin Wolf typedef struct BDRVQcowState {
120f7d0fe02SKevin Wolf     int cluster_bits;
121f7d0fe02SKevin Wolf     int cluster_size;
122f7d0fe02SKevin Wolf     int cluster_sectors;
123f7d0fe02SKevin Wolf     int l2_bits;
124f7d0fe02SKevin Wolf     int l2_size;
125f7d0fe02SKevin Wolf     int l1_size;
126f7d0fe02SKevin Wolf     int l1_vm_state_index;
127f7d0fe02SKevin Wolf     int csize_shift;
128f7d0fe02SKevin Wolf     int csize_mask;
129f7d0fe02SKevin Wolf     uint64_t cluster_offset_mask;
130f7d0fe02SKevin Wolf     uint64_t l1_table_offset;
131f7d0fe02SKevin Wolf     uint64_t *l1_table;
13229c1a730SKevin Wolf 
13329c1a730SKevin Wolf     Qcow2Cache* l2_table_cache;
13429c1a730SKevin Wolf     Qcow2Cache* refcount_block_cache;
13529c1a730SKevin Wolf 
136f7d0fe02SKevin Wolf     uint8_t *cluster_cache;
137f7d0fe02SKevin Wolf     uint8_t *cluster_data;
138f7d0fe02SKevin Wolf     uint64_t cluster_cache_offset;
13972cf2d4fSBlue Swirl     QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
140f7d0fe02SKevin Wolf 
141f7d0fe02SKevin Wolf     uint64_t *refcount_table;
142f7d0fe02SKevin Wolf     uint64_t refcount_table_offset;
143f7d0fe02SKevin Wolf     uint32_t refcount_table_size;
144f7d0fe02SKevin Wolf     int64_t free_cluster_index;
145f7d0fe02SKevin Wolf     int64_t free_byte_offset;
146f7d0fe02SKevin Wolf 
14768d100e9SKevin Wolf     CoMutex lock;
14868d100e9SKevin Wolf 
149f7d0fe02SKevin Wolf     uint32_t crypt_method; /* current crypt method, 0 if no key yet */
150f7d0fe02SKevin Wolf     uint32_t crypt_method_header;
151f7d0fe02SKevin Wolf     AES_KEY aes_encrypt_key;
152f7d0fe02SKevin Wolf     AES_KEY aes_decrypt_key;
153f7d0fe02SKevin Wolf     uint64_t snapshots_offset;
154f7d0fe02SKevin Wolf     int snapshots_size;
155f7d0fe02SKevin Wolf     int nb_snapshots;
156f7d0fe02SKevin Wolf     QCowSnapshot *snapshots;
15706d9260fSAnthony Liguori 
15806d9260fSAnthony Liguori     int flags;
1596744cbabSKevin Wolf     int qcow_version;
1606744cbabSKevin Wolf 
1616744cbabSKevin Wolf     uint64_t incompatible_features;
1626744cbabSKevin Wolf     uint64_t compatible_features;
1636744cbabSKevin Wolf     uint64_t autoclear_features;
1646744cbabSKevin Wolf 
1656744cbabSKevin Wolf     size_t unknown_header_fields_size;
1666744cbabSKevin Wolf     void* unknown_header_fields;
16775bab85cSKevin Wolf     QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
168f7d0fe02SKevin Wolf } BDRVQcowState;
169f7d0fe02SKevin Wolf 
170f7d0fe02SKevin Wolf /* XXX: use std qcow open function ? */
171f7d0fe02SKevin Wolf typedef struct QCowCreateState {
172f7d0fe02SKevin Wolf     int cluster_size;
173f7d0fe02SKevin Wolf     int cluster_bits;
174f7d0fe02SKevin Wolf     uint16_t *refcount_block;
175f7d0fe02SKevin Wolf     uint64_t *refcount_table;
176f7d0fe02SKevin Wolf     int64_t l1_table_offset;
177f7d0fe02SKevin Wolf     int64_t refcount_table_offset;
178f7d0fe02SKevin Wolf     int64_t refcount_block_offset;
179f7d0fe02SKevin Wolf } QCowCreateState;
180f7d0fe02SKevin Wolf 
181f214978aSKevin Wolf struct QCowAIOCB;
182f214978aSKevin Wolf 
18345aba42fSKevin Wolf /* XXX This could be private for qcow2-cluster.c */
18445aba42fSKevin Wolf typedef struct QCowL2Meta
18545aba42fSKevin Wolf {
18645aba42fSKevin Wolf     uint64_t offset;
187148da7eaSKevin Wolf     uint64_t cluster_offset;
188250196f1SKevin Wolf     uint64_t alloc_offset;
18945aba42fSKevin Wolf     int n_start;
19045aba42fSKevin Wolf     int nb_available;
19145aba42fSKevin Wolf     int nb_clusters;
19268d100e9SKevin Wolf     CoQueue dependent_requests;
193f214978aSKevin Wolf 
19472cf2d4fSBlue Swirl     QLIST_ENTRY(QCowL2Meta) next_in_flight;
19545aba42fSKevin Wolf } QCowL2Meta;
19645aba42fSKevin Wolf 
19768d000a3SKevin Wolf enum {
19868d000a3SKevin Wolf     QCOW2_CLUSTER_UNALLOCATED,
19968d000a3SKevin Wolf     QCOW2_CLUSTER_NORMAL,
20068d000a3SKevin Wolf     QCOW2_CLUSTER_COMPRESSED,
2016377af48SKevin Wolf     QCOW2_CLUSTER_ZERO
20268d000a3SKevin Wolf };
20368d000a3SKevin Wolf 
20468d000a3SKevin Wolf #define L1E_OFFSET_MASK 0x00ffffffffffff00ULL
20568d000a3SKevin Wolf #define L2E_OFFSET_MASK 0x00ffffffffffff00ULL
20668d000a3SKevin Wolf #define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
20768d000a3SKevin Wolf 
20876dc9e0cSKevin Wolf #define REFT_OFFSET_MASK 0xffffffffffffff00ULL
20976dc9e0cSKevin Wolf 
21045aba42fSKevin Wolf static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
211f7d0fe02SKevin Wolf {
212f7d0fe02SKevin Wolf     return (size + (s->cluster_size - 1)) >> s->cluster_bits;
213f7d0fe02SKevin Wolf }
214f7d0fe02SKevin Wolf 
215419b19d9SStefan Hajnoczi static inline int size_to_l1(BDRVQcowState *s, int64_t size)
216419b19d9SStefan Hajnoczi {
217419b19d9SStefan Hajnoczi     int shift = s->cluster_bits + s->l2_bits;
218419b19d9SStefan Hajnoczi     return (size + (1ULL << shift) - 1) >> shift;
219419b19d9SStefan Hajnoczi }
220419b19d9SStefan Hajnoczi 
221c142442bSKevin Wolf static inline int64_t align_offset(int64_t offset, int n)
222c142442bSKevin Wolf {
223c142442bSKevin Wolf     offset = (offset + n - 1) & ~(n - 1);
224c142442bSKevin Wolf     return offset;
225c142442bSKevin Wolf }
226c142442bSKevin Wolf 
22768d000a3SKevin Wolf static inline int qcow2_get_cluster_type(uint64_t l2_entry)
22868d000a3SKevin Wolf {
22968d000a3SKevin Wolf     if (l2_entry & QCOW_OFLAG_COMPRESSED) {
23068d000a3SKevin Wolf         return QCOW2_CLUSTER_COMPRESSED;
2316377af48SKevin Wolf     } else if (l2_entry & QCOW_OFLAG_ZERO) {
2326377af48SKevin Wolf         return QCOW2_CLUSTER_ZERO;
23368d000a3SKevin Wolf     } else if (!(l2_entry & L2E_OFFSET_MASK)) {
23468d000a3SKevin Wolf         return QCOW2_CLUSTER_UNALLOCATED;
23568d000a3SKevin Wolf     } else {
23668d000a3SKevin Wolf         return QCOW2_CLUSTER_NORMAL;
23768d000a3SKevin Wolf     }
23868d000a3SKevin Wolf }
23968d000a3SKevin Wolf 
240c142442bSKevin Wolf 
241f7d0fe02SKevin Wolf // FIXME Need qcow2_ prefix to global functions
242f7d0fe02SKevin Wolf 
243f7d0fe02SKevin Wolf /* qcow2.c functions */
244bd28f835SKevin Wolf int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
245bd28f835SKevin Wolf                   int64_t sector_num, int nb_sectors);
246e24e49e6SKevin Wolf int qcow2_update_header(BlockDriverState *bs);
247f7d0fe02SKevin Wolf 
248f7d0fe02SKevin Wolf /* qcow2-refcount.c functions */
249ed6ccf0fSKevin Wolf int qcow2_refcount_init(BlockDriverState *bs);
250ed6ccf0fSKevin Wolf void qcow2_refcount_close(BlockDriverState *bs);
251f7d0fe02SKevin Wolf 
252ed6ccf0fSKevin Wolf int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
253256900b1SKevin Wolf int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
254256900b1SKevin Wolf     int nb_clusters);
255ed6ccf0fSKevin Wolf int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
256ed6ccf0fSKevin Wolf void qcow2_free_clusters(BlockDriverState *bs,
257f7d0fe02SKevin Wolf     int64_t offset, int64_t size);
258ed6ccf0fSKevin Wolf void qcow2_free_any_clusters(BlockDriverState *bs,
25945aba42fSKevin Wolf     uint64_t cluster_offset, int nb_clusters);
260f7d0fe02SKevin Wolf 
261ed6ccf0fSKevin Wolf int qcow2_update_snapshot_refcount(BlockDriverState *bs,
262ed6ccf0fSKevin Wolf     int64_t l1_table_offset, int l1_size, int addend);
263f7d0fe02SKevin Wolf 
264*166acf54SKevin Wolf int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
265*166acf54SKevin Wolf                           BdrvCheckMode fix);
266f7d0fe02SKevin Wolf 
26745aba42fSKevin Wolf /* qcow2-cluster.c functions */
26872893756SStefan Hajnoczi int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
269ed6ccf0fSKevin Wolf void qcow2_l2_cache_reset(BlockDriverState *bs);
27066f82ceeSKevin Wolf int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
271ed6ccf0fSKevin Wolf void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
27245aba42fSKevin Wolf                      uint8_t *out_buf, const uint8_t *in_buf,
27345aba42fSKevin Wolf                      int nb_sectors, int enc,
27445aba42fSKevin Wolf                      const AES_KEY *key);
27545aba42fSKevin Wolf 
2761c46efaaSKevin Wolf int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
2771c46efaaSKevin Wolf     int *num, uint64_t *cluster_offset);
278f4f0d391SKevin Wolf int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
279f4f0d391SKevin Wolf     int n_start, int n_end, int *num, QCowL2Meta *m);
280ed6ccf0fSKevin Wolf uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
28145aba42fSKevin Wolf                                          uint64_t offset,
28245aba42fSKevin Wolf                                          int compressed_size);
28345aba42fSKevin Wolf 
284148da7eaSKevin Wolf int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
2855ea929e3SKevin Wolf int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
2865ea929e3SKevin Wolf     int nb_sectors);
287621f0589SKevin Wolf int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
28845aba42fSKevin Wolf 
289c142442bSKevin Wolf /* qcow2-snapshot.c functions */
290ed6ccf0fSKevin Wolf int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
291ed6ccf0fSKevin Wolf int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
292ed6ccf0fSKevin Wolf int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
293ed6ccf0fSKevin Wolf int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
29451ef6727Sedison int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
295c142442bSKevin Wolf 
296ed6ccf0fSKevin Wolf void qcow2_free_snapshots(BlockDriverState *bs);
297ed6ccf0fSKevin Wolf int qcow2_read_snapshots(BlockDriverState *bs);
298c142442bSKevin Wolf 
29949381094SKevin Wolf /* qcow2-cache.c functions */
30049381094SKevin Wolf Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
30149381094SKevin Wolf     bool writethrough);
30249381094SKevin Wolf int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
30393913dfdSKevin Wolf bool qcow2_cache_set_writethrough(BlockDriverState *bs, Qcow2Cache *c,
30493913dfdSKevin Wolf     bool enable);
30549381094SKevin Wolf 
30649381094SKevin Wolf void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
30749381094SKevin Wolf int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
30849381094SKevin Wolf int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
30949381094SKevin Wolf     Qcow2Cache *dependency);
3103de0a294SKevin Wolf void qcow2_cache_depends_on_flush(Qcow2Cache *c);
31149381094SKevin Wolf 
31249381094SKevin Wolf int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
31349381094SKevin Wolf     void **table);
31449381094SKevin Wolf int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
31549381094SKevin Wolf     void **table);
31649381094SKevin Wolf int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
31749381094SKevin Wolf 
318f7d0fe02SKevin Wolf #endif
319