root / block-qcow.c @ c5d6edc3
History | View | Annotate | Download (22.6 kB)
1 | ea2384d3 | bellard | /*
|
---|---|---|---|
2 | ea2384d3 | bellard | * Block driver for the QCOW format
|
3 | ea2384d3 | bellard | *
|
4 | ea2384d3 | bellard | * Copyright (c) 2004 Fabrice Bellard
|
5 | ea2384d3 | bellard | *
|
6 | ea2384d3 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | ea2384d3 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | ea2384d3 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | ea2384d3 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | ea2384d3 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | ea2384d3 | bellard | * furnished to do so, subject to the following conditions:
|
12 | ea2384d3 | bellard | *
|
13 | ea2384d3 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | ea2384d3 | bellard | * all copies or substantial portions of the Software.
|
15 | ea2384d3 | bellard | *
|
16 | ea2384d3 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | ea2384d3 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | ea2384d3 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | ea2384d3 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | ea2384d3 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | ea2384d3 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | ea2384d3 | bellard | * THE SOFTWARE.
|
23 | ea2384d3 | bellard | */
|
24 | ea2384d3 | bellard | #include "vl.h" |
25 | ea2384d3 | bellard | #include "block_int.h" |
26 | 28d34b82 | bellard | #include <zlib.h> |
27 | ea2384d3 | bellard | #include "aes.h" |
28 | ea2384d3 | bellard | |
29 | ea2384d3 | bellard | /**************************************************************/
|
30 | ea2384d3 | bellard | /* QEMU COW block driver with compression and encryption support */
|
31 | ea2384d3 | bellard | |
32 | ea2384d3 | bellard | #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb) |
33 | ea2384d3 | bellard | #define QCOW_VERSION 1 |
34 | ea2384d3 | bellard | |
35 | ea2384d3 | bellard | #define QCOW_CRYPT_NONE 0 |
36 | ea2384d3 | bellard | #define QCOW_CRYPT_AES 1 |
37 | ea2384d3 | bellard | |
38 | ea2384d3 | bellard | #define QCOW_OFLAG_COMPRESSED (1LL << 63) |
39 | ea2384d3 | bellard | |
40 | ea2384d3 | bellard | typedef struct QCowHeader { |
41 | ea2384d3 | bellard | uint32_t magic; |
42 | ea2384d3 | bellard | uint32_t version; |
43 | ea2384d3 | bellard | uint64_t backing_file_offset; |
44 | ea2384d3 | bellard | uint32_t backing_file_size; |
45 | ea2384d3 | bellard | uint32_t mtime; |
46 | ea2384d3 | bellard | uint64_t size; /* in bytes */
|
47 | ea2384d3 | bellard | uint8_t cluster_bits; |
48 | ea2384d3 | bellard | uint8_t l2_bits; |
49 | ea2384d3 | bellard | uint32_t crypt_method; |
50 | ea2384d3 | bellard | uint64_t l1_table_offset; |
51 | ea2384d3 | bellard | } QCowHeader; |
52 | ea2384d3 | bellard | |
53 | ea2384d3 | bellard | #define L2_CACHE_SIZE 16 |
54 | ea2384d3 | bellard | |
55 | ea2384d3 | bellard | typedef struct BDRVQcowState { |
56 | ea2384d3 | bellard | int fd;
|
57 | ea2384d3 | bellard | int cluster_bits;
|
58 | ea2384d3 | bellard | int cluster_size;
|
59 | ea2384d3 | bellard | int cluster_sectors;
|
60 | ea2384d3 | bellard | int l2_bits;
|
61 | ea2384d3 | bellard | int l2_size;
|
62 | ea2384d3 | bellard | int l1_size;
|
63 | ea2384d3 | bellard | uint64_t cluster_offset_mask; |
64 | ea2384d3 | bellard | uint64_t l1_table_offset; |
65 | ea2384d3 | bellard | uint64_t *l1_table; |
66 | ea2384d3 | bellard | uint64_t *l2_cache; |
67 | ea2384d3 | bellard | uint64_t l2_cache_offsets[L2_CACHE_SIZE]; |
68 | ea2384d3 | bellard | uint32_t l2_cache_counts[L2_CACHE_SIZE]; |
69 | ea2384d3 | bellard | uint8_t *cluster_cache; |
70 | ea2384d3 | bellard | uint8_t *cluster_data; |
71 | ea2384d3 | bellard | uint64_t cluster_cache_offset; |
72 | ea2384d3 | bellard | uint32_t crypt_method; /* current crypt method, 0 if no key yet */
|
73 | ea2384d3 | bellard | uint32_t crypt_method_header; |
74 | ea2384d3 | bellard | AES_KEY aes_encrypt_key; |
75 | ea2384d3 | bellard | AES_KEY aes_decrypt_key; |
76 | ea2384d3 | bellard | } BDRVQcowState; |
77 | ea2384d3 | bellard | |
78 | ea2384d3 | bellard | static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset); |
79 | ea2384d3 | bellard | |
80 | ea2384d3 | bellard | static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) |
81 | ea2384d3 | bellard | { |
82 | ea2384d3 | bellard | const QCowHeader *cow_header = (const void *)buf; |
83 | 712e7874 | bellard | |
84 | 712e7874 | bellard | if (buf_size >= sizeof(QCowHeader) && |
85 | 712e7874 | bellard | be32_to_cpu(cow_header->magic) == QCOW_MAGIC && |
86 | ea2384d3 | bellard | be32_to_cpu(cow_header->version) == QCOW_VERSION) |
87 | ea2384d3 | bellard | return 100; |
88 | ea2384d3 | bellard | else
|
89 | ea2384d3 | bellard | return 0; |
90 | ea2384d3 | bellard | } |
91 | ea2384d3 | bellard | |
92 | ea2384d3 | bellard | static int qcow_open(BlockDriverState *bs, const char *filename) |
93 | ea2384d3 | bellard | { |
94 | ea2384d3 | bellard | BDRVQcowState *s = bs->opaque; |
95 | ea2384d3 | bellard | int fd, len, i, shift;
|
96 | ea2384d3 | bellard | QCowHeader header; |
97 | ea2384d3 | bellard | |
98 | ea2384d3 | bellard | fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
99 | ea2384d3 | bellard | if (fd < 0) { |
100 | ea2384d3 | bellard | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
101 | ea2384d3 | bellard | if (fd < 0) |
102 | ea2384d3 | bellard | return -1; |
103 | ea2384d3 | bellard | } |
104 | ea2384d3 | bellard | s->fd = fd; |
105 | ea2384d3 | bellard | if (read(fd, &header, sizeof(header)) != sizeof(header)) |
106 | ea2384d3 | bellard | goto fail;
|
107 | ea2384d3 | bellard | be32_to_cpus(&header.magic); |
108 | ea2384d3 | bellard | be32_to_cpus(&header.version); |
109 | ea2384d3 | bellard | be64_to_cpus(&header.backing_file_offset); |
110 | ea2384d3 | bellard | be32_to_cpus(&header.backing_file_size); |
111 | ea2384d3 | bellard | be32_to_cpus(&header.mtime); |
112 | ea2384d3 | bellard | be64_to_cpus(&header.size); |
113 | ea2384d3 | bellard | be32_to_cpus(&header.crypt_method); |
114 | ea2384d3 | bellard | be64_to_cpus(&header.l1_table_offset); |
115 | ea2384d3 | bellard | |
116 | ea2384d3 | bellard | if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
|
117 | ea2384d3 | bellard | goto fail;
|
118 | ea2384d3 | bellard | if (header.size <= 1 || header.cluster_bits < 9) |
119 | ea2384d3 | bellard | goto fail;
|
120 | ea2384d3 | bellard | if (header.crypt_method > QCOW_CRYPT_AES)
|
121 | ea2384d3 | bellard | goto fail;
|
122 | ea2384d3 | bellard | s->crypt_method_header = header.crypt_method; |
123 | ea2384d3 | bellard | if (s->crypt_method_header)
|
124 | ea2384d3 | bellard | bs->encrypted = 1;
|
125 | ea2384d3 | bellard | s->cluster_bits = header.cluster_bits; |
126 | ea2384d3 | bellard | s->cluster_size = 1 << s->cluster_bits;
|
127 | ea2384d3 | bellard | s->cluster_sectors = 1 << (s->cluster_bits - 9); |
128 | ea2384d3 | bellard | s->l2_bits = header.l2_bits; |
129 | ea2384d3 | bellard | s->l2_size = 1 << s->l2_bits;
|
130 | ea2384d3 | bellard | bs->total_sectors = header.size / 512;
|
131 | ea2384d3 | bellard | s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1; |
132 | ea2384d3 | bellard | |
133 | ea2384d3 | bellard | /* read the level 1 table */
|
134 | ea2384d3 | bellard | shift = s->cluster_bits + s->l2_bits; |
135 | ea2384d3 | bellard | s->l1_size = (header.size + (1LL << shift) - 1) >> shift; |
136 | ea2384d3 | bellard | |
137 | ea2384d3 | bellard | s->l1_table_offset = header.l1_table_offset; |
138 | ea2384d3 | bellard | s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
|
139 | ea2384d3 | bellard | if (!s->l1_table)
|
140 | ea2384d3 | bellard | goto fail;
|
141 | d5249393 | bellard | lseek(fd, s->l1_table_offset, SEEK_SET); |
142 | ea2384d3 | bellard | if (read(fd, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
143 | ea2384d3 | bellard | s->l1_size * sizeof(uint64_t))
|
144 | ea2384d3 | bellard | goto fail;
|
145 | ea2384d3 | bellard | for(i = 0;i < s->l1_size; i++) { |
146 | ea2384d3 | bellard | be64_to_cpus(&s->l1_table[i]); |
147 | ea2384d3 | bellard | } |
148 | ea2384d3 | bellard | /* alloc L2 cache */
|
149 | ea2384d3 | bellard | s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
|
150 | ea2384d3 | bellard | if (!s->l2_cache)
|
151 | ea2384d3 | bellard | goto fail;
|
152 | ea2384d3 | bellard | s->cluster_cache = qemu_malloc(s->cluster_size); |
153 | ea2384d3 | bellard | if (!s->cluster_cache)
|
154 | ea2384d3 | bellard | goto fail;
|
155 | ea2384d3 | bellard | s->cluster_data = qemu_malloc(s->cluster_size); |
156 | ea2384d3 | bellard | if (!s->cluster_data)
|
157 | ea2384d3 | bellard | goto fail;
|
158 | ea2384d3 | bellard | s->cluster_cache_offset = -1;
|
159 | ea2384d3 | bellard | |
160 | ea2384d3 | bellard | /* read the backing file name */
|
161 | ea2384d3 | bellard | if (header.backing_file_offset != 0) { |
162 | ea2384d3 | bellard | len = header.backing_file_size; |
163 | ea2384d3 | bellard | if (len > 1023) |
164 | ea2384d3 | bellard | len = 1023;
|
165 | d5249393 | bellard | lseek(fd, header.backing_file_offset, SEEK_SET); |
166 | ea2384d3 | bellard | if (read(fd, bs->backing_file, len) != len)
|
167 | ea2384d3 | bellard | goto fail;
|
168 | ea2384d3 | bellard | bs->backing_file[len] = '\0';
|
169 | ea2384d3 | bellard | } |
170 | ea2384d3 | bellard | return 0; |
171 | ea2384d3 | bellard | |
172 | ea2384d3 | bellard | fail:
|
173 | ea2384d3 | bellard | qemu_free(s->l1_table); |
174 | ea2384d3 | bellard | qemu_free(s->l2_cache); |
175 | ea2384d3 | bellard | qemu_free(s->cluster_cache); |
176 | ea2384d3 | bellard | qemu_free(s->cluster_data); |
177 | ea2384d3 | bellard | close(fd); |
178 | ea2384d3 | bellard | return -1; |
179 | ea2384d3 | bellard | } |
180 | ea2384d3 | bellard | |
181 | ea2384d3 | bellard | static int qcow_set_key(BlockDriverState *bs, const char *key) |
182 | ea2384d3 | bellard | { |
183 | ea2384d3 | bellard | BDRVQcowState *s = bs->opaque; |
184 | ea2384d3 | bellard | uint8_t keybuf[16];
|
185 | ea2384d3 | bellard | int len, i;
|
186 | ea2384d3 | bellard | |
187 | ea2384d3 | bellard | memset(keybuf, 0, 16); |
188 | ea2384d3 | bellard | len = strlen(key); |
189 | ea2384d3 | bellard | if (len > 16) |
190 | ea2384d3 | bellard | len = 16;
|
191 | ea2384d3 | bellard | /* XXX: we could compress the chars to 7 bits to increase
|
192 | ea2384d3 | bellard | entropy */
|
193 | ea2384d3 | bellard | for(i = 0;i < len;i++) { |
194 | ea2384d3 | bellard | keybuf[i] = key[i]; |
195 | ea2384d3 | bellard | } |
196 | ea2384d3 | bellard | s->crypt_method = s->crypt_method_header; |
197 | ea2384d3 | bellard | |
198 | ea2384d3 | bellard | if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) |
199 | ea2384d3 | bellard | return -1; |
200 | ea2384d3 | bellard | if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) |
201 | ea2384d3 | bellard | return -1; |
202 | ea2384d3 | bellard | #if 0
|
203 | ea2384d3 | bellard | /* test */
|
204 | ea2384d3 | bellard | {
|
205 | ea2384d3 | bellard | uint8_t in[16];
|
206 | ea2384d3 | bellard | uint8_t out[16];
|
207 | ea2384d3 | bellard | uint8_t tmp[16];
|
208 | ea2384d3 | bellard | for(i=0;i<16;i++)
|
209 | ea2384d3 | bellard | in[i] = i;
|
210 | ea2384d3 | bellard | AES_encrypt(in, tmp, &s->aes_encrypt_key);
|
211 | ea2384d3 | bellard | AES_decrypt(tmp, out, &s->aes_decrypt_key);
|
212 | ea2384d3 | bellard | for(i = 0; i < 16; i++)
|
213 | ea2384d3 | bellard | printf(" %02x", tmp[i]);
|
214 | ea2384d3 | bellard | printf("\n");
|
215 | ea2384d3 | bellard | for(i = 0; i < 16; i++)
|
216 | ea2384d3 | bellard | printf(" %02x", out[i]);
|
217 | ea2384d3 | bellard | printf("\n");
|
218 | ea2384d3 | bellard | }
|
219 | ea2384d3 | bellard | #endif
|
220 | ea2384d3 | bellard | return 0; |
221 | ea2384d3 | bellard | } |
222 | ea2384d3 | bellard | |
223 | ea2384d3 | bellard | /* The crypt function is compatible with the linux cryptoloop
|
224 | ea2384d3 | bellard | algorithm for < 4 GB images. NOTE: out_buf == in_buf is
|
225 | ea2384d3 | bellard | supported */
|
226 | ea2384d3 | bellard | static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num, |
227 | ea2384d3 | bellard | uint8_t *out_buf, const uint8_t *in_buf,
|
228 | ea2384d3 | bellard | int nb_sectors, int enc, |
229 | ea2384d3 | bellard | const AES_KEY *key)
|
230 | ea2384d3 | bellard | { |
231 | ea2384d3 | bellard | union {
|
232 | ea2384d3 | bellard | uint64_t ll[2];
|
233 | ea2384d3 | bellard | uint8_t b[16];
|
234 | ea2384d3 | bellard | } ivec; |
235 | ea2384d3 | bellard | int i;
|
236 | ea2384d3 | bellard | |
237 | ea2384d3 | bellard | for(i = 0; i < nb_sectors; i++) { |
238 | ea2384d3 | bellard | ivec.ll[0] = cpu_to_le64(sector_num);
|
239 | ea2384d3 | bellard | ivec.ll[1] = 0; |
240 | ea2384d3 | bellard | AES_cbc_encrypt(in_buf, out_buf, 512, key,
|
241 | ea2384d3 | bellard | ivec.b, enc); |
242 | ea2384d3 | bellard | sector_num++; |
243 | ea2384d3 | bellard | in_buf += 512;
|
244 | ea2384d3 | bellard | out_buf += 512;
|
245 | ea2384d3 | bellard | } |
246 | ea2384d3 | bellard | } |
247 | ea2384d3 | bellard | |
248 | ea2384d3 | bellard | /* 'allocate' is:
|
249 | ea2384d3 | bellard | *
|
250 | ea2384d3 | bellard | * 0 to not allocate.
|
251 | ea2384d3 | bellard | *
|
252 | ea2384d3 | bellard | * 1 to allocate a normal cluster (for sector indexes 'n_start' to
|
253 | ea2384d3 | bellard | * 'n_end')
|
254 | ea2384d3 | bellard | *
|
255 | ea2384d3 | bellard | * 2 to allocate a compressed cluster of size
|
256 | ea2384d3 | bellard | * 'compressed_size'. 'compressed_size' must be > 0 and <
|
257 | ea2384d3 | bellard | * cluster_size
|
258 | ea2384d3 | bellard | *
|
259 | ea2384d3 | bellard | * return 0 if not allocated.
|
260 | ea2384d3 | bellard | */
|
261 | ea2384d3 | bellard | static uint64_t get_cluster_offset(BlockDriverState *bs,
|
262 | ea2384d3 | bellard | uint64_t offset, int allocate,
|
263 | ea2384d3 | bellard | int compressed_size,
|
264 | ea2384d3 | bellard | int n_start, int n_end) |
265 | ea2384d3 | bellard | { |
266 | ea2384d3 | bellard | BDRVQcowState *s = bs->opaque; |
267 | ea2384d3 | bellard | int min_index, i, j, l1_index, l2_index;
|
268 | ea2384d3 | bellard | uint64_t l2_offset, *l2_table, cluster_offset, tmp; |
269 | ea2384d3 | bellard | uint32_t min_count; |
270 | ea2384d3 | bellard | int new_l2_table;
|
271 | ea2384d3 | bellard | |
272 | ea2384d3 | bellard | l1_index = offset >> (s->l2_bits + s->cluster_bits); |
273 | ea2384d3 | bellard | l2_offset = s->l1_table[l1_index]; |
274 | ea2384d3 | bellard | new_l2_table = 0;
|
275 | ea2384d3 | bellard | if (!l2_offset) {
|
276 | ea2384d3 | bellard | if (!allocate)
|
277 | ea2384d3 | bellard | return 0; |
278 | ea2384d3 | bellard | /* allocate a new l2 entry */
|
279 | d5249393 | bellard | l2_offset = lseek(s->fd, 0, SEEK_END);
|
280 | ea2384d3 | bellard | /* round to cluster size */
|
281 | ea2384d3 | bellard | l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1); |
282 | ea2384d3 | bellard | /* update the L1 entry */
|
283 | ea2384d3 | bellard | s->l1_table[l1_index] = l2_offset; |
284 | ea2384d3 | bellard | tmp = cpu_to_be64(l2_offset); |
285 | d5249393 | bellard | lseek(s->fd, s->l1_table_offset + l1_index * sizeof(tmp), SEEK_SET);
|
286 | ea2384d3 | bellard | if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp)) |
287 | ea2384d3 | bellard | return 0; |
288 | ea2384d3 | bellard | new_l2_table = 1;
|
289 | ea2384d3 | bellard | } |
290 | ea2384d3 | bellard | for(i = 0; i < L2_CACHE_SIZE; i++) { |
291 | ea2384d3 | bellard | if (l2_offset == s->l2_cache_offsets[i]) {
|
292 | ea2384d3 | bellard | /* increment the hit count */
|
293 | ea2384d3 | bellard | if (++s->l2_cache_counts[i] == 0xffffffff) { |
294 | ea2384d3 | bellard | for(j = 0; j < L2_CACHE_SIZE; j++) { |
295 | ea2384d3 | bellard | s->l2_cache_counts[j] >>= 1;
|
296 | ea2384d3 | bellard | } |
297 | ea2384d3 | bellard | } |
298 | ea2384d3 | bellard | l2_table = s->l2_cache + (i << s->l2_bits); |
299 | ea2384d3 | bellard | goto found;
|
300 | ea2384d3 | bellard | } |
301 | ea2384d3 | bellard | } |
302 | ea2384d3 | bellard | /* not found: load a new entry in the least used one */
|
303 | ea2384d3 | bellard | min_index = 0;
|
304 | ea2384d3 | bellard | min_count = 0xffffffff;
|
305 | ea2384d3 | bellard | for(i = 0; i < L2_CACHE_SIZE; i++) { |
306 | ea2384d3 | bellard | if (s->l2_cache_counts[i] < min_count) {
|
307 | ea2384d3 | bellard | min_count = s->l2_cache_counts[i]; |
308 | ea2384d3 | bellard | min_index = i; |
309 | ea2384d3 | bellard | } |
310 | ea2384d3 | bellard | } |
311 | ea2384d3 | bellard | l2_table = s->l2_cache + (min_index << s->l2_bits); |
312 | ea2384d3 | bellard | lseek(s->fd, l2_offset, SEEK_SET); |
313 | ea2384d3 | bellard | if (new_l2_table) {
|
314 | ea2384d3 | bellard | memset(l2_table, 0, s->l2_size * sizeof(uint64_t)); |
315 | ea2384d3 | bellard | if (write(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) != |
316 | ea2384d3 | bellard | s->l2_size * sizeof(uint64_t))
|
317 | ea2384d3 | bellard | return 0; |
318 | ea2384d3 | bellard | } else {
|
319 | ea2384d3 | bellard | if (read(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) != |
320 | ea2384d3 | bellard | s->l2_size * sizeof(uint64_t))
|
321 | ea2384d3 | bellard | return 0; |
322 | ea2384d3 | bellard | } |
323 | ea2384d3 | bellard | s->l2_cache_offsets[min_index] = l2_offset; |
324 | ea2384d3 | bellard | s->l2_cache_counts[min_index] = 1;
|
325 | ea2384d3 | bellard | found:
|
326 | ea2384d3 | bellard | l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
|
327 | ea2384d3 | bellard | cluster_offset = be64_to_cpu(l2_table[l2_index]); |
328 | ea2384d3 | bellard | if (!cluster_offset ||
|
329 | ea2384d3 | bellard | ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
|
330 | ea2384d3 | bellard | if (!allocate)
|
331 | ea2384d3 | bellard | return 0; |
332 | ea2384d3 | bellard | /* allocate a new cluster */
|
333 | ea2384d3 | bellard | if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
|
334 | ea2384d3 | bellard | (n_end - n_start) < s->cluster_sectors) { |
335 | ea2384d3 | bellard | /* if the cluster is already compressed, we must
|
336 | ea2384d3 | bellard | decompress it in the case it is not completely
|
337 | ea2384d3 | bellard | overwritten */
|
338 | ea2384d3 | bellard | if (decompress_cluster(s, cluster_offset) < 0) |
339 | ea2384d3 | bellard | return 0; |
340 | d5249393 | bellard | cluster_offset = lseek(s->fd, 0, SEEK_END);
|
341 | ea2384d3 | bellard | cluster_offset = (cluster_offset + s->cluster_size - 1) &
|
342 | ea2384d3 | bellard | ~(s->cluster_size - 1);
|
343 | ea2384d3 | bellard | /* write the cluster content */
|
344 | d5249393 | bellard | lseek(s->fd, cluster_offset, SEEK_SET); |
345 | ea2384d3 | bellard | if (write(s->fd, s->cluster_cache, s->cluster_size) !=
|
346 | ea2384d3 | bellard | s->cluster_size) |
347 | ea2384d3 | bellard | return -1; |
348 | ea2384d3 | bellard | } else {
|
349 | d5249393 | bellard | cluster_offset = lseek(s->fd, 0, SEEK_END);
|
350 | ea2384d3 | bellard | if (allocate == 1) { |
351 | ea2384d3 | bellard | /* round to cluster size */
|
352 | ea2384d3 | bellard | cluster_offset = (cluster_offset + s->cluster_size - 1) &
|
353 | ea2384d3 | bellard | ~(s->cluster_size - 1);
|
354 | ea2384d3 | bellard | ftruncate(s->fd, cluster_offset + s->cluster_size); |
355 | ea2384d3 | bellard | /* if encrypted, we must initialize the cluster
|
356 | ea2384d3 | bellard | content which won't be written */
|
357 | ea2384d3 | bellard | if (s->crypt_method &&
|
358 | ea2384d3 | bellard | (n_end - n_start) < s->cluster_sectors) { |
359 | ea2384d3 | bellard | uint64_t start_sect; |
360 | ea2384d3 | bellard | start_sect = (offset & ~(s->cluster_size - 1)) >> 9; |
361 | ea2384d3 | bellard | memset(s->cluster_data + 512, 0xaa, 512); |
362 | ea2384d3 | bellard | for(i = 0; i < s->cluster_sectors; i++) { |
363 | ea2384d3 | bellard | if (i < n_start || i >= n_end) {
|
364 | ea2384d3 | bellard | encrypt_sectors(s, start_sect + i, |
365 | ea2384d3 | bellard | s->cluster_data, |
366 | ea2384d3 | bellard | s->cluster_data + 512, 1, 1, |
367 | ea2384d3 | bellard | &s->aes_encrypt_key); |
368 | d5249393 | bellard | lseek(s->fd, cluster_offset + i * 512, SEEK_SET);
|
369 | ea2384d3 | bellard | if (write(s->fd, s->cluster_data, 512) != 512) |
370 | ea2384d3 | bellard | return -1; |
371 | ea2384d3 | bellard | } |
372 | ea2384d3 | bellard | } |
373 | ea2384d3 | bellard | } |
374 | ea2384d3 | bellard | } else {
|
375 | ea2384d3 | bellard | cluster_offset |= QCOW_OFLAG_COMPRESSED | |
376 | ea2384d3 | bellard | (uint64_t)compressed_size << (63 - s->cluster_bits);
|
377 | ea2384d3 | bellard | } |
378 | ea2384d3 | bellard | } |
379 | ea2384d3 | bellard | /* update L2 table */
|
380 | ea2384d3 | bellard | tmp = cpu_to_be64(cluster_offset); |
381 | ea2384d3 | bellard | l2_table[l2_index] = tmp; |
382 | d5249393 | bellard | lseek(s->fd, l2_offset + l2_index * sizeof(tmp), SEEK_SET);
|
383 | ea2384d3 | bellard | if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp)) |
384 | ea2384d3 | bellard | return 0; |
385 | ea2384d3 | bellard | } |
386 | ea2384d3 | bellard | return cluster_offset;
|
387 | ea2384d3 | bellard | } |
388 | ea2384d3 | bellard | |
389 | ea2384d3 | bellard | static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
390 | ea2384d3 | bellard | int nb_sectors, int *pnum) |
391 | ea2384d3 | bellard | { |
392 | ea2384d3 | bellard | BDRVQcowState *s = bs->opaque; |
393 | ea2384d3 | bellard | int index_in_cluster, n;
|
394 | ea2384d3 | bellard | uint64_t cluster_offset; |
395 | ea2384d3 | bellard | |
396 | ea2384d3 | bellard | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0); |
397 | ea2384d3 | bellard | index_in_cluster = sector_num & (s->cluster_sectors - 1);
|
398 | ea2384d3 | bellard | n = s->cluster_sectors - index_in_cluster; |
399 | ea2384d3 | bellard | if (n > nb_sectors)
|
400 | ea2384d3 | bellard | n = nb_sectors; |
401 | ea2384d3 | bellard | *pnum = n; |
402 | ea2384d3 | bellard | return (cluster_offset != 0); |
403 | ea2384d3 | bellard | } |
404 | ea2384d3 | bellard | |
405 | ea2384d3 | bellard | static int decompress_buffer(uint8_t *out_buf, int out_buf_size, |
406 | ea2384d3 | bellard | const uint8_t *buf, int buf_size) |
407 | ea2384d3 | bellard | { |
408 | ea2384d3 | bellard | z_stream strm1, *strm = &strm1; |
409 | ea2384d3 | bellard | int ret, out_len;
|
410 | ea2384d3 | bellard | |
411 | ea2384d3 | bellard | memset(strm, 0, sizeof(*strm)); |
412 | ea2384d3 | bellard | |
413 | ea2384d3 | bellard | strm->next_in = (uint8_t *)buf; |
414 | ea2384d3 | bellard | strm->avail_in = buf_size; |
415 | ea2384d3 | bellard | strm->next_out = out_buf; |
416 | ea2384d3 | bellard | strm->avail_out = out_buf_size; |
417 | ea2384d3 | bellard | |
418 | ea2384d3 | bellard | ret = inflateInit2(strm, -12);
|
419 | ea2384d3 | bellard | if (ret != Z_OK)
|
420 | ea2384d3 | bellard | return -1; |
421 | ea2384d3 | bellard | ret = inflate(strm, Z_FINISH); |
422 | ea2384d3 | bellard | out_len = strm->next_out - out_buf; |
423 | ea2384d3 | bellard | if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
|
424 | ea2384d3 | bellard | out_len != out_buf_size) { |
425 | ea2384d3 | bellard | inflateEnd(strm); |
426 | ea2384d3 | bellard | return -1; |
427 | ea2384d3 | bellard | } |
428 | ea2384d3 | bellard | inflateEnd(strm); |
429 | ea2384d3 | bellard | return 0; |
430 | ea2384d3 | bellard | } |
431 | ea2384d3 | bellard | |
432 | ea2384d3 | bellard | static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset) |
433 | ea2384d3 | bellard | { |
434 | ea2384d3 | bellard | int ret, csize;
|
435 | ea2384d3 | bellard | uint64_t coffset; |
436 | ea2384d3 | bellard | |
437 | ea2384d3 | bellard | coffset = cluster_offset & s->cluster_offset_mask; |
438 | ea2384d3 | bellard | if (s->cluster_cache_offset != coffset) {
|
439 | ea2384d3 | bellard | csize = cluster_offset >> (63 - s->cluster_bits);
|
440 | ea2384d3 | bellard | csize &= (s->cluster_size - 1);
|
441 | d5249393 | bellard | lseek(s->fd, coffset, SEEK_SET); |
442 | ea2384d3 | bellard | ret = read(s->fd, s->cluster_data, csize); |
443 | ea2384d3 | bellard | if (ret != csize)
|
444 | ea2384d3 | bellard | return -1; |
445 | ea2384d3 | bellard | if (decompress_buffer(s->cluster_cache, s->cluster_size,
|
446 | ea2384d3 | bellard | s->cluster_data, csize) < 0) {
|
447 | ea2384d3 | bellard | return -1; |
448 | ea2384d3 | bellard | } |
449 | ea2384d3 | bellard | s->cluster_cache_offset = coffset; |
450 | ea2384d3 | bellard | } |
451 | ea2384d3 | bellard | return 0; |
452 | ea2384d3 | bellard | } |
453 | ea2384d3 | bellard | |
454 | ea2384d3 | bellard | static int qcow_read(BlockDriverState *bs, int64_t sector_num, |
455 | ea2384d3 | bellard | uint8_t *buf, int nb_sectors)
|
456 | ea2384d3 | bellard | { |
457 | ea2384d3 | bellard | BDRVQcowState *s = bs->opaque; |
458 | ea2384d3 | bellard | int ret, index_in_cluster, n;
|
459 | ea2384d3 | bellard | uint64_t cluster_offset; |
460 | ea2384d3 | bellard | |
461 | ea2384d3 | bellard | while (nb_sectors > 0) { |
462 | ea2384d3 | bellard | cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0); |
463 | ea2384d3 | bellard | index_in_cluster = sector_num & (s->cluster_sectors - 1);
|
464 | ea2384d3 | bellard | n = s->cluster_sectors - index_in_cluster; |
465 | ea2384d3 | bellard | if (n > nb_sectors)
|
466 | ea2384d3 | bellard | n = nb_sectors; |
467 | ea2384d3 | bellard | if (!cluster_offset) {
|
468 | ea2384d3 | bellard | memset(buf, 0, 512 * n); |
469 | ea2384d3 | bellard | } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { |
470 | ea2384d3 | bellard | if (decompress_cluster(s, cluster_offset) < 0) |
471 | ea2384d3 | bellard | return -1; |
472 | ea2384d3 | bellard | memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n); |
473 | ea2384d3 | bellard | } else {
|
474 | d5249393 | bellard | lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
|
475 | ea2384d3 | bellard | ret = read(s->fd, buf, n * 512);
|
476 | ea2384d3 | bellard | if (ret != n * 512) |
477 | ea2384d3 | bellard | return -1; |
478 | ea2384d3 | bellard | if (s->crypt_method) {
|
479 | ea2384d3 | bellard | encrypt_sectors(s, sector_num, buf, buf, n, 0,
|
480 | ea2384d3 | bellard | &s->aes_decrypt_key); |
481 | ea2384d3 | bellard | } |
482 | ea2384d3 | bellard | } |
483 | ea2384d3 | bellard | nb_sectors -= n; |
484 | ea2384d3 | bellard | sector_num += n; |
485 | ea2384d3 | bellard | buf += n * 512;
|
486 | ea2384d3 | bellard | } |
487 | ea2384d3 | bellard | return 0; |
488 | ea2384d3 | bellard | } |
489 | ea2384d3 | bellard | |
490 | ea2384d3 | bellard | static int qcow_write(BlockDriverState *bs, int64_t sector_num, |
491 | ea2384d3 | bellard | const uint8_t *buf, int nb_sectors) |
492 | ea2384d3 | bellard | { |
493 | ea2384d3 | bellard | BDRVQcowState *s = bs->opaque; |
494 | ea2384d3 | bellard | int ret, index_in_cluster, n;
|
495 | ea2384d3 | bellard | uint64_t cluster_offset; |
496 | ea2384d3 | bellard | |
497 | ea2384d3 | bellard | while (nb_sectors > 0) { |
498 | ea2384d3 | bellard | index_in_cluster = sector_num & (s->cluster_sectors - 1);
|
499 | ea2384d3 | bellard | n = s->cluster_sectors - index_in_cluster; |
500 | ea2384d3 | bellard | if (n > nb_sectors)
|
501 | ea2384d3 | bellard | n = nb_sectors; |
502 | ea2384d3 | bellard | cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0, |
503 | ea2384d3 | bellard | index_in_cluster, |
504 | ea2384d3 | bellard | index_in_cluster + n); |
505 | ea2384d3 | bellard | if (!cluster_offset)
|
506 | ea2384d3 | bellard | return -1; |
507 | d5249393 | bellard | lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
|
508 | ea2384d3 | bellard | if (s->crypt_method) {
|
509 | ea2384d3 | bellard | encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
|
510 | ea2384d3 | bellard | &s->aes_encrypt_key); |
511 | ea2384d3 | bellard | ret = write(s->fd, s->cluster_data, n * 512);
|
512 | ea2384d3 | bellard | } else {
|
513 | ea2384d3 | bellard | ret = write(s->fd, buf, n * 512);
|
514 | ea2384d3 | bellard | } |
515 | ea2384d3 | bellard | if (ret != n * 512) |
516 | ea2384d3 | bellard | return -1; |
517 | ea2384d3 | bellard | nb_sectors -= n; |
518 | ea2384d3 | bellard | sector_num += n; |
519 | ea2384d3 | bellard | buf += n * 512;
|
520 | ea2384d3 | bellard | } |
521 | ea2384d3 | bellard | s->cluster_cache_offset = -1; /* disable compressed cache */ |
522 | ea2384d3 | bellard | return 0; |
523 | ea2384d3 | bellard | } |
524 | ea2384d3 | bellard | |
525 | e2731add | bellard | static void qcow_close(BlockDriverState *bs) |
526 | ea2384d3 | bellard | { |
527 | ea2384d3 | bellard | BDRVQcowState *s = bs->opaque; |
528 | ea2384d3 | bellard | qemu_free(s->l1_table); |
529 | ea2384d3 | bellard | qemu_free(s->l2_cache); |
530 | ea2384d3 | bellard | qemu_free(s->cluster_cache); |
531 | ea2384d3 | bellard | qemu_free(s->cluster_data); |
532 | ea2384d3 | bellard | close(s->fd); |
533 | ea2384d3 | bellard | } |
534 | ea2384d3 | bellard | |
535 | ea2384d3 | bellard | static int qcow_create(const char *filename, int64_t total_size, |
536 | ea2384d3 | bellard | const char *backing_file, int flags) |
537 | ea2384d3 | bellard | { |
538 | ea2384d3 | bellard | int fd, header_size, backing_filename_len, l1_size, i, shift;
|
539 | ea2384d3 | bellard | QCowHeader header; |
540 | ea2384d3 | bellard | char backing_filename[1024]; |
541 | ea2384d3 | bellard | uint64_t tmp; |
542 | ea2384d3 | bellard | struct stat st;
|
543 | ea2384d3 | bellard | |
544 | ea2384d3 | bellard | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
545 | ea2384d3 | bellard | 0644);
|
546 | ea2384d3 | bellard | if (fd < 0) |
547 | ea2384d3 | bellard | return -1; |
548 | ea2384d3 | bellard | memset(&header, 0, sizeof(header)); |
549 | ea2384d3 | bellard | header.magic = cpu_to_be32(QCOW_MAGIC); |
550 | ea2384d3 | bellard | header.version = cpu_to_be32(QCOW_VERSION); |
551 | ea2384d3 | bellard | header.size = cpu_to_be64(total_size * 512);
|
552 | ea2384d3 | bellard | header_size = sizeof(header);
|
553 | ea2384d3 | bellard | backing_filename_len = 0;
|
554 | ea2384d3 | bellard | if (backing_file) {
|
555 | 95389c86 | bellard | if (strcmp(backing_file, "fat:")) { |
556 | 95389c86 | bellard | const char *p; |
557 | 95389c86 | bellard | /* XXX: this is a hack: we do not attempt to check for URL
|
558 | 95389c86 | bellard | like syntax */
|
559 | 95389c86 | bellard | p = strchr(backing_file, ':');
|
560 | 95389c86 | bellard | if (p && (p - backing_file) >= 2) { |
561 | 95389c86 | bellard | /* URL like but exclude "c:" like filenames */
|
562 | 95389c86 | bellard | pstrcpy(backing_filename, sizeof(backing_filename),
|
563 | 95389c86 | bellard | backing_file); |
564 | 95389c86 | bellard | } else {
|
565 | 95389c86 | bellard | realpath(backing_file, backing_filename); |
566 | 95389c86 | bellard | if (stat(backing_filename, &st) != 0) { |
567 | 95389c86 | bellard | return -1; |
568 | 95389c86 | bellard | } |
569 | 95389c86 | bellard | } |
570 | 95389c86 | bellard | header.backing_file_offset = cpu_to_be64(header_size); |
571 | 95389c86 | bellard | backing_filename_len = strlen(backing_filename); |
572 | 95389c86 | bellard | header.backing_file_size = cpu_to_be32(backing_filename_len); |
573 | 95389c86 | bellard | header_size += backing_filename_len; |
574 | 95389c86 | bellard | } else
|
575 | 95389c86 | bellard | backing_file = NULL;
|
576 | ea2384d3 | bellard | header.mtime = cpu_to_be32(st.st_mtime); |
577 | ea2384d3 | bellard | header.cluster_bits = 9; /* 512 byte cluster to avoid copying |
578 | ea2384d3 | bellard | unmodifyed sectors */
|
579 | ea2384d3 | bellard | header.l2_bits = 12; /* 32 KB L2 tables */ |
580 | ea2384d3 | bellard | } else {
|
581 | ea2384d3 | bellard | header.cluster_bits = 12; /* 4 KB clusters */ |
582 | ea2384d3 | bellard | header.l2_bits = 9; /* 4 KB L2 tables */ |
583 | ea2384d3 | bellard | } |
584 | ea2384d3 | bellard | header_size = (header_size + 7) & ~7; |
585 | ea2384d3 | bellard | shift = header.cluster_bits + header.l2_bits; |
586 | ea2384d3 | bellard | l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift; |
587 | ea2384d3 | bellard | |
588 | ea2384d3 | bellard | header.l1_table_offset = cpu_to_be64(header_size); |
589 | ea2384d3 | bellard | if (flags) {
|
590 | ea2384d3 | bellard | header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); |
591 | ea2384d3 | bellard | } else {
|
592 | ea2384d3 | bellard | header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); |
593 | ea2384d3 | bellard | } |
594 | ea2384d3 | bellard | |
595 | ea2384d3 | bellard | /* write all the data */
|
596 | ea2384d3 | bellard | write(fd, &header, sizeof(header));
|
597 | ea2384d3 | bellard | if (backing_file) {
|
598 | ea2384d3 | bellard | write(fd, backing_filename, backing_filename_len); |
599 | ea2384d3 | bellard | } |
600 | ea2384d3 | bellard | lseek(fd, header_size, SEEK_SET); |
601 | ea2384d3 | bellard | tmp = 0;
|
602 | ea2384d3 | bellard | for(i = 0;i < l1_size; i++) { |
603 | ea2384d3 | bellard | write(fd, &tmp, sizeof(tmp));
|
604 | ea2384d3 | bellard | } |
605 | ea2384d3 | bellard | close(fd); |
606 | ea2384d3 | bellard | return 0; |
607 | ea2384d3 | bellard | } |
608 | ea2384d3 | bellard | |
609 | 95389c86 | bellard | int qcow_make_empty(BlockDriverState *bs)
|
610 | 95389c86 | bellard | { |
611 | 95389c86 | bellard | BDRVQcowState *s = bs->opaque; |
612 | 95389c86 | bellard | uint32_t l1_length = s->l1_size * sizeof(uint64_t);
|
613 | 95389c86 | bellard | |
614 | 95389c86 | bellard | memset(s->l1_table, 0, l1_length);
|
615 | 95389c86 | bellard | lseek(s->fd, s->l1_table_offset, SEEK_SET); |
616 | 95389c86 | bellard | if (write(s->fd, s->l1_table, l1_length) < 0) |
617 | 95389c86 | bellard | return -1; |
618 | 95389c86 | bellard | ftruncate(s->fd, s->l1_table_offset + l1_length); |
619 | 95389c86 | bellard | |
620 | 95389c86 | bellard | memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); |
621 | 95389c86 | bellard | memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t)); |
622 | 95389c86 | bellard | memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t)); |
623 | 95389c86 | bellard | |
624 | 95389c86 | bellard | return 0; |
625 | 95389c86 | bellard | } |
626 | 95389c86 | bellard | |
627 | ea2384d3 | bellard | int qcow_get_cluster_size(BlockDriverState *bs)
|
628 | ea2384d3 | bellard | { |
629 | ea2384d3 | bellard | BDRVQcowState *s = bs->opaque; |
630 | ea2384d3 | bellard | if (bs->drv != &bdrv_qcow)
|
631 | ea2384d3 | bellard | return -1; |
632 | ea2384d3 | bellard | return s->cluster_size;
|
633 | ea2384d3 | bellard | } |
634 | ea2384d3 | bellard | |
635 | ea2384d3 | bellard | /* XXX: put compressed sectors first, then all the cluster aligned
|
636 | ea2384d3 | bellard | tables to avoid losing bytes in alignment */
|
637 | ea2384d3 | bellard | int qcow_compress_cluster(BlockDriverState *bs, int64_t sector_num,
|
638 | ea2384d3 | bellard | const uint8_t *buf)
|
639 | ea2384d3 | bellard | { |
640 | ea2384d3 | bellard | BDRVQcowState *s = bs->opaque; |
641 | ea2384d3 | bellard | z_stream strm; |
642 | ea2384d3 | bellard | int ret, out_len;
|
643 | ea2384d3 | bellard | uint8_t *out_buf; |
644 | ea2384d3 | bellard | uint64_t cluster_offset; |
645 | ea2384d3 | bellard | |
646 | ea2384d3 | bellard | if (bs->drv != &bdrv_qcow)
|
647 | ea2384d3 | bellard | return -1; |
648 | ea2384d3 | bellard | |
649 | ea2384d3 | bellard | out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); |
650 | ea2384d3 | bellard | if (!out_buf)
|
651 | ea2384d3 | bellard | return -1; |
652 | ea2384d3 | bellard | |
653 | ea2384d3 | bellard | /* best compression, small window, no zlib header */
|
654 | ea2384d3 | bellard | memset(&strm, 0, sizeof(strm)); |
655 | ea2384d3 | bellard | ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, |
656 | ea2384d3 | bellard | Z_DEFLATED, -12,
|
657 | ea2384d3 | bellard | 9, Z_DEFAULT_STRATEGY);
|
658 | ea2384d3 | bellard | if (ret != 0) { |
659 | ea2384d3 | bellard | qemu_free(out_buf); |
660 | ea2384d3 | bellard | return -1; |
661 | ea2384d3 | bellard | } |
662 | ea2384d3 | bellard | |
663 | ea2384d3 | bellard | strm.avail_in = s->cluster_size; |
664 | ea2384d3 | bellard | strm.next_in = (uint8_t *)buf; |
665 | ea2384d3 | bellard | strm.avail_out = s->cluster_size; |
666 | ea2384d3 | bellard | strm.next_out = out_buf; |
667 | ea2384d3 | bellard | |
668 | ea2384d3 | bellard | ret = deflate(&strm, Z_FINISH); |
669 | ea2384d3 | bellard | if (ret != Z_STREAM_END && ret != Z_OK) {
|
670 | ea2384d3 | bellard | qemu_free(out_buf); |
671 | ea2384d3 | bellard | deflateEnd(&strm); |
672 | ea2384d3 | bellard | return -1; |
673 | ea2384d3 | bellard | } |
674 | ea2384d3 | bellard | out_len = strm.next_out - out_buf; |
675 | ea2384d3 | bellard | |
676 | ea2384d3 | bellard | deflateEnd(&strm); |
677 | ea2384d3 | bellard | |
678 | ea2384d3 | bellard | if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
|
679 | ea2384d3 | bellard | /* could not compress: write normal cluster */
|
680 | ea2384d3 | bellard | qcow_write(bs, sector_num, buf, s->cluster_sectors); |
681 | ea2384d3 | bellard | } else {
|
682 | ea2384d3 | bellard | cluster_offset = get_cluster_offset(bs, sector_num << 9, 2, |
683 | ea2384d3 | bellard | out_len, 0, 0); |
684 | ea2384d3 | bellard | cluster_offset &= s->cluster_offset_mask; |
685 | d5249393 | bellard | lseek(s->fd, cluster_offset, SEEK_SET); |
686 | ea2384d3 | bellard | if (write(s->fd, out_buf, out_len) != out_len) {
|
687 | ea2384d3 | bellard | qemu_free(out_buf); |
688 | ea2384d3 | bellard | return -1; |
689 | ea2384d3 | bellard | } |
690 | ea2384d3 | bellard | } |
691 | ea2384d3 | bellard | |
692 | ea2384d3 | bellard | qemu_free(out_buf); |
693 | ea2384d3 | bellard | return 0; |
694 | ea2384d3 | bellard | } |
695 | ea2384d3 | bellard | |
696 | 7a6cba61 | pbrook | static void qcow_flush(BlockDriverState *bs) |
697 | 7a6cba61 | pbrook | { |
698 | 7a6cba61 | pbrook | BDRVQcowState *s = bs->opaque; |
699 | 7a6cba61 | pbrook | fsync(s->fd); |
700 | 7a6cba61 | pbrook | } |
701 | 7a6cba61 | pbrook | |
702 | ea2384d3 | bellard | BlockDriver bdrv_qcow = { |
703 | ea2384d3 | bellard | "qcow",
|
704 | ea2384d3 | bellard | sizeof(BDRVQcowState),
|
705 | ea2384d3 | bellard | qcow_probe, |
706 | ea2384d3 | bellard | qcow_open, |
707 | ea2384d3 | bellard | qcow_read, |
708 | ea2384d3 | bellard | qcow_write, |
709 | ea2384d3 | bellard | qcow_close, |
710 | ea2384d3 | bellard | qcow_create, |
711 | 7a6cba61 | pbrook | qcow_flush, |
712 | ea2384d3 | bellard | qcow_is_allocated, |
713 | ea2384d3 | bellard | qcow_set_key, |
714 | 95389c86 | bellard | qcow_make_empty |
715 | ea2384d3 | bellard | }; |
716 | ea2384d3 | bellard |