root / block / qcow2.c @ 72cf2d4f
History | View | Annotate | Download (33.7 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 | 9b80ddf3 | aliguori | |
56 | 585f8587 | bellard | |
57 | 585f8587 | bellard | static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) |
58 | 585f8587 | bellard | { |
59 | 585f8587 | bellard | const QCowHeader *cow_header = (const void *)buf; |
60 | 3b46e624 | ths | |
61 | 585f8587 | bellard | if (buf_size >= sizeof(QCowHeader) && |
62 | 585f8587 | bellard | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && |
63 | 5fafdf24 | ths | be32_to_cpu(cow_header->version) == QCOW_VERSION) |
64 | 585f8587 | bellard | return 100; |
65 | 585f8587 | bellard | else
|
66 | 585f8587 | bellard | return 0; |
67 | 585f8587 | bellard | } |
68 | 585f8587 | bellard | |
69 | 9b80ddf3 | aliguori | |
70 | 9b80ddf3 | aliguori | /*
|
71 | 9b80ddf3 | aliguori | * read qcow2 extension and fill bs
|
72 | 9b80ddf3 | aliguori | * start reading from start_offset
|
73 | 9b80ddf3 | aliguori | * finish reading upon magic of value 0 or when end_offset reached
|
74 | 9b80ddf3 | aliguori | * unknown magic is skipped (future extension this version knows nothing about)
|
75 | 9b80ddf3 | aliguori | * return 0 upon success, non-0 otherwise
|
76 | 9b80ddf3 | aliguori | */
|
77 | 9b80ddf3 | aliguori | static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset, |
78 | 9b80ddf3 | aliguori | uint64_t end_offset) |
79 | 9b80ddf3 | aliguori | { |
80 | 9b80ddf3 | aliguori | BDRVQcowState *s = bs->opaque; |
81 | 9b80ddf3 | aliguori | QCowExtension ext; |
82 | 9b80ddf3 | aliguori | uint64_t offset; |
83 | 9b80ddf3 | aliguori | |
84 | 9b80ddf3 | aliguori | #ifdef DEBUG_EXT
|
85 | 9b80ddf3 | aliguori | printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
|
86 | 9b80ddf3 | aliguori | #endif
|
87 | 9b80ddf3 | aliguori | offset = start_offset; |
88 | 9b80ddf3 | aliguori | while (offset < end_offset) {
|
89 | 9b80ddf3 | aliguori | |
90 | 9b80ddf3 | aliguori | #ifdef DEBUG_EXT
|
91 | 9b80ddf3 | aliguori | /* Sanity check */
|
92 | 9b80ddf3 | aliguori | if (offset > s->cluster_size)
|
93 | 9b80ddf3 | aliguori | printf("qcow_handle_extension: suspicious offset %lu\n", offset);
|
94 | 9b80ddf3 | aliguori | |
95 | 9b80ddf3 | aliguori | printf("attemting to read extended header in offset %lu\n", offset);
|
96 | 9b80ddf3 | aliguori | #endif
|
97 | 9b80ddf3 | aliguori | |
98 | 9b80ddf3 | aliguori | if (bdrv_pread(s->hd, offset, &ext, sizeof(ext)) != sizeof(ext)) { |
99 | 4c978075 | aliguori | fprintf(stderr, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
|
100 | 4c978075 | aliguori | (unsigned long long)offset); |
101 | 9b80ddf3 | aliguori | return 1; |
102 | 9b80ddf3 | aliguori | } |
103 | 9b80ddf3 | aliguori | be32_to_cpus(&ext.magic); |
104 | 9b80ddf3 | aliguori | be32_to_cpus(&ext.len); |
105 | 9b80ddf3 | aliguori | offset += sizeof(ext);
|
106 | 9b80ddf3 | aliguori | #ifdef DEBUG_EXT
|
107 | 9b80ddf3 | aliguori | printf("ext.magic = 0x%x\n", ext.magic);
|
108 | 9b80ddf3 | aliguori | #endif
|
109 | 9b80ddf3 | aliguori | switch (ext.magic) {
|
110 | 9b80ddf3 | aliguori | case QCOW_EXT_MAGIC_END:
|
111 | 9b80ddf3 | aliguori | return 0; |
112 | f965509c | aliguori | |
113 | f965509c | aliguori | case QCOW_EXT_MAGIC_BACKING_FORMAT:
|
114 | f965509c | aliguori | if (ext.len >= sizeof(bs->backing_format)) { |
115 | f965509c | aliguori | fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
|
116 | 4c978075 | aliguori | " (>=%zu)\n",
|
117 | f965509c | aliguori | ext.len, sizeof(bs->backing_format));
|
118 | f965509c | aliguori | return 2; |
119 | f965509c | aliguori | } |
120 | f965509c | aliguori | if (bdrv_pread(s->hd, offset , bs->backing_format,
|
121 | f965509c | aliguori | ext.len) != ext.len) |
122 | f965509c | aliguori | return 3; |
123 | f965509c | aliguori | bs->backing_format[ext.len] = '\0';
|
124 | f965509c | aliguori | #ifdef DEBUG_EXT
|
125 | f965509c | aliguori | printf("Qcow2: Got format extension %s\n", bs->backing_format);
|
126 | f965509c | aliguori | #endif
|
127 | f965509c | aliguori | offset += ((ext.len + 7) & ~7); |
128 | f965509c | aliguori | break;
|
129 | f965509c | aliguori | |
130 | 9b80ddf3 | aliguori | default:
|
131 | 9b80ddf3 | aliguori | /* unknown magic -- just skip it */
|
132 | 9b80ddf3 | aliguori | offset += ((ext.len + 7) & ~7); |
133 | 9b80ddf3 | aliguori | break;
|
134 | 9b80ddf3 | aliguori | } |
135 | 9b80ddf3 | aliguori | } |
136 | 9b80ddf3 | aliguori | |
137 | 9b80ddf3 | aliguori | return 0; |
138 | 9b80ddf3 | aliguori | } |
139 | 9b80ddf3 | aliguori | |
140 | 9b80ddf3 | aliguori | |
141 | 585f8587 | bellard | static int qcow_open(BlockDriverState *bs, const char *filename, int flags) |
142 | 585f8587 | bellard | { |
143 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
144 | 585f8587 | bellard | int len, i, shift, ret;
|
145 | 585f8587 | bellard | QCowHeader header; |
146 | 9b80ddf3 | aliguori | uint64_t ext_end; |
147 | 585f8587 | bellard | |
148 | b5eff355 | aurel32 | ret = bdrv_file_open(&s->hd, filename, flags); |
149 | 585f8587 | bellard | if (ret < 0) |
150 | 585f8587 | bellard | return ret;
|
151 | 585f8587 | bellard | if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header)) |
152 | 585f8587 | bellard | goto fail;
|
153 | 585f8587 | bellard | be32_to_cpus(&header.magic); |
154 | 585f8587 | bellard | be32_to_cpus(&header.version); |
155 | 585f8587 | bellard | be64_to_cpus(&header.backing_file_offset); |
156 | 585f8587 | bellard | be32_to_cpus(&header.backing_file_size); |
157 | 585f8587 | bellard | be64_to_cpus(&header.size); |
158 | 585f8587 | bellard | be32_to_cpus(&header.cluster_bits); |
159 | 585f8587 | bellard | be32_to_cpus(&header.crypt_method); |
160 | 585f8587 | bellard | be64_to_cpus(&header.l1_table_offset); |
161 | 585f8587 | bellard | be32_to_cpus(&header.l1_size); |
162 | 585f8587 | bellard | be64_to_cpus(&header.refcount_table_offset); |
163 | 585f8587 | bellard | be32_to_cpus(&header.refcount_table_clusters); |
164 | 585f8587 | bellard | be64_to_cpus(&header.snapshots_offset); |
165 | 585f8587 | bellard | be32_to_cpus(&header.nb_snapshots); |
166 | 3b46e624 | ths | |
167 | 585f8587 | bellard | if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
|
168 | 585f8587 | bellard | goto fail;
|
169 | 5fafdf24 | ths | if (header.size <= 1 || |
170 | 73c632ed | Kevin Wolf | header.cluster_bits < MIN_CLUSTER_BITS || |
171 | 73c632ed | Kevin Wolf | header.cluster_bits > MAX_CLUSTER_BITS) |
172 | 585f8587 | bellard | goto fail;
|
173 | 585f8587 | bellard | if (header.crypt_method > QCOW_CRYPT_AES)
|
174 | 585f8587 | bellard | goto fail;
|
175 | 585f8587 | bellard | s->crypt_method_header = header.crypt_method; |
176 | 585f8587 | bellard | if (s->crypt_method_header)
|
177 | 585f8587 | bellard | bs->encrypted = 1;
|
178 | 585f8587 | bellard | s->cluster_bits = header.cluster_bits; |
179 | 585f8587 | bellard | s->cluster_size = 1 << s->cluster_bits;
|
180 | 585f8587 | bellard | s->cluster_sectors = 1 << (s->cluster_bits - 9); |
181 | 585f8587 | bellard | s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ |
182 | 585f8587 | bellard | s->l2_size = 1 << s->l2_bits;
|
183 | 585f8587 | bellard | bs->total_sectors = header.size / 512;
|
184 | 585f8587 | bellard | s->csize_shift = (62 - (s->cluster_bits - 8)); |
185 | 585f8587 | bellard | s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; |
186 | 585f8587 | bellard | s->cluster_offset_mask = (1LL << s->csize_shift) - 1; |
187 | 585f8587 | bellard | s->refcount_table_offset = header.refcount_table_offset; |
188 | 5fafdf24 | ths | s->refcount_table_size = |
189 | 585f8587 | bellard | header.refcount_table_clusters << (s->cluster_bits - 3);
|
190 | 585f8587 | bellard | |
191 | 585f8587 | bellard | s->snapshots_offset = header.snapshots_offset; |
192 | 585f8587 | bellard | s->nb_snapshots = header.nb_snapshots; |
193 | 585f8587 | bellard | |
194 | 585f8587 | bellard | /* read the level 1 table */
|
195 | 585f8587 | bellard | s->l1_size = header.l1_size; |
196 | 585f8587 | bellard | shift = s->cluster_bits + s->l2_bits; |
197 | 585f8587 | bellard | s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift; |
198 | 585f8587 | bellard | /* the L1 table must contain at least enough entries to put
|
199 | 585f8587 | bellard | header.size bytes */
|
200 | 585f8587 | bellard | if (s->l1_size < s->l1_vm_state_index)
|
201 | 585f8587 | bellard | goto fail;
|
202 | 585f8587 | bellard | s->l1_table_offset = header.l1_table_offset; |
203 | 3f6a3ee5 | Kevin Wolf | s->l1_table = qemu_mallocz( |
204 | 3f6a3ee5 | Kevin Wolf | align_offset(s->l1_size * sizeof(uint64_t), 512)); |
205 | 5fafdf24 | ths | if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
206 | 585f8587 | bellard | s->l1_size * sizeof(uint64_t))
|
207 | 585f8587 | bellard | goto fail;
|
208 | 585f8587 | bellard | for(i = 0;i < s->l1_size; i++) { |
209 | 585f8587 | bellard | be64_to_cpus(&s->l1_table[i]); |
210 | 585f8587 | bellard | } |
211 | 585f8587 | bellard | /* alloc L2 cache */
|
212 | 585f8587 | bellard | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
|
213 | 585f8587 | bellard | s->cluster_cache = qemu_malloc(s->cluster_size); |
214 | 585f8587 | bellard | /* one more sector for decompressed data alignment */
|
215 | 095a9c58 | aliguori | s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size |
216 | 095a9c58 | aliguori | + 512);
|
217 | 585f8587 | bellard | s->cluster_cache_offset = -1;
|
218 | 3b46e624 | ths | |
219 | ed6ccf0f | Kevin Wolf | if (qcow2_refcount_init(bs) < 0) |
220 | 585f8587 | bellard | goto fail;
|
221 | 585f8587 | bellard | |
222 | 72cf2d4f | Blue Swirl | QLIST_INIT(&s->cluster_allocs); |
223 | f214978a | Kevin Wolf | |
224 | 9b80ddf3 | aliguori | /* read qcow2 extensions */
|
225 | 9b80ddf3 | aliguori | if (header.backing_file_offset)
|
226 | 9b80ddf3 | aliguori | ext_end = header.backing_file_offset; |
227 | 9b80ddf3 | aliguori | else
|
228 | 9b80ddf3 | aliguori | ext_end = s->cluster_size; |
229 | 9b80ddf3 | aliguori | if (qcow_read_extensions(bs, sizeof(header), ext_end)) |
230 | 9b80ddf3 | aliguori | goto fail;
|
231 | 9b80ddf3 | aliguori | |
232 | 585f8587 | bellard | /* read the backing file name */
|
233 | 585f8587 | bellard | if (header.backing_file_offset != 0) { |
234 | 585f8587 | bellard | len = header.backing_file_size; |
235 | 585f8587 | bellard | if (len > 1023) |
236 | 585f8587 | bellard | len = 1023;
|
237 | 585f8587 | bellard | if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
|
238 | 585f8587 | bellard | goto fail;
|
239 | 585f8587 | bellard | bs->backing_file[len] = '\0';
|
240 | 585f8587 | bellard | } |
241 | ed6ccf0f | Kevin Wolf | if (qcow2_read_snapshots(bs) < 0) |
242 | 585f8587 | bellard | goto fail;
|
243 | 585f8587 | bellard | |
244 | 585f8587 | bellard | #ifdef DEBUG_ALLOC
|
245 | 14899cdf | Filip Navara | qcow2_check_refcounts(bs); |
246 | 585f8587 | bellard | #endif
|
247 | 585f8587 | bellard | return 0; |
248 | 585f8587 | bellard | |
249 | 585f8587 | bellard | fail:
|
250 | ed6ccf0f | Kevin Wolf | qcow2_free_snapshots(bs); |
251 | ed6ccf0f | Kevin Wolf | qcow2_refcount_close(bs); |
252 | 585f8587 | bellard | qemu_free(s->l1_table); |
253 | 585f8587 | bellard | qemu_free(s->l2_cache); |
254 | 585f8587 | bellard | qemu_free(s->cluster_cache); |
255 | 585f8587 | bellard | qemu_free(s->cluster_data); |
256 | 585f8587 | bellard | bdrv_delete(s->hd); |
257 | 585f8587 | bellard | return -1; |
258 | 585f8587 | bellard | } |
259 | 585f8587 | bellard | |
260 | 585f8587 | bellard | static int qcow_set_key(BlockDriverState *bs, const char *key) |
261 | 585f8587 | bellard | { |
262 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
263 | 585f8587 | bellard | uint8_t keybuf[16];
|
264 | 585f8587 | bellard | int len, i;
|
265 | 3b46e624 | ths | |
266 | 585f8587 | bellard | memset(keybuf, 0, 16); |
267 | 585f8587 | bellard | len = strlen(key); |
268 | 585f8587 | bellard | if (len > 16) |
269 | 585f8587 | bellard | len = 16;
|
270 | 585f8587 | bellard | /* XXX: we could compress the chars to 7 bits to increase
|
271 | 585f8587 | bellard | entropy */
|
272 | 585f8587 | bellard | for(i = 0;i < len;i++) { |
273 | 585f8587 | bellard | keybuf[i] = key[i]; |
274 | 585f8587 | bellard | } |
275 | 585f8587 | bellard | s->crypt_method = s->crypt_method_header; |
276 | 585f8587 | bellard | |
277 | 585f8587 | bellard | if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) |
278 | 585f8587 | bellard | return -1; |
279 | 585f8587 | bellard | if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) |
280 | 585f8587 | bellard | return -1; |
281 | 585f8587 | bellard | #if 0
|
282 | 585f8587 | bellard | /* test */
|
283 | 585f8587 | bellard | {
|
284 | 585f8587 | bellard | uint8_t in[16];
|
285 | 585f8587 | bellard | uint8_t out[16];
|
286 | 585f8587 | bellard | uint8_t tmp[16];
|
287 | 585f8587 | bellard | for(i=0;i<16;i++)
|
288 | 585f8587 | bellard | in[i] = i;
|
289 | 585f8587 | bellard | AES_encrypt(in, tmp, &s->aes_encrypt_key);
|
290 | 585f8587 | bellard | AES_decrypt(tmp, out, &s->aes_decrypt_key);
|
291 | 585f8587 | bellard | for(i = 0; i < 16; i++)
|
292 | 585f8587 | bellard | printf(" %02x", tmp[i]);
|
293 | 585f8587 | bellard | printf("\n");
|
294 | 585f8587 | bellard | for(i = 0; i < 16; i++)
|
295 | 585f8587 | bellard | printf(" %02x", out[i]);
|
296 | 585f8587 | bellard | printf("\n");
|
297 | 585f8587 | bellard | }
|
298 | 585f8587 | bellard | #endif
|
299 | 585f8587 | bellard | return 0; |
300 | 585f8587 | bellard | } |
301 | 585f8587 | bellard | |
302 | 5fafdf24 | ths | static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
303 | 585f8587 | bellard | int nb_sectors, int *pnum) |
304 | 585f8587 | bellard | { |
305 | 585f8587 | bellard | uint64_t cluster_offset; |
306 | 585f8587 | bellard | |
307 | 095a9c58 | aliguori | *pnum = nb_sectors; |
308 | ed6ccf0f | Kevin Wolf | cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, pnum);
|
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 | ed6ccf0f | Kevin Wolf | int qcow2_backing_read1(BlockDriverState *bs,
|
315 | 45aba42f | Kevin Wolf | int64_t sector_num, uint8_t *buf, 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 | a9465922 | bellard | memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1)); |
325 | a9465922 | bellard | return n1;
|
326 | a9465922 | bellard | } |
327 | a9465922 | bellard | |
328 | ce1a14dc | pbrook | typedef struct QCowAIOCB { |
329 | ce1a14dc | pbrook | BlockDriverAIOCB common; |
330 | 585f8587 | bellard | int64_t sector_num; |
331 | f141eafe | aliguori | QEMUIOVector *qiov; |
332 | 585f8587 | bellard | uint8_t *buf; |
333 | f141eafe | aliguori | void *orig_buf;
|
334 | 585f8587 | bellard | int nb_sectors;
|
335 | 585f8587 | bellard | int n;
|
336 | 585f8587 | bellard | uint64_t cluster_offset; |
337 | 5fafdf24 | ths | uint8_t *cluster_data; |
338 | 585f8587 | bellard | BlockDriverAIOCB *hd_aiocb; |
339 | c87c0672 | aliguori | struct iovec hd_iov;
|
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 | c16b5a2c | Christoph Hellwig | QCowAIOCB *acb = (QCowAIOCB *)blockacb; |
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 | ed6ccf0f | Kevin Wolf | qcow2_encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, |
401 | 5fafdf24 | ths | acb->n, 0,
|
402 | 585f8587 | bellard | &s->aes_decrypt_key); |
403 | 585f8587 | bellard | } |
404 | 585f8587 | bellard | } |
405 | 585f8587 | bellard | |
406 | ce1a14dc | pbrook | acb->nb_sectors -= acb->n; |
407 | ce1a14dc | pbrook | acb->sector_num += acb->n; |
408 | ce1a14dc | pbrook | acb->buf += acb->n * 512;
|
409 | 585f8587 | bellard | |
410 | ce1a14dc | pbrook | if (acb->nb_sectors == 0) { |
411 | 585f8587 | bellard | /* request completed */
|
412 | f141eafe | aliguori | ret = 0;
|
413 | f141eafe | aliguori | goto done;
|
414 | 585f8587 | bellard | } |
415 | 3b46e624 | ths | |
416 | 585f8587 | bellard | /* prepare next AIO request */
|
417 | 095a9c58 | aliguori | acb->n = acb->nb_sectors; |
418 | ed6ccf0f | Kevin Wolf | acb->cluster_offset = |
419 | ed6ccf0f | Kevin Wolf | qcow2_get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
|
420 | ce1a14dc | pbrook | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
|
421 | ce1a14dc | pbrook | |
422 | ce1a14dc | pbrook | if (!acb->cluster_offset) {
|
423 | 585f8587 | bellard | if (bs->backing_hd) {
|
424 | 585f8587 | bellard | /* read from the base image */
|
425 | ed6ccf0f | Kevin Wolf | n1 = qcow2_backing_read1(bs->backing_hd, acb->sector_num, |
426 | ce1a14dc | pbrook | acb->buf, acb->n); |
427 | a9465922 | bellard | if (n1 > 0) { |
428 | 3f4cb3d3 | blueswir1 | acb->hd_iov.iov_base = (void *)acb->buf;
|
429 | c87c0672 | aliguori | acb->hd_iov.iov_len = acb->n * 512;
|
430 | c87c0672 | aliguori | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
|
431 | c87c0672 | aliguori | acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num, |
432 | c87c0672 | aliguori | &acb->hd_qiov, acb->n, |
433 | c87c0672 | aliguori | qcow_aio_read_cb, acb); |
434 | ce1a14dc | pbrook | if (acb->hd_aiocb == NULL) |
435 | f141eafe | aliguori | goto done;
|
436 | a9465922 | bellard | } else {
|
437 | a32ef786 | aliguori | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
438 | a32ef786 | aliguori | if (ret < 0) |
439 | f141eafe | aliguori | goto done;
|
440 | a9465922 | bellard | } |
441 | 585f8587 | bellard | } else {
|
442 | 585f8587 | bellard | /* Note: in this case, no need to wait */
|
443 | ce1a14dc | pbrook | memset(acb->buf, 0, 512 * acb->n); |
444 | a32ef786 | aliguori | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
445 | a32ef786 | aliguori | if (ret < 0) |
446 | f141eafe | aliguori | goto done;
|
447 | 585f8587 | bellard | } |
448 | ce1a14dc | pbrook | } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
449 | 585f8587 | bellard | /* add AIO support for compressed blocks ? */
|
450 | ed6ccf0f | Kevin Wolf | if (qcow2_decompress_cluster(s, acb->cluster_offset) < 0) |
451 | f141eafe | aliguori | goto done;
|
452 | 5fafdf24 | ths | memcpy(acb->buf, |
453 | ce1a14dc | pbrook | s->cluster_cache + index_in_cluster * 512, 512 * acb->n); |
454 | a32ef786 | aliguori | ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
455 | a32ef786 | aliguori | if (ret < 0) |
456 | f141eafe | aliguori | goto done;
|
457 | 585f8587 | bellard | } else {
|
458 | ce1a14dc | pbrook | if ((acb->cluster_offset & 511) != 0) { |
459 | 585f8587 | bellard | ret = -EIO; |
460 | f141eafe | aliguori | goto done;
|
461 | 585f8587 | bellard | } |
462 | c87c0672 | aliguori | |
463 | 3f4cb3d3 | blueswir1 | acb->hd_iov.iov_base = (void *)acb->buf;
|
464 | c87c0672 | aliguori | acb->hd_iov.iov_len = acb->n * 512;
|
465 | c87c0672 | aliguori | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
|
466 | c87c0672 | aliguori | acb->hd_aiocb = bdrv_aio_readv(s->hd, |
467 | 5fafdf24 | ths | (acb->cluster_offset >> 9) + index_in_cluster,
|
468 | c87c0672 | aliguori | &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb); |
469 | ce1a14dc | pbrook | if (acb->hd_aiocb == NULL) |
470 | f141eafe | aliguori | goto done;
|
471 | f141eafe | aliguori | } |
472 | f141eafe | aliguori | |
473 | f141eafe | aliguori | return;
|
474 | f141eafe | aliguori | done:
|
475 | f141eafe | aliguori | if (acb->qiov->niov > 1) { |
476 | f141eafe | aliguori | qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size); |
477 | f141eafe | aliguori | qemu_vfree(acb->orig_buf); |
478 | 585f8587 | bellard | } |
479 | f141eafe | aliguori | acb->common.cb(acb->common.opaque, ret); |
480 | f141eafe | aliguori | qemu_aio_release(acb); |
481 | 585f8587 | bellard | } |
482 | 585f8587 | bellard | |
483 | ce1a14dc | pbrook | static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
|
484 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
485 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque, int is_write) |
486 | 585f8587 | bellard | { |
487 | ce1a14dc | pbrook | QCowAIOCB *acb; |
488 | ce1a14dc | pbrook | |
489 | c16b5a2c | Christoph Hellwig | acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque); |
490 | ce1a14dc | pbrook | if (!acb)
|
491 | ce1a14dc | pbrook | return NULL; |
492 | ce1a14dc | pbrook | acb->hd_aiocb = NULL;
|
493 | ce1a14dc | pbrook | acb->sector_num = sector_num; |
494 | f141eafe | aliguori | acb->qiov = qiov; |
495 | f141eafe | aliguori | if (qiov->niov > 1) { |
496 | e268ca52 | aliguori | acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size); |
497 | f141eafe | aliguori | if (is_write)
|
498 | f141eafe | aliguori | qemu_iovec_to_buffer(qiov, acb->buf); |
499 | 3f4cb3d3 | blueswir1 | } else {
|
500 | 3f4cb3d3 | blueswir1 | acb->buf = (uint8_t *)qiov->iov->iov_base; |
501 | 3f4cb3d3 | blueswir1 | } |
502 | ce1a14dc | pbrook | acb->nb_sectors = nb_sectors; |
503 | ce1a14dc | pbrook | acb->n = 0;
|
504 | ce1a14dc | pbrook | acb->cluster_offset = 0;
|
505 | e976c6a1 | aliguori | acb->l2meta.nb_clusters = 0;
|
506 | 72cf2d4f | Blue Swirl | QLIST_INIT(&acb->l2meta.dependent_requests); |
507 | ce1a14dc | pbrook | return acb;
|
508 | ce1a14dc | pbrook | } |
509 | ce1a14dc | pbrook | |
510 | f141eafe | aliguori | static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs,
|
511 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
512 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
513 | ce1a14dc | pbrook | { |
514 | ce1a14dc | pbrook | QCowAIOCB *acb; |
515 | ce1a14dc | pbrook | |
516 | f141eafe | aliguori | acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
|
517 | ce1a14dc | pbrook | if (!acb)
|
518 | ce1a14dc | pbrook | return NULL; |
519 | 585f8587 | bellard | |
520 | 585f8587 | bellard | qcow_aio_read_cb(acb, 0);
|
521 | ce1a14dc | pbrook | return &acb->common;
|
522 | 585f8587 | bellard | } |
523 | 585f8587 | bellard | |
524 | f214978a | Kevin Wolf | static void qcow_aio_write_cb(void *opaque, int ret); |
525 | f214978a | Kevin Wolf | |
526 | f214978a | Kevin Wolf | static void run_dependent_requests(QCowL2Meta *m) |
527 | f214978a | Kevin Wolf | { |
528 | f214978a | Kevin Wolf | QCowAIOCB *req; |
529 | f214978a | Kevin Wolf | QCowAIOCB *next; |
530 | f214978a | Kevin Wolf | |
531 | f214978a | Kevin Wolf | /* Take the request off the list of running requests */
|
532 | f214978a | Kevin Wolf | if (m->nb_clusters != 0) { |
533 | 72cf2d4f | Blue Swirl | QLIST_REMOVE(m, next_in_flight); |
534 | f214978a | Kevin Wolf | } |
535 | f214978a | Kevin Wolf | |
536 | f214978a | Kevin Wolf | /*
|
537 | f214978a | Kevin Wolf | * Restart all dependent requests.
|
538 | 72cf2d4f | Blue Swirl | * Can't use QLIST_FOREACH here - the next link might not be the same
|
539 | f214978a | Kevin Wolf | * any more after the callback (request could depend on a different
|
540 | f214978a | Kevin Wolf | * request now)
|
541 | f214978a | Kevin Wolf | */
|
542 | f214978a | Kevin Wolf | for (req = m->dependent_requests.lh_first; req != NULL; req = next) { |
543 | f214978a | Kevin Wolf | next = req->next_depend.le_next; |
544 | f214978a | Kevin Wolf | qcow_aio_write_cb(req, 0);
|
545 | f214978a | Kevin Wolf | } |
546 | f214978a | Kevin Wolf | |
547 | f214978a | Kevin Wolf | /* Empty the list for the next part of the request */
|
548 | 72cf2d4f | Blue Swirl | QLIST_INIT(&m->dependent_requests); |
549 | f214978a | Kevin Wolf | } |
550 | f214978a | Kevin Wolf | |
551 | 585f8587 | bellard | static void qcow_aio_write_cb(void *opaque, int ret) |
552 | 585f8587 | bellard | { |
553 | ce1a14dc | pbrook | QCowAIOCB *acb = opaque; |
554 | ce1a14dc | pbrook | BlockDriverState *bs = acb->common.bs; |
555 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
556 | 585f8587 | bellard | int index_in_cluster;
|
557 | 585f8587 | bellard | const uint8_t *src_buf;
|
558 | 095a9c58 | aliguori | int n_end;
|
559 | ce1a14dc | pbrook | |
560 | ce1a14dc | pbrook | acb->hd_aiocb = NULL;
|
561 | ce1a14dc | pbrook | |
562 | f214978a | Kevin Wolf | if (ret >= 0) { |
563 | f214978a | Kevin Wolf | ret = qcow2_alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta); |
564 | f214978a | Kevin Wolf | } |
565 | f214978a | Kevin Wolf | |
566 | f214978a | Kevin Wolf | run_dependent_requests(&acb->l2meta); |
567 | f214978a | Kevin Wolf | |
568 | f141eafe | aliguori | if (ret < 0) |
569 | f141eafe | aliguori | goto done;
|
570 | 585f8587 | bellard | |
571 | ce1a14dc | pbrook | acb->nb_sectors -= acb->n; |
572 | ce1a14dc | pbrook | acb->sector_num += acb->n; |
573 | ce1a14dc | pbrook | acb->buf += acb->n * 512;
|
574 | 585f8587 | bellard | |
575 | ce1a14dc | pbrook | if (acb->nb_sectors == 0) { |
576 | 585f8587 | bellard | /* request completed */
|
577 | f141eafe | aliguori | ret = 0;
|
578 | f141eafe | aliguori | goto done;
|
579 | 585f8587 | bellard | } |
580 | 3b46e624 | ths | |
581 | ce1a14dc | pbrook | index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
|
582 | 095a9c58 | aliguori | n_end = index_in_cluster + acb->nb_sectors; |
583 | 095a9c58 | aliguori | if (s->crypt_method &&
|
584 | 095a9c58 | aliguori | n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) |
585 | 095a9c58 | aliguori | n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; |
586 | 095a9c58 | aliguori | |
587 | ed6ccf0f | Kevin Wolf | acb->cluster_offset = qcow2_alloc_cluster_offset(bs, acb->sector_num << 9,
|
588 | 05203524 | aliguori | index_in_cluster, |
589 | e976c6a1 | aliguori | n_end, &acb->n, &acb->l2meta); |
590 | f214978a | Kevin Wolf | |
591 | f214978a | Kevin Wolf | /* Need to wait for another request? If so, we are done for now. */
|
592 | f214978a | Kevin Wolf | if (!acb->cluster_offset && acb->l2meta.depends_on != NULL) { |
593 | 72cf2d4f | Blue Swirl | QLIST_INSERT_HEAD(&acb->l2meta.depends_on->dependent_requests, |
594 | f214978a | Kevin Wolf | acb, next_depend); |
595 | f214978a | Kevin Wolf | return;
|
596 | f214978a | Kevin Wolf | } |
597 | f214978a | Kevin Wolf | |
598 | e976c6a1 | aliguori | if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) { |
599 | 585f8587 | bellard | ret = -EIO; |
600 | f141eafe | aliguori | goto done;
|
601 | 585f8587 | bellard | } |
602 | 585f8587 | bellard | if (s->crypt_method) {
|
603 | ce1a14dc | pbrook | if (!acb->cluster_data) {
|
604 | 095a9c58 | aliguori | acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * |
605 | 095a9c58 | aliguori | s->cluster_size); |
606 | 585f8587 | bellard | } |
607 | ed6ccf0f | Kevin Wolf | qcow2_encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf, |
608 | ce1a14dc | pbrook | acb->n, 1, &s->aes_encrypt_key);
|
609 | ce1a14dc | pbrook | src_buf = acb->cluster_data; |
610 | 585f8587 | bellard | } else {
|
611 | ce1a14dc | pbrook | src_buf = acb->buf; |
612 | 585f8587 | bellard | } |
613 | c87c0672 | aliguori | acb->hd_iov.iov_base = (void *)src_buf;
|
614 | c87c0672 | aliguori | acb->hd_iov.iov_len = acb->n * 512;
|
615 | c87c0672 | aliguori | qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
|
616 | c87c0672 | aliguori | acb->hd_aiocb = bdrv_aio_writev(s->hd, |
617 | c87c0672 | aliguori | (acb->cluster_offset >> 9) + index_in_cluster,
|
618 | c87c0672 | aliguori | &acb->hd_qiov, acb->n, |
619 | c87c0672 | aliguori | qcow_aio_write_cb, acb); |
620 | ce1a14dc | pbrook | if (acb->hd_aiocb == NULL) |
621 | f141eafe | aliguori | goto done;
|
622 | f141eafe | aliguori | |
623 | f141eafe | aliguori | return;
|
624 | f141eafe | aliguori | |
625 | f141eafe | aliguori | done:
|
626 | f141eafe | aliguori | if (acb->qiov->niov > 1) |
627 | f141eafe | aliguori | qemu_vfree(acb->orig_buf); |
628 | f141eafe | aliguori | acb->common.cb(acb->common.opaque, ret); |
629 | f141eafe | aliguori | qemu_aio_release(acb); |
630 | 585f8587 | bellard | } |
631 | 585f8587 | bellard | |
632 | f141eafe | aliguori | static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs,
|
633 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
634 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
635 | 585f8587 | bellard | { |
636 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
637 | ce1a14dc | pbrook | QCowAIOCB *acb; |
638 | 3b46e624 | ths | |
639 | 585f8587 | bellard | s->cluster_cache_offset = -1; /* disable compressed cache */ |
640 | 585f8587 | bellard | |
641 | f141eafe | aliguori | acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
|
642 | ce1a14dc | pbrook | if (!acb)
|
643 | ce1a14dc | pbrook | return NULL; |
644 | 3b46e624 | ths | |
645 | 585f8587 | bellard | qcow_aio_write_cb(acb, 0);
|
646 | ce1a14dc | pbrook | return &acb->common;
|
647 | 585f8587 | bellard | } |
648 | 585f8587 | bellard | |
649 | 585f8587 | bellard | static void qcow_close(BlockDriverState *bs) |
650 | 585f8587 | bellard | { |
651 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
652 | 585f8587 | bellard | qemu_free(s->l1_table); |
653 | 585f8587 | bellard | qemu_free(s->l2_cache); |
654 | 585f8587 | bellard | qemu_free(s->cluster_cache); |
655 | 585f8587 | bellard | qemu_free(s->cluster_data); |
656 | ed6ccf0f | Kevin Wolf | qcow2_refcount_close(bs); |
657 | 585f8587 | bellard | bdrv_delete(s->hd); |
658 | 585f8587 | bellard | } |
659 | 585f8587 | bellard | |
660 | 73c632ed | Kevin Wolf | static int get_bits_from_size(size_t size) |
661 | 73c632ed | Kevin Wolf | { |
662 | 73c632ed | Kevin Wolf | int res = 0; |
663 | 73c632ed | Kevin Wolf | |
664 | 73c632ed | Kevin Wolf | if (size == 0) { |
665 | 73c632ed | Kevin Wolf | return -1; |
666 | 73c632ed | Kevin Wolf | } |
667 | 73c632ed | Kevin Wolf | |
668 | 73c632ed | Kevin Wolf | while (size != 1) { |
669 | 73c632ed | Kevin Wolf | /* Not a power of two */
|
670 | 73c632ed | Kevin Wolf | if (size & 1) { |
671 | 73c632ed | Kevin Wolf | return -1; |
672 | 73c632ed | Kevin Wolf | } |
673 | 73c632ed | Kevin Wolf | |
674 | 73c632ed | Kevin Wolf | size >>= 1;
|
675 | 73c632ed | Kevin Wolf | res++; |
676 | 73c632ed | Kevin Wolf | } |
677 | 73c632ed | Kevin Wolf | |
678 | 73c632ed | Kevin Wolf | return res;
|
679 | 73c632ed | Kevin Wolf | } |
680 | 73c632ed | Kevin Wolf | |
681 | a35e1c17 | Kevin Wolf | |
682 | a35e1c17 | Kevin Wolf | static int preallocate(BlockDriverState *bs) |
683 | a35e1c17 | Kevin Wolf | { |
684 | a35e1c17 | Kevin Wolf | BDRVQcowState *s = bs->opaque; |
685 | 2000cbc5 | Blue Swirl | uint64_t cluster_offset = 0;
|
686 | a35e1c17 | Kevin Wolf | uint64_t nb_sectors; |
687 | a35e1c17 | Kevin Wolf | uint64_t offset; |
688 | a35e1c17 | Kevin Wolf | int num;
|
689 | a35e1c17 | Kevin Wolf | QCowL2Meta meta; |
690 | a35e1c17 | Kevin Wolf | |
691 | a35e1c17 | Kevin Wolf | nb_sectors = bdrv_getlength(bs) >> 9;
|
692 | a35e1c17 | Kevin Wolf | offset = 0;
|
693 | 72cf2d4f | Blue Swirl | QLIST_INIT(&meta.dependent_requests); |
694 | a35e1c17 | Kevin Wolf | |
695 | a35e1c17 | Kevin Wolf | while (nb_sectors) {
|
696 | a35e1c17 | Kevin Wolf | num = MIN(nb_sectors, INT_MAX >> 9);
|
697 | a35e1c17 | Kevin Wolf | cluster_offset = qcow2_alloc_cluster_offset(bs, offset, 0, num, &num,
|
698 | a35e1c17 | Kevin Wolf | &meta); |
699 | a35e1c17 | Kevin Wolf | |
700 | a35e1c17 | Kevin Wolf | if (cluster_offset == 0) { |
701 | a35e1c17 | Kevin Wolf | return -1; |
702 | a35e1c17 | Kevin Wolf | } |
703 | a35e1c17 | Kevin Wolf | |
704 | a35e1c17 | Kevin Wolf | if (qcow2_alloc_cluster_link_l2(bs, cluster_offset, &meta) < 0) { |
705 | a35e1c17 | Kevin Wolf | qcow2_free_any_clusters(bs, cluster_offset, meta.nb_clusters); |
706 | a35e1c17 | Kevin Wolf | return -1; |
707 | a35e1c17 | Kevin Wolf | } |
708 | a35e1c17 | Kevin Wolf | |
709 | f214978a | Kevin Wolf | /* There are no dependent requests, but we need to remove our request
|
710 | f214978a | Kevin Wolf | * from the list of in-flight requests */
|
711 | f214978a | Kevin Wolf | run_dependent_requests(&meta); |
712 | f214978a | Kevin Wolf | |
713 | a35e1c17 | Kevin Wolf | /* TODO Preallocate data if requested */
|
714 | a35e1c17 | Kevin Wolf | |
715 | a35e1c17 | Kevin Wolf | nb_sectors -= num; |
716 | a35e1c17 | Kevin Wolf | offset += num << 9;
|
717 | a35e1c17 | Kevin Wolf | } |
718 | a35e1c17 | Kevin Wolf | |
719 | a35e1c17 | Kevin Wolf | /*
|
720 | a35e1c17 | Kevin Wolf | * It is expected that the image file is large enough to actually contain
|
721 | a35e1c17 | Kevin Wolf | * all of the allocated clusters (otherwise we get failing reads after
|
722 | a35e1c17 | Kevin Wolf | * EOF). Extend the image to the last allocated sector.
|
723 | a35e1c17 | Kevin Wolf | */
|
724 | a35e1c17 | Kevin Wolf | if (cluster_offset != 0) { |
725 | ea80b906 | Kevin Wolf | uint8_t buf[512];
|
726 | ea80b906 | Kevin Wolf | memset(buf, 0, 512); |
727 | ea80b906 | Kevin Wolf | bdrv_write(s->hd, (cluster_offset >> 9) + num - 1, buf, 1); |
728 | a35e1c17 | Kevin Wolf | } |
729 | a35e1c17 | Kevin Wolf | |
730 | a35e1c17 | Kevin Wolf | return 0; |
731 | a35e1c17 | Kevin Wolf | } |
732 | a35e1c17 | Kevin Wolf | |
733 | f965509c | aliguori | static int qcow_create2(const char *filename, int64_t total_size, |
734 | f965509c | aliguori | const char *backing_file, const char *backing_format, |
735 | a35e1c17 | Kevin Wolf | int flags, size_t cluster_size, int prealloc) |
736 | 585f8587 | bellard | { |
737 | f965509c | aliguori | |
738 | 585f8587 | bellard | int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
|
739 | 2d2431f0 | aliguori | int ref_clusters, backing_format_len = 0; |
740 | 585f8587 | bellard | QCowHeader header; |
741 | 585f8587 | bellard | uint64_t tmp, offset; |
742 | 585f8587 | bellard | QCowCreateState s1, *s = &s1; |
743 | f965509c | aliguori | QCowExtension ext_bf = {0, 0}; |
744 | f965509c | aliguori | |
745 | 3b46e624 | ths | |
746 | 585f8587 | bellard | memset(s, 0, sizeof(*s)); |
747 | 585f8587 | bellard | |
748 | 585f8587 | bellard | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
|
749 | 585f8587 | bellard | if (fd < 0) |
750 | 585f8587 | bellard | return -1; |
751 | 585f8587 | bellard | memset(&header, 0, sizeof(header)); |
752 | 585f8587 | bellard | header.magic = cpu_to_be32(QCOW_MAGIC); |
753 | 585f8587 | bellard | header.version = cpu_to_be32(QCOW_VERSION); |
754 | 585f8587 | bellard | header.size = cpu_to_be64(total_size * 512);
|
755 | 585f8587 | bellard | header_size = sizeof(header);
|
756 | 585f8587 | bellard | backing_filename_len = 0;
|
757 | 585f8587 | bellard | if (backing_file) {
|
758 | f965509c | aliguori | if (backing_format) {
|
759 | f965509c | aliguori | ext_bf.magic = QCOW_EXT_MAGIC_BACKING_FORMAT; |
760 | f965509c | aliguori | backing_format_len = strlen(backing_format); |
761 | f965509c | aliguori | ext_bf.len = (backing_format_len + 7) & ~7; |
762 | f965509c | aliguori | header_size += ((sizeof(ext_bf) + ext_bf.len + 7) & ~7); |
763 | f965509c | aliguori | } |
764 | 585f8587 | bellard | header.backing_file_offset = cpu_to_be64(header_size); |
765 | 585f8587 | bellard | backing_filename_len = strlen(backing_file); |
766 | 585f8587 | bellard | header.backing_file_size = cpu_to_be32(backing_filename_len); |
767 | 585f8587 | bellard | header_size += backing_filename_len; |
768 | 585f8587 | bellard | } |
769 | 73c632ed | Kevin Wolf | |
770 | 73c632ed | Kevin Wolf | /* Cluster size */
|
771 | 73c632ed | Kevin Wolf | s->cluster_bits = get_bits_from_size(cluster_size); |
772 | 73c632ed | Kevin Wolf | if (s->cluster_bits < MIN_CLUSTER_BITS ||
|
773 | 73c632ed | Kevin Wolf | s->cluster_bits > MAX_CLUSTER_BITS) |
774 | 73c632ed | Kevin Wolf | { |
775 | 73c632ed | Kevin Wolf | fprintf(stderr, "Cluster size must be a power of two between "
|
776 | 73c632ed | Kevin Wolf | "%d and %dk\n",
|
777 | 73c632ed | Kevin Wolf | 1 << MIN_CLUSTER_BITS,
|
778 | 73c632ed | Kevin Wolf | 1 << (MAX_CLUSTER_BITS - 10)); |
779 | 73c632ed | Kevin Wolf | return -EINVAL;
|
780 | 73c632ed | Kevin Wolf | } |
781 | 585f8587 | bellard | s->cluster_size = 1 << s->cluster_bits;
|
782 | 73c632ed | Kevin Wolf | |
783 | 585f8587 | bellard | header.cluster_bits = cpu_to_be32(s->cluster_bits); |
784 | 585f8587 | bellard | header_size = (header_size + 7) & ~7; |
785 | ec36ba14 | ths | if (flags & BLOCK_FLAG_ENCRYPT) {
|
786 | 585f8587 | bellard | header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); |
787 | 585f8587 | bellard | } else {
|
788 | 585f8587 | bellard | header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); |
789 | 585f8587 | bellard | } |
790 | 585f8587 | bellard | l2_bits = s->cluster_bits - 3;
|
791 | 585f8587 | bellard | shift = s->cluster_bits + l2_bits; |
792 | 585f8587 | bellard | l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift); |
793 | 585f8587 | bellard | offset = align_offset(header_size, s->cluster_size); |
794 | 585f8587 | bellard | s->l1_table_offset = offset; |
795 | 585f8587 | bellard | header.l1_table_offset = cpu_to_be64(s->l1_table_offset); |
796 | 585f8587 | bellard | header.l1_size = cpu_to_be32(l1_size); |
797 | 15e6690a | bellard | offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
|
798 | 585f8587 | bellard | |
799 | 585f8587 | bellard | s->refcount_table = qemu_mallocz(s->cluster_size); |
800 | 3b46e624 | ths | |
801 | 585f8587 | bellard | s->refcount_table_offset = offset; |
802 | 585f8587 | bellard | header.refcount_table_offset = cpu_to_be64(offset); |
803 | 585f8587 | bellard | header.refcount_table_clusters = cpu_to_be32(1);
|
804 | 585f8587 | bellard | offset += s->cluster_size; |
805 | 585f8587 | bellard | s->refcount_block_offset = offset; |
806 | 2d2431f0 | aliguori | |
807 | 2d2431f0 | aliguori | /* count how many refcount blocks needed */
|
808 | 2d2431f0 | aliguori | tmp = offset >> s->cluster_bits; |
809 | 2d2431f0 | aliguori | ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1;
|
810 | 2d2431f0 | aliguori | for (i=0; i < ref_clusters; i++) { |
811 | 2d2431f0 | aliguori | s->refcount_table[i] = cpu_to_be64(offset); |
812 | 2d2431f0 | aliguori | offset += s->cluster_size; |
813 | 2d2431f0 | aliguori | } |
814 | 2d2431f0 | aliguori | |
815 | 2d2431f0 | aliguori | s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size); |
816 | 585f8587 | bellard | |
817 | 585f8587 | bellard | /* update refcounts */
|
818 | ed6ccf0f | Kevin Wolf | qcow2_create_refcount_update(s, 0, header_size);
|
819 | ed6ccf0f | Kevin Wolf | qcow2_create_refcount_update(s, s->l1_table_offset, |
820 | ed6ccf0f | Kevin Wolf | l1_size * sizeof(uint64_t));
|
821 | ed6ccf0f | Kevin Wolf | qcow2_create_refcount_update(s, s->refcount_table_offset, s->cluster_size); |
822 | ed6ccf0f | Kevin Wolf | qcow2_create_refcount_update(s, s->refcount_block_offset, |
823 | ed6ccf0f | Kevin Wolf | ref_clusters * s->cluster_size); |
824 | 3b46e624 | ths | |
825 | 585f8587 | bellard | /* write all the data */
|
826 | 585f8587 | bellard | write(fd, &header, sizeof(header));
|
827 | 585f8587 | bellard | if (backing_file) {
|
828 | f965509c | aliguori | if (backing_format_len) {
|
829 | f965509c | aliguori | char zero[16]; |
830 | f965509c | aliguori | int d = ext_bf.len - backing_format_len;
|
831 | f965509c | aliguori | |
832 | f965509c | aliguori | memset(zero, 0, sizeof(zero)); |
833 | f965509c | aliguori | cpu_to_be32s(&ext_bf.magic); |
834 | f965509c | aliguori | cpu_to_be32s(&ext_bf.len); |
835 | f965509c | aliguori | write(fd, &ext_bf, sizeof(ext_bf));
|
836 | f965509c | aliguori | write(fd, backing_format, backing_format_len); |
837 | f965509c | aliguori | if (d>0) { |
838 | f965509c | aliguori | write(fd, zero, d); |
839 | f965509c | aliguori | } |
840 | f965509c | aliguori | } |
841 | 585f8587 | bellard | write(fd, backing_file, backing_filename_len); |
842 | 585f8587 | bellard | } |
843 | 585f8587 | bellard | lseek(fd, s->l1_table_offset, SEEK_SET); |
844 | 585f8587 | bellard | tmp = 0;
|
845 | 585f8587 | bellard | for(i = 0;i < l1_size; i++) { |
846 | 585f8587 | bellard | write(fd, &tmp, sizeof(tmp));
|
847 | 585f8587 | bellard | } |
848 | 585f8587 | bellard | lseek(fd, s->refcount_table_offset, SEEK_SET); |
849 | 585f8587 | bellard | write(fd, s->refcount_table, s->cluster_size); |
850 | 3b46e624 | ths | |
851 | 585f8587 | bellard | lseek(fd, s->refcount_block_offset, SEEK_SET); |
852 | 2d2431f0 | aliguori | write(fd, s->refcount_block, ref_clusters * s->cluster_size); |
853 | 585f8587 | bellard | |
854 | 585f8587 | bellard | qemu_free(s->refcount_table); |
855 | 585f8587 | bellard | qemu_free(s->refcount_block); |
856 | 585f8587 | bellard | close(fd); |
857 | a35e1c17 | Kevin Wolf | |
858 | a35e1c17 | Kevin Wolf | /* Preallocate metadata */
|
859 | a35e1c17 | Kevin Wolf | if (prealloc) {
|
860 | a35e1c17 | Kevin Wolf | BlockDriverState *bs; |
861 | a35e1c17 | Kevin Wolf | bs = bdrv_new("");
|
862 | a35e1c17 | Kevin Wolf | bdrv_open(bs, filename, BDRV_O_CACHE_WB); |
863 | a35e1c17 | Kevin Wolf | preallocate(bs); |
864 | a35e1c17 | Kevin Wolf | bdrv_close(bs); |
865 | a35e1c17 | Kevin Wolf | } |
866 | a35e1c17 | Kevin Wolf | |
867 | 585f8587 | bellard | return 0; |
868 | 585f8587 | bellard | } |
869 | 585f8587 | bellard | |
870 | 0e7e1989 | Kevin Wolf | static int qcow_create(const char *filename, QEMUOptionParameter *options) |
871 | 0e7e1989 | Kevin Wolf | { |
872 | 0e7e1989 | Kevin Wolf | const char *backing_file = NULL; |
873 | 0e7e1989 | Kevin Wolf | const char *backing_fmt = NULL; |
874 | 0e7e1989 | Kevin Wolf | uint64_t sectors = 0;
|
875 | 0e7e1989 | Kevin Wolf | int flags = 0; |
876 | 9ccb258e | Kevin Wolf | size_t cluster_size = 65536;
|
877 | a35e1c17 | Kevin Wolf | int prealloc = 0; |
878 | 0e7e1989 | Kevin Wolf | |
879 | 0e7e1989 | Kevin Wolf | /* Read out options */
|
880 | 0e7e1989 | Kevin Wolf | while (options && options->name) {
|
881 | 0e7e1989 | Kevin Wolf | if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
|
882 | 0e7e1989 | Kevin Wolf | sectors = options->value.n / 512;
|
883 | 0e7e1989 | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { |
884 | 0e7e1989 | Kevin Wolf | backing_file = options->value.s; |
885 | 0e7e1989 | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { |
886 | 0e7e1989 | Kevin Wolf | backing_fmt = options->value.s; |
887 | 0e7e1989 | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) { |
888 | 0e7e1989 | Kevin Wolf | flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
|
889 | 73c632ed | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { |
890 | 73c632ed | Kevin Wolf | if (options->value.n) {
|
891 | 73c632ed | Kevin Wolf | cluster_size = options->value.n; |
892 | 73c632ed | Kevin Wolf | } |
893 | a35e1c17 | Kevin Wolf | } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { |
894 | a35e1c17 | Kevin Wolf | if (!options->value.s || !strcmp(options->value.s, "off")) { |
895 | a35e1c17 | Kevin Wolf | prealloc = 0;
|
896 | a35e1c17 | Kevin Wolf | } else if (!strcmp(options->value.s, "metadata")) { |
897 | a35e1c17 | Kevin Wolf | prealloc = 1;
|
898 | a35e1c17 | Kevin Wolf | } else {
|
899 | a35e1c17 | Kevin Wolf | fprintf(stderr, "Invalid preallocation mode: '%s'\n",
|
900 | a35e1c17 | Kevin Wolf | options->value.s); |
901 | a35e1c17 | Kevin Wolf | return -EINVAL;
|
902 | a35e1c17 | Kevin Wolf | } |
903 | 0e7e1989 | Kevin Wolf | } |
904 | 0e7e1989 | Kevin Wolf | options++; |
905 | 0e7e1989 | Kevin Wolf | } |
906 | 0e7e1989 | Kevin Wolf | |
907 | a35e1c17 | Kevin Wolf | if (backing_file && prealloc) {
|
908 | a35e1c17 | Kevin Wolf | fprintf(stderr, "Backing file and preallocation cannot be used at "
|
909 | a35e1c17 | Kevin Wolf | "the same time\n");
|
910 | a35e1c17 | Kevin Wolf | return -EINVAL;
|
911 | a35e1c17 | Kevin Wolf | } |
912 | a35e1c17 | Kevin Wolf | |
913 | 73c632ed | Kevin Wolf | return qcow_create2(filename, sectors, backing_file, backing_fmt, flags,
|
914 | a35e1c17 | Kevin Wolf | cluster_size, prealloc); |
915 | f965509c | aliguori | } |
916 | f965509c | aliguori | |
917 | 585f8587 | bellard | static int qcow_make_empty(BlockDriverState *bs) |
918 | 585f8587 | bellard | { |
919 | 585f8587 | bellard | #if 0
|
920 | 585f8587 | bellard | /* XXX: not correct */
|
921 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque;
|
922 | 585f8587 | bellard | uint32_t l1_length = s->l1_size * sizeof(uint64_t);
|
923 | 585f8587 | bellard | int ret;
|
924 | 585f8587 | bellard | |
925 | 585f8587 | bellard | memset(s->l1_table, 0, l1_length);
|
926 | 585f8587 | bellard | if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
|
927 | ac674887 | aliguori | return -1;
|
928 | 585f8587 | bellard | ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
|
929 | 585f8587 | bellard | if (ret < 0)
|
930 | 585f8587 | bellard | return ret;
|
931 | 3b46e624 | ths | |
932 | 585f8587 | bellard | l2_cache_reset(bs);
|
933 | 585f8587 | bellard | #endif
|
934 | 585f8587 | bellard | return 0; |
935 | 585f8587 | bellard | } |
936 | 585f8587 | bellard | |
937 | 585f8587 | bellard | /* XXX: put compressed sectors first, then all the cluster aligned
|
938 | 585f8587 | bellard | tables to avoid losing bytes in alignment */
|
939 | 5fafdf24 | ths | static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, |
940 | 585f8587 | bellard | const uint8_t *buf, int nb_sectors) |
941 | 585f8587 | bellard | { |
942 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
943 | 585f8587 | bellard | z_stream strm; |
944 | 585f8587 | bellard | int ret, out_len;
|
945 | 585f8587 | bellard | uint8_t *out_buf; |
946 | 585f8587 | bellard | uint64_t cluster_offset; |
947 | 585f8587 | bellard | |
948 | 585f8587 | bellard | if (nb_sectors == 0) { |
949 | 585f8587 | bellard | /* align end of file to a sector boundary to ease reading with
|
950 | 585f8587 | bellard | sector based I/Os */
|
951 | 585f8587 | bellard | cluster_offset = bdrv_getlength(s->hd); |
952 | 585f8587 | bellard | cluster_offset = (cluster_offset + 511) & ~511; |
953 | 585f8587 | bellard | bdrv_truncate(s->hd, cluster_offset); |
954 | 585f8587 | bellard | return 0; |
955 | 585f8587 | bellard | } |
956 | 585f8587 | bellard | |
957 | 585f8587 | bellard | if (nb_sectors != s->cluster_sectors)
|
958 | 585f8587 | bellard | return -EINVAL;
|
959 | 585f8587 | bellard | |
960 | 585f8587 | bellard | out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); |
961 | 585f8587 | bellard | |
962 | 585f8587 | bellard | /* best compression, small window, no zlib header */
|
963 | 585f8587 | bellard | memset(&strm, 0, sizeof(strm)); |
964 | 585f8587 | bellard | ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, |
965 | 5fafdf24 | ths | Z_DEFLATED, -12,
|
966 | 585f8587 | bellard | 9, Z_DEFAULT_STRATEGY);
|
967 | 585f8587 | bellard | if (ret != 0) { |
968 | 585f8587 | bellard | qemu_free(out_buf); |
969 | 585f8587 | bellard | return -1; |
970 | 585f8587 | bellard | } |
971 | 585f8587 | bellard | |
972 | 585f8587 | bellard | strm.avail_in = s->cluster_size; |
973 | 585f8587 | bellard | strm.next_in = (uint8_t *)buf; |
974 | 585f8587 | bellard | strm.avail_out = s->cluster_size; |
975 | 585f8587 | bellard | strm.next_out = out_buf; |
976 | 585f8587 | bellard | |
977 | 585f8587 | bellard | ret = deflate(&strm, Z_FINISH); |
978 | 585f8587 | bellard | if (ret != Z_STREAM_END && ret != Z_OK) {
|
979 | 585f8587 | bellard | qemu_free(out_buf); |
980 | 585f8587 | bellard | deflateEnd(&strm); |
981 | 585f8587 | bellard | return -1; |
982 | 585f8587 | bellard | } |
983 | 585f8587 | bellard | out_len = strm.next_out - out_buf; |
984 | 585f8587 | bellard | |
985 | 585f8587 | bellard | deflateEnd(&strm); |
986 | 585f8587 | bellard | |
987 | 585f8587 | bellard | if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
|
988 | 585f8587 | bellard | /* could not compress: write normal cluster */
|
989 | ade40677 | Kevin Wolf | bdrv_write(bs, sector_num, buf, s->cluster_sectors); |
990 | 585f8587 | bellard | } else {
|
991 | ed6ccf0f | Kevin Wolf | cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, |
992 | ed6ccf0f | Kevin Wolf | sector_num << 9, out_len);
|
993 | 52d893ec | aliguori | if (!cluster_offset)
|
994 | 52d893ec | aliguori | return -1; |
995 | 585f8587 | bellard | cluster_offset &= s->cluster_offset_mask; |
996 | 585f8587 | bellard | if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
|
997 | 585f8587 | bellard | qemu_free(out_buf); |
998 | 585f8587 | bellard | return -1; |
999 | 585f8587 | bellard | } |
1000 | 585f8587 | bellard | } |
1001 | 3b46e624 | ths | |
1002 | 585f8587 | bellard | qemu_free(out_buf); |
1003 | 585f8587 | bellard | return 0; |
1004 | 585f8587 | bellard | } |
1005 | 585f8587 | bellard | |
1006 | 585f8587 | bellard | static void qcow_flush(BlockDriverState *bs) |
1007 | 585f8587 | bellard | { |
1008 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
1009 | 585f8587 | bellard | bdrv_flush(s->hd); |
1010 | 585f8587 | bellard | } |
1011 | 585f8587 | bellard | |
1012 | 45566e9c | Christoph Hellwig | static int64_t qcow_vm_state_offset(BDRVQcowState *s)
|
1013 | 45566e9c | Christoph Hellwig | { |
1014 | 45566e9c | Christoph Hellwig | return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
|
1015 | 45566e9c | Christoph Hellwig | } |
1016 | 45566e9c | Christoph Hellwig | |
1017 | 585f8587 | bellard | static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
1018 | 585f8587 | bellard | { |
1019 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque; |
1020 | 585f8587 | bellard | bdi->cluster_size = s->cluster_size; |
1021 | 45566e9c | Christoph Hellwig | bdi->vm_state_offset = qcow_vm_state_offset(s); |
1022 | 585f8587 | bellard | return 0; |
1023 | 585f8587 | bellard | } |
1024 | 585f8587 | bellard | |
1025 | 585f8587 | bellard | |
1026 | e97fc193 | aliguori | static int qcow_check(BlockDriverState *bs) |
1027 | e97fc193 | aliguori | { |
1028 | ed6ccf0f | Kevin Wolf | return qcow2_check_refcounts(bs);
|
1029 | 585f8587 | bellard | } |
1030 | 585f8587 | bellard | |
1031 | 585f8587 | bellard | #if 0
|
1032 | 585f8587 | bellard | static void dump_refcounts(BlockDriverState *bs)
|
1033 | 585f8587 | bellard | {
|
1034 | 585f8587 | bellard | BDRVQcowState *s = bs->opaque;
|
1035 | 585f8587 | bellard | int64_t nb_clusters, k, k1, size;
|
1036 | 585f8587 | bellard | int refcount;
|
1037 | 585f8587 | bellard | |
1038 | 585f8587 | bellard | size = bdrv_getlength(s->hd);
|
1039 | 6db6c638 | aliguori | nb_clusters = size_to_clusters(s, size);
|
1040 | 585f8587 | bellard | for(k = 0; k < nb_clusters;) {
|
1041 | 585f8587 | bellard | k1 = k;
|
1042 | 585f8587 | bellard | refcount = get_refcount(bs, k);
|
1043 | 585f8587 | bellard | k++;
|
1044 | 585f8587 | bellard | while (k < nb_clusters && get_refcount(bs, k) == refcount)
|
1045 | 585f8587 | bellard | k++;
|
1046 | 585f8587 | bellard | printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
|
1047 | 585f8587 | bellard | }
|
1048 | 585f8587 | bellard | }
|
1049 | 585f8587 | bellard | #endif
|
1050 | 585f8587 | bellard | |
1051 | 45566e9c | Christoph Hellwig | static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
1052 | 178e08a5 | aliguori | int64_t pos, int size)
|
1053 | 178e08a5 | aliguori | { |
1054 | 45566e9c | Christoph Hellwig | BDRVQcowState *s = bs->opaque; |
1055 | 178e08a5 | aliguori | int growable = bs->growable;
|
1056 | 178e08a5 | aliguori | |
1057 | 178e08a5 | aliguori | bs->growable = 1;
|
1058 | 45566e9c | Christoph Hellwig | bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size); |
1059 | 178e08a5 | aliguori | bs->growable = growable; |
1060 | 178e08a5 | aliguori | |
1061 | 178e08a5 | aliguori | return size;
|
1062 | 178e08a5 | aliguori | } |
1063 | 178e08a5 | aliguori | |
1064 | 45566e9c | Christoph Hellwig | static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf, |
1065 | 178e08a5 | aliguori | int64_t pos, int size)
|
1066 | 178e08a5 | aliguori | { |
1067 | 45566e9c | Christoph Hellwig | BDRVQcowState *s = bs->opaque; |
1068 | 178e08a5 | aliguori | int growable = bs->growable;
|
1069 | 178e08a5 | aliguori | int ret;
|
1070 | 178e08a5 | aliguori | |
1071 | 178e08a5 | aliguori | bs->growable = 1;
|
1072 | 45566e9c | Christoph Hellwig | ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size); |
1073 | 178e08a5 | aliguori | bs->growable = growable; |
1074 | 178e08a5 | aliguori | |
1075 | 178e08a5 | aliguori | return ret;
|
1076 | 178e08a5 | aliguori | } |
1077 | 178e08a5 | aliguori | |
1078 | 0e7e1989 | Kevin Wolf | static QEMUOptionParameter qcow_create_options[] = {
|
1079 | db08adf5 | Kevin Wolf | { |
1080 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_SIZE, |
1081 | db08adf5 | Kevin Wolf | .type = OPT_SIZE, |
1082 | db08adf5 | Kevin Wolf | .help = "Virtual disk size"
|
1083 | db08adf5 | Kevin Wolf | }, |
1084 | db08adf5 | Kevin Wolf | { |
1085 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_BACKING_FILE, |
1086 | db08adf5 | Kevin Wolf | .type = OPT_STRING, |
1087 | db08adf5 | Kevin Wolf | .help = "File name of a base image"
|
1088 | db08adf5 | Kevin Wolf | }, |
1089 | db08adf5 | Kevin Wolf | { |
1090 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_BACKING_FMT, |
1091 | db08adf5 | Kevin Wolf | .type = OPT_STRING, |
1092 | db08adf5 | Kevin Wolf | .help = "Image format of the base image"
|
1093 | db08adf5 | Kevin Wolf | }, |
1094 | db08adf5 | Kevin Wolf | { |
1095 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_ENCRYPT, |
1096 | db08adf5 | Kevin Wolf | .type = OPT_FLAG, |
1097 | db08adf5 | Kevin Wolf | .help = "Encrypt the image"
|
1098 | db08adf5 | Kevin Wolf | }, |
1099 | db08adf5 | Kevin Wolf | { |
1100 | db08adf5 | Kevin Wolf | .name = BLOCK_OPT_CLUSTER_SIZE, |
1101 | db08adf5 | Kevin Wolf | .type = OPT_SIZE, |
1102 | db08adf5 | Kevin Wolf | .help = "qcow2 cluster size"
|
1103 | db08adf5 | Kevin Wolf | }, |
1104 | a35e1c17 | Kevin Wolf | { |
1105 | a35e1c17 | Kevin Wolf | .name = BLOCK_OPT_PREALLOC, |
1106 | a35e1c17 | Kevin Wolf | .type = OPT_STRING, |
1107 | a35e1c17 | Kevin Wolf | .help = "Preallocation mode (allowed values: off, metadata)"
|
1108 | a35e1c17 | Kevin Wolf | }, |
1109 | 0e7e1989 | Kevin Wolf | { NULL }
|
1110 | 0e7e1989 | Kevin Wolf | }; |
1111 | 0e7e1989 | Kevin Wolf | |
1112 | 5efa9d5a | Anthony Liguori | static BlockDriver bdrv_qcow2 = {
|
1113 | e60f469c | aurel32 | .format_name = "qcow2",
|
1114 | e60f469c | aurel32 | .instance_size = sizeof(BDRVQcowState),
|
1115 | e60f469c | aurel32 | .bdrv_probe = qcow_probe, |
1116 | e60f469c | aurel32 | .bdrv_open = qcow_open, |
1117 | e60f469c | aurel32 | .bdrv_close = qcow_close, |
1118 | e60f469c | aurel32 | .bdrv_create = qcow_create, |
1119 | e60f469c | aurel32 | .bdrv_flush = qcow_flush, |
1120 | e60f469c | aurel32 | .bdrv_is_allocated = qcow_is_allocated, |
1121 | e60f469c | aurel32 | .bdrv_set_key = qcow_set_key, |
1122 | e60f469c | aurel32 | .bdrv_make_empty = qcow_make_empty, |
1123 | e60f469c | aurel32 | |
1124 | f141eafe | aliguori | .bdrv_aio_readv = qcow_aio_readv, |
1125 | f141eafe | aliguori | .bdrv_aio_writev = qcow_aio_writev, |
1126 | 585f8587 | bellard | .bdrv_write_compressed = qcow_write_compressed, |
1127 | 585f8587 | bellard | |
1128 | ed6ccf0f | Kevin Wolf | .bdrv_snapshot_create = qcow2_snapshot_create, |
1129 | ed6ccf0f | Kevin Wolf | .bdrv_snapshot_goto = qcow2_snapshot_goto, |
1130 | ed6ccf0f | Kevin Wolf | .bdrv_snapshot_delete = qcow2_snapshot_delete, |
1131 | ed6ccf0f | Kevin Wolf | .bdrv_snapshot_list = qcow2_snapshot_list, |
1132 | e60f469c | aurel32 | .bdrv_get_info = qcow_get_info, |
1133 | f965509c | aliguori | |
1134 | 45566e9c | Christoph Hellwig | .bdrv_save_vmstate = qcow_save_vmstate, |
1135 | 45566e9c | Christoph Hellwig | .bdrv_load_vmstate = qcow_load_vmstate, |
1136 | 178e08a5 | aliguori | |
1137 | 0e7e1989 | Kevin Wolf | .create_options = qcow_create_options, |
1138 | e97fc193 | aliguori | .bdrv_check = qcow_check, |
1139 | 585f8587 | bellard | }; |
1140 | 5efa9d5a | Anthony Liguori | |
1141 | 5efa9d5a | Anthony Liguori | static void bdrv_qcow2_init(void) |
1142 | 5efa9d5a | Anthony Liguori | { |
1143 | 5efa9d5a | Anthony Liguori | bdrv_register(&bdrv_qcow2); |
1144 | 5efa9d5a | Anthony Liguori | } |
1145 | 5efa9d5a | Anthony Liguori | |
1146 | 5efa9d5a | Anthony Liguori | block_init(bdrv_qcow2_init); |