qcow.c (98c2b2f4371f9a496c0dc479d7f86b086de3b4e0) qcow.c (66f82ceed6781261c09e65fb440ca76842fd0500)
1/*
2 * Block driver for the QCOW format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 62 unchanged lines hidden (view full) ---

71 uint8_t *cluster_data;
72 uint64_t cluster_cache_offset;
73 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
74 uint32_t crypt_method_header;
75 AES_KEY aes_encrypt_key;
76 AES_KEY aes_decrypt_key;
77} BDRVQcowState;
78
1/*
2 * Block driver for the QCOW format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 62 unchanged lines hidden (view full) ---

71 uint8_t *cluster_data;
72 uint64_t cluster_cache_offset;
73 uint32_t crypt_method; /* current crypt method, 0 if no key yet */
74 uint32_t crypt_method_header;
75 AES_KEY aes_encrypt_key;
76 AES_KEY aes_decrypt_key;
77} BDRVQcowState;
78
79static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
79static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
80
81static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
82{
83 const QCowHeader *cow_header = (const void *)buf;
84
85 if (buf_size >= sizeof(QCowHeader) &&
86 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
87 be32_to_cpu(cow_header->version) == QCOW_VERSION)
88 return 100;
89 else
90 return 0;
91}
92
80
81static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
82{
83 const QCowHeader *cow_header = (const void *)buf;
84
85 if (buf_size >= sizeof(QCowHeader) &&
86 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
87 be32_to_cpu(cow_header->version) == QCOW_VERSION)
88 return 100;
89 else
90 return 0;
91}
92
93static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
93static int qcow_open(BlockDriverState *bs, int flags)
94{
95 BDRVQcowState *s = bs->opaque;
94{
95 BDRVQcowState *s = bs->opaque;
96 int len, i, shift, ret;
96 int len, i, shift;
97 QCowHeader header;
98
97 QCowHeader header;
98
99 ret = bdrv_file_open(&s->hd, filename, flags);
100 if (ret < 0)
101 return ret;
102 if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
99 if (bdrv_pread(bs->file, 0, &header, sizeof(header)) != sizeof(header))
103 goto fail;
104 be32_to_cpus(&header.magic);
105 be32_to_cpus(&header.version);
106 be64_to_cpus(&header.backing_file_offset);
107 be32_to_cpus(&header.backing_file_size);
108 be32_to_cpus(&header.mtime);
109 be64_to_cpus(&header.size);
110 be32_to_cpus(&header.crypt_method);

--- 19 unchanged lines hidden (view full) ---

130 /* read the level 1 table */
131 shift = s->cluster_bits + s->l2_bits;
132 s->l1_size = (header.size + (1LL << shift) - 1) >> shift;
133
134 s->l1_table_offset = header.l1_table_offset;
135 s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
136 if (!s->l1_table)
137 goto fail;
100 goto fail;
101 be32_to_cpus(&header.magic);
102 be32_to_cpus(&header.version);
103 be64_to_cpus(&header.backing_file_offset);
104 be32_to_cpus(&header.backing_file_size);
105 be32_to_cpus(&header.mtime);
106 be64_to_cpus(&header.size);
107 be32_to_cpus(&header.crypt_method);

--- 19 unchanged lines hidden (view full) ---

127 /* read the level 1 table */
128 shift = s->cluster_bits + s->l2_bits;
129 s->l1_size = (header.size + (1LL << shift) - 1) >> shift;
130
131 s->l1_table_offset = header.l1_table_offset;
132 s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
133 if (!s->l1_table)
134 goto fail;
138 if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
135 if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
139 s->l1_size * sizeof(uint64_t))
140 goto fail;
141 for(i = 0;i < s->l1_size; i++) {
142 be64_to_cpus(&s->l1_table[i]);
143 }
144 /* alloc L2 cache */
145 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
146 if (!s->l2_cache)

--- 6 unchanged lines hidden (view full) ---

153 goto fail;
154 s->cluster_cache_offset = -1;
155
156 /* read the backing file name */
157 if (header.backing_file_offset != 0) {
158 len = header.backing_file_size;
159 if (len > 1023)
160 len = 1023;
136 s->l1_size * sizeof(uint64_t))
137 goto fail;
138 for(i = 0;i < s->l1_size; i++) {
139 be64_to_cpus(&s->l1_table[i]);
140 }
141 /* alloc L2 cache */
142 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
143 if (!s->l2_cache)

--- 6 unchanged lines hidden (view full) ---

150 goto fail;
151 s->cluster_cache_offset = -1;
152
153 /* read the backing file name */
154 if (header.backing_file_offset != 0) {
155 len = header.backing_file_size;
156 if (len > 1023)
157 len = 1023;
161 if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
158 if (bdrv_pread(bs->file, header.backing_file_offset, bs->backing_file, len) != len)
162 goto fail;
163 bs->backing_file[len] = '\0';
164 }
165 return 0;
166
167 fail:
168 qemu_free(s->l1_table);
169 qemu_free(s->l2_cache);
170 qemu_free(s->cluster_cache);
171 qemu_free(s->cluster_data);
159 goto fail;
160 bs->backing_file[len] = '\0';
161 }
162 return 0;
163
164 fail:
165 qemu_free(s->l1_table);
166 qemu_free(s->l2_cache);
167 qemu_free(s->cluster_cache);
168 qemu_free(s->cluster_data);
172 bdrv_delete(s->hd);
173 return -1;
174}
175
176static int qcow_set_key(BlockDriverState *bs, const char *key)
177{
178 BDRVQcowState *s = bs->opaque;
179 uint8_t keybuf[16];
180 int len, i;

--- 85 unchanged lines hidden (view full) ---

266
267 l1_index = offset >> (s->l2_bits + s->cluster_bits);
268 l2_offset = s->l1_table[l1_index];
269 new_l2_table = 0;
270 if (!l2_offset) {
271 if (!allocate)
272 return 0;
273 /* allocate a new l2 entry */
169 return -1;
170}
171
172static int qcow_set_key(BlockDriverState *bs, const char *key)
173{
174 BDRVQcowState *s = bs->opaque;
175 uint8_t keybuf[16];
176 int len, i;

--- 85 unchanged lines hidden (view full) ---

262
263 l1_index = offset >> (s->l2_bits + s->cluster_bits);
264 l2_offset = s->l1_table[l1_index];
265 new_l2_table = 0;
266 if (!l2_offset) {
267 if (!allocate)
268 return 0;
269 /* allocate a new l2 entry */
274 l2_offset = bdrv_getlength(s->hd);
270 l2_offset = bdrv_getlength(bs->file);
275 /* round to cluster size */
276 l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
277 /* update the L1 entry */
278 s->l1_table[l1_index] = l2_offset;
279 tmp = cpu_to_be64(l2_offset);
271 /* round to cluster size */
272 l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
273 /* update the L1 entry */
274 s->l1_table[l1_index] = l2_offset;
275 tmp = cpu_to_be64(l2_offset);
280 if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp),
276 if (bdrv_pwrite(bs->file, s->l1_table_offset + l1_index * sizeof(tmp),
281 &tmp, sizeof(tmp)) != sizeof(tmp))
282 return 0;
283 new_l2_table = 1;
284 }
285 for(i = 0; i < L2_CACHE_SIZE; i++) {
286 if (l2_offset == s->l2_cache_offsets[i]) {
287 /* increment the hit count */
288 if (++s->l2_cache_counts[i] == 0xffffffff) {

--- 12 unchanged lines hidden (view full) ---

301 if (s->l2_cache_counts[i] < min_count) {
302 min_count = s->l2_cache_counts[i];
303 min_index = i;
304 }
305 }
306 l2_table = s->l2_cache + (min_index << s->l2_bits);
307 if (new_l2_table) {
308 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
277 &tmp, sizeof(tmp)) != sizeof(tmp))
278 return 0;
279 new_l2_table = 1;
280 }
281 for(i = 0; i < L2_CACHE_SIZE; i++) {
282 if (l2_offset == s->l2_cache_offsets[i]) {
283 /* increment the hit count */
284 if (++s->l2_cache_counts[i] == 0xffffffff) {

--- 12 unchanged lines hidden (view full) ---

297 if (s->l2_cache_counts[i] < min_count) {
298 min_count = s->l2_cache_counts[i];
299 min_index = i;
300 }
301 }
302 l2_table = s->l2_cache + (min_index << s->l2_bits);
303 if (new_l2_table) {
304 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
309 if (bdrv_pwrite(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
305 if (bdrv_pwrite(bs->file, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
310 s->l2_size * sizeof(uint64_t))
311 return 0;
312 } else {
306 s->l2_size * sizeof(uint64_t))
307 return 0;
308 } else {
313 if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
309 if (bdrv_pread(bs->file, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
314 s->l2_size * sizeof(uint64_t))
315 return 0;
316 }
317 s->l2_cache_offsets[min_index] = l2_offset;
318 s->l2_cache_counts[min_index] = 1;
319 found:
320 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
321 cluster_offset = be64_to_cpu(l2_table[l2_index]);
322 if (!cluster_offset ||
323 ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
324 if (!allocate)
325 return 0;
326 /* allocate a new cluster */
327 if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
328 (n_end - n_start) < s->cluster_sectors) {
329 /* if the cluster is already compressed, we must
330 decompress it in the case it is not completely
331 overwritten */
310 s->l2_size * sizeof(uint64_t))
311 return 0;
312 }
313 s->l2_cache_offsets[min_index] = l2_offset;
314 s->l2_cache_counts[min_index] = 1;
315 found:
316 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
317 cluster_offset = be64_to_cpu(l2_table[l2_index]);
318 if (!cluster_offset ||
319 ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
320 if (!allocate)
321 return 0;
322 /* allocate a new cluster */
323 if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
324 (n_end - n_start) < s->cluster_sectors) {
325 /* if the cluster is already compressed, we must
326 decompress it in the case it is not completely
327 overwritten */
332 if (decompress_cluster(s, cluster_offset) < 0)
328 if (decompress_cluster(bs, cluster_offset) < 0)
333 return 0;
329 return 0;
334 cluster_offset = bdrv_getlength(s->hd);
330 cluster_offset = bdrv_getlength(bs->file);
335 cluster_offset = (cluster_offset + s->cluster_size - 1) &
336 ~(s->cluster_size - 1);
337 /* write the cluster content */
331 cluster_offset = (cluster_offset + s->cluster_size - 1) &
332 ~(s->cluster_size - 1);
333 /* write the cluster content */
338 if (bdrv_pwrite(s->hd, cluster_offset, s->cluster_cache, s->cluster_size) !=
334 if (bdrv_pwrite(bs->file, cluster_offset, s->cluster_cache, s->cluster_size) !=
339 s->cluster_size)
340 return -1;
341 } else {
335 s->cluster_size)
336 return -1;
337 } else {
342 cluster_offset = bdrv_getlength(s->hd);
338 cluster_offset = bdrv_getlength(bs->file);
343 if (allocate == 1) {
344 /* round to cluster size */
345 cluster_offset = (cluster_offset + s->cluster_size - 1) &
346 ~(s->cluster_size - 1);
339 if (allocate == 1) {
340 /* round to cluster size */
341 cluster_offset = (cluster_offset + s->cluster_size - 1) &
342 ~(s->cluster_size - 1);
347 bdrv_truncate(s->hd, cluster_offset + s->cluster_size);
343 bdrv_truncate(bs->file, cluster_offset + s->cluster_size);
348 /* if encrypted, we must initialize the cluster
349 content which won't be written */
350 if (s->crypt_method &&
351 (n_end - n_start) < s->cluster_sectors) {
352 uint64_t start_sect;
353 start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
354 memset(s->cluster_data + 512, 0x00, 512);
355 for(i = 0; i < s->cluster_sectors; i++) {
356 if (i < n_start || i >= n_end) {
357 encrypt_sectors(s, start_sect + i,
358 s->cluster_data,
359 s->cluster_data + 512, 1, 1,
360 &s->aes_encrypt_key);
344 /* if encrypted, we must initialize the cluster
345 content which won't be written */
346 if (s->crypt_method &&
347 (n_end - n_start) < s->cluster_sectors) {
348 uint64_t start_sect;
349 start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
350 memset(s->cluster_data + 512, 0x00, 512);
351 for(i = 0; i < s->cluster_sectors; i++) {
352 if (i < n_start || i >= n_end) {
353 encrypt_sectors(s, start_sect + i,
354 s->cluster_data,
355 s->cluster_data + 512, 1, 1,
356 &s->aes_encrypt_key);
361 if (bdrv_pwrite(s->hd, cluster_offset + i * 512,
357 if (bdrv_pwrite(bs->file, cluster_offset + i * 512,
362 s->cluster_data, 512) != 512)
363 return -1;
364 }
365 }
366 }
367 } else if (allocate == 2) {
368 cluster_offset |= QCOW_OFLAG_COMPRESSED |
369 (uint64_t)compressed_size << (63 - s->cluster_bits);
370 }
371 }
372 /* update L2 table */
373 tmp = cpu_to_be64(cluster_offset);
374 l2_table[l2_index] = tmp;
358 s->cluster_data, 512) != 512)
359 return -1;
360 }
361 }
362 }
363 } else if (allocate == 2) {
364 cluster_offset |= QCOW_OFLAG_COMPRESSED |
365 (uint64_t)compressed_size << (63 - s->cluster_bits);
366 }
367 }
368 /* update L2 table */
369 tmp = cpu_to_be64(cluster_offset);
370 l2_table[l2_index] = tmp;
375 if (bdrv_pwrite(s->hd,
371 if (bdrv_pwrite(bs->file,
376 l2_offset + l2_index * sizeof(tmp), &tmp, sizeof(tmp)) != sizeof(tmp))
377 return 0;
378 }
379 return cluster_offset;
380}
381
382static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
383 int nb_sectors, int *pnum)

--- 33 unchanged lines hidden (view full) ---

417 out_len != out_buf_size) {
418 inflateEnd(strm);
419 return -1;
420 }
421 inflateEnd(strm);
422 return 0;
423}
424
372 l2_offset + l2_index * sizeof(tmp), &tmp, sizeof(tmp)) != sizeof(tmp))
373 return 0;
374 }
375 return cluster_offset;
376}
377
378static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num,
379 int nb_sectors, int *pnum)

