Statistics
| Branch: | Revision:

root / block / qcow2.h @ f53ec699

History | View | Annotate | Download (13 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 753d9b82 Aurelien Jarno
#include "qemu/aes.h"
29 737e150e Paolo Bonzini
#include "block/coroutine.h"
30 f7d0fe02 Kevin Wolf
31 14899cdf Filip Navara
//#define DEBUG_ALLOC
32 14899cdf Filip Navara
//#define DEBUG_ALLOC2
33 14899cdf Filip Navara
//#define DEBUG_EXT
34 14899cdf Filip Navara
35 f7d0fe02 Kevin Wolf
#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
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 6377af48 Kevin Wolf
/* The cluster reads as all zeros */
47 6377af48 Kevin Wolf
#define QCOW_OFLAG_ZERO (1LL << 0)
48 f7d0fe02 Kevin Wolf
49 f7d0fe02 Kevin Wolf
#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
50 f7d0fe02 Kevin Wolf
51 f7d0fe02 Kevin Wolf
#define MIN_CLUSTER_BITS 9
52 80ee15a6 Kevin Wolf
#define MAX_CLUSTER_BITS 21
53 f7d0fe02 Kevin Wolf
54 f7d0fe02 Kevin Wolf
#define L2_CACHE_SIZE 16
55 f7d0fe02 Kevin Wolf
56 29c1a730 Kevin Wolf
/* Must be at least 4 to cover all cases of refcount table growth */
57 29c1a730 Kevin Wolf
#define REFCOUNT_CACHE_SIZE 4
58 29c1a730 Kevin Wolf
59 99cce9fa Kevin Wolf
#define DEFAULT_CLUSTER_SIZE 65536
60 99cce9fa Kevin Wolf
61 acdfb480 Kevin Wolf
62 64aa99d3 Kevin Wolf
#define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
63 64aa99d3 Kevin Wolf
#define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
64 64aa99d3 Kevin Wolf
#define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
65 64aa99d3 Kevin Wolf
#define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
66 acdfb480 Kevin Wolf
67 f7d0fe02 Kevin Wolf
typedef struct QCowHeader {
68 f7d0fe02 Kevin Wolf
    uint32_t magic;
69 f7d0fe02 Kevin Wolf
    uint32_t version;
70 f7d0fe02 Kevin Wolf
    uint64_t backing_file_offset;
71 f7d0fe02 Kevin Wolf
    uint32_t backing_file_size;
72 f7d0fe02 Kevin Wolf
    uint32_t cluster_bits;
73 f7d0fe02 Kevin Wolf
    uint64_t size; /* in bytes */
74 f7d0fe02 Kevin Wolf
    uint32_t crypt_method;
75 f7d0fe02 Kevin Wolf
    uint32_t l1_size; /* XXX: save number of clusters instead ? */
76 f7d0fe02 Kevin Wolf
    uint64_t l1_table_offset;
77 f7d0fe02 Kevin Wolf
    uint64_t refcount_table_offset;
78 f7d0fe02 Kevin Wolf
    uint32_t refcount_table_clusters;
79 f7d0fe02 Kevin Wolf
    uint32_t nb_snapshots;
80 f7d0fe02 Kevin Wolf
    uint64_t snapshots_offset;
81 6744cbab Kevin Wolf
82 6744cbab Kevin Wolf
    /* The following fields are only valid for version >= 3 */
83 6744cbab Kevin Wolf
    uint64_t incompatible_features;
84 6744cbab Kevin Wolf
    uint64_t compatible_features;
85 6744cbab Kevin Wolf
    uint64_t autoclear_features;
86 6744cbab Kevin Wolf
87 6744cbab Kevin Wolf
    uint32_t refcount_order;
88 6744cbab Kevin Wolf
    uint32_t header_length;
89 f7d0fe02 Kevin Wolf
} QCowHeader;
90 f7d0fe02 Kevin Wolf
91 f7d0fe02 Kevin Wolf
typedef struct QCowSnapshot {
92 f7d0fe02 Kevin Wolf
    uint64_t l1_table_offset;
93 f7d0fe02 Kevin Wolf
    uint32_t l1_size;
94 f7d0fe02 Kevin Wolf
    char *id_str;
95 f7d0fe02 Kevin Wolf
    char *name;
96 90b27759 Kevin Wolf
    uint64_t disk_size;
97 c2c9a466 Kevin Wolf
    uint64_t vm_state_size;
98 f7d0fe02 Kevin Wolf
    uint32_t date_sec;
99 f7d0fe02 Kevin Wolf
    uint32_t date_nsec;
100 f7d0fe02 Kevin Wolf
    uint64_t vm_clock_nsec;
101 f7d0fe02 Kevin Wolf
} QCowSnapshot;
102 f7d0fe02 Kevin Wolf
103 49381094 Kevin Wolf
struct Qcow2Cache;
104 49381094 Kevin Wolf
typedef struct Qcow2Cache Qcow2Cache;
105 49381094 Kevin Wolf
106 75bab85c Kevin Wolf
typedef struct Qcow2UnknownHeaderExtension {
107 75bab85c Kevin Wolf
    uint32_t magic;
108 75bab85c Kevin Wolf
    uint32_t len;
109 75bab85c Kevin Wolf
    QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
110 75bab85c Kevin Wolf
    uint8_t data[];
111 75bab85c Kevin Wolf
} Qcow2UnknownHeaderExtension;
112 75bab85c Kevin Wolf
113 cfcc4c62 Kevin Wolf
enum {
114 cfcc4c62 Kevin Wolf
    QCOW2_FEAT_TYPE_INCOMPATIBLE    = 0,
115 cfcc4c62 Kevin Wolf
    QCOW2_FEAT_TYPE_COMPATIBLE      = 1,
116 cfcc4c62 Kevin Wolf
    QCOW2_FEAT_TYPE_AUTOCLEAR       = 2,
117 cfcc4c62 Kevin Wolf
};
118 cfcc4c62 Kevin Wolf
119 c61d0004 Stefan Hajnoczi
/* Incompatible feature bits */
120 c61d0004 Stefan Hajnoczi
enum {
121 c61d0004 Stefan Hajnoczi
    QCOW2_INCOMPAT_DIRTY_BITNR   = 0,
122 c61d0004 Stefan Hajnoczi
    QCOW2_INCOMPAT_DIRTY         = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
123 c61d0004 Stefan Hajnoczi
124 c61d0004 Stefan Hajnoczi
    QCOW2_INCOMPAT_MASK          = QCOW2_INCOMPAT_DIRTY,
125 c61d0004 Stefan Hajnoczi
};
126 c61d0004 Stefan Hajnoczi
127 bfe8043e Stefan Hajnoczi
/* Compatible feature bits */
128 bfe8043e Stefan Hajnoczi
enum {
129 bfe8043e Stefan Hajnoczi
    QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
130 bfe8043e Stefan Hajnoczi
    QCOW2_COMPAT_LAZY_REFCOUNTS       = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
131 bfe8043e Stefan Hajnoczi
132 bfe8043e Stefan Hajnoczi
    QCOW2_COMPAT_FEAT_MASK            = QCOW2_COMPAT_LAZY_REFCOUNTS,
133 bfe8043e Stefan Hajnoczi
};
134 bfe8043e Stefan Hajnoczi
135 6cfcb9b8 Kevin Wolf
enum qcow2_discard_type {
136 6cfcb9b8 Kevin Wolf
    QCOW2_DISCARD_NEVER = 0,
137 6cfcb9b8 Kevin Wolf
    QCOW2_DISCARD_ALWAYS,
138 6cfcb9b8 Kevin Wolf
    QCOW2_DISCARD_REQUEST,
139 6cfcb9b8 Kevin Wolf
    QCOW2_DISCARD_SNAPSHOT,
140 6cfcb9b8 Kevin Wolf
    QCOW2_DISCARD_OTHER,
141 6cfcb9b8 Kevin Wolf
    QCOW2_DISCARD_MAX
142 6cfcb9b8 Kevin Wolf
};
143 6cfcb9b8 Kevin Wolf
144 cfcc4c62 Kevin Wolf
typedef struct Qcow2Feature {
145 cfcc4c62 Kevin Wolf
    uint8_t type;
146 cfcc4c62 Kevin Wolf
    uint8_t bit;
147 cfcc4c62 Kevin Wolf
    char    name[46];
148 cfcc4c62 Kevin Wolf
} QEMU_PACKED Qcow2Feature;
149 cfcc4c62 Kevin Wolf
150 0b919fae Kevin Wolf
typedef struct Qcow2DiscardRegion {
151 0b919fae Kevin Wolf
    BlockDriverState *bs;
152 0b919fae Kevin Wolf
    uint64_t offset;
153 0b919fae Kevin Wolf
    uint64_t bytes;
154 0b919fae Kevin Wolf
    QTAILQ_ENTRY(Qcow2DiscardRegion) next;
155 0b919fae Kevin Wolf
} Qcow2DiscardRegion;
156 0b919fae Kevin Wolf
157 f7d0fe02 Kevin Wolf
typedef struct BDRVQcowState {
158 f7d0fe02 Kevin Wolf
    int cluster_bits;
159 f7d0fe02 Kevin Wolf
    int cluster_size;
160 f7d0fe02 Kevin Wolf
    int cluster_sectors;
161 f7d0fe02 Kevin Wolf
    int l2_bits;
162 f7d0fe02 Kevin Wolf
    int l2_size;
163 f7d0fe02 Kevin Wolf
    int l1_size;
164 f7d0fe02 Kevin Wolf
    int l1_vm_state_index;
165 f7d0fe02 Kevin Wolf
    int csize_shift;
166 f7d0fe02 Kevin Wolf
    int csize_mask;
167 f7d0fe02 Kevin Wolf
    uint64_t cluster_offset_mask;
168 f7d0fe02 Kevin Wolf
    uint64_t l1_table_offset;
169 f7d0fe02 Kevin Wolf
    uint64_t *l1_table;
170 29c1a730 Kevin Wolf
171 29c1a730 Kevin Wolf
    Qcow2Cache* l2_table_cache;
172 29c1a730 Kevin Wolf
    Qcow2Cache* refcount_block_cache;
173 29c1a730 Kevin Wolf
174 f7d0fe02 Kevin Wolf
    uint8_t *cluster_cache;
175 f7d0fe02 Kevin Wolf
    uint8_t *cluster_data;
176 f7d0fe02 Kevin Wolf
    uint64_t cluster_cache_offset;
177 72cf2d4f Blue Swirl
    QLIST_HEAD(QCowClusterAlloc, QCowL2Meta) cluster_allocs;
178 f7d0fe02 Kevin Wolf
179 f7d0fe02 Kevin Wolf
    uint64_t *refcount_table;
180 f7d0fe02 Kevin Wolf
    uint64_t refcount_table_offset;
181 f7d0fe02 Kevin Wolf
    uint32_t refcount_table_size;
182 f7d0fe02 Kevin Wolf
    int64_t free_cluster_index;
183 f7d0fe02 Kevin Wolf
    int64_t free_byte_offset;
184 f7d0fe02 Kevin Wolf
185 68d100e9 Kevin Wolf
    CoMutex lock;
186 68d100e9 Kevin Wolf
187 f7d0fe02 Kevin Wolf
    uint32_t crypt_method; /* current crypt method, 0 if no key yet */
188 f7d0fe02 Kevin Wolf
    uint32_t crypt_method_header;
189 f7d0fe02 Kevin Wolf
    AES_KEY aes_encrypt_key;
190 f7d0fe02 Kevin Wolf
    AES_KEY aes_decrypt_key;
191 f7d0fe02 Kevin Wolf
    uint64_t snapshots_offset;
192 f7d0fe02 Kevin Wolf
    int snapshots_size;
193 f7d0fe02 Kevin Wolf
    int nb_snapshots;
194 f7d0fe02 Kevin Wolf
    QCowSnapshot *snapshots;
195 06d9260f Anthony Liguori
196 06d9260f Anthony Liguori
    int flags;
197 6744cbab Kevin Wolf
    int qcow_version;
198 74c4510a Kevin Wolf
    bool use_lazy_refcounts;
199 6744cbab Kevin Wolf
200 67af674e Kevin Wolf
    bool discard_passthrough[QCOW2_DISCARD_MAX];
201 67af674e Kevin Wolf
202 6744cbab Kevin Wolf
    uint64_t incompatible_features;
203 6744cbab Kevin Wolf
    uint64_t compatible_features;
204 6744cbab Kevin Wolf
    uint64_t autoclear_features;
205 6744cbab Kevin Wolf
206 6744cbab Kevin Wolf
    size_t unknown_header_fields_size;
207 6744cbab Kevin Wolf
    void* unknown_header_fields;
208 75bab85c Kevin Wolf
    QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
209 0b919fae Kevin Wolf
    QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
210 0b919fae Kevin Wolf
    bool cache_discards;
211 f7d0fe02 Kevin Wolf
} BDRVQcowState;
212 f7d0fe02 Kevin Wolf
213 f7d0fe02 Kevin Wolf
/* XXX: use std qcow open function ? */
214 f7d0fe02 Kevin Wolf
typedef struct QCowCreateState {
215 f7d0fe02 Kevin Wolf
    int cluster_size;
216 f7d0fe02 Kevin Wolf
    int cluster_bits;
217 f7d0fe02 Kevin Wolf
    uint16_t *refcount_block;
218 f7d0fe02 Kevin Wolf
    uint64_t *refcount_table;
219 f7d0fe02 Kevin Wolf
    int64_t l1_table_offset;
220 f7d0fe02 Kevin Wolf
    int64_t refcount_table_offset;
221 f7d0fe02 Kevin Wolf
    int64_t refcount_block_offset;
222 f7d0fe02 Kevin Wolf
} QCowCreateState;
223 f7d0fe02 Kevin Wolf
224 f214978a Kevin Wolf
struct QCowAIOCB;
225 f214978a Kevin Wolf
226 593fb83c Kevin Wolf
typedef struct Qcow2COWRegion {
227 593fb83c Kevin Wolf
    /**
228 593fb83c Kevin Wolf
     * Offset of the COW region in bytes from the start of the first cluster
229 593fb83c Kevin Wolf
     * touched by the request.
230 593fb83c Kevin Wolf
     */
231 593fb83c Kevin Wolf
    uint64_t    offset;
232 593fb83c Kevin Wolf
233 593fb83c Kevin Wolf
    /** Number of sectors to copy */
234 593fb83c Kevin Wolf
    int         nb_sectors;
235 593fb83c Kevin Wolf
} Qcow2COWRegion;
236 593fb83c Kevin Wolf
237 f50f88b9 Kevin Wolf
/**
238 f50f88b9 Kevin Wolf
 * Describes an in-flight (part of a) write request that writes to clusters
239 f50f88b9 Kevin Wolf
 * that are not referenced in their L2 table yet.
240 f50f88b9 Kevin Wolf
 */
241 45aba42f Kevin Wolf
typedef struct QCowL2Meta
242 45aba42f Kevin Wolf
{
243 1d3afd64 Kevin Wolf
    /** Guest offset of the first newly allocated cluster */
244 45aba42f Kevin Wolf
    uint64_t offset;
245 1d3afd64 Kevin Wolf
246 1d3afd64 Kevin Wolf
    /** Host offset of the first newly allocated cluster */
247 250196f1 Kevin Wolf
    uint64_t alloc_offset;
248 1d3afd64 Kevin Wolf
249 1d3afd64 Kevin Wolf
    /**
250 1d3afd64 Kevin Wolf
     * Number of sectors from the start of the first allocated cluster to
251 1d3afd64 Kevin Wolf
     * the end of the (possibly shortened) request
252 1d3afd64 Kevin Wolf
     */
253 45aba42f Kevin Wolf
    int nb_available;
254 1d3afd64 Kevin Wolf
255 1d3afd64 Kevin Wolf
    /** Number of newly allocated clusters */
256 45aba42f Kevin Wolf
    int nb_clusters;
257 1d3afd64 Kevin Wolf
258 1d3afd64 Kevin Wolf
    /**
259 1d3afd64 Kevin Wolf
     * Requests that overlap with this allocation and wait to be restarted
260 1d3afd64 Kevin Wolf
     * when the allocating request has completed.
261 1d3afd64 Kevin Wolf
     */
262 68d100e9 Kevin Wolf
    CoQueue dependent_requests;
263 f214978a Kevin Wolf
264 593fb83c Kevin Wolf
    /**
265 593fb83c Kevin Wolf
     * The COW Region between the start of the first allocated cluster and the
266 593fb83c Kevin Wolf
     * area the guest actually writes to.
267 593fb83c Kevin Wolf
     */
268 593fb83c Kevin Wolf
    Qcow2COWRegion cow_start;
269 593fb83c Kevin Wolf
270 593fb83c Kevin Wolf
    /**
271 593fb83c Kevin Wolf
     * The COW Region between the area the guest actually writes to and the
272 593fb83c Kevin Wolf
     * end of the last allocated cluster.
273 593fb83c Kevin Wolf
     */
274 593fb83c Kevin Wolf
    Qcow2COWRegion cow_end;
275 593fb83c Kevin Wolf
276 88c6588c Kevin Wolf
    /** Pointer to next L2Meta of the same write request */
277 88c6588c Kevin Wolf
    struct QCowL2Meta *next;
278 88c6588c Kevin Wolf
279 72cf2d4f Blue Swirl
    QLIST_ENTRY(QCowL2Meta) next_in_flight;
280 45aba42f Kevin Wolf
} QCowL2Meta;
281 45aba42f Kevin Wolf
282 68d000a3 Kevin Wolf
enum {
283 68d000a3 Kevin Wolf
    QCOW2_CLUSTER_UNALLOCATED,
284 68d000a3 Kevin Wolf
    QCOW2_CLUSTER_NORMAL,
285 68d000a3 Kevin Wolf
    QCOW2_CLUSTER_COMPRESSED,
286 6377af48 Kevin Wolf
    QCOW2_CLUSTER_ZERO
287 68d000a3 Kevin Wolf
};
288 68d000a3 Kevin Wolf
289 68d000a3 Kevin Wolf
#define L1E_OFFSET_MASK 0x00ffffffffffff00ULL
290 68d000a3 Kevin Wolf
#define L2E_OFFSET_MASK 0x00ffffffffffff00ULL
291 68d000a3 Kevin Wolf
#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
292 68d000a3 Kevin Wolf
293 76dc9e0c Kevin Wolf
#define REFT_OFFSET_MASK 0xffffffffffffff00ULL
294 76dc9e0c Kevin Wolf
295 3b8e2e26 Kevin Wolf
static inline int64_t start_of_cluster(BDRVQcowState *s, int64_t offset)
296 3b8e2e26 Kevin Wolf
{
297 3b8e2e26 Kevin Wolf
    return offset & ~(s->cluster_size - 1);
298 3b8e2e26 Kevin Wolf
}
299 3b8e2e26 Kevin Wolf
300 c37f4cd7 Kevin Wolf
static inline int64_t offset_into_cluster(BDRVQcowState *s, int64_t offset)
301 c37f4cd7 Kevin Wolf
{
302 c37f4cd7 Kevin Wolf
    return offset & (s->cluster_size - 1);
303 c37f4cd7 Kevin Wolf
}
304 c37f4cd7 Kevin Wolf
305 45aba42f Kevin Wolf
static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
306 f7d0fe02 Kevin Wolf
{
307 f7d0fe02 Kevin Wolf
    return (size + (s->cluster_size - 1)) >> s->cluster_bits;
308 f7d0fe02 Kevin Wolf
}
309 f7d0fe02 Kevin Wolf
310 2cf7cfa1 Kevin Wolf
static inline int64_t size_to_l1(BDRVQcowState *s, int64_t size)
311 419b19d9 Stefan Hajnoczi
{
312 419b19d9 Stefan Hajnoczi
    int shift = s->cluster_bits + s->l2_bits;
313 419b19d9 Stefan Hajnoczi
    return (size + (1ULL << shift) - 1) >> shift;
314 419b19d9 Stefan Hajnoczi
}
315 419b19d9 Stefan Hajnoczi
316 17a71e58 Kevin Wolf
static inline int offset_to_l2_index(BDRVQcowState *s, int64_t offset)
317 17a71e58 Kevin Wolf
{
318 17a71e58 Kevin Wolf
    return (offset >> s->cluster_bits) & (s->l2_size - 1);
319 17a71e58 Kevin Wolf
}
320 17a71e58 Kevin Wolf
321 c142442b Kevin Wolf
static inline int64_t align_offset(int64_t offset, int n)
322 c142442b Kevin Wolf
{
323 c142442b Kevin Wolf
    offset = (offset + n - 1) & ~(n - 1);
324 c142442b Kevin Wolf
    return offset;
325 c142442b Kevin Wolf
}
326 c142442b Kevin Wolf
327 68d000a3 Kevin Wolf
static inline int qcow2_get_cluster_type(uint64_t l2_entry)
328 68d000a3 Kevin Wolf
{
329 68d000a3 Kevin Wolf
    if (l2_entry & QCOW_OFLAG_COMPRESSED) {
330 68d000a3 Kevin Wolf
        return QCOW2_CLUSTER_COMPRESSED;
331 6377af48 Kevin Wolf
    } else if (l2_entry & QCOW_OFLAG_ZERO) {
332 6377af48 Kevin Wolf
        return QCOW2_CLUSTER_ZERO;
333 68d000a3 Kevin Wolf
    } else if (!(l2_entry & L2E_OFFSET_MASK)) {
334 68d000a3 Kevin Wolf
        return QCOW2_CLUSTER_UNALLOCATED;
335 68d000a3 Kevin Wolf
    } else {
336 68d000a3 Kevin Wolf
        return QCOW2_CLUSTER_NORMAL;
337 68d000a3 Kevin Wolf
    }
338 68d000a3 Kevin Wolf
}
339 68d000a3 Kevin Wolf
340 bfe8043e Stefan Hajnoczi
/* Check whether refcounts are eager or lazy */
341 bfe8043e Stefan Hajnoczi
static inline bool qcow2_need_accurate_refcounts(BDRVQcowState *s)
342 bfe8043e Stefan Hajnoczi
{
343 bfe8043e Stefan Hajnoczi
    return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
344 bfe8043e Stefan Hajnoczi
}
345 c142442b Kevin Wolf
346 65eb2e35 Kevin Wolf
static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
347 65eb2e35 Kevin Wolf
{
348 65eb2e35 Kevin Wolf
    return m->offset + m->cow_start.offset;
349 65eb2e35 Kevin Wolf
}
350 65eb2e35 Kevin Wolf
351 65eb2e35 Kevin Wolf
static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
352 65eb2e35 Kevin Wolf
{
353 65eb2e35 Kevin Wolf
    return m->offset + m->cow_end.offset
354 65eb2e35 Kevin Wolf
        + (m->cow_end.nb_sectors << BDRV_SECTOR_BITS);
355 65eb2e35 Kevin Wolf
}
356 65eb2e35 Kevin Wolf
357 f7d0fe02 Kevin Wolf
// FIXME Need qcow2_ prefix to global functions
358 f7d0fe02 Kevin Wolf
359 f7d0fe02 Kevin Wolf
/* qcow2.c functions */
360 bd28f835 Kevin Wolf
int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov,
361 bd28f835 Kevin Wolf
                  int64_t sector_num, int nb_sectors);
