blob: 83cfa0c391de7a26dfc36d4387fa574c88082207 [file] [log] [blame]
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001/*
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
Daniel P. Berrangeb25b3872017-06-23 17:24:10 +010028#include "crypto/block.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010029#include "qemu/coroutine.h"
Leonid Blochb6a95c62018-09-26 19:04:41 +030030#include "qemu/units.h"
Vladimir Sementsov-Ogievskiy9353db42019-05-06 17:27:34 +030031#include "block/block_int.h"
Kevin Wolff7d0fe02009-05-28 16:07:04 +020032
Filip Navara14899cd2009-06-23 19:17:30 -050033//#define DEBUG_ALLOC
34//#define DEBUG_ALLOC2
35//#define DEBUG_EXT
36
Kevin Wolff7d0fe02009-05-28 16:07:04 +020037#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
Kevin Wolff7d0fe02009-05-28 16:07:04 +020038
39#define QCOW_CRYPT_NONE 0
40#define QCOW_CRYPT_AES 1
Daniel P. Berrange4652b8f2017-06-23 17:24:12 +010041#define QCOW_CRYPT_LUKS 2
Kevin Wolff7d0fe02009-05-28 16:07:04 +020042
43#define QCOW_MAX_CRYPT_CLUSTERS 32
Kevin Wolfce48f2f42014-03-26 13:05:45 +010044#define QCOW_MAX_SNAPSHOTS 65536
Kevin Wolff7d0fe02009-05-28 16:07:04 +020045
Eric Blake77d6a212018-11-13 17:03:18 -060046/* Field widths in qcow2 mean normal cluster offsets cannot reach
47 * 64PB; depending on cluster size, compressed clusters can have a
48 * smaller limit (64PB for up to 16k clusters, then ramps down to
49 * 512TB for 2M clusters). */
50#define QCOW_MAX_CLUSTER_OFFSET ((1ULL << 56) - 1)
51
Kevin Wolf2b5d5952014-03-26 13:05:50 +010052/* 8 MB refcount table is enough for 2 PB images at 64k cluster size
53 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
Markus Armbruster14632122019-01-11 20:14:01 +010054#define QCOW_MAX_REFTABLE_SIZE (8 * MiB)
Kevin Wolf2b5d5952014-03-26 13:05:50 +010055
Kevin Wolf6a83f8b2014-03-26 13:06:06 +010056/* 32 MB L1 table is enough for 2 PB images at 64k cluster size
57 * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
Markus Armbruster14632122019-01-11 20:14:01 +010058#define QCOW_MAX_L1_SIZE (32 * MiB)
Kevin Wolf6a83f8b2014-03-26 13:06:06 +010059
Kevin Wolf5dae6e32014-03-26 13:06:07 +010060/* Allow for an average of 1k per snapshot table entry, should be plenty of
61 * space for snapshot names and IDs */
62#define QCOW_MAX_SNAPSHOTS_SIZE (1024 * QCOW_MAX_SNAPSHOTS)
63
Max Reitzfcf9a6b2019-10-11 17:28:02 +020064/* Maximum amount of extra data per snapshot table entry to accept */
65#define QCOW_MAX_SNAPSHOT_EXTRA_DATA 1024
66
Vladimir Sementsov-Ogievskiy88ddffa2017-06-28 15:05:08 +030067/* Bitmap header extension constraints */
68#define QCOW2_MAX_BITMAPS 65535
69#define QCOW2_MAX_BITMAP_DIRECTORY_SIZE (1024 * QCOW2_MAX_BITMAPS)
70
Vladimir Sementsov-Ogievskiyd710cf52019-09-16 20:53:24 +030071/* Maximum of parallel sub-request per guest request */
72#define QCOW2_MAX_WORKERS 8
73
Kevin Wolff7d0fe02009-05-28 16:07:04 +020074/* indicate that the refcount of the referenced cluster is exactly one. */
Peter Maydell127c84e2013-08-23 17:35:45 +010075#define QCOW_OFLAG_COPIED (1ULL << 63)
Kevin Wolff7d0fe02009-05-28 16:07:04 +020076/* indicate that the cluster is compressed (they never have the copied flag) */
Peter Maydell127c84e2013-08-23 17:35:45 +010077#define QCOW_OFLAG_COMPRESSED (1ULL << 62)
Kevin Wolf6377af42012-03-16 15:02:38 +010078/* The cluster reads as all zeros */
Peter Maydell127c84e2013-08-23 17:35:45 +010079#define QCOW_OFLAG_ZERO (1ULL << 0)
Kevin Wolff7d0fe02009-05-28 16:07:04 +020080
Alberto Garciad0346b52020-07-10 18:12:51 +020081#define QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER 32
82
Alberto Garcia34905d82020-07-10 18:12:56 +020083/* The subcluster X [0..31] is allocated */
84#define QCOW_OFLAG_SUB_ALLOC(X) (1ULL << (X))
85/* The subcluster X [0..31] reads as zeroes */
86#define QCOW_OFLAG_SUB_ZERO(X) (QCOW_OFLAG_SUB_ALLOC(X) << 32)
87/* Subclusters [X, Y) (0 <= X <= Y <= 32) are allocated */
88#define QCOW_OFLAG_SUB_ALLOC_RANGE(X, Y) \
89 (QCOW_OFLAG_SUB_ALLOC(Y) - QCOW_OFLAG_SUB_ALLOC(X))
90/* Subclusters [X, Y) (0 <= X <= Y <= 32) read as zeroes */
91#define QCOW_OFLAG_SUB_ZERO_RANGE(X, Y) \
92 (QCOW_OFLAG_SUB_ALLOC_RANGE(X, Y) << 32)
93/* L2 entry bitmap with all allocation bits set */
94#define QCOW_L2_BITMAP_ALL_ALLOC (QCOW_OFLAG_SUB_ALLOC_RANGE(0, 32))
95/* L2 entry bitmap with all "read as zeroes" bits set */
96#define QCOW_L2_BITMAP_ALL_ZEROES (QCOW_OFLAG_SUB_ZERO_RANGE(0, 32))
97
Alberto Garciac8fd8552020-07-10 18:12:54 +020098/* Size of normal and extended L2 entries */
99#define L2E_SIZE_NORMAL (sizeof(uint64_t))
100#define L2E_SIZE_EXTENDED (sizeof(uint64_t) * 2)
101
Alberto Garcia02b1ecf2020-08-28 13:08:28 +0200102/* Size of L1 table entries */
103#define L1E_SIZE (sizeof(uint64_t))
104
105/* Size of reftable entries */
106#define REFTABLE_ENTRY_SIZE (sizeof(uint64_t))
107
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200108#define MIN_CLUSTER_BITS 9
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200109#define MAX_CLUSTER_BITS 21
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200110
Alberto Garciab6c24692019-05-10 19:22:54 +0300111/* Defined in the qcow2 spec (compressed cluster descriptor) */
112#define QCOW2_COMPRESSED_SECTOR_SIZE 512U
Max Reitz24552fe2019-10-28 17:18:40 +0100113#define QCOW2_COMPRESSED_SECTOR_MASK (~(QCOW2_COMPRESSED_SECTOR_SIZE - 1ULL))
Alberto Garciab6c24692019-05-10 19:22:54 +0300114
Max Reitz57e21662015-06-01 18:09:17 +0200115/* Must be at least 2 to cover COW */
Alberto Garcia1221fe62018-02-05 16:33:36 +0200116#define MIN_L2_CACHE_SIZE 2 /* cache entries */
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200117
Kevin Wolf29c1a732011-01-10 17:17:28 +0100118/* Must be at least 4 to cover all cases of refcount table growth */
Max Reitz440ba082014-08-18 22:07:31 +0200119#define MIN_REFCOUNT_CACHE_SIZE 4 /* clusters */
120
Leonid Bloch80668d02018-09-26 19:04:44 +0300121#ifdef CONFIG_LINUX
Markus Armbruster14632122019-01-11 20:14:01 +0100122#define DEFAULT_L2_CACHE_MAX_SIZE (32 * MiB)
Leonid Bloche957b502018-09-26 19:04:46 +0300123#define DEFAULT_CACHE_CLEAN_INTERVAL 600 /* seconds */
Leonid Bloch80668d02018-09-26 19:04:44 +0300124#else
Markus Armbruster14632122019-01-11 20:14:01 +0100125#define DEFAULT_L2_CACHE_MAX_SIZE (8 * MiB)
Leonid Bloche957b502018-09-26 19:04:46 +0300126/* Cache clean interval is currently available only on Linux, so must be 0 */
127#define DEFAULT_CACHE_CLEAN_INTERVAL 0
Leonid Bloch80668d02018-09-26 19:04:44 +0300128#endif
Max Reitz440ba082014-08-18 22:07:31 +0200129
Markus Armbruster14632122019-01-11 20:14:01 +0100130#define DEFAULT_CLUSTER_SIZE 65536
Kevin Wolf99cce9f2011-05-31 15:01:46 +0200131
Kevin Wolf0e8c08b2019-01-29 17:13:57 +0100132#define QCOW2_OPT_DATA_FILE "data-file"
Kevin Wolf64aa99d2013-07-17 14:45:34 +0200133#define QCOW2_OPT_LAZY_REFCOUNTS "lazy-refcounts"
134#define QCOW2_OPT_DISCARD_REQUEST "pass-discard-request"
135#define QCOW2_OPT_DISCARD_SNAPSHOT "pass-discard-snapshot"
136#define QCOW2_OPT_DISCARD_OTHER "pass-discard-other"
Max Reitz05de7e82013-10-10 11:09:25 +0200137#define QCOW2_OPT_OVERLAP "overlap-check"
Max Reitzee42b5c2014-08-20 19:59:35 +0200138#define QCOW2_OPT_OVERLAP_TEMPLATE "overlap-check.template"
Max Reitz05de7e82013-10-10 11:09:25 +0200139#define QCOW2_OPT_OVERLAP_MAIN_HEADER "overlap-check.main-header"
140#define QCOW2_OPT_OVERLAP_ACTIVE_L1 "overlap-check.active-l1"
141#define QCOW2_OPT_OVERLAP_ACTIVE_L2 "overlap-check.active-l2"
142#define QCOW2_OPT_OVERLAP_REFCOUNT_TABLE "overlap-check.refcount-table"
143#define QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK "overlap-check.refcount-block"
144#define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
145#define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
146#define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
Vladimir Sementsov-Ogievskiy0e4e4312018-07-05 18:15:15 +0300147#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
Max Reitz6c1c8d52014-08-18 22:07:33 +0200148#define QCOW2_OPT_CACHE_SIZE "cache-size"
149#define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
Alberto Garcia1221fe62018-02-05 16:33:36 +0200150#define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size"
Max Reitz6c1c8d52014-08-18 22:07:33 +0200151#define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
Alberto Garcia279621c2015-08-04 15:14:40 +0300152#define QCOW2_OPT_CACHE_CLEAN_INTERVAL "cache-clean-interval"
Kevin Wolfacdfb482013-03-18 13:08:10 +0100153
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200154typedef struct QCowHeader {
155 uint32_t magic;
156 uint32_t version;
157 uint64_t backing_file_offset;
158 uint32_t backing_file_size;
159 uint32_t cluster_bits;
160 uint64_t size; /* in bytes */
161 uint32_t crypt_method;
162 uint32_t l1_size; /* XXX: save number of clusters instead ? */
163 uint64_t l1_table_offset;
164 uint64_t refcount_table_offset;
165 uint32_t refcount_table_clusters;
166 uint32_t nb_snapshots;
167 uint64_t snapshots_offset;
Kevin Wolf6744cba2011-12-15 12:20:58 +0100168
169 /* The following fields are only valid for version >= 3 */
170 uint64_t incompatible_features;
171 uint64_t compatible_features;
172 uint64_t autoclear_features;
173
174 uint32_t refcount_order;
175 uint32_t header_length;
Denis Plotnikov572ad972020-05-07 11:25:18 +0300176
177 /* Additional fields */
178 uint8_t compression_type;
179
180 /* header must be a multiple of 8 */
181 uint8_t padding[7];
Jeff Codyc4217f62013-09-25 12:08:50 -0400182} QEMU_PACKED QCowHeader;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200183
Denis Plotnikov572ad972020-05-07 11:25:18 +0300184QEMU_BUILD_BUG_ON(!QEMU_IS_ALIGNED(sizeof(QCowHeader), 8));
185
Kevin Wolfce48f2f42014-03-26 13:05:45 +0100186typedef struct QEMU_PACKED QCowSnapshotHeader {
187 /* header is 8 byte aligned */
188 uint64_t l1_table_offset;
189
190 uint32_t l1_size;
191 uint16_t id_str_size;
192 uint16_t name_size;
193
194 uint32_t date_sec;
195 uint32_t date_nsec;
196
197 uint64_t vm_clock_nsec;
198
199 uint32_t vm_state_size;
200 uint32_t extra_data_size; /* for extension */
201 /* extra data follows */
202 /* id_str follows */
203 /* name follows */
204} QCowSnapshotHeader;
205
206typedef struct QEMU_PACKED QCowSnapshotExtraData {
207 uint64_t vm_state_size_large;
208 uint64_t disk_size;
209} QCowSnapshotExtraData;
210
211
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200212typedef struct QCowSnapshot {
213 uint64_t l1_table_offset;
214 uint32_t l1_size;
215 char *id_str;
216 char *name;
Kevin Wolf90b27752012-04-11 16:33:50 +0200217 uint64_t disk_size;
Kevin Wolfc2c9a462011-11-16 11:35:54 +0100218 uint64_t vm_state_size;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200219 uint32_t date_sec;
220 uint32_t date_nsec;
221 uint64_t vm_clock_nsec;
Max Reitzfcf9a6b2019-10-11 17:28:02 +0200222 /* Size of all extra data, including QCowSnapshotExtraData if available */
223 uint32_t extra_data_size;
224 /* Data beyond QCowSnapshotExtraData, if any */
225 void *unknown_extra_data;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200226} QCowSnapshot;
227
Kevin Wolf49381092011-01-10 17:15:10 +0100228struct Qcow2Cache;
229typedef struct Qcow2Cache Qcow2Cache;
230
Daniel P. Berrange4652b8f2017-06-23 17:24:12 +0100231typedef struct Qcow2CryptoHeaderExtension {
232 uint64_t offset;
233 uint64_t length;
234} QEMU_PACKED Qcow2CryptoHeaderExtension;
235
Kevin Wolf75bab852012-02-02 14:52:08 +0100236typedef struct Qcow2UnknownHeaderExtension {
237 uint32_t magic;
238 uint32_t len;
239 QLIST_ENTRY(Qcow2UnknownHeaderExtension) next;
240 uint8_t data[];
241} Qcow2UnknownHeaderExtension;
242
Kevin Wolfcfcc4c62012-04-12 15:20:27 +0200243enum {
244 QCOW2_FEAT_TYPE_INCOMPATIBLE = 0,
245 QCOW2_FEAT_TYPE_COMPATIBLE = 1,
246 QCOW2_FEAT_TYPE_AUTOCLEAR = 2,
247};
248
Stefan Hajnoczic61d0002012-07-27 09:05:19 +0100249/* Incompatible feature bits */
250enum {
Kevin Wolf93c24932019-01-14 16:48:25 +0100251 QCOW2_INCOMPAT_DIRTY_BITNR = 0,
252 QCOW2_INCOMPAT_CORRUPT_BITNR = 1,
253 QCOW2_INCOMPAT_DATA_FILE_BITNR = 2,
Denis Plotnikov572ad972020-05-07 11:25:18 +0300254 QCOW2_INCOMPAT_COMPRESSION_BITNR = 3,
Alberto Garcia7be20252020-07-10 18:13:13 +0200255 QCOW2_INCOMPAT_EXTL2_BITNR = 4,
Kevin Wolf93c24932019-01-14 16:48:25 +0100256 QCOW2_INCOMPAT_DIRTY = 1 << QCOW2_INCOMPAT_DIRTY_BITNR,
257 QCOW2_INCOMPAT_CORRUPT = 1 << QCOW2_INCOMPAT_CORRUPT_BITNR,
258 QCOW2_INCOMPAT_DATA_FILE = 1 << QCOW2_INCOMPAT_DATA_FILE_BITNR,
Denis Plotnikov572ad972020-05-07 11:25:18 +0300259 QCOW2_INCOMPAT_COMPRESSION = 1 << QCOW2_INCOMPAT_COMPRESSION_BITNR,
Alberto Garcia7be20252020-07-10 18:13:13 +0200260 QCOW2_INCOMPAT_EXTL2 = 1 << QCOW2_INCOMPAT_EXTL2_BITNR,
Stefan Hajnoczic61d0002012-07-27 09:05:19 +0100261
Kevin Wolf93c24932019-01-14 16:48:25 +0100262 QCOW2_INCOMPAT_MASK = QCOW2_INCOMPAT_DIRTY
Kevin Wolf0e8c08b2019-01-29 17:13:57 +0100263 | QCOW2_INCOMPAT_CORRUPT
Denis Plotnikov572ad972020-05-07 11:25:18 +0300264 | QCOW2_INCOMPAT_DATA_FILE
Alberto Garcia7be20252020-07-10 18:13:13 +0200265 | QCOW2_INCOMPAT_COMPRESSION
266 | QCOW2_INCOMPAT_EXTL2,
Stefan Hajnoczic61d0002012-07-27 09:05:19 +0100267};
268
Stefan Hajnoczibfe80432012-07-27 09:05:22 +0100269/* Compatible feature bits */
270enum {
271 QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR = 0,
272 QCOW2_COMPAT_LAZY_REFCOUNTS = 1 << QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
273
274 QCOW2_COMPAT_FEAT_MASK = QCOW2_COMPAT_LAZY_REFCOUNTS,
275};
276
Vladimir Sementsov-Ogievskiy88ddffa2017-06-28 15:05:08 +0300277/* Autoclear feature bits */
278enum {
Kevin Wolf93c24932019-01-14 16:48:25 +0100279 QCOW2_AUTOCLEAR_BITMAPS_BITNR = 0,
280 QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR = 1,
281 QCOW2_AUTOCLEAR_BITMAPS = 1 << QCOW2_AUTOCLEAR_BITMAPS_BITNR,
282 QCOW2_AUTOCLEAR_DATA_FILE_RAW = 1 << QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR,
Vladimir Sementsov-Ogievskiy88ddffa2017-06-28 15:05:08 +0300283
Kevin Wolf6c3944d2019-02-22 14:29:38 +0100284 QCOW2_AUTOCLEAR_MASK = QCOW2_AUTOCLEAR_BITMAPS
285 | QCOW2_AUTOCLEAR_DATA_FILE_RAW,
Vladimir Sementsov-Ogievskiy88ddffa2017-06-28 15:05:08 +0300286};
287
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +0200288enum qcow2_discard_type {
289 QCOW2_DISCARD_NEVER = 0,
290 QCOW2_DISCARD_ALWAYS,
291 QCOW2_DISCARD_REQUEST,
292 QCOW2_DISCARD_SNAPSHOT,
293 QCOW2_DISCARD_OTHER,
294 QCOW2_DISCARD_MAX
295};
296
Kevin Wolfcfcc4c62012-04-12 15:20:27 +0200297typedef struct Qcow2Feature {
298 uint8_t type;
299 uint8_t bit;
300 char name[46];
301} QEMU_PACKED Qcow2Feature;
302
Kevin Wolf0b919fa2013-06-19 13:44:20 +0200303typedef struct Qcow2DiscardRegion {
304 BlockDriverState *bs;
305 uint64_t offset;
306 uint64_t bytes;
307 QTAILQ_ENTRY(Qcow2DiscardRegion) next;
308} Qcow2DiscardRegion;
309
Max Reitz7453c962015-02-10 15:28:50 -0500310typedef uint64_t Qcow2GetRefcountFunc(const void *refcount_array,
311 uint64_t index);
312typedef void Qcow2SetRefcountFunc(void *refcount_array,
313 uint64_t index, uint64_t value);
314
Vladimir Sementsov-Ogievskiy88ddffa2017-06-28 15:05:08 +0300315typedef struct Qcow2BitmapHeaderExt {
316 uint32_t nb_bitmaps;
317 uint32_t reserved32;
318 uint64_t bitmap_directory_size;
319 uint64_t bitmap_directory_offset;
320} QEMU_PACKED Qcow2BitmapHeaderExt;
321
Vladimir Sementsov-Ogievskiy8ac0f152019-05-06 17:27:41 +0300322#define QCOW2_MAX_THREADS 4
323
Kevin Wolfff991292015-09-07 17:12:56 +0200324typedef struct BDRVQcow2State {
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200325 int cluster_bits;
326 int cluster_size;
Alberto Garcia3c2e5112018-02-05 16:33:13 +0200327 int l2_slice_size;
Alberto Garciad0346b52020-07-10 18:12:51 +0200328 int subcluster_bits;
329 int subcluster_size;
330 int subclusters_per_cluster;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200331 int l2_bits;
332 int l2_size;
333 int l1_size;
334 int l1_vm_state_index;
Max Reitz1d13d652014-10-22 14:09:28 +0200335 int refcount_block_bits;
336 int refcount_block_size;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200337 int csize_shift;
338 int csize_mask;
339 uint64_t cluster_offset_mask;
340 uint64_t l1_table_offset;
341 uint64_t *l1_table;
Kevin Wolf29c1a732011-01-10 17:17:28 +0100342
343 Qcow2Cache* l2_table_cache;
344 Qcow2Cache* refcount_block_cache;
Alberto Garcia279621c2015-08-04 15:14:40 +0300345 QEMUTimer *cache_clean_timer;
346 unsigned cache_clean_interval;
Kevin Wolf29c1a732011-01-10 17:17:28 +0100347
Paolo Bonzinib58deb32018-12-06 11:58:10 +0100348 QLIST_HEAD(, QCowL2Meta) cluster_allocs;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200349
350 uint64_t *refcount_table;
351 uint64_t refcount_table_offset;
352 uint32_t refcount_table_size;
Alberto Garcia7061a072017-02-01 14:38:28 +0200353 uint32_t max_refcount_table_index; /* Last used entry in refcount_table */
Kevin Wolfbb572ae2014-03-26 13:05:51 +0100354 uint64_t free_cluster_index;
355 uint64_t free_byte_offset;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200356
Kevin Wolf68d100e2011-06-30 17:42:09 +0200357 CoMutex lock;
358
Daniel P. Berrange4652b8f2017-06-23 17:24:12 +0100359 Qcow2CryptoHeaderExtension crypto_header; /* QCow2 header extension */
Daniel P. Berrangeb25b3872017-06-23 17:24:10 +0100360 QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
361 QCryptoBlock *crypto; /* Disk encryption format driver */
Daniel P. Berrange4652b8f2017-06-23 17:24:12 +0100362 bool crypt_physical_offset; /* Whether to use virtual or physical offset
363 for encryption initialization vector tweak */
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200364 uint32_t crypt_method_header;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200365 uint64_t snapshots_offset;
366 int snapshots_size;
Kevin Wolfce48f2f42014-03-26 13:05:45 +0100367 unsigned int nb_snapshots;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200368 QCowSnapshot *snapshots;
Anthony Liguori06d92602011-11-14 15:09:46 -0600369
Vladimir Sementsov-Ogievskiy88ddffa2017-06-28 15:05:08 +0300370 uint32_t nb_bitmaps;
371 uint64_t bitmap_directory_size;
372 uint64_t bitmap_directory_offset;
373
Anthony Liguori06d92602011-11-14 15:09:46 -0600374 int flags;
Kevin Wolf6744cba2011-12-15 12:20:58 +0100375 int qcow_version;
Kevin Wolf74c45102013-03-15 10:35:08 +0100376 bool use_lazy_refcounts;
Max Reitzb6481f32013-09-03 10:09:53 +0200377 int refcount_order;
Max Reitz346a53d2015-02-10 15:28:43 -0500378 int refcount_bits;
379 uint64_t refcount_max;
Kevin Wolf6744cba2011-12-15 12:20:58 +0100380
Max Reitz7453c962015-02-10 15:28:50 -0500381 Qcow2GetRefcountFunc *get_refcount;
382 Qcow2SetRefcountFunc *set_refcount;
383
Kevin Wolf67af6742013-06-19 13:44:19 +0200384 bool discard_passthrough[QCOW2_DISCARD_MAX];
385
Max Reitz3e355392013-10-10 11:09:24 +0200386 int overlap_check; /* bitmask of Qcow2MetadataOverlap values */
Max Reitz85186eb2014-09-05 16:07:16 +0200387 bool signaled_corruption;
Max Reitz3e355392013-10-10 11:09:24 +0200388
Kevin Wolf6744cba2011-12-15 12:20:58 +0100389 uint64_t incompatible_features;
390 uint64_t compatible_features;
391 uint64_t autoclear_features;
392
393 size_t unknown_header_fields_size;
394 void* unknown_header_fields;
Kevin Wolf75bab852012-02-02 14:52:08 +0100395 QLIST_HEAD(, Qcow2UnknownHeaderExtension) unknown_header_ext;
Kevin Wolf0b919fa2013-06-19 13:44:20 +0200396 QTAILQ_HEAD (, Qcow2DiscardRegion) discards;
397 bool cache_discards;
Kevin Wolfe4603fe2015-04-07 15:03:16 +0200398
399 /* Backing file path and format as stored in the image (this is not the
400 * effective path/format, which may be the result of a runtime option
401 * override) */
402 char *image_backing_file;
403 char *image_backing_format;
Kevin Wolf9b890bd2019-01-15 19:02:40 +0100404 char *image_data_file;
Vladimir Sementsov-Ogievskiyceb029c2018-06-20 17:48:37 +0300405
Vladimir Sementsov-Ogievskiy6f13a312019-05-06 17:27:38 +0300406 CoQueue thread_task_queue;
407 int nb_threads;
Kevin Wolf93c24932019-01-14 16:48:25 +0100408
409 BdrvChild *data_file;
Vladimir Sementsov-Ogievskiy69f47502019-04-08 19:26:17 +0300410
411 bool metadata_preallocation_checked;
412 bool metadata_preallocation;
Denis Plotnikov572ad972020-05-07 11:25:18 +0300413 /*
414 * Compression type used for the image. Default: 0 - ZLIB
415 * The image compression type is set on image creation.
416 * For now, the only way to change the compression type
417 * is to convert the image with the desired compression type set.
418 */
419 Qcow2CompressionType compression_type;
Kevin Wolfff991292015-09-07 17:12:56 +0200420} BDRVQcow2State;
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200421
Kevin Wolf593fb832012-12-07 18:08:43 +0100422typedef struct Qcow2COWRegion {
423 /**
424 * Offset of the COW region in bytes from the start of the first cluster
425 * touched by the request.
426 */
Alberto Garciae034f5b2017-06-19 16:40:03 +0300427 unsigned offset;
Kevin Wolf593fb832012-12-07 18:08:43 +0100428
Kevin Wolf85567392016-06-01 14:08:56 +0200429 /** Number of bytes to copy */
Alberto Garciae034f5b2017-06-19 16:40:03 +0300430 unsigned nb_bytes;
Kevin Wolf593fb832012-12-07 18:08:43 +0100431} Qcow2COWRegion;
432
Kevin Wolff50f88b2012-12-07 18:08:46 +0100433/**
434 * Describes an in-flight (part of a) write request that writes to clusters
435 * that are not referenced in their L2 table yet.
436 */
Kevin Wolf45aba422009-05-28 16:07:05 +0200437typedef struct QCowL2Meta
438{
Kevin Wolf1d3afd62012-12-07 18:08:42 +0100439 /** Guest offset of the first newly allocated cluster */
Kevin Wolf45aba422009-05-28 16:07:05 +0200440 uint64_t offset;
Kevin Wolf1d3afd62012-12-07 18:08:42 +0100441
Kevin Wolf1d3afd62012-12-07 18:08:42 +0100442 /** Host offset of the first newly allocated cluster */
Kevin Wolf250196f2012-03-02 14:10:54 +0100443 uint64_t alloc_offset;
Kevin Wolf1d3afd62012-12-07 18:08:42 +0100444
Kevin Wolf1d3afd62012-12-07 18:08:42 +0100445 /** Number of newly allocated clusters */
Kevin Wolf45aba422009-05-28 16:07:05 +0200446 int nb_clusters;
Kevin Wolf1d3afd62012-12-07 18:08:42 +0100447
Max Reitz564a6b62017-05-04 01:11:18 +0200448 /** Do not free the old clusters */
449 bool keep_old_clusters;
450
Kevin Wolf1d3afd62012-12-07 18:08:42 +0100451 /**
452 * Requests that overlap with this allocation and wait to be restarted
453 * when the allocating request has completed.
454 */
Kevin Wolf68d100e2011-06-30 17:42:09 +0200455 CoQueue dependent_requests;
Kevin Wolff2149782009-08-31 16:48:49 +0200456
Kevin Wolf593fb832012-12-07 18:08:43 +0100457 /**
458 * The COW Region between the start of the first allocated cluster and the
459 * area the guest actually writes to.
460 */
461 Qcow2COWRegion cow_start;
462
463 /**
464 * The COW Region between the area the guest actually writes to and the
465 * end of the last allocated cluster.
466 */
467 Qcow2COWRegion cow_end;
468
Anton Nefedovc8bb23c2019-05-16 17:27:49 +0300469 /*
470 * Indicates that COW regions are already handled and do not require
471 * any more processing.
472 */
473 bool skip_cow;
474
Alberto Garciaee22a9d2017-06-19 16:40:08 +0300475 /**
Alberto Garcia40dee942020-07-10 18:13:12 +0200476 * Indicates that this is not a normal write request but a preallocation.
477 * If the image has extended L2 entries this means that no new individual
478 * subclusters will be marked as allocated in the L2 bitmap (but any
479 * existing contents of that bitmap will be kept).
480 */
481 bool prealloc;
482
483 /**
Alberto Garciaee22a9d2017-06-19 16:40:08 +0300484 * The I/O vector with the data from the actual guest write request.
485 * If non-NULL, this is meant to be merged together with the data
486 * from @cow_start and @cow_end into one single write operation.
487 */
488 QEMUIOVector *data_qiov;
Vladimir Sementsov-Ogievskiy53962342019-06-04 19:15:14 +0300489 size_t data_qiov_offset;
Alberto Garciaee22a9d2017-06-19 16:40:08 +0300490
Kevin Wolf88c65882013-03-26 17:50:11 +0100491 /** Pointer to next L2Meta of the same write request */
492 struct QCowL2Meta *next;
493
Blue Swirl72cf2d42009-09-12 07:36:22 +0000494 QLIST_ENTRY(QCowL2Meta) next_in_flight;
Kevin Wolf45aba422009-05-28 16:07:05 +0200495} QCowL2Meta;
496
Alberto Garcia34905d82020-07-10 18:12:56 +0200497/*
498 * In images with standard L2 entries all clusters are treated as if
499 * they had one subcluster so QCow2ClusterType and QCow2SubclusterType
500 * can be mapped to each other and have the exact same meaning
501 * (QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC cannot happen in these images).
502 *
503 * In images with extended L2 entries QCow2ClusterType refers to the
504 * complete cluster and QCow2SubclusterType to each of the individual
505 * subclusters, so there are several possible combinations:
506 *
507 * |--------------+---------------------------|
508 * | Cluster type | Possible subcluster types |
509 * |--------------+---------------------------|
510 * | UNALLOCATED | UNALLOCATED_PLAIN |
511 * | | ZERO_PLAIN |
512 * |--------------+---------------------------|
513 * | NORMAL | UNALLOCATED_ALLOC |
514 * | | ZERO_ALLOC |
515 * | | NORMAL |
516 * |--------------+---------------------------|
517 * | COMPRESSED | COMPRESSED |
518 * |--------------+---------------------------|
519 *
520 * QCOW2_SUBCLUSTER_INVALID means that the L2 entry is incorrect and
521 * the image should be marked corrupt.
522 */
523
Eric Blake3ef95212017-05-06 19:05:45 -0500524typedef enum QCow2ClusterType {
Kevin Wolf68d000a2012-03-14 19:15:03 +0100525 QCOW2_CLUSTER_UNALLOCATED,
Eric Blakefdfab372017-05-06 19:05:46 -0500526 QCOW2_CLUSTER_ZERO_PLAIN,
527 QCOW2_CLUSTER_ZERO_ALLOC,
Kevin Wolf68d000a2012-03-14 19:15:03 +0100528 QCOW2_CLUSTER_NORMAL,
529 QCOW2_CLUSTER_COMPRESSED,
Eric Blake3ef95212017-05-06 19:05:45 -0500530} QCow2ClusterType;
Kevin Wolf68d000a2012-03-14 19:15:03 +0100531
Alberto Garcia34905d82020-07-10 18:12:56 +0200532typedef enum QCow2SubclusterType {
533 QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN,
534 QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC,
535 QCOW2_SUBCLUSTER_ZERO_PLAIN,
536 QCOW2_SUBCLUSTER_ZERO_ALLOC,
537 QCOW2_SUBCLUSTER_NORMAL,
538 QCOW2_SUBCLUSTER_COMPRESSED,
539 QCOW2_SUBCLUSTER_INVALID,
540} QCow2SubclusterType;
541
Max Reitza40f1c22013-08-30 14:34:25 +0200542typedef enum QCow2MetadataOverlap {
Vladimir Sementsov-Ogievskiy0e4e4312018-07-05 18:15:15 +0300543 QCOW2_OL_MAIN_HEADER_BITNR = 0,
544 QCOW2_OL_ACTIVE_L1_BITNR = 1,
545 QCOW2_OL_ACTIVE_L2_BITNR = 2,
546 QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
547 QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
548 QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
549 QCOW2_OL_INACTIVE_L1_BITNR = 6,
550 QCOW2_OL_INACTIVE_L2_BITNR = 7,
551 QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
Max Reitza40f1c22013-08-30 14:34:25 +0200552
Vladimir Sementsov-Ogievskiy0e4e4312018-07-05 18:15:15 +0300553 QCOW2_OL_MAX_BITNR = 9,
Max Reitza40f1c22013-08-30 14:34:25 +0200554
Vladimir Sementsov-Ogievskiy0e4e4312018-07-05 18:15:15 +0300555 QCOW2_OL_NONE = 0,
556 QCOW2_OL_MAIN_HEADER = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
557 QCOW2_OL_ACTIVE_L1 = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
558 QCOW2_OL_ACTIVE_L2 = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
559 QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
560 QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
561 QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
562 QCOW2_OL_INACTIVE_L1 = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
Max Reitza40f1c22013-08-30 14:34:25 +0200563 /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
564 * reads. */
Vladimir Sementsov-Ogievskiy0e4e4312018-07-05 18:15:15 +0300565 QCOW2_OL_INACTIVE_L2 = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
566 QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
Max Reitza40f1c22013-08-30 14:34:25 +0200567} QCow2MetadataOverlap;
568
Max Reitz4a273c32013-10-10 11:09:27 +0200569/* Perform all overlap checks which can be done in constant time */
570#define QCOW2_OL_CONSTANT \
571 (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
Vladimir Sementsov-Ogievskiy0e4e4312018-07-05 18:15:15 +0300572 QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
Max Reitz4a273c32013-10-10 11:09:27 +0200573
Max Reitza40f1c22013-08-30 14:34:25 +0200574/* Perform all overlap checks which don't require disk access */
575#define QCOW2_OL_CACHED \
Max Reitz4a273c32013-10-10 11:09:27 +0200576 (QCOW2_OL_CONSTANT | QCOW2_OL_ACTIVE_L2 | QCOW2_OL_REFCOUNT_BLOCK | \
577 QCOW2_OL_INACTIVE_L1)
578
579/* Perform all overlap checks */
580#define QCOW2_OL_ALL \
581 (QCOW2_OL_CACHED | QCOW2_OL_INACTIVE_L2)
Max Reitza40f1c22013-08-30 14:34:25 +0200582
Hu Tao46bae922014-01-21 11:30:02 +0800583#define L1E_OFFSET_MASK 0x00fffffffffffe00ULL
584#define L2E_OFFSET_MASK 0x00fffffffffffe00ULL
Kevin Wolf68d000a2012-03-14 19:15:03 +0100585#define L2E_COMPRESSED_OFFSET_SIZE_MASK 0x3fffffffffffffffULL
586
Hu Tao46bae922014-01-21 11:30:02 +0800587#define REFT_OFFSET_MASK 0xfffffffffffffe00ULL
Kevin Wolf76dc9e02012-03-16 14:09:08 +0100588
Kevin Wolfc6d619c2019-01-18 14:40:36 +0100589#define INV_OFFSET (-1ULL)
590
Alberto Garciaa3c7d912020-07-10 18:12:50 +0200591static inline bool has_subclusters(BDRVQcow2State *s)
592{
Alberto Garcia7be20252020-07-10 18:13:13 +0200593 return s->incompatible_features & QCOW2_INCOMPAT_EXTL2;
Alberto Garciaa3c7d912020-07-10 18:12:50 +0200594}
595
Alberto Garciac8fd8552020-07-10 18:12:54 +0200596static inline size_t l2_entry_size(BDRVQcow2State *s)
597{
598 return has_subclusters(s) ? L2E_SIZE_EXTENDED : L2E_SIZE_NORMAL;
599}
600
Alberto Garcia12c6aeb2020-07-10 18:12:48 +0200601static inline uint64_t get_l2_entry(BDRVQcow2State *s, uint64_t *l2_slice,
602 int idx)
603{
Alberto Garcia39a9f0a2020-07-10 18:12:55 +0200604 idx *= l2_entry_size(s) / sizeof(uint64_t);
Alberto Garcia12c6aeb2020-07-10 18:12:48 +0200605 return be64_to_cpu(l2_slice[idx]);
606}
607
Alberto Garcia39a9f0a2020-07-10 18:12:55 +0200608static inline uint64_t get_l2_bitmap(BDRVQcow2State *s, uint64_t *l2_slice,
609 int idx)
610{
611 if (has_subclusters(s)) {
612 idx *= l2_entry_size(s) / sizeof(uint64_t);
613 return be64_to_cpu(l2_slice[idx + 1]);
614 } else {
615 return 0; /* For convenience only; this value has no meaning. */
616 }
617}
618
Alberto Garcia12c6aeb2020-07-10 18:12:48 +0200619static inline void set_l2_entry(BDRVQcow2State *s, uint64_t *l2_slice,
620 int idx, uint64_t entry)
621{
Alberto Garcia39a9f0a2020-07-10 18:12:55 +0200622 idx *= l2_entry_size(s) / sizeof(uint64_t);
Alberto Garcia12c6aeb2020-07-10 18:12:48 +0200623 l2_slice[idx] = cpu_to_be64(entry);
624}
625
Alberto Garcia39a9f0a2020-07-10 18:12:55 +0200626static inline void set_l2_bitmap(BDRVQcow2State *s, uint64_t *l2_slice,
627 int idx, uint64_t bitmap)
628{
629 assert(has_subclusters(s));
630 idx *= l2_entry_size(s) / sizeof(uint64_t);
631 l2_slice[idx + 1] = cpu_to_be64(bitmap);
632}
633
Kevin Wolf93c24932019-01-14 16:48:25 +0100634static inline bool has_data_file(BlockDriverState *bs)
635{
636 BDRVQcow2State *s = bs->opaque;
637 return (s->data_file != bs->file);
638}
639
Kevin Wolf6c3944d2019-02-22 14:29:38 +0100640static inline bool data_file_is_raw(BlockDriverState *bs)
641{
642 BDRVQcow2State *s = bs->opaque;
643 return !!(s->autoclear_features & QCOW2_AUTOCLEAR_DATA_FILE_RAW);
644}
645
Kevin Wolfff991292015-09-07 17:12:56 +0200646static inline int64_t start_of_cluster(BDRVQcow2State *s, int64_t offset)
Kevin Wolf3b8e2e22013-03-26 17:50:02 +0100647{
648 return offset & ~(s->cluster_size - 1);
649}
650
Kevin Wolfff991292015-09-07 17:12:56 +0200651static inline int64_t offset_into_cluster(BDRVQcow2State *s, int64_t offset)
Kevin Wolfc37f4cd2013-03-26 17:50:03 +0100652{
653 return offset & (s->cluster_size - 1);
654}
655
Alberto Garcia3e719812020-07-10 18:12:53 +0200656static inline int64_t offset_into_subcluster(BDRVQcow2State *s, int64_t offset)
657{
658 return offset & (s->subcluster_size - 1);
659}
660
Max Reitzb6d36de2015-09-14 16:39:47 +0200661static inline uint64_t size_to_clusters(BDRVQcow2State *s, uint64_t size)
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200662{
663 return (size + (s->cluster_size - 1)) >> s->cluster_bits;
664}
665
Alberto Garcia3e719812020-07-10 18:12:53 +0200666static inline uint64_t size_to_subclusters(BDRVQcow2State *s, uint64_t size)
667{
668 return (size + (s->subcluster_size - 1)) >> s->subcluster_bits;
669}
670
Kevin Wolfff991292015-09-07 17:12:56 +0200671static inline int64_t size_to_l1(BDRVQcow2State *s, int64_t size)
Stefan Hajnoczi419b19d2010-04-28 11:36:11 +0100672{
673 int shift = s->cluster_bits + s->l2_bits;
674 return (size + (1ULL << shift) - 1) >> shift;
675}
676
Alberto Garcia05b5b6e2018-02-05 16:33:12 +0200677static inline int offset_to_l1_index(BDRVQcow2State *s, uint64_t offset)
678{
679 return offset >> (s->l2_bits + s->cluster_bits);
680}
681
Kevin Wolfff991292015-09-07 17:12:56 +0200682static inline int offset_to_l2_index(BDRVQcow2State *s, int64_t offset)
Kevin Wolf17a71e52013-03-26 17:49:56 +0100683{
684 return (offset >> s->cluster_bits) & (s->l2_size - 1);
685}
686
Alberto Garcia8f818172018-02-05 16:33:14 +0200687static inline int offset_to_l2_slice_index(BDRVQcow2State *s, int64_t offset)
688{
689 return (offset >> s->cluster_bits) & (s->l2_slice_size - 1);
690}
691
Alberto Garciaa53e8b72020-07-10 18:12:52 +0200692static inline int offset_to_sc_index(BDRVQcow2State *s, int64_t offset)
693{
694 return (offset >> s->subcluster_bits) & (s->subclusters_per_cluster - 1);
695}
696
Kevin Wolfff991292015-09-07 17:12:56 +0200697static inline int64_t qcow2_vm_state_offset(BDRVQcow2State *s)
Kevin Wolf1ebf5612013-09-06 12:20:08 +0200698{
699 return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
700}
701
Kevin Wolf808c2bb2019-01-17 18:42:40 +0100702static inline QCow2ClusterType qcow2_get_cluster_type(BlockDriverState *bs,
703 uint64_t l2_entry)
Kevin Wolf68d000a2012-03-14 19:15:03 +0100704{
Alberto Garcia34905d82020-07-10 18:12:56 +0200705 BDRVQcow2State *s = bs->opaque;
706
Kevin Wolf68d000a2012-03-14 19:15:03 +0100707 if (l2_entry & QCOW_OFLAG_COMPRESSED) {
708 return QCOW2_CLUSTER_COMPRESSED;
Alberto Garcia34905d82020-07-10 18:12:56 +0200709 } else if ((l2_entry & QCOW_OFLAG_ZERO) && !has_subclusters(s)) {
Eric Blakefdfab372017-05-06 19:05:46 -0500710 if (l2_entry & L2E_OFFSET_MASK) {
711 return QCOW2_CLUSTER_ZERO_ALLOC;
712 }
713 return QCOW2_CLUSTER_ZERO_PLAIN;
Kevin Wolf68d000a2012-03-14 19:15:03 +0100714 } else if (!(l2_entry & L2E_OFFSET_MASK)) {
Kevin Wolfa4ea1842019-01-29 13:47:24 +0100715 /* Offset 0 generally means unallocated, but it is ambiguous with
716 * external data files because 0 is a valid offset there. However, all
717 * clusters in external data files always have refcount 1, so we can
718 * rely on QCOW_OFLAG_COPIED to disambiguate. */
719 if (has_data_file(bs) && (l2_entry & QCOW_OFLAG_COPIED)) {
720 return QCOW2_CLUSTER_NORMAL;
721 } else {
722 return QCOW2_CLUSTER_UNALLOCATED;
723 }
Kevin Wolf68d000a2012-03-14 19:15:03 +0100724 } else {
725 return QCOW2_CLUSTER_NORMAL;
726 }
727}
728
Alberto Garcia34905d82020-07-10 18:12:56 +0200729/*
Alberto Garcia34905d82020-07-10 18:12:56 +0200730 * In an image without subsclusters @l2_bitmap is ignored and
731 * @sc_index must be 0.
732 * Return QCOW2_SUBCLUSTER_INVALID if an invalid l2 entry is detected
733 * (this checks the whole entry and bitmap, not only the bits related
734 * to subcluster @sc_index).
735 */
736static inline
737QCow2SubclusterType qcow2_get_subcluster_type(BlockDriverState *bs,
738 uint64_t l2_entry,
739 uint64_t l2_bitmap,
740 unsigned sc_index)
741{
742 BDRVQcow2State *s = bs->opaque;
743 QCow2ClusterType type = qcow2_get_cluster_type(bs, l2_entry);
744 assert(sc_index < s->subclusters_per_cluster);
745
746 if (has_subclusters(s)) {
747 switch (type) {
748 case QCOW2_CLUSTER_COMPRESSED:
749 return QCOW2_SUBCLUSTER_COMPRESSED;
750 case QCOW2_CLUSTER_NORMAL:
751 if ((l2_bitmap >> 32) & l2_bitmap) {
752 return QCOW2_SUBCLUSTER_INVALID;
753 } else if (l2_bitmap & QCOW_OFLAG_SUB_ZERO(sc_index)) {
754 return QCOW2_SUBCLUSTER_ZERO_ALLOC;
755 } else if (l2_bitmap & QCOW_OFLAG_SUB_ALLOC(sc_index)) {
756 return QCOW2_SUBCLUSTER_NORMAL;
757 } else {
758 return QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC;
759 }
760 case QCOW2_CLUSTER_UNALLOCATED:
761 if (l2_bitmap & QCOW_L2_BITMAP_ALL_ALLOC) {
762 return QCOW2_SUBCLUSTER_INVALID;
763 } else if (l2_bitmap & QCOW_OFLAG_SUB_ZERO(sc_index)) {
764 return QCOW2_SUBCLUSTER_ZERO_PLAIN;
765 } else {
766 return QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN;
767 }
768 default:
769 g_assert_not_reached();
770 }
771 } else {
Alberto Garcia3f9c6b32020-07-10 18:13:03 +0200772 switch (type) {
773 case QCOW2_CLUSTER_COMPRESSED:
774 return QCOW2_SUBCLUSTER_COMPRESSED;
775 case QCOW2_CLUSTER_ZERO_PLAIN:
776 return QCOW2_SUBCLUSTER_ZERO_PLAIN;
777 case QCOW2_CLUSTER_ZERO_ALLOC:
778 return QCOW2_SUBCLUSTER_ZERO_ALLOC;
779 case QCOW2_CLUSTER_NORMAL:
780 return QCOW2_SUBCLUSTER_NORMAL;
781 case QCOW2_CLUSTER_UNALLOCATED:
782 return QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN;
783 default:
784 g_assert_not_reached();
785 }
Alberto Garcia34905d82020-07-10 18:12:56 +0200786 }
787}
788
Alberto Garciac94d0372020-07-10 18:12:58 +0200789static inline bool qcow2_cluster_is_allocated(QCow2ClusterType type)
790{
791 return (type == QCOW2_CLUSTER_COMPRESSED || type == QCOW2_CLUSTER_NORMAL ||
792 type == QCOW2_CLUSTER_ZERO_ALLOC);
793}
794
Stefan Hajnoczibfe80432012-07-27 09:05:22 +0100795/* Check whether refcounts are eager or lazy */
Kevin Wolfff991292015-09-07 17:12:56 +0200796static inline bool qcow2_need_accurate_refcounts(BDRVQcow2State *s)
Stefan Hajnoczibfe80432012-07-27 09:05:22 +0100797{
798 return !(s->incompatible_features & QCOW2_INCOMPAT_DIRTY);
799}
Kevin Wolfc1424422009-05-28 16:07:06 +0200800
Kevin Wolf65eb2e32013-03-26 17:49:58 +0100801static inline uint64_t l2meta_cow_start(QCowL2Meta *m)
802{
803 return m->offset + m->cow_start.offset;
804}
805
806static inline uint64_t l2meta_cow_end(QCowL2Meta *m)
807{
Kevin Wolf85567392016-06-01 14:08:56 +0200808 return m->offset + m->cow_end.offset + m->cow_end.nb_bytes;
Kevin Wolf65eb2e32013-03-26 17:49:58 +0100809}
810
Max Reitz0e065282015-02-10 15:28:48 -0500811static inline uint64_t refcount_diff(uint64_t r1, uint64_t r2)
Max Reitz2aabe7c2015-02-10 15:28:47 -0500812{
813 return r1 > r2 ? r1 - r2 : r2 - r1;
814}
815
Pavel Butsykin46b732c2017-09-18 15:42:29 +0300816static inline
817uint32_t offset_to_reftable_index(BDRVQcow2State *s, uint64_t offset)
818{
819 return offset >> (s->refcount_block_bits + s->cluster_bits);
820}
821
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200822/* qcow2.c functions */
Max Reitz12cc30a2017-06-13 22:21:03 +0200823int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
824 int refcount_order, bool generous_increase,
825 uint64_t *refblock_count);
826
Kevin Wolf280d3732012-12-07 18:08:47 +0100827int qcow2_mark_dirty(BlockDriverState *bs);
Max Reitz69c98722013-08-30 14:34:24 +0200828int qcow2_mark_corrupt(BlockDriverState *bs);
829int qcow2_mark_consistent(BlockDriverState *bs);
Kevin Wolfe24e49e2012-02-02 12:32:31 +0100830int qcow2_update_header(BlockDriverState *bs);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200831
Max Reitz85186eb2014-09-05 16:07:16 +0200832void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
833 int64_t size, const char *message_format, ...)
834 GCC_FMT_ATTR(5, 6);
835
Alberto Garcia0cf0e592018-03-06 18:14:06 +0200836int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
837 uint64_t entries, size_t entry_len,
838 int64_t max_size_bytes, const char *table_name,
839 Error **errp);
840
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200841/* qcow2-refcount.c functions */
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200842int qcow2_refcount_init(BlockDriverState *bs);
843void qcow2_refcount_close(BlockDriverState *bs);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200844
Max Reitz7324c102015-02-10 15:28:46 -0500845int qcow2_get_refcount(BlockDriverState *bs, int64_t cluster_index,
Max Reitz0e065282015-02-10 15:28:48 -0500846 uint64_t *refcount);
Max Reitz44751912014-10-27 11:12:54 +0100847
Max Reitz32b64442013-09-03 10:09:52 +0200848int qcow2_update_cluster_refcount(BlockDriverState *bs, int64_t cluster_index,
Max Reitz0e065282015-02-10 15:28:48 -0500849 uint64_t addend, bool decrease,
Max Reitz2aabe7c2015-02-10 15:28:47 -0500850 enum qcow2_discard_type type);
Max Reitz32b64442013-09-03 10:09:52 +0200851
Max Reitz772d1f92017-06-13 22:21:05 +0200852int64_t qcow2_refcount_area(BlockDriverState *bs, uint64_t offset,
853 uint64_t additional_clusters, bool exact_size,
854 int new_refblock_index,
855 uint64_t new_refblock_offset);
856
Kevin Wolfbb572ae2014-03-26 13:05:51 +0100857int64_t qcow2_alloc_clusters(BlockDriverState *bs, uint64_t size);
Max Reitzb6d36de2015-09-14 16:39:47 +0200858int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
859 int64_t nb_clusters);
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200860int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size);
861void qcow2_free_clusters(BlockDriverState *bs,
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +0200862 int64_t offset, int64_t size,
863 enum qcow2_discard_type type);
864void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
865 int nb_clusters, enum qcow2_discard_type type);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200866
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200867int qcow2_update_snapshot_refcount(BlockDriverState *bs,
868 int64_t l1_table_offset, int l1_size, int addend);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200869
Paolo Bonzini8b220eb2018-03-01 17:36:14 +0100870int coroutine_fn qcow2_flush_caches(BlockDriverState *bs);
871int coroutine_fn qcow2_write_caches(BlockDriverState *bs);
Kevin Wolf166acf52012-05-11 18:18:36 +0200872int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
873 BdrvCheckMode fix);
Kevin Wolff7d0fe02009-05-28 16:07:04 +0200874
Kevin Wolf0b919fa2013-06-19 13:44:20 +0200875void qcow2_process_discards(BlockDriverState *bs, int ret);
876
Max Reitz231bb262013-10-10 11:09:23 +0200877int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
Max Reitza40f1c22013-08-30 14:34:25 +0200878 int64_t size);
Max Reitz231bb262013-10-10 11:09:23 +0200879int qcow2_pre_write_overlap_check(BlockDriverState *bs, int ign, int64_t offset,
Kevin Wolf966b0002019-01-15 20:39:06 +0100880 int64_t size, bool data_file);
Vladimir Sementsov-Ogievskiy8a5bb1f2017-06-28 15:05:07 +0300881int qcow2_inc_refcounts_imrt(BlockDriverState *bs, BdrvCheckResult *res,
882 void **refcount_table,
883 int64_t *refcount_table_size,
884 int64_t offset, int64_t size);
Max Reitza40f1c22013-08-30 14:34:25 +0200885
Max Reitz791c9a02015-07-27 17:51:37 +0200886int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
887 BlockDriverAmendStatusCB *status_cb,
888 void *cb_opaque, Error **errp);
Pavel Butsykin46b732c2017-09-18 15:42:29 +0300889int qcow2_shrink_reftable(BlockDriverState *bs);
Pavel Butsykin163bc392017-09-29 15:16:13 +0300890int64_t qcow2_get_last_cluster(BlockDriverState *bs, int64_t size);
Vladimir Sementsov-Ogievskiy69f47502019-04-08 19:26:17 +0300891int qcow2_detect_metadata_preallocation(BlockDriverState *bs);
Max Reitz791c9a02015-07-27 17:51:37 +0200892
Kevin Wolf45aba422009-05-28 16:07:05 +0200893/* qcow2-cluster.c functions */
Kevin Wolf2cf7cfa2013-05-14 16:14:33 +0200894int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
895 bool exact_size);
Pavel Butsykin46b732c2017-09-18 15:42:29 +0300896int qcow2_shrink_l1_table(BlockDriverState *bs, uint64_t max_size);
Max Reitze23e4002013-08-30 14:34:28 +0200897int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
Kevin Wolfff991292015-09-07 17:12:56 +0200898int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num,
Daniel P. Berrange446d3062017-06-23 17:24:09 +0100899 uint8_t *buf, int nb_sectors, bool enc, Error **errp);
Kevin Wolf45aba422009-05-28 16:07:05 +0200900
Alberto Garcia388e5812020-07-10 18:12:44 +0200901int qcow2_get_host_offset(BlockDriverState *bs, uint64_t offset,
Alberto Garciaca4a0bb2020-07-10 18:12:59 +0200902 unsigned int *bytes, uint64_t *host_offset,
Alberto Garcia10dabdc52020-07-10 18:13:00 +0200903 QCow2SubclusterType *subcluster_type);
Kevin Wolff4f0d392010-02-02 15:20:57 +0100904int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
Kevin Wolfd46a0bb2016-06-01 16:55:05 +0200905 unsigned int *bytes, uint64_t *host_offset,
906 QCowL2Meta **m);
Kevin Wolf77e023f2019-02-27 10:26:24 +0100907int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
908 uint64_t offset,
909 int compressed_size,
910 uint64_t *host_offset);
Kevin Wolf45aba422009-05-28 16:07:05 +0200911
Kevin Wolf148da7e2010-01-20 15:03:01 +0100912int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m);
Kevin Wolf8b24cd12018-06-28 17:05:45 +0200913void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m);
Eric Blaked2cb36a2017-05-06 19:05:52 -0500914int qcow2_cluster_discard(BlockDriverState *bs, uint64_t offset,
915 uint64_t bytes, enum qcow2_discard_type type,
916 bool full_discard);
Alberto Garciaa6841a22020-07-10 18:13:10 +0200917int qcow2_subcluster_zeroize(BlockDriverState *bs, uint64_t offset,
918 uint64_t bytes, int flags);
Kevin Wolf45aba422009-05-28 16:07:05 +0200919
Max Reitz4057a2b2014-10-27 11:12:53 +0100920int qcow2_expand_zero_clusters(BlockDriverState *bs,
Max Reitz8b139762015-07-27 17:51:32 +0200921 BlockDriverAmendStatusCB *status_cb,
922 void *cb_opaque);
Max Reitz32b64442013-09-03 10:09:52 +0200923
Kevin Wolfc1424422009-05-28 16:07:06 +0200924/* qcow2-snapshot.c functions */
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200925int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
926int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800927int qcow2_snapshot_delete(BlockDriverState *bs,
928 const char *snapshot_id,
929 const char *name,
930 Error **errp);
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200931int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
Wenchao Xia7b4c4782013-12-04 17:10:54 +0800932int qcow2_snapshot_load_tmp(BlockDriverState *bs,
933 const char *snapshot_id,
934 const char *name,
935 Error **errp);
Kevin Wolfc1424422009-05-28 16:07:06 +0200936
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200937void qcow2_free_snapshots(BlockDriverState *bs);
Max Reitzecf6c7c2019-10-11 17:28:01 +0200938int qcow2_read_snapshots(BlockDriverState *bs, Error **errp);
Max Reitze0314b52019-10-11 17:28:03 +0200939int qcow2_write_snapshots(BlockDriverState *bs);
Kevin Wolfc1424422009-05-28 16:07:06 +0200940
Max Reitz8bc584f2019-10-11 17:28:06 +0200941int coroutine_fn qcow2_check_read_snapshot_table(BlockDriverState *bs,
942 BdrvCheckResult *result,
943 BdrvCheckMode fix);
Max Reitzfe446b52019-10-11 17:28:07 +0200944int coroutine_fn qcow2_check_fix_snapshot_table(BlockDriverState *bs,
945 BdrvCheckResult *result,
946 BdrvCheckMode fix);
Max Reitz8bc584f2019-10-11 17:28:06 +0200947
Kevin Wolf49381092011-01-10 17:15:10 +0100948/* qcow2-cache.c functions */
Alberto Garcia1221fe62018-02-05 16:33:36 +0200949Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
950 unsigned table_size);
Alberto Garciae64d4072018-02-05 16:33:08 +0200951int qcow2_cache_destroy(Qcow2Cache *c);
Kevin Wolf49381092011-01-10 17:15:10 +0100952
Alberto Garcia2d135ee2018-02-05 16:33:06 +0200953void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
Kevin Wolf49381092011-01-10 17:15:10 +0100954int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
Denis V. Lunevf3c3b872016-06-02 18:58:15 +0300955int qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c);
Kevin Wolf49381092011-01-10 17:15:10 +0100956int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
957 Qcow2Cache *dependency);
Kevin Wolf3de0a292011-01-14 15:55:38 +0100958void qcow2_cache_depends_on_flush(Qcow2Cache *c);
Kevin Wolf49381092011-01-10 17:15:10 +0100959
Alberto Garciab2f68bf2018-02-05 16:33:09 +0200960void qcow2_cache_clean_unused(Qcow2Cache *c);
Max Reitze7108fe2013-09-03 10:09:51 +0200961int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c);
962
Kevin Wolf49381092011-01-10 17:15:10 +0100963int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
964 void **table);
965int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
966 void **table);
Alberto Garcia2013c3d2018-02-05 16:33:07 +0200967void qcow2_cache_put(Qcow2Cache *c, void **table);
Alberto Garcia6e6fa762018-02-05 16:33:11 +0200968void *qcow2_cache_is_table_offset(Qcow2Cache *c, uint64_t offset);
Alberto Garcia77aadd72018-02-05 16:33:10 +0200969void qcow2_cache_discard(Qcow2Cache *c, void *table);
Kevin Wolf49381092011-01-10 17:15:10 +0100970
Vladimir Sementsov-Ogievskiy88ddffa2017-06-28 15:05:08 +0300971/* qcow2-bitmap.c functions */
972int qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
973 void **refcount_table,
974 int64_t *refcount_table_size);
Vladimir Sementsov-Ogievskiy3e99da52018-02-02 19:07:52 +0300975bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp);
Andrey Shinkevichb8968c82019-02-08 18:06:07 +0300976Qcow2BitmapInfoList *qcow2_get_bitmap_info_list(BlockDriverState *bs,
977 Error **errp);
Vladimir Sementsov-Ogievskiy1b6b0562017-06-28 15:05:14 +0300978int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp);
John Snowd19c6b32019-03-11 21:51:46 +0300979int qcow2_truncate_bitmaps_check(BlockDriverState *bs, Error **errp);
Vladimir Sementsov-Ogievskiy644ddbb2019-09-27 15:23:52 +0300980void qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs,
981 bool release_stored, Error **errp);
Vladimir Sementsov-Ogievskiy169b8792017-06-28 15:05:20 +0300982int qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp);
Vladimir Sementsov-Ogievskiyd2c30802019-09-20 11:25:43 +0300983bool qcow2_co_can_store_new_dirty_bitmap(BlockDriverState *bs,
984 const char *name,
985 uint32_t granularity,
Vladimir Sementsov-Ogievskiyb56a1e32019-09-20 11:25:42 +0300986 Error **errp);
Vladimir Sementsov-Ogievskiyd2c30802019-09-20 11:25:43 +0300987int qcow2_co_remove_persistent_dirty_bitmap(BlockDriverState *bs,
988 const char *name,
989 Error **errp);
Eric Blakeef893b52020-05-12 20:16:42 -0500990bool qcow2_supports_persistent_dirty_bitmap(BlockDriverState *bs);
Eric Blake5d72c682020-05-21 14:21:34 -0500991uint64_t qcow2_get_persistent_dirty_bitmap_size(BlockDriverState *bs,
992 uint32_t cluster_size);
Vladimir Sementsov-Ogievskiyd1258dd2017-06-28 15:05:11 +0300993
Vladimir Sementsov-Ogievskiy56e2f1d2019-05-06 17:27:35 +0300994ssize_t coroutine_fn
995qcow2_co_compress(BlockDriverState *bs, void *dest, size_t dest_size,
996 const void *src, size_t src_size);
997ssize_t coroutine_fn
998qcow2_co_decompress(BlockDriverState *bs, void *dest, size_t dest_size,
999 const void *src, size_t src_size);
Vladimir Sementsov-Ogievskiy8ac0f152019-05-06 17:27:41 +03001000int coroutine_fn
Maxim Levitsky603fbd02019-09-15 23:36:54 +03001001qcow2_co_encrypt(BlockDriverState *bs, uint64_t host_offset,
1002 uint64_t guest_offset, void *buf, size_t len);
Vladimir Sementsov-Ogievskiy8ac0f152019-05-06 17:27:41 +03001003int coroutine_fn
Maxim Levitsky603fbd02019-09-15 23:36:54 +03001004qcow2_co_decrypt(BlockDriverState *bs, uint64_t host_offset,
1005 uint64_t guest_offset, void *buf, size_t len);
Vladimir Sementsov-Ogievskiy56e2f1d2019-05-06 17:27:35 +03001006
Kevin Wolff7d0fe02009-05-28 16:07:04 +02001007#endif