root / block / qcow2.c @ 72893756
History | View | Annotate | Download (41.1 kB)
1 | 585f8587 | bellard | /*
|
---|---|---|---|
2 | 585f8587 | bellard | * Block driver for the QCOW version 2 format
|
3 | 5fafdf24 | ths | *
|
4 | 585f8587 | bellard | * Copyright (c) 2004-2006 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 585f8587 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 585f8587 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 585f8587 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 585f8587 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 585f8587 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 585f8587 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 585f8587 | bellard | *
|
13 | 585f8587 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 585f8587 | bellard | * all copies or substantial portions of the Software.
|
15 | 585f8587 | bellard | *
|
16 | 585f8587 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 585f8587 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 585f8587 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 585f8587 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 585f8587 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 585f8587 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 585f8587 | bellard | * THE SOFTWARE.
|
23 | 585f8587 | bellard | */
|
24 | faf07963 | pbrook | #include "qemu-common.h" |
25 | 585f8587 | bellard | #include "block_int.h" |
26 | 5efa9d5a | Anthony Liguori | #include "module.h" |
27 | 585f8587 | bellard | #include <zlib.h> |
28 | 585f8587 | bellard | #include "aes.h" |
29 | f7d0fe02 | Kevin Wolf | #include "block/qcow2.h" |
30 | 585f8587 | bellard | |
31 | 585f8587 | bellard | /*
|
32 | 585f8587 | bellard | Differences with QCOW:
|
33 | 585f8587 | bellard | |
34 | 585f8587 | bellard | - Support for multiple incremental snapshots.
|
35 | 585f8587 | bellard | - Memory management by reference counts.
|
36 | 585f8587 | bellard | - Clusters which have a reference count of one have the bit
|
37 | 585f8587 | bellard | QCOW_OFLAG_COPIED to optimize write performance.
|
38 | 5fafdf24 | ths | - Size of compressed clusters is stored in sectors to reduce bit usage
|
39 | 585f8587 | bellard | in the cluster offsets.
|
40 | 585f8587 | bellard | - Support for storing additional data (such as the VM state) in the
|
41 | 3b46e624 | ths | snapshots.
|
42 | 585f8587 | bellard | - If a backing store is used, the cluster size is not constrained
|
43 | 585f8587 | bellard | (could be backported to QCOW).
|
44 | 585f8587 | bellard | - L2 tables have always a size of one cluster.
|
45 | 585f8587 | bellard | */
|
46 | 585f8587 | bellard | |
47 | 9b80ddf3 | aliguori | |
48 | 9b80ddf3 | aliguori | typedef struct { |
49 | 9b80ddf3 | aliguori | uint32_t magic; |
50 | 9b80ddf3 | aliguori | uint32_t len; |
51 | 9b80ddf3 | aliguori | } QCowExtension; |
52 | 9b80ddf3 | aliguori | #define QCOW_EXT_MAGIC_END 0 |
53 | f965509c | aliguori | #define QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA |
54 | 9b80ddf3 | aliguori | |
55 | 585f8587 | bellard | static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) |
56 | 585f8587 | bellard | { |
57 | 585f8587 | bellard | const QCowHeader *cow_header = (const void *)buf; |
58 | 3b46e624 | ths | |
59 | 585f8587 | bellard | if (buf_size >= sizeof(QCowHeader) && |
60 | 585f8587 | bellard | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && |
61 | 5fafdf24 | ths | be32_to_cpu(cow_header->version) == QCOW_VERSION) |
62 | 585f8587 | bellard | return 100; |
63 | 585f8587 | bellard | else
|
64 | 585f8587 | bellard | return 0; |
65 | 585f8587 | bellard | } |
66 | 585f8587 | bellard | |
67 | 9b80ddf3 | aliguori | |
68 | 9b80ddf3 | aliguori | /*
|
69 | 9b80ddf3 | aliguori | * read qcow2 extension and fill bs
|
70 | 9b80ddf3 | aliguori | * start reading from start_offset
|
71 | 9b80ddf3 | aliguori | * finish reading upon magic of value 0 or when end_offset reached
|
72 | 9b80ddf3 | aliguori | * unknown magic is skipped (future extension this version knows nothing about)
|
73 | 9b80ddf3 | aliguori | * return 0 upon success, non-0 otherwise
|
74 | 9b80ddf3 | aliguori | */
|
75 | 9b80ddf3 | aliguori | static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset, |
76 | 9b80ddf3 | aliguori | uint64_t end_offset) |
77 | 9b80ddf3 | aliguori | { |
78 | 9b80ddf3 | aliguori | QCowExtension ext; |
79 | 9b80ddf3 | aliguori | uint64_t offset; |
80 | 9b80ddf3 | aliguori | |
81 | 9b80ddf3 | aliguori | #ifdef DEBUG_EXT
|
82 | 9b80ddf3 | aliguori | printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
|
83 | 9b80ddf3 | aliguori | #endif
|
84 | 9b80ddf3 | aliguori | offset = start_offset; |
85 | 9b80ddf3 | aliguori | while (offset < end_offset) {
|
86 | 9b80ddf3 | aliguori | |
87 | 9b80ddf3 | aliguori | #ifdef DEBUG_EXT
|
88 | 9b80ddf3 | aliguori | /* Sanity check */
|
89 | 9b80ddf3 | aliguori | if (offset > s->cluster_size)
|
90 | 9b80ddf3 | aliguori | printf("qcow_handle_extension: suspicious offset %lu\n", offset);
|
91 | 9b80ddf3 | aliguori | |
92 | 9b80ddf3 | aliguori | printf("attemting to read extended header in offset %lu\n", offset);
|
93 | 9b80ddf3 | aliguori | #endif
|
94 | 9b80ddf3 | aliguori | |
95 | 66f82cee | Kevin Wolf | if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) { |
96 | 0bfcd599 | Blue Swirl | fprintf(stderr, "qcow_handle_extension: ERROR: "
|
97 | 0bfcd599 | Blue Swirl | "pread fail from offset %" PRIu64 "\n", |
98 | 0bfcd599 | Blue Swirl | offset); |
99 | 9b80ddf3 | aliguori | return 1; |
100 | 9b80ddf3 | aliguori | } |
101 | 9b80ddf3 | aliguori | be32_to_cpus(&ext.magic); |
102 | 9b80ddf3 | aliguori | be32_to_cpus(&ext.len); |
103 | 9b80ddf3 | aliguori | offset += sizeof(ext);
|
104 | 9b80ddf3 | aliguori | #ifdef DEBUG_EXT
|
105 | 9b80ddf3 | aliguori | printf("ext.magic = 0x%x\n", ext.magic);
|
106 | 9b80ddf3 | aliguori | #endif
|
107 | 9b80ddf3 | aliguori | switch (ext.magic) {
|
108 | 9b80ddf3 | aliguori | case QCOW_EXT_MAGIC_END:
|
109 | 9b80ddf3 | aliguori | return 0; |
110 | f965509c | aliguori | |
111 | f965509c | aliguori | case QCOW_EXT_MAGIC_BACKING_FORMAT:
|
112 | f965509c | aliguori | if (ext.len >= sizeof(bs->backing_format)) { |
113 | f965509c | aliguori | fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
|
114 | 4c978075 | aliguori | " (>=%zu)\n",
|
115 | f965509c | aliguori | ext.len, sizeof(bs->backing_format));
|
116 | f965509c | aliguori | return 2; |
117 | f965509c | aliguori | } |
118 | 66f82cee | Kevin Wolf | if (bdrv_pread(bs->file, offset , bs->backing_format,
|
119 | f965509c | aliguori | ext.len) != ext.len) |
120 | f965509c | aliguori | return 3; |
121 | f965509c | aliguori | bs->backing_format[ext.len] = '\0';
|
122 | f965509c | aliguori | #ifdef DEBUG_EXT
|
123 | f965509c | aliguori | printf("Qcow2: Got format extension %s\n", bs->backing_format);
|
124 | f965509c | aliguori | #endif
|
125 | e1c7f0e3 | Kevin Wolf | offset = ((offset + ext.len + 7) & ~7); |
126 | f965509c | aliguori | break;
|
127 | f965509c | aliguori | |
128 | 9b80ddf3 | aliguori | default:
|
129 | 9b80ddf3 | aliguori | /* unknown magic -- just skip it */
|
130 | e1c7f0e3 | Kevin Wolf | offset = ((offset + ext.len + 7) & ~7); |
131 | 9b80ddf3 | aliguori | break;
|
132 | 9b80ddf3 | aliguori | } |
133 | 9b80ddf3 | aliguori | } |
134 | 9b80ddf3 | aliguori | |
135 | 9b80ddf3 | aliguori | return 0; |
136 | 9b80ddf3 | aliguori | } |
137 | 9b80ddf3 | aliguori | |
138 | 9b80ddf3 | aliguori | |
139 | 66f82cee | Kevin Wolf | static int qcow_open(BlockDriverState *bs, int flags) |
140 | 585f8587 | bellard | { |
141 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
142 | 419b19d9 | Stefan Hajnoczi | int len, i;
|
143 | 585f8587 | bellard | QCowHeader header; |
144 | 9b80ddf3 | aliguori | uint64_t ext_end; |
145 | 585f8587 | bellard | |
146 | 66f82cee | Kevin Wolf | if (bdrv_pread(bs->file, 0, &header, sizeof(header)) != sizeof(header)) |
147 | 585f8587 | bellard | goto fail;
|
148 | 585f8587 | bellard | be32_to_cpus(&header.magic); |
149 | 585f8587 | bellard | be32_to_cpus(&header.version); |
150 | 585f8587 | bellard | be64_to_cpus(&header.backing_file_offset); |
151 | 585f8587 | bellard | be32_to_cpus(&header.backing_file_size); |
152 | 585f8587 | bellard | be64_to_cpus(&header.size); |
153 | 585f8587 | bellard | be32_to_cpus(&header.cluster_bits); |
154 | 585f8587 | bellard | be32_to_cpus(&header.crypt_method); |
155 | 585f8587 | bellard | be64_to_cpus(&header.l1_table_offset); |
156 | 585f8587 | bellard | be32_to_cpus(&header.l1_size); |
157 | 585f8587 | bellard | be64_to_cpus(&header.refcount_table_offset); |
158 | 585f8587 | bellard | be32_to_cpus(&header.refcount_table_clusters); |
159 | 585f8587 | bellard | be64_to_cpus(&header.snapshots_offset); |
160 | 585f8587 | bellard | be32_to_cpus(&header.nb_snapshots); |
161 | 3b46e624 | ths | |
162 | 585f8587 | bellard | if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
|
163 | 585f8587 | bellard | goto fail;
|
164 | d191d12d | Stefan Weil | if (header.cluster_bits < MIN_CLUSTER_BITS ||
|
165 | 73c632ed | Kevin Wolf | header.cluster_bits > MAX_CLUSTER_BITS) |
166 | 585f8587 | bellard | goto fail;
|
167 | 585f8587 | bellard | if (header.crypt_method > QCOW_CRYPT_AES)
|
168 | 585f8587 | bellard | goto fail;
|
169 | 585f8587 | bellard | s->crypt_method_header = header.crypt_method; |
170 | 585f8587 | bellard | if (s->crypt_method_header)
|
171 | 585f8587 | bellard | bs->encrypted = 1;
|
172 | 585f8587 | bellard | s->cluster_bits = header.cluster_bits; |
173 | 585f8587 | bellard | s->cluster_size = 1 << s->cluster_bits;
|
174 | 585f8587 | bellard | s->cluster_sectors = 1 << (s->cluster_bits - 9); |
175 | 585f8587 | bellard | s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ |
176 | 585f8587 | bellard | s->l2_size = 1 << s->l2_bits;
|
177 | 585f8587 | bellard | bs->total_sectors = header.size / 512;
|
178 | 585f8587 | bellard | s->csize_shift = (62 - (s->cluster_bits - 8)); |
179 | 585f8587 | bellard | s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; |
180 | 585f8587 | bellard | s->cluster_offset_mask = (1LL << s->csize_shift) - 1; |
181 | 585f8587 | bellard | s->refcount_table_offset = header.refcount_table_offset; |
182 | 5fafdf24 | ths | s->refcount_table_size = |
183 | 585f8587 | bellard | header.refcount_table_clusters << (s->cluster_bits - 3);
|
184 | 585f8587 | bellard | |
185 | 585f8587 | bellard | s->snapshots_offset = header.snapshots_offset; |
186 | 585f8587 | bellard | s->nb_snapshots = header.nb_snapshots; |
187 | 585f8587 | bellard | |
188 | 585f8587 | bellard | /* read the level 1 table */
|
189 | 585f8587 | bellard | s->l1_size = header.l1_size; |
190 | 419b19d9 | Stefan Hajnoczi | s->l1_vm_state_index = size_to_l1(s, header.size); |
191 | 585f8587 | bellard | /* the L1 table must contain at least enough entries to put
|
192 | 585f8587 | bellard | header.size bytes */
|
193 | 585f8587 | bellard | if (s->l1_size < s->l1_vm_state_index)
|
194 | 585f8587 | bellard | goto fail;
|
195 | 585f8587 | bellard | s->l1_table_offset = header.l1_table_offset; |
196 | d191d12d | Stefan Weil | if (s->l1_size > 0) { |
197 | d191d12d | Stefan Weil | s->l1_table = qemu_mallocz( |
198 | d191d12d | Stefan Weil | align_offset(s->l1_size * sizeof(uint64_t), 512)); |
199 | 66f82cee | Kevin Wolf | if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
200 | d191d12d | Stefan Weil | s->l1_size * sizeof(uint64_t))
|
201 | d191d12d | Stefan Weil | goto fail;
|
202 | d191d12d | Stefan Weil | for(i = 0;i < s->l1_size; i++) { |
203 | d191d12d | Stefan Weil | be64_to_cpus(&s->l1_table[i]); |
204 | d191d12d | Stefan Weil | } |
205 | 585f8587 | bellard | } |
206 | 585f8587 | bellard | /* alloc L2 cache */
|
207 | 585f8587 | bellard | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
|
208 | 585f8587 | bellard | s->cluster_cache = qemu_malloc(s->cluster_size); |
209 | 585f8587 | bellard | /* one more sector for decompressed data alignment */
|
210 | 095a9c58 | aliguori | s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size |
211 | 095a9c58 | aliguori | + 512);
|
212 | 585f8587 | bellard | s->cluster_cache_offset = -1;
|
213 | 3b46e624 | ths | |
214 | ed6ccf0f | Kevin Wolf | if (qcow2_refcount_init(bs) < 0) |
215 | 585f8587 | bellard | goto fail;
|
216 | 585f8587 | bellard | |
217 | 72cf2d4f | Blue Swirl | QLIST_INIT(&s->cluster_allocs); |
218 | f214978a | Kevin Wolf | |
219 | 9b80ddf3 | aliguori | /* read qcow2 extensions */
|
220 | 9b80ddf3 | aliguori | if (header.backing_file_offset)
|
221 | 9b80ddf3 | aliguori | ext_end = header.backing_file_offset; |
222 | 9b80ddf3 | aliguori | else
|
223 | 9b80ddf3 | aliguori | ext_end = s->cluster_size; |
224 | 9b80ddf3 | aliguori | if (qcow_read_extensions(bs, sizeof(header), ext_end)) |
225 | 9b80ddf3 | aliguori | goto fail;
|
226 | 9b80ddf3 | aliguori | |
227 | 585f8587 | bellard | /* read the backing file name */
|
228 | 585f8587 | bellard | if (header.backing_file_offset != 0) { |
229 | 585f8587 | bellard | len = header.backing_file_size; |
230 | 585f8587 | bellard | if (len > 1023) |
231 | 585f8587 | bellard | len = 1023;
|
232 | 66f82cee | Kevin Wolf | if (bdrv_pread(bs->file, header.backing_file_offset, bs->backing_file, len) != len)
|
233 | 585f8587 | bellard | goto fail;
|
234 | 585f8587 | bellard | bs->backing_file[len] = '\0';
|
235 | 585f8587 | bellard | } |
236 | ed6ccf0f | Kevin Wolf | if (qcow2_read_snapshots(bs) < 0) |
237 | 585f8587 | bellard | goto fail;
|
238 | 585f8587 | bellard | |
239 | 585f8587 | bellard | #ifdef DEBUG_ALLOC
|
240 | 14899cdf | Filip Navara | qcow2_check_refcounts(bs); |
241 | 585f8587 | bellard | #endif
|
242 | 585f8587 | bellard | return 0; |
243 | 585f8587 | bellard | |
244 | 585f8587 | bellard | fail:
|
245 | ed6ccf0f | Kevin Wolf | qcow2_free_snapshots(bs); |
246 | ed6ccf0f | Kevin Wolf | qcow2_refcount_close(bs); |
247 | 585f8587 | bellard | qemu_free(s->l1_table); |
248 | 585f8587 | bellard | qemu_free(s->l2_cache); |
249 | 585f8587 | bellard | qemu_free(s->cluster_cache); |
250 | 585f8587 | bellard | qemu_free(s->cluster_data); |
251 | 585f8587 | bellard | return -1; |
252 | 585f8587 | bellard | } |
253 | 585f8587 | bellard | |
254 | 585f8587 | bellard | static int qcow_set_key(BlockDriverState *bs, const char *key) |
255 | 585f8587 | bellard | { |
256 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
257 | 585f8587 | bellard | uint8_t keybuf[16];
|
258 | 585f8587 | bellard | int len, i;
|
259 | 3b46e624 | ths | |
260 | 585f8587 | bellard | memset(keybuf, 0, 16); |
261 | 585f8587 | bellard | len = strlen(key); |
262 | 585f8587 | bellard | if (len > 16) |
263 | 585f8587 | bellard | len = 16;
|
264 | 585f8587 | bellard | /* XXX: we could compress the chars to 7 bits to increase
|
265 | 585f8587 | bellard | entropy */
|
266 | 585f8587 | bellard | for(i = 0;i < len;i++) { |
267 | 585f8587 | bellard | keybuf[i] = key[i]; |
268 | 585f8587 | bellard | } |
269 | 585f8587 | bellard | s->crypt_method = s->crypt_method_header; |
270 | 585f8587 | bellard | |
271 | 585f8587 | bellard | if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) |
272 | 585f8587 | bellard | return -1; |
273 | 585f8587 | bellard | if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) |
274 | 585f8587 | bellard | return -1; |
275 | 585f8587 | bellard | #if 0
|
276 | 585f8587 | bellard | /* test */
|
277 | 585f8587 | bellard | {
|
278 | 585f8587 | bellard | uint8_t in[16];
|
279 | 585f8587 | bellard | uint8_t out[16];
|
280 | 585f8587 | bellard | uint8_t tmp[16];
|
281 | 585f8587 | bellard | for(i=0;i<16;i++)
|
282 | 585f8587 | bellard | in[i] = i;
|
283 | 585f8587 | bellard | AES_encrypt(in, tmp, &s->aes_encrypt_key);
|
284 | 585f8587 | bellard | AES_decrypt(tmp, out, &s->aes_decrypt_key);
|
285 | 585f8587 | bellard | for(i = 0; i < 16; i++)
|
286 | 585f8587 | bellard | printf(" %02x", tmp[i]);
|
287 | 585f8587 | bellard | printf("\n");
|
288 | 585f8587 | bellard | for(i = 0; i < 16; i++)
|
289 | 585f8587 | bellard | printf(" %02x", out[i]);
|
290 | 585f8587 | bellard | printf("\n");
|
291 | 585f8587 | bellard | }
|
292 | 585f8587 | bellard | #endif
|
293 | 585f8587 | bellard | return 0; |
294 | 585f8587 | bellard | } |
295 | 585f8587 | bellard | |
296 | 5fafdf24 | ths | static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
297 | 585f8587 | bellard | int nb_sectors, int *pnum) |
298 | 585f8587 | bellard | { |
299 | 585f8587 | bellard | uint64_t cluster_offset; |
300 | 1c46efaa | Kevin Wolf | int ret;
|
301 | 585f8587 | bellard | |
302 | 095a9c58 | aliguori | *pnum = nb_sectors; |
303 | 1c46efaa | Kevin Wolf | /* FIXME We can get errors here, but the bdrv_is_allocated interface can't
|
304 | 1c46efaa | Kevin Wolf | * pass them on today */
|
305 | 1c46efaa | Kevin Wolf | ret = qcow2_get_cluster_offset(bs, sector_num << 9, pnum, &cluster_offset);
|
306 | 1c46efaa | Kevin Wolf | if (ret < 0) { |
307 | 1c46efaa | Kevin Wolf | *pnum = 0;
|
308 | 1c46efaa | Kevin Wolf | } |
309 | 095a9c58 | aliguori | |
310 | 585f8587 | bellard | return (cluster_offset != 0); |
311 | 585f8587 | bellard | } |
312 | 585f8587 | bellard | |
313 | a9465922 | bellard | /* handle reading after the end of the backing file */
|
314 | bd28f835 | Kevin Wolf | int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
|
315 | bd28f835 | Kevin Wolf | int64_t sector_num, int nb_sectors)
|
316 | a9465922 | bellard | { |
317 | a9465922 | bellard | int n1;
|
318 | a9465922 | bellard | if ((sector_num + nb_sectors) <= bs->total_sectors)
|
319 | a9465922 | bellard | return nb_sectors;
|
320 | a9465922 | bellard | if (sector_num >= bs->total_sectors)
|
321 | a9465922 | bellard | n1 = 0;
|
322 | a9465922 | bellard | else
|
323 | a9465922 | bellard | n1 = bs->total_sectors - sector_num; |
324 | bd28f835 | Kevin Wolf | |
325 | bd28f835 | Kevin Wolf | qemu_iovec_memset(qiov, 0, 512 * (nb_sectors - n1)); |
326 | bd28f835 | Kevin Wolf | |
327 | a9465922 | bellard | return n1;
|
328 | a9465922 | bellard | } |
329 | a9465922 | bellard | |
330 | ce1a14dc | pbrook | typedef struct QCowAIOCB { |
331 | ce1a14dc | pbrook | BlockDriverAIOCB common; |
332 | 585f8587 | bellard | int64_t sector_num; |
333 | f141eafe | aliguori | QEMUIOVector *qiov; |
334 | 7b88e48b | Christoph Hellwig | int remaining_sectors;
|
335 | 7b88e48b | Christoph Hellwig | int cur_nr_sectors; /* number of sectors in current iteration */ |
336 | bd28f835 | Kevin Wolf | uint64_t bytes_done; |
337 | 585f8587 | bellard | uint64_t cluster_offset; |
338 | 5fafdf24 | ths | uint8_t *cluster_data; |
339 | 585f8587 | bellard | BlockDriverAIOCB *hd_aiocb; |
340 | c87c0672 | aliguori | QEMUIOVector hd_qiov; |
341 | 1490791f | aliguori | QEMUBH *bh; |
342 | e976c6a1 | aliguori | QCowL2Meta l2meta; |
343 | 72cf2d4f | Blue Swirl | QLIST_ENTRY(QCowAIOCB) next_depend; |
344 | 585f8587 | bellard | } QCowAIOCB; |
345 | 585f8587 | bellard | |
346 | c16b5a2c | Christoph Hellwig | static void qcow_aio_cancel(BlockDriverAIOCB *blockacb) |
347 | c16b5a2c | Christoph Hellwig | { |
348 | b666d239 | Kevin Wolf | QCowAIOCB *acb = container_of(blockacb, QCowAIOCB, common); |
349 | c16b5a2c | Christoph Hellwig | if (acb->hd_aiocb)
|
350 | c16b5a2c | Christoph Hellwig | bdrv_aio_cancel(acb->hd_aiocb); |
351 | c16b5a2c | Christoph Hellwig | qemu_aio_release(acb); |
352 | c16b5a2c | Christoph Hellwig | } |
353 | c16b5a2c | Christoph Hellwig | |
354 | c16b5a2c | Christoph Hellwig | static AIOPool qcow_aio_pool = {
|
355 | c16b5a2c | Christoph Hellwig | .aiocb_size = sizeof(QCowAIOCB),
|
356 | c16b5a2c | Christoph Hellwig | .cancel = qcow_aio_cancel, |
357 | c16b5a2c | Christoph Hellwig | }; |
358 | c16b5a2c | Christoph Hellwig | |
359 | 1490791f | aliguori | static void qcow_aio_read_cb(void *opaque, int ret); |
360 | 1490791f | aliguori | static void qcow_aio_read_bh(void *opaque) |
361 | 1490791f | aliguori | { |
362 | 1490791f | aliguori | QCowAIOCB *acb = opaque; |
363 | 1490791f | aliguori | qemu_bh_delete(acb->bh); |
364 | 1490791f | aliguori | acb->bh = NULL;
|
365 | 1490791f | aliguori | qcow_aio_read_cb(opaque, 0);
|
366 | 1490791f | aliguori | } |
367 | 1490791f | aliguori | |
368 | a32ef786 | aliguori | static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb) |
369 | a32ef786 | aliguori | { |
370 | a32ef786 | aliguori | if (acb->bh)
|
371 | a32ef786 | aliguori | return -EIO;
|
372 | a32ef786 | aliguori | |
373 | a32ef786 | aliguori | acb->bh = qemu_bh_new(cb, acb); |
374 | a32ef786 | aliguori | if (!acb->bh)
|
375 | a32ef786 | aliguori | return -EIO;
|
376 | a32ef786 | aliguori | |
377 | a32ef786 | aliguori | qemu_bh_schedule(acb->bh); |
378 | a32ef786 | aliguori | |
379 | a32ef786 | aliguori | return 0; |
380 | a32ef786 | aliguori | } |
381 | a32ef786 | aliguori | |
382 | 585f8587 | bellard | static void qcow_aio_read_cb(void *opaque, int ret) |
383 | 585f8587 | bellard | { |
384 | ce1a14dc | pbrook | QCowAIOCB *acb = opaque; |
385 | ce1a14dc | pbrook | BlockDriverState *bs = acb->common.bs; |
386 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
387 | a9465922 | bellard | int index_in_cluster, n1;
|
388 | 585f8587 | bellard | |
389 | ce1a14dc | pbrook | acb->hd_aiocb = NULL;
|
390 | f141eafe | aliguori | if (ret < 0) |
391 | f141eafe | aliguori | goto done;
|
392 | 585f8587 | bellard | |
393 | 585f8587 | bellard | /* post process the read buffer */
|
394 | ce1a14dc | pbrook | if (!acb->cluster_offset) {
|
395 | 585f8587 | bellard | /* nothing to do */
|
396 | ce1a14dc | pbrook | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
397 | 585f8587 | bellard | /* nothing to do */
|
398 | 585f8587 | bellard | } else {
|
399 | 585f8587 | bellard | if (s->crypt_method) {
|
400 | bd28f835 | Kevin Wolf | qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data, |
401 | bd28f835 | Kevin Wolf | acb->cluster_data, acb->cur_nr_sectors, 0, &s->aes_decrypt_key);
|
402 | bd28f835 | Kevin Wolf | qemu_iovec_reset(&acb->hd_qiov); |
403 | bd28f835 | Kevin Wolf | qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done, |
404 | bd28f835 | Kevin Wolf | acb->cur_nr_sectors * 512);
|
405 | bd28f835 | Kevin Wolf | qemu_iovec_from_buffer(&acb->hd_qiov, acb->cluster_data, |
406 | bd28f835 | Kevin Wolf | 512 * acb->cur_nr_sectors);
|
407 | 585f8587 | bellard | } |
408 | 585f8587 | bellard | } |
409 | 585f8587 | bellard | |
410 | 7b88e48b | Christoph Hellwig | acb->remaining_sectors -= acb->cur_nr_sectors; |
411 | 7b88e48b | Christoph Hellwig | acb->sector_num += acb->cur_nr_sectors; |
412 | bd28f835 | Kevin Wolf | acb->bytes_done += acb->cur_nr_sectors * 512;
|
413 | 585f8587 | bellard | |
414 | 7b88e48b | Christoph Hellwig | if (acb->remaining_sectors == 0) { |
415 | 585f8587 | bellard | /* request completed */
|
416 | f141eafe | aliguori | ret = 0;
|
417 | f141eafe | aliguori | goto done;
|
418 | 585f8587 | bellard | } |
419 | 3b46e624 | ths | |
420 | 585f8587 | bellard | /* prepare next AIO request */
|
421 | 7b88e48b | Christoph Hellwig | acb->cur_nr_sectors = acb->remaining_sectors; |
422 | bd28f835 | Kevin Wolf | if (s->crypt_method) {
|
423 | bd28f835 | Kevin Wolf | acb->cur_nr_sectors = MIN(acb->cur_nr_sectors, |
424 | bd28f835 | Kevin Wolf | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); |
425 | bd28f835 | Kevin Wolf | } |
426 | bd28f835 | Kevin Wolf | |
427 | 1c46efaa | Kevin Wolf | ret = qcow2_get_cluster_offset(bs, acb->sector_num << 9,
|
428 | 1c46efaa | Kevin Wolf | &acb->cur_nr_sectors, &acb->cluster_offset); |
429 | 1c46efaa | Kevin Wolf | if (ret < 0) { |
430 | 1c46efaa | Kevin Wolf | goto done;
|
431 | 1c46efaa | Kevin Wolf | } |
432 | 1c46efaa | Kevin Wolf | |
433 | ce1a14dc | pbrook | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
|
434 | ce1a14dc | pbrook | |
435 | bd28f835 | Kevin Wolf | qemu_iovec_reset(&acb->hd_qiov); |
436 | bd28f835 | Kevin Wolf | qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done, |
437 | bd28f835 | Kevin Wolf | acb->cur_nr_sectors * 512);
|
438 | bd28f835 | Kevin Wolf | |
439 | ce1a14dc | pbrook | if (!acb->cluster_offset) {
|
440 | bd28f835 | Kevin Wolf | |
441 | 585f8587 | bellard | if (bs->backing_hd) {
|
442 | 585f8587 | bellard | /* read from the base image */
|
443 | bd28f835 | Kevin Wolf | n1 = qcow2_backing_read1(bs->backing_hd, &acb->hd_qiov, |
444 | bd28f835 | Kevin Wolf | acb->sector_num, acb->cur_nr_sectors); |
445 | a9465922 | bellard | if (n1 > 0) { |
446 | 66f82cee | Kevin Wolf | BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); |
447 | c87c0672 | aliguori | acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num, |
448 | 7b88e48b | Christoph Hellwig | &acb->hd_qiov, acb->cur_nr_sectors, |
449 | c87c0672 | aliguori | qcow_aio_read_cb, acb); |
450 | ce1a14dc | pbrook | if (acb->hd_aiocb == NULL) |
451 | f141eafe | aliguori | goto done;
|
452 | a9465922 | bellard | } else {
|
453 | a32ef786 | aliguori | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
454 | a32ef786 | aliguori | if (ret < 0) |
455 | f141eafe | aliguori | goto done;
|
456 | a9465922 | bellard | } |
457 | 585f8587 | bellard | } else {
|
458 | 585f8587 | bellard | /* Note: in this case, no need to wait */
|
459 | bd28f835 | Kevin Wolf | qemu_iovec_memset(&acb->hd_qiov, 0, 512 * acb->cur_nr_sectors); |
460 | a32ef786 | aliguori | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
461 | a32ef786 | aliguori | if (ret < 0) |
462 | f141eafe | aliguori | goto done;
|
463 | 585f8587 | bellard | } |
464 | ce1a14dc | pbrook | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
465 | 585f8587 | bellard | /* add AIO support for compressed blocks ? */
|
466 | 66f82cee | Kevin Wolf | if (qcow2_decompress_cluster(bs, acb->cluster_offset) < 0) |
467 | f141eafe | aliguori | goto done;
|
468 | bd28f835 | Kevin Wolf | |
469 | bd28f835 | Kevin Wolf | qemu_iovec_from_buffer(&acb->hd_qiov, |
470 | bd28f835 | Kevin Wolf | s->cluster_cache + index_in_cluster * 512,
|
471 | bd28f835 | Kevin Wolf | 512 * acb->cur_nr_sectors);
|
472 | bd28f835 | Kevin Wolf | |
473 | a32ef786 | aliguori | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
474 | a32ef786 | aliguori | if (ret < 0) |
475 | f141eafe | aliguori | goto done;
|
476 | 585f8587 | bellard | } else {
|
477 | ce1a14dc | pbrook | if ((acb->cluster_offset & 511) != 0) { |
478 | 585f8587 | bellard | ret = -EIO; |
479 | f141eafe | aliguori | goto done;
|
480 | 585f8587 | bellard | } |
481 | c87c0672 | aliguori | |
482 | bd28f835 | Kevin Wolf | if (s->crypt_method) {
|
483 | bd28f835 | Kevin Wolf | /*
|
484 | bd28f835 | Kevin Wolf | * For encrypted images, read everything into a temporary
|
485 | bd28f835 | Kevin Wolf | * contiguous buffer on which the AES functions can work.
|
486 | bd28f835 | Kevin Wolf | */
|
487 | bd28f835 | Kevin Wolf | if (!acb->cluster_data) {
|
488 | bd28f835 | Kevin Wolf | acb->cluster_data = |
489 | bd28f835 | Kevin Wolf | qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); |
490 | bd28f835 | Kevin Wolf | } |
491 | bd28f835 | Kevin Wolf | |
492 | bd28f835 | Kevin Wolf | assert(acb->cur_nr_sectors <= |
493 | bd28f835 | Kevin Wolf | QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors); |
494 | bd28f835 | Kevin Wolf | qemu_iovec_reset(&acb->hd_qiov); |
495 | bd28f835 | Kevin Wolf | qemu_iovec_add(&acb->hd_qiov, acb->cluster_data, |
496 | bd28f835 | Kevin Wolf | 512 * acb->cur_nr_sectors);
|
497 | bd28f835 | Kevin Wolf | } |
498 | bd28f835 | Kevin Wolf | |
499 | 66f82cee | Kevin Wolf | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); |
500 | 66f82cee | Kevin Wolf | acb->hd_aiocb = bdrv_aio_readv(bs->file, |
501 | 5fafdf24 | ths | (acb->cluster_offset >> 9) + index_in_cluster,
|
502 | 7b88e48b | Christoph Hellwig | &acb->hd_qiov, acb->cur_nr_sectors, |
503 | 7b88e48b | Christoph Hellwig | qcow_aio_read_cb, acb); |
504 | 171e3d6b | Kevin Wolf | if (acb->hd_aiocb == NULL) { |
505 | 171e3d6b | Kevin Wolf | ret = -EIO; |
506 | f141eafe | aliguori | goto done;
|
507 | 171e3d6b | Kevin Wolf | } |
508 | f141eafe | aliguori | } |
509 | f141eafe | aliguori | |
510 | f141eafe | aliguori | return;
|
511 | f141eafe | aliguori | done:
|
512 | f141eafe | aliguori | acb->common.cb(acb->common.opaque, ret); |
513 | bd28f835 | Kevin Wolf | qemu_iovec_destroy(&acb->hd_qiov); |
514 | f141eafe | aliguori | qemu_aio_release(acb); |
515 | 585f8587 | bellard | } |
516 | 585f8587 | bellard | |
517 | ce1a14dc | pbrook | static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
|
518 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
519 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque, int is_write) |
520 | 585f8587 | bellard | { |
521 | ce1a14dc | pbrook | QCowAIOCB *acb; |
522 | ce1a14dc | pbrook | |
523 | c16b5a2c | Christoph Hellwig | acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque); |
524 | ce1a14dc | pbrook | if (!acb)
|
525 | ce1a14dc | pbrook | return NULL; |
526 | ce1a14dc | pbrook | acb->hd_aiocb = NULL;
|
527 | ce1a14dc | pbrook | acb->sector_num = sector_num; |
528 | f141eafe | aliguori | acb->qiov = qiov; |
529 | bd28f835 | Kevin Wolf | |
530 | 6f5f060b | Kevin Wolf | qemu_iovec_init(&acb->hd_qiov, qiov->niov); |
531 | bd28f835 | Kevin Wolf | |
532 | bd28f835 | Kevin Wolf | acb->bytes_done = 0;
|
533 | 7b88e48b | Christoph Hellwig | acb->remaining_sectors = nb_sectors; |
534 | 7b88e48b | Christoph Hellwig | acb->cur_nr_sectors = 0;
|
535 | ce1a14dc | pbrook | acb->cluster_offset = 0;
|
536 | e976c6a1 | aliguori | acb->l2meta.nb_clusters = 0;
|
537 | 72cf2d4f | Blue Swirl | QLIST_INIT(&acb->l2meta.dependent_requests); |
538 | ce1a14dc | pbrook | return acb;
|
539 | ce1a14dc | pbrook | } |
540 | ce1a14dc | pbrook | |
541 | f141eafe | aliguori | static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs,
|
542 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
543 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
544 | ce1a14dc | pbrook | { |
545 | ce1a14dc | pbrook | QCowAIOCB *acb; |
546 | ce1a14dc | pbrook | |
547 | f141eafe | aliguori | acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
|
548 | ce1a14dc | pbrook | if (!acb)
|
549 | ce1a14dc | pbrook | return NULL; |
550 | 585f8587 | bellard | |
551 | 585f8587 | bellard | qcow_aio_read_cb(acb, 0);
|
552 | ce1a14dc | pbrook | return &acb->common;
|
553 | 585f8587 | bellard | } |
554 | 585f8587 | bellard | |
555 | f214978a | Kevin Wolf | static void qcow_aio_write_cb(void *opaque, int ret); |
556 | f214978a | Kevin Wolf | |
557 | f214978a | Kevin Wolf | static void run_dependent_requests(QCowL2Meta *m) |
558 | f214978a | Kevin Wolf | { |
559 | f214978a | Kevin Wolf | QCowAIOCB *req; |
560 | f214978a | Kevin Wolf | QCowAIOCB *next; |
561 | f214978a | Kevin Wolf | |
562 | f214978a | Kevin Wolf | /* Take the request off the list of running requests */
|
563 | f214978a | Kevin Wolf | if (m->nb_clusters != 0) { |
564 | 72cf2d4f | Blue Swirl | QLIST_REMOVE(m, next_in_flight); |
565 | f214978a | Kevin Wolf | } |
566 | f214978a | Kevin Wolf | |
567 | d4c146f0 | Stefan Hajnoczi | /* Restart all dependent requests */
|
568 | d4c146f0 | Stefan Hajnoczi | QLIST_FOREACH_SAFE(req, &m->dependent_requests, next_depend, next) { |
569 | f214978a | Kevin Wolf | qcow_aio_write_cb(req, 0);
|
570 | f214978a | Kevin Wolf | } |
571 | f214978a | Kevin Wolf | |
572 | f214978a | Kevin Wolf | /* Empty the list for the next part of the request */
|
573 | 72cf2d4f | Blue Swirl | QLIST_INIT(&m->dependent_requests); |
574 | f214978a | Kevin Wolf | } |
575 | f214978a | Kevin Wolf | |
576 | 585f8587 | bellard | static void qcow_aio_write_cb(void *opaque, int ret) |
577 | 585f8587 | bellard | { |
578 | ce1a14dc | pbrook | QCowAIOCB *acb = opaque; |
579 | ce1a14dc | pbrook | BlockDriverState *bs = acb->common.bs; |
580 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
581 | 585f8587 | bellard | int index_in_cluster;
|
582 | 095a9c58 | aliguori | int n_end;
|
583 | ce1a14dc | pbrook | |
584 | ce1a14dc | pbrook | acb->hd_aiocb = NULL;
|
585 | ce1a14dc | pbrook | |
586 | f214978a | Kevin Wolf | if (ret >= 0) { |
587 | 148da7ea | Kevin Wolf | ret = qcow2_alloc_cluster_link_l2(bs, &acb->l2meta); |
588 | f214978a | Kevin Wolf | } |
589 | f214978a | Kevin Wolf | |
590 | f214978a | Kevin Wolf | run_dependent_requests(&acb->l2meta); |
591 | f214978a | Kevin Wolf | |
592 | f141eafe | aliguori | if (ret < 0) |
593 | f141eafe | aliguori | goto done;
|
594 | 585f8587 | bellard | |
595 | 7b88e48b | Christoph Hellwig | acb->remaining_sectors -= acb->cur_nr_sectors; |
596 | 7b88e48b | Christoph Hellwig | acb->sector_num += acb->cur_nr_sectors; |
597 | 6f5f060b | Kevin Wolf | acb->bytes_done += acb->cur_nr_sectors * 512;
|
598 | 585f8587 | bellard | |
599 | 7b88e48b | Christoph Hellwig | if (acb->remaining_sectors == 0) { |
600 | 585f8587 | bellard | /* request completed */
|
601 | f141eafe | aliguori | ret = 0;
|
602 | f141eafe | aliguori | goto done;
|
603 | 585f8587 | bellard | } |
604 | 3b46e624 | ths | |
605 | ce1a14dc | pbrook | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
|
606 | 7b88e48b | Christoph Hellwig | n_end = index_in_cluster + acb->remaining_sectors; |
607 | 095a9c58 | aliguori | if (s->crypt_method &&
|
608 | 095a9c58 | aliguori | n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) |
609 | 095a9c58 | aliguori | n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; |
610 | 095a9c58 | aliguori | |
611 | 148da7ea | Kevin Wolf | ret = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9,
|
612 | 7b88e48b | Christoph Hellwig | index_in_cluster, n_end, &acb->cur_nr_sectors, &acb->l2meta); |
613 | 148da7ea | Kevin Wolf | if (ret < 0) { |
614 | 148da7ea | Kevin Wolf | goto done;
|
615 | 148da7ea | Kevin Wolf | } |
616 | 148da7ea | Kevin Wolf | |
617 | 148da7ea | Kevin Wolf | acb->cluster_offset = acb->l2meta.cluster_offset; |
618 | f214978a | Kevin Wolf | |
619 | f214978a | Kevin Wolf | /* Need to wait for another request? If so, we are done for now. */
|
620 | 148da7ea | Kevin Wolf | if (acb->l2meta.nb_clusters == 0 && acb->l2meta.depends_on != NULL) { |
621 | 72cf2d4f | Blue Swirl | QLIST_INSERT_HEAD(&acb->l2meta.depends_on->dependent_requests, |
622 | f214978a | Kevin Wolf | acb, next_depend); |
623 | f214978a | Kevin Wolf | return;
|
624 | f214978a | Kevin Wolf | } |
625 | f214978a | Kevin Wolf | |
626 | 148da7ea | Kevin Wolf | assert((acb->cluster_offset & 511) == 0); |
627 | 148da7ea | Kevin Wolf | |
628 | 6f5f060b | Kevin Wolf | qemu_iovec_reset(&acb->hd_qiov); |
629 | 6f5f060b | Kevin Wolf | qemu_iovec_copy(&acb->hd_qiov, acb->qiov, acb->bytes_done, |
630 | 6f5f060b | Kevin Wolf | acb->cur_nr_sectors * 512);
|
631 | 6f5f060b | Kevin Wolf | |
632 | 585f8587 | bellard | if (s->crypt_method) {
|
633 | ce1a14dc | pbrook | if (!acb->cluster_data) {
|
634 | 095a9c58 | aliguori | acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * |
635 | 095a9c58 | aliguori | s->cluster_size); |
636 | 585f8587 | bellard | } |
637 | 6f5f060b | Kevin Wolf | |
638 | 6f5f060b | Kevin Wolf | assert(acb->hd_qiov.size <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); |
639 | 6f5f060b | Kevin Wolf | qemu_iovec_to_buffer(&acb->hd_qiov, acb->cluster_data); |
640 | 6f5f060b | Kevin Wolf | |
641 | 6f5f060b | Kevin Wolf | qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data, |
642 | 6f5f060b | Kevin Wolf | acb->cluster_data, acb->cur_nr_sectors, 1, &s->aes_encrypt_key);
|
643 | 6f5f060b | Kevin Wolf | |
644 | 6f5f060b | Kevin Wolf | qemu_iovec_reset(&acb->hd_qiov); |
645 | 6f5f060b | Kevin Wolf | qemu_iovec_add(&acb->hd_qiov, acb->cluster_data, |
646 | 6f5f060b | Kevin Wolf | acb->cur_nr_sectors * 512);
|
647 | 585f8587 | bellard | } |
648 | 6f5f060b | Kevin Wolf | |
649 | 66f82cee | Kevin Wolf | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); |
650 | 66f82cee | Kevin Wolf | acb->hd_aiocb = bdrv_aio_writev(bs->file, |
651 | c87c0672 | aliguori | (acb->cluster_offset >> 9) + index_in_cluster,
|
652 | 7b88e48b | Christoph Hellwig | &acb->hd_qiov, acb->cur_nr_sectors, |
653 | c87c0672 | aliguori | qcow_aio_write_cb, acb); |
654 | 171e3d6b | Kevin Wolf | if (acb->hd_aiocb == NULL) { |
655 | 171e3d6b | Kevin Wolf | ret = -EIO; |
656 | c644db3d | Kevin Wolf | goto fail;
|
657 | 171e3d6b | Kevin Wolf | } |
658 | f141eafe | aliguori | |
659 | f141eafe | aliguori | return;
|
660 | f141eafe | aliguori | |
661 | c644db3d | Kevin Wolf | fail:
|
662 | c644db3d | Kevin Wolf | if (acb->l2meta.nb_clusters != 0) { |
663 | c644db3d | Kevin Wolf | QLIST_REMOVE(&acb->l2meta, next_in_flight); |
664 | c644db3d | Kevin Wolf | } |
665 | f141eafe | aliguori | done:
|
666 | f141eafe | aliguori | acb->common.cb(acb->common.opaque, ret); |
667 | 6f5f060b | Kevin Wolf | qemu_iovec_destroy(&acb->hd_qiov); |
668 | f141eafe | aliguori | qemu_aio_release(acb); |
669 | 585f8587 | bellard | } |
670 | 585f8587 | bellard | |
671 | f141eafe | aliguori | static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs,
|
672 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
673 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
674 | 585f8587 | bellard | { |
675 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
676 | ce1a14dc | pbrook | QCowAIOCB *acb; |
677 | 3b46e624 | ths | |
678 | 585f8587 | bellard | s->cluster_cache_offset = -1; /* disable compressed cache */ |
679 | 585f8587 | bellard | |
680 | f141eafe | aliguori | acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
|
681 | ce1a14dc | pbrook | if (!acb)
|
682 | ce1a14dc | pbrook | return NULL; |
683 | 3b46e624 | ths | |
684 | 585f8587 | bellard | qcow_aio_write_cb(acb, 0);
|
685 | ce1a14dc | pbrook | return &acb->common;
|
686 | 585f8587 | bellard | } |
687 | 585f8587 | bellard | |
688 | 585f8587 | bellard | static void qcow_close(BlockDriverState *bs) |
689 | 585f8587 | bellard | { |
690 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
691 | 585f8587 | bellard | qemu_free(s->l1_table); |
692 | 585f8587 | bellard | qemu_free(s->l2_cache); |
693 | 585f8587 | bellard | qemu_free(s->cluster_cache); |
694 | 585f8587 | bellard | qemu_free(s->cluster_data); |
695 | ed6ccf0f | Kevin Wolf | qcow2_refcount_close(bs); |
696 | 585f8587 | bellard | } |
697 | 585f8587 | bellard | |
698 | 756e6736 | Kevin Wolf | /*
|
699 | 756e6736 | Kevin Wolf | * Updates the variable length parts of the qcow2 header, i.e. the backing file
|
700 | 756e6736 | Kevin Wolf | * name and all extensions. qcow2 was not designed to allow such changes, so if
|
701 | 756e6736 | Kevin Wolf | * we run out of space (we can only use the first cluster) this function may
|
702 | 756e6736 | Kevin Wolf | * fail.
|
703 | 756e6736 | Kevin Wolf | *
|
704 | 756e6736 | Kevin Wolf | * Returns 0 on success, -errno in error cases.
|
705 | 756e6736 | Kevin Wolf | */
|
706 | 756e6736 | Kevin Wolf | static int qcow2_update_ext_header(BlockDriverState *bs, |
707 | 756e6736 | Kevin Wolf | const char *backing_file, const char *backing_fmt) |
708 | 756e6736 | Kevin Wolf | { |
709 | 756e6736 | Kevin Wolf | size_t backing_file_len = 0;
|
710 | 756e6736 | Kevin Wolf | size_t backing_fmt_len = 0;
|
711 | 756e6736 | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
712 | 756e6736 | Kevin Wolf | QCowExtension ext_backing_fmt = {0, 0}; |
713 | 756e6736 | Kevin Wolf | int ret;
|
714 | 756e6736 | Kevin Wolf | |
715 | 756e6736 | Kevin Wolf | /* Backing file format doesn't make sense without a backing file */
|
716 | 756e6736 | Kevin Wolf | if (backing_fmt && !backing_file) {
|
717 | 756e6736 | Kevin Wolf | return -EINVAL;
|
718 | 756e6736 | Kevin Wolf | } |
719 | 756e6736 | Kevin Wolf | |
720 | 756e6736 | Kevin Wolf | /* Prepare the backing file format extension if needed */
|
721 | 756e6736 | Kevin Wolf | if (backing_fmt) {
|
722 | 756e6736 | Kevin Wolf | ext_backing_fmt.len = cpu_to_be32(strlen(backing_fmt)); |
723 | 756e6736 | Kevin Wolf | ext_backing_fmt.magic = cpu_to_be32(QCOW_EXT_MAGIC_BACKING_FORMAT); |
724 | 756e6736 | Kevin Wolf | backing_fmt_len = ((sizeof(ext_backing_fmt)
|
725 | 756e6736 | Kevin Wolf | + strlen(backing_fmt) + 7) & ~7); |
726 | 756e6736 | Kevin Wolf | } |
727 | 756e6736 | Kevin Wolf | |
728 | 756e6736 | Kevin Wolf | /* Check if we can fit the new header into the first cluster */
|
729 | 756e6736 | Kevin Wolf | if (backing_file) {
|
730 | 756e6736 | Kevin Wolf | backing_file_len = strlen(backing_file); |
731 | 756e6736 | Kevin Wolf | } |
732 | 756e6736 | Kevin Wolf | |
733 | 756e6736 | Kevin Wolf | size_t header_size = sizeof(QCowHeader) + backing_file_len
|
734 | 756e6736 | Kevin Wolf | + backing_fmt_len; |
735 | 756e6736 | Kevin Wolf | |
736 | 756e6736 | Kevin Wolf | if (header_size > s->cluster_size) {
|
737 | 756e6736 | Kevin Wolf | return -ENOSPC;
|
738 | 756e6736 | Kevin Wolf | } |
739 | 756e6736 | Kevin Wolf | |
740 | 756e6736 | Kevin Wolf | /* Rewrite backing file name and qcow2 extensions */
|
741 | 756e6736 | Kevin Wolf | size_t ext_size = header_size - sizeof(QCowHeader);
|
742 | 756e6736 | Kevin Wolf | uint8_t buf[ext_size]; |
743 | 756e6736 | Kevin Wolf | size_t offset = 0;
|
744 | 756e6736 | Kevin Wolf | size_t backing_file_offset = 0;
|
745 | 756e6736 | Kevin Wolf | |
746 | 756e6736 | Kevin Wolf | if (backing_file) {
|
747 | 756e6736 | Kevin Wolf | if (backing_fmt) {
|
748 | 756e6736 | Kevin Wolf | int padding = backing_fmt_len -
|
749 | 756e6736 | Kevin Wolf | (sizeof(ext_backing_fmt) + strlen(backing_fmt));
|
750 | 756e6736 | Kevin Wolf | |
751 | 756e6736 | Kevin Wolf | memcpy(buf + offset, &ext_backing_fmt, sizeof(ext_backing_fmt));
|
752 | 756e6736 | Kevin Wolf | offset += sizeof(ext_backing_fmt);
|
753 | 756e6736 | Kevin Wolf | |
754 | 756e6736 | Kevin Wolf | memcpy(buf + offset, backing_fmt, strlen(backing_fmt)); |
755 | 756e6736 | Kevin Wolf | offset += strlen(backing_fmt); |
756 | 756e6736 | Kevin Wolf | |
757 | 756e6736 | Kevin Wolf | memset(buf + offset, 0, padding);
|
758 | 756e6736 | Kevin Wolf | offset += padding; |
759 | 756e6736 | Kevin Wolf | } |
760 | 756e6736 | Kevin Wolf | |
761 | 756e6736 | Kevin Wolf | memcpy(buf + offset, backing_file, backing_file_len); |
762 | 756e6736 | Kevin Wolf | backing_file_offset = sizeof(QCowHeader) + offset;
|
763 | 756e6736 | Kevin Wolf | } |
764 | 756e6736 | Kevin Wolf | |
765 | 8b3b7206 | Kevin Wolf | ret = bdrv_pwrite_sync(bs->file, sizeof(QCowHeader), buf, ext_size);
|
766 | 756e6736 | Kevin Wolf | if (ret < 0) { |
767 | 756e6736 | Kevin Wolf | goto fail;
|
768 | 756e6736 | Kevin Wolf | } |
769 | 756e6736 | Kevin Wolf | |
770 | 756e6736 | Kevin Wolf | /* Update header fields */
|
771 | 756e6736 | Kevin Wolf | uint64_t be_backing_file_offset = cpu_to_be64(backing_file_offset); |
772 | 756e6736 | Kevin Wolf | uint32_t be_backing_file_size = cpu_to_be32(backing_file_len); |
773 | 756e6736 | Kevin Wolf | |
774 | 8b3b7206 | Kevin Wolf | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_offset), |
775 | 756e6736 | Kevin Wolf | &be_backing_file_offset, sizeof(uint64_t));
|
776 | 756e6736 | Kevin Wolf | if (ret < 0) { |
777 | 756e6736 | Kevin Wolf | goto fail;
|
778 | 756e6736 | Kevin Wolf | } |
779 | 756e6736 | Kevin Wolf | |
780 | 8b3b7206 | Kevin Wolf | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, backing_file_size), |
781 | 756e6736 | Kevin Wolf | &be_backing_file_size, sizeof(uint32_t));
|
782 | 756e6736 | Kevin Wolf | if (ret < 0) { |
783 | 756e6736 | Kevin Wolf | goto fail;
|
784 | 756e6736 | Kevin Wolf | } |
785 | 756e6736 | Kevin Wolf | |
786 | 756e6736 | Kevin Wolf | ret = 0;
|
787 | 756e6736 | Kevin Wolf | fail:
|
788 | 756e6736 | Kevin Wolf | return ret;
|
789 | 756e6736 | Kevin Wolf | } |
790 | 756e6736 | Kevin Wolf | |
791 | 756e6736 | Kevin Wolf | static int qcow2_change_backing_file(BlockDriverState *bs, |
792 | 756e6736 | Kevin Wolf | const char *backing_file, const char *backing_fmt) |
793 | 756e6736 | Kevin Wolf | { |
794 | 756e6736 | Kevin Wolf | return qcow2_update_ext_header(bs, backing_file, backing_fmt);
|
795 | 756e6736 | Kevin Wolf | } |
796 | 756e6736 | Kevin Wolf | |
797 | 73c632ed | Kevin Wolf | static int get_bits_from_size(size_t size) |
798 | 73c632ed | Kevin Wolf | { |
799 | 73c632ed | Kevin Wolf | int res = 0; |
800 | 73c632ed | Kevin Wolf | |
801 | 73c632ed | Kevin Wolf | if (size == 0) { |
802 | 73c632ed | Kevin Wolf | return -1; |
803 | 73c632ed | Kevin Wolf | } |
804 | 73c632ed | Kevin Wolf | |
805 | 73c632ed | Kevin Wolf | while (size != 1) { |
806 | 73c632ed | Kevin Wolf | /* Not a power of two */
|
807 | 73c632ed | Kevin Wolf | if (size & 1) { |
808 | 73c632ed | Kevin Wolf | return -1; |
809 | 73c632ed | Kevin Wolf | } |
810 | 73c632ed | Kevin Wolf | |
811 | 73c632ed | Kevin Wolf | size >>= 1;
|
812 | 73c632ed | Kevin Wolf | res++; |
813 | 73c632ed | Kevin Wolf | } |
814 | 73c632ed | Kevin Wolf | |
815 | 73c632ed | Kevin Wolf | return res;
|
816 | 73c632ed | Kevin Wolf | } |
817 | 73c632ed | Kevin Wolf | |
818 | a35e1c17 | Kevin Wolf | |
819 | a35e1c17 | Kevin Wolf | static int preallocate(BlockDriverState *bs) |
820 | a35e1c17 | Kevin Wolf | { |
821 | a35e1c17 | Kevin Wolf | uint64_t nb_sectors; |
822 | a35e1c17 | Kevin Wolf | uint64_t offset; |
823 | a35e1c17 | Kevin Wolf | int num;
|
824 | 148da7ea | Kevin Wolf | int ret;
|
825 | a35e1c17 | Kevin Wolf | QCowL2Meta meta; |
826 | a35e1c17 | Kevin Wolf | |
827 | a35e1c17 | Kevin Wolf | nb_sectors = bdrv_getlength(bs) >> 9;
|
828 | a35e1c17 | Kevin Wolf | offset = 0;
|
829 | 72cf2d4f | Blue Swirl | QLIST_INIT(&meta.dependent_requests); |
830 | 148da7ea | Kevin Wolf | meta.cluster_offset = 0;
|
831 | a35e1c17 | Kevin Wolf | |
832 | a35e1c17 | Kevin Wolf | while (nb_sectors) {
|
833 | a35e1c17 | Kevin Wolf | num = MIN(nb_sectors, INT_MAX >> 9);
|
834 | 148da7ea | Kevin Wolf | ret = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num, &meta);
|
835 | 148da7ea | Kevin Wolf | if (ret < 0) { |
836 | 19dbcbf7 | Kevin Wolf | return ret;
|
837 | a35e1c17 | Kevin Wolf | } |
838 | a35e1c17 | Kevin Wolf | |
839 | 19dbcbf7 | Kevin Wolf | ret = qcow2_alloc_cluster_link_l2(bs, &meta); |
840 | 19dbcbf7 | Kevin Wolf | if (ret < 0) { |
841 | 148da7ea | Kevin Wolf | qcow2_free_any_clusters(bs, meta.cluster_offset, meta.nb_clusters); |
842 | 19dbcbf7 | Kevin Wolf | return ret;
|
843 | a35e1c17 | Kevin Wolf | } |
844 | a35e1c17 | Kevin Wolf | |
845 | f214978a | Kevin Wolf | /* There are no dependent requests, but we need to remove our request
|
846 | f214978a | Kevin Wolf | * from the list of in-flight requests */
|
847 | f214978a | Kevin Wolf | run_dependent_requests(&meta); |
848 | f214978a | Kevin Wolf | |
849 | a35e1c17 | Kevin Wolf | /* TODO Preallocate data if requested */
|
850 | a35e1c17 | Kevin Wolf | |
851 | a35e1c17 | Kevin Wolf | nb_sectors -= num; |
852 | a35e1c17 | Kevin Wolf | offset += num << 9;
|
853 | a35e1c17 | Kevin Wolf | } |
854 | a35e1c17 | Kevin Wolf | |
855 | a35e1c17 | Kevin Wolf | /*
|
856 | a35e1c17 | Kevin Wolf | * It is expected that the image file is large enough to actually contain
|
857 | a35e1c17 | Kevin Wolf | * all of the allocated clusters (otherwise we get failing reads after
|
858 | a35e1c17 | Kevin Wolf | * EOF). Extend the image to the last allocated sector.
|
859 | a35e1c17 | Kevin Wolf | */
|
860 | 148da7ea | Kevin Wolf | if (meta.cluster_offset != 0) { |
861 | ea80b906 | Kevin Wolf | uint8_t buf[512];
|
862 | ea80b906 | Kevin Wolf | memset(buf, 0, 512); |
863 | 19dbcbf7 | Kevin Wolf | ret = bdrv_write(bs->file, (meta.cluster_offset >> 9) + num - 1, buf, 1); |
864 | 19dbcbf7 | Kevin Wolf | if (ret < 0) { |
865 | 19dbcbf7 | Kevin Wolf | return ret;
|
866 | 19dbcbf7 | Kevin Wolf | } |
867 | a35e1c17 | Kevin Wolf | } |
868 | a35e1c17 | Kevin Wolf | |
869 | a35e1c17 | Kevin Wolf | return 0; |
870 | a35e1c17 | Kevin Wolf | } |
871 | a35e1c17 | Kevin Wolf | |
872 | de5f3f40 | Kevin Wolf | static int qcow_create2(const char *filename, int64_t total_size, |
873 | de5f3f40 | Kevin Wolf | const char *backing_file, const char *backing_format, |
874 | de5f3f40 | Kevin Wolf | int flags, size_t cluster_size, int prealloc) |
875 | de5f3f40 | Kevin Wolf | { |
876 | de5f3f40 | Kevin Wolf | |
877 | de5f3f40 | Kevin Wolf | int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
|
878 | de5f3f40 | Kevin Wolf | int ref_clusters, reftable_clusters, backing_format_len = 0; |
879 | de5f3f40 | Kevin Wolf | int rounded_ext_bf_len = 0; |
880 | de5f3f40 | Kevin Wolf | QCowHeader header; |
881 | de5f3f40 | Kevin Wolf | uint64_t tmp, offset; |
882 | de5f3f40 | Kevin Wolf | uint64_t old_ref_clusters; |
883 | de5f3f40 | Kevin Wolf | QCowCreateState s1, *s = &s1; |
884 | de5f3f40 | Kevin Wolf | QCowExtension ext_bf = {0, 0}; |
885 | de5f3f40 | Kevin Wolf | int ret;
|
886 | de5f3f40 | Kevin Wolf | |
887 | de5f3f40 | Kevin Wolf | memset(s, 0, sizeof(*s)); |
888 | de5f3f40 | Kevin Wolf | |
889 | de5f3f40 | Kevin Wolf | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
|
890 | de5f3f40 | Kevin Wolf | if (fd < 0) |
891 | de5f3f40 | Kevin Wolf | return -errno;
|
892 | de5f3f40 | Kevin Wolf | memset(&header, 0, sizeof(header)); |
893 | de5f3f40 | Kevin Wolf | header.magic = cpu_to_be32(QCOW_MAGIC); |
894 | de5f3f40 | Kevin Wolf | header.version = cpu_to_be32(QCOW_VERSION); |
895 | de5f3f40 | Kevin Wolf | header.size = cpu_to_be64(total_size * 512);
|
896 | de5f3f40 | Kevin Wolf | header_size = sizeof(header);
|
897 | de5f3f40 | Kevin Wolf | backing_filename_len = 0;
|
898 | de5f3f40 | Kevin Wolf | if (backing_file) {
|
899 | de5f3f40 | Kevin Wolf | if (backing_format) {
|
900 | de5f3f40 | Kevin Wolf | ext_bf.magic = QCOW_EXT_MAGIC_BACKING_FORMAT; |
901 | de5f3f40 | Kevin Wolf | backing_format_len = strlen(backing_format); |
902 | de5f3f40 | Kevin Wolf | ext_bf.len = backing_format_len; |
903 | de5f3f40 | Kevin Wolf | rounded_ext_bf_len = (sizeof(ext_bf) + ext_bf.len + 7) & ~7; |
904 | de5f3f40 | Kevin Wolf | header_size += rounded_ext_bf_len; |
905 | de5f3f40 | Kevin Wolf | } |
906 | de5f3f40 | Kevin Wolf | header.backing_file_offset = cpu_to_be64(header_size); |
907 | de5f3f40 | Kevin Wolf | backing_filename_len = strlen(backing_file); |
908 | de5f3f40 | Kevin Wolf | header.backing_file_size = cpu_to_be32(backing_filename_len); |
909 | de5f3f40 | Kevin Wolf | header_size += backing_filename_len; |
910 | de5f3f40 | Kevin Wolf | } |
911 | de5f3f40 | Kevin Wolf | |
912 | de5f3f40 | Kevin Wolf | /* Cluster size */
|
913 | de5f3f40 | Kevin Wolf | s->cluster_bits = get_bits_from_size(cluster_size); |
914 | de5f3f40 | Kevin Wolf | if (s->cluster_bits < MIN_CLUSTER_BITS ||
|
915 | de5f3f40 | Kevin Wolf | s->cluster_bits > MAX_CLUSTER_BITS) |
916 | de5f3f40 | Kevin Wolf | { |
917 | de5f3f40 | Kevin Wolf | fprintf(stderr, "Cluster size must be a power of two between "
|
918 | de5f3f40 | Kevin Wolf | "%d and %dk\n",
|
919 | de5f3f40 | Kevin Wolf | 1 << MIN_CLUSTER_BITS,
|
920 | de5f3f40 | Kevin Wolf | 1 << (MAX_CLUSTER_BITS - 10)); |
921 | de5f3f40 | Kevin Wolf | return -EINVAL;
|
922 | de5f3f40 | Kevin Wolf | } |
923 | de5f3f40 | Kevin Wolf | s->cluster_size = 1 << s->cluster_bits;
|
924 | de5f3f40 | Kevin Wolf | |
925 | de5f3f40 | Kevin Wolf | header.cluster_bits = cpu_to_be32(s->cluster_bits); |
926 | de5f3f40 | Kevin Wolf | header_size = (header_size + 7) & ~7; |
927 | de5f3f40 | Kevin Wolf | if (flags & BLOCK_FLAG_ENCRYPT) {
|
928 | de5f3f40 | Kevin Wolf | header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); |
929 | de5f3f40 | Kevin Wolf | } else {
|
930 | de5f3f40 | Kevin Wolf | header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); |
931 | de5f3f40 | Kevin Wolf | } |
932 | de5f3f40 | Kevin Wolf | l2_bits = s->cluster_bits - 3;
|
933 | de5f3f40 | Kevin Wolf | shift = s->cluster_bits + l2_bits; |
934 | de5f3f40 | Kevin Wolf | l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift); |
935 | de5f3f40 | Kevin Wolf | offset = align_offset(header_size, s->cluster_size); |
936 | de5f3f40 | Kevin Wolf | s->l1_table_offset = offset; |
937 | de5f3f40 | Kevin Wolf | header.l1_table_offset = cpu_to_be64(s->l1_table_offset); |
938 | de5f3f40 | Kevin Wolf | header.l1_size = cpu_to_be32(l1_size); |
939 | de5f3f40 | Kevin Wolf | offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
|
940 | de5f3f40 | Kevin Wolf | |
941 | de5f3f40 | Kevin Wolf | /* count how many refcount blocks needed */
|
942 | de5f3f40 | Kevin Wolf | |
943 | de5f3f40 | Kevin Wolf | #define NUM_CLUSTERS(bytes) \
|
944 | de5f3f40 | Kevin Wolf | (((bytes) + (s->cluster_size) - 1) / (s->cluster_size))
|
945 | de5f3f40 | Kevin Wolf | |
946 | de5f3f40 | Kevin Wolf | ref_clusters = NUM_CLUSTERS(NUM_CLUSTERS(offset) * sizeof(uint16_t));
|
947 | de5f3f40 | Kevin Wolf | |
948 | de5f3f40 | Kevin Wolf | do {
|
949 | de5f3f40 | Kevin Wolf | uint64_t image_clusters; |
950 | de5f3f40 | Kevin Wolf | old_ref_clusters = ref_clusters; |
951 | de5f3f40 | Kevin Wolf | |
952 | de5f3f40 | Kevin Wolf | /* Number of clusters used for the refcount table */
|
953 | de5f3f40 | Kevin Wolf | reftable_clusters = NUM_CLUSTERS(ref_clusters * sizeof(uint64_t));
|
954 | de5f3f40 | Kevin Wolf | |
955 | de5f3f40 | Kevin Wolf | /* Number of clusters that the whole image will have */
|
956 | de5f3f40 | Kevin Wolf | image_clusters = NUM_CLUSTERS(offset) + ref_clusters |
957 | de5f3f40 | Kevin Wolf | + reftable_clusters; |
958 | de5f3f40 | Kevin Wolf | |
959 | de5f3f40 | Kevin Wolf | /* Number of refcount blocks needed for the image */
|
960 | de5f3f40 | Kevin Wolf | ref_clusters = NUM_CLUSTERS(image_clusters * sizeof(uint16_t));
|
961 | de5f3f40 | Kevin Wolf | |
962 | de5f3f40 | Kevin Wolf | } while (ref_clusters != old_ref_clusters);
|
963 | de5f3f40 | Kevin Wolf | |
964 | de5f3f40 | Kevin Wolf | s->refcount_table = qemu_mallocz(reftable_clusters * s->cluster_size); |
965 | de5f3f40 | Kevin Wolf | |
966 | de5f3f40 | Kevin Wolf | s->refcount_table_offset = offset; |
967 | de5f3f40 | Kevin Wolf | header.refcount_table_offset = cpu_to_be64(offset); |
968 | de5f3f40 | Kevin Wolf | header.refcount_table_clusters = cpu_to_be32(reftable_clusters); |
969 | de5f3f40 | Kevin Wolf | offset += (reftable_clusters * s->cluster_size); |
970 | de5f3f40 | Kevin Wolf | s->refcount_block_offset = offset; |
971 | de5f3f40 | Kevin Wolf | |
972 | de5f3f40 | Kevin Wolf | for (i=0; i < ref_clusters; i++) { |
973 | de5f3f40 | Kevin Wolf | s->refcount_table[i] = cpu_to_be64(offset); |
974 | de5f3f40 | Kevin Wolf | offset += s->cluster_size; |
975 | de5f3f40 | Kevin Wolf | } |
976 | de5f3f40 | Kevin Wolf | |
977 | de5f3f40 | Kevin Wolf | s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size); |
978 | de5f3f40 | Kevin Wolf | |
979 | de5f3f40 | Kevin Wolf | /* update refcounts */
|
980 | de5f3f40 | Kevin Wolf | qcow2_create_refcount_update(s, 0, header_size);
|
981 | de5f3f40 | Kevin Wolf | qcow2_create_refcount_update(s, s->l1_table_offset, |
982 | de5f3f40 | Kevin Wolf | l1_size * sizeof(uint64_t));
|
983 | de5f3f40 | Kevin Wolf | qcow2_create_refcount_update(s, s->refcount_table_offset, |
984 | de5f3f40 | Kevin Wolf | reftable_clusters * s->cluster_size); |
985 | de5f3f40 | Kevin Wolf | qcow2_create_refcount_update(s, s->refcount_block_offset, |
986 | de5f3f40 | Kevin Wolf | ref_clusters * s->cluster_size); |
987 | de5f3f40 | Kevin Wolf | |
988 | de5f3f40 | Kevin Wolf | /* write all the data */
|
989 | de5f3f40 | Kevin Wolf | ret = qemu_write_full(fd, &header, sizeof(header));
|
990 | de5f3f40 | Kevin Wolf | if (ret != sizeof(header)) { |
991 | de5f3f40 | Kevin Wolf | ret = -errno; |
992 | de5f3f40 | Kevin Wolf | goto exit;
|
993 | de5f3f40 | Kevin Wolf | } |
994 | de5f3f40 | Kevin Wolf | if (backing_file) {
|
995 | de5f3f40 | Kevin Wolf | if (backing_format_len) {
|
996 | de5f3f40 | Kevin Wolf | char zero[16]; |
997 | de5f3f40 | Kevin Wolf | int padding = rounded_ext_bf_len - (ext_bf.len + sizeof(ext_bf)); |
998 | de5f3f40 | Kevin Wolf | |
999 | de5f3f40 | Kevin Wolf | memset(zero, 0, sizeof(zero)); |
1000 | de5f3f40 | Kevin Wolf | cpu_to_be32s(&ext_bf.magic); |
1001 | de5f3f40 | Kevin Wolf | cpu_to_be32s(&ext_bf.len); |
1002 | de5f3f40 | Kevin Wolf | ret = qemu_write_full(fd, &ext_bf, sizeof(ext_bf));
|
1003 | de5f3f40 | Kevin Wolf | if (ret != sizeof(ext_bf)) { |
1004 | de5f3f40 | Kevin Wolf | ret = -errno; |
1005 | de5f3f40 | Kevin Wolf | goto exit;
|
1006 | de5f3f40 | Kevin Wolf | } |
1007 | de5f3f40 | Kevin Wolf | ret = qemu_write_full(fd, backing_format, backing_format_len); |
1008 | de5f3f40 | Kevin Wolf | if (ret != backing_format_len) {
|
1009 | de5f3f40 | Kevin Wolf | ret = -errno; |
1010 | de5f3f40 | Kevin Wolf | goto exit;
|
1011 | de5f3f40 | Kevin Wolf | } |
1012 | de5f3f40 | Kevin Wolf | if (padding > 0) { |
1013 | de5f3f40 | Kevin Wolf | ret = qemu_write_full(fd, zero, padding); |
1014 | de5f3f40 | Kevin Wolf | if (ret != padding) {
|
1015 | de5f3f40 | Kevin Wolf | ret = -errno; |
1016 | de5f3f40 | Kevin Wolf | goto exit;
|
1017 | de5f3f40 | Kevin Wolf | } |
1018 | de5f3f40 | Kevin Wolf | } |
1019 | de5f3f40 | Kevin Wolf | } |
1020 | de5f3f40 | Kevin Wolf | ret = qemu_write_full(fd, backing_file, backing_filename_len); |
1021 | de5f3f40 | Kevin Wolf | if (ret != backing_filename_len) {
|
1022 | de5f3f40 | Kevin Wolf | ret = -errno; |
1023 | de5f3f40 | Kevin Wolf | goto exit;
|
1024 | de5f3f40 | Kevin Wolf | } |
1025 | de5f3f40 | Kevin Wolf | } |
1026 | de5f3f40 | Kevin Wolf | lseek(fd, s->l1_table_offset, SEEK_SET); |
1027 | de5f3f40 | Kevin Wolf | tmp = 0;
|
1028 | de5f3f40 | Kevin Wolf | for(i = 0;i < l1_size; i++) { |
1029 | de5f3f40 | Kevin Wolf | ret = qemu_write_full(fd, &tmp, sizeof(tmp));
|
1030 | de5f3f40 | Kevin Wolf | if (ret != sizeof(tmp)) { |
1031 | de5f3f40 | Kevin Wolf | ret = -errno; |
1032 | de5f3f40 | Kevin Wolf | goto exit;
|
1033 | de5f3f40 | Kevin Wolf | } |
1034 | de5f3f40 | Kevin Wolf | } |
1035 | de5f3f40 | Kevin Wolf | lseek(fd, s->refcount_table_offset, SEEK_SET); |
1036 | de5f3f40 | Kevin Wolf | ret = qemu_write_full(fd, s->refcount_table, |
1037 | de5f3f40 | Kevin Wolf | reftable_clusters * s->cluster_size); |
1038 | de5f3f40 | Kevin Wolf | if (ret != reftable_clusters * s->cluster_size) {
|
1039 | de5f3f40 | Kevin Wolf | ret = -errno; |
1040 | de5f3f40 | Kevin Wolf | goto exit;
|
1041 | de5f3f40 | Kevin Wolf | } |
1042 | de5f3f40 | Kevin Wolf | |
1043 | de5f3f40 | Kevin Wolf | lseek(fd, s->refcount_block_offset, SEEK_SET); |
1044 | de5f3f40 | Kevin Wolf | ret = qemu_write_full(fd, s->refcount_block, |
1045 | de5f3f40 | Kevin Wolf | ref_clusters * s->cluster_size); |
1046 | de5f3f40 | Kevin Wolf | if (ret != ref_clusters * s->cluster_size) {
|
1047 | de5f3f40 | Kevin Wolf | ret = -errno; |
1048 | de5f3f40 | Kevin Wolf | goto exit;
|
1049 | de5f3f40 | Kevin Wolf | } |
1050 | de5f3f40 | Kevin Wolf | |
1051 | de5f3f40 | Kevin Wolf | ret = 0;
|
1052 | de5f3f40 | Kevin Wolf | exit:
|
1053 | de5f3f40 | Kevin Wolf | qemu_free(s->refcount_table); |
1054 | de5f3f40 | Kevin Wolf | qemu_free(s->refcount_block); |
1055 | de5f3f40 | Kevin Wolf | close(fd); |
1056 | de5f3f40 | Kevin Wolf | |
1057 | de5f3f40 | Kevin Wolf | /* Preallocate metadata */
|
1058 | de5f3f40 | Kevin Wolf | if (ret == 0 && prealloc) { |
1059 | de5f3f40 | Kevin Wolf | BlockDriverState *bs; |
1060 | 92b30744 | Kevin Wolf | BlockDriver *drv = bdrv_find_format("qcow2");
|
1061 | de5f3f40 | Kevin Wolf | bs = bdrv_new("");
|
1062 | 92b30744 | Kevin Wolf | bdrv_open(bs, filename, BDRV_O_CACHE_WB | BDRV_O_RDWR, drv); |
1063 | 19dbcbf7 | Kevin Wolf | ret = preallocate(bs); |
1064 | de5f3f40 | Kevin Wolf | bdrv_close(bs); |
1065 | de5f3f40 | Kevin Wolf | } |
1066 | de5f3f40 | Kevin Wolf | |
1067 | de5f3f40 | Kevin Wolf | return ret;
|
1068 | de5f3f40 | Kevin Wolf | } |
1069 | de5f3f40 | Kevin Wolf | |
1070 | de5f3f40 | Kevin Wolf | static int qcow_create(const char *filename, QEMUOptionParameter *options) |
1071 | de5f3f40 | Kevin Wolf | { |
1072 | de5f3f40 | Kevin Wolf | const char *backing_file = NULL; |
1073 | de5f3f40 | Kevin Wolf | const char *backing_fmt = NULL; |
1074 | de5f3f40 | Kevin Wolf | uint64_t sectors = 0;
|
1075 | de5f3f40 | Kevin Wolf | int flags = 0; |
1076 | de5f3f40 | Kevin Wolf | size_t cluster_size = 65536;
|
1077 | de5f3f40 | Kevin Wolf | int prealloc = 0; |
1078 | de5f3f40 | Kevin Wolf | |
1079 | de5f3f40 | Kevin Wolf | /* Read out options */
|
1080 | de5f3f40 | Kevin Wolf | while (options && options->name) {
|
1081 | de5f3f40 | Kevin Wolf | if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
|
1082 | de5f3f40 | Kevin Wolf | sectors = options->value.n / 512;
|
1083 | de5f3f40 | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { |
1084 | de5f3f40 | Kevin Wolf | backing_file = options->value.s; |
1085 | de5f3f40 | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { |
1086 | de5f3f40 | Kevin Wolf | backing_fmt = options->value.s; |
1087 | de5f3f40 | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) { |
1088 | de5f3f40 | Kevin Wolf | flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
|
1089 | de5f3f40 | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { |
1090 | de5f3f40 | Kevin Wolf | if (options->value.n) {
|
1091 | de5f3f40 | Kevin Wolf | cluster_size = options->value.n; |
1092 | de5f3f40 | Kevin Wolf | } |
1093 | de5f3f40 | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { |
1094 | de5f3f40 | Kevin Wolf | if (!options->value.s || !strcmp(options->value.s, "off")) { |
1095 | de5f3f40 | Kevin Wolf | prealloc = 0;
|
1096 | de5f3f40 | Kevin Wolf | } else if (!strcmp(options->value.s, "metadata")) { |
1097 | de5f3f40 | Kevin Wolf | prealloc = 1;
|
1098 | de5f3f40 | Kevin Wolf | } else {
|
1099 | de5f3f40 | Kevin Wolf | fprintf(stderr, "Invalid preallocation mode: '%s'\n",
|
1100 | de5f3f40 | Kevin Wolf | options->value.s); |
1101 | de5f3f40 | Kevin Wolf | return -EINVAL;
|
1102 | de5f3f40 | Kevin Wolf | } |
1103 | de5f3f40 | Kevin Wolf | } |
1104 | de5f3f40 | Kevin Wolf | options++; |
1105 | de5f3f40 | Kevin Wolf | } |
1106 | de5f3f40 | Kevin Wolf | |
1107 | de5f3f40 | Kevin Wolf | if (backing_file && prealloc) {
|
1108 | de5f3f40 | Kevin Wolf | fprintf(stderr, "Backing file and preallocation cannot be used at "
|
1109 | de5f3f40 | Kevin Wolf | "the same time\n");
|
1110 | de5f3f40 | Kevin Wolf | return -EINVAL;
|
1111 | de5f3f40 | Kevin Wolf | } |
1112 | de5f3f40 | Kevin Wolf | |
1113 | de5f3f40 | Kevin Wolf | return qcow_create2(filename, sectors, backing_file, backing_fmt, flags,
|
1114 | de5f3f40 | Kevin Wolf | cluster_size, prealloc); |
1115 | de5f3f40 | Kevin Wolf | } |
1116 | de5f3f40 | Kevin Wolf | |
1117 | 20d97356 | Blue Swirl | static int qcow_make_empty(BlockDriverState *bs) |
1118 | 20d97356 | Blue Swirl | { |
1119 | 20d97356 | Blue Swirl | #if 0
|
1120 | 20d97356 | Blue Swirl | /* XXX: not correct */
|
1121 | 20d97356 | Blue Swirl | BDRVQcowState *s = bs->opaque;
|
1122 | 20d97356 | Blue Swirl | uint32_t l1_length = s->l1_size * sizeof(uint64_t);
|
1123 | 20d97356 | Blue Swirl | int ret;
|
1124 | 20d97356 | Blue Swirl | |
1125 | 20d97356 | Blue Swirl | memset(s->l1_table, 0, l1_length);
|
1126 | 66f82cee | Kevin Wolf | if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0)
|
1127 | 20d97356 | Blue Swirl | return -1;
|
1128 | 66f82cee | Kevin Wolf | ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
|
1129 | 20d97356 | Blue Swirl | if (ret < 0)
|
1130 | 20d97356 | Blue Swirl | return ret;
|
1131 | 20d97356 | Blue Swirl | |
1132 | 20d97356 | Blue Swirl | l2_cache_reset(bs);
|
1133 | 20d97356 | Blue Swirl | #endif
|
1134 | 20d97356 | Blue Swirl | return 0; |
1135 | 20d97356 | Blue Swirl | } |
1136 | 20d97356 | Blue Swirl | |
1137 | 419b19d9 | Stefan Hajnoczi | static int qcow2_truncate(BlockDriverState *bs, int64_t offset) |
1138 | 419b19d9 | Stefan Hajnoczi | { |
1139 | 419b19d9 | Stefan Hajnoczi | BDRVQcowState *s = bs->opaque; |
1140 | 419b19d9 | Stefan Hajnoczi | int ret, new_l1_size;
|
1141 | 419b19d9 | Stefan Hajnoczi | |
1142 | 419b19d9 | Stefan Hajnoczi | if (offset & 511) { |
1143 | 419b19d9 | Stefan Hajnoczi | return -EINVAL;
|
1144 | 419b19d9 | Stefan Hajnoczi | } |
1145 | 419b19d9 | Stefan Hajnoczi | |
1146 | 419b19d9 | Stefan Hajnoczi | /* cannot proceed if image has snapshots */
|
1147 | 419b19d9 | Stefan Hajnoczi | if (s->nb_snapshots) {
|
1148 | 419b19d9 | Stefan Hajnoczi | return -ENOTSUP;
|
1149 | 419b19d9 | Stefan Hajnoczi | } |
1150 | 419b19d9 | Stefan Hajnoczi | |
1151 | 419b19d9 | Stefan Hajnoczi | /* shrinking is currently not supported */
|
1152 | 419b19d9 | Stefan Hajnoczi | if (offset < bs->total_sectors * 512) { |
1153 | 419b19d9 | Stefan Hajnoczi | return -ENOTSUP;
|
1154 | 419b19d9 | Stefan Hajnoczi | } |
1155 | 419b19d9 | Stefan Hajnoczi | |
1156 | 419b19d9 | Stefan Hajnoczi | new_l1_size = size_to_l1(s, offset); |
1157 | 72893756 | Stefan Hajnoczi | ret = qcow2_grow_l1_table(bs, new_l1_size, true);
|
1158 | 419b19d9 | Stefan Hajnoczi | if (ret < 0) { |
1159 | 419b19d9 | Stefan Hajnoczi | return ret;
|
1160 | 419b19d9 | Stefan Hajnoczi | } |
1161 | 419b19d9 | Stefan Hajnoczi | |
1162 | 419b19d9 | Stefan Hajnoczi | /* write updated header.size */
|
1163 | 419b19d9 | Stefan Hajnoczi | offset = cpu_to_be64(offset); |
1164 | 8b3b7206 | Kevin Wolf | ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), |
1165 | 8b3b7206 | Kevin Wolf | &offset, sizeof(uint64_t));
|
1166 | 419b19d9 | Stefan Hajnoczi | if (ret < 0) { |
1167 | 419b19d9 | Stefan Hajnoczi | return ret;
|
1168 | 419b19d9 | Stefan Hajnoczi | } |
1169 | 419b19d9 | Stefan Hajnoczi | |
1170 | 419b19d9 | Stefan Hajnoczi | s->l1_vm_state_index = new_l1_size; |
1171 | 419b19d9 | Stefan Hajnoczi | return 0; |
1172 | 419b19d9 | Stefan Hajnoczi | } |
1173 | 419b19d9 | Stefan Hajnoczi | |
1174 | 20d97356 | Blue Swirl | /* XXX: put compressed sectors first, then all the cluster aligned
|
1175 | 20d97356 | Blue Swirl | tables to avoid losing bytes in alignment */
|
1176 | 20d97356 | Blue Swirl | static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, |
1177 | 20d97356 | Blue Swirl | const uint8_t *buf, int nb_sectors) |
1178 | 20d97356 | Blue Swirl | { |
1179 | 20d97356 | Blue Swirl | BDRVQcowState *s = bs->opaque; |
1180 | 20d97356 | Blue Swirl | z_stream strm; |
1181 | 20d97356 | Blue Swirl | int ret, out_len;
|
1182 | 20d97356 | Blue Swirl | uint8_t *out_buf; |
1183 | 20d97356 | Blue Swirl | uint64_t cluster_offset; |
1184 | 20d97356 | Blue Swirl | |
1185 | 20d97356 | Blue Swirl | if (nb_sectors == 0) { |
1186 | 20d97356 | Blue Swirl | /* align end of file to a sector boundary to ease reading with
|
1187 | 20d97356 | Blue Swirl | sector based I/Os */
|
1188 | 66f82cee | Kevin Wolf | cluster_offset = bdrv_getlength(bs->file); |
1189 | 20d97356 | Blue Swirl | cluster_offset = (cluster_offset + 511) & ~511; |
1190 | 66f82cee | Kevin Wolf | bdrv_truncate(bs->file, cluster_offset); |
1191 | 20d97356 | Blue Swirl | return 0; |
1192 | 20d97356 | Blue Swirl | } |
1193 | 20d97356 | Blue Swirl | |
1194 | 20d97356 | Blue Swirl | if (nb_sectors != s->cluster_sectors)
|
1195 | 20d97356 | Blue Swirl | return -EINVAL;
|
1196 | 20d97356 | Blue Swirl | |
1197 | 20d97356 | Blue Swirl | out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); |
1198 | 20d97356 | Blue Swirl | |
1199 | 20d97356 | Blue Swirl | /* best compression, small window, no zlib header */
|
1200 | 20d97356 | Blue Swirl | memset(&strm, 0, sizeof(strm)); |
1201 | 20d97356 | Blue Swirl | ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, |
1202 | 20d97356 | Blue Swirl | Z_DEFLATED, -12,
|
1203 | 20d97356 | Blue Swirl | 9, Z_DEFAULT_STRATEGY);
|
1204 | 20d97356 | Blue Swirl | if (ret != 0) { |
1205 | 20d97356 | Blue Swirl | qemu_free(out_buf); |
1206 | 20d97356 | Blue Swirl | return -1; |
1207 | 20d97356 | Blue Swirl | } |
1208 | 20d97356 | Blue Swirl | |
1209 | 20d97356 | Blue Swirl | strm.avail_in = s->cluster_size; |
1210 | 20d97356 | Blue Swirl | strm.next_in = (uint8_t *)buf; |
1211 | 20d97356 | Blue Swirl | strm.avail_out = s->cluster_size; |
1212 | 20d97356 | Blue Swirl | strm.next_out = out_buf; |
1213 | 20d97356 | Blue Swirl | |
1214 | 20d97356 | Blue Swirl | ret = deflate(&strm, Z_FINISH); |
1215 | 20d97356 | Blue Swirl | if (ret != Z_STREAM_END && ret != Z_OK) {
|
1216 | 20d97356 | Blue Swirl | qemu_free(out_buf); |
1217 | 20d97356 | Blue Swirl | deflateEnd(&strm); |
1218 | 20d97356 | Blue Swirl | return -1; |
1219 | 20d97356 | Blue Swirl | } |
1220 | 20d97356 | Blue Swirl | out_len = strm.next_out - out_buf; |
1221 | 20d97356 | Blue Swirl | |
1222 | 20d97356 | Blue Swirl | deflateEnd(&strm); |
1223 | 20d97356 | Blue Swirl | |
1224 | 20d97356 | Blue Swirl | if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
|
1225 | 20d97356 | Blue Swirl | /* could not compress: write normal cluster */
|
1226 | 20d97356 | Blue Swirl | bdrv_write(bs, sector_num, buf, s->cluster_sectors); |
1227 | 20d97356 | Blue Swirl | } else {
|
1228 | 20d97356 | Blue Swirl | cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, |
1229 | 20d97356 | Blue Swirl | sector_num << 9, out_len);
|
1230 | 20d97356 | Blue Swirl | if (!cluster_offset)
|
1231 | 20d97356 | Blue Swirl | return -1; |
1232 | 20d97356 | Blue Swirl | cluster_offset &= s->cluster_offset_mask; |
1233 | 66f82cee | Kevin Wolf | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED); |
1234 | 66f82cee | Kevin Wolf | if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
|
1235 | 20d97356 | Blue Swirl | qemu_free(out_buf); |
1236 | 20d97356 | Blue Swirl | return -1; |
1237 | 20d97356 | Blue Swirl | } |
1238 | 20d97356 | Blue Swirl | } |
1239 | 20d97356 | Blue Swirl | |
1240 | 20d97356 | Blue Swirl | qemu_free(out_buf); |
1241 | 20d97356 | Blue Swirl | return 0; |
1242 | 20d97356 | Blue Swirl | } |
1243 | 20d97356 | Blue Swirl | |
1244 | 20d97356 | Blue Swirl | static void qcow_flush(BlockDriverState *bs) |
1245 | 20d97356 | Blue Swirl | { |
1246 | 66f82cee | Kevin Wolf | bdrv_flush(bs->file); |
1247 | 20d97356 | Blue Swirl | } |
1248 | 20d97356 | Blue Swirl | |
1249 | 20d97356 | Blue Swirl | static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
|
1250 | 20d97356 | Blue Swirl | BlockDriverCompletionFunc *cb, void *opaque)
|
1251 | 20d97356 | Blue Swirl | { |
1252 | 66f82cee | Kevin Wolf | return bdrv_aio_flush(bs->file, cb, opaque);
|
1253 | 20d97356 | Blue Swirl | } |
1254 | 20d97356 | Blue Swirl | |
1255 | 20d97356 | Blue Swirl | static int64_t qcow_vm_state_offset(BDRVQcowState *s)
|
1256 | 20d97356 | Blue Swirl | { |
1257 | 20d97356 | Blue Swirl | return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
|
1258 | 20d97356 | Blue Swirl | } |
1259 | 20d97356 | Blue Swirl | |
1260 | 20d97356 | Blue Swirl | static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
1261 | 20d97356 | Blue Swirl | { |
1262 | 20d97356 | Blue Swirl | BDRVQcowState *s = bs->opaque; |
1263 | 20d97356 | Blue Swirl | bdi->cluster_size = s->cluster_size; |
1264 | 20d97356 | Blue Swirl | bdi->vm_state_offset = qcow_vm_state_offset(s); |
1265 | 20d97356 | Blue Swirl | return 0; |
1266 | 20d97356 | Blue Swirl | } |
1267 | 20d97356 | Blue Swirl | |
1268 | 20d97356 | Blue Swirl | |
1269 | 9ac228e0 | Kevin Wolf | static int qcow_check(BlockDriverState *bs, BdrvCheckResult *result) |
1270 | 20d97356 | Blue Swirl | { |
1271 | 9ac228e0 | Kevin Wolf | return qcow2_check_refcounts(bs, result);
|
1272 | 20d97356 | Blue Swirl | } |
1273 | 20d97356 | Blue Swirl | |
1274 | 20d97356 | Blue Swirl | #if 0
|
1275 | 20d97356 | Blue Swirl | static void dump_refcounts(BlockDriverState *bs)
|
1276 | 20d97356 | Blue Swirl | {
|
1277 | 20d97356 | Blue Swirl | BDRVQcowState *s = bs->opaque;
|
1278 | 20d97356 | Blue Swirl | int64_t nb_clusters, k, k1, size;
|
1279 | 20d97356 | Blue Swirl | int refcount;
|
1280 | 20d97356 | Blue Swirl | |
1281 | 66f82cee | Kevin Wolf | size = bdrv_getlength(bs->file);
|
1282 | 20d97356 | Blue Swirl | nb_clusters = size_to_clusters(s, size);
|
1283 | 20d97356 | Blue Swirl | for(k = 0; k < nb_clusters;) {
|
1284 | 20d97356 | Blue Swirl | k1 = k;
|
1285 | 20d97356 | Blue Swirl | refcount = get_refcount(bs, k);
|
1286 | 20d97356 | Blue Swirl | k++;
|
1287 | 20d97356 | Blue Swirl | while (k < nb_clusters && get_refcount(bs, k) == refcount)
|
1288 | 20d97356 | Blue Swirl | k++;
|
1289 | 0bfcd599 | Blue Swirl | printf("%" PRId64 ": refcount=%d nb=%" PRId64 "\n", k, refcount,
|
1290 | 0bfcd599 | Blue Swirl | k - k1);
|
1291 | 20d97356 | Blue Swirl | }
|
1292 | 20d97356 | Blue Swirl | }
|
1293 | 20d97356 | Blue Swirl | #endif
|
1294 | 20d97356 | Blue Swirl | |
1295 | 20d97356 | Blue Swirl | static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
1296 | 20d97356 | Blue Swirl | int64_t pos, int size)
|
1297 | 20d97356 | Blue Swirl | { |
1298 | 20d97356 | Blue Swirl | BDRVQcowState *s = bs->opaque; |
1299 | 20d97356 | Blue Swirl | int growable = bs->growable;
|
1300 | 20d97356 | Blue Swirl | int ret;
|
1301 | 20d97356 | Blue Swirl | |
1302 | 66f82cee | Kevin Wolf | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE); |
1303 | 20d97356 | Blue Swirl | bs->growable = 1;
|
1304 | 20d97356 | Blue Swirl | ret = bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size); |
1305 | 20d97356 | Blue Swirl | bs->growable = growable; |
1306 | 20d97356 | Blue Swirl | |
1307 | 20d97356 | Blue Swirl | return ret;
|
1308 | 20d97356 | Blue Swirl | } |
1309 | 20d97356 | Blue Swirl | |
1310 | 20d97356 | Blue Swirl | static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
1311 | 20d97356 | Blue Swirl | int64_t pos, int size)
|
1312 | 20d97356 | Blue Swirl | { |
1313 | 20d97356 | Blue Swirl | BDRVQcowState *s = bs->opaque; |
1314 | 20d97356 | Blue Swirl | int growable = bs->growable;
|
1315 | 20d97356 | Blue Swirl | int ret;
|
1316 | 20d97356 | Blue Swirl | |
1317 | 66f82cee | Kevin Wolf | BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD); |
1318 | 20d97356 | Blue Swirl | bs->growable = 1;
|
1319 | 20d97356 | Blue Swirl | ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size); |
1320 | 20d97356 | Blue Swirl | bs->growable = growable; |
1321 | 20d97356 | Blue Swirl | |
1322 | 20d97356 | Blue Swirl | return ret;
|
1323 | 20d97356 | Blue Swirl | } |
1324 | 20d97356 | Blue Swirl | |
1325 | 20d97356 | Blue Swirl | static QEMUOptionParameter qcow_create_options[] = {
|
1326 | 20d97356 | Blue Swirl | { |
1327 | 20d97356 | Blue Swirl | .name = BLOCK_OPT_SIZE, |
1328 | 20d97356 | Blue Swirl | .type = OPT_SIZE, |
1329 | 20d97356 | Blue Swirl | .help = "Virtual disk size"
|
1330 | 20d97356 | Blue Swirl | }, |
1331 | 20d97356 | Blue Swirl | { |
1332 | 20d97356 | Blue Swirl | .name = BLOCK_OPT_BACKING_FILE, |
1333 | 20d97356 | Blue Swirl | .type = OPT_STRING, |
1334 | 20d97356 | Blue Swirl | .help = "File name of a base image"
|
1335 | 20d97356 | Blue Swirl | }, |
1336 | 20d97356 | Blue Swirl | { |
1337 | 20d97356 | Blue Swirl | .name = BLOCK_OPT_BACKING_FMT, |
1338 | 20d97356 | Blue Swirl | .type = OPT_STRING, |
1339 | 20d97356 | Blue Swirl | .help = "Image format of the base image"
|
1340 | 20d97356 | Blue Swirl | }, |
1341 | 20d97356 | Blue Swirl | { |
1342 | 20d97356 | Blue Swirl | .name = BLOCK_OPT_ENCRYPT, |
1343 | 20d97356 | Blue Swirl | .type = OPT_FLAG, |
1344 | 20d97356 | Blue Swirl | .help = "Encrypt the image"
|
1345 | 20d97356 | Blue Swirl | }, |
1346 | 20d97356 | Blue Swirl | { |
1347 | 20d97356 | Blue Swirl | .name = BLOCK_OPT_CLUSTER_SIZE, |
1348 | 20d97356 | Blue Swirl | .type = OPT_SIZE, |
1349 | 20d97356 | Blue Swirl | .help = "qcow2 cluster size"
|
1350 | 20d97356 | Blue Swirl | }, |
1351 | 20d97356 | Blue Swirl | { |
1352 | 20d97356 | Blue Swirl | .name = BLOCK_OPT_PREALLOC, |
1353 | 20d97356 | Blue Swirl | .type = OPT_STRING, |
1354 | 20d97356 | Blue Swirl | .help = "Preallocation mode (allowed values: off, metadata)"
|
1355 | 20d97356 | Blue Swirl | }, |
1356 | 20d97356 | Blue Swirl | { NULL }
|
1357 | 20d97356 | Blue Swirl | }; |
1358 | 20d97356 | Blue Swirl | |
1359 | 20d97356 | Blue Swirl | static BlockDriver bdrv_qcow2 = {
|
1360 | 20d97356 | Blue Swirl | .format_name = "qcow2",
|
1361 | 20d97356 | Blue Swirl | .instance_size = sizeof(BDRVQcowState),
|
1362 | 20d97356 | Blue Swirl | .bdrv_probe = qcow_probe, |
1363 | 20d97356 | Blue Swirl | .bdrv_open = qcow_open, |
1364 | 20d97356 | Blue Swirl | .bdrv_close = qcow_close, |
1365 | 20d97356 | Blue Swirl | .bdrv_create = qcow_create, |
1366 | 20d97356 | Blue Swirl | .bdrv_flush = qcow_flush, |
1367 | 20d97356 | Blue Swirl | .bdrv_is_allocated = qcow_is_allocated, |
1368 | 20d97356 | Blue Swirl | .bdrv_set_key = qcow_set_key, |
1369 | 20d97356 | Blue Swirl | .bdrv_make_empty = qcow_make_empty, |
1370 | 20d97356 | Blue Swirl | |
1371 | 20d97356 | Blue Swirl | .bdrv_aio_readv = qcow_aio_readv, |
1372 | 20d97356 | Blue Swirl | .bdrv_aio_writev = qcow_aio_writev, |
1373 | 20d97356 | Blue Swirl | .bdrv_aio_flush = qcow_aio_flush, |
1374 | 419b19d9 | Stefan Hajnoczi | |
1375 | 419b19d9 | Stefan Hajnoczi | .bdrv_truncate = qcow2_truncate, |
1376 | 419b19d9 | Stefan Hajnoczi | .bdrv_write_compressed = qcow_write_compressed, |
1377 | 20d97356 | Blue Swirl | |
1378 | 20d97356 | Blue Swirl | .bdrv_snapshot_create = qcow2_snapshot_create, |
1379 | 20d97356 | Blue Swirl | .bdrv_snapshot_goto = qcow2_snapshot_goto, |
1380 | 20d97356 | Blue Swirl | .bdrv_snapshot_delete = qcow2_snapshot_delete, |
1381 | 20d97356 | Blue Swirl | .bdrv_snapshot_list = qcow2_snapshot_list, |
1382 | 20d97356 | Blue Swirl | .bdrv_get_info = qcow_get_info, |
1383 | 20d97356 | Blue Swirl | |
1384 | 20d97356 | Blue Swirl | .bdrv_save_vmstate = qcow_save_vmstate, |
1385 | 20d97356 | Blue Swirl | .bdrv_load_vmstate = qcow_load_vmstate, |
1386 | 20d97356 | Blue Swirl | |
1387 | 20d97356 | Blue Swirl | .bdrv_change_backing_file = qcow2_change_backing_file, |
1388 | 20d97356 | Blue Swirl | |
1389 | 20d97356 | Blue Swirl | .create_options = qcow_create_options, |
1390 | 20d97356 | Blue Swirl | .bdrv_check = qcow_check, |
1391 | 20d97356 | Blue Swirl | }; |
1392 | 20d97356 | Blue Swirl | |
1393 | 5efa9d5a | Anthony Liguori | static void bdrv_qcow2_init(void) |
1394 | 5efa9d5a | Anthony Liguori | { |
1395 | 5efa9d5a | Anthony Liguori | bdrv_register(&bdrv_qcow2); |
1396 | 5efa9d5a | Anthony Liguori | } |
1397 | 5efa9d5a | Anthony Liguori | |
1398 | 5efa9d5a | Anthony Liguori | block_init(bdrv_qcow2_init); |