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