qcow2.c (20d97356c9df6d68fbd37d6334fdb7063f24eab6) | qcow2.c (66f82ceed6781261c09e65fb440ca76842fd0500) |
---|---|
1/* 2 * Block driver for the QCOW version 2 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 --- 63 unchanged lines hidden (view full) --- 72 * start reading from start_offset 73 * finish reading upon magic of value 0 or when end_offset reached 74 * unknown magic is skipped (future extension this version knows nothing about) 75 * return 0 upon success, non-0 otherwise 76 */ 77static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset, 78 uint64_t end_offset) 79{ | 1/* 2 * Block driver for the QCOW version 2 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 --- 63 unchanged lines hidden (view full) --- 72 * start reading from start_offset 73 * finish reading upon magic of value 0 or when end_offset reached 74 * unknown magic is skipped (future extension this version knows nothing about) 75 * return 0 upon success, non-0 otherwise 76 */ 77static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset, 78 uint64_t end_offset) 79{ |
80 BDRVQcowState *s = bs->opaque; | |
81 QCowExtension ext; 82 uint64_t offset; 83 84#ifdef DEBUG_EXT 85 printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); 86#endif 87 offset = start_offset; 88 while (offset < end_offset) { 89 90#ifdef DEBUG_EXT 91 /* Sanity check */ 92 if (offset > s->cluster_size) 93 printf("qcow_handle_extension: suspicious offset %lu\n", offset); 94 95 printf("attemting to read extended header in offset %lu\n", offset); 96#endif 97 | 80 QCowExtension ext; 81 uint64_t offset; 82 83#ifdef DEBUG_EXT 84 printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset); 85#endif 86 offset = start_offset; 87 while (offset < end_offset) { 88 89#ifdef DEBUG_EXT 90 /* Sanity check */ 91 if (offset > s->cluster_size) 92 printf("qcow_handle_extension: suspicious offset %lu\n", offset); 93 94 printf("attemting to read extended header in offset %lu\n", offset); 95#endif 96 |
98 if (bdrv_pread(s->hd, offset, &ext, sizeof(ext)) != sizeof(ext)) { | 97 if (bdrv_pread(bs->file, offset, &ext, sizeof(ext)) != sizeof(ext)) { |
99 fprintf(stderr, "qcow_handle_extension: ERROR: pread fail from offset %llu\n", 100 (unsigned long long)offset); 101 return 1; 102 } 103 be32_to_cpus(&ext.magic); 104 be32_to_cpus(&ext.len); 105 offset += sizeof(ext); 106#ifdef DEBUG_EXT --- 5 unchanged lines hidden (view full) --- 112 113 case QCOW_EXT_MAGIC_BACKING_FORMAT: 114 if (ext.len >= sizeof(bs->backing_format)) { 115 fprintf(stderr, "ERROR: ext_backing_format: len=%u too large" 116 " (>=%zu)\n", 117 ext.len, sizeof(bs->backing_format)); 118 return 2; 119 } | 98 fprintf(stderr, "qcow_handle_extension: ERROR: pread fail from offset %llu\n", 99 (unsigned long long)offset); 100 return 1; 101 } 102 be32_to_cpus(&ext.magic); 103 be32_to_cpus(&ext.len); 104 offset += sizeof(ext); 105#ifdef DEBUG_EXT --- 5 unchanged lines hidden (view full) --- 111 112 case QCOW_EXT_MAGIC_BACKING_FORMAT: 113 if (ext.len >= sizeof(bs->backing_format)) { 114 fprintf(stderr, "ERROR: ext_backing_format: len=%u too large" 115 " (>=%zu)\n", 116 ext.len, sizeof(bs->backing_format)); 117 return 2; 118 } |
120 if (bdrv_pread(s->hd, offset , bs->backing_format, | 119 if (bdrv_pread(bs->file, offset , bs->backing_format, |
121 ext.len) != ext.len) 122 return 3; 123 bs->backing_format[ext.len] = '\0'; 124#ifdef DEBUG_EXT 125 printf("Qcow2: Got format extension %s\n", bs->backing_format); 126#endif 127 offset = ((offset + ext.len + 7) & ~7); 128 break; --- 4 unchanged lines hidden (view full) --- 133 break; 134 } 135 } 136 137 return 0; 138} 139 140 | 120 ext.len) != ext.len) 121 return 3; 122 bs->backing_format[ext.len] = '\0'; 123#ifdef DEBUG_EXT 124 printf("Qcow2: Got format extension %s\n", bs->backing_format); 125#endif 126 offset = ((offset + ext.len + 7) & ~7); 127 break; --- 4 unchanged lines hidden (view full) --- 132 break; 133 } 134 } 135 136 return 0; 137} 138 139 |
141static int qcow_open(BlockDriverState *bs, const char *filename, int flags) | 140static int qcow_open(BlockDriverState *bs, int flags) |
142{ 143 BDRVQcowState *s = bs->opaque; | 141{ 142 BDRVQcowState *s = bs->opaque; |
144 int len, i, shift, ret; | 143 int len, i, shift; |
145 QCowHeader header; 146 uint64_t ext_end; 147 | 144 QCowHeader header; 145 uint64_t ext_end; 146 |
148 ret = bdrv_file_open(&s->hd, filename, flags); 149 if (ret < 0) 150 return ret; 151 if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header)) | 147 if (bdrv_pread(bs->file, 0, &header, sizeof(header)) != sizeof(header)) |
152 goto fail; 153 be32_to_cpus(&header.magic); 154 be32_to_cpus(&header.version); 155 be64_to_cpus(&header.backing_file_offset); 156 be32_to_cpus(&header.backing_file_size); 157 be64_to_cpus(&header.size); 158 be32_to_cpus(&header.cluster_bits); 159 be32_to_cpus(&header.crypt_method); --- 37 unchanged lines hidden (view full) --- 197 /* the L1 table must contain at least enough entries to put 198 header.size bytes */ 199 if (s->l1_size < s->l1_vm_state_index) 200 goto fail; 201 s->l1_table_offset = header.l1_table_offset; 202 if (s->l1_size > 0) { 203 s->l1_table = qemu_mallocz( 204 align_offset(s->l1_size * sizeof(uint64_t), 512)); | 148 goto fail; 149 be32_to_cpus(&header.magic); 150 be32_to_cpus(&header.version); 151 be64_to_cpus(&header.backing_file_offset); 152 be32_to_cpus(&header.backing_file_size); 153 be64_to_cpus(&header.size); 154 be32_to_cpus(&header.cluster_bits); 155 be32_to_cpus(&header.crypt_method); --- 37 unchanged lines hidden (view full) --- 193 /* the L1 table must contain at least enough entries to put 194 header.size bytes */ 195 if (s->l1_size < s->l1_vm_state_index) 196 goto fail; 197 s->l1_table_offset = header.l1_table_offset; 198 if (s->l1_size > 0) { 199 s->l1_table = qemu_mallocz( 200 align_offset(s->l1_size * sizeof(uint64_t), 512)); |
205 if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != | 201 if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
206 s->l1_size * sizeof(uint64_t)) 207 goto fail; 208 for(i = 0;i < s->l1_size; i++) { 209 be64_to_cpus(&s->l1_table[i]); 210 } 211 } 212 /* alloc L2 cache */ 213 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); --- 16 unchanged lines hidden (view full) --- 230 if (qcow_read_extensions(bs, sizeof(header), ext_end)) 231 goto fail; 232 233 /* read the backing file name */ 234 if (header.backing_file_offset != 0) { 235 len = header.backing_file_size; 236 if (len > 1023) 237 len = 1023; | 202 s->l1_size * sizeof(uint64_t)) 203 goto fail; 204 for(i = 0;i < s->l1_size; i++) { 205 be64_to_cpus(&s->l1_table[i]); 206 } 207 } 208 /* alloc L2 cache */ 209 s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t)); --- 16 unchanged lines hidden (view full) --- 226 if (qcow_read_extensions(bs, sizeof(header), ext_end)) 227 goto fail; 228 229 /* read the backing file name */ 230 if (header.backing_file_offset != 0) { 231 len = header.backing_file_size; 232 if (len > 1023) 233 len = 1023; |
238 if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len) | 234 if (bdrv_pread(bs->file, header.backing_file_offset, bs->backing_file, len) != len) |
239 goto fail; 240 bs->backing_file[len] = '\0'; 241 } 242 if (qcow2_read_snapshots(bs) < 0) 243 goto fail; 244 245#ifdef DEBUG_ALLOC 246 qcow2_check_refcounts(bs); 247#endif 248 return 0; 249 250 fail: 251 qcow2_free_snapshots(bs); 252 qcow2_refcount_close(bs); 253 qemu_free(s->l1_table); 254 qemu_free(s->l2_cache); 255 qemu_free(s->cluster_cache); 256 qemu_free(s->cluster_data); | 235 goto fail; 236 bs->backing_file[len] = '\0'; 237 } 238 if (qcow2_read_snapshots(bs) < 0) 239 goto fail; 240 241#ifdef DEBUG_ALLOC 242 qcow2_check_refcounts(bs); 243#endif 244 return 0; 245 246 fail: 247 qcow2_free_snapshots(bs); 248 qcow2_refcount_close(bs); 249 qemu_free(s->l1_table); 250 qemu_free(s->l2_cache); 251 qemu_free(s->cluster_cache); 252 qemu_free(s->cluster_data); |
257 bdrv_delete(s->hd); | |
258 return -1; 259} 260 261static int qcow_set_key(BlockDriverState *bs, const char *key) 262{ 263 BDRVQcowState *s = bs->opaque; 264 uint8_t keybuf[16]; 265 int len, i; --- 158 unchanged lines hidden (view full) --- 424 if (bs->backing_hd) { 425 /* read from the base image */ 426 n1 = qcow2_backing_read1(bs->backing_hd, acb->sector_num, 427 acb->buf, acb->cur_nr_sectors); 428 if (n1 > 0) { 429 acb->hd_iov.iov_base = (void *)acb->buf; 430 acb->hd_iov.iov_len = acb->cur_nr_sectors * 512; 431 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); | 253 return -1; 254} 255 256static int qcow_set_key(BlockDriverState *bs, const char *key) 257{ 258 BDRVQcowState *s = bs->opaque; 259 uint8_t keybuf[16]; 260 int len, i; --- 158 unchanged lines hidden (view full) --- 419 if (bs->backing_hd) { 420 /* read from the base image */ 421 n1 = qcow2_backing_read1(bs->backing_hd, acb->sector_num, 422 acb->buf, acb->cur_nr_sectors); 423 if (n1 > 0) { 424 acb->hd_iov.iov_base = (void *)acb->buf; 425 acb->hd_iov.iov_len = acb->cur_nr_sectors * 512; 426 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); |
432 BLKDBG_EVENT(s->hd, BLKDBG_READ_BACKING_AIO); | 427 BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); |
433 acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num, 434 &acb->hd_qiov, acb->cur_nr_sectors, 435 qcow_aio_read_cb, acb); 436 if (acb->hd_aiocb == NULL) 437 goto done; 438 } else { 439 ret = qcow_schedule_bh(qcow_aio_read_bh, acb); 440 if (ret < 0) 441 goto done; 442 } 443 } else { 444 /* Note: in this case, no need to wait */ 445 memset(acb->buf, 0, 512 * acb->cur_nr_sectors); 446 ret = qcow_schedule_bh(qcow_aio_read_bh, acb); 447 if (ret < 0) 448 goto done; 449 } 450 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { 451 /* add AIO support for compressed blocks ? */ | 428 acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num, 429 &acb->hd_qiov, acb->cur_nr_sectors, 430 qcow_aio_read_cb, acb); 431 if (acb->hd_aiocb == NULL) 432 goto done; 433 } else { 434 ret = qcow_schedule_bh(qcow_aio_read_bh, acb); 435 if (ret < 0) 436 goto done; 437 } 438 } else { 439 /* Note: in this case, no need to wait */ 440 memset(acb->buf, 0, 512 * acb->cur_nr_sectors); 441 ret = qcow_schedule_bh(qcow_aio_read_bh, acb); 442 if (ret < 0) 443 goto done; 444 } 445 } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { 446 /* add AIO support for compressed blocks ? */ |
452 if (qcow2_decompress_cluster(s, acb->cluster_offset) < 0) | 447 if (qcow2_decompress_cluster(bs, acb->cluster_offset) < 0) |
453 goto done; 454 memcpy(acb->buf, s->cluster_cache + index_in_cluster * 512, 455 512 * acb->cur_nr_sectors); 456 ret = qcow_schedule_bh(qcow_aio_read_bh, acb); 457 if (ret < 0) 458 goto done; 459 } else { 460 if ((acb->cluster_offset & 511) != 0) { 461 ret = -EIO; 462 goto done; 463 } 464 465 acb->hd_iov.iov_base = (void *)acb->buf; 466 acb->hd_iov.iov_len = acb->cur_nr_sectors * 512; 467 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); | 448 goto done; 449 memcpy(acb->buf, s->cluster_cache + index_in_cluster * 512, 450 512 * acb->cur_nr_sectors); 451 ret = qcow_schedule_bh(qcow_aio_read_bh, acb); 452 if (ret < 0) 453 goto done; 454 } else { 455 if ((acb->cluster_offset & 511) != 0) { 456 ret = -EIO; 457 goto done; 458 } 459 460 acb->hd_iov.iov_base = (void *)acb->buf; 461 acb->hd_iov.iov_len = acb->cur_nr_sectors * 512; 462 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); |
468 BLKDBG_EVENT(s->hd, BLKDBG_READ_AIO); 469 acb->hd_aiocb = bdrv_aio_readv(s->hd, | 463 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); 464 acb->hd_aiocb = bdrv_aio_readv(bs->file, |
470 (acb->cluster_offset >> 9) + index_in_cluster, 471 &acb->hd_qiov, acb->cur_nr_sectors, 472 qcow_aio_read_cb, acb); 473 if (acb->hd_aiocb == NULL) { 474 ret = -EIO; 475 goto done; 476 } 477 } --- 132 unchanged lines hidden (view full) --- 610 acb->cur_nr_sectors, 1, &s->aes_encrypt_key); 611 src_buf = acb->cluster_data; 612 } else { 613 src_buf = acb->buf; 614 } 615 acb->hd_iov.iov_base = (void *)src_buf; 616 acb->hd_iov.iov_len = acb->cur_nr_sectors * 512; 617 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); | 465 (acb->cluster_offset >> 9) + index_in_cluster, 466 &acb->hd_qiov, acb->cur_nr_sectors, 467 qcow_aio_read_cb, acb); 468 if (acb->hd_aiocb == NULL) { 469 ret = -EIO; 470 goto done; 471 } 472 } --- 132 unchanged lines hidden (view full) --- 605 acb->cur_nr_sectors, 1, &s->aes_encrypt_key); 606 src_buf = acb->cluster_data; 607 } else { 608 src_buf = acb->buf; 609 } 610 acb->hd_iov.iov_base = (void *)src_buf; 611 acb->hd_iov.iov_len = acb->cur_nr_sectors * 512; 612 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1); |
618 BLKDBG_EVENT(s->hd, BLKDBG_WRITE_AIO); 619 acb->hd_aiocb = bdrv_aio_writev(s->hd, | 613 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); 614 acb->hd_aiocb = bdrv_aio_writev(bs->file, |
620 (acb->cluster_offset >> 9) + index_in_cluster, 621 &acb->hd_qiov, acb->cur_nr_sectors, 622 qcow_aio_write_cb, acb); 623 if (acb->hd_aiocb == NULL) { 624 ret = -EIO; 625 goto fail; 626 } 627 --- 30 unchanged lines hidden (view full) --- 658static void qcow_close(BlockDriverState *bs) 659{ 660 BDRVQcowState *s = bs->opaque; 661 qemu_free(s->l1_table); 662 qemu_free(s->l2_cache); 663 qemu_free(s->cluster_cache); 664 qemu_free(s->cluster_data); 665 qcow2_refcount_close(bs); | 615 (acb->cluster_offset >> 9) + index_in_cluster, 616 &acb->hd_qiov, acb->cur_nr_sectors, 617 qcow_aio_write_cb, acb); 618 if (acb->hd_aiocb == NULL) { 619 ret = -EIO; 620 goto fail; 621 } 622 --- 30 unchanged lines hidden (view full) --- 653static void qcow_close(BlockDriverState *bs) 654{ 655 BDRVQcowState *s = bs->opaque; 656 qemu_free(s->l1_table); 657 qemu_free(s->l2_cache); 658 qemu_free(s->cluster_cache); 659 qemu_free(s->cluster_data); 660 qcow2_refcount_close(bs); |
666 bdrv_delete(s->hd); | |
667} 668 669/* 670 * Updates the variable length parts of the qcow2 header, i.e. the backing file 671 * name and all extensions. qcow2 was not designed to allow such changes, so if 672 * we run out of space (we can only use the first cluster) this function may 673 * fail. 674 * --- 53 unchanged lines hidden (view full) --- 728 memset(buf + offset, 0, padding); 729 offset += padding; 730 } 731 732 memcpy(buf + offset, backing_file, backing_file_len); 733 backing_file_offset = sizeof(QCowHeader) + offset; 734 } 735 | 661} 662 663/* 664 * Updates the variable length parts of the qcow2 header, i.e. the backing file 665 * name and all extensions. qcow2 was not designed to allow such changes, so if 666 * we run out of space (we can only use the first cluster) this function may 667 * fail. 668 * --- 53 unchanged lines hidden (view full) --- 722 memset(buf + offset, 0, padding); 723 offset += padding; 724 } 725 726 memcpy(buf + offset, backing_file, backing_file_len); 727 backing_file_offset = sizeof(QCowHeader) + offset; 728 } 729 |
736 ret = bdrv_pwrite(s->hd, sizeof(QCowHeader), buf, ext_size); | 730 ret = bdrv_pwrite(bs->file, sizeof(QCowHeader), buf, ext_size); |
737 if (ret < 0) { 738 goto fail; 739 } 740 741 /* Update header fields */ 742 uint64_t be_backing_file_offset = cpu_to_be64(backing_file_offset); 743 uint32_t be_backing_file_size = cpu_to_be32(backing_file_len); 744 | 731 if (ret < 0) { 732 goto fail; 733 } 734 735 /* Update header fields */ 736 uint64_t be_backing_file_offset = cpu_to_be64(backing_file_offset); 737 uint32_t be_backing_file_size = cpu_to_be32(backing_file_len); 738 |
745 ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, backing_file_offset), | 739 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, backing_file_offset), |
746 &be_backing_file_offset, sizeof(uint64_t)); 747 if (ret < 0) { 748 goto fail; 749 } 750 | 740 &be_backing_file_offset, sizeof(uint64_t)); 741 if (ret < 0) { 742 goto fail; 743 } 744 |
751 ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, backing_file_size), | 745 ret = bdrv_pwrite(bs->file, offsetof(QCowHeader, backing_file_size), |
752 &be_backing_file_size, sizeof(uint32_t)); 753 if (ret < 0) { 754 goto fail; 755 } 756 757 ret = 0; 758fail: 759 return ret; --- 24 unchanged lines hidden (view full) --- 784 } 785 786 return res; 787} 788 789 790static int preallocate(BlockDriverState *bs) 791{ | 746 &be_backing_file_size, sizeof(uint32_t)); 747 if (ret < 0) { 748 goto fail; 749 } 750 751 ret = 0; 752fail: 753 return ret; --- 24 unchanged lines hidden (view full) --- 778 } 779 780 return res; 781} 782 783 784static int preallocate(BlockDriverState *bs) 785{ |
792 BDRVQcowState *s = bs->opaque; | |
793 uint64_t nb_sectors; 794 uint64_t offset; 795 int num; 796 int ret; 797 QCowL2Meta meta; 798 799 nb_sectors = bdrv_getlength(bs) >> 9; 800 offset = 0; --- 26 unchanged lines hidden (view full) --- 827 /* 828 * It is expected that the image file is large enough to actually contain 829 * all of the allocated clusters (otherwise we get failing reads after 830 * EOF). Extend the image to the last allocated sector. 831 */ 832 if (meta.cluster_offset != 0) { 833 uint8_t buf[512]; 834 memset(buf, 0, 512); | 786 uint64_t nb_sectors; 787 uint64_t offset; 788 int num; 789 int ret; 790 QCowL2Meta meta; 791 792 nb_sectors = bdrv_getlength(bs) >> 9; 793 offset = 0; --- 26 unchanged lines hidden (view full) --- 820 /* 821 * It is expected that the image file is large enough to actually contain 822 * all of the allocated clusters (otherwise we get failing reads after 823 * EOF). Extend the image to the last allocated sector. 824 */ 825 if (meta.cluster_offset != 0) { 826 uint8_t buf[512]; 827 memset(buf, 0, 512); |
835 bdrv_write(s->hd, (meta.cluster_offset >> 9) + num - 1, buf, 1); | 828 bdrv_write(bs->file, (meta.cluster_offset >> 9) + num - 1, buf, 1); |
836 } 837 838 return 0; 839} 840 841static int qcow_make_empty(BlockDriverState *bs) 842{ 843#if 0 844 /* XXX: not correct */ 845 BDRVQcowState *s = bs->opaque; 846 uint32_t l1_length = s->l1_size * sizeof(uint64_t); 847 int ret; 848 849 memset(s->l1_table, 0, l1_length); | 829 } 830 831 return 0; 832} 833 834static int qcow_make_empty(BlockDriverState *bs) 835{ 836#if 0 837 /* XXX: not correct */ 838 BDRVQcowState *s = bs->opaque; 839 uint32_t l1_length = s->l1_size * sizeof(uint64_t); 840 int ret; 841 842 memset(s->l1_table, 0, l1_length); |
850 if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0) | 843 if (bdrv_pwrite(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0) |
851 return -1; | 844 return -1; |
852 ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length); | 845 ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length); |
853 if (ret < 0) 854 return ret; 855 856 l2_cache_reset(bs); 857#endif 858 return 0; 859} 860 --- 6 unchanged lines hidden (view full) --- 867 z_stream strm; 868 int ret, out_len; 869 uint8_t *out_buf; 870 uint64_t cluster_offset; 871 872 if (nb_sectors == 0) { 873 /* align end of file to a sector boundary to ease reading with 874 sector based I/Os */ | 846 if (ret < 0) 847 return ret; 848 849 l2_cache_reset(bs); 850#endif 851 return 0; 852} 853 --- 6 unchanged lines hidden (view full) --- 860 z_stream strm; 861 int ret, out_len; 862 uint8_t *out_buf; 863 uint64_t cluster_offset; 864 865 if (nb_sectors == 0) { 866 /* align end of file to a sector boundary to ease reading with 867 sector based I/Os */ |
875 cluster_offset = bdrv_getlength(s->hd); | 868 cluster_offset = bdrv_getlength(bs->file); |
876 cluster_offset = (cluster_offset + 511) & ~511; | 869 cluster_offset = (cluster_offset + 511) & ~511; |
877 bdrv_truncate(s->hd, cluster_offset); | 870 bdrv_truncate(bs->file, cluster_offset); |
878 return 0; 879 } 880 881 if (nb_sectors != s->cluster_sectors) 882 return -EINVAL; 883 884 out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); 885 --- 26 unchanged lines hidden (view full) --- 912 /* could not compress: write normal cluster */ 913 bdrv_write(bs, sector_num, buf, s->cluster_sectors); 914 } else { 915 cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, 916 sector_num << 9, out_len); 917 if (!cluster_offset) 918 return -1; 919 cluster_offset &= s->cluster_offset_mask; | 871 return 0; 872 } 873 874 if (nb_sectors != s->cluster_sectors) 875 return -EINVAL; 876 877 out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); 878 --- 26 unchanged lines hidden (view full) --- 905 /* could not compress: write normal cluster */ 906 bdrv_write(bs, sector_num, buf, s->cluster_sectors); 907 } else { 908 cluster_offset = qcow2_alloc_compressed_cluster_offset(bs, 909 sector_num << 9, out_len); 910 if (!cluster_offset) 911 return -1; 912 cluster_offset &= s->cluster_offset_mask; |
920 BLKDBG_EVENT(s->hd, BLKDBG_WRITE_COMPRESSED); 921 if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) { | 913 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED); 914 if (bdrv_pwrite(bs->file, cluster_offset, out_buf, out_len) != out_len) { |
922 qemu_free(out_buf); 923 return -1; 924 } 925 } 926 927 qemu_free(out_buf); 928 return 0; 929} 930 931static void qcow_flush(BlockDriverState *bs) 932{ | 915 qemu_free(out_buf); 916 return -1; 917 } 918 } 919 920 qemu_free(out_buf); 921 return 0; 922} 923 924static void qcow_flush(BlockDriverState *bs) 925{ |
933 BDRVQcowState *s = bs->opaque; 934 bdrv_flush(s->hd); | 926 bdrv_flush(bs->file); |
935} 936 937static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs, 938 BlockDriverCompletionFunc *cb, void *opaque) 939{ | 927} 928 929static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs, 930 BlockDriverCompletionFunc *cb, void *opaque) 931{ |
940 BDRVQcowState *s = bs->opaque; 941 942 return bdrv_aio_flush(s->hd, cb, opaque); | 932 return bdrv_aio_flush(bs->file, cb, opaque); |
943} 944 945static int64_t qcow_vm_state_offset(BDRVQcowState *s) 946{ 947 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); 948} 949 950static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) --- 12 unchanged lines hidden (view full) --- 963 964#if 0 965static void dump_refcounts(BlockDriverState *bs) 966{ 967 BDRVQcowState *s = bs->opaque; 968 int64_t nb_clusters, k, k1, size; 969 int refcount; 970 | 933} 934 935static int64_t qcow_vm_state_offset(BDRVQcowState *s) 936{ 937 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits); 938} 939 940static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) --- 12 unchanged lines hidden (view full) --- 953 954#if 0 955static void dump_refcounts(BlockDriverState *bs) 956{ 957 BDRVQcowState *s = bs->opaque; 958 int64_t nb_clusters, k, k1, size; 959 int refcount; 960 |
971 size = bdrv_getlength(s->hd); | 961 size = bdrv_getlength(bs->file); |
972 nb_clusters = size_to_clusters(s, size); 973 for(k = 0; k < nb_clusters;) { 974 k1 = k; 975 refcount = get_refcount(bs, k); 976 k++; 977 while (k < nb_clusters && get_refcount(bs, k) == refcount) 978 k++; 979 printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1); 980 } 981} 982#endif 983 984static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 985 int64_t pos, int size) 986{ 987 BDRVQcowState *s = bs->opaque; 988 int growable = bs->growable; 989 int ret; 990 | 962 nb_clusters = size_to_clusters(s, size); 963 for(k = 0; k < nb_clusters;) { 964 k1 = k; 965 refcount = get_refcount(bs, k); 966 k++; 967 while (k < nb_clusters && get_refcount(bs, k) == refcount) 968 k++; 969 printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1); 970 } 971} 972#endif 973 974static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 975 int64_t pos, int size) 976{ 977 BDRVQcowState *s = bs->opaque; 978 int growable = bs->growable; 979 int ret; 980 |
991 BLKDBG_EVENT(s->hd, BLKDBG_VMSTATE_SAVE); | 981 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_SAVE); |
992 bs->growable = 1; 993 ret = bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size); 994 bs->growable = growable; 995 996 return ret; 997} 998 999static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf, 1000 int64_t pos, int size) 1001{ 1002 BDRVQcowState *s = bs->opaque; 1003 int growable = bs->growable; 1004 int ret; 1005 | 982 bs->growable = 1; 983 ret = bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size); 984 bs->growable = growable; 985 986 return ret; 987} 988 989static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf, 990 int64_t pos, int size) 991{ 992 BDRVQcowState *s = bs->opaque; 993 int growable = bs->growable; 994 int ret; 995 |
1006 BLKDBG_EVENT(s->hd, BLKDBG_VMSTATE_LOAD); | 996 BLKDBG_EVENT(bs->file, BLKDBG_VMSTATE_LOAD); |
1007 bs->growable = 1; 1008 ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size); 1009 bs->growable = growable; 1010 1011 return ret; 1012} 1013 1014static QEMUOptionParameter qcow_create_options[] = { --- 315 unchanged lines hidden --- | 997 bs->growable = 1; 998 ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size); 999 bs->growable = growable; 1000 1001 return ret; 1002} 1003 1004static QEMUOptionParameter qcow_create_options[] = { --- 315 unchanged lines hidden --- |