--- 33 unchanged lines hidden (view full) ---

413 out_len != out_buf_size) {
414 inflateEnd(strm);
415 return -1;
416 }
417 inflateEnd(strm);
418 return 0;
419}
420
425static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
421static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
426{
422{
423 BDRVQcowState *s = bs->opaque;
427 int ret, csize;
428 uint64_t coffset;
429
430 coffset = cluster_offset & s->cluster_offset_mask;
431 if (s->cluster_cache_offset != coffset) {
432 csize = cluster_offset >> (63 - s->cluster_bits);
433 csize &= (s->cluster_size - 1);
424 int ret, csize;
425 uint64_t coffset;
426
427 coffset = cluster_offset & s->cluster_offset_mask;
428 if (s->cluster_cache_offset != coffset) {
429 csize = cluster_offset >> (63 - s->cluster_bits);
430 csize &= (s->cluster_size - 1);
434 ret = bdrv_pread(s->hd, coffset, s->cluster_data, csize);
431 ret = bdrv_pread(bs->file, coffset, s->cluster_data, csize);
435 if (ret != csize)
436 return -1;
437 if (decompress_buffer(s->cluster_cache, s->cluster_size,
438 s->cluster_data, csize) < 0) {
439 return -1;
440 }
441 s->cluster_cache_offset = coffset;
442 }

--- 20 unchanged lines hidden (view full) ---

463 /* read from the base image */
464 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
465 if (ret < 0)
466 return -1;
467 } else {
468 memset(buf, 0, 512 * n);
469 }
470 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
432 if (ret != csize)
433 return -1;
434 if (decompress_buffer(s->cluster_cache, s->cluster_size,
435 s->cluster_data, csize) < 0) {
436 return -1;
437 }
438 s->cluster_cache_offset = coffset;
439 }

--- 20 unchanged lines hidden (view full) ---

460 /* read from the base image */
461 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
462 if (ret < 0)
463 return -1;
464 } else {
465 memset(buf, 0, 512 * n);
466 }
467 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
471 if (decompress_cluster(s, cluster_offset) < 0)
468 if (decompress_cluster(bs, cluster_offset) < 0)
472 return -1;
473 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
474 } else {
469 return -1;
470 memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
471 } else {
475 ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
472 ret = bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512);
476 if (ret != n * 512)
477 return -1;
478 if (s->crypt_method) {
479 encrypt_sectors(s, sector_num, buf, buf, n, 0,
480 &s->aes_decrypt_key);
481 }
482 }
483 nb_sectors -= n;

