Statistics
| Branch: | Revision:

root / block / qcow2.h @ 1e5b9d2f

History | View | Annotate | Download (6.8 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 14899cdf Filip Navara
//#define DEBUG_ALLOC
31 14899cdf Filip Navara
//#define DEBUG_ALLOC2
32 14899cdf Filip Navara
//#define DEBUG_EXT
33 14899cdf Filip Navara
34 f7d0fe02 Kevin Wolf
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
35 f7d0fe02 Kevin Wolf
#define QCOW_VERSION 2
36 f7d0fe02 Kevin Wolf
37 f7d0fe02 Kevin Wolf
#define QCOW_CRYPT_NONE 0
38 f7d0fe02 Kevin Wolf
#define QCOW_CRYPT_AES  1
39 f7d0fe02 Kevin Wolf
40 f7d0fe02 Kevin Wolf
#define QCOW_MAX_CRYPT_CLUSTERS 32
41 f7d0fe02 Kevin Wolf
42 f7d0fe02 Kevin Wolf
/* indicate that the refcount of the referenced cluster is exactly one. */
43 f7d0fe02 Kevin Wolf
#define QCOW_OFLAG_COPIED     (1LL << 63)
44 f7d0fe02 Kevin Wolf
/* indicate that the cluster is compressed (they never have the copied flag) */
45 f7d0fe02 Kevin Wolf
#define QCOW_OFLAG_COMPRESSED (1LL << 62)
46 f7d0fe02 Kevin Wolf
47 f7d0fe02 Kevin Wolf
#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
48 f7d0fe02 Kevin Wolf
49 f7d0fe02 Kevin Wolf
#define MIN_CLUSTER_BITS 9
50 80ee15a6 Kevin Wolf
#define MAX_CLUSTER_BITS 21
51 f7d0fe02 Kevin Wolf
52 f7d0fe02 Kevin Wolf
#define L2_CACHE_SIZE 16
53 f7d0fe02 Kevin Wolf
54 f7d0fe02 Kevin Wolf
typedef struct QCowHeader {
55 f7d0fe02 Kevin Wolf
    uint32_t magic;
56 f7d0fe02 Kevin Wolf
    uint32_t version;
57 f7d0fe02 Kevin Wolf
    uint64_t backing_file_offset;
58 f7d0fe02 Kevin Wolf
    uint32_t backing_file_size;
59 f7d0fe02 Kevin Wolf
    uint32_t cluster_bits;
60 f7d0fe02 Kevin Wolf
    uint64_t size; /* in bytes */
61 f7d0fe02 Kevin Wolf
    uint32_t crypt_method;
62 f7d0fe02 Kevin Wolf
    uint32_t l1_size; /* XXX: save number of clusters instead ? */
63 f7d0fe02 Kevin Wolf
    uint64_t l1_table_offset;
64 f7d0fe02 Kevin Wolf
    uint64_t refcount_table_offset;
65 f7d0fe02 Kevin Wolf
    uint32_t refcount_table_clusters;
66 f7d0fe02 Kevin Wolf
    uint32_t nb_snapshots;
67 f7d0fe02 Kevin Wolf
    uint64_t snapshots_offset;
68 f7d0fe02 Kevin Wolf
} QCowHeader;
69 f7d0fe02 Kevin Wolf
70 f7d0fe02 Kevin Wolf
typedef struct QCowSnapshot {
71 f7d0fe02 Kevin Wolf
    uint64_t l1_table_offset;
72 f7d0fe02 Kevin Wolf
    uint32_t l1_size;
73 f7d0fe02 Kevin Wolf
    char *id_str;
74 f7d0fe02 Kevin Wolf
    char *name;
75 f7d0fe02 Kevin Wolf
    uint32_t vm_state_size;
76 f7d0fe02 Kevin Wolf
    uint32_t date_sec;
77 f7d0fe02 Kevin Wolf
    uint32_t date_nsec;
78 f7d0fe02 Kevin Wolf
    uint64_t vm_clock_nsec;
79 f7d0fe02 Kevin Wolf
} QCowSnapshot;
80 f7d0fe02 Kevin Wolf
81 f7d0fe02 Kevin Wolf
typedef struct BDRVQcowState {
82 f7d0fe02 Kevin Wolf
    BlockDriverState *hd;
83 f7d0fe02 Kevin Wolf
    int cluster_bits;
84 f7d0fe02 Kevin Wolf
    int cluster_size;
85 f7d0fe02 Kevin Wolf
    int cluster_sectors;
86 f7d0fe02 Kevin Wolf
    int l2_bits;
87 f7d0fe02 Kevin Wolf
    int l2_size;
88 f7d0fe02 Kevin Wolf
    int l1_size;
89 f7d0fe02 Kevin Wolf
    int l1_vm_state_index;
90 f7d0fe02 Kevin Wolf
    int csize_shift;
91 f7d0fe02 Kevin Wolf
    int csize_mask;
92 f7d0fe02 Kevin Wolf
    uint64_t cluster_offset_mask;
93 f7d0fe02 Kevin Wolf
    uint64_t l1_table_offset;
94 f7d0fe02 Kevin Wolf
    uint64_t *l1_table;
95 f7d0fe02 Kevin Wolf
    uint64_t *l2_cache;
96 f7d0fe02 Kevin Wolf
    uint64_t l2_cache_offsets[L2_CACHE_SIZE];
97 f7d0fe02 Kevin Wolf
    uint32_t l2_cache_counts[L2_CACHE_SIZE];
98 f7d0fe02 Kevin Wolf
    uint8_t *cluster_cache;
99 f7d0fe02 Kevin Wolf
    uint8_t *cluster_data;
100 f7d0fe02 Kevin Wolf
    uint64_t cluster_cache_offset;
101 72cf2d4f Blue Swirl
    QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
102 f7d0fe02 Kevin Wolf
103 f7d0fe02 Kevin Wolf
    uint64_t *refcount_table;
104 f7d0fe02 Kevin Wolf
    uint64_t refcount_table_offset;
105 f7d0fe02 Kevin Wolf
    uint32_t refcount_table_size;
106 f7d0fe02 Kevin Wolf
    uint64_t refcount_block_cache_offset;
107 f7d0fe02 Kevin Wolf
    uint16_t *refcount_block_cache;
108 f7d0fe02 Kevin Wolf
    int64_t free_cluster_index;
109 f7d0fe02 Kevin Wolf
    int64_t free_byte_offset;
110 f7d0fe02 Kevin Wolf
111 f7d0fe02 Kevin Wolf
    uint32_t crypt_method; /* current crypt method, 0 if no key yet */
112 f7d0fe02 Kevin Wolf
    uint32_t crypt_method_header;
113 f7d0fe02 Kevin Wolf
    AES_KEY aes_encrypt_key;
114 f7d0fe02 Kevin Wolf
    AES_KEY aes_decrypt_key;
115 f7d0fe02 Kevin Wolf
    uint64_t snapshots_offset;
116 f7d0fe02 Kevin Wolf
    int snapshots_size;
117 f7d0fe02 Kevin Wolf
    int nb_snapshots;
118 f7d0fe02 Kevin Wolf
    QCowSnapshot *snapshots;
119 f7d0fe02 Kevin Wolf
} BDRVQcowState;
120 f7d0fe02 Kevin Wolf
121 f7d0fe02 Kevin Wolf
/* XXX: use std qcow open function ? */
122 f7d0fe02 Kevin Wolf
typedef struct QCowCreateState {
123 f7d0fe02 Kevin Wolf
    int cluster_size;
124 f7d0fe02 Kevin Wolf
    int cluster_bits;
125 f7d0fe02 Kevin Wolf
    uint16_t *refcount_block;
126 f7d0fe02 Kevin Wolf
    uint64_t *refcount_table;
127 f7d0fe02 Kevin Wolf
    int64_t l1_table_offset;
128 f7d0fe02 Kevin Wolf
    int64_t refcount_table_offset;
129 f7d0fe02 Kevin Wolf
    int64_t refcount_block_offset;
130 f7d0fe02 Kevin Wolf
} QCowCreateState;
131 f7d0fe02 Kevin Wolf
132 f214978a Kevin Wolf
struct QCowAIOCB;
133 f214978a Kevin Wolf
134 45aba42f Kevin Wolf
/* XXX This could be private for qcow2-cluster.c */
135 45aba42f Kevin Wolf
typedef struct QCowL2Meta
136 45aba42f Kevin Wolf
{
137 45aba42f Kevin Wolf
    uint64_t offset;
138 45aba42f Kevin Wolf
    int n_start;
139 45aba42f Kevin Wolf
    int nb_available;
140 45aba42f Kevin Wolf
    int nb_clusters;
141 f214978a Kevin Wolf
    struct QCowL2Meta *depends_on;
142 72cf2d4f Blue Swirl
    QLIST_HEAD(QCowAioDependencies, QCowAIOCB) dependent_requests;
143 f214978a Kevin Wolf
144 72cf2d4f Blue Swirl
    QLIST_ENTRY(QCowL2Meta) next_in_flight;
145 45aba42f Kevin Wolf
} QCowL2Meta;
146 45aba42f Kevin Wolf
147 45aba42f Kevin Wolf
static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
148 f7d0fe02 Kevin Wolf
{
149 f7d0fe02 Kevin Wolf
    return (size + (s->cluster_size - 1)) >> s->cluster_bits;
150 f7d0fe02 Kevin Wolf
}
151 f7d0fe02 Kevin Wolf
152 c142442b Kevin Wolf
static inline int64_t align_offset(int64_t offset, int n)
153 c142442b Kevin Wolf
{
154 c142442b Kevin Wolf
    offset = (offset + n - 1) & ~(n - 1);
155 c142442b Kevin Wolf
    return offset;
156 c142442b Kevin Wolf
}
157 c142442b Kevin Wolf
158 c142442b Kevin Wolf
159 f7d0fe02 Kevin Wolf
// FIXME Need qcow2_ prefix to global functions
160 f7d0fe02 Kevin Wolf
161 f7d0fe02 Kevin Wolf
/* qcow2.c functions */
162 ed6ccf0f Kevin Wolf
int qcow2_backing_read1(BlockDriverState *bs,
163 45aba42f Kevin Wolf
                  int64_t sector_num, uint8_t *buf, int nb_sectors);
164 f7d0fe02 Kevin Wolf
165 f7d0fe02 Kevin Wolf
/* qcow2-refcount.c functions */
166 ed6ccf0f Kevin Wolf
int qcow2_refcount_init(BlockDriverState *bs);
167 ed6ccf0f Kevin Wolf
void qcow2_refcount_close(BlockDriverState *bs);
168 f7d0fe02 Kevin Wolf
169 ed6ccf0f Kevin Wolf
int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
170 ed6ccf0f Kevin Wolf
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
171 ed6ccf0f Kevin Wolf
void qcow2_free_clusters(BlockDriverState *bs,
172 45aba42f Kevin Wolf
    int64_t offset, int64_t size);
173 ed6ccf0f Kevin Wolf
void qcow2_free_any_clusters(BlockDriverState *bs,
174 45aba42f Kevin Wolf
    uint64_t cluster_offset, int nb_clusters);
175 f7d0fe02 Kevin Wolf
176 ed6ccf0f Kevin Wolf
void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
177 ed6ccf0f Kevin Wolf
    int64_t size);
