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