Statistics
| Branch: | Revision:

root / block / qcow2.h @ 49381094

History | View | Annotate | Download (7.7 kB)

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
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24

    
25
#ifndef BLOCK_QCOW2_H
26
#define BLOCK_QCOW2_H
27

    
28
#include "aes.h"
29

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

    
34
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
35
#define QCOW_VERSION 2
36

    
37
#define QCOW_CRYPT_NONE 0
38
#define QCOW_CRYPT_AES  1
39

    
40
#define QCOW_MAX_CRYPT_CLUSTERS 32
41

    
42
/* indicate that the refcount of the referenced cluster is exactly one. */
43
#define QCOW_OFLAG_COPIED     (1LL << 63)
44
/* indicate that the cluster is compressed (they never have the copied flag) */
45
#define QCOW_OFLAG_COMPRESSED (1LL << 62)
46

    
47
#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
48

    
49
#define MIN_CLUSTER_BITS 9
50
#define MAX_CLUSTER_BITS 21
51

    
52
#define L2_CACHE_SIZE 16
53

    
54
typedef struct QCowHeader {
55
    uint32_t magic;
56
    uint32_t version;
57
    uint64_t backing_file_offset;
58
    uint32_t backing_file_size;
59
    uint32_t cluster_bits;
60
    uint64_t size; /* in bytes */
61
    uint32_t crypt_method;
62
    uint32_t l1_size; /* XXX: save number of clusters instead ? */
63
    uint64_t l1_table_offset;
64
    uint64_t refcount_table_offset;
65
    uint32_t refcount_table_clusters;
66
    uint32_t nb_snapshots;
67
    uint64_t snapshots_offset;
68
} QCowHeader;
69

    
70
typedef struct QCowSnapshot {
71
    uint64_t l1_table_offset;
72
    uint32_t l1_size;
73
    char *id_str;
74
    char *name;
75
    uint32_t vm_state_size;
76
    uint32_t date_sec;
77
    uint32_t date_nsec;
78
    uint64_t vm_clock_nsec;
79
} QCowSnapshot;
80

    
81
struct Qcow2Cache;
82
typedef struct Qcow2Cache Qcow2Cache;
83

    
84
typedef struct BDRVQcowState {
85
    int cluster_bits;
86
    int cluster_size;
87
    int cluster_sectors;
88
    int l2_bits;
89
    int l2_size;
90
    int l1_size;
91
    int l1_vm_state_index;
92
    int csize_shift;
93
    int csize_mask;
94
    uint64_t cluster_offset_mask;
95
    uint64_t l1_table_offset;
96
    uint64_t *l1_table;
97
    uint64_t *l2_cache;
98
    uint64_t l2_cache_offsets[L2_CACHE_SIZE];
99
    uint32_t l2_cache_counts[L2_CACHE_SIZE];
100
    uint8_t *cluster_cache;
101
    uint8_t *cluster_data;
102
    uint64_t cluster_cache_offset;
103
    QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
104

    
105
    uint64_t *refcount_table;
106
    uint64_t refcount_table_offset;
107
    uint32_t refcount_table_size;
108
    uint64_t refcount_block_cache_offset;
109
    uint16_t *refcount_block_cache;
110
    int64_t free_cluster_index;
111
    int64_t free_byte_offset;
112

    
113
    uint32_t crypt_method; /* current crypt method, 0 if no key yet */
114
    uint32_t crypt_method_header;
115
    AES_KEY aes_encrypt_key;
116
    AES_KEY aes_decrypt_key;
117
    uint64_t snapshots_offset;
118
    int snapshots_size;
119
    int nb_snapshots;
120
    QCowSnapshot *snapshots;
121
} BDRVQcowState;
122

    
123
/* XXX: use std qcow open function ? */
124
typedef struct QCowCreateState {
125
    int cluster_size;
126
    int cluster_bits;
127
    uint16_t *refcount_block;
128
    uint64_t *refcount_table;
129
    int64_t l1_table_offset;
130
    int64_t refcount_table_offset;
131
    int64_t refcount_block_offset;
132
} QCowCreateState;
133

    
134
struct QCowAIOCB;
135

    
136
/* XXX This could be private for qcow2-cluster.c */
137
typedef struct QCowL2Meta
138
{
139
    uint64_t offset;
140
    uint64_t cluster_offset;
141
    int n_start;
142
    int nb_available;
143
    int nb_clusters;
144
    struct QCowL2Meta *depends_on;
145
    QLIST_HEAD(QCowAioDependencies, QCowAIOCB) dependent_requests;
146

    
147
    QLIST_ENTRY(QCowL2Meta) next_in_flight;
148
} QCowL2Meta;
149

    
150
static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
151
{
152
    return (size + (s->cluster_size - 1)) >> s->cluster_bits;
153
}
154

    
155
static inline int size_to_l1(BDRVQcowState *s, int64_t size)
156
{
157
    int shift = s->cluster_bits + s->l2_bits;
158
    return (size + (1ULL << shift) - 1) >> shift;
159
}
160

    
161
static inline int64_t align_offset(int64_t offset, int n)
162
{
163
    offset = (offset + n - 1) & ~(n - 1);
164
    return offset;
165
}
166

    
167

    
168
// FIXME Need qcow2_ prefix to global functions
169

    
170
/* qcow2.c functions */
171
int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
172
                  int64_t sector_num, int nb_sectors);
173

    
174
/* qcow2-refcount.c functions */
175
int qcow2_refcount_init(BlockDriverState *bs);
176
void qcow2_refcount_close(BlockDriverState *bs);
177

    
178
int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
179
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
180
void qcow2_free_clusters(BlockDriverState *bs,
181
    int64_t offset, int64_t size);
182
void qcow2_free_any_clusters(BlockDriverState *bs,
183
    uint64_t cluster_offset, int nb_clusters);
184

    
185
void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
186
    int64_t size);
187
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
188
    int64_t l1_table_offset, int l1_size, int addend);
189

    
190
int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res);
191

    
192
/* qcow2-cluster.c functions */
193
int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
194
void qcow2_l2_cache_reset(BlockDriverState *bs);
195
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
196
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
197
                     uint8_t *out_buf, const uint8_t *in_buf,
198
                     int nb_sectors, int enc,
199
                     const AES_KEY *key);
200

    
201
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
202
    int *num, uint64_t *cluster_offset);
203
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
204
    int n_start, int n_end, int *num, QCowL2Meta *m);
205
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
206
                                         uint64_t offset,
207
                                         int compressed_size);
208

    
209
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
210

    
211
/* qcow2-snapshot.c functions */
212
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
213
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
214
int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
215
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
216
int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
217

    
218
void qcow2_free_snapshots(BlockDriverState *bs);
219
int qcow2_read_snapshots(BlockDriverState *bs);
220

    
221
/* qcow2-cache.c functions */
222
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
223
    bool writethrough);
224
int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
225

    
226
void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
227
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
228
int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
229
    Qcow2Cache *dependency);
230

    
231
int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
232
    void **table);
233
int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
234
    void **table);
235
int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
236

    
237
#endif