qcow2.h 18.5 KB
Newer Older
K
Kevin Wolf 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Block driver for the QCOW version 2 format
 *
 * Copyright (c) 2004-2006 Fabrice Bellard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef BLOCK_QCOW2_H
#define BLOCK_QCOW2_H

28
#include "qemu/aes.h"
29
#include "block/coroutine.h"
K
Kevin Wolf 已提交
30

31 32 33 34
//#define DEBUG_ALLOC
//#define DEBUG_ALLOC2
//#define DEBUG_EXT

K
Kevin Wolf 已提交
35 36 37 38 39 40
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)

#define QCOW_CRYPT_NONE 0
#define QCOW_CRYPT_AES  1

#define QCOW_MAX_CRYPT_CLUSTERS 32
41
#define QCOW_MAX_SNAPSHOTS 65536
K
Kevin Wolf 已提交
42

43 44 45 46
/* 8 MB refcount table is enough for 2 PB images at 64k cluster size
 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
#define QCOW_MAX_REFTABLE_SIZE 0x800000

47 48 49 50
/* 32 MB L1 table is enough for 2 PB images at 64k cluster size
 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
#define QCOW_MAX_L1_SIZE 0x2000000

K
Kevin Wolf 已提交
51 52 53 54
/* Allow for an average of 1k per snapshot table entry, should be plenty of
 * space for snapshot names and IDs */
#define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS)

K
Kevin Wolf 已提交
55
/* indicate that the refcount of the referenced cluster is exactly one. */
56
#define QCOW_OFLAG_COPIED     (1ULL << 63)
K
Kevin Wolf 已提交
57
/* indicate that the cluster is compressed (they never have the copied flag) */
58
#define QCOW_OFLAG_COMPRESSED (1ULL << 62)
59
/* The cluster reads as all zeros */
60
#define QCOW_OFLAG_ZERO (1ULL << 0)
K
Kevin Wolf 已提交
61 62

#define MIN_CLUSTER_BITS 9
63
#define MAX_CLUSTER_BITS 21
K
Kevin Wolf 已提交
64

M
Max Reitz 已提交
65
#define MIN_L2_CACHE_SIZE 1 /* cluster */
K
Kevin Wolf 已提交
66

K
Kevin Wolf 已提交
67
/* Must be at least 4 to cover all cases of refcount table growth */
M
Max Reitz 已提交
68 69 70 71 72 73 74
#define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */

#define DEFAULT_L2_CACHE_BYTE_SIZE 1048576 /* bytes */

/* The refblock cache needs only a fourth of the L2 cache size to cover as many
 * clusters */
#define DEFAULT_L2_REFCOUNT_SIZE_RATIO 4
K
Kevin Wolf 已提交
75

76 77
#define DEFAULT_CLUSTER_SIZE 65536

78

79 80 81 82
#define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
#define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
#define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
#define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
M
Max Reitz 已提交
83
#define QCOW2_OPT_OVERLAP "overlap-check"
84
#define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
M
Max Reitz 已提交
85 86 87 88 89 90 91 92
#define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
#define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
#define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
#define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
#define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
#define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
#define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
#define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
93 94 95
#define QCOW2_OPT_CACHE_SIZE "cache-size"
#define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
#define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
96

K
Kevin Wolf 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110
typedef struct QCowHeader {
    uint32_t magic;
    uint32_t version;
    uint64_t backing_file_offset;
    uint32_t backing_file_size;
    uint32_t cluster_bits;
    uint64_t size; /* in bytes */
    uint32_t crypt_method;
    uint32_t l1_size; /* XXX: save number of clusters instead ? */
    uint64_t l1_table_offset;
    uint64_t refcount_table_offset;
    uint32_t refcount_table_clusters;
    uint32_t nb_snapshots;
    uint64_t snapshots_offset;
K
Kevin Wolf 已提交
111 112 113 114 115 116 117 118

    /* The following fields are only valid for version >= 3 */
    uint64_t incompatible_features;
    uint64_t compatible_features;
    uint64_t autoclear_features;

    uint32_t refcount_order;
    uint32_t header_length;
119
} QEMU_PACKED QCowHeader;
K
Kevin Wolf 已提交
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
typedef struct QEMU_PACKED QCowSnapshotHeader {
    /* header is 8 byte aligned */
    uint64_t l1_table_offset;

    uint32_t l1_size;
    uint16_t id_str_size;
    uint16_t name_size;

    uint32_t date_sec;
    uint32_t date_nsec;

    uint64_t vm_clock_nsec;

    uint32_t vm_state_size;
    uint32_t extra_data_size; /* for extension */
    /* extra data follows */
    /* id_str follows */
    /* name follows  */
} QCowSnapshotHeader;

