]>
Commit | Line | Data |
---|---|---|
585f8587 FB |
1 | /* |
2 | * Block driver for the QCOW version 2 format | |
5fafdf24 | 3 | * |
585f8587 | 4 | * Copyright (c) 2004-2006 Fabrice Bellard |
5fafdf24 | 5 | * |
585f8587 FB |
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 | */ | |
faf07963 | 24 | #include "qemu-common.h" |
585f8587 | 25 | #include "block_int.h" |
5efa9d5a | 26 | #include "module.h" |
585f8587 FB |
27 | #include <zlib.h> |
28 | #include "aes.h" | |
f7d0fe02 | 29 | #include "block/qcow2.h" |
585f8587 FB |
30 | |
31 | /* | |
32 | Differences with QCOW: | |
33 | ||
34 | - Support for multiple incremental snapshots. | |
35 | - Memory management by reference counts. | |
36 | - Clusters which have a reference count of one have the bit | |
37 | QCOW_OFLAG_COPIED to optimize write performance. | |
5fafdf24 | 38 | - Size of compressed clusters is stored in sectors to reduce bit usage |
585f8587 FB |
39 | in the cluster offsets. |
40 | - Support for storing additional data (such as the VM state) in the | |
3b46e624 | 41 | snapshots. |
585f8587 FB |
42 | - If a backing store is used, the cluster size is not constrained |
43 | (could be backported to QCOW). | |
44 | - L2 tables have always a size of one cluster. | |
45 | */ | |
46 | ||
9b80ddf3 AL |
47 | |
48 | typedef struct { | |
49 | uint32_t magic; | |
50 | uint32_t len; | |
51 | } QCowExtension; | |
52 | #define QCOW_EXT_MAGIC_END 0 | |
f965509c | 53 | #define QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA |
9b80ddf3 AL |
54 | |
55 | ||
585f8587 FB |
56 | |
57 | static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) | |
58 | { | |
59 | const QCowHeader *cow_header = (const void *)buf; | |
3b46e624 | 60 | |
585f8587 FB |
61 | if (buf_size >= sizeof(QCowHeader) && |
62 | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && | |
5fafdf24 | 63 | be32_to_cpu(cow_header->version) == QCOW_VERSION) |
585f8587 FB |
64 | return 100; |
65 | else | |
66 | return 0; | |
67 | } | |
68 | ||
9b80ddf3 AL |
69 | |
70 | /* | |
71 | * read qcow2 extension and fill bs | |
72 | * start reading from start_offset | |
73 | * finish reading upon magic of value 0 or when end_offset reached | |
74 | * unknown magic is skipped (future extension this version knows nothing about) | |
75 | * return 0 upon success, non-0 otherwise | |
76 | */ | |
77 | static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset, | |
78 | uint64_t end_offset) | |
79 | { | |
80 | BDRVQcowState *s = bs->opaque; | |
81 | QCowExtension ext; | |
82 | uint64_t offset; | |
83 | ||
84 | #ifdef DEBUG_EXT | |
85 | printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); | |
86 | #endif | |
87 | offset = start_offset; | |
88 | while (offset < end_offset) { | |
89 | ||
90 | #ifdef DEBUG_EXT | |
91 | /* Sanity check */ | |
92 | if (offset > s->cluster_size) | |
93 | printf("qcow_handle_extension: suspicious offset %lu\n", offset); | |
94 | ||
95 | printf("attemting to read extended header in offset %lu\n", offset); | |
96 | #endif | |
97 | ||
98 | if (bdrv_pread(s->hd, offset, &ext, sizeof(ext)) != sizeof(ext)) { | |
4c978075 AL |
99 | fprintf(stderr, "qcow_handle_extension: ERROR: pread fail from offset %llu\n", |
100 | (unsigned long long)offset); | |
9b80ddf3 AL |
101 | return 1; |
102 | } | |
103 | be32_to_cpus(&ext.magic); | |
104 | be32_to_cpus(&ext.len); | |
105 | offset += sizeof(ext); | |
106 | #ifdef DEBUG_EXT | |
107 | printf("ext.magic = 0x%x\n", ext.magic); | |
108 | #endif | |
109 | switch (ext.magic) { | |
110 | case QCOW_EXT_MAGIC_END: | |
111 | return 0; | |
f965509c AL |
112 | |
113 | case QCOW_EXT_MAGIC_BACKING_FORMAT: | |
114 | if (ext.len >= sizeof(bs->backing_format)) { | |
115 | fprintf(stderr, "ERROR: ext_backing_format: len=%u too large" | |
4c978075 | 116 | " (>=%zu)\n", |
f965509c AL |
117 | ext.len, sizeof(bs->backing_format)); |
118 | return 2; | |
119 | } | |
120 | if (bdrv_pread(s->hd, offset , bs->backing_format, | |
121 | ext.len) != ext.len) | |
122 | return 3; | |
123 | bs->backing_format[ext.len] = '\0'; | |
124 | #ifdef DEBUG_EXT | |
125 | printf("Qcow2: Got format extension %s\n", bs->backing_format); | |
126 | #endif | |
127 | offset += ((ext.len + 7) & ~7); | |
128 | break; | |
129 | ||
9b80ddf3 AL |
130 | default: |
131 | /* unknown magic -- just skip it */ | |
132 | offset += ((ext.len + 7) & ~7); | |
133 | break; | |
134 | } | |
135 | } | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
140 | ||
585f8587 FB |
141 | static int qcow_open(BlockDriverState *bs, const char *filename, int flags) |
142 | { | |
143 | BDRVQcowState *s = bs->opaque; | |
144 | int len, i, shift, ret; | |
145 | QCowHeader header; | |
9b80ddf3 | 146 | uint64_t ext_end; |
585f8587 | 147 | |
b5eff355 | 148 | ret = bdrv_file_open(&s->hd, filename, flags); |
585f8587 FB |
149 | if (ret < 0) |
150 | return ret; | |
151 | if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header)) | |
152 | goto fail; | |
153 | be32_to_cpus(&header.magic); | |
154 | be32_to_cpus(&header.version); | |
155 | be64_to_cpus(&header.backing_file_offset); | |
156 | be32_to_cpus(&header.backing_file_size); | |
157 | be64_to_cpus(&header.size); | |
158 | be32_to_cpus(&header.cluster_bits); | |
159 | be32_to_cpus(&header.crypt_method); | |
160 | be64_to_cpus(&header.l1_table_offset); | |
161 | be32_to_cpus(&header.l1_size); | |
162 | be64_to_cpus(&header.refcount_table_offset); | |
163 | be32_to_cpus(&header.refcount_table_clusters); | |
164 | be64_to_cpus(&header.snapshots_offset); | |
165 | be32_to_cpus(&header.nb_snapshots); | |
3b46e624 | 166 | |
585f8587 FB |
167 | if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION) |
168 | goto fail; | |
5fafdf24 | 169 | if (header.size <= 1 || |
73c632ed KW |
170 | header.cluster_bits < MIN_CLUSTER_BITS || |
171 | header.cluster_bits > MAX_CLUSTER_BITS) | |
585f8587 FB |
172 | goto fail; |
173 | if (header.crypt_method > QCOW_CRYPT_AES) | |
174 | goto fail; | |
175 | s->crypt_method_header = header.crypt_method; | |
176 | if (s->crypt_method_header) | |
177 | bs->encrypted = 1; | |
178 | s->cluster_bits = header.cluster_bits; | |
179 | s->cluster_size = 1 << s->cluster_bits; | |
180 | s->cluster_sectors = 1 << (s->cluster_bits - 9); | |
181 | s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ | |
182 | s->l2_size = 1 << s->l2_bits; | |
183 | bs->total_sectors = header.size / 512; | |
184 | s->csize_shift = (62 - (s->cluster_bits - 8)); | |
185 | s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; | |
186 | s->cluster_offset_mask = (1LL << s->csize_shift) - 1; | |
187 | s->refcount_table_offset = header.refcount_table_offset; | |
5fafdf24 | 188 | s->refcount_table_size = |
585f8587 FB |
189 | header.refcount_table_clusters << (s->cluster_bits - 3); |
190 | ||
191 | s->snapshots_offset = header.snapshots_offset; | |
192 | s->nb_snapshots = header.nb_snapshots; | |
193 | ||
194 | /* read the level 1 table */ | |
195 | s->l1_size = header.l1_size; | |
196 | shift = s->cluster_bits + s->l2_bits; | |
197 | s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift; | |
198 | /* the L1 table must contain at least enough entries to put | |
199 | header.size bytes */ | |
200 | if (s->l1_size < s->l1_vm_state_index) | |
201 | goto fail; | |
202 | s->l1_table_offset = header.l1_table_offset; | |
3f6a3ee5 KW |
203 | s->l1_table = qemu_mallocz( |
204 | align_offset(s->l1_size * sizeof(uint64_t), 512)); | |
5fafdf24 | 205 | if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
585f8587 FB |
206 | s->l1_size * sizeof(uint64_t)) |
207 | goto fail; | |
208 | for(i = 0;i < s->l1_size; i++) { | |
209 | be64_to_cpus(&s->l1_table[i]); | |
210 | } | |
211 | /* alloc L2 cache */ | |
212 | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); | |
585f8587 | 213 | s->cluster_cache = qemu_malloc(s->cluster_size); |
585f8587 | 214 | /* one more sector for decompressed data alignment */ |
095a9c58 AL |
215 | s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size |
216 | + 512); | |
585f8587 | 217 | s->cluster_cache_offset = -1; |
3b46e624 | 218 | |
ed6ccf0f | 219 | if (qcow2_refcount_init(bs) < 0) |
585f8587 FB |
220 | goto fail; |
221 | ||
9b80ddf3 AL |
222 | /* read qcow2 extensions */ |
223 | if (header.backing_file_offset) | |
224 | ext_end = header.backing_file_offset; | |
225 | else | |
226 | ext_end = s->cluster_size; | |
227 | if (qcow_read_extensions(bs, sizeof(header), ext_end)) | |
228 | goto fail; | |
229 | ||
585f8587 FB |
230 | /* read the backing file name */ |
231 | if (header.backing_file_offset != 0) { | |
232 | len = header.backing_file_size; | |
233 | if (len > 1023) | |
234 | len = 1023; | |
235 | if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len) | |
236 | goto fail; | |
237 | bs->backing_file[len] = '\0'; | |
238 | } | |
ed6ccf0f | 239 | if (qcow2_read_snapshots(bs) < 0) |
585f8587 FB |
240 | goto fail; |
241 | ||
242 | #ifdef DEBUG_ALLOC | |
14899cdf | 243 | qcow2_check_refcounts(bs); |
585f8587 FB |
244 | #endif |
245 | return 0; | |
246 | ||
247 | fail: | |
ed6ccf0f KW |
248 | qcow2_free_snapshots(bs); |
249 | qcow2_refcount_close(bs); | |
585f8587 FB |
250 | qemu_free(s->l1_table); |
251 | qemu_free(s->l2_cache); | |
252 | qemu_free(s->cluster_cache); | |
253 | qemu_free(s->cluster_data); | |
254 | bdrv_delete(s->hd); | |
255 | return -1; | |
256 | } | |
257 | ||
258 | static int qcow_set_key(BlockDriverState *bs, const char *key) | |
259 | { | |
260 | BDRVQcowState *s = bs->opaque; | |
261 | uint8_t keybuf[16]; | |
262 | int len, i; | |
3b46e624 | 263 | |
585f8587 FB |
264 | memset(keybuf, 0, 16); |
265 | len = strlen(key); | |
266 | if (len > 16) | |
267 | len = 16; | |
268 | /* XXX: we could compress the chars to 7 bits to increase | |
269 | entropy */ | |
270 | for(i = 0;i < len;i++) { | |
271 | keybuf[i] = key[i]; | |
272 | } | |
273 | s->crypt_method = s->crypt_method_header; | |
274 | ||
275 | if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) | |
276 | return -1; | |
277 | if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) | |
278 | return -1; | |
279 | #if 0 | |
280 | /* test */ | |
281 | { | |
282 | uint8_t in[16]; | |
283 | uint8_t out[16]; | |
284 | uint8_t tmp[16]; | |
285 | for(i=0;i<16;i++) | |
286 | in[i] = i; | |
287 | AES_encrypt(in, tmp, &s->aes_encrypt_key); | |
288 | AES_decrypt(tmp, out, &s->aes_decrypt_key); | |
289 | for(i = 0; i < 16; i++) | |
290 | printf(" %02x", tmp[i]); | |
291 | printf("\n"); | |
292 | for(i = 0; i < 16; i++) | |
293 | printf(" %02x", out[i]); | |
294 | printf("\n"); | |
295 | } | |
296 | #endif | |
297 | return 0; | |
298 | } | |
299 | ||
5fafdf24 | 300 | static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
585f8587 FB |
301 | int nb_sectors, int *pnum) |
302 | { | |
585f8587 FB |
303 | uint64_t cluster_offset; |
304 | ||
095a9c58 | 305 | *pnum = nb_sectors; |
ed6ccf0f | 306 | cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, pnum); |
095a9c58 | 307 | |
585f8587 FB |
308 | return (cluster_offset != 0); |
309 | } | |
310 | ||
a9465922 | 311 | /* handle reading after the end of the backing file */ |
ed6ccf0f | 312 | int qcow2_backing_read1(BlockDriverState *bs, |
45aba42f | 313 | int64_t sector_num, uint8_t *buf, int nb_sectors) |
a9465922 FB |
314 | { |
315 | int n1; | |
316 | if ((sector_num + nb_sectors) <= bs->total_sectors) | |
317 | return nb_sectors; | |
318 | if (sector_num >= bs->total_sectors) | |
319 | n1 = 0; | |
320 | else | |
321 | n1 = bs->total_sectors - sector_num; | |
322 | memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1)); | |
323 | return n1; | |
324 | } | |
325 | ||
ce1a14dc PB |
326 | typedef struct QCowAIOCB { |
327 | BlockDriverAIOCB common; | |
585f8587 | 328 | int64_t sector_num; |
f141eafe | 329 | QEMUIOVector *qiov; |
585f8587 | 330 | uint8_t *buf; |
f141eafe | 331 | void *orig_buf; |
585f8587 FB |
332 | int nb_sectors; |
333 | int n; | |
334 | uint64_t cluster_offset; | |
5fafdf24 | 335 | uint8_t *cluster_data; |
585f8587 | 336 | BlockDriverAIOCB *hd_aiocb; |
c87c0672 AL |
337 | struct iovec hd_iov; |
338 | QEMUIOVector hd_qiov; | |
1490791f | 339 | QEMUBH *bh; |
e976c6a1 | 340 | QCowL2Meta l2meta; |
585f8587 FB |
341 | } QCowAIOCB; |
342 | ||
c16b5a2c CH |
343 | static void qcow_aio_cancel(BlockDriverAIOCB *blockacb) |
344 | { | |
345 | QCowAIOCB *acb = (QCowAIOCB *)blockacb; | |
346 | if (acb->hd_aiocb) | |
347 | bdrv_aio_cancel(acb->hd_aiocb); | |
348 | qemu_aio_release(acb); | |
349 | } | |
350 | ||
351 | static AIOPool qcow_aio_pool = { | |
352 | .aiocb_size = sizeof(QCowAIOCB), | |
353 | .cancel = qcow_aio_cancel, | |
354 | }; | |
355 | ||
1490791f AL |
356 | static void qcow_aio_read_cb(void *opaque, int ret); |
357 | static void qcow_aio_read_bh(void *opaque) | |
358 | { | |
359 | QCowAIOCB *acb = opaque; | |
360 | qemu_bh_delete(acb->bh); | |
361 | acb->bh = NULL; | |
362 | qcow_aio_read_cb(opaque, 0); | |
363 | } | |
364 | ||
a32ef786 AL |
365 | static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb) |
366 | { | |
367 | if (acb->bh) | |
368 | return -EIO; | |
369 | ||
370 | acb->bh = qemu_bh_new(cb, acb); | |
371 | if (!acb->bh) | |
372 | return -EIO; | |
373 | ||
374 | qemu_bh_schedule(acb->bh); | |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
585f8587 FB |
379 | static void qcow_aio_read_cb(void *opaque, int ret) |
380 | { | |
ce1a14dc PB |
381 | QCowAIOCB *acb = opaque; |
382 | BlockDriverState *bs = acb->common.bs; | |
585f8587 | 383 | BDRVQcowState *s = bs->opaque; |
a9465922 | 384 | int index_in_cluster, n1; |
585f8587 | 385 | |
ce1a14dc | 386 | acb->hd_aiocb = NULL; |
f141eafe AL |
387 | if (ret < 0) |
388 | goto done; | |
585f8587 | 389 | |
585f8587 | 390 | /* post process the read buffer */ |
ce1a14dc | 391 | if (!acb->cluster_offset) { |
585f8587 | 392 | /* nothing to do */ |
ce1a14dc | 393 | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
585f8587 FB |
394 | /* nothing to do */ |
395 | } else { | |
396 | if (s->crypt_method) { | |
ed6ccf0f | 397 | qcow2_encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, |
5fafdf24 | 398 | acb->n, 0, |
585f8587 FB |
399 | &s->aes_decrypt_key); |
400 | } | |
401 | } | |
402 | ||
ce1a14dc PB |
403 | acb->nb_sectors -= acb->n; |
404 | acb->sector_num += acb->n; | |
405 | acb->buf += acb->n * 512; | |
585f8587 | 406 | |
ce1a14dc | 407 | if (acb->nb_sectors == 0) { |
585f8587 | 408 | /* request completed */ |
f141eafe AL |
409 | ret = 0; |
410 | goto done; | |
585f8587 | 411 | } |
3b46e624 | 412 | |
585f8587 | 413 | /* prepare next AIO request */ |
095a9c58 | 414 | acb->n = acb->nb_sectors; |
ed6ccf0f KW |
415 | acb->cluster_offset = |
416 | qcow2_get_cluster_offset(bs, acb->sector_num << 9, &acb->n); | |
ce1a14dc | 417 | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); |
ce1a14dc PB |
418 | |
419 | if (!acb->cluster_offset) { | |
585f8587 FB |
420 | if (bs->backing_hd) { |
421 | /* read from the base image */ | |
ed6ccf0f | 422 | n1 = qcow2_backing_read1(bs->backing_hd, acb->sector_num, |
ce1a14dc | 423 | acb->buf, acb->n); |
a9465922 | 424 | if (n1 > 0) { |
3f4cb3d3 | 425 | acb->hd_iov.iov_base = (void *)acb->buf; |
c87c0672 AL |
426 | acb->hd_iov.iov_len = acb->n * 512; |
427 | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); | |
428 | acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num, | |
429 | &acb->hd_qiov, acb->n, | |
430 | qcow_aio_read_cb, acb); | |
ce1a14dc | 431 | if (acb->hd_aiocb == NULL) |
f141eafe | 432 | goto done; |
a9465922 | 433 | } else { |
a32ef786 AL |
434 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
435 | if (ret < 0) | |
f141eafe | 436 | goto done; |
a9465922 | 437 | } |
585f8587 FB |
438 | } else { |
439 | /* Note: in this case, no need to wait */ | |
ce1a14dc | 440 | memset(acb->buf, 0, 512 * acb->n); |
a32ef786 AL |
441 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
442 | if (ret < 0) | |
f141eafe | 443 | goto done; |
585f8587 | 444 | } |
ce1a14dc | 445 | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
585f8587 | 446 | /* add AIO support for compressed blocks ? */ |
ed6ccf0f | 447 | if (qcow2_decompress_cluster(s, acb->cluster_offset) < 0) |
f141eafe | 448 | goto done; |
5fafdf24 | 449 | memcpy(acb->buf, |
ce1a14dc | 450 | s->cluster_cache + index_in_cluster * 512, 512 * acb->n); |
a32ef786 AL |
451 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
452 | if (ret < 0) | |
f141eafe | 453 | goto done; |
585f8587 | 454 | } else { |
ce1a14dc | 455 | if ((acb->cluster_offset & 511) != 0) { |
585f8587 | 456 | ret = -EIO; |
f141eafe | 457 | goto done; |
585f8587 | 458 | } |
c87c0672 | 459 | |
3f4cb3d3 | 460 | acb->hd_iov.iov_base = (void *)acb->buf; |
c87c0672 AL |
461 | acb->hd_iov.iov_len = acb->n * 512; |
462 | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); | |
463 | acb->hd_aiocb = bdrv_aio_readv(s->hd, | |
5fafdf24 | 464 | (acb->cluster_offset >> 9) + index_in_cluster, |
c87c0672 | 465 | &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb); |
ce1a14dc | 466 | if (acb->hd_aiocb == NULL) |
f141eafe AL |
467 | goto done; |
468 | } | |
469 | ||
470 | return; | |
471 | done: | |
472 | if (acb->qiov->niov > 1) { | |
473 | qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size); | |
474 | qemu_vfree(acb->orig_buf); | |
585f8587 | 475 | } |
f141eafe AL |
476 | acb->common.cb(acb->common.opaque, ret); |
477 | qemu_aio_release(acb); | |
585f8587 FB |
478 | } |
479 | ||
ce1a14dc | 480 | static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs, |
f141eafe AL |
481 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
482 | BlockDriverCompletionFunc *cb, void *opaque, int is_write) | |
585f8587 | 483 | { |
ce1a14dc PB |
484 | QCowAIOCB *acb; |
485 | ||
c16b5a2c | 486 | acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque); |
ce1a14dc PB |
487 | if (!acb) |
488 | return NULL; | |
489 | acb->hd_aiocb = NULL; | |
490 | acb->sector_num = sector_num; | |
f141eafe AL |
491 | acb->qiov = qiov; |
492 | if (qiov->niov > 1) { | |
e268ca52 | 493 | acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size); |
f141eafe AL |
494 | if (is_write) |
495 | qemu_iovec_to_buffer(qiov, acb->buf); | |
3f4cb3d3 BS |
496 | } else { |
497 | acb->buf = (uint8_t *)qiov->iov->iov_base; | |
498 | } | |
ce1a14dc PB |
499 | acb->nb_sectors = nb_sectors; |
500 | acb->n = 0; | |
501 | acb->cluster_offset = 0; | |
e976c6a1 | 502 | acb->l2meta.nb_clusters = 0; |
ce1a14dc PB |
503 | return acb; |
504 | } | |
505 | ||
f141eafe AL |
506 | static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs, |
507 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc PB |
508 | BlockDriverCompletionFunc *cb, void *opaque) |
509 | { | |
510 | QCowAIOCB *acb; | |
511 | ||
f141eafe | 512 | acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
ce1a14dc PB |
513 | if (!acb) |
514 | return NULL; | |
585f8587 FB |
515 | |
516 | qcow_aio_read_cb(acb, 0); | |
ce1a14dc | 517 | return &acb->common; |
585f8587 FB |
518 | } |
519 | ||
520 | static void qcow_aio_write_cb(void *opaque, int ret) | |
521 | { | |
ce1a14dc PB |
522 | QCowAIOCB *acb = opaque; |
523 | BlockDriverState *bs = acb->common.bs; | |
585f8587 | 524 | BDRVQcowState *s = bs->opaque; |
585f8587 | 525 | int index_in_cluster; |
585f8587 | 526 | const uint8_t *src_buf; |
095a9c58 | 527 | int n_end; |
ce1a14dc PB |
528 | |
529 | acb->hd_aiocb = NULL; | |
530 | ||
f141eafe AL |
531 | if (ret < 0) |
532 | goto done; | |
585f8587 | 533 | |
ed6ccf0f KW |
534 | if (qcow2_alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta) < 0) { |
535 | qcow2_free_any_clusters(bs, acb->cluster_offset, acb->l2meta.nb_clusters); | |
f141eafe | 536 | goto done; |
e976c6a1 AL |
537 | } |
538 | ||
ce1a14dc PB |
539 | acb->nb_sectors -= acb->n; |
540 | acb->sector_num += acb->n; | |
541 | acb->buf += acb->n * 512; | |
585f8587 | 542 | |
ce1a14dc | 543 | if (acb->nb_sectors == 0) { |
585f8587 | 544 | /* request completed */ |
f141eafe AL |
545 | ret = 0; |
546 | goto done; | |
585f8587 | 547 | } |
3b46e624 | 548 | |
ce1a14dc | 549 | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); |
095a9c58 AL |
550 | n_end = index_in_cluster + acb->nb_sectors; |
551 | if (s->crypt_method && | |
552 | n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) | |
553 | n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; | |
554 | ||
ed6ccf0f | 555 | acb->cluster_offset = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9, |
05203524 | 556 | index_in_cluster, |
e976c6a1 AL |
557 | n_end, &acb->n, &acb->l2meta); |
558 | if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) { | |
585f8587 | 559 | ret = -EIO; |
f141eafe | 560 | goto done; |
585f8587 FB |
561 | } |
562 | if (s->crypt_method) { | |
ce1a14dc | 563 | if (!acb->cluster_data) { |
095a9c58 AL |
564 | acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * |
565 | s->cluster_size); | |
585f8587 | 566 | } |
ed6ccf0f | 567 | qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf, |
ce1a14dc PB |
568 | acb->n, 1, &s->aes_encrypt_key); |
569 | src_buf = acb->cluster_data; | |
585f8587 | 570 | } else { |
ce1a14dc | 571 | src_buf = acb->buf; |
585f8587 | 572 | } |
c87c0672 AL |
573 | acb->hd_iov.iov_base = (void *)src_buf; |
574 | acb->hd_iov.iov_len = acb->n * 512; | |
575 | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); | |
576 | acb->hd_aiocb = bdrv_aio_writev(s->hd, | |
577 | (acb->cluster_offset >> 9) + index_in_cluster, | |
578 | &acb->hd_qiov, acb->n, | |
579 | qcow_aio_write_cb, acb); | |
ce1a14dc | 580 | if (acb->hd_aiocb == NULL) |
f141eafe AL |
581 | goto done; |
582 | ||
583 | return; | |
584 | ||
585 | done: | |
586 | if (acb->qiov->niov > 1) | |
587 | qemu_vfree(acb->orig_buf); | |
588 | acb->common.cb(acb->common.opaque, ret); | |
589 | qemu_aio_release(acb); | |
585f8587 FB |
590 | } |
591 | ||
f141eafe AL |
592 | static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs, |
593 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 594 | BlockDriverCompletionFunc *cb, void *opaque) |
585f8587 | 595 | { |
585f8587 | 596 | BDRVQcowState *s = bs->opaque; |
ce1a14dc | 597 | QCowAIOCB *acb; |
3b46e624 | 598 | |
585f8587 FB |
599 | s->cluster_cache_offset = -1; /* disable compressed cache */ |
600 | ||
f141eafe | 601 | acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
ce1a14dc PB |
602 | if (!acb) |
603 | return NULL; | |
3b46e624 | 604 | |
585f8587 | 605 | qcow_aio_write_cb(acb, 0); |
ce1a14dc | 606 | return &acb->common; |
585f8587 FB |
607 | } |
608 | ||
585f8587 FB |
609 | static void qcow_close(BlockDriverState *bs) |
610 | { | |
611 | BDRVQcowState *s = bs->opaque; | |
612 | qemu_free(s->l1_table); | |
613 | qemu_free(s->l2_cache); | |
614 | qemu_free(s->cluster_cache); | |
615 | qemu_free(s->cluster_data); | |
ed6ccf0f | 616 | qcow2_refcount_close(bs); |
585f8587 FB |
617 | bdrv_delete(s->hd); |
618 | } | |
619 | ||
73c632ed KW |
620 | static int get_bits_from_size(size_t size) |
621 | { | |
622 | int res = 0; | |
623 | ||
624 | if (size == 0) { | |
625 | return -1; | |
626 | } | |
627 | ||
628 | while (size != 1) { | |
629 | /* Not a power of two */ | |
630 | if (size & 1) { | |
631 | return -1; | |
632 | } | |
633 | ||
634 | size >>= 1; | |
635 | res++; | |
636 | } | |
637 | ||
638 | return res; | |
639 | } | |
640 | ||
f965509c AL |
641 | static int qcow_create2(const char *filename, int64_t total_size, |
642 | const char *backing_file, const char *backing_format, | |
73c632ed | 643 | int flags, size_t cluster_size) |
585f8587 | 644 | { |
f965509c | 645 | |
585f8587 | 646 | int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits; |
2d2431f0 | 647 | int ref_clusters, backing_format_len = 0; |
585f8587 FB |
648 | QCowHeader header; |
649 | uint64_t tmp, offset; | |
650 | QCowCreateState s1, *s = &s1; | |
f965509c AL |
651 | QCowExtension ext_bf = {0, 0}; |
652 | ||
3b46e624 | 653 | |
585f8587 FB |
654 | memset(s, 0, sizeof(*s)); |
655 | ||
656 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); | |
657 | if (fd < 0) | |
658 | return -1; | |
659 | memset(&header, 0, sizeof(header)); | |
660 | header.magic = cpu_to_be32(QCOW_MAGIC); | |
661 | header.version = cpu_to_be32(QCOW_VERSION); | |
662 | header.size = cpu_to_be64(total_size * 512); | |
663 | header_size = sizeof(header); | |
664 | backing_filename_len = 0; | |
665 | if (backing_file) { | |
f965509c AL |
666 | if (backing_format) { |
667 | ext_bf.magic = QCOW_EXT_MAGIC_BACKING_FORMAT; | |
668 | backing_format_len = strlen(backing_format); | |
669 | ext_bf.len = (backing_format_len + 7) & ~7; | |
670 | header_size += ((sizeof(ext_bf) + ext_bf.len + 7) & ~7); | |
671 | } | |
585f8587 FB |
672 | header.backing_file_offset = cpu_to_be64(header_size); |
673 | backing_filename_len = strlen(backing_file); | |
674 | header.backing_file_size = cpu_to_be32(backing_filename_len); | |
675 | header_size += backing_filename_len; | |
676 | } | |
73c632ed KW |
677 | |
678 | /* Cluster size */ | |
679 | s->cluster_bits = get_bits_from_size(cluster_size); | |
680 | if (s->cluster_bits < MIN_CLUSTER_BITS || | |
681 | s->cluster_bits > MAX_CLUSTER_BITS) | |
682 | { | |
683 | fprintf(stderr, "Cluster size must be a power of two between " | |
684 | "%d and %dk\n", | |
685 | 1 << MIN_CLUSTER_BITS, | |
686 | 1 << (MAX_CLUSTER_BITS - 10)); | |
687 | return -EINVAL; | |
688 | } | |
585f8587 | 689 | s->cluster_size = 1 << s->cluster_bits; |
73c632ed | 690 | |
585f8587 FB |
691 | header.cluster_bits = cpu_to_be32(s->cluster_bits); |
692 | header_size = (header_size + 7) & ~7; | |
ec36ba14 | 693 | if (flags & BLOCK_FLAG_ENCRYPT) { |
585f8587 FB |
694 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); |
695 | } else { | |
696 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); | |
697 | } | |
698 | l2_bits = s->cluster_bits - 3; | |
699 | shift = s->cluster_bits + l2_bits; | |
700 | l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift); | |
701 | offset = align_offset(header_size, s->cluster_size); | |
702 | s->l1_table_offset = offset; | |
703 | header.l1_table_offset = cpu_to_be64(s->l1_table_offset); | |
704 | header.l1_size = cpu_to_be32(l1_size); | |
15e6690a | 705 | offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size); |
585f8587 FB |
706 | |
707 | s->refcount_table = qemu_mallocz(s->cluster_size); | |
3b46e624 | 708 | |
585f8587 FB |
709 | s->refcount_table_offset = offset; |
710 | header.refcount_table_offset = cpu_to_be64(offset); | |
711 | header.refcount_table_clusters = cpu_to_be32(1); | |
712 | offset += s->cluster_size; | |
585f8587 | 713 | s->refcount_block_offset = offset; |
2d2431f0 AL |
714 | |
715 | /* count how many refcount blocks needed */ | |
716 | tmp = offset >> s->cluster_bits; | |
717 | ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1; | |
718 | for (i=0; i < ref_clusters; i++) { | |
719 | s->refcount_table[i] = cpu_to_be64(offset); | |
720 | offset += s->cluster_size; | |
721 | } | |
722 | ||
723 | s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size); | |
585f8587 FB |
724 | |
725 | /* update refcounts */ | |
ed6ccf0f KW |
726 | qcow2_create_refcount_update(s, 0, header_size); |
727 | qcow2_create_refcount_update(s, s->l1_table_offset, | |
728 | l1_size * sizeof(uint64_t)); | |
729 | qcow2_create_refcount_update(s, s->refcount_table_offset, s->cluster_size); | |
730 | qcow2_create_refcount_update(s, s->refcount_block_offset, | |
731 | ref_clusters * s->cluster_size); | |
3b46e624 | 732 | |
585f8587 FB |
733 | /* write all the data */ |
734 | write(fd, &header, sizeof(header)); | |
735 | if (backing_file) { | |
f965509c AL |
736 | if (backing_format_len) { |
737 | char zero[16]; | |
738 | int d = ext_bf.len - backing_format_len; | |
739 | ||
740 | memset(zero, 0, sizeof(zero)); | |
741 | cpu_to_be32s(&ext_bf.magic); | |
742 | cpu_to_be32s(&ext_bf.len); | |
743 | write(fd, &ext_bf, sizeof(ext_bf)); | |
744 | write(fd, backing_format, backing_format_len); | |
745 | if (d>0) { | |
746 | write(fd, zero, d); | |
747 | } | |
748 | } | |
585f8587 FB |
749 | write(fd, backing_file, backing_filename_len); |
750 | } | |
751 | lseek(fd, s->l1_table_offset, SEEK_SET); | |
752 | tmp = 0; | |
753 | for(i = 0;i < l1_size; i++) { | |
754 | write(fd, &tmp, sizeof(tmp)); | |
755 | } | |
756 | lseek(fd, s->refcount_table_offset, SEEK_SET); | |
757 | write(fd, s->refcount_table, s->cluster_size); | |
3b46e624 | 758 | |
585f8587 | 759 | lseek(fd, s->refcount_block_offset, SEEK_SET); |
2d2431f0 | 760 | write(fd, s->refcount_block, ref_clusters * s->cluster_size); |
585f8587 FB |
761 | |
762 | qemu_free(s->refcount_table); | |
763 | qemu_free(s->refcount_block); | |
764 | close(fd); | |
765 | return 0; | |
585f8587 FB |
766 | } |
767 | ||
0e7e1989 KW |
768 | static int qcow_create(const char *filename, QEMUOptionParameter *options) |
769 | { | |
770 | const char *backing_file = NULL; | |
771 | const char *backing_fmt = NULL; | |
772 | uint64_t sectors = 0; | |
773 | int flags = 0; | |
9ccb258e | 774 | size_t cluster_size = 65536; |
0e7e1989 KW |
775 | |
776 | /* Read out options */ | |
777 | while (options && options->name) { | |
778 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
779 | sectors = options->value.n / 512; | |
780 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { | |
781 | backing_file = options->value.s; | |
782 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { | |
783 | backing_fmt = options->value.s; | |
784 | } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) { | |
785 | flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0; | |
73c632ed KW |
786 | } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { |
787 | if (options->value.n) { | |
788 | cluster_size = options->value.n; | |
789 | } | |
0e7e1989 KW |
790 | } |
791 | options++; | |
792 | } | |
793 | ||
73c632ed KW |
794 | return qcow_create2(filename, sectors, backing_file, backing_fmt, flags, |
795 | cluster_size); | |
f965509c AL |
796 | } |
797 | ||
585f8587 FB |
798 | static int qcow_make_empty(BlockDriverState *bs) |
799 | { | |
800 | #if 0 | |
801 | /* XXX: not correct */ | |
802 | BDRVQcowState *s = bs->opaque; | |
803 | uint32_t l1_length = s->l1_size * sizeof(uint64_t); | |
804 | int ret; | |
805 | ||
806 | memset(s->l1_table, 0, l1_length); | |
807 | if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0) | |
ac674887 | 808 | return -1; |
585f8587 FB |
809 | ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length); |
810 | if (ret < 0) | |
811 | return ret; | |
3b46e624 | 812 | |
585f8587 FB |
813 | l2_cache_reset(bs); |
814 | #endif | |
815 | return 0; | |
816 | } | |
817 | ||
818 | /* XXX: put compressed sectors first, then all the cluster aligned | |
819 | tables to avoid losing bytes in alignment */ | |
5fafdf24 | 820 | static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, |
585f8587 FB |
821 | const uint8_t *buf, int nb_sectors) |
822 | { | |
823 | BDRVQcowState *s = bs->opaque; | |
824 | z_stream strm; | |
825 | int ret, out_len; | |
826 | uint8_t *out_buf; | |
827 | uint64_t cluster_offset; | |
828 | ||
829 | if (nb_sectors == 0) { | |
830 | /* align end of file to a sector boundary to ease reading with | |
831 | sector based I/Os */ | |
832 | cluster_offset = bdrv_getlength(s->hd); | |
833 | cluster_offset = (cluster_offset + 511) & ~511; | |
834 | bdrv_truncate(s->hd, cluster_offset); | |
835 | return 0; | |
836 | } | |
837 | ||
838 | if (nb_sectors != s->cluster_sectors) | |
839 | return -EINVAL; | |
840 | ||
841 | out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); | |
585f8587 FB |
842 | |
843 | /* best compression, small window, no zlib header */ | |
844 | memset(&strm, 0, sizeof(strm)); | |
845 | ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, | |
5fafdf24 | 846 | Z_DEFLATED, -12, |
585f8587 FB |
847 | 9, Z_DEFAULT_STRATEGY); |
848 | if (ret != 0) { | |
849 | qemu_free(out_buf); | |
850 | return -1; | |
851 | } | |
852 | ||
853 | strm.avail_in = s->cluster_size; | |
854 | strm.next_in = (uint8_t *)buf; | |
855 | strm.avail_out = s->cluster_size; | |
856 | strm.next_out = out_buf; | |
857 | ||
858 | ret = deflate(&strm, Z_FINISH); | |
859 | if (ret != Z_STREAM_END && ret != Z_OK) { | |
860 | qemu_free(out_buf); | |
861 | deflateEnd(&strm); | |
862 | return -1; | |
863 | } | |
864 | out_len = strm.next_out - out_buf; | |
865 | ||
866 | deflateEnd(&strm); | |
867 | ||
868 | if (ret != Z_STREAM_END || out_len >= s->cluster_size) { | |
869 | /* could not compress: write normal cluster */ | |
ade40677 | 870 | bdrv_write(bs, sector_num, buf, s->cluster_sectors); |
585f8587 | 871 | } else { |
ed6ccf0f KW |
872 | cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, |
873 | sector_num << 9, out_len); | |
52d893ec AL |
874 | if (!cluster_offset) |
875 | return -1; | |
585f8587 FB |
876 | cluster_offset &= s->cluster_offset_mask; |
877 | if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) { | |
878 | qemu_free(out_buf); | |
879 | return -1; | |
880 | } | |
881 | } | |
3b46e624 | 882 | |
585f8587 FB |
883 | qemu_free(out_buf); |
884 | return 0; | |
885 | } | |
886 | ||
887 | static void qcow_flush(BlockDriverState *bs) | |
888 | { | |
889 | BDRVQcowState *s = bs->opaque; | |
890 | bdrv_flush(s->hd); | |
891 | } | |
892 | ||
45566e9c CH |
893 | static int64_t qcow_vm_state_offset(BDRVQcowState *s) |
894 | { | |
895 | return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); | |
896 | } | |
897 | ||
585f8587 FB |
898 | static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
899 | { | |
900 | BDRVQcowState *s = bs->opaque; | |
901 | bdi->cluster_size = s->cluster_size; | |
45566e9c | 902 | bdi->vm_state_offset = qcow_vm_state_offset(s); |
585f8587 FB |
903 | return 0; |
904 | } | |
905 | ||
585f8587 | 906 | |
e97fc193 AL |
907 | static int qcow_check(BlockDriverState *bs) |
908 | { | |
ed6ccf0f | 909 | return qcow2_check_refcounts(bs); |
585f8587 FB |
910 | } |
911 | ||
912 | #if 0 | |
913 | static void dump_refcounts(BlockDriverState *bs) | |
914 | { | |
915 | BDRVQcowState *s = bs->opaque; | |
916 | int64_t nb_clusters, k, k1, size; | |
917 | int refcount; | |
918 | ||
919 | size = bdrv_getlength(s->hd); | |
6db6c638 | 920 | nb_clusters = size_to_clusters(s, size); |
585f8587 FB |
921 | for(k = 0; k < nb_clusters;) { |
922 | k1 = k; | |
923 | refcount = get_refcount(bs, k); | |
924 | k++; | |
925 | while (k < nb_clusters && get_refcount(bs, k) == refcount) | |
926 | k++; | |
927 | printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1); | |
928 | } | |
929 | } | |
930 | #endif | |
585f8587 | 931 | |
45566e9c | 932 | static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
178e08a5 AL |
933 | int64_t pos, int size) |
934 | { | |
45566e9c | 935 | BDRVQcowState *s = bs->opaque; |
178e08a5 AL |
936 | int growable = bs->growable; |
937 | ||
938 | bs->growable = 1; | |
45566e9c | 939 | bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size); |
178e08a5 AL |
940 | bs->growable = growable; |
941 | ||
942 | return size; | |
943 | } | |
944 | ||
45566e9c | 945 | static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
178e08a5 AL |
946 | int64_t pos, int size) |
947 | { | |
45566e9c | 948 | BDRVQcowState *s = bs->opaque; |
178e08a5 AL |
949 | int growable = bs->growable; |
950 | int ret; | |
951 | ||
952 | bs->growable = 1; | |
45566e9c | 953 | ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size); |
178e08a5 AL |
954 | bs->growable = growable; |
955 | ||
956 | return ret; | |
957 | } | |
958 | ||
0e7e1989 | 959 | static QEMUOptionParameter qcow_create_options[] = { |
db08adf5 KW |
960 | { |
961 | .name = BLOCK_OPT_SIZE, | |
962 | .type = OPT_SIZE, | |
963 | .help = "Virtual disk size" | |
964 | }, | |
965 | { | |
966 | .name = BLOCK_OPT_BACKING_FILE, | |
967 | .type = OPT_STRING, | |
968 | .help = "File name of a base image" | |
969 | }, | |
970 | { | |
971 | .name = BLOCK_OPT_BACKING_FMT, | |
972 | .type = OPT_STRING, | |
973 | .help = "Image format of the base image" | |
974 | }, | |
975 | { | |
976 | .name = BLOCK_OPT_ENCRYPT, | |
977 | .type = OPT_FLAG, | |
978 | .help = "Encrypt the image" | |
979 | }, | |
980 | { | |
981 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
982 | .type = OPT_SIZE, | |
983 | .help = "qcow2 cluster size" | |
984 | }, | |
0e7e1989 KW |
985 | { NULL } |
986 | }; | |
987 | ||
5efa9d5a | 988 | static BlockDriver bdrv_qcow2 = { |
e60f469c AJ |
989 | .format_name = "qcow2", |
990 | .instance_size = sizeof(BDRVQcowState), | |
991 | .bdrv_probe = qcow_probe, | |
992 | .bdrv_open = qcow_open, | |
993 | .bdrv_close = qcow_close, | |
994 | .bdrv_create = qcow_create, | |
995 | .bdrv_flush = qcow_flush, | |
996 | .bdrv_is_allocated = qcow_is_allocated, | |
997 | .bdrv_set_key = qcow_set_key, | |
998 | .bdrv_make_empty = qcow_make_empty, | |
999 | ||
f141eafe AL |
1000 | .bdrv_aio_readv = qcow_aio_readv, |
1001 | .bdrv_aio_writev = qcow_aio_writev, | |
585f8587 FB |
1002 | .bdrv_write_compressed = qcow_write_compressed, |
1003 | ||
ed6ccf0f KW |
1004 | .bdrv_snapshot_create = qcow2_snapshot_create, |
1005 | .bdrv_snapshot_goto = qcow2_snapshot_goto, | |
1006 | .bdrv_snapshot_delete = qcow2_snapshot_delete, | |
1007 | .bdrv_snapshot_list = qcow2_snapshot_list, | |
e60f469c | 1008 | .bdrv_get_info = qcow_get_info, |
f965509c | 1009 | |
45566e9c CH |
1010 | .bdrv_save_vmstate = qcow_save_vmstate, |
1011 | .bdrv_load_vmstate = qcow_load_vmstate, | |
178e08a5 | 1012 | |
0e7e1989 | 1013 | .create_options = qcow_create_options, |
e97fc193 | 1014 | .bdrv_check = qcow_check, |
585f8587 | 1015 | }; |
5efa9d5a AL |
1016 | |
1017 | static void bdrv_qcow2_init(void) | |
1018 | { | |
1019 | bdrv_register(&bdrv_qcow2); | |
1020 | } | |
1021 | ||
1022 | block_init(bdrv_qcow2_init); |