xref: /qemu/block/qcow2.h (revision 51ef6727)
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 
54f7d0fe02SKevin Wolf typedef struct QCowHeader {
55f7d0fe02SKevin Wolf     uint32_t magic;
56f7d0fe02SKevin Wolf     uint32_t version;
57f7d0fe02SKevin Wolf     uint64_t backing_file_offset;
58f7d0fe02SKevin Wolf     uint32_t backing_file_size;
59f7d0fe02SKevin Wolf     uint32_t cluster_bits;
60f7d0fe02SKevin Wolf     uint64_t size; /* in bytes */
61f7d0fe02SKevin Wolf     uint32_t crypt_method;
62f7d0fe02SKevin Wolf     uint32_t l1_size; /* XXX: save number of clusters instead ? */
63f7d0fe02SKevin Wolf     uint64_t l1_table_offset;
64f7d0fe02SKevin Wolf     uint64_t refcount_table_offset;
65f7d0fe02SKevin Wolf     uint32_t refcount_table_clusters;
66f7d0fe02SKevin Wolf     uint32_t nb_snapshots;
67f7d0fe02SKevin Wolf     uint64_t snapshots_offset;
68f7d0fe02SKevin Wolf } QCowHeader;
69f7d0fe02SKevin Wolf 
70f7d0fe02SKevin Wolf typedef struct QCowSnapshot {
71f7d0fe02SKevin Wolf     uint64_t l1_table_offset;
72f7d0fe02SKevin Wolf     uint32_t l1_size;
73f7d0fe02SKevin Wolf     char *id_str;
74f7d0fe02SKevin Wolf     char *name;
75f7d0fe02SKevin Wolf     uint32_t vm_state_size;
76f7d0fe02SKevin Wolf     uint32_t date_sec;
77f7d0fe02SKevin Wolf     uint32_t date_nsec;
78f7d0fe02SKevin Wolf     uint64_t vm_clock_nsec;
79f7d0fe02SKevin Wolf } QCowSnapshot;
80f7d0fe02SKevin Wolf 
81f7d0fe02SKevin Wolf typedef struct BDRVQcowState {
82f7d0fe02SKevin Wolf     BlockDriverState *hd;
83f7d0fe02SKevin Wolf     int cluster_bits;
84f7d0fe02SKevin Wolf     int cluster_size;
85f7d0fe02SKevin Wolf     int cluster_sectors;
86f7d0fe02SKevin Wolf     int l2_bits;
87f7d0fe02SKevin Wolf     int l2_size;
88f7d0fe02SKevin Wolf     int l1_size;
89f7d0fe02SKevin Wolf     int l1_vm_state_index;
90f7d0fe02SKevin Wolf     int csize_shift;
91f7d0fe02SKevin Wolf     int csize_mask;
92f7d0fe02SKevin Wolf     uint64_t cluster_offset_mask;
93f7d0fe02SKevin Wolf     uint64_t l1_table_offset;
94f7d0fe02SKevin Wolf     uint64_t *l1_table;
95f7d0fe02SKevin Wolf     uint64_t *l2_cache;
96f7d0fe02SKevin Wolf     uint64_t l2_cache_offsets[L2_CACHE_SIZE];
97f7d0fe02SKevin Wolf     uint32_t l2_cache_counts[L2_CACHE_SIZE];
98f7d0fe02SKevin Wolf     uint8_t *cluster_cache;
99f7d0fe02SKevin Wolf     uint8_t *cluster_data;
100f7d0fe02SKevin Wolf     uint64_t cluster_cache_offset;
10172cf2d4fSBlue Swirl     QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
102f7d0fe02SKevin Wolf 
103f7d0fe02SKevin Wolf     uint64_t *refcount_table;
104f7d0fe02SKevin Wolf     uint64_t refcount_table_offset;
105f7d0fe02SKevin Wolf     uint32_t refcount_table_size;
106f7d0fe02SKevin Wolf     uint64_t refcount_block_cache_offset;
107f7d0fe02SKevin Wolf     uint16_t *refcount_block_cache;
108f7d0fe02SKevin Wolf     int64_t free_cluster_index;
109f7d0fe02SKevin Wolf     int64_t free_byte_offset;
110f7d0fe02SKevin Wolf 
111f7d0fe02SKevin Wolf     uint32_t crypt_method; /* current crypt method, 0 if no key yet */
112f7d0fe02SKevin Wolf     uint32_t crypt_method_header;
113f7d0fe02SKevin Wolf     AES_KEY aes_encrypt_key;
114f7d0fe02SKevin Wolf     AES_KEY aes_decrypt_key;
115f7d0fe02SKevin Wolf     uint64_t snapshots_offset;
116f7d0fe02SKevin Wolf     int snapshots_size;
117f7d0fe02SKevin Wolf     int nb_snapshots;
118f7d0fe02SKevin Wolf     QCowSnapshot *snapshots;
119f7d0fe02SKevin Wolf } BDRVQcowState;
120f7d0fe02SKevin Wolf 
121f7d0fe02SKevin Wolf /* XXX: use std qcow open function ? */
122f7d0fe02SKevin Wolf typedef struct QCowCreateState {
123f7d0fe02SKevin Wolf     int cluster_size;
124f7d0fe02SKevin Wolf     int cluster_bits;
125f7d0fe02SKevin Wolf     uint16_t *refcount_block;
126f7d0fe02SKevin Wolf     uint64_t *refcount_table;
127f7d0fe02SKevin Wolf     int64_t l1_table_offset;
128f7d0fe02SKevin Wolf     int64_t refcount_table_offset;
129f7d0fe02SKevin Wolf     int64_t refcount_block_offset;
130f7d0fe02SKevin Wolf } QCowCreateState;
131f7d0fe02SKevin Wolf 
132f214978aSKevin Wolf struct QCowAIOCB;
133f214978aSKevin Wolf 
13445aba42fSKevin Wolf /* XXX This could be private for qcow2-cluster.c */
13545aba42fSKevin Wolf typedef struct QCowL2Meta
13645aba42fSKevin Wolf {
13745aba42fSKevin Wolf     uint64_t offset;
138148da7eaSKevin Wolf     uint64_t cluster_offset;
13945aba42fSKevin Wolf     int n_start;
14045aba42fSKevin Wolf     int nb_available;
14145aba42fSKevin Wolf     int nb_clusters;
142f214978aSKevin Wolf     struct QCowL2Meta *depends_on;
14372cf2d4fSBlue Swirl     QLIST_HEAD(QCowAioDependencies, QCowAIOCB) dependent_requests;
144f214978aSKevin Wolf 
14572cf2d4fSBlue Swirl     QLIST_ENTRY(QCowL2Meta) next_in_flight;
14645aba42fSKevin Wolf } QCowL2Meta;
14745aba42fSKevin Wolf 
14845aba42fSKevin Wolf static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
149f7d0fe02SKevin Wolf {
150f7d0fe02SKevin Wolf     return (size + (s->cluster_size - 1)) >> s->cluster_bits;
151f7d0fe02SKevin Wolf }
152f7d0fe02SKevin Wolf 
153419b19d9SStefan Hajnoczi static inline int size_to_l1(BDRVQcowState *s, int64_t size)
154419b19d9SStefan Hajnoczi {
155419b19d9SStefan Hajnoczi     int shift = s->cluster_bits + s->l2_bits;
156419b19d9SStefan Hajnoczi     return (size + (1ULL << shift) - 1) >> shift;
157419b19d9SStefan Hajnoczi }
158419b19d9SStefan Hajnoczi 
159c142442bSKevin Wolf static inline int64_t align_offset(int64_t offset, int n)
160c142442bSKevin Wolf {
161c142442bSKevin Wolf     offset = (offset + n - 1) & ~(n - 1);
162c142442bSKevin Wolf     return offset;
163c142442bSKevin Wolf }
164c142442bSKevin Wolf 
165c142442bSKevin Wolf 
166f7d0fe02SKevin Wolf // FIXME Need qcow2_ prefix to global functions
167f7d0fe02SKevin Wolf 
168f7d0fe02SKevin Wolf /* qcow2.c functions */
169bd28f835SKevin Wolf int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
170bd28f835SKevin Wolf                   int64_t sector_num, int nb_sectors);
171f7d0fe02SKevin Wolf 
172f7d0fe02SKevin Wolf /* qcow2-refcount.c functions */
173ed6ccf0fSKevin Wolf int qcow2_refcount_init(BlockDriverState *bs);
174ed6ccf0fSKevin Wolf void qcow2_refcount_close(BlockDriverState *bs);
175f7d0fe02SKevin Wolf 
176ed6ccf0fSKevin Wolf int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
177ed6ccf0fSKevin Wolf int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
178ed6ccf0fSKevin Wolf void qcow2_free_clusters(BlockDriverState *bs,
179f7d0fe02SKevin Wolf     int64_t offset, int64_t size);
180ed6ccf0fSKevin Wolf void qcow2_free_any_clusters(BlockDriverState *bs,
18145aba42fSKevin Wolf     uint64_t cluster_offset, int nb_clusters);
182f7d0fe02SKevin Wolf 
183ed6ccf0fSKevin Wolf void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
184ed6ccf0fSKevin Wolf     int64_t size);
185ed6ccf0fSKevin Wolf int qcow2_update_snapshot_refcount(BlockDriverState *bs,
186ed6ccf0fSKevin Wolf     int64_t l1_table_offset, int l1_size, int addend);
187f7d0fe02SKevin Wolf 
1889ac228e0SKevin Wolf int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res);
189f7d0fe02SKevin Wolf 
19045aba42fSKevin Wolf /* qcow2-cluster.c functions */
19172893756SStefan Hajnoczi int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
192ed6ccf0fSKevin Wolf void qcow2_l2_cache_reset(BlockDriverState *bs);
19366f82ceeSKevin Wolf int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
194ed6ccf0fSKevin Wolf void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
19545aba42fSKevin Wolf                      uint8_t *out_buf, const uint8_t *in_buf,
19645aba42fSKevin Wolf                      int nb_sectors, int enc,
19745aba42fSKevin Wolf                      const AES_KEY *key);
19845aba42fSKevin Wolf 
1991c46efaaSKevin Wolf int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
2001c46efaaSKevin Wolf     int *num, uint64_t *cluster_offset);
201f4f0d391SKevin Wolf int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
202f4f0d391SKevin Wolf     int n_start, int n_end, int *num, QCowL2Meta *m);
203ed6ccf0fSKevin Wolf uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
20445aba42fSKevin Wolf                                          uint64_t offset,
20545aba42fSKevin Wolf                                          int compressed_size);
20645aba42fSKevin Wolf 
207148da7eaSKevin Wolf int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
20845aba42fSKevin Wolf 
209c142442bSKevin Wolf /* qcow2-snapshot.c functions */
210ed6ccf0fSKevin Wolf int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
211ed6ccf0fSKevin Wolf int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
212ed6ccf0fSKevin Wolf int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
213ed6ccf0fSKevin Wolf int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
214*51ef6727Sedison int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
215c142442bSKevin Wolf 
216ed6ccf0fSKevin Wolf void qcow2_free_snapshots(BlockDriverState *bs);
217ed6ccf0fSKevin Wolf int qcow2_read_snapshots(BlockDriverState *bs);
218c142442bSKevin Wolf 
219f7d0fe02SKevin Wolf #endif
220