178 ed6ccf0f Kevin Wolf
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
179 ed6ccf0f Kevin Wolf
    int64_t l1_table_offset, int l1_size, int addend);
180 f7d0fe02 Kevin Wolf
181 ed6ccf0f Kevin Wolf
int qcow2_check_refcounts(BlockDriverState *bs);
182 f7d0fe02 Kevin Wolf
183 45aba42f Kevin Wolf
/* qcow2-cluster.c functions */
184 ed6ccf0f Kevin Wolf
int qcow2_grow_l1_table(BlockDriverState *bs, int min_size);
185 ed6ccf0f Kevin Wolf
void qcow2_l2_cache_reset(BlockDriverState *bs);
186 ed6ccf0f Kevin Wolf
int qcow2_decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
187 ed6ccf0f Kevin Wolf
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
188 45aba42f Kevin Wolf
                     uint8_t *out_buf, const uint8_t *in_buf,
189 45aba42f Kevin Wolf
                     int nb_sectors, int enc,
190 45aba42f Kevin Wolf
                     const AES_KEY *key);
191 45aba42f Kevin Wolf
192 ed6ccf0f Kevin Wolf
uint64_t qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
193 ed6ccf0f Kevin Wolf
    int *num);
194 ed6ccf0f Kevin Wolf
uint64_t qcow2_alloc_cluster_offset(BlockDriverState *bs,
195 45aba42f Kevin Wolf
                              uint64_t offset,
196 45aba42f Kevin Wolf
                              int n_start, int n_end,
197 45aba42f Kevin Wolf
                              int *num, QCowL2Meta *m);
198 ed6ccf0f Kevin Wolf
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
199 45aba42f Kevin Wolf
                                         uint64_t offset,
200 45aba42f Kevin Wolf
                                         int compressed_size);
201 45aba42f Kevin Wolf
202 ed6ccf0f Kevin Wolf
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
203 45aba42f Kevin Wolf
    QCowL2Meta *m);
204 45aba42f Kevin Wolf
205 c142442b Kevin Wolf
/* qcow2-snapshot.c functions */
206 ed6ccf0f Kevin Wolf
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
207 ed6ccf0f Kevin Wolf
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
208 ed6ccf0f Kevin Wolf
int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
209 ed6ccf0f Kevin Wolf
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
210 c142442b Kevin Wolf
211 ed6ccf0f Kevin Wolf
void qcow2_free_snapshots(BlockDriverState *bs);
212 ed6ccf0f Kevin Wolf
int qcow2_read_snapshots(BlockDriverState *bs);
213 c142442b Kevin Wolf
214 f7d0fe02 Kevin Wolf
#endif