xref: /qemu/block/qcow2.c (revision 83c2201fc47bd0dfa656bde7202bd0e2539d54a0)
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 #include "qemu/osdep.h"
26 
27 #include "block/qdict.h"
28 #include "system/block-backend.h"
29 #include "qemu/main-loop.h"
30 #include "qemu/module.h"
31 #include "qcow2.h"
32 #include "qemu/error-report.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-events-block-core.h"
35 #include "qobject/qdict.h"
36 #include "qobject/qstring.h"
37 #include "trace.h"
38 #include "qemu/option_int.h"
39 #include "qemu/cutils.h"
40 #include "qemu/bswap.h"
41 #include "qemu/memalign.h"
42 #include "qapi/qobject-input-visitor.h"
43 #include "qapi/qapi-visit-block-core.h"
44 #include "crypto.h"
45 #include "block/aio_task.h"
46 #include "block/dirty-bitmap.h"
47 
48 /*
49   Differences with QCOW:
50 
51   - Support for multiple incremental snapshots.
52   - Memory management by reference counts.
53   - Clusters which have a reference count of one have the bit
54     QCOW_OFLAG_COPIED to optimize write performance.
55   - Size of compressed clusters is stored in sectors to reduce bit usage
56     in the cluster offsets.
57   - Support for storing additional data (such as the VM state) in the
58     snapshots.
59   - If a backing store is used, the cluster size is not constrained
60     (could be backported to QCOW).
61   - L2 tables have always a size of one cluster.
62 */
63 
64 
65 typedef struct {
66     uint32_t magic;
67     uint32_t len;
68 } QEMU_PACKED QCowExtension;
69 
70 #define  QCOW2_EXT_MAGIC_END 0
71 #define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xe2792aca
72 #define  QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
73 #define  QCOW2_EXT_MAGIC_CRYPTO_HEADER 0x0537be77
74 #define  QCOW2_EXT_MAGIC_BITMAPS 0x23852875
75 #define  QCOW2_EXT_MAGIC_DATA_FILE 0x44415441
76 
77 static int coroutine_fn
78 qcow2_co_preadv_compressed(BlockDriverState *bs,
79                            uint64_t l2_entry,
80                            uint64_t offset,
81                            uint64_t bytes,
82                            QEMUIOVector *qiov,
83                            size_t qiov_offset);
84 
qcow2_probe(const uint8_t * buf,int buf_size,const char * filename)85 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
86 {
87     const QCowHeader *cow_header = (const void *)buf;
88 
89     if (buf_size >= sizeof(QCowHeader) &&
90         be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
91         be32_to_cpu(cow_header->version) >= 2)
92         return 100;
93     else
94         return 0;
95 }
96 
97 
98 static int GRAPH_RDLOCK
qcow2_crypto_hdr_read_func(QCryptoBlock * block,size_t offset,uint8_t * buf,size_t buflen,void * opaque,Error ** errp)99 qcow2_crypto_hdr_read_func(QCryptoBlock *block, size_t offset,
100                            uint8_t *buf, size_t buflen,
101                            void *opaque, Error **errp)
102 {
103     BlockDriverState *bs = opaque;
104     BDRVQcow2State *s = bs->opaque;
105     ssize_t ret;
106 
107     if ((offset + buflen) > s->crypto_header.length) {
108         error_setg(errp, "Request for data outside of extension header");
109         return -1;
110     }
111 
112     ret = bdrv_pread(bs->file, s->crypto_header.offset + offset, buflen, buf,
113                      0);
114     if (ret < 0) {
115         error_setg_errno(errp, -ret, "Could not read encryption header");
116         return -1;
117     }
118     return 0;
119 }
120 
121 
122 static int coroutine_fn GRAPH_RDLOCK
qcow2_crypto_hdr_init_func(QCryptoBlock * block,size_t headerlen,void * opaque,Error ** errp)123 qcow2_crypto_hdr_init_func(QCryptoBlock *block, size_t headerlen, void *opaque,
124                            Error **errp)
125 {
126     BlockDriverState *bs = opaque;
127     BDRVQcow2State *s = bs->opaque;
128     int64_t ret;
129     int64_t clusterlen;
130 
131     ret = qcow2_alloc_clusters(bs, headerlen);
132     if (ret < 0) {
133         error_setg_errno(errp, -ret,
134                          "Cannot allocate cluster for LUKS header size %zu",
135                          headerlen);
136         return -1;
137     }
138 
139     s->crypto_header.length = headerlen;
140     s->crypto_header.offset = ret;
141 
142     /*
143      * Zero fill all space in cluster so it has predictable
144      * content, as we may not initialize some regions of the
145      * header (eg only 1 out of 8 key slots will be initialized)
146      */
147     clusterlen = size_to_clusters(s, headerlen) * s->cluster_size;
148     assert(qcow2_pre_write_overlap_check(bs, 0, ret, clusterlen, false) == 0);
149     ret = bdrv_co_pwrite_zeroes(bs->file, ret, clusterlen, 0);
150     if (ret < 0) {
151         error_setg_errno(errp, -ret, "Could not zero fill encryption header");
152         return -1;
153     }
154 
155     return 0;
156 }
157 
158 
159 /* The graph lock must be held when called in coroutine context */
160 static int coroutine_mixed_fn GRAPH_RDLOCK
qcow2_crypto_hdr_write_func(QCryptoBlock * block,size_t offset,const uint8_t * buf,size_t buflen,void * opaque,Error ** errp)161 qcow2_crypto_hdr_write_func(QCryptoBlock *block, size_t offset,
162                             const uint8_t *buf, size_t buflen,
163                             void *opaque, Error **errp)
164 {
165     BlockDriverState *bs = opaque;
166     BDRVQcow2State *s = bs->opaque;
167     ssize_t ret;
168 
169     if ((offset + buflen) > s->crypto_header.length) {
170         error_setg(errp, "Request for data outside of extension header");
171         return -1;
172     }
173 
174     ret = bdrv_pwrite(bs->file, s->crypto_header.offset + offset, buflen, buf,
175                       0);
176     if (ret < 0) {
177         error_setg_errno(errp, -ret, "Could not read encryption header");
178         return -1;
179     }
180     return 0;
181 }
182 
183 static QDict*
qcow2_extract_crypto_opts(QemuOpts * opts,const char * fmt,Error ** errp)184 qcow2_extract_crypto_opts(QemuOpts *opts, const char *fmt, Error **errp)
185 {
186     QDict *cryptoopts_qdict;
187     QDict *opts_qdict;
188 
189     /* Extract "encrypt." options into a qdict */
190     opts_qdict = qemu_opts_to_qdict(opts, NULL);
191     qdict_extract_subqdict(opts_qdict, &cryptoopts_qdict, "encrypt.");
192     qobject_unref(opts_qdict);
193     qdict_put_str(cryptoopts_qdict, "format", fmt);
194     return cryptoopts_qdict;
195 }
196 
197 /*
198  * read qcow2 extension and fill bs
199  * start reading from start_offset
200  * finish reading upon magic of value 0 or when end_offset reached
201  * unknown magic is skipped (future extension this version knows nothing about)
202  * return 0 upon success, non-0 otherwise
203  */
204 static int coroutine_fn GRAPH_RDLOCK
qcow2_read_extensions(BlockDriverState * bs,uint64_t start_offset,uint64_t end_offset,void ** p_feature_table,int flags,bool * need_update_header,Error ** errp)205 qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
206                       uint64_t end_offset, void **p_feature_table,
207                       int flags, bool *need_update_header, Error **errp)
208 {
209     BDRVQcow2State *s = bs->opaque;
210     QCowExtension ext;
211     uint64_t offset;
212     int ret;
213     Qcow2BitmapHeaderExt bitmaps_ext;
214 
215     if (need_update_header != NULL) {
216         *need_update_header = false;
217     }
218 
219 #ifdef DEBUG_EXT
220     printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
221 #endif
222     offset = start_offset;
223     while (offset < end_offset) {
224 
225 #ifdef DEBUG_EXT
226         /* Sanity check */
227         if (offset > s->cluster_size)
228             printf("qcow2_read_extension: suspicious offset %lu\n", offset);
229 
230         printf("attempting to read extended header in offset %lu\n", offset);
231 #endif
232 
233         ret = bdrv_co_pread(bs->file, offset, sizeof(ext), &ext, 0);
234         if (ret < 0) {
235             error_setg_errno(errp, -ret, "qcow2_read_extension: ERROR: "
236                              "pread fail from offset %" PRIu64, offset);
237             return 1;
238         }
239         ext.magic = be32_to_cpu(ext.magic);
240         ext.len = be32_to_cpu(ext.len);
241         offset += sizeof(ext);
242 #ifdef DEBUG_EXT
243         printf("ext.magic = 0x%x\n", ext.magic);
244 #endif
245         if (offset > end_offset || ext.len > end_offset - offset) {
246             error_setg(errp, "Header extension too large");
247             return -EINVAL;
248         }
249 
250         switch (ext.magic) {
251         case QCOW2_EXT_MAGIC_END:
252             return 0;
253 
254         case QCOW2_EXT_MAGIC_BACKING_FORMAT:
255             if (ext.len >= sizeof(bs->backing_format)) {
256                 error_setg(errp, "ERROR: ext_backing_format: len=%" PRIu32
257                            " too large (>=%zu)", ext.len,
258                            sizeof(bs->backing_format));
259                 return 2;
260             }
261             ret = bdrv_co_pread(bs->file, offset, ext.len, bs->backing_format, 0);
262             if (ret < 0) {
263                 error_setg_errno(errp, -ret, "ERROR: ext_backing_format: "
264                                  "Could not read format name");
265                 return 3;
266             }
267             bs->backing_format[ext.len] = '\0';
268             s->image_backing_format = g_strdup(bs->backing_format);
269 #ifdef DEBUG_EXT
270             printf("Qcow2: Got format extension %s\n", bs->backing_format);
271 #endif
272             break;
273 
274         case QCOW2_EXT_MAGIC_FEATURE_TABLE:
275             if (p_feature_table != NULL) {
276                 void *feature_table = g_malloc0(ext.len + 2 * sizeof(Qcow2Feature));
277                 ret = bdrv_co_pread(bs->file, offset, ext.len, feature_table, 0);
278                 if (ret < 0) {
279                     error_setg_errno(errp, -ret, "ERROR: ext_feature_table: "
280                                      "Could not read table");
281                     g_free(feature_table);
282                     return ret;
283                 }
284 
285                 *p_feature_table = feature_table;
286             }
287             break;
288 
289         case QCOW2_EXT_MAGIC_CRYPTO_HEADER: {
290             unsigned int cflags = 0;
291             if (s->crypt_method_header != QCOW_CRYPT_LUKS) {
292                 error_setg(errp, "CRYPTO header extension only "
293                            "expected with LUKS encryption method");
294                 return -EINVAL;
295             }
296             if (ext.len != sizeof(Qcow2CryptoHeaderExtension)) {
297                 error_setg(errp, "CRYPTO header extension size %u, "
298                            "but expected size %zu", ext.len,
299                            sizeof(Qcow2CryptoHeaderExtension));
300                 return -EINVAL;
301             }
302 
303             ret = bdrv_co_pread(bs->file, offset, ext.len, &s->crypto_header, 0);
304             if (ret < 0) {
305                 error_setg_errno(errp, -ret,
306                                  "Unable to read CRYPTO header extension");
307                 return ret;
308             }
309             s->crypto_header.offset = be64_to_cpu(s->crypto_header.offset);
310             s->crypto_header.length = be64_to_cpu(s->crypto_header.length);
311 
312             if ((s->crypto_header.offset % s->cluster_size) != 0) {
313                 error_setg(errp, "Encryption header offset '%" PRIu64 "' is "
314                            "not a multiple of cluster size '%u'",
315                            s->crypto_header.offset, s->cluster_size);
316                 return -EINVAL;
317             }
318 
319             if (flags & BDRV_O_NO_IO) {
320                 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
321             }
322             s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
323                                            qcow2_crypto_hdr_read_func,
324                                            bs, cflags, errp);
325             if (!s->crypto) {
326                 return -EINVAL;
327             }
328         }   break;
329 
330         case QCOW2_EXT_MAGIC_BITMAPS:
331             if (ext.len != sizeof(bitmaps_ext)) {
332                 error_setg_errno(errp, -ret, "bitmaps_ext: "
333                                  "Invalid extension length");
334                 return -EINVAL;
335             }
336 
337             if (!(s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) {
338                 if (s->qcow_version < 3) {
339                     /* Let's be a bit more specific */
340                     warn_report("This qcow2 v2 image contains bitmaps, but "
341                                 "they may have been modified by a program "
342                                 "without persistent bitmap support; so now "
343                                 "they must all be considered inconsistent");
344                 } else {
345                     warn_report("a program lacking bitmap support "
346                                 "modified this file, so all bitmaps are now "
347                                 "considered inconsistent");
348                 }
349                 error_printf("Some clusters may be leaked, "
350                              "run 'qemu-img check -r' on the image "
351                              "file to fix.");
352                 if (need_update_header != NULL) {
353                     /* Updating is needed to drop invalid bitmap extension. */
354                     *need_update_header = true;
355                 }
356                 break;
357             }
358 
359             ret = bdrv_co_pread(bs->file, offset, ext.len, &bitmaps_ext, 0);
360             if (ret < 0) {
361                 error_setg_errno(errp, -ret, "bitmaps_ext: "
362                                  "Could not read ext header");
363                 return ret;
364             }
365 
366             if (bitmaps_ext.reserved32 != 0) {
367                 error_setg_errno(errp, -ret, "bitmaps_ext: "
368                                  "Reserved field is not zero");
369                 return -EINVAL;
370             }
371 
372             bitmaps_ext.nb_bitmaps = be32_to_cpu(bitmaps_ext.nb_bitmaps);
373             bitmaps_ext.bitmap_directory_size =
374                 be64_to_cpu(bitmaps_ext.bitmap_directory_size);
375             bitmaps_ext.bitmap_directory_offset =
376                 be64_to_cpu(bitmaps_ext.bitmap_directory_offset);
377 
378             if (bitmaps_ext.nb_bitmaps > QCOW2_MAX_BITMAPS) {
379                 error_setg(errp,
380                            "bitmaps_ext: Image has %" PRIu32 " bitmaps, "
381                            "exceeding the QEMU supported maximum of %d",
382                            bitmaps_ext.nb_bitmaps, QCOW2_MAX_BITMAPS);
383                 return -EINVAL;
384             }
385 
386             if (bitmaps_ext.nb_bitmaps == 0) {
387                 error_setg(errp, "found bitmaps extension with zero bitmaps");
388                 return -EINVAL;
389             }
390 
391             if (offset_into_cluster(s, bitmaps_ext.bitmap_directory_offset)) {
392                 error_setg(errp, "bitmaps_ext: "
393                                  "invalid bitmap directory offset");
394                 return -EINVAL;
395             }
396 
397             if (bitmaps_ext.bitmap_directory_size >
398                 QCOW2_MAX_BITMAP_DIRECTORY_SIZE) {
399                 error_setg(errp, "bitmaps_ext: "
400                                  "bitmap directory size (%" PRIu64 ") exceeds "
401                                  "the maximum supported size (%d)",
402                                  bitmaps_ext.bitmap_directory_size,
403                                  QCOW2_MAX_BITMAP_DIRECTORY_SIZE);
404                 return -EINVAL;
405             }
406 
407             s->nb_bitmaps = bitmaps_ext.nb_bitmaps;
408             s->bitmap_directory_offset =
409                     bitmaps_ext.bitmap_directory_offset;
410             s->bitmap_directory_size =
411                     bitmaps_ext.bitmap_directory_size;
412 
413 #ifdef DEBUG_EXT
414             printf("Qcow2: Got bitmaps extension: "
415                    "offset=%" PRIu64 " nb_bitmaps=%" PRIu32 "\n",
416                    s->bitmap_directory_offset, s->nb_bitmaps);
417 #endif
418             break;
419 
420         case QCOW2_EXT_MAGIC_DATA_FILE:
421         {
422             s->image_data_file = g_malloc0(ext.len + 1);
423             ret = bdrv_co_pread(bs->file, offset, ext.len, s->image_data_file, 0);
424             if (ret < 0) {
425                 error_setg_errno(errp, -ret,
426                                  "ERROR: Could not read data file name");
427                 return ret;
428             }
429 #ifdef DEBUG_EXT
430             printf("Qcow2: Got external data file %s\n", s->image_data_file);
431 #endif
432             break;
433         }
434 
435         default:
436             /* unknown magic - save it in case we need to rewrite the header */
437             /* If you add a new feature, make sure to also update the fast
438              * path of qcow2_make_empty() to deal with it. */
439             {
440                 Qcow2UnknownHeaderExtension *uext;
441 
442                 uext = g_malloc0(sizeof(*uext)  + ext.len);
443                 uext->magic = ext.magic;
444                 uext->len = ext.len;
445                 QLIST_INSERT_HEAD(&s->unknown_header_ext, uext, next);
446 
447                 ret = bdrv_co_pread(bs->file, offset, uext->len, uext->data, 0);
448                 if (ret < 0) {
449                     error_setg_errno(errp, -ret, "ERROR: unknown extension: "
450                                      "Could not read data");
451                     return ret;
452                 }
453             }
454             break;
455         }
456 
457         offset += ((ext.len + 7) & ~7);
458     }
459 
460     return 0;
461 }
462 
cleanup_unknown_header_ext(BlockDriverState * bs)463 static void cleanup_unknown_header_ext(BlockDriverState *bs)
464 {
465     BDRVQcow2State *s = bs->opaque;
466     Qcow2UnknownHeaderExtension *uext, *next;
467 
468     QLIST_FOREACH_SAFE(uext, &s->unknown_header_ext, next, next) {
469         QLIST_REMOVE(uext, next);
470         g_free(uext);
471     }
472 }
473 
report_unsupported_feature(Error ** errp,Qcow2Feature * table,uint64_t mask)474 static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
475                                        uint64_t mask)
476 {
477     g_autoptr(GString) features = g_string_sized_new(60);
478 
479     while (table && table->name[0] != '\0') {
480         if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
481             if (mask & (1ULL << table->bit)) {
482                 if (features->len > 0) {
483                     g_string_append(features, ", ");
484                 }
485                 g_string_append_printf(features, "%.46s", table->name);
486                 mask &= ~(1ULL << table->bit);
487             }
488         }
489         table++;
490     }
491 
492     if (mask) {
493         if (features->len > 0) {
494             g_string_append(features, ", ");
495         }
496         g_string_append_printf(features,
497                                "Unknown incompatible feature: %" PRIx64, mask);
498     }
499 
500     error_setg(errp, "Unsupported qcow2 feature(s): %s", features->str);
501 }
502 
503 /*
504  * Sets the dirty bit and flushes afterwards if necessary.
505  *
506  * The incompatible_features bit is only set if the image file header was
507  * updated successfully.  Therefore it is not required to check the return
508  * value of this function.
509  */
qcow2_mark_dirty(BlockDriverState * bs)510 int qcow2_mark_dirty(BlockDriverState *bs)
511 {
512     BDRVQcow2State *s = bs->opaque;
513     uint64_t val;
514     int ret;
515 
516     assert(s->qcow_version >= 3);
517 
518     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
519         return 0; /* already dirty */
520     }
521 
522     val = cpu_to_be64(s->incompatible_features | QCOW2_INCOMPAT_DIRTY);
523     ret = bdrv_pwrite_sync(bs->file,
524                            offsetof(QCowHeader, incompatible_features),
525                            sizeof(val), &val, 0);
526     if (ret < 0) {
527         return ret;
528     }
529 
530     /* Only treat image as dirty if the header was updated successfully */
531     s->incompatible_features |= QCOW2_INCOMPAT_DIRTY;
532     return 0;
533 }
534 
535 /*
536  * Clears the dirty bit and flushes before if necessary.  Only call this
537  * function when there are no pending requests, it does not guard against
538  * concurrent requests dirtying the image.
539  */
qcow2_mark_clean(BlockDriverState * bs)540 static int GRAPH_RDLOCK qcow2_mark_clean(BlockDriverState *bs)
541 {
542     BDRVQcow2State *s = bs->opaque;
543 
544     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
545         int ret;
546 
547         s->incompatible_features &= ~QCOW2_INCOMPAT_DIRTY;
548 
549         ret = qcow2_flush_caches(bs);
550         if (ret < 0) {
551             return ret;
552         }
553 
554         return qcow2_update_header(bs);
555     }
556     return 0;
557 }
558 
559 /*
560  * Marks the image as corrupt.
561  */
qcow2_mark_corrupt(BlockDriverState * bs)562 int qcow2_mark_corrupt(BlockDriverState *bs)
563 {
564     BDRVQcow2State *s = bs->opaque;
565 
566     s->incompatible_features |= QCOW2_INCOMPAT_CORRUPT;
567     return qcow2_update_header(bs);
568 }
569 
570 /*
571  * Marks the image as consistent, i.e., unsets the corrupt bit, and flushes
572  * before if necessary.
573  */
574 static int coroutine_fn GRAPH_RDLOCK
qcow2_mark_consistent(BlockDriverState * bs)575 qcow2_mark_consistent(BlockDriverState *bs)
576 {
577     BDRVQcow2State *s = bs->opaque;
578 
579     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
580         int ret = qcow2_flush_caches(bs);
581         if (ret < 0) {
582             return ret;
583         }
584 
585         s->incompatible_features &= ~QCOW2_INCOMPAT_CORRUPT;
586         return qcow2_update_header(bs);
587     }
588     return 0;
589 }
590 
qcow2_add_check_result(BdrvCheckResult * out,const BdrvCheckResult * src,bool set_allocation_info)591 static void qcow2_add_check_result(BdrvCheckResult *out,
592                                    const BdrvCheckResult *src,
593                                    bool set_allocation_info)
594 {
595     out->corruptions += src->corruptions;
596     out->leaks += src->leaks;
597     out->check_errors += src->check_errors;
598     out->corruptions_fixed += src->corruptions_fixed;
599     out->leaks_fixed += src->leaks_fixed;
600 
601     if (set_allocation_info) {
602         out->image_end_offset = src->image_end_offset;
603         out->bfi = src->bfi;
604     }
605 }
606 
607 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_check_locked(BlockDriverState * bs,BdrvCheckResult * result,BdrvCheckMode fix)608 qcow2_co_check_locked(BlockDriverState *bs, BdrvCheckResult *result,
609                       BdrvCheckMode fix)
610 {
611     BdrvCheckResult snapshot_res = {};
612     BdrvCheckResult refcount_res = {};
613     int ret;
614 
615     memset(result, 0, sizeof(*result));
616 
617     ret = qcow2_check_read_snapshot_table(bs, &snapshot_res, fix);
618     if (ret < 0) {
619         qcow2_add_check_result(result, &snapshot_res, false);
620         return ret;
621     }
622 
623     ret = qcow2_check_refcounts(bs, &refcount_res, fix);
624     qcow2_add_check_result(result, &refcount_res, true);
625     if (ret < 0) {
626         qcow2_add_check_result(result, &snapshot_res, false);
627         return ret;
628     }
629 
630     ret = qcow2_check_fix_snapshot_table(bs, &snapshot_res, fix);
631     qcow2_add_check_result(result, &snapshot_res, false);
632     if (ret < 0) {
633         return ret;
634     }
635 
636     if (fix && result->check_errors == 0 && result->corruptions == 0) {
637         ret = qcow2_mark_clean(bs);
638         if (ret < 0) {
639             return ret;
640         }
641         return qcow2_mark_consistent(bs);
642     }
643     return ret;
644 }
645 
646 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_check(BlockDriverState * bs,BdrvCheckResult * result,BdrvCheckMode fix)647 qcow2_co_check(BlockDriverState *bs, BdrvCheckResult *result,
648                BdrvCheckMode fix)
649 {
650     BDRVQcow2State *s = bs->opaque;
651     int ret;
652 
653     qemu_co_mutex_lock(&s->lock);
654     ret = qcow2_co_check_locked(bs, result, fix);
655     qemu_co_mutex_unlock(&s->lock);
656     return ret;
657 }
658 
qcow2_validate_table(BlockDriverState * bs,uint64_t offset,uint64_t entries,size_t entry_len,int64_t max_size_bytes,const char * table_name,Error ** errp)659 int qcow2_validate_table(BlockDriverState *bs, uint64_t offset,
660                          uint64_t entries, size_t entry_len,
661                          int64_t max_size_bytes, const char *table_name,
662                          Error **errp)
663 {
664     BDRVQcow2State *s = bs->opaque;
665 
666     if (entries > max_size_bytes / entry_len) {
667         error_setg(errp, "%s too large", table_name);
668         return -EFBIG;
669     }
670 
671     /* Use signed INT64_MAX as the maximum even for uint64_t header fields,
672      * because values will be passed to qemu functions taking int64_t. */
673     if ((INT64_MAX - entries * entry_len < offset) ||
674         (offset_into_cluster(s, offset) != 0)) {
675         error_setg(errp, "%s offset invalid", table_name);
676         return -EINVAL;
677     }
678 
679     return 0;
680 }
681 
682 static const char *const mutable_opts[] = {
683     QCOW2_OPT_LAZY_REFCOUNTS,
684     QCOW2_OPT_DISCARD_REQUEST,
685     QCOW2_OPT_DISCARD_SNAPSHOT,
686     QCOW2_OPT_DISCARD_OTHER,
687     QCOW2_OPT_DISCARD_NO_UNREF,
688     QCOW2_OPT_OVERLAP,
689     QCOW2_OPT_OVERLAP_TEMPLATE,
690     QCOW2_OPT_OVERLAP_MAIN_HEADER,
691     QCOW2_OPT_OVERLAP_ACTIVE_L1,
692     QCOW2_OPT_OVERLAP_ACTIVE_L2,
693     QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
694     QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
695     QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
696     QCOW2_OPT_OVERLAP_INACTIVE_L1,
697     QCOW2_OPT_OVERLAP_INACTIVE_L2,
698     QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
699     QCOW2_OPT_CACHE_SIZE,
700     QCOW2_OPT_L2_CACHE_SIZE,
701     QCOW2_OPT_L2_CACHE_ENTRY_SIZE,
702     QCOW2_OPT_REFCOUNT_CACHE_SIZE,
703     QCOW2_OPT_CACHE_CLEAN_INTERVAL,
704     NULL
705 };
706 
707 static QemuOptsList qcow2_runtime_opts = {
708     .name = "qcow2",
709     .head = QTAILQ_HEAD_INITIALIZER(qcow2_runtime_opts.head),
710     .desc = {
711         {
712             .name = QCOW2_OPT_LAZY_REFCOUNTS,
713             .type = QEMU_OPT_BOOL,
714             .help = "Postpone refcount updates",
715         },
716         {
717             .name = QCOW2_OPT_DISCARD_REQUEST,
718             .type = QEMU_OPT_BOOL,
719             .help = "Pass guest discard requests to the layer below",
720         },
721         {
722             .name = QCOW2_OPT_DISCARD_SNAPSHOT,
723             .type = QEMU_OPT_BOOL,
724             .help = "Generate discard requests when snapshot related space "
725                     "is freed",
726         },
727         {
728             .name = QCOW2_OPT_DISCARD_OTHER,
729             .type = QEMU_OPT_BOOL,
730             .help = "Generate discard requests when other clusters are freed",
731         },
732         {
733             .name = QCOW2_OPT_DISCARD_NO_UNREF,
734             .type = QEMU_OPT_BOOL,
735             .help = "Do not unreference discarded clusters",
736         },
737         {
738             .name = QCOW2_OPT_OVERLAP,
739             .type = QEMU_OPT_STRING,
740             .help = "Selects which overlap checks to perform from a range of "
741                     "templates (none, constant, cached, all)",
742         },
743         {
744             .name = QCOW2_OPT_OVERLAP_TEMPLATE,
745             .type = QEMU_OPT_STRING,
746             .help = "Selects which overlap checks to perform from a range of "
747                     "templates (none, constant, cached, all)",
748         },
749         {
750             .name = QCOW2_OPT_OVERLAP_MAIN_HEADER,
751             .type = QEMU_OPT_BOOL,
752             .help = "Check for unintended writes into the main qcow2 header",
753         },
754         {
755             .name = QCOW2_OPT_OVERLAP_ACTIVE_L1,
756             .type = QEMU_OPT_BOOL,
757             .help = "Check for unintended writes into the active L1 table",
758         },
759         {
760             .name = QCOW2_OPT_OVERLAP_ACTIVE_L2,
761             .type = QEMU_OPT_BOOL,
762             .help = "Check for unintended writes into an active L2 table",
763         },
764         {
765             .name = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
766             .type = QEMU_OPT_BOOL,
767             .help = "Check for unintended writes into the refcount table",
768         },
769         {
770             .name = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
771             .type = QEMU_OPT_BOOL,
772             .help = "Check for unintended writes into a refcount block",
773         },
774         {
775             .name = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
776             .type = QEMU_OPT_BOOL,
777             .help = "Check for unintended writes into the snapshot table",
778         },
779         {
780             .name = QCOW2_OPT_OVERLAP_INACTIVE_L1,
781             .type = QEMU_OPT_BOOL,
782             .help = "Check for unintended writes into an inactive L1 table",
783         },
784         {
785             .name = QCOW2_OPT_OVERLAP_INACTIVE_L2,
786             .type = QEMU_OPT_BOOL,
787             .help = "Check for unintended writes into an inactive L2 table",
788         },
789         {
790             .name = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
791             .type = QEMU_OPT_BOOL,
792             .help = "Check for unintended writes into the bitmap directory",
793         },
794         {
795             .name = QCOW2_OPT_CACHE_SIZE,
796             .type = QEMU_OPT_SIZE,
797             .help = "Maximum combined metadata (L2 tables and refcount blocks) "
798                     "cache size",
799         },
800         {
801             .name = QCOW2_OPT_L2_CACHE_SIZE,
802             .type = QEMU_OPT_SIZE,
803             .help = "Maximum L2 table cache size",
804         },
805         {
806             .name = QCOW2_OPT_L2_CACHE_ENTRY_SIZE,
807             .type = QEMU_OPT_SIZE,
808             .help = "Size of each entry in the L2 cache",
809         },
810         {
811             .name = QCOW2_OPT_REFCOUNT_CACHE_SIZE,
812             .type = QEMU_OPT_SIZE,
813             .help = "Maximum refcount block cache size",
814         },
815         {
816             .name = QCOW2_OPT_CACHE_CLEAN_INTERVAL,
817             .type = QEMU_OPT_NUMBER,
818             .help = "Clean unused cache entries after this time (in seconds)",
819         },
820         BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",
821             "ID of secret providing qcow2 AES key or LUKS passphrase"),
822         { /* end of list */ }
823     },
824 };
825 
826 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
827     [QCOW2_OL_MAIN_HEADER_BITNR]      = QCOW2_OPT_OVERLAP_MAIN_HEADER,
828     [QCOW2_OL_ACTIVE_L1_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L1,
829     [QCOW2_OL_ACTIVE_L2_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L2,
830     [QCOW2_OL_REFCOUNT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
831     [QCOW2_OL_REFCOUNT_BLOCK_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
832     [QCOW2_OL_SNAPSHOT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
833     [QCOW2_OL_INACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L1,
834     [QCOW2_OL_INACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L2,
835     [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
836 };
837 
cache_clean_timer_cb(void * opaque)838 static void cache_clean_timer_cb(void *opaque)
839 {
840     BlockDriverState *bs = opaque;
841     BDRVQcow2State *s = bs->opaque;
842     qcow2_cache_clean_unused(s->l2_table_cache);
843     qcow2_cache_clean_unused(s->refcount_block_cache);
844     timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
845               (int64_t) s->cache_clean_interval * 1000);
846 }
847 
cache_clean_timer_init(BlockDriverState * bs,AioContext * context)848 static void cache_clean_timer_init(BlockDriverState *bs, AioContext *context)
849 {
850     BDRVQcow2State *s = bs->opaque;
851     if (s->cache_clean_interval > 0) {
852         s->cache_clean_timer =
853             aio_timer_new_with_attrs(context, QEMU_CLOCK_VIRTUAL,
854                                      SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
855                                      cache_clean_timer_cb, bs);
856         timer_mod(s->cache_clean_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
857                   (int64_t) s->cache_clean_interval * 1000);
858     }
859 }
860 
cache_clean_timer_del(BlockDriverState * bs)861 static void cache_clean_timer_del(BlockDriverState *bs)
862 {
863     BDRVQcow2State *s = bs->opaque;
864     if (s->cache_clean_timer) {
865         timer_free(s->cache_clean_timer);
866         s->cache_clean_timer = NULL;
867     }
868 }
869 
qcow2_detach_aio_context(BlockDriverState * bs)870 static void qcow2_detach_aio_context(BlockDriverState *bs)
871 {
872     cache_clean_timer_del(bs);
873 }
874 
qcow2_attach_aio_context(BlockDriverState * bs,AioContext * new_context)875 static void qcow2_attach_aio_context(BlockDriverState *bs,
876                                      AioContext *new_context)
877 {
878     cache_clean_timer_init(bs, new_context);
879 }
880 
read_cache_sizes(BlockDriverState * bs,QemuOpts * opts,uint64_t * l2_cache_size,uint64_t * l2_cache_entry_size,uint64_t * refcount_cache_size,Error ** errp)881 static bool read_cache_sizes(BlockDriverState *bs, QemuOpts *opts,
882                              uint64_t *l2_cache_size,
883                              uint64_t *l2_cache_entry_size,
884                              uint64_t *refcount_cache_size, Error **errp)
885 {
886     BDRVQcow2State *s = bs->opaque;
887     uint64_t combined_cache_size, l2_cache_max_setting;
888     bool l2_cache_size_set, refcount_cache_size_set, combined_cache_size_set;
889     bool l2_cache_entry_size_set;
890     int min_refcount_cache = MIN_REFCOUNT_CACHE_SIZE * s->cluster_size;
891     uint64_t virtual_disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
892     uint64_t max_l2_entries = DIV_ROUND_UP(virtual_disk_size, s->cluster_size);
893     /* An L2 table is always one cluster in size so the max cache size
894      * should be a multiple of the cluster size. */
895     uint64_t max_l2_cache = ROUND_UP(max_l2_entries * l2_entry_size(s),
896                                      s->cluster_size);
897 
898     combined_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_CACHE_SIZE);
899     l2_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_SIZE);
900     refcount_cache_size_set = qemu_opt_get(opts, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
901     l2_cache_entry_size_set = qemu_opt_get(opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE);
902 
903     combined_cache_size = qemu_opt_get_size(opts, QCOW2_OPT_CACHE_SIZE, 0);
904     l2_cache_max_setting = qemu_opt_get_size(opts, QCOW2_OPT_L2_CACHE_SIZE,
905                                              DEFAULT_L2_CACHE_MAX_SIZE);
906     *refcount_cache_size = qemu_opt_get_size(opts,
907                                              QCOW2_OPT_REFCOUNT_CACHE_SIZE, 0);
908 
909     *l2_cache_entry_size = qemu_opt_get_size(
910         opts, QCOW2_OPT_L2_CACHE_ENTRY_SIZE, s->cluster_size);
911 
912     *l2_cache_size = MIN(max_l2_cache, l2_cache_max_setting);
913 
914     if (combined_cache_size_set) {
915         if (l2_cache_size_set && refcount_cache_size_set) {
916             error_setg(errp, QCOW2_OPT_CACHE_SIZE ", " QCOW2_OPT_L2_CACHE_SIZE
917                        " and " QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not be set "
918                        "at the same time");
919             return false;
920         } else if (l2_cache_size_set &&
921                    (l2_cache_max_setting > combined_cache_size)) {
922             error_setg(errp, QCOW2_OPT_L2_CACHE_SIZE " may not exceed "
923                        QCOW2_OPT_CACHE_SIZE);
924             return false;
925         } else if (*refcount_cache_size > combined_cache_size) {
926             error_setg(errp, QCOW2_OPT_REFCOUNT_CACHE_SIZE " may not exceed "
927                        QCOW2_OPT_CACHE_SIZE);
928             return false;
929         }
930 
931         if (l2_cache_size_set) {
932             *refcount_cache_size = combined_cache_size - *l2_cache_size;
933         } else if (refcount_cache_size_set) {
934             *l2_cache_size = combined_cache_size - *refcount_cache_size;
935         } else {
936             /* Assign as much memory as possible to the L2 cache, and
937              * use the remainder for the refcount cache */
938             if (combined_cache_size >= max_l2_cache + min_refcount_cache) {
939                 *l2_cache_size = max_l2_cache;
940                 *refcount_cache_size = combined_cache_size - *l2_cache_size;
941             } else {
942                 *refcount_cache_size =
943                     MIN(combined_cache_size, min_refcount_cache);
944                 *l2_cache_size = combined_cache_size - *refcount_cache_size;
945             }
946         }
947     }
948 
949     /*
950      * If the L2 cache is not enough to cover the whole disk then
951      * default to 4KB entries. Smaller entries reduce the cost of
952      * loads and evictions and increase I/O performance.
953      */
954     if (*l2_cache_size < max_l2_cache && !l2_cache_entry_size_set) {
955         *l2_cache_entry_size = MIN(s->cluster_size, 4096);
956     }
957 
958     /* l2_cache_size and refcount_cache_size are ensured to have at least
959      * their minimum values in qcow2_update_options_prepare() */
960 
961     if (*l2_cache_entry_size < (1 << MIN_CLUSTER_BITS) ||
962         *l2_cache_entry_size > s->cluster_size ||
963         !is_power_of_2(*l2_cache_entry_size)) {
964         error_setg(errp, "L2 cache entry size must be a power of two "
965                    "between %d and the cluster size (%d)",
966                    1 << MIN_CLUSTER_BITS, s->cluster_size);
967         return false;
968     }
969 
970     return true;
971 }
972 
973 typedef struct Qcow2ReopenState {
974     Qcow2Cache *l2_table_cache;
975     Qcow2Cache *refcount_block_cache;
976     int l2_slice_size; /* Number of entries in a slice of the L2 table */
977     bool use_lazy_refcounts;
978     int overlap_check;
979     bool discard_passthrough[QCOW2_DISCARD_MAX];
980     bool discard_no_unref;
981     uint64_t cache_clean_interval;
982     QCryptoBlockOpenOptions *crypto_opts; /* Disk encryption runtime options */
983 } Qcow2ReopenState;
984 
985 static int GRAPH_RDLOCK
qcow2_update_options_prepare(BlockDriverState * bs,Qcow2ReopenState * r,QDict * options,int flags,Error ** errp)986 qcow2_update_options_prepare(BlockDriverState *bs, Qcow2ReopenState *r,
987                              QDict *options, int flags, Error **errp)
988 {
989     BDRVQcow2State *s = bs->opaque;
990     QemuOpts *opts = NULL;
991     const char *opt_overlap_check, *opt_overlap_check_template;
992     int overlap_check_template = 0;
993     uint64_t l2_cache_size, l2_cache_entry_size, refcount_cache_size;
994     int i;
995     const char *encryptfmt;
996     QDict *encryptopts = NULL;
997     int ret;
998 
999     qdict_extract_subqdict(options, &encryptopts, "encrypt.");
1000     encryptfmt = qdict_get_try_str(encryptopts, "format");
1001 
1002     opts = qemu_opts_create(&qcow2_runtime_opts, NULL, 0, &error_abort);
1003     if (!qemu_opts_absorb_qdict(opts, options, errp)) {
1004         ret = -EINVAL;
1005         goto fail;
1006     }
1007 
1008     /* get L2 table/refcount block cache size from command line options */
1009     if (!read_cache_sizes(bs, opts, &l2_cache_size, &l2_cache_entry_size,
1010                           &refcount_cache_size, errp)) {
1011         ret = -EINVAL;
1012         goto fail;
1013     }
1014 
1015     l2_cache_size /= l2_cache_entry_size;
1016     if (l2_cache_size < MIN_L2_CACHE_SIZE) {
1017         l2_cache_size = MIN_L2_CACHE_SIZE;
1018     }
1019     if (l2_cache_size > INT_MAX) {
1020         error_setg(errp, "L2 cache size too big");
1021         ret = -EINVAL;
1022         goto fail;
1023     }
1024 
1025     refcount_cache_size /= s->cluster_size;
1026     if (refcount_cache_size < MIN_REFCOUNT_CACHE_SIZE) {
1027         refcount_cache_size = MIN_REFCOUNT_CACHE_SIZE;
1028     }
1029     if (refcount_cache_size > INT_MAX) {
1030         error_setg(errp, "Refcount cache size too big");
1031         ret = -EINVAL;
1032         goto fail;
1033     }
1034 
1035     /* alloc new L2 table/refcount block cache, flush old one */
1036     if (s->l2_table_cache) {
1037         ret = qcow2_cache_flush(bs, s->l2_table_cache);
1038         if (ret) {
1039             error_setg_errno(errp, -ret, "Failed to flush the L2 table cache");
1040             goto fail;
1041         }
1042     }
1043 
1044     if (s->refcount_block_cache) {
1045         ret = qcow2_cache_flush(bs, s->refcount_block_cache);
1046         if (ret) {
1047             error_setg_errno(errp, -ret,
1048                              "Failed to flush the refcount block cache");
1049             goto fail;
1050         }
1051     }
1052 
1053     r->l2_slice_size = l2_cache_entry_size / l2_entry_size(s);
1054     r->l2_table_cache = qcow2_cache_create(bs, l2_cache_size,
1055                                            l2_cache_entry_size);
1056     r->refcount_block_cache = qcow2_cache_create(bs, refcount_cache_size,
1057                                                  s->cluster_size);
1058     if (r->l2_table_cache == NULL || r->refcount_block_cache == NULL) {
1059         error_setg(errp, "Could not allocate metadata caches");
1060         ret = -ENOMEM;
1061         goto fail;
1062     }
1063 
1064     /* New interval for cache cleanup timer */
1065     r->cache_clean_interval =
1066         qemu_opt_get_number(opts, QCOW2_OPT_CACHE_CLEAN_INTERVAL,
1067                             DEFAULT_CACHE_CLEAN_INTERVAL);
1068 #ifndef CONFIG_LINUX
1069     if (r->cache_clean_interval != 0) {
1070         error_setg(errp, QCOW2_OPT_CACHE_CLEAN_INTERVAL
1071                    " not supported on this host");
1072         ret = -EINVAL;
1073         goto fail;
1074     }
1075 #endif
1076     if (r->cache_clean_interval > UINT_MAX) {
1077         error_setg(errp, "Cache clean interval too big");
1078         ret = -EINVAL;
1079         goto fail;
1080     }
1081 
1082     /* lazy-refcounts; flush if going from enabled to disabled */
1083     r->use_lazy_refcounts = qemu_opt_get_bool(opts, QCOW2_OPT_LAZY_REFCOUNTS,
1084         (s->compatible_features & QCOW2_COMPAT_LAZY_REFCOUNTS));
1085     if (r->use_lazy_refcounts && s->qcow_version < 3) {
1086         error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
1087                    "qemu 1.1 compatibility level");
1088         ret = -EINVAL;
1089         goto fail;
1090     }
1091 
1092     if (s->use_lazy_refcounts && !r->use_lazy_refcounts) {
1093         ret = qcow2_mark_clean(bs);
1094         if (ret < 0) {
1095             error_setg_errno(errp, -ret, "Failed to disable lazy refcounts");
1096             goto fail;
1097         }
1098     }
1099 
1100     /* Overlap check options */
1101     opt_overlap_check = qemu_opt_get(opts, QCOW2_OPT_OVERLAP);
1102     opt_overlap_check_template = qemu_opt_get(opts, QCOW2_OPT_OVERLAP_TEMPLATE);
1103     if (opt_overlap_check_template && opt_overlap_check &&
1104         strcmp(opt_overlap_check_template, opt_overlap_check))
1105     {
1106         error_setg(errp, "Conflicting values for qcow2 options '"
1107                    QCOW2_OPT_OVERLAP "' ('%s') and '" QCOW2_OPT_OVERLAP_TEMPLATE
1108                    "' ('%s')", opt_overlap_check, opt_overlap_check_template);
1109         ret = -EINVAL;
1110         goto fail;
1111     }
1112     if (!opt_overlap_check) {
1113         opt_overlap_check = opt_overlap_check_template ?: "cached";
1114     }
1115 
1116     if (!strcmp(opt_overlap_check, "none")) {
1117         overlap_check_template = 0;
1118     } else if (!strcmp(opt_overlap_check, "constant")) {
1119         overlap_check_template = QCOW2_OL_CONSTANT;
1120     } else if (!strcmp(opt_overlap_check, "cached")) {
1121         overlap_check_template = QCOW2_OL_CACHED;
1122     } else if (!strcmp(opt_overlap_check, "all")) {
1123         overlap_check_template = QCOW2_OL_ALL;
1124     } else {
1125         error_setg(errp, "Unsupported value '%s' for qcow2 option "
1126                    "'overlap-check'. Allowed are any of the following: "
1127                    "none, constant, cached, all", opt_overlap_check);
1128         ret = -EINVAL;
1129         goto fail;
1130     }
1131 
1132     r->overlap_check = 0;
1133     for (i = 0; i < QCOW2_OL_MAX_BITNR; i++) {
1134         /* overlap-check defines a template bitmask, but every flag may be
1135          * overwritten through the associated boolean option */
1136         r->overlap_check |=
1137             qemu_opt_get_bool(opts, overlap_bool_option_names[i],
1138                               overlap_check_template & (1 << i)) << i;
1139     }
1140 
1141     r->discard_passthrough[QCOW2_DISCARD_NEVER] = false;
1142     r->discard_passthrough[QCOW2_DISCARD_ALWAYS] = true;
1143     r->discard_passthrough[QCOW2_DISCARD_REQUEST] =
1144         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_REQUEST,
1145                           flags & BDRV_O_UNMAP);
1146     r->discard_passthrough[QCOW2_DISCARD_SNAPSHOT] =
1147         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_SNAPSHOT, true);
1148     r->discard_passthrough[QCOW2_DISCARD_OTHER] =
1149         qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_OTHER, false);
1150 
1151     r->discard_no_unref = qemu_opt_get_bool(opts, QCOW2_OPT_DISCARD_NO_UNREF,
1152                                             false);
1153     if (r->discard_no_unref && s->qcow_version < 3) {
1154         error_setg(errp,
1155                    "discard-no-unref is only supported since qcow2 version 3");
1156         ret = -EINVAL;
1157         goto fail;
1158     }
1159 
1160     switch (s->crypt_method_header) {
1161     case QCOW_CRYPT_NONE:
1162         if (encryptfmt) {
1163             error_setg(errp, "No encryption in image header, but options "
1164                        "specified format '%s'", encryptfmt);
1165             ret = -EINVAL;
1166             goto fail;
1167         }
1168         break;
1169 
1170     case QCOW_CRYPT_AES:
1171         if (encryptfmt && !g_str_equal(encryptfmt, "aes")) {
1172             error_setg(errp,
1173                        "Header reported 'aes' encryption format but "
1174                        "options specify '%s'", encryptfmt);
1175             ret = -EINVAL;
1176             goto fail;
1177         }
1178         qdict_put_str(encryptopts, "format", "qcow");
1179         r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp);
1180         if (!r->crypto_opts) {
1181             ret = -EINVAL;
1182             goto fail;
1183         }
1184         break;
1185 
1186     case QCOW_CRYPT_LUKS:
1187         if (encryptfmt && !g_str_equal(encryptfmt, "luks")) {
1188             error_setg(errp,
1189                        "Header reported 'luks' encryption format but "
1190                        "options specify '%s'", encryptfmt);
1191             ret = -EINVAL;
1192             goto fail;
1193         }
1194         qdict_put_str(encryptopts, "format", "luks");
1195         r->crypto_opts = block_crypto_open_opts_init(encryptopts, errp);
1196         if (!r->crypto_opts) {
1197             ret = -EINVAL;
1198             goto fail;
1199         }
1200         break;
1201 
1202     default:
1203         error_setg(errp, "Unsupported encryption method %d",
1204                    s->crypt_method_header);
1205         ret = -EINVAL;
1206         goto fail;
1207     }
1208 
1209     ret = 0;
1210 fail:
1211     qobject_unref(encryptopts);
1212     qemu_opts_del(opts);
1213     opts = NULL;
1214     return ret;
1215 }
1216 
qcow2_update_options_commit(BlockDriverState * bs,Qcow2ReopenState * r)1217 static void qcow2_update_options_commit(BlockDriverState *bs,
1218                                         Qcow2ReopenState *r)
1219 {
1220     BDRVQcow2State *s = bs->opaque;
1221     int i;
1222 
1223     if (s->l2_table_cache) {
1224         qcow2_cache_destroy(s->l2_table_cache);
1225     }
1226     if (s->refcount_block_cache) {
1227         qcow2_cache_destroy(s->refcount_block_cache);
1228     }
1229     s->l2_table_cache = r->l2_table_cache;
1230     s->refcount_block_cache = r->refcount_block_cache;
1231     s->l2_slice_size = r->l2_slice_size;
1232 
1233     s->overlap_check = r->overlap_check;
1234     s->use_lazy_refcounts = r->use_lazy_refcounts;
1235 
1236     for (i = 0; i < QCOW2_DISCARD_MAX; i++) {
1237         s->discard_passthrough[i] = r->discard_passthrough[i];
1238     }
1239 
1240     s->discard_no_unref = r->discard_no_unref;
1241 
1242     if (s->cache_clean_interval != r->cache_clean_interval) {
1243         cache_clean_timer_del(bs);
1244         s->cache_clean_interval = r->cache_clean_interval;
1245         cache_clean_timer_init(bs, bdrv_get_aio_context(bs));
1246     }
1247 
1248     qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
1249     s->crypto_opts = r->crypto_opts;
1250 }
1251 
qcow2_update_options_abort(BlockDriverState * bs,Qcow2ReopenState * r)1252 static void qcow2_update_options_abort(BlockDriverState *bs,
1253                                        Qcow2ReopenState *r)
1254 {
1255     if (r->l2_table_cache) {
1256         qcow2_cache_destroy(r->l2_table_cache);
1257     }
1258     if (r->refcount_block_cache) {
1259         qcow2_cache_destroy(r->refcount_block_cache);
1260     }
1261     qapi_free_QCryptoBlockOpenOptions(r->crypto_opts);
1262 }
1263 
1264 static int coroutine_fn GRAPH_RDLOCK
qcow2_update_options(BlockDriverState * bs,QDict * options,int flags,Error ** errp)1265 qcow2_update_options(BlockDriverState *bs, QDict *options, int flags,
1266                      Error **errp)
1267 {
1268     Qcow2ReopenState r = {};
1269     int ret;
1270 
1271     ret = qcow2_update_options_prepare(bs, &r, options, flags, errp);
1272     if (ret >= 0) {
1273         qcow2_update_options_commit(bs, &r);
1274     } else {
1275         qcow2_update_options_abort(bs, &r);
1276     }
1277 
1278     return ret;
1279 }
1280 
validate_compression_type(BDRVQcow2State * s,Error ** errp)1281 static int validate_compression_type(BDRVQcow2State *s, Error **errp)
1282 {
1283     switch (s->compression_type) {
1284     case QCOW2_COMPRESSION_TYPE_ZLIB:
1285 #ifdef CONFIG_ZSTD
1286     case QCOW2_COMPRESSION_TYPE_ZSTD:
1287 #endif
1288         break;
1289 
1290     default:
1291         error_setg(errp, "qcow2: unknown compression type: %u",
1292                    s->compression_type);
1293         return -ENOTSUP;
1294     }
1295 
1296     /*
1297      * if the compression type differs from QCOW2_COMPRESSION_TYPE_ZLIB
1298      * the incompatible feature flag must be set
1299      */
1300     if (s->compression_type == QCOW2_COMPRESSION_TYPE_ZLIB) {
1301         if (s->incompatible_features & QCOW2_INCOMPAT_COMPRESSION) {
1302             error_setg(errp, "qcow2: Compression type incompatible feature "
1303                              "bit must not be set");
1304             return -EINVAL;
1305         }
1306     } else {
1307         if (!(s->incompatible_features & QCOW2_INCOMPAT_COMPRESSION)) {
1308             error_setg(errp, "qcow2: Compression type incompatible feature "
1309                              "bit must be set");
1310             return -EINVAL;
1311         }
1312     }
1313 
1314     return 0;
1315 }
1316 
1317 /* Called with s->lock held.  */
1318 static int coroutine_fn GRAPH_RDLOCK
qcow2_do_open(BlockDriverState * bs,QDict * options,int flags,bool open_data_file,Error ** errp)1319 qcow2_do_open(BlockDriverState *bs, QDict *options, int flags,
1320               bool open_data_file, Error **errp)
1321 {
1322     ERRP_GUARD();
1323     BDRVQcow2State *s = bs->opaque;
1324     unsigned int len, i;
1325     int ret = 0;
1326     QCowHeader header;
1327     uint64_t ext_end;
1328     uint64_t l1_vm_state_index;
1329     bool update_header = false;
1330 
1331     ret = bdrv_co_pread(bs->file, 0, sizeof(header), &header, 0);
1332     if (ret < 0) {
1333         error_setg_errno(errp, -ret, "Could not read qcow2 header");
1334         goto fail;
1335     }
1336     header.magic = be32_to_cpu(header.magic);
1337     header.version = be32_to_cpu(header.version);
1338     header.backing_file_offset = be64_to_cpu(header.backing_file_offset);
1339     header.backing_file_size = be32_to_cpu(header.backing_file_size);
1340     header.size = be64_to_cpu(header.size);
1341     header.cluster_bits = be32_to_cpu(header.cluster_bits);
1342     header.crypt_method = be32_to_cpu(header.crypt_method);
1343     header.l1_table_offset = be64_to_cpu(header.l1_table_offset);
1344     header.l1_size = be32_to_cpu(header.l1_size);
1345     header.refcount_table_offset = be64_to_cpu(header.refcount_table_offset);
1346     header.refcount_table_clusters =
1347         be32_to_cpu(header.refcount_table_clusters);
1348     header.snapshots_offset = be64_to_cpu(header.snapshots_offset);
1349     header.nb_snapshots = be32_to_cpu(header.nb_snapshots);
1350 
1351     if (header.magic != QCOW_MAGIC) {
1352         error_setg(errp, "Image is not in qcow2 format");
1353         ret = -EINVAL;
1354         goto fail;
1355     }
1356     if (header.version < 2 || header.version > 3) {
1357         error_setg(errp, "Unsupported qcow2 version %" PRIu32, header.version);
1358         ret = -ENOTSUP;
1359         goto fail;
1360     }
1361 
1362     s->qcow_version = header.version;
1363 
1364     /* Initialise cluster size */
1365     if (header.cluster_bits < MIN_CLUSTER_BITS ||
1366         header.cluster_bits > MAX_CLUSTER_BITS) {
1367         error_setg(errp, "Unsupported cluster size: 2^%" PRIu32,
1368                    header.cluster_bits);
1369         ret = -EINVAL;
1370         goto fail;
1371     }
1372 
1373     s->cluster_bits = header.cluster_bits;
1374     s->cluster_size = 1 << s->cluster_bits;
1375 
1376     /* Initialise version 3 header fields */
1377     if (header.version == 2) {
1378         header.incompatible_features    = 0;
1379         header.compatible_features      = 0;
1380         header.autoclear_features       = 0;
1381         header.refcount_order           = 4;
1382         header.header_length            = 72;
1383     } else {
1384         header.incompatible_features =
1385             be64_to_cpu(header.incompatible_features);
1386         header.compatible_features = be64_to_cpu(header.compatible_features);
1387         header.autoclear_features = be64_to_cpu(header.autoclear_features);
1388         header.refcount_order = be32_to_cpu(header.refcount_order);
1389         header.header_length = be32_to_cpu(header.header_length);
1390 
1391         if (header.header_length < 104) {
1392             error_setg(errp, "qcow2 header too short");
1393             ret = -EINVAL;
1394             goto fail;
1395         }
1396     }
1397 
1398     if (header.header_length > s->cluster_size) {
1399         error_setg(errp, "qcow2 header exceeds cluster size");
1400         ret = -EINVAL;
1401         goto fail;
1402     }
1403 
1404     if (header.header_length > sizeof(header)) {
1405         s->unknown_header_fields_size = header.header_length - sizeof(header);
1406         s->unknown_header_fields = g_malloc(s->unknown_header_fields_size);
1407         ret = bdrv_co_pread(bs->file, sizeof(header),
1408                             s->unknown_header_fields_size,
1409                             s->unknown_header_fields, 0);
1410         if (ret < 0) {
1411             error_setg_errno(errp, -ret, "Could not read unknown qcow2 header "
1412                              "fields");
1413             goto fail;
1414         }
1415     }
1416 
1417     if (header.backing_file_offset > s->cluster_size) {
1418         error_setg(errp, "Invalid backing file offset");
1419         ret = -EINVAL;
1420         goto fail;
1421     }
1422 
1423     if (header.backing_file_offset) {
1424         ext_end = header.backing_file_offset;
1425     } else {
1426         ext_end = 1 << header.cluster_bits;
1427     }
1428 
1429     /* Handle feature bits */
1430     s->incompatible_features    = header.incompatible_features;
1431     s->compatible_features      = header.compatible_features;
1432     s->autoclear_features       = header.autoclear_features;
1433 
1434     /*
1435      * Handle compression type
1436      * Older qcow2 images don't contain the compression type header.
1437      * Distinguish them by the header length and use
1438      * the only valid (default) compression type in that case
1439      */
1440     if (header.header_length > offsetof(QCowHeader, compression_type)) {
1441         s->compression_type = header.compression_type;
1442     } else {
1443         s->compression_type = QCOW2_COMPRESSION_TYPE_ZLIB;
1444     }
1445 
1446     ret = validate_compression_type(s, errp);
1447     if (ret) {
1448         goto fail;
1449     }
1450 
1451     if (s->incompatible_features & ~QCOW2_INCOMPAT_MASK) {
1452         void *feature_table = NULL;
1453         qcow2_read_extensions(bs, header.header_length, ext_end,
1454                               &feature_table, flags, NULL, NULL);
1455         report_unsupported_feature(errp, feature_table,
1456                                    s->incompatible_features &
1457                                    ~QCOW2_INCOMPAT_MASK);
1458         ret = -ENOTSUP;
1459         g_free(feature_table);
1460         goto fail;
1461     }
1462 
1463     if (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT) {
1464         /* Corrupt images may not be written to unless they are being repaired
1465          */
1466         if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
1467             error_setg(errp, "qcow2: Image is corrupt; cannot be opened "
1468                        "read/write");
1469             ret = -EACCES;
1470             goto fail;
1471         }
1472     }
1473 
1474     s->subclusters_per_cluster =
1475         has_subclusters(s) ? QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER : 1;
1476     s->subcluster_size = s->cluster_size / s->subclusters_per_cluster;
1477     s->subcluster_bits = ctz32(s->subcluster_size);
1478 
1479     if (s->subcluster_size < (1 << MIN_CLUSTER_BITS)) {
1480         error_setg(errp, "Unsupported subcluster size: %d", s->subcluster_size);
1481         ret = -EINVAL;
1482         goto fail;
1483     }
1484 
1485     /* Check support for various header values */
1486     if (header.refcount_order > 6) {
1487         error_setg(errp, "Reference count entry width too large; may not "
1488                    "exceed 64 bits");
1489         ret = -EINVAL;
1490         goto fail;
1491     }
1492     s->refcount_order = header.refcount_order;
1493     s->refcount_bits = 1 << s->refcount_order;
1494     s->refcount_max = UINT64_C(1) << (s->refcount_bits - 1);
1495     s->refcount_max += s->refcount_max - 1;
1496 
1497     s->crypt_method_header = header.crypt_method;
1498     if (s->crypt_method_header) {
1499         if (bdrv_uses_whitelist() &&
1500             s->crypt_method_header == QCOW_CRYPT_AES) {
1501             error_setg(errp,
1502                        "Use of AES-CBC encrypted qcow2 images is no longer "
1503                        "supported in system emulators");
1504             error_append_hint(errp,
1505                               "You can use 'qemu-img convert' to convert your "
1506                               "image to an alternative supported format, such "
1507                               "as unencrypted qcow2, or raw with the LUKS "
1508                               "format instead.\n");
1509             ret = -ENOSYS;
1510             goto fail;
1511         }
1512 
1513         if (s->crypt_method_header == QCOW_CRYPT_AES) {
1514             s->crypt_physical_offset = false;
1515         } else {
1516             /* Assuming LUKS and any future crypt methods we
1517              * add will all use physical offsets, due to the
1518              * fact that the alternative is insecure...  */
1519             s->crypt_physical_offset = true;
1520         }
1521 
1522         bs->encrypted = true;
1523     }
1524 
1525     s->l2_bits = s->cluster_bits - ctz32(l2_entry_size(s));
1526     s->l2_size = 1 << s->l2_bits;
1527     /* 2^(s->refcount_order - 3) is the refcount width in bytes */
1528     s->refcount_block_bits = s->cluster_bits - (s->refcount_order - 3);
1529     s->refcount_block_size = 1 << s->refcount_block_bits;
1530     bs->total_sectors = header.size / BDRV_SECTOR_SIZE;
1531     s->csize_shift = (62 - (s->cluster_bits - 8));
1532     s->csize_mask = (1 << (s->cluster_bits - 8)) - 1;
1533     s->cluster_offset_mask = (1LL << s->csize_shift) - 1;
1534 
1535     s->refcount_table_offset = header.refcount_table_offset;
1536     s->refcount_table_size =
1537         header.refcount_table_clusters << (s->cluster_bits - 3);
1538 
1539     if (header.refcount_table_clusters == 0 && !(flags & BDRV_O_CHECK)) {
1540         error_setg(errp, "Image does not contain a reference count table");
1541         ret = -EINVAL;
1542         goto fail;
1543     }
1544 
1545     ret = qcow2_validate_table(bs, s->refcount_table_offset,
1546                                header.refcount_table_clusters,
1547                                s->cluster_size, QCOW_MAX_REFTABLE_SIZE,
1548                                "Reference count table", errp);
1549     if (ret < 0) {
1550         goto fail;
1551     }
1552 
1553     if (!(flags & BDRV_O_CHECK)) {
1554         /*
1555          * The total size in bytes of the snapshot table is checked in
1556          * qcow2_read_snapshots() because the size of each snapshot is
1557          * variable and we don't know it yet.
1558          * Here we only check the offset and number of snapshots.
1559          */
1560         ret = qcow2_validate_table(bs, header.snapshots_offset,
1561                                    header.nb_snapshots,
1562                                    sizeof(QCowSnapshotHeader),
1563                                    sizeof(QCowSnapshotHeader) *
1564                                        QCOW_MAX_SNAPSHOTS,
1565                                    "Snapshot table", errp);
1566         if (ret < 0) {
1567             goto fail;
1568         }
1569     }
1570 
1571     /* read the level 1 table */
1572     ret = qcow2_validate_table(bs, header.l1_table_offset,
1573                                header.l1_size, L1E_SIZE,
1574                                QCOW_MAX_L1_SIZE, "Active L1 table", errp);
1575     if (ret < 0) {
1576         goto fail;
1577     }
1578     s->l1_size = header.l1_size;
1579     s->l1_table_offset = header.l1_table_offset;
1580 
1581     l1_vm_state_index = size_to_l1(s, header.size);
1582     if (l1_vm_state_index > INT_MAX) {
1583         error_setg(errp, "Image is too big");
1584         ret = -EFBIG;
1585         goto fail;
1586     }
1587     s->l1_vm_state_index = l1_vm_state_index;
1588 
1589     /* the L1 table must contain at least enough entries to put
1590        header.size bytes */
1591     if (s->l1_size < s->l1_vm_state_index) {
1592         error_setg(errp, "L1 table is too small");
1593         ret = -EINVAL;
1594         goto fail;
1595     }
1596 
1597     if (s->l1_size > 0) {
1598         s->l1_table = qemu_try_blockalign(bs->file->bs, s->l1_size * L1E_SIZE);
1599         if (s->l1_table == NULL) {
1600             error_setg(errp, "Could not allocate L1 table");
1601             ret = -ENOMEM;
1602             goto fail;
1603         }
1604         ret = bdrv_co_pread(bs->file, s->l1_table_offset, s->l1_size * L1E_SIZE,
1605                             s->l1_table, 0);
1606         if (ret < 0) {
1607             error_setg_errno(errp, -ret, "Could not read L1 table");
1608             goto fail;
1609         }
1610         for(i = 0;i < s->l1_size; i++) {
1611             s->l1_table[i] = be64_to_cpu(s->l1_table[i]);
1612         }
1613     }
1614 
1615     /* Parse driver-specific options */
1616     ret = qcow2_update_options(bs, options, flags, errp);
1617     if (ret < 0) {
1618         goto fail;
1619     }
1620 
1621     s->flags = flags;
1622 
1623     ret = qcow2_refcount_init(bs);
1624     if (ret != 0) {
1625         error_setg_errno(errp, -ret, "Could not initialize refcount handling");
1626         goto fail;
1627     }
1628 
1629     QLIST_INIT(&s->cluster_allocs);
1630     QTAILQ_INIT(&s->discards);
1631 
1632     /* read qcow2 extensions */
1633     if (qcow2_read_extensions(bs, header.header_length, ext_end, NULL,
1634                               flags, &update_header, errp)) {
1635         ret = -EINVAL;
1636         goto fail;
1637     }
1638 
1639     if (open_data_file && (flags & BDRV_O_NO_IO)) {
1640         /*
1641          * Don't open the data file for 'qemu-img info' so that it can be used
1642          * to verify that an untrusted qcow2 image doesn't refer to external
1643          * files.
1644          *
1645          * Note: This still makes has_data_file() return true.
1646          */
1647         if (s->incompatible_features & QCOW2_INCOMPAT_DATA_FILE) {
1648             s->data_file = NULL;
1649         } else {
1650             s->data_file = bs->file;
1651         }
1652         qdict_extract_subqdict(options, NULL, "data-file.");
1653         qdict_del(options, "data-file");
1654     } else if (open_data_file) {
1655         /* Open external data file */
1656         bdrv_graph_co_rdunlock();
1657         s->data_file = bdrv_co_open_child(NULL, options, "data-file", bs,
1658                                           &child_of_bds, BDRV_CHILD_DATA,
1659                                           true, errp);
1660         bdrv_graph_co_rdlock();
1661         if (*errp) {
1662             ret = -EINVAL;
1663             goto fail;
1664         }
1665 
1666         if (s->incompatible_features & QCOW2_INCOMPAT_DATA_FILE) {
1667             if (!s->data_file && s->image_data_file) {
1668                 bdrv_graph_co_rdunlock();
1669                 s->data_file = bdrv_co_open_child(s->image_data_file, options,
1670                                                   "data-file", bs,
1671                                                   &child_of_bds,
1672                                                   BDRV_CHILD_DATA, false, errp);
1673                 bdrv_graph_co_rdlock();
1674                 if (!s->data_file) {
1675                     ret = -EINVAL;
1676                     goto fail;
1677                 }
1678             }
1679             if (!s->data_file) {
1680                 error_setg(errp, "'data-file' is required for this image");
1681                 ret = -EINVAL;
1682                 goto fail;
1683             }
1684 
1685             /* No data here */
1686             bs->file->role &= ~BDRV_CHILD_DATA;
1687 
1688             /* Must succeed because we have given up permissions if anything */
1689             bdrv_child_refresh_perms(bs, bs->file, &error_abort);
1690         } else {
1691             if (s->data_file) {
1692                 error_setg(errp, "'data-file' can only be set for images with "
1693                                  "an external data file");
1694                 ret = -EINVAL;
1695                 goto fail;
1696             }
1697 
1698             s->data_file = bs->file;
1699 
1700             if (data_file_is_raw(bs)) {
1701                 error_setg(errp, "data-file-raw requires a data file");
1702                 ret = -EINVAL;
1703                 goto fail;
1704             }
1705         }
1706     }
1707 
1708     /* qcow2_read_extension may have set up the crypto context
1709      * if the crypt method needs a header region, some methods
1710      * don't need header extensions, so must check here
1711      */
1712     if (s->crypt_method_header && !s->crypto) {
1713         if (s->crypt_method_header == QCOW_CRYPT_AES) {
1714             unsigned int cflags = 0;
1715             if (flags & BDRV_O_NO_IO) {
1716                 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
1717             }
1718             s->crypto = qcrypto_block_open(s->crypto_opts, "encrypt.",
1719                                            NULL, NULL, cflags, errp);
1720             if (!s->crypto) {
1721                 ret = -EINVAL;
1722                 goto fail;
1723             }
1724         } else {
1725             error_setg(errp, "Missing CRYPTO header for crypt method %d",
1726                        s->crypt_method_header);
1727             ret = -EINVAL;
1728             goto fail;
1729         }
1730     }
1731 
1732     /* read the backing file name */
1733     if (header.backing_file_offset != 0) {
1734         len = header.backing_file_size;
1735         if (len > MIN(1023, s->cluster_size - header.backing_file_offset) ||
1736             len >= sizeof(bs->backing_file)) {
1737             error_setg(errp, "Backing file name too long");
1738             ret = -EINVAL;
1739             goto fail;
1740         }
1741 
1742         s->image_backing_file = g_malloc(len + 1);
1743         ret = bdrv_co_pread(bs->file, header.backing_file_offset, len,
1744                             s->image_backing_file, 0);
1745         if (ret < 0) {
1746             error_setg_errno(errp, -ret, "Could not read backing file name");
1747             goto fail;
1748         }
1749         s->image_backing_file[len] = '\0';
1750 
1751         /*
1752          * Update only when something has changed.  This function is called by
1753          * qcow2_co_invalidate_cache(), and we do not want to reset
1754          * auto_backing_file unless necessary.
1755          */
1756         if (!g_str_equal(s->image_backing_file, bs->backing_file)) {
1757             pstrcpy(bs->backing_file, sizeof(bs->backing_file),
1758                     s->image_backing_file);
1759             pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
1760                     s->image_backing_file);
1761         }
1762     }
1763 
1764     /*
1765      * Internal snapshots; skip reading them in check mode, because
1766      * we do not need them then, and we do not want to abort because
1767      * of a broken table.
1768      */
1769     if (!(flags & BDRV_O_CHECK)) {
1770         s->snapshots_offset = header.snapshots_offset;
1771         s->nb_snapshots = header.nb_snapshots;
1772 
1773         ret = qcow2_read_snapshots(bs, errp);
1774         if (ret < 0) {
1775             goto fail;
1776         }
1777     }
1778 
1779     /* Clear unknown autoclear feature bits */
1780     update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK;
1781     update_header = update_header && bdrv_is_writable(bs);
1782     if (update_header) {
1783         s->autoclear_features &= QCOW2_AUTOCLEAR_MASK;
1784     }
1785 
1786     /* == Handle persistent dirty bitmaps ==
1787      *
1788      * We want load dirty bitmaps in three cases:
1789      *
1790      * 1. Normal open of the disk in active mode, not related to invalidation
1791      *    after migration.
1792      *
1793      * 2. Invalidation of the target vm after pre-copy phase of migration, if
1794      *    bitmaps are _not_ migrating through migration channel, i.e.
1795      *    'dirty-bitmaps' capability is disabled.
1796      *
1797      * 3. Invalidation of source vm after failed or canceled migration.
1798      *    This is a very interesting case. There are two possible types of
1799      *    bitmaps:
1800      *
1801      *    A. Stored on inactivation and removed. They should be loaded from the
1802      *       image.
1803      *
1804      *    B. Not stored: not-persistent bitmaps and bitmaps, migrated through
1805      *       the migration channel (with dirty-bitmaps capability).
1806      *
1807      *    On the other hand, there are two possible sub-cases:
1808      *
1809      *    3.1 disk was changed by somebody else while were inactive. In this
1810      *        case all in-RAM dirty bitmaps (both persistent and not) are
1811      *        definitely invalid. And we don't have any method to determine
1812      *        this.
1813      *
1814      *        Simple and safe thing is to just drop all the bitmaps of type B on
1815      *        inactivation. But in this case we lose bitmaps in valid 4.2 case.
1816      *
1817      *        On the other hand, resuming source vm, if disk was already changed
1818      *        is a bad thing anyway: not only bitmaps, the whole vm state is
1819      *        out of sync with disk.
1820      *
1821      *        This means, that user or management tool, who for some reason
1822      *        decided to resume source vm, after disk was already changed by
1823      *        target vm, should at least drop all dirty bitmaps by hand.
1824      *
1825      *        So, we can ignore this case for now, but TODO: "generation"
1826      *        extension for qcow2, to determine, that image was changed after
1827      *        last inactivation. And if it is changed, we will drop (or at least
1828      *        mark as 'invalid' all the bitmaps of type B, both persistent
1829      *        and not).
1830      *
1831      *    3.2 disk was _not_ changed while were inactive. Bitmaps may be saved
1832      *        to disk ('dirty-bitmaps' capability disabled), or not saved
1833      *        ('dirty-bitmaps' capability enabled), but we don't need to care
1834      *        of: let's load bitmaps as always: stored bitmaps will be loaded,
1835      *        and not stored has flag IN_USE=1 in the image and will be skipped
1836      *        on loading.
1837      *
1838      * One remaining possible case when we don't want load bitmaps:
1839      *
1840      * 4. Open disk in inactive mode in target vm (bitmaps are migrating or
1841      *    will be loaded on invalidation, no needs try loading them before)
1842      */
1843 
1844     if (!(bdrv_get_flags(bs) & BDRV_O_INACTIVE)) {
1845         /* It's case 1, 2 or 3.2. Or 3.1 which is BUG in management layer. */
1846         bool header_updated;
1847         if (!qcow2_load_dirty_bitmaps(bs, &header_updated, errp)) {
1848             ret = -EINVAL;
1849             goto fail;
1850         }
1851 
1852         update_header = update_header && !header_updated;
1853     }
1854 
1855     if (update_header) {
1856         ret = qcow2_update_header(bs);
1857         if (ret < 0) {
1858             error_setg_errno(errp, -ret, "Could not update qcow2 header");
1859             goto fail;
1860         }
1861     }
1862 
1863     bs->supported_zero_flags = header.version >= 3 ?
1864                                BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK : 0;
1865     bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
1866 
1867     /* Repair image if dirty */
1868     if (!(flags & BDRV_O_CHECK) && bdrv_is_writable(bs) &&
1869         (s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
1870         BdrvCheckResult result = {0};
1871 
1872         ret = qcow2_co_check_locked(bs, &result,
1873                                     BDRV_FIX_ERRORS | BDRV_FIX_LEAKS);
1874         if (ret < 0 || result.check_errors) {
1875             if (ret >= 0) {
1876                 ret = -EIO;
1877             }
1878             error_setg_errno(errp, -ret, "Could not repair dirty image");
1879             goto fail;
1880         }
1881     }
1882 
1883 #ifdef DEBUG_ALLOC
1884     {
1885         BdrvCheckResult result = {0};
1886         qcow2_check_refcounts(bs, &result, 0);
1887     }
1888 #endif
1889 
1890     qemu_co_queue_init(&s->thread_task_queue);
1891 
1892     return ret;
1893 
1894  fail:
1895     g_free(s->image_data_file);
1896     if (open_data_file && has_data_file(bs)) {
1897         bdrv_graph_co_rdunlock();
1898         bdrv_drain_all_begin();
1899         bdrv_co_unref_child(bs, s->data_file);
1900         bdrv_drain_all_end();
1901         bdrv_graph_co_rdlock();
1902         s->data_file = NULL;
1903     }
1904     g_free(s->unknown_header_fields);
1905     cleanup_unknown_header_ext(bs);
1906     qcow2_free_snapshots(bs);
1907     qcow2_refcount_close(bs);
1908     qemu_vfree(s->l1_table);
1909     /* else pre-write overlap checks in cache_destroy may crash */
1910     s->l1_table = NULL;
1911     cache_clean_timer_del(bs);
1912     if (s->l2_table_cache) {
1913         qcow2_cache_destroy(s->l2_table_cache);
1914     }
1915     if (s->refcount_block_cache) {
1916         qcow2_cache_destroy(s->refcount_block_cache);
1917     }
1918     qcrypto_block_free(s->crypto);
1919     qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
1920     return ret;
1921 }
1922 
1923 typedef struct QCow2OpenCo {
1924     BlockDriverState *bs;
1925     QDict *options;
1926     int flags;
1927     Error **errp;
1928     int ret;
1929 } QCow2OpenCo;
1930 
qcow2_open_entry(void * opaque)1931 static void coroutine_fn qcow2_open_entry(void *opaque)
1932 {
1933     QCow2OpenCo *qoc = opaque;
1934     BDRVQcow2State *s = qoc->bs->opaque;
1935 
1936     GRAPH_RDLOCK_GUARD();
1937 
1938     qemu_co_mutex_lock(&s->lock);
1939     qoc->ret = qcow2_do_open(qoc->bs, qoc->options, qoc->flags, true,
1940                              qoc->errp);
1941     qemu_co_mutex_unlock(&s->lock);
1942 
1943     aio_wait_kick();
1944 }
1945 
qcow2_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)1946 static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
1947                       Error **errp)
1948 {
1949     BDRVQcow2State *s = bs->opaque;
1950     QCow2OpenCo qoc = {
1951         .bs = bs,
1952         .options = options,
1953         .flags = flags,
1954         .errp = errp,
1955         .ret = -EINPROGRESS
1956     };
1957     int ret;
1958 
1959     ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
1960     if (ret < 0) {
1961         return ret;
1962     }
1963 
1964     /* Initialise locks */
1965     qemu_co_mutex_init(&s->lock);
1966 
1967     assert(!qemu_in_coroutine());
1968     assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1969 
1970     aio_co_enter(bdrv_get_aio_context(bs),
1971                  qemu_coroutine_create(qcow2_open_entry, &qoc));
1972     AIO_WAIT_WHILE_UNLOCKED(NULL, qoc.ret == -EINPROGRESS);
1973 
1974     return qoc.ret;
1975 }
1976 
qcow2_refresh_limits(BlockDriverState * bs,Error ** errp)1977 static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp)
1978 {
1979     BDRVQcow2State *s = bs->opaque;
1980 
1981     if (s->crypto) {
1982         /* Encryption works on a sector granularity */
1983         bs->bl.request_alignment = qcrypto_block_get_sector_size(s->crypto);
1984     }
1985     bs->bl.pwrite_zeroes_alignment = s->subcluster_size;
1986     bs->bl.pdiscard_alignment = s->cluster_size;
1987 }
1988 
1989 static int GRAPH_UNLOCKED
qcow2_reopen_prepare(BDRVReopenState * state,BlockReopenQueue * queue,Error ** errp)1990 qcow2_reopen_prepare(BDRVReopenState *state,BlockReopenQueue *queue,
1991                      Error **errp)
1992 {
1993     BDRVQcow2State *s = state->bs->opaque;
1994     Qcow2ReopenState *r;
1995     int ret;
1996 
1997     GLOBAL_STATE_CODE();
1998     GRAPH_RDLOCK_GUARD_MAINLOOP();
1999 
2000     r = g_new0(Qcow2ReopenState, 1);
2001     state->opaque = r;
2002 
2003     ret = qcow2_update_options_prepare(state->bs, r, state->options,
2004                                        state->flags, errp);
2005     if (ret < 0) {
2006         goto fail;
2007     }
2008 
2009     /* We need to write out any unwritten data if we reopen read-only. */
2010     if ((state->flags & BDRV_O_RDWR) == 0) {
2011         ret = qcow2_reopen_bitmaps_ro(state->bs, errp);
2012         if (ret < 0) {
2013             goto fail;
2014         }
2015 
2016         ret = bdrv_flush(state->bs);
2017         if (ret < 0) {
2018             goto fail;
2019         }
2020 
2021         ret = qcow2_mark_clean(state->bs);
2022         if (ret < 0) {
2023             goto fail;
2024         }
2025     }
2026 
2027     /*
2028      * Without an external data file, s->data_file points to the same BdrvChild
2029      * as bs->file. It needs to be resynced after reopen because bs->file may
2030      * be changed. We can't use it in the meantime.
2031      */
2032     if (!has_data_file(state->bs)) {
2033         assert(s->data_file == state->bs->file);
2034         s->data_file = NULL;
2035     }
2036 
2037     return 0;
2038 
2039 fail:
2040     qcow2_update_options_abort(state->bs, r);
2041     g_free(r);
2042     return ret;
2043 }
2044 
qcow2_reopen_commit(BDRVReopenState * state)2045 static void qcow2_reopen_commit(BDRVReopenState *state)
2046 {
2047     BDRVQcow2State *s = state->bs->opaque;
2048 
2049     GRAPH_RDLOCK_GUARD_MAINLOOP();
2050 
2051     qcow2_update_options_commit(state->bs, state->opaque);
2052     if (!s->data_file) {
2053         /*
2054          * If we don't have an external data file, s->data_file was cleared by
2055          * qcow2_reopen_prepare() and needs to be updated.
2056          */
2057         s->data_file = state->bs->file;
2058     }
2059     g_free(state->opaque);
2060 }
2061 
qcow2_reopen_commit_post(BDRVReopenState * state)2062 static void qcow2_reopen_commit_post(BDRVReopenState *state)
2063 {
2064     GRAPH_RDLOCK_GUARD_MAINLOOP();
2065 
2066     if (state->flags & BDRV_O_RDWR) {
2067         Error *local_err = NULL;
2068 
2069         if (qcow2_reopen_bitmaps_rw(state->bs, &local_err) < 0) {
2070             /*
2071              * This is not fatal, bitmaps just left read-only, so all following
2072              * writes will fail. User can remove read-only bitmaps to unblock
2073              * writes or retry reopen.
2074              */
2075             error_reportf_err(local_err,
2076                               "%s: Failed to make dirty bitmaps writable: ",
2077                               bdrv_get_node_name(state->bs));
2078         }
2079     }
2080 }
2081 
qcow2_reopen_abort(BDRVReopenState * state)2082 static void qcow2_reopen_abort(BDRVReopenState *state)
2083 {
2084     BDRVQcow2State *s = state->bs->opaque;
2085 
2086     GRAPH_RDLOCK_GUARD_MAINLOOP();
2087 
2088     if (!s->data_file) {
2089         /*
2090          * If we don't have an external data file, s->data_file was cleared by
2091          * qcow2_reopen_prepare() and needs to be restored.
2092          */
2093         s->data_file = state->bs->file;
2094     }
2095     qcow2_update_options_abort(state->bs, state->opaque);
2096     g_free(state->opaque);
2097 }
2098 
qcow2_join_options(QDict * options,QDict * old_options)2099 static void qcow2_join_options(QDict *options, QDict *old_options)
2100 {
2101     bool has_new_overlap_template =
2102         qdict_haskey(options, QCOW2_OPT_OVERLAP) ||
2103         qdict_haskey(options, QCOW2_OPT_OVERLAP_TEMPLATE);
2104     bool has_new_total_cache_size =
2105         qdict_haskey(options, QCOW2_OPT_CACHE_SIZE);
2106     bool has_all_cache_options;
2107 
2108     /* New overlap template overrides all old overlap options */
2109     if (has_new_overlap_template) {
2110         qdict_del(old_options, QCOW2_OPT_OVERLAP);
2111         qdict_del(old_options, QCOW2_OPT_OVERLAP_TEMPLATE);
2112         qdict_del(old_options, QCOW2_OPT_OVERLAP_MAIN_HEADER);
2113         qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L1);
2114         qdict_del(old_options, QCOW2_OPT_OVERLAP_ACTIVE_L2);
2115         qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_TABLE);
2116         qdict_del(old_options, QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK);
2117         qdict_del(old_options, QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE);
2118         qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L1);
2119         qdict_del(old_options, QCOW2_OPT_OVERLAP_INACTIVE_L2);
2120     }
2121 
2122     /* New total cache size overrides all old options */
2123     if (qdict_haskey(options, QCOW2_OPT_CACHE_SIZE)) {
2124         qdict_del(old_options, QCOW2_OPT_L2_CACHE_SIZE);
2125         qdict_del(old_options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
2126     }
2127 
2128     qdict_join(options, old_options, false);
2129 
2130     /*
2131      * If after merging all cache size options are set, an old total size is
2132      * overwritten. Do keep all options, however, if all three are new. The
2133      * resulting error message is what we want to happen.
2134      */
2135     has_all_cache_options =
2136         qdict_haskey(options, QCOW2_OPT_CACHE_SIZE) ||
2137         qdict_haskey(options, QCOW2_OPT_L2_CACHE_SIZE) ||
2138         qdict_haskey(options, QCOW2_OPT_REFCOUNT_CACHE_SIZE);
2139 
2140     if (has_all_cache_options && !has_new_total_cache_size) {
2141         qdict_del(options, QCOW2_OPT_CACHE_SIZE);
2142     }
2143 }
2144 
2145 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_block_status(BlockDriverState * bs,unsigned int mode,int64_t offset,int64_t count,int64_t * pnum,int64_t * map,BlockDriverState ** file)2146 qcow2_co_block_status(BlockDriverState *bs, unsigned int mode,
2147                       int64_t offset, int64_t count, int64_t *pnum,
2148                       int64_t *map, BlockDriverState **file)
2149 {
2150     BDRVQcow2State *s = bs->opaque;
2151     uint64_t host_offset;
2152     unsigned int bytes;
2153     QCow2SubclusterType type;
2154     int ret, status = 0;
2155 
2156     qemu_co_mutex_lock(&s->lock);
2157 
2158     if (!s->metadata_preallocation_checked) {
2159         ret = qcow2_detect_metadata_preallocation(bs);
2160         s->metadata_preallocation = (ret == 1);
2161         s->metadata_preallocation_checked = true;
2162     }
2163 
2164     bytes = MIN(INT_MAX, count);
2165     ret = qcow2_get_host_offset(bs, offset, &bytes, &host_offset, &type);
2166     qemu_co_mutex_unlock(&s->lock);
2167     if (ret < 0) {
2168         return ret;
2169     }
2170 
2171     *pnum = bytes;
2172 
2173     if ((type == QCOW2_SUBCLUSTER_NORMAL ||
2174          type == QCOW2_SUBCLUSTER_ZERO_ALLOC ||
2175          type == QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC) && !s->crypto) {
2176         *map = host_offset;
2177         *file = s->data_file->bs;
2178         status |= BDRV_BLOCK_OFFSET_VALID;
2179     }
2180     if (type == QCOW2_SUBCLUSTER_ZERO_PLAIN ||
2181         type == QCOW2_SUBCLUSTER_ZERO_ALLOC) {
2182         status |= BDRV_BLOCK_ZERO;
2183     } else if (type != QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN &&
2184                type != QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC) {
2185         status |= BDRV_BLOCK_DATA;
2186     }
2187     if (s->metadata_preallocation && (status & BDRV_BLOCK_DATA) &&
2188         (status & BDRV_BLOCK_OFFSET_VALID))
2189     {
2190         status |= BDRV_BLOCK_RECURSE;
2191     }
2192     if (type == QCOW2_SUBCLUSTER_COMPRESSED) {
2193         status |= BDRV_BLOCK_COMPRESSED;
2194     }
2195     return status;
2196 }
2197 
2198 static int coroutine_fn GRAPH_RDLOCK
qcow2_handle_l2meta(BlockDriverState * bs,QCowL2Meta ** pl2meta,bool link_l2)2199 qcow2_handle_l2meta(BlockDriverState *bs, QCowL2Meta **pl2meta, bool link_l2)
2200 {
2201     int ret = 0;
2202     QCowL2Meta *l2meta = *pl2meta;
2203 
2204     while (l2meta != NULL) {
2205         QCowL2Meta *next;
2206 
2207         if (link_l2) {
2208             ret = qcow2_alloc_cluster_link_l2(bs, l2meta);
2209             if (ret) {
2210                 goto out;
2211             }
2212         } else {
2213             qcow2_alloc_cluster_abort(bs, l2meta);
2214         }
2215 
2216         /* Take the request off the list of running requests */
2217         QLIST_REMOVE(l2meta, next_in_flight);
2218 
2219         qemu_co_queue_restart_all(&l2meta->dependent_requests);
2220 
2221         next = l2meta->next;
2222         g_free(l2meta);
2223         l2meta = next;
2224     }
2225 out:
2226     *pl2meta = l2meta;
2227     return ret;
2228 }
2229 
2230 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_preadv_encrypted(BlockDriverState * bs,uint64_t host_offset,uint64_t offset,uint64_t bytes,QEMUIOVector * qiov,uint64_t qiov_offset)2231 qcow2_co_preadv_encrypted(BlockDriverState *bs,
2232                            uint64_t host_offset,
2233                            uint64_t offset,
2234                            uint64_t bytes,
2235                            QEMUIOVector *qiov,
2236                            uint64_t qiov_offset)
2237 {
2238     int ret;
2239     BDRVQcow2State *s = bs->opaque;
2240     uint8_t *buf;
2241 
2242     assert(bs->encrypted && s->crypto);
2243     assert(bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
2244 
2245     /*
2246      * For encrypted images, read everything into a temporary
2247      * contiguous buffer on which the AES functions can work.
2248      * Also, decryption in a separate buffer is better as it
2249      * prevents the guest from learning information about the
2250      * encrypted nature of the virtual disk.
2251      */
2252 
2253     buf = qemu_try_blockalign(s->data_file->bs, bytes);
2254     if (buf == NULL) {
2255         return -ENOMEM;
2256     }
2257 
2258     BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_AIO);
2259     ret = bdrv_co_pread(s->data_file, host_offset, bytes, buf, 0);
2260     if (ret < 0) {
2261         goto fail;
2262     }
2263 
2264     if (qcow2_co_decrypt(bs, host_offset, offset, buf, bytes) < 0)
2265     {
2266         ret = -EIO;
2267         goto fail;
2268     }
2269     qemu_iovec_from_buf(qiov, qiov_offset, buf, bytes);
2270 
2271 fail:
2272     qemu_vfree(buf);
2273 
2274     return ret;
2275 }
2276 
2277 typedef struct Qcow2AioTask {
2278     AioTask task;
2279 
2280     BlockDriverState *bs;
2281     QCow2SubclusterType subcluster_type; /* only for read */
2282     uint64_t host_offset; /* or l2_entry for compressed read */
2283     uint64_t offset;
2284     uint64_t bytes;
2285     QEMUIOVector *qiov;
2286     uint64_t qiov_offset;
2287     QCowL2Meta *l2meta; /* only for write */
2288 } Qcow2AioTask;
2289 
2290 static coroutine_fn int qcow2_co_preadv_task_entry(AioTask *task);
qcow2_add_task(BlockDriverState * bs,AioTaskPool * pool,AioTaskFunc func,QCow2SubclusterType subcluster_type,uint64_t host_offset,uint64_t offset,uint64_t bytes,QEMUIOVector * qiov,size_t qiov_offset,QCowL2Meta * l2meta)2291 static coroutine_fn int qcow2_add_task(BlockDriverState *bs,
2292                                        AioTaskPool *pool,
2293                                        AioTaskFunc func,
2294                                        QCow2SubclusterType subcluster_type,
2295                                        uint64_t host_offset,
2296                                        uint64_t offset,
2297                                        uint64_t bytes,
2298                                        QEMUIOVector *qiov,
2299                                        size_t qiov_offset,
2300                                        QCowL2Meta *l2meta)
2301 {
2302     Qcow2AioTask local_task;
2303     Qcow2AioTask *task = pool ? g_new(Qcow2AioTask, 1) : &local_task;
2304 
2305     *task = (Qcow2AioTask) {
2306         .task.func = func,
2307         .bs = bs,
2308         .subcluster_type = subcluster_type,
2309         .qiov = qiov,
2310         .host_offset = host_offset,
2311         .offset = offset,
2312         .bytes = bytes,
2313         .qiov_offset = qiov_offset,
2314         .l2meta = l2meta,
2315     };
2316 
2317     trace_qcow2_add_task(qemu_coroutine_self(), bs, pool,
2318                          func == qcow2_co_preadv_task_entry ? "read" : "write",
2319                          subcluster_type, host_offset, offset, bytes,
2320                          qiov, qiov_offset);
2321 
2322     if (!pool) {
2323         return func(&task->task);
2324     }
2325 
2326     aio_task_pool_start_task(pool, &task->task);
2327 
2328     return 0;
2329 }
2330 
2331 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_preadv_task(BlockDriverState * bs,QCow2SubclusterType subc_type,uint64_t host_offset,uint64_t offset,uint64_t bytes,QEMUIOVector * qiov,size_t qiov_offset)2332 qcow2_co_preadv_task(BlockDriverState *bs, QCow2SubclusterType subc_type,
2333                      uint64_t host_offset, uint64_t offset, uint64_t bytes,
2334                      QEMUIOVector *qiov, size_t qiov_offset)
2335 {
2336     BDRVQcow2State *s = bs->opaque;
2337 
2338     switch (subc_type) {
2339     case QCOW2_SUBCLUSTER_ZERO_PLAIN:
2340     case QCOW2_SUBCLUSTER_ZERO_ALLOC:
2341         /* Both zero types are handled in qcow2_co_preadv_part */
2342         g_assert_not_reached();
2343 
2344     case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
2345     case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
2346         assert(bs->backing); /* otherwise handled in qcow2_co_preadv_part */
2347 
2348         BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
2349         return bdrv_co_preadv_part(bs->backing, offset, bytes,
2350                                    qiov, qiov_offset, 0);
2351 
2352     case QCOW2_SUBCLUSTER_COMPRESSED:
2353         return qcow2_co_preadv_compressed(bs, host_offset,
2354                                           offset, bytes, qiov, qiov_offset);
2355 
2356     case QCOW2_SUBCLUSTER_NORMAL:
2357         if (bs->encrypted) {
2358             return qcow2_co_preadv_encrypted(bs, host_offset,
2359                                              offset, bytes, qiov, qiov_offset);
2360         }
2361 
2362         BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_AIO);
2363         return bdrv_co_preadv_part(s->data_file, host_offset,
2364                                    bytes, qiov, qiov_offset, 0);
2365 
2366     default:
2367         g_assert_not_reached();
2368     }
2369 
2370     g_assert_not_reached();
2371 }
2372 
2373 /*
2374  * This function can count as GRAPH_RDLOCK because qcow2_co_preadv_part() holds
2375  * the graph lock and keeps it until this coroutine has terminated.
2376  */
qcow2_co_preadv_task_entry(AioTask * task)2377 static int coroutine_fn GRAPH_RDLOCK qcow2_co_preadv_task_entry(AioTask *task)
2378 {
2379     Qcow2AioTask *t = container_of(task, Qcow2AioTask, task);
2380 
2381     assert(!t->l2meta);
2382 
2383     return qcow2_co_preadv_task(t->bs, t->subcluster_type,
2384                                 t->host_offset, t->offset, t->bytes,
2385                                 t->qiov, t->qiov_offset);
2386 }
2387 
2388 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_preadv_part(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,size_t qiov_offset,BdrvRequestFlags flags)2389 qcow2_co_preadv_part(BlockDriverState *bs, int64_t offset, int64_t bytes,
2390                      QEMUIOVector *qiov, size_t qiov_offset,
2391                      BdrvRequestFlags flags)
2392 {
2393     BDRVQcow2State *s = bs->opaque;
2394     int ret = 0;
2395     unsigned int cur_bytes; /* number of bytes in current iteration */
2396     uint64_t host_offset = 0;
2397     QCow2SubclusterType type;
2398     AioTaskPool *aio = NULL;
2399 
2400     while (bytes != 0 && aio_task_pool_status(aio) == 0) {
2401         /* prepare next request */
2402         cur_bytes = MIN(bytes, INT_MAX);
2403         if (s->crypto) {
2404             cur_bytes = MIN(cur_bytes,
2405                             QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
2406         }
2407 
2408         qemu_co_mutex_lock(&s->lock);
2409         ret = qcow2_get_host_offset(bs, offset, &cur_bytes,
2410                                     &host_offset, &type);
2411         qemu_co_mutex_unlock(&s->lock);
2412         if (ret < 0) {
2413             goto out;
2414         }
2415 
2416         if (type == QCOW2_SUBCLUSTER_ZERO_PLAIN ||
2417             type == QCOW2_SUBCLUSTER_ZERO_ALLOC ||
2418             (type == QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN && !bs->backing) ||
2419             (type == QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC && !bs->backing))
2420         {
2421             qemu_iovec_memset(qiov, qiov_offset, 0, cur_bytes);
2422         } else {
2423             if (!aio && cur_bytes != bytes) {
2424                 aio = aio_task_pool_new(QCOW2_MAX_WORKERS);
2425             }
2426             ret = qcow2_add_task(bs, aio, qcow2_co_preadv_task_entry, type,
2427                                  host_offset, offset, cur_bytes,
2428                                  qiov, qiov_offset, NULL);
2429             if (ret < 0) {
2430                 goto out;
2431             }
2432         }
2433 
2434         bytes -= cur_bytes;
2435         offset += cur_bytes;
2436         qiov_offset += cur_bytes;
2437     }
2438 
2439 out:
2440     if (aio) {
2441         aio_task_pool_wait_all(aio);
2442         if (ret == 0) {
2443             ret = aio_task_pool_status(aio);
2444         }
2445         g_free(aio);
2446     }
2447 
2448     return ret;
2449 }
2450 
2451 /* Check if it's possible to merge a write request with the writing of
2452  * the data from the COW regions */
merge_cow(uint64_t offset,unsigned bytes,QEMUIOVector * qiov,size_t qiov_offset,QCowL2Meta * l2meta)2453 static bool merge_cow(uint64_t offset, unsigned bytes,
2454                       QEMUIOVector *qiov, size_t qiov_offset,
2455                       QCowL2Meta *l2meta)
2456 {
2457     QCowL2Meta *m;
2458 
2459     for (m = l2meta; m != NULL; m = m->next) {
2460         /* If both COW regions are empty then there's nothing to merge */
2461         if (m->cow_start.nb_bytes == 0 && m->cow_end.nb_bytes == 0) {
2462             continue;
2463         }
2464 
2465         /* If COW regions are handled already, skip this too */
2466         if (m->skip_cow) {
2467             continue;
2468         }
2469 
2470         /*
2471          * The write request should start immediately after the first
2472          * COW region. This does not always happen because the area
2473          * touched by the request can be larger than the one defined
2474          * by @m (a single request can span an area consisting of a
2475          * mix of previously unallocated and allocated clusters, that
2476          * is why @l2meta is a list).
2477          */
2478         if (l2meta_cow_start(m) + m->cow_start.nb_bytes != offset) {
2479             /* In this case the request starts before this region */
2480             assert(offset < l2meta_cow_start(m));
2481             assert(m->cow_start.nb_bytes == 0);
2482             continue;
2483         }
2484 
2485         /* The write request should end immediately before the second
2486          * COW region (see above for why it does not always happen) */
2487         if (m->offset + m->cow_end.offset != offset + bytes) {
2488             assert(offset + bytes > m->offset + m->cow_end.offset);
2489             assert(m->cow_end.nb_bytes == 0);
2490             continue;
2491         }
2492 
2493         /* Make sure that adding both COW regions to the QEMUIOVector
2494          * does not exceed IOV_MAX */
2495         if (qemu_iovec_subvec_niov(qiov, qiov_offset, bytes) > IOV_MAX - 2) {
2496             continue;
2497         }
2498 
2499         m->data_qiov = qiov;
2500         m->data_qiov_offset = qiov_offset;
2501         return true;
2502     }
2503 
2504     return false;
2505 }
2506 
2507 /*
2508  * Return 1 if the COW regions read as zeroes, 0 if not, < 0 on error.
2509  * Note that returning 0 does not guarantee non-zero data.
2510  */
2511 static int coroutine_fn GRAPH_RDLOCK
is_zero_cow(BlockDriverState * bs,QCowL2Meta * m)2512 is_zero_cow(BlockDriverState *bs, QCowL2Meta *m)
2513 {
2514     /*
2515      * This check is designed for optimization shortcut so it must be
2516      * efficient.
2517      * Instead of is_zero(), use bdrv_co_is_zero_fast() as it is
2518      * faster (but not as accurate and can result in false negatives).
2519      */
2520     int ret = bdrv_co_is_zero_fast(bs, m->offset + m->cow_start.offset,
2521                                    m->cow_start.nb_bytes);
2522     if (ret <= 0) {
2523         return ret;
2524     }
2525 
2526     return bdrv_co_is_zero_fast(bs, m->offset + m->cow_end.offset,
2527                                 m->cow_end.nb_bytes);
2528 }
2529 
2530 static int coroutine_fn GRAPH_RDLOCK
handle_alloc_space(BlockDriverState * bs,QCowL2Meta * l2meta)2531 handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta)
2532 {
2533     BDRVQcow2State *s = bs->opaque;
2534     QCowL2Meta *m;
2535 
2536     if (!(s->data_file->bs->supported_zero_flags & BDRV_REQ_NO_FALLBACK)) {
2537         return 0;
2538     }
2539 
2540     if (bs->encrypted) {
2541         return 0;
2542     }
2543 
2544     for (m = l2meta; m != NULL; m = m->next) {
2545         int ret;
2546         uint64_t start_offset = m->alloc_offset + m->cow_start.offset;
2547         unsigned nb_bytes = m->cow_end.offset + m->cow_end.nb_bytes -
2548             m->cow_start.offset;
2549 
2550         if (!m->cow_start.nb_bytes && !m->cow_end.nb_bytes) {
2551             continue;
2552         }
2553 
2554         ret = is_zero_cow(bs, m);
2555         if (ret < 0) {
2556             return ret;
2557         } else if (ret == 0) {
2558             continue;
2559         }
2560 
2561         /*
2562          * instead of writing zero COW buffers,
2563          * efficiently zero out the whole clusters
2564          */
2565 
2566         ret = qcow2_pre_write_overlap_check(bs, 0, start_offset, nb_bytes,
2567                                             true);
2568         if (ret < 0) {
2569             return ret;
2570         }
2571 
2572         BLKDBG_CO_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC_SPACE);
2573         ret = bdrv_co_pwrite_zeroes(s->data_file, start_offset, nb_bytes,
2574                                     BDRV_REQ_NO_FALLBACK);
2575         if (ret < 0) {
2576             if (ret != -ENOTSUP && ret != -EAGAIN) {
2577                 return ret;
2578             }
2579             continue;
2580         }
2581 
2582         trace_qcow2_skip_cow(qemu_coroutine_self(), m->offset, m->nb_clusters);
2583         m->skip_cow = true;
2584     }
2585     return 0;
2586 }
2587 
2588 /*
2589  * qcow2_co_pwritev_task
2590  * Called with s->lock unlocked
2591  * l2meta  - if not NULL, qcow2_co_pwritev_task() will consume it. Caller must
2592  *           not use it somehow after qcow2_co_pwritev_task() call
2593  */
2594 static coroutine_fn GRAPH_RDLOCK
qcow2_co_pwritev_task(BlockDriverState * bs,uint64_t host_offset,uint64_t offset,uint64_t bytes,QEMUIOVector * qiov,uint64_t qiov_offset,QCowL2Meta * l2meta)2595 int qcow2_co_pwritev_task(BlockDriverState *bs, uint64_t host_offset,
2596                           uint64_t offset, uint64_t bytes, QEMUIOVector *qiov,
2597                           uint64_t qiov_offset, QCowL2Meta *l2meta)
2598 {
2599     int ret;
2600     BDRVQcow2State *s = bs->opaque;
2601     void *crypt_buf = NULL;
2602     QEMUIOVector encrypted_qiov;
2603 
2604     if (bs->encrypted) {
2605         assert(s->crypto);
2606         assert(bytes <= QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size);
2607         crypt_buf = qemu_try_blockalign(bs->file->bs, bytes);
2608         if (crypt_buf == NULL) {
2609             ret = -ENOMEM;
2610             goto out_unlocked;
2611         }
2612         qemu_iovec_to_buf(qiov, qiov_offset, crypt_buf, bytes);
2613 
2614         if (qcow2_co_encrypt(bs, host_offset, offset, crypt_buf, bytes) < 0) {
2615             ret = -EIO;
2616             goto out_unlocked;
2617         }
2618 
2619         qemu_iovec_init_buf(&encrypted_qiov, crypt_buf, bytes);
2620         qiov = &encrypted_qiov;
2621         qiov_offset = 0;
2622     }
2623 
2624     /* Try to efficiently initialize the physical space with zeroes */
2625     ret = handle_alloc_space(bs, l2meta);
2626     if (ret < 0) {
2627         goto out_unlocked;
2628     }
2629 
2630     /*
2631      * If we need to do COW, check if it's possible to merge the
2632      * writing of the guest data together with that of the COW regions.
2633      * If it's not possible (or not necessary) then write the
2634      * guest data now.
2635      */
2636     if (!merge_cow(offset, bytes, qiov, qiov_offset, l2meta)) {
2637         BLKDBG_CO_EVENT(bs->file, BLKDBG_WRITE_AIO);
2638         trace_qcow2_writev_data(qemu_coroutine_self(), host_offset);
2639         ret = bdrv_co_pwritev_part(s->data_file, host_offset,
2640                                    bytes, qiov, qiov_offset, 0);
2641         if (ret < 0) {
2642             goto out_unlocked;
2643         }
2644     }
2645 
2646     qemu_co_mutex_lock(&s->lock);
2647 
2648     ret = qcow2_handle_l2meta(bs, &l2meta, true);
2649     goto out_locked;
2650 
2651 out_unlocked:
2652     qemu_co_mutex_lock(&s->lock);
2653 
2654 out_locked:
2655     qcow2_handle_l2meta(bs, &l2meta, false);
2656     qemu_co_mutex_unlock(&s->lock);
2657 
2658     qemu_vfree(crypt_buf);
2659 
2660     return ret;
2661 }
2662 
2663 /*
2664  * This function can count as GRAPH_RDLOCK because qcow2_co_pwritev_part() holds
2665  * the graph lock and keeps it until this coroutine has terminated.
2666  */
qcow2_co_pwritev_task_entry(AioTask * task)2667 static coroutine_fn GRAPH_RDLOCK int qcow2_co_pwritev_task_entry(AioTask *task)
2668 {
2669     Qcow2AioTask *t = container_of(task, Qcow2AioTask, task);
2670 
2671     assert(!t->subcluster_type);
2672 
2673     return qcow2_co_pwritev_task(t->bs, t->host_offset,
2674                                  t->offset, t->bytes, t->qiov, t->qiov_offset,
2675                                  t->l2meta);
2676 }
2677 
2678 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_pwritev_part(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,size_t qiov_offset,BdrvRequestFlags flags)2679 qcow2_co_pwritev_part(BlockDriverState *bs, int64_t offset, int64_t bytes,
2680                       QEMUIOVector *qiov, size_t qiov_offset,
2681                       BdrvRequestFlags flags)
2682 {
2683     BDRVQcow2State *s = bs->opaque;
2684     int offset_in_cluster;
2685     int ret;
2686     unsigned int cur_bytes; /* number of sectors in current iteration */
2687     uint64_t host_offset;
2688     QCowL2Meta *l2meta = NULL;
2689     AioTaskPool *aio = NULL;
2690 
2691     trace_qcow2_writev_start_req(qemu_coroutine_self(), offset, bytes);
2692 
2693     while (bytes != 0 && aio_task_pool_status(aio) == 0) {
2694 
2695         l2meta = NULL;
2696 
2697         trace_qcow2_writev_start_part(qemu_coroutine_self());
2698         offset_in_cluster = offset_into_cluster(s, offset);
2699         cur_bytes = MIN(bytes, INT_MAX);
2700         if (bs->encrypted) {
2701             cur_bytes = MIN(cur_bytes,
2702                             QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size
2703                             - offset_in_cluster);
2704         }
2705 
2706         qemu_co_mutex_lock(&s->lock);
2707 
2708         ret = qcow2_alloc_host_offset(bs, offset, &cur_bytes,
2709                                       &host_offset, &l2meta);
2710         if (ret < 0) {
2711             goto out_locked;
2712         }
2713 
2714         ret = qcow2_pre_write_overlap_check(bs, 0, host_offset,
2715                                             cur_bytes, true);
2716         if (ret < 0) {
2717             goto out_locked;
2718         }
2719 
2720         qemu_co_mutex_unlock(&s->lock);
2721 
2722         if (!aio && cur_bytes != bytes) {
2723             aio = aio_task_pool_new(QCOW2_MAX_WORKERS);
2724         }
2725         ret = qcow2_add_task(bs, aio, qcow2_co_pwritev_task_entry, 0,
2726                              host_offset, offset,
2727                              cur_bytes, qiov, qiov_offset, l2meta);
2728         l2meta = NULL; /* l2meta is consumed by qcow2_co_pwritev_task() */
2729         if (ret < 0) {
2730             goto fail_nometa;
2731         }
2732 
2733         bytes -= cur_bytes;
2734         offset += cur_bytes;
2735         qiov_offset += cur_bytes;
2736         trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes);
2737     }
2738     ret = 0;
2739 
2740     qemu_co_mutex_lock(&s->lock);
2741 
2742 out_locked:
2743     qcow2_handle_l2meta(bs, &l2meta, false);
2744 
2745     qemu_co_mutex_unlock(&s->lock);
2746 
2747 fail_nometa:
2748     if (aio) {
2749         aio_task_pool_wait_all(aio);
2750         if (ret == 0) {
2751             ret = aio_task_pool_status(aio);
2752         }
2753         g_free(aio);
2754     }
2755 
2756     trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
2757 
2758     return ret;
2759 }
2760 
qcow2_inactivate(BlockDriverState * bs)2761 static int GRAPH_RDLOCK qcow2_inactivate(BlockDriverState *bs)
2762 {
2763     BDRVQcow2State *s = bs->opaque;
2764     int ret, result = 0;
2765     Error *local_err = NULL;
2766 
2767     qcow2_store_persistent_dirty_bitmaps(bs, true, &local_err);
2768     if (local_err != NULL) {
2769         result = -EINVAL;
2770         error_reportf_err(local_err, "Lost persistent bitmaps during "
2771                           "inactivation of node '%s': ",
2772                           bdrv_get_device_or_node_name(bs));
2773     }
2774 
2775     ret = qcow2_cache_flush(bs, s->l2_table_cache);
2776     if (ret) {
2777         result = ret;
2778         error_report("Failed to flush the L2 table cache: %s",
2779                      strerror(-ret));
2780     }
2781 
2782     ret = qcow2_cache_flush(bs, s->refcount_block_cache);
2783     if (ret) {
2784         result = ret;
2785         error_report("Failed to flush the refcount block cache: %s",
2786                      strerror(-ret));
2787     }
2788 
2789     if (result == 0) {
2790         qcow2_mark_clean(bs);
2791     }
2792 
2793     return result;
2794 }
2795 
2796 static void coroutine_mixed_fn GRAPH_RDLOCK
qcow2_do_close(BlockDriverState * bs,bool close_data_file)2797 qcow2_do_close(BlockDriverState *bs, bool close_data_file)
2798 {
2799     BDRVQcow2State *s = bs->opaque;
2800     qemu_vfree(s->l1_table);
2801     /* else pre-write overlap checks in cache_destroy may crash */
2802     s->l1_table = NULL;
2803 
2804     if (!(s->flags & BDRV_O_INACTIVE)) {
2805         qcow2_inactivate(bs);
2806     }
2807 
2808     cache_clean_timer_del(bs);
2809     qcow2_cache_destroy(s->l2_table_cache);
2810     qcow2_cache_destroy(s->refcount_block_cache);
2811 
2812     qcrypto_block_free(s->crypto);
2813     s->crypto = NULL;
2814     qapi_free_QCryptoBlockOpenOptions(s->crypto_opts);
2815 
2816     g_free(s->unknown_header_fields);
2817     cleanup_unknown_header_ext(bs);
2818 
2819     g_free(s->image_data_file);
2820     g_free(s->image_backing_file);
2821     g_free(s->image_backing_format);
2822 
2823     if (close_data_file && has_data_file(bs)) {
2824         GLOBAL_STATE_CODE();
2825         bdrv_graph_rdunlock_main_loop();
2826         bdrv_drain_all_begin();
2827         bdrv_graph_wrlock();
2828         bdrv_unref_child(bs, s->data_file);
2829         bdrv_graph_wrunlock();
2830         bdrv_drain_all_end();
2831         s->data_file = NULL;
2832         bdrv_graph_rdlock_main_loop();
2833     }
2834 
2835     qcow2_refcount_close(bs);
2836     qcow2_free_snapshots(bs);
2837 }
2838 
qcow2_close(BlockDriverState * bs)2839 static void GRAPH_UNLOCKED qcow2_close(BlockDriverState *bs)
2840 {
2841     GLOBAL_STATE_CODE();
2842     GRAPH_RDLOCK_GUARD_MAINLOOP();
2843 
2844     qcow2_do_close(bs, true);
2845 }
2846 
2847 static void coroutine_fn GRAPH_RDLOCK
qcow2_co_invalidate_cache(BlockDriverState * bs,Error ** errp)2848 qcow2_co_invalidate_cache(BlockDriverState *bs, Error **errp)
2849 {
2850     ERRP_GUARD();
2851     BDRVQcow2State *s = bs->opaque;
2852     BdrvChild *data_file;
2853     int flags = s->flags;
2854     QCryptoBlock *crypto = NULL;
2855     QDict *options;
2856     int ret;
2857 
2858     /*
2859      * Backing files are read-only which makes all of their metadata immutable,
2860      * that means we don't have to worry about reopening them here.
2861      */
2862 
2863     crypto = s->crypto;
2864     s->crypto = NULL;
2865 
2866     /*
2867      * Do not reopen s->data_file (i.e., have qcow2_do_close() not close it,
2868      * and then prevent qcow2_do_open() from opening it), because this function
2869      * runs in the I/O path and as such we must not invoke global-state
2870      * functions like bdrv_unref_child() and bdrv_open_child().
2871      */
2872 
2873     qcow2_do_close(bs, false);
2874 
2875     data_file = s->data_file;
2876     memset(s, 0, sizeof(BDRVQcow2State));
2877     s->data_file = data_file;
2878 
2879     options = qdict_clone_shallow(bs->options);
2880 
2881     flags &= ~BDRV_O_INACTIVE;
2882     qemu_co_mutex_lock(&s->lock);
2883     ret = qcow2_do_open(bs, options, flags, false, errp);
2884     qemu_co_mutex_unlock(&s->lock);
2885     qobject_unref(options);
2886     if (ret < 0) {
2887         error_prepend(errp, "Could not reopen qcow2 layer: ");
2888         bs->drv = NULL;
2889         return;
2890     }
2891 
2892     s->crypto = crypto;
2893 }
2894 
header_ext_add(char * buf,uint32_t magic,const void * s,size_t len,size_t buflen)2895 static size_t header_ext_add(char *buf, uint32_t magic, const void *s,
2896     size_t len, size_t buflen)
2897 {
2898     QCowExtension *ext_backing_fmt = (QCowExtension*) buf;
2899     size_t ext_len = sizeof(QCowExtension) + ((len + 7) & ~7);
2900 
2901     if (buflen < ext_len) {
2902         return -ENOSPC;
2903     }
2904 
2905     *ext_backing_fmt = (QCowExtension) {
2906         .magic  = cpu_to_be32(magic),
2907         .len    = cpu_to_be32(len),
2908     };
2909 
2910     if (len) {
2911         memcpy(buf + sizeof(QCowExtension), s, len);
2912     }
2913 
2914     return ext_len;
2915 }
2916 
2917 /*
2918  * Updates the qcow2 header, including the variable length parts of it, i.e.
2919  * the backing file name and all extensions. qcow2 was not designed to allow
2920  * such changes, so if we run out of space (we can only use the first cluster)
2921  * this function may fail.
2922  *
2923  * Returns 0 on success, -errno in error cases.
2924  */
qcow2_update_header(BlockDriverState * bs)2925 int qcow2_update_header(BlockDriverState *bs)
2926 {
2927     BDRVQcow2State *s = bs->opaque;
2928     QCowHeader *header;
2929     char *buf;
2930     size_t buflen = s->cluster_size;
2931     int ret;
2932     uint64_t total_size;
2933     uint32_t refcount_table_clusters;
2934     size_t header_length;
2935     Qcow2UnknownHeaderExtension *uext;
2936 
2937     buf = qemu_blockalign(bs, buflen);
2938 
2939     /* Header structure */
2940     header = (QCowHeader*) buf;
2941 
2942     if (buflen < sizeof(*header)) {
2943         ret = -ENOSPC;
2944         goto fail;
2945     }
2946 
2947     header_length = sizeof(*header) + s->unknown_header_fields_size;
2948     total_size = bs->total_sectors * BDRV_SECTOR_SIZE;
2949     refcount_table_clusters = s->refcount_table_size >> (s->cluster_bits - 3);
2950 
2951     ret = validate_compression_type(s, NULL);
2952     if (ret) {
2953         goto fail;
2954     }
2955 
2956     *header = (QCowHeader) {
2957         /* Version 2 fields */
2958         .magic                  = cpu_to_be32(QCOW_MAGIC),
2959         .version                = cpu_to_be32(s->qcow_version),
2960         .backing_file_offset    = 0,
2961         .backing_file_size      = 0,
2962         .cluster_bits           = cpu_to_be32(s->cluster_bits),
2963         .size                   = cpu_to_be64(total_size),
2964         .crypt_method           = cpu_to_be32(s->crypt_method_header),
2965         .l1_size                = cpu_to_be32(s->l1_size),
2966         .l1_table_offset        = cpu_to_be64(s->l1_table_offset),
2967         .refcount_table_offset  = cpu_to_be64(s->refcount_table_offset),
2968         .refcount_table_clusters = cpu_to_be32(refcount_table_clusters),
2969         .nb_snapshots           = cpu_to_be32(s->nb_snapshots),
2970         .snapshots_offset       = cpu_to_be64(s->snapshots_offset),
2971 
2972         /* Version 3 fields */
2973         .incompatible_features  = cpu_to_be64(s->incompatible_features),
2974         .compatible_features    = cpu_to_be64(s->compatible_features),
2975         .autoclear_features     = cpu_to_be64(s->autoclear_features),
2976         .refcount_order         = cpu_to_be32(s->refcount_order),
2977         .header_length          = cpu_to_be32(header_length),
2978         .compression_type       = s->compression_type,
2979     };
2980 
2981     /* For older versions, write a shorter header */
2982     switch (s->qcow_version) {
2983     case 2:
2984         ret = offsetof(QCowHeader, incompatible_features);
2985         break;
2986     case 3:
2987         ret = sizeof(*header);
2988         break;
2989     default:
2990         ret = -EINVAL;
2991         goto fail;
2992     }
2993 
2994     buf += ret;
2995     buflen -= ret;
2996     memset(buf, 0, buflen);
2997 
2998     /* Preserve any unknown field in the header */
2999     if (s->unknown_header_fields_size) {
3000         if (buflen < s->unknown_header_fields_size) {
3001             ret = -ENOSPC;
3002             goto fail;
3003         }
3004 
3005         memcpy(buf, s->unknown_header_fields, s->unknown_header_fields_size);
3006         buf += s->unknown_header_fields_size;
3007         buflen -= s->unknown_header_fields_size;
3008     }
3009 
3010     /* Backing file format header extension */
3011     if (s->image_backing_format) {
3012         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BACKING_FORMAT,
3013                              s->image_backing_format,
3014                              strlen(s->image_backing_format),
3015                              buflen);
3016         if (ret < 0) {
3017             goto fail;
3018         }
3019 
3020         buf += ret;
3021         buflen -= ret;
3022     }
3023 
3024     /* External data file header extension */
3025     if (has_data_file(bs) && s->image_data_file) {
3026         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_DATA_FILE,
3027                              s->image_data_file, strlen(s->image_data_file),
3028                              buflen);
3029         if (ret < 0) {
3030             goto fail;
3031         }
3032 
3033         buf += ret;
3034         buflen -= ret;
3035     }
3036 
3037     /* Full disk encryption header pointer extension */
3038     if (s->crypto_header.offset != 0) {
3039         s->crypto_header.offset = cpu_to_be64(s->crypto_header.offset);
3040         s->crypto_header.length = cpu_to_be64(s->crypto_header.length);
3041         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_CRYPTO_HEADER,
3042                              &s->crypto_header, sizeof(s->crypto_header),
3043                              buflen);
3044         s->crypto_header.offset = be64_to_cpu(s->crypto_header.offset);
3045         s->crypto_header.length = be64_to_cpu(s->crypto_header.length);
3046         if (ret < 0) {
3047             goto fail;
3048         }
3049         buf += ret;
3050         buflen -= ret;
3051     }
3052 
3053     /*
3054      * Feature table.  A mere 8 feature names occupies 392 bytes, and
3055      * when coupled with the v3 minimum header of 104 bytes plus the
3056      * 8-byte end-of-extension marker, that would leave only 8 bytes
3057      * for a backing file name in an image with 512-byte clusters.
3058      * Thus, we choose to omit this header for cluster sizes 4k and
3059      * smaller.
3060      */
3061     if (s->qcow_version >= 3 && s->cluster_size > 4096) {
3062         static const Qcow2Feature features[] = {
3063             {
3064                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
3065                 .bit  = QCOW2_INCOMPAT_DIRTY_BITNR,
3066                 .name = "dirty bit",
3067             },
3068             {
3069                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
3070                 .bit  = QCOW2_INCOMPAT_CORRUPT_BITNR,
3071                 .name = "corrupt bit",
3072             },
3073             {
3074                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
3075                 .bit  = QCOW2_INCOMPAT_DATA_FILE_BITNR,
3076                 .name = "external data file",
3077             },
3078             {
3079                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
3080                 .bit  = QCOW2_INCOMPAT_COMPRESSION_BITNR,
3081                 .name = "compression type",
3082             },
3083             {
3084                 .type = QCOW2_FEAT_TYPE_INCOMPATIBLE,
3085                 .bit  = QCOW2_INCOMPAT_EXTL2_BITNR,
3086                 .name = "extended L2 entries",
3087             },
3088             {
3089                 .type = QCOW2_FEAT_TYPE_COMPATIBLE,
3090                 .bit  = QCOW2_COMPAT_LAZY_REFCOUNTS_BITNR,
3091                 .name = "lazy refcounts",
3092             },
3093             {
3094                 .type = QCOW2_FEAT_TYPE_AUTOCLEAR,
3095                 .bit  = QCOW2_AUTOCLEAR_BITMAPS_BITNR,
3096                 .name = "bitmaps",
3097             },
3098             {
3099                 .type = QCOW2_FEAT_TYPE_AUTOCLEAR,
3100                 .bit  = QCOW2_AUTOCLEAR_DATA_FILE_RAW_BITNR,
3101                 .name = "raw external data",
3102             },
3103         };
3104 
3105         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_FEATURE_TABLE,
3106                              features, sizeof(features), buflen);
3107         if (ret < 0) {
3108             goto fail;
3109         }
3110         buf += ret;
3111         buflen -= ret;
3112     }
3113 
3114     /* Bitmap extension */
3115     if (s->nb_bitmaps > 0) {
3116         Qcow2BitmapHeaderExt bitmaps_header = {
3117             .nb_bitmaps = cpu_to_be32(s->nb_bitmaps),
3118             .bitmap_directory_size =
3119                     cpu_to_be64(s->bitmap_directory_size),
3120             .bitmap_directory_offset =
3121                     cpu_to_be64(s->bitmap_directory_offset)
3122         };
3123         ret = header_ext_add(buf, QCOW2_EXT_MAGIC_BITMAPS,
3124                              &bitmaps_header, sizeof(bitmaps_header),
3125                              buflen);
3126         if (ret < 0) {
3127             goto fail;
3128         }
3129         buf += ret;
3130         buflen -= ret;
3131     }
3132 
3133     /* Keep unknown header extensions */
3134     QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
3135         ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
3136         if (ret < 0) {
3137             goto fail;
3138         }
3139 
3140         buf += ret;
3141         buflen -= ret;
3142     }
3143 
3144     /* End of header extensions */
3145     ret = header_ext_add(buf, QCOW2_EXT_MAGIC_END, NULL, 0, buflen);
3146     if (ret < 0) {
3147         goto fail;
3148     }
3149 
3150     buf += ret;
3151     buflen -= ret;
3152 
3153     /* Backing file name */
3154     if (s->image_backing_file) {
3155         size_t backing_file_len = strlen(s->image_backing_file);
3156 
3157         if (buflen < backing_file_len) {
3158             ret = -ENOSPC;
3159             goto fail;
3160         }
3161 
3162         /* Using strncpy is ok here, since buf is not NUL-terminated. */
3163         strncpy(buf, s->image_backing_file, buflen);
3164 
3165         header->backing_file_offset = cpu_to_be64(buf - ((char*) header));
3166         header->backing_file_size   = cpu_to_be32(backing_file_len);
3167     }
3168 
3169     /* Write the new header */
3170     ret = bdrv_pwrite(bs->file, 0, s->cluster_size, header, 0);
3171     if (ret < 0) {
3172         goto fail;
3173     }
3174 
3175     ret = 0;
3176 fail:
3177     qemu_vfree(header);
3178     return ret;
3179 }
3180 
3181 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_change_backing_file(BlockDriverState * bs,const char * backing_file,const char * backing_fmt)3182 qcow2_co_change_backing_file(BlockDriverState *bs, const char *backing_file,
3183                              const char *backing_fmt)
3184 {
3185     BDRVQcow2State *s = bs->opaque;
3186 
3187     /* Adding a backing file means that the external data file alone won't be
3188      * enough to make sense of the content */
3189     if (backing_file && data_file_is_raw(bs)) {
3190         return -EINVAL;
3191     }
3192 
3193     if (backing_file && strlen(backing_file) > 1023) {
3194         return -EINVAL;
3195     }
3196 
3197     pstrcpy(bs->auto_backing_file, sizeof(bs->auto_backing_file),
3198             backing_file ?: "");
3199     pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
3200     pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
3201 
3202     g_free(s->image_backing_file);
3203     g_free(s->image_backing_format);
3204 
3205     s->image_backing_file = backing_file ? g_strdup(bs->backing_file) : NULL;
3206     s->image_backing_format = backing_fmt ? g_strdup(bs->backing_format) : NULL;
3207 
3208     return qcow2_update_header(bs);
3209 }
3210 
3211 static int coroutine_fn GRAPH_RDLOCK
qcow2_set_up_encryption(BlockDriverState * bs,QCryptoBlockCreateOptions * cryptoopts,Error ** errp)3212 qcow2_set_up_encryption(BlockDriverState *bs,
3213                         QCryptoBlockCreateOptions *cryptoopts,
3214                         Error **errp)
3215 {
3216     BDRVQcow2State *s = bs->opaque;
3217     QCryptoBlock *crypto = NULL;
3218     int fmt, ret;
3219 
3220     switch (cryptoopts->format) {
3221     case QCRYPTO_BLOCK_FORMAT_LUKS:
3222         fmt = QCOW_CRYPT_LUKS;
3223         break;
3224     case QCRYPTO_BLOCK_FORMAT_QCOW:
3225         fmt = QCOW_CRYPT_AES;
3226         break;
3227     default:
3228         error_setg(errp, "Crypto format not supported in qcow2");
3229         return -EINVAL;
3230     }
3231 
3232     s->crypt_method_header = fmt;
3233 
3234     crypto = qcrypto_block_create(cryptoopts, "encrypt.",
3235                                   qcow2_crypto_hdr_init_func,
3236                                   qcow2_crypto_hdr_write_func,
3237                                   bs, 0, errp);
3238     if (!crypto) {
3239         return -EINVAL;
3240     }
3241 
3242     ret = qcow2_update_header(bs);
3243     if (ret < 0) {
3244         error_setg_errno(errp, -ret, "Could not write encryption header");
3245         goto out;
3246     }
3247 
3248     ret = 0;
3249  out:
3250     qcrypto_block_free(crypto);
3251     return ret;
3252 }
3253 
3254 /**
3255  * Preallocates metadata structures for data clusters between @offset (in the
3256  * guest disk) and @new_length (which is thus generally the new guest disk
3257  * size).
3258  *
3259  * Returns: 0 on success, -errno on failure.
3260  */
3261 static int coroutine_fn GRAPH_RDLOCK
preallocate_co(BlockDriverState * bs,uint64_t offset,uint64_t new_length,PreallocMode mode,Error ** errp)3262 preallocate_co(BlockDriverState *bs, uint64_t offset, uint64_t new_length,
3263                PreallocMode mode, Error **errp)
3264 {
3265     BDRVQcow2State *s = bs->opaque;
3266     uint64_t bytes;
3267     uint64_t host_offset = 0;
3268     int64_t file_length;
3269     unsigned int cur_bytes;
3270     int ret;
3271     QCowL2Meta *meta = NULL, *m;
3272 
3273     assert(offset <= new_length);
3274     bytes = new_length - offset;
3275 
3276     while (bytes) {
3277         cur_bytes = MIN(bytes, QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size));
3278         ret = qcow2_alloc_host_offset(bs, offset, &cur_bytes,
3279                                       &host_offset, &meta);
3280         if (ret < 0) {
3281             error_setg_errno(errp, -ret, "Allocating clusters failed");
3282             goto out;
3283         }
3284 
3285         for (m = meta; m != NULL; m = m->next) {
3286             m->prealloc = true;
3287         }
3288 
3289         ret = qcow2_handle_l2meta(bs, &meta, true);
3290         if (ret < 0) {
3291             error_setg_errno(errp, -ret, "Mapping clusters failed");
3292             goto out;
3293         }
3294 
3295         /* TODO Preallocate data if requested */
3296 
3297         bytes -= cur_bytes;
3298         offset += cur_bytes;
3299     }
3300 
3301     /*
3302      * It is expected that the image file is large enough to actually contain
3303      * all of the allocated clusters (otherwise we get failing reads after
3304      * EOF). Extend the image to the last allocated sector.
3305      */
3306     file_length = bdrv_co_getlength(s->data_file->bs);
3307     if (file_length < 0) {
3308         error_setg_errno(errp, -file_length, "Could not get file size");
3309         ret = file_length;
3310         goto out;
3311     }
3312 
3313     if (host_offset + cur_bytes > file_length) {
3314         if (mode == PREALLOC_MODE_METADATA) {
3315             mode = PREALLOC_MODE_OFF;
3316         }
3317         ret = bdrv_co_truncate(s->data_file, host_offset + cur_bytes, false,
3318                                mode, 0, errp);
3319         if (ret < 0) {
3320             goto out;
3321         }
3322     }
3323 
3324     ret = 0;
3325 
3326 out:
3327     qcow2_handle_l2meta(bs, &meta, false);
3328     return ret;
3329 }
3330 
3331 /* qcow2_refcount_metadata_size:
3332  * @clusters: number of clusters to refcount (including data and L1/L2 tables)
3333  * @cluster_size: size of a cluster, in bytes
3334  * @refcount_order: refcount bits power-of-2 exponent
3335  * @generous_increase: allow for the refcount table to be 1.5x as large as it
3336  *                     needs to be
3337  *
3338  * Returns: Number of bytes required for refcount blocks and table metadata.
3339  */
qcow2_refcount_metadata_size(int64_t clusters,size_t cluster_size,int refcount_order,bool generous_increase,uint64_t * refblock_count)3340 int64_t qcow2_refcount_metadata_size(int64_t clusters, size_t cluster_size,
3341                                      int refcount_order, bool generous_increase,
3342                                      uint64_t *refblock_count)
3343 {
3344     /*
3345      * Every host cluster is reference-counted, including metadata (even
3346      * refcount metadata is recursively included).
3347      *
3348      * An accurate formula for the size of refcount metadata size is difficult
3349      * to derive.  An easier method of calculation is finding the fixed point
3350      * where no further refcount blocks or table clusters are required to
3351      * reference count every cluster.
3352      */
3353     int64_t blocks_per_table_cluster = cluster_size / REFTABLE_ENTRY_SIZE;
3354     int64_t refcounts_per_block = cluster_size * 8 / (1 << refcount_order);
3355     int64_t table = 0;  /* number of refcount table clusters */
3356     int64_t blocks = 0; /* number of refcount block clusters */
3357     int64_t last;
3358     int64_t n = 0;
3359 
3360     do {
3361         last = n;
3362         blocks = DIV_ROUND_UP(clusters + table + blocks, refcounts_per_block);
3363         table = DIV_ROUND_UP(blocks, blocks_per_table_cluster);
3364         n = clusters + blocks + table;
3365 
3366         if (n == last && generous_increase) {
3367             clusters += DIV_ROUND_UP(table, 2);
3368             n = 0; /* force another loop */
3369             generous_increase = false;
3370         }
3371     } while (n != last);
3372 
3373     if (refblock_count) {
3374         *refblock_count = blocks;
3375     }
3376 
3377     return (blocks + table) * cluster_size;
3378 }
3379 
3380 /**
3381  * qcow2_calc_prealloc_size:
3382  * @total_size: virtual disk size in bytes
3383  * @cluster_size: cluster size in bytes
3384  * @refcount_order: refcount bits power-of-2 exponent
3385  * @extended_l2: true if the image has extended L2 entries
3386  *
3387  * Returns: Total number of bytes required for the fully allocated image
3388  * (including metadata).
3389  */
qcow2_calc_prealloc_size(int64_t total_size,size_t cluster_size,int refcount_order,bool extended_l2)3390 static int64_t qcow2_calc_prealloc_size(int64_t total_size,
3391                                         size_t cluster_size,
3392                                         int refcount_order,
3393                                         bool extended_l2)
3394 {
3395     int64_t meta_size = 0;
3396     uint64_t nl1e, nl2e;
3397     int64_t aligned_total_size = ROUND_UP(total_size, cluster_size);
3398     size_t l2e_size = extended_l2 ? L2E_SIZE_EXTENDED : L2E_SIZE_NORMAL;
3399 
3400     /* header: 1 cluster */
3401     meta_size += cluster_size;
3402 
3403     /* total size of L2 tables */
3404     nl2e = aligned_total_size / cluster_size;
3405     nl2e = ROUND_UP(nl2e, cluster_size / l2e_size);
3406     meta_size += nl2e * l2e_size;
3407 
3408     /* total size of L1 tables */
3409     nl1e = nl2e * l2e_size / cluster_size;
3410     nl1e = ROUND_UP(nl1e, cluster_size / L1E_SIZE);
3411     meta_size += nl1e * L1E_SIZE;
3412 
3413     /* total size of refcount table and blocks */
3414     meta_size += qcow2_refcount_metadata_size(
3415             (meta_size + aligned_total_size) / cluster_size,
3416             cluster_size, refcount_order, false, NULL);
3417 
3418     return meta_size + aligned_total_size;
3419 }
3420 
validate_cluster_size(size_t cluster_size,bool extended_l2,Error ** errp)3421 static bool validate_cluster_size(size_t cluster_size, bool extended_l2,
3422                                   Error **errp)
3423 {
3424     int cluster_bits = ctz32(cluster_size);
3425     if (cluster_bits < MIN_CLUSTER_BITS || cluster_bits > MAX_CLUSTER_BITS ||
3426         (1 << cluster_bits) != cluster_size)
3427     {
3428         error_setg(errp, "Cluster size must be a power of two between %d and "
3429                    "%dk", 1 << MIN_CLUSTER_BITS, 1 << (MAX_CLUSTER_BITS - 10));
3430         return false;
3431     }
3432 
3433     if (extended_l2) {
3434         unsigned min_cluster_size =
3435             (1 << MIN_CLUSTER_BITS) * QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER;
3436         if (cluster_size < min_cluster_size) {
3437             error_setg(errp, "Extended L2 entries are only supported with "
3438                        "cluster sizes of at least %u bytes", min_cluster_size);
3439             return false;
3440         }
3441     }
3442 
3443     return true;
3444 }
3445 
qcow2_opt_get_cluster_size_del(QemuOpts * opts,bool extended_l2,Error ** errp)3446 static size_t qcow2_opt_get_cluster_size_del(QemuOpts *opts, bool extended_l2,
3447                                              Error **errp)
3448 {
3449     size_t cluster_size;
3450 
3451     cluster_size = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE,
3452                                          DEFAULT_CLUSTER_SIZE);
3453     if (!validate_cluster_size(cluster_size, extended_l2, errp)) {
3454         return 0;
3455     }
3456     return cluster_size;
3457 }
3458 
qcow2_opt_get_version_del(QemuOpts * opts,Error ** errp)3459 static int qcow2_opt_get_version_del(QemuOpts *opts, Error **errp)
3460 {
3461     char *buf;
3462     int ret;
3463 
3464     buf = qemu_opt_get_del(opts, BLOCK_OPT_COMPAT_LEVEL);
3465     if (!buf) {
3466         ret = 3; /* default */
3467     } else if (!strcmp(buf, "0.10")) {
3468         ret = 2;
3469     } else if (!strcmp(buf, "1.1")) {
3470         ret = 3;
3471     } else {
3472         error_setg(errp, "Invalid compatibility level: '%s'", buf);
3473         ret = -EINVAL;
3474     }
3475     g_free(buf);
3476     return ret;
3477 }
3478 
qcow2_opt_get_refcount_bits_del(QemuOpts * opts,int version,Error ** errp)3479 static uint64_t qcow2_opt_get_refcount_bits_del(QemuOpts *opts, int version,
3480                                                 Error **errp)
3481 {
3482     uint64_t refcount_bits;
3483 
3484     refcount_bits = qemu_opt_get_number_del(opts, BLOCK_OPT_REFCOUNT_BITS, 16);
3485     if (refcount_bits > 64 || !is_power_of_2(refcount_bits)) {
3486         error_setg(errp, "Refcount width must be a power of two and may not "
3487                    "exceed 64 bits");
3488         return 0;
3489     }
3490 
3491     if (version < 3 && refcount_bits != 16) {
3492         error_setg(errp, "Different refcount widths than 16 bits require "
3493                    "compatibility level 1.1 or above (use compat=1.1 or "
3494                    "greater)");
3495         return 0;
3496     }
3497 
3498     return refcount_bits;
3499 }
3500 
3501 static int coroutine_fn GRAPH_UNLOCKED
qcow2_co_create(BlockdevCreateOptions * create_options,Error ** errp)3502 qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
3503 {
3504     ERRP_GUARD();
3505     BlockdevCreateOptionsQcow2 *qcow2_opts;
3506     QDict *options;
3507 
3508     /*
3509      * Open the image file and write a minimal qcow2 header.
3510      *
3511      * We keep things simple and start with a zero-sized image. We also
3512      * do without refcount blocks or a L1 table for now. We'll fix the
3513      * inconsistency later.
3514      *
3515      * We do need a refcount table because growing the refcount table means
3516      * allocating two new refcount blocks - the second of which would be at
3517      * 2 GB for 64k clusters, and we don't want to have a 2 GB initial file
3518      * size for any qcow2 image.
3519      */
3520     BlockBackend *blk = NULL;
3521     BlockDriverState *bs = NULL;
3522     BlockDriverState *data_bs = NULL;
3523     QCowHeader *header;
3524     size_t cluster_size;
3525     int version;
3526     int refcount_order;
3527     uint64_t *refcount_table;
3528     int ret;
3529     uint8_t compression_type = QCOW2_COMPRESSION_TYPE_ZLIB;
3530 
3531     assert(create_options->driver == BLOCKDEV_DRIVER_QCOW2);
3532     qcow2_opts = &create_options->u.qcow2;
3533 
3534     bs = bdrv_co_open_blockdev_ref(qcow2_opts->file, errp);
3535     if (bs == NULL) {
3536         return -EIO;
3537     }
3538 
3539     /* Validate options and set default values */
3540     if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) {
3541         error_setg(errp, "Image size must be a multiple of %u bytes",
3542                    (unsigned) BDRV_SECTOR_SIZE);
3543         ret = -EINVAL;
3544         goto out;
3545     }
3546 
3547     if (qcow2_opts->has_version) {
3548         switch (qcow2_opts->version) {
3549         case BLOCKDEV_QCOW2_VERSION_V2:
3550             version = 2;
3551             break;
3552         case BLOCKDEV_QCOW2_VERSION_V3:
3553             version = 3;
3554             break;
3555         default:
3556             g_assert_not_reached();
3557         }
3558     } else {
3559         version = 3;
3560     }
3561 
3562     if (qcow2_opts->has_cluster_size) {
3563         cluster_size = qcow2_opts->cluster_size;
3564     } else {
3565         cluster_size = DEFAULT_CLUSTER_SIZE;
3566     }
3567 
3568     if (!qcow2_opts->has_extended_l2) {
3569         qcow2_opts->extended_l2 = false;
3570     }
3571     if (qcow2_opts->extended_l2) {
3572         if (version < 3) {
3573             error_setg(errp, "Extended L2 entries are only supported with "
3574                        "compatibility level 1.1 and above (use version=v3 or "
3575                        "greater)");
3576             ret = -EINVAL;
3577             goto out;
3578         }
3579     }
3580 
3581     if (!validate_cluster_size(cluster_size, qcow2_opts->extended_l2, errp)) {
3582         ret = -EINVAL;
3583         goto out;
3584     }
3585 
3586     if (!qcow2_opts->has_preallocation) {
3587         qcow2_opts->preallocation = PREALLOC_MODE_OFF;
3588     }
3589     if (qcow2_opts->backing_file &&
3590         qcow2_opts->preallocation != PREALLOC_MODE_OFF &&
3591         !qcow2_opts->extended_l2)
3592     {
3593         error_setg(errp, "Backing file and preallocation can only be used at "
3594                    "the same time if extended_l2 is on");
3595         ret = -EINVAL;
3596         goto out;
3597     }
3598     if (qcow2_opts->has_backing_fmt && !qcow2_opts->backing_file) {
3599         error_setg(errp, "Backing format cannot be used without backing file");
3600         ret = -EINVAL;
3601         goto out;
3602     }
3603 
3604     if (!qcow2_opts->has_lazy_refcounts) {
3605         qcow2_opts->lazy_refcounts = false;
3606     }
3607     if (version < 3 && qcow2_opts->lazy_refcounts) {
3608         error_setg(errp, "Lazy refcounts only supported with compatibility "
3609                    "level 1.1 and above (use version=v3 or greater)");
3610         ret = -EINVAL;
3611         goto out;
3612     }
3613 
3614     if (!qcow2_opts->has_refcount_bits) {
3615         qcow2_opts->refcount_bits = 16;
3616     }
3617     if (qcow2_opts->refcount_bits > 64 ||
3618         !is_power_of_2(qcow2_opts->refcount_bits))
3619     {
3620         error_setg(errp, "Refcount width must be a power of two and may not "
3621                    "exceed 64 bits");
3622         ret = -EINVAL;
3623         goto out;
3624     }
3625     if (version < 3 && qcow2_opts->refcount_bits != 16) {
3626         error_setg(errp, "Different refcount widths than 16 bits require "
3627                    "compatibility level 1.1 or above (use version=v3 or "
3628                    "greater)");
3629         ret = -EINVAL;
3630         goto out;
3631     }
3632     refcount_order = ctz32(qcow2_opts->refcount_bits);
3633 
3634     if (qcow2_opts->data_file_raw && !qcow2_opts->data_file) {
3635         error_setg(errp, "data-file-raw requires data-file");
3636         ret = -EINVAL;
3637         goto out;
3638     }
3639     if (qcow2_opts->data_file_raw && qcow2_opts->backing_file) {
3640         error_setg(errp, "Backing file and data-file-raw cannot be used at "
3641                    "the same time");
3642         ret = -EINVAL;
3643         goto out;
3644     }
3645     if (qcow2_opts->data_file_raw &&
3646         qcow2_opts->preallocation == PREALLOC_MODE_OFF)
3647     {
3648         /*
3649          * data-file-raw means that "the external data file can be
3650          * read as a consistent standalone raw image without looking
3651          * at the qcow2 metadata."  It does not say that the metadata
3652          * must be ignored, though (and the qcow2 driver in fact does
3653          * not ignore it), so the L1/L2 tables must be present and
3654          * give a 1:1 mapping, so you get the same result regardless
3655          * of whether you look at the metadata or whether you ignore
3656          * it.
3657          */
3658         qcow2_opts->preallocation = PREALLOC_MODE_METADATA;
3659 
3660         /*
3661          * Cannot use preallocation with backing files, but giving a
3662          * backing file when specifying data_file_raw is an error
3663          * anyway.
3664          */
3665         assert(!qcow2_opts->backing_file);
3666     }
3667 
3668     if (qcow2_opts->data_file) {
3669         if (version < 3) {
3670             error_setg(errp, "External data files are only supported with "
3671                        "compatibility level 1.1 and above (use version=v3 or "
3672                        "greater)");
3673             ret = -EINVAL;
3674             goto out;
3675         }
3676         data_bs = bdrv_co_open_blockdev_ref(qcow2_opts->data_file, errp);
3677         if (data_bs == NULL) {
3678             ret = -EIO;
3679             goto out;
3680         }
3681     }
3682 
3683     if (qcow2_opts->has_compression_type &&
3684         qcow2_opts->compression_type != QCOW2_COMPRESSION_TYPE_ZLIB) {
3685 
3686         ret = -EINVAL;
3687 
3688         if (version < 3) {
3689             error_setg(errp, "Non-zlib compression type is only supported with "
3690                        "compatibility level 1.1 and above (use version=v3 or "
3691                        "greater)");
3692             goto out;
3693         }
3694 
3695         switch (qcow2_opts->compression_type) {
3696 #ifdef CONFIG_ZSTD
3697         case QCOW2_COMPRESSION_TYPE_ZSTD:
3698             break;
3699 #endif
3700         default:
3701             error_setg(errp, "Unknown compression type");
3702             goto out;
3703         }
3704 
3705         compression_type = qcow2_opts->compression_type;
3706     }
3707 
3708     /* Create BlockBackend to write to the image */
3709     blk = blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
3710                              errp);
3711     if (!blk) {
3712         ret = -EPERM;
3713         goto out;
3714     }
3715     blk_set_allow_write_beyond_eof(blk, true);
3716 
3717     /* Write the header */
3718     QEMU_BUILD_BUG_ON((1 << MIN_CLUSTER_BITS) < sizeof(*header));
3719     header = g_malloc0(cluster_size);
3720     *header = (QCowHeader) {
3721         .magic                      = cpu_to_be32(QCOW_MAGIC),
3722         .version                    = cpu_to_be32(version),
3723         .cluster_bits               = cpu_to_be32(ctz32(cluster_size)),
3724         .size                       = cpu_to_be64(0),
3725         .l1_table_offset            = cpu_to_be64(0),
3726         .l1_size                    = cpu_to_be32(0),
3727         .refcount_table_offset      = cpu_to_be64(cluster_size),
3728         .refcount_table_clusters    = cpu_to_be32(1),
3729         .refcount_order             = cpu_to_be32(refcount_order),
3730         /* don't deal with endianness since compression_type is 1 byte long */
3731         .compression_type           = compression_type,
3732         .header_length              = cpu_to_be32(sizeof(*header)),
3733     };
3734 
3735     /* We'll update this to correct value later */
3736     header->crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
3737 
3738     if (qcow2_opts->lazy_refcounts) {
3739         header->compatible_features |=
3740             cpu_to_be64(QCOW2_COMPAT_LAZY_REFCOUNTS);
3741     }
3742     if (data_bs) {
3743         header->incompatible_features |=
3744             cpu_to_be64(QCOW2_INCOMPAT_DATA_FILE);
3745     }
3746     if (qcow2_opts->data_file_raw) {
3747         header->autoclear_features |=
3748             cpu_to_be64(QCOW2_AUTOCLEAR_DATA_FILE_RAW);
3749     }
3750     if (compression_type != QCOW2_COMPRESSION_TYPE_ZLIB) {
3751         header->incompatible_features |=
3752             cpu_to_be64(QCOW2_INCOMPAT_COMPRESSION);
3753     }
3754 
3755     if (qcow2_opts->extended_l2) {
3756         header->incompatible_features |=
3757             cpu_to_be64(QCOW2_INCOMPAT_EXTL2);
3758     }
3759 
3760     ret = blk_co_pwrite(blk, 0, cluster_size, header, 0);
3761     g_free(header);
3762     if (ret < 0) {
3763         error_setg_errno(errp, -ret, "Could not write qcow2 header");
3764         goto out;
3765     }
3766 
3767     /* Write a refcount table with one refcount block */
3768     refcount_table = g_malloc0(2 * cluster_size);
3769     refcount_table[0] = cpu_to_be64(2 * cluster_size);
3770     ret = blk_co_pwrite(blk, cluster_size, 2 * cluster_size, refcount_table, 0);
3771     g_free(refcount_table);
3772 
3773     if (ret < 0) {
3774         error_setg_errno(errp, -ret, "Could not write refcount table");
3775         goto out;
3776     }
3777 
3778     blk_co_unref(blk);
3779     blk = NULL;
3780 
3781     /*
3782      * And now open the image and make it consistent first (i.e. increase the
3783      * refcount of the cluster that is occupied by the header and the refcount
3784      * table)
3785      */
3786     options = qdict_new();
3787     qdict_put_str(options, "driver", "qcow2");
3788     qdict_put_str(options, "file", bs->node_name);
3789     if (data_bs) {
3790         qdict_put_str(options, "data-file", data_bs->node_name);
3791     }
3792     blk = blk_co_new_open(NULL, NULL, options,
3793                           BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_NO_FLUSH,
3794                           errp);
3795     if (blk == NULL) {
3796         ret = -EIO;
3797         goto out;
3798     }
3799 
3800     bdrv_graph_co_rdlock();
3801     ret = qcow2_alloc_clusters(blk_bs(blk), 3 * cluster_size);
3802     if (ret < 0) {
3803         bdrv_graph_co_rdunlock();
3804         error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
3805                          "header and refcount table");
3806         goto out;
3807 
3808     } else if (ret != 0) {
3809         error_report("Huh, first cluster in empty image is already in use?");
3810         abort();
3811     }
3812 
3813     /* Set the external data file if necessary */
3814     if (data_bs) {
3815         BDRVQcow2State *s = blk_bs(blk)->opaque;
3816         s->image_data_file = g_strdup(data_bs->filename);
3817     }
3818 
3819     /* Create a full header (including things like feature table) */
3820     ret = qcow2_update_header(blk_bs(blk));
3821     bdrv_graph_co_rdunlock();
3822 
3823     if (ret < 0) {
3824         error_setg_errno(errp, -ret, "Could not update qcow2 header");
3825         goto out;
3826     }
3827 
3828     /* Okay, now that we have a valid image, let's give it the right size */
3829     ret = blk_co_truncate(blk, qcow2_opts->size, false,
3830                           qcow2_opts->preallocation, 0, errp);
3831     if (ret < 0) {
3832         error_prepend(errp, "Could not resize image: ");
3833         goto out;
3834     }
3835 
3836     /* Want a backing file? There you go. */
3837     if (qcow2_opts->backing_file) {
3838         const char *backing_format = NULL;
3839 
3840         if (qcow2_opts->has_backing_fmt) {
3841             backing_format = BlockdevDriver_str(qcow2_opts->backing_fmt);
3842         }
3843 
3844         bdrv_graph_co_rdlock();
3845         ret = bdrv_co_change_backing_file(blk_bs(blk), qcow2_opts->backing_file,
3846                                           backing_format, false);
3847         bdrv_graph_co_rdunlock();
3848 
3849         if (ret < 0) {
3850             error_setg_errno(errp, -ret, "Could not assign backing file '%s' "
3851                              "with format '%s'", qcow2_opts->backing_file,
3852                              backing_format);
3853             goto out;
3854         }
3855     }
3856 
3857     /* Want encryption? There you go. */
3858     if (qcow2_opts->encrypt) {
3859         bdrv_graph_co_rdlock();
3860         ret = qcow2_set_up_encryption(blk_bs(blk), qcow2_opts->encrypt, errp);
3861         bdrv_graph_co_rdunlock();
3862 
3863         if (ret < 0) {
3864             goto out;
3865         }
3866     }
3867 
3868     blk_co_unref(blk);
3869     blk = NULL;
3870 
3871     /* Reopen the image without BDRV_O_NO_FLUSH to flush it before returning.
3872      * Using BDRV_O_NO_IO, since encryption is now setup we don't want to
3873      * have to setup decryption context. We're not doing any I/O on the top
3874      * level BlockDriverState, only lower layers, where BDRV_O_NO_IO does
3875      * not have effect.
3876      */
3877     options = qdict_new();
3878     qdict_put_str(options, "driver", "qcow2");
3879     qdict_put_str(options, "file", bs->node_name);
3880     if (data_bs) {
3881         qdict_put_str(options, "data-file", data_bs->node_name);
3882     }
3883     blk = blk_co_new_open(NULL, NULL, options,
3884                           BDRV_O_RDWR | BDRV_O_NO_BACKING | BDRV_O_NO_IO,
3885                           errp);
3886     if (blk == NULL) {
3887         ret = -EIO;
3888         goto out;
3889     }
3890 
3891     ret = 0;
3892 out:
3893     blk_co_unref(blk);
3894     bdrv_co_unref(bs);
3895     bdrv_co_unref(data_bs);
3896     return ret;
3897 }
3898 
3899 static int coroutine_fn GRAPH_UNLOCKED
qcow2_co_create_opts(BlockDriver * drv,const char * filename,QemuOpts * opts,Error ** errp)3900 qcow2_co_create_opts(BlockDriver *drv, const char *filename, QemuOpts *opts,
3901                      Error **errp)
3902 {
3903     BlockdevCreateOptions *create_options = NULL;
3904     QDict *qdict;
3905     Visitor *v;
3906     BlockDriverState *bs = NULL;
3907     BlockDriverState *data_bs = NULL;
3908     const char *val;
3909     int ret;
3910 
3911     /* Only the keyval visitor supports the dotted syntax needed for
3912      * encryption, so go through a QDict before getting a QAPI type. Ignore
3913      * options meant for the protocol layer so that the visitor doesn't
3914      * complain. */
3915     qdict = qemu_opts_to_qdict_filtered(opts, NULL, bdrv_qcow2.create_opts,
3916                                         true);
3917 
3918     /* Handle encryption options */
3919     val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT);
3920     if (val && !strcmp(val, "on")) {
3921         qdict_put_str(qdict, BLOCK_OPT_ENCRYPT, "qcow");
3922     } else if (val && !strcmp(val, "off")) {
3923         qdict_del(qdict, BLOCK_OPT_ENCRYPT);
3924     }
3925 
3926     val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT);
3927     if (val && !strcmp(val, "aes")) {
3928         qdict_put_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT, "qcow");
3929     }
3930 
3931     /* Convert compat=0.10/1.1 into compat=v2/v3, to be renamed into
3932      * version=v2/v3 below. */
3933     val = qdict_get_try_str(qdict, BLOCK_OPT_COMPAT_LEVEL);
3934     if (val && !strcmp(val, "0.10")) {
3935         qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v2");
3936     } else if (val && !strcmp(val, "1.1")) {
3937         qdict_put_str(qdict, BLOCK_OPT_COMPAT_LEVEL, "v3");
3938     }
3939 
3940     /* Change legacy command line options into QMP ones */
3941     static const QDictRenames opt_renames[] = {
3942         { BLOCK_OPT_BACKING_FILE,       "backing-file" },
3943         { BLOCK_OPT_BACKING_FMT,        "backing-fmt" },
3944         { BLOCK_OPT_CLUSTER_SIZE,       "cluster-size" },
3945         { BLOCK_OPT_LAZY_REFCOUNTS,     "lazy-refcounts" },
3946         { BLOCK_OPT_EXTL2,              "extended-l2" },
3947         { BLOCK_OPT_REFCOUNT_BITS,      "refcount-bits" },
3948         { BLOCK_OPT_ENCRYPT,            BLOCK_OPT_ENCRYPT_FORMAT },
3949         { BLOCK_OPT_COMPAT_LEVEL,       "version" },
3950         { BLOCK_OPT_DATA_FILE_RAW,      "data-file-raw" },
3951         { BLOCK_OPT_COMPRESSION_TYPE,   "compression-type" },
3952         { NULL, NULL },
3953     };
3954 
3955     if (!qdict_rename_keys(qdict, opt_renames, errp)) {
3956         ret = -EINVAL;
3957         goto finish;
3958     }
3959 
3960     /* Create and open the file (protocol layer) */
3961     ret = bdrv_co_create_file(filename, opts, errp);
3962     if (ret < 0) {
3963         goto finish;
3964     }
3965 
3966     bs = bdrv_co_open(filename, NULL, NULL,
3967                       BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
3968     if (bs == NULL) {
3969         ret = -EIO;
3970         goto finish;
3971     }
3972 
3973     /* Create and open an external data file (protocol layer) */
3974     val = qdict_get_try_str(qdict, BLOCK_OPT_DATA_FILE);
3975     if (val) {
3976         ret = bdrv_co_create_file(val, opts, errp);
3977         if (ret < 0) {
3978             goto finish;
3979         }
3980 
3981         data_bs = bdrv_co_open(val, NULL, NULL,
3982                                BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL,
3983                                errp);
3984         if (data_bs == NULL) {
3985             ret = -EIO;
3986             goto finish;
3987         }
3988 
3989         qdict_del(qdict, BLOCK_OPT_DATA_FILE);
3990         qdict_put_str(qdict, "data-file", data_bs->node_name);
3991     }
3992 
3993     /* Set 'driver' and 'node' options */
3994     qdict_put_str(qdict, "driver", "qcow2");
3995     qdict_put_str(qdict, "file", bs->node_name);
3996 
3997     /* Now get the QAPI type BlockdevCreateOptions */
3998     v = qobject_input_visitor_new_flat_confused(qdict, errp);
3999     if (!v) {
4000         ret = -EINVAL;
4001         goto finish;
4002     }
4003 
4004     visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
4005     visit_free(v);
4006     if (!create_options) {
4007         ret = -EINVAL;
4008         goto finish;
4009     }
4010 
4011     /* Silently round up size */
4012     create_options->u.qcow2.size = ROUND_UP(create_options->u.qcow2.size,
4013                                             BDRV_SECTOR_SIZE);
4014 
4015     /* Create the qcow2 image (format layer) */
4016     ret = qcow2_co_create(create_options, errp);
4017 finish:
4018     if (ret < 0) {
4019         bdrv_graph_co_rdlock();
4020         bdrv_co_delete_file_noerr(bs);
4021         bdrv_co_delete_file_noerr(data_bs);
4022         bdrv_graph_co_rdunlock();
4023     } else {
4024         ret = 0;
4025     }
4026 
4027     qobject_unref(qdict);
4028     bdrv_co_unref(bs);
4029     bdrv_co_unref(data_bs);
4030     qapi_free_BlockdevCreateOptions(create_options);
4031     return ret;
4032 }
4033 
4034 
4035 static bool coroutine_fn GRAPH_RDLOCK
is_zero(BlockDriverState * bs,int64_t offset,int64_t bytes)4036 is_zero(BlockDriverState *bs, int64_t offset, int64_t bytes)
4037 {
4038     int64_t nr;
4039     int res;
4040 
4041     /* Clamp to image length, before checking status of underlying sectors */
4042     if (offset + bytes > bs->total_sectors * BDRV_SECTOR_SIZE) {
4043         bytes = bs->total_sectors * BDRV_SECTOR_SIZE - offset;
4044     }
4045 
4046     if (!bytes) {
4047         return true;
4048     }
4049 
4050     /*
4051      * bdrv_block_status_above doesn't merge different types of zeros, for
4052      * example, zeros which come from the region which is unallocated in
4053      * the whole backing chain, and zeros which come because of a short
4054      * backing file. So, we need a loop.
4055      */
4056     do {
4057         res = bdrv_co_block_status_above(bs, NULL, offset, bytes, &nr, NULL, NULL);
4058         offset += nr;
4059         bytes -= nr;
4060     } while (res >= 0 && (res & BDRV_BLOCK_ZERO) && nr && bytes);
4061 
4062     return res >= 0 && (res & BDRV_BLOCK_ZERO) && bytes == 0;
4063 }
4064 
4065 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_pwrite_zeroes(BlockDriverState * bs,int64_t offset,int64_t bytes,BdrvRequestFlags flags)4066 qcow2_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
4067                        BdrvRequestFlags flags)
4068 {
4069     int ret;
4070     BDRVQcow2State *s = bs->opaque;
4071 
4072     uint32_t head = offset_into_subcluster(s, offset);
4073     uint32_t tail = ROUND_UP(offset + bytes, s->subcluster_size) -
4074         (offset + bytes);
4075 
4076     trace_qcow2_pwrite_zeroes_start_req(qemu_coroutine_self(), offset, bytes);
4077     if (offset + bytes == bs->total_sectors * BDRV_SECTOR_SIZE) {
4078         tail = 0;
4079     }
4080 
4081     if (head || tail) {
4082         uint64_t off;
4083         unsigned int nr;
4084         QCow2SubclusterType type;
4085 
4086         assert(head + bytes + tail <= s->subcluster_size);
4087 
4088         /* check whether remainder of cluster already reads as zero */
4089         if (!(is_zero(bs, offset - head, head) &&
4090               is_zero(bs, offset + bytes, tail))) {
4091             return -ENOTSUP;
4092         }
4093 
4094         qemu_co_mutex_lock(&s->lock);
4095         /* We can have new write after previous check */
4096         offset -= head;
4097         bytes = s->subcluster_size;
4098         nr = s->subcluster_size;
4099         ret = qcow2_get_host_offset(bs, offset, &nr, &off, &type);
4100         if (ret < 0 ||
4101             (type != QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN &&
4102              type != QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC &&
4103              type != QCOW2_SUBCLUSTER_ZERO_PLAIN &&
4104              type != QCOW2_SUBCLUSTER_ZERO_ALLOC)) {
4105             qemu_co_mutex_unlock(&s->lock);
4106             return ret < 0 ? ret : -ENOTSUP;
4107         }
4108     } else {
4109         qemu_co_mutex_lock(&s->lock);
4110     }
4111 
4112     trace_qcow2_pwrite_zeroes(qemu_coroutine_self(), offset, bytes);
4113 
4114     /* Whatever is left can use real zero subclusters */
4115     ret = qcow2_subcluster_zeroize(bs, offset, bytes, flags);
4116     qemu_co_mutex_unlock(&s->lock);
4117 
4118     return ret;
4119 }
4120 
4121 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_pdiscard(BlockDriverState * bs,int64_t offset,int64_t bytes)4122 qcow2_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
4123 {
4124     int ret;
4125     BDRVQcow2State *s = bs->opaque;
4126 
4127     /* If the image does not support QCOW_OFLAG_ZERO then discarding
4128      * clusters could expose stale data from the backing file. */
4129     if (s->qcow_version < 3 && bs->backing) {
4130         return -ENOTSUP;
4131     }
4132 
4133     if (!QEMU_IS_ALIGNED(offset | bytes, s->cluster_size)) {
4134         assert(bytes < s->cluster_size);
4135         /* Ignore partial clusters, except for the special case of the
4136          * complete partial cluster at the end of an unaligned file */
4137         if (!QEMU_IS_ALIGNED(offset, s->cluster_size) ||
4138             offset + bytes != bs->total_sectors * BDRV_SECTOR_SIZE) {
4139             return -ENOTSUP;
4140         }
4141     }
4142 
4143     qemu_co_mutex_lock(&s->lock);
4144     ret = qcow2_cluster_discard(bs, offset, bytes, QCOW2_DISCARD_REQUEST,
4145                                 false);
4146     qemu_co_mutex_unlock(&s->lock);
4147     return ret;
4148 }
4149 
4150 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_copy_range_from(BlockDriverState * bs,BdrvChild * src,int64_t src_offset,BdrvChild * dst,int64_t dst_offset,int64_t bytes,BdrvRequestFlags read_flags,BdrvRequestFlags write_flags)4151 qcow2_co_copy_range_from(BlockDriverState *bs,
4152                          BdrvChild *src, int64_t src_offset,
4153                          BdrvChild *dst, int64_t dst_offset,
4154                          int64_t bytes, BdrvRequestFlags read_flags,
4155                          BdrvRequestFlags write_flags)
4156 {
4157     BDRVQcow2State *s = bs->opaque;
4158     int ret;
4159     unsigned int cur_bytes; /* number of bytes in current iteration */
4160     BdrvChild *child = NULL;
4161     BdrvRequestFlags cur_write_flags;
4162 
4163     assert(!bs->encrypted);
4164     qemu_co_mutex_lock(&s->lock);
4165 
4166     while (bytes != 0) {
4167         uint64_t copy_offset = 0;
4168         QCow2SubclusterType type;
4169         /* prepare next request */
4170         cur_bytes = MIN(bytes, INT_MAX);
4171         cur_write_flags = write_flags;
4172 
4173         ret = qcow2_get_host_offset(bs, src_offset, &cur_bytes,
4174                                     &copy_offset, &type);
4175         if (ret < 0) {
4176             goto out;
4177         }
4178 
4179         switch (type) {
4180         case QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN:
4181         case QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC:
4182             if (bs->backing && bs->backing->bs) {
4183                 int64_t backing_length = bdrv_co_getlength(bs->backing->bs);
4184                 if (src_offset >= backing_length) {
4185                     cur_write_flags |= BDRV_REQ_ZERO_WRITE;
4186                 } else {
4187                     child = bs->backing;
4188                     cur_bytes = MIN(cur_bytes, backing_length - src_offset);
4189                     copy_offset = src_offset;
4190                 }
4191             } else {
4192                 cur_write_flags |= BDRV_REQ_ZERO_WRITE;
4193             }
4194             break;
4195 
4196         case QCOW2_SUBCLUSTER_ZERO_PLAIN:
4197         case QCOW2_SUBCLUSTER_ZERO_ALLOC:
4198             cur_write_flags |= BDRV_REQ_ZERO_WRITE;
4199             break;
4200 
4201         case QCOW2_SUBCLUSTER_COMPRESSED:
4202             ret = -ENOTSUP;
4203             goto out;
4204 
4205         case QCOW2_SUBCLUSTER_NORMAL:
4206             child = s->data_file;
4207             break;
4208 
4209         default:
4210             abort();
4211         }
4212         qemu_co_mutex_unlock(&s->lock);
4213         ret = bdrv_co_copy_range_from(child,
4214                                       copy_offset,
4215                                       dst, dst_offset,
4216                                       cur_bytes, read_flags, cur_write_flags);
4217         qemu_co_mutex_lock(&s->lock);
4218         if (ret < 0) {
4219             goto out;
4220         }
4221 
4222         bytes -= cur_bytes;
4223         src_offset += cur_bytes;
4224         dst_offset += cur_bytes;
4225     }
4226     ret = 0;
4227 
4228 out:
4229     qemu_co_mutex_unlock(&s->lock);
4230     return ret;
4231 }
4232 
4233 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_copy_range_to(BlockDriverState * bs,BdrvChild * src,int64_t src_offset,BdrvChild * dst,int64_t dst_offset,int64_t bytes,BdrvRequestFlags read_flags,BdrvRequestFlags write_flags)4234 qcow2_co_copy_range_to(BlockDriverState *bs,
4235                        BdrvChild *src, int64_t src_offset,
4236                        BdrvChild *dst, int64_t dst_offset,
4237                        int64_t bytes, BdrvRequestFlags read_flags,
4238                        BdrvRequestFlags write_flags)
4239 {
4240     BDRVQcow2State *s = bs->opaque;
4241     int ret;
4242     unsigned int cur_bytes; /* number of sectors in current iteration */
4243     uint64_t host_offset;
4244     QCowL2Meta *l2meta = NULL;
4245 
4246     assert(!bs->encrypted);
4247 
4248     qemu_co_mutex_lock(&s->lock);
4249 
4250     while (bytes != 0) {
4251 
4252         l2meta = NULL;
4253 
4254         cur_bytes = MIN(bytes, INT_MAX);
4255 
4256         /* TODO:
4257          * If src->bs == dst->bs, we could simply copy by incrementing
4258          * the refcnt, without copying user data.
4259          * Or if src->bs == dst->bs->backing->bs, we could copy by discarding. */
4260         ret = qcow2_alloc_host_offset(bs, dst_offset, &cur_bytes,
4261                                       &host_offset, &l2meta);
4262         if (ret < 0) {
4263             goto fail;
4264         }
4265 
4266         ret = qcow2_pre_write_overlap_check(bs, 0, host_offset, cur_bytes,
4267                                             true);
4268         if (ret < 0) {
4269             goto fail;
4270         }
4271 
4272         qemu_co_mutex_unlock(&s->lock);
4273         ret = bdrv_co_copy_range_to(src, src_offset, s->data_file, host_offset,
4274                                     cur_bytes, read_flags, write_flags);
4275         qemu_co_mutex_lock(&s->lock);
4276         if (ret < 0) {
4277             goto fail;
4278         }
4279 
4280         ret = qcow2_handle_l2meta(bs, &l2meta, true);
4281         if (ret) {
4282             goto fail;
4283         }
4284 
4285         bytes -= cur_bytes;
4286         src_offset += cur_bytes;
4287         dst_offset += cur_bytes;
4288     }
4289     ret = 0;
4290 
4291 fail:
4292     qcow2_handle_l2meta(bs, &l2meta, false);
4293 
4294     qemu_co_mutex_unlock(&s->lock);
4295 
4296     trace_qcow2_writev_done_req(qemu_coroutine_self(), ret);
4297 
4298     return ret;
4299 }
4300 
4301 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_truncate(BlockDriverState * bs,int64_t offset,bool exact,PreallocMode prealloc,BdrvRequestFlags flags,Error ** errp)4302 qcow2_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
4303                   PreallocMode prealloc, BdrvRequestFlags flags, Error **errp)
4304 {
4305     ERRP_GUARD();
4306     BDRVQcow2State *s = bs->opaque;
4307     uint64_t old_length;
4308     int64_t new_l1_size;
4309     int ret;
4310     QDict *options;
4311 
4312     if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_METADATA &&
4313         prealloc != PREALLOC_MODE_FALLOC && prealloc != PREALLOC_MODE_FULL)
4314     {
4315         error_setg(errp, "Unsupported preallocation mode '%s'",
4316                    PreallocMode_str(prealloc));
4317         return -ENOTSUP;
4318     }
4319 
4320     if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
4321         error_setg(errp, "The new size must be a multiple of %u",
4322                    (unsigned) BDRV_SECTOR_SIZE);
4323         return -EINVAL;
4324     }
4325 
4326     qemu_co_mutex_lock(&s->lock);
4327 
4328     /*
4329      * Even though we store snapshot size for all images, it was not
4330      * required until v3, so it is not safe to proceed for v2.
4331      */
4332     if (s->nb_snapshots && s->qcow_version < 3) {
4333         error_setg(errp, "Can't resize a v2 image which has snapshots");
4334         ret = -ENOTSUP;
4335         goto fail;
4336     }
4337 
4338     /* See qcow2-bitmap.c for which bitmap scenarios prevent a resize. */
4339     if (qcow2_truncate_bitmaps_check(bs, errp)) {
4340         ret = -ENOTSUP;
4341         goto fail;
4342     }
4343 
4344     old_length = bs->total_sectors * BDRV_SECTOR_SIZE;
4345     new_l1_size = size_to_l1(s, offset);
4346 
4347     if (offset < old_length) {
4348         int64_t last_cluster, old_file_size;
4349         if (prealloc != PREALLOC_MODE_OFF) {
4350             error_setg(errp,
4351                        "Preallocation can't be used for shrinking an image");
4352             ret = -EINVAL;
4353             goto fail;
4354         }
4355 
4356         ret = qcow2_cluster_discard(bs, ROUND_UP(offset, s->cluster_size),
4357                                     old_length - ROUND_UP(offset,
4358                                                           s->cluster_size),
4359                                     QCOW2_DISCARD_ALWAYS, true);
4360         if (ret < 0) {
4361             error_setg_errno(errp, -ret, "Failed to discard cropped clusters");
4362             goto fail;
4363         }
4364 
4365         ret = qcow2_shrink_l1_table(bs, new_l1_size);
4366         if (ret < 0) {
4367             error_setg_errno(errp, -ret,
4368                              "Failed to reduce the number of L2 tables");
4369             goto fail;
4370         }
4371 
4372         ret = qcow2_shrink_reftable(bs);
4373         if (ret < 0) {
4374             error_setg_errno(errp, -ret,
4375                              "Failed to discard unused refblocks");
4376             goto fail;
4377         }
4378 
4379         old_file_size = bdrv_co_getlength(bs->file->bs);
4380         if (old_file_size < 0) {
4381             error_setg_errno(errp, -old_file_size,
4382                              "Failed to inquire current file length");
4383             ret = old_file_size;
4384             goto fail;
4385         }
4386         last_cluster = qcow2_get_last_cluster(bs, old_file_size);
4387         if (last_cluster < 0) {
4388             error_setg_errno(errp, -last_cluster,
4389                              "Failed to find the last cluster");
4390             ret = last_cluster;
4391             goto fail;
4392         }
4393         if ((last_cluster + 1) * s->cluster_size < old_file_size) {
4394             Error *local_err = NULL;
4395 
4396             /*
4397              * Do not pass @exact here: It will not help the user if
4398              * we get an error here just because they wanted to shrink
4399              * their qcow2 image (on a block device) with qemu-img.
4400              * (And on the qcow2 layer, the @exact requirement is
4401              * always fulfilled, so there is no need to pass it on.)
4402              */
4403             bdrv_co_truncate(bs->file, (last_cluster + 1) * s->cluster_size,
4404                              false, PREALLOC_MODE_OFF, 0, &local_err);
4405             if (local_err) {
4406                 warn_reportf_err(local_err,
4407                                  "Failed to truncate the tail of the image: ");
4408             }
4409         }
4410     } else {
4411         ret = qcow2_grow_l1_table(bs, new_l1_size, true);
4412         if (ret < 0) {
4413             error_setg_errno(errp, -ret, "Failed to grow the L1 table");
4414             goto fail;
4415         }
4416 
4417         if (data_file_is_raw(bs) && prealloc == PREALLOC_MODE_OFF) {
4418             /*
4419              * When creating a qcow2 image with data-file-raw, we enforce
4420              * at least prealloc=metadata, so that the L1/L2 tables are
4421              * fully allocated and reading from the data file will return
4422              * the same data as reading from the qcow2 image.  When the
4423              * image is grown, we must consequently preallocate the
4424              * metadata structures to cover the added area.
4425              */
4426             prealloc = PREALLOC_MODE_METADATA;
4427         }
4428     }
4429 
4430     switch (prealloc) {
4431     case PREALLOC_MODE_OFF:
4432         if (has_data_file(bs)) {
4433             /*
4434              * If the caller wants an exact resize, the external data
4435              * file should be resized to the exact target size, too,
4436              * so we pass @exact here.
4437              */
4438             ret = bdrv_co_truncate(s->data_file, offset, exact, prealloc, 0,
4439                                    errp);
4440             if (ret < 0) {
4441                 goto fail;
4442             }
4443         }
4444         break;
4445 
4446     case PREALLOC_MODE_METADATA:
4447         ret = preallocate_co(bs, old_length, offset, prealloc, errp);
4448         if (ret < 0) {
4449             goto fail;
4450         }
4451         break;
4452 
4453     case PREALLOC_MODE_FALLOC:
4454     case PREALLOC_MODE_FULL:
4455     {
4456         int64_t allocation_start, host_offset, guest_offset;
4457         int64_t clusters_allocated;
4458         int64_t old_file_size, last_cluster, new_file_size;
4459         uint64_t nb_new_data_clusters, nb_new_l2_tables;
4460         bool subclusters_need_allocation = false;
4461 
4462         /* With a data file, preallocation means just allocating the metadata
4463          * and forwarding the truncate request to the data file */
4464         if (has_data_file(bs)) {
4465             ret = preallocate_co(bs, old_length, offset, prealloc, errp);
4466             if (ret < 0) {
4467                 goto fail;
4468             }
4469             break;
4470         }
4471 
4472         old_file_size = bdrv_co_getlength(bs->file->bs);
4473         if (old_file_size < 0) {
4474             error_setg_errno(errp, -old_file_size,
4475                              "Failed to inquire current file length");
4476             ret = old_file_size;
4477             goto fail;
4478         }
4479 
4480         last_cluster = qcow2_get_last_cluster(bs, old_file_size);
4481         if (last_cluster >= 0) {
4482             old_file_size = (last_cluster + 1) * s->cluster_size;
4483         } else {
4484             old_file_size = ROUND_UP(old_file_size, s->cluster_size);
4485         }
4486 
4487         nb_new_data_clusters = (ROUND_UP(offset, s->cluster_size) -
4488             start_of_cluster(s, old_length)) >> s->cluster_bits;
4489 
4490         /* This is an overestimation; we will not actually allocate space for
4491          * these in the file but just make sure the new refcount structures are
4492          * able to cover them so we will not have to allocate new refblocks
4493          * while entering the data blocks in the potentially new L2 tables.
4494          * (We do not actually care where the L2 tables are placed. Maybe they
4495          *  are already allocated or they can be placed somewhere before
4496          *  @old_file_size. It does not matter because they will be fully
4497          *  allocated automatically, so they do not need to be covered by the
4498          *  preallocation. All that matters is that we will not have to allocate
4499          *  new refcount structures for them.) */
4500         nb_new_l2_tables = DIV_ROUND_UP(nb_new_data_clusters,
4501                                         s->cluster_size / l2_entry_size(s));
4502         /* The cluster range may not be aligned to L2 boundaries, so add one L2
4503          * table for a potential head/tail */
4504         nb_new_l2_tables++;
4505 
4506         allocation_start = qcow2_refcount_area(bs, old_file_size,
4507                                                nb_new_data_clusters +
4508                                                nb_new_l2_tables,
4509                                                true, 0, 0);
4510         if (allocation_start < 0) {
4511             error_setg_errno(errp, -allocation_start,
4512                              "Failed to resize refcount structures");
4513             ret = allocation_start;
4514             goto fail;
4515         }
4516 
4517         clusters_allocated = qcow2_alloc_clusters_at(bs, allocation_start,
4518                                                      nb_new_data_clusters);
4519         if (clusters_allocated < 0) {
4520             error_setg_errno(errp, -clusters_allocated,
4521                              "Failed to allocate data clusters");
4522             ret = clusters_allocated;
4523             goto fail;
4524         }
4525 
4526         assert(clusters_allocated == nb_new_data_clusters);
4527 
4528         /* Allocate the data area */
4529         new_file_size = allocation_start +
4530                         nb_new_data_clusters * s->cluster_size;
4531         /*
4532          * Image file grows, so @exact does not matter.
4533          *
4534          * If we need to zero out the new area, try first whether the protocol
4535          * driver can already take care of this.
4536          */
4537         if (flags & BDRV_REQ_ZERO_WRITE) {
4538             ret = bdrv_co_truncate(bs->file, new_file_size, false, prealloc,
4539                                    BDRV_REQ_ZERO_WRITE, NULL);
4540             if (ret >= 0) {
4541                 flags &= ~BDRV_REQ_ZERO_WRITE;
4542                 /* Ensure that we read zeroes and not backing file data */
4543                 subclusters_need_allocation = true;
4544             }
4545         } else {
4546             ret = -1;
4547         }
4548         if (ret < 0) {
4549             ret = bdrv_co_truncate(bs->file, new_file_size, false, prealloc, 0,
4550                                    errp);
4551         }
4552         if (ret < 0) {
4553             error_prepend(errp, "Failed to resize underlying file: ");
4554             qcow2_free_clusters(bs, allocation_start,
4555                                 nb_new_data_clusters * s->cluster_size,
4556                                 QCOW2_DISCARD_OTHER);
4557             goto fail;
4558         }
4559 
4560         /* Create the necessary L2 entries */
4561         host_offset = allocation_start;
4562         guest_offset = old_length;
4563         while (nb_new_data_clusters) {
4564             int64_t nb_clusters = MIN(
4565                 nb_new_data_clusters,
4566                 s->l2_slice_size - offset_to_l2_slice_index(s, guest_offset));
4567             unsigned cow_start_length = offset_into_cluster(s, guest_offset);
4568             QCowL2Meta allocation;
4569             guest_offset = start_of_cluster(s, guest_offset);
4570             allocation = (QCowL2Meta) {
4571                 .offset       = guest_offset,
4572                 .alloc_offset = host_offset,
4573                 .nb_clusters  = nb_clusters,
4574                 .cow_start    = {
4575                     .offset       = 0,
4576                     .nb_bytes     = cow_start_length,
4577                 },
4578                 .cow_end      = {
4579                     .offset       = nb_clusters << s->cluster_bits,
4580                     .nb_bytes     = 0,
4581                 },
4582                 .prealloc     = !subclusters_need_allocation,
4583             };
4584             qemu_co_queue_init(&allocation.dependent_requests);
4585 
4586             ret = qcow2_alloc_cluster_link_l2(bs, &allocation);
4587             if (ret < 0) {
4588                 error_setg_errno(errp, -ret, "Failed to update L2 tables");
4589                 qcow2_free_clusters(bs, host_offset,
4590                                     nb_new_data_clusters * s->cluster_size,
4591                                     QCOW2_DISCARD_OTHER);
4592                 goto fail;
4593             }
4594 
4595             guest_offset += nb_clusters * s->cluster_size;
4596             host_offset += nb_clusters * s->cluster_size;
4597             nb_new_data_clusters -= nb_clusters;
4598         }
4599         break;
4600     }
4601 
4602     default:
4603         g_assert_not_reached();
4604     }
4605 
4606     if ((flags & BDRV_REQ_ZERO_WRITE) && offset > old_length) {
4607         uint64_t zero_start = QEMU_ALIGN_UP(old_length, s->subcluster_size);
4608 
4609         /*
4610          * Use zero clusters as much as we can. qcow2_subcluster_zeroize()
4611          * requires a subcluster-aligned start. The end may be unaligned if
4612          * it is at the end of the image (which it is here).
4613          */
4614         if (offset > zero_start) {
4615             ret = qcow2_subcluster_zeroize(bs, zero_start, offset - zero_start,
4616                                            0);
4617             if (ret < 0) {
4618                 error_setg_errno(errp, -ret, "Failed to zero out new clusters");
4619                 goto fail;
4620             }
4621         }
4622 
4623         /* Write explicit zeros for the unaligned head */
4624         if (zero_start > old_length) {
4625             uint64_t len = MIN(zero_start, offset) - old_length;
4626             uint8_t *buf = qemu_blockalign0(bs, len);
4627             QEMUIOVector qiov;
4628             qemu_iovec_init_buf(&qiov, buf, len);
4629 
4630             qemu_co_mutex_unlock(&s->lock);
4631             ret = qcow2_co_pwritev_part(bs, old_length, len, &qiov, 0, 0);
4632             qemu_co_mutex_lock(&s->lock);
4633 
4634             qemu_vfree(buf);
4635             if (ret < 0) {
4636                 error_setg_errno(errp, -ret, "Failed to zero out the new area");
4637                 goto fail;
4638             }
4639         }
4640     }
4641 
4642     if (prealloc != PREALLOC_MODE_OFF) {
4643         /* Flush metadata before actually changing the image size */
4644         ret = qcow2_write_caches(bs);
4645         if (ret < 0) {
4646             error_setg_errno(errp, -ret,
4647                              "Failed to flush the preallocated area to disk");
4648             goto fail;
4649         }
4650     }
4651 
4652     bs->total_sectors = offset / BDRV_SECTOR_SIZE;
4653 
4654     /* write updated header.size */
4655     offset = cpu_to_be64(offset);
4656     ret = bdrv_co_pwrite_sync(bs->file, offsetof(QCowHeader, size),
4657                               sizeof(offset), &offset, 0);
4658     if (ret < 0) {
4659         error_setg_errno(errp, -ret, "Failed to update the image size");
4660         goto fail;
4661     }
4662 
4663     s->l1_vm_state_index = new_l1_size;
4664 
4665     /* Update cache sizes */
4666     options = qdict_clone_shallow(bs->options);
4667     ret = qcow2_update_options(bs, options, s->flags, errp);
4668     qobject_unref(options);
4669     if (ret < 0) {
4670         goto fail;
4671     }
4672     ret = 0;
4673 fail:
4674     qemu_co_mutex_unlock(&s->lock);
4675     return ret;
4676 }
4677 
4678 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_pwritev_compressed_task(BlockDriverState * bs,uint64_t offset,uint64_t bytes,QEMUIOVector * qiov,size_t qiov_offset)4679 qcow2_co_pwritev_compressed_task(BlockDriverState *bs,
4680                                  uint64_t offset, uint64_t bytes,
4681                                  QEMUIOVector *qiov, size_t qiov_offset)
4682 {
4683     BDRVQcow2State *s = bs->opaque;
4684     int ret;
4685     ssize_t out_len;
4686     uint8_t *buf, *out_buf;
4687     uint64_t cluster_offset;
4688 
4689     assert(bytes == s->cluster_size || (bytes < s->cluster_size &&
4690            (offset + bytes == bs->total_sectors << BDRV_SECTOR_BITS)));
4691 
4692     buf = qemu_blockalign(bs, s->cluster_size);
4693     if (bytes < s->cluster_size) {
4694         /* Zero-pad last write if image size is not cluster aligned */
4695         memset(buf + bytes, 0, s->cluster_size - bytes);
4696     }
4697     qemu_iovec_to_buf(qiov, qiov_offset, buf, bytes);
4698 
4699     out_buf = g_malloc(s->cluster_size);
4700 
4701     out_len = qcow2_co_compress(bs, out_buf, s->cluster_size - 1,
4702                                 buf, s->cluster_size);
4703     if (out_len == -ENOMEM) {
4704         /* could not compress: write normal cluster */
4705         ret = qcow2_co_pwritev_part(bs, offset, bytes, qiov, qiov_offset, 0);
4706         if (ret < 0) {
4707             goto fail;
4708         }
4709         goto success;
4710     } else if (out_len < 0) {
4711         ret = -EINVAL;
4712         goto fail;
4713     }
4714 
4715     qemu_co_mutex_lock(&s->lock);
4716     ret = qcow2_alloc_compressed_cluster_offset(bs, offset, out_len,
4717                                                 &cluster_offset);
4718     if (ret < 0) {
4719         qemu_co_mutex_unlock(&s->lock);
4720         goto fail;
4721     }
4722 
4723     ret = qcow2_pre_write_overlap_check(bs, 0, cluster_offset, out_len, true);
4724     qemu_co_mutex_unlock(&s->lock);
4725     if (ret < 0) {
4726         goto fail;
4727     }
4728 
4729     BLKDBG_CO_EVENT(s->data_file, BLKDBG_WRITE_COMPRESSED);
4730     ret = bdrv_co_pwrite(s->data_file, cluster_offset, out_len, out_buf, 0);
4731     if (ret < 0) {
4732         goto fail;
4733     }
4734 success:
4735     ret = 0;
4736 fail:
4737     qemu_vfree(buf);
4738     g_free(out_buf);
4739     return ret;
4740 }
4741 
4742 /*
4743  * This function can count as GRAPH_RDLOCK because
4744  * qcow2_co_pwritev_compressed_part() holds the graph lock and keeps it until
4745  * this coroutine has terminated.
4746  */
4747 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_pwritev_compressed_task_entry(AioTask * task)4748 qcow2_co_pwritev_compressed_task_entry(AioTask *task)
4749 {
4750     Qcow2AioTask *t = container_of(task, Qcow2AioTask, task);
4751 
4752     assert(!t->subcluster_type && !t->l2meta);
4753 
4754     return qcow2_co_pwritev_compressed_task(t->bs, t->offset, t->bytes, t->qiov,
4755                                             t->qiov_offset);
4756 }
4757 
4758 /*
4759  * XXX: put compressed sectors first, then all the cluster aligned
4760  * tables to avoid losing bytes in alignment
4761  */
4762 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_pwritev_compressed_part(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,size_t qiov_offset)4763 qcow2_co_pwritev_compressed_part(BlockDriverState *bs,
4764                                  int64_t offset, int64_t bytes,
4765                                  QEMUIOVector *qiov, size_t qiov_offset)
4766 {
4767     BDRVQcow2State *s = bs->opaque;
4768     AioTaskPool *aio = NULL;
4769     int ret = 0;
4770 
4771     if (has_data_file(bs)) {
4772         return -ENOTSUP;
4773     }
4774 
4775     if (bytes == 0) {
4776         /*
4777          * align end of file to a sector boundary to ease reading with
4778          * sector based I/Os
4779          */
4780         int64_t len = bdrv_co_getlength(bs->file->bs);
4781         if (len < 0) {
4782             return len;
4783         }
4784         return bdrv_co_truncate(bs->file, len, false, PREALLOC_MODE_OFF, 0,
4785                                 NULL);
4786     }
4787 
4788     if (offset_into_cluster(s, offset)) {
4789         return -EINVAL;
4790     }
4791 
4792     if (offset_into_cluster(s, bytes) &&
4793         (offset + bytes) != (bs->total_sectors << BDRV_SECTOR_BITS)) {
4794         return -EINVAL;
4795     }
4796 
4797     while (bytes && aio_task_pool_status(aio) == 0) {
4798         uint64_t chunk_size = MIN(bytes, s->cluster_size);
4799 
4800         if (!aio && chunk_size != bytes) {
4801             aio = aio_task_pool_new(QCOW2_MAX_WORKERS);
4802         }
4803 
4804         ret = qcow2_add_task(bs, aio, qcow2_co_pwritev_compressed_task_entry,
4805                              0, 0, offset, chunk_size, qiov, qiov_offset, NULL);
4806         if (ret < 0) {
4807             break;
4808         }
4809         qiov_offset += chunk_size;
4810         offset += chunk_size;
4811         bytes -= chunk_size;
4812     }
4813 
4814     if (aio) {
4815         aio_task_pool_wait_all(aio);
4816         if (ret == 0) {
4817             ret = aio_task_pool_status(aio);
4818         }
4819         g_free(aio);
4820     }
4821 
4822     return ret;
4823 }
4824 
4825 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_preadv_compressed(BlockDriverState * bs,uint64_t l2_entry,uint64_t offset,uint64_t bytes,QEMUIOVector * qiov,size_t qiov_offset)4826 qcow2_co_preadv_compressed(BlockDriverState *bs,
4827                            uint64_t l2_entry,
4828                            uint64_t offset,
4829                            uint64_t bytes,
4830                            QEMUIOVector *qiov,
4831                            size_t qiov_offset)
4832 {
4833     BDRVQcow2State *s = bs->opaque;
4834     int ret = 0, csize;
4835     uint64_t coffset;
4836     uint8_t *buf, *out_buf;
4837     int offset_in_cluster = offset_into_cluster(s, offset);
4838 
4839     qcow2_parse_compressed_l2_entry(bs, l2_entry, &coffset, &csize);
4840 
4841     buf = g_try_malloc(csize);
4842     if (!buf) {
4843         return -ENOMEM;
4844     }
4845 
4846     out_buf = qemu_blockalign(bs, s->cluster_size);
4847 
4848     BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
4849     ret = bdrv_co_pread(bs->file, coffset, csize, buf, 0);
4850     if (ret < 0) {
4851         goto fail;
4852     }
4853 
4854     if (qcow2_co_decompress(bs, out_buf, s->cluster_size, buf, csize) < 0) {
4855         ret = -EIO;
4856         goto fail;
4857     }
4858 
4859     qemu_iovec_from_buf(qiov, qiov_offset, out_buf + offset_in_cluster, bytes);
4860 
4861 fail:
4862     qemu_vfree(out_buf);
4863     g_free(buf);
4864 
4865     return ret;
4866 }
4867 
make_completely_empty(BlockDriverState * bs)4868 static int GRAPH_RDLOCK make_completely_empty(BlockDriverState *bs)
4869 {
4870     BDRVQcow2State *s = bs->opaque;
4871     Error *local_err = NULL;
4872     int ret, l1_clusters;
4873     int64_t offset;
4874     uint64_t *new_reftable = NULL;
4875     uint64_t rt_entry, l1_size2;
4876     struct {
4877         uint64_t l1_offset;
4878         uint64_t reftable_offset;
4879         uint32_t reftable_clusters;
4880     } QEMU_PACKED l1_ofs_rt_ofs_cls;
4881 
4882     ret = qcow2_cache_empty(bs, s->l2_table_cache);
4883     if (ret < 0) {
4884         goto fail;
4885     }
4886 
4887     ret = qcow2_cache_empty(bs, s->refcount_block_cache);
4888     if (ret < 0) {
4889         goto fail;
4890     }
4891 
4892     /* Refcounts will be broken utterly */
4893     ret = qcow2_mark_dirty(bs);
4894     if (ret < 0) {
4895         goto fail;
4896     }
4897 
4898     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
4899 
4900     l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / L1E_SIZE);
4901     l1_size2 = (uint64_t)s->l1_size * L1E_SIZE;
4902 
4903     /* After this call, neither the in-memory nor the on-disk refcount
4904      * information accurately describe the actual references */
4905 
4906     ret = bdrv_pwrite_zeroes(bs->file, s->l1_table_offset,
4907                              l1_clusters * s->cluster_size, 0);
4908     if (ret < 0) {
4909         goto fail_broken_refcounts;
4910     }
4911     memset(s->l1_table, 0, l1_size2);
4912 
4913     BLKDBG_EVENT(bs->file, BLKDBG_EMPTY_IMAGE_PREPARE);
4914 
4915     /* Overwrite enough clusters at the beginning of the sectors to place
4916      * the refcount table, a refcount block and the L1 table in; this may
4917      * overwrite parts of the existing refcount and L1 table, which is not
4918      * an issue because the dirty flag is set, complete data loss is in fact
4919      * desired and partial data loss is consequently fine as well */
4920     ret = bdrv_pwrite_zeroes(bs->file, s->cluster_size,
4921                              (2 + l1_clusters) * s->cluster_size, 0);
4922     /* This call (even if it failed overall) may have overwritten on-disk
4923      * refcount structures; in that case, the in-memory refcount information
4924      * will probably differ from the on-disk information which makes the BDS
4925      * unusable */
4926     if (ret < 0) {
4927         goto fail_broken_refcounts;
4928     }
4929 
4930     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
4931     BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
4932 
4933     /* "Create" an empty reftable (one cluster) directly after the image
4934      * header and an empty L1 table three clusters after the image header;
4935      * the cluster between those two will be used as the first refblock */
4936     l1_ofs_rt_ofs_cls.l1_offset = cpu_to_be64(3 * s->cluster_size);
4937     l1_ofs_rt_ofs_cls.reftable_offset = cpu_to_be64(s->cluster_size);
4938     l1_ofs_rt_ofs_cls.reftable_clusters = cpu_to_be32(1);
4939     ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_table_offset),
4940                            sizeof(l1_ofs_rt_ofs_cls), &l1_ofs_rt_ofs_cls, 0);
4941     if (ret < 0) {
4942         goto fail_broken_refcounts;
4943     }
4944 
4945     s->l1_table_offset = 3 * s->cluster_size;
4946 
4947     new_reftable = g_try_new0(uint64_t, s->cluster_size / REFTABLE_ENTRY_SIZE);
4948     if (!new_reftable) {
4949         ret = -ENOMEM;
4950         goto fail_broken_refcounts;
4951     }
4952 
4953     s->refcount_table_offset = s->cluster_size;
4954     s->refcount_table_size   = s->cluster_size / REFTABLE_ENTRY_SIZE;
4955     s->max_refcount_table_index = 0;
4956 
4957     g_free(s->refcount_table);
4958     s->refcount_table = new_reftable;
4959     new_reftable = NULL;
4960 
4961     /* Now the in-memory refcount information again corresponds to the on-disk
4962      * information (reftable is empty and no refblocks (the refblock cache is
4963      * empty)); however, this means some clusters (e.g. the image header) are
4964      * referenced, but not refcounted, but the normal qcow2 code assumes that
4965      * the in-memory information is always correct */
4966 
4967     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
4968 
4969     /* Enter the first refblock into the reftable */
4970     rt_entry = cpu_to_be64(2 * s->cluster_size);
4971     ret = bdrv_pwrite_sync(bs->file, s->cluster_size, sizeof(rt_entry),
4972                            &rt_entry, 0);
4973     if (ret < 0) {
4974         goto fail_broken_refcounts;
4975     }
4976     s->refcount_table[0] = 2 * s->cluster_size;
4977 
4978     s->free_cluster_index = 0;
4979     assert(3 + l1_clusters <= s->refcount_block_size);
4980     offset = qcow2_alloc_clusters(bs, 3 * s->cluster_size + l1_size2);
4981     if (offset < 0) {
4982         ret = offset;
4983         goto fail_broken_refcounts;
4984     } else if (offset > 0) {
4985         error_report("First cluster in emptied image is in use");
4986         abort();
4987     }
4988 
4989     /* Now finally the in-memory information corresponds to the on-disk
4990      * structures and is correct */
4991     ret = qcow2_mark_clean(bs);
4992     if (ret < 0) {
4993         goto fail;
4994     }
4995 
4996     ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size, false,
4997                         PREALLOC_MODE_OFF, 0, &local_err);
4998     if (ret < 0) {
4999         error_report_err(local_err);
5000         goto fail;
5001     }
5002 
5003     return 0;
5004 
5005 fail_broken_refcounts:
5006     /* The BDS is unusable at this point. If we wanted to make it usable, we
5007      * would have to call qcow2_refcount_close(), qcow2_refcount_init(),
5008      * qcow2_check_refcounts(), qcow2_refcount_close() and qcow2_refcount_init()
5009      * again. However, because the functions which could have caused this error
5010      * path to be taken are used by those functions as well, it's very likely
5011      * that that sequence will fail as well. Therefore, just eject the BDS. */
5012     bs->drv = NULL;
5013 
5014 fail:
5015     g_free(new_reftable);
5016     return ret;
5017 }
5018 
qcow2_make_empty(BlockDriverState * bs)5019 static int GRAPH_RDLOCK qcow2_make_empty(BlockDriverState *bs)
5020 {
5021     BDRVQcow2State *s = bs->opaque;
5022     uint64_t offset, end_offset;
5023     int step = QEMU_ALIGN_DOWN(INT_MAX, s->cluster_size);
5024     int l1_clusters, ret = 0;
5025 
5026     l1_clusters = DIV_ROUND_UP(s->l1_size, s->cluster_size / L1E_SIZE);
5027 
5028     if (s->qcow_version >= 3 && !s->snapshots && !s->nb_bitmaps &&
5029         3 + l1_clusters <= s->refcount_block_size &&
5030         s->crypt_method_header != QCOW_CRYPT_LUKS &&
5031         !has_data_file(bs)) {
5032         /* The following function only works for qcow2 v3 images (it
5033          * requires the dirty flag) and only as long as there are no
5034          * features that reserve extra clusters (such as snapshots,
5035          * LUKS header, or persistent bitmaps), because it completely
5036          * empties the image.  Furthermore, the L1 table and three
5037          * additional clusters (image header, refcount table, one
5038          * refcount block) have to fit inside one refcount block. It
5039          * only resets the image file, i.e. does not work with an
5040          * external data file. */
5041         return make_completely_empty(bs);
5042     }
5043 
5044     /* This fallback code simply discards every active cluster; this is slow,
5045      * but works in all cases */
5046     end_offset = bs->total_sectors * BDRV_SECTOR_SIZE;
5047     for (offset = 0; offset < end_offset; offset += step) {
5048         /* As this function is generally used after committing an external
5049          * snapshot, QCOW2_DISCARD_SNAPSHOT seems appropriate. Also, the
5050          * default action for this kind of discard is to pass the discard,
5051          * which will ideally result in an actually smaller image file, as
5052          * is probably desired. */
5053         ret = qcow2_cluster_discard(bs, offset, MIN(step, end_offset - offset),
5054                                     QCOW2_DISCARD_SNAPSHOT, true);
5055         if (ret < 0) {
5056             break;
5057         }
5058     }
5059 
5060     return ret;
5061 }
5062 
qcow2_co_flush_to_os(BlockDriverState * bs)5063 static coroutine_fn GRAPH_RDLOCK int qcow2_co_flush_to_os(BlockDriverState *bs)
5064 {
5065     BDRVQcow2State *s = bs->opaque;
5066     int ret;
5067 
5068     qemu_co_mutex_lock(&s->lock);
5069     ret = qcow2_write_caches(bs);
5070     qemu_co_mutex_unlock(&s->lock);
5071 
5072     return ret;
5073 }
5074 
qcow2_measure(QemuOpts * opts,BlockDriverState * in_bs,Error ** errp)5075 static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
5076                                        Error **errp)
5077 {
5078     Error *local_err = NULL;
5079     BlockMeasureInfo *info;
5080     uint64_t required = 0; /* bytes that contribute to required size */
5081     uint64_t virtual_size; /* disk size as seen by guest */
5082     uint64_t refcount_bits;
5083     uint64_t l2_tables;
5084     uint64_t luks_payload_size = 0;
5085     size_t cluster_size;
5086     int version;
5087     char *optstr;
5088     PreallocMode prealloc;
5089     bool has_backing_file;
5090     bool has_luks;
5091     bool extended_l2;
5092     size_t l2e_size;
5093 
5094     /* Parse image creation options */
5095     extended_l2 = qemu_opt_get_bool_del(opts, BLOCK_OPT_EXTL2, false);
5096 
5097     cluster_size = qcow2_opt_get_cluster_size_del(opts, extended_l2,
5098                                                   &local_err);
5099     if (local_err) {
5100         goto err;
5101     }
5102 
5103     version = qcow2_opt_get_version_del(opts, &local_err);
5104     if (local_err) {
5105         goto err;
5106     }
5107 
5108     refcount_bits = qcow2_opt_get_refcount_bits_del(opts, version, &local_err);
5109     if (local_err) {
5110         goto err;
5111     }
5112 
5113     optstr = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
5114     prealloc = qapi_enum_parse(&PreallocMode_lookup, optstr,
5115                                PREALLOC_MODE_OFF, &local_err);
5116     g_free(optstr);
5117     if (local_err) {
5118         goto err;
5119     }
5120 
5121     optstr = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
5122     has_backing_file = !!optstr;
5123     g_free(optstr);
5124 
5125     optstr = qemu_opt_get_del(opts, BLOCK_OPT_ENCRYPT_FORMAT);
5126     has_luks = optstr && strcmp(optstr, "luks") == 0;
5127     g_free(optstr);
5128 
5129     if (has_luks) {
5130         g_autoptr(QCryptoBlockCreateOptions) create_opts = NULL;
5131         QDict *cryptoopts = qcow2_extract_crypto_opts(opts, "luks", errp);
5132         size_t headerlen;
5133 
5134         create_opts = block_crypto_create_opts_init(cryptoopts, errp);
5135         qobject_unref(cryptoopts);
5136         if (!create_opts) {
5137             goto err;
5138         }
5139 
5140         if (!qcrypto_block_calculate_payload_offset(create_opts,
5141                                                     "encrypt.",
5142                                                     &headerlen,
5143                                                     &local_err)) {
5144             goto err;
5145         }
5146 
5147         luks_payload_size = ROUND_UP(headerlen, cluster_size);
5148     }
5149 
5150     virtual_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
5151     virtual_size = ROUND_UP(virtual_size, cluster_size);
5152 
5153     /* Check that virtual disk size is valid */
5154     l2e_size = extended_l2 ? L2E_SIZE_EXTENDED : L2E_SIZE_NORMAL;
5155     l2_tables = DIV_ROUND_UP(virtual_size / cluster_size,
5156                              cluster_size / l2e_size);
5157     if (l2_tables * L1E_SIZE > QCOW_MAX_L1_SIZE) {
5158         error_setg(&local_err, "The image size is too large "
5159                                "(try using a larger cluster size)");
5160         goto err;
5161     }
5162 
5163     /* Account for input image */
5164     if (in_bs) {
5165         int64_t ssize = bdrv_getlength(in_bs);
5166         if (ssize < 0) {
5167             error_setg_errno(&local_err, -ssize,
5168                              "Unable to get image virtual_size");
5169             goto err;
5170         }
5171 
5172         virtual_size = ROUND_UP(ssize, cluster_size);
5173 
5174         if (has_backing_file) {
5175             /* We don't how much of the backing chain is shared by the input
5176              * image and the new image file.  In the worst case the new image's
5177              * backing file has nothing in common with the input image.  Be
5178              * conservative and assume all clusters need to be written.
5179              */
5180             required = virtual_size;
5181         } else {
5182             int64_t offset;
5183             int64_t pnum = 0;
5184 
5185             for (offset = 0; offset < ssize; offset += pnum) {
5186                 int ret;
5187 
5188                 ret = bdrv_block_status_above(in_bs, NULL, offset,
5189                                               ssize - offset, &pnum, NULL,
5190                                               NULL);
5191                 if (ret < 0) {
5192                     error_setg_errno(&local_err, -ret,
5193                                      "Unable to get block status");
5194                     goto err;
5195                 }
5196 
5197                 if (ret & BDRV_BLOCK_ZERO) {
5198                     /* Skip zero regions (safe with no backing file) */
5199                 } else if ((ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) ==
5200                            (BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED)) {
5201                     /* Extend pnum to end of cluster for next iteration */
5202                     pnum = ROUND_UP(offset + pnum, cluster_size) - offset;
5203 
5204                     /* Count clusters we've seen */
5205                     required += offset % cluster_size + pnum;
5206                 }
5207             }
5208         }
5209     }
5210 
5211     /* Take into account preallocation.  Nothing special is needed for
5212      * PREALLOC_MODE_METADATA since metadata is always counted.
5213      */
5214     if (prealloc == PREALLOC_MODE_FULL || prealloc == PREALLOC_MODE_FALLOC) {
5215         required = virtual_size;
5216     }
5217 
5218     info = g_new0(BlockMeasureInfo, 1);
5219     info->fully_allocated = luks_payload_size +
5220         qcow2_calc_prealloc_size(virtual_size, cluster_size,
5221                                  ctz32(refcount_bits), extended_l2);
5222 
5223     /*
5224      * Remove data clusters that are not required.  This overestimates the
5225      * required size because metadata needed for the fully allocated file is
5226      * still counted.  Show bitmaps only if both source and destination
5227      * would support them.
5228      */
5229     info->required = info->fully_allocated - virtual_size + required;
5230     info->has_bitmaps = version >= 3 && in_bs &&
5231         bdrv_supports_persistent_dirty_bitmap(in_bs);
5232     if (info->has_bitmaps) {
5233         info->bitmaps = qcow2_get_persistent_dirty_bitmap_size(in_bs,
5234                                                                cluster_size);
5235     }
5236     return info;
5237 
5238 err:
5239     error_propagate(errp, local_err);
5240     return NULL;
5241 }
5242 
5243 static int coroutine_fn
qcow2_co_get_info(BlockDriverState * bs,BlockDriverInfo * bdi)5244 qcow2_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
5245 {
5246     BDRVQcow2State *s = bs->opaque;
5247     bdi->cluster_size = s->cluster_size;
5248     bdi->subcluster_size = s->subcluster_size;
5249     bdi->vm_state_offset = qcow2_vm_state_offset(s);
5250     bdi->is_dirty = s->incompatible_features & QCOW2_INCOMPAT_DIRTY;
5251     return 0;
5252 }
5253 
5254 static ImageInfoSpecific * GRAPH_RDLOCK
qcow2_get_specific_info(BlockDriverState * bs,Error ** errp)5255 qcow2_get_specific_info(BlockDriverState *bs, Error **errp)
5256 {
5257     BDRVQcow2State *s = bs->opaque;
5258     ImageInfoSpecific *spec_info;
5259     QCryptoBlockInfo *encrypt_info = NULL;
5260 
5261     if (s->crypto != NULL) {
5262         encrypt_info = qcrypto_block_get_info(s->crypto, errp);
5263         if (!encrypt_info) {
5264             return NULL;
5265         }
5266     }
5267 
5268     spec_info = g_new(ImageInfoSpecific, 1);
5269     *spec_info = (ImageInfoSpecific){
5270         .type  = IMAGE_INFO_SPECIFIC_KIND_QCOW2,
5271         .u.qcow2.data = g_new0(ImageInfoSpecificQCow2, 1),
5272     };
5273     if (s->qcow_version == 2) {
5274         *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
5275             .compat             = g_strdup("0.10"),
5276             .refcount_bits      = s->refcount_bits,
5277         };
5278     } else if (s->qcow_version == 3) {
5279         Qcow2BitmapInfoList *bitmaps;
5280         if (!qcow2_get_bitmap_info_list(bs, &bitmaps, errp)) {
5281             qapi_free_ImageInfoSpecific(spec_info);
5282             qapi_free_QCryptoBlockInfo(encrypt_info);
5283             return NULL;
5284         }
5285         *spec_info->u.qcow2.data = (ImageInfoSpecificQCow2){
5286             .compat             = g_strdup("1.1"),
5287             .lazy_refcounts     = s->compatible_features &
5288                                   QCOW2_COMPAT_LAZY_REFCOUNTS,
5289             .has_lazy_refcounts = true,
5290             .corrupt            = s->incompatible_features &
5291                                   QCOW2_INCOMPAT_CORRUPT,
5292             .has_corrupt        = true,
5293             .has_extended_l2    = true,
5294             .extended_l2        = has_subclusters(s),
5295             .refcount_bits      = s->refcount_bits,
5296             .has_bitmaps        = !!bitmaps,
5297             .bitmaps            = bitmaps,
5298             .data_file          = g_strdup(s->image_data_file),
5299             .has_data_file_raw  = has_data_file(bs),
5300             .data_file_raw      = data_file_is_raw(bs),
5301             .compression_type   = s->compression_type,
5302         };
5303     } else {
5304         /* if this assertion fails, this probably means a new version was
5305          * added without having it covered here */
5306         g_assert_not_reached();
5307     }
5308 
5309     if (encrypt_info) {
5310         ImageInfoSpecificQCow2Encryption *qencrypt =
5311             g_new(ImageInfoSpecificQCow2Encryption, 1);
5312         switch (encrypt_info->format) {
5313         case QCRYPTO_BLOCK_FORMAT_QCOW:
5314             qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_AES;
5315             break;
5316         case QCRYPTO_BLOCK_FORMAT_LUKS:
5317             qencrypt->format = BLOCKDEV_QCOW2_ENCRYPTION_FORMAT_LUKS;
5318             qencrypt->u.luks = encrypt_info->u.luks;
5319             break;
5320         default:
5321             abort();
5322         }
5323         /* Since we did shallow copy above, erase any pointers
5324          * in the original info */
5325         memset(&encrypt_info->u, 0, sizeof(encrypt_info->u));
5326         qapi_free_QCryptoBlockInfo(encrypt_info);
5327 
5328         spec_info->u.qcow2.data->encrypt = qencrypt;
5329     }
5330 
5331     return spec_info;
5332 }
5333 
5334 static int coroutine_mixed_fn GRAPH_RDLOCK
qcow2_has_zero_init(BlockDriverState * bs)5335 qcow2_has_zero_init(BlockDriverState *bs)
5336 {
5337     BDRVQcow2State *s = bs->opaque;
5338     bool preallocated;
5339 
5340     if (qemu_in_coroutine()) {
5341         qemu_co_mutex_lock(&s->lock);
5342     }
5343     /*
5344      * Check preallocation status: Preallocated images have all L2
5345      * tables allocated, nonpreallocated images have none.  It is
5346      * therefore enough to check the first one.
5347      */
5348     preallocated = s->l1_size > 0 && s->l1_table[0] != 0;
5349     if (qemu_in_coroutine()) {
5350         qemu_co_mutex_unlock(&s->lock);
5351     }
5352 
5353     if (!preallocated) {
5354         return 1;
5355     } else if (bs->encrypted) {
5356         return 0;
5357     } else {
5358         return bdrv_has_zero_init(s->data_file->bs);
5359     }
5360 }
5361 
5362 /*
5363  * Check the request to vmstate. On success return
5364  *      qcow2_vm_state_offset(bs) + @pos
5365  */
qcow2_check_vmstate_request(BlockDriverState * bs,QEMUIOVector * qiov,int64_t pos)5366 static int64_t qcow2_check_vmstate_request(BlockDriverState *bs,
5367                                            QEMUIOVector *qiov, int64_t pos)
5368 {
5369     BDRVQcow2State *s = bs->opaque;
5370     int64_t vmstate_offset = qcow2_vm_state_offset(s);
5371     int ret;
5372 
5373     /* Incoming requests must be OK */
5374     bdrv_check_qiov_request(pos, qiov->size, qiov, 0, &error_abort);
5375 
5376     if (INT64_MAX - pos < vmstate_offset) {
5377         return -EIO;
5378     }
5379 
5380     pos += vmstate_offset;
5381     ret = bdrv_check_qiov_request(pos, qiov->size, qiov, 0, NULL);
5382     if (ret < 0) {
5383         return ret;
5384     }
5385 
5386     return pos;
5387 }
5388 
5389 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_save_vmstate(BlockDriverState * bs,QEMUIOVector * qiov,int64_t pos)5390 qcow2_co_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
5391 {
5392     int64_t offset = qcow2_check_vmstate_request(bs, qiov, pos);
5393     if (offset < 0) {
5394         return offset;
5395     }
5396 
5397     BLKDBG_CO_EVENT(bs->file, BLKDBG_VMSTATE_SAVE);
5398     return bs->drv->bdrv_co_pwritev_part(bs, offset, qiov->size, qiov, 0, 0);
5399 }
5400 
5401 static int coroutine_fn GRAPH_RDLOCK
qcow2_co_load_vmstate(BlockDriverState * bs,QEMUIOVector * qiov,int64_t pos)5402 qcow2_co_load_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
5403 {
5404     int64_t offset = qcow2_check_vmstate_request(bs, qiov, pos);
5405     if (offset < 0) {
5406         return offset;
5407     }
5408 
5409     BLKDBG_CO_EVENT(bs->file, BLKDBG_VMSTATE_LOAD);
5410     return bs->drv->bdrv_co_preadv_part(bs, offset, qiov->size, qiov, 0, 0);
5411 }
5412 
qcow2_has_compressed_clusters(BlockDriverState * bs)5413 static int GRAPH_RDLOCK qcow2_has_compressed_clusters(BlockDriverState *bs)
5414 {
5415     int64_t offset = 0;
5416     int64_t bytes = bdrv_getlength(bs);
5417 
5418     if (bytes < 0) {
5419         return bytes;
5420     }
5421 
5422     while (bytes != 0) {
5423         int ret;
5424         QCow2SubclusterType type;
5425         unsigned int cur_bytes = MIN(INT_MAX, bytes);
5426         uint64_t host_offset;
5427 
5428         ret = qcow2_get_host_offset(bs, offset, &cur_bytes, &host_offset,
5429                                     &type);
5430         if (ret < 0) {
5431             return ret;
5432         }
5433 
5434         if (type == QCOW2_SUBCLUSTER_COMPRESSED) {
5435             return 1;
5436         }
5437 
5438         offset += cur_bytes;
5439         bytes -= cur_bytes;
5440     }
5441 
5442     return 0;
5443 }
5444 
5445 /*
5446  * Downgrades an image's version. To achieve this, any incompatible features
5447  * have to be removed.
5448  */
5449 static int GRAPH_RDLOCK
qcow2_downgrade(BlockDriverState * bs,int target_version,BlockDriverAmendStatusCB * status_cb,void * cb_opaque,Error ** errp)5450 qcow2_downgrade(BlockDriverState *bs, int target_version,
5451                 BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
5452                 Error **errp)
5453 {
5454     BDRVQcow2State *s = bs->opaque;
5455     int current_version = s->qcow_version;
5456     int ret;
5457     int i;
5458 
5459     /* This is qcow2_downgrade(), not qcow2_upgrade() */
5460     assert(target_version < current_version);
5461 
5462     /* There are no other versions (now) that you can downgrade to */
5463     assert(target_version == 2);
5464 
5465     if (s->refcount_order != 4) {
5466         error_setg(errp, "compat=0.10 requires refcount_bits=16");
5467         return -ENOTSUP;
5468     }
5469 
5470     if (has_data_file(bs)) {
5471         error_setg(errp, "Cannot downgrade an image with a data file");
5472         return -ENOTSUP;
5473     }
5474 
5475     /*
5476      * If any internal snapshot has a different size than the current
5477      * image size, or VM state size that exceeds 32 bits, downgrading
5478      * is unsafe.  Even though we would still use v3-compliant output
5479      * to preserve that data, other v2 programs might not realize
5480      * those optional fields are important.
5481      */
5482     for (i = 0; i < s->nb_snapshots; i++) {
5483         if (s->snapshots[i].vm_state_size > UINT32_MAX ||
5484             s->snapshots[i].disk_size != bs->total_sectors * BDRV_SECTOR_SIZE) {
5485             error_setg(errp, "Internal snapshots prevent downgrade of image");
5486             return -ENOTSUP;
5487         }
5488     }
5489 
5490     /* clear incompatible features */
5491     if (s->incompatible_features & QCOW2_INCOMPAT_DIRTY) {
5492         ret = qcow2_mark_clean(bs);
5493         if (ret < 0) {
5494             error_setg_errno(errp, -ret, "Failed to make the image clean");
5495             return ret;
5496         }
5497     }
5498 
5499     /* with QCOW2_INCOMPAT_CORRUPT, it is pretty much impossible to get here in
5500      * the first place; if that happens nonetheless, returning -ENOTSUP is the
5501      * best thing to do anyway */
5502 
5503     if (s->incompatible_features & ~QCOW2_INCOMPAT_COMPRESSION) {
5504         error_setg(errp, "Cannot downgrade an image with incompatible features "
5505                    "0x%" PRIx64 " set",
5506                    s->incompatible_features & ~QCOW2_INCOMPAT_COMPRESSION);
5507         return -ENOTSUP;
5508     }
5509 
5510     /* since we can ignore compatible features, we can set them to 0 as well */
5511     s->compatible_features = 0;
5512     /* if lazy refcounts have been used, they have already been fixed through
5513      * clearing the dirty flag */
5514 
5515     /* clearing autoclear features is trivial */
5516     s->autoclear_features = 0;
5517 
5518     ret = qcow2_expand_zero_clusters(bs, status_cb, cb_opaque);
5519     if (ret < 0) {
5520         error_setg_errno(errp, -ret, "Failed to turn zero into data clusters");
5521         return ret;
5522     }
5523 
5524     if (s->incompatible_features & QCOW2_INCOMPAT_COMPRESSION) {
5525         ret = qcow2_has_compressed_clusters(bs);
5526         if (ret < 0) {
5527             error_setg(errp, "Failed to check block status");
5528             return -EINVAL;
5529         }
5530         if (ret) {
5531             error_setg(errp, "Cannot downgrade an image with zstd compression "
5532                        "type and existing compressed clusters");
5533             return -ENOTSUP;
5534         }
5535         /*
5536          * No compressed clusters for now, so just chose default zlib
5537          * compression.
5538          */
5539         s->incompatible_features &= ~QCOW2_INCOMPAT_COMPRESSION;
5540         s->compression_type = QCOW2_COMPRESSION_TYPE_ZLIB;
5541     }
5542 
5543     assert(s->incompatible_features == 0);
5544 
5545     s->qcow_version = target_version;
5546     ret = qcow2_update_header(bs);
5547     if (ret < 0) {
5548         s->qcow_version = current_version;
5549         error_setg_errno(errp, -ret, "Failed to update the image header");
5550         return ret;
5551     }
5552     return 0;
5553 }
5554 
5555 /*
5556  * Upgrades an image's version.  While newer versions encompass all
5557  * features of older versions, some things may have to be presented
5558  * differently.
5559  */
5560 static int GRAPH_RDLOCK
qcow2_upgrade(BlockDriverState * bs,int target_version,BlockDriverAmendStatusCB * status_cb,void * cb_opaque,Error ** errp)5561 qcow2_upgrade(BlockDriverState *bs, int target_version,
5562               BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
5563               Error **errp)
5564 {
5565     BDRVQcow2State *s = bs->opaque;
5566     bool need_snapshot_update;
5567     int current_version = s->qcow_version;
5568     int i;
5569     int ret;
5570 
5571     /* This is qcow2_upgrade(), not qcow2_downgrade() */
5572     assert(target_version > current_version);
5573 
5574     /* There are no other versions (yet) that you can upgrade to */
5575     assert(target_version == 3);
5576 
5577     status_cb(bs, 0, 2, cb_opaque);
5578 
5579     /*
5580      * In v2, snapshots do not need to have extra data.  v3 requires
5581      * the 64-bit VM state size and the virtual disk size to be
5582      * present.
5583      * qcow2_write_snapshots() will always write the list in the
5584      * v3-compliant format.
5585      */
5586     need_snapshot_update = false;
5587     for (i = 0; i < s->nb_snapshots; i++) {
5588         if (s->snapshots[i].extra_data_size <
5589             sizeof_field(QCowSnapshotExtraData, vm_state_size_large) +
5590             sizeof_field(QCowSnapshotExtraData, disk_size))
5591         {
5592             need_snapshot_update = true;
5593             break;
5594         }
5595     }
5596     if (need_snapshot_update) {
5597         ret = qcow2_write_snapshots(bs);
5598         if (ret < 0) {
5599             error_setg_errno(errp, -ret, "Failed to update the snapshot table");
5600             return ret;
5601         }
5602     }
5603     status_cb(bs, 1, 2, cb_opaque);
5604 
5605     s->qcow_version = target_version;
5606     ret = qcow2_update_header(bs);
5607     if (ret < 0) {
5608         s->qcow_version = current_version;
5609         error_setg_errno(errp, -ret, "Failed to update the image header");
5610         return ret;
5611     }
5612     status_cb(bs, 2, 2, cb_opaque);
5613 
5614     return 0;
5615 }
5616 
5617 typedef enum Qcow2AmendOperation {
5618     /* This is the value Qcow2AmendHelperCBInfo::last_operation will be
5619      * statically initialized to so that the helper CB can discern the first
5620      * invocation from an operation change */
5621     QCOW2_NO_OPERATION = 0,
5622 
5623     QCOW2_UPGRADING,
5624     QCOW2_UPDATING_ENCRYPTION,
5625     QCOW2_CHANGING_REFCOUNT_ORDER,
5626     QCOW2_DOWNGRADING,
5627 } Qcow2AmendOperation;
5628 
5629 typedef struct Qcow2AmendHelperCBInfo {
5630     /* The code coordinating the amend operations should only modify
5631      * these four fields; the rest will be managed by the CB */
5632     BlockDriverAmendStatusCB *original_status_cb;
5633     void *original_cb_opaque;
5634 
5635     Qcow2AmendOperation current_operation;
5636 
5637     /* Total number of operations to perform (only set once) */
5638     int total_operations;
5639 
5640     /* The following fields are managed by the CB */
5641 
5642     /* Number of operations completed */
5643     int operations_completed;
5644 
5645     /* Cumulative offset of all completed operations */
5646     int64_t offset_completed;
5647 
5648     Qcow2AmendOperation last_operation;
5649     int64_t last_work_size;
5650 } Qcow2AmendHelperCBInfo;
5651 
qcow2_amend_helper_cb(BlockDriverState * bs,int64_t operation_offset,int64_t operation_work_size,void * opaque)5652 static void qcow2_amend_helper_cb(BlockDriverState *bs,
5653                                   int64_t operation_offset,
5654                                   int64_t operation_work_size, void *opaque)
5655 {
5656     Qcow2AmendHelperCBInfo *info = opaque;
5657     int64_t current_work_size;
5658     int64_t projected_work_size;
5659 
5660     if (info->current_operation != info->last_operation) {
5661         if (info->last_operation != QCOW2_NO_OPERATION) {
5662             info->offset_completed += info->last_work_size;
5663             info->operations_completed++;
5664         }
5665 
5666         info->last_operation = info->current_operation;
5667     }
5668 
5669     assert(info->total_operations > 0);
5670     assert(info->operations_completed < info->total_operations);
5671 
5672     info->last_work_size = operation_work_size;
5673 
5674     current_work_size = info->offset_completed + operation_work_size;
5675 
5676     /* current_work_size is the total work size for (operations_completed + 1)
5677      * operations (which includes this one), so multiply it by the number of
5678      * operations not covered and divide it by the number of operations
5679      * covered to get a projection for the operations not covered */
5680     projected_work_size = current_work_size * (info->total_operations -
5681                                                info->operations_completed - 1)
5682                                             / (info->operations_completed + 1);
5683 
5684     info->original_status_cb(bs, info->offset_completed + operation_offset,
5685                              current_work_size + projected_work_size,
5686                              info->original_cb_opaque);
5687 }
5688 
5689 static int GRAPH_RDLOCK
qcow2_amend_options(BlockDriverState * bs,QemuOpts * opts,BlockDriverAmendStatusCB * status_cb,void * cb_opaque,bool force,Error ** errp)5690 qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,
5691                     BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
5692                     bool force, Error **errp)
5693 {
5694     BDRVQcow2State *s = bs->opaque;
5695     int old_version = s->qcow_version, new_version = old_version;
5696     uint64_t new_size = 0;
5697     const char *backing_file = NULL, *backing_format = NULL, *data_file = NULL;
5698     bool lazy_refcounts = s->use_lazy_refcounts;
5699     bool data_file_raw = data_file_is_raw(bs);
5700     const char *compat = NULL;
5701     int refcount_bits = s->refcount_bits;
5702     int ret;
5703     QemuOptDesc *desc = opts->list->desc;
5704     Qcow2AmendHelperCBInfo helper_cb_info;
5705     bool encryption_update = false;
5706 
5707     while (desc && desc->name) {
5708         if (!qemu_opt_find(opts, desc->name)) {
5709             /* only change explicitly defined options */
5710             desc++;
5711             continue;
5712         }
5713 
5714         if (!strcmp(desc->name, BLOCK_OPT_COMPAT_LEVEL)) {
5715             compat = qemu_opt_get(opts, BLOCK_OPT_COMPAT_LEVEL);
5716             if (!compat) {
5717                 /* preserve default */
5718             } else if (!strcmp(compat, "0.10") || !strcmp(compat, "v2")) {
5719                 new_version = 2;
5720             } else if (!strcmp(compat, "1.1") || !strcmp(compat, "v3")) {
5721                 new_version = 3;
5722             } else {
5723                 error_setg(errp, "Unknown compatibility level %s", compat);
5724                 return -EINVAL;
5725             }
5726         } else if (!strcmp(desc->name, BLOCK_OPT_SIZE)) {
5727             new_size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0);
5728         } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FILE)) {
5729             backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
5730         } else if (!strcmp(desc->name, BLOCK_OPT_BACKING_FMT)) {
5731             backing_format = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
5732         } else if (g_str_has_prefix(desc->name, "encrypt.")) {
5733             if (!s->crypto) {
5734                 error_setg(errp,
5735                            "Can't amend encryption options - encryption not present");
5736                 return -EINVAL;
5737             }
5738             if (s->crypt_method_header != QCOW_CRYPT_LUKS) {
5739                 error_setg(errp,
5740                            "Only LUKS encryption options can be amended");
5741                 return -ENOTSUP;
5742             }
5743             encryption_update = true;
5744         } else if (!strcmp(desc->name, BLOCK_OPT_LAZY_REFCOUNTS)) {
5745             lazy_refcounts = qemu_opt_get_bool(opts, BLOCK_OPT_LAZY_REFCOUNTS,
5746                                                lazy_refcounts);
5747         } else if (!strcmp(desc->name, BLOCK_OPT_REFCOUNT_BITS)) {
5748             refcount_bits = qemu_opt_get_number(opts, BLOCK_OPT_REFCOUNT_BITS,
5749                                                 refcount_bits);
5750 
5751             if (refcount_bits <= 0 || refcount_bits > 64 ||
5752                 !is_power_of_2(refcount_bits))
5753             {
5754                 error_setg(errp, "Refcount width must be a power of two and "
5755                            "may not exceed 64 bits");
5756                 return -EINVAL;
5757             }
5758         } else if (!strcmp(desc->name, BLOCK_OPT_DATA_FILE)) {
5759             data_file = qemu_opt_get(opts, BLOCK_OPT_DATA_FILE);
5760             if (data_file && !has_data_file(bs)) {
5761                 error_setg(errp, "data-file can only be set for images that "
5762                                  "use an external data file");
5763                 return -EINVAL;
5764             }
5765         } else if (!strcmp(desc->name, BLOCK_OPT_DATA_FILE_RAW)) {
5766             data_file_raw = qemu_opt_get_bool(opts, BLOCK_OPT_DATA_FILE_RAW,
5767                                               data_file_raw);
5768             if (data_file_raw && !data_file_is_raw(bs)) {
5769                 error_setg(errp, "data-file-raw cannot be set on existing "
5770                                  "images");
5771                 return -EINVAL;
5772             }
5773         } else {
5774             /* if this point is reached, this probably means a new option was
5775              * added without having it covered here */
5776             abort();
5777         }
5778 
5779         desc++;
5780     }
5781 
5782     helper_cb_info = (Qcow2AmendHelperCBInfo){
5783         .original_status_cb = status_cb,
5784         .original_cb_opaque = cb_opaque,
5785         .total_operations = (new_version != old_version)
5786                           + (s->refcount_bits != refcount_bits) +
5787                             (encryption_update == true)
5788     };
5789 
5790     /* Upgrade first (some features may require compat=1.1) */
5791     if (new_version > old_version) {
5792         helper_cb_info.current_operation = QCOW2_UPGRADING;
5793         ret = qcow2_upgrade(bs, new_version, &qcow2_amend_helper_cb,
5794                             &helper_cb_info, errp);
5795         if (ret < 0) {
5796             return ret;
5797         }
5798     }
5799 
5800     if (encryption_update) {
5801         QDict *amend_opts_dict;
5802         QCryptoBlockAmendOptions *amend_opts;
5803 
5804         helper_cb_info.current_operation = QCOW2_UPDATING_ENCRYPTION;
5805         amend_opts_dict = qcow2_extract_crypto_opts(opts, "luks", errp);
5806         if (!amend_opts_dict) {
5807             return -EINVAL;
5808         }
5809         amend_opts = block_crypto_amend_opts_init(amend_opts_dict, errp);
5810         qobject_unref(amend_opts_dict);
5811         if (!amend_opts) {
5812             return -EINVAL;
5813         }
5814         ret = qcrypto_block_amend_options(s->crypto,
5815                                           qcow2_crypto_hdr_read_func,
5816                                           qcow2_crypto_hdr_write_func,
5817                                           bs,
5818                                           amend_opts,
5819                                           force,
5820                                           errp);
5821         qapi_free_QCryptoBlockAmendOptions(amend_opts);
5822         if (ret < 0) {
5823             return ret;
5824         }
5825     }
5826 
5827     if (s->refcount_bits != refcount_bits) {
5828         int refcount_order = ctz32(refcount_bits);
5829 
5830         if (new_version < 3 && refcount_bits != 16) {
5831             error_setg(errp, "Refcount widths other than 16 bits require "
5832                        "compatibility level 1.1 or above (use compat=1.1 or "
5833                        "greater)");
5834             return -EINVAL;
5835         }
5836 
5837         helper_cb_info.current_operation = QCOW2_CHANGING_REFCOUNT_ORDER;
5838         ret = qcow2_change_refcount_order(bs, refcount_order,
5839                                           &qcow2_amend_helper_cb,
5840                                           &helper_cb_info, errp);
5841         if (ret < 0) {
5842             return ret;
5843         }
5844     }
5845 
5846     /* data-file-raw blocks backing files, so clear it first if requested */
5847     if (data_file_raw) {
5848         s->autoclear_features |= QCOW2_AUTOCLEAR_DATA_FILE_RAW;
5849     } else {
5850         s->autoclear_features &= ~QCOW2_AUTOCLEAR_DATA_FILE_RAW;
5851     }
5852 
5853     if (data_file) {
5854         g_free(s->image_data_file);
5855         s->image_data_file = *data_file ? g_strdup(data_file) : NULL;
5856     }
5857 
5858     ret = qcow2_update_header(bs);
5859     if (ret < 0) {
5860         error_setg_errno(errp, -ret, "Failed to update the image header");
5861         return ret;
5862     }
5863 
5864     if (backing_file || backing_format) {
5865         if (g_strcmp0(backing_file, s->image_backing_file) ||
5866             g_strcmp0(backing_format, s->image_backing_format)) {
5867             error_setg(errp, "Cannot amend the backing file");
5868             error_append_hint(errp,
5869                               "You can use 'qemu-img rebase' instead.\n");
5870             return -EINVAL;
5871         }
5872     }
5873 
5874     if (s->use_lazy_refcounts != lazy_refcounts) {
5875         if (lazy_refcounts) {
5876             if (new_version < 3) {
5877                 error_setg(errp, "Lazy refcounts only supported with "
5878                            "compatibility level 1.1 and above (use compat=1.1 "
5879                            "or greater)");
5880                 return -EINVAL;
5881             }
5882             s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
5883             ret = qcow2_update_header(bs);
5884             if (ret < 0) {
5885                 s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
5886                 error_setg_errno(errp, -ret, "Failed to update the image header");
5887                 return ret;
5888             }
5889             s->use_lazy_refcounts = true;
5890         } else {
5891             /* make image clean first */
5892             ret = qcow2_mark_clean(bs);
5893             if (ret < 0) {
5894                 error_setg_errno(errp, -ret, "Failed to make the image clean");
5895                 return ret;
5896             }
5897             /* now disallow lazy refcounts */
5898             s->compatible_features &= ~QCOW2_COMPAT_LAZY_REFCOUNTS;
5899             ret = qcow2_update_header(bs);
5900             if (ret < 0) {
5901                 s->compatible_features |= QCOW2_COMPAT_LAZY_REFCOUNTS;
5902                 error_setg_errno(errp, -ret, "Failed to update the image header");
5903                 return ret;
5904             }
5905             s->use_lazy_refcounts = false;
5906         }
5907     }
5908 
5909     if (new_size) {
5910         BlockBackend *blk = blk_new_with_bs(bs, BLK_PERM_RESIZE, BLK_PERM_ALL,
5911                                             errp);
5912         if (!blk) {
5913             return -EPERM;
5914         }
5915 
5916         /*
5917          * Amending image options should ensure that the image has
5918          * exactly the given new values, so pass exact=true here.
5919          */
5920         ret = blk_truncate(blk, new_size, true, PREALLOC_MODE_OFF, 0, errp);
5921         blk_unref(blk);
5922         if (ret < 0) {
5923             return ret;
5924         }
5925     }
5926 
5927     /* Downgrade last (so unsupported features can be removed before) */
5928     if (new_version < old_version) {
5929         helper_cb_info.current_operation = QCOW2_DOWNGRADING;
5930         ret = qcow2_downgrade(bs, new_version, &qcow2_amend_helper_cb,
5931                               &helper_cb_info, errp);
5932         if (ret < 0) {
5933             return ret;
5934         }
5935     }
5936 
5937     return 0;
5938 }
5939 
qcow2_co_amend(BlockDriverState * bs,BlockdevAmendOptions * opts,bool force,Error ** errp)5940 static int coroutine_fn qcow2_co_amend(BlockDriverState *bs,
5941                                        BlockdevAmendOptions *opts,
5942                                        bool force,
5943                                        Error **errp)
5944 {
5945     BlockdevAmendOptionsQcow2 *qopts = &opts->u.qcow2;
5946     BDRVQcow2State *s = bs->opaque;
5947     int ret = 0;
5948 
5949     if (qopts->encrypt) {
5950         if (!s->crypto) {
5951             error_setg(errp, "image is not encrypted, can't amend");
5952             return -EOPNOTSUPP;
5953         }
5954 
5955         if (qopts->encrypt->format != QCRYPTO_BLOCK_FORMAT_LUKS) {
5956             error_setg(errp,
5957                        "Amend can't be used to change the qcow2 encryption format");
5958             return -EOPNOTSUPP;
5959         }
5960 
5961         if (s->crypt_method_header != QCOW_CRYPT_LUKS) {
5962             error_setg(errp,
5963                        "Only LUKS encryption options can be amended for qcow2 with blockdev-amend");
5964             return -EOPNOTSUPP;
5965         }
5966 
5967         ret = qcrypto_block_amend_options(s->crypto,
5968                                           qcow2_crypto_hdr_read_func,
5969                                           qcow2_crypto_hdr_write_func,
5970                                           bs,
5971                                           qopts->encrypt,
5972                                           force,
5973                                           errp);
5974     }
5975     return ret;
5976 }
5977 
5978 /*
5979  * If offset or size are negative, respectively, they will not be included in
5980  * the BLOCK_IMAGE_CORRUPTED event emitted.
5981  * fatal will be ignored for read-only BDS; corruptions found there will always
5982  * be considered non-fatal.
5983  */
qcow2_signal_corruption(BlockDriverState * bs,bool fatal,int64_t offset,int64_t size,const char * message_format,...)5984 void qcow2_signal_corruption(BlockDriverState *bs, bool fatal, int64_t offset,
5985                              int64_t size, const char *message_format, ...)
5986 {
5987     BDRVQcow2State *s = bs->opaque;
5988     const char *node_name;
5989     char *message;
5990     va_list ap;
5991 
5992     fatal = fatal && bdrv_is_writable(bs);
5993 
5994     if (s->signaled_corruption &&
5995         (!fatal || (s->incompatible_features & QCOW2_INCOMPAT_CORRUPT)))
5996     {
5997         return;
5998     }
5999 
6000     va_start(ap, message_format);
6001     message = g_strdup_vprintf(message_format, ap);
6002     va_end(ap);
6003 
6004     if (fatal) {
6005         fprintf(stderr, "qcow2: Marking image as corrupt: %s; further "
6006                 "corruption events will be suppressed\n", message);
6007     } else {
6008         fprintf(stderr, "qcow2: Image is corrupt: %s; further non-fatal "
6009                 "corruption events will be suppressed\n", message);
6010     }
6011 
6012     node_name = bdrv_get_node_name(bs);
6013     qapi_event_send_block_image_corrupted(bdrv_get_device_name(bs),
6014                                           *node_name ? node_name : NULL,
6015                                           message, offset >= 0, offset,
6016                                           size >= 0, size,
6017                                           fatal);
6018     g_free(message);
6019 
6020     if (fatal) {
6021         qcow2_mark_corrupt(bs);
6022         bs->drv = NULL; /* make BDS unusable */
6023     }
6024 
6025     s->signaled_corruption = true;
6026 }
6027 
6028 #define QCOW_COMMON_OPTIONS                                         \
6029     {                                                               \
6030         .name = BLOCK_OPT_SIZE,                                     \
6031         .type = QEMU_OPT_SIZE,                                      \
6032         .help = "Virtual disk size"                                 \
6033     },                                                              \
6034     {                                                               \
6035         .name = BLOCK_OPT_COMPAT_LEVEL,                             \
6036         .type = QEMU_OPT_STRING,                                    \
6037         .help = "Compatibility level (v2 [0.10] or v3 [1.1])"       \
6038     },                                                              \
6039     {                                                               \
6040         .name = BLOCK_OPT_BACKING_FILE,                             \
6041         .type = QEMU_OPT_STRING,                                    \
6042         .help = "File name of a base image"                         \
6043     },                                                              \
6044     {                                                               \
6045         .name = BLOCK_OPT_BACKING_FMT,                              \
6046         .type = QEMU_OPT_STRING,                                    \
6047         .help = "Image format of the base image"                    \
6048     },                                                              \
6049     {                                                               \
6050         .name = BLOCK_OPT_DATA_FILE,                                \
6051         .type = QEMU_OPT_STRING,                                    \
6052         .help = "File name of an external data file"                \
6053     },                                                              \
6054     {                                                               \
6055         .name = BLOCK_OPT_DATA_FILE_RAW,                            \
6056         .type = QEMU_OPT_BOOL,                                      \
6057         .help = "The external data file must stay valid "           \
6058                 "as a raw image"                                    \
6059     },                                                              \
6060     {                                                               \
6061         .name = BLOCK_OPT_LAZY_REFCOUNTS,                           \
6062         .type = QEMU_OPT_BOOL,                                      \
6063         .help = "Postpone refcount updates",                        \
6064         .def_value_str = "off"                                      \
6065     },                                                              \
6066     {                                                               \
6067         .name = BLOCK_OPT_REFCOUNT_BITS,                            \
6068         .type = QEMU_OPT_NUMBER,                                    \
6069         .help = "Width of a reference count entry in bits",         \
6070         .def_value_str = "16"                                       \
6071     }
6072 
6073 static QemuOptsList qcow2_create_opts = {
6074     .name = "qcow2-create-opts",
6075     .head = QTAILQ_HEAD_INITIALIZER(qcow2_create_opts.head),
6076     .desc = {
6077         {                                                               \
6078             .name = BLOCK_OPT_ENCRYPT,                                  \
6079             .type = QEMU_OPT_BOOL,                                      \
6080             .help = "Encrypt the image with format 'aes'. (Deprecated " \
6081                     "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)",    \
6082         },                                                              \
6083         {                                                               \
6084             .name = BLOCK_OPT_ENCRYPT_FORMAT,                           \
6085             .type = QEMU_OPT_STRING,                                    \
6086             .help = "Encrypt the image, format choices: 'aes', 'luks'", \
6087         },                                                              \
6088         BLOCK_CRYPTO_OPT_DEF_KEY_SECRET("encrypt.",                     \
6089             "ID of secret providing qcow AES key or LUKS passphrase"),  \
6090         BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG("encrypt."),               \
6091         BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE("encrypt."),              \
6092         BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG("encrypt."),                \
6093         BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG("encrypt."),           \
6094         BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG("encrypt."),                 \
6095         BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),                \
6096         {                                                               \
6097             .name = BLOCK_OPT_CLUSTER_SIZE,                             \
6098             .type = QEMU_OPT_SIZE,                                      \
6099             .help = "qcow2 cluster size",                               \
6100             .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)            \
6101         },                                                              \
6102         {                                                               \
6103             .name = BLOCK_OPT_EXTL2,                                    \
6104             .type = QEMU_OPT_BOOL,                                      \
6105             .help = "Extended L2 tables",                               \
6106             .def_value_str = "off"                                      \
6107         },                                                              \
6108         {                                                               \
6109             .name = BLOCK_OPT_PREALLOC,                                 \
6110             .type = QEMU_OPT_STRING,                                    \
6111             .help = "Preallocation mode (allowed values: off, "         \
6112                     "metadata, falloc, full)"                           \
6113         },                                                              \
6114         {                                                               \
6115             .name = BLOCK_OPT_COMPRESSION_TYPE,                         \
6116             .type = QEMU_OPT_STRING,                                    \
6117             .help = "Compression method used for image cluster "        \
6118                     "compression",                                      \
6119             .def_value_str = "zlib"                                     \
6120         },
6121         QCOW_COMMON_OPTIONS,
6122         { /* end of list */ }
6123     }
6124 };
6125 
6126 static QemuOptsList qcow2_amend_opts = {
6127     .name = "qcow2-amend-opts",
6128     .head = QTAILQ_HEAD_INITIALIZER(qcow2_amend_opts.head),
6129     .desc = {
6130         BLOCK_CRYPTO_OPT_DEF_LUKS_STATE("encrypt."),
6131         BLOCK_CRYPTO_OPT_DEF_LUKS_KEYSLOT("encrypt."),
6132         BLOCK_CRYPTO_OPT_DEF_LUKS_OLD_SECRET("encrypt."),
6133         BLOCK_CRYPTO_OPT_DEF_LUKS_NEW_SECRET("encrypt."),
6134         BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME("encrypt."),
6135         QCOW_COMMON_OPTIONS,
6136         { /* end of list */ }
6137     }
6138 };
6139 
6140 static const char *const qcow2_strong_runtime_opts[] = {
6141     "encrypt." BLOCK_CRYPTO_OPT_QCOW_KEY_SECRET,
6142 
6143     NULL
6144 };
6145 
6146 BlockDriver bdrv_qcow2 = {
6147     .format_name                        = "qcow2",
6148     .instance_size                      = sizeof(BDRVQcow2State),
6149     .bdrv_probe                         = qcow2_probe,
6150     .bdrv_open                          = qcow2_open,
6151     .bdrv_close                         = qcow2_close,
6152     .bdrv_reopen_prepare                = qcow2_reopen_prepare,
6153     .bdrv_reopen_commit                 = qcow2_reopen_commit,
6154     .bdrv_reopen_commit_post            = qcow2_reopen_commit_post,
6155     .bdrv_reopen_abort                  = qcow2_reopen_abort,
6156     .bdrv_join_options                  = qcow2_join_options,
6157     .bdrv_child_perm                    = bdrv_default_perms,
6158     .bdrv_co_create_opts                = qcow2_co_create_opts,
6159     .bdrv_co_create                     = qcow2_co_create,
6160     .bdrv_has_zero_init                 = qcow2_has_zero_init,
6161     .bdrv_co_block_status               = qcow2_co_block_status,
6162 
6163     .bdrv_co_preadv_part                = qcow2_co_preadv_part,
6164     .bdrv_co_pwritev_part               = qcow2_co_pwritev_part,
6165     .bdrv_co_flush_to_os                = qcow2_co_flush_to_os,
6166 
6167     .bdrv_co_pwrite_zeroes              = qcow2_co_pwrite_zeroes,
6168     .bdrv_co_pdiscard                   = qcow2_co_pdiscard,
6169     .bdrv_co_copy_range_from            = qcow2_co_copy_range_from,
6170     .bdrv_co_copy_range_to              = qcow2_co_copy_range_to,
6171     .bdrv_co_truncate                   = qcow2_co_truncate,
6172     .bdrv_co_pwritev_compressed_part    = qcow2_co_pwritev_compressed_part,
6173     .bdrv_make_empty                    = qcow2_make_empty,
6174 
6175     .bdrv_snapshot_create               = qcow2_snapshot_create,
6176     .bdrv_snapshot_goto                 = qcow2_snapshot_goto,
6177     .bdrv_snapshot_delete               = qcow2_snapshot_delete,
6178     .bdrv_snapshot_list                 = qcow2_snapshot_list,
6179     .bdrv_snapshot_load_tmp             = qcow2_snapshot_load_tmp,
6180     .bdrv_measure                       = qcow2_measure,
6181     .bdrv_co_get_info                   = qcow2_co_get_info,
6182     .bdrv_get_specific_info             = qcow2_get_specific_info,
6183 
6184     .bdrv_co_save_vmstate               = qcow2_co_save_vmstate,
6185     .bdrv_co_load_vmstate               = qcow2_co_load_vmstate,
6186 
6187     .is_format                          = true,
6188     .supports_backing                   = true,
6189     .bdrv_co_change_backing_file        = qcow2_co_change_backing_file,
6190 
6191     .bdrv_refresh_limits                = qcow2_refresh_limits,
6192     .bdrv_co_invalidate_cache           = qcow2_co_invalidate_cache,
6193     .bdrv_inactivate                    = qcow2_inactivate,
6194 
6195     .create_opts                        = &qcow2_create_opts,
6196     .amend_opts                         = &qcow2_amend_opts,
6197     .strong_runtime_opts                = qcow2_strong_runtime_opts,
6198     .mutable_opts                       = mutable_opts,
6199     .bdrv_co_check                      = qcow2_co_check,
6200     .bdrv_amend_options                 = qcow2_amend_options,
6201     .bdrv_co_amend                      = qcow2_co_amend,
6202 
6203     .bdrv_detach_aio_context            = qcow2_detach_aio_context,
6204     .bdrv_attach_aio_context            = qcow2_attach_aio_context,
6205 
6206     .bdrv_supports_persistent_dirty_bitmap =
6207             qcow2_supports_persistent_dirty_bitmap,
6208     .bdrv_co_can_store_new_dirty_bitmap = qcow2_co_can_store_new_dirty_bitmap,
6209     .bdrv_co_remove_persistent_dirty_bitmap =
6210             qcow2_co_remove_persistent_dirty_bitmap,
6211 };
6212 
bdrv_qcow2_init(void)6213 static void bdrv_qcow2_init(void)
6214 {
6215     bdrv_register(&bdrv_qcow2);
6216 }
6217 
6218 block_init(bdrv_qcow2_init);
6219