362 280d3735 Kevin Wolf
363 280d3735 Kevin Wolf
int qcow2_mark_dirty(BlockDriverState *bs);
364 e24e49e6 Kevin Wolf
int qcow2_update_header(BlockDriverState *bs);
365 f7d0fe02 Kevin Wolf
366 f7d0fe02 Kevin Wolf
/* qcow2-refcount.c functions */
367 ed6ccf0f Kevin Wolf
int qcow2_refcount_init(BlockDriverState *bs);
368 ed6ccf0f Kevin Wolf
void qcow2_refcount_close(BlockDriverState *bs);
369 f7d0fe02 Kevin Wolf
370 ed6ccf0f Kevin Wolf
int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size);
371 256900b1 Kevin Wolf
int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
372 256900b1 Kevin Wolf
    int nb_clusters);
373 ed6ccf0f Kevin Wolf
int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
374 ed6ccf0f Kevin Wolf
void qcow2_free_clusters(BlockDriverState *bs,
375 6cfcb9b8 Kevin Wolf
                          int64_t offset, int64_t size,
376 6cfcb9b8 Kevin Wolf
                          enum qcow2_discard_type type);
377 6cfcb9b8 Kevin Wolf
void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
378 6cfcb9b8 Kevin Wolf
                             int nb_clusters, enum qcow2_discard_type type);
