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
30 #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
31 #define QCOW_VERSION 2
32
33 #define QCOW_CRYPT_NONE 0
34 #define QCOW_CRYPT_AES 1
35
36 #define QCOW_MAX_CRYPT_CLUSTERS 32
37
38 /* indicate that the refcount of the referenced cluster is exactly one. */
39 #define QCOW_OFLAG_COPIED (1LL << 63)
40 /* indicate that the cluster is compressed (they never have the copied flag) */
41 #define QCOW_OFLAG_COMPRESSED (1LL << 62)
42
43 #define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
44
45 #define MIN_CLUSTER_BITS 9
46 #define MAX_CLUSTER_BITS 16
47
48 #define L2_CACHE_SIZE 16
49
50 typedef struct QCowHeader {
51 uint32_t magic;
52 uint32_t version;
53 uint64_t backing_file_offset;
54 uint32_t backing_file_size;
55 uint32_t cluster_bits;
56 uint64_t size; /* in bytes */
57 uint32_t crypt_method;
58 uint32_t l1_size; /* XXX: save number of clusters instead ? */
59 uint64_t l1_table_offset;
60 uint64_t refcount_table_offset;
61 uint32_t refcount_table_clusters;
62 uint32_t nb_snapshots;
63 uint64_t snapshots_offset;
64 } QCowHeader;
65
66 typedef struct QCowSnapshot {
67 uint64_t l1_table_offset;
68 uint32_t l1_size;
69 char *id_str;
70 char *name;
71 uint32_t vm_state_size;
72 uint32_t date_sec;
73 uint32_t date_nsec;
74 uint64_t vm_clock_nsec;
75 } QCowSnapshot;
76
77 typedef struct BDRVQcowState {
78 BlockDriverState *hd;
79 int cluster_bits;
80 int cluster_size;
81 int cluster_sectors;
82 int l2_bits;
83 int l2_size;
84 int l1_size;
85 int l1_vm_state_index;
86 int csize_shift;
87 int csize_mask;
88 uint64_t cluster_offset_mask;
89 uint64_t l1_table_offset;
90 uint64_t *l1_table;
91 uint64_t *l2_cache;
92 uint64_t l2_cache_offsets[L2_CACHE_SIZE];
93 uint32_t l2_cache_counts[L2_CACHE_SIZE];
94 uint8_t *cluster_cache;
95 uint8_t *cluster_data;
96 uint64_t cluster_cache_offset;
97
98 uint64_t *refcount_table;
99 uint64_t refcount_table_offset;
100 uint32_t refcount_table_size;
101 uint64_t refcount_block_cache_offset;
102 uint16_t *refcount_block_cache;
103 int64_t free_cluster_index;
104 int64_t free_byte_offset;
105
106 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
107 uint32_t crypt_method_header;
108 AES_KEY aes_encrypt_key;
109 AES_KEY aes_decrypt_key;
110 uint64_t snapshots_offset;
111 int snapshots_size;
112 int nb_snapshots;
113 QCowSnapshot *snapshots;
114 } BDRVQcowState;
115
116 /* XXX: use std qcow open function ? */
117 typedef struct QCowCreateState {
118 int cluster_size;
119 int cluster_bits;
120 uint16_t *refcount_block;
121 uint64_t *refcount_table;
122 int64_t l1_table_offset;
123 int64_t refcount_table_offset;
124 int64_t refcount_block_offset;
125 } QCowCreateState;
126
127 /* XXX This could be private for qcow2-cluster.c */
128 typedef struct QCowL2Meta
129 {
130 uint64_t offset;
131 int n_start;
132 int nb_available;
133 int nb_clusters;
134 } QCowL2Meta;
135
size_to_clusters(BDRVQcowState * s,int64_t size)136 static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
137 {
138 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
139 }
140
align_offset(int64_t offset,int n)141 static inline int64_t align_offset(int64_t offset, int n)
142 {
143 offset = (offset + n - 1) & ~(n - 1);
144 return offset;
145 }
146
147
148 // FIXME Need qcow2_ prefix to global functions
149
150 /* qcow2.c functions */
151 int qcow2_backing_read1(BlockDriverState *bs,
152 int64_t sector_num, uint8_t *buf, int nb_sectors);
153
154 /* qcow2-refcount.c functions */
155 int qcow2_refcount_init(BlockDriverState *bs);
156 void qcow2_refcount_close(BlockDriverState *bs);
157
158 int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
159 int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
160 void qcow2_free_clusters(BlockDriverState *bs,
161 int64_t offset, int64_t size);
162 void qcow2_free_any_clusters(BlockDriverState *bs,
163 uint64_t cluster_offset, int nb_clusters);
164
165 void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
166 int64_t size);
167 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
168 int64_t l1_table_offset, int l1_size, int addend);
169
170 int qcow2_check_refcounts(BlockDriverState *bs);
171
172 /* qcow2-cluster.c functions */
173 int qcow2_grow_l1_table(BlockDriverState *bs, int min_size);
174 void qcow2_l2_cache_reset(BlockDriverState *bs);
175 int qcow2_decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
176 void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
177 uint8_t *out_buf, const uint8_t *in_buf,
178 int nb_sectors, int enc,
179 const AES_KEY *key);
180
181 uint64_t qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
182 int *num);
183 uint64_t qcow2_alloc_cluster_offset(BlockDriverState *bs,
184 uint64_t offset,
185 int n_start, int n_end,
186 int *num, QCowL2Meta *m);
187 uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
188 uint64_t offset,
189 int compressed_size);
190
191 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
192 QCowL2Meta *m);
193
194 /* qcow2-snapshot.c functions */
195 int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
196 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
197 int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
198 int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
199
200 void qcow2_free_snapshots(BlockDriverState *bs);
201 int qcow2_read_snapshots(BlockDriverState *bs);
202
203 #endif
204