typedef struct QEMU_PACKED QCowSnapshotExtraData {
    uint64_t vm_state_size_large;
    uint64_t disk_size;
} QCowSnapshotExtraData;


K
Kevin Wolf 已提交
147 148 149 150 151
typedef struct QCowSnapshot {
    uint64_t l1_table_offset;
    uint32_t l1_size;
    char *id_str;
    char *name;
152
    uint64_t disk_size;
K
Kevin Wolf 已提交
153
    uint64_t vm_state_size;
K
Kevin Wolf 已提交
154 155 156 157 158
    uint32_t date_sec;
    uint32_t date_nsec;
    uint64_t vm_clock_nsec;
} QCowSnapshot;

K
Kevin Wolf 已提交
159 160 161
struct Qcow2Cache;
typedef struct Qcow2Cache Qcow2Cache;

162 163 164 165 166 167 168
typedef struct Qcow2UnknownHeaderExtension {
    uint32_t magic;
    uint32_t len;
    QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
    uint8_t data[];
} Qcow2UnknownHeaderExtension;

169 170 171 172 173 174
enum {
    QCOW2_FEAT_TYPE_INCOMPATIBLE    = 0,
    QCOW2_FEAT_TYPE_COMPATIBLE      = 1,
    QCOW2_FEAT_TYPE_AUTOCLEAR       = 2,
};

S
Stefan Hajnoczi 已提交
175 176 177
/* Incompatible feature bits */
enum {
    QCOW2_INCOMPAT_DIRTY_BITNR   = 0,
M
Max Reitz 已提交
178
    QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
S
Stefan Hajnoczi 已提交
179
    QCOW2_INCOMPAT_DIRTY         = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
M
Max Reitz 已提交
180
    QCOW2_INCOMPAT_CORRUPT       = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
S
Stefan Hajnoczi 已提交
181

M
Max Reitz 已提交
182 183
    QCOW2_INCOMPAT_MASK          = QCOW2_INCOMPAT_DIRTY
                                 | QCOW2_INCOMPAT_CORRUPT,
S
Stefan Hajnoczi 已提交
184 185
};

186 187 188 189 190 191 192 193
/* Compatible feature bits */
enum {
    QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
    QCOW2_COMPAT_LAZY_REFCOUNTS       = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,

    QCOW2_COMPAT_FEAT_MASK            = QCOW2_COMPAT_LAZY_REFCOUNTS,
};

194 195 196 197 198 199 200 201 202
enum qcow2_discard_type {
    QCOW2_DISCARD_NEVER = 0,
    QCOW2_DISCARD_ALWAYS,
    QCOW2_DISCARD_REQUEST,
    QCOW2_DISCARD_SNAPSHOT,
    QCOW2_DISCARD_OTHER,
    QCOW2_DISCARD_MAX
};

203 204 205 206 207 208
typedef struct Qcow2Feature {
    uint8_t type;
    uint8_t bit;
    char    name[46];
} QEMU_PACKED Qcow2Feature;

K
Kevin Wolf 已提交
209 210 211 212 213 214 215
typedef struct Qcow2DiscardRegion {
    BlockDriverState *bs;
    uint64_t offset;
    uint64_t bytes;
    QTAILQ_ENTRY(Qcow2DiscardRegion) next;
} Qcow2DiscardRegion;

