]> git.proxmox.com Git - mirror_qemu.git/blame - block-qcow2.c
Exclude unix: from vnc call to unix_listen() (Ryan Harper)
[mirror_qemu.git] / block-qcow2.c
CommitLineData
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
FB
25#include "block_int.h"
26#include <zlib.h>
27#include "aes.h"
28#include <assert.h>
29
30/*
31 Differences with QCOW:
32
33 - Support for multiple incremental snapshots.
34 - Memory management by reference counts.
35 - Clusters which have a reference count of one have the bit
36 QCOW_OFLAG_COPIED to optimize write performance.
5fafdf24 37 - Size of compressed clusters is stored in sectors to reduce bit usage
585f8587
FB
38 in the cluster offsets.
39 - Support for storing additional data (such as the VM state) in the
3b46e624 40 snapshots.
585f8587
FB
41 - If a backing store is used, the cluster size is not constrained
42 (could be backported to QCOW).
43 - L2 tables have always a size of one cluster.
44*/
45
46//#define DEBUG_ALLOC
47//#define DEBUG_ALLOC2
5fafdf24 48
585f8587
FB
49#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
50#define QCOW_VERSION 2
51
52#define QCOW_CRYPT_NONE 0
53#define QCOW_CRYPT_AES 1
54
095a9c58
AL
55#define QCOW_MAX_CRYPT_CLUSTERS 32
56
585f8587
FB
57/* indicate that the refcount of the referenced cluster is exactly one. */
58#define QCOW_OFLAG_COPIED (1LL << 63)
59/* indicate that the cluster is compressed (they never have the copied flag) */
60#define QCOW_OFLAG_COMPRESSED (1LL << 62)
61
62#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
63
585f8587
FB
64typedef struct QCowHeader {
65 uint32_t magic;
66 uint32_t version;
67 uint64_t backing_file_offset;
68 uint32_t backing_file_size;
69 uint32_t cluster_bits;
70 uint64_t size; /* in bytes */
71 uint32_t crypt_method;
72 uint32_t l1_size; /* XXX: save number of clusters instead ? */
73 uint64_t l1_table_offset;
74 uint64_t refcount_table_offset;
75 uint32_t refcount_table_clusters;
76 uint32_t nb_snapshots;
77 uint64_t snapshots_offset;
78} QCowHeader;
79
80typedef struct __attribute__((packed)) QCowSnapshotHeader {
81 /* header is 8 byte aligned */
82 uint64_t l1_table_offset;
83
84 uint32_t l1_size;
85 uint16_t id_str_size;
86 uint16_t name_size;
87
88 uint32_t date_sec;
89 uint32_t date_nsec;
90
91 uint64_t vm_clock_nsec;
92
93 uint32_t vm_state_size;
94 uint32_t extra_data_size; /* for extension */
95 /* extra data follows */
96 /* id_str follows */
97 /* name follows */
98} QCowSnapshotHeader;
99
100#define L2_CACHE_SIZE 16
101
102typedef struct QCowSnapshot {
103 uint64_t l1_table_offset;
104 uint32_t l1_size;
105 char *id_str;
106 char *name;
107 uint32_t vm_state_size;
108 uint32_t date_sec;
109 uint32_t date_nsec;
110 uint64_t vm_clock_nsec;
111} QCowSnapshot;
112
113typedef struct BDRVQcowState {
114 BlockDriverState *hd;
115 int cluster_bits;
116 int cluster_size;
117 int cluster_sectors;
118 int l2_bits;
119 int l2_size;
120 int l1_size;
121 int l1_vm_state_index;
122 int csize_shift;
123 int csize_mask;
124 uint64_t cluster_offset_mask;
125 uint64_t l1_table_offset;
126 uint64_t *l1_table;
127 uint64_t *l2_cache;
128 uint64_t l2_cache_offsets[L2_CACHE_SIZE];
129 uint32_t l2_cache_counts[L2_CACHE_SIZE];
130 uint8_t *cluster_cache;
131 uint8_t *cluster_data;
132 uint64_t cluster_cache_offset;
133
134 uint64_t *refcount_table;
135 uint64_t refcount_table_offset;
136 uint32_t refcount_table_size;
137 uint64_t refcount_block_cache_offset;
138 uint16_t *refcount_block_cache;
139 int64_t free_cluster_index;
140 int64_t free_byte_offset;
141
142 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
143 uint32_t crypt_method_header;
144 AES_KEY aes_encrypt_key;
145 AES_KEY aes_decrypt_key;
146 uint64_t snapshots_offset;
147 int snapshots_size;
148 int nb_snapshots;
149 QCowSnapshot *snapshots;
150} BDRVQcowState;
151
152static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
5fafdf24 153static int qcow_read(BlockDriverState *bs, int64_t sector_num,
585f8587
FB
154 uint8_t *buf, int nb_sectors);
155static int qcow_read_snapshots(BlockDriverState *bs);
156static void qcow_free_snapshots(BlockDriverState *bs);
157static int refcount_init(BlockDriverState *bs);
158static void refcount_close(BlockDriverState *bs);
159static int get_refcount(BlockDriverState *bs, int64_t cluster_index);
5fafdf24 160static int update_cluster_refcount(BlockDriverState *bs,
585f8587
FB
161 int64_t cluster_index,
162 int addend);
5fafdf24
TS
163static void update_refcount(BlockDriverState *bs,
164 int64_t offset, int64_t length,
585f8587
FB
165 int addend);
166static int64_t alloc_clusters(BlockDriverState *bs, int64_t size);
167static int64_t alloc_bytes(BlockDriverState *bs, int size);
5fafdf24 168static void free_clusters(BlockDriverState *bs,
585f8587
FB
169 int64_t offset, int64_t size);
170#ifdef DEBUG_ALLOC
171static void check_refcounts(BlockDriverState *bs);
172#endif
173
174static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
175{
176 const QCowHeader *cow_header = (const void *)buf;
3b46e624 177
585f8587
FB
178 if (buf_size >= sizeof(QCowHeader) &&
179 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
5fafdf24 180 be32_to_cpu(cow_header->version) == QCOW_VERSION)
585f8587
FB
181 return 100;
182 else
183 return 0;
184}
185
186static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
187{
188 BDRVQcowState *s = bs->opaque;
189 int len, i, shift, ret;
190 QCowHeader header;
191
b5eff355 192 ret = bdrv_file_open(&s->hd, filename, flags);
585f8587
FB
193 if (ret < 0)
194 return ret;
195 if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
196 goto fail;
197 be32_to_cpus(&header.magic);
198 be32_to_cpus(&header.version);
199 be64_to_cpus(&header.backing_file_offset);
200 be32_to_cpus(&header.backing_file_size);
201 be64_to_cpus(&header.size);
202 be32_to_cpus(&header.cluster_bits);
203 be32_to_cpus(&header.crypt_method);
204 be64_to_cpus(&header.l1_table_offset);
205 be32_to_cpus(&header.l1_size);
206 be64_to_cpus(&header.refcount_table_offset);
207 be32_to_cpus(&header.refcount_table_clusters);
208 be64_to_cpus(&header.snapshots_offset);
209 be32_to_cpus(&header.nb_snapshots);
3b46e624 210
585f8587
FB
211 if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
212 goto fail;
5fafdf24
TS
213 if (header.size <= 1 ||
214 header.cluster_bits < 9 ||
585f8587
FB
215 header.cluster_bits > 16)
216 goto fail;
217 if (header.crypt_method > QCOW_CRYPT_AES)
218 goto fail;
219 s->crypt_method_header = header.crypt_method;
220 if (s->crypt_method_header)
221 bs->encrypted = 1;
222 s->cluster_bits = header.cluster_bits;
223 s->cluster_size = 1 << s->cluster_bits;
224 s->cluster_sectors = 1 << (s->cluster_bits - 9);
225 s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
226 s->l2_size = 1 << s->l2_bits;
227 bs->total_sectors = header.size / 512;
228 s->csize_shift = (62 - (s->cluster_bits - 8));
229 s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
230 s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
231 s->refcount_table_offset = header.refcount_table_offset;
5fafdf24 232 s->refcount_table_size =
585f8587
FB
233 header.refcount_table_clusters << (s->cluster_bits - 3);
234
235 s->snapshots_offset = header.snapshots_offset;
236 s->nb_snapshots = header.nb_snapshots;
237
238 /* read the level 1 table */
239 s->l1_size = header.l1_size;
240 shift = s->cluster_bits + s->l2_bits;
241 s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift;
242 /* the L1 table must contain at least enough entries to put
243 header.size bytes */
244 if (s->l1_size < s->l1_vm_state_index)
245 goto fail;
246 s->l1_table_offset = header.l1_table_offset;
247 s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
248 if (!s->l1_table)
249 goto fail;
5fafdf24 250 if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
585f8587
FB
251 s->l1_size * sizeof(uint64_t))
252 goto fail;
253 for(i = 0;i < s->l1_size; i++) {
254 be64_to_cpus(&s->l1_table[i]);
255 }
256 /* alloc L2 cache */
257 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
258 if (!s->l2_cache)
259 goto fail;
260 s->cluster_cache = qemu_malloc(s->cluster_size);
261 if (!s->cluster_cache)
262 goto fail;
263 /* one more sector for decompressed data alignment */
095a9c58
AL
264 s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
265 + 512);
585f8587
FB
266 if (!s->cluster_data)
267 goto fail;
268 s->cluster_cache_offset = -1;
3b46e624 269
585f8587
FB
270 if (refcount_init(bs) < 0)
271 goto fail;
272
273 /* read the backing file name */
274 if (header.backing_file_offset != 0) {
275 len = header.backing_file_size;
276 if (len > 1023)
277 len = 1023;
278 if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
279 goto fail;
280 bs->backing_file[len] = '\0';
281 }
282 if (qcow_read_snapshots(bs) < 0)
283 goto fail;
284
285#ifdef DEBUG_ALLOC
286 check_refcounts(bs);
287#endif
288 return 0;
289
290 fail:
291 qcow_free_snapshots(bs);
292 refcount_close(bs);
293 qemu_free(s->l1_table);
294 qemu_free(s->l2_cache);
295 qemu_free(s->cluster_cache);
296 qemu_free(s->cluster_data);
297 bdrv_delete(s->hd);
298 return -1;
299}
300
301static int qcow_set_key(BlockDriverState *bs, const char *key)
302{
303 BDRVQcowState *s = bs->opaque;
304 uint8_t keybuf[16];
305 int len, i;
3b46e624 306
585f8587
FB
307 memset(keybuf, 0, 16);
308 len = strlen(key);
309 if (len > 16)
310 len = 16;
311 /* XXX: we could compress the chars to 7 bits to increase
312 entropy */
313 for(i = 0;i < len;i++) {
314 keybuf[i] = key[i];
315 }
316 s->crypt_method = s->crypt_method_header;
317
318 if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
319 return -1;
320 if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
321 return -1;
322#if 0
323 /* test */
324 {
325 uint8_t in[16];
326 uint8_t out[16];
327 uint8_t tmp[16];
328 for(i=0;i<16;i++)
329 in[i] = i;
330 AES_encrypt(in, tmp, &s->aes_encrypt_key);
331 AES_decrypt(tmp, out, &s->aes_decrypt_key);
332 for(i = 0; i < 16; i++)
333 printf(" %02x", tmp[i]);
334 printf("\n");
335 for(i = 0; i < 16; i++)
336 printf(" %02x", out[i]);
337 printf("\n");
338 }
339#endif
340 return 0;
341}
342
343/* The crypt function is compatible with the linux cryptoloop
344 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
345 supported */
346static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
347 uint8_t *out_buf, const uint8_t *in_buf,
348 int nb_sectors, int enc,
349 const AES_KEY *key)
350{
351 union {
352 uint64_t ll[2];
353 uint8_t b[16];
354 } ivec;
355 int i;
356
357 for(i = 0; i < nb_sectors; i++) {
358 ivec.ll[0] = cpu_to_le64(sector_num);
359 ivec.ll[1] = 0;
5fafdf24 360 AES_cbc_encrypt(in_buf, out_buf, 512, key,
585f8587
FB
361 ivec.b, enc);
362 sector_num++;
363 in_buf += 512;
364 out_buf += 512;
365 }
366}
367
368static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
369 uint64_t cluster_offset, int n_start, int n_end)
370{
371 BDRVQcowState *s = bs->opaque;
372 int n, ret;
373
374 n = n_end - n_start;
375 if (n <= 0)
376 return 0;
377 ret = qcow_read(bs, start_sect + n_start, s->cluster_data, n);
378 if (ret < 0)
379 return ret;
380 if (s->crypt_method) {
5fafdf24
TS
381 encrypt_sectors(s, start_sect + n_start,
382 s->cluster_data,
585f8587
FB
383 s->cluster_data, n, 1,
384 &s->aes_encrypt_key);
385 }
5fafdf24 386 ret = bdrv_write(s->hd, (cluster_offset >> 9) + n_start,
585f8587
FB
387 s->cluster_data, n);
388 if (ret < 0)
389 return ret;
390 return 0;
391}
392
393static void l2_cache_reset(BlockDriverState *bs)
394{
395 BDRVQcowState *s = bs->opaque;
396
397 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
398 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
399 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
400}
401
402static inline int l2_cache_new_entry(BlockDriverState *bs)
403{
404 BDRVQcowState *s = bs->opaque;
405 uint32_t min_count;
406 int min_index, i;
407
408 /* find a new entry in the least used one */
409 min_index = 0;
410 min_count = 0xffffffff;
411 for(i = 0; i < L2_CACHE_SIZE; i++) {
412 if (s->l2_cache_counts[i] < min_count) {
413 min_count = s->l2_cache_counts[i];
414 min_index = i;
415 }
416 }
417 return min_index;
418}
419
420static int64_t align_offset(int64_t offset, int n)
421{
422 offset = (offset + n - 1) & ~(n - 1);
423 return offset;
424}
425
426static int grow_l1_table(BlockDriverState *bs, int min_size)
427{
428 BDRVQcowState *s = bs->opaque;
429 int new_l1_size, new_l1_size2, ret, i;
430 uint64_t *new_l1_table;
431 uint64_t new_l1_table_offset;
432 uint64_t data64;
433 uint32_t data32;
434
435 new_l1_size = s->l1_size;
436 if (min_size <= new_l1_size)
437 return 0;
438 while (min_size > new_l1_size) {
439 new_l1_size = (new_l1_size * 3 + 1) / 2;
440 }
441#ifdef DEBUG_ALLOC2
442 printf("grow l1_table from %d to %d\n", s->l1_size, new_l1_size);
443#endif
444
445 new_l1_size2 = sizeof(uint64_t) * new_l1_size;
446 new_l1_table = qemu_mallocz(new_l1_size2);
447 if (!new_l1_table)
448 return -ENOMEM;
449 memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
450
451 /* write new table (align to cluster) */
452 new_l1_table_offset = alloc_clusters(bs, new_l1_size2);
3b46e624 453
585f8587
FB
454 for(i = 0; i < s->l1_size; i++)
455 new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
456 ret = bdrv_pwrite(s->hd, new_l1_table_offset, new_l1_table, new_l1_size2);
457 if (ret != new_l1_size2)
458 goto fail;
459 for(i = 0; i < s->l1_size; i++)
460 new_l1_table[i] = be64_to_cpu(new_l1_table[i]);
3b46e624 461
585f8587
FB
462 /* set new table */
463 data64 = cpu_to_be64(new_l1_table_offset);
464 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_table_offset),
465 &data64, sizeof(data64)) != sizeof(data64))
466 goto fail;
467 data32 = cpu_to_be32(new_l1_size);
468 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, l1_size),
469 &data32, sizeof(data32)) != sizeof(data32))
470 goto fail;
471 qemu_free(s->l1_table);
472 free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
473 s->l1_table_offset = new_l1_table_offset;
474 s->l1_table = new_l1_table;
475 s->l1_size = new_l1_size;
476 return 0;
477 fail:
478 qemu_free(s->l1_table);
479 return -EIO;
480}
481
108534b9
AL
482/*
483 * seek_l2_table
585f8587 484 *
108534b9
AL
485 * seek l2_offset in the l2_cache table
486 * if not found, return NULL,
487 * if found,
488 * increments the l2 cache hit count of the entry,
489 * if counter overflow, divide by two all counters
490 * return the pointer to the l2 cache entry
585f8587 491 *
108534b9
AL
492 */
493
494static uint64_t *seek_l2_table(BDRVQcowState *s, uint64_t l2_offset)
495{
496 int i, j;
497
498 for(i = 0; i < L2_CACHE_SIZE; i++) {
499 if (l2_offset == s->l2_cache_offsets[i]) {
500 /* increment the hit count */
501 if (++s->l2_cache_counts[i] == 0xffffffff) {
502 for(j = 0; j < L2_CACHE_SIZE; j++) {
503 s->l2_cache_counts[j] >>= 1;
504 }
505 }
506 return s->l2_cache + (i << s->l2_bits);
507 }
508 }
509 return NULL;
510}
511
512/*
513 * l2_load
514 *
515 * Loads a L2 table into memory. If the table is in the cache, the cache
516 * is used; otherwise the L2 table is loaded from the image file.
517 *
518 * Returns a pointer to the L2 table on success, or NULL if the read from
519 * the image file failed.
520 */
521
522static uint64_t *l2_load(BlockDriverState *bs, uint64_t l2_offset)
523{
524 BDRVQcowState *s = bs->opaque;
525 int min_index;
526 uint64_t *l2_table;
527
528 /* seek if the table for the given offset is in the cache */
529
530 l2_table = seek_l2_table(s, l2_offset);
531 if (l2_table != NULL)
532 return l2_table;
533
534 /* not found: load a new entry in the least used one */
535
536 min_index = l2_cache_new_entry(bs);
537 l2_table = s->l2_cache + (min_index << s->l2_bits);
538 if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
539 s->l2_size * sizeof(uint64_t))
540 return NULL;
541 s->l2_cache_offsets[min_index] = l2_offset;
542 s->l2_cache_counts[min_index] = 1;
543
544 return l2_table;
545}
546
547/*
548 * l2_allocate
585f8587 549 *
108534b9
AL
550 * Allocate a new l2 entry in the file. If l1_index points to an already
551 * used entry in the L2 table (i.e. we are doing a copy on write for the L2
552 * table) copy the contents of the old L2 table into the newly allocated one.
553 * Otherwise the new table is initialized with zeros.
585f8587 554 *
585f8587 555 */
108534b9
AL
556
557static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
558{
559 BDRVQcowState *s = bs->opaque;
560 int min_index;
561 uint64_t old_l2_offset, tmp;
562 uint64_t *l2_table, l2_offset;
563
564 old_l2_offset = s->l1_table[l1_index];
565
566 /* allocate a new l2 entry */
567
568 l2_offset = alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
569
570 /* update the L1 entry */
571
572 s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
573
574 tmp = cpu_to_be64(l2_offset | QCOW_OFLAG_COPIED);
575 if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp),
576 &tmp, sizeof(tmp)) != sizeof(tmp))
577 return NULL;
578
579 /* allocate a new entry in the l2 cache */
580
581 min_index = l2_cache_new_entry(bs);
582 l2_table = s->l2_cache + (min_index << s->l2_bits);
583
584 if (old_l2_offset == 0) {
585 /* if there was no old l2 table, clear the new table */
586 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
587 } else {
588 /* if there was an old l2 table, read it from the disk */
589 if (bdrv_pread(s->hd, old_l2_offset,
590 l2_table, s->l2_size * sizeof(uint64_t)) !=
591 s->l2_size * sizeof(uint64_t))
592 return NULL;
593 }
594 /* write the l2 table to the file */
595 if (bdrv_pwrite(s->hd, l2_offset,
596 l2_table, s->l2_size * sizeof(uint64_t)) !=
597 s->l2_size * sizeof(uint64_t))
598 return NULL;
599
600 /* update the l2 cache entry */
601
602 s->l2_cache_offsets[min_index] = l2_offset;
603 s->l2_cache_counts[min_index] = 1;
604
605 return l2_table;
606}
607
05203524
AL
608/*
609 * get_cluster_offset
610 *
611 * For a given offset of the disk image, return cluster offset in
612 * qcow2 file.
613 *
095a9c58
AL
614 * on entry, *num is the number of contiguous clusters we'd like to
615 * access following offset.
616 *
617 * on exit, *num is the number of contiguous clusters we can read.
618 *
05203524
AL
619 * Return 1, if the offset is found
620 * Return 0, otherwise.
621 *
622 */
623
095a9c58
AL
624static uint64_t get_cluster_offset(BlockDriverState *bs,
625 uint64_t offset, int *num)
05203524
AL
626{
627 BDRVQcowState *s = bs->opaque;
628 int l1_index, l2_index;
095a9c58
AL
629 uint64_t l2_offset, *l2_table, cluster_offset, next;
630 int l1_bits;
631 int index_in_cluster, nb_available, nb_needed;
632
633 index_in_cluster = (offset >> 9) & (s->cluster_sectors - 1);
634 nb_needed = *num + index_in_cluster;
635
636 l1_bits = s->l2_bits + s->cluster_bits;
637
638 /* compute how many bytes there are between the offset and
639 * and the end of the l1 entry
640 */
641
642 nb_available = (1 << l1_bits) - (offset & ((1 << l1_bits) - 1));
643
644 /* compute the number of available sectors */
645
646 nb_available = (nb_available >> 9) + index_in_cluster;
647
648 cluster_offset = 0;
05203524
AL
649
650 /* seek the the l2 offset in the l1 table */
651
095a9c58 652 l1_index = offset >> l1_bits;
05203524 653 if (l1_index >= s->l1_size)
095a9c58 654 goto out;
05203524
AL
655
656 l2_offset = s->l1_table[l1_index];
657
658 /* seek the l2 table of the given l2 offset */
659
660 if (!l2_offset)
095a9c58 661 goto out;
05203524
AL
662
663 /* load the l2 table in memory */
664
665 l2_offset &= ~QCOW_OFLAG_COPIED;
666 l2_table = l2_load(bs, l2_offset);
667 if (l2_table == NULL)
768706a5 668 return 0;
05203524
AL
669
670 /* find the cluster offset for the given disk offset */
671
672 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
673 cluster_offset = be64_to_cpu(l2_table[l2_index]);
095a9c58
AL
674 nb_available = s->cluster_sectors;
675 l2_index++;
676
677 if (!cluster_offset) {
05203524 678
095a9c58
AL
679 /* how many empty clusters ? */
680
681 while (nb_available < nb_needed && !l2_table[l2_index]) {
682 l2_index++;
683 nb_available += s->cluster_sectors;
684 }
685 } else {
686
687 /* how many allocated clusters ? */
688
689 cluster_offset &= ~QCOW_OFLAG_COPIED;
690 while (nb_available < nb_needed) {
691 next = be64_to_cpu(l2_table[l2_index]) & ~QCOW_OFLAG_COPIED;
692 if (next != cluster_offset + (nb_available << 9))
693 break;
694 l2_index++;
695 nb_available += s->cluster_sectors;
696 }
697 }
698
699out:
700 if (nb_available > nb_needed)
701 nb_available = nb_needed;
702
703 *num = nb_available - index_in_cluster;
704
705 return cluster_offset;
05203524
AL
706}
707
708/*
52d893ec 709 * free_any_clusters
05203524 710 *
52d893ec 711 * free clusters according to its type: compressed or not
05203524 712 *
52d893ec
AL
713 */
714
715static void free_any_clusters(BlockDriverState *bs,
095a9c58 716 uint64_t cluster_offset, int nb_clusters)
52d893ec
AL
717{
718 BDRVQcowState *s = bs->opaque;
719
52d893ec
AL
720 /* free the cluster */
721
722 if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
723 int nb_csectors;
724 nb_csectors = ((cluster_offset >> s->csize_shift) &
725 s->csize_mask) + 1;
726 free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511,
727 nb_csectors * 512);
728 return;
729 }
730
095a9c58
AL
731 free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
732
733 return;
52d893ec
AL
734}
735
736/*
737 * get_cluster_table
05203524 738 *
52d893ec
AL
739 * for a given disk offset, load (and allocate if needed)
740 * the l2 table.
741 *
742 * the l2 table offset in the qcow2 file and the cluster index
743 * in the l2 table are given to the caller.
05203524
AL
744 *
745 */
746
52d893ec
AL
747static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
748 uint64_t **new_l2_table,
749 uint64_t *new_l2_offset,
750 int *new_l2_index)
585f8587
FB
751{
752 BDRVQcowState *s = bs->opaque;
108534b9 753 int l1_index, l2_index, ret;
52d893ec 754 uint64_t l2_offset, *l2_table;
108534b9
AL
755
756 /* seek the the l2 offset in the l1 table */
3b46e624 757
585f8587
FB
758 l1_index = offset >> (s->l2_bits + s->cluster_bits);
759 if (l1_index >= s->l1_size) {
108534b9
AL
760 ret = grow_l1_table(bs, l1_index + 1);
761 if (ret < 0)
585f8587
FB
762 return 0;
763 }
764 l2_offset = s->l1_table[l1_index];
108534b9
AL
765
766 /* seek the l2 table of the given l2 offset */
767
05203524
AL
768 if (l2_offset & QCOW_OFLAG_COPIED) {
769 /* load the l2 table in memory */
770 l2_offset &= ~QCOW_OFLAG_COPIED;
771 l2_table = l2_load(bs, l2_offset);
772 if (l2_table == NULL)
585f8587 773 return 0;
05203524
AL
774 } else {
775 if (l2_offset)
776 free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
108534b9
AL
777 l2_table = l2_allocate(bs, l1_index);
778 if (l2_table == NULL)
585f8587 779 return 0;
108534b9 780 l2_offset = s->l1_table[l1_index] & ~QCOW_OFLAG_COPIED;
585f8587 781 }
108534b9
AL
782
783 /* find the cluster offset for the given disk offset */
784
585f8587 785 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
05203524 786
52d893ec
AL
787 *new_l2_table = l2_table;
788 *new_l2_offset = l2_offset;
789 *new_l2_index = l2_index;
790
791 return 1;
792}
793
794/*
795 * alloc_compressed_cluster_offset
796 *
797 * For a given offset of the disk image, return cluster offset in
798 * qcow2 file.
799 *
800 * If the offset is not found, allocate a new compressed cluster.
801 *
802 * Return the cluster offset if successful,
803 * Return 0, otherwise.
804 *
805 */
806
807static uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
808 uint64_t offset,
809 int compressed_size)
810{
811 BDRVQcowState *s = bs->opaque;
812 int l2_index, ret;
813 uint64_t l2_offset, *l2_table, cluster_offset;
814 int nb_csectors;
815
816 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
817 if (ret == 0)
818 return 0;
819
820 cluster_offset = be64_to_cpu(l2_table[l2_index]);
05203524
AL
821 if (cluster_offset & QCOW_OFLAG_COPIED)
822 return cluster_offset & ~QCOW_OFLAG_COPIED;
823
095a9c58
AL
824 if (cluster_offset)
825 free_any_clusters(bs, cluster_offset, 1);
108534b9 826
52d893ec
AL
827 cluster_offset = alloc_bytes(bs, compressed_size);
828 nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
829 (cluster_offset >> 9);
05203524 830
52d893ec
AL
831 cluster_offset |= QCOW_OFLAG_COMPRESSED |
832 ((uint64_t)nb_csectors << s->csize_shift);
05203524 833
52d893ec 834 /* update L2 table */
05203524 835
52d893ec 836 /* compressed clusters never have the copied flag */
05203524 837
52d893ec
AL
838 l2_table[l2_index] = cpu_to_be64(cluster_offset);
839 if (bdrv_pwrite(s->hd,
840 l2_offset + l2_index * sizeof(uint64_t),
841 l2_table + l2_index,
842 sizeof(uint64_t)) != sizeof(uint64_t))
843 return 0;
05203524 844
52d893ec
AL
845 return cluster_offset;
846}
05203524 847
52d893ec
AL
848/*
849 * alloc_cluster_offset
850 *
851 * For a given offset of the disk image, return cluster offset in
852 * qcow2 file.
853 *
854 * If the offset is not found, allocate a new cluster.
855 *
856 * Return the cluster offset if successful,
857 * Return 0, otherwise.
858 *
859 */
860
861static uint64_t alloc_cluster_offset(BlockDriverState *bs,
862 uint64_t offset,
095a9c58
AL
863 int n_start, int n_end,
864 int *num)
52d893ec
AL
865{
866 BDRVQcowState *s = bs->opaque;
867 int l2_index, ret;
868 uint64_t l2_offset, *l2_table, cluster_offset;
bc352085 869 int nb_available, nb_clusters, i, j;
095a9c58 870 uint64_t start_sect, current;
52d893ec
AL
871
872 ret = get_cluster_table(bs, offset, &l2_table, &l2_offset, &l2_index);
873 if (ret == 0)
874 return 0;
875
095a9c58
AL
876 nb_clusters = ((n_end << 9) + s->cluster_size - 1) >>
877 s->cluster_bits;
878 if (nb_clusters > s->l2_size - l2_index)
879 nb_clusters = s->l2_size - l2_index;
880
52d893ec 881 cluster_offset = be64_to_cpu(l2_table[l2_index]);
52d893ec 882
095a9c58
AL
883 /* We keep all QCOW_OFLAG_COPIED clusters */
884
885 if (cluster_offset & QCOW_OFLAG_COPIED) {
886
887 for (i = 1; i < nb_clusters; i++) {
888 current = be64_to_cpu(l2_table[l2_index + i]);
889 if (cluster_offset + (i << s->cluster_bits) != current)
890 break;
891 }
892 nb_clusters = i;
893
894 nb_available = nb_clusters << (s->cluster_bits - 9);
895 if (nb_available > n_end)
896 nb_available = n_end;
897
898 cluster_offset &= ~QCOW_OFLAG_COPIED;
899
900 goto out;
901 }
902
903 /* for the moment, multiple compressed clusters are not managed */
904
905 if (cluster_offset & QCOW_OFLAG_COMPRESSED)
906 nb_clusters = 1;
907
bc352085 908 /* how many available clusters ? */
095a9c58 909
bc352085
AL
910 i = 0;
911 while (i < nb_clusters) {
095a9c58 912
bc352085 913 i++;
095a9c58 914
bc352085 915 if (!cluster_offset) {
095a9c58 916
bc352085 917 /* how many free clusters ? */
095a9c58 918
bc352085 919 while (i < nb_clusters) {
f4af02ed 920 cluster_offset = be64_to_cpu(l2_table[l2_index + i]);
bc352085
AL
921 if (cluster_offset != 0)
922 break;
923 i++;
924 }
095a9c58 925
bc352085
AL
926 if ((cluster_offset & QCOW_OFLAG_COPIED) ||
927 (cluster_offset & QCOW_OFLAG_COMPRESSED))
095a9c58 928 break;
095a9c58 929
bc352085
AL
930 } else {
931
932 /* how many contiguous clusters ? */
933
934 j = 1;
935 current = 0;
936 while (i < nb_clusters) {
937 current = be64_to_cpu(l2_table[l2_index + i]);
938 if (cluster_offset + (j << s->cluster_bits) != current)
939 break;
940
941 i++;
942 j++;
943 }
944
945 free_any_clusters(bs, cluster_offset, j);
946 if (current)
947 break;
948 cluster_offset = current;
949 }
095a9c58 950 }
bc352085 951 nb_clusters = i;
05203524
AL
952
953 /* allocate a new cluster */
954
095a9c58 955 cluster_offset = alloc_clusters(bs, nb_clusters * s->cluster_size);
05203524
AL
956
957 /* we must initialize the cluster content which won't be
958 written */
959
095a9c58
AL
960 nb_available = nb_clusters << (s->cluster_bits - 9);
961 if (nb_available > n_end)
962 nb_available = n_end;
05203524 963
095a9c58
AL
964 /* copy content of unmodified sectors */
965
966 start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
967 if (n_start) {
968 ret = copy_sectors(bs, start_sect, cluster_offset, 0, n_start);
05203524
AL
969 if (ret < 0)
970 return 0;
095a9c58
AL
971 }
972
973 if (nb_available & (s->cluster_sectors - 1)) {
974 uint64_t end = nb_available & ~(uint64_t)(s->cluster_sectors - 1);
975 ret = copy_sectors(bs, start_sect + end,
976 cluster_offset + (end << 9),
977 nb_available - end,
978 s->cluster_sectors);
05203524
AL
979 if (ret < 0)
980 return 0;
585f8587 981 }
05203524 982
585f8587 983 /* update L2 table */
05203524 984
095a9c58
AL
985 for (i = 0; i < nb_clusters; i++)
986 l2_table[l2_index + i] = cpu_to_be64((cluster_offset +
987 (i << s->cluster_bits)) |
988 QCOW_OFLAG_COPIED);
989
5fafdf24 990 if (bdrv_pwrite(s->hd,
05203524
AL
991 l2_offset + l2_index * sizeof(uint64_t),
992 l2_table + l2_index,
095a9c58
AL
993 nb_clusters * sizeof(uint64_t)) !=
994 nb_clusters * sizeof(uint64_t))
585f8587 995 return 0;
05203524 996
095a9c58
AL
997out:
998 *num = nb_available - n_start;
999
585f8587
FB
1000 return cluster_offset;
1001}
1002
5fafdf24 1003static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
585f8587
FB
1004 int nb_sectors, int *pnum)
1005{
585f8587
FB
1006 uint64_t cluster_offset;
1007
095a9c58
AL
1008 *pnum = nb_sectors;
1009 cluster_offset = get_cluster_offset(bs, sector_num << 9, pnum);
1010
585f8587
FB
1011 return (cluster_offset != 0);
1012}
1013
1014static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
1015 const uint8_t *buf, int buf_size)
1016{
1017 z_stream strm1, *strm = &strm1;
1018 int ret, out_len;
1019
1020 memset(strm, 0, sizeof(*strm));
1021
1022 strm->next_in = (uint8_t *)buf;
1023 strm->avail_in = buf_size;
1024 strm->next_out = out_buf;
1025 strm->avail_out = out_buf_size;
1026
1027 ret = inflateInit2(strm, -12);
1028 if (ret != Z_OK)
1029 return -1;
1030 ret = inflate(strm, Z_FINISH);
1031 out_len = strm->next_out - out_buf;
1032 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
1033 out_len != out_buf_size) {
1034 inflateEnd(strm);
1035 return -1;
1036 }
1037 inflateEnd(strm);
1038 return 0;
1039}
3b46e624 1040
585f8587
FB
1041static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
1042{
1043 int ret, csize, nb_csectors, sector_offset;
1044 uint64_t coffset;
1045
1046 coffset = cluster_offset & s->cluster_offset_mask;
1047 if (s->cluster_cache_offset != coffset) {
1048 nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
1049 sector_offset = coffset & 511;
1050 csize = nb_csectors * 512 - sector_offset;
1051 ret = bdrv_read(s->hd, coffset >> 9, s->cluster_data, nb_csectors);
1052 if (ret < 0) {
1053 return -1;
1054 }
1055 if (decompress_buffer(s->cluster_cache, s->cluster_size,
1056 s->cluster_data + sector_offset, csize) < 0) {
1057 return -1;
1058 }
1059 s->cluster_cache_offset = coffset;
1060 }
1061 return 0;
1062}
1063
a9465922 1064/* handle reading after the end of the backing file */
5fafdf24 1065static int backing_read1(BlockDriverState *bs,
a9465922
FB
1066 int64_t sector_num, uint8_t *buf, int nb_sectors)
1067{
1068 int n1;
1069 if ((sector_num + nb_sectors) <= bs->total_sectors)
1070 return nb_sectors;
1071 if (sector_num >= bs->total_sectors)
1072 n1 = 0;
1073 else
1074 n1 = bs->total_sectors - sector_num;
1075 memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1));
1076 return n1;
1077}
1078
5fafdf24 1079static int qcow_read(BlockDriverState *bs, int64_t sector_num,
585f8587
FB
1080 uint8_t *buf, int nb_sectors)
1081{
1082 BDRVQcowState *s = bs->opaque;
a9465922 1083 int ret, index_in_cluster, n, n1;
585f8587 1084 uint64_t cluster_offset;
3b46e624 1085
585f8587 1086 while (nb_sectors > 0) {
095a9c58
AL
1087 n = nb_sectors;
1088 cluster_offset = get_cluster_offset(bs, sector_num << 9, &n);
585f8587 1089 index_in_cluster = sector_num & (s->cluster_sectors - 1);
585f8587
FB
1090 if (!cluster_offset) {
1091 if (bs->backing_hd) {
1092 /* read from the base image */
a9465922
FB
1093 n1 = backing_read1(bs->backing_hd, sector_num, buf, n);
1094 if (n1 > 0) {
1095 ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
1096 if (ret < 0)
1097 return -1;
1098 }
585f8587
FB
1099 } else {
1100 memset(buf, 0, 512 * n);
1101 }
1102 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
1103 if (decompress_cluster(s, cluster_offset) < 0)
1104 return -1;
1105 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
1106 } else {
1107 ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
5fafdf24 1108 if (ret != n * 512)
585f8587
FB
1109 return -1;
1110 if (s->crypt_method) {
5fafdf24 1111 encrypt_sectors(s, sector_num, buf, buf, n, 0,
585f8587
FB
1112 &s->aes_decrypt_key);
1113 }
1114 }
1115 nb_sectors -= n;
1116 sector_num += n;
1117 buf += n * 512;
1118 }
1119 return 0;
1120}
1121
5fafdf24 1122static int qcow_write(BlockDriverState *bs, int64_t sector_num,
585f8587
FB
1123 const uint8_t *buf, int nb_sectors)
1124{
1125 BDRVQcowState *s = bs->opaque;
1126 int ret, index_in_cluster, n;
1127 uint64_t cluster_offset;
095a9c58 1128 int n_end;
3b46e624 1129
585f8587
FB
1130 while (nb_sectors > 0) {
1131 index_in_cluster = sector_num & (s->cluster_sectors - 1);
095a9c58
AL
1132 n_end = index_in_cluster + nb_sectors;
1133 if (s->crypt_method &&
1134 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1135 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
52d893ec 1136 cluster_offset = alloc_cluster_offset(bs, sector_num << 9,
05203524 1137 index_in_cluster,
095a9c58 1138 n_end, &n);
585f8587
FB
1139 if (!cluster_offset)
1140 return -1;
1141 if (s->crypt_method) {
1142 encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
1143 &s->aes_encrypt_key);
5fafdf24 1144 ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,
585f8587
FB
1145 s->cluster_data, n * 512);
1146 } else {
1147 ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1148 }
5fafdf24 1149 if (ret != n * 512)
585f8587
FB
1150 return -1;
1151 nb_sectors -= n;
1152 sector_num += n;
1153 buf += n * 512;
1154 }
1155 s->cluster_cache_offset = -1; /* disable compressed cache */
1156 return 0;
1157}
1158
ce1a14dc
PB
1159typedef struct QCowAIOCB {
1160 BlockDriverAIOCB common;
585f8587
FB
1161 int64_t sector_num;
1162 uint8_t *buf;
1163 int nb_sectors;
1164 int n;
1165 uint64_t cluster_offset;
5fafdf24 1166 uint8_t *cluster_data;
585f8587 1167 BlockDriverAIOCB *hd_aiocb;
1490791f 1168 QEMUBH *bh;
585f8587
FB
1169} QCowAIOCB;
1170
1490791f
AL
1171static void qcow_aio_read_cb(void *opaque, int ret);
1172static void qcow_aio_read_bh(void *opaque)
1173{
1174 QCowAIOCB *acb = opaque;
1175 qemu_bh_delete(acb->bh);
1176 acb->bh = NULL;
1177 qcow_aio_read_cb(opaque, 0);
1178}
1179
585f8587
FB
1180static void qcow_aio_read_cb(void *opaque, int ret)
1181{
ce1a14dc
PB
1182 QCowAIOCB *acb = opaque;
1183 BlockDriverState *bs = acb->common.bs;
585f8587 1184 BDRVQcowState *s = bs->opaque;
a9465922 1185 int index_in_cluster, n1;
585f8587 1186
ce1a14dc 1187 acb->hd_aiocb = NULL;
585f8587
FB
1188 if (ret < 0) {
1189 fail:
ce1a14dc
PB
1190 acb->common.cb(acb->common.opaque, ret);
1191 qemu_aio_release(acb);
585f8587
FB
1192 return;
1193 }
1194
585f8587 1195 /* post process the read buffer */
ce1a14dc 1196 if (!acb->cluster_offset) {
585f8587 1197 /* nothing to do */
ce1a14dc 1198 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
585f8587
FB
1199 /* nothing to do */
1200 } else {
1201 if (s->crypt_method) {
5fafdf24
TS
1202 encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
1203 acb->n, 0,
585f8587
FB
1204 &s->aes_decrypt_key);
1205 }
1206 }
1207
ce1a14dc
PB
1208 acb->nb_sectors -= acb->n;
1209 acb->sector_num += acb->n;
1210 acb->buf += acb->n * 512;
585f8587 1211
ce1a14dc 1212 if (acb->nb_sectors == 0) {
585f8587 1213 /* request completed */
ce1a14dc
PB
1214 acb->common.cb(acb->common.opaque, 0);
1215 qemu_aio_release(acb);
585f8587
FB
1216 return;
1217 }
3b46e624 1218
585f8587 1219 /* prepare next AIO request */
095a9c58
AL
1220 acb->n = acb->nb_sectors;
1221 acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
ce1a14dc 1222 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
ce1a14dc
PB
1223
1224 if (!acb->cluster_offset) {
585f8587
FB
1225 if (bs->backing_hd) {
1226 /* read from the base image */
5fafdf24 1227 n1 = backing_read1(bs->backing_hd, acb->sector_num,
ce1a14dc 1228 acb->buf, acb->n);
a9465922 1229 if (n1 > 0) {
5fafdf24 1230 acb->hd_aiocb = bdrv_aio_read(bs->backing_hd, acb->sector_num,
ce1a14dc
PB
1231 acb->buf, acb->n, qcow_aio_read_cb, acb);
1232 if (acb->hd_aiocb == NULL)
a9465922
FB
1233 goto fail;
1234 } else {
1490791f
AL
1235 if (acb->bh) {
1236 ret = -EIO;
1237 goto fail;
1238 }
1239 acb->bh = qemu_bh_new(qcow_aio_read_bh, acb);
1240 if (!acb->bh) {
1241 ret = -EIO;
1242 goto fail;
1243 }
1244 qemu_bh_schedule(acb->bh);
a9465922 1245 }
585f8587
FB
1246 } else {
1247 /* Note: in this case, no need to wait */
ce1a14dc 1248 memset(acb->buf, 0, 512 * acb->n);
1490791f
AL
1249 if (acb->bh) {
1250 ret = -EIO;
1251 goto fail;
1252 }
1253 acb->bh = qemu_bh_new(qcow_aio_read_bh, acb);
1254 if (!acb->bh) {
1255 ret = -EIO;
1256 goto fail;
1257 }
1258 qemu_bh_schedule(acb->bh);
585f8587 1259 }
ce1a14dc 1260 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
585f8587 1261 /* add AIO support for compressed blocks ? */
ce1a14dc 1262 if (decompress_cluster(s, acb->cluster_offset) < 0)
585f8587 1263 goto fail;
5fafdf24 1264 memcpy(acb->buf,
ce1a14dc 1265 s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
1490791f
AL
1266 if (acb->bh) {
1267 ret = -EIO;
1268 goto fail;
1269 }
1270 acb->bh = qemu_bh_new(qcow_aio_read_bh, acb);
1271 if (!acb->bh) {
1272 ret = -EIO;
1273 goto fail;
1274 }
1275 qemu_bh_schedule(acb->bh);
585f8587 1276 } else {
ce1a14dc 1277 if ((acb->cluster_offset & 511) != 0) {
585f8587
FB
1278 ret = -EIO;
1279 goto fail;
1280 }
ce1a14dc 1281 acb->hd_aiocb = bdrv_aio_read(s->hd,
5fafdf24 1282 (acb->cluster_offset >> 9) + index_in_cluster,
ce1a14dc
PB
1283 acb->buf, acb->n, qcow_aio_read_cb, acb);
1284 if (acb->hd_aiocb == NULL)
585f8587
FB
1285 goto fail;
1286 }
1287}
1288
ce1a14dc
PB
1289static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
1290 int64_t sector_num, uint8_t *buf, int nb_sectors,
1291 BlockDriverCompletionFunc *cb, void *opaque)
585f8587 1292{
ce1a14dc
PB
1293 QCowAIOCB *acb;
1294
1295 acb = qemu_aio_get(bs, cb, opaque);
1296 if (!acb)
1297 return NULL;
1298 acb->hd_aiocb = NULL;
1299 acb->sector_num = sector_num;
1300 acb->buf = buf;
1301 acb->nb_sectors = nb_sectors;
1302 acb->n = 0;
1303 acb->cluster_offset = 0;
1304 return acb;
1305}
1306
1307static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs,
1308 int64_t sector_num, uint8_t *buf, int nb_sectors,
1309 BlockDriverCompletionFunc *cb, void *opaque)
1310{
1311 QCowAIOCB *acb;
1312
1313 acb = qcow_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1314 if (!acb)
1315 return NULL;
585f8587
FB
1316
1317 qcow_aio_read_cb(acb, 0);
ce1a14dc 1318 return &acb->common;
585f8587
FB
1319}
1320
1321static void qcow_aio_write_cb(void *opaque, int ret)
1322{
ce1a14dc
PB
1323 QCowAIOCB *acb = opaque;
1324 BlockDriverState *bs = acb->common.bs;
585f8587 1325 BDRVQcowState *s = bs->opaque;
585f8587
FB
1326 int index_in_cluster;
1327 uint64_t cluster_offset;
1328 const uint8_t *src_buf;
095a9c58 1329 int n_end;
ce1a14dc
PB
1330
1331 acb->hd_aiocb = NULL;
1332
585f8587
FB
1333 if (ret < 0) {
1334 fail:
ce1a14dc
PB
1335 acb->common.cb(acb->common.opaque, ret);
1336 qemu_aio_release(acb);
585f8587
FB
1337 return;
1338 }
1339
ce1a14dc
PB
1340 acb->nb_sectors -= acb->n;
1341 acb->sector_num += acb->n;
1342 acb->buf += acb->n * 512;
585f8587 1343
ce1a14dc 1344 if (acb->nb_sectors == 0) {
585f8587 1345 /* request completed */
ce1a14dc
PB
1346 acb->common.cb(acb->common.opaque, 0);
1347 qemu_aio_release(acb);
585f8587
FB
1348 return;
1349 }
3b46e624 1350
ce1a14dc 1351 index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
095a9c58
AL
1352 n_end = index_in_cluster + acb->nb_sectors;
1353 if (s->crypt_method &&
1354 n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1355 n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1356
52d893ec 1357 cluster_offset = alloc_cluster_offset(bs, acb->sector_num << 9,
05203524 1358 index_in_cluster,
095a9c58 1359 n_end, &acb->n);
585f8587
FB
1360 if (!cluster_offset || (cluster_offset & 511) != 0) {
1361 ret = -EIO;
1362 goto fail;
1363 }
1364 if (s->crypt_method) {
ce1a14dc 1365 if (!acb->cluster_data) {
095a9c58
AL
1366 acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS *
1367 s->cluster_size);
ce1a14dc 1368 if (!acb->cluster_data) {
585f8587
FB
1369 ret = -ENOMEM;
1370 goto fail;
1371 }
1372 }
5fafdf24 1373 encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
ce1a14dc
PB
1374 acb->n, 1, &s->aes_encrypt_key);
1375 src_buf = acb->cluster_data;
585f8587 1376 } else {
ce1a14dc 1377 src_buf = acb->buf;
585f8587 1378 }
ce1a14dc 1379 acb->hd_aiocb = bdrv_aio_write(s->hd,
5fafdf24
TS
1380 (cluster_offset >> 9) + index_in_cluster,
1381 src_buf, acb->n,
ce1a14dc
PB
1382 qcow_aio_write_cb, acb);
1383 if (acb->hd_aiocb == NULL)
585f8587
FB
1384 goto fail;
1385}
1386
ce1a14dc
PB
1387static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs,
1388 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1389 BlockDriverCompletionFunc *cb, void *opaque)
585f8587 1390{
585f8587 1391 BDRVQcowState *s = bs->opaque;
ce1a14dc 1392 QCowAIOCB *acb;
3b46e624 1393
585f8587
FB
1394 s->cluster_cache_offset = -1; /* disable compressed cache */
1395
ce1a14dc
PB
1396 acb = qcow_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
1397 if (!acb)
1398 return NULL;
3b46e624 1399
585f8587 1400 qcow_aio_write_cb(acb, 0);
ce1a14dc 1401 return &acb->common;
585f8587
FB
1402}
1403
ce1a14dc 1404static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
585f8587 1405{
ce1a14dc
PB
1406 QCowAIOCB *acb = (QCowAIOCB *)blockacb;
1407 if (acb->hd_aiocb)
1408 bdrv_aio_cancel(acb->hd_aiocb);
1409 qemu_aio_release(acb);
585f8587
FB
1410}
1411
1412static void qcow_close(BlockDriverState *bs)
1413{
1414 BDRVQcowState *s = bs->opaque;
1415 qemu_free(s->l1_table);
1416 qemu_free(s->l2_cache);
1417 qemu_free(s->cluster_cache);
1418 qemu_free(s->cluster_data);
1419 refcount_close(bs);
1420 bdrv_delete(s->hd);
1421}
1422
1423/* XXX: use std qcow open function ? */
1424typedef struct QCowCreateState {
1425 int cluster_size;
1426 int cluster_bits;
1427 uint16_t *refcount_block;
1428 uint64_t *refcount_table;
1429 int64_t l1_table_offset;
1430 int64_t refcount_table_offset;
1431 int64_t refcount_block_offset;
1432} QCowCreateState;
1433
1434static void create_refcount_update(QCowCreateState *s,
1435 int64_t offset, int64_t size)
1436{
1437 int refcount;
1438 int64_t start, last, cluster_offset;
1439 uint16_t *p;
1440
1441 start = offset & ~(s->cluster_size - 1);
1442 last = (offset + size - 1) & ~(s->cluster_size - 1);
5fafdf24 1443 for(cluster_offset = start; cluster_offset <= last;
585f8587
FB
1444 cluster_offset += s->cluster_size) {
1445 p = &s->refcount_block[cluster_offset >> s->cluster_bits];
1446 refcount = be16_to_cpu(*p);
1447 refcount++;
1448 *p = cpu_to_be16(refcount);
1449 }
1450}
1451
1452static int qcow_create(const char *filename, int64_t total_size,
1453 const char *backing_file, int flags)
1454{
1455 int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
1456 QCowHeader header;
1457 uint64_t tmp, offset;
1458 QCowCreateState s1, *s = &s1;
3b46e624 1459
585f8587
FB
1460 memset(s, 0, sizeof(*s));
1461
1462 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
1463 if (fd < 0)
1464 return -1;
1465 memset(&header, 0, sizeof(header));
1466 header.magic = cpu_to_be32(QCOW_MAGIC);
1467 header.version = cpu_to_be32(QCOW_VERSION);
1468 header.size = cpu_to_be64(total_size * 512);
1469 header_size = sizeof(header);
1470 backing_filename_len = 0;
1471 if (backing_file) {
1472 header.backing_file_offset = cpu_to_be64(header_size);
1473 backing_filename_len = strlen(backing_file);
1474 header.backing_file_size = cpu_to_be32(backing_filename_len);
1475 header_size += backing_filename_len;
1476 }
1477 s->cluster_bits = 12; /* 4 KB clusters */
1478 s->cluster_size = 1 << s->cluster_bits;
1479 header.cluster_bits = cpu_to_be32(s->cluster_bits);
1480 header_size = (header_size + 7) & ~7;
ec36ba14 1481 if (flags & BLOCK_FLAG_ENCRYPT) {
585f8587
FB
1482 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1483 } else {
1484 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1485 }
1486 l2_bits = s->cluster_bits - 3;
1487 shift = s->cluster_bits + l2_bits;
1488 l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift);
1489 offset = align_offset(header_size, s->cluster_size);
1490 s->l1_table_offset = offset;
1491 header.l1_table_offset = cpu_to_be64(s->l1_table_offset);
1492 header.l1_size = cpu_to_be32(l1_size);
15e6690a 1493 offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
585f8587
FB
1494
1495 s->refcount_table = qemu_mallocz(s->cluster_size);
1496 if (!s->refcount_table)
1497 goto fail;
1498 s->refcount_block = qemu_mallocz(s->cluster_size);
1499 if (!s->refcount_block)
1500 goto fail;
3b46e624 1501
585f8587
FB
1502 s->refcount_table_offset = offset;
1503 header.refcount_table_offset = cpu_to_be64(offset);
1504 header.refcount_table_clusters = cpu_to_be32(1);
1505 offset += s->cluster_size;
1506
1507 s->refcount_table[0] = cpu_to_be64(offset);
1508 s->refcount_block_offset = offset;
1509 offset += s->cluster_size;
1510
1511 /* update refcounts */
1512 create_refcount_update(s, 0, header_size);
15e6690a 1513 create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t));
585f8587
FB
1514 create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
1515 create_refcount_update(s, s->refcount_block_offset, s->cluster_size);
3b46e624 1516
585f8587
FB
1517 /* write all the data */
1518 write(fd, &header, sizeof(header));
1519 if (backing_file) {
1520 write(fd, backing_file, backing_filename_len);
1521 }
1522 lseek(fd, s->l1_table_offset, SEEK_SET);
1523 tmp = 0;
1524 for(i = 0;i < l1_size; i++) {
1525 write(fd, &tmp, sizeof(tmp));
1526 }
1527 lseek(fd, s->refcount_table_offset, SEEK_SET);
1528 write(fd, s->refcount_table, s->cluster_size);
3b46e624 1529
585f8587
FB
1530 lseek(fd, s->refcount_block_offset, SEEK_SET);
1531 write(fd, s->refcount_block, s->cluster_size);
1532
1533 qemu_free(s->refcount_table);
1534 qemu_free(s->refcount_block);
1535 close(fd);
1536 return 0;
1537 fail:
1538 qemu_free(s->refcount_table);
1539 qemu_free(s->refcount_block);
1540 close(fd);
1541 return -ENOMEM;
1542}
1543
1544static int qcow_make_empty(BlockDriverState *bs)
1545{
1546#if 0
1547 /* XXX: not correct */
1548 BDRVQcowState *s = bs->opaque;
1549 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
1550 int ret;
1551
1552 memset(s->l1_table, 0, l1_length);
1553 if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
1554 return -1;
1555 ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
1556 if (ret < 0)
1557 return ret;
3b46e624 1558
585f8587
FB
1559 l2_cache_reset(bs);
1560#endif
1561 return 0;
1562}
1563
1564/* XXX: put compressed sectors first, then all the cluster aligned
1565 tables to avoid losing bytes in alignment */
5fafdf24 1566static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
585f8587
FB
1567 const uint8_t *buf, int nb_sectors)
1568{
1569 BDRVQcowState *s = bs->opaque;
1570 z_stream strm;
1571 int ret, out_len;
1572 uint8_t *out_buf;
1573 uint64_t cluster_offset;
1574
1575 if (nb_sectors == 0) {
1576 /* align end of file to a sector boundary to ease reading with
1577 sector based I/Os */
1578 cluster_offset = bdrv_getlength(s->hd);
1579 cluster_offset = (cluster_offset + 511) & ~511;
1580 bdrv_truncate(s->hd, cluster_offset);
1581 return 0;
1582 }
1583
1584 if (nb_sectors != s->cluster_sectors)
1585 return -EINVAL;
1586
1587 out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
1588 if (!out_buf)
1589 return -ENOMEM;
1590
1591 /* best compression, small window, no zlib header */
1592 memset(&strm, 0, sizeof(strm));
1593 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
5fafdf24 1594 Z_DEFLATED, -12,
585f8587
FB
1595 9, Z_DEFAULT_STRATEGY);
1596 if (ret != 0) {
1597 qemu_free(out_buf);
1598 return -1;
1599 }
1600
1601 strm.avail_in = s->cluster_size;
1602 strm.next_in = (uint8_t *)buf;
1603 strm.avail_out = s->cluster_size;
1604 strm.next_out = out_buf;
1605
1606 ret = deflate(&strm, Z_FINISH);
1607 if (ret != Z_STREAM_END && ret != Z_OK) {
1608 qemu_free(out_buf);
1609 deflateEnd(&strm);
1610 return -1;
1611 }
1612 out_len = strm.next_out - out_buf;
1613
1614 deflateEnd(&strm);
1615
1616 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1617 /* could not compress: write normal cluster */
1618 qcow_write(bs, sector_num, buf, s->cluster_sectors);
1619 } else {
52d893ec
AL
1620 cluster_offset = alloc_compressed_cluster_offset(bs, sector_num << 9,
1621 out_len);
1622 if (!cluster_offset)
1623 return -1;
585f8587
FB
1624 cluster_offset &= s->cluster_offset_mask;
1625 if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
1626 qemu_free(out_buf);
1627 return -1;
1628 }
1629 }
3b46e624 1630
585f8587
FB
1631 qemu_free(out_buf);
1632 return 0;
1633}
1634
1635static void qcow_flush(BlockDriverState *bs)
1636{
1637 BDRVQcowState *s = bs->opaque;
1638 bdrv_flush(s->hd);
1639}
1640
1641static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1642{
1643 BDRVQcowState *s = bs->opaque;
1644 bdi->cluster_size = s->cluster_size;
5fafdf24 1645 bdi->vm_state_offset = (int64_t)s->l1_vm_state_index <<
585f8587
FB
1646 (s->cluster_bits + s->l2_bits);
1647 return 0;
1648}
1649
1650/*********************************************************/
1651/* snapshot support */
1652
1653/* update the refcounts of snapshots and the copied flag */
5fafdf24 1654static int update_snapshot_refcount(BlockDriverState *bs,
585f8587
FB
1655 int64_t l1_table_offset,
1656 int l1_size,
1657 int addend)
1658{
1659 BDRVQcowState *s = bs->opaque;
1660 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
1661 int64_t old_offset, old_l2_offset;
1662 int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
3b46e624 1663
585f8587
FB
1664 l2_cache_reset(bs);
1665
1666 l2_table = NULL;
1667 l1_table = NULL;
1668 l1_size2 = l1_size * sizeof(uint64_t);
1669 l1_allocated = 0;
1670 if (l1_table_offset != s->l1_table_offset) {
1671 l1_table = qemu_malloc(l1_size2);
1672 if (!l1_table)
1673 goto fail;
1674 l1_allocated = 1;
5fafdf24 1675 if (bdrv_pread(s->hd, l1_table_offset,
585f8587
FB
1676 l1_table, l1_size2) != l1_size2)
1677 goto fail;
1678 for(i = 0;i < l1_size; i++)
1679 be64_to_cpus(&l1_table[i]);
1680 } else {
1681 assert(l1_size == s->l1_size);
1682 l1_table = s->l1_table;
1683 l1_allocated = 0;
1684 }
3b46e624 1685
585f8587
FB
1686 l2_size = s->l2_size * sizeof(uint64_t);
1687 l2_table = qemu_malloc(l2_size);
1688 if (!l2_table)
1689 goto fail;
1690 l1_modified = 0;
1691 for(i = 0; i < l1_size; i++) {
1692 l2_offset = l1_table[i];
1693 if (l2_offset) {
1694 old_l2_offset = l2_offset;
1695 l2_offset &= ~QCOW_OFLAG_COPIED;
1696 l2_modified = 0;
1697 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
1698 goto fail;
1699 for(j = 0; j < s->l2_size; j++) {
1700 offset = be64_to_cpu(l2_table[j]);
1701 if (offset != 0) {
1702 old_offset = offset;
1703 offset &= ~QCOW_OFLAG_COPIED;
1704 if (offset & QCOW_OFLAG_COMPRESSED) {
5fafdf24 1705 nb_csectors = ((offset >> s->csize_shift) &
585f8587
FB
1706 s->csize_mask) + 1;
1707 if (addend != 0)
1708 update_refcount(bs, (offset & s->cluster_offset_mask) & ~511,
1709 nb_csectors * 512, addend);
1710 /* compressed clusters are never modified */
5fafdf24 1711 refcount = 2;
585f8587
FB
1712 } else {
1713 if (addend != 0) {
1714 refcount = update_cluster_refcount(bs, offset >> s->cluster_bits, addend);
1715 } else {
1716 refcount = get_refcount(bs, offset >> s->cluster_bits);
1717 }
1718 }
1719
1720 if (refcount == 1) {
1721 offset |= QCOW_OFLAG_COPIED;
1722 }
1723 if (offset != old_offset) {
1724 l2_table[j] = cpu_to_be64(offset);
1725 l2_modified = 1;
1726 }
1727 }
1728 }
1729 if (l2_modified) {
5fafdf24 1730 if (bdrv_pwrite(s->hd,
585f8587
FB
1731 l2_offset, l2_table, l2_size) != l2_size)
1732 goto fail;
1733 }
1734
1735 if (addend != 0) {
1736 refcount = update_cluster_refcount(bs, l2_offset >> s->cluster_bits, addend);
1737 } else {
1738 refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
1739 }
1740 if (refcount == 1) {
1741 l2_offset |= QCOW_OFLAG_COPIED;
1742 }
1743 if (l2_offset != old_l2_offset) {
1744 l1_table[i] = l2_offset;
1745 l1_modified = 1;
1746 }
1747 }
1748 }
1749 if (l1_modified) {
1750 for(i = 0; i < l1_size; i++)
1751 cpu_to_be64s(&l1_table[i]);
5fafdf24 1752 if (bdrv_pwrite(s->hd, l1_table_offset, l1_table,
585f8587
FB
1753 l1_size2) != l1_size2)
1754 goto fail;
1755 for(i = 0; i < l1_size; i++)
1756 be64_to_cpus(&l1_table[i]);
1757 }
1758 if (l1_allocated)
1759 qemu_free(l1_table);
1760 qemu_free(l2_table);
1761 return 0;
1762 fail:
1763 if (l1_allocated)
1764 qemu_free(l1_table);
1765 qemu_free(l2_table);
1766 return -EIO;
1767}
1768
1769static void qcow_free_snapshots(BlockDriverState *bs)
1770{
1771 BDRVQcowState *s = bs->opaque;
1772 int i;
1773
1774 for(i = 0; i < s->nb_snapshots; i++) {
1775 qemu_free(s->snapshots[i].name);
1776 qemu_free(s->snapshots[i].id_str);
1777 }
1778 qemu_free(s->snapshots);
1779 s->snapshots = NULL;
1780 s->nb_snapshots = 0;
1781}
1782
1783static int qcow_read_snapshots(BlockDriverState *bs)
1784{
1785 BDRVQcowState *s = bs->opaque;
1786 QCowSnapshotHeader h;
1787 QCowSnapshot *sn;
1788 int i, id_str_size, name_size;
1789 int64_t offset;
1790 uint32_t extra_data_size;
1791
1792 offset = s->snapshots_offset;
1793 s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
1794 if (!s->snapshots)
1795 goto fail;
1796 for(i = 0; i < s->nb_snapshots; i++) {
1797 offset = align_offset(offset, 8);
1798 if (bdrv_pread(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1799 goto fail;
1800 offset += sizeof(h);
1801 sn = s->snapshots + i;
1802 sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
1803 sn->l1_size = be32_to_cpu(h.l1_size);
1804 sn->vm_state_size = be32_to_cpu(h.vm_state_size);
1805 sn->date_sec = be32_to_cpu(h.date_sec);
1806 sn->date_nsec = be32_to_cpu(h.date_nsec);
1807 sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
1808 extra_data_size = be32_to_cpu(h.extra_data_size);
1809
1810 id_str_size = be16_to_cpu(h.id_str_size);
1811 name_size = be16_to_cpu(h.name_size);
1812
1813 offset += extra_data_size;
1814
1815 sn->id_str = qemu_malloc(id_str_size + 1);
1816 if (!sn->id_str)
1817 goto fail;
1818 if (bdrv_pread(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1819 goto fail;
1820 offset += id_str_size;
1821 sn->id_str[id_str_size] = '\0';
1822
1823 sn->name = qemu_malloc(name_size + 1);
1824 if (!sn->name)
1825 goto fail;
1826 if (bdrv_pread(s->hd, offset, sn->name, name_size) != name_size)
1827 goto fail;
1828 offset += name_size;
1829 sn->name[name_size] = '\0';
1830 }
1831 s->snapshots_size = offset - s->snapshots_offset;
1832 return 0;
1833 fail:
1834 qcow_free_snapshots(bs);
1835 return -1;
1836}
1837
1838/* add at the end of the file a new list of snapshots */
1839static int qcow_write_snapshots(BlockDriverState *bs)
1840{
1841 BDRVQcowState *s = bs->opaque;
1842 QCowSnapshot *sn;
1843 QCowSnapshotHeader h;
1844 int i, name_size, id_str_size, snapshots_size;
1845 uint64_t data64;
1846 uint32_t data32;
1847 int64_t offset, snapshots_offset;
1848
1849 /* compute the size of the snapshots */
1850 offset = 0;
1851 for(i = 0; i < s->nb_snapshots; i++) {
1852 sn = s->snapshots + i;
1853 offset = align_offset(offset, 8);
1854 offset += sizeof(h);
1855 offset += strlen(sn->id_str);
1856 offset += strlen(sn->name);
1857 }
1858 snapshots_size = offset;
1859
1860 snapshots_offset = alloc_clusters(bs, snapshots_size);
1861 offset = snapshots_offset;
3b46e624 1862
585f8587
FB
1863 for(i = 0; i < s->nb_snapshots; i++) {
1864 sn = s->snapshots + i;
1865 memset(&h, 0, sizeof(h));
1866 h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
1867 h.l1_size = cpu_to_be32(sn->l1_size);
1868 h.vm_state_size = cpu_to_be32(sn->vm_state_size);
1869 h.date_sec = cpu_to_be32(sn->date_sec);
1870 h.date_nsec = cpu_to_be32(sn->date_nsec);
1871 h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
3b46e624 1872
585f8587
FB
1873 id_str_size = strlen(sn->id_str);
1874 name_size = strlen(sn->name);
1875 h.id_str_size = cpu_to_be16(id_str_size);
1876 h.name_size = cpu_to_be16(name_size);
1877 offset = align_offset(offset, 8);
1878 if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h))
1879 goto fail;
1880 offset += sizeof(h);
1881 if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
1882 goto fail;
1883 offset += id_str_size;
1884 if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size)
1885 goto fail;
1886 offset += name_size;
1887 }
1888
1889 /* update the various header fields */
1890 data64 = cpu_to_be64(snapshots_offset);
1891 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset),
1892 &data64, sizeof(data64)) != sizeof(data64))
1893 goto fail;
1894 data32 = cpu_to_be32(s->nb_snapshots);
1895 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots),
1896 &data32, sizeof(data32)) != sizeof(data32))
1897 goto fail;
1898
1899 /* free the old snapshot table */
1900 free_clusters(bs, s->snapshots_offset, s->snapshots_size);
1901 s->snapshots_offset = snapshots_offset;
1902 s->snapshots_size = snapshots_size;
1903 return 0;
1904 fail:
1905 return -1;
1906}
1907
1908static void find_new_snapshot_id(BlockDriverState *bs,
1909 char *id_str, int id_str_size)
1910{
1911 BDRVQcowState *s = bs->opaque;
1912 QCowSnapshot *sn;
1913 int i, id, id_max = 0;
1914
1915 for(i = 0; i < s->nb_snapshots; i++) {
1916 sn = s->snapshots + i;
1917 id = strtoul(sn->id_str, NULL, 10);
1918 if (id > id_max)
1919 id_max = id;
1920 }
1921 snprintf(id_str, id_str_size, "%d", id_max + 1);
1922}
1923
1924static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
1925{
1926 BDRVQcowState *s = bs->opaque;
1927 int i;
1928
1929 for(i = 0; i < s->nb_snapshots; i++) {
1930 if (!strcmp(s->snapshots[i].id_str, id_str))
1931 return i;
1932 }
1933 return -1;
1934}
1935
1936static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
1937{
1938 BDRVQcowState *s = bs->opaque;
1939 int i, ret;
3b46e624 1940
585f8587
FB
1941 ret = find_snapshot_by_id(bs, name);
1942 if (ret >= 0)
1943 return ret;
1944 for(i = 0; i < s->nb_snapshots; i++) {
1945 if (!strcmp(s->snapshots[i].name, name))
1946 return i;
1947 }
1948 return -1;
1949}
1950
1951/* if no id is provided, a new one is constructed */
5fafdf24 1952static int qcow_snapshot_create(BlockDriverState *bs,
585f8587
FB
1953 QEMUSnapshotInfo *sn_info)
1954{
1955 BDRVQcowState *s = bs->opaque;
1956 QCowSnapshot *snapshots1, sn1, *sn = &sn1;
1957 int i, ret;
1958 uint64_t *l1_table = NULL;
3b46e624 1959
585f8587
FB
1960 memset(sn, 0, sizeof(*sn));
1961
1962 if (sn_info->id_str[0] == '\0') {
1963 /* compute a new id */
1964 find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
1965 }
1966
1967 /* check that the ID is unique */
1968 if (find_snapshot_by_id(bs, sn_info->id_str) >= 0)
1969 return -ENOENT;
1970
1971 sn->id_str = qemu_strdup(sn_info->id_str);
1972 if (!sn->id_str)
1973 goto fail;
1974 sn->name = qemu_strdup(sn_info->name);
1975 if (!sn->name)
1976 goto fail;
1977 sn->vm_state_size = sn_info->vm_state_size;
1978 sn->date_sec = sn_info->date_sec;
1979 sn->date_nsec = sn_info->date_nsec;
1980 sn->vm_clock_nsec = sn_info->vm_clock_nsec;
1981
1982 ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
1983 if (ret < 0)
1984 goto fail;
1985
1986 /* create the L1 table of the snapshot */
1987 sn->l1_table_offset = alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
1988 sn->l1_size = s->l1_size;
1989
1990 l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
1991 if (!l1_table)
1992 goto fail;
1993 for(i = 0; i < s->l1_size; i++) {
1994 l1_table[i] = cpu_to_be64(s->l1_table[i]);
1995 }
1996 if (bdrv_pwrite(s->hd, sn->l1_table_offset,
5fafdf24 1997 l1_table, s->l1_size * sizeof(uint64_t)) !=
585f8587
FB
1998 (s->l1_size * sizeof(uint64_t)))
1999 goto fail;
2000 qemu_free(l1_table);
2001 l1_table = NULL;
2002
2003 snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
2004 if (!snapshots1)
2005 goto fail;
2006 memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
2007 s->snapshots = snapshots1;
2008 s->snapshots[s->nb_snapshots++] = *sn;
2009
2010 if (qcow_write_snapshots(bs) < 0)
2011 goto fail;
2012#ifdef DEBUG_ALLOC
2013 check_refcounts(bs);
2014#endif
2015 return 0;
2016 fail:
2017 qemu_free(sn->name);
2018 qemu_free(l1_table);
2019 return -1;
2020}
2021
2022/* copy the snapshot 'snapshot_name' into the current disk image */
5fafdf24 2023static int qcow_snapshot_goto(BlockDriverState *bs,
585f8587
FB
2024 const char *snapshot_id)
2025{
2026 BDRVQcowState *s = bs->opaque;
2027 QCowSnapshot *sn;
2028 int i, snapshot_index, l1_size2;
2029
2030 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
2031 if (snapshot_index < 0)
2032 return -ENOENT;
2033 sn = &s->snapshots[snapshot_index];
2034
2035 if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0)
2036 goto fail;
2037
2038 if (grow_l1_table(bs, sn->l1_size) < 0)
2039 goto fail;
2040
2041 s->l1_size = sn->l1_size;
2042 l1_size2 = s->l1_size * sizeof(uint64_t);
2043 /* copy the snapshot l1 table to the current l1 table */
5fafdf24 2044 if (bdrv_pread(s->hd, sn->l1_table_offset,
585f8587
FB
2045 s->l1_table, l1_size2) != l1_size2)
2046 goto fail;
2047 if (bdrv_pwrite(s->hd, s->l1_table_offset,
2048 s->l1_table, l1_size2) != l1_size2)
2049 goto fail;
2050 for(i = 0;i < s->l1_size; i++) {
2051 be64_to_cpus(&s->l1_table[i]);
2052 }
2053
2054 if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
2055 goto fail;
2056
2057#ifdef DEBUG_ALLOC
2058 check_refcounts(bs);
2059#endif
2060 return 0;
2061 fail:
2062 return -EIO;
2063}
2064
2065static int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2066{
2067 BDRVQcowState *s = bs->opaque;
2068 QCowSnapshot *sn;
2069 int snapshot_index, ret;
3b46e624 2070
585f8587
FB
2071 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
2072 if (snapshot_index < 0)
2073 return -ENOENT;
2074 sn = &s->snapshots[snapshot_index];
2075
2076 ret = update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
2077 if (ret < 0)
2078 return ret;
2079 /* must update the copied flag on the current cluster offsets */
2080 ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
2081 if (ret < 0)
2082 return ret;
2083 free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
2084
2085 qemu_free(sn->id_str);
2086 qemu_free(sn->name);
2087 memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn));
2088 s->nb_snapshots--;
2089 ret = qcow_write_snapshots(bs);
2090 if (ret < 0) {
2091 /* XXX: restore snapshot if error ? */
2092 return ret;
2093 }
2094#ifdef DEBUG_ALLOC
2095 check_refcounts(bs);
2096#endif
2097 return 0;
2098}
2099
5fafdf24 2100static int qcow_snapshot_list(BlockDriverState *bs,
585f8587
FB
2101 QEMUSnapshotInfo **psn_tab)
2102{
2103 BDRVQcowState *s = bs->opaque;
2104 QEMUSnapshotInfo *sn_tab, *sn_info;
2105 QCowSnapshot *sn;
2106 int i;
2107
2108 sn_tab = qemu_mallocz(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
2109 if (!sn_tab)
2110 goto fail;
2111 for(i = 0; i < s->nb_snapshots; i++) {
2112 sn_info = sn_tab + i;
2113 sn = s->snapshots + i;
2114 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
2115 sn->id_str);
2116 pstrcpy(sn_info->name, sizeof(sn_info->name),
2117 sn->name);
2118 sn_info->vm_state_size = sn->vm_state_size;
2119 sn_info->date_sec = sn->date_sec;
2120 sn_info->date_nsec = sn->date_nsec;
2121 sn_info->vm_clock_nsec = sn->vm_clock_nsec;
2122 }
2123 *psn_tab = sn_tab;
2124 return s->nb_snapshots;
2125 fail:
2126 qemu_free(sn_tab);
2127 *psn_tab = NULL;
2128 return -ENOMEM;
2129}
2130
2131/*********************************************************/
2132/* refcount handling */
2133
2134static int refcount_init(BlockDriverState *bs)
2135{
2136 BDRVQcowState *s = bs->opaque;
2137 int ret, refcount_table_size2, i;
3b46e624 2138
585f8587
FB
2139 s->refcount_block_cache = qemu_malloc(s->cluster_size);
2140 if (!s->refcount_block_cache)
2141 goto fail;
2142 refcount_table_size2 = s->refcount_table_size * sizeof(uint64_t);
2143 s->refcount_table = qemu_malloc(refcount_table_size2);
2144 if (!s->refcount_table)
2145 goto fail;
2146 if (s->refcount_table_size > 0) {
2147 ret = bdrv_pread(s->hd, s->refcount_table_offset,
2148 s->refcount_table, refcount_table_size2);
2149 if (ret != refcount_table_size2)
2150 goto fail;
2151 for(i = 0; i < s->refcount_table_size; i++)
2152 be64_to_cpus(&s->refcount_table[i]);
2153 }
2154 return 0;
2155 fail:
2156 return -ENOMEM;
2157}
2158
2159static void refcount_close(BlockDriverState *bs)
2160{
2161 BDRVQcowState *s = bs->opaque;
2162 qemu_free(s->refcount_block_cache);
2163 qemu_free(s->refcount_table);
2164}
2165
2166
5fafdf24 2167static int load_refcount_block(BlockDriverState *bs,
585f8587
FB
2168 int64_t refcount_block_offset)
2169{
2170 BDRVQcowState *s = bs->opaque;
2171 int ret;
5fafdf24 2172 ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
585f8587
FB
2173 s->cluster_size);
2174 if (ret != s->cluster_size)
2175 return -EIO;
2176 s->refcount_block_cache_offset = refcount_block_offset;
2177 return 0;
2178}
2179
2180static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
2181{
2182 BDRVQcowState *s = bs->opaque;
2183 int refcount_table_index, block_index;
2184 int64_t refcount_block_offset;
2185
2186 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2187 if (refcount_table_index >= s->refcount_table_size)
2188 return 0;
2189 refcount_block_offset = s->refcount_table[refcount_table_index];
2190 if (!refcount_block_offset)
2191 return 0;
2192 if (refcount_block_offset != s->refcount_block_cache_offset) {
2193 /* better than nothing: return allocated if read error */
2194 if (load_refcount_block(bs, refcount_block_offset) < 0)
2195 return 1;
2196 }
5fafdf24 2197 block_index = cluster_index &
585f8587
FB
2198 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2199 return be16_to_cpu(s->refcount_block_cache[block_index]);
2200}
2201
2202/* return < 0 if error */
2203static int64_t alloc_clusters_noref(BlockDriverState *bs, int64_t size)
2204{
2205 BDRVQcowState *s = bs->opaque;
2206 int i, nb_clusters;
2207
2208 nb_clusters = (size + s->cluster_size - 1) >> s->cluster_bits;
2209 for(;;) {
2210 if (get_refcount(bs, s->free_cluster_index) == 0) {
2211 s->free_cluster_index++;
2212 for(i = 1; i < nb_clusters; i++) {
2213 if (get_refcount(bs, s->free_cluster_index) != 0)
2214 goto not_found;
2215 s->free_cluster_index++;
2216 }
2217#ifdef DEBUG_ALLOC2
2218 printf("alloc_clusters: size=%lld -> %lld\n",
5fafdf24 2219 size,
585f8587
FB
2220 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
2221#endif
2222 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
2223 } else {
2224 not_found:
2225 s->free_cluster_index++;
2226 }
2227 }
2228}
2229
2230static int64_t alloc_clusters(BlockDriverState *bs, int64_t size)
2231{
2232 int64_t offset;
2233
2234 offset = alloc_clusters_noref(bs, size);
2235 update_refcount(bs, offset, size, 1);
2236 return offset;
2237}
2238
2239/* only used to allocate compressed sectors. We try to allocate
2240 contiguous sectors. size must be <= cluster_size */
2241static int64_t alloc_bytes(BlockDriverState *bs, int size)
2242{
2243 BDRVQcowState *s = bs->opaque;
2244 int64_t offset, cluster_offset;
2245 int free_in_cluster;
3b46e624 2246
585f8587
FB
2247 assert(size > 0 && size <= s->cluster_size);
2248 if (s->free_byte_offset == 0) {
2249 s->free_byte_offset = alloc_clusters(bs, s->cluster_size);
2250 }
2251 redo:
5fafdf24 2252 free_in_cluster = s->cluster_size -
585f8587
FB
2253 (s->free_byte_offset & (s->cluster_size - 1));
2254 if (size <= free_in_cluster) {
2255 /* enough space in current cluster */
2256 offset = s->free_byte_offset;
2257 s->free_byte_offset += size;
2258 free_in_cluster -= size;
2259 if (free_in_cluster == 0)
2260 s->free_byte_offset = 0;
2261 if ((offset & (s->cluster_size - 1)) != 0)
2262 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2263 } else {
2264 offset = alloc_clusters(bs, s->cluster_size);
2265 cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
2266 if ((cluster_offset + s->cluster_size) == offset) {
2267 /* we are lucky: contiguous data */
2268 offset = s->free_byte_offset;
2269 update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
2270 s->free_byte_offset += size;
2271 } else {
2272 s->free_byte_offset = offset;
2273 goto redo;
2274 }
2275 }
2276 return offset;
2277}
2278
5fafdf24 2279static void free_clusters(BlockDriverState *bs,
585f8587
FB
2280 int64_t offset, int64_t size)
2281{
2282 update_refcount(bs, offset, size, -1);
2283}
2284
2285static int grow_refcount_table(BlockDriverState *bs, int min_size)
2286{
2287 BDRVQcowState *s = bs->opaque;
2288 int new_table_size, new_table_size2, refcount_table_clusters, i, ret;
2289 uint64_t *new_table;
2290 int64_t table_offset;
2291 uint64_t data64;
2292 uint32_t data32;
23be50f1
TS
2293 int old_table_size;
2294 int64_t old_table_offset;
585f8587
FB
2295
2296 if (min_size <= s->refcount_table_size)
2297 return 0;
2298 /* compute new table size */
2299 refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2300 for(;;) {
2301 if (refcount_table_clusters == 0) {
2302 refcount_table_clusters = 1;
2303 } else {
2304 refcount_table_clusters = (refcount_table_clusters * 3 + 1) / 2;
2305 }
2306 new_table_size = refcount_table_clusters << (s->cluster_bits - 3);
2307 if (min_size <= new_table_size)
2308 break;
2309 }
15e6690a
FB
2310#ifdef DEBUG_ALLOC2
2311 printf("grow_refcount_table from %d to %d\n",
2312 s->refcount_table_size,
2313 new_table_size);
2314#endif
585f8587
FB
2315 new_table_size2 = new_table_size * sizeof(uint64_t);
2316 new_table = qemu_mallocz(new_table_size2);
2317 if (!new_table)
2318 return -ENOMEM;
5fafdf24 2319 memcpy(new_table, s->refcount_table,
585f8587
FB
2320 s->refcount_table_size * sizeof(uint64_t));
2321 for(i = 0; i < s->refcount_table_size; i++)
2322 cpu_to_be64s(&new_table[i]);
2323 /* Note: we cannot update the refcount now to avoid recursion */
2324 table_offset = alloc_clusters_noref(bs, new_table_size2);
2325 ret = bdrv_pwrite(s->hd, table_offset, new_table, new_table_size2);
5fafdf24 2326 if (ret != new_table_size2)
585f8587
FB
2327 goto fail;
2328 for(i = 0; i < s->refcount_table_size; i++)
2329 be64_to_cpus(&new_table[i]);
2330
2331 data64 = cpu_to_be64(table_offset);
2332 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_offset),
2333 &data64, sizeof(data64)) != sizeof(data64))
2334 goto fail;
2335 data32 = cpu_to_be32(refcount_table_clusters);
2336 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, refcount_table_clusters),
2337 &data32, sizeof(data32)) != sizeof(data32))
2338 goto fail;
2339 qemu_free(s->refcount_table);
23be50f1
TS
2340 old_table_offset = s->refcount_table_offset;
2341 old_table_size = s->refcount_table_size;
585f8587
FB
2342 s->refcount_table = new_table;
2343 s->refcount_table_size = new_table_size;
a4080ece 2344 s->refcount_table_offset = table_offset;
585f8587
FB
2345
2346 update_refcount(bs, table_offset, new_table_size2, 1);
23be50f1 2347 free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
585f8587
FB
2348 return 0;
2349 fail:
2350 free_clusters(bs, table_offset, new_table_size2);
2351 qemu_free(new_table);
2352 return -EIO;
2353}
2354
2355/* addend must be 1 or -1 */
2356/* XXX: cache several refcount block clusters ? */
5fafdf24 2357static int update_cluster_refcount(BlockDriverState *bs,
585f8587
FB
2358 int64_t cluster_index,
2359 int addend)
2360{
2361 BDRVQcowState *s = bs->opaque;
2362 int64_t offset, refcount_block_offset;
2363 int ret, refcount_table_index, block_index, refcount;
2364 uint64_t data64;
2365
2366 refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
2367 if (refcount_table_index >= s->refcount_table_size) {
2368 if (addend < 0)
2369 return -EINVAL;
2370 ret = grow_refcount_table(bs, refcount_table_index + 1);
2371 if (ret < 0)
2372 return ret;
2373 }
2374 refcount_block_offset = s->refcount_table[refcount_table_index];
2375 if (!refcount_block_offset) {
2376 if (addend < 0)
2377 return -EINVAL;
2378 /* create a new refcount block */
2379 /* Note: we cannot update the refcount now to avoid recursion */
2380 offset = alloc_clusters_noref(bs, s->cluster_size);
2381 memset(s->refcount_block_cache, 0, s->cluster_size);
2382 ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
2383 if (ret != s->cluster_size)
2384 return -EINVAL;
2385 s->refcount_table[refcount_table_index] = offset;
2386 data64 = cpu_to_be64(offset);
5fafdf24
TS
2387 ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
2388 refcount_table_index * sizeof(uint64_t),
585f8587
FB
2389 &data64, sizeof(data64));
2390 if (ret != sizeof(data64))
2391 return -EINVAL;
2392
2393 refcount_block_offset = offset;
2394 s->refcount_block_cache_offset = offset;
2395 update_refcount(bs, offset, s->cluster_size, 1);
2396 } else {
2397 if (refcount_block_offset != s->refcount_block_cache_offset) {
2398 if (load_refcount_block(bs, refcount_block_offset) < 0)
2399 return -EIO;
2400 }
2401 }
2402 /* we can update the count and save it */
5fafdf24 2403 block_index = cluster_index &
585f8587
FB
2404 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2405 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
2406 refcount += addend;
2407 if (refcount < 0 || refcount > 0xffff)
2408 return -EINVAL;
2409 if (refcount == 0 && cluster_index < s->free_cluster_index) {
2410 s->free_cluster_index = cluster_index;
2411 }
2412 s->refcount_block_cache[block_index] = cpu_to_be16(refcount);
5fafdf24
TS
2413 if (bdrv_pwrite(s->hd,
2414 refcount_block_offset + (block_index << REFCOUNT_SHIFT),
585f8587
FB
2415 &s->refcount_block_cache[block_index], 2) != 2)
2416 return -EIO;
2417 return refcount;
2418}
2419
5fafdf24
TS
2420static void update_refcount(BlockDriverState *bs,
2421 int64_t offset, int64_t length,
585f8587
FB
2422 int addend)
2423{
2424 BDRVQcowState *s = bs->opaque;
2425 int64_t start, last, cluster_offset;
2426
2427#ifdef DEBUG_ALLOC2
5fafdf24 2428 printf("update_refcount: offset=%lld size=%lld addend=%d\n",
585f8587
FB
2429 offset, length, addend);
2430#endif
2431 if (length <= 0)
2432 return;
2433 start = offset & ~(s->cluster_size - 1);
2434 last = (offset + length - 1) & ~(s->cluster_size - 1);
5fafdf24 2435 for(cluster_offset = start; cluster_offset <= last;
585f8587
FB
2436 cluster_offset += s->cluster_size) {
2437 update_cluster_refcount(bs, cluster_offset >> s->cluster_bits, addend);
2438 }
2439}
2440
2441#ifdef DEBUG_ALLOC
5fafdf24
TS
2442static void inc_refcounts(BlockDriverState *bs,
2443 uint16_t *refcount_table,
585f8587
FB
2444 int refcount_table_size,
2445 int64_t offset, int64_t size)
2446{
2447 BDRVQcowState *s = bs->opaque;
2448 int64_t start, last, cluster_offset;
2449 int k;
3b46e624 2450
585f8587
FB
2451 if (size <= 0)
2452 return;
2453
2454 start = offset & ~(s->cluster_size - 1);
2455 last = (offset + size - 1) & ~(s->cluster_size - 1);
5fafdf24 2456 for(cluster_offset = start; cluster_offset <= last;
585f8587
FB
2457 cluster_offset += s->cluster_size) {
2458 k = cluster_offset >> s->cluster_bits;
2459 if (k < 0 || k >= refcount_table_size) {
2460 printf("ERROR: invalid cluster offset=0x%llx\n", cluster_offset);
2461 } else {
2462 if (++refcount_table[k] == 0) {
2463 printf("ERROR: overflow cluster offset=0x%llx\n", cluster_offset);
2464 }
2465 }
2466 }
2467}
2468
5fafdf24
TS
2469static int check_refcounts_l1(BlockDriverState *bs,
2470 uint16_t *refcount_table,
585f8587
FB
2471 int refcount_table_size,
2472 int64_t l1_table_offset, int l1_size,
2473 int check_copied)
2474{
2475 BDRVQcowState *s = bs->opaque;
2476 uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2;
2477 int l2_size, i, j, nb_csectors, refcount;
2478
2479 l2_table = NULL;
2480 l1_size2 = l1_size * sizeof(uint64_t);
2481
2482 inc_refcounts(bs, refcount_table, refcount_table_size,
2483 l1_table_offset, l1_size2);
2484
2485 l1_table = qemu_malloc(l1_size2);
2486 if (!l1_table)
2487 goto fail;
5fafdf24 2488 if (bdrv_pread(s->hd, l1_table_offset,
585f8587
FB
2489 l1_table, l1_size2) != l1_size2)
2490 goto fail;
2491 for(i = 0;i < l1_size; i++)
2492 be64_to_cpus(&l1_table[i]);
3b46e624 2493
585f8587
FB
2494 l2_size = s->l2_size * sizeof(uint64_t);
2495 l2_table = qemu_malloc(l2_size);
2496 if (!l2_table)
2497 goto fail;
2498 for(i = 0; i < l1_size; i++) {
2499 l2_offset = l1_table[i];
2500 if (l2_offset) {
2501 if (check_copied) {
2502 refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2503 if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
2504 printf("ERROR OFLAG_COPIED: l2_offset=%llx refcount=%d\n",
2505 l2_offset, refcount);
2506 }
2507 }
2508 l2_offset &= ~QCOW_OFLAG_COPIED;
2509 if (bdrv_pread(s->hd, l2_offset, l2_table, l2_size) != l2_size)
2510 goto fail;
2511 for(j = 0; j < s->l2_size; j++) {
2512 offset = be64_to_cpu(l2_table[j]);
2513 if (offset != 0) {
2514 if (offset & QCOW_OFLAG_COMPRESSED) {
2515 if (offset & QCOW_OFLAG_COPIED) {
2516 printf("ERROR: cluster %lld: copied flag must never be set for compressed clusters\n",
2517 offset >> s->cluster_bits);
2518 offset &= ~QCOW_OFLAG_COPIED;
2519 }
5fafdf24 2520 nb_csectors = ((offset >> s->csize_shift) &
585f8587
FB
2521 s->csize_mask) + 1;
2522 offset &= s->cluster_offset_mask;
5fafdf24 2523 inc_refcounts(bs, refcount_table,
585f8587
FB
2524 refcount_table_size,
2525 offset & ~511, nb_csectors * 512);
2526 } else {
2527 if (check_copied) {
2528 refcount = get_refcount(bs, (offset & ~QCOW_OFLAG_COPIED) >> s->cluster_bits);
2529 if ((refcount == 1) != ((offset & QCOW_OFLAG_COPIED) != 0)) {
2530 printf("ERROR OFLAG_COPIED: offset=%llx refcount=%d\n",
2531 offset, refcount);
2532 }
2533 }
2534 offset &= ~QCOW_OFLAG_COPIED;
5fafdf24 2535 inc_refcounts(bs, refcount_table,
585f8587
FB
2536 refcount_table_size,
2537 offset, s->cluster_size);
2538 }
2539 }
2540 }
5fafdf24 2541 inc_refcounts(bs, refcount_table,
585f8587
FB
2542 refcount_table_size,
2543 l2_offset,
2544 s->cluster_size);
2545 }
2546 }
2547 qemu_free(l1_table);
2548 qemu_free(l2_table);
2549 return 0;
2550 fail:
2551 printf("ERROR: I/O error in check_refcounts_l1\n");
2552 qemu_free(l1_table);
2553 qemu_free(l2_table);
2554 return -EIO;
2555}
2556
2557static void check_refcounts(BlockDriverState *bs)
2558{
2559 BDRVQcowState *s = bs->opaque;
2560 int64_t size;
2561 int nb_clusters, refcount1, refcount2, i;
2562 QCowSnapshot *sn;
2563 uint16_t *refcount_table;
2564
2565 size = bdrv_getlength(s->hd);
2566 nb_clusters = (size + s->cluster_size - 1) >> s->cluster_bits;
2567 refcount_table = qemu_mallocz(nb_clusters * sizeof(uint16_t));
15e6690a 2568
585f8587
FB
2569 /* header */
2570 inc_refcounts(bs, refcount_table, nb_clusters,
2571 0, s->cluster_size);
3b46e624 2572
585f8587
FB
2573 check_refcounts_l1(bs, refcount_table, nb_clusters,
2574 s->l1_table_offset, s->l1_size, 1);
2575
2576 /* snapshots */
2577 for(i = 0; i < s->nb_snapshots; i++) {
2578 sn = s->snapshots + i;
2579 check_refcounts_l1(bs, refcount_table, nb_clusters,
2580 sn->l1_table_offset, sn->l1_size, 0);
2581 }
2582 inc_refcounts(bs, refcount_table, nb_clusters,
2583 s->snapshots_offset, s->snapshots_size);
2584
2585 /* refcount data */
2586 inc_refcounts(bs, refcount_table, nb_clusters,
5fafdf24 2587 s->refcount_table_offset,
585f8587
FB
2588 s->refcount_table_size * sizeof(uint64_t));
2589 for(i = 0; i < s->refcount_table_size; i++) {
2590 int64_t offset;
2591 offset = s->refcount_table[i];
2592 if (offset != 0) {
2593 inc_refcounts(bs, refcount_table, nb_clusters,
2594 offset, s->cluster_size);
2595 }
2596 }
2597
2598 /* compare ref counts */
2599 for(i = 0; i < nb_clusters; i++) {
2600 refcount1 = get_refcount(bs, i);
2601 refcount2 = refcount_table[i];
2602 if (refcount1 != refcount2)
2603 printf("ERROR cluster %d refcount=%d reference=%d\n",
2604 i, refcount1, refcount2);
2605 }
2606
2607 qemu_free(refcount_table);
2608}
2609
2610#if 0
2611static void dump_refcounts(BlockDriverState *bs)
2612{
2613 BDRVQcowState *s = bs->opaque;
2614 int64_t nb_clusters, k, k1, size;
2615 int refcount;
2616
2617 size = bdrv_getlength(s->hd);
2618 nb_clusters = (size + s->cluster_size - 1) >> s->cluster_bits;
2619 for(k = 0; k < nb_clusters;) {
2620 k1 = k;
2621 refcount = get_refcount(bs, k);
2622 k++;
2623 while (k < nb_clusters && get_refcount(bs, k) == refcount)
2624 k++;
2625 printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
2626 }
2627}
2628#endif
2629#endif
2630
2631BlockDriver bdrv_qcow2 = {
2632 "qcow2",
2633 sizeof(BDRVQcowState),
2634 qcow_probe,
2635 qcow_open,
2636 NULL,
2637 NULL,
2638 qcow_close,
2639 qcow_create,
2640 qcow_flush,
2641 qcow_is_allocated,
2642 qcow_set_key,
2643 qcow_make_empty,
2644
585f8587
FB
2645 .bdrv_aio_read = qcow_aio_read,
2646 .bdrv_aio_write = qcow_aio_write,
2647 .bdrv_aio_cancel = qcow_aio_cancel,
ce1a14dc 2648 .aiocb_size = sizeof(QCowAIOCB),
585f8587
FB
2649 .bdrv_write_compressed = qcow_write_compressed,
2650
2651 .bdrv_snapshot_create = qcow_snapshot_create,
2652 .bdrv_snapshot_goto = qcow_snapshot_goto,
2653 .bdrv_snapshot_delete = qcow_snapshot_delete,
2654 .bdrv_snapshot_list = qcow_snapshot_list,
2655 .bdrv_get_info = qcow_get_info,
2656};