Statistics
| Branch: | Revision:

root / block / qcow2.h @ f7d0fe02

History | View | Annotate | Download (4.6 kB)

1 f7d0fe02 Kevin Wolf
/*
2 f7d0fe02 Kevin Wolf
 * Block driver for the QCOW version 2 format
3 f7d0fe02 Kevin Wolf
 *
4 f7d0fe02 Kevin Wolf
 * Copyright (c) 2004-2006 Fabrice Bellard
5 f7d0fe02 Kevin Wolf
 *
6 f7d0fe02 Kevin Wolf
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 f7d0fe02 Kevin Wolf
 * of this software and associated documentation files (the "Software"), to deal
8 f7d0fe02 Kevin Wolf
 * in the Software without restriction, including without limitation the rights
9 f7d0fe02 Kevin Wolf
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 f7d0fe02 Kevin Wolf
 * copies of the Software, and to permit persons to whom the Software is
11 f7d0fe02 Kevin Wolf
 * furnished to do so, subject to the following conditions:
12 f7d0fe02 Kevin Wolf
 *
13 f7d0fe02 Kevin Wolf
 * The above copyright notice and this permission notice shall be included in
14 f7d0fe02 Kevin Wolf
 * all copies or substantial portions of the Software.
15 f7d0fe02 Kevin Wolf
 *
16 f7d0fe02 Kevin Wolf
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 f7d0fe02 Kevin Wolf
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 f7d0fe02 Kevin Wolf
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 f7d0fe02 Kevin Wolf
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 f7d0fe02 Kevin Wolf
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 f7d0fe02 Kevin Wolf
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 f7d0fe02 Kevin Wolf
 * THE SOFTWARE.
23 f7d0fe02 Kevin Wolf
 */
