Revision a9420734

b/block/qcow2.c
27 27
#include <zlib.h>
28 28
#include "aes.h"
29 29
#include "block/qcow2.h"
30
#include "qemu-error.h"
30 31

  
31 32
/*
32 33
  Differences with QCOW:
......
794 795
    return qcow2_update_ext_header(bs, backing_file, backing_fmt);
795 796
}
796 797

  
798
#if 0
797 799
static int get_bits_from_size(size_t size)
798 800
{
799 801
    int res = 0;
......
814 816

  
815 817
    return res;
816 818
}
819
#endif
817 820

  
818 821

  
819 822
static int preallocate(BlockDriverState *bs)
......
869 872
    return 0;
870 873
}
871 874

  
875
#if 0
872 876
static int qcow_create2(const char *filename, int64_t total_size,
873 877
                        const char *backing_file, const char *backing_format,
874 878
                        int flags, size_t cluster_size, int prealloc)
......
1066 1070

  
1067 1071
    return ret;
1068 1072
}
1073
#else
1074
static int qcow_create2(const char *filename, int64_t total_size,
1075
                        const char *backing_file, const char *backing_format,
1076
                        int flags, size_t cluster_size, int prealloc,
1077
                        QEMUOptionParameter *options)
1078
{
1079
    /* Calulate cluster_bits */
1080
    int cluster_bits;
1081
    cluster_bits = ffs(cluster_size) - 1;
1082
    if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
1083
        (1 << cluster_bits) != cluster_size)
1084
    {
1085
        error_report(
1086
            "Cluster size must be a power of two between %d and %dk\n",
1087
            1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
1088
        return -EINVAL;
1089
    }
1090

  
1091
    /*
1092
     * Open the image file and write a minimal qcow2 header.
1093
     *
1094
     * We keep things simple and start with a zero-sized image. We also
1095
     * do without refcount blocks or a L1 table for now. We'll fix the
1096
     * inconsistency later.
1097
     *
1098
     * We do need a refcount table because growing the refcount table means
1099
     * allocating two new refcount blocks - the seconds of which would be at
1100
     * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
1101
     * size for any qcow2 image.
1102
     */
1103
    BlockDriverState* bs;
1104
    QCowHeader header;
1105
    uint8_t* refcount_table;
1106
    int ret;
1107

  
1108
    ret = bdrv_create_file(filename, options);
1109
    if (ret < 0) {
1110
        return ret;
1111
    }
1112

  
1113
    ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR);
1114
    if (ret < 0) {
1115
        return ret;
1116
    }
1117

  
1118
    /* Write the header */
1119
    memset(&header, 0, sizeof(header));
1120
    header.magic = cpu_to_be32(QCOW_MAGIC);
1121
    header.version = cpu_to_be32(QCOW_VERSION);
1122
    header.cluster_bits = cpu_to_be32(cluster_bits);
1123
    header.size = cpu_to_be64(0);
1124
    header.l1_table_offset = cpu_to_be64(0);
1125
    header.l1_size = cpu_to_be32(0);
1126
    header.refcount_table_offset = cpu_to_be64(cluster_size);
1127
    header.refcount_table_clusters = cpu_to_be32(1);
1128

  
1129
    if (flags & BLOCK_FLAG_ENCRYPT) {
1130
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
1131
    } else {
1132
        header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
1133
    }
1134

  
1135
    ret = bdrv_pwrite(bs, 0, &header, sizeof(header));
1136
    if (ret < 0) {
1137
        goto out;
1138
    }
1139

  
1140
    /* Write an empty refcount table */
1141
    refcount_table = qemu_mallocz(cluster_size);
1142
    ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
1143
    qemu_free(refcount_table);
1144

  
1145
    if (ret < 0) {
1146
        goto out;
1147
    }
1148

  
1149
    bdrv_close(bs);
1150

  
1151
    /*
1152
     * And now open the image and make it consistent first (i.e. increase the
1153
     * refcount of the cluster that is occupied by the header and the refcount
1154
     * table)
1155
     */
1156
    BlockDriver* drv = bdrv_find_format("qcow2");
1157
    assert(drv != NULL);
1158
    ret = bdrv_open(bs, filename, BDRV_O_RDWR | BDRV_O_NO_FLUSH, drv);
1159
    if (ret < 0) {
1160
        goto out;
1161
    }
1162

  
1163
    ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
1164
    if (ret < 0) {
1165
        goto out;
1166

  
1167
    } else if (ret != 0) {
1168
        error_report("Huh, first cluster in empty image is already in use?");
1169
        abort();
1170
    }
1171

  
1172
    /* Okay, now that we have a valid image, let's give it the right size */
1173
    ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
1174
    if (ret < 0) {
1175
        goto out;
1176
    }
1177

  
1178
    /* Want a backing file? There you go.*/
1179
    if (backing_file) {
1180
        ret = bdrv_change_backing_file(bs, backing_file, backing_format);
1181
        if (ret < 0) {
1182
            goto out;
1183
        }
1184
    }
1185

  
1186
    /* And if we're supposed to preallocate metadata, do that now */
1187
    if (prealloc) {
1188
        ret = preallocate(bs);
1189
        if (ret < 0) {
1190
            goto out;
1191
        }
1192
    }
1193

  
1194
    ret = 0;
1195
out:
1196
    bdrv_delete(bs);
1197
    return ret;
1198
}
1199
#endif
1069 1200

  
1070 1201
static int qcow_create(const char *filename, QEMUOptionParameter *options)
1071 1202
{
......
1111 1242
    }
1112 1243

  
1113 1244
    return qcow_create2(filename, sectors, backing_file, backing_fmt, flags,
1114
        cluster_size, prealloc);
1245
        cluster_size, prealloc, options);
1115 1246
}
1116 1247

  
1117 1248
static int qcow_make_empty(BlockDriverState *bs)

Also available in: Unified diff