]> git.proxmox.com Git - mirror_qemu.git/blame - block/qcow2.h
qcow2: Use QcowCache
[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"
29
14899cdf
FN
30//#define DEBUG_ALLOC
31//#define DEBUG_ALLOC2
32//#define DEBUG_EXT
33
f7d0fe02
KW
34#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
35#define QCOW_VERSION 2
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)
46
47#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
48
49#define MIN_CLUSTER_BITS 9
80ee15a6 50#define MAX_CLUSTER_BITS 21
f7d0fe02
KW
51
52#define L2_CACHE_SIZE 16
53
29c1a730
KW
54/* Must be at least 4 to cover all cases of refcount table growth */
55#define REFCOUNT_CACHE_SIZE 4
56
f7d0fe02
KW
57typedef struct QCowHeader {
58 uint32_t magic;
59 uint32_t version;
60 uint64_t backing_file_offset;
61 uint32_t backing_file_size;
62 uint32_t cluster_bits;
63 uint64_t size; /* in bytes */
64 uint32_t crypt_method;
65 uint32_t l1_size; /* XXX: save number of clusters instead ? */
66 uint64_t l1_table_offset;
67 uint64_t refcount_table_offset;
68 uint32_t refcount_table_clusters;
69 uint32_t nb_snapshots;
70 uint64_t snapshots_offset;
71} QCowHeader;
72
73typedef struct QCowSnapshot {
74 uint64_t l1_table_offset;
75 uint32_t l1_size;
76 char *id_str;
77 char *name;
78 uint32_t vm_state_size;
79 uint32_t date_sec;
80 uint32_t date_nsec;
81 uint64_t vm_clock_nsec;
82} QCowSnapshot;
83
49381094
KW
84struct Qcow2Cache;
85typedef struct Qcow2Cache Qcow2Cache;
86
f7d0fe02 87typedef struct BDRVQcowState {
f7d0fe02
KW
88 int cluster_bits;
89 int cluster_size;
90 int cluster_sectors;
91 int l2_bits;
92 int l2_size;
93 int l1_size;
94 int l1_vm_state_index;
95 int csize_shift;
96 int csize_mask;
97 uint64_t cluster_offset_mask;
98 uint64_t l1_table_offset;
99 uint64_t *l1_table;
29c1a730
KW
100
101 Qcow2Cache* l2_table_cache;
102 Qcow2Cache* refcount_block_cache;
103
f7d0fe02
KW
104 uint8_t *cluster_cache;
105 uint8_t *cluster_data;
106 uint64_t cluster_cache_offset;
72cf2d4f 107 QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
f7d0fe02
KW
108
109 uint64_t *refcount_table;
110 uint64_t refcount_table_offset;
111 uint32_t refcount_table_size;
f7d0fe02
KW
112 int64_t free_cluster_index;
113 int64_t free_byte_offset;
114
115 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
116 uint32_t crypt_method_header;
117 AES_KEY aes_encrypt_key;
118 AES_KEY aes_decrypt_key;
119 uint64_t snapshots_offset;
120 int snapshots_size;
121 int nb_snapshots;
122 QCowSnapshot *snapshots;
123} BDRVQcowState;
124
125/* XXX: use std qcow open function ? */
126typedef struct QCowCreateState {
127 int cluster_size;
128 int cluster_bits;
129 uint16_t *refcount_block;
130 uint64_t *refcount_table;
131 int64_t l1_table_offset;
132 int64_t refcount_table_offset;
133 int64_t refcount_block_offset;
134} QCowCreateState;
135
f214978a
KW
136struct QCowAIOCB;
137
45aba42f
KW
138/* XXX This could be private for qcow2-cluster.c */
139typedef struct QCowL2Meta
140{
141 uint64_t offset;
148da7ea 142 uint64_t cluster_offset;
45aba42f
KW
143 int n_start;
144 int nb_available;
145 int nb_clusters;
f214978a 146 struct QCowL2Meta *depends_on;
72cf2d4f 147 QLIST_HEAD(QCowAioDependencies, QCowAIOCB) dependent_requests;
f214978a 148
72cf2d4f 149 QLIST_ENTRY(QCowL2Meta) next_in_flight;
45aba42f
KW
150} QCowL2Meta;
151
152static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
f7d0fe02
KW
153{
154 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
155}
156
419b19d9
SH
157static inline int size_to_l1(BDRVQcowState *s, int64_t size)
158{
159 int shift = s->cluster_bits + s->l2_bits;
160 return (size + (1ULL << shift) - 1) >> shift;
161}
162
c142442b
KW
163static inline int64_t align_offset(int64_t offset, int n)
164{
165 offset = (offset + n - 1) & ~(n - 1);
166 return offset;
167}
168
169
f7d0fe02
KW
170// FIXME Need qcow2_ prefix to global functions
171
172/* qcow2.c functions */
bd28f835
KW
173int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
174 int64_t sector_num, int nb_sectors);
f7d0fe02
KW
175
176/* qcow2-refcount.c functions */
ed6ccf0f
KW
177int qcow2_refcount_init(BlockDriverState *bs);
178void qcow2_refcount_close(BlockDriverState *bs);
f7d0fe02 179
ed6ccf0f
KW
180int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
181int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
182void qcow2_free_clusters(BlockDriverState *bs,
45aba42f 183 int64_t offset, int64_t size);
ed6ccf0f 184void qcow2_free_any_clusters(BlockDriverState *bs,
45aba42f 185 uint64_t cluster_offset, int nb_clusters);
f7d0fe02 186
ed6ccf0f
KW
187void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
188 int64_t size);
189int qcow2_update_snapshot_refcount(BlockDriverState *bs,
190 int64_t l1_table_offset, int l1_size, int addend);
f7d0fe02 191
9ac228e0 192int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res);
f7d0fe02 193
45aba42f 194/* qcow2-cluster.c functions */
72893756 195int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
ed6ccf0f 196void qcow2_l2_cache_reset(BlockDriverState *bs);
66f82cee 197int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
ed6ccf0f 198void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
45aba42f
KW
199 uint8_t *out_buf, const uint8_t *in_buf,
200 int nb_sectors, int enc,
201 const AES_KEY *key);
202
1c46efaa
KW
203int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
204 int *num, uint64_t *cluster_offset);
f4f0d391
KW
205int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
206 int n_start, int n_end, int *num, QCowL2Meta *m);
ed6ccf0f 207uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
45aba42f
KW
208 uint64_t offset,
209 int compressed_size);
210
148da7ea 211int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
45aba42f 212
c142442b 213/* qcow2-snapshot.c functions */
ed6ccf0f
KW
214int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
215int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
216int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
217int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
51ef6727 218int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
c142442b 219
ed6ccf0f
KW
220void qcow2_free_snapshots(BlockDriverState *bs);
221int qcow2_read_snapshots(BlockDriverState *bs);
c142442b 222
49381094
KW
223/* qcow2-cache.c functions */
224Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
225 bool writethrough);
226int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
227
228void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
229int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
230int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
231 Qcow2Cache *dependency);
232
233int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
234 void **table);
235int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
236 void **table);
237int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
238
f7d0fe02 239#endif