379 f7d0fe02 Kevin Wolf
380 ed6ccf0f Kevin Wolf
int qcow2_update_snapshot_refcount(BlockDriverState *bs,
381 ed6ccf0f Kevin Wolf
    int64_t l1_table_offset, int l1_size, int addend);
382 f7d0fe02 Kevin Wolf
383 166acf54 Kevin Wolf
int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
384 166acf54 Kevin Wolf
                          BdrvCheckMode fix);
385 f7d0fe02 Kevin Wolf
386 0b919fae Kevin Wolf
void qcow2_process_discards(BlockDriverState *bs, int ret);
387 0b919fae Kevin Wolf
388 45aba42f Kevin Wolf
/* qcow2-cluster.c functions */
389 2cf7cfa1 Kevin Wolf
int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
390 2cf7cfa1 Kevin Wolf
                        bool exact_size);
391 ed6ccf0f Kevin Wolf
void qcow2_l2_cache_reset(BlockDriverState *bs);
392 66f82cee Kevin Wolf
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
393 ed6ccf0f Kevin Wolf
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
394 45aba42f Kevin Wolf
                     uint8_t *out_buf, const uint8_t *in_buf,
395 45aba42f Kevin Wolf
                     int nb_sectors, int enc,
396 45aba42f Kevin Wolf
                     const AES_KEY *key);