K
Kevin Wolf 已提交
216 217 218 219 220 221 222 223
typedef struct BDRVQcowState {
    int cluster_bits;
    int cluster_size;
    int cluster_sectors;
    int l2_bits;
    int l2_size;
    int l1_size;
    int l1_vm_state_index;
224 225
    int refcount_block_bits;
    int refcount_block_size;
K
Kevin Wolf 已提交
226 227 228 229 230
    int csize_shift;
    int csize_mask;
    uint64_t cluster_offset_mask;
    uint64_t l1_table_offset;
    uint64_t *l1_table;
K
Kevin Wolf 已提交
231 232 233 234

    Qcow2Cache* l2_table_cache;
    Qcow2Cache* refcount_block_cache;

K
Kevin Wolf 已提交
235 236 237
    uint8_t *cluster_cache;
    uint8_t *cluster_data;
    uint64_t cluster_cache_offset;
B
Blue Swirl 已提交
238
    QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
K
Kevin Wolf 已提交
239 240 241 242

    uint64_t *refcount_table;
    uint64_t refcount_table_offset;
    uint32_t refcount_table_size;
243 244
    uint64_t free_cluster_index;
    uint64_t free_byte_offset;
K
Kevin Wolf 已提交
245

K
Kevin Wolf 已提交
246 247
    CoMutex lock;

K
Kevin Wolf 已提交
248 249 250 251 252 253
    uint32_t crypt_method; /* current crypt method, 0 if no key yet */
    uint32_t crypt_method_header;
    AES_KEY aes_encrypt_key;
    AES_KEY aes_decrypt_key;
    uint64_t snapshots_offset;
    int snapshots_size;
254
    unsigned int nb_snapshots;
K
Kevin Wolf 已提交
255
    QCowSnapshot *snapshots;
256 257

    int flags;
K
Kevin Wolf 已提交
258
    int qcow_version;
259
    bool use_lazy_refcounts;
260
    int refcount_order;
K
Kevin Wolf 已提交
261

262 263
    bool discard_passthrough[QCOW2_DISCARD_MAX];

264
    int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
M
Max Reitz 已提交
265
    bool signaled_corruption;
266

K
Kevin Wolf 已提交
267 268 269 270 271 272
    uint64_t incompatible_features;
    uint64_t compatible_features;
    uint64_t autoclear_features;

    size_t unknown_header_fields_size;
    void* unknown_header_fields;
273
    QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
K
Kevin Wolf 已提交
274 275
    QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
    bool cache_discards;
K
Kevin Wolf 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288
} BDRVQcowState;

/* XXX: use std qcow open function ? */
typedef struct QCowCreateState {
    int cluster_size;
    int cluster_bits;
    uint16_t *refcount_block;
    uint64_t *refcount_table;
    int64_t l1_table_offset;
    int64_t refcount_table_offset;
    int64_t refcount_block_offset;
} QCowCreateState;

289 290
struct QCowAIOCB;

K
Kevin Wolf 已提交
291 292 293 294 295 296 297 298 299 300 301
typedef struct Qcow2COWRegion {
    /**
     * Offset of the COW region in bytes from the start of the first cluster
     * touched by the request.
     */
    uint64_t    offset;

    /** Number of sectors to copy */
    int         nb_sectors;
} Qcow2COWRegion;

302 303 304 305
/**
 * Describes an in-flight (part of a) write request that writes to clusters
 * that are not referenced in their L2 table yet.
 */
306 307
typedef struct QCowL2Meta
{
308
    /** Guest offset of the first newly allocated cluster */
309
    uint64_t offset;
310 311

    /** Host offset of the first newly allocated cluster */
312
    uint64_t alloc_offset;
313 314 315 316 317

    /**
     * Number of sectors from the start of the first allocated cluster to
     * the end of the (possibly shortened) request
     */
318
    int nb_available;
319 320

    /** Number of newly allocated clusters */
321
    int nb_clusters;
322 323 324 325 326

    /**
     * Requests that overlap with this allocation and wait to be restarted
     * when the allocating request has completed.
     */
K
Kevin Wolf 已提交
327
    CoQueue dependent_requests;
328

K
Kevin Wolf 已提交
329 330 331 332 333 334 335 336 337 338 339 340
    /**
     * The COW Region between the start of the first allocated cluster and the
     * area the guest actually writes to.
     */
    Qcow2COWRegion cow_start;

    /**
     * The COW Region between the area the guest actually writes to and the
     * end of the last allocated cluster.
     */
    Qcow2COWRegion cow_end;

341 342 343
    /** Pointer to next L2Meta of the same write request */
    struct QCowL2Meta *next;

B
Blue Swirl 已提交
344
    QLIST_ENTRY(QCowL2Meta) next_in_flight;
345 346
} QCowL2Meta;

347 348 349 350
enum {
    QCOW2_CLUSTER_UNALLOCATED,
    QCOW2_CLUSTER_NORMAL,
    QCOW2_CLUSTER_COMPRESSED,
351
    QCOW2_CLUSTER_ZERO
352 353
};

