]>
Commit | Line | Data |
---|---|---|
ea2384d3 FB |
1 | /* |
2 | * Block driver for the QCOW format | |
5fafdf24 | 3 | * |
83f64091 | 4 | * Copyright (c) 2004-2006 Fabrice Bellard |
5fafdf24 | 5 | * |
ea2384d3 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" |
ea2384d3 | 25 | #include "block_int.h" |
28d34b82 | 26 | #include <zlib.h> |
ea2384d3 FB |
27 | #include "aes.h" |
28 | ||
29 | /**************************************************************/ | |
30 | /* QEMU COW block driver with compression and encryption support */ | |
31 | ||
32 | #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) | |
33 | #define QCOW_VERSION 1 | |
34 | ||
35 | #define QCOW_CRYPT_NONE 0 | |
36 | #define QCOW_CRYPT_AES 1 | |
37 | ||
38 | #define QCOW_OFLAG_COMPRESSED (1LL << 63) | |
39 | ||
40 | typedef struct QCowHeader { | |
41 | uint32_t magic; | |
42 | uint32_t version; | |
43 | uint64_t backing_file_offset; | |
44 | uint32_t backing_file_size; | |
45 | uint32_t mtime; | |
46 | uint64_t size; /* in bytes */ | |
47 | uint8_t cluster_bits; | |
48 | uint8_t l2_bits; | |
49 | uint32_t crypt_method; | |
50 | uint64_t l1_table_offset; | |
51 | } QCowHeader; | |
52 | ||
53 | #define L2_CACHE_SIZE 16 | |
54 | ||
55 | typedef struct BDRVQcowState { | |
83f64091 | 56 | BlockDriverState *hd; |
ea2384d3 FB |
57 | int cluster_bits; |
58 | int cluster_size; | |
59 | int cluster_sectors; | |
60 | int l2_bits; | |
61 | int l2_size; | |
62 | int l1_size; | |
63 | uint64_t cluster_offset_mask; | |
64 | uint64_t l1_table_offset; | |
65 | uint64_t *l1_table; | |
66 | uint64_t *l2_cache; | |
67 | uint64_t l2_cache_offsets[L2_CACHE_SIZE]; | |
68 | uint32_t l2_cache_counts[L2_CACHE_SIZE]; | |
69 | uint8_t *cluster_cache; | |
70 | uint8_t *cluster_data; | |
71 | uint64_t cluster_cache_offset; | |
72 | uint32_t crypt_method; /* current crypt method, 0 if no key yet */ | |
73 | uint32_t crypt_method_header; | |
74 | AES_KEY aes_encrypt_key; | |
75 | AES_KEY aes_decrypt_key; | |
76 | } BDRVQcowState; | |
77 | ||
78 | static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset); | |
79 | ||
80 | static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) | |
81 | { | |
82 | const QCowHeader *cow_header = (const void *)buf; | |
3b46e624 | 83 | |
712e7874 FB |
84 | if (buf_size >= sizeof(QCowHeader) && |
85 | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && | |
5fafdf24 | 86 | be32_to_cpu(cow_header->version) == QCOW_VERSION) |
ea2384d3 FB |
87 | return 100; |
88 | else | |
89 | return 0; | |
90 | } | |
91 | ||
83f64091 | 92 | static int qcow_open(BlockDriverState *bs, const char *filename, int flags) |
ea2384d3 FB |
93 | { |
94 | BDRVQcowState *s = bs->opaque; | |
83f64091 | 95 | int len, i, shift, ret; |
ea2384d3 | 96 | QCowHeader header; |
83f64091 | 97 | |
b5eff355 | 98 | ret = bdrv_file_open(&s->hd, filename, flags); |
83f64091 FB |
99 | if (ret < 0) |
100 | return ret; | |
101 | if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header)) | |
ea2384d3 FB |
102 | goto fail; |
103 | be32_to_cpus(&header.magic); | |
104 | be32_to_cpus(&header.version); | |
105 | be64_to_cpus(&header.backing_file_offset); | |
106 | be32_to_cpus(&header.backing_file_size); | |
107 | be32_to_cpus(&header.mtime); | |
108 | be64_to_cpus(&header.size); | |
109 | be32_to_cpus(&header.crypt_method); | |
110 | be64_to_cpus(&header.l1_table_offset); | |
3b46e624 | 111 | |
ea2384d3 FB |
112 | if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION) |
113 | goto fail; | |
114 | if (header.size <= 1 || header.cluster_bits < 9) | |
115 | goto fail; | |
116 | if (header.crypt_method > QCOW_CRYPT_AES) | |
117 | goto fail; | |
118 | s->crypt_method_header = header.crypt_method; | |
119 | if (s->crypt_method_header) | |
120 | bs->encrypted = 1; | |
121 | s->cluster_bits = header.cluster_bits; | |
122 | s->cluster_size = 1 << s->cluster_bits; | |
123 | s->cluster_sectors = 1 << (s->cluster_bits - 9); | |
124 | s->l2_bits = header.l2_bits; | |
125 | s->l2_size = 1 << s->l2_bits; | |
126 | bs->total_sectors = header.size / 512; | |
127 | s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1; | |
128 | ||
129 | /* read the level 1 table */ | |
130 | shift = s->cluster_bits + s->l2_bits; | |
131 | s->l1_size = (header.size + (1LL << shift) - 1) >> shift; | |
132 | ||
133 | s->l1_table_offset = header.l1_table_offset; | |
134 | s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t)); | |
135 | if (!s->l1_table) | |
136 | goto fail; | |
5fafdf24 | 137 | if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
ea2384d3 FB |
138 | s->l1_size * sizeof(uint64_t)) |
139 | goto fail; | |
140 | for(i = 0;i < s->l1_size; i++) { | |
141 | be64_to_cpus(&s->l1_table[i]); | |
142 | } | |
143 | /* alloc L2 cache */ | |
144 | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); | |
145 | if (!s->l2_cache) | |
146 | goto fail; | |
147 | s->cluster_cache = qemu_malloc(s->cluster_size); | |
148 | if (!s->cluster_cache) | |
149 | goto fail; | |
150 | s->cluster_data = qemu_malloc(s->cluster_size); | |
151 | if (!s->cluster_data) | |
152 | goto fail; | |
153 | s->cluster_cache_offset = -1; | |
3b46e624 | 154 | |
ea2384d3 FB |
155 | /* read the backing file name */ |
156 | if (header.backing_file_offset != 0) { | |
157 | len = header.backing_file_size; | |
158 | if (len > 1023) | |
159 | len = 1023; | |
83f64091 | 160 | if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len) |
ea2384d3 FB |
161 | goto fail; |
162 | bs->backing_file[len] = '\0'; | |
163 | } | |
164 | return 0; | |
165 | ||
166 | fail: | |
167 | qemu_free(s->l1_table); | |
168 | qemu_free(s->l2_cache); | |
169 | qemu_free(s->cluster_cache); | |
170 | qemu_free(s->cluster_data); | |
83f64091 | 171 | bdrv_delete(s->hd); |
ea2384d3 FB |
172 | return -1; |
173 | } | |
174 | ||
175 | static int qcow_set_key(BlockDriverState *bs, const char *key) | |
176 | { | |
177 | BDRVQcowState *s = bs->opaque; | |
178 | uint8_t keybuf[16]; | |
179 | int len, i; | |
3b46e624 | 180 | |
ea2384d3 FB |
181 | memset(keybuf, 0, 16); |
182 | len = strlen(key); | |
183 | if (len > 16) | |
184 | len = 16; | |
185 | /* XXX: we could compress the chars to 7 bits to increase | |
186 | entropy */ | |
187 | for(i = 0;i < len;i++) { | |
188 | keybuf[i] = key[i]; | |
189 | } | |
190 | s->crypt_method = s->crypt_method_header; | |
191 | ||
192 | if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) | |
193 | return -1; | |
194 | if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) | |
195 | return -1; | |
196 | #if 0 | |
197 | /* test */ | |
198 | { | |
199 | uint8_t in[16]; | |
200 | uint8_t out[16]; | |
201 | uint8_t tmp[16]; | |
202 | for(i=0;i<16;i++) | |
203 | in[i] = i; | |
204 | AES_encrypt(in, tmp, &s->aes_encrypt_key); | |
205 | AES_decrypt(tmp, out, &s->aes_decrypt_key); | |
206 | for(i = 0; i < 16; i++) | |
207 | printf(" %02x", tmp[i]); | |
208 | printf("\n"); | |
209 | for(i = 0; i < 16; i++) | |
210 | printf(" %02x", out[i]); | |
211 | printf("\n"); | |
212 | } | |
213 | #endif | |
214 | return 0; | |
215 | } | |
216 | ||
217 | /* The crypt function is compatible with the linux cryptoloop | |
218 | algorithm for < 4 GB images. NOTE: out_buf == in_buf is | |
219 | supported */ | |
220 | static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num, | |
221 | uint8_t *out_buf, const uint8_t *in_buf, | |
222 | int nb_sectors, int enc, | |
223 | const AES_KEY *key) | |
224 | { | |
225 | union { | |
226 | uint64_t ll[2]; | |
227 | uint8_t b[16]; | |
228 | } ivec; | |
229 | int i; | |
230 | ||
231 | for(i = 0; i < nb_sectors; i++) { | |
232 | ivec.ll[0] = cpu_to_le64(sector_num); | |
233 | ivec.ll[1] = 0; | |
5fafdf24 | 234 | AES_cbc_encrypt(in_buf, out_buf, 512, key, |
ea2384d3 FB |
235 | ivec.b, enc); |
236 | sector_num++; | |
237 | in_buf += 512; | |
238 | out_buf += 512; | |
239 | } | |
240 | } | |
241 | ||
242 | /* 'allocate' is: | |
243 | * | |
244 | * 0 to not allocate. | |
245 | * | |
246 | * 1 to allocate a normal cluster (for sector indexes 'n_start' to | |
247 | * 'n_end') | |
248 | * | |
249 | * 2 to allocate a compressed cluster of size | |
250 | * 'compressed_size'. 'compressed_size' must be > 0 and < | |
5fafdf24 | 251 | * cluster_size |
ea2384d3 FB |
252 | * |
253 | * return 0 if not allocated. | |
254 | */ | |
255 | static uint64_t get_cluster_offset(BlockDriverState *bs, | |
256 | uint64_t offset, int allocate, | |
257 | int compressed_size, | |
258 | int n_start, int n_end) | |
259 | { | |
260 | BDRVQcowState *s = bs->opaque; | |
261 | int min_index, i, j, l1_index, l2_index; | |
262 | uint64_t l2_offset, *l2_table, cluster_offset, tmp; | |
263 | uint32_t min_count; | |
264 | int new_l2_table; | |
3b46e624 | 265 | |
ea2384d3 FB |
266 | l1_index = offset >> (s->l2_bits + s->cluster_bits); |
267 | l2_offset = s->l1_table[l1_index]; | |
268 | new_l2_table = 0; | |
269 | if (!l2_offset) { | |
270 | if (!allocate) | |
271 | return 0; | |
272 | /* allocate a new l2 entry */ | |
83f64091 | 273 | l2_offset = bdrv_getlength(s->hd); |
ea2384d3 FB |
274 | /* round to cluster size */ |
275 | l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1); | |
276 | /* update the L1 entry */ | |
277 | s->l1_table[l1_index] = l2_offset; | |
278 | tmp = cpu_to_be64(l2_offset); | |
5fafdf24 | 279 | if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp), |
83f64091 | 280 | &tmp, sizeof(tmp)) != sizeof(tmp)) |
ea2384d3 FB |
281 | return 0; |
282 | new_l2_table = 1; | |
283 | } | |
284 | for(i = 0; i < L2_CACHE_SIZE; i++) { | |
285 | if (l2_offset == s->l2_cache_offsets[i]) { | |
286 | /* increment the hit count */ | |
287 | if (++s->l2_cache_counts[i] == 0xffffffff) { | |
288 | for(j = 0; j < L2_CACHE_SIZE; j++) { | |
289 | s->l2_cache_counts[j] >>= 1; | |
290 | } | |
291 | } | |
292 | l2_table = s->l2_cache + (i << s->l2_bits); | |
293 | goto found; | |
294 | } | |
295 | } | |
296 | /* not found: load a new entry in the least used one */ | |
297 | min_index = 0; | |
298 | min_count = 0xffffffff; | |
299 | for(i = 0; i < L2_CACHE_SIZE; i++) { | |
300 | if (s->l2_cache_counts[i] < min_count) { | |
301 | min_count = s->l2_cache_counts[i]; | |
302 | min_index = i; | |
303 | } | |
304 | } | |
305 | l2_table = s->l2_cache + (min_index << s->l2_bits); | |
ea2384d3 FB |
306 | if (new_l2_table) { |
307 | memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); | |
83f64091 | 308 | if (bdrv_pwrite(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) != |
ea2384d3 FB |
309 | s->l2_size * sizeof(uint64_t)) |
310 | return 0; | |
311 | } else { | |
5fafdf24 | 312 | if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) != |
ea2384d3 FB |
313 | s->l2_size * sizeof(uint64_t)) |
314 | return 0; | |
315 | } | |
316 | s->l2_cache_offsets[min_index] = l2_offset; | |
317 | s->l2_cache_counts[min_index] = 1; | |
318 | found: | |
319 | l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1); | |
320 | cluster_offset = be64_to_cpu(l2_table[l2_index]); | |
5fafdf24 | 321 | if (!cluster_offset || |
ea2384d3 FB |
322 | ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) { |
323 | if (!allocate) | |
324 | return 0; | |
325 | /* allocate a new cluster */ | |
326 | if ((cluster_offset & QCOW_OFLAG_COMPRESSED) && | |
327 | (n_end - n_start) < s->cluster_sectors) { | |
328 | /* if the cluster is already compressed, we must | |
329 | decompress it in the case it is not completely | |
330 | overwritten */ | |
331 | if (decompress_cluster(s, cluster_offset) < 0) | |
332 | return 0; | |
83f64091 | 333 | cluster_offset = bdrv_getlength(s->hd); |
5fafdf24 | 334 | cluster_offset = (cluster_offset + s->cluster_size - 1) & |
ea2384d3 FB |
335 | ~(s->cluster_size - 1); |
336 | /* write the cluster content */ | |
5fafdf24 | 337 | if (bdrv_pwrite(s->hd, cluster_offset, s->cluster_cache, s->cluster_size) != |
ea2384d3 FB |
338 | s->cluster_size) |
339 | return -1; | |
340 | } else { | |
83f64091 | 341 | cluster_offset = bdrv_getlength(s->hd); |
7f48fa1f AL |
342 | if (allocate == 1) { |
343 | /* round to cluster size */ | |
344 | cluster_offset = (cluster_offset + s->cluster_size - 1) & | |
345 | ~(s->cluster_size - 1); | |
346 | bdrv_truncate(s->hd, cluster_offset + s->cluster_size); | |
347 | /* if encrypted, we must initialize the cluster | |
348 | content which won't be written */ | |
349 | if (s->crypt_method && | |
350 | (n_end - n_start) < s->cluster_sectors) { | |
351 | uint64_t start_sect; | |
352 | start_sect = (offset & ~(s->cluster_size - 1)) >> 9; | |
353 | memset(s->cluster_data + 512, 0x00, 512); | |
354 | for(i = 0; i < s->cluster_sectors; i++) { | |
355 | if (i < n_start || i >= n_end) { | |
356 | encrypt_sectors(s, start_sect + i, | |
357 | s->cluster_data, | |
358 | s->cluster_data + 512, 1, 1, | |
359 | &s->aes_encrypt_key); | |
360 | if (bdrv_pwrite(s->hd, cluster_offset + i * 512, | |
361 | s->cluster_data, 512) != 512) | |
362 | return -1; | |
363 | } | |
ea2384d3 FB |
364 | } |
365 | } | |
7f48fa1f AL |
366 | } else if (allocate == 2) { |
367 | cluster_offset |= QCOW_OFLAG_COMPRESSED | | |
368 | (uint64_t)compressed_size << (63 - s->cluster_bits); | |
ea2384d3 FB |
369 | } |
370 | } | |
371 | /* update L2 table */ | |
372 | tmp = cpu_to_be64(cluster_offset); | |
373 | l2_table[l2_index] = tmp; | |
5fafdf24 | 374 | if (bdrv_pwrite(s->hd, |
83f64091 | 375 | l2_offset + l2_index * sizeof(tmp), &tmp, sizeof(tmp)) != sizeof(tmp)) |
ea2384d3 FB |
376 | return 0; |
377 | } | |
378 | return cluster_offset; | |
379 | } | |
380 | ||
5fafdf24 | 381 | static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
ea2384d3 FB |
382 | int nb_sectors, int *pnum) |
383 | { | |
384 | BDRVQcowState *s = bs->opaque; | |
385 | int index_in_cluster, n; | |
386 | uint64_t cluster_offset; | |
387 | ||
388 | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0); | |
389 | index_in_cluster = sector_num & (s->cluster_sectors - 1); | |
390 | n = s->cluster_sectors - index_in_cluster; | |
391 | if (n > nb_sectors) | |
392 | n = nb_sectors; | |
393 | *pnum = n; | |
394 | return (cluster_offset != 0); | |
395 | } | |
396 | ||
397 | static int decompress_buffer(uint8_t *out_buf, int out_buf_size, | |
398 | const uint8_t *buf, int buf_size) | |
399 | { | |
400 | z_stream strm1, *strm = &strm1; | |
401 | int ret, out_len; | |
402 | ||
403 | memset(strm, 0, sizeof(*strm)); | |
404 | ||
405 | strm->next_in = (uint8_t *)buf; | |
406 | strm->avail_in = buf_size; | |
407 | strm->next_out = out_buf; | |
408 | strm->avail_out = out_buf_size; | |
409 | ||
410 | ret = inflateInit2(strm, -12); | |
411 | if (ret != Z_OK) | |
412 | return -1; | |
413 | ret = inflate(strm, Z_FINISH); | |
414 | out_len = strm->next_out - out_buf; | |
415 | if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) || | |
416 | out_len != out_buf_size) { | |
417 | inflateEnd(strm); | |
418 | return -1; | |
419 | } | |
420 | inflateEnd(strm); | |
421 | return 0; | |
422 | } | |
3b46e624 | 423 | |
ea2384d3 FB |
424 | static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset) |
425 | { | |
426 | int ret, csize; | |
427 | uint64_t coffset; | |
428 | ||
429 | coffset = cluster_offset & s->cluster_offset_mask; | |
430 | if (s->cluster_cache_offset != coffset) { | |
431 | csize = cluster_offset >> (63 - s->cluster_bits); | |
432 | csize &= (s->cluster_size - 1); | |
83f64091 | 433 | ret = bdrv_pread(s->hd, coffset, s->cluster_data, csize); |
5fafdf24 | 434 | if (ret != csize) |
ea2384d3 FB |
435 | return -1; |
436 | if (decompress_buffer(s->cluster_cache, s->cluster_size, | |
437 | s->cluster_data, csize) < 0) { | |
438 | return -1; | |
439 | } | |
440 | s->cluster_cache_offset = coffset; | |
441 | } | |
442 | return 0; | |
443 | } | |
444 | ||
83f64091 FB |
445 | #if 0 |
446 | ||
5fafdf24 | 447 | static int qcow_read(BlockDriverState *bs, int64_t sector_num, |
ea2384d3 FB |
448 | uint8_t *buf, int nb_sectors) |
449 | { | |
450 | BDRVQcowState *s = bs->opaque; | |
451 | int ret, index_in_cluster, n; | |
452 | uint64_t cluster_offset; | |
3b46e624 | 453 | |
ea2384d3 FB |
454 | while (nb_sectors > 0) { |
455 | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0); | |
456 | index_in_cluster = sector_num & (s->cluster_sectors - 1); | |
457 | n = s->cluster_sectors - index_in_cluster; | |
458 | if (n > nb_sectors) | |
459 | n = nb_sectors; | |
460 | if (!cluster_offset) { | |
83f64091 FB |
461 | if (bs->backing_hd) { |
462 | /* read from the base image */ | |
463 | ret = bdrv_read(bs->backing_hd, sector_num, buf, n); | |
464 | if (ret < 0) | |
465 | return -1; | |
466 | } else { | |
467 | memset(buf, 0, 512 * n); | |
468 | } | |
ea2384d3 FB |
469 | } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { |
470 | if (decompress_cluster(s, cluster_offset) < 0) | |
471 | return -1; | |
472 | memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n); | |
473 | } else { | |
83f64091 | 474 | ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512); |
5fafdf24 | 475 | if (ret != n * 512) |
ea2384d3 FB |
476 | return -1; |
477 | if (s->crypt_method) { | |
5fafdf24 | 478 | encrypt_sectors(s, sector_num, buf, buf, n, 0, |
ea2384d3 FB |
479 | &s->aes_decrypt_key); |
480 | } | |
481 | } | |
482 | nb_sectors -= n; | |
483 | sector_num += n; | |
484 | buf += n * 512; | |
485 | } | |
486 | return 0; | |
487 | } | |
83f64091 | 488 | #endif |
ea2384d3 | 489 | |
5fafdf24 | 490 | static int qcow_write(BlockDriverState *bs, int64_t sector_num, |
ea2384d3 FB |
491 | const uint8_t *buf, int nb_sectors) |
492 | { | |
493 | BDRVQcowState *s = bs->opaque; | |
494 | int ret, index_in_cluster, n; | |
495 | uint64_t cluster_offset; | |
3b46e624 | 496 | |
ea2384d3 FB |
497 | while (nb_sectors > 0) { |
498 | index_in_cluster = sector_num & (s->cluster_sectors - 1); | |
499 | n = s->cluster_sectors - index_in_cluster; | |
500 | if (n > nb_sectors) | |
501 | n = nb_sectors; | |
5fafdf24 TS |
502 | cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0, |
503 | index_in_cluster, | |
ea2384d3 FB |
504 | index_in_cluster + n); |
505 | if (!cluster_offset) | |
506 | return -1; | |
ea2384d3 FB |
507 | if (s->crypt_method) { |
508 | encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1, | |
509 | &s->aes_encrypt_key); | |
5fafdf24 | 510 | ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, |
83f64091 | 511 | s->cluster_data, n * 512); |
ea2384d3 | 512 | } else { |
83f64091 | 513 | ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512); |
ea2384d3 | 514 | } |
5fafdf24 | 515 | if (ret != n * 512) |
ea2384d3 FB |
516 | return -1; |
517 | nb_sectors -= n; | |
518 | sector_num += n; | |
519 | buf += n * 512; | |
520 | } | |
521 | s->cluster_cache_offset = -1; /* disable compressed cache */ | |
522 | return 0; | |
523 | } | |
524 | ||
ce1a14dc PB |
525 | typedef struct QCowAIOCB { |
526 | BlockDriverAIOCB common; | |
83f64091 FB |
527 | int64_t sector_num; |
528 | uint8_t *buf; | |
529 | int nb_sectors; | |
530 | int n; | |
531 | uint64_t cluster_offset; | |
5fafdf24 | 532 | uint8_t *cluster_data; |
c87c0672 AL |
533 | struct iovec hd_iov; |
534 | QEMUIOVector hd_qiov; | |
83f64091 | 535 | BlockDriverAIOCB *hd_aiocb; |
83f64091 FB |
536 | } QCowAIOCB; |
537 | ||
83f64091 FB |
538 | static void qcow_aio_read_cb(void *opaque, int ret) |
539 | { | |
ce1a14dc PB |
540 | QCowAIOCB *acb = opaque; |
541 | BlockDriverState *bs = acb->common.bs; | |
83f64091 | 542 | BDRVQcowState *s = bs->opaque; |
83f64091 FB |
543 | int index_in_cluster; |
544 | ||
ce1a14dc | 545 | acb->hd_aiocb = NULL; |
83f64091 FB |
546 | if (ret < 0) { |
547 | fail: | |
ce1a14dc PB |
548 | acb->common.cb(acb->common.opaque, ret); |
549 | qemu_aio_release(acb); | |
83f64091 FB |
550 | return; |
551 | } | |
552 | ||
553 | redo: | |
554 | /* post process the read buffer */ | |
ce1a14dc | 555 | if (!acb->cluster_offset) { |
83f64091 | 556 | /* nothing to do */ |
ce1a14dc | 557 | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
83f64091 FB |
558 | /* nothing to do */ |
559 | } else { | |
560 | if (s->crypt_method) { | |
5fafdf24 TS |
561 | encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, |
562 | acb->n, 0, | |
83f64091 FB |
563 | &s->aes_decrypt_key); |
564 | } | |
565 | } | |
566 | ||
ce1a14dc PB |
567 | acb->nb_sectors -= acb->n; |
568 | acb->sector_num += acb->n; | |
569 | acb->buf += acb->n * 512; | |
83f64091 | 570 | |
ce1a14dc | 571 | if (acb->nb_sectors == 0) { |
83f64091 | 572 | /* request completed */ |
ce1a14dc PB |
573 | acb->common.cb(acb->common.opaque, 0); |
574 | qemu_aio_release(acb); | |
83f64091 FB |
575 | return; |
576 | } | |
3b46e624 | 577 | |
83f64091 | 578 | /* prepare next AIO request */ |
5fafdf24 | 579 | acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, |
ce1a14dc PB |
580 | 0, 0, 0, 0); |
581 | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); | |
582 | acb->n = s->cluster_sectors - index_in_cluster; | |
583 | if (acb->n > acb->nb_sectors) | |
584 | acb->n = acb->nb_sectors; | |
585 | ||
586 | if (!acb->cluster_offset) { | |
83f64091 FB |
587 | if (bs->backing_hd) { |
588 | /* read from the base image */ | |
c87c0672 AL |
589 | acb->hd_iov.iov_base = acb->buf; |
590 | acb->hd_iov.iov_len = acb->n * 512; | |
591 | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); | |
592 | acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num, | |
593 | &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb); | |
ce1a14dc | 594 | if (acb->hd_aiocb == NULL) |
83f64091 FB |
595 | goto fail; |
596 | } else { | |
597 | /* Note: in this case, no need to wait */ | |
ce1a14dc | 598 | memset(acb->buf, 0, 512 * acb->n); |
83f64091 FB |
599 | goto redo; |
600 | } | |
ce1a14dc | 601 | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
83f64091 | 602 | /* add AIO support for compressed blocks ? */ |
ce1a14dc | 603 | if (decompress_cluster(s, acb->cluster_offset) < 0) |
83f64091 | 604 | goto fail; |
5fafdf24 | 605 | memcpy(acb->buf, |
ce1a14dc | 606 | s->cluster_cache + index_in_cluster * 512, 512 * acb->n); |
83f64091 FB |
607 | goto redo; |
608 | } else { | |
ce1a14dc | 609 | if ((acb->cluster_offset & 511) != 0) { |
83f64091 FB |
610 | ret = -EIO; |
611 | goto fail; | |
612 | } | |
c87c0672 AL |
613 | acb->hd_iov.iov_base = acb->buf; |
614 | acb->hd_iov.iov_len = acb->n * 512; | |
615 | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); | |
616 | acb->hd_aiocb = bdrv_aio_readv(s->hd, | |
5fafdf24 | 617 | (acb->cluster_offset >> 9) + index_in_cluster, |
c87c0672 | 618 | &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb); |
ce1a14dc | 619 | if (acb->hd_aiocb == NULL) |
83f64091 FB |
620 | goto fail; |
621 | } | |
622 | } | |
623 | ||
ce1a14dc PB |
624 | static BlockDriverAIOCB *qcow_aio_read(BlockDriverState *bs, |
625 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
626 | BlockDriverCompletionFunc *cb, void *opaque) | |
83f64091 | 627 | { |
ce1a14dc PB |
628 | QCowAIOCB *acb; |
629 | ||
630 | acb = qemu_aio_get(bs, cb, opaque); | |
631 | if (!acb) | |
632 | return NULL; | |
633 | acb->hd_aiocb = NULL; | |
634 | acb->sector_num = sector_num; | |
635 | acb->buf = buf; | |
636 | acb->nb_sectors = nb_sectors; | |
637 | acb->n = 0; | |
3b46e624 | 638 | acb->cluster_offset = 0; |
83f64091 FB |
639 | |
640 | qcow_aio_read_cb(acb, 0); | |
ce1a14dc | 641 | return &acb->common; |
83f64091 FB |
642 | } |
643 | ||
644 | static void qcow_aio_write_cb(void *opaque, int ret) | |
645 | { | |
ce1a14dc PB |
646 | QCowAIOCB *acb = opaque; |
647 | BlockDriverState *bs = acb->common.bs; | |
83f64091 | 648 | BDRVQcowState *s = bs->opaque; |
83f64091 FB |
649 | int index_in_cluster; |
650 | uint64_t cluster_offset; | |
651 | const uint8_t *src_buf; | |
ce1a14dc PB |
652 | |
653 | acb->hd_aiocb = NULL; | |
654 | ||
83f64091 FB |
655 | if (ret < 0) { |
656 | fail: | |
ce1a14dc PB |
657 | acb->common.cb(acb->common.opaque, ret); |
658 | qemu_aio_release(acb); | |
83f64091 FB |
659 | return; |
660 | } | |
661 | ||
ce1a14dc PB |
662 | acb->nb_sectors -= acb->n; |
663 | acb->sector_num += acb->n; | |
664 | acb->buf += acb->n * 512; | |
83f64091 | 665 | |
ce1a14dc | 666 | if (acb->nb_sectors == 0) { |
83f64091 | 667 | /* request completed */ |
ce1a14dc PB |
668 | acb->common.cb(acb->common.opaque, 0); |
669 | qemu_aio_release(acb); | |
83f64091 FB |
670 | return; |
671 | } | |
3b46e624 | 672 | |
ce1a14dc PB |
673 | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1); |
674 | acb->n = s->cluster_sectors - index_in_cluster; | |
675 | if (acb->n > acb->nb_sectors) | |
676 | acb->n = acb->nb_sectors; | |
5fafdf24 TS |
677 | cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, 1, 0, |
678 | index_in_cluster, | |
ce1a14dc | 679 | index_in_cluster + acb->n); |
83f64091 FB |
680 | if (!cluster_offset || (cluster_offset & 511) != 0) { |
681 | ret = -EIO; | |
682 | goto fail; | |
683 | } | |
684 | if (s->crypt_method) { | |
ce1a14dc PB |
685 | if (!acb->cluster_data) { |
686 | acb->cluster_data = qemu_mallocz(s->cluster_size); | |
687 | if (!acb->cluster_data) { | |
83f64091 FB |
688 | ret = -ENOMEM; |
689 | goto fail; | |
690 | } | |
691 | } | |
5fafdf24 | 692 | encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf, |
ce1a14dc PB |
693 | acb->n, 1, &s->aes_encrypt_key); |
694 | src_buf = acb->cluster_data; | |
83f64091 | 695 | } else { |
ce1a14dc | 696 | src_buf = acb->buf; |
83f64091 | 697 | } |
c87c0672 AL |
698 | |
699 | acb->hd_iov.iov_base = (void *)src_buf; | |
700 | acb->hd_iov.iov_len = acb->n * 512; | |
701 | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); | |
702 | acb->hd_aiocb = bdrv_aio_writev(s->hd, | |
703 | (cluster_offset >> 9) + index_in_cluster, | |
704 | &acb->hd_qiov, acb->n, | |
705 | qcow_aio_write_cb, acb); | |
ce1a14dc | 706 | if (acb->hd_aiocb == NULL) |
83f64091 FB |
707 | goto fail; |
708 | } | |
709 | ||
ce1a14dc PB |
710 | static BlockDriverAIOCB *qcow_aio_write(BlockDriverState *bs, |
711 | int64_t sector_num, const uint8_t *buf, int nb_sectors, | |
712 | BlockDriverCompletionFunc *cb, void *opaque) | |
83f64091 | 713 | { |
83f64091 | 714 | BDRVQcowState *s = bs->opaque; |
ce1a14dc | 715 | QCowAIOCB *acb; |
3b46e624 | 716 | |
83f64091 FB |
717 | s->cluster_cache_offset = -1; /* disable compressed cache */ |
718 | ||
ce1a14dc PB |
719 | acb = qemu_aio_get(bs, cb, opaque); |
720 | if (!acb) | |
721 | return NULL; | |
722 | acb->hd_aiocb = NULL; | |
723 | acb->sector_num = sector_num; | |
724 | acb->buf = (uint8_t *)buf; | |
725 | acb->nb_sectors = nb_sectors; | |
726 | acb->n = 0; | |
3b46e624 | 727 | |
83f64091 | 728 | qcow_aio_write_cb(acb, 0); |
ce1a14dc | 729 | return &acb->common; |
83f64091 FB |
730 | } |
731 | ||
ce1a14dc | 732 | static void qcow_aio_cancel(BlockDriverAIOCB *blockacb) |
83f64091 | 733 | { |
ce1a14dc PB |
734 | QCowAIOCB *acb = (QCowAIOCB *)blockacb; |
735 | if (acb->hd_aiocb) | |
736 | bdrv_aio_cancel(acb->hd_aiocb); | |
737 | qemu_aio_release(acb); | |
83f64091 FB |
738 | } |
739 | ||
e2731add | 740 | static void qcow_close(BlockDriverState *bs) |
ea2384d3 FB |
741 | { |
742 | BDRVQcowState *s = bs->opaque; | |
743 | qemu_free(s->l1_table); | |
744 | qemu_free(s->l2_cache); | |
745 | qemu_free(s->cluster_cache); | |
746 | qemu_free(s->cluster_data); | |
83f64091 | 747 | bdrv_delete(s->hd); |
ea2384d3 FB |
748 | } |
749 | ||
750 | static int qcow_create(const char *filename, int64_t total_size, | |
751 | const char *backing_file, int flags) | |
752 | { | |
753 | int fd, header_size, backing_filename_len, l1_size, i, shift; | |
754 | QCowHeader header; | |
ea2384d3 | 755 | uint64_t tmp; |
ea2384d3 | 756 | |
83f64091 | 757 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); |
ea2384d3 FB |
758 | if (fd < 0) |
759 | return -1; | |
760 | memset(&header, 0, sizeof(header)); | |
761 | header.magic = cpu_to_be32(QCOW_MAGIC); | |
762 | header.version = cpu_to_be32(QCOW_VERSION); | |
763 | header.size = cpu_to_be64(total_size * 512); | |
764 | header_size = sizeof(header); | |
765 | backing_filename_len = 0; | |
766 | if (backing_file) { | |
7852e5da AJ |
767 | if (strcmp(backing_file, "fat:")) { |
768 | header.backing_file_offset = cpu_to_be64(header_size); | |
769 | backing_filename_len = strlen(backing_file); | |
770 | header.backing_file_size = cpu_to_be32(backing_filename_len); | |
771 | header_size += backing_filename_len; | |
772 | } else { | |
773 | /* special backing file for vvfat */ | |
774 | backing_file = NULL; | |
775 | } | |
ea2384d3 FB |
776 | header.cluster_bits = 9; /* 512 byte cluster to avoid copying |
777 | unmodifyed sectors */ | |
778 | header.l2_bits = 12; /* 32 KB L2 tables */ | |
779 | } else { | |
780 | header.cluster_bits = 12; /* 4 KB clusters */ | |
781 | header.l2_bits = 9; /* 4 KB L2 tables */ | |
782 | } | |
783 | header_size = (header_size + 7) & ~7; | |
784 | shift = header.cluster_bits + header.l2_bits; | |
785 | l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift; | |
786 | ||
787 | header.l1_table_offset = cpu_to_be64(header_size); | |
ec36ba14 | 788 | if (flags & BLOCK_FLAG_ENCRYPT) { |
ea2384d3 FB |
789 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); |
790 | } else { | |
791 | header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); | |
792 | } | |
3b46e624 | 793 | |
ea2384d3 FB |
794 | /* write all the data */ |
795 | write(fd, &header, sizeof(header)); | |
796 | if (backing_file) { | |
83f64091 | 797 | write(fd, backing_file, backing_filename_len); |
ea2384d3 FB |
798 | } |
799 | lseek(fd, header_size, SEEK_SET); | |
800 | tmp = 0; | |
801 | for(i = 0;i < l1_size; i++) { | |
802 | write(fd, &tmp, sizeof(tmp)); | |
803 | } | |
804 | close(fd); | |
805 | return 0; | |
806 | } | |
807 | ||
c47c33b0 | 808 | static int qcow_make_empty(BlockDriverState *bs) |
95389c86 FB |
809 | { |
810 | BDRVQcowState *s = bs->opaque; | |
811 | uint32_t l1_length = s->l1_size * sizeof(uint64_t); | |
83f64091 | 812 | int ret; |
95389c86 FB |
813 | |
814 | memset(s->l1_table, 0, l1_length); | |
83f64091 | 815 | if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0) |
95389c86 | 816 | return -1; |
83f64091 FB |
817 | ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length); |
818 | if (ret < 0) | |
819 | return ret; | |
95389c86 FB |
820 | |
821 | memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); | |
822 | memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t)); | |
823 | memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t)); | |
824 | ||
825 | return 0; | |
826 | } | |
827 | ||
ea2384d3 FB |
828 | /* XXX: put compressed sectors first, then all the cluster aligned |
829 | tables to avoid losing bytes in alignment */ | |
5fafdf24 | 830 | static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, |
c47c33b0 | 831 | const uint8_t *buf, int nb_sectors) |
ea2384d3 FB |
832 | { |
833 | BDRVQcowState *s = bs->opaque; | |
834 | z_stream strm; | |
835 | int ret, out_len; | |
836 | uint8_t *out_buf; | |
837 | uint64_t cluster_offset; | |
838 | ||
c47c33b0 FB |
839 | if (nb_sectors != s->cluster_sectors) |
840 | return -EINVAL; | |
ea2384d3 FB |
841 | |
842 | out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); | |
843 | if (!out_buf) | |
844 | return -1; | |
845 | ||
846 | /* best compression, small window, no zlib header */ | |
847 | memset(&strm, 0, sizeof(strm)); | |
848 | ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, | |
5fafdf24 | 849 | Z_DEFLATED, -12, |
ea2384d3 FB |
850 | 9, Z_DEFAULT_STRATEGY); |
851 | if (ret != 0) { | |
852 | qemu_free(out_buf); | |
853 | return -1; | |
854 | } | |
855 | ||
856 | strm.avail_in = s->cluster_size; | |
857 | strm.next_in = (uint8_t *)buf; | |
858 | strm.avail_out = s->cluster_size; | |
859 | strm.next_out = out_buf; | |
860 | ||
861 | ret = deflate(&strm, Z_FINISH); | |
862 | if (ret != Z_STREAM_END && ret != Z_OK) { | |
863 | qemu_free(out_buf); | |
864 | deflateEnd(&strm); | |
865 | return -1; | |
866 | } | |
867 | out_len = strm.next_out - out_buf; | |
868 | ||
869 | deflateEnd(&strm); | |
870 | ||
871 | if (ret != Z_STREAM_END || out_len >= s->cluster_size) { | |
872 | /* could not compress: write normal cluster */ | |
873 | qcow_write(bs, sector_num, buf, s->cluster_sectors); | |
874 | } else { | |
5fafdf24 | 875 | cluster_offset = get_cluster_offset(bs, sector_num << 9, 2, |
ea2384d3 FB |
876 | out_len, 0, 0); |
877 | cluster_offset &= s->cluster_offset_mask; | |
83f64091 | 878 | if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) { |
ea2384d3 FB |
879 | qemu_free(out_buf); |
880 | return -1; | |
881 | } | |
882 | } | |
3b46e624 | 883 | |
ea2384d3 FB |
884 | qemu_free(out_buf); |
885 | return 0; | |
886 | } | |
887 | ||
7a6cba61 PB |
888 | static void qcow_flush(BlockDriverState *bs) |
889 | { | |
890 | BDRVQcowState *s = bs->opaque; | |
83f64091 | 891 | bdrv_flush(s->hd); |
7a6cba61 PB |
892 | } |
893 | ||
c47c33b0 FB |
894 | static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
895 | { | |
896 | BDRVQcowState *s = bs->opaque; | |
897 | bdi->cluster_size = s->cluster_size; | |
898 | return 0; | |
899 | } | |
900 | ||
ea2384d3 | 901 | BlockDriver bdrv_qcow = { |
e60f469c AJ |
902 | .format_name = "qcow", |
903 | .instance_size = sizeof(BDRVQcowState), | |
904 | .bdrv_probe = qcow_probe, | |
905 | .bdrv_open = qcow_open, | |
906 | .bdrv_close = qcow_close, | |
907 | .bdrv_create = qcow_create, | |
908 | .bdrv_flush = qcow_flush, | |
909 | .bdrv_is_allocated = qcow_is_allocated, | |
910 | .bdrv_set_key = qcow_set_key, | |
911 | .bdrv_make_empty = qcow_make_empty, | |
912 | .bdrv_aio_read = qcow_aio_read, | |
913 | .bdrv_aio_write = qcow_aio_write, | |
914 | .bdrv_aio_cancel = qcow_aio_cancel, | |
915 | .aiocb_size = sizeof(QCowAIOCB), | |
c47c33b0 | 916 | .bdrv_write_compressed = qcow_write_compressed, |
e60f469c | 917 | .bdrv_get_info = qcow_get_info, |
ea2384d3 | 918 | }; |