--- 112 unchanged lines hidden (view full) ---

596 goto done;
597 } else {
598 /* Note: in this case, no need to wait */
599 memset(acb->buf, 0, 512 * acb->n);
600 goto redo;
601 }
602 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
603 /* add AIO support for compressed blocks ? */
473 if (ret != n * 512)
474 return -1;
475 if (s->crypt_method) {
476 encrypt_sectors(s, sector_num, buf, buf, n, 0,
477 &s->aes_decrypt_key);
478 }
479 }
480 nb_sectors -= n;

--- 112 unchanged lines hidden (view full) ---

593 goto done;
594 } else {
595 /* Note: in this case, no need to wait */
596 memset(acb->buf, 0, 512 * acb->n);
597 goto redo;
598 }
599 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
600 /* add AIO support for compressed blocks ? */
604 if (decompress_cluster(s, acb->cluster_offset) < 0)
601 if (decompress_cluster(bs, acb->cluster_offset) < 0)
605 goto done;
606 memcpy(acb->buf,
607 s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
608 goto redo;
609 } else {
610 if ((acb->cluster_offset & 511) != 0) {
611 ret = -EIO;
612 goto done;
613 }
614 acb->hd_iov.iov_base = (void *)acb->buf;
615 acb->hd_iov.iov_len = acb->n * 512;
616 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
602 goto done;
603 memcpy(acb->buf,
604 s->cluster_cache + index_in_cluster * 512, 512 * acb->n);
605 goto redo;
606 } else {
607 if ((acb->cluster_offset & 511) != 0) {
608 ret = -EIO;
609 goto done;
610 }
611 acb->hd_iov.iov_base = (void *)acb->buf;
612 acb->hd_iov.iov_len = acb->n * 512;
613 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
617 acb->hd_aiocb = bdrv_aio_readv(s->hd,
614 acb->hd_aiocb = bdrv_aio_readv(bs->file,
618 (acb->cluster_offset >> 9) + index_in_cluster,
619 &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb);
620 if (acb->hd_aiocb == NULL)
621 goto done;
622 }
623
624 return;
625

--- 68 unchanged lines hidden (view full) ---

694 src_buf = acb->cluster_data;
695 } else {
696 src_buf = acb->buf;
697 }
698
699 acb->hd_iov.iov_base = (void *)src_buf;
700 acb->hd_iov.iov_len = acb->n * 512;
701 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
615 (acb->cluster_offset >> 9) + index_in_cluster,
616 &acb->hd_qiov, acb->n, qcow_aio_read_cb, acb);
617 if (acb->hd_aiocb == NULL)
618 goto done;
619 }
620
621 return;
622

