]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2.h
ide: support enable/disable write cache
[mirror_qemu.git] / block / qcow2.h
CommitLineData
f7d0fe02
KW
1/*
2 * Block driver for the QCOW version 2 format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#ifndef BLOCK_QCOW2_H
26#define BLOCK_QCOW2_H
27
28#include "aes.h"
68d100e9 29#include "qemu-coroutine.h"
f7d0fe02 30
14899cdf
FN
31//#define DEBUG_ALLOC
32//#define DEBUG_ALLOC2
33//#define DEBUG_EXT
34
f7d0fe02 35#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
f7d0fe02
KW
36
37#define QCOW_CRYPT_NONE 0
38#define QCOW_CRYPT_AES 1
39
40#define QCOW_MAX_CRYPT_CLUSTERS 32
41
42/* indicate that the refcount of the referenced cluster is exactly one. */
43#define QCOW_OFLAG_COPIED (1LL << 63)
44/* indicate that the cluster is compressed (they never have the copied flag) */
45#define QCOW_OFLAG_COMPRESSED (1LL << 62)
6377af48
KW
46/* The cluster reads as all zeros */
47#define QCOW_OFLAG_ZERO (1LL << 0)
f7d0fe02
KW
48
49#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
50
51#define MIN_CLUSTER_BITS 9
80ee15a6 52#define MAX_CLUSTER_BITS 21
f7d0fe02
KW
53
54#define L2_CACHE_SIZE 16
55
29c1a730
KW
56/* Must be at least 4 to cover all cases of refcount table growth */
57#define REFCOUNT_CACHE_SIZE 4
58
99cce9fa
KW
59#define DEFAULT_CLUSTER_SIZE 65536
60
f7d0fe02
KW
61typedef struct QCowHeader {
62 uint32_t magic;
63 uint32_t version;
64 uint64_t backing_file_offset;
65 uint32_t backing_file_size;
66 uint32_t cluster_bits;
67 uint64_t size; /* in bytes */
68 uint32_t crypt_method;
69 uint32_t l1_size; /* XXX: save number of clusters instead ? */
70 uint64_t l1_table_offset;
71 uint64_t refcount_table_offset;
72 uint32_t refcount_table_clusters;
73 uint32_t nb_snapshots;
74 uint64_t snapshots_offset;
6744cbab
KW
75
76 /* The following fields are only valid for version >= 3 */
77 uint64_t incompatible_features;
78 uint64_t compatible_features;
79 uint64_t autoclear_features;
80
81 uint32_t refcount_order;
82 uint32_t header_length;
f7d0fe02
KW
83} QCowHeader;
84
85typedef struct QCowSnapshot {
86 uint64_t l1_table_offset;
87 uint32_t l1_size;
88 char *id_str;
89 char *name;
90b27759 90 uint64_t disk_size;
c2c9a466 91 uint64_t vm_state_size;
f7d0fe02
KW
92 uint32_t date_sec;
93 uint32_t date_nsec;
94 uint64_t vm_clock_nsec;
95} QCowSnapshot;
96
49381094
KW
97struct Qcow2Cache;
98typedef struct Qcow2Cache Qcow2Cache;
99
75bab85c
KW
100typedef struct Qcow2UnknownHeaderExtension {
101 uint32_t magic;
102 uint32_t len;
103 QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
104 uint8_t data[];
105} Qcow2UnknownHeaderExtension;
106
cfcc4c62
KW
107enum {
108 QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
109 QCOW2_FEAT_TYPE_COMPATIBLE = 1,
110 QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
111};
112
113typedef struct Qcow2Feature {
114 uint8_t type;
115 uint8_t bit;
116 char name[46];
117} QEMU_PACKED Qcow2Feature;
118
f7d0fe02 119typedef struct BDRVQcowState {
f7d0fe02
KW
120 int cluster_bits;
121 int cluster_size;
122 int cluster_sectors;
123 int l2_bits;
124 int l2_size;
125 int l1_size;
126 int l1_vm_state_index;
127 int csize_shift;
128 int csize_mask;
129 uint64_t cluster_offset_mask;
130 uint64_t l1_table_offset;
131 uint64_t *l1_table;
29c1a730
KW
132
133 Qcow2Cache* l2_table_cache;
134 Qcow2Cache* refcount_block_cache;
135
f7d0fe02
KW
136 uint8_t *cluster_cache;
137 uint8_t *cluster_data;
138 uint64_t cluster_cache_offset;
72cf2d4f 139 QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
f7d0fe02
KW
140
141 uint64_t *refcount_table;
142 uint64_t refcount_table_offset;
143 uint32_t refcount_table_size;
f7d0fe02
KW
144 int64_t free_cluster_index;
145 int64_t free_byte_offset;
146
68d100e9
KW
147 CoMutex lock;
148
f7d0fe02
KW
149 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
150 uint32_t crypt_method_header;
151 AES_KEY aes_encrypt_key;
152 AES_KEY aes_decrypt_key;
153 uint64_t snapshots_offset;
154 int snapshots_size;
155 int nb_snapshots;
156 QCowSnapshot *snapshots;
06d9260f
AL
157
158 int flags;
6744cbab
KW
159 int qcow_version;
160
161 uint64_t incompatible_features;
162 uint64_t compatible_features;
163 uint64_t autoclear_features;
164
165 size_t unknown_header_fields_size;
166 void* unknown_header_fields;
75bab85c 167 QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
f7d0fe02
KW
168} BDRVQcowState;
169
170/* XXX: use std qcow open function ? */
171typedef struct QCowCreateState {
172 int cluster_size;
173 int cluster_bits;
174 uint16_t *refcount_block;
175 uint64_t *refcount_table;
176 int64_t l1_table_offset;
177 int64_t refcount_table_offset;
178 int64_t refcount_block_offset;
179} QCowCreateState;
180
f214978a
KW
181struct QCowAIOCB;
182
45aba42f
KW
183/* XXX This could be private for qcow2-cluster.c */
184typedef struct QCowL2Meta
185{
186 uint64_t offset;
148da7ea 187 uint64_t cluster_offset;
250196f1 188 uint64_t alloc_offset;
45aba42f
KW
189 int n_start;
190 int nb_available;
191 int nb_clusters;
68d100e9 192 CoQueue dependent_requests;
f214978a 193
72cf2d4f 194 QLIST_ENTRY(QCowL2Meta) next_in_flight;
45aba42f
KW
195} QCowL2Meta;
196
68d000a3
KW
197enum {
198 QCOW2_CLUSTER_UNALLOCATED,
199 QCOW2_CLUSTER_NORMAL,
200 QCOW2_CLUSTER_COMPRESSED,
6377af48 201 QCOW2_CLUSTER_ZERO
68d000a3
KW
202};
203
204#define L1E_OFFSET_MASK 0x00ffffffffffff00ULL
205#define L2E_OFFSET_MASK 0x00ffffffffffff00ULL
206#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
207
76dc9e0c
KW
208#define REFT_OFFSET_MASK 0xffffffffffffff00ULL
209
45aba42f 210static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
f7d0fe02
KW
211{
212 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
213}
214
419b19d9
SH
215static inline int size_to_l1(BDRVQcowState *s, int64_t size)
216{
217 int shift = s->cluster_bits + s->l2_bits;
218 return (size + (1ULL << shift) - 1) >> shift;
219}
220
c142442b
KW
221static inline int64_t align_offset(int64_t offset, int n)
222{
223 offset = (offset + n - 1) & ~(n - 1);
224 return offset;
225}
226
68d000a3
KW
227static inline int qcow2_get_cluster_type(uint64_t l2_entry)
228{
229 if (l2_entry & QCOW_OFLAG_COMPRESSED) {
230 return QCOW2_CLUSTER_COMPRESSED;
6377af48
KW
231 } else if (l2_entry & QCOW_OFLAG_ZERO) {
232 return QCOW2_CLUSTER_ZERO;
68d000a3
KW
233 } else if (!(l2_entry & L2E_OFFSET_MASK)) {
234 return QCOW2_CLUSTER_UNALLOCATED;
235 } else {
236 return QCOW2_CLUSTER_NORMAL;
237 }
238}
239
c142442b 240
f7d0fe02
KW
241// FIXME Need qcow2_ prefix to global functions
242
243/* qcow2.c functions */
bd28f835
KW
244int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
245 int64_t sector_num, int nb_sectors);
e24e49e6 246int qcow2_update_header(BlockDriverState *bs);
f7d0fe02
KW
247
248/* qcow2-refcount.c functions */
ed6ccf0f
KW
249int qcow2_refcount_init(BlockDriverState *bs);
250void qcow2_refcount_close(BlockDriverState *bs);
f7d0fe02 251
ed6ccf0f 252int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
256900b1
KW
253int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
254 int nb_clusters);
ed6ccf0f
KW
255int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
256void qcow2_free_clusters(BlockDriverState *bs,
45aba42f 257 int64_t offset, int64_t size);
ed6ccf0f 258void qcow2_free_any_clusters(BlockDriverState *bs,
45aba42f 259 uint64_t cluster_offset, int nb_clusters);
f7d0fe02 260
ed6ccf0f
KW
261int qcow2_update_snapshot_refcount(BlockDriverState *bs,
262 int64_t l1_table_offset, int l1_size, int addend);
f7d0fe02 263
166acf54
KW
264int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
265 BdrvCheckMode fix);
f7d0fe02 266
45aba42f 267/* qcow2-cluster.c functions */
72893756 268int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
ed6ccf0f 269void qcow2_l2_cache_reset(BlockDriverState *bs);
66f82cee 270int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
ed6ccf0f 271void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
45aba42f
KW
272 uint8_t *out_buf, const uint8_t *in_buf,
273 int nb_sectors, int enc,
274 const AES_KEY *key);
275
1c46efaa
KW
276int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
277 int *num, uint64_t *cluster_offset);
f4f0d391
KW
278int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
279 int n_start, int n_end, int *num, QCowL2Meta *m);
ed6ccf0f 280uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
45aba42f
KW
281 uint64_t offset,
282 int compressed_size);
283
148da7ea 284int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
5ea929e3
KW
285int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
286 int nb_sectors);
621f0589 287int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
45aba42f 288
c142442b 289/* qcow2-snapshot.c functions */
ed6ccf0f
KW
290int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
291int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
292int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
293int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
51ef6727 294int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
c142442b 295
ed6ccf0f
KW
296void qcow2_free_snapshots(BlockDriverState *bs);
297int qcow2_read_snapshots(BlockDriverState *bs);
c142442b 298
49381094
KW
299/* qcow2-cache.c functions */
300Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
301 bool writethrough);
302int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
93913dfd
KW
303bool qcow2_cache_set_writethrough(BlockDriverState *bs, Qcow2Cache *c,
304 bool enable);
49381094
KW
305
306void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
307int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
308int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
309 Qcow2Cache *dependency);
3de0a294 310void qcow2_cache_depends_on_flush(Qcow2Cache *c);
49381094
KW
311
312int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
313 void **table);
314int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
315 void **table);
316int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
317
f7d0fe02 318#endif