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