397 45aba42f Kevin Wolf
398 1c46efaa Kevin Wolf
int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
399 1c46efaa Kevin Wolf
    int *num, uint64_t *cluster_offset);
400 f4f0d391 Kevin Wolf
int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
401 f50f88b9 Kevin Wolf
    int n_start, int n_end, int *num, uint64_t *host_offset, QCowL2Meta **m);
402 ed6ccf0f Kevin Wolf
uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
403 45aba42f Kevin Wolf
                                         uint64_t offset,
404 45aba42f Kevin Wolf
                                         int compressed_size);
405 45aba42f Kevin Wolf
406 148da7ea Kevin Wolf
int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
407 5ea929e3 Kevin Wolf
int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
408 5ea929e3 Kevin Wolf
    int nb_sectors);
409 621f0589 Kevin Wolf
int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
410 45aba42f Kevin Wolf
411 c142442b Kevin Wolf
/* qcow2-snapshot.c functions */
412 ed6ccf0f Kevin Wolf
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
413 ed6ccf0f Kevin Wolf
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
414 ed6ccf0f Kevin Wolf
int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
415 ed6ccf0f Kevin Wolf
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
416 51ef6727 edison
int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
417 c142442b Kevin Wolf
418 ed6ccf0f Kevin Wolf
void qcow2_free_snapshots(BlockDriverState *bs);
419 ed6ccf0f Kevin Wolf
int qcow2_read_snapshots(BlockDriverState *bs);
420 c142442b Kevin Wolf
421 49381094 Kevin Wolf
/* qcow2-cache.c functions */
422 6af4e9ea Paolo Bonzini
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
423 49381094 Kevin Wolf
int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
424 49381094 Kevin Wolf
425 49381094 Kevin Wolf
void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
426 49381094 Kevin Wolf
int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
427 49381094 Kevin Wolf
int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
428 49381094 Kevin Wolf
    Qcow2Cache *dependency);
429 3de0a294 Kevin Wolf
void qcow2_cache_depends_on_flush(Qcow2Cache *c);
430 49381094 Kevin Wolf
431 49381094 Kevin Wolf
int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
432 49381094 Kevin Wolf
    void **table);
433 49381094 Kevin Wolf
int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
434 49381094 Kevin Wolf
    void **table);
435 49381094 Kevin Wolf
int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
436 49381094 Kevin Wolf
437 f7d0fe02 Kevin Wolf
#endif