xref: /qemu/block/qcow2.h (revision 66f82cee)
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 
153c142442bSKevin Wolf static inline int64_t align_offset(int64_t offset, int n)
154c142442bSKevin Wolf {
155c142442bSKevin Wolf     offset = (offset + n - 1) & ~(n - 1);
156c142442bSKevin Wolf     return offset;
157c142442bSKevin Wolf }
158c142442bSKevin Wolf 
159c142442bSKevin Wolf 
160f7d0fe02SKevin Wolf // FIXME Need qcow2_ prefix to global functions
161f7d0fe02SKevin Wolf 
162f7d0fe02SKevin Wolf /* qcow2.c functions */
163ed6ccf0fSKevin Wolf int qcow2_backing_read1(BlockDriverState *bs,
16445aba42fSKevin Wolf                   int64_t sector_num, uint8_t *buf, int nb_sectors);
165f7d0fe02SKevin Wolf 
166f7d0fe02SKevin Wolf /* qcow2-refcount.c functions */
167ed6ccf0fSKevin Wolf int qcow2_refcount_init(BlockDriverState *bs);
168ed6ccf0fSKevin Wolf void qcow2_refcount_close(BlockDriverState *bs);
169f7d0fe02SKevin Wolf 
170ed6ccf0fSKevin Wolf int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
171ed6ccf0fSKevin Wolf int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
172ed6ccf0fSKevin Wolf void qcow2_free_clusters(BlockDriverState *bs,
173f7d0fe02SKevin Wolf     int64_t offset, int64_t size);
174ed6ccf0fSKevin Wolf void qcow2_free_any_clusters(BlockDriverState *bs,
17545aba42fSKevin Wolf     uint64_t cluster_offset, int nb_clusters);
176f7d0fe02SKevin Wolf 
177ed6ccf0fSKevin Wolf void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
178ed6ccf0fSKevin Wolf     int64_t size);
179ed6ccf0fSKevin Wolf int qcow2_update_snapshot_refcount(BlockDriverState *bs,
180ed6ccf0fSKevin Wolf     int64_t l1_table_offset, int l1_size, int addend);
181f7d0fe02SKevin Wolf 
182ed6ccf0fSKevin Wolf int qcow2_check_refcounts(BlockDriverState *bs);
183f7d0fe02SKevin Wolf 
18445aba42fSKevin Wolf /* qcow2-cluster.c functions */
185ed6ccf0fSKevin Wolf int qcow2_grow_l1_table(BlockDriverState *bs, int min_size);
186ed6ccf0fSKevin Wolf void qcow2_l2_cache_reset(BlockDriverState *bs);
187*66f82ceeSKevin Wolf int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
188ed6ccf0fSKevin Wolf void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
18945aba42fSKevin Wolf                      uint8_t *out_buf, const uint8_t *in_buf,
19045aba42fSKevin Wolf                      int nb_sectors, int enc,
19145aba42fSKevin Wolf                      const AES_KEY *key);
19245aba42fSKevin Wolf 
193ed6ccf0fSKevin Wolf uint64_t qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
194ed6ccf0fSKevin Wolf     int *num);
195f4f0d391SKevin Wolf int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
196f4f0d391SKevin Wolf     int n_start, int n_end, int *num, QCowL2Meta *m);
197ed6ccf0fSKevin Wolf uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
19845aba42fSKevin Wolf                                          uint64_t offset,
19945aba42fSKevin Wolf                                          int compressed_size);
20045aba42fSKevin Wolf 
201148da7eaSKevin Wolf int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
20245aba42fSKevin Wolf 
203c142442bSKevin Wolf /* qcow2-snapshot.c functions */
204ed6ccf0fSKevin Wolf int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
205ed6ccf0fSKevin Wolf int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
206ed6ccf0fSKevin Wolf int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
207ed6ccf0fSKevin Wolf int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
208c142442bSKevin Wolf 
209ed6ccf0fSKevin Wolf void qcow2_free_snapshots(BlockDriverState *bs);
210ed6ccf0fSKevin Wolf int qcow2_read_snapshots(BlockDriverState *bs);
211c142442bSKevin Wolf 
212f7d0fe02SKevin Wolf #endif
213