qcow2.c (e1c7f0e3f998866bedc9bdb53d247859b7beb5ce) | qcow2.c (756e6736a12a46330d9532d5f861ba15b38886d8) |
---|---|
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 --- 644 unchanged lines hidden (view full) --- 653 qemu_free(s->l1_table); 654 qemu_free(s->l2_cache); 655 qemu_free(s->cluster_cache); 656 qemu_free(s->cluster_data); 657 qcow2_refcount_close(bs); 658 bdrv_delete(s->hd); 659} 660 | 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 --- 644 unchanged lines hidden (view full) --- 653 qemu_free(s->l1_table); 654 qemu_free(s->l2_cache); 655 qemu_free(s->cluster_cache); 656 qemu_free(s->cluster_data); 657 qcow2_refcount_close(bs); 658 bdrv_delete(s->hd); 659} 660 |
661/* 662 * Updates the variable length parts of the qcow2 header, i.e. the backing file 663 * name and all extensions. qcow2 was not designed to allow such changes, so if 664 * we run out of space (we can only use the first cluster) this function may 665 * fail. 666 * 667 * Returns 0 on success, -errno in error cases. 668 */ 669static int qcow2_update_ext_header(BlockDriverState *bs, 670 const char *backing_file, const char *backing_fmt) 671{ 672 size_t backing_file_len = 0; 673 size_t backing_fmt_len = 0; 674 BDRVQcowState *s = bs->opaque; 675 QCowExtension ext_backing_fmt = {0, 0}; 676 int ret; 677 678 /* Backing file format doesn't make sense without a backing file */ 679 if (backing_fmt && !backing_file) { 680 return -EINVAL; 681 } 682 683 /* Prepare the backing file format extension if needed */ 684 if (backing_fmt) { 685 ext_backing_fmt.len = cpu_to_be32(strlen(backing_fmt)); 686 ext_backing_fmt.magic = cpu_to_be32(QCOW_EXT_MAGIC_BACKING_FORMAT); 687 backing_fmt_len = ((sizeof(ext_backing_fmt) 688 + strlen(backing_fmt) + 7) & ~7); 689 } 690 691 /* Check if we can fit the new header into the first cluster */ 692 if (backing_file) { 693 backing_file_len = strlen(backing_file); 694 } 695 696 size_t header_size = sizeof(QCowHeader) + backing_file_len 697 + backing_fmt_len; 698 699 if (header_size > s->cluster_size) { 700 return -ENOSPC; 701 } 702 703 /* Rewrite backing file name and qcow2 extensions */ 704 size_t ext_size = header_size - sizeof(QCowHeader); 705 uint8_t buf[ext_size]; 706 size_t offset = 0; 707 size_t backing_file_offset = 0; 708 709 if (backing_file) { 710 if (backing_fmt) { 711 int padding = backing_fmt_len - 712 (sizeof(ext_backing_fmt) + strlen(backing_fmt)); 713 714 memcpy(buf + offset, &ext_backing_fmt, sizeof(ext_backing_fmt)); 715 offset += sizeof(ext_backing_fmt); 716 717 memcpy(buf + offset, backing_fmt, strlen(backing_fmt)); 718 offset += strlen(backing_fmt); 719 720 memset(buf + offset, 0, padding); 721 offset += padding; 722 } 723 724 memcpy(buf + offset, backing_file, backing_file_len); 725 backing_file_offset = sizeof(QCowHeader) + offset; 726 } 727 728 ret = bdrv_pwrite(s->hd, sizeof(QCowHeader), buf, ext_size); 729 if (ret < 0) { 730 goto fail; 731 } 732 733 /* Update header fields */ 734 uint64_t be_backing_file_offset = cpu_to_be64(backing_file_offset); 735 uint32_t be_backing_file_size = cpu_to_be32(backing_file_len); 736 737 ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, backing_file_offset), 738 &be_backing_file_offset, sizeof(uint64_t)); 739 if (ret < 0) { 740 goto fail; 741 } 742 743 ret = bdrv_pwrite(s->hd, offsetof(QCowHeader, backing_file_size), 744 &be_backing_file_size, sizeof(uint32_t)); 745 if (ret < 0) { 746 goto fail; 747 } 748 749 ret = 0; 750fail: 751 return ret; 752} 753 754static int qcow2_change_backing_file(BlockDriverState *bs, 755 const char *backing_file, const char *backing_fmt) 756{ 757 return qcow2_update_ext_header(bs, backing_file, backing_fmt); 758} 759 |
|
661static int get_bits_from_size(size_t size) 662{ 663 int res = 0; 664 665 if (size == 0) { 666 return -1; 667 } 668 --- 463 unchanged lines hidden (view full) --- 1132 .bdrv_snapshot_goto = qcow2_snapshot_goto, 1133 .bdrv_snapshot_delete = qcow2_snapshot_delete, 1134 .bdrv_snapshot_list = qcow2_snapshot_list, 1135 .bdrv_get_info = qcow_get_info, 1136 1137 .bdrv_save_vmstate = qcow_save_vmstate, 1138 .bdrv_load_vmstate = qcow_load_vmstate, 1139 | 760static int get_bits_from_size(size_t size) 761{ 762 int res = 0; 763 764 if (size == 0) { 765 return -1; 766 } 767 --- 463 unchanged lines hidden (view full) --- 1231 .bdrv_snapshot_goto = qcow2_snapshot_goto, 1232 .bdrv_snapshot_delete = qcow2_snapshot_delete, 1233 .bdrv_snapshot_list = qcow2_snapshot_list, 1234 .bdrv_get_info = qcow_get_info, 1235 1236 .bdrv_save_vmstate = qcow_save_vmstate, 1237 .bdrv_load_vmstate = qcow_load_vmstate, 1238 |
1239 .bdrv_change_backing_file = qcow2_change_backing_file, 1240 |
|
1140 .create_options = qcow_create_options, 1141 .bdrv_check = qcow_check, 1142}; 1143 1144static void bdrv_qcow2_init(void) 1145{ 1146 bdrv_register(&bdrv_qcow2); 1147} 1148 1149block_init(bdrv_qcow2_init); | 1241 .create_options = qcow_create_options, 1242 .bdrv_check = qcow_check, 1243}; 1244 1245static void bdrv_qcow2_init(void) 1246{ 1247 bdrv_register(&bdrv_qcow2); 1248} 1249 1250block_init(bdrv_qcow2_init); |