24 f7d0fe02 Kevin Wolf
25 f7d0fe02 Kevin Wolf
#ifndef BLOCK_QCOW2_H
26 f7d0fe02 Kevin Wolf
#define BLOCK_QCOW2_H
27 f7d0fe02 Kevin Wolf
28 f7d0fe02 Kevin Wolf
#include "aes.h"
29 f7d0fe02 Kevin Wolf
30 f7d0fe02 Kevin Wolf
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
31 f7d0fe02 Kevin Wolf
#define QCOW_VERSION 2
32 f7d0fe02 Kevin Wolf
33 f7d0fe02 Kevin Wolf
#define QCOW_CRYPT_NONE 0
34 f7d0fe02 Kevin Wolf
#define QCOW_CRYPT_AES  1
35 f7d0fe02 Kevin Wolf
36 f7d0fe02 Kevin Wolf
#define QCOW_MAX_CRYPT_CLUSTERS 32
37 f7d0fe02 Kevin Wolf
38 f7d0fe02 Kevin Wolf
/* indicate that the refcount of the referenced cluster is exactly one. */
39 f7d0fe02 Kevin Wolf
#define QCOW_OFLAG_COPIED     (1LL << 63)
40 f7d0fe02 Kevin Wolf
/* indicate that the cluster is compressed (they never have the copied flag) */
41 f7d0fe02 Kevin Wolf
#define QCOW_OFLAG_COMPRESSED (1LL << 62)
42 f7d0fe02 Kevin Wolf
43 f7d0fe02 Kevin Wolf
#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
44 f7d0fe02 Kevin Wolf
45 f7d0fe02 Kevin Wolf
#define MIN_CLUSTER_BITS 9
46 f7d0fe02 Kevin Wolf
#define MAX_CLUSTER_BITS 16
47 f7d0fe02 Kevin Wolf
48 f7d0fe02 Kevin Wolf
#define L2_CACHE_SIZE 16
49 f7d0fe02 Kevin Wolf
50 f7d0fe02 Kevin Wolf
typedef struct QCowHeader {
51 f7d0fe02 Kevin Wolf
    uint32_t magic;
52 f7d0fe02 Kevin Wolf
    uint32_t version;
53 f7d0fe02 Kevin Wolf
    uint64_t backing_file_offset;
54 f7d0fe02 Kevin Wolf
    uint32_t backing_file_size;
55 f7d0fe02 Kevin Wolf
    uint32_t cluster_bits;
56 f7d0fe02 Kevin Wolf
    uint64_t size; /* in bytes */
57 f7d0fe02 Kevin Wolf
    uint32_t crypt_method;
58 f7d0fe02 Kevin Wolf
    uint32_t l1_size; /* XXX: save number of clusters instead ? */
59 f7d0fe02 Kevin Wolf
    uint64_t l1_table_offset;
60 f7d0fe02 Kevin Wolf
    uint64_t refcount_table_offset;
61 f7d0fe02 Kevin Wolf
    uint32_t refcount_table_clusters;
62 f7d0fe02 Kevin Wolf
    uint32_t nb_snapshots;
63 f7d0fe02 Kevin Wolf
    uint64_t snapshots_offset;
64 f7d0fe02 Kevin Wolf
} QCowHeader;
65 f7d0fe02 Kevin Wolf
66 f7d0fe02 Kevin Wolf
typedef struct QCowSnapshot {
67 f7d0fe02 Kevin Wolf
    uint64_t l1_table_offset;
68 f7d0fe02 Kevin Wolf
    uint32_t l1_size;
69 f7d0fe02 Kevin Wolf
    char *id_str;
70 f7d0fe02 Kevin Wolf
    char *name;
71 f7d0fe02 Kevin Wolf
    uint32_t vm_state_size;
72 f7d0fe02 Kevin Wolf
    uint32_t date_sec;
73 f7d0fe02 Kevin Wolf
    uint32_t date_nsec;
74 f7d0fe02 Kevin Wolf
    uint64_t vm_clock_nsec;
75 f7d0fe02 Kevin Wolf
} QCowSnapshot;
76 f7d0fe02 Kevin Wolf
77 f7d0fe02 Kevin Wolf
typedef struct BDRVQcowState {
78 f7d0fe02 Kevin Wolf
    BlockDriverState *hd;
79 f7d0fe02 Kevin Wolf
    int cluster_bits;
80 f7d0fe02 Kevin Wolf
    int cluster_size;
81 f7d0fe02 Kevin Wolf
    int cluster_sectors;
82 f7d0fe02 Kevin Wolf
    int l2_bits;
83 f7d0fe02 Kevin Wolf
    int l2_size;
84 f7d0fe02 Kevin Wolf
    int l1_size;
85 f7d0fe02 Kevin Wolf
    int l1_vm_state_index;
86 f7d0fe02 Kevin Wolf
    int csize_shift;
87 f7d0fe02 Kevin Wolf
    int csize_mask;
88 f7d0fe02 Kevin Wolf
    uint64_t cluster_offset_mask;
89 f7d0fe02 Kevin Wolf
    uint64_t l1_table_offset;
90 f7d0fe02 Kevin Wolf
    uint64_t *l1_table;
91 f7d0fe02 Kevin Wolf
    uint64_t *l2_cache;
92 f7d0fe02 Kevin Wolf
    uint64_t l2_cache_offsets[L2_CACHE_SIZE];
93 f7d0fe02 Kevin Wolf
    uint32_t l2_cache_counts[L2_CACHE_SIZE];
94 f7d0fe02 Kevin Wolf
    uint8_t *cluster_cache;
95 f7d0fe02 Kevin Wolf
    uint8_t *cluster_data;
96 f7d0fe02 Kevin Wolf
    uint64_t cluster_cache_offset;
97 f7d0fe02 Kevin Wolf
98 f7d0fe02 Kevin Wolf
    uint64_t *refcount_table;
99 f7d0fe02 Kevin Wolf
    uint64_t refcount_table_offset;
100 f7d0fe02 Kevin Wolf
    uint32_t refcount_table_size;
101 f7d0fe02 Kevin Wolf
    uint64_t refcount_block_cache_offset;
102 f7d0fe02 Kevin Wolf
    uint16_t *refcount_block_cache;
103 f7d0fe02 Kevin Wolf
    int64_t free_cluster_index;
104 f7d0fe02 Kevin Wolf
    int64_t free_byte_offset;
105 f7d0fe02 Kevin Wolf
106 f7d0fe02 Kevin Wolf
    uint32_t crypt_method; /* current crypt method, 0 if no key yet */
107 f7d0fe02 Kevin Wolf
    uint32_t crypt_method_header;
108 f7d0fe02 Kevin Wolf
    AES_KEY aes_encrypt_key;
109 f7d0fe02 Kevin Wolf
    AES_KEY aes_decrypt_key;
110 f7d0fe02 Kevin Wolf
    uint64_t snapshots_offset;
111 f7d0fe02 Kevin Wolf
    int snapshots_size;
112 f7d0fe02 Kevin Wolf
    int nb_snapshots;
113 f7d0fe02 Kevin Wolf
    QCowSnapshot *snapshots;
114 f7d0fe02 Kevin Wolf
} BDRVQcowState;
115 f7d0fe02 Kevin Wolf
116 f7d0fe02 Kevin Wolf
/* XXX: use std qcow open function ? */
117 f7d0fe02 Kevin Wolf
typedef struct QCowCreateState {
118 f7d0fe02 Kevin Wolf
    int cluster_size;
119 f7d0fe02 Kevin Wolf
    int cluster_bits;
120 f7d0fe02 Kevin Wolf
    uint16_t *refcount_block;
121 f7d0fe02 Kevin Wolf
    uint64_t *refcount_table;
122 f7d0fe02 Kevin Wolf
    int64_t l1_table_offset;
123 f7d0fe02 Kevin Wolf
    int64_t refcount_table_offset;
124 f7d0fe02 Kevin Wolf
    int64_t refcount_block_offset;
125 f7d0fe02 Kevin Wolf
} QCowCreateState;
126 f7d0fe02 Kevin Wolf
127 f7d0fe02 Kevin Wolf
static int size_to_clusters(BDRVQcowState *s, int64_t size)
128 f7d0fe02 Kevin Wolf
{
129 f7d0fe02 Kevin Wolf
    return (size + (s->cluster_size - 1)) >> s->cluster_bits;
130 f7d0fe02 Kevin Wolf
}
131 f7d0fe02 Kevin Wolf
132 f7d0fe02 Kevin Wolf
// FIXME Need qcow2_ prefix to global functions
133 f7d0fe02 Kevin Wolf
134 f7d0fe02 Kevin Wolf
/* qcow2.c functions */
135 f7d0fe02 Kevin Wolf
void l2_cache_reset(BlockDriverState *bs);
136 f7d0fe02 Kevin Wolf
137 f7d0fe02 Kevin Wolf
/* qcow2-refcount.c functions */
138 f7d0fe02 Kevin Wolf
int refcount_init(BlockDriverState *bs);
139 f7d0fe02 Kevin Wolf
void refcount_close(BlockDriverState *bs);
140 f7d0fe02 Kevin Wolf
141 f7d0fe02 Kevin Wolf
int64_t alloc_clusters(BlockDriverState *bs, int64_t size);
142 f7d0fe02 Kevin Wolf
int64_t alloc_bytes(BlockDriverState *bs, int size);
143 f7d0fe02 Kevin Wolf
void free_clusters(BlockDriverState *bs,
144 f7d0fe02 Kevin Wolf
                          int64_t offset, int64_t size);
145 f7d0fe02 Kevin Wolf
146 f7d0fe02 Kevin Wolf
void create_refcount_update(QCowCreateState *s, int64_t offset, int64_t size);
147 f7d0fe02 Kevin Wolf
int update_snapshot_refcount(BlockDriverState *bs,
148 f7d0fe02 Kevin Wolf
                             int64_t l1_table_offset,
149 f7d0fe02 Kevin Wolf
                             int l1_size,
150 f7d0fe02 Kevin Wolf
                             int addend);
151 f7d0fe02 Kevin Wolf
152 f7d0fe02 Kevin Wolf
int check_refcounts(BlockDriverState *bs);
153 f7d0fe02 Kevin Wolf
154 f7d0fe02 Kevin Wolf
#endif