Revision ade40677 block/qcow2.c

b/block/qcow2.c
1229 1229
    return 0;
1230 1230
}
1231 1231

  
1232
static int qcow_write(BlockDriverState *bs, int64_t sector_num,
1233
                     const uint8_t *buf, int nb_sectors)
1234
{
1235
    BDRVQcowState *s = bs->opaque;
1236
    int ret, index_in_cluster, n;
1237
    uint64_t cluster_offset;
1238
    int n_end;
1239
    QCowL2Meta l2meta;
1240

  
1241
    while (nb_sectors > 0) {
1242
        index_in_cluster = sector_num & (s->cluster_sectors - 1);
1243
        n_end = index_in_cluster + nb_sectors;
1244
        if (s->crypt_method &&
1245
            n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
1246
            n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;
1247
        cluster_offset = alloc_cluster_offset(bs, sector_num << 9,
1248
                                              index_in_cluster,
1249
                                              n_end, &n, &l2meta);
1250
        if (!cluster_offset)
1251
            return -1;
1252
        if (s->crypt_method) {
1253
            encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
1254
                            &s->aes_encrypt_key);
1255
            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,
1256
                              s->cluster_data, n * 512);
1257
        } else {
1258
            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
1259
        }
1260
        if (ret != n * 512 || alloc_cluster_link_l2(bs, cluster_offset, &l2meta) < 0) {
1261
            free_any_clusters(bs, cluster_offset, l2meta.nb_clusters);
1262
            return -1;
1263
        }
1264
        nb_sectors -= n;
1265
        sector_num += n;
1266
        buf += n * 512;
1267
    }
1268
    s->cluster_cache_offset = -1; /* disable compressed cache */
1269
    return 0;
1270
}
1271

  
1272 1232
typedef struct QCowAIOCB {
1273 1233
    BlockDriverAIOCB common;
1274 1234
    int64_t sector_num;
......
1834 1794

  
1835 1795
    if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
1836 1796
        /* could not compress: write normal cluster */
1837
        qcow_write(bs, sector_num, buf, s->cluster_sectors);
1797
        bdrv_write(bs, sector_num, buf, s->cluster_sectors);
1838 1798
    } else {
1839 1799
        cluster_offset = alloc_compressed_cluster_offset(bs, sector_num << 9,
1840 1800
                                              out_len);

Also available in: Unified diff