M
Max Reitz 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
typedef enum QCow2MetadataOverlap {
    QCOW2_OL_MAIN_HEADER_BITNR    = 0,
    QCOW2_OL_ACTIVE_L1_BITNR      = 1,
    QCOW2_OL_ACTIVE_L2_BITNR      = 2,
    QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
    QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
    QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
    QCOW2_OL_INACTIVE_L1_BITNR    = 6,
    QCOW2_OL_INACTIVE_L2_BITNR    = 7,

    QCOW2_OL_MAX_BITNR            = 8,

    QCOW2_OL_NONE           = 0,
    QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
    QCOW2_OL_ACTIVE_L1      = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
    QCOW2_OL_ACTIVE_L2      = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
    QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
    QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
    QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
    QCOW2_OL_INACTIVE_L1    = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
    /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
     * reads. */
    QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
} QCow2MetadataOverlap;

379 380 381 382 383
/* Perform all overlap checks which can be done in constant time */
#define QCOW2_OL_CONSTANT \
    (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
     QCOW2_OL_SNAPSHOT_TABLE)

M
Max Reitz 已提交
384 385
/* Perform all overlap checks which don't require disk access */
#define QCOW2_OL_CACHED \
386 387 388 389 390 391
    (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
     QCOW2_OL_INACTIVE_L1)

/* Perform all overlap checks */
#define QCOW2_OL_ALL \
    (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
M
Max Reitz 已提交
392

393 394
#define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
#define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
395 396
#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL

397
#define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
398

399 400 401 402 403
static inline int64_t start_of_cluster(BDRVQcowState *s, int64_t offset)
{
    return offset & ~(s->cluster_size - 1);
}

404 405 406 407 408
static inline int64_t offset_into_cluster(BDRVQcowState *s, int64_t offset)
{
    return offset & (s->cluster_size - 1);
}

409
static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
K
Kevin Wolf 已提交
410 411 412 413
{
    return (size + (s->cluster_size - 1)) >> s->cluster_bits;
}

414
static inline int64_t size_to_l1(BDRVQcowState *s, int64_t size)
415 416 417 418 419
{
    int shift = s->cluster_bits + s->l2_bits;
    return (size + (1ULL << shift) - 1) >> shift;
}

K
Kevin Wolf 已提交
420 421 422 423 424
static inline int offset_to_l2_index(BDRVQcowState *s, int64_t offset)
{
    return (offset >> s->cluster_bits) & (s->l2_size - 1);
}

K
Kevin Wolf 已提交
425 426 427 428 429 430
static inline int64_t align_offset(int64_t offset, int n)
{
    offset = (offset + n - 1) & ~(n - 1);
    return offset;
}

431 432 433 434 435
static inline int64_t qcow2_vm_state_offset(BDRVQcowState *s)
{
    return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
}

436 437 438 439 440
static inline uint64_t qcow2_max_refcount_clusters(BDRVQcowState *s)
{
    return QCOW_MAX_REFTABLE_SIZE >> s->cluster_bits;
}

441 442 443 444
static inline int qcow2_get_cluster_type(uint64_t l2_entry)
{
    if (l2_entry & QCOW_OFLAG_COMPRESSED) {
        return QCOW2_CLUSTER_COMPRESSED;
445 446
    } else if (l2_entry & QCOW_OFLAG_ZERO) {
        return QCOW2_CLUSTER_ZERO;
447 448 449 450 451 452 453
    } else if (!(l2_entry & L2E_OFFSET_MASK)) {
        return QCOW2_CLUSTER_UNALLOCATED;
    } else {
        return QCOW2_CLUSTER_NORMAL;
    }
}

454 455 456 457 458
/* Check whether refcounts are eager or lazy */
static inline bool qcow2_need_accurate_refcounts(BDRVQcowState *s)
{
    return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
}
K
Kevin Wolf 已提交
459

460 461 462 463 464 465 466 467 468 469 470
static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
{
    return m->offset + m->cow_start.offset;
}

static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
{
    return m->offset + m->cow_end.offset
        + (m->cow_end.nb_sectors << BDRV_SECTOR_BITS);
}

K
Kevin Wolf 已提交
471 472 473
// FIXME Need qcow2_ prefix to global functions

/* qcow2.c functions */
474 475
int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
                  int64_t sector_num, int nb_sectors);
476 477