--- 68 unchanged lines hidden (view full) ---

691 src_buf = acb->cluster_data;
692 } else {
693 src_buf = acb->buf;
694 }
695
696 acb->hd_iov.iov_base = (void *)src_buf;
697 acb->hd_iov.iov_len = acb->n * 512;
698 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
702 acb->hd_aiocb = bdrv_aio_writev(s->hd,
699 acb->hd_aiocb = bdrv_aio_writev(bs->file,
703 (cluster_offset >> 9) + index_in_cluster,
704 &acb->hd_qiov, acb->n,
705 qcow_aio_write_cb, acb);
706 if (acb->hd_aiocb == NULL)
707 goto done;
708 return;
709
710done:

--- 23 unchanged lines hidden (view full) ---

734
735static void qcow_close(BlockDriverState *bs)
736{
737 BDRVQcowState *s = bs->opaque;
738 qemu_free(s->l1_table);
739 qemu_free(s->l2_cache);
740 qemu_free(s->cluster_cache);
741 qemu_free(s->cluster_data);
700 (cluster_offset >> 9) + index_in_cluster,
701 &acb->hd_qiov, acb->n,
702 qcow_aio_write_cb, acb);
703 if (acb->hd_aiocb == NULL)
704 goto done;
705 return;
706
707done:

--- 23 unchanged lines hidden (view full) ---

731
732static void qcow_close(BlockDriverState *bs)
733{
734 BDRVQcowState *s = bs->opaque;
735 qemu_free(s->l1_table);
736 qemu_free(s->l2_cache);
737 qemu_free(s->cluster_cache);
738 qemu_free(s->cluster_data);
742 bdrv_delete(s->hd);
743}
744
745static int qcow_create(const char *filename, QEMUOptionParameter *options)
746{
747 int fd, header_size, backing_filename_len, l1_size, i, shift;
748 QCowHeader header;
749 uint64_t tmp;
750 int64_t total_size = 0;

--- 83 unchanged lines hidden (view full) ---

834
835static int qcow_make_empty(BlockDriverState *bs)
836{
837 BDRVQcowState *s = bs->opaque;
838 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
839 int ret;
840
841 memset(s->l1_table, 0, l1_length);
739}
740
741static int qcow_create(const char *filename, QEMUOptionParameter *options)
742{
743 int fd, header_size, backing_filename_len, l1_size, i, shift;
744 QCowHeader header;
745 uint64_t tmp;
746 int64_t total_size = 0;

--- 83 unchanged lines hidden (view full) ---

830
831static int qcow_make_empty(BlockDriverState *bs)
832{
833 BDRVQcowState *s = bs->opaque;
834 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
835 int ret;
836
837 memset(s->l1_table, 0, l1_length);
842 if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
838 if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0)
843 return -1;
839 return -1;
844 ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
840 ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);
845 if (ret < 0)
846 return ret;
847
848 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
849 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
850 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
851
852 return 0;

--- 44 unchanged lines hidden (view full) ---

897
898 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
899 /* could not compress: write normal cluster */
900 bdrv_write(bs, sector_num, buf, s->cluster_sectors);
901 } else {
902 cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
903 out_len, 0, 0);
904 cluster_offset &= s->cluster_offset_mask;
841 if (ret < 0)
842 return ret;
843
844 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
845 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
846 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
847
848 return 0;

--- 44 unchanged lines hidden (view full) ---

893
894 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
895 /* could not compress: write normal cluster */
896 bdrv_write(bs, sector_num, buf, s->cluster_sectors);
897 } else {
898 cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
899 out_len, 0, 0);
900 cluster_offset &= s->cluster_offset_mask;
905 if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
901 if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) {
906 qemu_free(out_buf);
907 return -1;
908 }
909 }
910
911 qemu_free(out_buf);
912 return 0;
913}
914
915static void qcow_flush(BlockDriverState *bs)
916{
902 qemu_free(out_buf);
903 return -1;
904 }
905 }
906
907 qemu_free(out_buf);
908 return 0;
909}
910
911static void qcow_flush(BlockDriverState *bs)
912{
917 BDRVQcowState *s = bs->opaque;
918 bdrv_flush(s->hd);
913 bdrv_flush(bs->file);
919}
920
921static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
922 BlockDriverCompletionFunc *cb, void *opaque)
923{
914}
915
916static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
917 BlockDriverCompletionFunc *cb, void *opaque)
918{
924 BDRVQcowState *s = bs->opaque;
925
926 return bdrv_aio_flush(s->hd, cb, opaque);
919 return bdrv_aio_flush(bs->file, cb, opaque);
927}
928
929static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
930{
931 BDRVQcowState *s = bs->opaque;
932 bdi->cluster_size = s->cluster_size;
933 return 0;
934}

--- 47 unchanged lines hidden ---
920}
921
922static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
923{
924 BDRVQcowState *s = bs->opaque;
925 bdi->cluster_size = s->cluster_size;
926 return 0;
927}

--- 47 unchanged lines hidden ---