xref: /qemu/block/qcow2.h (revision 5ea929e3)
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"
29f7d0fe02SKevin Wolf 
3014899cdfSFilip Navara //#define DEBUG_ALLOC
3114899cdfSFilip Navara //#define DEBUG_ALLOC2
3214899cdfSFilip Navara //#define DEBUG_EXT
3314899cdfSFilip Navara 
34f7d0fe02SKevin Wolf #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
35f7d0fe02SKevin Wolf #define QCOW_VERSION 2
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)
46f7d0fe02SKevin Wolf 
47f7d0fe02SKevin Wolf #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
48f7d0fe02SKevin Wolf 
49f7d0fe02SKevin Wolf #define MIN_CLUSTER_BITS 9
5080ee15a6SKevin Wolf #define MAX_CLUSTER_BITS 21
51f7d0fe02SKevin Wolf 
52f7d0fe02SKevin Wolf #define L2_CACHE_SIZE 16
53f7d0fe02SKevin Wolf 
5429c1a730SKevin Wolf /* Must be at least 4 to cover all cases of refcount table growth */
5529c1a730SKevin Wolf #define REFCOUNT_CACHE_SIZE 4
5629c1a730SKevin Wolf 
57f7d0fe02SKevin Wolf typedef struct QCowHeader {
58f7d0fe02SKevin Wolf     uint32_t magic;
59f7d0fe02SKevin Wolf     uint32_t version;
60f7d0fe02SKevin Wolf     uint64_t backing_file_offset;
61f7d0fe02SKevin Wolf     uint32_t backing_file_size;
62f7d0fe02SKevin Wolf     uint32_t cluster_bits;
63f7d0fe02SKevin Wolf     uint64_t size; /* in bytes */
64f7d0fe02SKevin Wolf     uint32_t crypt_method;
65f7d0fe02SKevin Wolf     uint32_t l1_size; /* XXX: save number of clusters instead ? */
66f7d0fe02SKevin Wolf     uint64_t l1_table_offset;
67f7d0fe02SKevin Wolf     uint64_t refcount_table_offset;
68f7d0fe02SKevin Wolf     uint32_t refcount_table_clusters;
69f7d0fe02SKevin Wolf     uint32_t nb_snapshots;
70f7d0fe02SKevin Wolf     uint64_t snapshots_offset;
71f7d0fe02SKevin Wolf } QCowHeader;
72f7d0fe02SKevin Wolf 
73f7d0fe02SKevin Wolf typedef struct QCowSnapshot {
74f7d0fe02SKevin Wolf     uint64_t l1_table_offset;
75f7d0fe02SKevin Wolf     uint32_t l1_size;
76f7d0fe02SKevin Wolf     char *id_str;
77f7d0fe02SKevin Wolf     char *name;
78f7d0fe02SKevin Wolf     uint32_t vm_state_size;
79f7d0fe02SKevin Wolf     uint32_t date_sec;
80f7d0fe02SKevin Wolf     uint32_t date_nsec;
81f7d0fe02SKevin Wolf     uint64_t vm_clock_nsec;
82f7d0fe02SKevin Wolf } QCowSnapshot;
83f7d0fe02SKevin Wolf 
8449381094SKevin Wolf struct Qcow2Cache;
8549381094SKevin Wolf typedef struct Qcow2Cache Qcow2Cache;
8649381094SKevin Wolf 
87f7d0fe02SKevin Wolf typedef struct BDRVQcowState {
88f7d0fe02SKevin Wolf     int cluster_bits;
89f7d0fe02SKevin Wolf     int cluster_size;
90f7d0fe02SKevin Wolf     int cluster_sectors;
91f7d0fe02SKevin Wolf     int l2_bits;
92f7d0fe02SKevin Wolf     int l2_size;
93f7d0fe02SKevin Wolf     int l1_size;
94f7d0fe02SKevin Wolf     int l1_vm_state_index;
95f7d0fe02SKevin Wolf     int csize_shift;
96f7d0fe02SKevin Wolf     int csize_mask;
97f7d0fe02SKevin Wolf     uint64_t cluster_offset_mask;
98f7d0fe02SKevin Wolf     uint64_t l1_table_offset;
99f7d0fe02SKevin Wolf     uint64_t *l1_table;
10029c1a730SKevin Wolf 
10129c1a730SKevin Wolf     Qcow2Cache* l2_table_cache;
10229c1a730SKevin Wolf     Qcow2Cache* refcount_block_cache;
10329c1a730SKevin Wolf 
104f7d0fe02SKevin Wolf     uint8_t *cluster_cache;
105f7d0fe02SKevin Wolf     uint8_t *cluster_data;
106f7d0fe02SKevin Wolf     uint64_t cluster_cache_offset;
10772cf2d4fSBlue Swirl     QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
108f7d0fe02SKevin Wolf 
109f7d0fe02SKevin Wolf     uint64_t *refcount_table;
110f7d0fe02SKevin Wolf     uint64_t refcount_table_offset;
111f7d0fe02SKevin Wolf     uint32_t refcount_table_size;
112f7d0fe02SKevin Wolf     int64_t free_cluster_index;
113f7d0fe02SKevin Wolf     int64_t free_byte_offset;
114f7d0fe02SKevin Wolf 
115f7d0fe02SKevin Wolf     uint32_t crypt_method; /* current crypt method, 0 if no key yet */
116f7d0fe02SKevin Wolf     uint32_t crypt_method_header;
117f7d0fe02SKevin Wolf     AES_KEY aes_encrypt_key;
118f7d0fe02SKevin Wolf     AES_KEY aes_decrypt_key;
119f7d0fe02SKevin Wolf     uint64_t snapshots_offset;
120f7d0fe02SKevin Wolf     int snapshots_size;
121f7d0fe02SKevin Wolf     int nb_snapshots;
122f7d0fe02SKevin Wolf     QCowSnapshot *snapshots;
123f7d0fe02SKevin Wolf } BDRVQcowState;
124f7d0fe02SKevin Wolf 
125f7d0fe02SKevin Wolf /* XXX: use std qcow open function ? */
126f7d0fe02SKevin Wolf typedef struct QCowCreateState {
127f7d0fe02SKevin Wolf     int cluster_size;
128f7d0fe02SKevin Wolf     int cluster_bits;
129f7d0fe02SKevin Wolf     uint16_t *refcount_block;
130f7d0fe02SKevin Wolf     uint64_t *refcount_table;
131f7d0fe02SKevin Wolf     int64_t l1_table_offset;
132f7d0fe02SKevin Wolf     int64_t refcount_table_offset;
133f7d0fe02SKevin Wolf     int64_t refcount_block_offset;
134f7d0fe02SKevin Wolf } QCowCreateState;
135f7d0fe02SKevin Wolf 
136f214978aSKevin Wolf struct QCowAIOCB;
137f214978aSKevin Wolf 
13845aba42fSKevin Wolf /* XXX This could be private for qcow2-cluster.c */
13945aba42fSKevin Wolf typedef struct QCowL2Meta
14045aba42fSKevin Wolf {
14145aba42fSKevin Wolf     uint64_t offset;
142148da7eaSKevin Wolf     uint64_t cluster_offset;
14345aba42fSKevin Wolf     int n_start;
14445aba42fSKevin Wolf     int nb_available;
14545aba42fSKevin Wolf     int nb_clusters;
146f214978aSKevin Wolf     struct QCowL2Meta *depends_on;
14772cf2d4fSBlue Swirl     QLIST_HEAD(QCowAioDependencies, QCowAIOCB) dependent_requests;
148f214978aSKevin Wolf 
14972cf2d4fSBlue Swirl     QLIST_ENTRY(QCowL2Meta) next_in_flight;
15045aba42fSKevin Wolf } QCowL2Meta;
15145aba42fSKevin Wolf 
15245aba42fSKevin Wolf static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
153f7d0fe02SKevin Wolf {
154f7d0fe02SKevin Wolf     return (size + (s->cluster_size - 1)) >> s->cluster_bits;
155f7d0fe02SKevin Wolf }
156f7d0fe02SKevin Wolf 
157419b19d9SStefan Hajnoczi static inline int size_to_l1(BDRVQcowState *s, int64_t size)
158419b19d9SStefan Hajnoczi {
159419b19d9SStefan Hajnoczi     int shift = s->cluster_bits + s->l2_bits;
160419b19d9SStefan Hajnoczi     return (size + (1ULL << shift) - 1) >> shift;
161419b19d9SStefan Hajnoczi }
162419b19d9SStefan Hajnoczi 
163c142442bSKevin Wolf static inline int64_t align_offset(int64_t offset, int n)
164c142442bSKevin Wolf {
165c142442bSKevin Wolf     offset = (offset + n - 1) & ~(n - 1);
166c142442bSKevin Wolf     return offset;
167c142442bSKevin Wolf }
168c142442bSKevin Wolf 
169c142442bSKevin Wolf 
170f7d0fe02SKevin Wolf // FIXME Need qcow2_ prefix to global functions
171f7d0fe02SKevin Wolf 
172f7d0fe02SKevin Wolf /* qcow2.c functions */
173bd28f835SKevin Wolf int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
174bd28f835SKevin Wolf                   int64_t sector_num, int nb_sectors);
175f7d0fe02SKevin Wolf 
176f7d0fe02SKevin Wolf /* qcow2-refcount.c functions */
177ed6ccf0fSKevin Wolf int qcow2_refcount_init(BlockDriverState *bs);
178ed6ccf0fSKevin Wolf void qcow2_refcount_close(BlockDriverState *bs);
179f7d0fe02SKevin Wolf 
180ed6ccf0fSKevin Wolf int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
181ed6ccf0fSKevin Wolf int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
182ed6ccf0fSKevin Wolf void qcow2_free_clusters(BlockDriverState *bs,
183f7d0fe02SKevin Wolf     int64_t offset, int64_t size);
184ed6ccf0fSKevin Wolf void qcow2_free_any_clusters(BlockDriverState *bs,
18545aba42fSKevin Wolf     uint64_t cluster_offset, int nb_clusters);
186f7d0fe02SKevin Wolf 
187ed6ccf0fSKevin Wolf void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
188ed6ccf0fSKevin Wolf     int64_t size);
189ed6ccf0fSKevin Wolf int qcow2_update_snapshot_refcount(BlockDriverState *bs,
190ed6ccf0fSKevin Wolf     int64_t l1_table_offset, int l1_size, int addend);
191f7d0fe02SKevin Wolf 
1929ac228e0SKevin Wolf int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res);
193f7d0fe02SKevin Wolf 
19445aba42fSKevin Wolf /* qcow2-cluster.c functions */
19572893756SStefan Hajnoczi int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
196ed6ccf0fSKevin Wolf void qcow2_l2_cache_reset(BlockDriverState *bs);
19766f82ceeSKevin Wolf int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
198ed6ccf0fSKevin Wolf void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
19945aba42fSKevin Wolf                      uint8_t *out_buf, const uint8_t *in_buf,
20045aba42fSKevin Wolf                      int nb_sectors, int enc,
20145aba42fSKevin Wolf                      const AES_KEY *key);
20245aba42fSKevin Wolf 
2031c46efaaSKevin Wolf int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
2041c46efaaSKevin Wolf     int *num, uint64_t *cluster_offset);
205f4f0d391SKevin Wolf int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
206f4f0d391SKevin Wolf     int n_start, int n_end, int *num, QCowL2Meta *m);
207ed6ccf0fSKevin Wolf uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
20845aba42fSKevin Wolf                                          uint64_t offset,
20945aba42fSKevin Wolf                                          int compressed_size);
21045aba42fSKevin Wolf 
211148da7eaSKevin Wolf int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
212*5ea929e3SKevin Wolf int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
213*5ea929e3SKevin Wolf     int nb_sectors);
21445aba42fSKevin Wolf 
215c142442bSKevin Wolf /* qcow2-snapshot.c functions */
216ed6ccf0fSKevin Wolf int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
217ed6ccf0fSKevin Wolf int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
218ed6ccf0fSKevin Wolf int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
219ed6ccf0fSKevin Wolf int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
22051ef6727Sedison int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
221c142442bSKevin Wolf 
222ed6ccf0fSKevin Wolf void qcow2_free_snapshots(BlockDriverState *bs);
223ed6ccf0fSKevin Wolf int qcow2_read_snapshots(BlockDriverState *bs);
224c142442bSKevin Wolf 
22549381094SKevin Wolf /* qcow2-cache.c functions */
22649381094SKevin Wolf Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
22749381094SKevin Wolf     bool writethrough);
22849381094SKevin Wolf int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
22949381094SKevin Wolf 
23049381094SKevin Wolf void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
23149381094SKevin Wolf int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
23249381094SKevin Wolf int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
23349381094SKevin Wolf     Qcow2Cache *dependency);
2343de0a294SKevin Wolf void qcow2_cache_depends_on_flush(Qcow2Cache *c);
23549381094SKevin Wolf 
23649381094SKevin Wolf int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
23749381094SKevin Wolf     void **table);
23849381094SKevin Wolf int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
23949381094SKevin Wolf     void **table);
24049381094SKevin Wolf int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
24149381094SKevin Wolf 
242f7d0fe02SKevin Wolf #endif
243