]>
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" |
a9420734 | 30 | #include "qemu-error.h" |
585f8587 FB |
31 | |
32 | /* | |
33 | Differences with QCOW: | |
34 | ||
35 | - Support for multiple incremental snapshots. | |
36 | - Memory management by reference counts. | |
37 | - Clusters which have a reference count of one have the bit | |
38 | QCOW_OFLAG_COPIED to optimize write performance. | |
5fafdf24 | 39 | - Size of compressed clusters is stored in sectors to reduce bit usage |
585f8587 FB |
40 | in the cluster offsets. |
41 | - Support for storing additional data (such as the VM state) in the | |
3b46e624 | 42 | snapshots. |
585f8587 FB |
43 | - If a backing store is used, the cluster size is not constrained |
44 | (could be backported to QCOW). | |
45 | - L2 tables have always a size of one cluster. | |
46 | */ | |
47 | ||
9b80ddf3 AL |
48 | |
49 | typedef struct { | |
50 | uint32_t magic; | |
51 | uint32_t len; | |
52 | } QCowExtension; | |
53 | #define QCOW_EXT_MAGIC_END 0 | |
f965509c | 54 | #define QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA |
9b80ddf3 | 55 | |
585f8587 FB |
56 | static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) |
57 | { | |
58 | const QCowHeader *cow_header = (const void *)buf; | |
3b46e624 | 59 | |
585f8587 FB |
60 | if (buf_size >= sizeof(QCowHeader) && |
61 | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && | |
5fafdf24 | 62 | be32_to_cpu(cow_header->version) == QCOW_VERSION) |
585f8587 FB |
63 | return 100; |
64 | else | |
65 | return 0; | |
66 | } | |
67 | ||
9b80ddf3 AL |
68 | |
69 | /* | |
70 | * read qcow2 extension and fill bs | |
71 | * start reading from start_offset | |
72 | * finish reading upon magic of value 0 or when end_offset reached | |
73 | * unknown magic is skipped (future extension this version knows nothing about) | |
74 | * return 0 upon success, non-0 otherwise | |
75 | */ | |
76 | static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset, | |
77 | uint64_t end_offset) | |
78 | { | |
9b80ddf3 AL |
79 | QCowExtension ext; |
80 | uint64_t offset; | |
81 | ||
82 | #ifdef DEBUG_EXT | |
83 | printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); | |
84 | #endif | |
85 | offset = start_offset; | |
86 | while (offset < end_offset) { | |
87 | ||
88 | #ifdef DEBUG_EXT | |
89 | /* Sanity check */ | |
90 | if (offset > s->cluster_size) | |
91 | printf("qcow_handle_extension: suspicious offset %lu\n", offset); | |
92 | ||
93 | printf("attemting to read extended header in offset %lu\n", offset); | |
94 | #endif | |
95 | ||
66f82cee | 96 | if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) { |
0bfcd599 BS |
97 | fprintf(stderr, "qcow_handle_extension: ERROR: " |
98 | "pread fail from offset %" PRIu64 "\n", | |
99 | offset); | |
9b80ddf3 AL |
100 | return 1; |
101 | } | |
102 | be32_to_cpus(&ext.magic); | |
103 | be32_to_cpus(&ext.len); | |
104 | offset += sizeof(ext); | |
105 | #ifdef DEBUG_EXT | |
106 | printf("ext.magic = 0x%x\n", ext.magic); | |
107 | #endif | |
108 | switch (ext.magic) { | |
109 | case QCOW_EXT_MAGIC_END: | |
110 | return 0; | |
f965509c AL |
111 | |
112 | case QCOW_EXT_MAGIC_BACKING_FORMAT: | |
113 | if (ext.len >= sizeof(bs->backing_format)) { | |
114 | fprintf(stderr, "ERROR: ext_backing_format: len=%u too large" | |
4c978075 | 115 | " (>=%zu)\n", |
f965509c AL |
116 | ext.len, sizeof(bs->backing_format)); |
117 | return 2; | |
118 | } | |
66f82cee | 119 | if (bdrv_pread(bs->file, offset , bs->backing_format, |
f965509c AL |
120 | ext.len) != ext.len) |
121 | return 3; | |
122 | bs->backing_format[ext.len] = '\0'; | |
123 | #ifdef DEBUG_EXT | |
124 | printf("Qcow2: Got format extension %s\n", bs->backing_format); | |
125 | #endif | |
e1c7f0e3 | 126 | offset = ((offset + ext.len + 7) & ~7); |
f965509c AL |
127 | break; |
128 | ||
9b80ddf3 AL |
129 | default: |
130 | /* unknown magic -- just skip it */ | |
e1c7f0e3 | 131 | offset = ((offset + ext.len + 7) & ~7); |
9b80ddf3 AL |
132 | break; |
133 | } | |
134 | } | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
139 | ||
66f82cee | 140 | static int qcow_open(BlockDriverState *bs, int flags) |
585f8587 FB |
141 | { |
142 | BDRVQcowState *s = bs->opaque; | |
419b19d9 | 143 | int len, i; |
585f8587 | 144 | QCowHeader header; |
9b80ddf3 | 145 | uint64_t ext_end; |
585f8587 | 146 | |
66f82cee | 147 | if (bdrv_pread(bs->file, 0, &header, sizeof(header)) != sizeof(header)) |
585f8587 FB |
148 | goto fail; |
149 | be32_to_cpus(&header.magic); | |
150 | be32_to_cpus(&header.version); | |
151 | be64_to_cpus(&header.backing_file_offset); | |
152 | be32_to_cpus(&header.backing_file_size); | |
153 | be64_to_cpus(&header.size); | |
154 | be32_to_cpus(&header.cluster_bits); | |
155 | be32_to_cpus(&header.crypt_method); | |
156 | be64_to_cpus(&header.l1_table_offset); | |
157 | be32_to_cpus(&header.l1_size); | |
158 | be64_to_cpus(&header.refcount_table_offset); | |
159 | be32_to_cpus(&header.refcount_table_clusters); | |
160 | be64_to_cpus(&header.snapshots_offset); | |
161 | be32_to_cpus(&header.nb_snapshots); | |
3b46e624 | 162 | |
585f8587 FB |
163 | if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION) |
164 | goto fail; | |
d191d12d | 165 | if (header.cluster_bits < MIN_CLUSTER_BITS || |
73c632ed | 166 | header.cluster_bits > MAX_CLUSTER_BITS) |
585f8587 FB |
167 | goto fail; |
168 | if (header.crypt_method > QCOW_CRYPT_AES) | |
169 | goto fail; | |
170 | s->crypt_method_header = header.crypt_method; | |
171 | if (s->crypt_method_header) | |
172 | bs->encrypted = 1; | |
173 | s->cluster_bits = header.cluster_bits; | |
174 | s->cluster_size = 1 << s->cluster_bits; | |
175 | s->cluster_sectors = 1 << (s->cluster_bits - 9); | |
176 | s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ | |
177 | s->l2_size = 1 << s->l2_bits; | |
178 | bs->total_sectors = header.size / 512; | |
179 | s->csize_shift = (62 - (s->cluster_bits - 8)); | |
180 | s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; | |
181 | s->cluster_offset_mask = (1LL << s->csize_shift) - 1; | |
182 | s->refcount_table_offset = header.refcount_table_offset; | |
5fafdf24 | 183 | s->refcount_table_size = |
585f8587 FB |
184 | header.refcount_table_clusters << (s->cluster_bits - 3); |
185 | ||
186 | s->snapshots_offset = header.snapshots_offset; | |
187 | s->nb_snapshots = header.nb_snapshots; | |
188 | ||
189 | /* read the level 1 table */ | |
190 | s->l1_size = header.l1_size; | |
419b19d9 | 191 | s->l1_vm_state_index = size_to_l1(s, header.size); |
585f8587 FB |
192 | /* the L1 table must contain at least enough entries to put |
193 | header.size bytes */ | |
194 | if (s->l1_size < s->l1_vm_state_index) | |
195 | goto fail; | |
196 | s->l1_table_offset = header.l1_table_offset; | |
d191d12d SW |
197 | if (s->l1_size > 0) { |
198 | s->l1_table = qemu_mallocz( | |
199 | align_offset(s->l1_size * sizeof(uint64_t), 512)); | |
66f82cee | 200 | if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
d191d12d SW |
201 | s->l1_size * sizeof(uint64_t)) |
202 | goto fail; | |
203 | for(i = 0;i < s->l1_size; i++) { | |
204 | be64_to_cpus(&s->l1_table[i]); | |
205 | } | |
585f8587 FB |
206 | } |
207 | /* alloc L2 cache */ | |
208 | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); | |
585f8587 | 209 | s->cluster_cache = qemu_malloc(s->cluster_size); |
585f8587 | 210 | /* one more sector for decompressed data alignment */ |
095a9c58 AL |
211 | s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size |
212 | + 512); | |
585f8587 | 213 | s->cluster_cache_offset = -1; |
3b46e624 | 214 | |
ed6ccf0f | 215 | if (qcow2_refcount_init(bs) < 0) |
585f8587 FB |
216 | goto fail; |
217 | ||
72cf2d4f | 218 | QLIST_INIT(&s->cluster_allocs); |
f214978a | 219 | |
9b80ddf3 AL |
220 | /* read qcow2 extensions */ |
221 | if (header.backing_file_offset) | |
222 | ext_end = header.backing_file_offset; | |
223 | else | |
224 | ext_end = s->cluster_size; | |
225 | if (qcow_read_extensions(bs, sizeof(header), ext_end)) | |
226 | goto fail; | |
227 | ||
585f8587 FB |
228 | /* read the backing file name */ |
229 | if (header.backing_file_offset != 0) { | |
230 | len = header.backing_file_size; | |
231 | if (len > 1023) | |
232 | len = 1023; | |
66f82cee | 233 | if (bdrv_pread(bs->file, header.backing_file_offset, bs->backing_file, len) != len) |
585f8587 FB |
234 | goto fail; |
235 | bs->backing_file[len] = '\0'; | |
236 | } | |
ed6ccf0f | 237 | if (qcow2_read_snapshots(bs) < 0) |
585f8587 FB |
238 | goto fail; |
239 | ||
240 | #ifdef DEBUG_ALLOC | |
14899cdf | 241 | qcow2_check_refcounts(bs); |
585f8587 FB |
242 | #endif |
243 | return 0; | |
244 | ||
245 | fail: | |
ed6ccf0f KW |
246 | qcow2_free_snapshots(bs); |
247 | qcow2_refcount_close(bs); | |
585f8587 FB |
248 | qemu_free(s->l1_table); |
249 | qemu_free(s->l2_cache); | |
250 | qemu_free(s->cluster_cache); | |
251 | qemu_free(s->cluster_data); | |
585f8587 FB |
252 | return -1; |
253 | } | |
254 | ||
255 | static int qcow_set_key(BlockDriverState *bs, const char *key) | |
256 | { | |
257 | BDRVQcowState *s = bs->opaque; | |
258 | uint8_t keybuf[16]; | |
259 | int len, i; | |
3b46e624 | 260 | |
585f8587 FB |
261 | memset(keybuf, 0, 16); |
262 | len = strlen(key); | |
263 | if (len > 16) | |
264 | len = 16; | |
265 | /* XXX: we could compress the chars to 7 bits to increase | |
266 | entropy */ | |
267 | for(i = 0;i < len;i++) { | |
268 | keybuf[i] = key[i]; | |
269 | } | |
270 | s->crypt_method = s->crypt_method_header; | |
271 | ||
272 | if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) | |
273 | return -1; | |
274 | if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) | |
275 | return -1; | |
276 | #if 0 | |
277 | /* test */ | |
278 | { | |
279 | uint8_t in[16]; | |
280 | uint8_t out[16]; | |
281 | uint8_t tmp[16]; | |
282 | for(i=0;i<16;i++) | |
283 | in[i] = i; | |
284 | AES_encrypt(in, tmp, &s->aes_encrypt_key); | |
285 | AES_decrypt(tmp, out, &s->aes_decrypt_key); | |
286 | for(i = 0; i < 16; i++) | |
287 | printf(" %02x", tmp[i]); | |
288 | printf("\n"); | |
289 | for(i = 0; i < 16; i++) | |
290 | printf(" %02x", out[i]); | |
291 | printf("\n"); | |
292 | } | |
293 | #endif | |
294 | return 0; | |
295 | } | |
296 | ||
5fafdf24 | 297 | static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
585f8587 FB |
298 | int nb_sectors, int *pnum) |
299 | { | |
585f8587 | 300 | uint64_t cluster_offset; |
1c46efaa | 301 | int ret; |
585f8587 | 302 | |
095a9c58 | 303 | *pnum = nb_sectors; |
1c46efaa KW |
304 | /* FIXME We can get errors here, but the bdrv_is_allocated interface can't |
305 | * pass them on today */ | |
306 | ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset); | |
307 | if (ret < 0) { | |
308 | *pnum = 0; | |
309 | } | |
095a9c58 | 310 | |
585f8587 FB |
311 | return (cluster_offset != 0); |
312 | } | |
313 | ||
a9465922 | 314 | /* handle reading after the end of the backing file */ |
bd28f835 KW |
315 | int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, |
316 | int64_t sector_num, int nb_sectors) | |
a9465922 FB |
317 | { |
318 | int n1; | |
319 | if ((sector_num + nb_sectors) <= bs->total_sectors) | |
320 | return nb_sectors; | |
321 | if (sector_num >= bs->total_sectors) | |
322 | n1 = 0; | |
323 | else | |
324 | n1 = bs->total_sectors - sector_num; | |
bd28f835 KW |
325 | |
326 | qemu_iovec_memset(qiov, 0, 512 * (nb_sectors - n1)); | |
327 | ||
a9465922 FB |
328 | return n1; |
329 | } | |
330 | ||
ce1a14dc PB |
331 | typedef struct QCowAIOCB { |
332 | BlockDriverAIOCB common; | |
585f8587 | 333 | int64_t sector_num; |
f141eafe | 334 | QEMUIOVector *qiov; |
7b88e48b CH |
335 | int remaining_sectors; |
336 | int cur_nr_sectors; /* number of sectors in current iteration */ | |
bd28f835 | 337 | uint64_t bytes_done; |
585f8587 | 338 | uint64_t cluster_offset; |
5fafdf24 | 339 | uint8_t *cluster_data; |
585f8587 | 340 | BlockDriverAIOCB *hd_aiocb; |
c87c0672 | 341 | QEMUIOVector hd_qiov; |
1490791f | 342 | QEMUBH *bh; |
e976c6a1 | 343 | QCowL2Meta l2meta; |
72cf2d4f | 344 | QLIST_ENTRY(QCowAIOCB) next_depend; |
585f8587 FB |
345 | } QCowAIOCB; |
346 | ||
c16b5a2c CH |
347 | static void qcow_aio_cancel(BlockDriverAIOCB *blockacb) |
348 | { | |
b666d239 | 349 | QCowAIOCB *acb = container_of(blockacb, QCowAIOCB, common); |
c16b5a2c CH |
350 | if (acb->hd_aiocb) |
351 | bdrv_aio_cancel(acb->hd_aiocb); | |
352 | qemu_aio_release(acb); | |
353 | } | |
354 | ||
355 | static AIOPool qcow_aio_pool = { | |
356 | .aiocb_size = sizeof(QCowAIOCB), | |
357 | .cancel = qcow_aio_cancel, | |
358 | }; | |
359 | ||
1490791f AL |
360 | static void qcow_aio_read_cb(void *opaque, int ret); |
361 | static void qcow_aio_read_bh(void *opaque) | |
362 | { | |
363 | QCowAIOCB *acb = opaque; | |
364 | qemu_bh_delete(acb->bh); | |
365 | acb->bh = NULL; | |
366 | qcow_aio_read_cb(opaque, 0); | |
367 | } | |
368 | ||
a32ef786 AL |
369 | static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb) |
370 | { | |
371 | if (acb->bh) | |
372 | return -EIO; | |
373 | ||
374 | acb->bh = qemu_bh_new(cb, acb); | |
375 | if (!acb->bh) | |
376 | return -EIO; | |
377 | ||
378 | qemu_bh_schedule(acb->bh); | |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
585f8587 FB |
383 | static void qcow_aio_read_cb(void *opaque, int ret) |
384 | { | |
ce1a14dc PB |
385 | QCowAIOCB *acb = opaque; |
386 | BlockDriverState *bs = acb->common.bs; | |
585f8587 | 387 | BDRVQcowState *s = bs->opaque; |
a9465922 | 388 | int index_in_cluster, n1; |
585f8587 | 389 | |
ce1a14dc | 390 | acb->hd_aiocb = NULL; |
f141eafe AL |
391 | if (ret < 0) |
392 | goto done; | |
585f8587 | 393 | |
585f8587 | 394 | /* post process the read buffer */ |
ce1a14dc | 395 | if (!acb->cluster_offset) { |
585f8587 | 396 | /* nothing to do */ |
ce1a14dc | 397 | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
585f8587 FB |
398 | /* nothing to do */ |
399 | } else { | |
400 | if (s->crypt_method) { | |
bd28f835 KW |
401 | qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data, |
402 | acb->cluster_data, acb->cur_nr_sectors, 0, &s->aes_decrypt_key); | |
403 | qemu_iovec_reset(&acb->hd_qiov); | |
404 | qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done, | |
405 | acb->cur_nr_sectors * 512); | |
406 | qemu_iovec_from_buffer(&acb->hd_qiov, acb->cluster_data, | |
407 | 512 * acb->cur_nr_sectors); | |
585f8587 FB |
408 | } |
409 | } | |
410 | ||
7b88e48b CH |
411 | acb->remaining_sectors -= acb->cur_nr_sectors; |
412 | acb->sector_num += acb->cur_nr_sectors; | |
bd28f835 | 413 | acb->bytes_done += acb->cur_nr_sectors * 512; |
585f8587 | 414 | |
7b88e48b | 415 | if (acb->remaining_sectors == 0) { |
585f8587 | 416 | /* request completed */ |
f141eafe AL |
417 | ret = 0; |
418 | goto done; | |
585f8587 | 419 | } |
3b46e624 | 420 | |
585f8587 | 421 | /* prepare next AIO request */ |
7b88e48b | 422 | acb->cur_nr_sectors = acb->remaining_sectors; |
bd28f835 KW |
423 | if (s->crypt_method) { |
424 | acb->cur_nr_sectors = MIN(acb->cur_nr_sectors, | |
425 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); | |
426 | } | |
427 | ||
1c46efaa KW |
428 | ret = qcow2_get_cluster_offset(bs, acb->sector_num << 9, |
429 | &acb->cur_nr_sectors, &acb->cluster_offset); | |
430 | if (ret < 0) { | |
431 | goto done; | |
432 | } | |
433 | ||
ce1a14dc | 434 | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); |
ce1a14dc | 435 | |
bd28f835 KW |
436 | qemu_iovec_reset(&acb->hd_qiov); |
437 | qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done, | |
438 | acb->cur_nr_sectors * 512); | |
439 | ||
ce1a14dc | 440 | if (!acb->cluster_offset) { |
bd28f835 | 441 | |
585f8587 FB |
442 | if (bs->backing_hd) { |
443 | /* read from the base image */ | |
bd28f835 KW |
444 | n1 = qcow2_backing_read1(bs->backing_hd, &acb->hd_qiov, |
445 | acb->sector_num, acb->cur_nr_sectors); | |
a9465922 | 446 | if (n1 > 0) { |
66f82cee | 447 | BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); |
c87c0672 | 448 | acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num, |
7b88e48b | 449 | &acb->hd_qiov, acb->cur_nr_sectors, |
c87c0672 | 450 | qcow_aio_read_cb, acb); |
ce1a14dc | 451 | if (acb->hd_aiocb == NULL) |
f141eafe | 452 | goto done; |
a9465922 | 453 | } else { |
a32ef786 AL |
454 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
455 | if (ret < 0) | |
f141eafe | 456 | goto done; |
a9465922 | 457 | } |
585f8587 FB |
458 | } else { |
459 | /* Note: in this case, no need to wait */ | |
bd28f835 | 460 | qemu_iovec_memset(&acb->hd_qiov, 0, 512 * acb->cur_nr_sectors); |
a32ef786 AL |
461 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
462 | if (ret < 0) | |
f141eafe | 463 | goto done; |
585f8587 | 464 | } |
ce1a14dc | 465 | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
585f8587 | 466 | /* add AIO support for compressed blocks ? */ |
66f82cee | 467 | if (qcow2_decompress_cluster(bs, acb->cluster_offset) < 0) |
f141eafe | 468 | goto done; |
bd28f835 KW |
469 | |
470 | qemu_iovec_from_buffer(&acb->hd_qiov, | |
471 | s->cluster_cache + index_in_cluster * 512, | |
472 | 512 * acb->cur_nr_sectors); | |
473 | ||
a32ef786 AL |
474 | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
475 | if (ret < 0) | |
f141eafe | 476 | goto done; |
585f8587 | 477 | } else { |
ce1a14dc | 478 | if ((acb->cluster_offset & 511) != 0) { |
585f8587 | 479 | ret = -EIO; |
f141eafe | 480 | goto done; |
585f8587 | 481 | } |
c87c0672 | 482 | |
bd28f835 KW |
483 | if (s->crypt_method) { |
484 | /* | |
485 | * For encrypted images, read everything into a temporary | |
486 | * contiguous buffer on which the AES functions can work. | |
487 | */ | |
488 | if (!acb->cluster_data) { | |
489 | acb->cluster_data = | |
490 | qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); | |
491 | } | |
492 | ||
493 | assert(acb->cur_nr_sectors <= | |
494 | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); | |
495 | qemu_iovec_reset(&acb->hd_qiov); | |
496 | qemu_iovec_add(&acb->hd_qiov, acb->cluster_data, | |
497 | 512 * acb->cur_nr_sectors); | |
498 | } | |
499 | ||
66f82cee KW |
500 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); |
501 | acb->hd_aiocb = bdrv_aio_readv(bs->file, | |
5fafdf24 | 502 | (acb->cluster_offset >> 9) + index_in_cluster, |
7b88e48b CH |
503 | &acb->hd_qiov, acb->cur_nr_sectors, |
504 | qcow_aio_read_cb, acb); | |
171e3d6b KW |
505 | if (acb->hd_aiocb == NULL) { |
506 | ret = -EIO; | |
f141eafe | 507 | goto done; |
171e3d6b | 508 | } |
f141eafe AL |
509 | } |
510 | ||
511 | return; | |
512 | done: | |
f141eafe | 513 | acb->common.cb(acb->common.opaque, ret); |
bd28f835 | 514 | qemu_iovec_destroy(&acb->hd_qiov); |
f141eafe | 515 | qemu_aio_release(acb); |
585f8587 FB |
516 | } |
517 | ||
ce1a14dc | 518 | static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs, |
f141eafe AL |
519 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, |
520 | BlockDriverCompletionFunc *cb, void *opaque, int is_write) | |
585f8587 | 521 | { |
ce1a14dc PB |
522 | QCowAIOCB *acb; |
523 | ||
c16b5a2c | 524 | acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque); |
ce1a14dc PB |
525 | if (!acb) |
526 | return NULL; | |
527 | acb->hd_aiocb = NULL; | |
528 | acb->sector_num = sector_num; | |
f141eafe | 529 | acb->qiov = qiov; |
bd28f835 | 530 | |
6f5f060b | 531 | qemu_iovec_init(&acb->hd_qiov, qiov->niov); |
bd28f835 KW |
532 | |
533 | acb->bytes_done = 0; | |
7b88e48b CH |
534 | acb->remaining_sectors = nb_sectors; |
535 | acb->cur_nr_sectors = 0; | |
ce1a14dc | 536 | acb->cluster_offset = 0; |
e976c6a1 | 537 | acb->l2meta.nb_clusters = 0; |
72cf2d4f | 538 | QLIST_INIT(&acb->l2meta.dependent_requests); |
ce1a14dc PB |
539 | return acb; |
540 | } | |
541 | ||
f141eafe AL |
542 | static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs, |
543 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc PB |
544 | BlockDriverCompletionFunc *cb, void *opaque) |
545 | { | |
546 | QCowAIOCB *acb; | |
547 | ||
f141eafe | 548 | acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
ce1a14dc PB |
549 | if (!acb) |
550 | return NULL; | |
585f8587 FB |
551 | |
552 | qcow_aio_read_cb(acb, 0); | |
ce1a14dc | 553 | return &acb->common; |
585f8587 FB |
554 | } |
555 | ||
f214978a KW |
556 | static void qcow_aio_write_cb(void *opaque, int ret); |
557 | ||
558 | static void run_dependent_requests(QCowL2Meta *m) | |
559 | { | |
560 | QCowAIOCB *req; | |
561 | QCowAIOCB *next; | |
562 | ||
563 | /* Take the request off the list of running requests */ | |
564 | if (m->nb_clusters != 0) { | |
72cf2d4f | 565 | QLIST_REMOVE(m, next_in_flight); |
f214978a KW |
566 | } |
567 | ||
d4c146f0 SH |
568 | /* Restart all dependent requests */ |
569 | QLIST_FOREACH_SAFE(req, &m->dependent_requests, next_depend, next) { | |
f214978a KW |
570 | qcow_aio_write_cb(req, 0); |
571 | } | |
572 | ||
573 | /* Empty the list for the next part of the request */ | |
72cf2d4f | 574 | QLIST_INIT(&m->dependent_requests); |
f214978a KW |
575 | } |
576 | ||
585f8587 FB |
577 | static void qcow_aio_write_cb(void *opaque, int ret) |
578 | { | |
ce1a14dc PB |
579 | QCowAIOCB *acb = opaque; |
580 | BlockDriverState *bs = acb->common.bs; | |
585f8587 | 581 | BDRVQcowState *s = bs->opaque; |
585f8587 | 582 | int index_in_cluster; |
095a9c58 | 583 | int n_end; |
ce1a14dc PB |
584 | |
585 | acb->hd_aiocb = NULL; | |
586 | ||
f214978a | 587 | if (ret >= 0) { |
148da7ea | 588 | ret = qcow2_alloc_cluster_link_l2(bs, &acb->l2meta); |
f214978a KW |
589 | } |
590 | ||
591 | run_dependent_requests(&acb->l2meta); | |
592 | ||
f141eafe AL |
593 | if (ret < 0) |
594 | goto done; | |
585f8587 | 595 | |
7b88e48b CH |
596 | acb->remaining_sectors -= acb->cur_nr_sectors; |
597 | acb->sector_num += acb->cur_nr_sectors; | |
6f5f060b | 598 | acb->bytes_done += acb->cur_nr_sectors * 512; |
585f8587 | 599 | |
7b88e48b | 600 | if (acb->remaining_sectors == 0) { |
585f8587 | 601 | /* request completed */ |
f141eafe AL |
602 | ret = 0; |
603 | goto done; | |
585f8587 | 604 | } |
3b46e624 | 605 | |
ce1a14dc | 606 | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); |
7b88e48b | 607 | n_end = index_in_cluster + acb->remaining_sectors; |
095a9c58 AL |
608 | if (s->crypt_method && |
609 | n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) | |
610 | n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; | |
611 | ||
148da7ea | 612 | ret = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9, |
7b88e48b | 613 | index_in_cluster, n_end, &acb->cur_nr_sectors, &acb->l2meta); |
148da7ea KW |
614 | if (ret < 0) { |
615 | goto done; | |
616 | } | |
617 | ||
618 | acb->cluster_offset = acb->l2meta.cluster_offset; | |
f214978a KW |
619 | |
620 | /* Need to wait for another request? If so, we are done for now. */ | |
148da7ea | 621 | if (acb->l2meta.nb_clusters == 0 && acb->l2meta.depends_on != NULL) { |
72cf2d4f | 622 | QLIST_INSERT_HEAD(&acb->l2meta.depends_on->dependent_requests, |
f214978a KW |
623 | acb, next_depend); |
624 | return; | |
625 | } | |
626 | ||
148da7ea KW |
627 | assert((acb->cluster_offset & 511) == 0); |
628 | ||
6f5f060b KW |
629 | qemu_iovec_reset(&acb->hd_qiov); |
630 | qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done, | |
631 | acb->cur_nr_sectors * 512); | |
632 | ||
585f8587 | 633 | if (s->crypt_method) { |
ce1a14dc | 634 | if (!acb->cluster_data) { |
095a9c58 AL |
635 | acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * |
636 | s->cluster_size); | |
585f8587 | 637 | } |
6f5f060b KW |
638 | |
639 | assert(acb->hd_qiov.size <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); | |
640 | qemu_iovec_to_buffer(&acb->hd_qiov, acb->cluster_data); | |
641 | ||
642 | qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data, | |
643 | acb->cluster_data, acb->cur_nr_sectors, 1, &s->aes_encrypt_key); | |
644 | ||
645 | qemu_iovec_reset(&acb->hd_qiov); | |
646 | qemu_iovec_add(&acb->hd_qiov, acb->cluster_data, | |
647 | acb->cur_nr_sectors * 512); | |
585f8587 | 648 | } |
6f5f060b | 649 | |
66f82cee KW |
650 | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); |
651 | acb->hd_aiocb = bdrv_aio_writev(bs->file, | |
c87c0672 | 652 | (acb->cluster_offset >> 9) + index_in_cluster, |
7b88e48b | 653 | &acb->hd_qiov, acb->cur_nr_sectors, |
c87c0672 | 654 | qcow_aio_write_cb, acb); |
171e3d6b KW |
655 | if (acb->hd_aiocb == NULL) { |
656 | ret = -EIO; | |
c644db3d | 657 | goto fail; |
171e3d6b | 658 | } |
f141eafe AL |
659 | |
660 | return; | |
661 | ||
c644db3d KW |
662 | fail: |
663 | if (acb->l2meta.nb_clusters != 0) { | |
664 | QLIST_REMOVE(&acb->l2meta, next_in_flight); | |
665 | } | |
f141eafe | 666 | done: |
f141eafe | 667 | acb->common.cb(acb->common.opaque, ret); |
6f5f060b | 668 | qemu_iovec_destroy(&acb->hd_qiov); |
f141eafe | 669 | qemu_aio_release(acb); |
585f8587 FB |
670 | } |
671 | ||
f141eafe AL |
672 | static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs, |
673 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
ce1a14dc | 674 | BlockDriverCompletionFunc *cb, void *opaque) |
585f8587 | 675 | { |
585f8587 | 676 | BDRVQcowState *s = bs->opaque; |
ce1a14dc | 677 | QCowAIOCB *acb; |
3b46e624 | 678 | |
585f8587 FB |
679 | s->cluster_cache_offset = -1; /* disable compressed cache */ |
680 | ||
f141eafe | 681 | acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
ce1a14dc PB |
682 | if (!acb) |
683 | return NULL; | |
3b46e624 | 684 | |
585f8587 | 685 | qcow_aio_write_cb(acb, 0); |
ce1a14dc | 686 | return &acb->common; |
585f8587 FB |
687 | } |
688 | ||
585f8587 FB |
689 | static void qcow_close(BlockDriverState *bs) |
690 | { | |
691 | BDRVQcowState *s = bs->opaque; | |
692 | qemu_free(s->l1_table); | |
693 | qemu_free(s->l2_cache); | |
694 | qemu_free(s->cluster_cache); | |
695 | qemu_free(s->cluster_data); | |
ed6ccf0f | 696 | qcow2_refcount_close(bs); |
585f8587 FB |
697 | } |
698 | ||
756e6736 KW |
699 | /* |
700 | * Updates the variable length parts of the qcow2 header, i.e. the backing file | |
701 | * name and all extensions. qcow2 was not designed to allow such changes, so if | |
702 | * we run out of space (we can only use the first cluster) this function may | |
703 | * fail. | |
704 | * | |
705 | * Returns 0 on success, -errno in error cases. | |
706 | */ | |
707 | static int qcow2_update_ext_header(BlockDriverState *bs, | |
708 | const char *backing_file, const char *backing_fmt) | |
709 | { | |
710 | size_t backing_file_len = 0; | |
711 | size_t backing_fmt_len = 0; | |
712 | BDRVQcowState *s = bs->opaque; | |
713 | QCowExtension ext_backing_fmt = {0, 0}; | |
714 | int ret; | |
715 | ||
716 | /* Backing file format doesn't make sense without a backing file */ | |
717 | if (backing_fmt && !backing_file) { | |
718 | return -EINVAL; | |
719 | } | |
720 | ||
721 | /* Prepare the backing file format extension if needed */ | |
722 | if (backing_fmt) { | |
723 | ext_backing_fmt.len = cpu_to_be32(strlen(backing_fmt)); | |
724 | ext_backing_fmt.magic = cpu_to_be32(QCOW_EXT_MAGIC_BACKING_FORMAT); | |
725 | backing_fmt_len = ((sizeof(ext_backing_fmt) | |
726 | + strlen(backing_fmt) + 7) & ~7); | |
727 | } | |
728 | ||
729 | /* Check if we can fit the new header into the first cluster */ | |
730 | if (backing_file) { | |
731 | backing_file_len = strlen(backing_file); | |
732 | } | |
733 | ||
734 | size_t header_size = sizeof(QCowHeader) + backing_file_len | |
735 | + backing_fmt_len; | |
736 | ||
737 | if (header_size > s->cluster_size) { | |
738 | return -ENOSPC; | |
739 | } | |
740 | ||
741 | /* Rewrite backing file name and qcow2 extensions */ | |
742 | size_t ext_size = header_size - sizeof(QCowHeader); | |
743 | uint8_t buf[ext_size]; | |
744 | size_t offset = 0; | |
745 | size_t backing_file_offset = 0; | |
746 | ||
747 | if (backing_file) { | |
748 | if (backing_fmt) { | |
749 | int padding = backing_fmt_len - | |
750 | (sizeof(ext_backing_fmt) + strlen(backing_fmt)); | |
751 | ||
752 | memcpy(buf + offset, &ext_backing_fmt, sizeof(ext_backing_fmt)); | |
753 | offset += sizeof(ext_backing_fmt); | |
754 | ||
755 | memcpy(buf + offset, backing_fmt, strlen(backing_fmt)); | |
756 | offset += strlen(backing_fmt); | |
757 | ||
758 | memset(buf + offset, 0, padding); | |
759 | offset += padding; | |
760 | } | |
761 | ||
762 | memcpy(buf + offset, backing_file, backing_file_len); | |
763 | backing_file_offset = sizeof(QCowHeader) + offset; | |
764 | } | |
765 | ||
8b3b7206 | 766 | ret = bdrv_pwrite_sync(bs->file, sizeof(QCowHeader), buf, ext_size); |
756e6736 KW |
767 | if (ret < 0) { |
768 | goto fail; | |
769 | } | |
770 | ||
771 | /* Update header fields */ | |
772 | uint64_t be_backing_file_offset = cpu_to_be64(backing_file_offset); | |
773 | uint32_t be_backing_file_size = cpu_to_be32(backing_file_len); | |
774 | ||
8b3b7206 | 775 | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_offset), |
756e6736 KW |
776 | &be_backing_file_offset, sizeof(uint64_t)); |
777 | if (ret < 0) { | |
778 | goto fail; | |
779 | } | |
780 | ||
8b3b7206 | 781 | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_size), |
756e6736 KW |
782 | &be_backing_file_size, sizeof(uint32_t)); |
783 | if (ret < 0) { | |
784 | goto fail; | |
785 | } | |
786 | ||
787 | ret = 0; | |
788 | fail: | |
789 | return ret; | |
790 | } | |
791 | ||
792 | static int qcow2_change_backing_file(BlockDriverState *bs, | |
793 | const char *backing_file, const char *backing_fmt) | |
794 | { | |
795 | return qcow2_update_ext_header(bs, backing_file, backing_fmt); | |
796 | } | |
797 | ||
a35e1c17 KW |
798 | static int preallocate(BlockDriverState *bs) |
799 | { | |
a35e1c17 KW |
800 | uint64_t nb_sectors; |
801 | uint64_t offset; | |
802 | int num; | |
148da7ea | 803 | int ret; |
a35e1c17 KW |
804 | QCowL2Meta meta; |
805 | ||
806 | nb_sectors = bdrv_getlength(bs) >> 9; | |
807 | offset = 0; | |
72cf2d4f | 808 | QLIST_INIT(&meta.dependent_requests); |
148da7ea | 809 | meta.cluster_offset = 0; |
a35e1c17 KW |
810 | |
811 | while (nb_sectors) { | |
812 | num = MIN(nb_sectors, INT_MAX >> 9); | |
148da7ea | 813 | ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, &meta); |
148da7ea | 814 | if (ret < 0) { |
19dbcbf7 | 815 | return ret; |
a35e1c17 KW |
816 | } |
817 | ||
19dbcbf7 KW |
818 | ret = qcow2_alloc_cluster_link_l2(bs, &meta); |
819 | if (ret < 0) { | |
148da7ea | 820 | qcow2_free_any_clusters(bs, meta.cluster_offset, meta.nb_clusters); |
19dbcbf7 | 821 | return ret; |
a35e1c17 KW |
822 | } |
823 | ||
f214978a KW |
824 | /* There are no dependent requests, but we need to remove our request |
825 | * from the list of in-flight requests */ | |
826 | run_dependent_requests(&meta); | |
827 | ||
a35e1c17 KW |
828 | /* TODO Preallocate data if requested */ |
829 | ||
830 | nb_sectors -= num; | |
831 | offset += num << 9; | |
832 | } | |
833 | ||
834 | /* | |
835 | * It is expected that the image file is large enough to actually contain | |
836 | * all of the allocated clusters (otherwise we get failing reads after | |
837 | * EOF). Extend the image to the last allocated sector. | |
838 | */ | |
148da7ea | 839 | if (meta.cluster_offset != 0) { |
ea80b906 KW |
840 | uint8_t buf[512]; |
841 | memset(buf, 0, 512); | |
19dbcbf7 KW |
842 | ret = bdrv_write(bs->file, (meta.cluster_offset >> 9) + num - 1, buf, 1); |
843 | if (ret < 0) { | |
844 | return ret; | |
845 | } | |
a35e1c17 KW |
846 | } |
847 | ||
848 | return 0; | |
849 | } | |
850 | ||
a9420734 KW |
851 | static int qcow_create2(const char *filename, int64_t total_size, |
852 | const char *backing_file, const char *backing_format, | |
853 | int flags, size_t cluster_size, int prealloc, | |
854 | QEMUOptionParameter *options) | |
855 | { | |
856 | /* Calulate cluster_bits */ | |
857 | int cluster_bits; | |
858 | cluster_bits = ffs(cluster_size) - 1; | |
859 | if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS || | |
860 | (1 << cluster_bits) != cluster_size) | |
861 | { | |
862 | error_report( | |
863 | "Cluster size must be a power of two between %d and %dk\n", | |
864 | 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10)); | |
865 | return -EINVAL; | |
866 | } | |
867 | ||
868 | /* | |
869 | * Open the image file and write a minimal qcow2 header. | |
870 | * | |
871 | * We keep things simple and start with a zero-sized image. We also | |
872 | * do without refcount blocks or a L1 table for now. We'll fix the | |
873 | * inconsistency later. | |
874 | * | |
875 | * We do need a refcount table because growing the refcount table means | |
876 | * allocating two new refcount blocks - the seconds of which would be at | |
877 | * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file | |
878 | * size for any qcow2 image. | |
879 | */ | |
880 | BlockDriverState* bs; | |
881 | QCowHeader header; | |
882 | uint8_t* refcount_table; | |
883 | int ret; | |
884 | ||
885 | ret = bdrv_create_file(filename, options); | |
886 | if (ret < 0) { | |
887 | return ret; | |
888 | } | |
889 | ||
890 | ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR); | |
891 | if (ret < 0) { | |
892 | return ret; | |
893 | } | |
894 | ||
895 | /* Write the header */ | |
896 | memset(&header, 0, sizeof(header)); | |
897 | header.magic = cpu_to_be32(QCOW_MAGIC); | |
898 | header.version = cpu_to_be32(QCOW_VERSION); | |
899 | header.cluster_bits = cpu_to_be32(cluster_bits); | |
900 | header.size = cpu_to_be64(0); | |
901 | header.l1_table_offset = cpu_to_be64(0); | |
902 | header.l1_size = cpu_to_be32(0); | |
903 | header.refcount_table_offset = cpu_to_be64(cluster_size); | |
904 | header.refcount_table_clusters = cpu_to_be32(1); | |
905 | ||
906 | if (flags & BLOCK_FLAG_ENCRYPT) { | |
907 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); | |
908 | } else { | |
909 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); | |
910 | } | |
911 | ||
912 | ret = bdrv_pwrite(bs, 0, &header, sizeof(header)); | |
913 | if (ret < 0) { | |
914 | goto out; | |
915 | } | |
916 | ||
917 | /* Write an empty refcount table */ | |
918 | refcount_table = qemu_mallocz(cluster_size); | |
919 | ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size); | |
920 | qemu_free(refcount_table); | |
921 | ||
922 | if (ret < 0) { | |
923 | goto out; | |
924 | } | |
925 | ||
926 | bdrv_close(bs); | |
927 | ||
928 | /* | |
929 | * And now open the image and make it consistent first (i.e. increase the | |
930 | * refcount of the cluster that is occupied by the header and the refcount | |
931 | * table) | |
932 | */ | |
933 | BlockDriver* drv = bdrv_find_format("qcow2"); | |
934 | assert(drv != NULL); | |
935 | ret = bdrv_open(bs, filename, BDRV_O_RDWR | BDRV_O_NO_FLUSH, drv); | |
936 | if (ret < 0) { | |
937 | goto out; | |
938 | } | |
939 | ||
940 | ret = qcow2_alloc_clusters(bs, 2 * cluster_size); | |
941 | if (ret < 0) { | |
942 | goto out; | |
943 | ||
944 | } else if (ret != 0) { | |
945 | error_report("Huh, first cluster in empty image is already in use?"); | |
946 | abort(); | |
947 | } | |
948 | ||
949 | /* Okay, now that we have a valid image, let's give it the right size */ | |
950 | ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE); | |
951 | if (ret < 0) { | |
952 | goto out; | |
953 | } | |
954 | ||
955 | /* Want a backing file? There you go.*/ | |
956 | if (backing_file) { | |
957 | ret = bdrv_change_backing_file(bs, backing_file, backing_format); | |
958 | if (ret < 0) { | |
959 | goto out; | |
960 | } | |
961 | } | |
962 | ||
963 | /* And if we're supposed to preallocate metadata, do that now */ | |
964 | if (prealloc) { | |
965 | ret = preallocate(bs); | |
966 | if (ret < 0) { | |
967 | goto out; | |
968 | } | |
969 | } | |
970 | ||
971 | ret = 0; | |
972 | out: | |
973 | bdrv_delete(bs); | |
974 | return ret; | |
975 | } | |
de5f3f40 KW |
976 | |
977 | static int qcow_create(const char *filename, QEMUOptionParameter *options) | |
978 | { | |
979 | const char *backing_file = NULL; | |
980 | const char *backing_fmt = NULL; | |
981 | uint64_t sectors = 0; | |
982 | int flags = 0; | |
983 | size_t cluster_size = 65536; | |
984 | int prealloc = 0; | |
985 | ||
986 | /* Read out options */ | |
987 | while (options && options->name) { | |
988 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
989 | sectors = options->value.n / 512; | |
990 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { | |
991 | backing_file = options->value.s; | |
992 | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { | |
993 | backing_fmt = options->value.s; | |
994 | } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) { | |
995 | flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0; | |
996 | } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { | |
997 | if (options->value.n) { | |
998 | cluster_size = options->value.n; | |
999 | } | |
1000 | } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { | |
1001 | if (!options->value.s || !strcmp(options->value.s, "off")) { | |
1002 | prealloc = 0; | |
1003 | } else if (!strcmp(options->value.s, "metadata")) { | |
1004 | prealloc = 1; | |
1005 | } else { | |
1006 | fprintf(stderr, "Invalid preallocation mode: '%s'\n", | |
1007 | options->value.s); | |
1008 | return -EINVAL; | |
1009 | } | |
1010 | } | |
1011 | options++; | |
1012 | } | |
1013 | ||
1014 | if (backing_file && prealloc) { | |
1015 | fprintf(stderr, "Backing file and preallocation cannot be used at " | |
1016 | "the same time\n"); | |
1017 | return -EINVAL; | |
1018 | } | |
1019 | ||
1020 | return qcow_create2(filename, sectors, backing_file, backing_fmt, flags, | |
a9420734 | 1021 | cluster_size, prealloc, options); |
de5f3f40 KW |
1022 | } |
1023 | ||
20d97356 BS |
1024 | static int qcow_make_empty(BlockDriverState *bs) |
1025 | { | |
1026 | #if 0 | |
1027 | /* XXX: not correct */ | |
1028 | BDRVQcowState *s = bs->opaque; | |
1029 | uint32_t l1_length = s->l1_size * sizeof(uint64_t); | |
1030 | int ret; | |
1031 | ||
1032 | memset(s->l1_table, 0, l1_length); | |
66f82cee | 1033 | if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0) |
20d97356 | 1034 | return -1; |
66f82cee | 1035 | ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length); |
20d97356 BS |
1036 | if (ret < 0) |
1037 | return ret; | |
1038 | ||
1039 | l2_cache_reset(bs); | |
1040 | #endif | |
1041 | return 0; | |
1042 | } | |
1043 | ||
419b19d9 SH |
1044 | static int qcow2_truncate(BlockDriverState *bs, int64_t offset) |
1045 | { | |
1046 | BDRVQcowState *s = bs->opaque; | |
1047 | int ret, new_l1_size; | |
1048 | ||
1049 | if (offset & 511) { | |
1050 | return -EINVAL; | |
1051 | } | |
1052 | ||
1053 | /* cannot proceed if image has snapshots */ | |
1054 | if (s->nb_snapshots) { | |
1055 | return -ENOTSUP; | |
1056 | } | |
1057 | ||
1058 | /* shrinking is currently not supported */ | |
1059 | if (offset < bs->total_sectors * 512) { | |
1060 | return -ENOTSUP; | |
1061 | } | |
1062 | ||
1063 | new_l1_size = size_to_l1(s, offset); | |
72893756 | 1064 | ret = qcow2_grow_l1_table(bs, new_l1_size, true); |
419b19d9 SH |
1065 | if (ret < 0) { |
1066 | return ret; | |
1067 | } | |
1068 | ||
1069 | /* write updated header.size */ | |
1070 | offset = cpu_to_be64(offset); | |
8b3b7206 KW |
1071 | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), |
1072 | &offset, sizeof(uint64_t)); | |
419b19d9 SH |
1073 | if (ret < 0) { |
1074 | return ret; | |
1075 | } | |
1076 | ||
1077 | s->l1_vm_state_index = new_l1_size; | |
1078 | return 0; | |
1079 | } | |
1080 | ||
20d97356 BS |
1081 | /* XXX: put compressed sectors first, then all the cluster aligned |
1082 | tables to avoid losing bytes in alignment */ | |
1083 | static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, | |
1084 | const uint8_t *buf, int nb_sectors) | |
1085 | { | |
1086 | BDRVQcowState *s = bs->opaque; | |
1087 | z_stream strm; | |
1088 | int ret, out_len; | |
1089 | uint8_t *out_buf; | |
1090 | uint64_t cluster_offset; | |
1091 | ||
1092 | if (nb_sectors == 0) { | |
1093 | /* align end of file to a sector boundary to ease reading with | |
1094 | sector based I/Os */ | |
66f82cee | 1095 | cluster_offset = bdrv_getlength(bs->file); |
20d97356 | 1096 | cluster_offset = (cluster_offset + 511) & ~511; |
66f82cee | 1097 | bdrv_truncate(bs->file, cluster_offset); |
20d97356 BS |
1098 | return 0; |
1099 | } | |
1100 | ||
1101 | if (nb_sectors != s->cluster_sectors) | |
1102 | return -EINVAL; | |
1103 | ||
1104 | out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); | |
1105 | ||
1106 | /* best compression, small window, no zlib header */ | |
1107 | memset(&strm, 0, sizeof(strm)); | |
1108 | ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, | |
1109 | Z_DEFLATED, -12, | |
1110 | 9, Z_DEFAULT_STRATEGY); | |
1111 | if (ret != 0) { | |
1112 | qemu_free(out_buf); | |
1113 | return -1; | |
1114 | } | |
1115 | ||
1116 | strm.avail_in = s->cluster_size; | |
1117 | strm.next_in = (uint8_t *)buf; | |
1118 | strm.avail_out = s->cluster_size; | |
1119 | strm.next_out = out_buf; | |
1120 | ||
1121 | ret = deflate(&strm, Z_FINISH); | |
1122 | if (ret != Z_STREAM_END && ret != Z_OK) { | |
1123 | qemu_free(out_buf); | |
1124 | deflateEnd(&strm); | |
1125 | return -1; | |
1126 | } | |
1127 | out_len = strm.next_out - out_buf; | |
1128 | ||
1129 | deflateEnd(&strm); | |
1130 | ||
1131 | if (ret != Z_STREAM_END || out_len >= s->cluster_size) { | |
1132 | /* could not compress: write normal cluster */ | |
1133 | bdrv_write(bs, sector_num, buf, s->cluster_sectors); | |
1134 | } else { | |
1135 | cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, | |
1136 | sector_num << 9, out_len); | |
1137 | if (!cluster_offset) | |
1138 | return -1; | |
1139 | cluster_offset &= s->cluster_offset_mask; | |
66f82cee KW |
1140 | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED); |
1141 | if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) { | |
20d97356 BS |
1142 | qemu_free(out_buf); |
1143 | return -1; | |
1144 | } | |
1145 | } | |
1146 | ||
1147 | qemu_free(out_buf); | |
1148 | return 0; | |
1149 | } | |
1150 | ||
205ef796 | 1151 | static int qcow_flush(BlockDriverState *bs) |
20d97356 | 1152 | { |
205ef796 | 1153 | return bdrv_flush(bs->file); |
20d97356 BS |
1154 | } |
1155 | ||
1156 | static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs, | |
1157 | BlockDriverCompletionFunc *cb, void *opaque) | |
1158 | { | |
66f82cee | 1159 | return bdrv_aio_flush(bs->file, cb, opaque); |
20d97356 BS |
1160 | } |
1161 | ||
1162 | static int64_t qcow_vm_state_offset(BDRVQcowState *s) | |
1163 | { | |
1164 | return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); | |
1165 | } | |
1166 | ||
1167 | static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) | |
1168 | { | |
1169 | BDRVQcowState *s = bs->opaque; | |
1170 | bdi->cluster_size = s->cluster_size; | |
1171 | bdi->vm_state_offset = qcow_vm_state_offset(s); | |
1172 | return 0; | |
1173 | } | |
1174 | ||
1175 | ||
9ac228e0 | 1176 | static int qcow_check(BlockDriverState *bs, BdrvCheckResult *result) |
20d97356 | 1177 | { |
9ac228e0 | 1178 | return qcow2_check_refcounts(bs, result); |
20d97356 BS |
1179 | } |
1180 | ||
1181 | #if 0 | |
1182 | static void dump_refcounts(BlockDriverState *bs) | |
1183 | { | |
1184 | BDRVQcowState *s = bs->opaque; | |
1185 | int64_t nb_clusters, k, k1, size; | |
1186 | int refcount; | |
1187 | ||
66f82cee | 1188 | size = bdrv_getlength(bs->file); |
20d97356 BS |
1189 | nb_clusters = size_to_clusters(s, size); |
1190 | for(k = 0; k < nb_clusters;) { | |
1191 | k1 = k; | |
1192 | refcount = get_refcount(bs, k); | |
1193 | k++; | |
1194 | while (k < nb_clusters && get_refcount(bs, k) == refcount) | |
1195 | k++; | |
0bfcd599 BS |
1196 | printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount, |
1197 | k - k1); | |
20d97356 BS |
1198 | } |
1199 | } | |
1200 | #endif | |
1201 | ||
1202 | static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf, | |
1203 | int64_t pos, int size) | |
1204 | { | |
1205 | BDRVQcowState *s = bs->opaque; | |
1206 | int growable = bs->growable; | |
1207 | int ret; | |
1208 | ||
66f82cee | 1209 | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE); |
20d97356 BS |
1210 | bs->growable = 1; |
1211 | ret = bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size); | |
1212 | bs->growable = growable; | |
1213 | ||
1214 | return ret; | |
1215 | } | |
1216 | ||
1217 | static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf, | |
1218 | int64_t pos, int size) | |
1219 | { | |
1220 | BDRVQcowState *s = bs->opaque; | |
1221 | int growable = bs->growable; | |
1222 | int ret; | |
1223 | ||
66f82cee | 1224 | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD); |
20d97356 BS |
1225 | bs->growable = 1; |
1226 | ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size); | |
1227 | bs->growable = growable; | |
1228 | ||
1229 | return ret; | |
1230 | } | |
1231 | ||
1232 | static QEMUOptionParameter qcow_create_options[] = { | |
1233 | { | |
1234 | .name = BLOCK_OPT_SIZE, | |
1235 | .type = OPT_SIZE, | |
1236 | .help = "Virtual disk size" | |
1237 | }, | |
1238 | { | |
1239 | .name = BLOCK_OPT_BACKING_FILE, | |
1240 | .type = OPT_STRING, | |
1241 | .help = "File name of a base image" | |
1242 | }, | |
1243 | { | |
1244 | .name = BLOCK_OPT_BACKING_FMT, | |
1245 | .type = OPT_STRING, | |
1246 | .help = "Image format of the base image" | |
1247 | }, | |
1248 | { | |
1249 | .name = BLOCK_OPT_ENCRYPT, | |
1250 | .type = OPT_FLAG, | |
1251 | .help = "Encrypt the image" | |
1252 | }, | |
1253 | { | |
1254 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
1255 | .type = OPT_SIZE, | |
1256 | .help = "qcow2 cluster size" | |
1257 | }, | |
1258 | { | |
1259 | .name = BLOCK_OPT_PREALLOC, | |
1260 | .type = OPT_STRING, | |
1261 | .help = "Preallocation mode (allowed values: off, metadata)" | |
1262 | }, | |
1263 | { NULL } | |
1264 | }; | |
1265 | ||
1266 | static BlockDriver bdrv_qcow2 = { | |
1267 | .format_name = "qcow2", | |
1268 | .instance_size = sizeof(BDRVQcowState), | |
1269 | .bdrv_probe = qcow_probe, | |
1270 | .bdrv_open = qcow_open, | |
1271 | .bdrv_close = qcow_close, | |
1272 | .bdrv_create = qcow_create, | |
1273 | .bdrv_flush = qcow_flush, | |
1274 | .bdrv_is_allocated = qcow_is_allocated, | |
1275 | .bdrv_set_key = qcow_set_key, | |
1276 | .bdrv_make_empty = qcow_make_empty, | |
1277 | ||
1278 | .bdrv_aio_readv = qcow_aio_readv, | |
1279 | .bdrv_aio_writev = qcow_aio_writev, | |
1280 | .bdrv_aio_flush = qcow_aio_flush, | |
419b19d9 SH |
1281 | |
1282 | .bdrv_truncate = qcow2_truncate, | |
1283 | .bdrv_write_compressed = qcow_write_compressed, | |
20d97356 BS |
1284 | |
1285 | .bdrv_snapshot_create = qcow2_snapshot_create, | |
1286 | .bdrv_snapshot_goto = qcow2_snapshot_goto, | |
1287 | .bdrv_snapshot_delete = qcow2_snapshot_delete, | |
1288 | .bdrv_snapshot_list = qcow2_snapshot_list, | |
51ef6727 | 1289 | .bdrv_snapshot_load_tmp = qcow2_snapshot_load_tmp, |
20d97356 BS |
1290 | .bdrv_get_info = qcow_get_info, |
1291 | ||
1292 | .bdrv_save_vmstate = qcow_save_vmstate, | |
1293 | .bdrv_load_vmstate = qcow_load_vmstate, | |
1294 | ||
1295 | .bdrv_change_backing_file = qcow2_change_backing_file, | |
1296 | ||
1297 | .create_options = qcow_create_options, | |
1298 | .bdrv_check = qcow_check, | |
1299 | }; | |
1300 | ||
5efa9d5a AL |
1301 | static void bdrv_qcow2_init(void) |
1302 | { | |
1303 | bdrv_register(&bdrv_qcow2); | |
1304 | } | |
1305 | ||
1306 | block_init(bdrv_qcow2_init); |