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