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