int qcow2_mark_dirty(BlockDriverState *bs);
M
Max Reitz 已提交
478 479
int qcow2_mark_corrupt(BlockDriverState *bs);
int qcow2_mark_consistent(BlockDriverState *bs);
K
Kevin Wolf 已提交
480
int qcow2_update_header(BlockDriverState *bs);
K
Kevin Wolf 已提交
481

M
Max Reitz 已提交
482 483 484 485
void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
                             int64_t size, const char *message_format, ...)
                             GCC_FMT_ATTR(5, 6);

K
Kevin Wolf 已提交
486
/* qcow2-refcount.c functions */
K
Kevin Wolf 已提交
487 488
int qcow2_refcount_init(BlockDriverState *bs);
void qcow2_refcount_close(BlockDriverState *bs);
K
Kevin Wolf 已提交
489

490 491
int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index);

M
Max Reitz 已提交
492 493 494
int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
                                  int addend, enum qcow2_discard_type type);

495
int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
496 497
int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
    int nb_clusters);
K
Kevin Wolf 已提交
498 499
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
void qcow2_free_clusters(BlockDriverState *bs,
500 501 502 503
                          int64_t offset, int64_t size,
                          enum qcow2_discard_type type);
void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
                             int nb_clusters, enum qcow2_discard_type type);
K
Kevin Wolf 已提交
504

K
Kevin Wolf 已提交
505 506
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
    int64_t l1_table_offset, int l1_size, int addend);
K
Kevin Wolf 已提交
507

508 509
int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
                          BdrvCheckMode fix);
K
Kevin Wolf 已提交
510

K
Kevin Wolf 已提交
511 512
void qcow2_process_discards(BlockDriverState *bs, int ret);

M
Max Reitz 已提交
513
int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
M
Max Reitz 已提交
514
                                 int64_t size);
M
Max Reitz 已提交
515
int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
M
Max Reitz 已提交
516 517
                                  int64_t size);

518
/* qcow2-cluster.c functions */
519 520
int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
                        bool exact_size);
521
int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
K
Kevin Wolf 已提交
522
void qcow2_l2_cache_reset(BlockDriverState *bs);
523
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
K
Kevin Wolf 已提交
524
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
525 526 527 528
                     uint8_t *out_buf, const uint8_t *in_buf,
                     int nb_sectors, int enc,
                     const AES_KEY *key);

529 530
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
    int *num, uint64_t *cluster_offset);
K
Kevin Wolf 已提交
531
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
532
    int *num, uint64_t *host_offset, QCowL2Meta **m);
K
Kevin Wolf 已提交
533
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
534 535 536
                                         uint64_t offset,
                                         int compressed_size);

537
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
K
Kevin Wolf 已提交
538
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
M
Max Reitz 已提交
539
    int nb_sectors, enum qcow2_discard_type type, bool full_discard);
K
Kevin Wolf 已提交
540
int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
541

542 543
int qcow2_expand_zero_clusters(BlockDriverState *bs,
                               BlockDriverAmendStatusCB *status_cb);
M
Max Reitz 已提交
544

K
Kevin Wolf 已提交
545
/* qcow2-snapshot.c functions */
K
Kevin Wolf 已提交
546 547
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
548 549 550 551
int qcow2_snapshot_delete(BlockDriverState *bs,
                          const char *snapshot_id,
                          const char *name,
                          Error **errp);
K
Kevin Wolf 已提交
552
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
553 554 555 556
int qcow2_snapshot_load_tmp(BlockDriverState *bs,
                            const char *snapshot_id,
                            const char *name,
                            Error **errp);
K
Kevin Wolf 已提交
557

K
Kevin Wolf 已提交
558 559
void qcow2_free_snapshots(BlockDriverState *bs);
int qcow2_read_snapshots(BlockDriverState *bs);
K
Kevin Wolf 已提交
560

K
Kevin Wolf 已提交
561
/* qcow2-cache.c functions */
562
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
K
Kevin Wolf 已提交
563 564 565 566 567 568
int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);

void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
    Qcow2Cache *dependency);
K
Kevin Wolf 已提交
569
void qcow2_cache_depends_on_flush(Qcow2Cache *c);
K
Kevin Wolf 已提交
570

M
Max Reitz 已提交
571 572
int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);

K
Kevin Wolf 已提交
573 574 575 576 577 578
int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
    void **table);
int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
    void **table);
int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);

K
Kevin Wolf 已提交
579
#endif